77 "strings"
88
99 "github.com/gookit/goutil/errorx"
10+ "github.com/gookit/goutil/sysutil/cmdr"
1011)
1112
1213// some from: https://github.com/github/hub/blob/master/git/git.go
@@ -19,7 +20,7 @@ func Version() (string, error) {
1920 return "" , errorx .Wrap (err , "error running git version" )
2021 }
2122
22- return FirstLine (output ), nil
23+ return cmdr . FirstLine (output ), nil
2324}
2425
2526var cachedDir string
@@ -50,7 +51,7 @@ func DataDir() (string, error) {
5051 }
5152 }
5253
53- gitDir := FirstLine (output )
54+ gitDir := cmdr . FirstLine (output )
5455 if ! filepath .IsAbs (gitDir ) {
5556 if chdir != "" {
5657 gitDir = filepath .Join (chdir , gitDir )
@@ -85,7 +86,7 @@ func WorkdirName() (string, error) {
8586 toplevelCmd .Stderr = nil
8687 output , err := toplevelCmd .Output ()
8788
88- dir := FirstLine (output )
89+ dir := cmdr . FirstLine (output )
8990 if dir == "" {
9091 return "" , fmt .Errorf ("unable to determine git working directory" )
9192 }
@@ -98,7 +99,7 @@ func HasFile(segments ...string) bool {
9899 pathCmd := gitCmd ("rev-parse" , "-q" , "--git-path" , filepath .Join (segments ... ))
99100 pathCmd .Stderr = nil
100101 if output , err := pathCmd .Output (); err == nil {
101- if lines := OutputLines (output ); len (lines ) == 1 {
102+ if lines := cmdr . OutputLines (output ); len (lines ) == 1 {
102103 if _ , err := os .Stat (lines [0 ]); err == nil {
103104 return true
104105 }
@@ -132,7 +133,7 @@ func SymbolicRef(ref string) (string, error) {
132133 refCmd := gitCmd ("symbolic-ref" , ref )
133134 refCmd .Stderr = nil
134135 output , err := refCmd .Output ()
135- return FirstLine (output ), err
136+ return cmdr . FirstLine (output ), err
136137}
137138
138139// SymbolicFullName reads a branch name from a ref such as "@{upstream}"
@@ -144,7 +145,7 @@ func SymbolicFullName(name string) (string, error) {
144145 return "" , errorx .Newf ("unknown revision or path not in the working tree: %s" , name )
145146 }
146147
147- return FirstLine (output ), nil
148+ return cmdr . FirstLine (output ), nil
148149}
149150
150151// Ref get
@@ -156,7 +157,7 @@ func Ref(ref string) (string, error) {
156157 return "" , errorx .Newf ("unknown revision or path not in the working tree: %s" , ref )
157158 }
158159
159- return FirstLine (output ), nil
160+ return cmdr . FirstLine (output ), nil
160161}
161162
162163// RefList for two sha
@@ -170,7 +171,7 @@ func RefList(a, b string) ([]string, error) {
170171 return nil , errorx .Newf ("can't load rev-list for %s" , ref )
171172 }
172173
173- return OutputLines (output ), nil
174+ return cmdr . OutputLines (output ), nil
174175}
175176
176177// NewRange object
@@ -182,7 +183,7 @@ func NewRange(a, b string) (*Range, error) {
182183 return nil , err
183184 }
184185
185- lines := OutputLines (output )
186+ lines := cmdr . OutputLines (output )
186187 if len (lines ) != 2 {
187188 return nil , fmt .Errorf ("can't parse range %s..%s" , a , b )
188189 }
@@ -243,6 +244,7 @@ func ShowDiff(sha string) (string, error) {
243244// ShowLogs show git log between sha1 to sha2
244245//
245246// Usage:
247+ //
246248// gitw.ShowLogs("v1.0.2", "v1.0.3")
247249// gitw.ShowLogs("commit id 1", "commit id 2")
248250func ShowLogs (sha1 , sha2 string ) (string , error ) {
@@ -266,16 +268,18 @@ func ShowLogs(sha1, sha2 string) (string, error) {
266268// Tags list
267269//
268270// something:
269- // `git tag -l` == `git tag --format '%(refname:strip=2)'`
271+ //
272+ // `git tag -l` == `git tag --format '%(refname:strip=2)'`
270273//
271274// more e.g:
272- // // refname - sorts in a lexicographic order
273- // // version:refname or v:refname - this sorts based on version
274- // git tag --sort=-version:refname
275- // git tag -l --sort version:refname
276- // git tag --format '%(refname:strip=2)' --sort=-taggerdate
277- // git tag --format '%(refname:strip=2) %(objectname)' --sort=-taggerdate
278- // git log --tags --simplify-by-decoration --pretty="format:%d - %cr"
275+ //
276+ // // refname - sorts in a lexicographic order
277+ // // version:refname or v:refname - this sorts based on version
278+ // git tag --sort=-version:refname
279+ // git tag -l --sort version:refname
280+ // git tag --format '%(refname:strip=2)' --sort=-taggerdate
281+ // git tag --format '%(refname:strip=2) %(objectname)' --sort=-taggerdate
282+ // git log --tags --simplify-by-decoration --pretty="format:%d - %cr"
279283func Tags (args ... string ) ([]string , error ) {
280284 if len (args ) > 0 {
281285 args1 := make ([]string , len (args )+ 1 )
@@ -297,7 +301,7 @@ func Branches() ([]string, error) {
297301 }
298302
299303 var branches []string
300- for _ , branch := range OutputLines (output ) {
304+ for _ , branch := range cmdr . OutputLines (output ) {
301305 branches = append (branches , branch [2 :])
302306 }
303307 return branches , nil
@@ -312,7 +316,7 @@ func Remotes() ([]string, error) {
312316 return nil , err
313317 }
314318
315- return OutputLines (output ), nil
319+ return cmdr . OutputLines (output ), nil
316320}
317321
318322// -------------------------------------------------
@@ -322,8 +326,9 @@ func Remotes() ([]string, error) {
322326// Var get by git var.
323327//
324328// Example
325- // all: git var -l
326- // one: git var GIT_EDITOR
329+ //
330+ // all: git var -l
331+ // one: git var GIT_EDITOR
327332func Var (name string ) string {
328333 val , err := New ("var" , name ).Output ()
329334 if err != nil {
@@ -363,7 +368,7 @@ func ConfigAll(name string) ([]string, error) {
363368 if err != nil {
364369 return nil , errorx .Newf ("unknown config %s" , name )
365370 }
366- return OutputLines (output ), nil
371+ return cmdr . OutputLines (output ), nil
367372}
368373
369374// GlobalConfig get git global config by name
@@ -384,13 +389,13 @@ func gitConfigGet(args ...string) (string, error) {
384389 return "" , fmt .Errorf ("unknown config %s" , args [len (args )- 1 ])
385390 }
386391
387- return FirstLine (output ), nil
392+ return cmdr . FirstLine (output ), nil
388393}
389394
390395func gitConfig (args ... string ) ([]string , error ) {
391396 configCmd := gitCmd (gitConfigCommand (args )... )
392397 output , err := configCmd .Output ()
393- return OutputLines (output ), err
398+ return cmdr . OutputLines (output ), err
394399}
395400
396401func gitConfigCommand (args []string ) []string {
@@ -438,7 +443,7 @@ func IsGitCommand(command string) bool {
438443 return false
439444 }
440445
441- for _ , helpCmdOutputLine := range OutputLines (cmdOutput ) {
446+ for _ , helpCmdOutputLine := range cmdr . OutputLines (cmdOutput ) {
442447 if strings .HasPrefix (helpCmdOutputLine , " " ) {
443448 for _ , gitCommand := range strings .Split (helpCmdOutputLine , " " ) {
444449 if gitCommand == command {
0 commit comments