1#![doc = include_str!("../README.md")]
2#![deny(unsafe_code)]
22
23pub mod clap_shim;
24pub mod cli;
25pub mod commands;
26pub mod config;
27pub mod editor;
28pub mod exit;
29pub mod format;
30pub mod progress;
31pub mod remote_dispatch;
32pub mod signal;
33#[cfg(feature = "sparse-checkout")]
34pub mod sparse_cache;
35pub mod term;
36
37use std::io::Write;
38
39#[must_use]
48#[allow(clippy::too_many_lines)] pub fn dispatch(argv: &[String]) -> u8 {
50 let (cmd_idx, overrides) = match parse_global_flags(argv) {
54 Ok(parsed) => parsed,
55 Err(code) => return code,
56 };
57 config::set_cli_overrides(overrides);
58
59 if cmd_idx >= argv.len() {
60 print_usage_stderr();
61 return exit::USAGE;
62 }
63 let cmd = &argv[cmd_idx];
64 let rest: Vec<String> = argv.iter().skip(cmd_idx + 1).cloned().collect();
65
66 match cmd.as_str() {
67 "-h" | "--help" | "help" => {
68 let mut stdout = std::io::stdout().lock();
69 let _ = stdout.write_all(cli::HELP_TEXT.as_bytes());
70 exit::OK
71 }
72 "version" | "--version" | "-V" => {
73 let mut stdout = std::io::stdout().lock();
74 let _ = writeln!(stdout, "mkit {}", cli::CLI_VERSION);
82 exit::OK
83 }
84 "init" => commands::init::run(&rest),
85 "key" => commands::key::run(&rest),
86 "keygen" => commands::keygen::run(&rest),
87 "hash" => commands::hash_cmd::run(&rest),
88 "cat" => commands::cat::run(&rest),
89 "cat-file" => commands::cat_file::run(&rest),
90 "ls-tree" => commands::ls_tree::run(&rest),
91 "ls-files" => commands::ls_files::run(&rest),
92 "rev-parse" => commands::rev_parse::run(&rest),
93 "show" => commands::show::run(&rest),
94 "show-ref" => commands::show_ref::run(&rest),
95 "for-each-ref" => commands::for_each_ref::run(&rest),
96 "symbolic-ref" => commands::symbolic_ref::run(&rest),
97 "update-ref" => commands::update_ref::run(&rest),
98 "ref" => commands::ref_cmd::run(&rest),
99 "tree" => commands::tree::run(&rest),
100 "add" => commands::add::run(&rest),
101 "rm" => commands::rm::run(&rest),
102 "mv" => commands::mv::run(&rest),
103 "restore" => commands::restore::run(&rest),
104 "reset" => commands::reset::run(&rest),
105 "status" => commands::status::run(&rest),
106 "commit" => commands::commit::run(&rest),
107 "log" => commands::log::run(&rest),
108 "reflog" => commands::reflog::run(&rest),
109 "branch" => commands::branch::run(&rest),
110 "tag" => commands::tag::run(&rest),
111 "checkout" => commands::checkout::run(&rest),
112 "switch" => commands::switch::run(&rest),
113 "merge-base" => commands::merge_base::run(&rest),
114 "rev-list" => commands::rev_list::run(&rest),
115 "clean" => commands::clean::run(&rest),
116 "diff" => commands::diff::run(&rest),
117 "verify" => commands::verify::run(&rest),
118 "attest" => commands::attest::run(&rest),
119 "verify-attest" => commands::verify_attest::run(&rest),
120 "trust" => commands::trust::run(&rest),
121 "config" => commands::config_cmd::run(&rest),
122 "remote" => commands::remote::run(&rest),
123 "push" => commands::push::run(&rest),
124 "pull" => commands::pull::run(&rest),
125 "fetch" => commands::fetch::run(&rest),
126 "clone" => commands::clone::run(&rest),
127 "mcp" => commands::mcp::run(&rest),
128 "merge" => commands::merge::run(&rest),
129 "cherry-pick" => commands::cherry_pick::run(&rest),
130 "revert" => commands::revert::run(&rest),
131 "rebase" => commands::rebase::run(&rest),
132 "bisect" => commands::bisect::run(&rest),
133 "gc" => commands::gc::run(&rest),
134 "stash" => commands::stash::run(&rest),
135 "worktree" => commands::worktree::run(&rest),
136 "blame" => commands::blame::run(&rest),
137 "self" => commands::self_update::run(&rest),
138 "serve" => commands::serve::run(&rest),
139 #[cfg(feature = "git-bridge")]
140 "git" => commands::git::run(&rest),
141 #[cfg(not(feature = "git-bridge"))]
142 "git" => {
143 let mut stderr = std::io::stderr().lock();
144 let _ = writeln!(
145 stderr,
146 "error: the git bridge is not compiled into this binary; \
147 rebuild with `--features git-bridge` (see docs/specs/SPEC-GIT-BRIDGE.md)"
148 );
149 exit::UNAVAILABLE
150 }
151 "sparse-checkout" => commands::sparse_checkout::run(&rest),
152 #[cfg(feature = "pack-shards")]
153 "pack-shard" => commands::pack_shard::run(&rest),
154 #[cfg(not(feature = "pack-shards"))]
155 "pack-shard" => {
156 let mut stderr = std::io::stderr().lock();
161 let _ = writeln!(
162 stderr,
163 "error: pack-shard is not compiled into this binary; \
164 rebuild with `--features pack-shards`"
165 );
166 exit::UNAVAILABLE
167 }
168 other => {
169 let mut stderr = std::io::stderr().lock();
170 let _ = writeln!(
171 stderr,
172 "error: unknown command '{other}' (run 'mkit --help' for a list of commands)"
173 );
174 exit::USAGE
175 }
176 }
177}
178
179fn parse_global_flags(argv: &[String]) -> Result<(usize, Vec<(String, String)>), u8> {
187 let mut i = 1; let mut overrides: Vec<(String, String)> = Vec::new();
189 while i < argv.len() {
190 let arg = argv[i].as_str();
191 if arg == "-C" {
192 let Some(path) = argv.get(i + 1) else {
193 return Err(global_flag_err("option `-C` requires a path"));
194 };
195 chdir(path)?;
196 i += 2;
197 } else if let Some(path) = arg.strip_prefix("-C").filter(|p| !p.is_empty()) {
198 chdir(path)?;
199 i += 1;
200 } else if arg == "-c" {
201 let Some(kv) = argv.get(i + 1) else {
202 return Err(global_flag_err("option `-c` requires <key>=<value>"));
203 };
204 overrides.push(split_config_override(kv)?);
205 i += 2;
206 } else if let Some(kv) = arg.strip_prefix("-c").filter(|kv| !kv.is_empty()) {
207 overrides.push(split_config_override(kv)?);
208 i += 1;
209 } else if matches!(arg, "--no-pager" | "-P" | "--paginate") {
210 i += 1;
213 } else {
214 break;
215 }
216 }
217 Ok((i, overrides))
218}
219
220fn chdir(path: &str) -> Result<(), u8> {
223 std::env::set_current_dir(path).map_err(|e| {
224 let mut stderr = std::io::stderr().lock();
225 let _ = writeln!(stderr, "error: cannot change to '{path}': {e}");
226 exit::NOINPUT
227 })
228}
229
230fn split_config_override(kv: &str) -> Result<(String, String), u8> {
232 match kv.split_once('=') {
233 Some((k, v)) if !k.is_empty() => Ok((k.to_string(), v.to_string())),
234 _ => Err(global_flag_err(
235 "option `-c` expects <key>=<value> (e.g. -c user.email=ci@example.com)",
236 )),
237 }
238}
239
240fn global_flag_err(msg: &str) -> u8 {
241 let mut stderr = std::io::stderr().lock();
242 let _ = writeln!(stderr, "error: {msg}");
243 exit::USAGE
244}
245
246fn print_usage_stderr() {
247 let mut stderr = std::io::stderr().lock();
248 let _ = stderr.write_all(cli::HELP_TEXT.as_bytes());
249}
250
251#[cfg(test)]
252mod tests {
253 use super::*;
254
255 #[test]
256 fn dispatch_version_returns_ok() {
257 let argv = vec!["mkit".to_string(), "version".to_string()];
259 assert_eq!(dispatch(&argv), exit::OK);
260 }
261
262 #[test]
263 fn dispatch_unknown_command_returns_usage() {
264 let argv = vec!["mkit".to_string(), "definitely-not-a-command".to_string()];
265 assert_eq!(dispatch(&argv), exit::USAGE);
266 }
267
268 #[test]
269 fn dispatch_bare_binary_returns_usage() {
270 let argv = vec!["mkit".to_string()];
271 assert_eq!(dispatch(&argv), exit::USAGE);
272 }
273}