Skip to content

Commit 61e8add

Browse files
committed
Fix Remotes.Add(name, url, refspec) implementation
Prevents the creation of a default fetch refspec beside the passed in one.
1 parent 08fb122 commit 61e8add

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

LibGit2Sharp.Tests/RemoteFixture.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,27 @@ public void DoesNotThrowWhenARemoteHasNoUrlSet()
170170
Assert.Equal(1, remotes.Count(r => r.Name == "no_url"));
171171
}
172172
}
173+
174+
[Fact]
175+
public void CreatingARemoteAddsADefaultFetchRefSpec()
176+
{
177+
var path = CloneStandardTestRepo();
178+
using (var repo = new Repository(path))
179+
{
180+
var remote = repo.Network.Remotes.Add("one", "http://github.com/up/stream");
181+
Assert.Equal("+refs/heads/*:refs/remotes/one/*", remote.RefSpecs.Single().Specification);
182+
}
183+
}
184+
185+
[Fact]
186+
public void CanCreateARemoteWithASpecifiedFetchRefSpec()
187+
{
188+
var path = CloneStandardTestRepo();
189+
using (var repo = new Repository(path))
190+
{
191+
var remote = repo.Network.Remotes.Add("two", "http://github.com/up/stream", "+refs/heads/*:refs/remotes/grmpf/*");
192+
Assert.Equal("+refs/heads/*:refs/remotes/grmpf/*", remote.RefSpecs.Single().Specification);
193+
}
194+
}
173195
}
174196
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,15 @@ internal static extern int git_remote_create_inmemory(
953953
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string refspec,
954954
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string url);
955955

956+
957+
[DllImport(libgit2)]
958+
internal static extern int git_remote_create_with_fetchspec(
959+
out RemoteSafeHandle remote,
960+
RepositorySafeHandle repo,
961+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name,
962+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string url,
963+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string refspec);
964+
956965
[DllImport(libgit2)]
957966
internal static extern void git_remote_disconnect(RemoteSafeHandle remote);
958967

@@ -1009,11 +1018,6 @@ internal static extern int git_remote_set_callbacks(
10091018
RemoteSafeHandle remote,
10101019
ref GitRemoteCallbacks callbacks);
10111020

1012-
[DllImport(libgit2)]
1013-
internal static extern int git_remote_add_fetch(
1014-
RemoteSafeHandle remote,
1015-
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof (StrictUtf8Marshaler))] string refspec);
1016-
10171021
internal delegate int remote_progress_callback(IntPtr str, int len, IntPtr data);
10181022

10191023
internal delegate int remote_completion_callback(RemoteCompletionType type, IntPtr data);

LibGit2Sharp/Core/Proxy.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,18 @@ public static RemoteSafeHandle git_remote_create(RepositorySafeHandle repo, stri
16791679
}
16801680
}
16811681

1682+
public static RemoteSafeHandle git_remote_create_with_fetchspec(RepositorySafeHandle repo, string name, string url, string refspec)
1683+
{
1684+
using (ThreadAffinity())
1685+
{
1686+
RemoteSafeHandle handle;
1687+
int res = NativeMethods.git_remote_create_with_fetchspec(out handle, repo, name, url, refspec);
1688+
Ensure.ZeroResult(res);
1689+
1690+
return handle;
1691+
}
1692+
}
1693+
16821694
public static RemoteSafeHandle git_remote_create_inmemory(RepositorySafeHandle repo, string url, string refspec)
16831695
{
16841696
using (ThreadAffinity())
@@ -1862,15 +1874,6 @@ public static void git_remote_set_autotag(RemoteSafeHandle remote, TagFetchMode
18621874
NativeMethods.git_remote_set_autotag(remote, value);
18631875
}
18641876

1865-
public static void git_remote_add_fetch(RemoteSafeHandle remote, string refspec)
1866-
{
1867-
using (ThreadAffinity())
1868-
{
1869-
int res = NativeMethods.git_remote_add_fetch(remote, refspec);
1870-
Ensure.ZeroResult(res);
1871-
}
1872-
}
1873-
18741877
public static void git_remote_set_callbacks(RemoteSafeHandle remote, ref GitRemoteCallbacks callbacks)
18751878
{
18761879
using (ThreadAffinity())

LibGit2Sharp/RemoteCollection.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,8 @@ public virtual Remote Add(string name, string url, string fetchRefSpec)
120120
Ensure.ArgumentNotNull(url, "url");
121121
Ensure.ArgumentNotNull(fetchRefSpec, "fetchRefSpec");
122122

123-
using (RemoteSafeHandle handle = Proxy.git_remote_create(repository.Handle, name, url))
123+
using (RemoteSafeHandle handle = Proxy.git_remote_create_with_fetchspec(repository.Handle, name, url, fetchRefSpec))
124124
{
125-
Proxy.git_remote_add_fetch(handle, fetchRefSpec);
126-
Proxy.git_remote_save(handle);
127125
return Remote.BuildFromPtr(handle, this.repository);
128126
}
129127
}

0 commit comments

Comments
 (0)