8000 Make Index.Stage(), Index.Unstage(), Index.Move() and Index.Remove() … · joncham/libgit2sharp@53a4c85 · GitHub
[go: up one dir, main page]

Skip to content

Commit 53a4c85

Browse files
fbezdekanulltoken
authored andcommitted
Make Index.Stage(), Index.Unstage(), Index.Move() and Index.Remove() able to cope with native Windows directory separator char
Should temporarily fix issue libgit2#74.
1 parent 4726bae commit 53a4c85

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

LibGit2Sharp.Tests/IndexFixture.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,21 @@ public void StagingANewVersionOfAFileThenUnstagingItRevertsTheBlobToTheVersionOf
166166
{
167167
int count = repo.Index.Count;
168168

169-
const string fileName = "1/branch_file.txt";
170-
ObjectId blobId = repo.Index[fileName].Id;
169+
string filename = "1" + Path.DirectorySeparatorChar + "branch_file.txt";
170+
const string posixifiedFileName = "1/branch_file.txt";
171+
ObjectId blobId = repo.Index[posixifiedFileName].Id;
171172

172-
string fullpath = Path.Combine(repo.Info.WorkingDirectory, fileName);
173+
string fullpath = Path.Combine(repo.Info.WorkingDirectory, filename);
173174

174175
File.AppendAllText(fullpath, "Is there there anybody out there?");
175-
repo.Index.Stage(fileName);
176+
repo.Index.Stage(filename);
176177

177178
repo.Index.Count.ShouldEqual(count);
178-
repo.Index[fileName].Id.ShouldNotEqual((blobId));
179+
repo.Index[posixifiedFileName].Id.ShouldNotEqual((blobId));
179180

180-
repo.Index.Unstage(fileName);
181+
repo.Index.Unstage(posixifiedFileName);
181182
repo.Index.Count.ShouldEqual(count);
182-
repo.Index[fileName].Id.ShouldEqual((blobId));
183+
repo.Index[posixifiedFileName].Id.ShouldEqual((blobId));
183184
}
184185
}
185186

@@ -231,6 +232,29 @@ public void CanStageANewFileWithAFullPath()
231232
}
232233
}
233234

235+
[Test]
236+ 10000
public void CanStageANewFileWithARelativePathContainingNativeDirectorySeparatorCharacters()
237+
{
238+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(Constants.StandardTestRepoWorkingDirPath);
239+
using (var repo = new Repository(path.RepositoryPath))
240+
{
241+
int count = repo.Index.Count;
242+
243+
DirectoryInfo di = Directory.CreateDirectory(Path.Combine(repo.Info.WorkingDirectory, "Project"));
244+
string file = "Project" + Path.DirectorySeparatorChar + "a_file.txt";
245+
246+
File.WriteAllText(Path.Combine(di.FullName, "a_file.txt"), "With backward slash on Windows!");
247+
248+
repo.Index.Stage(file);
249+
250+
repo.Index.Count.ShouldEqual(count + 1);
251+
252+
const string posixifiedPath = "Project/a_file.txt";
253+
repo.Index[posixifiedPath].ShouldNotBeNull();
254+
repo.Index[posixifiedPath].Path.ShouldEqual(posixifiedPath);
255+
}
256+
}
257+
234258
[Test]
235259
public void StagingANewFileWithAFullPathWhichEscapesOutOfTheWorkingDirThrows()
236260
{
@@ -373,7 +397,7 @@ public void CanRemoveAFile()
373397
{
374398
int count = repo.Index.Count;
375399

376-
const string filename = "1/branch_file.txt";
400+
string filename = "1" + Path.DirectorySeparatorChar + "branch_file.txt";
377401
string fullpath = Path.Combine(repo.Info.WorkingDirectory, filename);
378402

379403
File.Exists(fullpath).ShouldBeTrue();

LibGit2Sharp/Index.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,16 @@ public void Remove(string path)
224224

225225
private void AddToIndex(string relativePath)
226226
{
227+
relativePath = PosixPathHelper.ToPosix(relativePath);
228+
227229
int res = NativeMethods.git_index_add(handle, relativePath);
228230
Ensure.Success(res);
229231
}
230232

231233
private void RemoveFromIndex(string relativePath)
232234
{
235+
relativePath = PosixPathHelper.ToPosix(relativePath);
236+
233237
int res = NativeMethods.git_index_find(handle, relativePath);
234238
Ensure.Success(res, true);
235239

@@ -306,7 +310,7 @@ public FileStatus RetrieveStatus(string filePath)
306310

307311
FileStatus status;
308312

309-
int res = NativeMethods.git_status_file(out status, repo.Handle, relativePath);
313+
int res = NativeMethods.git_status_file(out status, repo.Handle, PosixPathHelper.ToPosix(relativePath));
310314
if (res == (int)GitErrorCode.GIT_ENOTFOUND)
311315
{
312316
return FileStatus.Nonexistent;

0 commit comments

Comments
 (0)
0