8000 Put Move in Commands · libgit2/libgit2sharp@78be980 · GitHub
[go: up one dir, main page]

Skip to content

Commit 78be980

Browse files
committed
Put Move in Commands
1 parent 2d4d66d commit 78be980

9 files changed

+148
-118
lines changed

LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public void DetectsTheExactRenamingOfFilesByDefault()
293293

294294
Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature);
295295

296-
repo.Move(originalPath, renamedPath);
296+
Commands.Move(repo, originalPath, renamedPath);
297297

298298
Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);
299299

@@ -331,7 +331,7 @@ public void RenameThresholdsAreObeyed()
331331
// 8 lines, 50% are from original file
332332
Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\ne\nf\ng\nh\n");
333333
Commands.Stage(repo, originalPath);
334-
repo.Move(originalPath, renamedPath);
334+
Commands.Move(repo, originalPath, renamedPath);
335335

336336
Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);
337337

@@ -369,7 +369,7 @@ public void ExactModeDetectsExactRenames()
369369

370370
Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature);
371371

372-
repo.Move(originalPath, renamedPath);
372+
Commands.Move(repo, originalPath, renamedPath);
373373

374374
Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);
375375

@@ -434,7 +434,7 @@ public void ExactModeDoesntDetectRenamesWithEdits()
434434

435435
Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature);
436436

437-
repo.Move(originalPath, renamedPath);
437+
Commands.Move(repo, originalPath, renamedPath);
438438
File.AppendAllText(Path.Combine(repo.Info.WorkingDirectory, renamedPath), "e\nf\n");
439439
Commands.Stage(repo, renamedPath);
440440

@@ -508,7 +508,7 @@ public void CanNotDetectTheExactRenamingFilesWhenNotEnabled()
508508

509509
Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature);
510510

511-
repo.Move(originalPath, renamedPath);
511+
Commands.Move(repo, originalPath, renamedPath);
512512

513513
Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);
514514

@@ -725,7 +725,7 @@ public void CanDetectTheExactRenamingExactCopyingOfNonModifiedAndModifiedFilesWh
725725
Commands.Stage(repo, originalPath3);
726726
Commands.Stage(repo, copiedPath1);
727727
Commands.Stage(repo, copiedPath2);
728-
repo.Move(originalPath, renamedPath);
728+
Commands.Move(repo, originalPath, renamedPath);
729729

730730
Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature);
731731

LibGit2Sharp.Tests/FileHistoryFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void CanTellComplexCommitHistory()
152152

153153
// Move the first file to a new directory.
154154
var newPath1 = Path.Combine(SubFolderPath1, path1);
155-
repo.Move(path1, newPath1);
155+
Commands.Move(repo, path1, newPath1);
156156
var commit3 = repo.Commit("Moved " + path1 + " to " + newPath1,
157157
Constants.Signature, Constants.Signature);
158158

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void CanRenameAFile()
120120

121121
const string newName = "being.frakking.polite.txt";
122122

123-
repo.Move(oldName, newName);
123+
Commands.Move(repo, oldName, newName);
124124
Assert.Equal(FileStatus.DeletedFromIndex, repo.RetrieveStatus(oldName));
125125
Assert.Equal(FileStatus.NewInIndex, repo.RetrieveStatus(newName));
126126

@@ -150,7 +150,7 @@ public void CanMoveAnExistingFileOverANonExistingFile(string sourcePath, FileSta
150150
Assert.Equal(sourceStatus, repo.RetrieveStatus(sourcePath));
151151
Assert.Equal(destStatus, repo.RetrieveStatus(destPath));
152152

153-
repo.Move(sourcePath, destPath);
153+
Commands.Move(repo, sourcePath, destPath);
154154

155155
Assert.Equal(sourcePostStatus, repo.RetrieveStatus(sourcePath));
156156
Assert.Equal(destPostStatus, repo.RetrieveStatus(destPath));
@@ -193,7 +193,7 @@ private void InvalidMoveUseCases(string sourcePath, FileStatus sourceStatus, IEn
193193
foreach (var destPath in destPaths)
194194
{
195195
string path = destPath;
196-
Assert.Throws<LibGit2SharpException>(() => repo.Move(sourcePath, path));
196+
Assert.Throws<LibGit2SharpException>(() => Commands.Move(repo, sourcePath, path));
197197
}
198198
}
199199
}

