diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-02-13 19:18:05 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-02-13 19:18:05 +0100 |
commit | 940a16ca68457beded1eff551f23febeffb32f3e (patch) | |
tree | b914bf2ba817d9d6b614adc3ebb07ea04f41f906 /plumbing | |
parent | c551c29a93882658d4b34860b5300de0f3456059 (diff) | |
download | go-git-940a16ca68457beded1eff551f23febeffb32f3e.tar.gz |
format/index: sort the Entries before encode
Diffstat (limited to 'plumbing')
-rw-r--r-- | plumbing/format/index/encoder.go | 9 | ||||
-rw-r--r-- | plumbing/format/index/encoder_test.go | 10 | ||||
-rw-r--r-- | plumbing/format/index/index.go | 13 |
3 files changed, 28 insertions, 4 deletions
diff --git a/plumbing/format/index/encoder.go b/plumbing/format/index/encoder.go index e5de135..4699d43 100644 --- a/plumbing/format/index/encoder.go +++ b/plumbing/format/index/encoder.go @@ -6,6 +6,7 @@ import ( "errors" "hash" "io" + "sort" "time" "srcd.works/go-git.v4/utils/binary" @@ -61,6 +62,8 @@ func (e *Encoder) encodeHeader(idx *Index) error { } func (e *Encoder) encodeEntries(idx *Index) error { + sort.Sort(ByName(idx.Entries)) + for _, entry := range idx.Entries { if err := e.encodeEntry(&entry); err != nil { return err @@ -139,3 +142,9 @@ func (e *Encoder) padEntry(wrote int) error { func (e *Encoder) encodeFooter() error { return binary.Write(e.w, e.hash.Sum(nil)) } + +type ByName []Entry + +func (l ByName) Len() int { return len(l) } +func (l ByName) Swap(i, j int) { l[i], l[j] = l[j], l[i] } +func (l ByName) Less(i, j int) bool { return l[i].Name < l[j].Name } diff --git a/plumbing/format/index/encoder_test.go b/plumbing/format/index/encoder_test.go index 914ee26..a6a0ea2 100644 --- a/plumbing/format/index/encoder_test.go +++ b/plumbing/format/index/encoder_test.go @@ -26,6 +26,11 @@ func (s *IndexSuite) TestEncode(c *C) { }, { CreatedAt: time.Now(), ModifiedAt: time.Now(), + Name: "bar", + Size: 82, + }, { + CreatedAt: time.Now(), + ModifiedAt: time.Now(), Name: strings.Repeat(" ", 20), Size: 82, }}, @@ -42,6 +47,11 @@ func (s *IndexSuite) TestEncode(c *C) { c.Assert(err, IsNil) c.Assert(idx, DeepEquals, output) + + c.Assert(output.Entries[0].Name, Equals, strings.Repeat(" ", 20)) + c.Assert(output.Entries[1].Name, Equals, "bar") + c.Assert(output.Entries[2].Name, Equals, "foo") + } func (s *IndexSuite) TestEncodeUnsuportedVersion(c *C) { diff --git a/plumbing/format/index/index.go b/plumbing/format/index/index.go index e5dc178..a95dba2 100644 --- a/plumbing/format/index/index.go +++ b/plumbing/format/index/index.go @@ -36,9 +36,14 @@ const ( // in the worktree, having information about the working files. Changes in // worktree are detected using this Index. The Index is also used during merges type Index struct { - Version uint32 - Entries []Entry - Cache *Tree + // Version is index version + Version uint32 + // Entries collection of entries represented by this Index. The order of + // this collection is not guaranteed + Entries []Entry + // Cache represents the 'Cached tree' extension + Cache *Tree + // ResolveUndo represents the 'Resolve undo' extension ResolveUndo *ResolveUndo } @@ -84,7 +89,7 @@ type TreeEntry struct { // Path component (relative to its parent directory) Path string // Entries is the number of entries in the index that is covered by the tree - // this entry represents + // this entry represents. Entries int // Trees is the number that represents the number of subtrees this tree has Trees int |