//! `koh pluck` — put one save's change onto the current tree. //! //! The engine is per file, never per line: for every path in scope the current //! content must match either the save's parent (clean apply) or the save's own //! version (already applied). Anything else is a hard conflict, reported by //! name, and nothing is applied. module cli import json import src.fmt import src.apply import src.history import src.snapshot pub fn cmd_pluck(raw []string) int { args := parse('pluck', raw, Options{ flags: ['reverse', 'dry-run', 'yes', 'json'] }) or { return usage_error(err.msg()) } store := open_store() or { return refuse(args.json(), err.msg()) } defer { store.close() } id := args.positional_at(0) if id == '' { return usage_error('pluck needs a save ID') } source := history.resolve(store, id) or { return refuse(args.json(), err.msg()) } mut filters := []string{} if args.positional.len > 1 { filters = args.positional[1..] } reverse := args.flag('reverse') verb := if reverse { 'pluck reverse' } else { 'pluck' } source_tree := tree_of(store, source.id) or { return refuse(args.json(), err.msg()) } mut parent_tree := snapshot.Tree{} if source.parent != '' { parent_tree = tree_of(store, source.parent) or { return refuse(args.json(), err.msg()) } } current := working_tree(store) or { return refuse(args.json(), err.msg()) } // Forward pluck applies the save's change; --reverse applies its inverse. base := if reverse { source_tree } else { parent_tree } target := if reverse { parent_tree } else { source_tree } plan := apply.three_way(current.files, base.files, target.files, filters, []) if apply.conflicts(plan) { return refuse(args.json(), '${plan.conflict} has changed since [${source.id}]; koh does not merge lines — resolve it by hand, or load the save and re-edit') } if args.flag('dry-run') || plan.applied.len == 0 { if args.json() { emit_json(json.encode(PluckJson{ source: source.id reverse: reverse applied: plan.applied applied_ok: true })) return 0 } if plan.applied.len == 0 { ok('nothing to apply from [${source.id}]') return 0 } ok('${verb} [${source.id}] would change:') for path in plan.applied { println(' ${amber('~')} ${path}') } note('${fmt.plural(plan.applied.len, 'file')} — run again without --dry-run to apply') return 0 } ok('${verb}: [${source.id}] ${source.message}') for path in plan.applied { println(' ${amber('~')} ${path}') } note('${fmt.plural(plan.applied.len, 'file')} change') approved := confirm(args.flag('yes')) or { return refuse(args.json(), err.msg()) } if !approved { return 1 } // The resulting tree is the working tree with the change applied, so its // object bytes come from disk. save := history.commit_files(store, store.root, plan.files, history.SaveInput{ message: '${verb}: ${source.id}' }) or { return refuse(args.json(), err.msg()) } remove := not_in(current.files, plan.files) report := snapshot.restore_files(store, store.root, snapshot.load(store.root), plan.files, remove) or { return refuse(args.json(), err.msg()) } if args.json() { emit_json(json.encode(PluckedJson{ id: save.id source: source.id reverse: reverse applied: plan.applied written: report.written removed: report.removed })) return 0 } ok('plucked [${source.id}] into [${save.id}]') return 0 } // not_in lists the paths of `before` that `after` no longer has. fn not_in(before []snapshot.FileEntry, after []snapshot.FileEntry) []string { mut kept := map[string]bool{} for entry in after { kept[entry.path] = true } mut missing := []string{} for entry in before { if entry.path !in kept { missing << entry.path } } return missing } struct PluckJson { source string reverse bool applied []string applied_ok bool } struct PluckedJson { id string source string reverse bool applied []string written int removed int }