Lets 'ipfs add' add hidden files when explicit.
Fixes ipfs/go-ipfs/#2145. The --hidden switch (still) only affects recursive adding. License: MIT Signed-off-by: Stephen Whitmore <noffle@ipfs.io>
Stephen Whitmore committed
Jan 14, 2016 at 08:32 UTC
0828d1eb6491891c0279dbb7e557c2fa7408809d
3 files changed
+34
-12
core/commands/add.go
+16
-3
@@ -2,6 +2,7 @@ package commands
2
3
import (
4
"fmt"
5
+ "io"
6
7
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/cheggaaa/pb"
8
"github.com/ipfs/go-ipfs/core/coreunix"
@@ -49,7 +50,7 @@ remains to be implemented.
50
cmds.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation"),
51
cmds.BoolOption(onlyHashOptionName, "n", "Only chunk and hash - do not write to disk"),
52
cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object"),
52
- cmds.BoolOption(hiddenOptionName, "H", "Include files that are hidden"),
53
+ cmds.BoolOption(hiddenOptionName, "H", "Include files that are hidden. Only takes effect on recursive add."),
54
cmds.StringOption(chunkerOptionName, "s", "chunking algorithm to use"),
55
cmds.BoolOption(pinOptionName, "Pin this object when adding. Default true"),
56
},
@@ -147,8 +148,20 @@ remains to be implemented.
148
fileAdder.Silent = silent
149
150
addAllAndPin := func(f files.File) error {
150
- if err := fileAdder.AddFile(f); err != nil {
151
- return err
151
+ // Iterate over each top-level file and add individually. Otherwise the
152
+ // single files.File f is treated as a directory, affecting hidden file
153
+ // semantics.
154
+ for {
155
+ file, err := f.NextFile()
156
+ if err == io.EOF {
157
+ // Finished the list of files.
158
+ break
159
+ } else if err != nil {
160
+ return err
161
+ }
162
+ if err := fileAdder.AddFile(file); err != nil {
163
+ return err
164
+ }
165
}
166
167
if hash {
core/coreunix/add.go
+7
-9
@@ -359,11 +359,7 @@ func (adder *Adder) addFile(file files.File) error {
359
return err
360
}
361
362
- switch {
363
- case files.IsHidden(file) && !adder.Hidden:
364
- log.Infof("%s is hidden, skipping", file.FileName())
365
- return &hiddenFileError{file.FileName()}
366
- case file.IsDirectory():
362
+ if file.IsDirectory() {
363
return adder.addDir(file)
364
}
365
@@ -417,11 +413,13 @@ func (adder *Adder) addDir(dir files.File) error {
413
break
414
}
415
420
- err = adder.addFile(file)
421
- if _, ok := err.(*hiddenFileError); ok {
422
- // hidden file error, skip file
416
+ // Skip hidden files when adding recursively, unless Hidden is enabled.
417
+ if files.IsHidden(file) && !adder.Hidden {
418
+ log.Infof("%s is hidden, skipping", file.FileName())
419
continue
424
- } else if err != nil {
420
+ }
421
+ err = adder.addFile(file)
422
+ if err != nil {
423
return err
424
}
425
}
test/sharness/t0042-add-skip.sh
+11
@@ -48,6 +48,17 @@ test_add_skip() {
48
test_cmp expected actual
49
'
50
51
+ test_expect_success "'ipfs add' includes hidden files given explicitly even without --hidden" '
52
+ mkdir -p mountdir/dotfiles &&
53
+ echo "set nocompatible" > mountdir/dotfiles/.vimrc
54
+ cat >expected <<-\EOF &&
55
+added QmT4uMRDCN7EMpFeqwvKkboszbqeW1kWVGrBxBuCGqZcQc .vimrc
56
+ EOF
57
+ ipfs add mountdir/dotfiles/.vimrc >actual
58
+ cat actual
59
+ test_cmp expected actual
60
+ '
61
+
62
}
63
64
# should work offline