From 0073103e650bd8a4b6ebcf64ff1683709e2e4239 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 31 Oct 2018 23:57:53 +0700 Subject: lib/git: add function to fetch and list tags --- lib/git/git.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/git/git_test.go | 24 +++++++++++++++++++++ 2 files changed, 86 insertions(+) (limited to 'lib/git') diff --git a/lib/git/git.go b/lib/git/git.go index a77d88ee..c1e1c9de 100644 --- a/lib/git/git.go +++ b/lib/git/git.go @@ -161,6 +161,31 @@ func FetchAll(repoDir string) error { return err } +// +// FetchTags will fetch all tags from remote. +// +func FetchTags(repoDir string) error { + cmd := exec.Command("git", "fetch") + if debug.Value == 0 { + cmd.Args = append(cmd.Args, "--quiet") + } + cmd.Args = append(cmd.Args, "--tags") + cmd.Dir = repoDir + cmd.Stdout = _stdout + cmd.Stderr = _stderr + + if debug.Value >= 1 { + fmt.Printf("= FetchTags %s %s\n", cmd.Dir, cmd.Args) + } + + err := cmd.Run() + if err != nil { + err = fmt.Errorf("FetchTags: %s", err) + } + + return err +} + // // GetRemoteURL return remote URL or error if repository is not git or url is // empty. @@ -308,6 +333,43 @@ func LatestVersion(repoDir string) (version string, err error) { return } +// +// ListTags get all tags from repository. +// +func ListTags(repoDir string) (tags []string, err error) { + err = FetchTags(repoDir) + if err != nil { + return + } + + cmd := exec.Command("git") + cmd.Args = append(cmd.Args, "tag", "--list") + cmd.Dir = repoDir + cmd.Stderr = _stderr + + if debug.Value >= 1 { + fmt.Printf("= ListTags %s %s\n", cmd.Dir, cmd.Args) + } + + bout, err := cmd.Output() + if err != nil { + err = fmt.Errorf("ListTag: %s", err) + return + } + + sep := []byte{'\n'} + btags := bytes.Split(bout, sep) + + for x := 0; x < len(btags); x++ { + if len(btags[x]) == 0 { + continue + } + tags = append(tags, string(btags[x])) + } + + return +} + // // LogRevisions get commits between two revisions. // diff --git a/lib/git/git_test.go b/lib/git/git_test.go index e52d18b5..390c24f8 100644 --- a/lib/git/git_test.go +++ b/lib/git/git_test.go @@ -236,6 +236,30 @@ func TestLatestVersion(t *testing.T) { } } +func TestListTag(t *testing.T) { + cases := []struct { + desc string + exp []string + expErr string + }{{ + desc: "With default repo", + exp: []string{"v0.1.0", "v0.2.0"}, + }} + + for _, c := range cases { + t.Log(c.desc) + mock.Reset(true) + + got, err := ListTags(_testRepoDir) + if err != nil { + test.Assert(t, "err", c.expErr, err.Error(), true) + continue + } + + test.Assert(t, "tags", c.exp, got, true) + } +} + func TestLogRevisions(t *testing.T) { cases := []struct { desc string -- cgit v1.3