From f6ec0028f12c318c05dd280b87c67c86d15f8553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C3=84lgmyr?= Date: Sat, 2 Mar 2024 01:57:43 +0100 Subject: [PATCH] Add --context flag for diffs. Allows specifying the number of lines of context to show around diffs. The logic was already in place, just some plumbing was needed. --- cli/src/commands/diff.rs | 3 +++ cli/src/commands/interdiff.rs | 3 +++ cli/src/commands/obslog.rs | 7 ++++++- cli/src/description_util.rs | 1 + cli/src/diff_util.rs | 24 +++++++++++++++--------- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/cli/src/commands/diff.rs b/cli/src/commands/diff.rs index 5a072fa0120..01b834a1d23 100644 --- a/cli/src/commands/diff.rs +++ b/cli/src/commands/diff.rs @@ -50,6 +50,8 @@ pub(crate) struct DiffArgs { paths: Vec, #[command(flatten)] format: DiffFormatArgs, + #[arg(long)] + context: Option, } #[instrument(skip_all)] @@ -84,6 +86,7 @@ pub(crate) fn cmd_diff( &to_tree, matcher.as_ref(), &diff_formats, + args.context.unwrap_or(3), )?; Ok(()) } diff --git a/cli/src/commands/interdiff.rs b/cli/src/commands/interdiff.rs index b83f5f3b691..a5c07a37f98 100644 --- a/cli/src/commands/interdiff.rs +++ b/cli/src/commands/interdiff.rs @@ -39,6 +39,8 @@ pub(crate) struct InterdiffArgs { paths: Vec, #[command(flatten)] format: DiffFormatArgs, + #[arg(long)] + context: Option, } #[instrument(skip_all)] @@ -64,5 +66,6 @@ pub(crate) fn cmd_interdiff( &to_tree, matcher.as_ref(), &diff_formats, + args.context.unwrap_or(3), ) } diff --git a/cli/src/commands/obslog.rs b/cli/src/commands/obslog.rs index 5994b8fe28f..e37090db51b 100644 --- a/cli/src/commands/obslog.rs +++ b/cli/src/commands/obslog.rs @@ -53,6 +53,8 @@ pub(crate) struct ObslogArgs { patch: bool, #[command(flatten)] diff_format: DiffFormatArgs, + #[arg(long)] + context: Option, } #[instrument(skip_all)] @@ -114,6 +116,7 @@ pub(crate) fn cmd_obslog( &workspace_command, &commit, &diff_formats, + args.context, )?; } let node_symbol = if Some(commit.id()) == wc_commit_id { @@ -133,7 +136,7 @@ pub(crate) fn cmd_obslog( with_content_format .write(formatter, |formatter| template.format(&commit, formatter))?; if !diff_formats.is_empty() { - show_predecessor_patch(ui, formatter, &workspace_command, &commit, &diff_formats)?; + show_predecessor_patch(ui, formatter, &workspace_command, &commit, &diff_formats, args.context)?; } } } @@ -147,6 +150,7 @@ fn show_predecessor_patch( workspace_command: &WorkspaceCommandHelper, commit: &Commit, diff_formats: &[DiffFormat], + num_context_lines: Option, ) -> Result<(), CommandError> { let predecessors = commit.predecessors(); let predecessor = match predecessors.first() { @@ -164,5 +168,6 @@ fn show_predecessor_patch( &tree, &EverythingMatcher, diff_formats, + num_context_lines.unwrap_or(3), ) } diff --git a/cli/src/description_util.rs b/cli/src/description_util.rs index 745ab53ba06..749a890e410 100644 --- a/cli/src/description_util.rs +++ b/cli/src/description_util.rs @@ -110,6 +110,7 @@ pub fn description_template_for_commit( to_tree, &EverythingMatcher, &[DiffFormat::Summary], + 3, )?; let mut template_chunks = Vec::new(); if !intro.is_empty() { diff --git a/cli/src/diff_util.rs b/cli/src/diff_util.rs index 6f52ad294a5..8307d5b9598 100644 --- a/cli/src/diff_util.rs +++ b/cli/src/diff_util.rs @@ -175,6 +175,7 @@ pub fn show_diff( to_tree: &MergedTree, matcher: &dyn Matcher, formats: &[DiffFormat], + num_context_lines: usize, ) -> Result<(), CommandError> { for format in formats { match format { @@ -192,11 +193,11 @@ pub fn show_diff( } DiffFormat::Git => { let tree_diff = from_tree.diff_stream(to_tree, matcher); - show_git_diff(formatter, workspace_command, tree_diff)?; + show_git_diff(formatter, workspace_command, num_context_lines, tree_diff)?; } DiffFormat::ColorWords => { let tree_diff = from_tree.diff_stream(to_tree, matcher); - show_color_words_diff(formatter, workspace_command, tree_diff)?; + show_color_words_diff(formatter, workspace_command, num_context_lines, tree_diff)?; } DiffFormat::Tool(tool) => { merge_tools::generate_diff(ui, formatter.raw(), from_tree, to_tree, matcher, tool)?; @@ -225,16 +226,17 @@ pub fn show_patch( &to_tree, matcher, formats, + 3, ) } fn show_color_words_diff_hunks( left: &[u8], right: &[u8], + num_context_lines: usize, formatter: &mut dyn Formatter, ) -> io::Result<()> { const SKIPPED_CONTEXT_LINE: &str = " ...\n"; - let num_context_lines = 3; let mut context = VecDeque::new(); // Have we printed "..." for any skipped context? let mut skipped_context = false; @@ -429,6 +431,7 @@ fn basic_diff_file_type(value: &MaterializedTreeValue) -> &'static str { pub fn show_color_words_diff( formatter: &mut dyn Formatter, workspace_command: &WorkspaceCommandHelper, + context: usize, tree_diff: TreeDiffStream, ) -> Result<(), CommandError> { formatter.push_label("diff")?; @@ -449,7 +452,7 @@ pub fn show_color_words_diff( } else if right_content.is_binary { writeln!(formatter.labeled("binary"), " (binary)")?; } else { - show_color_words_diff_hunks(&[], &right_content.contents, formatter)?; + show_color_words_diff_hunks(&[], &right_content.contents, context, formatter)?; } } else if right_value.is_present() { let description = match (&left_value, &right_value) { @@ -511,6 +514,7 @@ pub fn show_color_words_diff( formatter, )?; } + show_color_words_diff_hunks(&left_content, &right_content, context, formatter)?; } else { let description = basic_diff_file_type(&left_value); writeln!( @@ -523,7 +527,7 @@ pub fn show_color_words_diff( } else if left_content.is_binary { writeln!(formatter.labeled("binary"), " (binary)")?; } else { - show_color_words_diff_hunks(&left_content.contents, &[], formatter)?; + show_color_words_diff_hunks(&left_content.contents, &[], context, formatter)?; } } } @@ -694,8 +698,9 @@ fn show_unified_diff_hunks( formatter: &mut dyn Formatter, left_content: &[u8], right_content: &[u8], + context: usize, ) -> Result<(), CommandError> { - for hunk in unified_diff_hunks(left_content, right_content, 3) { + for hunk in unified_diff_hunks(left_content, right_content, context) { writeln!( formatter.labeled("hunk_header"), "@@ -{},{} +{},{} @@", @@ -760,6 +765,7 @@ fn materialized_diff_stream<'a>( pub fn show_git_diff( formatter: &mut dyn Formatter, workspace_command: &WorkspaceCommandHelper, + context: usize, tree_diff: TreeDiffStream, ) -> Result<(), CommandError> { formatter.push_label("diff")?; @@ -778,7 +784,7 @@ pub fn show_git_diff( writeln!(formatter, "--- /dev/null")?; writeln!(formatter, "+++ b/{path_string}") })?; - show_unified_diff_hunks(formatter, &[], &right_part.content)?; + show_unified_diff_hunks(formatter, &[], &right_part.content, context)?; } else if right_value.is_present() { let left_part = git_diff_part(&path, left_value)?; let right_part = git_diff_part(&path, right_value)?; @@ -803,7 +809,7 @@ pub fn show_git_diff( } Ok(()) })?; - show_unified_diff_hunks(formatter, &left_part.content, &right_part.content)?; + show_unified_diff_hunks(formatter, &left_part.content, &right_part.content, context)?; } else { let left_part = git_diff_part(&path, left_value)?; formatter.with_label("file_header", |formatter| { @@ -813,7 +819,7 @@ pub fn show_git_diff( writeln!(formatter, "--- a/{path_string}")?; writeln!(formatter, "+++ /dev/null") })?; - show_unified_diff_hunks(formatter, &left_part.content, &[])?; + show_unified_diff_hunks(formatter, &left_part.content, &[], context)?; } } Ok::<(), CommandError>(())