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

Support git shallow clones and additional ref fetches #1521

Merged
merged 1 commit into from
Aug 3, 2019
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions api/openapi-spec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@
"repo"
],
"properties": {
"depth": {
"description": "Depth specifies clones/fetches should be shallow and include the given number of commits from the branch tip",
"type": "integer",
"format": "int32"
},
"fetch": {
"description": "Fetch specifies a number of refs that should be fetched before checkout",
"type": "array",
"items": {
"type": "string"
}
},
"insecureIgnoreHostKey": {
"description": "InsecureIgnoreHostKey disables SSH strict host key checking during git clone",
"type": "boolean"
Expand Down
11 changes: 11 additions & 0 deletions examples/input-artifact-git.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ spec:
# providers (github, bitbucket, gitlab, azure) as these keys are already baked into
# the executor image which performs the clone.
# insecureIgnoreHostKey: true
#
# Shallow clones/fetches can be performed by providing a `depth`.
# depth: 1
#
# Additional ref specs to fetch down prior to checkout can be
# provided with `fetch`. This may be necessary if `revision` is a
# non-branch/-tag ref and thus not covered by git's default fetch.
# See https://git-scm.com/book/en/v2/Git-Internals-The-Refspec for
# the refspec format.
# fetch: refs/meta/*
# fetch: refs/changes/*
container:
image: golang:1.10
command: [sh, -c]
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/workflow/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/apis/workflow/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,13 @@ type GitArtifact struct {
// Revision is the git commit, tag, branch to checkout
Revision string `json:"revision,omitempty"`

// Depth specifies clones/fetches should be shallow and include the given
// number of commits from the branch tip
Depth *uint `json:"depth,omitempty"`

// Fetch specifies a number of refs that should be fetched before checkout
Fetch []string `json:"fetch,omitempty"`

// UsernameSecret is the secret selector to the repository username
UsernameSecret *apiv1.SecretKeySelector `json:"usernameSecret,omitempty"`

Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/workflow/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion workflow/artifacts/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
ssh2 "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
Expand Down Expand Up @@ -86,10 +87,39 @@ func gitClone(path string, inputArtifact *wfv1.Artifact, auth transport.AuthMeth
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Auth: auth,
}
_, err := git.PlainClone(path, false, &cloneOptions)
if inputArtifact.Git.Depth != nil {
cloneOptions.Depth = int(*inputArtifact.Git.Depth)
}

repo, err := git.PlainClone(path, false, &cloneOptions)
if err != nil {
return errors.InternalWrapError(err)
}

if inputArtifact.Git.Fetch != nil {
refSpecs := make([]config.RefSpec, len(inputArtifact.Git.Fetch))
for i, spec := range inputArtifact.Git.Fetch {
refSpecs[i] = config.RefSpec(spec)
}

fetchOptions := git.FetchOptions{
RefSpecs: refSpecs,
}
if inputArtifact.Git.Depth != nil {
fetchOptions.Depth = int(*inputArtifact.Git.Depth)
}

err = fetchOptions.Validate()
if err != nil {
return errors.InternalWrapError(err)
}

err = repo.Fetch(&fetchOptions)
if err != nil {
return errors.InternalWrapError(err)
}
}

if inputArtifact.Git.Revision != "" {
// We still rely on forking git for checkout, since go-git does not have a reliable
// way of resolving revisions (e.g. mybranch, HEAD^, v1.2.3)
Expand Down