Skip to content

Commit 9f9b00d

Browse files
committed
👔 up: replace func FirstLine,OutputLines to cmdr.FirstLine,cmdr.OutputLines
1 parent 12c8897 commit 9f9b00d

4 files changed

Lines changed: 62 additions & 44 deletions

File tree

cmds.go

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
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

2526
var 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")
248250
func 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"
279283
func 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
327332
func 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

390395
func 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

396401
func 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 {

gitutil/gitutil_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,21 @@ func TestIsRepoPath(t *testing.T) {
6969
assert.Eq(t, tt.want, gitutil.IsRepoPath(tt.path))
7070
}
7171
}
72+
73+
func TestIsFullURL(t *testing.T) {
74+
tests := []struct {
75+
args string
76+
want bool
77+
}{
78+
{"inhere/gitw", false},
79+
{"github.com/inhere/gitw", false},
80+
{"https://github.com/inhere/gitw", true},
81+
{"http://github.com/inhere/gitw", true},
82+
{"git@github.com:inhere/gitw", true},
83+
{"ssh://git@github.com:inhere/gitw", true},
84+
}
85+
86+
for _, tt := range tests {
87+
assert.Eq(t, tt.want, gitutil.IsFullURL(tt.args))
88+
}
89+
}

repo.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/gookit/goutil/fsutil"
99
"github.com/gookit/goutil/maputil"
1010
"github.com/gookit/goutil/strutil"
11+
"github.com/gookit/goutil/sysutil/cmdr"
1112
)
1213

1314
const (
@@ -269,7 +270,7 @@ func (r *Repo) TagsSortedByRefName() []string {
269270
return nil
270271
}
271272

272-
return OutputLines(str)
273+
return cmdr.OutputLines(str)
273274
}
274275

275276
// TagsSortedByCreatorDate get repo tags list by creator date sort
@@ -282,7 +283,7 @@ func (r *Repo) TagsSortedByCreatorDate() []string {
282283
r.setErr(err)
283284
return nil
284285
}
285-
return OutputLines(str)
286+
return cmdr.OutputLines(str)
286287
}
287288

288289
// TagByDescribe get tag by describe command. if current not empty, will exclude it.
@@ -301,7 +302,7 @@ func (r *Repo) TagByDescribe(current string) (ver string) {
301302
r.setErr(err)
302303
return ""
303304
}
304-
return FirstLine(ver)
305+
return cmdr.FirstLine(ver)
305306
}
306307

307308
// Tags get repo tags list
@@ -442,7 +443,7 @@ func (r *Repo) CurBranchName() string {
442443
}
443444

444445
// eg: fea_pref
445-
brName = FirstLine(str)
446+
brName = cmdr.FirstLine(str)
446447
r.cache.Set(cacheCurrentBranch, brName)
447448

448449
return brName

util.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/gookit/goutil/cliutil"
1414
"github.com/gookit/goutil/fsutil"
1515
"github.com/gookit/goutil/sysutil"
16+
"github.com/gookit/goutil/sysutil/cmdr"
1617
"github.com/gookit/slog"
1718
)
1819

@@ -183,21 +184,14 @@ func editorCommands(editor string, args ...string) []string {
183184
}
184185

185186
// OutputLines split output to lines
186-
func OutputLines(output string) []string {
187-
output = strings.TrimSuffix(output, "\n")
188-
if output == "" {
189-
return nil
190-
}
191-
return strings.Split(output, "\n")
192-
}
193-
194-
// FirstLine from command output
195-
func FirstLine(output string) string {
196-
if i := strings.Index(output, "\n"); i >= 0 {
197-
return output[0:i]
198-
}
199-
return output
200-
}
187+
//
188+
// Deprecated: please use cmdr.OutputLines
189+
func OutputLines(output string) []string { return cmdr.OutputLines(output) }
190+
191+
// FirstLine from command output.
192+
//
193+
// Deprecated: please use cmdr.FirstLine
194+
func FirstLine(output string) string { return cmdr.FirstLine(output) }
201195

202196
func isDebugFromEnv() bool {
203197
return os.Getenv("GIT_CMD_VERBOSE") != ""

0 commit comments

Comments
 (0)