Skip to content

Commit

Permalink
[BUGFIX] Allow to move shadowed entries into their own folder (#2718)
Browse files Browse the repository at this point in the history
Fixes #892

Signed-off-by: Yolan Romailler <[email protected]>
  • Loading branch information
AnomalRoil authored Nov 23, 2023
1 parent 7dca311 commit ccd748e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
21 changes: 11 additions & 10 deletions internal/store/root/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,7 @@ func (r *Store) move(ctx context.Context, from, to string, del bool) error {
subFrom, fromPrefix := r.getStore(from)
subTo, _ := r.getStore(to)

srcIsDir := r.IsDir(ctx, from)
dstIsDir := r.IsDir(ctx, to)

if srcIsDir && r.Exists(ctx, to) && !dstIsDir {
return fmt.Errorf("destination is a file")
}

if err := r.moveFromTo(ctx, subFrom, from, to, fromPrefix, srcIsDir, dstIsDir, del); err != nil {
if err := r.moveFromTo(ctx, subFrom, from, to, fromPrefix, del); err != nil {
return err
}

Expand Down Expand Up @@ -124,13 +117,21 @@ func (r *Store) move(ctx context.Context, from, to string, del bool) error {
return nil
}

func (r *Store) moveFromTo(ctx context.Context, subFrom *leaf.Store, from, to, fromPrefix string, srcIsDir, dstIsDir, del bool) error {
func (r *Store) moveFromTo(ctx context.Context, subFrom *leaf.Store, from, to, fromPrefix string, del bool) error {
ctx = ctxutil.WithGitCommit(ctx, false)

// source is a directory and not a "shadowed" leaf
srcIsDir := r.IsDir(ctx, from) && !r.Exists(ctx, from)
dstIsDir := r.IsDir(ctx, to)

if srcIsDir && r.Exists(ctx, to) && !dstIsDir {
return fmt.Errorf("destination is a file")
}

entries := []string{from}
// if the source is a directory we enumerate all it's children
// and move them one by one.
if r.IsDir(ctx, from) {
if srcIsDir {
var err error

entries, err = subFrom.List(ctx, fromPrefix+"/")
Expand Down
37 changes: 37 additions & 0 deletions internal/store/root/move_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,40 @@ func TestComputeMoveDestination(t *testing.T) {
})
}
}

func TestRegression892(t *testing.T) {
u := gptest.NewUnitTester(t)
u.Entries = []string{
"some/example",
"some/example/test2",
"communication/t1",
}
require.NoError(t, u.InitStore(""))

ctx := context.Background()
ctx = ctxutil.WithAlwaysYes(ctx, true)
ctx = ctxutil.WithHidden(ctx, true)

rs, err := createRootStore(ctx, u)
require.NoError(t, err)
require.NoError(t, rs.Delete(ctx, "foo"))

// Initial state:
entries, err := rs.List(ctx, tree.INF)
require.NoError(t, err)
require.Equal(t, []string{
"communication/t1",
"some/example",
"some/example/test2",
}, entries)

// -> move comm email => Rename comm to email
require.NoError(t, rs.Move(ctx, "some/example", "some/example/test1"))
entries, err = rs.List(ctx, tree.INF)
require.NoError(t, err)
require.Equal(t, []string{
"communication/t1",
"some/example/test1",
"some/example/test2",
}, entries)
}

0 comments on commit ccd748e

Please sign in to comment.