| 1 |
package git |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/go-git/go-billy/v5/memfs" |
| 10 |
"github.com/go-git/go-billy/v5/osfs" |
| 11 |
"github.com/go-git/go-billy/v5/util" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 13 |
"github.com/go-git/go-git/v5/storage/filesystem" |
| 14 |
"github.com/go-git/go-git/v5/storage/memory" |
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
) |
| 18 |
|
| 19 |
// For additional context: #1159. |
| 20 |
func TestIndexEntrySizeUpdatedForNonRegularFiles(t *testing.T) { |
| 21 |
w := osfs.New(t.TempDir(), osfs.WithBoundOS()) |
| 22 |
dot, err := w.Chroot(GitDirName) |
| 23 |
require.NoError(t, err) |
| 24 |
|
| 25 |
s := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) |
| 26 |
r, err := Init(s, w) |
| 27 |
require.NoError(t, err) |
| 28 |
require.NotNil(t, r) |
| 29 |
|
| 30 |
wt, err := r.Worktree() |
| 31 |
require.NoError(t, err) |
| 32 |
require.NotNil(t, wt) |
| 33 |
|
| 34 |
file := "LICENSE" |
| 35 |
f, err := w.OpenFile(file, os.O_CREATE|os.O_WRONLY, 0o666) |
| 36 |
require.NoError(t, err) |
| 37 |
require.NotNil(t, f) |
| 38 |
|
| 39 |
content := []byte(strings.Repeat("a\n", 1000)) |
| 40 |
_, err = f.Write(content) |
| 41 |
require.NoError(t, err) |
| 42 |
err = f.Close() |
| 43 |
require.NoError(t, err) |
| 44 |
|
| 45 |
_, err = wt.Add(file) |
| 46 |
require.NoError(t, err) |
| 47 |
|
| 48 |
_, err = wt.Commit("add file", &CommitOptions{}) |
| 49 |
require.NoError(t, err) |
| 50 |
|
| 51 |
st, err := wt.StatusWithOptions(StatusOptions{Strategy: Preload}) |
| 52 |
require.NoError(t, err) |
| 53 |
assert.Equal(t, |
| 54 |
&FileStatus{Worktree: Unmodified, Staging: Unmodified}, |
| 55 |
st.File(file)) |
| 56 |
|
| 57 |
// Make the file not regular. The same would apply to a transition |
| 58 |
// from regular file to symlink. |
| 59 |
err = os.Chmod(filepath.Join(w.Root(), file), 0o777) |
| 60 |
require.NoError(t, err) |
| 61 |
|
| 62 |
f, err = w.OpenFile(file, os.O_APPEND|os.O_RDWR, 0o777) |
| 63 |
require.NoError(t, err) |
| 64 |
require.NotNil(t, f) |
| 65 |
|
| 66 |
_, err = f.Write([]byte("\n\n")) |
| 67 |
require.NoError(t, err) |
| 68 |
err = f.Close() |
| 69 |
require.NoError(t, err) |
| 70 |
|
| 71 |
_, err = wt.Add(file) |
| 72 |
assert.NoError(t, err) |
| 73 |
|
| 74 |
// go-git's Status diverges from "git status", so this check does not |
| 75 |
// fail, even when the issue is present. As at this point "git status" |
| 76 |
// reports the unstaged file was modified while "git diff" would return |
| 77 |
// empty, as the files are the same but the index has the incorrect file |
| 78 |
// size. |
| 79 |
st, err = wt.StatusWithOptions(StatusOptions{Strategy: Preload}) |
| 80 |
assert.NoError(t, err) |
| 81 |
assert.Equal(t, |
| 82 |
&FileStatus{Worktree: Unmodified, Staging: Modified}, |
| 83 |
st.File(file)) |
| 84 |
|
| 85 |
idx, err := wt.r.Storer.Index() |
| 86 |
assert.NoError(t, err) |
| 87 |
require.NotNil(t, idx) |
| 88 |
require.Len(t, idx.Entries, 1) |
| 89 |
|
| 90 |
// Check whether the index was updated with the two new line breaks. |
| 91 |
assert.Equal(t, uint32(len(content)+2), idx.Entries[0].Size) |
| 92 |
} |
| 93 |
|
| 94 |
// TestAddSubdirectoryForwardSlash verifies that Add("dir/foo") stores the |
| 95 |
// index entry with a forward-slash path. On Windows filepath.Clean converts |
| 96 |
// "dir/foo" to "dir\foo"; without filepath.ToSlash the cleaned path reaches |
| 97 |
// a filesystem, i.e. billy, which can treat the backslash as a literal |
| 98 |
// character rather than a separator. |
| 99 |
func TestAddSubdirectoryForwardSlash(t *testing.T) { |
| 100 |
fs := memfs.New() |
| 101 |
r, err := Init(memory.NewStorage(), fs) |
| 102 |
require.NoError(t, err) |
| 103 |
|
| 104 |
w, err := r.Worktree() |
| 105 |
require.NoError(t, err) |
| 106 |
|
| 107 |
err = util.WriteFile(w.Filesystem, "dir/foo", []byte("content"), 0644) |
| 108 |
require.NoError(t, err) |
| 109 |
|
| 110 |
_, err = w.Add("dir/foo") |
| 111 |
require.NoError(t, err) |
| 112 |
|
| 113 |
idx, err := r.Storer.Index() |
| 114 |
require.NoError(t, err) |
| 115 |
e, err := idx.Entry("dir/foo") |
| 116 |
require.NoError(t, err) |
| 117 |
assert.Equal(t, "dir/foo", e.Name) |
| 118 |
} |