Skip to content

Commit

Permalink
feat: Support detached HEAD in stack tree (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
draftcode authored Apr 20, 2023
1 parent ef6966d commit d428aa0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cmd/av/stack_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ var stackTreeCmd = &cobra.Command{
return err
}

currentBranch, err := repo.CurrentBranchName()
if err != nil {
var currentBranch string
if dh, err := repo.DetachedHead(); err != nil {
return err
} else if !dh {
currentBranch, err = repo.CurrentBranchName()
if err != nil {
return err
}
}

// TODO[polish]:
Expand Down
13 changes: 13 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,19 @@ func (r *Repo) GitStdin(args []string, stdin io.Reader) (string, error) {
return strings.TrimSpace(string(out)), nil
}

// DetachedHead returns true if the repository is in the detached HEAD.
func (r *Repo) DetachedHead() (bool, error) {
ret, err := r.Run(&RunOpts{
Args: []string{
"symbolic-ref", "--quiet", "HEAD",
},
})
if err != nil {
return false, err
}
return ret.ExitCode == 1, nil
}

// CurrentBranchName returns the name of the current branch.
// The name is return in "short" format -- i.e., without the "refs/heads/" prefix.
// IMPORTANT: This function will return an error if the repository is currently
Expand Down

0 comments on commit d428aa0

Please sign in to comment.