1use clap::{Arg, ArgAction, Command, ValueHint};
2use clap_complete::{Shell, generate};
3use std::io::{self, Write};
4
5pub fn dispatch(shell_raw: &str, extra: &[String]) -> i32 {
6 if !extra.is_empty() {
7 eprintln!("git-cli: error: expected `git-cli completion <bash|zsh>`");
8 return 1;
9 }
10
11 match shell_raw {
12 "bash" => generate_script(Shell::Bash),
13 "zsh" => generate_script(Shell::Zsh),
14 other => {
15 eprintln!("git-cli: error: unsupported completion shell '{other}'");
16 eprintln!("usage: git-cli completion <bash|zsh>");
17 1
18 }
19 }
20}
21
22fn generate_script(generator: Shell) -> i32 {
23 let mut command = build_command_model();
24 let bin_name = command.get_name().to_string();
25 if matches!(generator, Shell::Bash) {
26 let mut output = Vec::new();
27 generate(generator, &mut command, bin_name.clone(), &mut output);
28 let normalized = normalize_bash_completion(
29 String::from_utf8(output).expect("bash completion should be valid UTF-8"),
30 );
31 io::stdout()
32 .write_all(normalized.as_bytes())
33 .expect("failed to write bash completion");
34 return 0;
35 }
36
37 generate(generator, &mut command, bin_name, &mut io::stdout());
38 0
39}
40
41fn normalize_bash_completion(script: String) -> String {
42 script.replace("__subcmd__", "__")
43}
44
45fn build_command_model() -> Command {
46 Command::new("git-cli")
47 .version(env!("CARGO_PKG_VERSION"))
48 .long_version(nils_build_info::long_version(env!("CARGO_PKG_VERSION")))
49 .about("Git helper CLI")
50 .disable_help_subcommand(true)
51 .subcommand(build_utils_group())
52 .subcommand(build_reset_group())
53 .subcommand(build_commit_group())
54 .subcommand(build_branch_group())
55 .subcommand(build_worktree_group())
56 .subcommand(build_ci_group())
57 .subcommand(build_open_group())
58 .subcommand(Command::new("help").about("Display help message for git-cli"))
59 .subcommand(
60 Command::new("completion")
61 .about("Export shell completion script")
62 .arg(
63 Arg::new("shell")
64 .value_name("shell")
65 .value_parser(["bash", "zsh"])
66 .required(true),
67 ),
68 )
69}
70
71fn build_utils_group() -> Command {
72 Command::new("utils")
73 .about("Utility helpers")
74 .subcommand(Command::new("zip").about("Create zip archive from HEAD"))
75 .subcommand(
76 Command::new("copy-staged")
77 .visible_alias("copy")
78 .about("Copy staged diff to clipboard")
79 .arg(
80 Arg::new("stdout")
81 .long("stdout")
82 .help("Print staged diff to stdout")
83 .action(ArgAction::SetTrue),
84 )
85 .arg(
86 Arg::new("print")
87 .short('p')
88 .long("print")
89 .help("Alias for --stdout")
90 .action(ArgAction::SetTrue),
91 )
92 .arg(
93 Arg::new("both")
94 .long("both")
95 .help("Print diff and copy it to clipboard")
96 .action(ArgAction::SetTrue),
97 ),
98 )
99 .subcommand(
100 Command::new("root").about("Jump to git root").arg(
101 Arg::new("shell")
102 .long("shell")
103 .help("Print shell command instead of plain output")
104 .action(ArgAction::SetTrue),
105 ),
106 )
107 .subcommand(
108 Command::new("commit-hash")
109 .visible_alias("hash")
110 .about("Resolve commit hash")
111 .arg(Arg::new("ref").value_name("ref")),
112 )
113 .subcommand(Command::new("help").about("Display help message for utils"))
114}
115
116fn build_reset_group() -> Command {
117 let count_arg = || Arg::new("count").value_name("count");
118
119 Command::new("reset")
120 .about("Reset helpers")
121 .subcommand(
122 Command::new("soft")
123 .about("Reset to HEAD~N (soft)")
124 .arg(count_arg()),
125 )
126 .subcommand(
127 Command::new("mixed")
128 .about("Reset to HEAD~N (mixed)")
129 .arg(count_arg()),
130 )
131 .subcommand(
132 Command::new("hard")
133 .about("Reset to HEAD~N (hard)")
134 .arg(count_arg()),
135 )
136 .subcommand(Command::new("undo").about("Undo last reset"))
137 .subcommand(Command::new("back-head").about("Checkout HEAD@{1}"))
138 .subcommand(Command::new("back-checkout").about("Return to previous branch"))
139 .subcommand(
140 Command::new("remote")
141 .about("Reset to remote branch")
142 .arg(
143 Arg::new("ref")
144 .long("ref")
145 .help("Remote ref in <remote>/<branch> form")
146 .value_name("ref"),
147 )
148 .arg(
149 Arg::new("remote")
150 .short('r')
151 .long("remote")
152 .help("Remote name")
153 .value_name("remote"),
154 )
155 .arg(
156 Arg::new("branch")
157 .short('b')
158 .long("branch")
159 .help("Remote branch name")
160 .value_name("branch"),
161 )
162 .arg(
163 Arg::new("no-fetch")
164 .long("no-fetch")
165 .help("Skip fetching remote refs")
166 .action(ArgAction::SetTrue),
167 )
168 .arg(
169 Arg::new("prune")
170 .long("prune")
171 .help("Run fetch with --prune")
172 .action(ArgAction::SetTrue),
173 )
174 .arg(
175 Arg::new("clean")
176 .long("clean")
177 .help("Run git clean -fd after reset")
178 .action(ArgAction::SetTrue),
179 )
180 .arg(
181 Arg::new("set-upstream")
182 .long("set-upstream")
183 .help("Set upstream to the target remote branch")
184 .action(ArgAction::SetTrue),
185 )
186 .arg(
187 Arg::new("yes")
188 .short('y')
189 .long("yes")
190 .help("Skip confirmation prompts")
191 .action(ArgAction::SetTrue),
192 ),
193 )
194 .subcommand(Command::new("help").about("Display help message for reset"))
195}
196
197fn build_commit_group() -> Command {
198 Command::new("commit")
199 .about("Commit helpers")
200 .subcommand(
201 Command::new("context")
202 .about("Print commit context")
203 .arg(
204 Arg::new("stdout")
205 .long("stdout")
206 .help("Print report to stdout")
207 .action(ArgAction::SetTrue),
208 )
209 .arg(
210 Arg::new("both")
211 .long("both")
212 .help("Print report and write output file")
213 .action(ArgAction::SetTrue),
214 )
215 .arg(
216 Arg::new("no-color")
217 .long("no-color")
218 .help("Disable ANSI colors")
219 .action(ArgAction::SetTrue),
220 )
221 .arg(
222 Arg::new("include")
223 .long("include")
224 .help("Additional glob(s) to include")
225 .value_name("glob")
226 .num_args(1..),
227 ),
228 )
229 .subcommand(
230 Command::new("context-json")
231 .visible_aliases(["context_json", "contextjson", "json"])
232 .about("Print commit context as JSON")
233 .arg(
234 Arg::new("stdout")
235 .long("stdout")
236 .help("Print JSON to stdout")
237 .action(ArgAction::SetTrue),
238 )
239 .arg(
240 Arg::new("both")
241 .long("both")
242 .help("Print JSON and write files")
243 .action(ArgAction::SetTrue),
244 )
245 .arg(
246 Arg::new("pretty")
247 .long("pretty")
248 .help("Pretty-print JSON output")
249 .action(ArgAction::SetTrue),
250 )
251 .arg(
252 Arg::new("bundle")
253 .long("bundle")
254 .help("Write bundle files to output directory")
255 .action(ArgAction::SetTrue),
256 )
257 .arg(
258 Arg::new("out-dir")
259 .long("out-dir")
260 .help("Output directory for generated files")
261 .value_name("path"),
262 ),
263 )
264 .subcommand(
265 Command::new("to-stash")
266 .visible_alias("stash")
267 .about("Create stash from commit")
268 .arg(Arg::new("ref").value_name("ref")),
269 )
270 .subcommand(Command::new("help").about("Display help message for commit"))
271}
272
273fn build_branch_group() -> Command {
274 Command::new("branch")
275 .about("Branch helpers")
276 .subcommand(
277 Command::new("cleanup")
278 .visible_alias("delete-merged")
279 .about("Delete merged branches")
280 .arg(
281 Arg::new("base")
282 .short('b')
283 .long("base")
284 .help("Base ref used to determine merged branches")
285 .value_name("base"),
286 )
287 .arg(
288 Arg::new("squash")
289 .short('s')
290 .long("squash")
291 .help("Include branches already applied via squash")
292 .action(ArgAction::SetTrue),
293 )
294 .arg(
295 Arg::new("remove-worktrees")
296 .short('w')
297 .long("remove-worktrees")
298 .help("Force-remove linked worktrees for candidate branches")
299 .action(ArgAction::SetTrue),
300 ),
301 )
302 .subcommand(Command::new("help").about("Display help message for branch"))
303}
304
305fn build_worktree_group() -> Command {
306 Command::new("worktree")
307 .about("Worktree helpers")
308 .subcommand(
309 Command::new("add")
310 .about("Create a managed agent worktree")
311 .arg(Arg::new("slug").value_name("slug").required(true))
312 .arg(
313 Arg::new("from")
314 .long("from")
315 .help("Base ref for the new branch")
316 .value_name("ref"),
317 )
318 .arg(format_arg()),
319 )
320 .subcommand(
321 Command::new("list")
322 .about("List git worktrees")
323 .arg(format_arg()),
324 )
325 .subcommand(
326 Command::new("remove")
327 .about("Remove a managed worktree by slug or path")
328 .arg(
329 Arg::new("target")
330 .value_name("slug-or-path")
331 .required(true)
332 .value_hint(ValueHint::AnyPath),
333 )
334 .arg(format_arg()),
335 )
336 .subcommand(
337 Command::new("prune")
338 .about("Prune stale git worktree metadata")
339 .arg(format_arg()),
340 )
341 .subcommand(Command::new("help").about("Display help message for worktree"))
342}
343
344fn build_ci_group() -> Command {
345 Command::new("ci")
346 .about("CI helpers")
347 .subcommand(
348 Command::new("pick")
349 .about("Cherry-pick into CI branch")
350 .arg(
351 Arg::new("remote")
352 .short('r')
353 .long("remote")
354 .help("Remote used for fetch/push")
355 .value_name("name"),
356 )
357 .arg(
358 Arg::new("no-fetch")
359 .long("no-fetch")
360 .help("Skip remote fetch before branch creation")
361 .action(ArgAction::SetTrue),
362 )
363 .arg(
364 Arg::new("force")
365 .short('f')
366 .long("force")
367 .help("Reset existing CI branch and force push")
368 .action(ArgAction::SetTrue),
369 )
370 .arg(
371 Arg::new("stay")
372 .long("stay")
373 .help("Stay on CI branch after push")
374 .action(ArgAction::SetTrue),
375 ),
376 )
377 .subcommand(Command::new("help").about("Display help message for ci"))
378}
379
380fn build_open_group() -> Command {
381 Command::new("open")
382 .about("Open remote pages")
383 .subcommand(
384 Command::new("repo")
385 .about("Open repository page")
386 .arg(remotes_arg()),
387 )
388 .subcommand(
389 Command::new("branch")
390 .about("Open branch tree page")
391 .arg(Arg::new("ref").value_name("ref")),
392 )
393 .subcommand(
394 Command::new("default-branch")
395 .visible_alias("default")
396 .about("Open default branch tree page")
397 .arg(remotes_arg()),
398 )
399 .subcommand(
400 Command::new("commit")
401 .about("Open commit page")
402 .arg(Arg::new("ref").value_name("ref")),
403 )
404 .subcommand(
405 Command::new("compare")
406 .about("Open compare page")
407 .arg(Arg::new("from").value_name("from"))
408 .arg(Arg::new("to").value_name("to")),
409 )
410 .subcommand(
411 Command::new("pr")
412 .visible_aliases(["pull-request", "mr", "merge-request"])
413 .about("Open pull or merge request page")
414 .arg(Arg::new("id").value_name("id")),
415 )
416 .subcommand(
417 Command::new("pulls")
418 .visible_aliases(["prs", "merge-requests", "mrs"])
419 .about("Open pull or merge request list"),
420 )
421 .subcommand(
422 Command::new("issues")
423 .visible_alias("issue")
424 .about("Open issues list/page")
425 .arg(Arg::new("id").value_name("id")),
426 )
427 .subcommand(
428 Command::new("actions")
429 .visible_alias("action")
430 .about("Open actions page")
431 .arg(Arg::new("workflow").value_name("workflow")),
432 )
433 .subcommand(
434 Command::new("releases")
435 .visible_alias("release")
436 .about("Open releases list/page")
437 .arg(Arg::new("tag").value_name("tag")),
438 )
439 .subcommand(
440 Command::new("tags")
441 .visible_alias("tag")
442 .about("Open tags list/page")
443 .arg(Arg::new("tag").value_name("tag")),
444 )
445 .subcommand(
446 Command::new("commits")
447 .visible_alias("history")
448 .about("Open commit history page")
449 .arg(Arg::new("ref").value_name("ref")),
450 )
451 .subcommand(
452 Command::new("file")
453 .visible_alias("blob")
454 .about("Open file page")
455 .arg(
456 Arg::new("path")
457 .value_name("path")
458 .value_hint(ValueHint::FilePath),
459 )
460 .arg(Arg::new("ref").value_name("ref")),
461 )
462 .subcommand(
463 Command::new("blame")
464 .about("Open blame page")
465 .arg(
466 Arg::new("path")
467 .value_name("path")
468 .value_hint(ValueHint::FilePath),
469 )
470 .arg(Arg::new("ref").value_name("ref")),
471 )
472 .subcommand(Command::new("help").about("Display help message for open"))
473}
474
475fn remotes_arg() -> Arg {
476 Arg::new("remote").value_name("remote")
477}
478
479fn format_arg() -> Arg {
480 Arg::new("format")
481 .long("format")
482 .help("Output format")
483 .value_name("format")
484 .value_parser(["text", "json"])
485}