Skip to content

Commit

Permalink
✨ feat: add more git util func and update the gh ci config
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 9, 2023
1 parent e225152 commit b1d05ff
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
with:
fetch-depth: 0

- run: git fetch --force --tags

- name: Display Env
run: |
git remote -v
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ jobs:
name: Release new version
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
fail-fast: true

steps:
- name: Checkout
Expand Down
39 changes: 39 additions & 0 deletions gitutil/gitutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,42 @@ func IsFullURL(s string) bool {
}
return false
}

// FormatVersion string. eg: v1.2.0 -> 1.2.0
func FormatVersion(ver string) (string, bool) {
ver = strings.TrimLeft(ver, "vV")
if strutil.IsVersion(ver) {
return ver, true
}
return "", false
}

// IsValidVersion check
func IsValidVersion(ver string) bool {
ver = strings.TrimLeft(ver, "vV")
return strutil.IsVersion(ver)
}

// NextVersion build. eg: v1.2.0 -> v1.2.1
func NextVersion(ver string) string {
if len(ver) == 0 {
return "v0.0.1"
}

ver = strings.TrimLeft(ver, "vV")
nodes := strings.Split(ver, ".")
if len(nodes) == 1 {
return ver + ".0.1"
}

for i := len(nodes) - 1; i > 0; i-- {
num, err := strutil.ToInt(nodes[i])
if err != nil {
continue
}
nodes[i] = strutil.SafeString(num + 1)
break
}

return strings.Join(nodes, ".")
}
34 changes: 25 additions & 9 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func (r *Repo) PrintCmdOnExec() *Repo {
return r
}

// SetDryRun settings.
func (r *Repo) SetDryRun(dr bool) *Repo {
r.gw.DryRun = dr
return r
}

// Init run git init for the repo dir.
func (r *Repo) Init() error {
return r.gw.Init().Run()
Expand Down Expand Up @@ -431,21 +437,26 @@ func (r *Repo) CurBranchName() string {
return brName
}

// cat .git/HEAD
// cat .git/HEAD
// OR
// git symbolic-ref HEAD // out: refs/heads/fea_pref
// git symbolic-ref --short -q HEAD // on checkout tag, run will error
// git rev-parse --abbrev-ref -q HEAD
str, err := r.gw.RevParse("--abbrev-ref", "-q", "HEAD").Output()
if err != nil {
r.setErr(err)
return ""
// git branch --show-current // on high version git
// OR
// git symbolic-ref HEAD // out: refs/heads/fea_pref
// git symbolic-ref --short -q HEAD // on checkout tag, run will error
// Or
// git rev-parse --abbrev-ref -q HEAD // on init project, will error

str := r.gw.Branch("--show-current").SafeOutput()
if len(str) == 0 {
str, r.err = r.gw.RevParse("--abbrev-ref", "-q", "HEAD").Output()
if r.err != nil {
return ""
}
}

// eg: fea_pref
brName = cmdr.FirstLine(str)
r.cache.Set(cacheCurrentBranch, brName)

return brName
}

Expand Down Expand Up @@ -690,3 +701,8 @@ func (r *Repo) Git() *GitWrap {
func (r *Repo) Cmd(name string, args ...string) *GitWrap {
return r.gw.Cmd(name, args...)
}

// QuickRun git command
func (r *Repo) QuickRun(cmd string, args ...string) error {
return r.gw.Cmd(cmd, args...).Run()
}

0 comments on commit b1d05ff

Please sign in to comment.