cmd_save.v

3.8 KB · raw

1//! `koh save` and `koh checkpoint`.23module cli45import json26import src.fmt7import src.history8import src.snapshot910// cmd_save records the tree as a save, or as a checkpoint. It needs a message, and a plain save refuses11// when nothing has changed; a checkpoint records an unchanged tree.12pub fn cmd_save(raw []string, is_checkpoint bool) int {13	verb := if is_checkpoint { 'checkpoint' } else { 'save' }14	args := parse(verb, raw, Options{15		values: ['agent', 'session']16		flags:  ['json']17	}) or { return usage_error(err.msg()) }18	if args.positional.len > 1 {19		example := if is_checkpoint { 'before x' } else { 'fix the bug' }20		return usage_error('${verb} takes one message — quote it: koh ${verb} "${example}"')21	}22	message := args.positional_at(0)23	if message == '' {24		example := if is_checkpoint {25			'before combine replay engine'26		} else {27			'fix off-by-one in chunk boundary'28		}29		return usage_error('${verb} needs a message, e.g. koh ${verb} "${example}"')30	}31	// A message is printed by view, status and LOG.md, and may be read back from32	// a remote: refuse control characters here, before they can reach history.33	for field in [['message', message], ['agent', args.value('agent')],34		['session', args.value('session')]] {35		if byte := control_byte(field[1]) {36			return usage_error('a ${verb} ${field[0]} cannot contain control characters (found 0x${byte.hex()})')37		}38	}39	store := open_store() or { return refuse(args.json(), err.msg()) }40	defer {41		store.close()42	}43	mut hashing := reading()44	walked := snapshot.walk_report(store, store.root, snapshot.load(store.root), mut hashing) or {45		return refuse(args.json(), err.msg())46	}47	save := history.save_worktree(store, store.root, snapshot.load(store.root), history.SaveInput{48		message:       message49		agent:         args.value('agent')50		session:       args.value('session')51		is_checkpoint: is_checkpoint52		storing:       fn (done int, total int) {53			if done % 100 == 1 || done == total {54				working('storing ${done} / ${total} files')55			}56		}57	}) or { return refuse(args.json(), err.msg()) }5859	files := history.files_of(store, save.id) or { []snapshot.FileEntry{} }60	if args.json() {61		emit_json(json2.encode(SaveOut{62			id:            save.id63			parent:        save.parent64			message:       save.message65			created_at:    save.created_at66			machine:       save.machine67			is_checkpoint: save.is_checkpoint68			agent:         save.agent69			session:       save.session70			files:         files.len71			size_bytes:    save.size_bytes72			unsafe_paths:  walked.unsafe_paths73		}))74		auto_offer(store, true)75		return 076	}77	for path in walked.unsafe_paths {78		warn('not saved: unsafe name ${path}')79	}80	ok('saved [${save.id}]  ${fmt.safe_text(save.message)}')81	mut detail := []string{}82	if save.is_checkpoint {83		detail << 'checkpoint'84	}85	detail << fmt.stamp(save.created_at)86	detail << fmt.plural(files.len, 'file')87	detail << fmt.human_bytes(save.size_bytes)88	note(detail.join(' · '))89	mut attribution := []string{}90	if save.agent != '' {91		attribution << 'agent: ${fmt.safe_text(save.agent)}'92	}93	if save.session != '' {94		attribution << 'session: ${fmt.safe_text(save.session)}'95	}96	if attribution.len > 0 {97		note(attribution.join(' · '))98	}99	auto_offer(store, false)100	return 0101}102103// control_byte finds the first control character in a field, so the caller can104// refuse it by name before the store is opened.105fn control_byte(text string) ?u8 {106	for c in text.runes() {107		if c < 0x20 || c == 0x7f {108			return u8(c)109		}110	}111	return none112}113114// Every field is always present, so a consumer never has to tell "absent" from115// "missing key". An empty string means the save recorded none.116struct SaveOut {117	id            string118	parent        string119	message       string120	created_at    i64121	machine       string122	is_checkpoint bool123	agent         string124	session       string125	files         int126	size_bytes    i64127	unsafe_paths  []string128}