Skip to content

Commit 7a38d31

Browse files
committed
fix: improve fuzzy finder preview to show branch names instead of commit hashes
- Replace git log --oneline with --pretty=format to show only commit messages - Add branch comparison info showing commits ahead/behind main/master - Improve preview layout with cleaner working directory status - Fix code style issues (remove unnecessary else clause)
1 parent abdad30 commit 7a38d31

1 file changed

Lines changed: 71 additions & 12 deletions

File tree

internal/worktree/interactive.go

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,46 @@ func createBranchPreview(i, width, height int) string {
4242

4343
var preview strings.Builder
4444
preview.WriteString(fmt.Sprintf("Branch: %s\n", wt.Branch))
45-
preview.WriteString(fmt.Sprintf("Path: %s\n\n", wt.Path))
45+
preview.WriteString(fmt.Sprintf("Path: %s\n", wt.Path))
4646

47-
// Add git log for the branch
48-
gitLog, err := getGitLog(wt.Branch, 5)
49-
if err != nil {
50-
preview.WriteString("Recent commits: (unable to load)\n")
51-
} else {
52-
preview.WriteString("Recent commits:\n")
53-
preview.WriteString(gitLog)
47+
// Add branch comparison info
48+
branchInfo, err := getBranchInfo(wt.Branch)
49+
if err == nil && branchInfo != "" {
50+
preview.WriteString(fmt.Sprintf("Status: %s\n", branchInfo))
5451
}
5552

53+
preview.WriteString("\n")
54+
5655
// Add git status if available
5756
gitStatus, err := getGitStatus(wt.Path)
5857
if err == nil && gitStatus != "" {
59-
preview.WriteString("\nWorking directory status:\n")
60-
preview.WriteString(gitStatus)
58+
preview.WriteString(fmt.Sprintf("Working directory: %s\n\n", gitStatus))
59+
} else {
60+
preview.WriteString("Working directory: Clean\n\n")
61+
}
62+
63+
// Add recent commits
64+
gitLog, err := getGitLog(wt.Branch, 5)
65+
if err != nil {
66+
preview.WriteString("Recent commits: (unable to load)")
67+
} else {
68+
preview.WriteString("Recent commits:\n")
69+
preview.WriteString(gitLog)
6170
}
6271

6372
return preview.String()
6473
}
6574

66-
// getGitLog returns recent commits for a branch
75+
// getGitLog returns recent commit messages for a branch (without hashes)
6776
func getGitLog(branch string, count int) (string, error) {
6877
repo, err := GetRepoRoot()
6978
if err != nil {
7079
return "", err
7180
}
7281

82+
// Get just commit messages, not hashes
7383
cmd := exec.Command("git", "-C", repo, "log",
74-
"--oneline",
84+
"--pretty=format:• %s",
7585
fmt.Sprintf("-%d", count),
7686
branch)
7787

@@ -83,6 +93,55 @@ func getGitLog(branch string, count int) (string, error) {
8393
return string(output), nil
8494
}
8595

96+
// getBranchInfo returns branch comparison information
97+
func getBranchInfo(branch string) (string, error) {
98+
repo, err := GetRepoRoot()
99+
if err != nil {
100+
return "", err
101+
}
102+
103+
// Skip comparison for main branch
104+
if branch == "main" || branch == "master" {
105+
return "Main branch", nil
106+
}
107+
108+
// Check if main exists, fallback to master
109+
mainBranch := "main"
110+
cmd := exec.Command("git", "-C", repo, "rev-parse", "--verify", "main")
111+
if cmd.Run() != nil {
112+
cmd = exec.Command("git", "-C", repo, "rev-parse", "--verify", "master")
113+
if cmd.Run() != nil {
114+
return "No main/master branch found", nil
115+
}
116+
mainBranch = "master"
117+
}
118+
119+
// Get ahead/behind count
120+
cmd = exec.Command("git", "-C", repo, "rev-list", "--left-right", "--count",
121+
fmt.Sprintf("%s...%s", mainBranch, branch))
122+
output, err := cmd.Output()
123+
if err != nil {
124+
return "", err
125+
}
126+
127+
counts := strings.Fields(strings.TrimSpace(string(output)))
128+
if len(counts) != 2 {
129+
return "Up to date", nil
130+
}
131+
132+
behind := counts[0]
133+
ahead := counts[1]
134+
135+
if ahead == "0" && behind == "0" {
136+
return fmt.Sprintf("Up to date with %s", mainBranch), nil
137+
} else if ahead == "0" {
138+
return fmt.Sprintf("%s commits behind %s", behind, mainBranch), nil
139+
} else if behind == "0" {
140+
return fmt.Sprintf("%s commits ahead of %s", ahead, mainBranch), nil
141+
}
142+
return fmt.Sprintf("%s ahead, %s behind %s", ahead, behind, mainBranch), nil
143+
}
144+
86145
// getGitStatus returns the working directory status for a worktree
87146
func getGitStatus(worktreePath string) (string, error) {
88147
cmd := exec.Command("git", "-C", worktreePath, "status", "--porcelain")

0 commit comments

Comments
 (0)