8000 Expose the prune option for fetching · leoniDEV/libgit2sharp@4ecd3e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ecd3e8

Browse files
committed
Expose the prune option for fetching
1 parent 327dbff commit 4ecd3e8

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

LibGit2Sharp.Tests/NetworkFixture.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,5 +263,40 @@ public void CanMergeFetchedRefs()
263263
Assert.Equal(mergeResult.Status, MergeStatus.NonFastForward);
264264
}
265265
}
266+
267+
[Fact]
268+
public void CanPruneRefs()
269+
{
270+
string url = "https://github.com/libgit2/TestGitRepository";
271+
272+
var scd = BuildSelfCleaningDirectory();
273+
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);
274+
275+
var scd2 = BuildSelfCleaningDirectory();
276+
string clonedRepoPath2 = Repository.Clone(url, scd2.DirectoryPath);
277+
278+
279+
using (var repo = new Repository(clonedRepoPath))
280+
{
281+
repo.Network.Remotes.Add("pruner", "file://" + clonedRepoPath2);
282+
var remote = repo.Network.Remotes["pruner"];
283+
repo.Network.Fetch(remote);
284+
Assert.NotNull(repo.Refs["refs/remotes/pruner/master"]);
285+
286+
// Remove the branch from the source repository
287+
using (var repo2 = new Repository(clonedRepoPath2))
288+
{
289+
repo2.Refs.Remove("refs/heads/master");
290+
}
291+
292+
// and by default we don't prune it
293+
repo.Network.Fetch(remote);
294+
Assert.NotNull(repo.Refs["refs/remotes/pruner/master"]);
295+
296+
// but we do when asked by the user
297+
repo.Network.Fetch(remote, new FetchOptions { Prune = true} );
298+
Assert.Null(repo.Refs["refs/remotes/pruner/master"]);
299+
}
300+
}
266301
}
267302
}

LibGit2Sharp/FetchOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,14 @@ public sealed class FetchOptions : FetchOptionsBase
1616
/// retrieved during this fetch will be retrieved as well).</para>
1717
/// </summary>
1818
public TagFetchMode? TagFetchMode { get; set; }
19+
20+
/// <summary>
21+
/// Specifies the pruning behaviour for the fetch operation
22+
/// <para>
23+
/// If not set, the configuration's setting will take effect. If true, the branches which no longer
24+
/// exist on the remote will be removed from the remote-tracking branches.
25+
/// </para>
26+
/// </summary>
27+
public bool? Prune { get; set; }
1928
}
2029
}

LibGit2Sharp/Network.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ private static void DoFetch(FetchOptions options, RemoteSafeHandle remoteHandle,
206206
fetchOptions.download_tags = options.TagFetchMode.Value;
207207
}
208208

209+
if (options.Prune.HasValue)
210+
{
211+
fetchOptions.Prune = options.Prune.Value ? FetchPruneStrategy.Prune : FetchPruneStrategy.NoPrune;
212+
}
213+
else
214+
{
215+
fetchOptions.Prune = FetchPruneStrategy.FromConfigurationOrDefault;
216+
}
217+
209218
Proxy.git_remote_fetch(remoteHandle, refspecs, fetchOptions, logMessage);
210219
}
211220

0 commit comments

Comments
 (0)
0