8000 utils: fix diff so subpaths work for sparse checkouts, fixes 1455 by patricsss · Pull Request #1484 · go-git/go-git · GitHub
[go: up one dir, main page]

Skip to content

utils: fix diff so subpaths work for sparse checkouts, fixes 1455 #1484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion utils/merkletrie/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (l *Changes) addRecursive(root noder.Path, ctor noderToChangeFn) error {
}
return err
}
if current.IsDir() {
if current.IsDir() || current.Skip() {
continue
}
l.Add(ctor(current))
Expand Down
10 changes: 9 additions & 1 deletion utils/merkletrie/index/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ func NewRootNode(idx *index.Index) noder.Noder {
parent := fullpath
fullpath = path.Join(fullpath, part)

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

Expand Down
34 changes: 30 additions & 4 deletions utils/merkletrie/index/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package index

import (
"bytes"
"path/filepath"
"path"
"testing"

"github.com/go-git/go-git/v5/plumbing"
Expand Down Expand Up @@ -47,14 +47,14 @@ func (s *NoderSuite) TestDiff() {
func (s *NoderSuite) TestDiffChange() {
indexA := &index.Index{
Entries: []*index.Entry{{
Name: filepath.Join("bar", "baz", "bar"),
Name: path.Join("bar", "baz", "bar"),
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
}},
}

indexB := &index.Index{
Entries: []*index.Entry{{
Name: filepath.Join("bar", "baz", "foo"),
Name: path.Join("bar", "baz", "foo"),
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
}},
}
Expand All @@ -64,6 +64,32 @@ func (s *NoderSuite) TestDiffChange() {
s.Len(ch, 2)
}

func (s *NoderSuite) TestDiffSkipIssue1455() {
indexA := &index.Index{
Entries: []*index.Entry{
{
Name: path.Join("bar", "baz", "bar"),
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
SkipWorktree: true,
},
{
Name: path.Join("bar", "biz", "bat"),
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
SkipWorktree: false,
},
},
}

indexB := &index.Index{}

ch, err := merkletrie.DiffTree(NewRootNode(indexB), NewRootNode(indexA), isEquals)
s.NoError(err)
s.Len(ch, 1)
a, err := ch[0].Action()
s.NoError(err)
s.Equal(a, merkletrie.Insert)
}

func (s *NoderSuite) TestDiffDir() {
indexA := &index.Index{
Entries: []*index.Entry{{
Expand All @@ -74,7 +100,7 @@ func (s *NoderSuite) TestDiffDir() {

indexB := &index.Index{
Entries: []*index.Entry{{
Name: filepath.Join("foo", "bar"),
Name: path.Join("foo", "bar"),
Hash: plumbing.NewHash("8ab686eafeb1f44702738c8b0f24f2567c36da6d"),
}},
}
Expand Down
0