Skip to content

Commit b1d05ff

Browse files
committed
✨ feat: add more git util func and update the gh ci config
1 parent e225152 commit b1d05ff

File tree

4 files changed

+66
-11
lines changed

4 files changed

+66
-11
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ jobs:
2727
with:
2828
fetch-depth: 0
2929

30+
- run: git fetch --force --tags
31+
3032
- name: Display Env
3133
run: |
3234
git remote -v

.github/workflows/release.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ jobs:
1010
name: Release new version
1111
runs-on: ubuntu-latest
1212
timeout-minutes: 5
13-
strategy:
14-
fail-fast: true
1513

1614
steps:
1715
- name: Checkout

gitutil/gitutil.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,42 @@ func IsFullURL(s string) bool {
4949
}
5050
return false
5151
}
52+
53+
// FormatVersion string. eg: v1.2.0 -> 1.2.0
54+
func FormatVersion(ver string) (string, bool) {
55+
ver = strings.TrimLeft(ver, "vV")
56+
if strutil.IsVersion(ver) {
57+
return ver, true
58+
}
59+
return "", false
60+
}
61+
62+
// IsValidVersion check
63+
func IsValidVersion(ver string) bool {
64+
ver = strings.TrimLeft(ver, "vV")
65+
return strutil.IsVersion(ver)
66+
}
67+
68+
// NextVersion build. eg: v1.2.0 -> v1.2.1
69+
func NextVersion(ver string) string {
70+
if len(ver) == 0 {
71+
return "v0.0.1"
72+
}
73+
74+
ver = strings.TrimLeft(ver, "vV")
75+
nodes := strings.Split(ver, ".")
76+
if len(nodes) == 1 {
77+
return ver + ".0.1"
78+
}
79+
80+
for i := len(nodes) - 1; i > 0; i-- {
81+
num, err := strutil.ToInt(nodes[i])
82+
if err != nil {
83+
continue
84+
}
85+
nodes[i] = strutil.SafeString(num + 1)
86+
break
87+
}
88+
89+
return strings.Join(nodes, ".")
90+
}

repo.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ func (r *Repo) PrintCmdOnExec() *Repo {
9999
return r
100100
}
101101

102+
// SetDryRun settings.
103+
func (r *Repo) SetDryRun(dr bool) *Repo {
104+
r.gw.DryRun = dr
105+
return r
106+
}
107+
102108
// Init run git init for the repo dir.
103109
func (r *Repo) Init() error {
104110
return r.gw.Init().Run()
@@ -431,21 +437,26 @@ func (r *Repo) CurBranchName() string {
431437
return brName
432438
}
433439

434-
// cat .git/HEAD
440+
// cat .git/HEAD
435441
// OR
436-
// git symbolic-ref HEAD // out: refs/heads/fea_pref
437-
// git symbolic-ref --short -q HEAD // on checkout tag, run will error
438-
// git rev-parse --abbrev-ref -q HEAD
439-
str, err := r.gw.RevParse("--abbrev-ref", "-q", "HEAD").Output()
440-
if err != nil {
441-
r.setErr(err)
442-
return ""
442+
// git branch --show-current // on high version git
443+
// OR
444+
// git symbolic-ref HEAD // out: refs/heads/fea_pref
445+
// git symbolic-ref --short -q HEAD // on checkout tag, run will error
446+
// Or
447+
// git rev-parse --abbrev-ref -q HEAD // on init project, will error
448+
449+
str := r.gw.Branch("--show-current").SafeOutput()
450+
if len(str) == 0 {
451+
str, r.err = r.gw.RevParse("--abbrev-ref", "-q", "HEAD").Output()
452+
if r.err != nil {
453+
return ""
454+
}
443455
}
444456

445457
// eg: fea_pref
446458
brName = cmdr.FirstLine(str)
447459
r.cache.Set(cacheCurrentBranch, brName)
448-
449460
return brName
450461
}
451462

@@ -690,3 +701,8 @@ func (r *Repo) Git() *GitWrap {
690701
func (r *Repo) Cmd(name string, args ...string) *GitWrap {
691702
return r.gw.Cmd(name, args...)
692703
}
704+
705+
// QuickRun git command
706+
func (r *Repo) QuickRun(cmd string, args ...string) error {
707+
return r.gw.Cmd(cmd, args...).Run()
708+
}

0 commit comments

Comments
 (0)