12module cli34import os5import src.build6import src.exchange7import src.fmt8import src.events9import src.history10import src.snapshot11import src.storage1213fn doctor_history(saves []history.Save, head string) []string {14 mut problems := []string{}15 mut known := map[string]history.Save{}16 for save in saves {17 known[save.id] = save18 }19 if head == '' && saves.len > 0 { problems << 'history has saves but no head' }20 if head != '' && head !in known { problems << 'head ${head} names a missing save' }21 mut done := map[string]bool{}22 for save in saves {23 if save.parent != '' && save.parent !in known {24 problems << 'save ${save.id} has missing parent ${save.parent}'25 }26 if save.restores != '' && save.restores !in known {27 problems << 'save ${save.id} restores missing save ${save.restores}'28 }29 mut cursor := save.id30 mut seen := map[string]bool{}31 for cursor != '' && cursor in known && cursor !in done {32 if cursor in seen {33 problems << 'history has a parent cycle at ${cursor}'34 break35 }36 seen[cursor] = true37 cursor = known[cursor].parent38 }39 for id in seen.keys() {40 done[id] = true41 }42 }43 return problems44}454647fn missing_saves(saves []history.Save, head string) []string {48 mut known := map[string]bool{}49 for save in saves {50 known[save.id] = true51 }52 mut missing := []string{}53 for save in saves {54 for id in [save.parent, save.restores] {55 if id != '' && id !in known && id !in missing {56 missing << id57 }58 }59 }60 if head != '' && head !in known && head !in missing {61 missing << head62 }63 return missing64}6566fn doctor_files(id string, files []snapshot.FileEntry, event_list []events.Event,67 mut report DoctorJson) {68 69 70 strips := event_list.filter(events.valid_strip(it))71 mut paths := map[string]bool{}72 for file in files {73 paths[file.path] = true74 }75 for file in files {76 if !snapshot.legal_path(file.path) {77 report.problems << 'save [${id}] has unsafe path ${snapshot.escape_path(file.path)} — koh strip ${shell_quote(file.path)}'78 }79 mut parent := os.dir(file.path)80 for parent != '.' && parent != '' && parent != '/' {81 if parent in paths {82 report.problems << 'save ${id}: ${snapshot.escape_path(parent)} is both a file and a directory'83 break84 }85 parent = os.dir(parent)86 }87 for event in strips {88 if (event.save_id == '' || event.save_id == id)89 && (file.path == event.path || file.path.starts_with('${event.path.trim_right('/')}/')) {90 report.problems << 'save ${id} still contains stripped path ${snapshot.escape_path(file.path)}'91 report.strip_residue = true92 if event.path !in report.strip_residue_paths {93 report.strip_residue_paths << event.path94 }95 }96 }97 }98}99100fn doctor_events(event_list []events.Event, saves []history.Save, mut report DoctorJson) {101 ids := saves.map(it.id)102 mut flags := map[string]events.Event{}103 for event in event_list {104 if !exchange.valid_event_id(event.id) {105 report.problems << 'event ${event.id} has an invalid ID'106 }107 if event.created_at < 0 { report.problems << 'event ${event.id} has a negative timestamp' }108 if event.kind !in [events.kind_flag, events.kind_clear, events.kind_strip] {109 report.problems << 'event ${event.id} has unknown kind ${event.kind}'110 } else if event.kind != events.kind_strip && event.save_id == '' {111 report.problems << 'event ${event.id} has no save target'112 } else if event.kind == events.kind_strip && event.path == '' {113 report.problems << 'strip event ${event.id} has no path'114 }115 116 if event.save_id != '' && event.save_id !in ids {117 report.warnings << 'event ${event.id} targets unavailable save ${event.save_id}; nothing to do'118 }119 if event.kind in [events.kind_flag, events.kind_clear] {120 old := flags[event.save_id] or { events.Event{} }121 if event.created_at > old.created_at || (event.created_at == old.created_at && event.id > old.id) {122 flags[event.save_id] = event123 }124 }125 }126 report.events = event_list.len127 report.flagged_saves = flags.values().filter(it.kind == events.kind_flag).len128 if head_flag := flags[report.head_id] {129 report.head_flagged = head_flag.kind == events.kind_flag130 if report.head_flagged {131 report.warnings << 'head is flagged: ${fmt.safe_text(head_flag.reason)}; koh flag --clear removes the flag'132 }133 }134}135136fn doctor_local(store &storage.Store, mut report DoctorJson) ! {137 stats := store.doctor_stats()!138 report.metadata_available = true139 report.objects = int(stats.objects)140 report.saves = int(stats.saves)141 report.checkpoints = int(stats.checkpoints)142 report.cached_files = int(stats.cached_files)143 report.manifests = stats.manifests144 report.object_bytes = stats.object_bytes145 report.packed_bytes = stats.packed_bytes146 report.unreferenced_objects = stats.unused147 report.source = stats.state['source']148 report.remote_repo = stats.state['remote_repo']149 report.last_offered_head = stats.state['remote_head']150 report.head_id = stats.state[storage.head_key]151 report.restore_unfinished = 'restoring' in stats.state152 report.pending_strips = stats.pending153 report.upload_unsettled = exchange.unsettled_key in stats.state154 report.check('references and metadata', stats.problems)155 saves := history.all_saves(store)!156 report.check('history links and cycles', doctor_history(saves, report.head_id))157 report.missing_saves = missing_saves(saves, report.head_id)158 mut known := map[string]history.Save{}159 for save in saves {160 known[save.id] = save161 }162 mut seen := map[string]bool{}163 mut cursor := report.head_id164 for cursor != '' && cursor in known && cursor !in seen {165 seen[cursor] = true166 cursor = known[cursor].parent167 }168 report.line_saves = seen.len169 report.off_line_saves = saves.len - seen.len170 if head := known[report.head_id] {171 report.head = head172 report.head_files = history.files_of(store, head.id)!.len173 }174 event_list := events.all(store)!175 before := report.problems.len176 doctor_events(event_list, saves, mut report)177 178 strips := event_list.filter(events.valid_strip(it))179 for save in saves {180 doctor_files(save.id, history.files_of(store, save.id)!, strips, mut report)181 if save.replayed_from != '' && save.replayed_from !in known {182 report.warnings << 'save ${save.id} was replayed from unavailable save ${save.replayed_from}; nothing to do'183 }184 }185 for path in stats.pending {186 exchange.read_pending(store, path) or { report.problems << err.msg() }187 }188 exchange.unsettled(store) or { report.problems << err.msg() }189 report.check('saved paths and events', report.problems[before..].clone())190 if report.restore_unfinished {191 report.check('restore state', [snapshot.restore_unfinished])192 } else {193 report.check('restore state', []string{})194 }195 for path in report.pending_strips {196 report.warnings << 'remote strip unfinished for ${snapshot.escape_path(path)}; retry koh strip ${shell_quote(path)}'197 }198 if report.upload_unsettled {199 report.warnings << 'an upload stopped before it could remove what a remote strip covers; after koh steal, koh doctor can finish it'200 }201 if stats.unused > 0 {202 report.warnings << '${fmt.plural(int(stats.unused), 'unreferenced object')} retained; no save uses them and koh doctor can remove them'203 }204}205206fn doctor_working_tree(store &storage.Store, mut report DoctorJson) ! {207 store.drop_cache()!208 report.cache_cleared = true209 if !report.metadata_available || (report.saves > 0 && report.head.id == '') {210 report.checks << DoctorCheck{'working tree', 'skipped', 'head is missing', ''}211 return212 }213 for path in [os.join_path(store.root, snapshot.ignore_file), snapshot.global_ignore_path()] {214 if !os.exists(path) {215 continue216 }217 text := os.read_file(path) or {218 report.check('ignore rules', [err.msg()])219 continue220 }221 report.ignore_files[path] = text.split_into_lines().filter(it.trim_space() != '' && !it.trim_space().starts_with('#')).len222 }223 files := history.files_of(store, report.head_id)!224 ig := snapshot.load(store.root)225 report.ignored_saved_files = files.filter(ig.matches(it.path, false)).map(it.path)226 report.changes = compare_with_head(store, report.head)!227 mut problems := []string{}228 snapshot.check_links(store.root, files) or { problems << err.msg() }229 for path in report.changes.unsafe_paths {230 problems << 'unsafe working name ${path}; this file cannot be saved'231 }232 report.check('working tree safety', problems)233 report.checks << DoctorCheck{'working tree changes', 'pass', '${report.changes.added.len} added, ${report.changes.modified.len} modified, ${report.changes.removed.len} removed', ''}234 if report.ignored_saved_files.len > 0 {235 report.warnings << 'ignore rules now cover ${fmt.plural(report.ignored_saved_files.len, 'saved file')}; older versions remain in history, and koh strip removes them if they must go'236 }237}238239fn doctor_build(root string, mut report DoctorJson) {240 if !os.exists(os.join_path(root, build.config_name)) {241 report.checks << DoctorCheck{'build configuration', 'skipped', 'no koh.build', ''}242 return243 }244 config := build.read_config(root) or {245 report.check('build configuration', [err.msg()])246 return247 }248 report.build_platforms = config.platforms249 report.project_version = build.project_version(root) or {250 report.check('build configuration', [err.msg()])251 return252 }253 mut paths := map[string]bool{}254 mut problems := []string{}255 for platform in config.platforms {256 path := build.output_path(config, platform, report.project_version)257 if path in paths { problems << 'multiple platforms share build output ${path}' }258 paths[path] = true259 }260 report.check('build configuration', problems)261}