From 08da40bc82373442736bafabacb0123cbefca852 Mon Sep 17 00:00:00 2001 From: Jonathan Tan Date: Tue, 16 Jan 2024 14:49:12 -0800 Subject: [PATCH] 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. --- cli/src/commands/branch.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/cli/src/commands/branch.rs b/cli/src/commands/branch.rs index 52c8b8010..a798487c5 100644 --- a/cli/src/commands/branch.rs +++ b/cli/src/commands/branch.rs @@ -701,16 +701,24 @@ fn cmd_branch_list( revset::walk_revs(repo.as_ref(), &remote_added_ids, &local_added_ids)?.count(); let local_ahead_count = revset::walk_revs(repo.as_ref(), &local_added_ids, &remote_added_ids)?.count(); - if remote_ahead_count != 0 && local_ahead_count == 0 { - write!(formatter, " (ahead by {remote_ahead_count} commits)")?; - } else if remote_ahead_count == 0 && local_ahead_count != 0 { - write!(formatter, " (behind by {local_ahead_count} commits)")?; - } else if remote_ahead_count != 0 && local_ahead_count != 0 { - write!( - formatter, - " (ahead by {remote_ahead_count} commits, behind by {local_ahead_count} \ - commits)" - )?; + let remote_ahead_message = if remote_ahead_count != 0 { + Some(format!("ahead by {remote_ahead_count} commits")) + } else { + None + }; + let local_ahead_message = if local_ahead_count != 0 { + Some(format!("behind by {local_ahead_count} commits")) + } else { + 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)?;