diff options
author | Santiago M. Mola <santi@mola.io> | 2017-07-27 17:17:34 +0200 |
---|---|---|
committer | Santiago M. Mola <santi@mola.io> | 2017-08-01 13:01:59 +0200 |
commit | e5c6fa237776870483cbe227d7f7ea943f35cb12 (patch) | |
tree | d8708626c6e4bdacdf3d3b796a17025908b6606b /plumbing/format/config/option.go | |
parent | 1e70916ca7e4d5c0ad00edbfd1877e06d7587fc6 (diff) | |
download | go-git-e5c6fa237776870483cbe227d7f7ea943f35cb12.tar.gz |
config: preserve option order on config marshalling
Do not change order of options (e.g. in RemoteConfig)
when serializing for any option whose value has not changed.
Diffstat (limited to 'plumbing/format/config/option.go')
-rw-r--r-- | plumbing/format/config/option.go | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/plumbing/format/config/option.go b/plumbing/format/config/option.go index 481af02..d4775e4 100644 --- a/plumbing/format/config/option.go +++ b/plumbing/format/config/option.go @@ -79,16 +79,39 @@ func (opts Options) withAddedOption(key string, value string) Options { return append(opts, &Option{key, value}) } -func (opts Options) withSettedOption(key string, value string) Options { - for i := len(opts) - 1; i >= 0; i-- { - o := opts[i] - if o.IsKey(key) { - result := make(Options, len(opts)) - copy(result, opts) - result[i] = &Option{key, value} - return result +func (opts Options) withSettedOption(key string, values ...string) Options { + var result Options + var added []string + for _, o := range opts { + if !o.IsKey(key) { + result = append(result, o) + continue + } + + if contains(values, o.Value) { + added = append(added, o.Value) + result = append(result, o) + continue + } + } + + for _, value := range values { + if contains(added, value) { + continue + } + + result = result.withAddedOption(key, value) + } + + return result +} + +func contains(haystack []string, needle string) bool { + for _, s := range haystack { + if s == needle { + return true } } - return opts.withAddedOption(key, value) + return false } |