| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "sort" |
| 5 | "strings" |
| 6 | |
| 7 | "reasonix/desktop/internal/workspacestate" |
| 8 | ) |
| 9 | |
| 10 | // historicalVersionSourceKey reports whether key names a reviewed version: the |
| 11 | // original source key, ":review:", then that version's fingerprint. |
| 12 | func historicalVersionSourceKey(key string) bool { |
| 13 | source, version, reviewed := strings.Cut(key, ":review:") |
| 14 | return reviewed && source != "" && version != "" |
| 15 | } |
| 16 | |
| 17 | func historicalSourceKeyMatches(mappingKey, sourceID string) bool { |
| 18 | return mappingKey == sourceID || strings.HasPrefix(mappingKey, sourceID+":review:") |
| 19 | } |
| 20 | |
| 21 | func historicalMappingForSource(state workspacestate.State, sourceID string) (workspacestate.SourceMapping, bool, error) { |
| 22 | if mapping, ok, err := state.ResolveSource(sourceID); ok || err != nil { |
| 23 | return mapping, ok, err |
| 24 | } |
| 25 | keys := make([]string, 0, len(state.SourceMappings)) |
| 26 | for key := range state.SourceMappings { |
| 27 | for _, alias := range state.SourceKeys(key) { |
| 28 | if historicalSourceKeyMatches(alias, sourceID) { |
| 29 | keys = append(keys, key) |
| 30 | break |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | sort.Strings(keys) |
| 35 | if len(keys) == 0 { |
| 36 | return workspacestate.SourceMapping{}, false, nil |
| 37 | } |
| 38 | return state.SourceMappings[keys[0]], true, nil |
| 39 | } |
| 40 |