Skip to content

Commit

Permalink
Add --context flag for diffs.
Browse files Browse the repository at this point in the history
Allows specifying the number of lines of context to show around diffs.
The logic was already in place, just some plumbing was needed.
  • Loading branch information
algmyr committed Mar 2, 2024
1 parent b926fd8 commit f6ec002
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
3 changes: 3 additions & 0 deletions cli/src/commands/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub(crate) struct DiffArgs {
paths: Vec<String>,
#[command(flatten)]
format: DiffFormatArgs,
#[arg(long)]
context: Option<usize>,
}

#[instrument(skip_all)]
Expand Down Expand Up @@ -84,6 +86,7 @@ pub(crate) fn cmd_diff(
&to_tree,
matcher.as_ref(),
&diff_formats,
args.context.unwrap_or(3),
)?;
Ok(())
}
3 changes: 3 additions & 0 deletions cli/src/commands/interdiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub(crate) struct InterdiffArgs {
paths: Vec<String>,
#[command(flatten)]
format: DiffFormatArgs,
#[arg(long)]
context: Option<usize>,
}

#[instrument(skip_all)]
Expand All @@ -64,5 +66,6 @@ pub(crate) fn cmd_interdiff(
&to_tree,
matcher.as_ref(),
&diff_formats,
args.context.unwrap_or(3),
)
}
7 changes: 6 additions & 1 deletion cli/src/commands/obslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub(crate) struct ObslogArgs {
patch: bool,
#[command(flatten)]
diff_format: DiffFormatArgs,
#[arg(long)]
context: Option<usize>,
}

#[instrument(skip_all)]
Expand Down Expand Up @@ -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 {
Expand All @@ -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)?;
}
}
}
Expand All @@ -147,6 +150,7 @@ fn show_predecessor_patch(
workspace_command: &WorkspaceCommandHelper,
commit: &Commit,
diff_formats: &[DiffFormat],
num_context_lines: Option<usize>,
) -> Result<(), CommandError> {
let predecessors = commit.predecessors();
let predecessor = match predecessors.first() {
Expand All @@ -164,5 +168,6 @@ fn show_predecessor_patch(
&tree,
&EverythingMatcher,
diff_formats,
num_context_lines.unwrap_or(3),
)
}
1 change: 1 addition & 0 deletions cli/src/description_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
24 changes: 15 additions & 9 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)?;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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")?;
Expand All @@ -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) {
Expand Down Expand Up @@ -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!(
Expand All @@ -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)?;
}
}
}
Expand Down Expand Up @@ -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"),
"@@ -{},{} +{},{} @@",
Expand Down Expand Up @@ -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")?;
Expand All @@ -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)?;
Expand All @@ -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| {
Expand All @@ -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>(())
Expand Down

0 comments on commit f6ec002

Please sign in to comment.