forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositoryStatus.cs
More file actions
192 lines (167 loc) · 6.74 KB
/
RepositoryStatus.cs
File metadata and controls
192 lines (167 loc) · 6.74 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
namespace LibGit2Sharp
{
/// <summary>
/// Holds the result of the determination of the state of the working directory.
/// <para>Only files that differ from the current index and/or commit will be considered.</para>
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class RepositoryStatus : IEnumerable<StatusEntry>
{
private readonly ICollection<StatusEntry> statusEntries;
private readonly List<string> added = new List<string>();
private readonly List<string> staged = new List<string>();
private readonly List<string> removed = new List<string>();
private readonly List<string> missing = new List<string>();
private readonly List<string> modified = new List<string>();
private readonly List<string> untracked = new List<string>();
private readonly List<string> ignored = new List<string>();
private readonly bool isDirty;
private readonly IDictionary<FileStatus, Action<RepositoryStatus, string>> dispatcher = Build();
private static IDictionary<FileStatus, Action<RepositoryStatus, string>> Build()
{
return new Dictionary<FileStatus, Action<RepositoryStatus, string>>
{
{ FileStatus.Untracked, (rs, s) => rs.untracked.Add(s) },
{ FileStatus.Modified, (rs, s) => rs.modified.Add(s) },
{ FileStatus.Missing, (rs, s) => rs.missing.Add(s) },
{ FileStatus.Added, (rs, s) => rs.added.Add(s) },
{ FileStatus.Staged, (rs, s) => rs.staged.Add(s) },
{ FileStatus.Removed, (rs, s) => rs.removed.Add(s) },
{ FileStatus.Ignored, (rs, s) => rs.ignored.Add(s) },
};
}
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected RepositoryStatus()
{ }
internal RepositoryStatus(Repository repo)
{
statusEntries = Proxy.git_status_foreach(repo.Handle, StateChanged);
isDirty = statusEntries.Any(entry => entry.State != FileStatus.Ignored);
}
private StatusEntry StateChanged(IntPtr filePathPtr, uint state)
{
var filePath = FilePathMarshaler.FromNative(filePathPtr);
var gitStatus = (FileStatus)state;
foreach (KeyValuePair<FileStatus, Action<RepositoryStatus, string>> kvp in dispatcher)
{
if (!gitStatus.HasFlag(kvp.Key))
{
continue;
}
kvp.Value(this, filePath.Native);
}
return new StatusEntry(filePath.Native, gitStatus);
}
/// <summary>
/// Gets the <see cref = "FileStatus" /> for the specified relative path.
/// </summary>
public virtual FileStatus this[string path]
{
get
{
Ensure.ArgumentNotNullOrEmptyString(path, "path");
var entries = statusEntries.Where(e => string.Equals(e.FilePath, path, StringComparison.Ordinal)).ToList();
Debug.Assert(!(entries.Count > 1));
if (entries.Count == 0)
{
return FileStatus.Nonexistent;
}
return entries.Single().State;
}
}
/// <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<StatusEntry> GetEnumerator()
{
return statusEntries.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();
}
/// <summary>
/// List of files added to the index, which are not in the current commit
/// </summary>
public virtual IEnumerable<string> Added
{
get { return added; }
}
/// <summary>
/// List of files added to the index, which are already in the current commit with different content
/// </summary>
public virtual IEnumerable<string> Staged
{
get { return staged; }
}
/// <summary>
/// List of files removed from the index but are existent in the current commit
/// </summary>
public virtual IEnumerable<string> Removed
{
get { return removed; }
}
/// <summary>
/// List of files existent in the index but are missing in the working directory
/// </summary>
public virtual IEnumerable<string> Missing
{
get { return missing; }
}
/// <summary>
/// List of files with unstaged modifications. A file may be modified and staged at the same time if it has been modified after adding.
/// </summary>
public virtual IEnumerable<string> Modified
{
get { return modified; }
}
/// <summary>
/// List of files existing in the working directory but are neither tracked in the index nor in the current commit.
/// </summary>
public virtual IEnumerable<string> Untracked
{
get { return untracked; }
}
/// <summary>
/// List of files existing in the working directory that are ignored.
/// </summary>
public virtual IEnumerable<string> Ignored
{
get { return ignored; }
}
/// <summary>
/// True if the index or the working directory has been altered since the last commit. False otherwise.
/// </summary>
public virtual bool IsDirty
{
get { return isDirty; }
}
private string DebuggerDisplay
{
get
{
return string.Format(
CultureInfo.InvariantCulture,
"+{0} ~{1} -{2} | +{3} ~{4} -{5} | i{6}",
Added.Count(), Staged.Count(), Removed.Count(),
Untracked.Count(), Modified.Count(), Missing.Count(),
Ignored.Count());
}
}
}
}