Skip to contentSkip to conversation

diff.c: remove output_prefix_length field

PATCH Superseded Jacob Keller 3 participants 10 messages Aug 11, 2016 – Aug 15, 2016 2 messages not in the archive Documentation

v5 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/3 diff.c: remove output_prefix_length field

From: Junio C Hamano <[email protected]>

"diff/log --stat" has a logic that determines the display columns available for the diffstat part of the output and apportions it for pathnames and diffstat graph automatically.

5e71a84a (Add output_prefix_length to diff_options, 2012-04-16) added the output_prefix_length field to diff_options structure to allow this logic subtract the display columns used for display the history graph part from the total "terminal width"; this matters when the "git log --graph -p" option is in use.

The field be set to the number of display columns needed to show the output from the output_prefix() callback. Any new output_prefix() callback must also update the field accordingly, which is error prone. As there is only one user of the field, and the user has the actual value of the prefix string, let's get rid of the field and have the user count the display width itself.

---
This is the same as what Junio submitted already, just thought I'd add it for clarity.

 diff.c  | 2 +-
 diff.h  | 1 -
 graph.c | 2 --
 3 files changed, 1 insertion(+), 4 deletions(-)
diff.c +1 -1
index b43d3dd2ecb7..ae069c303077 100644
@@ -1625,7 +1625,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
 	 */
 
 	if (options->stat_width == -1)
-		width = term_columns() - options->output_prefix_length;
+		width = term_columns() - strlen(line_prefix);
 	else
 		width = options->stat_width ? options->stat_width : 80;
 	number_width = decimal_width(max_change) > number_width ?
diff.h +0 -1
index 125447be09eb..49e4aaafb2da 100644
@@ -174,7 +174,6 @@ struct diff_options {
 	diff_format_fn_t format_callback;
 	void *format_callback_data;
 	diff_prefix_fn_t output_prefix;
-	int output_prefix_length;
 	void *output_prefix_data;
 
 	int diff_path_counter;
graph.c +0 -2
index dd1720148dc5..a46803840511 100644
@@ -197,7 +197,6 @@ static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, void
 	assert(opt);
 	assert(graph);
 
-	opt->output_prefix_length = graph->width;
 	strbuf_reset(&msgbuf);
 	graph_padding_line(graph, &msgbuf);
 	return &msgbuf;
@@ -245,7 +244,6 @@ struct git_graph *graph_init(struct rev_info *opt)
 	 */
 	opt->diffopt.output_prefix = diff_output_prefix_callback;
 	opt->diffopt.output_prefix_data = graph;
-	opt->diffopt.output_prefix_length = 0;
 
 	return graph;
 }
Signature · 1 line
2.9.2.872.g367ebef.dirty
#1 lore raw

PATCH v5 2/3 diff: add --diff-line-prefix option for passing in a prefix

From: Jacob Keller <[email protected]>

Add an option to pass additional prefix to be displayed before diff output. This feature will be used in a following patch to output correct --graph prefix when using a child_process/run_command interface for submodules.

The prefix shall come first prior to any other prefix associated with the --graph option or other source.

Add tests for the same.

---
- v5
* Changed name to --diff-line-prefix since --line-prefix might indicate for other commands such as log, when it only modifies diff output

 Documentation/diff-options.txt                     |  3 +++
 diff.c                                             | 16 ++++++++++--
 diff.h                                             |  1 +
 t/t4013-diff-various.sh                            |  6 +++++
 ...diff_--diff-line-prefix=-->_master_master^_side | 29 ++++++++++++++++++++++
 .../diff.diff_--diff-line-prefix_--cached_--_file0 | 15 +++++++++++
 6 files changed, 68 insertions(+), 2 deletions(-)
 create mode 100644 t/t4013/diff.diff_--diff-line-prefix=-->_master_master^_side
 create mode 100644 t/t4013/diff.diff_--diff-line-prefix_--cached_--_file0
