Skip to content

Commit

Permalink
diff: Allow setting the default level of context in config.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mossop committed Oct 25, 2024
1 parent 3268409 commit 891fa88
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
belonging to a remote. This option can be combined with `--tracked` or
`--conflicted`.

* Added the config settings `diff.color-words.context` and `diff.git.context` to
control the default number of lines of context shown.

### Fixed bugs

* Error on `trunk()` revset resolution is now handled gracefully.
Expand Down
16 changes: 16 additions & 0 deletions cli/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,22 @@
"type": "integer",
"description": "Maximum number of removed/added word alternation to inline",
"default": 3
},
"context": {
"type": "integer",
"description": "Number of lines of context to show",
"default": 3
}
}
},
"git": {
"type": "object",
"description": "Options for git diffs",
"properties": {
"context": {
"type": "integer",
"description": "Number of lines of context to show",
"default": 3
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions cli/src/config/misc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ unamend = ["unsquash"]

[diff.color-words]
max-inline-alternation = 3
context = 3

[diff.git]
context = 3

[ui]
# TODO: delete ui.allow-filesets in jj 0.26+
Expand Down
23 changes: 16 additions & 7 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn diff_formats_from_args(
formats.push(DiffFormat::NameOnly);
}
if args.git {
let options = UnifiedDiffOptions::from_args(args);
let options = UnifiedDiffOptions::from_settings_and_args(settings, args)?;
formats.push(DiffFormat::Git(Box::new(options)));
}
if args.color_words {
Expand Down Expand Up @@ -230,7 +230,7 @@ fn default_diff_format(
"types" => Ok(DiffFormat::Types),
"name-only" => Ok(DiffFormat::NameOnly),
"git" => {
let options = UnifiedDiffOptions::from_args(args);
let options = UnifiedDiffOptions::from_settings_and_args(settings, args)?;
Ok(DiffFormat::Git(Box::new(options)))
}
"color-words" => {
Expand Down Expand Up @@ -520,8 +520,11 @@ impl ColorWordsDiffOptions {
})?),
}
};
let context = args
.context
.map_or_else(|| config.get("diff.color-words.context"), Ok)?;
Ok(ColorWordsDiffOptions {
context: args.context.unwrap_or(DEFAULT_CONTEXT_LINES),
context,
line_diff: LineDiffOptions::from_args(args),
max_inline_alternation,
})
Expand Down Expand Up @@ -1212,11 +1215,17 @@ pub struct UnifiedDiffOptions {
}

impl UnifiedDiffOptions {
fn from_args(args: &DiffFormatArgs) -> Self {
UnifiedDiffOptions {
context: args.context.unwrap_or(DEFAULT_CONTEXT_LINES),
fn from_settings_and_args(
settings: &UserSettings,
args: &DiffFormatArgs,
) -> Result<Self, config::ConfigError> {
let context = args
.context
.map_or_else(|| settings.config().get("diff.git.context"), Ok)?;
Ok(UnifiedDiffOptions {
context,
line_diff: LineDiffOptions::from_args(args),
}
})
}
}

Expand Down
94 changes: 94 additions & 0 deletions cli/tests/test_diff_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,100 @@ fn test_diff_skipped_context() {
"###);
}

#[test]
fn test_diff_skipped_context_from_settings_color_words() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

test_env.add_config(
r#"
[diff.color-words]
context = 0
"#,
);

std::fs::write(repo_path.join("file1"), "a\nb\nc\nd\ne").unwrap();
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "=== First commit"]);

test_env.jj_cmd_ok(&repo_path, &["new", "@", "-m", "=== Must show 0 context"]);
std::fs::write(repo_path.join("file1"), "a\nb\nC\nd\ne").unwrap();

let stdout = test_env.jj_cmd_success(
&repo_path,
&["log", "-Tdescription", "-p", "--no-graph", "--reversed"],
);
insta::assert_snapshot!(stdout, @r#"
=== First commit
Added regular file file1:
1: a
2: b
3: c
4: d
5: e
=== Must show 0 context
Modified regular file file1:
...
3 3: cC
...
"#);
}

#[test]
fn test_diff_skipped_context_from_settings_git() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

test_env.add_config(
r#"
[diff.git]
context = 0
"#,
);

std::fs::write(repo_path.join("file1"), "a\nb\nc\nd\ne").unwrap();
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "=== First commit"]);

test_env.jj_cmd_ok(&repo_path, &["new", "@", "-m", "=== Must show 0 context"]);
std::fs::write(repo_path.join("file1"), "a\nb\nC\nd\ne").unwrap();

let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"log",
"-Tdescription",
"-p",
"--git",
"--no-graph",
"--reversed",
],
);
insta::assert_snapshot!(stdout, @r#"
=== First commit
diff --git a/file1 b/file1
new file mode 100644
index 0000000000..0fec236860
--- /dev/null
+++ b/file1
@@ -1,0 +1,5 @@
+a
+b
+c
+d
+e
\ No newline at end of file
=== Must show 0 context
diff --git a/file1 b/file1
index 0fec236860..b7615dae52 100644
--- a/file1
+++ b/file1
@@ -3,1 +3,1 @@
-c
+C
"#);
}

#[test]
fn test_diff_skipped_context_nondefault() {
let test_env = TestEnvironment::default();
Expand Down
16 changes: 15 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ ui.diff.format = "git"

In color-words diffs, changed words are displayed inline by default. Because
it's difficult to read a diff line with many removed/added words, there's a
threshold to switch to traditional separate-line format.
threshold to switch to traditional separate-line format. You can also change
the default number of lines of context shown.

* `max-inline-alternation`: Maximum number of removed/added word alternation to
inline. For example, `<added> ... <added>` sequence has 1 alternation, so the
Expand All @@ -219,10 +220,23 @@ threshold to switch to traditional separate-line format.
The default is `3`.

**This parameter is experimental.** The definition is subject to change.
* `context`: Number of lines of context to show in the diff. The default is `3`.

```toml
[diff.color-words]
max-inline-alternation = 3
context = 3
```

#### Git diff options

In git diffs you can change the default number of lines of context shown.

* `context`: Number of lines of context to show in the diff. The default is `3`.

```toml
[diff.git]
context = 3
```

### Generating diffs by external command
Expand Down

0 comments on commit 891fa88

Please sign in to comment.