Fast implementation of Git in pure Go
1package object_test
2
3import (
4 "bytes"
5 "fmt"
6 "testing"
7
8 "codeberg.org/lindenii/furgit/internal/testgit"
9 "codeberg.org/lindenii/furgit/object"
10 "codeberg.org/lindenii/furgit/objectid"
11)
12
13func TestCommitParseFromGit(t *testing.T) {
14 t.Parallel()
15 testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
16 testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
17 _, treeID, commitID := testRepo.MakeCommit(t, "subject\n\nbody")
18
19 rawBody := testRepo.CatFile(t, "commit", commitID)
20
21 commit, err := object.ParseCommit(rawBody, algo)
22 if err != nil {
23 t.Fatalf("ParseCommit: %v", err)
24 }
25
26 if commit.Tree != treeID {
27 t.Fatalf("tree id mismatch: got %s want %s", commit.Tree, treeID)
28 }
29
30 if len(commit.Parents) != 0 {
31 t.Fatalf("parent count = %d, want 0", len(commit.Parents))
32 }
33
34 if !bytes.Equal(commit.Author.Name, []byte("Test Author")) {
35 t.Fatalf("author name = %q, want %q", commit.Author.Name, "Test Author")
36 }
37
38 if !bytes.Equal(commit.Committer.Name, []byte("Test Committer")) {
39 t.Fatalf("committer name = %q, want %q", commit.Committer.Name, "Test Committer")
40 }
41
42 if !bytes.Contains(commit.Message, []byte("subject")) {
43 t.Fatalf("commit message missing subject: %q", commit.Message)
44 }
45 })
46}
47
48func TestCommitParseMultipleParents(t *testing.T) {
49 t.Parallel()
50 testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
51 testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
52
53 _, treeID := testRepo.MakeSingleFileTree(t, "file.txt", []byte("merge-content\n"))
54 parent1 := testRepo.CommitTree(t, treeID, "parent-one")
55 parent2 := testRepo.CommitTree(t, treeID, "parent-two", parent1)
56
57 rawCommit := fmt.Sprintf(
58 "tree %s\nparent %s\nparent %s\nauthor Test Author <test@example.org> 1234567890 +0000\ncommitter Test Committer <committer@example.org> 1234567890 +0000\n\nMerge commit\n",
59 treeID,
60 parent1,
61 parent2,
62 )
63 mergeID := testRepo.HashObject(t, "commit", []byte(rawCommit))
64 rawBody := testRepo.CatFile(t, "commit", mergeID)
65
66 commit, err := object.ParseCommit(rawBody, algo)
67 if err != nil {
68 t.Fatalf("ParseCommit(merge): %v", err)
69 }
70
71 if commit.Tree != treeID {
72 t.Fatalf("merge tree = %s, want %s", commit.Tree, treeID)
73 }
74
75 if len(commit.Parents) != 2 {
76 t.Fatalf("merge parent count = %d, want 2", len(commit.Parents))
77 }
78
79 if commit.Parents[0] != parent1 {
80 t.Fatalf("merge parent[0] = %s, want %s", commit.Parents[0], parent1)
81 }
82
83 if commit.Parents[1] != parent2 {
84 t.Fatalf("merge parent[1] = %s, want %s", commit.Parents[1], parent2)
85 }
86
87 if !bytes.Equal(commit.Message, []byte("Merge commit\n")) {
88 t.Fatalf("merge message = %q, want %q", commit.Message, "Merge commit\n")
89 }
90 })
91}