aboutsummaryrefslogtreecommitdiff
path: root/pack_test.go
blob: 184a4e5cb50512e1205f25c2a3811e3262e696c8 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package furgit

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestPackfileRead(t *testing.T) {
	repoPath, cleanup := setupTestRepo(t)
	defer cleanup()

	gitCmd(t, repoPath, "config", "gc.auto", "0")

	workDir, cleanupWork := setupWorkDir(t)
	defer cleanupWork()

	err := os.WriteFile(filepath.Join(workDir, "file1.txt"), []byte("content1"), 0o644)
	if err != nil {
		t.Fatalf("failed to write file1.txt: %v", err)
	}
	err = os.WriteFile(filepath.Join(workDir, "file2.txt"), []byte("content2"), 0o644)
	if err != nil {
		t.Fatalf("failed to write file2.txt: %v", err)
	}

	gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".")
	gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "Test commit")
	commitHash := gitCmd(t, repoPath, "rev-parse", "HEAD")

	gitCmd(t, repoPath, "repack", "-a", "-d")

	repo, err := OpenRepository(repoPath)
	if err != nil {
		t.Fatalf("OpenRepository failed: %v", err)
	}
	defer func() { _ = repo.Close() }()

	hashObj, _ := repo.ParseHash(commitHash)
	obj, err := repo.ReadObject(hashObj)
	if err != nil {
		t.Fatalf("ReadObject from pack failed: %v", err)
	}

	commit, ok := obj.(*StoredCommit)
	if !ok {
		t.Fatalf("expected *StoredCommit, got %T", obj)
	}

	treeObj, err := repo.ReadObject(commit.Tree)
	if err != nil {
		t.Fatalf("ReadObject tree failed: %v", err)
	}

	tree, ok := treeObj.(*StoredTree)
	if !ok {
		t.Fatalf("expected *StoredTree, got %T", treeObj)
	}

	if len(tree.Entries) != 2 {
		t.Errorf("tree entries: got %d, want 2", len(tree.Entries))
	}

	gitLsTree := gitCmd(t, repoPath, "ls-tree", commit.Tree.String())
	for _, entry := range tree.Entries {
		if !strings.Contains(gitLsTree, string(entry.Name)) {
			t.Errorf("git ls-tree doesn't contain %s", entry.Name)
		}
	}
}

func TestPackfileLarge(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping large packfile test in short mode")
	}

	repoPath, cleanup := setupTestRepo(t)
	defer cleanup()

	gitCmd(t, repoPath, "config", "gc.auto", "0")

	workDir, cleanupWork := setupWorkDir(t)
	defer cleanupWork()

	numFiles := 1000
	for i := 0; i < numFiles; i++ {
		filename := filepath.Join(workDir, fmt.Sprintf("file%04d.txt", i))
		content := fmt.Sprintf("Content for file %d\n", i)
		err := os.WriteFile(filename, []byte(content), 0o644)
		if err != nil {
			t.Fatalf("failed to write %s: %v", filename, err)
		}
	}

	gitCmd(t, repoPath, "--work-tree="+workDir, "add", ".")
	gitCmd(t, repoPath, "--work-tree="+workDir, "commit", "-m", "Large commit")
	commitHash := gitCmd(t, repoPath, "rev-parse", "HEAD")

	gitCmd(t, repoPath, "repack", "-a", "-d")

	repo, err := OpenRepository(repoPath)
	if err != nil {
		t.Fatalf("OpenRepository failed: %v", err)
	}
	defer func() { _ = repo.Close() }()

	hashObj, _ := repo.ParseHash(commitHash)
	obj, _ := repo.ReadObject(hashObj)
	commit := obj.(*StoredCommit)

	treeObj, _ := repo.ReadObject(commit.Tree)
	tree := treeObj.(*StoredTree)

	if len(tree.Entries) != numFiles {
		t.Errorf("tree entries: got %d, want %d", len(tree.Entries), numFiles)
	}

	gitCount := gitCmd(t, repoPath, "ls-tree", commit.Tree.String())
	gitLines := strings.Count(gitCount, "\n") + 1
	if len(tree.Entries) != gitLines {
		t.Errorf("furgit found %d entries, git found %d", len(tree.Entries), gitLines)
	}

	for i := 0; i < 10; i++ {
		idx := i * (numFiles / 10)
		expectedName := fmt.Sprintf("file%04d.txt", idx)
		entry := tree.Entry([]byte(expectedName))
		if entry == nil {
			t.Errorf("expected to find entry %s", expectedName)
			continue
		}

		blobObj, _ := repo.ReadObject(entry.ID)
		blob := blobObj.(*StoredBlob)

		expectedContent := fmt.Sprintf("Content for file %d\n", idx)
		if string(blob.Data) != expectedContent {
			t.Errorf("blob %s: got %q, want %q", expectedName, blob.Data, expectedContent)
		}

		gitData := gitCatFile(t, repoPath, "blob", entry.ID.String())
		if !bytes.Equal(blob.Data, gitData) {
			t.Errorf("blob %s: furgit data doesn't match git data", expectedName)
		}
	}
}