@@ -16,11 +16,14 @@ const (
1616 cacheLastCommitID = "lastCID"
1717 cacheCurrentBranch = "curBranch"
1818 cacheMaxTagVersion = "maxVersion"
19+ cacheUpstreamPath = "upstreamTo"
1920)
2021
2122// RepoConfig struct
2223type 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.
9099func (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
356377func (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
477526func (r * Repo ) RandomRemoteInfo (typ ... string ) * RemoteInfo {
478527 r .loadRemoteInfos ()
0 commit comments