Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Depth to FetchOptions allowing for shallow cloning #2070

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be unnecessary


/// <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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FetchOptions could be null at this point - possible NRE

};

// 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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need another test for this extra options : what if it is null and being passed to the ctor?

Also, how about using using var ... (we might want to remove the brackets)

{
var gitCheckoutOptions = checkoutOptionsWrapper.Options;

Expand Down