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

Turn on Fetch By Commit For GitHub #3127

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions src/Agent.Plugins/GitSourceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ namespace Agent.Plugins.Repository
{
public class ExternalGitSourceProvider : GitSourceProvider
{
public override bool GitSupportsFetchingCommitBySha1Hash
public override bool GitSupportsFetchingCommitBySha1Hash(GitCliManager gitCommandManager)
{
get
{
return false;
}
return false;
}

// external git repository won't use auth header cmdline arg, since we don't know the auth scheme.
Expand Down Expand Up @@ -78,34 +75,31 @@ public override void RequirementCheck(AgentTaskPluginExecutionContext executionC

public class BitbucketGitSourceProvider : AuthenticatedGitSourceProvider
{
public override bool GitSupportsFetchingCommitBySha1Hash
public override bool GitSupportsFetchingCommitBySha1Hash(GitCliManager gitCommandManager)
{
get
{
return true;
}
return true;
}
}

public class GitHubSourceProvider : AuthenticatedGitSourceProvider
{
public override bool GitSupportsFetchingCommitBySha1Hash
public override bool GitSupportsFetchingCommitBySha1Hash(GitCliManager gitCommandManager)
{
get
if (gitCommandManager.EnsureGitVersion(_minGitVersionDefaultV2, throwOnNotMatch: false))
{
return false;

return true;
}

return false;
}
}

public class TfsGitSourceProvider : GitSourceProvider
{
public override bool GitSupportsFetchingCommitBySha1Hash
public override bool GitSupportsFetchingCommitBySha1Hash(GitCliManager gitCommandManager)
{
get
{
return true;
}
return true;
}

public override bool UseBearerAuthenticationForOAuth()
Expand Down Expand Up @@ -186,11 +180,13 @@ public abstract class GitSourceProvider : ISourceProvider
// min git-lfs version that support add extra auth header.
protected Version _minGitLfsVersionSupportAuthHeader = new Version(2, 1);

// min git version where v2 is defaulted
protected Version _minGitVersionDefaultV2 = new Version(2, 26);

public abstract bool GitSupportUseAuthHeader(AgentTaskPluginExecutionContext executionContext, GitCliManager gitCommandManager);
public abstract bool GitLfsSupportUseAuthHeader(AgentTaskPluginExecutionContext executionContext, GitCliManager gitCommandManager);
public abstract void RequirementCheck(AgentTaskPluginExecutionContext executionContext, Pipelines.RepositoryResource repository, GitCliManager gitCommandManager);

public abstract bool GitSupportsFetchingCommitBySha1Hash { get; }
public abstract bool GitSupportsFetchingCommitBySha1Hash(GitCliManager gitCommandManager);

public virtual bool UseBearerAuthenticationForOAuth()
{
Expand Down Expand Up @@ -317,9 +313,6 @@ public string GenerateAuthHeader(AgentTaskPluginExecutionContext executionContex

bool exposeCred = StringUtil.ConvertToBoolean(executionContext.GetInput(Pipelines.PipelineConstants.CheckoutTaskInputs.PersistCredentials));

// Read 'disable fetch by commit' value from the execution variable first, then from the environment variable if the first one is not set
bool fetchByCommit = GitSupportsFetchingCommitBySha1Hash && !AgentKnobs.DisableFetchByCommit.GetValue(executionContext).AsBoolean();

executionContext.Debug($"repository url={repositoryUrl}");
executionContext.Debug($"targetPath={targetPath}");
executionContext.Debug($"sourceBranch={sourceBranch}");
Expand Down Expand Up @@ -374,6 +367,9 @@ public string GenerateAuthHeader(AgentTaskPluginExecutionContext executionContex
GitCliManager gitCommandManager = GetCliManager(gitEnv);
await gitCommandManager.LoadGitExecutionInfo(executionContext, useBuiltInGit: !preferGitFromPath);

// Read 'disable fetch by commit' value from the execution variable first, then from the environment variable if the first one is not set
bool fetchByCommit = GitSupportsFetchingCommitBySha1Hash(gitCommandManager) && !AgentKnobs.DisableFetchByCommit.GetValue(executionContext).AsBoolean();

bool gitSupportAuthHeader = GitSupportUseAuthHeader(executionContext, gitCommandManager);

// Make sure the build machine met all requirements for the git repository
Expand Down Expand Up @@ -849,6 +845,19 @@ public string GenerateAuthHeader(AgentTaskPluginExecutionContext executionContex
throw new InvalidOperationException($"Git fetch failed with exit code: {exitCode_fetch}");
}

// If checking out by commit, explicity fetch it
// This is done as a separate fetch rather than adding an additional refspec on the proceeding fetch to prevent overriding previous behavior which may have dependencies in other tasks
// i.e. "git fetch origin" versus "git fetch origin commit"
if (fetchByCommit && !string.IsNullOrEmpty(sourceVersion))
{
List<string> commitFetchSpecs = new List<string>() { $"+{sourceVersion}:{_remoteRefsPrefix}{sourceVersion}" };
exitCode_fetch = await gitCommandManager.GitFetch(executionContext, targetPath, "origin", fetchDepth, commitFetchSpecs, string.Join(" ", additionalFetchArgs), cancellationToken);
if (exitCode_fetch != 0)
{
throw new InvalidOperationException($"Git fetch failed with exit code: {exitCode_fetch}");
}
}

// Checkout
// sourceToBuild is used for checkout
// if sourceBranch is a PR branch or sourceVersion is null, make sure branch name is a remote branch. we need checkout to detached head.
Expand Down