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

PatchStats.cs « LibGit2Sharp - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f22eecb1ae6ddc456c503ca0cf3a8ffc3f8a8154 (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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;

namespace LibGit2Sharp
{
    /// <summary>
    /// Holds summary information for a diff.
    /// <para>The individual patches for each file can be accessed through the indexer of this class.</para>
    /// </summary>
    [DebuggerDisplay("{DebuggerDisplay,nq}")]
    public class PatchStats : IEnumerable<ContentChangeStats>
    {
        private readonly IDictionary<FilePath, ContentChangeStats> changes = new Dictionary<FilePath, ContentChangeStats>();
        private readonly int totalLinesAdded;
        private readonly int totalLinesDeleted;

        /// <summary>
        /// For mocking.
        /// </summary>
        protected PatchStats()
        { }

        internal PatchStats(DiffSafeHandle diff)
        {
            int count = Proxy.git_diff_num_deltas(diff);
            for (int i = 0; i < count; i++)
            {
                using (var patch = Proxy.git_patch_from_diff(diff, i))
                {
                    var delta = Proxy.git_diff_get_delta(diff, i);
                    var pathPtr = delta.NewFile.Path != IntPtr.Zero ? delta.NewFile.Path : delta.OldFile.Path;
                    var newFilePath = LaxFilePathMarshaler.FromNative(pathPtr);

                    var stats = Proxy.git_patch_line_stats(patch);
                    int added = stats.Item1;
                    int deleted = stats.Item2;
                    changes.Add(newFilePath, new ContentChangeStats(added, deleted));
                    totalLinesAdded += added;
                    totalLinesDeleted += deleted;
                }

            }
        }

        #region IEnumerable<ContentChanges> Members

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
        public virtual IEnumerator<ContentChangeStats> GetEnumerator()
        {
            return changes.Values.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        /// <summary>
        /// Gets the <see cref="ContentChangeStats"/> corresponding to the specified <paramref name="path"/>.
        /// </summary>
        /// <param name="path"></param>
        public virtual ContentChangeStats this[string path]
        {
            get { return this[(FilePath) path]; }
        }

        private ContentChangeStats this[FilePath path]
        {
            get
            {
                ContentChangeStats stats;
                if (changes.TryGetValue(path, out stats))
                {
                    return stats;
                }
                return null;
            }
        }

        /// <summary>
        /// The total number of lines added in this diff.
        /// </summary>
        public virtual int TotalLinesAdded
        {
            get { return totalLinesAdded; }
        }

        /// <summary>
        /// The total number of lines deleted in this diff.
        /// </summary>
        public virtual int TotalLinesDeleted
        {
            get { return totalLinesDeleted; }
        }

        private string DebuggerDisplay
        {
            get
            {
                return string.Format(CultureInfo.InvariantCulture, "+{0} -{1}",
                    TotalLinesAdded, TotalLinesDeleted);
            }
        }
    }
}