package git import ( "context" "os" "os/exec" "path/filepath" "testing" ) func TestValidateRefAndCleanSubPath(t *testing.T) { if err := ValidateRef("main"); err != nil { t.Fatalf("expected 'main' to be valid ref: %v", err) } if err := ValidateRef("feature/ui-kit"); err != nil { t.Fatalf("expected 'feature/ui-kit' to be valid ref: %v", err) } for _, bad := range []string{"", "-flag", "a..b", "ref.lock", "ref with space"} { if err := ValidateRef(bad); err == nil { t.Fatalf("expected ref %q to be rejected", bad) } } if p, err := CleanSubPath("internal/web/views.go"); err != nil || p != "internal/web/views.go" { t.Fatalf("CleanSubPath failed: p=%q err=%v", p, err) } for _, badPath := range []string{"../secret", "a/../../b", "-flag"} { if _, err := CleanSubPath(badPath); err == nil { t.Fatalf("expected path %q to be rejected", badPath) } } } func TestBareRepoReadAndDiff(t *testing.T) { ctx := context.Background() tmp := t.TempDir() barePath := filepath.Join(tmp, "repo.git") if err := InitBare(ctx, barePath, "main"); err != nil { t.Fatalf("InitBare failed: %v", err) } _, isEmpty := EnsureDefaultBranch(ctx, barePath, "main") if !isEmpty { t.Fatalf("expected newly initialized bare repo to be empty") } // Create a working repo and push a commit into barePath work := filepath.Join(tmp, "work") if err := os.MkdirAll(filepath.Join(work, "pkg"), 0o755); err != nil { t.Fatalf("mkdir work: %v", err) } run := func(args ...string) { t.Helper() cmd := exec.CommandContext(ctx, "git", args...) cmd.Dir = work if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("git %v: %v (%s)", args, err, string(out)) } } run("init", "-b", "main") run("config", "user.name", "Alice") run("config", "user.email", "alice@example.com") _ = os.WriteFile(filepath.Join(work, "README.md"), []byte("# Test Repo\n"), 0o644) _ = os.WriteFile(filepath.Join(work, "pkg", "hello.go"), []byte("package pkg\n\nfunc Hello() string { return \"hi\" }\n"), 0o644) run("add", ".") run("commit", "-m", "Add README and hello.go") run("push", barePath, "main") branch, isEmpty := EnsureDefaultBranch(ctx, barePath, "main") if isEmpty || branch != "main" { t.Fatalf("expected non-empty repo on branch main, got branch=%q isEmpty=%v", branch, isEmpty) } entries, err := ListTree(ctx, barePath, "main", "") if err != nil || len(entries) != 2 { t.Fatalf("ListTree failed: len=%d err=%v", len(entries), err) } // Directory "pkg" should be sorted before file "README.md" if entries[0].Type != "tree" || entries[0].Name != "pkg" { t.Fatalf("expected directory 'pkg' first, got %+v", entries[0]) } readme, err := FindReadme(ctx, barePath, "main", "") if err != nil || readme.Name != "README.md" { t.Fatalf("FindReadme failed: %v", err) } blob, err := ReadBlob(ctx, barePath, "main", "pkg/hello.go") if err != nil || blob.IsBinary || blob.LineCount != 3 { t.Fatalf("ReadBlob failed: %+v err=%v", blob, err) } commits, err := ListCommits(ctx, barePath, "main", 10, 0) if err != nil || len(commits) != 1 { t.Fatalf("ListCommits failed: len=%d err=%v", len(commits), err) } detail, err := GetCommitDiff(ctx, barePath, commits[0].Hash) if err != nil || detail.FilesChanged != 2 || detail.TotalAdditions < 4 { t.Fatalf("GetCommitDiff failed: %+v err=%v", detail, err) } }