Documentation/diff-options.txt +3 -0
index 705a87394200..f924f57d4f62 100644
@@ -569,5 +569,8 @@ endif::git-format-patch[]
 --no-prefix::
 	Do not show any source or destination prefix.
 
+--diff-line-prefix=<prefix>::
+	Prepend an additional prefix to every line of diff output.
+
 For more detailed explanation on these common options, see also
 linkgit:gitdiffcore[7].
diff.c +14 -2
index ae069c303077..73dda58c440c 100644
@@ -1167,10 +1167,18 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)
 const char *diff_line_prefix(struct diff_options *opt)
 {
 	struct strbuf *msgbuf;
-	if (!opt->output_prefix)
-		return "";
+
+	if (!opt->output_prefix) {
+		if (opt->line_prefix)
+			return opt->line_prefix;
+		else
+			return "";
+	}
 
 	msgbuf = opt->output_prefix(opt, opt->output_prefix_data);
+	/* line prefix must be printed before the output_prefix() */
+	if (opt->line_prefix)
+		strbuf_insert(msgbuf, 0, opt->line_prefix, strlen(opt->line_prefix));
 	return msgbuf->buf;
 }
 
@@ -3966,6 +3974,10 @@ int diff_opt_parse(struct diff_options *options,
 		options->a_prefix = optarg;
 		return argcount;
 	}
