cmd_migrate.v

5.2 KB · raw

1//! `koh migrate` — one-time conversion from Koh 2.23module cli45import json26import os7import src.fmt8import src.machine9import src.migrate10import src.snapshot11import time1213pub fn cmd_migrate(raw []string) int {14	args := parse('migrate', raw, Options{15		values: ['lane', 'with']16		flags:  ['json', 'yes']17	}) or { return usage_error(err.msg()) }18	mut lane := args.value('lane')19	if lane == '' {20		lane = migrate.default_lane21	}22	if lane !in ['dev', 'main'] {23		return usage_error('--lane takes dev or main, not "${lane}"')24	}25	root := os.getwd()26	// A crash between the swap's two renames leaves a verified draft beside the27	// archive and no `.koh`: finish it instead of deciding there is no Koh 2 repo.28	if migrate.half_swapped(root) {29		report := migrate.finish_swap(root) or { return refuse(args.json(), err.msg()) }30		if args.json() {31			emit_json(json2.encode(MigrateJson{32				repo:         report.root33				ignore_moved: report.ignore_carried34				build_file:   report.build_renamed35			}))36			return 037		}38		ok('finished the interrupted migration')39		if report.ignore_carried {40			note('carried the ignore rules to ${snapshot.ignore_file}')41		}42		if report.build_renamed {43			note('renamed kepr.build to koh.build, and saved that')44		}45		note('the Koh 2 data is untouched in ${migrate.archive_dir}/ — move it back to go back')46		note('run "koh status" to see what the migration left, then "koh save"')47		return 048	}49	plan := migrate.prepare(root, lane, args.values_of('with')) or { return refuse(args.json(), err.msg()) }50	if !args.json() {51		note('Koh 2 repo · lane ${plan.lane} · ${fmt.plural(plan.saves, 'save')} (${fmt.plural(plan.checkpoints, 'checkpoint')}) · ${fmt.plural(plan.objects, 'object')} · last save ${time.unix(plan.last_saved).ymmdd()}')52		show_lost(plan.lost)53		note('this will:')54		note('  · copy the history into one .koh file and check every save')55		if plan.renames_build {56			note('  · rename kepr.build to koh.build, as its own save')57		}58		note('  · keep the Koh 2 data as ${migrate.archive_dir}/ — move it back to undo')59	}60	approved := confirm(args.flag('yes')) or { return refuse(args.json(), err.msg()) }61	if !approved {62		return refuse(args.json(), 'not migrating, then — nothing changed')63	}64	report := migrate.run(plan, fn (stage string, done int, total int) {65		working('${stage} ${done} / ${total} saves')66	}) or { return refuse(args.json(), err.msg()) }67	if args.json() {68		emit_json(json2.encode(MigrateJson{69			repo:         report.root70			lane:         report.lane71			saves:        report.saves72			checkpoints:  report.checkpoints73			flags:        report.flags74			objects:      report.objects75			head:         report.head76			ignore_moved: report.ignore_carried77			build_file:   report.build_renamed78			lost:         plan.lost.map(it.path)79			unsafe_paths: report.unsafe_paths80		}))81		return 082	}83	ok('migrated ${fmt.plural(report.saves, 'save')} from Koh 2 (lane ${report.lane})')84	note('${fmt.plural(report.checkpoints, 'checkpoint')} · ${fmt.plural(report.flags, 'flag')} · ${fmt.plural(report.objects, 'object')}')85	warn_unsafe_paths(report.unsafe_paths)86	note('the Koh 2 data is untouched in ${migrate.archive_dir}/ — move it back to go back')87	if report.ignore_carried {88		note('carried the ignore rules to ${snapshot.ignore_file}')89	}90	if report.build_renamed {91		note('renamed kepr.build to koh.build, and saved that')92	}93	if report.log_md_was_tracked {94		warn('LOG.md was tracked by Koh 2; koh writes it itself now, so it is no longer snapshotted')95	}96	note('run "koh status" to see what the migration left, then "koh save"')97	if machine.has_remote() {98		note('this machine has a remote — run "koh offer" to publish the migrated history')99	}100	return 0101}102103struct MigrateJson {104	repo         string105	lane         string106	saves        int107	checkpoints  int108	flags        int109	objects      int110	head         string111	ignore_moved bool112	build_file   bool113	lost         []string114	unsafe_paths []string115}116117// show_lost names each file Koh 2 lost versions of, where they were, and what118// can still be done about it before anything is written.119fn show_lost(lost []migrate.Lost) {120	if lost.len == 0 {121		return122	}123	mut paths := []string{}124	for gone in lost {125		if gone.path !in paths {126			paths << gone.path127		}128	}129	warn('Koh 2 has lost ${fmt.plural(lost.len, 'file version')} its saves still list:')130	for path in paths {131		versions := lost.filter(it.path == path)132		mut saves := 0133		mut first := versions[0].first134		mut last := versions[0].last135		for gone in versions {136			saves += gone.saves.len137			first = if gone.first < first { gone.first } else { first }138			last = if gone.last > last { gone.last } else { last }139		}140		mut when := time.unix(first).ymmdd()141		if time.unix(last).ymmdd() != when {142			when += ' to ${time.unix(last).ymmdd()}'143		}144		note('    ${path} · ${fmt.plural(versions.len, 'version')} · in ${fmt.plural(saves, 'save')} · ${when}')145	}146	one := lost.len == 1147	them := if one { 'that version' } else { 'any of those versions' }148	it := if one { 'it' } else { 'them' }149	note("  if you have ${them} — a backup, Time Machine, another machine's copy —")150	note('  hand ${it} back and Koh uses ${it} instead: koh migrate --with <file> …')151	note('  continuing brings those saves over without ${if one { 'it' } else { 'what was lost' }}, each one flagged')152}