Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Introduce RemoteCollection.Remove() #731

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

Merged
merged 1 commit into from
Jun 1, 2014
Merged
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
25 changes: 25 additions & 0 deletions LibGit2Sharp.Tests/RemoteFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,30 @@ public void CanCreateARemoteWithASpecifiedFetchRefSpec()
Assert.Equal("+refs/heads/*:refs/remotes/grmpf/*", remote.RefSpecs.Single().Specification);
}
}

[Fact]
public void CanDeleteExistingRemote()
{
var path = CloneStandardTestRepo();
using (var repo = new Repository(path))
{
Assert.NotNull(repo.Network.Remotes["origin"]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please assert refs/remotes/origin/test exists here....

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the API for getting that exact string? Only things I'm seeing is refSpecs stuff from either config or the remote itself.

I guess config is the place to check, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still isn't in the latest rebase, so the diff shouldn't be marked as outdated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like Assert.NotNull(repo.Branches["origin/test"]); should work.

Assert.NotEmpty(repo.Refs.FromGlob("refs/remotes/origin/*"));

repo.Network.Remotes.Remove("origin");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please assert that

  • The remote exists before removing it?
  • Some remote tracking branches exists before and they no longer exists after?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test that shows that removing a non existing remote doesn't throw (cf BranchFixture.CanRemoveANonExistingBranch())?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sec, that changes the functionality a bit. :D

Assert.Null(repo.Network.Remotes["origin"]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...end ensure it gets dropped here

Assert.Empty(repo.Refs.FromGlob("refs/remotes/origin/*"));
}
}

[Fact]
public void CanDeleteNonExistingRemote()
{
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Null(repo.Network.Remotes["i_dont_exist"]);
repo.Network.Remotes.Remove("i_dont_exist");
}
}
}
}
3 changes: 3 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,9 @@ internal static extern int git_remote_create_with_fetchspec(
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string url,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string refspec);

[DllImport(libgit2)]
internal static extern int git_remote_delete(RemoteSafeHandle remote);

[DllImport(libgit2)]
internal static extern void git_remote_disconnect(RemoteSafeHandle remote);

Expand Down
18 changes: 18 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,24 @@ public static void git_remote_connect(RemoteSafeHandle remote, GitDirection dire
}
}

public static void git_remote_delete(RepositorySafeHandle repo, string name)
{
using (ThreadAffinity())
{
using (RemoteSafeHandle remote = git_remote_load(repo, name, false))
{
if (remote == null)
{
return;
}

int res = NativeMethods.git_remote_delete(remote);
Ensure.ZeroResult(res);
remote.SetHandleAsInvalid();
}
}
}

public static void git_remote_disconnect(RemoteSafeHandle remote)
{
using (ThreadAffinity())
Expand Down
12 changes: 12 additions & 0 deletions LibGit2Sharp/RemoteCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ public virtual bool IsValidName(string name)
return Remote.IsValidName(name);
}

/// <summary>
/// Deletes the <see cref="Remote"/> with the specified name.
/// </summary>
/// <param name="name">The name of the remote to remove.</param>
/// <returns>A new <see cref="Remote"/>.</returns>
public virtual void Remove(string name)
{
Ensure.ArgumentNotNull(name, "name");

Proxy.git_remote_delete(repository.Handle, name);
}

private string DebuggerDisplay
{
get
Expand Down