Preserve remote conflicts in entry history

This commit is contained in:
Joe Julian
2026-03-29 21:19:42 -07:00
parent 59905ccd98
commit 6e2760c514
2 changed files with 181 additions and 0 deletions
+57
View File
@@ -368,6 +368,9 @@ func mergeEntrySet(base, local, latest []vault.Entry) []vault.Entry {
for id, current := range localByID {
original, hadBase := baseByID[id]
if !hadBase || !entriesEqual(original, current) {
if latestCurrent, latestChanged := latestByID[id]; hadBase && latestChanged && !entriesEqual(original, latestCurrent) && !entriesEqual(latestCurrent, current) {
current = mergeConflictedEntry(current, latestCurrent)
}
latestByID[id] = current
}
}
@@ -395,6 +398,24 @@ func mergeEntrySet(base, local, latest []vault.Entry) []vault.Entry {
return out
}
func mergeConflictedEntry(current, latest vault.Entry) vault.Entry {
displaced := cloneEntry(latest)
if sameEntryVersion(current, displaced) {
return current
}
mergedHistory := make([]vault.Entry, 0, len(current.History)+1)
mergedHistory = append(mergedHistory, displaced)
for _, item := range current.History {
if sameEntryVersion(item, displaced) {
continue
}
mergedHistory = append(mergedHistory, cloneEntry(item))
}
current.History = mergedHistory
return current
}
func mapEntries(entries []vault.Entry) map[string]vault.Entry {
out := make(map[string]vault.Entry, len(entries))
for _, item := range entries {
@@ -429,6 +450,42 @@ func equalAttachments(a, b map[string][]byte) bool {
return true
}
func cloneEntry(entry vault.Entry) vault.Entry {
entry.Tags = slices.Clone(entry.Tags)
entry.Path = slices.Clone(entry.Path)
entry.History = cloneHistory(entry.History)
if entry.Fields != nil {
fields := make(map[string]string, len(entry.Fields))
for key, value := range entry.Fields {
fields[key] = value
}
entry.Fields = fields
}
if entry.Attachments != nil {
attachments := make(map[string][]byte, len(entry.Attachments))
for key, value := range entry.Attachments {
attachments[key] = slices.Clone(value)
}
entry.Attachments = attachments
}
return entry
}
func cloneHistory(history []vault.Entry) []vault.Entry {
if len(history) == 0 {
return nil
}
out := make([]vault.Entry, len(history))
for i := range history {
out[i] = cloneEntry(history[i])
}
return out
}
func sameEntryVersion(a, b vault.Entry) bool {
return entriesEqual(a, b)
}
func mergeGroups(base, local, latest [][]string) [][]string {
set := map[string][]string{}
for _, path := range latest {