Skip to content

Commit fc9cdda

Browse files
committed
✨ feat: provide more repo methods for query branch info
1 parent d0c544b commit fc9cdda

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

gitw.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ import (
1616

1717
// some from: https://github.com/github/hub/blob/master/cmd/cmd.go
1818

19-
// GitDir name
20-
const GitDir = ".git"
21-
22-
// HeadFile in .git/
23-
const HeadFile = "HEAD"
24-
25-
// ConfFile in .git/
26-
const ConfFile = "config"
19+
const (
20+
// GitDir name
21+
GitDir = ".git"
22+
// HeadFile in .git/
23+
HeadFile = "HEAD"
24+
// ConfFile in .git/
25+
ConfFile = "config"
26+
)
2727

28-
var (
28+
const (
2929
// DefaultBin name
3030
DefaultBin = "git"
3131

info_branch.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ func (bs *BranchInfos) Parse() *BranchInfos {
137137
return bs
138138
}
139139

140+
// HasLocal branch check
141+
func (bs *BranchInfos) HasLocal(branch string) bool {
142+
return bs.GetByName(branch) != nil
143+
}
144+
145+
// HasRemote branch check
146+
func (bs *BranchInfos) HasRemote(branch, remote string) bool {
147+
return bs.GetByName(branch, remote) != nil
148+
}
149+
150+
// IsExists branch check
151+
func (bs *BranchInfos) IsExists(branch string, remote ...string) bool {
152+
return bs.GetByName(branch, remote...) != nil
153+
}
154+
140155
// GetByName find branch by name
141156
func (bs *BranchInfos) GetByName(branch string, remote ...string) *BranchInfo {
142157
if len(remote) > 0 && remote[0] != "" {

repo.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ const (
1616
cacheLastCommitID = "lastCID"
1717
cacheCurrentBranch = "curBranch"
1818
cacheMaxTagVersion = "maxVersion"
19+
cacheUpstreamPath = "upstreamTo"
1920
)
2021

2122
// RepoConfig struct
2223
type RepoConfig struct {
24+
// DefaultBranch name, default is DefaultBranchName
2325
DefaultBranch string
26+
// DefaultRemote name, default is DefaultRemoteName
2427
DefaultRemote string
2528
}
2629

@@ -86,6 +89,12 @@ func (r *Repo) WithConfigFn(fn func(cfg *RepoConfig)) *Repo {
8689
return r
8790
}
8891

92+
// PrintCmdOnExec settings.
93+
func (r *Repo) PrintCmdOnExec() *Repo {
94+
r.gw.BeforeExec = PrintCmdline
95+
return r
96+
}
97+
8998
// Init run git init for the repo dir.
9099
func (r *Repo) Init() error {
91100
return r.gw.Init().Run()
@@ -105,7 +114,7 @@ func (r *Repo) Info() *RepoInfo {
105114
Branch: r.CurBranchName(),
106115
Version: r.LargestTag(),
107116
LastHash: r.LastAbbrevID(),
108-
Upstream: r.FollowedRemote(),
117+
Upstream: r.UpstreamPath(),
109118
}
110119

111120
rt := r.loadRemoteInfos().DefaultRemoteInfo()
@@ -352,6 +361,18 @@ func (r *Repo) StatusInfo() *StatusInfo {
352361
// repo branch
353362
// -------------------------------------------------
354363

364+
func (r *Repo) HasBranch(branch string, remote ...string) bool {
365+
return r.loadBranchInfos().branchInfos.IsExists(branch, remote...)
366+
}
367+
368+
func (r *Repo) HasRemoteBranch(branch, remote string) bool {
369+
return r.loadBranchInfos().branchInfos.HasRemote(branch, remote)
370+
}
371+
372+
func (r *Repo) HasLocalBranch(branch string) bool {
373+
return r.loadBranchInfos().branchInfos.HasLocal(branch)
374+
}
375+
355376
// BranchInfos get branch infos of the repo
356377
func (r *Repo) BranchInfos() *BranchInfos {
357378
return r.loadBranchInfos().branchInfos
@@ -447,15 +468,38 @@ func (r *Repo) RemoteNames() []string {
447468
return r.loadRemoteInfos().remoteNames
448469
}
449470

450-
// FollowedRemote get current followed upstream remote name.
471+
// UpstreamPath get current upstream remote and branch.
451472
// Returns like: origin/main
452473
//
453474
// CMD:
454475
//
455476
// git rev-parse --abbrev-ref @{u}
456-
func (r *Repo) FollowedRemote() string {
477+
func (r *Repo) UpstreamPath() string {
478+
path := r.cache.Str(cacheUpstreamPath)
479+
457480
// RUN: git rev-parse --abbrev-ref @{u}
458-
return r.Git().RevParse("--abbrev-ref", "@{u}").SafeOutput()
481+
if path == "" {
482+
path = r.Git().RevParse("--abbrev-ref", "@{u}").SafeOutput()
483+
r.cache.Set(cacheUpstreamPath, path)
484+
}
485+
486+
return path
487+
}
488+
489+
// UpstreamRemote get current upstream remote name.
490+
func (r *Repo) UpstreamRemote() string {
491+
return strutil.OrHandle(r.UpstreamPath(), func(s string) string {
492+
remote, _ := strutil.QuietCut(s, "/")
493+
return remote
494+
})
495+
}
496+
497+
// UpstreamBranch get current upstream branch name.
498+
func (r *Repo) UpstreamBranch() string {
499+
return strutil.OrHandle(r.UpstreamPath(), func(s string) string {
500+
_, branch := strutil.QuietCut(s, "/")
501+
return branch
502+
})
459503
}
460504

461505
// RemoteInfos get by remote name
@@ -473,6 +517,11 @@ func (r *Repo) DefaultRemoteInfo(typ ...string) *RemoteInfo {
473517
return r.RemoteInfo(r.cfg.DefaultRemote, typ...)
474518
}
475519

520+
// FirstRemoteInfo get
521+
func (r *Repo) FirstRemoteInfo(typ ...string) *RemoteInfo {
522+
return r.RandomRemoteInfo(typ...)
523+
}
524+
476525
// RandomRemoteInfo get
477526
func (r *Repo) RandomRemoteInfo(typ ...string) *RemoteInfo {
478527
r.loadRemoteInfos()

0 commit comments

Comments
 (0)