diff --git a/CHANGELOG.md b/CHANGELOG.md index aa19935e91..456d550d2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/cli/src/config-schema.json b/cli/src/config-schema.json index bcb2631437..8efedcdc0f 100644 --- a/cli/src/config-schema.json +++ b/cli/src/config-schema.json @@ -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 } } } diff --git a/cli/src/config/misc.toml b/cli/src/config/misc.toml index 0f2306fda5..aead72170b 100644 --- a/cli/src/config/misc.toml +++ b/cli/src/config/misc.toml @@ -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+ diff --git a/cli/src/diff_util.rs b/cli/src/diff_util.rs index e75421b459..2563cdb91f 100644 --- a/cli/src/diff_util.rs +++ b/cli/src/diff_util.rs @@ -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 { @@ -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" => { @@ -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, }) @@ -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 { + let context = args + .context + .map_or_else(|| settings.config().get("diff.git.context"), Ok)?; + Ok(UnifiedDiffOptions { + context, line_diff: LineDiffOptions::from_args(args), - } + }) } } diff --git a/cli/tests/test_diff_command.rs b/cli/tests/test_diff_command.rs index c332278a70..411475c631 100644 --- a/cli/tests/test_diff_command.rs +++ b/cli/tests/test_diff_command.rs @@ -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(); diff --git a/docs/config.md b/docs/config.md index 8aa33d1216..ee73f9482f 100644 --- a/docs/config.md +++ b/docs/config.md @@ -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, ` ... ` sequence has 1 alternation, so the @@ -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