1use crate::prompt;
2use nils_common::git as common_git;
3use std::io::{self, BufRead, Write};
4use std::process::Output;
5
6pub fn dispatch(cmd: &str, args: &[String]) -> Option<i32> {
7 match cmd {
8 "soft" => Some(reset_by_count("soft", args)),
9 "mixed" => Some(reset_by_count("mixed", args)),
10 "hard" => Some(reset_by_count("hard", args)),
11 "undo" => Some(reset_undo()),
12 "back-head" => Some(back_head()),
13 "back-checkout" => Some(back_checkout()),
14 "remote" => Some(reset_remote(args)),
15 _ => None,
16 }
17}
18
19fn reset_by_count(mode: &str, args: &[String]) -> i32 {
20 let count_arg = args.first();
21 let extra_arg = args.get(1);
22 if extra_arg.is_some() {
23 eprintln!("❌ Too many arguments.");
24 eprintln!("Usage: git-reset-{mode} [N]");
25 return 2;
26 }
27
28 let count = match count_arg {
29 Some(value) => match parse_positive_int(value) {
30 Some(value) => value,
31 None => {
32 eprintln!("❌ Invalid commit count: {value} (must be a positive integer).");
33 eprintln!("Usage: git-reset-{mode} [N]");
34 return 2;
35 }
36 },
37 None => 1,
38 };
39
40 let target = format!("HEAD~{count}");
41 if !git_success(&["rev-parse", "--verify", "--quiet", &target]) {
42 eprintln!("❌ Cannot resolve {target} (not enough commits?).");
43 return 1;
44 }
45
46 let commit_label = if count > 1 {
47 format!("last {count} commits")
48 } else {
49 "last commit".to_string()
50 };
51
52 let (preface, prompt, failure, success) = match mode {
53 "soft" => (
54 vec![
55 format!("⚠️ This will rewind your {commit_label} (soft reset)"),
56 "🧠 Your changes will remain STAGED. Useful for rewriting commit message."
57 .to_string(),
58 ],
59 format!("❓ Proceed with 'git reset --soft {target}'? [y/N] "),
60 "❌ Soft reset failed.".to_string(),
61 "✅ Reset completed. Your changes are still staged.".to_string(),
62 ),
63 "mixed" => (
64 vec![
65 format!("⚠️ This will rewind your {commit_label} (mixed reset)"),
66 "🧠 Your changes will become UNSTAGED and editable in working directory."
67 .to_string(),
68 ],
69 format!("❓ Proceed with 'git reset --mixed {target}'? [y/N] "),
70 "❌ Mixed reset failed.".to_string(),
71 "✅ Reset completed. Your changes are now unstaged.".to_string(),
72 ),
73 "hard" => (
74 vec![
75 format!("⚠️ This will HARD RESET your repository to {target}."),
76 "🔥 Tracked staged/unstaged changes will be OVERWRITTEN.".to_string(),
77 format!("🧨 This is equivalent to: git reset --hard {target}"),
78 ],
79 "❓ Are you absolutely sure? [y/N] ".to_string(),
80 "❌ Hard reset failed.".to_string(),
81 format!("✅ Hard reset completed. HEAD moved back to {target}."),
82 ),
83 _ => {
84 eprintln!("❌ Unknown reset mode: {mode}");
85 return 2;
86 }
87 };
88
89 for line in preface {
90 println!("{line}");
91 }
92 println!("🧾 Commits to be rewound:");
93
94 let output = match git_output(&[
95 "log",
96 "--no-color",
97 "-n",
98 &count.to_string(),
99 "--date=format:%m-%d %H:%M",
100 "--pretty=%h %ad %an %s",
101 ]) {
102 Some(output) => output,
103 None => return 1,
104 };
105 if !output.status.success() {
106 emit_output(&output);
107 return exit_code(&output);
108 }
109 emit_output(&output);
110
111 if !confirm_or_abort(&prompt) {
112 return 1;
113 }
114
115 let code = git_status(&["reset", &format!("--{mode}"), &target]).unwrap_or(1);
116 if code != 0 {
117 println!("{failure}");
118 return 1;
119 }
120
121 println!("{success}");
122 0
123}
124
125fn reset_undo() -> i32 {
126 if !git_success(&["rev-parse", "--is-inside-work-tree"]) {
127 println!("❌ Not a git repository.");
128 return 1;
129 }
130
131 let target_commit = match git_stdout_trimmed(&["rev-parse", "HEAD@{1}"]).and_then(non_empty) {
132 Some(value) => value,
133 None => {
134 println!("❌ Cannot resolve HEAD@{{1}} (no previous HEAD position in reflog).");
135 return 1;
136 }
137 };
138
139 let op_warnings = detect_in_progress_ops();
140 if !op_warnings.is_empty() {
141 println!("🛡️ Detected an in-progress Git operation:");
142 for warning in op_warnings {
143 println!(" - {warning}");
144 }
145 println!("⚠️ Resetting during these operations can be confusing.");
146 if !confirm_or_abort("❓ Still run git-reset-undo (move HEAD back)? [y/N] ") {
147 return 1;
148 }
149 }
150
151 let mut reflog_line_current =
152 git_stdout_trimmed(&["reflog", "-1", "--pretty=%h %gs", "HEAD@{0}"]).and_then(non_empty);
153 let mut reflog_subject_current =
154 git_stdout_trimmed(&["reflog", "-1", "--pretty=%gs", "HEAD@{0}"]).and_then(non_empty);
155
156 if reflog_line_current.is_none() || reflog_subject_current.is_none() {
157 reflog_line_current =
158 git_stdout_trimmed(&["reflog", "show", "-1", "--pretty=%h %gs", "HEAD"])
159 .and_then(non_empty);
160 reflog_subject_current =
161 git_stdout_trimmed(&["reflog", "show", "-1", "--pretty=%gs", "HEAD"])
162 .and_then(non_empty);
163 }
164
165 let mut reflog_line_target =
166 git_stdout_trimmed(&["reflog", "-1", "--pretty=%h %gs", "HEAD@{1}"]).and_then(non_empty);
167 if reflog_line_target.is_none() {
168 reflog_line_target = reflog_show_line(2, "%h %gs").and_then(non_empty);
169 }
170
171 let line_current = reflog_line_current.unwrap_or_else(|| "(unavailable)".to_string());
172 let line_target = reflog_line_target.unwrap_or_else(|| "(unavailable)".to_string());
173 let subject_current = reflog_subject_current.unwrap_or_else(|| "(unavailable)".to_string());
174
175 println!("🧾 Current HEAD@{{0}} (last action):");
176 println!(" {line_current}");
177 println!("🧾 Target HEAD@{{1}} (previous HEAD position):");
178 println!(" {line_target}");
179
180 if line_current == "(unavailable)" || line_target == "(unavailable)" {
181 println!(
182 "ℹ️ Reflog display unavailable here; reset target is still the resolved SHA: {target_commit}"
183 );
184 }
185
186 if subject_current != "(unavailable)" && !subject_current.starts_with("reset:") {
187 println!("⚠️ The last action does NOT look like a reset operation.");
188 println!("🧠 It may be from checkout/rebase/merge/pull, etc.");
189 if !confirm_or_abort(
190 "❓ Still proceed to move HEAD back to the previous HEAD position? [y/N] ",
191 ) {
192 return 1;
193 }
194 }
195
196 println!("🕰 Target commit (resolved from HEAD@{{1}}):");
197 let log_output = match git_output(&["log", "--oneline", "-1", &target_commit]) {
198 Some(output) => output,
199 None => return 1,
200 };
201 if !log_output.status.success() {
202 emit_output(&log_output);
203 return exit_code(&log_output);
204 }
205 emit_output(&log_output);
206
207 let status_lines = match git_stdout_raw(&["status", "--porcelain"]) {
208 Some(value) => value,
209 None => return 1,
210 };
211
212 if status_lines.trim().is_empty() {
213 println!("✅ Working tree clean. Proceeding with: git reset --hard {target_commit}");
214 let code = git_status(&["reset", "--hard", &target_commit]).unwrap_or(1);
215 if code != 0 {
216 println!("❌ Hard reset failed.");
217 return 1;
218 }
219 println!("✅ Repository reset back to previous HEAD: {target_commit}");
220 return 0;
221 }
222
223 println!("⚠️ Working tree has changes:");
224 print!("{status_lines}");
225 if !status_lines.ends_with('\n') {
226 println!();
227 }
228 println!();
229 println!("Choose how to proceed:");
230 println!(
231 " 1) Keep changes + PRESERVE INDEX (staged vs new base) (git reset --soft {target_commit})"
232 );
233 println!(
234 " 2) Keep changes + UNSTAGE ALL (git reset --mixed {target_commit})"
235 );
236 println!(
237 " 3) Discard tracked changes (git reset --hard {target_commit})"
238 );
239 println!(" 4) Abort");
240
241 let choice = match read_line("❓ Select [1/2/3/4] (default: 4): ") {
242 Ok(value) => value,
243 Err(_) => {
244 println!("🚫 Aborted");
245 return 1;
246 }
247 };
248
249 match choice.as_str() {
250 "1" => {
251 println!(
252 "🧷 Preserving INDEX (staged) and working tree. Running: git reset --soft {target_commit}"
253 );
254 println!(
255 "⚠️ Note: The index is preserved, but what appears staged is relative to the new HEAD."
256 );
257 let code = git_status(&["reset", "--soft", &target_commit]).unwrap_or(1);
258 if code != 0 {
259 println!("❌ Soft reset failed.");
260 return 1;
261 }
262 println!("✅ HEAD moved back while preserving index + working tree: {target_commit}");
263 0
264 }
265 "2" => {
266 println!(
267 "🧷 Preserving working tree but clearing INDEX (unstage all). Running: git reset --mixed {target_commit}"
268 );
269 let code = git_status(&["reset", "--mixed", &target_commit]).unwrap_or(1);
270 if code != 0 {
271 println!("❌ Mixed reset failed.");
272 return 1;
273 }
274 println!("✅ HEAD moved back; working tree preserved; index reset: {target_commit}");
275 0
276 }
277 "3" => {
278 println!("🔥 Discarding tracked changes. Running: git reset --hard {target_commit}");
279 println!("⚠️ This overwrites tracked files in working tree + index.");
280 println!("ℹ️ Untracked files are NOT removed by reset --hard.");
281 if !confirm_or_abort("❓ Are you absolutely sure? [y/N] ") {
282 return 1;
283 }
284 let code = git_status(&["reset", "--hard", &target_commit]).unwrap_or(1);
285 if code != 0 {
286 println!("❌ Hard reset failed.");
287 return 1;
288 }
289 println!("✅ Repository reset back to previous HEAD: {target_commit}");
290 0
291 }
292 _ => {
293 println!("🚫 Aborted");
294 1
295 }
296 }
297}
298
299fn back_head() -> i32 {
300 let prev_head = match git_stdout_trimmed(&["rev-parse", "HEAD@{1}"]).and_then(non_empty) {
301 Some(value) => value,
302 None => {
303 println!("❌ Cannot find previous HEAD in reflog.");
304 return 1;
305 }
306 };
307
308 println!("⏪ This will move HEAD back to the previous position (HEAD@{{1}}):");
309 if let Some(oneline) = git_stdout_trimmed(&["log", "--oneline", "-1", &prev_head]) {
310 println!("🔁 {oneline}");
311 }
312 if !confirm_or_abort("❓ Proceed with 'git checkout HEAD@{1}'? [y/N] ") {
313 return 1;
314 }
315
316 let code = git_status(&["checkout", "HEAD@{1}"]).unwrap_or(1);
317 if code != 0 {
318 println!("❌ Checkout failed (likely due to local changes or invalid reflog state).");
319 return 1;
320 }
321
322 println!("✅ Restored to previous HEAD (HEAD@{{1}}): {prev_head}");
323 0
324}
325
326fn back_checkout() -> i32 {
327 let current_branch =
328 match git_stdout_trimmed(&["rev-parse", "--abbrev-ref", "HEAD"]).and_then(non_empty) {
329 Some(value) => value,
330 None => {
331 println!("❌ Cannot determine current branch.");
332 return 1;
333 }
334 };
335
336 if current_branch == "HEAD" {
337 println!(
338 "❌ You are in a detached HEAD state. This function targets branch-to-branch checkouts."
339 );
340 println!(
341 "🧠 Tip: Use `git reflog` to find the branch/commit you want, then `git checkout <branch>`."
342 );
343 return 1;
344 }
345
346 let from_branch = match find_previous_checkout(¤t_branch) {
347 Some(value) => value,
348 None => {
349 println!("❌ Could not find a previous checkout that switched to {current_branch}.");
350 return 1;
351 }
352 };
353
354 if from_branch.len() >= 7
355 && from_branch.len() <= 40
356 && from_branch.chars().all(|c| c.is_ascii_hexdigit())
357 {
358 println!(
359 "❌ Previous 'from' looks like a commit SHA ({from_branch}). Refusing to checkout to avoid detached HEAD."
360 );
361 println!("🧠 Use `git reflog` to choose the correct branch explicitly.");
362 return 1;
363 }
364
365 if !git_success(&[
366 "show-ref",
367 "--verify",
368 "--quiet",
369 &format!("refs/heads/{from_branch}"),
370 ]) {
371 println!("❌ '{from_branch}' is not an existing local branch.");
372 println!("🧠 If it's a remote branch, try: git checkout -t origin/{from_branch}");
373 return 1;
374 }
375
376 println!("⏪ This will move HEAD back to previous branch: {from_branch}");
377 if !confirm_or_abort(&format!(
378 "❓ Proceed with 'git checkout {from_branch}'? [y/N] "
379 )) {
380 return 1;
381 }
382
383 let code = git_status(&["checkout", &from_branch]).unwrap_or(1);
384 if code != 0 {
385 println!("❌ Checkout failed (likely due to local changes or conflicts).");
386 return 1;
387 }
388
389 println!("✅ Restored to previous branch: {from_branch}");
390 0
391}
392
393fn reset_remote(args: &[String]) -> i32 {
394 let mut want_help = false;
395 let mut want_yes = false;
396 let mut want_fetch = true;
397 let mut want_prune = false;
398 let mut want_clean = false;
399 let mut want_set_upstream = false;
400 let mut remote_arg: Option<String> = None;
401 let mut branch_arg: Option<String> = None;
402 let mut ref_arg: Option<String> = None;
403
404 let mut i = 0usize;
405 while i < args.len() {
406 let arg = args[i].as_str();
407 match arg {
408 "-h" | "--help" => {
409 want_help = true;
410 }
411 "-y" | "--yes" => {
412 want_yes = true;
413 }
414 "-r" | "--remote" => {
415 let Some(value) = args.get(i + 1) else {
416 return 2;
417 };
418 remote_arg = Some(value.to_string());
419 i += 1;
420 }
421 "-b" | "--branch" => {
422 let Some(value) = args.get(i + 1) else {
423 return 2;
424 };
425 branch_arg = Some(value.to_string());
426 i += 1;
427 }
428 "--ref" => {
429 let Some(value) = args.get(i + 1) else {
430 return 2;
431 };
432 ref_arg = Some(value.to_string());
433 i += 1;
434 }
435 "--no-fetch" => {
436 want_fetch = false;
437 }
438 "--prune" => {
439 want_prune = true;
440 }
441 "--clean" => {
442 want_clean = true;
443 }
444 "--set-upstream" => {
445 want_set_upstream = true;
446 }
447 _ => {}
448 }
449 i += 1;
450 }
451
452 if want_help {
453 print_reset_remote_help();
454 return 0;
455 }
456
457 let mut remote = remote_arg.clone().unwrap_or_default();
458 let mut remote_branch = branch_arg.clone().unwrap_or_default();
459
460 if let Some(reference) = ref_arg {
461 let Some((remote_ref, branch_ref)) = reference.split_once('/') else {
462 eprintln!("❌ --ref must look like '<remote>/<branch>' (got: {reference})");
463 return 2;
464 };
465 remote = remote_ref.to_string();
466 remote_branch = branch_ref.to_string();
467 }
468
469 if !git_success(&["rev-parse", "--git-dir"]) {
470 eprintln!("❌ Not inside a Git repository.");
471 return 1;
472 }
473
474 let current_branch = match git_stdout_trimmed(&["symbolic-ref", "--quiet", "--short", "HEAD"])
475 .and_then(non_empty)
476 {
477 Some(value) => value,
478 None => {
479 eprintln!("❌ Detached HEAD. Switch to a branch first.");
480 return 1;
481 }
482 };
483
484 let upstream =
485 git_stdout_trimmed(&["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"])
486 .unwrap_or_default();
487
488 if remote.is_empty()
489 && let Some((remote_ref, _)) = upstream.split_once('/')
490 {
491 remote = remote_ref.to_string();
492 }
493 if remote.is_empty() {
494 remote = "origin".to_string();
495 }
496
497 if remote_branch.is_empty() {
498 if let Some((_, branch_ref)) = upstream.split_once('/')
499 && branch_ref != "HEAD"
500 {
501 remote_branch = branch_ref.to_string();
502 }
503 if remote_branch.is_empty() {
504 remote_branch = current_branch.clone();
505 }
506 }
507
508 let target_ref = format!("{remote}/{remote_branch}");
509
510 if want_fetch {
511 let fetch_args = if want_prune {
512 vec!["fetch", "--prune", "--", &remote]
513 } else {
514 vec!["fetch", "--", &remote]
515 };
516 let code = git_status(&fetch_args).unwrap_or(1);
517 if code != 0 {
518 return code;
519 }
520 }
521
522 if !git_success(&[
523 "show-ref",
524 "--verify",
525 "--quiet",
526 &format!("refs/remotes/{remote}/{remote_branch}"),
527 ]) {
528 eprintln!("❌ Remote-tracking branch not found: {target_ref}");
529 eprintln!(" Try: git fetch --prune -- {remote}");
530 eprintln!(" Or verify: git branch -r | rg -n -- \"^\\\\s*{remote}/{remote_branch}$\"");
531 return 1;
532 }
533
534 let status_porcelain = git_stdout_raw(&["status", "--porcelain"]).unwrap_or_default();
535 if !want_yes {
536 println!("⚠️ This will OVERWRITE local branch '{current_branch}' with '{target_ref}'.");
537 if !status_porcelain.trim().is_empty() {
538 println!("🔥 Tracked staged/unstaged changes will be DISCARDED by --hard.");
539 println!("🧹 Untracked files will be kept (use --clean to remove).");
540 }
541 if !confirm_or_abort(&format!(
542 "❓ Proceed with: git reset --hard {target_ref} ? [y/N] "
543 )) {
544 return 1;
545 }
546 }
547
548 let code = git_status(&["reset", "--hard", &target_ref]).unwrap_or(1);
549 if code != 0 {
550 return code;
551 }
552
553 if want_clean {
554 if !want_yes {
555 println!("⚠️ Next: git clean -fd (removes untracked files/dirs)");
556 let ok = prompt::confirm("❓ Proceed with: git clean -fd ? [y/N] ").unwrap_or_default();
557 if !ok {
558 println!("ℹ️ Skipped git clean -fd");
559 want_clean = false;
560 }
561 }
562 if want_clean {
563 let code = git_status(&["clean", "-fd"]).unwrap_or(1);
564 if code != 0 {
565 return code;
566 }
567 }
568 }
569
570 if want_set_upstream || upstream.is_empty() {
571 let _ = git_status(&["branch", "--set-upstream-to", &target_ref, ¤t_branch]);
572 }
573
574 println!("✅ Done. '{current_branch}' now matches '{target_ref}'.");
575 0
576}
577
578fn parse_positive_int(raw: &str) -> Option<i64> {
579 if raw.is_empty() || !raw.chars().all(|c| c.is_ascii_digit()) {
580 return None;
581 }
582 let value = raw.parse::<i64>().ok()?;
583 if value <= 0 { None } else { Some(value) }
584}
585
586fn detect_in_progress_ops() -> Vec<String> {
587 let mut warnings = Vec::new();
588 if git_path_exists("MERGE_HEAD", true) {
589 warnings.push("merge in progress (suggest: git merge --abort)".to_string());
590 }
591 if git_path_exists("rebase-apply", false) || git_path_exists("rebase-merge", false) {
592 warnings.push("rebase in progress (suggest: git rebase --abort)".to_string());
593 }
594 if git_path_exists("CHERRY_PICK_HEAD", true) {
595 warnings.push("cherry-pick in progress (suggest: git cherry-pick --abort)".to_string());
596 }
597 if git_path_exists("REVERT_HEAD", true) {
598 warnings.push("revert in progress (suggest: git revert --abort)".to_string());
599 }
600 if git_path_exists("BISECT_LOG", true) {
601 warnings.push("bisect in progress (suggest: git bisect reset)".to_string());
602 }
603 warnings
604}
605
606fn git_path_exists(name: &str, is_file: bool) -> bool {
607 let output = git_stdout_trimmed(&["rev-parse", "--git-path", name]);
608 let Some(path) = output else {
609 return false;
610 };
611 let path = std::path::Path::new(&path);
612 if is_file {
613 path.is_file()
614 } else {
615 path.is_dir()
616 }
617}
618
619fn reflog_show_line(index: usize, pretty: &str) -> Option<String> {
620 let output = git_stdout_raw(&[
621 "reflog",
622 "show",
623 "-2",
624 &format!("--pretty={pretty}"),
625 "HEAD",
626 ])?;
627 output.lines().nth(index - 1).map(|line| line.to_string())
628}
629
630fn find_previous_checkout(current_branch: &str) -> Option<String> {
631 let output = git_stdout_raw(&["reflog", "--format=%gs"])?;
632 for line in output.lines() {
633 if !line.starts_with("checkout: moving from ") {
634 continue;
635 }
636 if !line.ends_with(&format!(" to {current_branch}")) {
637 continue;
638 }
639 let mut value = line.trim_start_matches("checkout: moving from ");
640 value = value.trim_end_matches(&format!(" to {current_branch}"));
641 return Some(value.to_string());
642 }
643 None
644}
645
646fn confirm_or_abort(prompt: &str) -> bool {
647 prompt::confirm_or_abort(prompt).is_ok()
648}
649
650fn read_line(prompt: &str) -> io::Result<String> {
651 let mut output = io::stdout();
652 output.write_all(prompt.as_bytes())?;
653 output.flush()?;
654 let mut input = String::new();
655 io::stdin().lock().read_line(&mut input)?;
656 Ok(input.trim_end_matches(['\n', '\r']).to_string())
657}
658
659fn git_output(args: &[&str]) -> Option<Output> {
660 common_git::run_output(args).ok()
661}
662
663fn git_status(args: &[&str]) -> Option<i32> {
664 common_git::run_status_inherit(args)
665 .ok()
666 .map(|status| status.code().unwrap_or(1))
667}
668
669fn git_success(args: &[&str]) -> bool {
670 matches!(git_output(args), Some(output) if output.status.success())
671}
672
673fn git_stdout_trimmed(args: &[&str]) -> Option<String> {
674 let output = git_output(args)?;
675 if !output.status.success() {
676 return None;
677 }
678 Some(trim_trailing_newlines(&String::from_utf8_lossy(
679 &output.stdout,
680 )))
681}
682
683fn git_stdout_raw(args: &[&str]) -> Option<String> {
684 let output = git_output(args)?;
685 if !output.status.success() {
686 return None;
687 }
688 Some(String::from_utf8_lossy(&output.stdout).to_string())
689}
690
691fn trim_trailing_newlines(input: &str) -> String {
692 input.trim_end_matches(['\n', '\r']).to_string()
693}
694
695fn non_empty(value: String) -> Option<String> {
696 if value.is_empty() { None } else { Some(value) }
697}
698
699fn emit_output(output: &Output) {
700 let _ = io::stdout().write_all(&output.stdout);
701 let _ = io::stderr().write_all(&output.stderr);
702}
703
704fn exit_code(output: &Output) -> i32 {
705 output.status.code().unwrap_or(1)
706}
707
708fn print_reset_remote_help() {
709 println!(
710 "git-reset-remote: overwrite current local branch with a remote-tracking branch (DANGEROUS)"
711 );
712 println!();
713 println!("Usage:");
714 println!(" git-reset-remote # reset current branch to its upstream (or origin/<branch>)");
715 println!(" git-reset-remote --ref origin/main");
716 println!(" git-reset-remote -r origin -b main");
717 println!();
718 println!("Options:");
719 println!(" -r, --remote <name> Remote name (default: from upstream, else origin)");
720 println!(
721 " -b, --branch <name> Remote branch name (default: from upstream, else current branch)"
722 );
723 println!(" --ref <remote/branch> Shortcut for --remote/--branch");
724 println!(" --no-fetch Skip 'git fetch' (uses existing remote-tracking refs)");
725 println!(" --prune Use 'git fetch --prune'");
726 println!(" --set-upstream Set upstream of current branch to <remote>/<branch>");
727 println!(
728 " --clean After reset, optionally run 'git clean -fd' (removes untracked)"
729 );
730 println!(" -y, --yes Skip confirmations");
731}
732
733#[cfg(test)]
734mod tests {
735 use super::{dispatch, non_empty, parse_positive_int, trim_trailing_newlines};
736 use nils_test_support::{CwdGuard, GlobalStateLock};
737 use pretty_assertions::assert_eq;
738
739 #[test]
740 fn dispatch_returns_none_for_unknown_subcommand() {
741 assert_eq!(dispatch("unknown", &[]), None);
742 }
743
744 #[test]
745 fn parse_positive_int_accepts_digits_only() {
746 assert_eq!(parse_positive_int("1"), Some(1));
747 assert_eq!(parse_positive_int("42"), Some(42));
748 assert_eq!(parse_positive_int("001"), Some(1));
749 }
750
751 #[test]
752 fn parse_positive_int_rejects_invalid_values() {
753 assert_eq!(parse_positive_int(""), None);
754 assert_eq!(parse_positive_int("0"), None);
755 assert_eq!(parse_positive_int("-1"), None);
756 assert_eq!(parse_positive_int("1.0"), None);
757 assert_eq!(parse_positive_int("abc"), None);
758 }
759
760 #[test]
761 fn trim_trailing_newlines_only_removes_line_endings() {
762 assert_eq!(trim_trailing_newlines("line\n"), "line");
763 assert_eq!(trim_trailing_newlines("line\r\n"), "line");
764 assert_eq!(trim_trailing_newlines("line "), "line ");
765 }
766
767 #[test]
768 fn non_empty_returns_none_for_empty_string() {
769 assert_eq!(non_empty(String::new()), None);
770 assert_eq!(non_empty("value".to_string()), Some("value".to_string()));
771 }
772
773 #[test]
774 fn reset_by_count_modes_return_usage_errors_for_invalid_arguments() {
775 let lock = GlobalStateLock::new();
776 let dir = tempfile::TempDir::new().expect("tempdir");
777 let _cwd = CwdGuard::set(&lock, dir.path()).expect("cwd");
778
779 let args = vec!["1".to_string(), "2".to_string()];
780 assert_eq!(dispatch("soft", &args), Some(2));
781 assert_eq!(dispatch("mixed", &args), Some(2));
782 assert_eq!(dispatch("hard", &args), Some(2));
783
784 let args = vec!["abc".to_string()];
785 assert_eq!(dispatch("soft", &args), Some(2));
786 }
787
788 #[test]
789 fn reset_by_count_returns_runtime_error_when_target_commit_missing() {
790 let lock = GlobalStateLock::new();
791 let dir = tempfile::TempDir::new().expect("tempdir");
792 let _cwd = CwdGuard::set(&lock, dir.path()).expect("cwd");
793 let args = vec!["999999".to_string()];
794 assert_eq!(dispatch("soft", &args), Some(1));
795 }
796
797 #[test]
798 fn reset_remote_argument_parsing_covers_help_and_usage_failures() {
799 let help_args = vec!["--help".to_string()];
800 assert_eq!(dispatch("remote", &help_args), Some(0));
801
802 let bad_ref_args = vec!["--ref".to_string(), "invalid".to_string()];
803 assert_eq!(dispatch("remote", &bad_ref_args), Some(2));
804
805 let missing_remote_value = vec!["--remote".to_string()];
806 assert_eq!(dispatch("remote", &missing_remote_value), Some(2));
807 }
808}