Skip to content

Commit d7cadda

Browse files
committed
Extract enums that encode announcements to users
1 parent e409a1c commit d7cadda

File tree

1 file changed

+133
-78
lines changed

1 file changed

+133
-78
lines changed

src/lib.rs

Lines changed: 133 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,13 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
4949
index.write()?;
5050

5151
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);
5753
return Ok(());
5854
}
5955

6056
we_added_everything_to_index = true;
6157
} 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);
6859
return Ok(());
6960
}
7061
}
@@ -344,14 +335,11 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
344335
&head_tree,
345336
&[&head_commit],
346337
)?)?;
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));
351339
} 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),
355343
);
356344
}
357345
} else {
@@ -370,11 +358,7 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
370358
}
371359

372360
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);
378362
return Ok(());
379363
}
380364

@@ -384,71 +368,44 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
384368
// Users that auto-stage changes may be accustomed to having untracked files
385369
// in their workspace that are not absorbed, so don't warn them.
386370
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);
392372
}
393373

394374
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);
400376

401377
match stack_end_reason {
402378
stack::StackEndReason::ReachedRoot => {
403-
warn!(
404-
logger,
405-
"Cannot fix up past first commit in the repository.";
406-
);
379+
announce(logger, Announcement::CannotFixUpPastFirstCommit);
407380
}
408381
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));
417387
}
418388
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));
428394
}
429395
stack::StackEndReason::ReachedLimit => {
430-
warn!(
396+
announce(
431397
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)),
436399
);
437400
}
438401
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()),
444405
);
445406
}
446407
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);
452409
}
453410
}
454411
}
@@ -491,11 +448,7 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
491448
command.args(["-C", path]);
492449
}
493450
_ => {
494-
warn!(
495-
logger,
496-
"Could not determine repository path for rebase. \
497-
Running in current directory."
498-
);
451+
announce(logger, Announcement::CouldNotFindRepositoryPath);
499452
}
500453
}
501454

@@ -506,17 +459,15 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
506459
}
507460

508461
if config.dry_run {
509-
info!(logger, "would have run git rebase"; "command" => format!("{:?}", command));
462+
announce(logger, Announcement::WouldHaveRebased(&command));
510463
} else {
511464
debug!(logger, "running git rebase"; "command" => format!("{:?}", command));
512465
// Don't check that we have successfully absorbed everything, nor git's
513466
// exit code -- as git will print helpful messages on its own.
514467
command.status().expect("could not run git rebase");
515468
}
516469
} 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(" ")));
520471
}
521472
}
522473

@@ -617,6 +568,110 @@ fn index_stats(repo: &git2::Repository) -> Result<git2::DiffStats> {
617568
Ok(stats)
618569
}
619570

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+
620675
#[cfg(test)]
621676
mod tests {
622677
use git2::message_trailers_strs;

0 commit comments

Comments
 (0)