From 4ecd3e807710ffb67626836ace056595fa6c4065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 15 Feb 2016 13:24:41 +0100 Subject: [PATCH] Expose the prune option for fetching --- LibGit2Sharp.Tests/NetworkFixture.cs | 35 ++++++++++++++++++++++++++++ LibGit2Sharp/FetchOptions.cs | 9 +++++++ LibGit2Sharp/Network.cs | 9 +++++++ 3 files changed, 53 insertions(+) diff --git a/LibGit2Sharp.Tests/NetworkFixture.cs b/LibGit2Sharp.Tests/NetworkFixture.cs index 46623a663..53a443469 100644 --- a/LibGit2Sharp.Tests/NetworkFixture.cs +++ b/LibGit2Sharp.Tests/NetworkFixture.cs @@ -263,5 +263,40 @@ public void CanMergeFetchedRefs() Assert.Equal(mergeResult.Status, MergeStatus.NonFastForward); } } + + [Fact] + public void CanPruneRefs() + { + string url = "https://github.com/libgit2/TestGitRepository"; + + var scd = BuildSelfCleaningDirectory(); + string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath); + + var scd2 = BuildSelfCleaningDirectory(); + string clonedRepoPath2 = Repository.Clone(url, scd2.DirectoryPath); + + + using (var repo = new Repository(clonedRepoPath)) + { + repo.Network.Remotes.Add("pruner", "file://" + clonedRepoPath2); + var remote = repo.Network.Remotes["pruner"]; + repo.Network.Fetch(remote); + Assert.NotNull(repo.Refs["refs/remotes/pruner/master"]); + + // Remove the branch from the source repository + using (var repo2 = new Repository(clonedRepoPath2)) + { + repo2.Refs.Remove("refs/heads/master"); + } + + // and by default we don't prune it + repo.Network.Fetch(remote); + Assert.NotNull(repo.Refs["refs/remotes/pruner/master"]); + + // but we do when asked by the user + repo.Network.Fetch(remote, new FetchOptions { Prune = true} ); + Assert.Null(repo.Refs["refs/remotes/pruner/master"]); + } + } } } diff --git a/LibGit2Sharp/FetchOptions.cs b/LibGit2Sharp/FetchOptions.cs index fba0a96f8..a9d2fb2c7 100644 --- a/LibGit2Sharp/FetchOptions.cs +++ b/LibGit2Sharp/FetchOptions.cs @@ -16,5 +16,14 @@ public sealed class FetchOptions : FetchOptionsBase /// retrieved during this fetch will be retrieved as well). /// public TagFetchMode? TagFetchMode { get; set; } + + /// + /// Specifies the pruning behaviour for the fetch operation + /// + /// If not set, the configuration's setting will take effect. If true, the branches which no longer + /// exist on the remote will be removed from the remote-tracking branches. + /// + /// + public bool? Prune { get; set; } } } diff --git a/LibGit2Sharp/Network.cs b/LibGit2Sharp/Network.cs index 851edb535..231911ada 100644 --- a/LibGit2Sharp/Network.cs +++ b/LibGit2Sharp/Network.cs @@ -206,6 +206,15 @@ private static void DoFetch(FetchOptions options, RemoteSafeHandle remoteHandle, fetchOptions.download_tags = options.TagFetchMode.Value; } + if (options.Prune.HasValue) + { + fetchOptions.Prune = options.Prune.Value ? FetchPruneStrategy.Prune : FetchPruneStrategy.NoPrune; + } + else + { + fetchOptions.Prune = FetchPruneStrategy.FromConfigurationOrDefault; + } + Proxy.git_remote_fetch(remoteHandle, refspecs, fetchOptions, logMessage); }