Skip to content

Commit

Permalink
up: rename some const and method name, del qoute on exec tag -l
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 31, 2022
1 parent 017466f commit 8da67b6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
8 changes: 4 additions & 4 deletions cmd/chlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func configCmd() {
cmd.StringVar(&opts.excludes, "exclude", "", "exclude commit by keywords, multi split by comma")
cmd.IntVar(&opts.tagType, "tag-type", 0, `get git tag name by tag type.
Allowed:
0 refname sort(<cyan>default</>)
1 createordate sort
0 ref-name sort(<cyan>default</>)
1 creator date sort
2 describe command;;t`)

cmd.AddArg("sha1", "The old git sha version. allow: tag name, commit id", true, nil)
Expand Down Expand Up @@ -160,8 +160,8 @@ func generate(cl *chlog.Changelog) error {
gitArgs = append(gitArgs, "--no-merges")
}

sha1 := repo.AutoMatchTagByTagType(opts.sha1, opts.tagType)
sha2 := repo.AutoMatchTagByTagType(opts.sha2, opts.tagType)
sha1 := repo.AutoMatchTagByType(opts.sha1, opts.tagType)
sha2 := repo.AutoMatchTagByType(opts.sha2, opts.tagType)
cliutil.Infof("Generate changelog: %s to %s\n", sha1, sha2)

cl.FetchGitLog(sha1, sha2, gitArgs...)
Expand Down
48 changes: 24 additions & 24 deletions repo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gitw

import (
"fmt"
"strings"

"github.com/gookit/goutil/arrutil"
Expand All @@ -18,11 +17,6 @@ const (
cacheMaxTagVersion = "maxVersion"
)

// CmdBuilder struct
// type CmdBuilder struct {
// Dir string
// }

// RepoConfig struct
type RepoConfig struct {
DefaultBranch string
Expand Down Expand Up @@ -131,20 +125,20 @@ const (
TagHead = "head"
)

// type value constants for fetch tags
// enum type value constants for fetch tags
const (
RefnameTagType int = iota
CreatordateTagType
RefNameTagType int = iota
CreatorDateTagType
DescribeTagType
)

// AutoMatchTag by given sha or tag name
func (r *Repo) AutoMatchTag(sha string) string {
return r.AutoMatchTagByTagType(sha, RefnameTagType)
return r.AutoMatchTagByType(sha, RefNameTagType)
}

// AutoMatchTagByTagType by given sha or tag name
func (r *Repo) AutoMatchTagByTagType(sha string, tagType int) string {
// AutoMatchTagByType by given sha or tag name.
func (r *Repo) AutoMatchTagByType(sha string, tagType int) string {
switch strings.ToLower(sha) {
case TagLast:
return r.LargestTagByTagType(tagType)
Expand Down Expand Up @@ -187,8 +181,8 @@ func (r *Repo) LargestTagByTagType(tagType int) string {

tags := make([]string, 0, 2)
switch tagType {
case CreatordateTagType:
tags = append(tags, r.TagsSortedByCreatordate()...)
case CreatorDateTagType:
tags = append(tags, r.TagsSortedByCreatorDate()...)
case DescribeTagType:
tags = append(tags, r.TagByDescribe(""))
default:
Expand Down Expand Up @@ -221,8 +215,8 @@ func (r *Repo) TagSecondMax() string {
func (r *Repo) TagSecondMaxByTagType(tagType int) string {
tags := make([]string, 0, 2)
switch tagType {
case CreatordateTagType:
tags = append(tags, r.TagsSortedByCreatordate()...)
case CreatorDateTagType:
tags = append(tags, r.TagsSortedByCreatorDate()...)
case DescribeTagType:
current := r.TagByDescribe("")
if len(current) != 0 {
Expand Down Expand Up @@ -251,30 +245,36 @@ func (r *Repo) TagsSortedByRefName() []string {
return OutputLines(str)
}

// TagsSortedByCreatordate get repo tags list by creatordate sort
func (r *Repo) TagsSortedByCreatordate() []string {
str, err := r.gw.Tag("-l", "--sort=-creatordate", "--format=\"%(refname:strip=2)\"").Output()
// TagsSortedByCreatorDate get repo tags list by creator date sort
func (r *Repo) TagsSortedByCreatorDate() []string {
str, err := r.gw.
Tag("-l", "--sort=-creatordate", "--format=%(refname:strip=2)").
Output()

if err != nil {
r.setErr(err)
return nil
}
return OutputLines(str)
}

// TagByDescribe get tag by describe command
func (r *Repo) TagByDescribe(current string) (str string) {
// TagByDescribe get tag by describe command. if current not empty, will exclude it.
func (r *Repo) TagByDescribe(current string) (ver string) {
var err error
if len(current) == 0 {
str, err = r.gw.Describe("--tags", "--abbrev=0").Output()
ver, err = r.gw.Describe("--tags", "--abbrev=0").Output()
} else {
str, err = r.gw.Describe("--tags", "--abbrev=0", fmt.Sprintf("tags/%s^", current)).Output()
ver, err = r.gw.
Describe("--tags", "--abbrev=0").
Argf("tags/%s^", current).
Output()
}

if err != nil {
r.setErr(err)
return ""
}
return FirstLine(str)
return FirstLine(ver)
}

// Tags get repo tags list
Expand Down
8 changes: 4 additions & 4 deletions repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func TestRepo_Info(t *testing.T) {
}

func TestRepo_AutoMatchTagByTagType(t *testing.T) {
assert.Equal(t, "HEAD", repo.AutoMatchTagByTagType("head", 0))
assert.Equal(t, "541fb9d", repo.AutoMatchTagByTagType("541fb9d", 0))
assert.Equal(t, "HEAD", repo.AutoMatchTagByType("head", 0))
assert.Equal(t, "541fb9d", repo.AutoMatchTagByType("541fb9d", 0))
}

func TestRepo_TagsSortedByCreatordate(t *testing.T) {
tags := repo.TagsSortedByCreatordate()
func TestRepo_TagsSortedByCreatorDate(t *testing.T) {
tags := repo.TagsSortedByCreatorDate()
dump.P(tags)
assert.NotEmpty(t, tags)
}
Expand Down

0 comments on commit 8da67b6

Please sign in to comment.