ipfs add output not sorted, cmds files sorted
I made the commands lib dir listing sort the contents so we get the same sequence of files from it repeatably.
Juan Batiz-Benet committed
Jan 6, 2015 at 13:40 UTC
0395a7af1ecb174f9e9a65adc7008608ea4d88c4
3 files changed
+18
-9
commands/cli/parse.go
+10
-1
@@ -7,6 +7,7 @@ import (
7
"os"
8
fp "path"
9
"runtime"
10
+ "sort"
11
"strings"
12
13
cmds "github.com/jbenet/go-ipfs/commands"
@@ -319,8 +320,10 @@ func openPath(file *os.File, path string) (cmds.File, error) {
320
return nil, err
321
}
322
322
- files := make([]cmds.File, 0, len(contents))
323
+ // make sure contents are sorted so -- repeatably -- we get the same inputs.
324
+ sort.Sort(sortFIByName(contents))
325
326
+ files := make([]cmds.File, 0, len(contents))
327
for _, child := range contents {
328
childPath := fp.Join(path, child.Name())
329
childFile, err := os.Open(childPath)
@@ -351,3 +354,9 @@ func isTerminal(stdin *os.File) (bool, error) {
354
// if stdin is a CharDevice, return true
355
return ((stat.Mode() & os.ModeCharDevice) != 0), nil
356
}
357
+
358
+type sortFIByName []os.FileInfo
359
+
360
+func (es sortFIByName) Len() int { return len(es) }
361
+func (es sortFIByName) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
362
+func (es sortFIByName) Less(i, j int) bool { return es[i].Name() < es[j].Name() }
core/commands/add.go
+6
-6
@@ -6,7 +6,6 @@ import (
6
"fmt"
7
"io"
8
"path"
9
- "sort"
9
10
cmds "github.com/jbenet/go-ipfs/commands"
11
core "github.com/jbenet/go-ipfs/core"
@@ -83,7 +82,8 @@ remains to be implemented.
82
return nil, u.ErrCast()
83
}
84
86
- sort.Stable(val)
85
+ // TODO: use this with an option
86
+ // sort.Stable(val)
87
88
var buf bytes.Buffer
89
for i, obj := range val.Objects {
@@ -204,12 +204,12 @@ func addDagnode(output *AddOutput, name string, dn *dag.Node) error {
204
// Sort interface implementation to sort add output by name
205
206
func (a AddOutput) Len() int {
207
- return len(a.Names)
207
+ return len(a.Names)
208
}
209
func (a AddOutput) Swap(i, j int) {
210
- a.Names[i], a.Names[j] = a.Names[j], a.Names[i]
211
- a.Objects[i], a.Objects[j] = a.Objects[j], a.Objects[i]
210
+ a.Names[i], a.Names[j] = a.Names[j], a.Names[i]
211
+ a.Objects[i], a.Objects[j] = a.Objects[j], a.Objects[i]
212
}
213
func (a AddOutput) Less(i, j int) bool {
214
- return a.Names[i] < a.Names[j]
214
+ return a.Names[i] < a.Names[j]
215
}
test/t0040-add-and-cat.sh
+2
-2
@@ -78,9 +78,9 @@ test_expect_success "'ipfs add -r' output looks good" '
78
PLANETS="QmWSgS32xQEcXMeqd3YPJLrNBLSdsfYCep2U7CFkyrjXwY" &&
79
MARS="QmPrrHqJzto9m7SyiRzarwkqPcCSsKR2EB1AyqJfe8L8tN" &&
80
VENUS="QmU5kp3BH3B8tnWUU2Pikdb2maksBNkb92FHRr56hyghh4" &&
81
- echo "added $PLANETS mountdir/planets" >expected &&
82
- echo "added $MARS mountdir/planets/mars.txt" >>expected &&
81
+ echo "added $MARS mountdir/planets/mars.txt" >expected &&
82
echo "added $VENUS mountdir/planets/venus.txt" >>expected &&
83
+ echo "added $PLANETS mountdir/planets" >>expected &&
84
test_cmp expected actual
85
'
86