-
Notifications
You must be signed in to change notification settings - Fork 897
/
Copy pathRemove.cs
241 lines (211 loc) · 11.2 KB
/
Remove.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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
public static partial class Commands
{
/// <summary>
/// Removes a file from the staging area, and optionally removes it from the working directory as well.
/// <para>
/// If the file has already been deleted from the working directory, this method will only deal
/// with promoting the removal to the staging area.
/// </para>
/// <para>
/// The default behavior is to remove the file from the working directory as well.
/// </para>
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="path">The path of the file within the working directory.</param>
public static void Remove(IRepository repository, string path)
{
Remove(repository, path, true, null);
}
/// <summary>
/// Removes a file from the staging area, and optionally removes it from the working directory as well.
/// <para>
/// If the file has already been deleted from the working directory, this method will only deal
/// with promoting the removal to the staging area.
/// </para>
/// <para>
/// The default behavior is to remove the file from the working directory as well.
/// </para>
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="path">The path of the file within the working directory.</param>
/// <param name="removeFromWorkingDirectory">True to remove the file from the working directory, False otherwise.</param>
public static void Remove(IRepository repository, string path, bool removeFromWorkingDirectory)
{
Remove(repository, path, removeFromWorkingDirectory, null);
}
/// <summary>
/// Removes a file from the staging area, and optionally removes it from the working directory as well.
/// <para>
/// If the file has already been deleted from the working directory, this method will only deal
/// with promoting the removal to the staging area.
/// </para>
/// <para>
/// The default behavior is to remove the file from the working directory as well.
/// </para>
/// <para>
/// When not passing a <paramref name="explicitPathsOptions"/>, the passed path will be treated as
/// a pathspec. You can for example use it to pass the relative path to a folder inside the working directory,
/// so that all files beneath this folders, and the folder itself, will be removed.
/// </para>
/// </summary>
/// <param name="repository">The repository in which to operate</param>
/// <param name="path">The path of the file within the working directory.</param>
/// <param name="removeFromWorkingDirectory">True to remove the file from the working directory, False otherwise.</param>
/// <param name="explicitPathsOptions">
/// The passed <paramref name="path"/> will be treated as an explicit path.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
public static void Remove(IRepository repository, string path, bool removeFromWorkingDirectory, ExplicitPathsOptions explicitPathsOptions)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNull(path, nameof(path));
Remove(repository, new[] { path }, removeFromWorkingDirectory, explicitPathsOptions);
}
/// <summary>
/// Removes a collection of fileS from the staging, and optionally removes them from the working directory as well.
/// <para>
/// If a file has already been deleted from the working directory, this method will only deal
/// with promoting the removal to the staging area.
/// </para>
/// <para>
/// The default behavior is to remove the files from the working directory as well.
/// </para>
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="paths">The collection of paths of the files within the working directory.</param>
public static void Remove(IRepository repository, IEnumerable<string> paths)
{
Remove(repository, paths, true, null);
}
/// <summary>
/// Removes a collection of fileS from the staging, and optionally removes them from the working directory as well.
/// <para>
/// If a file has already been deleted from the working directory, this method will only deal
/// with promoting the removal to the staging area.
/// </para>
/// <para>
/// The default behavior is to remove the files from the working directory as well.
/// </para>
/// <para>
/// When not passing a <paramref name="explicitPathsOptions"/>, the passed paths will be treated as
/// a pathspec. You can for example use it to pass the relative paths to folders inside the working directory,
/// so that all files beneath these folders, and the folders themselves, will be removed.
/// </para>
/// </summary>
/// <param name="repository">The repository in which to operate</param>
/// <param name="paths">The collection of paths of the files within the working directory.</param>
/// <param name="removeFromWorkingDirectory">True to remove the files from the working directory, False otherwise.</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 Remove(IRepository repository, IEnumerable<string> paths, bool removeFromWorkingDirectory, ExplicitPathsOptions explicitPathsOptions)
{
Ensure.ArgumentNotNull(repository, nameof(repository));
Ensure.ArgumentNotNullOrEmptyEnumerable<string>(paths, nameof(paths));
var pathsToDelete = paths.Where(p => Directory.Exists(Path.Combine(repository.Info.WorkingDirectory, p))).ToList();
var notConflictedPaths = new List<string>();
var index = repository.Index;
foreach (var path in paths)
{
Ensure.ArgumentNotNullOrEmptyString(path, nameof(path));
var conflict = index.Conflicts[path];
if (conflict != null)
{
index.Remove(path);
pathsToDelete.Add(path);
}
else
{
notConflictedPaths.Add(path);
}
}
// Make sure status will see the changes from before this
index.Write();
if (notConflictedPaths.Count > 0)
{
pathsToDelete.AddRange(RemoveStagedItems(repository, notConflictedPaths, removeFromWorkingDirectory, explicitPathsOptions));
}
if (removeFromWorkingDirectory)
{
RemoveFilesAndFolders(repository, pathsToDelete);
}
index.Write();
}
private static void RemoveFilesAndFolders(IRepository repository, IEnumerable<string> pathsList)
{
string wd = repository.Info.WorkingDirectory;
foreach (string path in pathsList)
{
string fileName = Path.Combine(wd, path);
if (Directory.Exists(fileName))
{
Directory.Delete(fileName, true);
continue;
}
if (!File.Exists(fileName))
{
continue;
}
File.Delete(fileName);
}
}
private static IEnumerable<string> RemoveStagedItems(IRepository repository, IEnumerable<string> paths, bool removeFromWorkingDirectory = true, ExplicitPathsOptions explicitPathsOptions = null)
{
var removed = new List<string>();
using (var changes = repository.Diff.Compare<TreeChanges>(DiffModifiers.IncludeUnmodified | DiffModifiers.IncludeUntracked, paths, explicitPathsOptions))
{
var index = repository.Index;
foreach (var treeEntryChanges in changes)
{
var status = repository.RetrieveStatus(treeEntryChanges.Path);
switch (treeEntryChanges.Status)
{
case ChangeKind.Added:
case ChangeKind.Deleted:
removed.Add(treeEntryChanges.Path);
index.Remove(treeEntryChanges.Path);
break;
case ChangeKind.Unmodified:
if (removeFromWorkingDirectory && (
status.HasFlag(FileStatus.ModifiedInIndex) ||
status.HasFlag(FileStatus.NewInIndex)))
{
throw new RemoveFromIndexException("Unable to remove file '{0}', as it has changes staged in the index. You can call the Remove() method with removeFromWorkingDirectory=false if you want to remove it from the index only.",
treeEntryChanges.Path);
}
removed.Add(treeEntryChanges.Path);
index.Remove(treeEntryChanges.Path);
continue;
case ChangeKind.Modified:
if (status.HasFlag(FileStatus.ModifiedInWorkdir) && status.HasFlag(FileStatus.ModifiedInIndex))
{
throw new RemoveFromIndexException("Unable to remove file '{0}', as it has staged content different from both the working directory and the HEAD.",
treeEntryChanges.Path);
}
if (removeFromWorkingDirectory)
{
throw new RemoveFromIndexException("Unable to remove file '{0}', as it has local modifications. You can call the Remove() method with removeFromWorkingDirectory=false if you want to remove it from the index only.",
treeEntryChanges.Path);
}
removed.Add(treeEntryChanges.Path);
index.Remove(treeEntryChanges.Path);
continue;
default:
throw new RemoveFromIndexException("Unable to remove file '{0}'. Its current status is '{1}'.",
treeEntryChanges.Path,
treeEntryChanges.Status);
}
}
index.Write();
return removed;
}
}
}
}