Skip to content

Commit

Permalink
ipfs add output not sorted, cmds files sorted
Browse files Browse the repository at this point in the history
I made the commands lib dir listing sort the contents
so we get the same sequence of files from it repeatably.
  • Loading branch information
jbenet committed Jan 6, 2015
1 parent 87c561b commit 0395a7a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
11 changes: 10 additions & 1 deletion commands/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
fp "path"
"runtime"
"sort"
"strings"

cmds "github.com/jbenet/go-ipfs/commands"
Expand Down Expand Up @@ -319,8 +320,10 @@ func openPath(file *os.File, path string) (cmds.File, error) {
return nil, err
}

files := make([]cmds.File, 0, len(contents))
// make sure contents are sorted so -- repeatably -- we get the same inputs.
sort.Sort(sortFIByName(contents))

files := make([]cmds.File, 0, len(contents))
for _, child := range contents {
childPath := fp.Join(path, child.Name())
childFile, err := os.Open(childPath)
Expand Down Expand Up @@ -351,3 +354,9 @@ func isTerminal(stdin *os.File) (bool, error) {
// if stdin is a CharDevice, return true
return ((stat.Mode() & os.ModeCharDevice) != 0), nil
}

type sortFIByName []os.FileInfo

func (es sortFIByName) Len() int { return len(es) }
func (es sortFIByName) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
func (es sortFIByName) Less(i, j int) bool { return es[i].Name() < es[j].Name() }
12 changes: 6 additions & 6 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"path"
"sort"

cmds "github.com/jbenet/go-ipfs/commands"
core "github.com/jbenet/go-ipfs/core"
Expand Down Expand Up @@ -83,7 +82,8 @@ remains to be implemented.
return nil, u.ErrCast()
}

sort.Stable(val)
// TODO: use this with an option
// sort.Stable(val)

var buf bytes.Buffer
for i, obj := range val.Objects {
Expand Down Expand Up @@ -204,12 +204,12 @@ func addDagnode(output *AddOutput, name string, dn *dag.Node) error {
// Sort interface implementation to sort add output by name

func (a AddOutput) Len() int {
return len(a.Names)
return len(a.Names)
}
func (a AddOutput) Swap(i, j int) {
a.Names[i], a.Names[j] = a.Names[j], a.Names[i]
a.Objects[i], a.Objects[j] = a.Objects[j], a.Objects[i]
a.Names[i], a.Names[j] = a.Names[j], a.Names[i]
a.Objects[i], a.Objects[j] = a.Objects[j], a.Objects[i]
}
func (a AddOutput) Less(i, j int) bool {
return a.Names[i] < a.Names[j]
return a.Names[i] < a.Names[j]
}
4 changes: 2 additions & 2 deletions test/t0040-add-and-cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ test_expect_success "'ipfs add -r' output looks good" '
PLANETS="QmWSgS32xQEcXMeqd3YPJLrNBLSdsfYCep2U7CFkyrjXwY" &&
MARS="QmPrrHqJzto9m7SyiRzarwkqPcCSsKR2EB1AyqJfe8L8tN" &&
VENUS="QmU5kp3BH3B8tnWUU2Pikdb2maksBNkb92FHRr56hyghh4" &&
echo "added $PLANETS mountdir/planets" >expected &&
echo "added $MARS mountdir/planets/mars.txt" >>expected &&
echo "added $MARS mountdir/planets/mars.txt" >expected &&
echo "added $VENUS mountdir/planets/venus.txt" >>expected &&
echo "added $PLANETS mountdir/planets" >>expected &&
test_cmp expected actual
'

Expand Down

0 comments on commit 0395a7a

Please sign in to comment.