Skip to content

Commit

Permalink
Adds Depth to FetchOptions allowing for shallow cloning (#2070)
Browse files Browse the repository at this point in the history
* Added Depth as a fetch option

* Map Depth from FetchOptions to GitFetchOptions

* Added tests for shallow cloning
  • Loading branch information
andersklepaker authored Nov 23, 2024
1 parent 5162c68 commit 47b2ee0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
28 changes: 28 additions & 0 deletions LibGit2Sharp.Tests/CloneFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
8 changes: 8 additions & 0 deletions LibGit2Sharp/FetchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public sealed class FetchOptions : FetchOptionsBase
/// </summary>
public bool? Prune { get; set; }

/// <summary>
/// Specifies the depth of the fetch to perform.
/// <para>
/// Default value is 0 (full) fetch.
/// </para>
/// </summary>
public int Depth { get; set; } = 0;

/// <summary>
/// Get/Set the custom headers.
///
Expand Down
9 changes: 8 additions & 1 deletion LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down

0 comments on commit 47b2ee0

Please sign in to comment.