@@ -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,109 @@ fn index_stats(repo: &git2::Repository) -> Result<git2::DiffStats> {
617
568
Ok ( stats)
618
569
}
619
570
571
+ enum Announcement < ' r > {
572
+ Committed ( & ' r git2:: Commit < ' r > , & ' r git2:: DiffStats ) ,
573
+ WouldHaveCommitted ( & ' r str , & ' r git2:: DiffStats ) ,
574
+ WouldHaveRebased ( & ' r std:: process:: Command ) ,
575
+ HowToSquash ( String ) ,
576
+ NothingStagedAfterAutoStaging ,
577
+ NothingStaged ,
578
+ NoFileModifications ,
579
+ NonFileModifications ,
580
+ FileModificationsWithoutTarget ,
581
+ CannotFixUpPastFirstCommit ,
582
+ CannotFixUpPastMerge ( & ' r git2:: Commit < ' r > ) ,
583
+ WillNotFixUpPastAnotherAuthor ( & ' r git2:: Commit < ' r > ) ,
584
+ WillNotFixUpPastStackLimit ( usize ) ,
585
+ CommitsHiddenByBase ( & ' r str ) ,
586
+ CommitsHiddenByBranches ,
587
+ CouldNotFindRepositoryPath ,
588
+ }
589
+
590
+ fn announce ( logger : & slog:: Logger , announcement : Announcement ) {
591
+ match announcement {
592
+ Announcement :: Committed ( commit, diff) => info ! (
593
+ logger,
594
+ "committed" ;
595
+ "commit" => & commit. id( ) . to_string( ) ,
596
+ "header" => format!( "+{},-{}" , & diff. insertions( ) , & diff. deletions( ) )
597
+ ) ,
598
+ Announcement :: WouldHaveCommitted ( fixup, diff) => info ! (
599
+ logger,
600
+ "would have committed" ;
601
+ "fixup" => fixup,
602
+ "header" => format!( "+{},-{}" , & diff. insertions( ) , & diff. deletions( ) )
603
+ ) ,
604
+ Announcement :: WouldHaveRebased ( command) => info ! (
605
+ logger, "would have run git rebase" ; "command" => format!( "{:?}" , command)
606
+ ) ,
607
+ Announcement :: HowToSquash ( rebase_args) => info ! (
608
+ logger,
609
+ "To squash the new commits, rebase:" ;
610
+ "command" => format!( "git {}" , rebase_args) ,
611
+ ) ,
612
+ Announcement :: NothingStagedAfterAutoStaging => warn ! (
613
+ logger,
614
+ "No changes staged, even after auto-staging. Try adding something to the index." ,
615
+ ) ,
616
+ Announcement :: NothingStaged => warn ! (
617
+ logger,
618
+ "No changes staged. Try adding something to the index or set {} = true." ,
619
+ config:: AUTO_STAGE_IF_NOTHING_STAGED_CONFIG_NAME
620
+ ) ,
621
+ Announcement :: NoFileModifications => warn ! (
622
+ logger,
623
+ "No changes were in-place file modifications. \
624
+ Added, removed, or renamed files cannot be automatically absorbed."
625
+ ) ,
626
+ Announcement :: NonFileModifications => warn ! (
627
+ logger,
628
+ "Some changes were not in-place file modifications. \
629
+ Added, removed, or renamed files cannot be automatically absorbed."
630
+ ) ,
631
+ Announcement :: FileModificationsWithoutTarget => warn ! (
632
+ logger,
633
+ "Some file modifications did not have an available commit to fix up. \
634
+ You will have to manually create fixup commits."
635
+ ) ,
636
+ Announcement :: CannotFixUpPastFirstCommit => warn ! (
637
+ logger,
638
+ "Cannot fix up past the first commit in the repository."
639
+ ) ,
640
+ Announcement :: CannotFixUpPastMerge ( commit) => warn ! (
641
+ logger,
642
+ "Cannot fix up past a merge commit" ;
643
+ "commit" => commit. id( ) . to_string( )
644
+ ) ,
645
+ Announcement :: WillNotFixUpPastAnotherAuthor ( commit) => warn ! (
646
+ logger,
647
+ "Will not fix up past commits by another author. Use --force-author to override" ;
648
+ "commit" => commit. id( ) . to_string( )
649
+ ) ,
650
+ Announcement :: WillNotFixUpPastStackLimit ( max_stack_limit) => warn ! (
651
+ logger,
652
+ "Will not fix up past maximum stack limit. Use --base or configure {} to override" ,
653
+ config:: MAX_STACK_CONFIG_NAME ;
654
+ "limit" => max_stack_limit,
655
+ ) ,
656
+ Announcement :: CommitsHiddenByBase ( base) => warn ! (
657
+ logger,
658
+ "Will not fix up past specified base commit. \
659
+ Consider using --base to specify a different base commit";
660
+ "base" => base,
661
+ ) ,
662
+ Announcement :: CommitsHiddenByBranches => warn ! (
663
+ logger,
664
+ "Will not fix up commits reachable by other branches. \
665
+ Use --base to specify a base commit."
666
+ ) ,
667
+ Announcement :: CouldNotFindRepositoryPath => warn ! (
668
+ logger,
669
+ "Could not determine repository path for rebase. Running in current directory."
670
+ ) ,
671
+ }
672
+ }
673
+
620
674
#[ cfg( test) ]
621
675
mod tests {
622
676
use git2:: message_trailers_strs;
0 commit comments