Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nulltoken/2761494 to your computer and use it in GitHub Desktop.
Save nulltoken/2761494 to your computer and use it in GitHub Desktop.
[LibGit2Sharp] Line ending diff handling
[Fact]
/*
* $ git init .
* $ echo -ne 'a' > file.txt
* $ git add .
* $ git commit -m "No line ending"
* $ echo -ne '\n' >> file.txt
* $ git add .
* $ git diff --cached
* diff --git a/file.txt b/file.txt
* index 2e65efe..7898192 100644
* --- a/file.txt
* +++ b/file.txt
* @@ -1 +1 @@
* -a
* \ No newline at end of file
* +a
*/
public void CanCopeWithEndOfFileNewlineChanges()
{
var scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.RootedDirectoryPath))
{
var fullpath = Path.Combine(repo.Info.WorkingDirectory, "file.txt");
File.WriteAllText(fullpath, "a");
repo.Index.Stage("file.txt");
repo.Commit("Add file without line ending", DummySignature, DummySignature);
File.AppendAllText(fullpath, "\n");
repo.Index.Stage("file.txt");
TreeChanges changes = repo.Diff.Compare(repo.Head.Tip.Tree, DiffTarget.Index);
Assert.Equal(1, changes.Modified.Count());
Assert.Equal(1, changes.LinesAdded);
Assert.Equal(1, changes.LinesDeleted);
var expected = new StringBuilder()
.Append("diff --git a/file.txt b/file.txt\n")
.Append("index 2e65efe..7898192 100644\n")
.Append("--- a/file.txt\n")
.Append("+++ b/file.txt\n")
.Append("@@ -1 +1 @@\n")
.Append("-a\n")
.Append("\\ No newline at end of file\n")
.Append("+a\n");
Assert.Equal(expected.ToString(), changes.Patch);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment