Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BlameHunk.cs « LibGit2Sharp - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 79dbf39452953c7f1321cc0654351fe5eff98a20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Diagnostics;
using System.Globalization;
using LibGit2Sharp.Core;

namespace LibGit2Sharp
{
    /// <summary>
    /// A contiguous group of lines that have been traced to a single commit.
    /// </summary>
    [DebuggerDisplay("{DebuggerDisplay,nq}")]
    public class BlameHunk : IEquatable<BlameHunk>
    {
        private static readonly LambdaEqualityHelper<BlameHunk> equalityHelper =
            new LambdaEqualityHelper<BlameHunk>(x => x.LineCount,
                                                x => x.FinalStartLineNumber,
                                                x => x.FinalSignature,
                                                x => x.InitialStartLineNumber,
                                                x => x.InitialSignature,
                                                x => x.InitialCommit);


        internal BlameHunk(IRepository repository, GitBlameHunk rawHunk)
        {
            finalCommit = new Lazy<Commit>(() => repository.Lookup<Commit>(rawHunk.FinalCommitId));
            origCommit = new Lazy<Commit>(() => repository.Lookup<Commit>(rawHunk.OrigCommitId));

            if (rawHunk.OrigPath != IntPtr.Zero)
            {
                InitialPath = LaxUtf8Marshaler.FromNative(rawHunk.OrigPath);
            }
            LineCount = rawHunk.LinesInHunk;

            // Libgit2's line numbers are 1-based
            FinalStartLineNumber = rawHunk.FinalStartLineNumber - 1;
            InitialStartLineNumber = rawHunk.OrigStartLineNumber - 1;

            // Signature objects need to have ownership of their native pointers
            if (rawHunk.FinalSignature != IntPtr.Zero)
            {
                FinalSignature = new Signature(Proxy.git_signature_dup(rawHunk.FinalSignature));
            }
            if (rawHunk.OrigSignature != IntPtr.Zero)
            {
                InitialSignature = new Signature(Proxy.git_signature_dup(rawHunk.OrigSignature));
            }
        }

        /// <summary>
        /// For easier mocking
        /// </summary>
        protected BlameHunk() { }

        /// <summary>
        /// Determine if this hunk contains a given line.
        /// </summary>
        /// <param name="line">Line number to test</param>
        /// <returns>True if this hunk contains the given line.</returns>
        public virtual bool ContainsLine(int line)
        {
            return FinalStartLineNumber <= line && line < FinalStartLineNumber + LineCount;
        }

        /// <summary>
        /// Number of lines in this hunk.
        /// </summary>
        public virtual int LineCount { get; private set; }

        /// <summary>
        /// The line number where this hunk begins, as of <see cref="FinalCommit"/>
        /// </summary>
        public virtual int FinalStartLineNumber { get; private set; }

        /// <summary>
        /// Signature of the most recent change to this hunk.
        /// </summary>
        public virtual Signature FinalSignature { get; private set; }

        /// <summary>
        /// Commit which most recently changed this file.
        /// </summary>
        public virtual Commit FinalCommit { get { return finalCommit.Value; } }

        /// <summary>
        /// Line number where this hunk begins, as of <see cref="FinalCommit"/>, in <see cref="InitialPath"/>.
        /// </summary>
        public virtual int InitialStartLineNumber { get; private set; }

        /// <summary>
        /// Signature of the oldest-traced change to this hunk.
        /// </summary>
        public virtual Signature InitialSignature { get; private set; }

        /// <summary>
        /// Commit to which the oldest change to this hunk has been traced.
        /// </summary>
        public virtual Commit InitialCommit { get { return origCommit.Value; } }

        /// <summary>
        /// Path to the file where this hunk originated, as of <see cref="InitialCommit"/>.
        /// </summary>
        public virtual string InitialPath { get; private set; }

        private string DebuggerDisplay
        {
            get
            {
                return string.Format(CultureInfo.InvariantCulture,
                                     "{0}-{1} ({2})",
                                     FinalStartLineNumber,
                                     FinalStartLineNumber+LineCount-1,
                                     FinalCommit.ToString().Substring(0,7));
            }
        }

        private readonly Lazy<Commit> finalCommit;
        private readonly Lazy<Commit> origCommit;

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(BlameHunk other)
        {
            return equalityHelper.Equals(this, other);
        }

        /// <summary>
        /// Determines whether the specified <see cref="Object"/> is equal to the current <see cref="BlameHunk"/>.
        /// </summary>
        /// <param name="obj">The <see cref="Object"/> to compare with the current <see cref="BlameHunk"/>.</param>
        /// <returns>True if the specified <see cref="Object"/> is equal to the current <see cref="BlameHunk"/>; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            return Equals(obj as BlameHunk);
        }

        /// <summary>
        /// Returns the hash code for this instance.
        /// </summary>
        /// <returns>A 32-bit signed integer hash code.</returns>
        public override int GetHashCode()
        {
            return equalityHelper.GetHashCode();
        }

        /// <summary>
        /// Tests if two <see cref="BlameHunk"/>s are equal.
        /// </summary>
        /// <param name="left">First hunk to compare.</param>
        /// <param name="right">Second hunk to compare.</param>
        /// <returns>True if the two objects are equal; false otherwise.</returns>
        public static bool operator ==(BlameHunk left, BlameHunk right)
        {
            return Equals(left, right);
        }

        /// <summary>
        /// Tests if two <see cref="BlameHunk"/>s are unequal.
        /// </summary>
        /// <param name="left">First hunk to compare.</param>
        /// <param name="right">Second hunk to compare.</param>
        /// <returns>True if the two objects are different; false otherwise.</returns>
        public static bool operator !=(BlameHunk left, BlameHunk right)
        {
            return !Equals(left, right);
        }
    }
}