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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
package repository_test
import (
"fmt"
"strings"
"testing"
"codeberg.org/lindenii/furgit/internal/testgit"
"codeberg.org/lindenii/furgit/object"
"codeberg.org/lindenii/furgit/objectid"
)
func TestReadStoredTyped(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
repoHarness := testgit.NewRepo(t, testgit.RepoOptions{
ObjectFormat: algo,
Bare: true,
RefFormat: "files",
})
blobID, treeID, commitID := repoHarness.MakeCommit(t, "stored types")
repo := repoHarness.OpenRepository(t)
blob, err := repo.Resolver().ExactBlob(blobID)
if err != nil {
t.Fatalf("ExactBlob: %v", err)
}
if blob.ID() != blobID {
t.Fatalf("blob ID = %s, want %s", blob.ID(), blobID)
}
if string(blob.Object().Data) != "commit-body\n" {
t.Fatalf("blob body = %q, want %q", blob.Object().Data, "commit-body\n")
}
tree, err := repo.Resolver().ExactTree(treeID)
if err != nil {
t.Fatalf("ExactTree: %v", err)
}
if tree.ID() != treeID {
t.Fatalf("tree ID = %s, want %s", tree.ID(), treeID)
}
if len(tree.Object().Entries) != 1 {
t.Fatalf("tree entries = %d, want 1", len(tree.Object().Entries))
}
commit, err := repo.Resolver().ExactCommit(commitID)
if err != nil {
t.Fatalf("ExactCommit: %v", err)
}
if commit.ID() != commitID {
t.Fatalf("commit ID = %s, want %s", commit.ID(), commitID)
}
if commit.Object().Tree != treeID {
t.Fatalf("commit tree = %s, want %s", commit.Object().Tree, treeID)
}
})
}
func TestResolverPath(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
repoHarness := testgit.NewRepo(t, testgit.RepoOptions{
ObjectFormat: algo,
Bare: true,
RefFormat: "files",
})
blobID := repoHarness.HashObject(t, "blob", []byte("nested-file\n"))
childTreeID := repoHarness.Mktree(t, fmt.Sprintf("100644 blob %s\tleaf.txt\n", blobID))
rootTreeID := repoHarness.Mktree(t, fmt.Sprintf("040000 tree %s\tdir\n", childTreeID))
repo := repoHarness.OpenRepository(t)
entry, err := repo.Resolver().Path(rootTreeID, [][]byte{[]byte("dir"), []byte("leaf.txt")})
if err != nil {
t.Fatalf("Path: %v", err)
}
if entry.Mode != object.FileModeRegular {
t.Fatalf("Path mode = %o, want %o", entry.Mode, object.FileModeRegular)
}
if entry.ID != blobID {
t.Fatalf("Path id = %s, want %s", entry.ID, blobID)
}
})
}
func TestResolverPathErrors(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
t.Run("missing path component", func(t *testing.T) {
t.Parallel()
repoHarness := testgit.NewRepo(t, testgit.RepoOptions{
ObjectFormat: algo,
Bare: true,
RefFormat: "files",
})
blobID := repoHarness.HashObject(t, "blob", []byte("body\n"))
rootTreeID := repoHarness.Mktree(t, fmt.Sprintf("100644 blob %s\tfile.txt\n", blobID))
repo := repoHarness.OpenRepository(t)
_, err := repo.Resolver().Path(rootTreeID, [][]byte{[]byte("missing")})
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Fatalf("Path missing: err = %v, want not found error", err)
}
})
t.Run("non-tree intermediate", func(t *testing.T) {
t.Parallel()
repoHarness := testgit.NewRepo(t, testgit.RepoOptions{
ObjectFormat: algo,
Bare: true,
RefFormat: "files",
})
blobID := repoHarness.HashObject(t, "blob", []byte("body\n"))
rootTreeID := repoHarness.Mktree(t, fmt.Sprintf("100644 blob %s\tdir\n", blobID))
repo := repoHarness.OpenRepository(t)
_, err := repo.Resolver().Path(rootTreeID, [][]byte{[]byte("dir"), []byte("leaf")})
if err == nil || !strings.Contains(err.Error(), "is not a tree") {
t.Fatalf("Path non-tree: err = %v, want non-tree error", err)
}
})
})
}
func TestResolverPathDeepPath(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
const depth = 50
repoHarness := testgit.NewRepo(t, testgit.RepoOptions{
ObjectFormat: algo,
Bare: true,
RefFormat: "files",
})
leafBlobID := repoHarness.HashObject(t, "blob", []byte("deep-content\n"))
currentTree := repoHarness.Mktree(t, fmt.Sprintf("100644 blob %s\tleaf.txt\n", leafBlobID))
parts := make([][]byte, 0, depth+1)
for i := depth - 1; i >= 0; i-- {
name := fmt.Sprintf("level%02d", i)
currentTree = repoHarness.Mktree(t, fmt.Sprintf("040000 tree %s\t%s\n", currentTree, name))
parts = append([][]byte{[]byte(name)}, parts...)
}
parts = append(parts, []byte("leaf.txt"))
repo := repoHarness.OpenRepository(t)
entry, err := repo.Resolver().Path(currentTree, parts)
if err != nil {
t.Fatalf("Path(deep): %v", err)
}
if entry.Mode != object.FileModeRegular {
t.Fatalf("Path(deep) mode = %o, want %o", entry.Mode, object.FileModeRegular)
}
if entry.ID != leafBlobID {
t.Fatalf("Path(deep) id = %s, want %s", entry.ID, leafBlobID)
}
})
}
func TestReadStoredTreeMixedModes(t *testing.T) {
t.Parallel()
testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
repoHarness := testgit.NewRepo(t, testgit.RepoOptions{
ObjectFormat: algo,
Bare: true,
RefFormat: "files",
})
normalID := repoHarness.HashObject(t, "blob", []byte("normal-file\n"))
execID := repoHarness.HashObject(t, "blob", []byte("#!/bin/sh\necho hi\n"))
symID := repoHarness.HashObject(t, "blob", []byte("normal.txt"))
nestedBlobID := repoHarness.HashObject(t, "blob", []byte("nested\n"))
nestedTreeID := repoHarness.Mktree(t, fmt.Sprintf("100644 blob %s\tleaf.txt\n", nestedBlobID))
rootTreeID := repoHarness.Mktree(t,
fmt.Sprintf(
"100644 blob %s\tnormal.txt\n100755 blob %s\trun.sh\n120000 blob %s\tlink.txt\n040000 tree %s\tdir\n",
normalID,
execID,
symID,
nestedTreeID,
),
)
repo := repoHarness.OpenRepository(t)
rootTree, err := repo.Resolver().ExactTree(rootTreeID)
if err != nil {
t.Fatalf("ExactTree(root): %v", err)
}
expect := map[string]object.FileMode{
"normal.txt": object.FileModeRegular,
"run.sh": object.FileModeExecutable,
"link.txt": object.FileModeSymlink,
"dir": object.FileModeDir,
}
for name, wantMode := range expect {
entry := rootTree.Object().Entry([]byte(name))
if entry == nil {
t.Fatalf("Entry(%q) returned nil", name)
}
if entry.Mode != wantMode {
t.Fatalf("Entry(%q) mode = %o, want %o", name, entry.Mode, wantMode)
}
}
})
}
|