diff options
author | Ryooooooga <eial5q265e5@gmail.com> | 2022-10-16 21:31:42 +0900 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-11-14 19:05:07 +1100 |
commit | 52a2e4c1dc270c95366d05687de514caed95f695 (patch) | |
tree | 3c0d5d086da0a365c8429166a903874f6c5aaf2f /pkg/commands/git_commands/branch.go | |
parent | b33ec5a05025a2755f4511f042e9e0fcb224bf47 (diff) |
fix: fix ambiguous branch name
test: add an integration test for checkout branch by name
fix: fix full ref name of detached head
refactor: refactor current branch loader
chore: use field name explicitly
Diffstat (limited to 'pkg/commands/git_commands/branch.go')
-rw-r--r-- | pkg/commands/git_commands/branch.go | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/pkg/commands/git_commands/branch.go b/pkg/commands/git_commands/branch.go index d8be71deb..117427778 100644 --- a/pkg/commands/git_commands/branch.go +++ b/pkg/commands/git_commands/branch.go @@ -30,18 +30,20 @@ func (self *BranchCommands) New(name string, base string) error { return self.cmd.New(fmt.Sprintf("git checkout -b %s %s", self.cmd.Quote(name), self.cmd.Quote(base))).Run() } -// CurrentBranchName get the current branch name and displayname. -// the first returned string is the name and the second is the displayname -// e.g. name is 123asdf and displayname is '(HEAD detached at 123asdf)' -func (self *BranchCommands) CurrentBranchName() (string, string, error) { +// CurrentBranchInfo get the current branch information. +func (self *BranchCommands) CurrentBranchInfo() (BranchInfo, error) { branchName, err := self.cmd.New("git symbolic-ref --short HEAD").DontLog().RunWithOutput() if err == nil && branchName != "HEAD\n" { trimmedBranchName := strings.TrimSpace(branchName) - return trimmedBranchName, trimmedBranchName, nil + return BranchInfo{ + RefName: trimmedBranchName, + DisplayName: trimmedBranchName, + DetachedHead: false, + }, nil } output, err := self.cmd.New("git branch --contains").DontLog().RunWithOutput() if err != nil { - return "", "", err + return BranchInfo{}, err } for _, line := range utils.SplitLines(output) { re := regexp.MustCompile(CurrentBranchNameRegex) @@ -49,10 +51,18 @@ func (self *BranchCommands) CurrentBranchName() (string, string, error) { if len(match) > 0 { branchName = match[1] displayBranchName := match[0][2:] - return branchName, displayBranchName, nil + return BranchInfo{ + RefName: branchName, + DisplayName: displayBranchName, + DetachedHead: true, + }, nil } } - return "HEAD", "HEAD", nil + return BranchInfo{ + RefName: "HEAD", + DisplayName: "HEAD", + DetachedHead: true, + }, nil } // Delete delete branch |