diff --git a/src/cmds/restic/cmd_tag.go b/src/cmds/restic/cmd_tag.go index 9235585f0..220e95bc2 100644 --- a/src/cmds/restic/cmd_tag.go +++ b/src/cmds/restic/cmd_tag.go @@ -62,38 +62,16 @@ func changeTags(repo *repository.Repository, snapshotID restic.ID, setTags, addT } if len(setTags) != 0 { - // Setting the tag to an empty string really means no more tags. + // Setting the tag to an empty string really means no tags. if len(setTags) == 1 && setTags[0] == "" { setTags = nil } sn.Tags = setTags changed = true } else { - for _, add := range addTags { - found := false - for _, tag := range sn.Tags { - if tag == add { - found = true - break - } - } - if !found { - sn.Tags = append(sn.Tags, add) - changed = true - } - } - for _, remove := range removeTags { - for i, tag := range sn.Tags { - if tag == remove { - // https://github.com/golang/go/wiki/SliceTricks - sn.Tags[i] = sn.Tags[len(sn.Tags)-1] - sn.Tags[len(sn.Tags)-1] = "" - sn.Tags = sn.Tags[:len(sn.Tags)-1] - - changed = true - break - } - } + changed = sn.AddTags(addTags) + if sn.RemoveTags(removeTags) { + changed = true } } diff --git a/src/restic/snapshot.go b/src/restic/snapshot.go index 91ffbd558..f57741138 100644 --- a/src/restic/snapshot.go +++ b/src/restic/snapshot.go @@ -73,8 +73,7 @@ func LoadAllSnapshots(repo Repository) (snapshots []*Snapshot, err error) { snapshots = append(snapshots, sn) } - - return snapshots, nil + return } func (sn Snapshot) String() string { @@ -99,6 +98,41 @@ func (sn *Snapshot) fillUserInfo() error { return err } +// AddTags adds the given tags to the snapshots tags, preventing duplicates. +// It returns true if any changes were made. +func (sn *Snapshot) AddTags(addTags []string) (changed bool) { +nextTag: + for _, add := range addTags { + for _, tag := range sn.Tags { + if tag == add { + continue nextTag + } + } + sn.Tags = append(sn.Tags, add) + changed = true + } + return +} + +// RemoveTags removes the given tags from the snapshots tags and +// returns true if any changes were made. +func (sn *Snapshot) RemoveTags(removeTags []string) (changed bool) { + for _, remove := range removeTags { + for i, tag := range sn.Tags { + if tag == remove { + // https://github.com/golang/go/wiki/SliceTricks + sn.Tags[i] = sn.Tags[len(sn.Tags)-1] + sn.Tags[len(sn.Tags)-1] = "" + sn.Tags = sn.Tags[:len(sn.Tags)-1] + + changed = true + break + } + } + } + return +} + // HasTags returns true if the snapshot has all the tags. func (sn *Snapshot) HasTags(tags []string) bool { nextTag: