@@ -49,22 +49,13 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
49
49
index. write ( ) ?;
50
50
51
51
if nothing_left_in_index ( repo) ? {
52
- warn ! (
53
- logger,
54
- "No changes staged, even after auto-staging. \
55
- Try adding something to the index."
56
- ) ;
52
+ announce ( logger, Announcement :: NothingStagedAfterAutoStaging ) ;
57
53
return Ok ( ( ) ) ;
58
54
}
59
55
60
56
we_added_everything_to_index = true ;
61
57
} else {
62
- warn ! (
63
- logger,
64
- "No changes staged. \
65
- Try adding something to the index or set {} = true.",
66
- config:: AUTO_STAGE_IF_NOTHING_STAGED_CONFIG_NAME
67
- ) ;
58
+ announce ( logger, Announcement :: NothingStaged ) ;
68
59
return Ok ( ( ) ) ;
69
60
}
70
61
}
@@ -344,14 +335,11 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
344
335
& head_tree,
345
336
& [ & head_commit] ,
346
337
) ?) ?;
347
- info ! ( logger, "committed" ;
348
- "commit" => head_commit. id( ) . to_string( ) ,
349
- "header" => format!( "+{},-{}" , diff. insertions( ) , diff. deletions( ) ) ,
350
- ) ;
338
+ announce ( logger, Announcement :: Committed ( & head_commit, & diff) ) ;
351
339
} else {
352
- info ! ( logger , "would have committed" ;
353
- "fixup" => dest_commit_locator ,
354
- "header" => format! ( "+{},-{}" , diff. insertions ( ) , diff . deletions ( ) ) ,
340
+ announce (
341
+ logger ,
342
+ Announcement :: WouldHaveCommitted ( dest_commit_locator , & diff) ,
355
343
) ;
356
344
}
357
345
} else {
@@ -370,11 +358,7 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
370
358
}
371
359
372
360
if non_modified_patches == index. len ( ) {
373
- warn ! (
374
- logger,
375
- "No changes were in-place file modifications. \
376
- Added, removed, or renamed files cannot be automatically absorbed."
377
- ) ;
361
+ announce ( logger, Announcement :: NoFileModifications ) ;
378
362
return Ok ( ( ) ) ;
379
363
}
380
364
@@ -384,71 +368,44 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
384
368
// Users that auto-stage changes may be accustomed to having untracked files
385
369
// in their workspace that are not absorbed, so don't warn them.
386
370
if non_modified_patches > 0 && !we_added_everything_to_index {
387
- warn ! (
388
- logger,
389
- "Some changes were not in-place file modifications. \
390
- Added, removed, or renamed files cannot be automatically absorbed."
391
- )
371
+ announce ( logger, Announcement :: NonFileModifications ) ;
392
372
}
393
373
394
374
if modified_hunks_without_target > 0 {
395
- warn ! (
396
- logger,
397
- "Some file modifications did not have an available commit to fix up. \
398
- You will have to manually create fixup commits."
399
- ) ;
375
+ announce ( logger, Announcement :: FileModificationsWithoutTarget ) ;
400
376
401
377
match stack_end_reason {
402
378
stack:: StackEndReason :: ReachedRoot => {
403
- warn ! (
404
- logger,
405
- "Cannot fix up past first commit in the repository." ;
406
- ) ;
379
+ announce ( logger, Announcement :: CannotFixUpPastFirstCommit ) ;
407
380
}
408
381
stack:: StackEndReason :: ReachedMergeCommit => {
409
- warn ! (
410
- logger,
411
- "Cannot fix up past a merge commit" ;
412
- "commit" => match stack. last( ) {
413
- Some ( commit) => commit. 0 . id( ) . to_string( ) ,
414
- None => head_commit. id( ) . to_string( ) ,
415
- }
416
- ) ;
382
+ let commit = match stack. last ( ) {
383
+ Some ( commit) => & commit. 0 ,
384
+ None => & head_commit,
385
+ } ;
386
+ announce ( logger, Announcement :: CannotFixUpPastMerge ( commit) ) ;
417
387
}
418
388
stack:: StackEndReason :: ReachedAnotherAuthor => {
419
- warn ! (
420
- logger,
421
- "Will not fix up past commits by another author. \
422
- Use --force-author to override";
423
- "commit" => match stack. last( ) {
424
- Some ( commit) => commit. 0 . id( ) . to_string( ) ,
425
- None => head_commit. id( ) . to_string( ) ,
426
- }
427
- ) ;
389
+ let commit = match stack. last ( ) {
390
+ Some ( commit) => & commit. 0 ,
391
+ None => & head_commit,
392
+ } ;
393
+ announce ( logger, Announcement :: WillNotFixUpPastAnotherAuthor ( commit) ) ;
428
394
}
429
395
stack:: StackEndReason :: ReachedLimit => {
430
- warn ! (
396
+ announce (
431
397
logger,
432
- "Will not fix up past maximum stack limit. \
433
- Use --base or configure {} to override",
434
- config:: MAX_STACK_CONFIG_NAME ;
435
- "limit" => config:: max_stack( repo) ,
398
+ Announcement :: WillNotFixUpPastStackLimit ( config:: max_stack ( repo) ) ,
436
399
) ;
437
400
}
438
401
stack:: StackEndReason :: CommitsHiddenByBase => {
439
- warn ! (
440
- logger,
441
- "Will not fix up past specified base commit. \
442
- Consider using --base to specify a different base commit";
443
- "base" => config. base. unwrap( ) ,
402
+ announce (
403
+ logger,
404
+ Announcement :: CommitsHiddenByBase ( config. base . unwrap ( ) ) ,
444
405
) ;
445
406
}
446
407
stack:: StackEndReason :: CommitsHiddenByBranches => {
447
- warn ! (
448
- logger,
449
- "Will not fix up commits reachable by other branches. \
450
- Use --base to specify a base commit.";
451
- ) ;
408
+ announce ( logger, Announcement :: CommitsHiddenByBranches ) ;
452
409
}
453
410
}
454
411
}
@@ -491,11 +448,7 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
491
448
command. args ( [ "-C" , path] ) ;
492
449
}
493
450
_ => {
494
- warn ! (
495
- logger,
496
- "Could not determine repository path for rebase. \
497
- Running in current directory."
498
- ) ;
451
+ announce ( logger, Announcement :: CouldNotFindRepositoryPath ) ;
499
452
}
500
453
}
501
454
@@ -506,17 +459,15 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
506
459
}
507
460
508
461
if config. dry_run {
509
- info ! ( logger, "would have run git rebase" ; "command" => format! ( "{:?}" , command) ) ;
462
+ announce ( logger, Announcement :: WouldHaveRebased ( & command) ) ;
510
463
} else {
511
464
debug ! ( logger, "running git rebase" ; "command" => format!( "{:?}" , command) ) ;
512
465
// Don't check that we have successfully absorbed everything, nor git's
513
466
// exit code -- as git will print helpful messages on its own.
514
467
command. status ( ) . expect ( "could not run git rebase" ) ;
515
468
}
516
469
} else if !config. dry_run {
517
- info ! ( logger, "To squash the new commits, rebase:" ;
518
- "command" => format!( "git {}" , rebase_args. join( " " ) ) ,
519
- ) ;
470
+ announce ( logger, Announcement :: HowToSquash ( rebase_args. join ( " " ) ) ) ;
520
471
}
521
472
}
522
473
@@ -617,6 +568,110 @@ fn index_stats(repo: &git2::Repository) -> Result<git2::DiffStats> {
617
568
Ok ( stats)
618
569
}
619
570
571
+ // Messages that will be shown to users during normal operations (not debug messages).
572
+ enum Announcement < ' r > {
573
+ Committed ( & ' r git2:: Commit < ' r > , & ' r git2:: DiffStats ) ,
574
+ WouldHaveCommitted ( & ' r str , & ' r git2:: DiffStats ) ,
575
+ WouldHaveRebased ( & ' r std:: process:: Command ) ,
576
+ HowToSquash ( String ) ,
577
+ NothingStagedAfterAutoStaging ,
578
+ NothingStaged ,
579
+ NoFileModifications ,
580
+ NonFileModifications ,
581
+ FileModificationsWithoutTarget ,
582
+ CannotFixUpPastFirstCommit ,
583
+ CannotFixUpPastMerge ( & ' r git2:: Commit < ' r > ) ,
584
+ WillNotFixUpPastAnotherAuthor ( & ' r git2:: Commit < ' r > ) ,
585
+ WillNotFixUpPastStackLimit ( usize ) ,
586
+ CommitsHiddenByBase ( & ' r str ) ,
587
+ CommitsHiddenByBranches ,
588
+ CouldNotFindRepositoryPath ,
589
+ }
590
+
591
+ fn announce ( logger : & slog:: Logger , announcement : Announcement ) {
592
+ match announcement {
593
+ Announcement :: Committed ( commit, diff) => info ! (
594
+ logger,
595
+ "committed" ;
596
+ "commit" => & commit. id( ) . to_string( ) ,
597
+ "header" => format!( "+{},-{}" , & diff. insertions( ) , & diff. deletions( ) )
598
+ ) ,
599
+ Announcement :: WouldHaveCommitted ( fixup, diff) => info ! (
600
+ logger,
601
+ "would have committed" ;
602
+ "fixup" => fixup,
603
+ "header" => format!( "+{},-{}" , & diff. insertions( ) , & diff. deletions( ) )
604
+ ) ,
605
+ Announcement :: WouldHaveRebased ( command) => info ! (
606
+ logger, "would have run git rebase" ; "command" => format!( "{:?}" , command)
607
+ ) ,
608
+ Announcement :: HowToSquash ( rebase_args) => info ! (
609
+ logger,
610
+ "To squash the new commits, rebase:" ;
611
+ "command" => format!( "git {}" , rebase_args) ,
612
+ ) ,
613
+ Announcement :: NothingStagedAfterAutoStaging => warn ! (
614
+ logger,
615
+ "No changes staged, even after auto-staging. Try adding something to the index." ,
616
+ ) ,
617
+ Announcement :: NothingStaged => warn ! (
618
+ logger,
619
+ "No changes staged. Try adding something to the index or set {} = true." ,
620
+ config:: AUTO_STAGE_IF_NOTHING_STAGED_CONFIG_NAME
621
+ ) ,
622
+ Announcement :: NoFileModifications => warn ! (
623
+ logger,
624
+ "No changes were in-place file modifications. \
625
+ Added, removed, or renamed files cannot be automatically absorbed."
626
+ ) ,
627
+ Announcement :: NonFileModifications => warn ! (
628
+ logger,
629
+ "Some changes were not in-place file modifications. \
630
+ Added, removed, or renamed files cannot be automatically absorbed."
631
+ ) ,
632
+ Announcement :: FileModificationsWithoutTarget => warn ! (
633
+ logger,
634
+ "Some file modifications did not have an available commit to fix up. \
635
+ You will have to manually create fixup commits."
636
+ ) ,
637
+ Announcement :: CannotFixUpPastFirstCommit => warn ! (
638
+ logger,
639
+ "Cannot fix up past the first commit in the repository."
640
+ ) ,
641
+ Announcement :: CannotFixUpPastMerge ( commit) => warn ! (
642
+ logger,
643
+ "Cannot fix up past a merge commit" ;
644
+ "commit" => commit. id( ) . to_string( )
645
+ ) ,
646
+ Announcement :: WillNotFixUpPastAnotherAuthor ( commit) => warn ! (
647
+ logger,
648
+ "Will not fix up past commits by another author. Use --force-author to override" ;
649
+ "commit" => commit. id( ) . to_string( )
650
+ ) ,
651
+ Announcement :: WillNotFixUpPastStackLimit ( max_stack_limit) => warn ! (
652
+ logger,
653
+ "Will not fix up past maximum stack limit. Use --base or configure {} to override" ,
654
+ config:: MAX_STACK_CONFIG_NAME ;
655
+ "limit" => max_stack_limit,
656
+ ) ,
657
+ Announcement :: CommitsHiddenByBase ( base) => warn ! (
658
+ logger,
659
+ "Will not fix up past specified base commit. \
660
+ Consider using --base to specify a different base commit";
661
+ "base" => base,
662
+ ) ,
663
+ Announcement :: CommitsHiddenByBranches => warn ! (
664
+ logger,
665
+ "Will not fix up commits reachable by other branches. \
666
+ Use --base to specify a base commit."
667
+ ) ,
668
+ Announcement :: CouldNotFindRepositoryPath => warn ! (
669
+ logger,
670
+ "Could not determine repository path for rebase. Running in current directory."
671
+ ) ,
672
+ }
673
+ }
674
+
620
675
#[ cfg( test) ]
621
676
mod tests {
622
677
use git2:: message_trailers_strs;
0 commit comments