Skip to contentSkip to conversation

diff: add --line-prefix option for passing in a prefix

PATCH In review Jacob Keller 3 participants 8 messages Aug 10, 2016 – Aug 11, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 diff: add --line-prefix option for passing in a prefix

From: Jacob Keller <[email protected]>

This will be used by a future patch which implements a diff mode for submodule display. Without this, the diff output would incorrectly display when using both -p and --graph during a git-log.

Note that the --line-prefix will be displayed first prior to any other output prefix.

---
 Documentation/diff-options.txt |  3 +++
 diff.c                         | 13 ++++++++++++-
 diff.h                         |  1 +
 3 files changed, 16 insertions(+), 1 deletion(-)
Documentation/diff-options.txt +3 -0
index 705a87394200..e7b729f3644f 100644
@@ -569,5 +569,8 @@ endif::git-format-patch[]
 --no-prefix::
 	Do not show any source or destination prefix.
 
+--line-prefix=<prefix>::
+	Prepend an additional prefix to every diff output line.
+
 For more detailed explanation on these common options, see also
 linkgit:gitdiffcore[7].
diff.c +12 -1
index b43d3dd2ecb7..6fa9668b19e5 100644
@@ -1167,10 +1167,17 @@ 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->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 +3973,10 @@ int diff_opt_parse(struct diff_options *options,
 		options->a_prefix = optarg;
 		return argcount;
 	}
