Skip to content

Commit 1686d7b

Browse files
committed
👔 up(all): update and add more git repo methods
1 parent be57426 commit 1686d7b

4 files changed

Lines changed: 72 additions & 20 deletions

File tree

gitw.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ import (
1919
// GitDir name
2020
const GitDir = ".git"
2121

22+
// HeadFile in .git/
23+
const HeadFile = "HEAD"
24+
25+
// ConfFile in .git/
26+
const ConfFile = "config"
27+
2228
var (
2329
// DefaultBin name
2430
DefaultBin = "git"
@@ -276,6 +282,7 @@ func (gw *GitWrap) OutputLines() ([]string, error) {
276282

277283
// SafeOutput run and return output
278284
func (gw *GitWrap) SafeOutput() string {
285+
gw.Stderr = nil // ignore stderr
279286
out, err := gw.Output()
280287
if err != nil {
281288
return ""
@@ -503,8 +510,9 @@ func (gw *GitWrap) RevList(args ...string) *GitWrap {
503510
// RevParse command for git
504511
//
505512
// rev-parse usage:
506-
// git rev-parse --show-toplevel // get git workdir, repo dir.
507-
// git rev-parse -q --git-dir // get git data dir name. eg: .git
513+
//
514+
// git rev-parse --show-toplevel // get git workdir, repo dir.
515+
// git rev-parse -q --git-dir // get git data dir name. eg: .git
508516
func (gw *GitWrap) RevParse(args ...string) *GitWrap {
509517
return gw.Cmd("rev-parse", args...)
510518
}

info.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ type RepoInfo struct {
2020
LastHash string
2121
Branch string
2222
Version string
23+
// Upstream remote name
24+
Upstream string
2325
}

info_remote.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,14 @@ func (r *RemoteInfo) buildHTTPURL(toHttps bool) string {
111111
return schema + "://" + r.Host + "/" + r.Group + "/" + r.Repo
112112
}
113113

114-
// HTTPScheme name string
115-
func (r *RemoteInfo) HTTPScheme() string {
116-
if r.Proto == ProtoHTTP {
117-
return r.Scheme
114+
// HTTPHost URL build. return like: https://github.com
115+
func (r *RemoteInfo) HTTPHost() string {
116+
schema := r.Scheme
117+
if r.Proto != ProtoHTTP {
118+
schema = SchemeHTTPS
118119
}
119-
return SchemeHTTPS
120-
}
121120

122-
// HTTPHost URL build.
123-
func (r *RemoteInfo) HTTPHost() string {
124-
return r.HTTPScheme() + "://" + r.Host
121+
return schema + "://" + r.Host
125122
}
126123

127124
// Path string

repo.go

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/gookit/goutil/arrutil"
77
"github.com/gookit/goutil/errorx"
8+
"github.com/gookit/goutil/fsutil"
89
"github.com/gookit/goutil/maputil"
910
"github.com/gookit/goutil/strutil"
1011
)
@@ -97,21 +98,26 @@ func (r *Repo) IsInited() bool {
9798

9899
// Info get repo information
99100
func (r *Repo) Info() *RepoInfo {
100-
rt := r.loadRemoteInfos().DefaultRemoteInfo()
101-
if rt == nil {
102-
return nil
103-
}
104-
105-
return &RepoInfo{
106-
Name: rt.Repo,
107-
Path: rt.Path(),
101+
ri := &RepoInfo{
108102
Dir: r.dir,
109-
URL: rt.RawURLOfHTTP(),
103+
Name: fsutil.Name(r.dir),
110104
// more
111105
Branch: r.CurBranchName(),
112106
Version: r.LargestTag(),
113107
LastHash: r.LastAbbrevID(),
108+
Upstream: r.FollowedRemote(),
114109
}
110+
111+
rt := r.loadRemoteInfos().DefaultRemoteInfo()
112+
if rt == nil {
113+
return ri
114+
}
115+
116+
ri.Name = rt.Repo
117+
ri.Path = rt.Path()
118+
ri.URL = rt.URLOrBuild()
119+
120+
return ri
115121
}
116122

117123
// -------------------------------------------------
@@ -409,6 +415,24 @@ func (r *Repo) CurBranchName() string {
409415
return brName
410416
}
411417

418+
// SetUpstreamTo set the branch upstream remote branch.
419+
// If `localBranch` is empty, will use `branch` as `localBranch`
420+
//
421+
// CMD:
422+
//
423+
// git branch --set-upstream-to=<remote>/<branch> <local_branch>
424+
func (r *Repo) SetUpstreamTo(remote, branch string, localBranch ...string) error {
425+
localBr := branch
426+
if len(localBranch) > 0 {
427+
localBr = localBranch[0]
428+
}
429+
430+
return r.gw.Cmd("branch").
431+
Argf("--set-upstream-to=%s/%s", remote, branch).
432+
AddArg(localBr).
433+
Run()
434+
}
435+
412436
// -------------------------------------------------
413437
// repo remote
414438
// -------------------------------------------------
@@ -423,6 +447,17 @@ func (r *Repo) RemoteNames() []string {
423447
return r.loadRemoteInfos().remoteNames
424448
}
425449

450+
// FollowedRemote get current followed upstream remote name.
451+
// Returns like: origin/main
452+
//
453+
// CMD:
454+
//
455+
// git rev-parse --abbrev-ref @{u}
456+
func (r *Repo) FollowedRemote() string {
457+
// RUN: git rev-parse --abbrev-ref @{u}
458+
return r.Git().RevParse("--abbrev-ref", "@{u}").SafeOutput()
459+
}
460+
426461
// RemoteInfos get by remote name
427462
func (r *Repo) RemoteInfos(remote string) RemoteInfos {
428463
r.loadRemoteInfos()
@@ -543,6 +578,16 @@ func (r *Repo) loadRemoteInfos() *Repo {
543578
// r.err = nil
544579
// }
545580

581+
// ReadConfig contents from REPO/.git/config
582+
func (r *Repo) ReadConfig() []byte {
583+
return fsutil.GetContents(fsutil.JoinPaths(r.dir, GitDir, ConfFile))
584+
}
585+
586+
// ReadHEAD contents from REPO/.git/HEAD
587+
func (r *Repo) ReadHEAD() []byte {
588+
return fsutil.GetContents(fsutil.JoinPaths(r.dir, GitDir, HeadFile))
589+
}
590+
546591
// -------------------------------------------------
547592
// helper methods
548593
// -------------------------------------------------

0 commit comments

Comments
 (0)