Skip to content

Commit 8d8fbb1

Browse files
committed
Teach RemoteUpdater to set the Url
1 parent dfd3fe6 commit 8d8fbb1

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

LibGit2Sharp.Tests/RemoteFixture.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,27 @@ public void CanSetTagFetchMode(TagFetchMode tagFetchMode)
6868
}
6969
}
7070

71+
[Fact]
72+
public void CanSetRemoteUrl()
73+
{
74+
string path = CloneBareTestRepo();
75+
using (var repo = new Repository(path))
76+
{
77+
const string name = "upstream";
78+
const string url = "https://github.com/libgit2/libgit2sharp.git";
79+
const string newUrl = "https://github.com/libgit2/libgit2.git";
80+
81+
repo.Network.Remotes.Add(name, url);
82+
Remote remote = repo.Network.Remotes[name];
83+
Assert.NotNull(remote);
84+
85+
Remote updatedremote = repo.Network.Remotes.Update(remote,
86+
r => r.Url = newUrl);
87+
88+
Assert.Equal(newUrl, updatedremote.Url);
89+
}
90+
}
91+
7192
[Fact]
7293
public void CanCheckEqualityOfRemote()
7394
{

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,11 @@ internal static extern int git_remote_fetch(
10691069
[DllImport(libgit2)]
10701070
internal static extern int git_remote_set_push_refspecs(RemoteSafeHandle remote, ref GitStrArray array);
10711071

1072+
[DllImport(libgit2)]
1073+
internal static extern int git_remote_set_url(
1074+
RemoteSafeHandle remote,
1075+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string url);
1076+
10721077
[DllImport(libgit2)]
10731078
internal static extern int git_remote_is_valid_name(
10741079
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string remote_name);

LibGit2Sharp/Core/Proxy.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,15 @@ public static void git_remote_set_push_refspecs(RemoteSafeHandle remote, IEnumer
19751975
}
19761976
}
19771977

1978+
public static void git_remote_set_url(RemoteSafeHandle remote, string url)
1979+
{
1980+
using (ThreadAffinity())
1981+
{
1982+
int res = NativeMethods.git_remote_set_url(remote, url);
1983+
Ensure.ZeroResult(res);
1984+
}
1985+
}
1986+
19781987
public static void git_remote_fetch(RemoteSafeHandle remote, Signature signature, string logMessage)
19791988
{
19801989
using (ThreadAffinity())

LibGit2Sharp/RemoteUpdater.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,25 @@ public virtual TagFetchMode TagFetchMode
8282
}
8383
}
8484

85+
/// <summary>
86+
/// Sets the url defined for this <see cref="Remote"/>
87+
/// </summary>
88+
public virtual string Url
89+
{
90+
set
91+
{
92+
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repo.Handle, remote.Name, true))
93+
{
94+
Proxy.git_remote_set_url(remoteHandle, value);
95+
Proxy.git_remote_save(remoteHandle);
96+
}
97+
}
98+
}
99+
85100
/// <summary>
86101
/// Sets the list of <see cref="RefSpec"/>s defined for this <see cref="Remote"/> that are intended to
87102
/// be used during a Fetch operation
88103
/// </summary>
89-
/// <remarks>Changing the list updates the <see cref="Remote" />.</remarks>
90104
public virtual ICollection<string> FetchRefSpecs
91105
{
92106
get { return fetchRefSpecs; }
@@ -97,7 +111,6 @@ public virtual ICollection<string> FetchRefSpecs
97111
/// Sets or gets the list of <see cref="RefSpec"/>s defined for this <see cref="Remote"/> that are intended to
98112
/// be used during a Push operation
99113
/// </summary>
100-
/// <remarks>Changing the list updates the <see cref="Remote" />.</remarks>
101114
public virtual ICollection<string> PushRefSpecs
102115
{
103116
get { return pushRefSpecs; }

0 commit comments

Comments
 (0)