8000 Add RemoteCollection.Create() · rlazev/libgit2sharp@c37d818 · GitHub
[go: up one dir, main page]

Skip to content

Commit c37d818

Browse files
committed
Add RemoteCollection.Create()
1 parent 2194806 commit c37d818

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

LibGit2Sharp.Tests/RemoteFixture.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ public void CanCheckEqualityOfRemote()
5757
Remote otherOrigin = repo.Remotes["origin"];
5858
otherOrigin.ShouldEqual(oneOrigin);
5959

60-
repo.Config.Set("remote.origin2.url", oneOrigin.Url);
60+
Remote createdRemote = repo.Remotes.Create("origin2", oneOrigin.Url);
6161

62-
Remote differentRemote = repo.Remotes["origin2"];
63-
differentRemote.ShouldNotBeNull();
62+
Remote loadedRemote = repo.Remotes["origin2"];
63+
loadedRemote.ShouldNotBeNull();
64+
loadedRemote.ShouldEqual(createdRemote);
6465

65-
differentRemote.ShouldNotEqual(oneOrigin);
66+
loadedRemote.ShouldNotEqual(oneOrigin);
6667
}
6768
}
6869
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,19 @@ public static extern int git_remote_load(
277277
[DllImport(libgit2)]
278278
public static extern IntPtr git_remote_name(RemoteSafeHandle remote);
279279

280+
[DllImport(libgit2)]
281+
public static extern int git_remote_new(
282+
out RemoteSafeHandle remote,
283+
RepositorySafeHandle repo,
284+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string url,
285+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);
286+
280287
[DllImport(libgit2)]
281288
public static extern IntPtr git_remote_url(RemoteSafeHandle remote);
282289

290+
[DllImport(libgit2)]
291+
public static extern int git_remote_save(RemoteSafeHandle remote);
292+
283293
[DllImport(libgit2)]
284294
public static extern int git_repository_config(
285295
out ConfigurationSafeHandle cfg,

LibGit2Sharp/RemoteCollection.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,29 @@ IEnumerator IEnumerable.GetEnumerator()
7171
{
7272
return GetEnumerator();
7373
}
74+
75+
/// <summary>
76+
/// Creates a <see cref="Remote"/> with the specified name and for the repository at the specified location
77+
/// </summary>
78+
/// <param name = "name">The name of the remote to create.</param>
79+
/// <param name = "url">The location of the repository.</param>
80+
/// <returns>A new <see cref = "Remote" />.</returns>
81+
public Remote Create(string name, string url)
82+
{
83+
Ensure.ArgumentNotNull(name, "name");
84+
Ensure.ArgumentNotNull(url, "url");
85+
86+
RemoteSafeHandle handle;
87+
int res = NativeMethods.git_remote_new(out handle, repository.Handle, url, name);
88+
Ensure.Success(res);
89+
90+
using (handle)
91+
{
92+
res = NativeMethods.git_remote_save(handle);
93+
Ensure.Success(res);
94+
95+
return Remote.CreateFromPtr(handle);
96+
}
97+
}
7498
}
7599
}

0 commit comments

Comments
 (0)
0