LibGit2Sharp.Tests/ResetIndexFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public void CanResetTheIndexWhenARenameExists()
116116
{
117117
using (var repo = new Repository(SandboxStandardTestRepo()))
118118
{
119-
repo.Move("branch_file.txt", "renamed_branch_file.txt");
119+
Commands.Move(repo, "branch_file.txt", "renamed_branch_file.txt");
120120
repo.Index.Replace(repo.Lookup<Commit>("32eab9c"));
121121

122122
RepositoryStatus status = repo.RetrieveStatus();
@@ -129,7 +129,7 @@ public void CanResetSourceOfARenameInIndex()
129129
{
130130
using (var repo = new Repository(SandboxStandardTestRepo()))
131131
{
132-
repo.Move("branch_file.txt", "renamed_branch_file.txt");
132+
Commands.Move(repo, "branch_file.txt", "renamed_branch_file.txt");
133133

134134
RepositoryStatus oldStatus = repo.RetrieveStatus();
135135
Assert.Equal(1, oldStatus.RenamedInIndex.Count());
@@ -150,7 +150,7 @@ public void CanResetTargetOfARenameInIndex()
150150
{
151151
using (var repo = new Repository(SandboxStandardTestRepo()))
152152
{
153-
repo.Move("branch_file.txt", "renamed_branch_file.txt");
153+
Commands.Move(repo, "branch_file.txt", "renamed_branch_file.txt");
154154

155155
RepositoryStatus oldStatus = repo.RetrieveStatus();
156156
Assert.Equal(1, oldStatus.RenamedInIndex.Count());

LibGit2Sharp.Tests/UnstageFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void CanUnstageSourceOfARename()
240240
{
241241
using (var repo = new Repository(SandboxStandardTestRepo()))
242242
{
243-
repo.Move("branch_file.txt", "renamed_branch_file.txt");
243+
Commands.Move(repo, "branch_file.txt", "renamed_branch_file.txt");
244244

245245
RepositoryStatus oldStatus = repo.RetrieveStatus();
246246
Assert.Equal(1, oldStatus.RenamedInIndex.Count());
@@ -261,7 +261,7 @@ public void CanUnstageTargetOfARename()
261261
{
262262
using (var repo = new Repository(SandboxStandardTestRepo()))
263263
{
264-
repo.Move("branch_file.txt", "renamed_branch_file.txt");
264+
Commands.Move(repo, "branch_file.txt", "renamed_branch_file.txt");
265265

266266
RepositoryStatus oldStatus = repo.RetrieveStatus();
267267
Assert.Equal(1, oldStatus.RenamedInIndex.Count());
@@ -281,7 +281,7 @@ public void CanUnstageBothSidesOfARename()
281281
{
282282
using (var repo = new Repository(SandboxStandardTestRepo()))
283283
{
284-
repo.Move("branch_file.txt", "renamed_branch_file.txt");
284+
Commands.Move(repo, "branch_file.txt", "renamed_branch_file.txt");
285285
Commands.Unstage(repo, new string[] { "branch_file.txt", "renamed_branch_file.txt" });
286286

287287
RepositoryStatus status = repo.RetrieveStatus();

LibGit2Sharp/Commands/Stage.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Globalization;
45
using System.Collections.Generic;
@@ -199,6 +200,123 @@ public static void Unstage(IRepository repository, IEnumerable<string> paths, Ex
199200
repository.Index.Replace(repository.Head.Tip, paths, explicitPathsOptions);
200201
}
201202
}
203+
204+
/// <summary>
205+
/// Moves and/or renames a file in the working directory and promotes the change to the staging area.
206+
/// </summary>
207+
/// <param name="repository">The repository to act on</param>
208+
/// <param name="sourcePath">The path of the file within the working directory which has to be moved/renamed.</param>
209+
/// <param name="destinationPath">The target path of the file within the working directory.</param>
210+
public static void Move(IRepository repository, string sourcePath, string destinationPath)
211+
{
212+
Move(repository, new[] { sourcePath }, new[] { destinationPath });
213+
}
214+
215+
/// <summary>
216+
/// Moves and/or renames a collection of files in the working directory and promotes the changes to the staging area.
217+
/// </summary>
218+
/// <param name="repository">The repository to act on</param>
219+
/// <param name="sourcePaths">The paths of the files within the working directory which have to be moved/renamed.</param>
220+
/// <param name="destinationPaths">The target paths of the files within the working directory.</param>
221+
public static void Move(IRepository repository, IEnumerable<string> sourcePaths, IEnumerable<string> destinationPaths)
222+
{
223+
Ensure.ArgumentNotNull(repository, "repository");
224+
Ensure.ArgumentNotNull(sourcePaths, "sourcePaths");
225+
Ensure.ArgumentNotNull(destinationPaths, "destinationPaths");
226+
227+
//TODO: Move() should support following use cases:
228+
// - Moving a file under a directory ('file' and 'dir' -> 'dir/file')
229+
// - Moving a directory (and its content) under another directory ('dir1' and 'dir2' -> 'dir2/dir1/*')
230+
231+
//TODO: Move() should throw when:
232+
// - Moving a directory under a file
233+
234+
IDictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>> 10000 ; batch = PrepareBatch(repository, sourcePaths, destinationPaths);
235+
236+
if (batch.Count == 0)
237+
{
238+
throw new ArgumentNullException("sourcePaths");
239+
}
240+
241+
foreach (KeyValuePair<Tuple<string, FileStatus>, Tuple<string, FileStatus>> keyValuePair in batch)
242+
{
243+
string sourcePath = keyValuePair.Key.Item1;
244+
string destPath = keyValuePair.Value.Item1;
245+
246+
if (Directory.Exists(sourcePath) || Directory.Exists(destPath))
247+
{
248+
throw new NotImplementedException();
249+
}
250+
251+
FileStatus sourceStatus = keyValuePair.Key.Item2;
252+
if (sourceStatus.HasAny(new Enum[] { FileStatus.Nonexistent, FileStatus.DeletedFromIndex, FileStatus.NewInWorkdir, FileStatus.DeletedFromWorkdir }))
253+
{
254+
throw new LibGit2SharpException("Unable to move file '{0}'. Its current status is '{1}'.",
255+
sourcePath,
256+
sourceStatus);
257+
}
258+
259+
FileStatus desStatus = keyValuePair.Value.Item2;
260+
if (desStatus.HasAny(new Enum[] { FileStatus.Nonexistent, FileStatus.DeletedFromWorkdir }))
261+
{
262+
continue;
263+
}
264+
265+
throw new LibGit2SharpException("Unable to overwrite file '{0}'. Its current status is '{1}'.",
266+
destPath,
267+
desStatus);
268+
}
269+
270+
string wd = repository.Info.WorkingDirectory;
271+
var index = repository.Index;
272+
foreach (KeyValuePair<Tuple<string, FileStatus>, Tuple<string, FileStatus>> keyValuePair in batch)
273+
{
274+
string from = keyValuePair.Key.Item1;
275+
string to = keyValuePair.Value.Item1;
276+
277+
index.Remove(from);
278+
File.Move(Path.Combine(wd, from), Path.Combine(wd, to));
279+
index.Add(to);
280+
}
281+
282+
index.Write();
283+
}
284+
285+
private static bool Enumerate(IEnumerator<string> leftEnum, IEnumerator<string> rightEnum)
286+
{
287+
bool isLeftEoF = leftEnum.MoveNext();
288+
bool isRightEoF = rightEnum.MoveNext();
289+
290+
if (isLeftEoF == isRightEoF)
291+
{
292+
return isLeftEoF;
293+
}
294+
295+
throw new ArgumentException("The collection of paths are of different lengths.");
296+
}
297+
298+
private static IDictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>> PrepareBatch(IRepository repository, IEnumerable<string> leftPaths, IEnumerable<string> rightPaths)
299+
{
300+
IDictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>> dic = new Dictionary<Tuple<string, FileStatus>, Tuple<string, FileStatus>>();
301+
302+
IEnumerator<string> leftEnum = leftPaths.GetEnumerator();
303+
IEnumerator<string> rightEnum = rightPaths.GetEnumerator();
304+
305+
while (Enumerate(leftEnum, rightEnum))
306+
{
307+
Tuple<string, FileStatus> from = BuildFrom(repository, leftEnum.Current);
308+
Tuple<string, FileStatus> to = BuildFrom(repository, rightEnum.Current);
309+
dic.Add(from, to);
310+
}
311+
312+
return dic;
313+
}
314+
315+
private static Tuple<string, FileStatus> BuildFrom(IRepository repository, string path)
316+
{
317+
string relativePath = repository.BuildRelativePathFrom(path);
318+
return new Tuple<string, FileStatus>(relativePath, repository.RetrieveStatus(relativePath));
319+
}
202320
}
203321
}
204322

LibGit2Sharp/IRepository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,15 @@ public interface IRepository : IDisposable
311311
/// </summary>
312312
/// <param name="sourcePath">The path of the file within the working directory which has to be moved/renamed.</param>
313313
/// <param name="destinationPath">The target path of the file within the working directory.</param>
314+
[Obsolete("This method is deprecatd. Please use LibGit2Sharp.Commands.Move()")]
314315
void Move(string sourcePath, string destinationPath);
315316

316317
/// <summary>
317318
/// Moves and/or renames a collection of files in the working directory and promotes the changes to the staging area.
318319
/// </summary>
319320
/// <param name="sourcePaths">The paths of the files within the working directory which have to be moved/renamed.</param>
320321
/// <param name="destinationPaths">The target paths of the files within the working directory.</param>
322+
[Obsolete("This method is deprecatd. Please use LibGit2Sharp.Commands.Move()")]
321323
void Move(IEnumerable<string> sourcePaths, IEnumerable<string> destinationPaths);
322324

323325
/// <summary>

0 commit comments

Comments
 (0)
0