diff options
author | Luka Markušić <luka.markusic@microblink.com> | 2022-07-30 19:02:19 +0200 |
---|---|---|
committer | Luka Markušić <luka.markusic@microblink.com> | 2022-07-30 19:02:19 +0200 |
commit | 966733240c0a6b2ca2233aa260966d53915254e0 (patch) | |
tree | 88623f0d03b86d2b86e0102984eccb43c6d147b9 /pkg/gui/recent_repos_panel.go | |
parent | 25ddac0d8fc227836f67f9a9402a32456d3ab1ee (diff) |
Refactor a bit, enable worktrees
Diffstat (limited to 'pkg/gui/recent_repos_panel.go')
-rw-r--r-- | pkg/gui/recent_repos_panel.go | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/pkg/gui/recent_repos_panel.go b/pkg/gui/recent_repos_panel.go index 6d7eacb8b..bfc363e76 100644 --- a/pkg/gui/recent_repos_panel.go +++ b/pkg/gui/recent_repos_panel.go @@ -17,13 +17,36 @@ import ( ) func (gui *Gui) getCurrentBranch(path string) string { - if headFile, err := ioutil.ReadFile(fmt.Sprintf("%s/.git/HEAD", path)); err == nil { - content := strings.TrimSpace(string(headFile)) - branch := strings.TrimPrefix(content, "ref: refs/heads/") - return branch + readHeadFile := func(path string) (string, error) { + headFile, err := ioutil.ReadFile(fmt.Sprintf("%s%cHEAD", path, os.PathSeparator)) + if err == nil { + content := strings.TrimSpace(string(headFile)) + branch := strings.TrimPrefix(content, "ref: refs/heads/") + return branch, nil + } + return "", err } - // worktrees don't have `.git/HEAD` - // and detached HEAD repos have only a hash in `.git/HEAD` + + gitDirPath := fmt.Sprintf("%s%c.git", path, os.PathSeparator) + + if gitDir, err := os.Stat(gitDirPath); err == nil { + if gitDir.IsDir() { + // ordinary repo + if branch, err := readHeadFile(gitDirPath); err == nil { + return branch + } + } else { + // worktree + if worktreeGitDir, err := ioutil.ReadFile(gitDirPath); err == nil { + content := strings.TrimSpace(string(worktreeGitDir)) + worktreePath := strings.TrimPrefix(content, "gitdir: ") + if branch, err := readHeadFile(worktreePath); err == nil { + return branch + } + } + } + } + return "HEAD" } |