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

Fix git-init in case master is not the default branch #2835

Merged
merged 1 commit into from
Jun 24, 2020
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
2 changes: 1 addition & 1 deletion docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ Params that can be added are the following:
is used. [git checkout][git-checkout] is used to switch to the
revision, and will result in a detached HEAD in most cases. Use refspec
along with revision if you want to checkout a particular branch without a
detached HEAD. _If no revision is specified, the resource will default to `master`._
detached HEAD. _If no revision is specified, the resource inspects remote repository to determine the correct default branch._
1. `refspec`: (Optional) specify a git [refspec][git-refspec] to pass to git-fetch.
Note that if this field is specified, it must specify all refs, branches, tags,
or commits required to checkout the specified `revision`. An additional fetch
Expand Down
10 changes: 5 additions & 5 deletions pkg/apis/resource/v1alpha1/git/git_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ func NewResource(name, gitImage string, r *resource.PipelineResource) (*Resource
gitResource.NOProxy = param.Value
}
}
// default revision to master if nothing is provided
if gitResource.Revision == "" {
gitResource.Revision = "master"
}

return &gitResource, nil
}

Expand Down Expand Up @@ -149,10 +146,13 @@ func (s *Resource) Replacements() map[string]string {
func (s *Resource) GetInputTaskModifier(_ *v1beta1.TaskSpec, path string) (v1beta1.TaskModifier, error) {
args := []string{
"-url", s.URL,
"-revision", s.Revision,
"-path", path,
}

if s.Revision != "" {
args = append(args, "-revision", s.Revision)
}

if s.Refspec != "" {
args = append(args, "-refspec", s.Refspec)
}
Expand Down
74 changes: 55 additions & 19 deletions pkg/apis/resource/v1alpha1/git/git_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestNewGitResource_Valid(t *testing.T) {
Name: "test-resource",
Type: resourcev1alpha1.PipelineResourceTypeGit,
URL: "[email protected]:test/test.git",
Revision: "master",
Revision: "",
Refspec: "",
GitImage: "override-with-git:latest",
Submodules: true,
Expand All @@ -96,7 +96,7 @@ func TestNewGitResource_Valid(t *testing.T) {
Name: "test-resource",
Type: resourcev1alpha1.PipelineResourceTypeGit,
URL: "[email protected]:test/test.git",
Revision: "master",
Revision: "",
Refspec: "refs/changes/22/222134",
GitImage: "override-with-git:latest",
Submodules: true,
Expand All @@ -117,7 +117,7 @@ func TestNewGitResource_Valid(t *testing.T) {
Name: "test-resource",
Type: resourcev1alpha1.PipelineResourceTypeGit,
URL: "[email protected]:test/test.git",
Revision: "master",
Revision: "",
Refspec: "",
GitImage: "override-with-git:latest",
Submodules: true,
Expand Down Expand Up @@ -394,10 +394,10 @@ func TestGitResource_GetDownloadTaskModifier(t *testing.T) {
Args: []string{
"-url",
"[email protected]:test/test.git",
"-revision",
"master",
"-path",
"/test/test",
"-revision",
"master",
},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
Expand Down Expand Up @@ -431,10 +431,10 @@ func TestGitResource_GetDownloadTaskModifier(t *testing.T) {
Args: []string{
"-url",
"[email protected]:test/test.git",
"-revision",
"master",
"-path",
"/test/test",
"-revision",
"master",
"-submodules=false",
},
WorkingDir: "/workspace",
Expand Down Expand Up @@ -469,10 +469,10 @@ func TestGitResource_GetDownloadTaskModifier(t *testing.T) {
Args: []string{
"-url",
"[email protected]:test/test.git",
"-revision",
"master",
"-path",
"/test/test",
"-revision",
"master",
"-depth",
"8",
},
Expand Down Expand Up @@ -508,10 +508,10 @@ func TestGitResource_GetDownloadTaskModifier(t *testing.T) {
Args: []string{
"-url",
"[email protected]:test/test.git",
"-revision",
"master",
"-path",
"/test/test",
"-revision",
"master",
"-submodules=false",
"-sslVerify=false",
},
Expand Down Expand Up @@ -546,10 +546,10 @@ func TestGitResource_GetDownloadTaskModifier(t *testing.T) {
Args: []string{
"-url",
"[email protected]:test/test.git",
"-revision",
"master",
"-path",
"/test/test",
"-revision",
"master",
"-submodules=false",
"-sslVerify=false",
},
Expand Down Expand Up @@ -583,10 +583,10 @@ func TestGitResource_GetDownloadTaskModifier(t *testing.T) {
Args: []string{
"-url",
"[email protected]:test/test.git",
"-revision",
"master",
"-path",
"/test/test",
"-revision",
"master",
"-submodules=false",
"-sslVerify=false",
},
Expand Down Expand Up @@ -620,10 +620,10 @@ func TestGitResource_GetDownloadTaskModifier(t *testing.T) {
Args: []string{
"-url",
"[email protected]:test/test.git",
"-revision",
"master",
"-path",
"/test/test",
"-revision",
"master",
"-submodules=false",
"-sslVerify=false",
},
Expand Down Expand Up @@ -658,10 +658,10 @@ func TestGitResource_GetDownloadTaskModifier(t *testing.T) {
Args: []string{
"-url",
"[email protected]:test/test.git",
"-revision",
"master",
"-path",
"/test/test",
"-revision",
"master",
"-refspec",
"refs/tags/v1.0:refs/tags/v1.0 refs/heads/master:refs/heads/master",
"-submodules=false",
Expand All @@ -675,6 +675,42 @@ func TestGitResource_GetDownloadTaskModifier(t *testing.T) {
{Name: "NO_PROXY", Value: "no-proxy.git.com"},
},
},
}, {
desc: "Without Refspec and without revision",
gitResource: &git.Resource{
Name: "git-resource",
Type: resourcev1alpha1.PipelineResourceTypeGit,
URL: "[email protected]:test/test.git",
Revision: "",
Refspec: "",
GitImage: "override-with-git:latest",
Submodules: false,
Depth: 1,
SSLVerify: true,
HTTPProxy: "http-proxy.git.com",
HTTPSProxy: "https-proxy.git.com",
NOProxy: "no-proxy.git.com",
},
want: corev1.Container{
Name: "git-source-git-resource-twkr2",
Image: "override-with-git:latest",
Command: []string{"/ko-app/git-init"},
Args: []string{
"-url",
"[email protected]:test/test.git",
"-path",
"/test/test",
"-submodules=false",
},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
{Name: "TEKTON_RESOURCE_NAME", Value: "git-resource"},
{Name: "HOME", Value: pipeline.HomeDir},
{Name: "HTTP_PROXY", Value: "http-proxy.git.com"},
{Name: "HTTPS_PROXY", Value: "https-proxy.git.com"},
{Name: "NO_PROXY", Value: "no-proxy.git.com"},
},
},
}} {
t.Run(tc.desc, func(t *testing.T) {
ts := v1beta1.TaskSpec{}
Expand Down
9 changes: 6 additions & 3 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ func Fetch(logger *zap.SugaredLogger, spec FetchSpec) error {
return err
}

if spec.Revision == "" {
spec.Revision = "master"
}
if spec.Path != "" {
if _, err := run(logger, "", "init", spec.Path); err != nil {
return err
Expand All @@ -85,6 +82,12 @@ func Fetch(logger *zap.SugaredLogger, spec FetchSpec) error {
logger.Warnf("Failed to set http.sslVerify in git config: %s", err)
return err
}
if spec.Revision == "" {
spec.Revision = "HEAD"
if _, err := run(logger, "", "symbolic-ref", spec.Revision, "refs/remotes/origin/HEAD"); err != nil {
return err
}
}

fetchArgs := []string{"fetch"}
if spec.Submodules {
Expand Down
16 changes: 8 additions & 8 deletions pkg/reconciler/taskrun/resources/input_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func TestAddInputResourceToTask(t *testing.T) {
Name: "git-source-the-git-9l9zj",
Image: "override-with-git:latest",
Command: []string{"/ko-app/git-init"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "master", "-path", "/workspace/gitspace"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-path", "/workspace/gitspace"},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
{Name: "TEKTON_RESOURCE_NAME", Value: "the-git"},
Expand Down Expand Up @@ -410,7 +410,7 @@ func TestAddInputResourceToTask(t *testing.T) {
Name: "git-source-the-git-with-branch-9l9zj",
Image: "override-with-git:latest",
Command: []string{"/ko-app/git-init"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "branch", "-path", "/workspace/gitspace"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-path", "/workspace/gitspace", "-revision", "branch"},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
{Name: "TEKTON_RESOURCE_NAME", Value: "the-git-with-branch"},
Expand Down Expand Up @@ -458,7 +458,7 @@ func TestAddInputResourceToTask(t *testing.T) {
Name: "git-source-the-git-with-branch-mz4c7",
Image: "override-with-git:latest",
Command: []string{"/ko-app/git-init"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "branch", "-path", "/workspace/gitspace"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-path", "/workspace/gitspace", "-revision", "branch"},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
{Name: "TEKTON_RESOURCE_NAME", Value: "the-git-with-branch"},
Expand All @@ -468,7 +468,7 @@ func TestAddInputResourceToTask(t *testing.T) {
Name: "git-source-the-git-with-branch-9l9zj",
Image: "override-with-git:latest",
Command: []string{"/ko-app/git-init"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "branch", "-path", "/workspace/git-duplicate-space"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-path", "/workspace/git-duplicate-space", "-revision", "branch"},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
{Name: "TEKTON_RESOURCE_NAME", Value: "the-git-with-branch"},
Expand Down Expand Up @@ -509,7 +509,7 @@ func TestAddInputResourceToTask(t *testing.T) {
Name: "git-source-the-git-9l9zj",
Image: "override-with-git:latest",
Command: []string{"/ko-app/git-init"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "master", "-path", "/workspace/gitspace"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-path", "/workspace/gitspace"},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
{Name: "TEKTON_RESOURCE_NAME", Value: "the-git"},
Expand Down Expand Up @@ -550,7 +550,7 @@ func TestAddInputResourceToTask(t *testing.T) {
Name: "git-source-the-git-with-branch-9l9zj",
Image: "override-with-git:latest",
Command: []string{"/ko-app/git-init"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "branch", "-path", "/workspace/gitspace"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-path", "/workspace/gitspace", "-revision", "branch"},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
{Name: "TEKTON_RESOURCE_NAME", Value: "the-git-with-branch"},
Expand Down Expand Up @@ -639,7 +639,7 @@ func TestAddInputResourceToTask(t *testing.T) {
Name: "git-source-the-git-with-sslVerify-false-9l9zj",
Image: "override-with-git:latest",
Command: []string{"/ko-app/git-init"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "branch", "-path", "/workspace/gitspace", "-sslVerify=false"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-path", "/workspace/gitspace", "-revision", "branch", "-sslVerify=false"},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
{Name: "TEKTON_RESOURCE_NAME", Value: "the-git-with-sslVerify-false"},
Expand Down Expand Up @@ -964,7 +964,7 @@ gsutil cp gs://fake-bucket/rules.zip /workspace/gcs-dir
Name: "git-source-the-git-with-branch-9l9zj",
Image: "override-with-git:latest",
Command: []string{"/ko-app/git-init"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "branch", "-path", "/workspace/gitspace"},
Args: []string{"-url", "https://github.com/grafeas/kritis", "-path", "/workspace/gitspace", "-revision", "branch"},
WorkingDir: "/workspace",
Env: []corev1.EnvVar{
{Name: "TEKTON_RESOURCE_NAME", Value: "the-git-with-branch"},
Expand Down
10 changes: 4 additions & 6 deletions pkg/reconciler/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ func TestReconcile(t *testing.T) {
tb.Command(entrypointLocation),
tb.Args("-wait_file", "/tekton/tools/0", "-post_file", "/tekton/tools/1", "-termination_path",
"/tekton/termination", "-entrypoint", "/ko-app/git-init", "--", "-url", "https://foo.git",
"-revision", "master", "-path", "/workspace/workspace"),
"-path", "/workspace/workspace"),
tb.WorkingDir(workspaceDir),
tb.EnvVar("HOME", "/tekton/home"),
tb.EnvVar("TEKTON_RESOURCE_NAME", "workspace"),
Expand Down Expand Up @@ -1023,8 +1023,6 @@ func TestReconcile(t *testing.T) {
"--",
"-url",
"https://foo.git",
"-revision",
"master",
"-path",
"/workspace/workspace",
),
Expand Down Expand Up @@ -1132,10 +1130,10 @@ func TestReconcile(t *testing.T) {
"--",
"-url",
"github.com/foo/bar.git",
"-revision",
"rel-can",
"-path",
"/workspace/workspace"),
"/workspace/workspace",
"-revision",
"rel-can"),
tb.WorkingDir(workspaceDir),
// Note: the duplication of HOME env var here is intentional: our pod builder
// adds it first and the git pipelineresource adds its own to ensure that HOME
Expand Down
Loading