cmd_pluck.v

4.8 KB · raw

1//! `koh pluck` — put one save's change onto the current tree.2//!3//! The engine is per file, never per line: for every path in scope the current4//! content must match either the save's parent (clean apply) or the save's own5//! version (already applied). Anything else is a hard conflict, reported by6//! name, and nothing is applied.78module cli910import json11import src.apply12import src.fmt13import src.history14import src.snapshot15import src.storage1617pub fn cmd_pluck(raw []string) int {18	args := parse('pluck', raw, Options{19		flags: ['reverse', 'dry-run', 'yes', 'json']20	}) or { return usage_error(err.msg()) }21	store := open_store() or { return refuse(args.json(), err.msg()) }22	defer {23		store.close()24	}25	id := args.positional_at(0)26	if id == '' {27		return usage_error('pluck needs a save ID')28	}29	source := history.resolve(store, id) or { return refuse(args.json(), err.msg()) }30	mut filters := []string{}31	if args.positional.len > 1 {32		filters = args.positional[1..]33	}34	// A dry run only reads, so it may look at a dirty tree; a real pluck35	// rewrites files and must not overwrite unsaved work.36	if !args.flag('dry-run') {37		require_clean(store) or { return refuse(args.json(), err.msg()) }38	}39	reverse := args.flag('reverse')40	verb := if reverse { 'pluck reverse' } else { 'pluck' }4142	plan, _ := plan_pluck(store, source, reverse, filters) or {43		return refuse(args.json(), err.msg())44	}4546	if args.flag('dry-run') || plan.applied.len == 0 {47		if args.json() {48			emit_json(json.encode(PluckJson{49				source:     source.id50				reverse:    reverse51				applied:    plan.applied52				applied_ok: true53			}))54			return 055		}56		if plan.applied.len == 0 {57			ok('nothing to apply from [${source.id}]')58			return 059		}60		ok('${verb} [${source.id}] would change:')61		for path in plan.applied {62			println('  ${fmt.amber('~')} ${path}')63		}64		note('${fmt.plural(plan.applied.len, 'file')} — run again without --dry-run to apply')65		return 066	}6768	ok('${verb}: [${source.id}]  ${source.message}')69	for path in plan.applied {70		println('  ${fmt.amber('~')} ${path}')71	}72	note('${fmt.plural(plan.applied.len, 'file')} change')73	approved := confirm(args.flag('yes')) or { return refuse(args.json(), err.msg()) }74	if !approved {75		return 176	}77	// The tree may have changed while the prompt waited, so the pluck acts on a78	// fresh plan; the one above only drew the preview.79	require_clean(store) or { return refuse(args.json(), err.msg()) }80	fresh, current := plan_pluck(store, source, reverse, filters) or {81		return refuse(args.json(), err.msg())82	}83	if fresh.applied.len == 0 {84		return refuse(args.json(),85			'nothing to apply from [${source.id}] any more — the tree changed while waiting')86	}87	// The resulting tree is the working tree with the change applied, so its88	// object bytes come from disk.89	save := history.commit_files(store, store.root, fresh.files, history.SaveInput{90		message: '${verb}: ${source.id}'91	}) or { return refuse(args.json(), err.msg()) }92	remove := not_in(current, fresh.files)93	report := snapshot.restore_files(store, store.root, snapshot.load(store.root), fresh.files,94		remove) or { return refuse(args.json(), err.msg()) }95	if args.json() {96		emit_json(json.encode(PluckedJson{97			id:      save.id98			source:  source.id99			reverse: reverse100			applied: fresh.applied101			written: report.written102			removed: report.removed103		}))104		return 0105	}106	ok('plucked [${source.id}] into [${save.id}]')107	return 0108}109110// plan_pluck applies the source save's change (or, reversed, its inverse) to111// the working tree, and returns the plan with the tree it was made from.112fn plan_pluck(store &storage.Store, source history.Save, reverse bool, filters []string) !(apply.Plan, []snapshot.FileEntry) {113	source_tree := tree_of(store, source.id)!114	mut parent_tree := snapshot.Tree{}115	if source.parent != '' {116		parent_tree = tree_of(store, source.parent)!117	}118	current := working_tree(store)!119	base := if reverse { source_tree } else { parent_tree }120	target := if reverse { parent_tree } else { source_tree }121	plan := apply.three_way(current.files, base.files, target.files, filters, [])122	if apply.conflicts(plan) {123		return error('${plan.conflict} has changed since [${source.id}]; koh does not merge lines — resolve it by hand, or load the save and re-edit')124	}125	return plan, current.files126}127128// not_in lists the paths of `before` that `after` no longer has.129fn not_in(before []snapshot.FileEntry, after []snapshot.FileEntry) []string {130	mut kept := map[string]bool{}131	for entry in after {132		kept[entry.path] = true133	}134	mut missing := []string{}135	for entry in before {136		if entry.path !in kept {137			missing << entry.path138		}139	}140	return missing141}142143struct PluckJson {144	source     string145	reverse    bool146	applied    []string147	applied_ok bool148}149150struct PluckedJson {151	id      string152	source  string153	reverse bool154	applied []string155	written int156	removed int157}