|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Globalization; |
| 4 | +using LibGit2Sharp.Core; |
| 5 | +using LibGit2Sharp.Core.Compat; |
| 6 | + |
| 7 | +namespace LibGit2Sharp |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// A contiguous group of lines that have been traced to a single commit. |
| 11 | + /// </summary> |
| 12 | + [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| 13 | + public class BlameHunk : IEquatable<BlameHunk> |
| 14 | + { |
| 15 | + private readonly IRepository repository; |
| 16 | + |
| 17 | + private static readonly LambdaEqualityHelper<BlameHunk> equalityHelper = |
| 18 | + new LambdaEqualityHelper<BlameHunk>(x => x.LineCount, |
| 19 | + x => x.FinalStartLineNumber, |
| 20 | + x => x.FinalSignature, |
| 21 | + x => x.InitialStartLineNumber, |
| 22 | + x => x.InitialSignature, |
| 23 | + x => x.InitialCommit); |
| 24 | + |
| 25 | + |
| 26 | + internal BlameHunk(IRepository repository, GitBlameHunk rawHunk) |
| 27 | + { |
| 28 | + this.repository = repository; |
| 29 | + |
| 30 | + finalCommit = new Lazy<Commit>(() => repository.Lookup<Commit>(rawHunk.FinalCommitId)); |
| 31 | + origCommit = new Lazy<Commit>(() => repository.Lookup<Commit>(rawHunk.OrigCommitId)); |
| 32 | + |
| 33 | + if (rawHunk.OrigPath != IntPtr.Zero) |
| 34 | + { |
| 35 | + InitialPath = LaxUtf8Marshaler.FromNative(rawHunk.OrigPath); |
| 36 | + } |
| 37 | + LineCount = rawHunk.LinesInHunk; |
| 38 | + |
| 39 | + // Libgit2's line numbers are 1-based |
| 40 | + FinalStartLineNumber = rawHunk.FinalStartLineNumber - 1; |
| 41 | + InitialStartLineNumber = rawHunk.OrigStartLineNumber - 1; |
| 42 | + |
| 43 | + // Signature objects need to have ownership of their native pointers |
| 44 | + if (rawHunk.FinalSignature != IntPtr.Zero) |
| 45 | + { |
| 46 | + FinalSignature = new Signature(NativeMethods.git_signature_dup(rawHunk.FinalSignature)); |
| 47 | + } |
| 48 | + if (rawHunk.OrigSignature != IntPtr.Zero) |
| 49 | + { |
| 50 | + InitialSignature = new Signature(NativeMethods.git_signature_dup(rawHunk.OrigSignature)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// For easier mocking |
| 56 | + /// </summary> |
| 57 | + protected BlameHunk() { } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Determine if this hunk contains a given line. |
| 61 | + /// </summary> |
| 62 | + /// <param name="line">Line number to test</param> |
| 63 | + /// <returns>True if this hunk contains the given line.</returns> |
| 64 | + public virtual bool ContainsLine(int line) |
| 65 | + { |
| 66 | + return FinalStartLineNumber <= line && line < FinalStartLineNumber + LineCount; |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Number of lines in this hunk. |
| 71 | + /// </summary> |
| 72 | + public virtual int LineCount { get; private set; } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// The line number where this hunk begins, as of <see cref="FinalCommit"/> |
| 76 | + /// </summary> |
| 77 | + public virtual int FinalStartLineNumber { get; private set; } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Signature of the most recent change to this hunk. |
| 81 | + /// </summary> |
| 82 | + public virtual Signature FinalSignature { get; private set; } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Commit which most recently changed this file. |
| 86 | + /// </summary> |
| 87 | + public virtual Commit FinalCommit { get { return finalCommit.Value; } } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Line number where this hunk begins, as of <see cref="FinalCommit"/>, in <see cref="InitialPath"/>. |
| 91 | + /// </summary> |
| 92 | + public virtual int InitialStartLineNumber { get; private set; } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Signature of the oldest-traced change to this hunk. |
| 96 | + /// </summary> |
| 97 | + public virtual Signature InitialSignature { get; private set; } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Commit to which the oldest change to this hunk has been traced. |
| 101 | + /// </summary> |
| 102 | + public virtual Commit InitialCommit { get { return origCommit.Value; } } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Path to the file where this hunk originated, as of <see cref="InitialCommit"/>. |
| 106 | + /// </summary> |
| 107 | + public virtual string InitialPath { get; private set; } |
| 108 | + |
| 109 | + private string DebuggerDisplay |
| 110 | + { |
| 111 | + get |
| 112 | + { |
| 113 | + return string.Format(CultureInfo.InvariantCulture, |
| 114 | + "{0}-{1} ({2})", |
| 115 | + FinalStartLineNumber, |
| 116 | + FinalStartLineNumber+LineCount, |
| 117 | + FinalCommit.ToString().Substring(0,7)); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private readonly Lazy<Commit> finalCommit; |
| 122 | + private readonly Lazy<Commit> origCommit; |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Indicates whether the current object is equal to another object of the same type. |
| 126 | + /// </summary> |
| 127 | + /// <returns> |
| 128 | + /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false. |
| 129 | + /// </returns> |
| 130 | + /// <param name="other">An object to compare with this object.</param> |
| 131 | + public bool Equals(BlameHunk other) |
| 132 | + { |
| 133 | + return equalityHelper.Equals(this, other); |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Determines whether the specified <see cref="Object"/> is equal to the current <see cref="BlameHunk"/>. |
| 138 | + /// </summary> |
| 139 | + /// <param name="obj">The <see cref="Object"/> to compare with the current <see cref="BlameHunk"/>.</param> |
| 140 | + /// <returns>True if the specified <see cref="Object"/> is equal to the current <see cref="BlameHunk"/>; otherwise, false.</returns> |
| 141 | + public override bool Equals(object obj) |
| 142 | + { |
| 143 | + return Equals(obj as BlameHunk); |
| 144 | + } |
| 145 | + |
| 146 | + /// <summary> |
| 147 | + /// Returns the hash code for this instance. |
| 148 | + /// </summary> |
| 149 | + /// <returns>A 32-bit signed integer hash code.</returns> |
| 150 | + public override int GetHashCode() |
| 151 | + { |
| 152 | + return equalityHelper.GetHashCode(); |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Tests if two <see cref="BlameHunk"/>s are equal. |
| 157 | + /// </summary> |
| 158 | + /// <param name="left">First hunk to compare.</param> |
| 159 | + /// <param name="right">Second hunk to compare.</param> |
| 160 | + /// <returns>True if the two objects are equal; false otherwise.</returns> |
| 161 | + public static bool operator ==(BlameHunk left, BlameHunk right) |
| 162 | + { |
| 163 | + return Equals(left, right); |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Tests if two <see cref="BlameHunk"/>s are unequal. |
| 168 | + /// </summary> |
| 169 | + /// <param name="left">First hunk to compare.</param> |
| 170 | + /// <param name="right">Second hunk to compare.</param> |
| 171 | + /// <returns>True if the two objects are different; false otherwise.</returns> |
| 172 | + public static bool operator !=(BlameHunk left, BlameHunk right) |
| 173 | + { |
| 174 | + return !Equals(left, right); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments