{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathworktree_status_test.go
More file actions
322 lines (268 loc) · 9.56 KB
/
Copy pathworktree_status_test.go
File metadata and controls
322 lines (268 loc) · 9.56 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package git
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/go-git/go-billy/v6/memfs"
"github.com/go-git/go-billy/v6/osfs"
"github.com/go-git/go-billy/v6/util"
fixtures "github.com/go-git/go-git-fixtures/v6"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-git/go-git/v6/plumbing/cache"
"github.com/go-git/go-git/v6/plumbing/object"
"github.com/go-git/go-git/v6/storage/filesystem"
)
// For additional context: #1159.
func TestIndexEntrySizeUpdatedForNonRegularFiles(t *testing.T) {
t.Parallel()
w := osfs.New(t.TempDir(), osfs.WithBoundOS())
dot, err := w.Chroot(GitDirName)
require.NoError(t, err)
s := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
r, err := Init(s, WithWorkTree(w))
require.NoError(t, err)
require.NotNil(t, r)
defer func() { _ = r.Close() }()
wt, err := r.Worktree()
require.NoError(t, err)
require.NotNil(t, wt)
file := "LICENSE"
f, err := w.OpenFile(file, os.O_CREATE|os.O_WRONLY, 0o666)
require.NoError(t, err)
require.NotNil(t, f)
content := []byte(strings.Repeat("a\n", 1000))
_, err = f.Write(content)
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)
_, err = wt.Add(file)
require.NoError(t, err)
_, err = wt.Commit("add file", &CommitOptions{})
require.NoError(t, err)
st, err := wt.StatusWithOptions(StatusOptions{Strategy: Preload})
require.NoError(t, err)
assert.Equal(t,
&FileStatus{Worktree: Unmodified, Staging: Unmodified},
st.File(file))
// Make the file not regular. The same would apply to a transition
// from regular file to symlink.
err = os.Chmod(filepath.Join(w.Root(), file), 0o777)
require.NoError(t, err)
f, err = w.OpenFile(file, os.O_APPEND|os.O_RDWR, 0o777)
require.NoError(t, err)
require.NotNil(t, f)
_, err = f.Write([]byte("\n\n"))
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)
_, err = wt.Add(file)
assert.NoError(t, err)
// go-git's Status diverges from "git status", so this check does not
// fail, even when the issue is present. As at this point "git status"
// reports the unstaged file was modified while "git diff" would return
// empty, as the files are the same but the index has the incorrect file
// size.
st, err = wt.StatusWithOptions(StatusOptions{Strategy: Preload})
assert.NoError(t, err)
assert.Equal(t,
&FileStatus{Worktree: Unmodified, Staging: Modified},
st.File(file))
idx, err := wt.r.Storer.Index()
assert.NoError(t, err)
require.NotNil(t, idx)
require.Len(t, idx.Entries, 1)
// Check whether the index was updated with the two new line breaks.
assert.Equal(t, uint32(len(content)+2), idx.Entries[0].Size)
}
// TestStatusReportsModifiedTrackedFileInIgnoredDirectory verifies that a
// file which is in the index but also matches a .gitignore rule (e.g. it
// was committed before the ignore rule was added) is still reported as
// Modified by Status(). The fast-path that skips ignored directories
// during the walk must descend into directories that contain tracked
// entries.
func TestStatusReportsModifiedTrackedFileInIgnoredDirectory(t *testing.T) {
t.Parallel()
repoDir := filepath.Join(t.TempDir(), "repo")
repo, err := PlainInit(repoDir, false)
require.NoError(t, err)
defer func() { _ = repo.Close() }()
wt, err := repo.Worktree()
require.NoError(t, err)
write := func(name string, data []byte) {
require.NoError(t, wt.Filesystem().MkdirAll(filepath.Dir(name), 0o755))
require.NoError(t, util.WriteFile(wt.Filesystem(), name, data, 0o644))
}
write("src/main.go", []byte("package main\n"))
write("vendor/keep.go", []byte("original\n"))
write(".gitignore", []byte("vendor/\n"))
for _, p := range []string{"src/main.go", "vendor/keep.go", ".gitignore"} {
_, err := wt.Add(p)
require.NoError(t, err)
}
sig := &object.Signature{Name: "test", Email: "test@test.com"}
_, err = wt.Commit("initial", &CommitOptions{Author: sig, Committer: sig})
require.NoError(t, err)
// Drop an untracked, ignored file alongside the tracked one. It must
// not appear in Status output.
write("vendor/extra.go", []byte("untracked\n"))
// Modify the tracked-but-ignored file. It MUST appear as Modified.
write("vendor/keep.go", []byte("changed\n"))
st, err := wt.Status()
require.NoError(t, err)
// Status.File auto-inserts a default entry for any path queried, so
// inspect the underlying map directly to assert presence/absence.
keep, ok := st["vendor/keep.go"]
require.True(t, ok, "tracked file inside an ignored directory must surface in Status")
assert.Equal(t, Modified, keep.Worktree, "tracked-but-ignored file must be reported as Modified")
_, ok = st["vendor/extra.go"]
assert.False(t, ok, "untracked file inside an ignored directory must not surface in Status")
}
func BenchmarkWorktreeStatus(b *testing.B) {
b.StopTimer()
f := fixtures.Basic().One()
dotgit, err := f.DotGit()
if err != nil {
b.Fatal(err)
}
st := filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault())
r, err := Open(st, memfs.New())
require.NoError(b, err)
defer func() { _ = r.Close() }()
wt, err := r.Worktree()
require.NoError(b, err)
err = wt.Reset(&ResetOptions{Mode: HardReset})
require.NoError(b, err)
b.StartTimer()
for b.Loop() {
wt.Status()
}
}
// TestAddSubdirectoryForwardSlash verifies that Add("dir/foo") stores the
// index entry with a forward-slash path. On Windows filepath.Clean converts
// "dir/foo" to "dir\foo"; without filepath.ToSlash the cleaned path would be
// stored in the index rather than the git-canonical forward-slash form.
func TestAddSubdirectoryForwardSlash(t *testing.T) {
t.Parallel()
repoDir := filepath.Join(t.TempDir(), "repo")
repo, err := PlainInit(repoDir, false)
require.NoError(t, err)
defer func() { _ = repo.Close() }()
wt, err := repo.Worktree()
require.NoError(t, err)
require.NoError(t, wt.Filesystem().MkdirAll("dir", 0o755))
require.NoError(t, util.WriteFile(wt.Filesystem(), "dir/foo", []byte("content"), 0o644))
_, err = wt.Add("dir/foo")
require.NoError(t, err)
idx, err := repo.Storer.Index()
require.NoError(t, err)
e, err := idx.Entry("dir/foo")
require.NoError(t, err)
assert.Equal(t, "dir/foo", e.Name)
}
// TestStatusMatchesReferenceGitForIgnoreLayouts compares the untracked set
// Status reports against `git ls-files --others --exclude-standard` over
// layouts that place ignore rules and negations at different depths. Ignore
// evaluation happens during the walk, so the check belongs at this level and
// not only against the gitignore package.
//
// Skipped under -short or without a git binary. Cases using a negation are
// skipped against Git 2.11.0, which CI builds and which does not honour
// re-include patterns in several positions.
func TestStatusMatchesReferenceGitForIgnoreLayouts(t *testing.T) {
t.Parallel()
if testing.Short() {
t.Skip("oracle disabled: -short")
}
if _, err := exec.LookPath("git"); err != nil {
t.Skipf("oracle disabled: git not found: %v", err)
}
for _, tc := range []struct {
name string
files map[string]string
}{{
name: "negation below an excluded directory, re-excluded by a nested rule",
files: map[string]string{
".gitignore": "outer/ignored/\n!outer/ignored/keep.txt\n",
"outer/ignored/.gitignore": "keep.txt\n",
"outer/ignored/keep.txt": "x\n",
},
}, {
name: "negation below an excluded directory",
files: map[string]string{
".gitignore": "outer/ignored/\n!outer/ignored/keep.txt\n",
"outer/ignored/keep.txt": "x\n",
},
}, {
name: "negation inside an excluded directory",
files: map[string]string{
".gitignore": "outer/ignored/\n",
"outer/ignored/.gitignore": "!keep.txt\n",
"outer/ignored/keep.txt": "x\n",
},
}, {
name: "rule declared in a subdirectory",
files: map[string]string{
"outer/.gitignore": "ignored/\n",
"outer/ignored/deep/f.txt": "x\n",
"outer/keep.txt": "x\n",
},
}, {
name: "rule names a grandchild",
files: map[string]string{
".gitignore": "outer/ignored/\n",
"outer/ignored/deep/f.txt": "x\n",
"outer/keep.txt": "x\n",
},
}, {
name: "children excluded but one re-included",
files: map[string]string{
".gitignore": "foo/*\n!foo/bar\n",
"foo/bar/baz.txt": "x\n",
"foo/other.txt": "x\n",
},
}} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if os.Getenv("GIT_VERSION") == "v2.11.0" {
for _, content := range tc.files {
if strings.Contains(content, "!") {
t.Skip("oracle disabled: Git 2.11.0 does not honour re-include patterns")
}
}
}
dir := filepath.Join(t.TempDir(), "repo")
require.NoError(t, os.MkdirAll(dir, 0o755))
require.NoError(t, exec.Command("git", "-c", "init.defaultBranch=main", "-C", dir, "init", "-q").Run())
for p, content := range tc.files {
abs := filepath.Join(dir, filepath.FromSlash(p))
require.NoError(t, os.MkdirAll(filepath.Dir(abs), 0o755))
require.NoError(t, os.WriteFile(abs, []byte(content), 0o644))
}
out, err := exec.Command("git", "-C", dir, "ls-files", "--others", "--exclude-standard").Output()
require.NoError(t, err)
want := map[string]bool{}
for line := range strings.SplitSeq(strings.TrimSpace(string(out)), "\n") {
if line != "" {
want[line] = true
}
}
repo, err := PlainOpen(dir)
require.NoError(t, err)
defer func() { _ = repo.Close() }()
wt, err := repo.Worktree()
require.NoError(t, err)
st, err := wt.Status()
require.NoError(t, err)
got := map[string]bool{}
for path, s := range st {
if s.Worktree == Untracked {
got[path] = true
}
}
assert.Equal(t, want, got, "untracked set must match reference git")
})
}
}
You can’t perform that action at this time.
