rin
Public Open-source lightweight git hosting.
git_test.go · 105 lines · 3.3 KB
Raw
1 package git
2
3 import (
4 "context"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "testing"
9 )
10
11 func TestValidateRefAndCleanSubPath(t *testing.T) {
12 if err := ValidateRef("main"); err != nil {
13 t.Fatalf("expected 'main' to be valid ref: %v", err)
14 }
15 if err := ValidateRef("feature/ui-kit"); err != nil {
16 t.Fatalf("expected 'feature/ui-kit' to be valid ref: %v", err)
17 }
18 for _, bad := range []string{"", "-flag", "a..b", "ref.lock", "ref with space"} {
19 if err := ValidateRef(bad); err == nil {
20 t.Fatalf("expected ref %q to be rejected", bad)
21 }
22 }
23
24 if p, err := CleanSubPath("internal/web/views.go"); err != nil || p != "internal/web/views.go" {
25 t.Fatalf("CleanSubPath failed: p=%q err=%v", p, err)
26 }
27 for _, badPath := range []string{"../secret", "a/../../b", "-flag"} {
28 if _, err := CleanSubPath(badPath); err == nil {
29 t.Fatalf("expected path %q to be rejected", badPath)
30 }
31 }
32 }
33
34 func TestBareRepoReadAndDiff(t *testing.T) {
35 ctx := context.Background()
36 tmp := t.TempDir()
37 barePath := filepath.Join(tmp, "repo.git")
38
39 if err := InitBare(ctx, barePath, "main"); err != nil {
40 t.Fatalf("InitBare failed: %v", err)
41 }
42
43 _, isEmpty := EnsureDefaultBranch(ctx, barePath, "main")
44 if !isEmpty {
45 t.Fatalf("expected newly initialized bare repo to be empty")
46 }
47
48 // Create a working repo and push a commit into barePath
49 work := filepath.Join(tmp, "work")
50 if err := os.MkdirAll(filepath.Join(work, "pkg"), 0o755); err != nil {
51 t.Fatalf("mkdir work: %v", err)
52 }
53
54 run := func(args ...string) {
55 t.Helper()
56 cmd := exec.CommandContext(ctx, "git", args...)
57 cmd.Dir = work
58 if out, err := cmd.CombinedOutput(); err != nil {
59 t.Fatalf("git %v: %v (%s)", args, err, string(out))
60 }
61 }
62
63 run("init", "-b", "main")
64 run("config", "user.name", "Alice")
65 run("config", "user.email", "alice@example.com")
66 _ = os.WriteFile(filepath.Join(work, "README.md"), []byte("# Test Repo\n"), 0o644)
67 _ = os.WriteFile(filepath.Join(work, "pkg", "hello.go"), []byte("package pkg\n\nfunc Hello() string { return \"hi\" }\n"), 0o644)
68 run("add", ".")
69 run("commit", "-m", "Add README and hello.go")
70 run("push", barePath, "main")
71
72 branch, isEmpty := EnsureDefaultBranch(ctx, barePath, "main")
73 if isEmpty || branch != "main" {
74 t.Fatalf("expected non-empty repo on branch main, got branch=%q isEmpty=%v", branch, isEmpty)
75 }
76
77 entries, err := ListTree(ctx, barePath, "main", "")
78 if err != nil || len(entries) != 2 {
79 t.Fatalf("ListTree failed: len=%d err=%v", len(entries), err)
80 }
81 // Directory "pkg" should be sorted before file "README.md"
82 if entries[0].Type != "tree" || entries[0].Name != "pkg" {
83 t.Fatalf("expected directory 'pkg' first, got %+v", entries[0])
84 }
85
86 readme, err := FindReadme(ctx, barePath, "main", "")
87 if err != nil || readme.Name != "README.md" {
88 t.Fatalf("FindReadme failed: %v", err)
89 }
90
91 blob, err := ReadBlob(ctx, barePath, "main", "pkg/hello.go")
92 if err != nil || blob.IsBinary || blob.LineCount != 3 {
93 t.Fatalf("ReadBlob failed: %+v err=%v", blob, err)
94 }
95
96 commits, err := ListCommits(ctx, barePath, "main", 10, 0)
97 if err != nil || len(commits) != 1 {
98 t.Fatalf("ListCommits failed: len=%d err=%v", len(commits), err)
99 }
100
101 detail, err := GetCommitDiff(ctx, barePath, commits[0].Hash)
102 if err != nil || detail.FilesChanged != 2 || detail.TotalAdditions < 4 {
103 t.Fatalf("GetCommitDiff failed: %+v err=%v", detail, err)
104 }
105 }
106