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_ci_group())
56 .subcommand(build_open_group())
57 .subcommand(Command::new("help").about("Display help message for git-cli"))
58 .subcommand(
59 Command::new("completion")
60 .about("Export shell completion script")
61 .arg(
62 Arg::new("shell")
63 .value_name("shell")
64 .value_parser(["bash", "zsh"])
65 .required(true),
66 ),
67 )
68}
69
70fn build_utils_group() -> Command {
71 Command::new("utils")
72 .about("Utility helpers")
73 .subcommand(Command::new("zip").about("Create zip archive from HEAD"))
74 .subcommand(
75 Command::new("copy-staged")
76 .visible_alias("copy")
77 .about("Copy staged diff to clipboard")
78 .arg(
79 Arg::new("stdout")
80 .long("stdout")
81 .help("Print staged diff to stdout")
82 .action(ArgAction::SetTrue),
83 )
84 .arg(
85 Arg::new("print")
86 .short('p')
87 .long("print")
88 .help("Alias for --stdout")
89 .action(ArgAction::SetTrue),
90 )
91 .arg(
92 Arg::new("both")
93 .long("both")
94 .help("Print diff and copy it to clipboard")
95 .action(ArgAction::SetTrue),
96 ),
97 )
98 .subcommand(
99 Command::new("root").about("Jump to git root").arg(
100 Arg::new("shell")
101 .long("shell")
102 .help("Print shell command instead of plain output")
103 .action(ArgAction::SetTrue),
104 ),
105 )
106 .subcommand(
107 Command::new("commit-hash")
108 .visible_alias("hash")
109 .about("Resolve commit hash")
110 .arg(Arg::new("ref").value_name("ref")),
111 )
112 .subcommand(Command::new("help").about("Display help message for utils"))
113}
114
115fn build_reset_group() -> Command {
116 let count_arg = || Arg::new("count").value_name("count");
117
118 Command::new("reset")
119 .about("Reset helpers")
120 .subcommand(
121 Command::new("soft")
122 .about("Reset to HEAD~N (soft)")
123 .arg(count_arg()),
124 )
125 .subcommand(
126 Command::new("mixed")
127 .about("Reset to HEAD~N (mixed)")
128 .arg(count_arg()),
129 )
130 .subcommand(
131 Command::new("hard")
132 .about("Reset to HEAD~N (hard)")
133 .arg(count_arg()),
134 )
135 .subcommand(Command::new("undo").about("Undo last reset"))
136 .subcommand(Command::new("back-head").about("Checkout HEAD@{1}"))
137 .subcommand(Command::new("back-checkout").about("Return to previous branch"))
138 .subcommand(
139 Command::new("remote")
140 .about("Reset to remote branch")
141 .arg(
142 Arg::new("ref")
143 .long("ref")
144 .help("Remote ref in <remote>/<branch> form")
145 .value_name("ref"),
146 )
147 .arg(
148 Arg::new("remote")
149 .short('r')
150 .long("remote")
151 .help("Remote name")
152 .value_name("remote"),
153 )
154 .arg(
155 Arg::new("branch")
156 .short('b')
157 .long("branch")
158 .help("Remote branch name")
159 .value_name("branch"),
160 )
161 .arg(
162 Arg::new("no-fetch")
163 .long("no-fetch")
164 .help("Skip fetching remote refs")
165 .action(ArgAction::SetTrue),
166 )
167 .arg(
168 Arg::new("prune")
169 .long("prune")
170 .help("Run fetch with --prune")
171 .action(ArgAction::SetTrue),
172 )
173 .arg(
174 Arg::new("clean")
175 .long("clean")
176 .help("Run git clean -fd after reset")
177 .action(ArgAction::SetTrue),
178 )
179 .arg(
180 Arg::new("set-upstream")
181 .long("set-upstream")
182 .help("Set upstream to the target remote branch")
183 .action(ArgAction::SetTrue),
184 )
185 .arg(
186 Arg::new("yes")
187 .short('y')
188 .long("yes")
189 .help("Skip confirmation prompts")
190 .action(ArgAction::SetTrue),
191 ),
192 )
193 .subcommand(Command::new("help").about("Display help message for reset"))
194}
195
196fn build_commit_group() -> Command {
197 Command::new("commit")
198 .about("Commit helpers")
199 .subcommand(
200 Command::new("context")
201 .about("Print commit context")
202 .arg(
203 Arg::new("stdout")
204 .long("stdout")
205 .help("Print report to stdout")
206 .action(ArgAction::SetTrue),
207 )
208 .arg(
209 Arg::new("both")
210 .long("both")
211 .help("Print report and write output file")
212 .action(ArgAction::SetTrue),
213 )
214 .arg(
215 Arg::new("no-color")
216 .long("no-color")
217 .help("Disable ANSI colors")
218 .action(ArgAction::SetTrue),
219 )
220 .arg(
221 Arg::new("include")
222 .long("include")
223 .help("Additional glob(s) to include")
224 .value_name("glob")
225 .num_args(1..),
226 ),
227 )
228 .subcommand(
229 Command::new("context-json")
230 .visible_aliases(["context_json", "contextjson", "json"])
231 .about("Print commit context as JSON")
232 .arg(
233 Arg::new("stdout")
234 .long("stdout")
235 .help("Print JSON to stdout")
236 .action(ArgAction::SetTrue),
237 )
238 .arg(
239 Arg::new("both")
240 .long("both")
241 .help("Print JSON and write files")
242 .action(ArgAction::SetTrue),
243 )
244 .arg(
245 Arg::new("pretty")
246 .long("pretty")
247 .help("Pretty-print JSON output")
248 .action(ArgAction::SetTrue),
249 )
250 .arg(
251 Arg::new("bundle")
252 .long("bundle")
253 .help("Write bundle files to output directory")
254 .action(ArgAction::SetTrue),
255 )
256 .arg(
257 Arg::new("out-dir")
258 .long("out-dir")
259 .help("Output directory for generated files")
260 .value_name("path"),
261 ),
262 )
263 .subcommand(
264 Command::new("to-stash")
265 .visible_alias("stash")
266 .about("Create stash from commit")
267 .arg(Arg::new("ref").value_name("ref")),
268 )
269 .subcommand(Command::new("help").about("Display help message for commit"))
270}
271
272fn build_branch_group() -> Command {
273 Command::new("branch")
274 .about("Branch helpers")
275 .subcommand(
276 Command::new("cleanup")
277 .visible_alias("delete-merged")
278 .about("Delete merged branches")
279 .arg(
280 Arg::new("base")
281 .short('b')
282 .long("base")
283 .help("Base ref used to determine merged branches")
284 .value_name("base"),
285 )
286 .arg(
287 Arg::new("squash")
288 .short('s')
289 .long("squash")
290 .help("Include branches already applied via squash")
291 .action(ArgAction::SetTrue),
292 )
293 .arg(
294 Arg::new("remove-worktrees")
295 .short('w')
296 .long("remove-worktrees")
297 .help("Force-remove linked worktrees for candidate branches")
298 .action(ArgAction::SetTrue),
299 ),
300 )
301 .subcommand(Command::new("help").about("Display help message for branch"))
302}
303
304fn build_ci_group() -> Command {
305 Command::new("ci")
306 .about("CI helpers")
307 .subcommand(
308 Command::new("pick")
309 .about("Cherry-pick into CI branch")
310 .arg(
311 Arg::new("remote")
312 .short('r')
313 .long("remote")
314 .help("Remote used for fetch/push")
315 .value_name("name"),
316 )
317 .arg(
318 Arg::new("no-fetch")
319 .long("no-fetch")
320 .help("Skip remote fetch before branch creation")
321 .action(ArgAction::SetTrue),
322 )
323 .arg(
324 Arg::new("force")
325 .short('f')
326 .long("force")
327 .help("Reset existing CI branch and force push")
328 .action(ArgAction::SetTrue),
329 )
330 .arg(
331 Arg::new("stay")
332 .long("stay")
333 .help("Stay on CI branch after push")
334 .action(ArgAction::SetTrue),
335 ),
336 )
337 .subcommand(Command::new("help").about("Display help message for ci"))
338}
339
340fn build_open_group() -> Command {
341 Command::new("open")
342 .about("Open remote pages")
343 .subcommand(
344 Command::new("repo")
345 .about("Open repository page")
346 .arg(remotes_arg()),
347 )
348 .subcommand(
349 Command::new("branch")
350 .about("Open branch tree page")
351 .arg(Arg::new("ref").value_name("ref")),
352 )
353 .subcommand(
354 Command::new("default-branch")
355 .visible_alias("default")
356 .about("Open default branch tree page")
357 .arg(remotes_arg()),
358 )
359 .subcommand(
360 Command::new("commit")
361 .about("Open commit page")
362 .arg(Arg::new("ref").value_name("ref")),
363 )
364 .subcommand(
365 Command::new("compare")
366 .about("Open compare page")
367 .arg(Arg::new("from").value_name("from"))
368 .arg(Arg::new("to").value_name("to")),
369 )
370 .subcommand(
371 Command::new("pr")
372 .visible_aliases(["pull-request", "mr", "merge-request"])
373 .about("Open pull or merge request page")
374 .arg(Arg::new("id").value_name("id")),
375 )
376 .subcommand(
377 Command::new("pulls")
378 .visible_aliases(["prs", "merge-requests", "mrs"])
379 .about("Open pull or merge request list"),
380 )
381 .subcommand(
382 Command::new("issues")
383 .visible_alias("issue")
384 .about("Open issues list/page")
385 .arg(Arg::new("id").value_name("id")),
386 )
387 .subcommand(
388 Command::new("actions")
389 .visible_alias("action")
390 .about("Open actions page")
391 .arg(Arg::new("workflow").value_name("workflow")),
392 )
393 .subcommand(
394 Command::new("releases")
395 .visible_alias("release")
396 .about("Open releases list/page")
397 .arg(Arg::new("tag").value_name("tag")),
398 )
399 .subcommand(
400 Command::new("tags")
401 .visible_alias("tag")
402 .about("Open tags list/page")
403 .arg(Arg::new("tag").value_name("tag")),
404 )
405 .subcommand(
406 Command::new("commits")
407 .visible_alias("history")
408 .about("Open commit history page")
409 .arg(Arg::new("ref").value_name("ref")),
410 )
411 .subcommand(
412 Command::new("file")
413 .visible_alias("blob")
414 .about("Open file page")
415 .arg(
416 Arg::new("path")
417 .value_name("path")
418 .value_hint(ValueHint::FilePath),
419 )
420 .arg(Arg::new("ref").value_name("ref")),
421 )
422 .subcommand(
423 Command::new("blame")
424 .about("Open blame page")
425 .arg(
426 Arg::new("path")
427 .value_name("path")
428 .value_hint(ValueHint::FilePath),
429 )
430 .arg(Arg::new("ref").value_name("ref")),
431 )
432 .subcommand(Command::new("help").about("Display help message for open"))
433}
434
435fn remotes_arg() -> Arg {
436 Arg::new("remote").value_name("remote")
437}