Skip to content

Commit 47b2ee0

Browse files
Adds Depth to FetchOptions allowing for shallow cloning (#2070)
* Added Depth as a fetch option * Map Depth from FetchOptions to GitFetchOptions * Added tests for shallow cloning
1 parent 5162c68 commit 47b2ee0

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

LibGit2Sharp.Tests/CloneFixture.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,34 @@ public void CanClone(string url)
3535
}
3636
}
3737

38+
[Theory]
39+
[InlineData("https://github.com/libgit2/TestGitRepository",1)]
40+
[InlineData("https://github.com/libgit2/TestGitRepository",5)]
41+
[InlineData("https://github.com/libgit2/TestGitRepository",7)]
42+
public void CanCloneShallow(string url, int depth)
43+
{
44+
var scd = BuildSelfCleaningDirectory();
45+
46+
var clonedRepoPath = Repository.Clone(url, scd.DirectoryPath, new CloneOptions
47+
{
48+
FetchOptions =
49+
{
50+
Depth = depth,
51+
},
52+
});
53+
54+
using (var repo = new Repository(clonedRepoPath))
55+
{
56+
var commitsFirstParentOnly = repo.Commits.QueryBy(new CommitFilter
57+
{
58+
FirstParentOnly = true,
59+
});
60+
61+
Assert.Equal(depth, commitsFirstParentOnly.Count());
62+
Assert.Equal("49322bb17d3acc9146f98c97d078513228bbf3c0", repo.Head.Tip.Id.ToString());
63+
}
64+
}
65+
3866
[Theory]
3967
[InlineData("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f")]
4068
[InlineData("packed", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9")]

LibGit2Sharp/FetchOptions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public sealed class FetchOptions : FetchOptionsBase
2626
/// </summary>
2727
public bool? Prune { get; set; }
2828

29+
/// <summary>
30+
/// Specifies the depth of the fetch to perform.
31+
/// <para>
32+
/// Default value is 0 (full) fetch.
33+
/// </para>
34+
/// </summary>
35+
public int Depth { get; set; } = 0;
36+
2937
/// <summary>
3038
/// Get/Set the custom headers.
3139
///

LibGit2Sharp/Repository.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,13 @@ public static string Clone(string sourceUrl, string workdirPath,
780780

781781
options ??= new CloneOptions();
782782

783+
// As default behaviour for GitFetchOptionsWrapper ctor is to create
784+
// a new instance of GitFetchOptions we only populate the Depth field.
785+
var fetchOptions = new GitFetchOptions
786+
{
787+
Depth = options.FetchOptions.Depth,
788+
};
789+
783790
// context variable that contains information on the repository that
784791
// we are cloning.
785792
var context = new RepositoryOperationContext(Path.GetFullPath(workdirPath), sourceUrl);
@@ -794,7 +801,7 @@ public static string Clone(string sourceUrl, string workdirPath,
794801
}
795802

796803
using (var checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
797-
using (var fetchOptionsWrapper = new GitFetchOptionsWrapper())
804+
using (var fetchOptionsWrapper = new GitFetchOptionsWrapper(fetchOptions))
798805
{
799806
var gitCheckoutOptions = checkoutOptionsWrapper.Options;
800807

0 commit comments

Comments
 (0)