From 5db6f627f30cde54ed3c4f2950ef8f5ac54a99bc Mon Sep 17 00:00:00 2001 From: tanner Date: Wed, 1 Dec 2021 11:36:26 -0600 Subject: [PATCH] default fetch depth to 1 if none provided --- cmd/vela-git/command.go | 44 +++++++++++------------------------- cmd/vela-git/command_test.go | 16 ++++++------- cmd/vela-git/main.go | 1 + cmd/vela-git/plugin.go | 17 ++++---------- 4 files changed, 26 insertions(+), 52 deletions(-) diff --git a/cmd/vela-git/command.go b/cmd/vela-git/command.go index cf3419d..2bdba6c 100644 --- a/cmd/vela-git/command.go +++ b/cmd/vela-git/command.go @@ -29,46 +29,28 @@ func execCmd(e *exec.Cmd) error { return e.Run() } -// fetchTagsCmd is a helper function to +// fetchCmd is a helper function to // download all objects, including tags, // from the ref for a git repo. -func fetchTagsCmd(ref string, depth string) *exec.Cmd { - logrus.Trace("returning fetchTagsCmd") +func fetchCmd(ref string, includeTags bool, depth string) *exec.Cmd { + logrus.Trace("returning fetchCmd") - args := []string{ - "fetch", - "--tags", - "origin", - ref, - } + args := []string{"fetch"} - if depth != "" { - args = append(args, []string{"--depth", depth}...) - } - - return exec.Command( - "git", - args..., - ) -} - -// fetchNoTagsCmd is a helper function to -// download all objects, excluding tags, -// from the ref for a git repo. -func fetchNoTagsCmd(ref string, depth string) *exec.Cmd { - logrus.Trace("returning fetchNoTagsCmd") - - args := []string{ - "fetch", - "--no-tags", - "origin", - ref, + if includeTags { + args = append(args, "--tags") + } else { + args = append(args, "--no-tags") } if depth != "" { - args = append(args, []string{"--depth", depth}...) + args = append(args, "--depth", depth) + } else { + args = append(args, "--depth", "1") } + args = append(args, "origin", ref) + return exec.Command( "git", args..., diff --git a/cmd/vela-git/command_test.go b/cmd/vela-git/command_test.go index 0b1e8f2..ad267a3 100644 --- a/cmd/vela-git/command_test.go +++ b/cmd/vela-git/command_test.go @@ -20,38 +20,38 @@ func TestGit_execCmd(t *testing.T) { } } -func TestGit_fetchTagsCmd(t *testing.T) { +func TestGit_fetchCmdWithTags(t *testing.T) { // setup types want := exec.Command( "git", "fetch", "--tags", - "origin", - "refs/heads/master", "--depth", "10", + "origin", + "refs/heads/master", ) - got := fetchTagsCmd("refs/heads/master", "10") + got := fetchCmd("refs/heads/master", true, "10") if !reflect.DeepEqual(got, want) { t.Errorf("fetchTagsCmd is %v, want %v", got, want) } } -func TestGit_fetchNoTagsCmd(t *testing.T) { +func TestGit_fetchCmdNoTags(t *testing.T) { // setup types want := exec.Command( "git", "fetch", "--no-tags", + "--depth", + "1", "origin", "refs/heads/master", - "--depth", - "10", ) - got := fetchNoTagsCmd("refs/heads/master", "10") + got := fetchCmd("refs/heads/master", false, "") if !reflect.DeepEqual(got, want) { t.Errorf("fetchNoTagsCmd is %v, want %v", got, want) diff --git a/cmd/vela-git/main.go b/cmd/vela-git/main.go index 9bdebf7..e8f63e5 100644 --- a/cmd/vela-git/main.go +++ b/cmd/vela-git/main.go @@ -92,6 +92,7 @@ func main() { FilePath: "/vela/parameters/git/depth,/vela/secrets/git/depth", Name: "build.depth", Usage: "enables fetching the repository with the specified depth", + Value: "1", }, // Netrc Flags diff --git a/cmd/vela-git/plugin.go b/cmd/vela-git/plugin.go index 4343e8c..8f5dd91 100644 --- a/cmd/vela-git/plugin.go +++ b/cmd/vela-git/plugin.go @@ -73,19 +73,10 @@ func (p *Plugin) Exec() error { return err } - // check if repo tags are enabled - if p.Repo.Tags { - // fetch repo state with tags - err = execCmd(fetchTagsCmd(p.Build.Ref, p.Build.Depth)) - if err != nil { - return err - } - } else { - // fetch repo state without tags - err = execCmd(fetchNoTagsCmd(p.Build.Ref, p.Build.Depth)) - if err != nil { - return err - } + // fetch the repo + err = execCmd(fetchCmd(p.Build.Ref, p.Repo.Tags, p.Build.Depth)) + if err != nil { + return err } // hard reset current state to build commit