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
99100func (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
427462func (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