Skip to content

Commit

Permalink
Add a --pin option to ipfs add (allowing --pin=false)
Browse files Browse the repository at this point in the history
Implements a solution for #1908

This PR replaces #1909

License: MIT
Signed-off-by: Andrew Chin <achin@eminence32.net>
  • Loading branch information
eminence authored and whyrusleeping committed Dec 22, 2015
1 parent 42b2507 commit 1b03ca0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
hiddenOptionName = "hidden"
onlyHashOptionName = "only-hash"
chunkerOptionName = "chunker"
pinOptionName = "pin"
)

var AddCmd = &cmds.Command{
Expand All @@ -49,6 +50,7 @@ remains to be implemented.
cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object"),
cmds.BoolOption(hiddenOptionName, "H", "Include files that are hidden"),
cmds.StringOption(chunkerOptionName, "s", "chunking algorithm to use"),
cmds.BoolOption(pinOptionName, "Pin this object when adding. Default true"),
},
PreRun: func(req cmds.Request) error {
if quiet, _, _ := req.Option(quietOptionName).Bool(); quiet {
Expand Down Expand Up @@ -94,6 +96,11 @@ remains to be implemented.
hash, _, _ := req.Option(onlyHashOptionName).Bool()
hidden, _, _ := req.Option(hiddenOptionName).Bool()
chunker, _, _ := req.Option(chunkerOptionName).String()
dopin, pin_found, _ := req.Option(pinOptionName).Bool()

if !pin_found { // default
dopin = true
}

if hash {
nilnode, err := core.NewNode(n.Context(), &core.BuildCfg{
Expand All @@ -117,6 +124,7 @@ remains to be implemented.
fileAdder.Hidden = hidden
fileAdder.Trickle = trickle
fileAdder.Wrap = wrap
fileAdder.Pin = dopin

// addAllFiles loops over a convenience slice file to
// add each file individually. e.g. 'ipfs add a b c'
Expand Down
3 changes: 3 additions & 0 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func (params *Adder) PinRoot() error {
if err != nil {
return err
}
if !params.Pin {
return nil
}

rnk, err := root.Key()
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions test/sharness/t0081-repo-pinning.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ HASH_DIR4="QmW98gV71Ns4bX7QbgWAqLiGF3SDC1JpveZSgBh4ExaSAd"
HASH_DIR3="QmRsCaNBMkweZ9vHT5PJRd2TT9rtNKEKyuognCEVxZxF1H"
HASH_DIR2="QmTUTQAgeVfughDSFukMZLbfGvetDJY7Ef5cDXkKK4abKC"
HASH_DIR1="QmNyZVFbgvmzguS2jVMRb8PQMNcCMJrn9E3doDhBbcPNTY"
HASH_NOPINDIR="QmWHjrRJYSfYKz5V9dWWSKu47GdY7NewyRhyTiroXgWcDU"
HASH_NOPIN_FILE1="QmUJT3GQi1dxQyTZbkaWeer9GkCn1d3W3HHRLSDr6PTcpx"
HASH_NOPIN_FILE2="QmarR7m9JT7qHEGhuFNZUEMAnoZ8E9QAfsthHCQ9Y2GfoT"

DIR1="dir1"
DIR2="dir1/dir2"
Expand Down Expand Up @@ -248,6 +251,34 @@ test_expect_success "recursive pin fails without objects" '
test_fsh cat err_expected8
'

test_expect_success "test add nopin file" '
echo "test nopin data" > test_nopin_data &&
NOPINHASH=$(ipfs add -q --pin=false test_nopin_data) &&
test_pin_flag "$NOPINHASH" direct false &&
test_pin_flag "$NOPINHASH" indirect false &&
test_pin_flag "$NOPINHASH" recursive false
'


test_expect_success "test add nopin dir" '
mkdir nopin_dir1 &&
echo "some nopin text 1" >nopin_dir1/file1 &&
echo "some nopin text 2" >nopin_dir1/file2 &&
ipfs add -q -r --pin=false nopin_dir1 | tail -n1 >actual1 &&
echo "$HASH_NOPINDIR" >expected1 &&
test_cmp actual1 expected1 &&
test_pin_flag "$HASH_NOPINDIR" direct false &&
test_pin_flag "$HASH_NOPINDIR" indirect false &&
test_pin_flag "$HASH_NOPINDIR" recursive false &&
test_pin_flag "$HASH_NOPIN_FILE1" direct false &&
test_pin_flag "$HASH_NOPIN_FILE1" indirect false &&
test_pin_flag "$HASH_NOPIN_FILE1" recursive false &&
test_pin_flag "$HASH_NOPIN_FILE2" direct false &&
test_pin_flag "$HASH_NOPIN_FILE2" indirect false &&
test_pin_flag "$HASH_NOPIN_FILE2" recursive false
'

# test_kill_ipfs_daemon

test_done

0 comments on commit 1b03ca0

Please sign in to comment.