-
Notifications
You must be signed in to change notification settings - Fork 897
/
Copy pathStage.cs
324 lines (277 loc) · 14.9 KB
/
Stage.cs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System;
using System.IO;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using LibGit2Sharp;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
public static partial class Commands
{
/// <summary>
/// Promotes to the staging area the latest modifications of a file in the working directory (addition, updation or removal).
///
/// If this path is ignored by configuration then it will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
/// </summary>
/// <param name="repository">The repository in which to act</param>
/// <param name="path">The path of the file within the working directory.</param>
public static void Stage(IRepository repository, string path)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNull(path, nameof(path));
Stage(repository, new[] { path }, null);
}
/// <summary>
/// Promotes to the staging area the latest modifications of a file in the working directory (addition, updation or removal).
///
/// If this path is ignored by configuration then it will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
/// </summary>
/// <param name="repository">The repository in which to act</param>
/// <param name="path">The path of the file within the working directory.</param>
/// <param name="stageOptions">Determines how paths will be staged.</param>
public static void Stage(IRepository repository, string path, StageOptions stageOptions)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNull(path, nameof(path));
Stage(repository, new[] { path }, stageOptions);
}
/// <summary>
/// Promotes to the staging area the latest modifications of a collection of files in the working directory (addition, updation or removal).
///
/// Any paths (even those listed explicitly) that are ignored by configuration will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
/// </summary>
/// <param name="repository">The repository in which to act</param>
/// <param name="paths">The collection of paths of the files within the working directory.</param>
public static void Stage(IRepository repository, IEnumerable<string> paths)
{
Stage(repository, paths, null);
}
/// <summary>
/// Promotes to the staging area the latest modifications of a collection of files in the working directory (addition, updation or removal).
///
/// Any paths (even those listed explicitly) that are ignored by configuration will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
/// </summary>
/// <param name="repository">The repository in which to act</param>
/// <param name="paths">The collection of paths of the files within the working directory.</param>
/// <param name="stageOptions">Determines how paths will be staged.</param>
public static void Stage(IRepository repository, IEnumerable<string> paths, StageOptions stageOptions)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNull(paths, nameof(paths));
DiffModifiers diffModifiers = DiffModifiers.IncludeUntracked;
ExplicitPathsOptions explicitPathsOptions = stageOptions != null ? stageOptions.ExplicitPathsOptions : null;
if (stageOptions != null && stageOptions.IncludeIgnored)
{
diffModifiers |= DiffModifiers.IncludeIgnored;
}
using (var changes = repository.Diff.Compare<TreeChanges>(diffModifiers, paths, explicitPathsOptions,
new CompareOptions { Similarity = SimilarityOptions.None }))
{
var unexpectedTypesOfChanges = changes
.Where(
tec => tec.Status != ChangeKind.Added &&
tec.Status != ChangeKind.Modified &&
tec.Status != ChangeKind.Conflicted &&
tec.Status != ChangeKind.Unmodified &&
tec.Status != ChangeKind.Deleted).ToList();
if (unexpectedTypesOfChanges.Count > 0)
{
throw new InvalidOperationException(
string.Format(CultureInfo.InvariantCulture,
"Entry '{0}' bears an unexpected ChangeKind '{1}'",
unexpectedTypesOfChanges[0].Path, unexpectedTypesOfChanges[0].Status));
}
/* Remove files from the index that don't exist on disk */
foreach (TreeEntryChanges treeEntryChanges in changes)
{
switch (treeEntryChanges.Status)
{
case ChangeKind.Conflicted:
if (!treeEntryChanges.Exists)
{
repository.Index.Remove(treeEntryChanges.Path);
}
break;
case ChangeKind.Deleted:
repository.Index.Remove(treeEntryChanges.Path);
break;
default:
continue;
}
}
foreach (TreeEntryChanges treeEntryChanges in changes)
{
switch (treeEntryChanges.Status)
{
case ChangeKind.Added:
case ChangeKind.Modified:
repository.Index.Add(treeEntryChanges.Path);
break;
case ChangeKind.Conflicted:
if (treeEntryChanges.Exists)
{
repository.Index.Add(treeEntryChanges.Path);
}
break;
default:
continue;
}
}
repository.Index.Write();
}
}
/// <summary>
/// Removes from the staging area all the modifications of a file since the latest commit (addition, updation or removal).
/// </summary>
/// <param name="repository">The repository in which to act</param>
/// <param name="path">The path of the file within the working directory.</param>
public static void Unstage(IRepository repository, string path)
{
Unstage(repository, path, null);
}
/// <summary>
/// Removes from the staging area all the modifications of a file since the latest commit (addition, updation or removal).
/// </summary>
/// <param name="repository">The repository in which to act</param>
/// <param name="path">The path of the file within the working directory.</param>
/// <param name="explicitPathsOptions">
/// The passed <paramref name="path"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
public static void Unstage(IRepository repository, string path, ExplicitPathsOptions explicitPathsOptions)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNull(path, nameof(path));
Unstage(repository, new[] { path }, explicitPathsOptions);
}
/// <summary>
/// Removes from the staging area all the modifications of a collection of file since the latest commit (addition, updation or removal).
/// </summary>
/// <param name="repository">The repository in which to act</param>
/// <param name="paths">The collection of paths of the files within the working directory.</param>
public static void Unstage(IRepository repository, IEnumerable<string> paths)
{
Unstage(repository, paths, null);
}
/// <summary>
/// Removes from the staging area all the modifications of a collection of file since the latest commit (addition, updation or removal).
/// </summary>
/// <param name="repository">The repository in which to act</param>
/// <param name="paths">The collection of paths of the files within the working directory.</param>
/// <param name="explicitPathsOptions">
/// The passed <paramref name="paths"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
public static void Unstage(IRepository repository, IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNull(paths, nameof(paths));
if (repository.Info.IsHeadUnborn)
{
using (var changes = repository.Diff.Compare<TreeChanges>(null, DiffTargets.Index, paths, explicitPathsOptions, new CompareOptions { Similarity = SimilarityOptions.None }))
repository.Index.Replace(changes);
}
else
{
repository.Index.Replace(repository.Head.Tip, paths, explicitPathsOptions);
}
repository.Index.Write();
}
/// <summary>
/// Moves and/or renames a file in the working directory and promotes the change to the staging area.
/// </summary>
/// <param name="repository">The repository to act on</param>
/// <param name="sourcePath">The path of the file within the working directory which has to be moved/renamed.</param>
/// <param name="destinationPath">The target path of the file within the working directory.</param>
public static void Move(IRepository repository, string sourcePath, string destinationPath)
{
Move(repository, new[] { sourcePath }, new[] { destinationPath });
}
/// <summary>
/// Moves and/or renames a collection of files in the working directory and promotes the changes to the staging area.
/// </summary>
/// <param name="repository">The repository to act on</param>
/// <param name="sourcePaths">The paths of the files within the working directory which have to be moved/renamed.</param>
/// <param name="destinationPaths">The target paths of the files within the working directory.</param>
public static void Move(IRepository repository, IEnumerable<string> sourcePaths, IEnumerable<string> destinationPaths)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNull(sourcePaths, nameof(sourcePaths));
Ensure.ArgumentNotNull(destinationPaths, nameof(destinationPaths));
//TODO: Move() should support following use cases:
// - Moving a file under a directory ('file' and 'dir' -> 'dir/file')
// - Moving a directory (and its content) under another directory ('dir1' and 'dir2' -> 'dir2/dir1/*')
//TODO: Move() should throw when:
// - Moving a directory under a file
IDictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>> batch = PrepareBatch(repository, sourcePaths, destinationPaths);
if (batch.Count == 0)
{
throw new ArgumentNullException(nameof(sourcePaths));
}
foreach (KeyValuePair<Tuple<string, FileStatus>, Tuple<string, FileStatus>> keyValuePair in batch)
{
string sourcePath = keyValuePair.Key.Item1;
string destPath = keyValuePair.Value.Item1;
if (Directory.Exists(sourcePath) || Directory.Exists(destPath))
{
throw new NotImplementedException();
}
FileStatus sourceStatus = keyValuePair.Key.Item2;
if (sourceStatus.HasAny(new Enum[] { FileStatus.Nonexistent, FileStatus.DeletedFromIndex, FileStatus.NewInWorkdir, FileStatus.DeletedFromWorkdir }))
{
throw new LibGit2SharpException("Unable to move file '{0}'. Its current status is '{1}'.",
sourcePath,
sourceStatus);
}
FileStatus desStatus = keyValuePair.Value.Item2;
if (desStatus.HasAny(new Enum[] { FileStatus.Nonexistent, FileStatus.DeletedFromWorkdir }))
{
continue;
}
throw new LibGit2SharpException("Unable to overwrite file '{0}'. Its current status is '{1}'.",
destPath,
desStatus);
}
string wd = repository.Info.WorkingDirectory;
var index = repository.Index;
foreach (KeyValuePair<Tuple<string, FileStatus>, Tuple<string, FileStatus>> keyValuePair in batch)
{
string from = keyValuePair.Key.Item1;
string to = keyValuePair.Value.Item1;
index.Remove(from);
File.Move(Path.Combine(wd, from), Path.Combine(wd, to));
index.Add(to);
}
index.Write();
}
private static bool Enumerate(IEnumerator<string> leftEnum, IEnumerator<string> rightEnum)
{
bool isLeftEoF = leftEnum.MoveNext();
bool isRightEoF = rightEnum.MoveNext();
if (isLeftEoF == isRightEoF)
{
return isLeftEoF;
}
throw new ArgumentException("The collection of paths are of different lengths.");
}
private static IDictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>> PrepareBatch(IRepository repository, IEnumerable<string> leftPaths, IEnumerable<string> rightPaths)
{
IDictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>> dic = new Dictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>>();
IEnumerator<string> leftEnum = leftPaths.GetEnumerator();
IEnumerator<string> rightEnum = rightPaths.GetEnumerator();
while (Enumerate(leftEnum, rightEnum))
{
Tuple<string, FileStatus> from = BuildFrom(repository, leftEnum.Current);
Tuple<string, FileStatus> to = BuildFrom(repository, rightEnum.Current);
dic.Add(from, to);
}
return dic;
}
private static Tuple<string, FileStatus> BuildFrom(IRepository repository, string path)
{
string relativePath = repository.BuildRelativePathFrom(path);
return new Tuple<string, FileStatus>(relativePath, repository.RetrieveStatus(relativePath));
}
}
}