1pub mod add;
8pub mod attest;
9pub mod attest_factory;
10pub mod bisect;
11pub mod blame;
12pub mod branch;
13pub mod cat;
14pub mod cat_file;
15pub mod checkout;
16pub mod cherry_pick;
17pub mod clean;
18pub mod clone;
19pub mod commit;
20pub mod config_cmd;
21pub mod conflict;
22pub mod diff;
23pub mod fetch;
24pub mod for_each_ref;
25pub mod gc;
26#[cfg(feature = "git-bridge")]
27pub mod git;
28#[cfg(feature = "git-bridge")]
29pub mod git_import;
30#[cfg(feature = "git-bridge")]
31pub mod git_tools;
32pub mod hash_cmd;
33pub mod init;
34pub mod key;
35pub mod keygen;
36pub mod log;
37pub mod ls_files;
38pub mod ls_tree;
39pub mod mcp;
40pub mod merge;
41pub mod merge_base;
42pub mod mv;
43#[cfg(feature = "pack-shards")]
44pub mod pack_shard;
45pub mod pull;
46pub mod push;
47pub mod rebase;
48pub mod ref_cmd;
49pub mod reflog;
50pub mod remote;
51pub mod reset;
52pub mod restore;
53pub mod rev_list;
54pub mod rev_parse;
55pub mod revert;
56pub mod revspec;
57pub mod rm;
58pub mod self_update;
59pub mod serve;
60pub mod show;
61pub mod show_ref;
62pub mod sparse_checkout;
63pub mod stash;
64pub mod status;
65pub mod summary;
66pub mod switch;
67pub mod symbolic_ref;
68pub mod tag;
69pub mod tree;
70pub mod trust;
71pub mod trust_roots;
72pub mod update_ref;
73pub mod verify;
74pub mod verify_attest;
75pub mod worktree;
76
77use crate::exit;
78use mkit_core::hash::Hash;
79use mkit_core::index::{EntryStatus, Index};
80use mkit_core::layout::RepoLayout;
81use mkit_core::object::Object;
82use mkit_core::ops::diff::{DiffKind, diff_trees};
83use mkit_core::ops::recovery::{self, RecoveryEntry};
84use mkit_core::ops::restore::{RestoreOptions, matches_sparse, restore_tree_to_worktree};
85use mkit_core::refs::{self, Head, RefError, RefWriteCondition};
86use mkit_core::store::ObjectStore;
87use mkit_core::worktree as core_worktree;
88use std::fs;
89use std::io::Write;
90use std::path::Path;
91
92pub(crate) fn commit_subject(store: &ObjectStore, commit: &Hash) -> String {
99 let msg = match store.read_object(commit) {
100 Ok(Object::Commit(c)) => c.message,
101 _ => return String::new(),
102 };
103 String::from_utf8_lossy(&msg)
104 .lines()
105 .next()
106 .unwrap_or("")
107 .to_owned()
108}
109
110pub fn open_store_configured(
114 layout: &RepoLayout,
115) -> Result<ObjectStore, mkit_core::store::StoreError> {
116 let mut store = ObjectStore::open(layout)?;
117 if let Ok(cfg) = crate::config::read_or_default(layout) {
118 store.set_sync_policy(cfg.object_sync_policy());
119 }
120 Ok(store)
121}
122
123pub(crate) fn read_object_bytes(store: &ObjectStore, hash: &Hash) -> Result<Vec<u8>, (String, u8)> {
131 store.read(hash).map_err(|e| {
132 (
133 format!("read {}: {e}", mkit_core::hash::to_hex(hash)),
134 exit::GENERAL_ERROR,
135 )
136 })
137}
138
139pub fn resolve_layout(cwd: &Path) -> Result<RepoLayout, u8> {
148 mkit_core::layout::discover(cwd)
149 .map_err(|e| error(&format!("worktree discovery: {e}"), exit::DATAERR))
150}
151
152#[must_use]
157pub fn not_yet_ported(cmd: &str) -> u8 {
158 let mut stderr = std::io::stderr().lock();
159 let _ = writeln!(stderr, "error: `mkit {cmd}` is not yet wired");
160 exit::TEMPFAIL
161}
162
163#[must_use]
165pub fn usage_error(msg: &str) -> u8 {
166 let mut stderr = std::io::stderr().lock();
167 let _ = writeln!(stderr, "error: {msg}");
168 exit::USAGE
169}
170
171#[must_use]
178pub(crate) fn error(msg: &str, code: u8) -> u8 {
179 let mut stderr = std::io::stderr().lock();
180 let _ = writeln!(stderr, "error: {msg}");
181 code
182}
183
184pub(crate) fn load_tree_hash(store: &ObjectStore, commit_hash: Hash) -> Result<Hash, u8> {
193 match store.read_object(&commit_hash) {
194 Ok(Object::Commit(c)) => Ok(c.tree_hash),
195 Ok(_) => Err(error("object is not a commit", exit::DATAERR)),
196 Err(e) => Err(error(&format!("read commit: {e}"), exit::GENERAL_ERROR)),
197 }
198}
199
200pub(crate) fn advance_head(layout: &RepoLayout, new_head: &Hash) -> Result<(), String> {
212 let head = refs::read_head(layout).map_err(|e| format!("read HEAD: {e}"))?;
213 match head {
214 Head::Branch(name) => {
215 write_ref_recording_history(layout, &name, RefWriteCondition::Any, new_head)
216 .map_err(|e| format!("write ref: {e}"))
217 }
218 Head::Detached(_) => {
219 refs::write_head_detached(layout, new_head).map_err(|e| format!("update HEAD: {e}"))
220 }
221 }
222}
223
224pub(crate) fn restore_head_ref(layout: &RepoLayout, target: &Hash) -> Result<(), u8> {
235 let head =
236 refs::read_head(layout).map_err(|e| error(&format!("read HEAD: {e}"), exit::DATAERR))?;
237 match head {
238 Head::Branch(name) => {
239 write_ref_recording_history(layout, &name, RefWriteCondition::Any, target)
240 .map_err(|e| error(&format!("restore ref: {e}"), exit::CANTCREAT))
241 }
242 Head::Detached(_) => refs::write_head_detached(layout, target)
243 .map_err(|e| error(&format!("restore HEAD: {e}"), exit::CANTCREAT)),
244 }
245}
246
247pub const WORKTREE_LOCK: &str = "worktree.lock";
254
255pub fn acquire_worktree_lock(layout: &RepoLayout) -> Result<mkit_core::repo_lock::RepoLock, u8> {
272 mkit_core::repo_lock::acquire_default(layout.worktree_state_dir(), WORKTREE_LOCK).map_err(|e| {
276 let mut stderr = std::io::stderr().lock();
277 let _ = writeln!(stderr, "error: repo lock: {e}");
278 exit::TEMPFAIL
279 })
280}
281
282pub const WORKTREES_REGISTRY_LOCK: &str = "worktrees.lock";
295
296pub fn acquire_worktrees_registry_lock(
302 layout: &RepoLayout,
303) -> Result<mkit_core::repo_lock::RepoLock, u8> {
304 mkit_core::repo_lock::acquire_default(layout.common_dir(), WORKTREES_REGISTRY_LOCK).map_err(
305 |e| {
306 let mut stderr = std::io::stderr().lock();
307 let _ = writeln!(stderr, "error: worktree registry lock: {e}");
308 exit::TEMPFAIL
309 },
310 )
311}
312
313pub(crate) fn all_worktree_layouts(
323 layout: &RepoLayout,
324) -> Result<Vec<(std::path::PathBuf, RepoLayout)>, String> {
325 let mut out = Vec::new();
326 if let Some(main_root) = layout.common_dir().parent() {
327 out.push((main_root.to_path_buf(), RepoLayout::single(main_root)));
328 }
329 for wt in mkit_core::layout::worktrees(layout).map_err(|e| format!("worktree registry: {e}"))? {
330 if wt.prunable.is_some() {
331 continue;
332 }
333 let Some(tree_root) = wt.tree_root else {
334 continue;
335 };
336 out.push((
337 tree_root.clone(),
338 RepoLayout::linked(tree_root, wt.state_dir, layout.common_dir()),
339 ));
340 }
341 Ok(out)
342}
343
344pub(crate) fn branch_checked_out_elsewhere(
353 layout: &RepoLayout,
354 branch: &str,
355) -> Result<Option<std::path::PathBuf>, String> {
356 let self_state = layout
357 .worktree_state_dir()
358 .canonicalize()
359 .unwrap_or_else(|_| layout.worktree_state_dir().to_path_buf());
360 for (tree_root, candidate) in all_worktree_layouts(layout)? {
361 let candidate_state = candidate
362 .worktree_state_dir()
363 .canonicalize()
364 .unwrap_or_else(|_| candidate.worktree_state_dir().to_path_buf());
365 if candidate_state == self_state {
366 continue; }
368 match refs::read_head(&candidate) {
369 Ok(Head::Branch(name)) if name == branch => return Ok(Some(tree_root)),
370 Ok(_) | Err(RefError::NoHead) => {}
372 Err(e) => {
373 return Err(format!(
374 "read HEAD of worktree at {}: {e}",
375 tree_root.display()
376 ));
377 }
378 }
379 }
380 Ok(None)
381}
382
383pub(crate) fn c_quote_path(path: &str) -> Option<String> {
395 let bytes = path.as_bytes();
396 let needs = bytes
397 .iter()
398 .any(|&b| b < 0x20 || b == b'"' || b == b'\\' || b >= 0x7f);
399 if !needs {
400 return None;
401 }
402 let mut out = String::with_capacity(bytes.len() + 2);
403 out.push('"');
404 for &b in bytes {
405 match b {
406 0x07 => out.push_str("\\a"),
407 0x08 => out.push_str("\\b"),
408 0x09 => out.push_str("\\t"),
409 0x0a => out.push_str("\\n"),
410 0x0b => out.push_str("\\v"),
411 0x0c => out.push_str("\\f"),
412 0x0d => out.push_str("\\r"),
413 b'"' => out.push_str("\\\""),
414 b'\\' => out.push_str("\\\\"),
415 0x20..=0x7e => out.push(b as char),
416 other => {
417 use std::fmt::Write as _;
418 let _ = write!(out, "\\{other:03o}");
419 }
420 }
421 }
422 out.push('"');
423 Some(out)
424}
425
426pub(crate) fn index_path_for_arg(root: &Path, arg: &Path) -> Result<String, String> {
432 use std::path::Component;
433 let rel = if arg.is_absolute() {
434 absolute_arg_to_repo_relative(root, arg)?
435 } else {
436 arg.to_path_buf()
437 };
438
439 let mut parts: Vec<String> = Vec::new();
440 for component in rel.as_path().components() {
441 match component {
442 Component::Normal(part) => {
443 let part = part
444 .to_str()
445 .ok_or_else(|| "path is not valid UTF-8".to_string())?;
446 parts.push(part.to_string());
447 }
448 Component::CurDir => {}
449 Component::ParentDir => {
450 if parts.pop().is_none() {
451 return Err(format!("invalid path: {}", arg.display()));
452 }
453 }
454 Component::Prefix(_) | Component::RootDir => {
455 return Err(format!("invalid path: {}", arg.display()));
456 }
457 }
458 }
459
460 let path = parts.join("/");
461 if !mkit_core::index::validate_index_path(&path) {
462 return Err(format!("invalid path: {path}"));
463 }
464 Ok(path)
465}
466
467pub(crate) fn absolute_arg_to_repo_relative(
471 root: &Path,
472 arg: &Path,
473) -> Result<std::path::PathBuf, String> {
474 use std::ffi::OsString;
475 let root = root.canonicalize().map_err(|e| format!("repo root: {e}"))?;
476
477 if let Ok(rel) = arg.strip_prefix(&root) {
478 return Ok(rel.to_path_buf());
479 }
480
481 let mut suffix: Vec<OsString> = vec![
482 arg.file_name()
483 .ok_or_else(|| format!("invalid path: {}", arg.display()))?
484 .to_os_string(),
485 ];
486 let mut ancestor = arg
487 .parent()
488 .ok_or_else(|| format!("invalid path: {}", arg.display()))?;
489 while ancestor.symlink_metadata().is_err() {
490 let name = ancestor
491 .file_name()
492 .ok_or_else(|| format!("path is outside repository: {}", arg.display()))?;
493 suffix.push(name.to_os_string());
494 ancestor = ancestor
495 .parent()
496 .ok_or_else(|| format!("path is outside repository: {}", arg.display()))?;
497 }
498
499 let mut normalized = ancestor
500 .canonicalize()
501 .map_err(|e| format!("path {}: {e}", ancestor.display()))?;
502 for component in suffix.iter().rev() {
503 normalized.push(component);
504 }
505
506 normalized
507 .strip_prefix(&root)
508 .map(Path::to_path_buf)
509 .map_err(|_| format!("path is outside repository: {}", arg.display()))
510}
511
512pub(crate) fn worktree_entry_state(
520 root: &Path,
521 store: &ObjectStore,
522 path: &str,
523) -> Result<Option<(EntryStatus, Hash)>, String> {
524 let abs = root.join(path);
525 let meta = match abs.symlink_metadata() {
526 Ok(m) => m,
527 Err(e)
528 if matches!(
529 e.kind(),
530 std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory
531 ) =>
532 {
533 return Ok(None);
534 }
535 Err(e) => return Err(format!("metadata {}: {e}", abs.display())),
536 };
537 if meta.file_type().is_file() {
538 let (opened_meta, bytes) = core_worktree::read_regular_file_bounded(&abs)
539 .map_err(|e| format!("read {}: {e}", abs.display()))?;
540 let h =
541 core_worktree::store_file_object(store, &bytes).map_err(|e| format!("store: {e}"))?;
542 Ok(Some((file_exec_status(&opened_meta), h)))
543 } else if meta.file_type().is_symlink() {
544 let target =
545 fs::read_link(&abs).map_err(|e| format!("read link {}: {e}", abs.display()))?;
546 let target_str = target
547 .to_str()
548 .ok_or_else(|| "symlink target is not valid UTF-8".to_string())?;
549 if !core_worktree::validate_symlink_target(target_str) {
550 return Err(format!("invalid symlink target: {target_str}"));
551 }
552 let blob = Object::Blob(mkit_core::object::Blob {
553 data: target_str.as_bytes().to_vec(),
554 });
555 let ser = mkit_core::serialize::serialize(&blob).map_err(|e| format!("serialize: {e}"))?;
556 let h = store.write(&ser).map_err(|e| format!("store: {e}"))?;
557 Ok(Some((EntryStatus::Symlink, h)))
558 } else {
559 Ok(None)
560 }
561}
562
563#[cfg(unix)]
564fn file_exec_status(meta: &fs::Metadata) -> EntryStatus {
565 use std::os::unix::fs::PermissionsExt;
566 if meta.permissions().mode() & 0o111 != 0 {
567 EntryStatus::Executable
568 } else {
569 EntryStatus::Blob
570 }
571}
572
573#[cfg(not(unix))]
574fn file_exec_status(_meta: &fs::Metadata) -> EntryStatus {
575 EntryStatus::Blob
576}
577
578pub(crate) fn index_path_matches_or_descends(path: &str, base: &str) -> bool {
579 path == base || index_path_descends_from(path, base)
580}
581
582pub(crate) fn index_path_descends_from(path: &str, base: &str) -> bool {
583 path.len() > base.len()
584 && path.starts_with(base)
585 && path.as_bytes().get(base.len()) == Some(&b'/')
586}
587
588#[cfg(feature = "history-mmr")]
614pub(crate) fn history_executor() -> std::sync::Arc<mkit_core::history::TokioExecutor> {
615 use std::sync::{Arc, OnceLock};
616 static EXECUTOR: OnceLock<Arc<mkit_core::history::TokioExecutor>> = OnceLock::new();
617 EXECUTOR
618 .get_or_init(|| {
619 let exec = mkit_core::history::TokioExecutor::new()
620 .expect("history-mmr tokio runtime must initialise");
621 Arc::new(exec)
622 })
623 .clone()
624}
625
626pub fn write_ref_recording_history(
664 layout: &RepoLayout,
665 branch: &str,
666 condition: RefWriteCondition,
667 new_hash: &Hash,
668) -> Result<(), RefError> {
669 #[cfg(feature = "history-mmr")]
670 {
671 let exec = history_executor();
672
673 let store = ObjectStore::open(layout)
677 .map_err(|e| RefError::InvalidRef(format!("{branch}: open object store: {e}")))?;
678
679 refs::open_and_update_ref_with_history_and_backfill(
691 layout,
692 branch,
693 condition,
694 new_hash,
695 exec,
696 |h| match store.read_object(h) {
697 Ok(Object::Commit(c)) => Ok(c.parents.first().copied()),
698 Ok(Object::Remix(r)) => Ok(r.parents.first().copied()),
699 Ok(_) => Err(format!(
700 "{}: object is not a commit or remix",
701 mkit_core::hash::to_hex(h)
702 )),
703 Err(e) => Err(e.to_string()),
704 },
705 )
706 }
707 #[cfg(not(feature = "history-mmr"))]
708 {
709 refs::update_ref(layout, branch, condition, new_hash)
710 }
711}
712
713pub fn delete_ref_recording_history(layout: &RepoLayout, branch: &str) -> Result<(), RefError> {
734 #[cfg(feature = "history-mmr")]
735 {
736 refs::delete_ref_safe_with_history(layout, branch, history_executor())
737 }
738 #[cfg(not(feature = "history-mmr"))]
739 {
740 refs::delete_ref_safe(layout, branch)
741 }
742}
743
744pub fn delete_ref_dropping_history(layout: &RepoLayout, branch: &str) -> Result<(), RefError> {
762 #[cfg(feature = "history-mmr")]
763 {
764 refs::delete_ref_with_history(layout, branch, history_executor())
765 }
766 #[cfg(not(feature = "history-mmr"))]
767 {
768 refs::delete_ref(layout, branch)
769 }
770}
771
772pub fn delete_ref_dropping_history_if_matches(
791 layout: &RepoLayout,
792 branch: &str,
793 expected: Hash,
794) -> Result<(), RefError> {
795 #[cfg(feature = "history-mmr")]
796 {
797 refs::delete_ref_with_history_if_matches(layout, branch, expected, history_executor())
798 }
799 #[cfg(not(feature = "history-mmr"))]
800 {
801 refs::delete_ref_if_matches(layout, branch, expected)
802 }
803}
804
805#[must_use]
808pub fn head_branch_name(layout: &RepoLayout) -> String {
809 match refs::read_head(layout) {
810 Ok(Head::Branch(name)) => name,
811 _ => String::new(),
812 }
813}
814
815pub fn record_superseded(
827 layout: &RepoLayout,
828 op: &str,
829 branch: &str,
830 superseded: Hash,
831) -> Result<(), (String, u8)> {
832 let timestamp = std::time::SystemTime::now()
833 .duration_since(std::time::UNIX_EPOCH)
834 .map_or(0, |d| d.as_secs());
835 let entry = RecoveryEntry {
836 timestamp,
837 op: op.to_owned(),
838 superseded,
839 branch: branch.to_owned(),
840 };
841 recovery::record(layout, &entry).map_err(|e| (format!("recovery log: {e}"), exit::CANTCREAT))
842}
843
844pub fn sync_index_to_tree(
850 layout: &RepoLayout,
851 store: &ObjectStore,
852 tree_hash: Hash,
853) -> Result<(), String> {
854 let mut idx =
855 mkit_core::index::from_tree(store, tree_hash).map_err(|e| format!("index: {e}"))?;
856 if let Ok(old) = mkit_core::index::read_index(layout) {
861 let by_path: std::collections::HashMap<&str, &mkit_core::index::IndexEntry> =
864 old.entries.iter().map(|o| (o.path.as_str(), o)).collect();
865 for e in &mut idx.entries {
866 if let Some(o) = by_path.get(e.path.as_str())
867 && o.object_hash == e.object_hash
868 && o.status == e.status
869 {
870 e.mtime_ns = o.mtime_ns;
871 e.size = o.size;
872 e.ino = o.ino;
873 e.ctime_ns = o.ctime_ns;
874 }
875 }
876 }
877 mkit_core::index::write_index(layout, &idx).map_err(|e| format!("write index: {e}"))
878}
879
880pub fn stage_removed_tombstones(
890 layout: &RepoLayout,
891 store: &ObjectStore,
892 base_tree: Option<Hash>,
893 result_tree: Hash,
894) -> Result<(), String> {
895 let diff = diff_trees(store, base_tree, Some(result_tree))
896 .map_err(|e| format!("diff for staged deletions: {e}"))?;
897 let removed: Vec<String> = diff
898 .entries
899 .iter()
900 .filter(|e| e.kind == DiffKind::Removed)
901 .map(|e| e.path.clone())
902 .collect();
903 if removed.is_empty() {
904 return Ok(());
905 }
906 let mut idx = mkit_core::index::read_index(layout).map_err(|e| format!("read index: {e}"))?;
907 for path in removed {
908 match idx.find_entry(&path) {
909 Some(j) => {
910 idx.entries[j].status = EntryStatus::Removed;
911 idx.entries[j].object_hash = mkit_core::hash::ZERO;
912 }
913 None => idx.upsert_entry(mkit_core::index::IndexEntry {
914 path,
915 status: EntryStatus::Removed,
916 object_hash: mkit_core::hash::ZERO,
917 mtime_ns: 0,
918 size: 0,
919 ino: 0,
920 ctime_ns: 0,
921 }),
922 }
923 }
924 mkit_core::index::write_index(layout, &idx).map_err(|e| format!("write index: {e}"))
925}
926
927pub fn restore_worktree_and_index(
929 layout: &RepoLayout,
930 store: &ObjectStore,
931 tree_hash: Hash,
932) -> Result<(), String> {
933 restore_tree_to_worktree(
934 store,
935 &tree_hash,
936 layout.worktree_root(),
937 &RestoreOptions::default(),
938 )
939 .map_err(|e| format!("restore worktree: {e}"))?;
940 sync_index_to_tree(layout, store, tree_hash)
941}
942
943pub fn ensure_restore_safe(
945 layout: &RepoLayout,
946 store: &ObjectStore,
947 target_tree: Hash,
948) -> Result<(), String> {
949 ensure_restore_safe_with_options(layout, store, target_tree, &RestoreOptions::default())
950}
951
952pub fn ensure_restore_safe_with_options(
954 layout: &RepoLayout,
955 store: &ObjectStore,
956 target_tree: Hash,
957 options: &RestoreOptions,
958) -> Result<(), String> {
959 let root = layout.worktree_root();
960 let current_tree = current_head_tree(layout, store)?;
961 let idx = read_or_seed_index_from_head(layout, store)?;
962 let snapshot = mkit_core::store::EphemeralSink::new(store);
965 let index_tree = core_worktree::build_tree_from_index_with(store, &snapshot, &idx, false)
966 .map_err(|e| format!("check index state: {e}"))?;
967
968 let staged = diff_trees(&snapshot, current_tree, Some(index_tree))
969 .map_err(|e| format!("check staged changes: {e}"))?;
970 if let Some(entry) = staged
971 .entries
972 .iter()
973 .find(|entry| restore_affects_path(options, &entry.path))
974 {
975 return Err(format!(
976 "restore would overwrite staged changes; commit, stash, or reset '{}' first",
977 entry.path
978 ));
979 }
980
981 let worktree_tree = core_worktree::build_tree_filtered(&snapshot, root, Some(&idx))
982 .map_err(|e| format!("check working tree changes: {e}"))?;
983 let unstaged = diff_trees(&snapshot, Some(index_tree), Some(worktree_tree))
984 .map_err(|e| format!("check working tree changes: {e}"))?;
985 if let Some(entry) = unstaged
986 .entries
987 .iter()
988 .find(|entry| entry.kind != DiffKind::Added && restore_affects_path(options, &entry.path))
989 {
990 return Err(format!(
991 "restore would overwrite local changes; commit, stash, or reset '{}' first",
992 entry.path
993 ));
994 }
995
996 let target_writes = diff_trees(&snapshot, Some(index_tree), Some(target_tree))
997 .map_err(|e| format!("check restore target: {e}"))?
998 .entries
999 .into_iter()
1000 .filter(|entry| entry.kind != DiffKind::Removed)
1001 .filter(|entry| restore_affects_path(options, &entry.path))
1002 .map(|entry| entry.path)
1003 .collect::<Vec<_>>();
1004 if target_writes.is_empty() && !options.clean {
1005 return Ok(());
1006 }
1007
1008 let ignore = mkit_core::ignore::load(root).map_err(|e| format!("read ignore file: {e}"))?;
1009 let mut worktree_paths = Vec::new();
1010 collect_worktree_paths(root, root, "", &mut worktree_paths)
1011 .map_err(|e| format!("check untracked paths: {e}"))?;
1012 if let Some(path) = worktree_paths.iter().find(|path| {
1013 !index_tracks_path_or_descendant(&idx, path)
1014 && target_writes
1015 .iter()
1016 .any(|target| paths_overlap(path, target))
1017 }) {
1018 return Err(format!(
1019 "restore would overwrite untracked path '{path}'; move or remove it first"
1020 ));
1021 }
1022
1023 if options.clean
1024 && let Some(path) = worktree_paths.iter().find(|path| {
1025 !index_tracks_path_or_descendant(&idx, path)
1026 && restore_affects_path(options, path)
1027 && *path != ".mkitignore"
1028 && *path != ".gitignore"
1029 && !is_ignored_worktree_path(root, &ignore, path)
1030 })
1031 {
1032 return Err(format!(
1033 "restore would remove untracked path '{path}'; move or remove it first"
1034 ));
1035 }
1036
1037 Ok(())
1038}
1039
1040pub(crate) fn restore_affects_path(options: &RestoreOptions, path: &str) -> bool {
1041 options
1042 .sparse_patterns
1043 .as_deref()
1044 .is_none_or(|patterns| matches_sparse(patterns, path, false))
1045}
1046
1047pub(crate) fn dropped_tracked_paths(
1054 layout: &RepoLayout,
1055 store: &ObjectStore,
1056 target_tree: Hash,
1057) -> Result<Vec<(String, EntryStatus, Hash)>, String> {
1058 let idx = read_or_seed_index_from_head(layout, store)?;
1059 let snapshot = mkit_core::store::EphemeralSink::new(store);
1060 let index_tree = core_worktree::build_tree_from_index_with(store, &snapshot, &idx, false)
1061 .map_err(|e| format!("index tree: {e}"))?;
1062 let mut out = Vec::new();
1063 for e in diff_trees(&snapshot, Some(index_tree), Some(target_tree))
1064 .map_err(|e| format!("diff index vs target: {e}"))?
1065 .entries
1066 .into_iter()
1067 .filter(|e| e.kind == DiffKind::Removed)
1068 {
1069 if let Some(entry) = idx
1070 .entries
1071 .iter()
1072 .find(|ie| ie.path == e.path && ie.status != EntryStatus::Removed)
1073 {
1074 out.push((e.path, entry.status, entry.object_hash));
1075 }
1076 }
1077 Ok(out)
1078}
1079
1080pub(crate) fn locally_modified_dropped_path(
1087 cwd: &Path,
1088 store: &ObjectStore,
1089 dropped: &[(String, EntryStatus, Hash)],
1090) -> Result<Option<String>, String> {
1091 for (path, idx_status, idx_hash) in dropped {
1092 if let Some((wt_status, wt_hash)) = worktree_entry_state(cwd, store, path)?
1093 && (wt_status != *idx_status || wt_hash != *idx_hash)
1094 {
1095 return Ok(Some(path.clone()));
1096 }
1097 }
1098 Ok(None)
1099}
1100
1101pub(crate) fn remove_dropped_path(abs: &Path) -> std::io::Result<()> {
1107 match fs::symlink_metadata(abs) {
1108 Ok(meta) if meta.is_dir() => Ok(()),
1109 Ok(_) => fs::remove_file(abs),
1110 Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
1111 Err(e) => Err(e),
1112 }
1113}
1114
1115fn is_ignored_worktree_path(
1116 root: &Path,
1117 ignore: &mkit_core::ignore::IgnoreList,
1118 path: &str,
1119) -> bool {
1120 let full_path = root.join(path);
1121 let Ok(meta) = fs::symlink_metadata(&full_path) else {
1122 return false;
1123 };
1124 ignore.is_ignored_with_ancestors(path, meta.is_dir())
1127}
1128
1129pub(crate) fn current_head_tree(
1130 layout: &RepoLayout,
1131 store: &ObjectStore,
1132) -> Result<Option<Hash>, String> {
1133 let Some(head_hash) = refs::resolve_head(layout).map_err(|e| format!("resolve HEAD: {e}"))?
1134 else {
1135 return Ok(None);
1136 };
1137 match store
1138 .read_object(&head_hash)
1139 .map_err(|e| format!("read HEAD: {e}"))?
1140 {
1141 Object::Commit(c) => Ok(Some(c.tree_hash)),
1142 Object::Remix(r) => Ok(Some(r.tree_hash)),
1143 _ => Err("HEAD does not resolve to a commit or remix".to_string()),
1144 }
1145}
1146
1147pub(crate) fn collect_worktree_paths(
1148 root: &Path,
1149 dir: &Path,
1150 prefix: &str,
1151 out: &mut Vec<String>,
1152) -> std::io::Result<()> {
1153 let read = match fs::read_dir(dir) {
1154 Ok(read) => read,
1155 Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
1156 Err(e) => return Err(e),
1157 };
1158 for entry in read {
1159 let entry = entry?;
1160 let name = entry.file_name();
1161 let Some(name) = name.to_str() else {
1162 continue;
1163 };
1164 if name.eq_ignore_ascii_case(".mkit") || name.eq_ignore_ascii_case(".git") {
1165 continue;
1166 }
1167 let path = if prefix.is_empty() {
1168 name.to_string()
1169 } else {
1170 format!("{prefix}/{name}")
1171 };
1172 out.push(path.clone());
1173 let full_path = root.join(&path);
1174 let meta = fs::symlink_metadata(&full_path)?;
1175 if meta.is_dir() {
1176 collect_worktree_paths(root, &full_path, &path, out)?;
1177 }
1178 }
1179 Ok(())
1180}
1181
1182pub(crate) fn index_tracks_path_or_descendant(index: &Index, path: &str) -> bool {
1183 index.tracks_path_or_descendant(path)
1188}
1189
1190fn paths_overlap(left: &str, right: &str) -> bool {
1191 index_path_matches_or_descends(left, right) || index_path_descends_from(right, left)
1192}
1193
1194pub fn read_or_seed_index_from_head(
1201 layout: &RepoLayout,
1202 store: &ObjectStore,
1203) -> Result<mkit_core::index::Index, String> {
1204 let idx = mkit_core::index::read_index(layout).map_err(|e| format!("read index: {e}"))?;
1205 if !idx.entries.is_empty() {
1206 return Ok(idx);
1207 }
1208
1209 let Some(head_hash) =
1210 mkit_core::refs::resolve_head(layout).map_err(|e| format!("resolve HEAD: {e}"))?
1211 else {
1212 return Ok(idx);
1213 };
1214 match store
1215 .read_object(&head_hash)
1216 .map_err(|e| format!("read HEAD: {e}"))?
1217 {
1218 Object::Commit(c) => mkit_core::index::from_tree(store, c.tree_hash)
1219 .map_err(|e| format!("index from HEAD: {e}")),
1220 Object::Remix(r) => mkit_core::index::from_tree(store, r.tree_hash)
1221 .map_err(|e| format!("index from HEAD: {e}")),
1222 _ => Err("HEAD does not resolve to a commit or remix".to_string()),
1223 }
1224}
1225
1226#[cfg(test)]
1227mod tests {
1228 use super::{advance_head, c_quote_path, restore_head_ref};
1229 use mkit_core::hash::Hash;
1230
1231 #[cfg(feature = "history-mmr")]
1232 fn write_commit(store: &mkit_core::store::ObjectStore, parents: Vec<Hash>, seed: u8) -> Hash {
1233 use mkit_core::object::{Commit, Identity, Object};
1234
1235 let commit = Commit::new_unannotated(
1236 [seed; 32],
1237 parents,
1238 Identity::ed25519([seed; 32]),
1239 [seed; 32],
1240 b"msg".to_vec(),
1241 0,
1242 [0u8; 64],
1243 );
1244 let bytes = mkit_core::serialize::serialize(&Object::Commit(commit)).unwrap();
1245 store.write(&bytes).unwrap()
1246 }
1247
1248 #[cfg(feature = "history-mmr")]
1249 #[test]
1250 fn write_ref_recording_history_backfills_v01x_style_repo_from_object_store() {
1251 use super::write_ref_recording_history;
1252 use mkit_core::history::{CommitHistory, Position, TokioExecutor, verify_inclusion};
1253 use mkit_core::refs::{self, RefWriteCondition};
1254 use mkit_core::store::ObjectStore;
1255 use std::sync::Arc;
1256
1257 let td = tempfile::tempdir().unwrap();
1258 let repo_root = td.path();
1259 let layout = mkit_core::layout::RepoLayout::single(repo_root);
1260 let store = ObjectStore::init(&layout).unwrap();
1261
1262 let c0 = write_commit(&store, vec![], 1);
1267 let c1 = write_commit(&store, vec![c0], 2);
1268 let c2 = write_commit(&store, vec![c1], 3);
1269 refs::write_ref(&layout, "main", &c2).unwrap();
1270
1271 let c3 = write_commit(&store, vec![c2], 4);
1274 write_ref_recording_history(&layout, "main", RefWriteCondition::Match(c2), &c3).unwrap();
1275
1276 assert_eq!(refs::read_ref(&layout, "main").unwrap(), Some(c3));
1277
1278 let exec = Arc::new(TokioExecutor::new().unwrap());
1281 let hist = CommitHistory::open_at(exec, &layout, "main").unwrap();
1282 assert_eq!(hist.len(), 4);
1283 let root = hist.root();
1284 for (i, c) in [c0, c1, c2, c3].into_iter().enumerate() {
1285 let pos = Position(i as u64);
1286 let proof = hist.prove(pos).unwrap();
1287 assert!(
1288 verify_inclusion(&c, pos, &proof, &root),
1289 "commit at position {i} failed inclusion proof after backfill"
1290 );
1291 }
1292 }
1293
1294 #[cfg(feature = "history-mmr")]
1295 #[test]
1296 fn write_ref_recording_history_does_not_backfill_a_genuinely_fresh_branch() {
1297 use super::write_ref_recording_history;
1298 use mkit_core::history::{CommitHistory, TokioExecutor};
1299 use mkit_core::refs::RefWriteCondition;
1300 use mkit_core::store::ObjectStore;
1301 use std::sync::Arc;
1302
1303 let td = tempfile::tempdir().unwrap();
1304 let repo_root = td.path();
1305 let layout = mkit_core::layout::RepoLayout::single(repo_root);
1306 let store = ObjectStore::init(&layout).unwrap();
1307
1308 let c0 = write_commit(&store, vec![], 1);
1311 write_ref_recording_history(&layout, "main", RefWriteCondition::Missing, &c0).unwrap();
1312
1313 let exec = Arc::new(TokioExecutor::new().unwrap());
1314 let hist = CommitHistory::open_at(exec, &layout, "main").unwrap();
1315 assert_eq!(
1316 hist.len(),
1317 1,
1318 "only the one real write, no phantom backfill entries"
1319 );
1320 }
1321
1322 #[cfg(feature = "history-mmr")]
1326 const CONCURRENT_BACKFILL_CHAIN_LEN: usize = 500;
1327
1328 #[cfg(feature = "history-mmr")]
1347 #[test]
1348 fn write_ref_recording_history_concurrent_backfill_does_not_duplicate_journal_leaves() {
1349 use super::write_ref_recording_history;
1350 use mkit_core::history::{CommitHistory, TokioExecutor};
1351 use mkit_core::refs::{self, RefWriteCondition};
1352 use mkit_core::store::ObjectStore;
1353 use std::sync::{Arc, Barrier};
1354
1355 let td = tempfile::tempdir().unwrap();
1356 let repo_root = td.path();
1357 let layout = Arc::new(mkit_core::layout::RepoLayout::single(repo_root));
1358 let store = ObjectStore::init(&layout).unwrap();
1359
1360 let mut tip: Option<Hash> = None;
1361 for seed in 0..CONCURRENT_BACKFILL_CHAIN_LEN {
1362 let seed = u8::try_from(seed % 256).expect("seed % 256 fits in u8");
1363 tip = Some(write_commit(&store, tip.into_iter().collect(), seed));
1364 }
1365 let tip = tip.unwrap();
1366 refs::write_ref(&layout, "main", &tip).unwrap();
1367
1368 let c_a = write_commit(&store, vec![tip], 250);
1371 let c_b = write_commit(&store, vec![tip], 251);
1372
1373 let barrier = Arc::new(Barrier::new(2));
1374
1375 let (layout_a, barrier_a) = (Arc::clone(&layout), Arc::clone(&barrier));
1376 let t_a = std::thread::spawn(move || {
1377 barrier_a.wait();
1378 write_ref_recording_history(&layout_a, "main", RefWriteCondition::Any, &c_a)
1379 });
1380 let (layout_b, barrier_b) = (Arc::clone(&layout), Arc::clone(&barrier));
1381 let t_b = std::thread::spawn(move || {
1382 barrier_b.wait();
1383 write_ref_recording_history(&layout_b, "main", RefWriteCondition::Any, &c_b)
1384 });
1385
1386 let res_a = t_a.join().expect("thread a must not panic");
1387 let res_b = t_b.join().expect("thread b must not panic");
1388 res_a.expect("writer a must succeed");
1389 res_b.expect("writer b must succeed");
1390
1391 let exec = Arc::new(TokioExecutor::new().unwrap());
1392 let hist = CommitHistory::open_at(exec, &layout, "main").unwrap();
1393 assert_eq!(
1394 hist.len(),
1395 CONCURRENT_BACKFILL_CHAIN_LEN as u64 + 2,
1396 "two concurrent first-writers on a never-journaled branch \
1397 must backfill the shared chain exactly once between them \
1398 (plus their own two real appends) — a leaf count above \
1399 this means the backfill ran twice and duplicated leaves"
1400 );
1401 }
1402
1403 #[test]
1404 fn c_quote_leaves_plain_paths_alone() {
1405 assert_eq!(c_quote_path("a.txt"), None);
1406 assert_eq!(c_quote_path("dir/with space.txt"), None); assert_eq!(c_quote_path("weird-but-ascii_!@#$%.rs"), None);
1408 }
1409
1410 #[test]
1411 fn c_quote_escapes_special_bytes() {
1412 assert_eq!(c_quote_path("a\tb.txt").as_deref(), Some(r#""a\tb.txt""#));
1413 assert_eq!(
1414 c_quote_path("line\nfeed").as_deref(),
1415 Some(r#""line\nfeed""#)
1416 );
1417 assert_eq!(c_quote_path("q\"x").as_deref(), Some(r#""q\"x""#));
1418 assert_eq!(
1419 c_quote_path("back\\slash").as_deref(),
1420 Some(r#""back\\slash""#)
1421 );
1422 }
1423
1424 #[test]
1425 fn c_quote_octal_escapes_non_ascii() {
1426 assert_eq!(c_quote_path("é").as_deref(), Some(r#""\303\251""#));
1428 assert_eq!(c_quote_path("x-é").as_deref(), Some(r#""x-\303\251""#));
1430 }
1431
1432 #[test]
1440 fn advance_head_errors_when_head_missing_instead_of_writing_main() {
1441 let td = tempfile::tempdir().unwrap();
1442 let layout = mkit_core::layout::RepoLayout::single(td.path());
1443 let new_head: Hash = [0x11; 32];
1445 let err = advance_head(&layout, &new_head).expect_err("missing HEAD must error");
1446 assert!(err.contains("read HEAD"), "unexpected error: {err}");
1447 assert!(
1449 !layout.heads_dir().join("main").exists(),
1450 "advance_head must not write refs/heads/main when HEAD is unreadable"
1451 );
1452 }
1453
1454 #[test]
1455 fn restore_head_ref_errors_when_head_missing_instead_of_writing_main() {
1456 let td = tempfile::tempdir().unwrap();
1457 let layout = mkit_core::layout::RepoLayout::single(td.path());
1458 let target: Hash = [0x22; 32];
1459 let code = restore_head_ref(&layout, &target).expect_err("missing HEAD must error");
1460 assert_eq!(code, crate::exit::DATAERR);
1461 assert!(
1462 !layout.heads_dir().join("main").exists(),
1463 "restore_head_ref must not write refs/heads/main when HEAD is unreadable"
1464 );
1465 }
1466}