Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

1.8 KB
Raw Blame
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)
		}
		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, ""},
		{"alias inside another repo", filepath.Join(outer, "workspace"), nested},
		{"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)
			}
		})
	}
}