{"thread":{"id":"53483","subject":"[PATCH v2] diff: add config option relative","startedAt":"2020-05-15T17:40:16Z","lastAt":"2020-05-15T17:40:16Z","messageCount":1,"participants":["Laurent Arnoud"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"397908","messageId":"20200515174011.GA1199744@spk-laptop","threadId":"53483","inReplyTo":null,"subject":"[PATCH v2] diff: add config option relative","fromName":"Laurent Arnoud","fromEmail":"laurent@spkdev.net","sentAt":"2020-05-15T17:40:11Z","receivedAt":"2020-05-15T17:40:16Z","isPatch":true,"sender":{"key":"laurent@spkdev.net","avatar":"https://avatars.githubusercontent.com/u/98590?v=4"},"body":"The `diff.relative` boolean option set to `true` show only changes on\nthe current directory and show relative pathnames.\n\nTeach --no-relative to override earlier --relative\n\nSigned-off-by: Laurent Arnoud <laurent@spkdev.net>\n---\n Documentation/config/diff.txt   |  4 ++\n Documentation/diff-options.txt  |  2 +\n diff.c                          | 10 ++++\n t/t9904-diff-relative-config.sh | 93 +++++++++++++++++++++++++++++++++\n 4 files changed, 109 insertions(+)\n create mode 100755 t/t9904-diff-relative-config.sh\n\ndiff --git a/Documentation/config/diff.txt b/Documentation/config/diff.txt\nindex ff09f1cf73..1d311358d8 100644\n--- a/Documentation/config/diff.txt\n+++ b/Documentation/config/diff.txt\n@@ -105,6 +105,10 @@ diff.mnemonicPrefix::\n diff.noprefix::\n \tIf set, 'git diff' does not show any source or destination prefix.\n \n+diff.relative::\n+\tIf set to \"true\", 'git diff' does not show changes outside of the directory\n+\tand show pathnames relative.\n+\n diff.orderFile::\n \tFile indicating how to order files within a diff.\n \tSee the '-O' option to linkgit:git-diff[1] for details.\ndiff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt\nindex bb31f0c42b..fdc5aefa37 100644\n--- a/Documentation/diff-options.txt\n+++ b/Documentation/diff-options.txt\n@@ -651,6 +651,8 @@ ifndef::git-format-patch[]\n \tnot in a subdirectory (e.g. in a bare repository), you\n \tcan name which subdirectory to make the output relative\n \tto by giving a <path> as an argument.\n+--no-relative::\n+\tTurn off relative pathnames and include all changes in the repository.\n endif::git-format-patch[]\n \n -a::\ndiff --git a/diff.c b/diff.c\nindex d1ad6a3c4a..c4dcf01ec0 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -48,6 +48,7 @@ static const char *diff_order_file_cfg;\n int diff_auto_refresh_index = 1;\n static int diff_mnemonic_prefix;\n static int diff_no_prefix;\n+static int diff_relative;\n static int diff_stat_graph_width;\n static int diff_dirstat_permille_default = 30;\n static struct diff_options default_diff_options;\n@@ -386,6 +387,10 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)\n \t\tdiff_no_prefix = git_config_bool(var, value);\n \t\treturn 0;\n \t}\n+\tif (!strcmp(var, \"diff.relative\")) {\n+\t\tdiff_relative = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n \tif (!strcmp(var, \"diff.statgraphwidth\")) {\n \t\tdiff_stat_graph_width = git_config_int(var, value);\n \t\treturn 0;\n@@ -4558,6 +4563,8 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)\n \t\toptions->b_prefix = \"b/\";\n \t}\n \n+\toptions->flags.relative_name = diff_relative;\n+\n \toptions->color_moved = diff_color_moved_default;\n \toptions->color_moved_ws_handling = diff_color_moved_ws_default;\n \n@@ -5494,6 +5501,9 @@ static void prep_parse_options(struct diff_options *options)\n \t\t\t       N_(\"when run from subdir, exclude changes outside and show relative paths\"),\n \t\t\t       PARSE_OPT_NONEG | PARSE_OPT_OPTARG,\n \t\t\t       diff_opt_relative),\n+\t\tOPT_SET_INT_F(0, \"no-relative\", &options->flags.relative_name,\n+\t\t\t      N_(\"disable diff.relative config option\"),\n+\t\t\t      0, PARSE_OPT_NONEG),\n \t\tOPT_BOOL('a', \"text\", &options->flags.text,\n \t\t\t N_(\"treat all files as text\")),\n \t\tOPT_BOOL('R', NULL, &options->flags.reverse_diff,\ndiff --git a/t/t9904-diff-relative-config.sh b/t/t9904-diff-relative-config.sh\nnew file mode 100755\nindex 0000000000..23ab1af5e0\n--- /dev/null\n+++ b/t/t9904-diff-relative-config.sh\n@@ -0,0 +1,93 @@\n+#!/bin/sh\n+\n+test_description='config diff.relative'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\tgit commit --allow-empty -m empty &&\n+\techo content >file1 &&\n+\tmkdir subdir &&\n+\techo other content >subdir/file2 &&\n+\tgit add . &&\n+\tgit commit -m one\n+'\n+\n+check_diff_relative () {\n+\tdir=$1\n+\tshift\n+\texpect=$1\n+\tshift\n+\trelative_opt=$1\n+\tshift\n+\tshort_blob=$(git rev-parse --short \"$(git hash-object subdir/file2)\")\n+\tcat >expected <<-EOF\n+\tdiff --git a/$expect b/$expect\n+\tnew file mode 100644\n+\tindex 0000000..$short_blob\n+\t--- /dev/null\n+\t+++ b/$expect\n+\t@@ -0,0 +1 @@\n+\t+other content\n+\tEOF\n+\ttest_expect_success \"config.relative $relative_opt -p $*\" \"\n+\t\ttest_config -C $dir diff.relative $relative_opt &&\n+\t\tgit -C '$dir' diff -p $* HEAD^ >actual &&\n+\t\ttest_cmp expected actual\n+\t\"\n+}\n+\n+check_diff_no_relative () {\n+\tdir=$1\n+\tshift\n+\texpect=$1\n+\tshift\n+\trelative_opt=$1\n+\tshift\n+\tshort_blob_file1=$(git rev-parse --short \"$(git hash-object file1)\")\n+\tshort_blob_file2=$(git rev-parse --short \"$(git hash-object subdir/file2)\")\n+\tcat >expected <<-EOF\n+\tdiff --git a/file1 b/file1\n+\tnew file mode 100644\n+\tindex 0000000..$short_blob_file1\n+\t--- /dev/null\n+\t+++ b/file1\n+\t@@ -0,0 +1 @@\n+\t+content\n+\tdiff --git a/$expect b/$expect\n+\tnew file mode 100644\n+\tindex 0000000..$short_blob_file2\n+\t--- /dev/null\n+\t+++ b/$expect\n+\t@@ -0,0 +1 @@\n+\t+other content\n+\tEOF\n+\tcat expected\n+\ttest_expect_success \"config.relative $relative_opt -p $*\" \"\n+\t\ttest_config -C $dir diff.relative $relative_opt &&\n+\t\tgit -C '$dir' diff -p $* HEAD^ >actual &&\n+\t\tgit -C '$dir' diff -p $* HEAD^ >/tmp/actual &&\n+\t\ttest_cmp expected actual\n+\t\"\n+}\n+\n+check_diff_relative . file2 false --relative=subdir/\n+check_diff_relative . file2 false --relative=subdir\n+check_diff_relative . file2 true --relative=subdir/\n+check_diff_relative . file2 true --relative=subdir\n+check_diff_relative subdir file2 false --relative\n+check_diff_relative subdir file2 true --relative\n+check_diff_relative subdir file2 true\n+check_diff_relative subdir file2 false --no-relative --relative\n+check_diff_relative subdir file2 true --no-relative --relative\n+check_diff_relative . file2 false --no-relative --relative=subdir\n+check_diff_relative . file2 true --no-relative --relative=subdir\n+\n+check_diff_no_relative . subdir/file2 false\n+check_diff_no_relative . subdir/file2 true --no-relative\n+check_diff_no_relative . subdir/file2 false --no-relative\n+check_diff_no_relative subdir subdir/file2 false\n+check_diff_no_relative subdir subdir/file2 true --no-relative\n+check_diff_no_relative subdir subdir/file2 false --no-relative\n+\n+test_done\n-- \n2.26.2\n"}]}