-
Notifications
You must be signed in to change notification settings - Fork 927
Expand file tree
/
Copy pathworktree_status_test.go
More file actions
119 lines (94 loc) · 2.94 KB
/
worktree_status_test.go
File metadata and controls
119 lines (94 loc) · 2.94 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
package git
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/go-git/go-billy/v6/memfs"
"github.com/go-git/go-billy/v6/osfs"
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/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)
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)
}
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)
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()
}
}