1234567891011module cli1213import json214import src.fmt15import src.history16import src.machine17import src.snapshot18import src.storage19import src.exchange2021struct StripPlan {22 path string23 scope exchange.Scope24 pending bool25}262728pub fn cmd_strip(raw []string) int {29 args := parse('strip', raw, Options{30 values: ['save']31 flags: ['json']32 }) or { return usage_error(err.msg()) }33 if args.positional.len == 0 {34 return usage_error('strip needs paths, e.g. koh strip secrets.env private/')35 }36 37 if !interactive() {38 return refuse(args.json(), 'strip needs a terminal — an agent can never strip')39 }40 store := open_store() or { return refuse(args.json(), err.msg()) }41 defer { store.close() }42 mut only_save := ''43 if given := args.value_or_none('save') {44 save := history.resolve(store, given) or { return refuse(args.json(), err.msg()) }45 only_save = save.id46 }47 mut plans := []StripPlan{}48 for item in args.positional {49 50 path := if exchange.strip_pending(store, item) { item } else { item.trim_right('/') }51 if plans.any(it.path == path) {52 continue53 }54 scope := exchange.strip_scope(store, path, only_save) or {55 return refuse(args.json(), err.msg())56 }57 pending := exchange.strip_pending(store, path)58 malformed := path == '' || path.starts_with('/')59 || path.split('/').any(it in ['', '.', '..'])60 61 if path == '' || (malformed && scope.files == 0 && !pending) {62 return refuse(args.json(), 'strip needs repo-relative paths: ${snapshot.escape_path(item)}')63 }64 plans << StripPlan{ path: path, scope: scope, pending: pending }65 }66 where := if only_save == '' { 'every save' } else { 'save ${only_save}' }67 for plan in plans {68 if plan.scope.files == 0 {69 if plan.pending {70 note('${snapshot.escape_path(plan.path)} is already stripped here — finishing the remote')71 } else {72 note('nothing in history matches ${snapshot.escape_path(plan.path)}')73 }74 } else {75 note('strip ${snapshot.escape_path(plan.path)}: ${fmt.plural(plan.scope.files, 'file')} in ${fmt.plural(plan.scope.saves, 'save')}, ${fmt.plural(plan.scope.objects, 'object')} — ${where}')76 }77 }78 if plans.any(it.scope.files > 0) {79 println(' ${fmt.red(fmt.glyph_fail)} this cannot be undone — strip is the only command that destroys information')80 note('strip removes paths from history; the files on disk are left alone')81 approved := confirm(false) or { return refuse(args.json(), err.msg()) }82 if !approved {83 note('nothing was stripped')84 return 185 }86 for plan in plans {87 if plan.scope.files == 0 {88 continue89 }90 note('confirm ${snapshot.escape_path(plan.path)}')91 if !typed_confirm('type the path to confirm:', plan.path) {92 note('nothing was stripped — the path did not match')93 return 194 }95 }96 }97 active := plans.filter(it.scope.files > 0 || it.pending)98 locals := exchange.strip_local_batch(store, active.map(it.path), only_save) or {99 return refuse(args.json(), err.msg())100 }101 mut reports := []StripJson{}102 mut index := 0103 mut exit_code := 0104 for plan in plans {105 if plan.scope.files == 0 && !plan.pending {106 reports << StripJson{ path: plan.path, save: only_save }107 continue108 }109 local := locals[index]110 index++111 path := plan.path112 113 sent := send_strip_to_remote(store, path)114 mut ignore_failed := ''115 ignored := only_save == '' && (local.files_removed > 0 || plan.pending)116 && (snapshot.add_ignore(store.root, path) or {117 ignore_failed = '${err.msg()} — add ${snapshot.ignore_rule(path, false, false)} to ${snapshot.ignore_file} by hand; the history ban still applies'118 exit_code = 1119 false120 })121 reports << StripJson{122 path: path123 save: only_save124 files_removed: local.files_removed125 objects_dropped: int(local.objects_dropped)126 records_rewritten: sent.remote.records127 remote_objects_dropped: sent.remote.objects128 remote_failed: sent.failed129 ignore_failed: ignore_failed130 }131 if args.json() {132 continue133 }134 if plan.scope.files > 0 {135 ok('stripped ${snapshot.escape_path(path)} from ${fmt.plural(local.files_removed, 'file')} across ${fmt.plural(plan.scope.saves, 'save')}')136 note('${fmt.plural(int(local.objects_dropped), 'object')} dropped')137 }138 if !sent.skipped {139 if sent.failed == '' {140 note('the remote was updated: ${fmt.plural(sent.remote.records, 'save record')} rewritten, ${fmt.plural(sent.remote.objects, 'object')} dropped')141 } else {142 warn('the remote could not be updated: ${sent.failed} — run the same strip again to finish it')143 }144 }145 if ignore_failed != '' {146 warn('could not update ${snapshot.ignore_file} for ${snapshot.escape_path(path)}: ${ignore_failed}')147 }148 if only_save != '' {149 note('remember to delete ${snapshot.escape_path(path)} or add it to ${snapshot.ignore_file}')150 } else if ignored {151 note('added /${snapshot.escape_path(path)} to ${snapshot.ignore_file} — it can never be saved again')152 }153 }154 if args.json() {155 if plans.len == 1 {156 emit_json(json2.encode(reports[0]))157 } else {158 emit_json(json2.encode({159 'strips': reports160 }))161 }162 }163 return exit_code164}165166struct SendOutcome {167mut:168 remote exchange.SendReport169 failed string170 skipped bool171}172173174fn send_strip_to_remote(store &storage.Store, path string) SendOutcome {175 if !machine.has_remote() {176 return SendOutcome{177 skipped: true178 }179 }180 mut backend := watched_backend('updating the remote') or {181 return SendOutcome{182 failed: err.msg()183 }184 }185 report := exchange.send_strip(store, mut backend, path) or {186 return SendOutcome{187 failed: err.msg()188 }189 }190 return SendOutcome{191 remote: report192 }193}194195struct StripJson {196 path string197 save string198 files_removed int199 objects_dropped int200 records_rewritten int201 remote_objects_dropped int202 remote_failed string203 ignore_failed string204}