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
fix(hooks): discover Git branch and remote from the physical working directory (#853)
Rudy Mizrahi Celekli10ef6d00a270
diff --git a/cli/beacon-hooks/internal/git/git.go b/cli/beacon-hooks/internal/git/git.goindex 316ac67d..832c8720 100644--- a/cli/beacon-hooks/internal/git/git.go+++ b/cli/beacon-hooks/internal/git/git.go@@ -38,12 +38,9 @@ func CurrentBranch(cwd string) string {// per-worktree gitdir, which holds this checkout's HEAD; the shared commondir// HEAD would report another worktree's branch.func findHeadFile(cwd string) string {+ cwd = physicalDir(cwd)if cwd == "" {- var err error- cwd, err = os.Getwd()- if err != nil {- return ""- }+ return ""}dir := cwd@@ -73,6 +70,23 @@ func findHeadFile(cwd string) string {}}+// physicalDir returns the directory Git would start discovery from: cwd (or the+// process working directory when empty) with symlinks resolved. A directory alias can+// point inside a different repository, and Git walks the physical path's ancestors, not+// the alias's. An unresolvable cwd is returned as given.+func physicalDir(cwd string) string {+ if cwd == "" {+ var err error+ if cwd, err = os.Getwd(); err != nil {+ return ""+ }+ }+ if resolved, err := filepath.EvalSymlinks(cwd); err == nil {+ return resolved+ }+ return cwd+}+// parseGitDirPointer reads a ".git" pointer file ("gitdir: <path>") and// returns the referenced gitdir as an absolute cleaned path, or "".func parseGitDirPointer(dotGitFile string) string {@@ -153,12 +167,9 @@ func GetRemoteInfo(cwd string) (owner, name string) {// Handles both normal repositories (.git is a directory) and worktrees (.git is a file// containing a "gitdir:" reference).func findGitDir(cwd string) string {+ cwd = physicalDir(cwd)if cwd == "" {- var err error- cwd, err = os.Getwd()- if err != nil {- return ""- }+ return ""}dir := cwddiff --git a/cli/beacon-hooks/internal/git/git_cwd_alias_test.go b/cli/beacon-hooks/internal/git/git_cwd_alias_test.gonew file mode 100644index 00000000..1e1ccce9--- /dev/null+++ b/cli/beacon-hooks/internal/git/git_cwd_alias_test.go@@ -0,0 +1,74 @@+package git++import (+ "os"+ "os/exec"+ "path/filepath"+ "strings"+ "testing"+)++func TestCurrentBranchFollowsThePhysicalWorkingDirectory(t *testing.T) {+ binary, err := exec.LookPath("git")+ if err != nil {+ t.Skip("native git unavailable")+ }+ base := t.TempDir()+ home := filepath.Join(base, "home")+ if err = os.Mkdir(home, 0700); err != nil {+ t.Fatal(err)+ }+ t.Setenv("HOME", home)+ t.Setenv("GIT_CONFIG_GLOBAL", filepath.Join(home, "none"))+ t.Setenv("GIT_CONFIG_SYSTEM", os.DevNull)+ initRepo := func(name, branch string) string {+ path := filepath.Join(base, name)+ cmd := exec.Command(binary, "init", "-b", branch, path)+ if out, err := cmd.CombinedOutput(); err != nil {+ t.Fatalf("init real repository: %v %s", err, out)+ }+ remote := exec.Command(binary, "-C", path, "remote", "add", "origin", "https://github.com/acme/"+name+".git")+ if out, err := remote.CombinedOutput(); err != nil {+ t.Fatalf("add origin: %v %s", err, out)+ }+ return path+ }+ target := initRepo("target", "feature/target")+ outer := initRepo("outer", "feature/outer")+ nested := filepath.Join(target, "pkg", "deep")+ if err = os.MkdirAll(nested, 0700); err != nil {+ t.Fatal(err)+ }+ for _, tc := range []struct{ name, path, target string }{+ {"physical", nested, ""},+ {"external alias", filepath.Join(base, "workspace"), nested},+ {"alias inside another repo", filepath.Join(outer, "workspace"), nested},+ {"repository root alias", filepath.Join(base, "root-alias"), target},+ {"relative alias inside another repo", filepath.Join(outer, "relative-workspace"), filepath.Join("..", "target", "pkg", "deep")},+ } {+ t.Run(tc.name, func(t *testing.T) {+ if tc.target != "" {+ if err := os.Symlink(tc.target, tc.path); err != nil {+ t.Skipf("directory symlink unavailable: %v", err)+ }+ }+ out, err := exec.Command(binary, "-C", tc.path, "symbolic-ref", "--short", "HEAD").CombinedOutput()+ if err != nil {+ t.Fatalf("native git oracle: %v %s", err, out)+ }+ want := strings.TrimSpace(string(out))+ if got := CurrentBranch(tc.path); got != want {+ t.Fatalf("CurrentBranch=%q native git=%q", got, want)+ }+ // Branch and remote must come from the same repository.+ url, err := exec.Command(binary, "-C", tc.path, "remote", "get-url", "origin").CombinedOutput()+ if err != nil {+ t.Fatalf("native git oracle: %v %s", err, url)+ }+ wantName := strings.TrimSuffix(filepath.Base(strings.TrimSpace(string(url))), ".git")+ if _, name := GetRemoteInfo(tc.path); name != wantName {+ t.Fatalf("GetRemoteInfo name=%q native git=%q", name, wantName)+ }+ })+ }+}