From 3495bf5d92ef54c53d3604190d1ca4e7fff8a417 Mon Sep 17 00:00:00 2001 From: Anders Klepaker Date: Tue, 2 Jan 2024 23:15:27 +0100 Subject: [PATCH 1/3] Added Depth as a fetch option --- LibGit2Sharp/FetchOptions.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/LibGit2Sharp/FetchOptions.cs b/LibGit2Sharp/FetchOptions.cs index 5bcf74bfa..378c4ad5c 100644 --- a/LibGit2Sharp/FetchOptions.cs +++ b/LibGit2Sharp/FetchOptions.cs @@ -26,6 +26,14 @@ public sealed class FetchOptions : FetchOptionsBase /// public bool? Prune { get; set; } + /// + /// Specifies the depth of the fetch to perform. + /// + /// Default value is 0 (full) fetch. + /// + /// + public int Depth { get; set; } = 0; + /// /// Get/Set the custom headers. /// From 95ac0889545a3194de7b22b75858d49075da37c1 Mon Sep 17 00:00:00 2001 From: Anders Klepaker Date: Tue, 2 Jan 2024 23:16:58 +0100 Subject: [PATCH 2/3] Map Depth from FetchOptions to GitFetchOptions --- LibGit2Sharp/Repository.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 73f560c3c..9f305cb5c 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -780,6 +780,13 @@ public static string Clone(string sourceUrl, string workdirPath, options ??= new CloneOptions(); + // As default behaviour for GitFetchOptionsWrapper ctor is to create + // a new instance of GitFetchOptions we only populate the Depth field. + var fetchOptions = new GitFetchOptions + { + Depth = options.FetchOptions.Depth, + }; + // context variable that contains information on the repository that // we are cloning. var context = new RepositoryOperationContext(Path.GetFullPath(workdirPath), sourceUrl); @@ -794,7 +801,7 @@ public static string Clone(string sourceUrl, string workdirPath, } using (var checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options)) - using (var fetchOptionsWrapper = new GitFetchOptionsWrapper()) + using (var fetchOptionsWrapper = new GitFetchOptionsWrapper(fetchOptions)) { var gitCheckoutOptions = checkoutOptionsWrapper.Options; From 94fc78dc98bb379b8a4790f0204e23a196e26330 Mon Sep 17 00:00:00 2001 From: Anders Klepaker Date: Tue, 2 Jan 2024 23:17:16 +0100 Subject: [PATCH 3/3] Added tests for shallow cloning --- LibGit2Sharp.Tests/CloneFixture.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/LibGit2Sharp.Tests/CloneFixture.cs b/LibGit2Sharp.Tests/CloneFixture.cs index b4de8797a..bcb9544c8 100644 --- a/LibGit2Sharp.Tests/CloneFixture.cs +++ b/LibGit2Sharp.Tests/CloneFixture.cs @@ -35,6 +35,34 @@ public void CanClone(string url) } } + [Theory] + [InlineData("https://github.com/libgit2/TestGitRepository",1)] + [InlineData("https://github.com/libgit2/TestGitRepository",5)] + [InlineData("https://github.com/libgit2/TestGitRepository",7)] + public void CanCloneShallow(string url, int depth) + { + var scd = BuildSelfCleaningDirectory(); + + var clonedRepoPath = Repository.Clone(url, scd.DirectoryPath, new CloneOptions + { + FetchOptions = + { + Depth = depth, + }, + }); + + using (var repo = new Repository(clonedRepoPath)) + { + var commitsFirstParentOnly = repo.Commits.QueryBy(new CommitFilter + { + FirstParentOnly = true, + }); + + Assert.Equal(depth, commitsFirstParentOnly.Count()); + Assert.Equal("49322bb17d3acc9146f98c97d078513228bbf3c0", repo.Head.Tip.Id.ToString()); + } + } + [Theory] [InlineData("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f")] [InlineData("packed", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9")]