cmd_save.v

3.9 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_head := history.head_id(store)45	walked := snapshot.walk_report(store, store.root, snapshot.load(store.root), mut hashing) or {46		return refuse(args.json(), err.msg())47	}48	save := history.save_walked(store, store.root, history.SaveInput{49		message:       message50		agent:         args.value('agent')51		session:       args.value('session')52		is_checkpoint: is_checkpoint53		storing:       fn (done int, total int) {54			if done % 100 == 1 || done == total {55				working('storing ${done} / ${total} files')56			}57		}58	}, walked_head, walked.files) or { return refuse(args.json(), err.msg()) }5960	files := history.files_of(store, save.id) or { []snapshot.FileEntry{} }61	if args.json() {62		emit_json(json2.encode(SaveOut{63			id:            save.id64			parent:        save.parent65			message:       save.message66			created_at:    save.created_at67			machine:       save.machine68			is_checkpoint: save.is_checkpoint69			agent:         save.agent70			session:       save.session71			files:         files.len72			size_bytes:    save.size_bytes73			unsafe_paths:  walked.unsafe_paths74		}))75		auto_offer(store, true)76		return 077	}78	for path in walked.unsafe_paths {79		warn('not saved: unsafe name ${path}')80	}81	ok('saved [${save.id}]  ${fmt.safe_text(save.message)}')82	mut detail := []string{}83	if save.is_checkpoint {84		detail << 'checkpoint'85	}86	detail << fmt.stamp(save.created_at)87	detail << fmt.plural(files.len, 'file')88	detail << fmt.human_bytes(save.size_bytes)89	note(detail.join(' · '))90	mut attribution := []string{}91	if save.agent != '' {92		attribution << 'agent: ${fmt.safe_text(save.agent)}'93	}94	if save.session != '' {95		attribution << 'session: ${fmt.safe_text(save.session)}'96	}97	if attribution.len > 0 {98		note(attribution.join(' · '))99	}100	auto_offer(store, false)101	return 0102}103104// control_byte finds the first control character in a field, so the caller can105// refuse it by name before the store is opened.106fn control_byte(text string) ?u8 {107	for c in text.runes() {108		if c < 0x20 || c == 0x7f {109			return u8(c)110		}111	}112	return none113}114115// Every field is always present, so a consumer never has to tell "absent" from116// "missing key". An empty string means the save recorded none.117struct SaveOut {118	id            string119	parent        string120	message       string121	created_at    i64122	machine       string123	is_checkpoint bool124	agent         string125	session       string126	files         int127	size_bytes    i64128	unsafe_paths  []string129}