+	else if ((argcount = parse_long_opt("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 125447be09eb..6a91a1139686 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;
 
Signature · 1 line
2.9.2.872.g2dc3c84
#1 lore raw

PATCH v4 2/2 diff: add SUBMODULE_DIFF format to display submodule diff

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.

---
 Documentation/diff-config.txt  |  3 +-
 Documentation/diff-options.txt |  6 ++--
 diff.c                         | 41 ++++++++++++++++----------
 diff.h                         |  9 +++++-
 submodule.c                    | 67 ++++++++++++++++++++++++++++++++++++++++++
 submodule.h                    |  6 ++++
 6 files changed, 113 insertions(+), 19 deletions(-)
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 +4 -2
index e7b729f3644f..988068225463 100644
@@ -215,8 +215,10 @@ 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.  Can be tweaked via the `diff.submodule` configuration variable.
 
 --color[=<when>]::
 	Show colored diff.
diff.c +26 -15

72 lines not on this page · raw message

diff.h +8 -1
index 6a91a1139686..3110a2afb3d6 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 +67 -0

73 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],
Signature · 1 line
2.9.2.872.g2dc3c84
#5 lore raw
In reply to Jacob Keller
Quoted 7 lines · Jacob Keller

const char *diff_line_prefix(struct diff_options *opt) {

(quoted diff) +2 -1
 	struct strbuf *msgbuf;
+
 	if (!opt->output_prefix)
-		return "";
+		if (opt->line_prefix)

Review

Quoted 10 lines
(quoted diff) +6 -0
+			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;
 }

The result of applying this change leaves the semantics of the line_prefix field, the output_prefix() callback, and the output_prefix_length field in the diff_options structure a bit tricky to reason about.

The code pretends as if the remainder of the system does not even care about the presence of line_prefix (i.e. output_prefix_length is not updated), but its only user of output_prefix_length cares, I think. It is in diff.c::show_stats() where it auto-computes the allowed display width for the stat portion:

        if (options->stat_width == -1)
                width = term_columns() - options->output_prefix_length;

The output_prefix_length is initialized to be 0, but when --graph is in effect, it is set to the width of the graph portion of the output in the output_prefix callback, diff_output_prefix_callback().

So the above change is clearly wrong in that it needs to add the number of display columns needed to show opt->line_prefix to the output_prefix_length, but I wonder if a better "fix" to this is to get rid of output_prefix_length field from diff_options struct as a preparatory step. That would make the bug in this patch disappear.

Perhaps like this. I do not know if Lucian is still interested in, or remembers what did for, Git in 2012, but this updates his code and replaces it with what I hope is an equivalent, so I added him to the Cc: line.

-- >8 --
Subject: diff.c: remove output_prefix_length field

"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.

---

 diff.c  | 2 +-
 diff.h  | 1 -
 graph.c | 2 --
 3 files changed, 1 insertion(+), 4 deletions(-)
diff.c +1 -1
index b43d3dd..ae069c3 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 125447b..49e4aaa 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 dd17201..a468038 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;
 }
#2 lore raw

Re: [PATCH v4 2/2] diff: add SUBMODULE_DIFF format to display submodule diff

In reply to Jacob Keller
Quoted 10 lines · 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.

Quoted 8 lines
---
 Documentation/diff-config.txt  |  3 +-
 Documentation/diff-options.txt |  6 ++--
 diff.c                         | 41 ++++++++++++++++----------
 diff.h                         |  9 +++++-
 submodule.c                    | 67 ++++++++++++++++++++++++++++++++++++++++++
 submodule.h                    |  6 ++++
 6 files changed, 113 insertions(+), 19 deletions(-)

This looks good.

You'd want some tests to make sure that the "--submodule" and the "--submodule=<format>" command line options and the diff.submodule configuration variables are parsed correctly (and when combined, the command line option overrides the configured default), and of course the machinery does the right thing, with and without "--graph" when used in "git log".

Quoted 5 lines
Documentation/diff-options.txt +0 -0
index e7b729f3644f..988068225463 100644

Documentation/diff-options.txt any of those replacements occurred. · line 221

Quoted 9 lines
Documentation/diff-options.txt +4 -2
@@ -215,8 +215,10 @@ 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.  Can be tweaked via the `diff.submodule` configuration variable.

This is carried over from the existing text, but "Can be tweaked via" sounds a bit wasteful (and strange); "Defaults to" (or "The default is taken from") is the phrase we seem to use more often.

Thanks.

#6 lore raw
In reply to Junio C Hamano
Quoted 13 lines · Junio C Hamano

-- >8 --
Subject: diff.c: remove output_prefix_length field

"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.

Quoted 7 lines

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.

Seems correct to me.

Thanks,
Jake

#3 lore raw

Re: [PATCH v4 2/2] diff: add SUBMODULE_DIFF format to display submodule diff

In reply to Junio C Hamano
Quoted 21 lines · Junio C Hamano
Quoted 10 lines · 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.

Quoted 8 lines
---
 Documentation/diff-config.txt  |  3 +-
 Documentation/diff-options.txt |  6 ++--
 diff.c                         | 41 ++++++++++++++++----------
 diff.h                         |  9 +++++-
 submodule.c                    | 67 ++++++++++++++++++++++++++++++++++++++++++
 submodule.h                    |  6 ++++
 6 files changed, 113 insertions(+), 19 deletions(-)
Quoted 8 lines

This looks good.

You'd want some tests to make sure that the "--submodule" and the "--submodule=<format>" command line options and the diff.submodule configuration variables are parsed correctly (and when combined, the command line option overrides the configured default), and of course the machinery does the right thing, with and without "--graph" when used in "git log".

Yes, I am adding tests now, but I ran into some interesting corner cases for this, that still need some work.

There's a bunch of issues with cases involving adding a submodule that isn't stored in .git/modules/etc, which the current tests for --submodule=log do.

There's also the case of empty trees, which I believe I have resolved now. Hopefully I can sort these cases correctly.

Quoted 11 lines
Quoted 11 lines
Documentation/diff-options.txt +0 -2
index e7b729f3644f..988068225463 100644
@@ -215,8 +215,10 @@ 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.
Quoted 8 lines

Documentation/diff-options.txt any of those replacements occurred. · line 221

Quoted 4 lines
Documentation/diff-options.txt +4 -0
+     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.  Can be tweaked via the `diff.submodule` configuration variable.

This is carried over from the existing text, but "Can be tweaked via" sounds a bit wasteful (and strange); "Defaults to" (or "The default is taken from") is the phrase we seem to use more often.

Probably worth fixing here. WIll do.

Thanks,
Jake

Quoted 2 lines

Thanks.

#4 lore raw

Re: [PATCH v4 2/2] diff: add SUBMODULE_DIFF format to display submodule diff

In reply to Jacob Keller
Quoted 12 lines · Jacob Keller
submodule.c +4 -0
index 1b5cdfb7e784..36f8fd00c3ce 100644
@@ -333,6 +333,73 @@ static void print_submodule_summary(struct rev_info *rev, FILE *f,
 	strbuf_release(&sb);
 }
 
+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,

submodule.c static void print_submodule_summary(struct rev_info *rev, FILE *f, · line 349

Quoted 10 lines
submodule.c +10 -0
+		const char *a_prefix, const char *b_prefix,
+		const char *reset)
+{
+	struct child_process cp = CHILD_PROCESS_INIT;
+	struct strbuf sb = STRBUF_INIT;
+	struct strbuf submodule_git_dir = STRBUF_INIT;
+	const char *git_dir, *message = NULL;
+	int create_dirty_diff = 0;
+	FILE *diff;
+	char c;

The variables message, diff and c are not used, are they?

#7 lore raw
In reply to Jacob Keller

Subject: SQUASH??? clarify the if/{if/else} nesting

Otherwise GCC helpfully warns you.

---
 diff.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff.c +2 -1
index bfc0a6b..d128b9d 100644
@@ -1170,11 +1170,12 @@ const char *diff_line_prefix(struct diff_options *opt)
 {
 	struct strbuf *msgbuf;
 
-	if (!opt->output_prefix)
+	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() */
Signature · 1 line
2.9.2-863-g71ed253