+	else if ((argcount = parse_long_opt("diff-line-prefix", av, &optarg))) {
+		options->line_prefix = optarg;
+		return argcount;
+	}
 	else if ((argcount = parse_long_opt("dst-prefix", av, &optarg))) {
 		options->b_prefix = optarg;
 		return argcount;
diff.h +1 -0
index 49e4aaafb2da..83d0b1ae8580 100644
@@ -115,6 +115,7 @@ struct diff_options {
 	const char *pickaxe;
 	const char *single_follow;
 	const char *a_prefix, *b_prefix;
+	const char *line_prefix;
 	unsigned flags;
 	unsigned touched_flags;
 
t/t4013-diff-various.sh +6 -0
index 94ef5000e787..5204645eb92b 100755
@@ -306,6 +306,8 @@ diff --no-index --name-status dir2 dir
 diff --no-index --name-status -- dir2 dir
 diff --no-index dir dir3
 diff master master^ side
+# Can't use spaces...
+diff --diff-line-prefix=--> master master^ side
 diff --dirstat master~1 master~2
 diff --dirstat initial rearrange
 diff --dirstat-by-file initial rearrange
@@ -325,6 +327,10 @@ test_expect_success 'diff --cached -- file on unborn branch' '
 	git diff --cached -- file0 >result &&
 	test_cmp "$TEST_DIRECTORY/t4013/diff.diff_--cached_--_file0" result
 '
+test_expect_success 'diff --diff-line-prefix with spaces' '
+	git diff --diff-line-prefix="| | | " --cached -- file0 >result &&
+	test_cmp "$TEST_DIRECTORY/t4013/diff.diff_--diff-line-prefix_--cached_--_file0" result
+'
 
 test_expect_success 'diff-tree --stdin with log formatting' '
 	cat >expect <<-\EOF &&
t/t4013/diff.diff_--diff-line-prefix=-->_master_master^_side new file +29 -0
new file mode 100644
index 000000000000..5cc90f27c2d9
@@ -0,0 +1,29 @@
+$ git diff --diff-line-prefix=--> master master^ side
+-->diff --cc dir/sub
+-->index cead32e,7289e35..992913c
+-->--- a/dir/sub
+-->+++ b/dir/sub
+-->@@@ -1,6 -1,4 +1,8 @@@
+-->  A
+-->  B
+--> +C
+--> +D
+--> +E
+--> +F
+-->+ 1
+-->+ 2
+-->diff --cc file0
+-->index b414108,f4615da..10a8a9f
+-->--- a/file0
+-->+++ b/file0
+-->@@@ -1,6 -1,6 +1,9 @@@
+-->  1
+-->  2
+-->  3
+--> +4
+--> +5
+--> +6
+-->+ A
+-->+ B
+-->+ C
+$
t/t4013/diff.diff_--diff-line-prefix_--cached_--_file0 new file +15 -0
new file mode 100644
index 000000000000..f41ba4d36aa1
@@ -0,0 +1,15 @@
+| | | diff --git a/file0 b/file0
+| | | new file mode 100644
+| | | index 0000000..10a8a9f
+| | | --- /dev/null
+| | | +++ b/file0
+| | | @@ -0,0 +1,9 @@
+| | | +1
+| | | +2
+| | | +3
+| | | +4
+| | | +5
+| | | +6
+| | | +A
+| | | +B
+| | | +C
Signature · 1 line
2.9.2.872.g367ebef.dirty
#11 lore raw

PATCH v5 3/3 diff: add SUBMODULE_DIFF format to display submodule diff

In reply to Jacob Keller

From: Jacob Keller <[email protected]>

Teach git-diff and friends a new format for displaying the difference of a submodule using git-diff inside the submodule project. This allows users to easily see exactly what source changed in a given commit that updates the submodule pointer. To do this, remove DIFF_SUBMODULE_LOG bit from the diff options, and instead add a new enum type for these formats.

Add some tests for the new format and option.

---
Major changes in v5 after adding tests, I will try to give an overview. Unfortunately an interdiff is not easy as I do not remember which reflog entry was v4 and it's been many edits, so I haven't bothered to dig enough yet.

- v5
* reword "can be tweaked" text in the diff options
* remove unused variables
* print the submodule contains untracked or modified header piece
* print (new submodule) and (deleted submodule)
* handle case where the submodule is not initialized at all, and thus has no .git/modules/xyz, making a diff impossible
* handle the git directory better, plus comments explaining
* handle modified files better. Untracked unfortunately is more difficult. Suggestions for this welcome.
* handle null sha1 correctly (replace with empty tree)

I also added tests, mostly for diff output. These should be viewed carefully since it's possible I didn't check the diff output very carefully. Also, I haven't yet added a test for --graph -p mode yet, as I haven't found a suitable place for it to be located.

 Documentation/diff-config.txt                |   3 +-
 Documentation/diff-options.txt               |   7 +-
 diff.c                                       |  41 +-
 diff.h                                       |   9 +-
 submodule.c                                  | 130 +++++
 submodule.h                                  |   6 +
 t/t4059-diff-submodule-option-diff-format.sh | 738 +++++++++++++++++++++++++++
 7 files changed, 915 insertions(+), 19 deletions(-)
 create mode 100755 t/t4059-diff-submodule-option-diff-format.sh
Documentation/diff-config.txt +2 -1
index d5a5b17d5088..f5d693afad6c 100644
@@ -123,7 +123,8 @@ diff.suppressBlankEmpty::
 diff.submodule::
 	Specify the format in which differences in submodules are
 	shown.  The "log" format lists the commits in the range like
-	linkgit:git-submodule[1] `summary` does.  The "short" format
+	linkgit:git-submodule[1] `summary` does.  The "diff" format shows the
+	diff between the beginning and end of the range. The "short" format
 	format just shows the names of the commits at the beginning
 	and end of the range.  Defaults to short.
 
Documentation/diff-options.txt +5 -2
index f924f57d4f62..9a8e86ba1477 100644
@@ -215,8 +215,11 @@ any of those replacements occurred.
 	the commits in the range like linkgit:git-submodule[1] `summary` does.
 	Omitting the `--submodule` option or specifying `--submodule=short`,
 	uses the 'short' format. This format just shows the names of the commits
-	at the beginning and end of the range.  Can be tweaked via the
-	`diff.submodule` configuration variable.
+	at the beginning and end of the range. When `--submodule=diff` is
+	given, the 'diff' format is used. This format shows the diff between
+	the old and new submodule commmit from the perspective of the
+	submodule.  Defaults to `diff.submodule` or 'short' if the config
+	option is unset.
 
 --color[=<when>]::
 	Show colored diff.
diff.c +26 -15

72 lines not on this page · raw message

diff.h +8 -1
index 83d0b1ae8580..abaa9a49c036 100644
@@ -83,7 +83,7 @@ typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data)
 #define DIFF_OPT_DIRSTAT_BY_FILE     (1 << 20)
 #define DIFF_OPT_ALLOW_TEXTCONV      (1 << 21)
 #define DIFF_OPT_DIFF_FROM_CONTENTS  (1 << 22)
-#define DIFF_OPT_SUBMODULE_LOG       (1 << 23)
+/* (1 << 23) unused */
 #define DIFF_OPT_DIRTY_SUBMODULES    (1 << 24)
 #define DIFF_OPT_IGNORE_UNTRACKED_IN_SUBMODULES (1 << 25)
 #define DIFF_OPT_IGNORE_DIRTY_SUBMODULES (1 << 26)
@@ -110,6 +110,12 @@ enum diff_words_type {
 	DIFF_WORDS_COLOR
 };
 
+enum diff_submodule_format {
+	DIFF_SUBMODULE_SHORT = 0,
+	DIFF_SUBMODULE_LOG,
+	DIFF_SUBMODULE_DIFF
+};
+
 struct diff_options {
 	const char *orderfile;
 	const char *pickaxe;
@@ -156,6 +162,7 @@ struct diff_options {
 	int stat_count;
 	const char *word_regex;
 	enum diff_words_type word_diff;
+	enum diff_submodule_format submodule_format;
 
 	/* this is set by diffcore for DIFF_FORMAT_PATCH */
 	int found_changes;
submodule.c +130 -0

136 lines not on this page · raw message

submodule.h +6 -0
index 2af939099819..f32a25c667ab 100644
@@ -41,6 +41,12 @@ int parse_submodule_update_strategy(const char *value,
 		struct submodule_update_strategy *dst);
 const char *submodule_strategy_to_string(const struct submodule_update_strategy *s);
 void handle_ignore_submodules_arg(struct diff_options *diffopt, const char *);
+void show_submodule_diff(FILE *f, const char *path,
+		const char *line_prefix,
+		unsigned char one[20], unsigned char two[20],
+		unsigned dirty_submodule, const char *meta,
+		const char *a_prefix, const char *b_prefix,
+		const char *reset);
 void show_submodule_summary(FILE *f, const char *path,
 		const char *line_prefix,
 		unsigned char one[20], unsigned char two[20],
t/t4059-diff-submodule-option-diff-format.sh new file +738 -0

738 lines not on this page · raw message

Signature · 1 line
2.9.2.872.g367ebef.dirty
#2 lore raw

Re: [PATCH v5 2/3] diff: add --diff-line-prefix option for passing in a prefix

In reply to Jacob Keller
Quoted 10 lines · Jacob Keller

The prefix shall come first prior to any other prefix associated with the --graph option or other source.

Add tests for the same.

---
- v5
* Changed name to --diff-line-prefix since --line-prefix might indicate for other commands such as log, when it only modifies diff output

As you are adding this to "diff.c", I think the option would be visible to "git log" anyway, and more importantly

    git log --line-prefix='I I ' --graph --boundary -p HEAD^..HEAD

should honor the line-prefix for the log message part. I'd expect that its output would be like:

I I * commit 3c90ffd2f01e2d0d080c8e42df2ee89709b324de

I I | Author: Jacob Keller <[email protected]>
I I | Date:   Thu Aug 11 15:59:45 2016 -0700
I I | 
I I |     diff: add --diff-line-prefix option for passing in a prefix
I I |     
I I |     Add an option to pass additional prefix to be displayed before diff
I I |     output. This feature will be used in a following patch to output correct
I I |     ...
I I |     Signed-off-by: Jacob Keller <[email protected]>
I I |     Signed-off-by: Junio C Hamano <[email protected]>
I I | 
I I | diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
I I | index 705a873..f924f57 100644
I I | --- a/Documentation/diff-options.txt
I I | +++ b/Documentation/diff-options.txt
I I | ...

Otherwise, a "git log --graph -p --submodule=log-with-diff", when showing a commit in the superproject that changes submodule from commit A to commit B, wouldn't be able to run "git log --graph -p A..B", i.e. as if the command recursed beyond the module boundary, would it?

#3 lore raw

Re: [PATCH v5 2/3] diff: add --diff-line-prefix option for passing in a prefix

Quoted 39 lines · Junio C Hamano
Quoted 10 lines · Jacob Keller

The prefix shall come first prior to any other prefix associated with the --graph option or other source.

Add tests for the same.

---
- v5
* Changed name to --diff-line-prefix since --line-prefix might indicate for other commands such as log, when it only modifies diff output

As you are adding this to "diff.c", I think the option would be visible to "git log" anyway, and more importantly

    git log --line-prefix='I I ' --graph --boundary -p HEAD^..HEAD

should honor the line-prefix for the log message part. I'd expect that its output would be like:

I I * commit 3c90ffd2f01e2d0d080c8e42df2ee89709b324de

I I | Author: Jacob Keller <[email protected]>
I I | Date:   Thu Aug 11 15:59:45 2016 -0700
I I |
I I |     diff: add --diff-line-prefix option for passing in a prefix
I I |
I I |     Add an option to pass additional prefix to be displayed before diff
I I |     output. This feature will be used in a following patch to output correct
I I |     ...
I I |     Signed-off-by: Jacob Keller <[email protected]>
I I |     Signed-off-by: Junio C Hamano <[email protected]>
I I |
I I | diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
I I | index 705a873..f924f57 100644
I I | --- a/Documentation/diff-options.txt
I I | +++ b/Documentation/diff-options.txt
I I | ...
Quoted 6 lines

Otherwise, a "git log --graph -p --submodule=log-with-diff", when showing a commit in the superproject that changes submodule from commit A to commit B, wouldn't be able to run "git log --graph -p A..B", i.e. as if the command recursed beyond the module boundary, would it?

Hmmmm, yes. I'll dig through and see if I can get the prefix to be honored by the git log as well.

Thanks,
Jake

Quoted 2 lines
#4 lore raw

Re: [PATCH v5 2/3] diff: add --diff-line-prefix option for passing in a prefix

Quoted 8 lines · Jacob Keller
Quoted 6 lines · Junio C Hamano

Otherwise, a "git log --graph -p --submodule=log-with-diff", when showing a commit in the superproject that changes submodule from commit A to commit B, wouldn't be able to run "git log --graph -p A..B", i.e. as if the command recursed beyond the module boundary, would it?

Quoted 5 lines

Hmmmm, yes. I'll dig through and see if I can get the prefix to be honored by the git log as well.

Thanks,
Jake

Ok so the big problem here is that unlike with diff which has support for line-prefixes already due to needing it for graph mode, I can't figure out where to get the prefix added for log output. Especially if we want it to be honored prior to the graph output. This seems like a much bigger task, which is better saved for future maybe?

Thanks,
Jake

#7 lore raw

Re: [PATCH v5 2/3] diff: add --diff-line-prefix option for passing in a prefix

In reply to a message not in the archive
Quoted 9 lines · Jacob Keller
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Jacob Keller

Ok so the big problem here is that unlike with diff which has support for line-prefixes already due to needing it for graph mode, I can't figure out where to get the prefix added for log output.

Doesn't the --graph mode show the graph lines already? I think most if not all of that processing happens inside log-tree.c::show_log().

Quoted 7 lines

Yes but the problem is how to get this additional prefix to be displayed for

(a) non graph mode
(b) for graph mode but display the prefix before the graph prefix

It doesn't seem straight forward, and I haven't figured out how the graph code already works.

Because you only need "diff --line-prefix" to work while leaving "log --line-prefix" broken for the purpose of your immediate purpose to update "diff/log [--graph and other options] --submodule=<type>" to show "diff A B" in the submodule, I think it is OK to leave it broken, as long as it is clearly documented that "--line-prefix" should also apply to the log message part but the current code doesn't. Eventually, when somebody wants to add a new <type> that runs "log -p A..B" instead of "diff A B" in submodules and wants to make it interact well with "log --graph" at the superproject level, they need to fix the breakage you leave behind. That would be much better than "because I cannot figure out how to prefix on the log part, I'll name this --diff-line-prefix", which will force us to support that half-baked option forever while having to add a proper "--line-prefix" eventually anyway.

#8 lore raw

Re: [PATCH v5 2/3] diff: add --diff-line-prefix option for passing in a prefix

Quoted 19 lines · Junio C Hamano
Quoted 9 lines · Jacob Keller
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Jacob Keller

Ok so the big problem here is that unlike with diff which has support for line-prefixes already due to needing it for graph mode, I can't figure out where to get the prefix added for log output.

Doesn't the --graph mode show the graph lines already? I think most if not all of that processing happens inside log-tree.c::show_log().

Quoted 7 lines

Yes but the problem is how to get this additional prefix to be displayed for

(a) non graph mode
(b) for graph mode but display the prefix before the graph prefix

It doesn't seem straight forward, and I haven't figured out how the graph code already works.

Quoted 14 lines

Because you only need "diff --line-prefix" to work while leaving "log --line-prefix" broken for the purpose of your immediate purpose to update "diff/log [--graph and other options] --submodule=<type>" to show "diff A B" in the submodule, I think it is OK to leave it broken, as long as it is clearly documented that "--line-prefix" should also apply to the log message part but the current code doesn't. Eventually, when somebody wants to add a new <type> that runs "log -p A..B" instead of "diff A B" in submodules and wants to make it interact well with "log --graph" at the superproject level, they need to fix the breakage you leave behind. That would be much better than "because I cannot figure out how to prefix on the log part, I'll name this --diff-line-prefix", which will force us to support that half-baked option forever while having to add a proper "--line-prefix" eventually anyway.

I will look more into how to do the log version tomorrow, if I am still stuck I will re-work the patches as you suggest here.

I am hoping I can find a good solution for how to handle it though.

Thanks,
Jake

#9 lore raw

Re: [PATCH v5 2/3] diff: add --diff-line-prefix option for passing in a prefix

Quoted 4 lines · Jacob Keller

I will look more into how to do the log version tomorrow, if I am still stuck I will re-work the patches as you suggest here.

I am hoping I can find a good solution for how to handle it though.

Thanks, I am hoping the same, too ;-)

#10 lore raw

Re: [PATCH v5 2/3] diff: add --diff-line-prefix option for passing in a prefix

Quoted 8 lines · Junio C Hamano
Quoted 4 lines · Jacob Keller

I will look more into how to do the log version tomorrow, if I am still stuck I will re-work the patches as you suggest here.

I am hoping I can find a good solution for how to handle it though.

Thanks, I am hoping the same, too ;-)

Ok, so I found a way that (ab)uses the graph API to enable line-prefix support. I moved the way we handle diff prefix into the graph callback handler as well, and I also moved around some code so that the graph API will work correctly. I found several places that actually broke if you tried calling the graph API with a null graph, because they would still try to access the FILE* pointer inside and could lead to a segfault. This should fix those up as well.

Hopefully this is along the lines of what you were thinking, and it should allow more advanced calls to a submodule format that shows the full log if we wanted.

Thanks,
Jake