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 9db6be3
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `jj branch list` now supports a `--tracked/-t` option which can be used to
show tracked branches only. Omits local Git-tracking branches by default.

* `jj diff/interdiff/obslog` now accept a `--context` flag for the number of
lines of context to show.

### Fixed bugs

* On Windows, symlinks in the repo are now materialized as regular files in the
Expand Down
4 changes: 4 additions & 0 deletions cli/src/commands/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub(crate) struct DiffArgs {
paths: Vec<String>,
#[command(flatten)]
format: DiffFormatArgs,
/// Number of lines of context to show.
#[arg(long)]
context: Option<usize>,
}

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

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

#[instrument(skip_all)]
Expand Down Expand Up @@ -114,6 +117,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 +137,14 @@ 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 +158,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 +176,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
39 changes: 30 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,
num_context_lines: usize,
tree_diff: TreeDiffStream,
) -> Result<(), CommandError> {
formatter.push_label("diff")?;
Expand All @@ -449,7 +452,12 @@ 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,
num_context_lines,
formatter,
)?;
}
} else if right_value.is_present() {
let description = match (&left_value, &right_value) {
Expand Down Expand Up @@ -508,6 +516,7 @@ pub fn show_color_words_diff(
show_color_words_diff_hunks(
&left_content.contents,
&right_content.contents,
num_context_lines,
formatter,
)?;
}
Expand All @@ -523,7 +532,12 @@ 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,
&[],
num_context_lines,
formatter,
)?;
}
}
}
Expand Down Expand Up @@ -694,8 +708,9 @@ fn show_unified_diff_hunks(
formatter: &mut dyn Formatter,
left_content: &[u8],
right_content: &[u8],
num_context_lines: usize,
) -> Result<(), CommandError> {
for hunk in unified_diff_hunks(left_content, right_content, 3) {
for hunk in unified_diff_hunks(left_content, right_content, num_context_lines) {
writeln!(
formatter.labeled("hunk_header"),
"@@ -{},{} +{},{} @@",
Expand Down Expand Up @@ -760,6 +775,7 @@ fn materialized_diff_stream<'a>(
pub fn show_git_diff(
formatter: &mut dyn Formatter,
workspace_command: &WorkspaceCommandHelper,
num_context_lines: usize,
tree_diff: TreeDiffStream,
) -> Result<(), CommandError> {
formatter.push_label("diff")?;
Expand All @@ -778,7 +794,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, num_context_lines)?;
} 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 +819,12 @@ 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,
num_context_lines,
)?;
} else {
let left_part = git_diff_part(&path, left_value)?;
formatter.with_label("file_header", |formatter| {
Expand All @@ -813,7 +834,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, &[], num_context_lines)?;
}
}
Ok::<(), CommandError>(())
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/cli-reference@.md.snap
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ With the `--from` and/or `--to` options, shows the difference from/to the given
Possible values: `true`, `false`
* `--tool <TOOL>` — Generate diff by external command
* `--context <CONTEXT>` — Number of lines of context to show
Expand Down Expand Up @@ -1000,6 +1001,7 @@ This excludes changes from other commits by temporarily rebasing `--from` onto `
Possible values: `true`, `false`
* `--tool <TOOL>` — Generate diff by external command
* `--context <CONTEXT>` — Number of lines of context to show
Expand Down Expand Up @@ -1221,6 +1223,7 @@ Show how a change has evolved as it's been updated, rebased, etc.
Possible values: `true`, `false`
* `--tool <TOOL>` — Generate diff by external command
* `--context <CONTEXT>` — Number of lines of context to show
Expand Down

0 comments on commit 9db6be3

Please sign in to comment.