8000 utils: fix diff so subpaths work for sparse checkouts. Fixes #1455 · go-git/go-git@96914bf · GitHub
[go: up one dir, main page]

Skip to content

Commit 96914bf

Browse files
committed
utils: fix diff so subpaths work for sparse checkouts. Fixes #1455
1 parent 4e67bbe commit 96914bf

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

utils/merkletrie/change.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (l *Changes) addRecursive(root noder.Path, ctor noderToChangeFn) error {
148148
}
149149
return err
150150
}
151-
if current.IsDir() {
151+
if current.IsDir() || current.Skip() {
152152
continue
153153
}
154154
l.Add(ctor(current))

utils/merkletrie/index/node.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ func NewRootNode(idx *index.Index) noder.Noder {
3636
parent := fullpath
3737
fullpath = path.Join(fullpath, part)
3838

39-
if _, ok := m[fullpath]; ok {
39+
// It's possible that the first occurrence of subdirectory is skipped.
40+
// The parent node can be created with SkipWorktree set to true, but
41+
// if any future children do not skip their subtree, the entire lineage
42+
// of the tree needs to have this value set to false so that subdirectories
43+
// are not ignored.
44+
if parentNode, ok := m[fullpath]; ok {
45+
if e.SkipWorktree == false {
46+
parentNode.skip = false
47+
}
4048
continue
4149
}
4250

utils/merkletrie/index/node_test.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package index
22

33
import (
44
"bytes"
5-
"path/filepath"
5+
"path"
66
"testing"
77

88
"github.com/go-git/go-git/v5/plumbing"
@@ -47,14 +47,14 @@ func (s *NoderSuite) TestDiff() {
4747
func (s *NoderSuite) TestDiffChange() {
4848
indexA := &index.Index{
4949
Entries: []*index.Entry{{
50-
Name: filepath.Join("bar", "baz", "bar"),
50+
Name: path.Join("bar", "baz", "bar"),
5151
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
5252
}},
5353
}
5454

5555
indexB := &index.Index{
5656
Entries: []*index.Entry{{
57-
Name: filepath.Join("bar", "baz", "foo"),
57+
Name: path.Join("bar", "baz", "foo"),
5858
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
5959
}},
6060
}
@@ -64,6 +64,32 @@ func (s *NoderSuite) TestDiffChange() {
6464
s.Len(ch, 2)
6565
}
6666

67+
func (s *NoderSuite) TestDiffSkipIssue1455() {
68+
indexA := &index.Index{
69+
Entries: []*index.Entry{
70+
{
71+
Name: path.Join("bar", "baz", "bar"),
72+
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
73+
SkipWorktree: true,
74+
},
75+
{
76+
Name: path.Join("bar", "biz", "bat"),
77+
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
78+
SkipWorktree: false,
79+
},
80+
},
81+
}
82+
83+
indexB := &index.Index{}
84+
85+
ch, err := merkletrie.DiffTree(NewRootNode(indexB), NewRootNode(indexA), isEquals)
86+
s.NoError(err)
87+
s.Len(ch, 1)
88+
a, err := ch[0].Action()
89+
s.NoError(err)
90+
s.Equal(a, merkletrie.Insert)
91+
}
92+
6793
func (s *NoderSuite) TestDiffDir() {
6894
indexA := &index.Index{
6995
Entries: []*index.Entry{{
@@ -74,7 +100,7 @@ func (s *NoderSuite) TestDiffDir() {
74100

75101
indexB := &index.Index{
76102
Entries: []*index.Entry{{
77-
Name: filepath.Join("foo", "bar"),
103+
Name: path.Join("foo", "bar"),
78104
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
79105
}},
80106
}

0 commit comments

Comments
 (0)
0