Skip to content

Commit 014d456

Browse files
fix(copy): preserve directory structure when copying with trailing slash (gopasspw#3396)
Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
1 parent 3b16cb2 commit 014d456

2 files changed

Lines changed: 4 additions & 37 deletions

File tree

‎internal/action/copy.go‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ package action
33
import (
44
"context"
55
"fmt"
6-
"path/filepath"
7-
"strings"
86

97
"github.com/gopasspw/gopass/internal/action/exit"
10-
"github.com/gopasspw/gopass/internal/tree"
118
"github.com/gopasspw/gopass/pkg/ctxutil"
129
"github.com/gopasspw/gopass/pkg/termio"
1310
"github.com/urfave/cli/v2"
@@ -43,40 +40,9 @@ func (s *secretHandler) copy(ctx context.Context, from, to string, force bool) e
4340
return exit.Error(exit.NotFound, nil, "%s does not exist", from)
4441
}
4542

46-
isSourceDir := s.Store.IsDir(ctx, from)
47-
hasTrailingSlash := strings.HasSuffix(to, "/")
48-
49-
if isSourceDir && hasTrailingSlash {
50-
return s.copyFlattenDir(ctx, from, to, force)
51-
}
52-
5343
return s.copyRegular(ctx, from, to, force)
5444
}
5545

56-
func (s *secretHandler) copyFlattenDir(ctx context.Context, from, to string, force bool) error {
57-
entries, err := s.Store.List(ctx, tree.INF)
58-
if err != nil {
59-
return exit.Error(exit.List, err, "failed to list entries in %q", from)
60-
}
61-
62-
fromPrefix := from
63-
if !strings.HasSuffix(fromPrefix, "/") {
64-
fromPrefix += "/"
65-
}
66-
67-
for _, entry := range entries {
68-
if strings.HasPrefix(entry, fromPrefix) {
69-
toPath := filepath.Join(to, filepath.Base(entry))
70-
71-
if err := s.copyRegular(ctx, entry, toPath, force); err != nil {
72-
return err
73-
}
74-
}
75-
}
76-
77-
return nil
78-
}
79-
8046
func (s *secretHandler) copyRegular(ctx context.Context, from, to string, force bool) error {
8147
if !force {
8248
if s.Store.Exists(ctx, to) && !termio.AskForConfirmation(ctx, fmt.Sprintf("%s already exists. Overwrite it?", to)) {

‎internal/action/copy_test.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,15 @@ func TestCopyWithTrailingSlash(t *testing.T) {
243243
require.NoError(t, act.Copy(c))
244244
buf.Reset()
245245

246-
// Verify the result
246+
// Verify the result: structure should be preserved
247247
require.NoError(t, act.List(gptest.CliCtx(ctx, t)))
248248
want := `gopass
249249
├── bar
250250
├── foo
251251
├── new/
252252
│ ├── baz
253-
│ └── zab
253+
│ └── some/
254+
│ └── zab
254255
└── secret/
255256
├── baz
256257
└── some/
@@ -263,7 +264,7 @@ func TestCopyWithTrailingSlash(t *testing.T) {
263264

264265
// Verify content of copied files
265266
ctx = ctxutil.WithTerminal(ctx, false)
266-
require.NoError(t, act.show(ctx, c, "new/zab", false))
267+
require.NoError(t, act.show(ctx, c, "new/some/zab", false))
267268
assert.Equal(t, "secret\n", buf.String())
268269
buf.Reset()
269270

0 commit comments

Comments
 (0)
⌂ 换网址