|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Globalization; |
| 6 | +using LibGit2Sharp.Core; |
| 7 | +using LibGit2Sharp.Core.Handles; |
| 8 | + |
| 9 | +namespace LibGit2Sharp |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Holds summary information for a diff. |
| 13 | + /// <para>The individual patches for each file can be accessed through the indexer of this class.</para> |
| 14 | + /// </summary> |
| 15 | + [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| 16 | + public class PatchStats : IEnumerable<ContentChangeStats> |
| 17 | + { |
| 18 | + private readonly IDictionary<FilePath, ContentChangeStats> changes = new Dictionary<FilePath, ContentChangeStats>(); |
| 19 | + private readonly int totalLinesAdded; |
| 20 | + private readonly int totalLinesDeleted; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// For mocking. |
| 24 | + /// </summary> |
| 25 | + protected PatchStats() |
| 26 | + { } |
| 27 | + |
| 28 | + internal PatchStats(DiffSafeHandle diff) |
| 29 | + { |
| 30 | + int count = Proxy.git_diff_num_deltas(diff); |
| 31 | + for (int i = 0; i < count; i++) |
| 32 | + { |
| 33 | + using (var patch = Proxy.git_patch_from_diff(diff, i)) |
| 34 | + { |
| 35 | + var delta = Proxy.git_diff_get_delta(diff, i); |
| 36 | + var pathPtr = delta.NewFile.Path != IntPtr.Zero ? delta.NewFile.Path : delta.OldFile.Path; |
| 37 | + var newFilePath = LaxFilePathMarshaler.FromNative(pathPtr); |
| 38 | + |
| 39 | + var stats = Proxy.git_patch_line_stats(patch); |
| 40 | + int added = stats.Item1; |
| 41 | + int deleted = stats.Item2; |
| 42 | + changes.Add(newFilePath, new ContentChangeStats(added, deleted)); |
| 43 | + totalLinesAdded += added; |
| 44 | + totalLinesDeleted += deleted; |
| 45 | + } |
| 46 | + |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + #region IEnumerable<ContentChanges> Members |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Returns an enumerator that iterates through the collection. |
| 54 | + /// </summary> |
| 55 | + /// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns> |
| 56 | + public virtual IEnumerator<ContentChangeStats> GetEnumerator() |
| 57 | + { |
| 58 | + return changes.Values.GetEnumerator(); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Returns an enumerator that iterates through the collection. |
| 63 | + /// </summary> |
| 64 | + /// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns> |
| 65 | + IEnumerator IEnumerable.GetEnumerator() |
| 66 | + { |
| 67 | + return GetEnumerator(); |
| 68 | + } |
| 69 | + |
| 70 | + #endregion |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Gets the <see cref="ContentChangeStats"/> corresponding to the specified <paramref name="path"/>. |
| 74 | + /// </summary> |
| 75 | + /// <param name="path"></param> |
| 76 | + public virtual ContentChangeStats this[string path] |
| 77 | + { |
| 78 | + get { return this[(FilePath) path]; } |
| 79 | + } |
| 80 | + |
| 81 | + private ContentChangeStats this[FilePath path] |
| 82 | + { |
| 83 | + get |
| 84 | + { |
| 85 | + ContentChangeStats stats; |
| 86 | + if (changes.TryGetValue(path, out stats)) |
| 87 | + { |
| 88 | + return stats; |
| 89 | + } |
| 90 | + return null; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// The total number of lines added in this diff. |
| 96 | + /// </summary> |
| 97 | + public virtual int TotalLinesAdded |
| 98 | + { |
| 99 | + get { return totalLinesAdded; } |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// The total number of lines deleted in this diff. |
| 104 | + /// </summary> |
| 105 | + public virtual int TotalLinesDeleted |
| 106 | + { |
| 107 | + get { return totalLinesDeleted; } |
| 108 | + } |
| 109 | + |
| 110 | + private string DebuggerDisplay |
| 111 | + { |
| 112 | + get |
| 113 | + { |
| 114 | + return string.Format(CultureInfo.InvariantCulture, "+{0} -{1}", |
| 115 | + TotalLinesAdded, TotalLinesDeleted); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments