feat: allow local repo reference clones to save disk
Use git --reference-if-able when a local repo is provided so rigs and crew share objects without changing remotes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -40,6 +40,12 @@ func (g *Git) WorkDir() string {
|
||||
return g.workDir
|
||||
}
|
||||
|
||||
// IsRepo returns true if the workDir is a git repository.
|
||||
func (g *Git) IsRepo() bool {
|
||||
_, err := g.run("rev-parse", "--git-dir")
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// run executes a git command and returns stdout.
|
||||
func (g *Git) run(args ...string) (string, error) {
|
||||
// If gitDir is set (bare repo), prepend --git-dir flag
|
||||
@@ -99,6 +105,18 @@ func (g *Git) Clone(url, dest string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloneWithReference clones a repository using a local repo as an object reference.
|
||||
// This saves disk by sharing objects without changing remotes.
|
||||
func (g *Git) CloneWithReference(url, dest, reference string) error {
|
||||
cmd := exec.Command("git", "clone", "--reference-if-able", reference, url, dest)
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return g.wrapError(err, stderr.String(), []string{"clone", "--reference-if-able", url})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloneBare clones a repository as a bare repo (no working directory).
|
||||
// This is used for the shared repo architecture where all worktrees share a single git database.
|
||||
func (g *Git) CloneBare(url, dest string) error {
|
||||
@@ -111,6 +129,17 @@ func (g *Git) CloneBare(url, dest string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloneBareWithReference clones a bare repository using a local repo as an object reference.
|
||||
func (g *Git) CloneBareWithReference(url, dest, reference string) error {
|
||||
cmd := exec.Command("git", "clone", "--bare", "--reference-if-able", reference, url, dest)
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return g.wrapError(err, stderr.String(), []string{"clone", "--bare", "--reference-if-able", url})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Checkout checks out the given ref.
|
||||
func (g *Git) Checkout(ref string) error {
|
||||
_, err := g.run("checkout", ref)
|
||||
|
||||
@@ -41,6 +41,53 @@ func initTestRepo(t *testing.T) string {
|
||||
return dir
|
||||
}
|
||||
|
||||
func TestIsRepo(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
g := NewGit(dir)
|
||||
|
||||
if g.IsRepo() {
|
||||
t.Fatal("expected IsRepo to be false for empty dir")
|
||||
}
|
||||
|
||||
cmd := exec.Command("git", "init")
|
||||
cmd.Dir = dir
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("git init: %v", err)
|
||||
}
|
||||
|
||||
if !g.IsRepo() {
|
||||
t.Fatal("expected IsRepo to be true after git init")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloneWithReferenceCreatesAlternates(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
src := filepath.Join(tmp, "src")
|
||||
dst := filepath.Join(tmp, "dst")
|
||||
|
||||
if err := exec.Command("git", "init", src).Run(); err != nil {
|
||||
t.Fatalf("init src: %v", err)
|
||||
}
|
||||
_ = exec.Command("git", "-C", src, "config", "user.email", "test@test.com").Run()
|
||||
_ = exec.Command("git", "-C", src, "config", "user.name", "Test User").Run()
|
||||
|
||||
if err := os.WriteFile(filepath.Join(src, "README.md"), []byte("# Test\n"), 0644); err != nil {
|
||||
t.Fatalf("write file: %v", err)
|
||||
}
|
||||
_ = exec.Command("git", "-C", src, "add", ".").Run()
|
||||
_ = exec.Command("git", "-C", src, "commit", "-m", "initial").Run()
|
||||
|
||||
g := NewGit(tmp)
|
||||
if err := g.CloneWithReference(src, dst, src); err != nil {
|
||||
t.Fatalf("CloneWithReference: %v", err)
|
||||
}
|
||||
|
||||
alternates := filepath.Join(dst, ".git", "objects", "info", "alternates")
|
||||
if _, err := os.Stat(alternates); err != nil {
|
||||
t.Fatalf("expected alternates file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentBranch(t *testing.T) {
|
||||
dir := initTestRepo(t)
|
||||
g := NewGit(dir)
|
||||
|
||||
Reference in New Issue
Block a user