diff --git a/LibGit2Sharp.Tests/CloneFixture.cs b/LibGit2Sharp.Tests/CloneFixture.cs index f205eddc2..9a4cfbb37 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")] 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. /// diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index fc777404e..19fd58668 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;