Skip to content

Commit edc0446

Browse files
committed
feat: display repo name and branch in status panel
Signed-off-by: Ayush <mail@ayuch.dev>
1 parent 6e98caf commit edc0446

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

internal/git/repo.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package git
2+
3+
import (
4+
"os/exec"
5+
"path/filepath"
6+
"strings"
7+
)
8+
9+
// GetRepoInfo returns the current repository and active branch name.
10+
func (g *GitCommands) GetRepoInfo() (repoName string, branchName string, err error) {
11+
// Get the root dir of the repo.
12+
repoPathBytes, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
13+
if err != nil {
14+
return "", "", err
15+
}
16+
repoPath := strings.TrimSpace(string(repoPathBytes))
17+
18+
repoName = filepath.Base(repoPath)
19+
20+
// Get the current branch name.
21+
repoBranchBytes, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output()
22+
if err != nil {
23+
return "", "", err
24+
}
25+
branchName = strings.TrimSpace(string(repoBranchBytes))
26+
27+
return repoName, branchName, nil
28+
}

internal/git/repo_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package git
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestGetRepoInfo(t *testing.T) {
10+
g := NewGitCommands()
11+
12+
// This test will only pass if run inside a git repository.
13+
repoName, branchName, err := g.GetRepoInfo()
14+
15+
if err != nil {
16+
t.Fatalf("GetRepoInfo() returned an error: %v", err)
17+
}
18+
19+
if repoName == "" {
20+
t.Error("Expected repoName to not be empty")
21+
}
22+
23+
if branchName == "" {
24+
t.Error("Expected branchName to not be empty")
25+
}
26+
27+
// Verify with actual git commands
28+
expectedBranchBytes, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output()
29+
if err != nil {
30+
t.Fatalf("Failed to get branch name from git: %v", err)
31+
}
32+
expectedBranch := strings.TrimSpace(string(expectedBranchBytes))
33+
34+
if branchName != expectedBranch {
35+
t.Errorf("got branch %q, want %q", branchName, expectedBranch)
36+
}
37+
}

internal/tui/model.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@ import (
1212
type Model struct {
1313
width int
1414
height int
15+
panels []panel
16+
panelHeights []int
17+
focusedPanel Panel
1518
theme Theme
1619
themeNames []string
1720
themeIndex int
18-
focusedPanel Panel
1921
help help.Model
2022
helpViewport viewport.Model
2123
helpContent string
2224
showHelp bool
2325
git *git.GitCommands
24-
panels []panel
25-
panelHeights []int
26+
repoName string
27+
branchName string
2628
}
2729

2830
// initialModel creates the initial state of the application.
2931
func initialModel() Model {
3032
themeNames := ThemeNames()
3133
gc := git.NewGitCommands()
34+
repoName, branchName, _ := gc.GetRepoInfo()
3235
initialContent := "Loading..."
3336

3437
// Create a slice to hold all our panels.
@@ -51,6 +54,8 @@ func initialModel() Model {
5154
helpViewport: viewport.New(0, 0),
5255
showHelp: false,
5356
git: gc,
57+
repoName: repoName,
58+
branchName: branchName,
5459
panels: panels,
5560
}
5661
}

internal/tui/update.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tui
22

33
import (
4+
"fmt"
45
"strings"
56

67
"github.com/charmbracelet/bubbles/key"
@@ -22,12 +23,13 @@ type panelContentUpdatedMsg struct {
2223
// fetchPanelContent is a generic command that fetches content for a given panel.
2324
func fetchPanelContent(gc *git.GitCommands, panel Panel) tea.Cmd {
2425
return func() tea.Msg {
25-
var content string
26+
var content, repoName, branchName string
2627
var err error
2728

2829
switch panel {
2930
case StatusPanel:
30-
content, err = gc.GetStatus()
31+
repoName, branchName, err = gc.GetRepoInfo()
32+
content = fmt.Sprintf("%s -> %s", repoName, branchName)
3133
case FilesPanel:
3234
content = "\nPLACEHOLDER DATA??\n1\n2\n cmd/gitx\n MM internal/tui\n file1.go\n M file2.txt\n A file3.md" // FIXME: Placeholder
3335
case BranchesPanel:

0 commit comments

Comments
 (0)