Skip to content

Commit

Permalink
fix: branch info parse error on checkout to tag,hash
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 29, 2022
1 parent a28032b commit 08779b9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
11 changes: 10 additions & 1 deletion info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ func TestNewRemoteInfo(t *testing.T) {
}

func TestParseBranchLine_simple(t *testing.T) {
info, err := gitw.ParseBranchLine("* fea/new_br001", false)
info, err := gitw.ParseBranchLine("* ", false)
assert.Error(t, err)

info, err = gitw.ParseBranchLine("* (HEAD)", false)
assert.Error(t, err)

info, err = gitw.ParseBranchLine("* fea/new_br001", false)
assert.NoError(t, err)

assert.True(t, info.Current)
Expand Down Expand Up @@ -81,6 +87,9 @@ func TestParseBranchLine_verbose(t *testing.T) {
assert.Equal(t, "my_new_br", info.Short)
assert.Equal(t, "6fb8dcd", info.Hash)
assert.Equal(t, "the message 003", info.HashMsg)

info, err = gitw.ParseBranchLine("* (头指针在 v0.2.3 分离) 3c08adf chore: update readme add branch info docs", true)
assert.Error(t, err)
}

func TestBranchInfo_parse_simple(t *testing.T) {
Expand Down
17 changes: 13 additions & 4 deletions info_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,27 @@ var ErrInvalidBrLine = errorx.Raw("invalid git branch line text")
// False - only branch name
// True - get by `git br -v --all`
// format: * BRANCH_NAME COMMIT_ID COMMIT_MSG
func ParseBranchLine(line string, verbose bool) (info *BranchInfo, err error) {
info = &BranchInfo{}
func ParseBranchLine(line string, verbose bool) (*BranchInfo, error) {
info := &BranchInfo{}
line = strings.TrimSpace(line)

if strings.HasPrefix(line, "*") {
info.Current = true
line = strings.Trim(line, "*\t ")
}

if line == "" {
return nil, ErrInvalidBrLine
}

// at tag head. eg: `* (头指针在 v0.2.3 分离) 3c08adf chore: update readme add branch info docs`
if strings.HasPrefix(line, "(") || strings.HasPrefix(line, "(") {
return nil, ErrInvalidBrLine
}

if !verbose {
info.SetName(line)
return
return info, nil
}

// parse name
Expand All @@ -103,7 +112,7 @@ func ParseBranchLine(line string, verbose bool) (info *BranchInfo, err error) {
}

info.Hash, info.HashMsg = nodes[0], nodes[1]
return
return info, nil
}

// eg:
Expand Down

0 comments on commit 08779b9

Please sign in to comment.