From b1d05ff43133deb02ea02ce6ea9f31392eae03ec Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 9 Apr 2023 12:55:52 +0800 Subject: [PATCH] :sparkles: feat: add more git util func and update the gh ci config --- .github/workflows/go.yml | 2 ++ .github/workflows/release.yml | 2 -- gitutil/gitutil.go | 39 +++++++++++++++++++++++++++++++++++ repo.go | 34 ++++++++++++++++++++++-------- 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 2ed74b8..3e6a49b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -27,6 +27,8 @@ jobs: with: fetch-depth: 0 + - run: git fetch --force --tags + - name: Display Env run: | git remote -v diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 40f289d..b85f944 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,8 +10,6 @@ jobs: name: Release new version runs-on: ubuntu-latest timeout-minutes: 5 - strategy: - fail-fast: true steps: - name: Checkout diff --git a/gitutil/gitutil.go b/gitutil/gitutil.go index f30e691..1702160 100644 --- a/gitutil/gitutil.go +++ b/gitutil/gitutil.go @@ -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, ".") +} diff --git a/repo.go b/repo.go index e42ce0f..ce3fdde 100644 --- a/repo.go +++ b/repo.go @@ -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() @@ -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 } @@ -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() +}