diff --git a/commands/cli/parse.go b/commands/cli/parse.go index 694951bed07..ee6b1656db1 100644 --- a/commands/cli/parse.go +++ b/commands/cli/parse.go @@ -7,6 +7,7 @@ import ( "os" fp "path" "runtime" + "sort" "strings" cmds "github.com/jbenet/go-ipfs/commands" @@ -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) @@ -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() } diff --git a/core/commands/add.go b/core/commands/add.go index 3ab27ad8fdc..fbac578a92b 100644 --- a/core/commands/add.go +++ b/core/commands/add.go @@ -6,7 +6,6 @@ import ( "fmt" "io" "path" - "sort" cmds "github.com/jbenet/go-ipfs/commands" core "github.com/jbenet/go-ipfs/core" @@ -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 { @@ -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] } diff --git a/test/t0040-add-and-cat.sh b/test/t0040-add-and-cat.sh index b6aae632da3..15a1de1c870 100755 --- a/test/t0040-add-and-cat.sh +++ b/test/t0040-add-and-cat.sh @@ -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 '