Skip to content

plumbing: fix err when adding a deleted directory; fixes #1485 #1479

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 1 commit into
base: previous-main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions worktree_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,31 @@ func (w *Worktree) doRemoveFile(idx *index.Index, path string) (plumbing.Hash, e
func (w *Worktree) deleteFromIndex(idx *index.Index, path string) (plumbing.Hash, error) {
e, err := idx.Remove(path)
if err != nil {
if errors.Is(err, index.ErrEntryNotFound) {
return plumbing.ZeroHash, w.deleteFilesFromIndex(idx, path)
}
return plumbing.ZeroHash, err
}

return e.Hash, nil
}

// when adding a deleted directory, you cannot process it through idx.Remove, need to traverse and process the index entry under the directory
func (w *Worktree) deleteFilesFromIndex(idx *index.Index, path string) error {
path = filepath.ToSlash(path)
count := 0
for index, e := range idx.Entries {
if strings.HasPrefix(e.Name, path) {
idx.Entries = append(idx.Entries[:index], idx.Entries[index+1:]...)
count++
}
Copy link
Member

Choose a reason for hiding this comment

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

@zjs1012 do we need to go through the entire index? Can't we just short-circuit once we find it?

Suggested change
}
break
}

}
if count == 0 {
return index.ErrEntryNotFound
}
return nil
}

func (w *Worktree) deleteFromFilesystem(path string) error {
err := w.Filesystem.Remove(path)
if os.IsNotExist(err) {
Expand Down
51 changes: 51 additions & 0 deletions worktree_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"strings"
"testing"

"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-billy/v5/util"
fixtures "github.com/go-git/go-git-fixtures/v4"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -87,3 +90,51 @@ func TestIndexEntrySizeUpdatedForNonRegularFiles(t *testing.T) {
// Check whether the index was updated with the two new line breaks.
assert.Equal(t, uint32(len(content)+2), idx.Entries[0].Size)
}

func TestAddEmptyDirectory(t *testing.T) {
f := fixtures.Basic().One()
fs := memfs.New()
r := NewRepositoryWithEmptyWorktree(f)
w := &Worktree{
r: r,
Filesystem: fs,
}

err := w.Checkout(&CheckoutOptions{})
require.NoError(t, err)

// write and add foo file in the dir directory
require.NoError(t, util.WriteFile(fs, "/dir/f1", []byte("foo"), 0644))

err = w.AddWithOptions(&AddOptions{
Path: "/dir/f1",
SkipStatus: true,
})
require.NoError(t, err)

_, err = w.Commit("commit foo only\n", &CommitOptions{
Author: defaultSignature(),
})
require.NoError(t, err)

// remove dir directory and add it
require.NoError(t, util.RemoveAll(fs, "/dir"))

err = w.AddWithOptions(&AddOptions{
Path: "/dir",
SkipStatus: true,
})
require.NoError(t, err)

_, err = w.Commit("commit remove dir\n", &CommitOptions{
Author: defaultSignature(),
})
require.NoError(t, err)

// add invalid directory with error
err = w.AddWithOptions(&AddOptions{
Path: "/dir2",
SkipStatus: true,
})
require.Error(t, err)
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy