Skip to contentSkip to conversation

diff: add configuration option for disabling diff prefixes.

PATCH New Eli Collins 3 participants 9 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 diff: add configuration option for disabling diff prefixes.

From: Eli Collins <[email protected]>

With new configuration "diff.noprefix", "git diff" does not show a source or destination prefix ala "git diff --no-prefix".

---
 Documentation/config.txt |    2 ++
 diff.c                   |    9 ++++++++-
 2 files changed, 10 insertions(+), 1 deletions(-)
Documentation/config.txt +2 -0
index 92f851e..e883952 100644
@@ -796,6 +796,8 @@ diff.mnemonicprefix::
 	standard "a/" and "b/" depending on what is being compared.  When
 	this configuration is in effect, reverse diff output also swaps
 	the order of the prefixes:
+diff.noprefix::
+	If set, 'git diff' does not show any source or destination prefix.
 `git diff`;;
 	compares the (i)ndex and the (w)ork tree;
 `git diff HEAD`;;
diff.c +8 -1
index d0ecbc3..07269c2 100644
@@ -30,6 +30,7 @@ static const char *diff_word_regex_cfg;
 static const char *external_diff_cmd_cfg;
 int diff_auto_refresh_index = 1;
 static int diff_mnemonic_prefix;
+static int diff_no_prefix;
 
 static char diff_colors[][COLOR_MAXLEN] = {
 	GIT_COLOR_RESET,
@@ -100,6 +101,10 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
 		diff_mnemonic_prefix = git_config_bool(var, value);
 		return 0;
 	}
+	if (!strcmp(var, "diff.noprefix")) {
+		diff_no_prefix = git_config_bool(var, value);
+		return 0;
+	}
 	if (!strcmp(var, "diff.external"))
 		return git_config_string(&external_diff_cmd_cfg, var, value);
 	if (!strcmp(var, "diff.wordregex"))
@@ -2559,7 +2564,9 @@ void diff_setup(struct diff_options *options)
 		DIFF_OPT_SET(options, COLOR_DIFF);
 	options->detect_rename = diff_detect_rename_default;
 
-	if (!diff_mnemonic_prefix) {
+	if (diff_no_prefix) {
+		options->a_prefix = options->b_prefix = "";
+	} else if (!diff_mnemonic_prefix) {
 		options->a_prefix = "a/";
 		options->b_prefix = "b/";
 	}
Signature · 1 line
1.7.1.dirty
#1 lore raw

PATCH 1/1 diff: add a 'path' meta header for non-renames and non-copies

Hi Eli,

I like to have a path without any prefix in the diff header, too, but also like to see the diff mnemonic prefix (see diff.mnemonicprefix config). For some diffs there is such path, which is in the extended header of the diff for copies and renames. So I wrote the appended patch wich adds also an extended header for non-copies and non-renames which shows the path without any prefix.

Regards,
Bert

--- 8< ---

From: Bert Wesarg <[email protected]>
Subject: [PATCH] diff: add a 'path' meta header for non-renames and non-copies

This way you have always a path without any diff mnemonic prefix.

---
 Documentation/config.txt              |    4 ++++
 Documentation/diff-generate-patch.txt |    4 ++++
 diff.c                                |   12 ++++++++++--
 3 files changed, 18 insertions(+), 2 deletions(-)
Documentation/config.txt +4 -0
index 92f851e..652365e 100644
@@ -807,6 +807,10 @@ diff.mnemonicprefix::
 `git diff --no-index a b`;;
 	compares two non-git things (1) and (2).
 
+diff.path::
+	Always add a 'path <path>' extended header into the diff output,
+	for non-copies and non-renames.
+
 diff.renameLimit::
 	The number of files to consider when performing the copy/rename
 	detection; equivalent to the 'git diff' option '-l'.
Documentation/diff-generate-patch.txt +4 -0
index 8f9a241..67ba78f 100644
@@ -30,6 +30,7 @@ the file that rename/copy produces, respectively.
        new mode <mode>
        deleted file mode <mode>
        new file mode <mode>
+       path <path>
        copy from <path>
        copy to <path>
        rename from <path>
@@ -38,6 +39,9 @@ the file that rename/copy produces, respectively.
        dissimilarity index <number>
        index <hash>..<hash> <mode>
 
+    The 'path' header will only show up, if the diff.path configure option
+    is set.
+
 3.  TAB, LF, double quote and backslash characters in pathnames
     are represented as `\t`, `\n`, `\"` and `\\`, respectively.
     If there is need for such substitution then the whole
diff.c +10 -2
index d0ecbc3..fa33b9c 100644
@@ -30,6 +30,7 @@ static const char *diff_word_regex_cfg;
 static const char *external_diff_cmd_cfg;
 int diff_auto_refresh_index = 1;
 static int diff_mnemonic_prefix;
+static int diff_path;
 
 static char diff_colors[][COLOR_MAXLEN] = {
 	GIT_COLOR_RESET,
@@ -104,6 +105,10 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
 		return git_config_string(&external_diff_cmd_cfg, var, value);
 	if (!strcmp(var, "diff.wordregex"))
 		return git_config_string(&diff_word_regex_cfg, var, value);
+	if (!strcmp(var, "diff.path")) {
+		diff_path = git_config_bool(var, value);
+		return 0;
+	}
 
 	return git_diff_basic_config(var, value, cb);
 }
@@ -2351,8 +2356,11 @@ static void fill_metainfo(struct strbuf *msg,
 		}
 		/* fallthru */
 	default:
-		/* nothing */
-		;
+		if (diff_path) {
+			strbuf_addstr(msg, "path ");
+			quote_c_style(name, msg, NULL, 0);
+			strbuf_addch(msg, '\n');
+		}
 	}
 	if (one && two && hashcmp(one->sha1, two->sha1)) {
 		int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
Signature · 1 line
tg: (ddd02b7..) bw/always-print-a-path-meta-header (depends on: master)
#2 lore raw

Re: [PATCH] diff: add a 'path' meta header for non-renames and non-copies

Sorry, but none of these configuration variables are acceptable as-is.

Many scripts (the ones shipped with core-git, in contrib, or written by users) depend on their invocation of "git diff-*" family without any funny command line arguments like --no-prefix nor --src-prefix to produce a patch that can be applied without giving a custom -p value to "git apply".

Letting the user break that promise is already bad, and not giving the scripts any way to protect themselves from these configuration variables by overriding from the command line makes these doubly bad ideas.

#3 lore raw

Re: [PATCH] diff: add a 'path' meta header for non-renames and non-copies

Quoted 10 lines · Junio C Hamano

Sorry, but none of these configuration variables are acceptable as-is.

Many scripts (the ones shipped with core-git, in contrib, or written by users) depend on their invocation of "git diff-*" family without any funny command line arguments like --no-prefix nor --src-prefix to produce a patch that can be applied without giving a custom -p value to "git apply".

Letting the user break that promise is already bad, and not giving the scripts any way to protect themselves from these configuration variables by overriding from the command line makes these doubly bad ideas.

As far as I understand git_diff_ui_config() applies only for "git diff" not for any of the plumping "git diff-*" tools. Also I thought that these extended headers are exactly there to support arbitrary meta information. Therefore I think your arguments don't hold for my 'path' extended header, do they?

Thanks,
Bert

Quoted 1 line
#4 lore raw

Re: [PATCH] diff: add a 'path' meta header for non-renames and non-copies

Quoted 2 lines · Bert Wesarg

As far as I understand git_diff_ui_config() applies only for "git diff" not for any of the plumping "git diff-*" tools.

Ah, I missed that the patch was to the diff_ui parser. Sorry for confusion and thanks for straightening me up.

#5 lore raw

Re: [PATCH] diff: add a 'path' meta header for non-renames and non-copies

Quoted 8 lines · Junio C Hamano
Quoted 2 lines · Bert Wesarg

As far as I understand git_diff_ui_config() applies only for "git diff" not for any of the plumping "git diff-*" tools.

Ah, I missed that the patch was to the diff_ui parser.  Sorry for confusion and thanks for straightening me up.

So, now that I render your arguments voidless, do you have any other thoughts on adding a <path> extended header for non-renames and non-copies?

Regards,
Bert

#6 lore raw

Re: [PATCH] diff: add a 'path' meta header for non-renames and non-copies

Quoted 3 lines · Bert Wesarg

... do you have any other
thoughts on adding a <path> extended header for non-renames and non-copies?

Other than "Why do we even need it?", no. We actually have been careful when designing the output format to make sure that necessary information appears.

See also this thread:

    http://thread.gmane.org/gmane.comp.version-control.git/3990/focus=4002

I wasn't too opposed to having a separate "Index: " line (or Anton's "name") like CVS does, but the conclusion of the thread was that it is not necessary.

#7 lore raw

Re: [PATCH] diff: add a 'path' meta header for non-renames and non-copies

Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Bert Wesarg

... do you have any other
thoughts on adding a <path> extended header for non-renames and non-copies?

Other than "Why do we even need it?", no.  We actually have been careful when designing the output format to make sure that necessary information appears.

Quoted 8 lines

See also this thread:

   http://thread.gmane.org/gmane.comp.version-control.git/3990/focus=4002

I wasn't too opposed to having a separate "Index: " line (or Anton's "name") like CVS does, but the conclusion of the thread was that it is not necessary.

Thanks for the pointer. The discussion revolves around technical arguments and the conclusion that this extra information is not necessary is reasonable. My intention for this information comes only from the usability and convenience side: I want a path without any prefix which I can select in the terminal and use this to open the file. Thats why it is only an option for the UI interface.

Bert

#8 lore raw

Re: [PATCH] diff: add a 'path' meta header for non-renames and non-copies

Ditto. I frequently generate patches for other systems (primarily svn) and they require diffs w/o perfixes. Seemed like there should be a way to enable this option in gitconfig so users don't need to type --no-prefix w every diff invocation or use an alias (aliasing diff to diff --no-prefix in gitconfig doesn't work and wouldn't be a good idea anyway).

Thanks,
Eli

Quoted 29 lines · Bert Wesarg
Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Bert Wesarg

... do you have any other
thoughts on adding a <path> extended header for non-renames and non-copies?

Other than "Why do we even need it?", no.  We actually have been careful when designing the output format to make sure that necessary information appears.

Quoted 8 lines

See also this thread:

   http://thread.gmane.org/gmane.comp.version-control.git/3990/focus=4002

I wasn't too opposed to having a separate "Index: " line (or Anton's "name") like CVS does, but the conclusion of the thread was that it is not necessary.

Thanks for the pointer. The discussion revolves around technical arguments and the conclusion that this extra information is not necessary is reasonable. My intention for this information comes only from the usability and convenience side: I want a path without any prefix which I can select in the terminal and use this to open the file. Thats why it is only an option for the UI interface.

Bert