package gitcmd import ( "context" "path/filepath" "strings" "testing" ) func TestInitBare(t *testing.T) { requireGit(t) dir := t.TempDir() bare := filepath.Join(dir, "nested", "sub", "repo.git") if err := InitBare(context.Background(), bare); err != nil { t.Fatalf("InitBare: %v", err) } r, err := OpenRepository(context.Background(), bare) if err != nil { t.Fatalf("OpenRepository: %v", err) } empty, err := r.IsEmpty() if err != nil || !empty { t.Errorf("freshly init'd bare IsEmpty = (%v, %v), want (true, nil)", empty, err) } out, _, err := NewCommand("rev-parse", "--is-bare-repository").WithDir(bare).RunStdString(context.Background()) if err != nil { t.Fatal(err) } if got := strings.TrimSpace(out); got != "true" { t.Errorf("is-bare-repository = %q, want true", got) } } func TestCloneBare(t *testing.T) { f := newFixture(t) dir := t.TempDir() dest := filepath.Join(dir, "nested", "clone.git") if err := CloneBare(context.Background(), f.repo.Path, dest); err != nil { t.Fatalf("CloneBare: %v", err) } r, err := OpenRepository(context.Background(), dest) if err != nil { t.Fatalf("OpenRepository clone: %v", err) } cs, err := r.ListCommits("HEAD", 0) if err != nil { t.Fatal(err) } if len(cs) != 5 { t.Errorf("clone commit count = %d, want 5", len(cs)) } if cs[0].ID != f.c5 { t.Errorf("clone HEAD = %s, want %s", cs[0].ID, f.c5) } bs, err := r.ListBranches() if err != nil { t.Fatal(err) } if len(bs) != 2 { t.Errorf("clone branch count = %d, want 2", len(bs)) } ts, err := r.ListTags() if err != nil { t.Fatal(err) } if len(ts) != 2 { t.Errorf("clone tag count = %d, want 2", len(ts)) } } func TestCloneBareMissingSrc(t *testing.T) { dir := t.TempDir() dest := filepath.Join(dir, "clone.git") err := CloneBare(context.Background(), filepath.Join(dir, "no-such-src.git"), dest) if err == nil { t.Fatal("expected error cloning from a missing source, got nil") } }