Skip to content

Commit

Permalink
default fetch depth to 1 if none provided
Browse files Browse the repository at this point in the history
  • Loading branch information
dtanner committed Dec 6, 2021
1 parent faafd58 commit 5db6f62
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 52 deletions.
44 changes: 13 additions & 31 deletions cmd/vela-git/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...,
Expand Down
16 changes: 8 additions & 8 deletions cmd/vela-git/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/vela-git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 4 additions & 13 deletions cmd/vela-git/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5db6f62

Please sign in to comment.