Skip to content

Add sparse reset directory validation and remove ResetSparsely. #1531

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

onee-only
Copy link
Contributor

Fixes #1500

  1. Added a validation to check if directory exists in the commit tree.
  2. Removed ResetSparsely and moved dirs into ResetOptions.
  3. Added a flag to disable validation. See this comment.

}

t, err := w.r.getTreeFromCommitHash(opts.Commit)
if err != nil {
return err
}

if len(opts.SparseDirs) > 0 && !opts.SkipSparseDirValidation {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(opts.SparseDirs) > 0 && !opts.SkipSparseDirValidation {
if !opts.SkipSparseDirValidation {

@@ -324,6 +337,20 @@ func (w *Worktree) ResetSparsely(opts *ResetOptions, dirs []string) error {
return nil
}

func treeContainsDirs(tree *object.Tree, dirs []string) bool {
for _, dir := range dirs {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for _, dir := range dirs {
if len(dirs) == 0 {
return false
}
for _, dir := range dirs {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that we should validate dirs of zero length on treeContainsDirs?

I think it will break the applications that migrate from v5 to v6, given that
opts.SkipSparseDirValidation == false and len(opts.SparseDirs) == 0 would be the default for options.

If that's affordable, I think something like ErrSparseResetWithNodirs would be more appropriate than ErrSparseResetDirectoryNotFound. So I suggest we check len(dirs) == 0 && !opts.SkipSparseDirValidation on ResetOptions.Validate().

if err != nil {
return false
}
if entry.Mode != filemode.Dir {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we always get an exclusive entry for each dir in the tree? I was under the impression that this was not necessarily always the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, yeah, git creates tree object for each directory that is not empty. Do you see any edge cases?

@@ -1344,7 +1344,7 @@ func (s *WorktreeSuite) TestResetSparsely() {

sparseResetDirs := []string{"php"}

err := w.ResetSparsely(&ResetOptions{Mode: HardReset}, sparseResetDirs)
err := w.Reset(&ResetOptions{Mode: HardReset, SparseDirs: sparseResetDirs})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with the change, but let's do that straight into the v6 instead. So please rebase and change the PR target to the v6-transport branch.

Comment on lines +1369 to +1370
{"non-existent"},
{"php/crappy.php"}, // non-dir
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's expand this to be more like table driven tests, so that we can have different input/output checks. With that, please add a test case ensuring the behaviour of SkipSparseDirValidation.

Filesystem: fs,
}

testcases := [][]string{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
testcases := [][]string{
tests := [][]string{

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SparseCheckoutDirectories behavior when directory path does not exist
2 participants