mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-06-09 06:04:12 +00:00
Stanza.WriteTo: Sort extra fields alphabetically
This makes the output deterministic. This is important to me as I am using `Packages` index files as a kind of lockfile and committing it to my git repository. Without this we get a lot of noise in the diff whenever the file is regenerated because [go randomises map iteration order][1]. [1]: https://nathanleclaire.com/blog/2014/04/27/a-surprising-feature-of-golang-that-colored-me-impressed/
This commit is contained in:
+11
-2
@@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
@@ -182,8 +183,16 @@ func (s Stanza) WriteTo(w *bufio.Writer, isSource, isRelease, isInstaller bool)
|
|||||||
|
|
||||||
// no extra fields in installer
|
// no extra fields in installer
|
||||||
if !isInstaller {
|
if !isInstaller {
|
||||||
for field, value := range s {
|
// Print extra fields in deterministic order (alphabetical)
|
||||||
err := writeField(w, field, value, isRelease)
|
keys := make([]string, len(s))
|
||||||
|
i := 0
|
||||||
|
for field := range s {
|
||||||
|
keys[i] = field
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
for _, field := range keys {
|
||||||
|
err := writeField(w, field, s[field], isRelease)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user