From 1204fdff3b815ab27f0afac21f40a399889e383d Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Mon, 20 Feb 2012 23:13:40 -0600 Subject: [PATCH] Add support for local tracking branches (branch..remote = ".") --- LibGit2Sharp.Tests/BranchFixture.cs | 16 +++++++++++++++- .../Resources/testrepo_wd/dot_git/config | 3 +++ .../testrepo_wd/dot_git/refs/heads/track-local | 1 + LibGit2Sharp/Branch.cs | 5 +++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 LibGit2Sharp.Tests/Resources/testrepo_wd/dot_git/refs/heads/track-local diff --git a/LibGit2Sharp.Tests/BranchFixture.cs b/LibGit2Sharp.Tests/BranchFixture.cs index 92d2999fd..40259abcf 100644 --- a/LibGit2Sharp.Tests/BranchFixture.cs +++ b/LibGit2Sharp.Tests/BranchFixture.cs @@ -172,7 +172,7 @@ public void CanListAllBranchesWhenGivenWorkingDir() { using (var repo = new Repository(StandardTestRepoWorkingDirPath)) { - var expectedWdBranches = new[] { "master", "origin/HEAD", "origin/br2", "origin/master", "origin/packed-test", "origin/test" }; + var expectedWdBranches = new[] { "master", "track-local", "origin/HEAD", "origin/br2", "origin/master", "origin/packed-test", "origin/test" }; CollectionAssert.AreEqual(expectedWdBranches, repo.Branches.Select(b => b.Name).ToArray()); } @@ -186,6 +186,7 @@ public void CanListAllBranchesIncludingRemoteRefs() var expectedBranchesIncludingRemoteRefs = new[] { new { Name = "master", Sha = "32eab9cb1f450b5fe7ab663462b77d7f4b703344", IsRemote = false }, + new { Name = "track-local", Sha = "580c2111be43802dab11328176d94c391f1deae9", IsRemote = false }, new { Name = "origin/HEAD", Sha = "580c2111be43802dab11328176d94c391f1deae9", IsRemote = true }, new { Name = "origin/br2", Sha = "a4a7dce85cf63874e984719f4fdd239f5145052f", IsRemote = true }, new { Name = "origin/master", Sha = "580c2111be43802dab11328176d94c391f1deae9", IsRemote = true }, @@ -266,6 +267,19 @@ public void CanGetTrackingInformationForTrackingBranch() } } + [Test] + public void CanGetTrackingInformationForLocalTrackingBranch() + { + using (var repo = new Repository(StandardTestRepoPath)) + { + var branch = repo.Branches["track-local"]; + branch.IsTracking.ShouldBeTrue(); + branch.TrackedBranch.ShouldEqual(repo.Branches["master"]); + branch.AheadBy.ShouldEqual(2); + branch.BehindBy.ShouldEqual(2); + } + } + [Test] public void CanWalkCommitsFromAnotherBranch() { diff --git a/LibGit2Sharp.Tests/Resources/testrepo_wd/dot_git/config b/LibGit2Sharp.Tests/Resources/testrepo_wd/dot_git/config index 7e002a7d2..45012594a 100644 --- a/LibGit2Sharp.Tests/Resources/testrepo_wd/dot_git/config +++ b/LibGit2Sharp.Tests/Resources/testrepo_wd/dot_git/config @@ -12,6 +12,9 @@ [branch "master"] remote = origin merge = refs/heads/master +[branch "track-local"] + remote = . + merge = refs/heads/master [unittests] longsetting = 15234 intsetting = 2 diff --git a/LibGit2Sharp.Tests/Resources/testrepo_wd/dot_git/refs/heads/track-local b/LibGit2Sharp.Tests/Resources/testrepo_wd/dot_git/refs/heads/track-local new file mode 100644 index 000000000..99098dc82 --- /dev/null +++ b/LibGit2Sharp.Tests/Resources/testrepo_wd/dot_git/refs/heads/track-local @@ -0,0 +1 @@ +580c2111be43802dab11328176d94c391f1deae9 diff --git a/LibGit2Sharp/Branch.cs b/LibGit2Sharp/Branch.cs index fdfd74fb3..f5e86de20 100644 --- a/LibGit2Sharp/Branch.cs +++ b/LibGit2Sharp/Branch.cs @@ -187,6 +187,11 @@ private Branch ResolveTrackedBranch() private static string ResolveTrackedReference(string trackedRemote, string trackedRefName) { + if (trackedRemote == ".") + { + return trackedRefName; + } + //TODO: To be replaced by native libgit2 git_branch_tracked_reference() when available. return trackedRefName.Replace("refs/heads/", string.Concat("refs/remotes/", trackedRemote, "/")); }