The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
package git
import (
"os"
"path/filepath"
"strings"
"testing"
)
func writeTestFile(t *testing.T, path, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir %s: %v", filepath.Dir(path), err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}
func TestCurrentBranchWithSlashes(t *testing.T) {
repo := t.TempDir()
writeTestFile(t, filepath.Join(repo, ".git", "HEAD"), "ref: refs/heads/feature/custom-fields\n")
if got := CurrentBranch(repo); got != "feature/custom-fields" {
t.Fatalf("CurrentBranch = %q, want feature/custom-fields", got)
}
}
func TestCurrentBranchOutsideRepo(t *testing.T) {
if got := CurrentBranch(t.TempDir()); got != "" {
t.Fatalf("CurrentBranch = %q, want empty outside a repo", got)
}
}
func TestCurrentBranchWorktreeUsesPerWorktreeHead(t *testing.T) {
base := t.TempDir()
mainRepo := filepath.Join(base, "main-repo")
worktree := filepath.Join(base, "wt")
writeTestFile(t, filepath.Join(mainRepo, ".git", "HEAD"), "ref: refs/heads/main\n")
worktreeGitDir := filepath.Join(mainRepo, ".git", "worktrees", "wt")
writeTestFile(t, filepath.Join(worktreeGitDir, "HEAD"), "ref: refs/heads/feature/wt-branch\n")
writeTestFile(t, filepath.Join(worktreeGitDir, "commondir"), "../..\n")
writeTestFile(t, filepath.Join(worktree, ".git"), "gitdir: "+worktreeGitDir+"\n")
if got := CurrentBranch(worktree); got != "feature/wt-branch" {
t.Fatalf("CurrentBranch = %q, want the worktree's own branch", got)
}
if got := CurrentBranch(mainRepo); got != "main" {
t.Fatalf("CurrentBranch = %q, want the main checkout's branch", got)
}
}
func TestCurrentBranchWorktreeRelativeGitDirPointer(t *testing.T) {
base := t.TempDir()
mainRepo := filepath.Join(base, "main-repo")
worktree := filepath.Join(base, "wt")
worktreeGitDir := filepath.Join(mainRepo, ".git", "worktrees", "wt")
writeTestFile(t, filepath.Join(worktreeGitDir, "HEAD"), "ref: refs/heads/relative-branch\n")
writeTestFile(t, filepath.Join(worktree, ".git"), "gitdir: ../main-repo/.git/worktrees/wt\n")
if got := CurrentBranch(worktree); got != "relative-branch" {
t.Fatalf("CurrentBranch = %q, want relative-branch", got)
}
}
func TestCurrentBranchBrokenWorktreeDoesNotReportParentBranch(t *testing.T) {
parent := t.TempDir()
writeTestFile(t, filepath.Join(parent, ".git", "HEAD"), "ref: refs/heads/parent-branch\n")
// The pointer parses but the referenced gitdir has no HEAD: the checkout
// is broken, so no branch should be reported — least of all the parent's.
broken := filepath.Join(parent, "broken-worktree")
writeTestFile(t, filepath.Join(broken, ".git"), "gitdir: "+filepath.Join(parent, ".git", "worktrees", "missing")+"\n")
if got := CurrentBranch(broken); got != "" {
t.Fatalf("CurrentBranch = %q, want empty for a broken worktree", got)
}
}
func TestCurrentBranchRejectsMalformedHead(t *testing.T) {
tests := []struct {
name string
head string
}{
{"detached", "0123456789abcdef0123456789abcdef01234567\n"},
{"non-branch ref", "ref: refs/remotes/origin/main\n"},
{"missing name", "ref: refs/heads/\n"},
{"control character", "ref: refs/heads/bad\x01name\n"},
{"embedded space", "ref: refs/heads/bad name\n"},
{"overlong name", "ref: refs/heads/" + strings.Repeat("a", 300) + "\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
repo := t.TempDir()
writeTestFile(t, filepath.Join(repo, ".git", "HEAD"), tt.head)
if got := CurrentBranch(repo); got != "" {
t.Fatalf("CurrentBranch = %q, want empty for %s HEAD", got, tt.name)
}
})
}
}