branch: refactor ahead/behind message

A subsequent commit will teach more variants of ahead/behind messages,
so refactor the existing code to avoid code duplication and a future
combinatorial explosion.
This commit is contained in:
Jonathan Tan 2024-01-16 14:49:12 -08:00 committed by jonathantanmy
parent 22117171bd
commit 08da40bc82

View file

@ -701,16 +701,24 @@ fn cmd_branch_list(
revset::walk_revs(repo.as_ref(), &remote_added_ids, &local_added_ids)?.count(); revset::walk_revs(repo.as_ref(), &remote_added_ids, &local_added_ids)?.count();
let local_ahead_count = let local_ahead_count =
revset::walk_revs(repo.as_ref(), &local_added_ids, &remote_added_ids)?.count(); revset::walk_revs(repo.as_ref(), &local_added_ids, &remote_added_ids)?.count();
if remote_ahead_count != 0 && local_ahead_count == 0 { let remote_ahead_message = if remote_ahead_count != 0 {
write!(formatter, " (ahead by {remote_ahead_count} commits)")?; Some(format!("ahead by {remote_ahead_count} commits"))
} else if remote_ahead_count == 0 && local_ahead_count != 0 { } else {
write!(formatter, " (behind by {local_ahead_count} commits)")?; None
} else if remote_ahead_count != 0 && local_ahead_count != 0 { };
write!( let local_ahead_message = if local_ahead_count != 0 {
formatter, Some(format!("behind by {local_ahead_count} commits"))
" (ahead by {remote_ahead_count} commits, behind by {local_ahead_count} \ } else {
commits)" None
)?; };
match (remote_ahead_message, local_ahead_message) {
(Some(rm), Some(lm)) => {
write!(formatter, " ({rm}, {lm})")?;
}
(Some(m), None) | (None, Some(m)) => {
write!(formatter, " ({m})")?;
}
(None, None) => { /* do nothing */ }
} }
} }
print_branch_target(formatter, &remote_ref.target)?; print_branch_target(formatter, &remote_ref.target)?;