internal/gitutil/commitfile_test.go

4a511e42574ead9c92dc73fd320f296cc4dbea07
gitbay/internal/gitutil/commitfile_test.go

83 lines · 2752 bytes

3 symbols in this file
 1package gitutil
 2
 3import "testing"
 4
 5func TestCommitFileChangeEmptyRepo(t *testing.T) {
 6	dir := t.TempDir()
 7	if err := InitBare(dir, "main", ""); err != nil {
 8		t.Fatal(err)
 9	}
10	sha, err := CommitFileChange(dir, "main", "profile/README.md",
11		[]byte("# hello\n"), "alice", "alice@example.org", "add about")
12	if err != nil {
13		t.Fatalf("first commit into an empty repository: %v", err)
14	}
15	if sha == "" {
16		t.Fatal("no sha returned")
17	}
18	raw, err := ReadBlob(dir, "main", "profile/README.md", 1<<20)
19	if err != nil {
20		t.Fatalf("reading it back: %v", err)
21	}
22	if string(raw) != "# hello\n" {
23		t.Errorf("read back %q", raw)
24	}
25
26	// The second commit still takes the parented path.
27	if _, err := CommitFileChange(dir, "main", "profile/README.md",
28		[]byte("# hello again\n"), "alice", "alice@example.org", "edit"); err != nil {
29		t.Fatalf("second commit: %v", err)
30	}
31
32	// An unknown branch in a repository that has history is a typo, not a
33	// new orphan branch.
34	if _, err := CommitFileChange(dir, "nope", "x.md",
35		[]byte("x"), "alice", "alice@example.org", "x"); err == nil {
36		t.Error("committing to an unknown branch of a non-empty repository succeeded")
37	}
38}
39
40// --from creates a branch; it never commits onto one that exists, and a
41// branch created meanwhile is not overwritten.
42func TestCommitFileFromCreatesOnly(t *testing.T) {
43	dir := t.TempDir()
44	if err := InitBare(dir, "main", ""); err != nil {
45		t.Fatal(err)
46	}
47	if _, err := CommitFileChange(dir, "main", "a.md", []byte("a\n"), "alice", "alice@example.org", "a"); err != nil {
48		t.Fatal(err)
49	}
50	if _, err := CommitFileFrom(dir, "topic", "main", "a.md", []byte("b\n"), "alice", "alice@example.org", "b"); err != nil {
51		t.Fatalf("creating topic from main: %v", err)
52	}
53	if _, err := CommitFileFrom(dir, "topic", "main", "a.md", []byte("c\n"), "alice", "alice@example.org", "c"); err == nil {
54		t.Error("--from committed onto an existing branch")
55	}
56	raw, _ := ReadBlob(dir, "topic", "a.md", 1<<20)
57	if string(raw) != "b\n" {
58		t.Errorf("topic holds %q, want %q", raw, "b\n")
59	}
60	main, _ := ResolveRef(dir, "refs/heads/main")
61	if err := updateRefCreate(dir, "refs/heads/topic", main); err == nil {
62		t.Error("updateRefCreate moved an existing ref")
63	}
64}
65
66func TestFirstParents(t *testing.T) {
67	dir := t.TempDir()
68	if err := InitBare(dir, "main", ""); err != nil {
69		t.Fatal(err)
70	}
71	a, err := CommitFileChange(dir, "main", "a.md", []byte("a\n"), "alice", "alice@example.org", "a")
72	if err != nil {
73		t.Fatal(err)
74	}
75	b, err := CommitFileChange(dir, "main", "a.md", []byte("b\n"), "alice", "alice@example.org", "b")
76	if err != nil {
77		t.Fatal(err)
78	}
79	got := FirstParents(dir, []string{b, a})
80	if got[b] != a || len(got) != 1 {
81		t.Errorf("FirstParents = %v, want {%s: %s}", got, b, a)
82	}
83}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form