Skip to content

Instantly share code, notes, and snippets.

@nulltoken
Created May 20, 2012 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nulltoken/2758810 to your computer and use it in GitHub Desktop.
Save nulltoken/2758810 to your computer and use it in GitHub Desktop.
[Libgit2] Diff issues?
using System.IO;
using System.Linq;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
namespace LibGit2Sharp.Tests
{
public class DiffTreeToTargetFixture : BaseFixture
{
private void SetUpSimpleDiffContext(Repository repo)
{
var fullpath = Path.Combine(repo.Info.WorkingDirectory, "file.txt");
File.WriteAllText(fullpath, "hello\n");
repo.Index.Stage(fullpath);
repo.Commit("Initial commit", DummySignature, DummySignature);
File.AppendAllText(fullpath, "world\n");
repo.Index.Stage(fullpath);
File.AppendAllText(fullpath, "!!!\n");
}
[Fact]
/*
* $ git diff
* diff --git a/file.txt b/file.txt
* index 94954ab..4f125e3 100644
* --- a/file.txt
* +++ b/file.txt
* @@ -1,2 +1,3 @@
* hello
* world
* +!!!
*/
public void CanCompareASimpleTreeAgainstTheWorkDirAndTheIndex()
{
var scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.RootedDirectoryPath))
{
SetUpSimpleDiffContext(repo);
TreeChanges changes = repo.Diff.Compare(repo.Head.Tip.Tree, DiffTarget.BothWorkingDirectoryAndIndex);
var expected = new StringBuilder()
.Append("diff --git a/file.txt b/file.txt\n")
.Append("index 94954ab..4f125e3 100644\n")
.Append("--- a/file.txt\n")
.Append("+++ b/file.txt\n")
.Append("@@ -1,2 +1,3 @@\n")
.Append(" hello\n")
.Append(" world\n")
.Append("+!!!\n");
Assert.Equal(expected.ToString(), changes.Patch);
}
}
[Fact]
/*
* No direct git equivalent but should output
*
* diff --git a/file.txt b/file.txt
* index ce01362..4f125e3 100644
* --- a/file.txt
* +++ b/file.txt
* @@ -1 +1,3 @@
* hello
* +world
* +!!!
*/
public void CanCompareASimpleTreeAgainstTheWorkDir()
{
var scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.RootedDirectoryPath))
{
SetUpSimpleDiffContext(repo);
TreeChanges changes = repo.Diff.Compare(repo.Head.Tip.Tree, DiffTarget.WorkingDirectory);
var expected = new StringBuilder()
.Append("diff --git a/file.txt b/file.txt\n")
.Append("index ce01362..4f125e3 100644\n")
.Append("--- a/file.txt\n")
.Append("+++ b/file.txt\n")
.Append("@@ -1 +1,3 @@\n")
.Append(" hello\n")
.Append("+world\n")
.Append("+!!!\n");
Assert.Equal(expected.ToString(), changes.Patch);
}
}
[Fact]
/*
* $ git diff --cached
* diff --git a/file.txt b/file.txt
* index ce01362..94954ab 100644
* --- a/file.txt
* +++ b/file.txt
* @@ -1 +1,2 @@
* hello
* +world
*/
public void CanCompareASimpleTreeAgainstTheIndex()
{
var scd = BuildSelfCleaningDirectory();
using (var repo = Repository.Init(scd.RootedDirectoryPath))
{
SetUpSimpleDiffContext(repo);
TreeChanges changes = repo.Diff.Compare(repo.Head.Tip.Tree, DiffTarget.Index);
var expected = new StringBuilder()
.Append("diff --git a/file.txt b/file.txt\n")
.Append("index ce01362..94954ab 100644\n")
.Append("--- a/file.txt\n")
.Append("+++ b/file.txt\n")
.Append("@@ -1 +1,2 @@\n")
.Append(" hello\n")
.Append("+world\n");
Assert.Equal(expected.ToString(), changes.Patch);
}
}
/*
* $ git diff --cached
* diff --git a/deleted_staged_file.txt b/deleted_staged_file.txt
* deleted file mode 100644
* index 5605472..0000000
* --- a/deleted_staged_file.txt
* +++ /dev/null
* @@ -1 +0,0 @@
* -things
* diff --git a/modified_staged_file.txt b/modified_staged_file.txt
* index 15d2ecc..e68bcc7 100644
* --- a/modified_staged_file.txt
* +++ b/modified_staged_file.txt
* @@ -1 +1,2 @@
* +a change
* more files!
* diff --git a/new_tracked_file.txt b/new_tracked_file.txt
* new file mode 100644
* index 0000000..935a81d
* --- /dev/null
* +++ b/new_tracked_file.txt
* @@ -0,0 +1 @@
* +a new file
*/
[Fact]
public void CanCompareAMoreComplexTreeAgainstTheIndex()
{
using (var repo = new Repository(StandardTestRepoPath))
{
Tree tree = repo.Head.Tip.Tree;
TreeChanges changes = repo.Diff.Compare(tree, DiffTarget.Index);
Assert.NotNull(changes);
Assert.Equal(3, changes.Count());
Assert.Equal("deleted_staged_file.txt", changes.Deleted.Single().Path);
Assert.Equal("new_tracked_file.txt", changes.Added.Single().Path);
Assert.Equal("modified_staged_file.txt", changes.Modified.Single().Path);
}
}
/*
Below the output of the test run
------ Test started: Assembly: LibGit2Sharp.Tests.dll ------
Test 'LibGit2Sharp.Tests.DiffTreeToTargetFixture.CanCompareASimpleTreeAgainstTheWorkDirAndTheIndex' failed: Assert.Equal() Failure
Position: First difference is at position 39
Expected: diff --git a/file.txt b/file.txt
index 94954ab..4f125e3 100644
--- a/file.txt
+++ b/file.txt
@@ -1,2 +1,3 @@
hello
world
+!!!
Actual: diff --git a/file.txt b/file.txt
index ce01362..4f125e3 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,3 @@
hello
+world
+!!!
DiffTreeToTargetFixture.cs(58,0): at LibGit2Sharp.Tests.DiffTreeToTargetFixture.CanCompareASimpleTreeAgainstTheWorkDirAndTheIndex()
3 passed, 1 failed, 0 skipped, took 5,59 seconds (xUnit.net 1.9.0 build 1566).
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment