Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multiple fetch specs when setting tracking branch #1782 #1783

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions LibGit2Sharp.Tests/BranchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,39 @@ public void CanUnsetTrackedBranch()
}
}

[Fact]
public void CanSetTrackedBranch_MultipleFetchSpecs()
{
const string testBranchName = "master";
const string trackedBranchName = "refs/remotes/origin/master";

string path = SandboxMultiFetchSpecTestRepo();
using (var repo = new Repository(path))
{
Branch trackedBranch = repo.Branches[trackedBranchName];
Assert.True(trackedBranch.IsRemote);

Branch branch = repo.CreateBranch(testBranchName, trackedBranch.Tip);
Assert.False(branch.IsTracking);

repo.Branches.Update(branch,
b => b.TrackedBranch = trackedBranch.CanonicalName);

// Verify the immutability of the branch.
Assert.False(branch.IsTracking);

// Get the updated branch information.
branch = repo.Branches[testBranchName];

Remote upstreamRemote = repo.Network.Remotes["origin"];
Assert.NotNull(upstreamRemote);

Assert.True(branch.IsTracking);
Assert.Equal(trackedBranch, branch.TrackedBranch);
Assert.Equal("origin", branch.RemoteName);
}
}

[Fact]
public void CanWalkCommitsFromBranch()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is another file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/PR-28
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
ignorecase = true
[remote "origin"]
url = ../multifetchspec_testrepo.git/
fetch = +refs/pull/28/head:refs/remotes/origin/PR-28
fetch = +refs/heads/master:refs/remotes/origin/master
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "PR-28"]
remote = origin
merge = refs/pull/28/head
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x+)JMU�0f040031Qp��/�H-r��I�+�(a(�X�y��ޟ��k֟8iS��6Ti^jy|L�+W���'����xg�f���m;|I %�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b9767f840d2eda4faeafe47712eb3bdf506f8a0a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b9767f840d2eda4faeafe47712eb3bdf506f8a0a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
651df87831579c12dab081797720d9973edf477a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New file
7 changes: 7 additions & 0 deletions LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static BaseFixture()

public static string BareTestRepoPath { get; private set; }
public static string StandardTestRepoWorkingDirPath { get; private set; }
public static string StandardMultiFetchSpecWorkingDirPath { get; private set; }
public static string StandardTestRepoPath { get; private set; }
public static string ShallowTestRepoPath { get; private set; }
public static string MergedTestRepoWorkingDirPath { get; private set; }
Expand Down Expand Up @@ -74,6 +75,7 @@ private static void SetUpTestEnvironment()
BareTestRepoPath = Path.Combine(ResourcesDirectory.FullName, "testrepo.git");
StandardTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "testrepo_wd");
StandardTestRepoPath = Path.Combine(StandardTestRepoWorkingDirPath, "dot_git");
StandardMultiFetchSpecWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "multifetchspec_testrepo_wd");
ShallowTestRepoPath = Path.Combine(ResourcesDirectory.FullName, "shallow.git");
MergedTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "mergedrepo_wd");
MergeRenamesTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "mergerenames_wd");
Expand Down Expand Up @@ -183,6 +185,11 @@ protected string SandboxStandardTestRepo()
return Sandbox(StandardTestRepoWorkingDirPath);
}

protected string SandboxMultiFetchSpecTestRepo()
{
return Sandbox(StandardMultiFetchSpecWorkingDirPath);
}

protected string SandboxMergedTestRepo()
{
return Sandbox(MergedTestRepoWorkingDirPath);
Expand Down
12 changes: 10 additions & 2 deletions LibGit2Sharp/Remote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,16 @@ internal unsafe string FetchSpecTransformToSource(string reference)
{
using (RemoteHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, Name, true))
{
git_refspec* fetchSpecPtr = Proxy.git_remote_get_refspec(remoteHandle, 0);
return Proxy.git_refspec_rtransform(new IntPtr(fetchSpecPtr), reference);
var fetchSpec = IntPtr.Zero;
var refspecCount = Proxy.git_remote_refspec_count(remoteHandle);
for (int i = 0; i < refspecCount; i++)
{
fetchSpec = new IntPtr(Proxy.git_remote_get_refspec(remoteHandle, i));
if (Proxy.git_refspec_dst_matches(fetchSpec, reference))
break;
}

return Proxy.git_refspec_rtransform(fetchSpec, reference);
}
}

Expand Down