Skip to content

Commit

Permalink
Tweak shallow cloning implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bording committed Nov 23, 2024
1 parent 47b2ee0 commit 19236db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
2 changes: 1 addition & 1 deletion LibGit2Sharp/FetchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class FetchOptions : FetchOptionsBase
/// <summary>
/// Specifies the depth of the fetch to perform.
/// <para>
/// Default value is 0 (full) fetch.
/// Default value is 0 (full fetch).
/// </para>
/// </summary>
public int Depth { get; set; } = 0;
Expand Down
27 changes: 8 additions & 19 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -772,46 +772,38 @@ public static string Clone(string sourceUrl, string workdirPath)
/// <param name="workdirPath">Local path to clone into</param>
/// <param name="options"><see cref="CloneOptions"/> controlling clone behavior</param>
/// <returns>The path to the created repository.</returns>
public static string Clone(string sourceUrl, string workdirPath,
CloneOptions options)
public static string Clone(string sourceUrl, string workdirPath, CloneOptions options)
{
Ensure.ArgumentNotNull(sourceUrl, "sourceUrl");
Ensure.ArgumentNotNull(workdirPath, "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);

// Notify caller that we are starting to work with the current repository.
bool continueOperation = OnRepositoryOperationStarting(options.FetchOptions.RepositoryOperationStarting,
context);
bool continueOperation = OnRepositoryOperationStarting(options.FetchOptions.RepositoryOperationStarting, context);

if (!continueOperation)
{
throw new UserCancelledException("Clone cancelled by the user.");
}

using (var checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
using (var fetchOptionsWrapper = new GitFetchOptionsWrapper(fetchOptions))
using (var fetchOptionsWrapper = new GitFetchOptionsWrapper())
{
var gitCheckoutOptions = checkoutOptionsWrapper.Options;

var gitFetchOptions = fetchOptionsWrapper.Options;
gitFetchOptions.Depth = options.FetchOptions.Depth;
gitFetchOptions.ProxyOptions = options.FetchOptions.ProxyOptions.CreateGitProxyOptions();
gitFetchOptions.RemoteCallbacks = new RemoteCallbacks(options.FetchOptions).GenerateCallbacks();

if (options.FetchOptions != null && options.FetchOptions.CustomHeaders != null)
{
gitFetchOptions.CustomHeaders =
GitStrArrayManaged.BuildFrom(options.FetchOptions.CustomHeaders);
gitFetchOptions.CustomHeaders = GitStrArrayManaged.BuildFrom(options.FetchOptions.CustomHeaders);
}

var cloneOpts = new GitCloneOptions
Expand Down Expand Up @@ -839,8 +831,7 @@ public static string Clone(string sourceUrl, string workdirPath,
}

// Notify caller that we are done with the current repository.
OnRepositoryOperationCompleted(options.FetchOptions.RepositoryOperationCompleted,
context);
OnRepositoryOperationCompleted(options.FetchOptions.RepositoryOperationCompleted, context);

// Recursively clone submodules if requested.
try
Expand All @@ -849,9 +840,7 @@ public static string Clone(string sourceUrl, string workdirPath,
}
catch (Exception ex)
{
throw new RecurseSubmodulesException("The top level repository was cloned, but there was an error cloning its submodules.",
ex,
clonedRepoPath);
throw new RecurseSubmodulesException("The top level repository was cloned, but there was an error cloning its submodules.", ex, clonedRepoPath);
}

return clonedRepoPath;
Expand Down

0 comments on commit 19236db

Please sign in to comment.