Add support for displaying submodules as a proper diff
v6 cover Add support for displaying submodules as a proper diff
From: Jacob Keller <[email protected]>
This patch series adds support for displaying a submodule as a difference between the pre and post commits. This allows projects who frequently update submodule contents to view the submodule in the log as if it were just one squashed commit that updates all the files in the submodule together, even if you mix-match the submodule part with a regular part you just see a complete diff that shows all the changes.
To make this work, I also extended the graph-aware output with a --line-prefix option. This option extends both diff and log to show a prefix. It's a bit of a hack in someways, but it works for showing a prefix both when graph is enabled and when its not. I think it works quite well.
I added several tests to the line-prefix, and also a few tests for the new submodule format.
I welcome comments on how to improve the graph line-prefix code, as well as the actual submodule diff format.
Jacob Keller (2):
graph: add support for --line-prefix on all graph-aware output
diff: add SUBMODULE_DIFF format to display submodule diff
Junio C Hamano (1):
diff.c: remove output_prefix_length field
Documentation/diff-config.txt | 3 +- Documentation/diff-options.txt | 10 +- builtin/rev-list.c | 70 +- diff.c | 49 +- diff.h | 11 +- graph.c | 105 +-- graph.h | 22 +- log-tree.c | 5 +- submodule.c | 130 ++++ submodule.h | 6 + t/t4013-diff-various.sh | 6 + ...diff.diff_--line-prefix=abc_master_master^_side | 29 + t/t4013/diff.diff_--line-prefix_--cached_--_file0 | 15 + t/t4059-diff-submodule-option-diff-format.sh | 738 +++++++++++++++++++++ t/t4202-log.sh | 323 +++++++++ 15 files changed, 1419 insertions(+), 103 deletions(-) create mode 100644 t/t4013/diff.diff_--line-prefix=abc_master_master^_side create mode 100644 t/t4013/diff.diff_--line-prefix_--cached_--_file0 create mode 100755 t/t4059-diff-submodule-option-diff-format.sh
Signature · 1 line
2.9.2.873.g47c31b4
PATCH v6 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.
--- diff.c | 2 +- diff.h | 1 - graph.c | 2 -- 3 files changed, 1 insertion(+), 4 deletions(-)
diff.c +1 -1
@@ -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
@@ -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
@@ -197,7 +197,6 @@ static struct strbuf *diff_output_prefix_callback(struct diff_options *opt, voidassert (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.873.g47c31b4
Re: [PATCH v6 1/3] diff.c: remove output_prefix_length field
Quoted 1 line · Jacob Keller
From: Junio C Hamano <[email protected]>
Thanks. I had quite a many typoes in this one.
Quoted 7 lines
"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(Addoutput_prefix_lengthtodiff_options, 2012-04-16) added theoutput_prefix_lengthfield todiff_optionsstructure to allow this logic subtract the display columns used for display the
s/^.*$/allow this logic to subtract the display columns used for the/
Quoted 4 lines
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
s/field be/field must be/
Quoted 2 lines
output from the
output_prefix()callback. Any newoutput_prefix()callback must also update the field accordingly, which is error
"output from the output_prefix() callback, which is error"
Quoted 1 line
prone.
No need to resend; I had it with the above fix in 'pu' for a few days already ;-)
Re: [PATCH v6 1/3] diff.c: remove output_prefix_length field
Quoted 9 lines · Junio C Hamano
Quoted 1 line · Jacob Keller
From: Junio C Hamano <[email protected]>
Thanks. I had quite a many typoes in this one.
No need to resend; I had it with the above fix in 'pu' for a few days already ;-)
Thanks,
Jake
PATCH v6 2/3 graph: add support for --line-prefix on all graph-aware output
In reply to Jacob KellerFrom: Jacob Keller <[email protected]>
Add an extension to git-diff and git-log (and any other graph-aware displayable output) such that "--line-prefix=<string>" will print the additional line-prefix on every line of output.
To make this work, we have to fix a few bugs in the graph API that force graph_show_commit_msg to be used only when you have a valid graph. Additionally, we extend the default_diff_output_prefix handler to work even when no graph is enabled.
This is somewhat of a hack on top of the graph API, but I think it should be acceptable here.
This will be used by a future extension of submodule display which displays the submodule diff as the actual diff between the pre and post commit in the submodule project.
Add some tests for both git-log and git-diff to ensure that the prefix is honored correctly.
--- Documentation/diff-options.txt | 3 + builtin/rev-list.c | 70 ++--- diff.c | 6 + diff.h | 1 + graph.c | 103 ++++--- graph.h | 22 +- log-tree.c | 5 +- t/t4013-diff-various.sh | 6 + ...diff.diff_--line-prefix=abc_master_master^_side | 29 ++ t/t4013/diff.diff_--line-prefix_--cached_--_file0 | 15 + t/t4202-log.sh | 323 +++++++++++++++++++++ 11 files changed, 503 insertions(+), 80 deletions(-) create mode 100644 t/t4013/diff.diff_--line-prefix=abc_master_master^_side create mode 100644 t/t4013/diff.diff_--line-prefix_--cached_--_file0
Documentation/diff-options.txt +3 -0
@@ -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 line of output. + For more detailed explanation on these common options, see also linkgit:gitdiffcore[7].
builtin/rev-list.c +31 -39
79 lines not on this page · raw message
diff.c +6 -0
@@ -18,6 +18,7 @@#include "ll-merge.h" #include "string-list.h" #include "argv-array.h" +#include "graph.h" #ifdef NO_FAST_WORKING_DIRECTORY #define FAST_WORKING_DIRECTORY 0
@@ -3966,6 +3967,11 @@ 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; +graph_setup_line_prefix (options); +return argcount; + }else if ((argcount =parse_long_opt ("dst-prefix" , av, &optarg))) { options->b_prefix = optarg;return argcount;
diff.h +1 -0
@@ -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;
graph.c +67 -36
215 lines not on this page · raw message
graph.h +21 -1
@@ -1,9 +1,22 @@#ifndef GRAPH_H #define GRAPH_H +#include "diff.h" /* A graph is a pointer to this opaque structure */ struct git_graph ; +/* +* Called to setup global display of line_prefix diff option. +* +* Passed a diff_options structure which indicates the line_prefix and the +* file to output the prefix to. This is sort of a hack used so that the +* line_prefix will be honored by all flows which also honor "--graph" +* regardless of whether a graph has actually been setup. The normal graph +* flow will honor the exact diff_options passed, but a NULL graph will cause +* display of a line_prefix to stdout. +*/ +void graph_setup_line_prefix (struct diff_options *diffopt); +/* * Set up a custom scheme for column colors. *
@@ -113,7 +126,14 @@ int graph_show_remainder(struct git_graph *graph);* missing a terminating newline (including if it is empty), the output * printed by graph_show_commit_msg() will also be missing a terminating * newline. +* +* Note that unlike some other graph display functions, you must pass the file +* handle directly. It is assumed that this is the same file handle as the +* file specified by the graph diff options. This is necessary so that +* graph_show_commit_msg can be called even with a NULL graph. */ -void graph_show_commit_msg (struct git_graph *graph,struct strbuf const *sb); +void graph_show_commit_msg (struct git_graph *graph, +FILE *file, +struct strbuf const *sb);#endif /* GRAPH_H */
log-tree.c +1 -4
@@ -715,10 +715,7 @@ void show_log(struct rev_info *opt)else opt->missing_newline =0 ; -if (opt->graph) -graph_show_commit_msg (opt->graph, &msgbuf); -else -fwrite (msgbuf.buf,sizeof (char ), msgbuf.len, opt->diffopt.file); +graph_show_commit_msg (opt->graph, opt->diffopt.file, &msgbuf);if (opt->use_terminator && !commit_format_is_empty (opt->commit_format)) {if (!opt->missing_newline)graph_show_padding (opt->graph);
t/t4013-diff-various.sh +6 -0
@@ -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 --line-prefix=abc 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 --line-prefix with spaces' ' + git diff --line-prefix="| | | " --cached -- file0 >result && + test_cmp "$TEST_DIRECTORY/t4013/diff.diff_--line-prefix_--cached_--_file0" result +' test_expect_success 'diff-tree --stdin with log formatting' ' cat >expect <<-\EOF &&
t/t4013/diff.diff_--line-prefix=abc_master_master^_side new file +29 -0
@@ -0,0 +1,29 @@ +$ git diff --line-prefix=abc master master^ side +abcdiff --cc dir/sub +abcindex cead32e,7289e35..992913c +abc--- a/dir/sub +abc+++ b/dir/sub +abc@@@ -1,6 -1,4 +1,8 @@@ +abc A +abc B +abc +C +abc +D +abc +E +abc +F +abc+ 1 +abc+ 2 +abcdiff --cc file0 +abcindex b414108,f4615da..10a8a9f +abc--- a/file0 +abc+++ b/file0 +abc@@@ -1,6 -1,6 +1,9 @@@ +abc 1 +abc 2 +abc 3 +abc +4 +abc +5 +abc +6 +abc+ A +abc+ B +abc+ C +$
t/t4013/diff.diff_--line-prefix_--cached_--_file0 new file +15 -0
@@ -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
t/t4202-log.sh +323 -0
347 lines not on this page · raw message
Signature · 1 line
2.9.2.873.g47c31b4
Re: [PATCH v6 2/3] graph: add support for --line-prefix on all graph-aware output
Quoted 7 lines · Jacob Keller
To make this work, we have to fix a few bugs in the graph API that force
graph_show_commit_msgto be used only when you have a valid graph. Additionally, we extend thedefault_diff_output_prefixhandler to work even when no graph is enabled.This is somewhat of a hack on top of the graph API, but I think it should be acceptable here.
Unlike the opt-prefix-length I removed in 1/3, the length of the line-prefix will never change during the lifetime of a single diff_options struct, so it might turn out that repeated strlen() on it for each and every line output is wasteful.
Other than that, I didn't spot anything immediately questionable.
Thanks.
Re: [PATCH v6 2/3] graph: add support for --line-prefix on all graph-aware output
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Jacob Keller
To make this work, we have to fix a few bugs in the graph API that force
graph_show_commit_msgto be used only when you have a valid graph. Additionally, we extend thedefault_diff_output_prefixhandler to work even when no graph is enabled.
Quoted 8 lines
Quoted 2 lines
This is somewhat of a hack on top of the graph API, but I think it should be acceptable here.
Unlike the opt-prefix-length I removed in 1/3, the length of the line-prefix will never change during the lifetime of a single
diff_optionsstruct, so it might turn out that repeatedstrlen()on it for each and every line output is wasteful.
I could change this to store the length at option initialization, probably a good idea. Would it be better to just store it as a strbuf, since these know their length already? Or just add a line_prefix_length field? I agree that calling strlen a lot is probably wasteful.
Quoted 2 lines
Other than that, I didn't spot anything immediately questionable.
Thanks. I definitely feel like this is somewhat abusing the graph API because we're adding something not exactly graph-related. But in some ways it really is graph related as well, so I think it makes some sense.
Regards,
Jake
Quoted 1 line
Thanks.
PATCH v6 3/3 diff: add SUBMODULE_DIFF format to display submodule diff
In reply to Jacob KellerFrom: 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.
--- 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
@@ -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
@@ -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
@@ -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
@@ -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.873.g47c31b4
Re: [PATCH v6 3/3] diff: add SUBMODULE_DIFF format to display submodule diff
Documentation/diff-config.txt diff.suppressBlankEmpty:: · line 129
Quoted 13 lines · Jacob Keller
Documentation/diff-config.txt +2
-1@@ -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.
It would be much better to describe the default one first and then more involved one next, no? That would also match what the change to "diff-options" in this patch does (can be seen below).
Quoted 20 lines
Documentation/diff-options.txt +5
-2@@ -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.@@ -2311,6 +2322,15 @@ static void builtin_diff(const char *name_a, two->dirty_submodule, meta, del, add, reset); return;
Documentation/diff-options.txt static void builtin_diff(const char *name_a, · line 2334
Quoted 10 lines
Documentation/diff-options.txt +9
-0+ } else if (o->submodule_format == DIFF_SUBMODULE_DIFF && + (!one->mode || S_ISGITLINK(one->mode)) && + (!two->mode || S_ISGITLINK(two->mode))) { + show_submodule_diff(o->file, one->path ? one->path : two->path, + line_prefix, + one->oid.hash, two->oid.hash, + two->dirty_submodule, + meta, a_prefix, b_prefix, reset); + return; }
The "either missing or is submodule" check used here is being consistent with the existing "submodule=log" case. Good.
Quoted 30 lines
submodule.c +22
-0@@ -333,6 +333,136 @@ 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, +const char *a_prefix,const char *b_prefix, +const char *reset) +{ +struct strbuf submodule_git_dir =STRBUF_INIT , sb =STRBUF_INIT ; +struct child_process cp =CHILD_PROCESS_INIT ; +const char *git_dir; + +if (dirty_submodule &DIRTY_SUBMODULE_UNTRACKED ) { +fprintf (f,"%sSubmodule %s contains untracked content\n" , + line_prefix, path); + } +if (dirty_submodule &DIRTY_SUBMODULE_MODIFIED ) { +fprintf (f,"%sSubmodule %s contains modified content\n" , + line_prefix, path); + } + + strbuf_addf(&sb, "%s%sSubmodule %s %s..", + line_prefix, meta, path,
submodule.c static void print_submodule_summary(struct rev_info *rev, FILE *f, · line 367
Quoted 10 lines
submodule.c +10
-0+ find_unique_abbrev(one, DEFAULT_ABBREV)); +strbuf_addf (&sb,"%s:%s" , +find_unique_abbrev (two,DEFAULT_ABBREV ), + reset); +fwrite (sb.buf, sb.len,1 , f); + +if (is_null_sha1 (one)) +fprintf (f," (new submodule)" ); +if (is_null_sha1 (two)) +fprintf (f," (submodule deleted)" );
These messages are in sync with show_submodule_summary() that is used in --submodule=log codepath. Good.
submodule.c static void print_submodule_summary(struct rev_info *rev, FILE *f, · line 376
Quoted 8 lines
submodule.c +8
-0+/* +* We need to determine the most accurate location to call the sub +* command, and handle the various corner cases involved. We'll first +* attempt to use the path directly if the submodule is checked out. +* Then, if that fails, we'll check the standard module location in +* the git directory. If even this fails, it means we can't do the +* lookup because the module has not been initialized. +*/
This is more elaborate than what show_submodule_summary() does, isn't it? Would it make the patch series (and the resulting code) more understandable if you used the same code by refactoring these two? If so, I wonder if it makes sense to split 3/3 into a few separate steps:
* Update the internal "--submodule=<type>" handling without adding the "--submodule=diff" and show_submodule_diff() function.
* Refactor the determination of the submodule status (i.e. does it even have a clone? where is its repository? etc.) from existing show_submodule_summary() into a separate helper function.
* Make that helper function more elaborate like what you do here, and update show_submodule_summary(). I think the state show_submodule_summary() calls "not checked out" corresponds to what you say "not initialized" below, and they should share the same logic to determine that the submodule is in that state, and share the same message, for example.
* Introduce "--submodule=diff", and show_submodule_diff() function; the latter would use the helper function prepared in the previous step.
perhaps?
Quoted 57 lines
submodule.c +57
-0+strbuf_addf (&submodule_git_dir,"%s/.git" , path); + git_dir =resolve_gitdir (submodule_git_dir.buf); +if (git_dir) { +/* +* If we can resolve a git dir, this means that the submodule +* has been checked out. In this case, just use the original +* path since we want access to the work tree +*/ + git_dir = path; + }else { +/* +* If we can't resolve a git dir, this means that the +* submodule has not been checked out. In this case, try the +* standard location for modules +*/ +strbuf_reset (&submodule_git_dir); +strbuf_addf (&submodule_git_dir,"%s/modules/%s" ,get_git_dir (), path); + git_dir =resolve_gitdir (submodule_git_dir.buf); +if (!git_dir) { +/* +* If we failed to find a git directory here, then the +* submodule must not have been initialized. Without +* the initialized contents of the submodule, we won't +* be able to perform the difference. +*/ +fprintf (f," (submodule not initialized)\n" ); + goto out; + } + } + +/* +* print a newline and flush the file so that the diff output appears +* starting on its own line +*/ +fprintf (f,"\n" ); +fflush (f); + + cp.git_cmd =1 ; + cp.dir = git_dir; + cp.out =dup (fileno (f)); + cp.no_stdin =1 ; + +argv_array_push (&cp.args,"diff" ); +argv_array_pushf (&cp.args,"--line-prefix=%s" , line_prefix); +argv_array_pushf (&cp.args,"--src-prefix=%s%s/" , a_prefix, path); +argv_array_pushf (&cp.args,"--dst-prefix=%s%s/" , b_prefix, path); + +if (is_null_sha1 (one)) { +/* +* If the origin commit is null, we want to use the empty tree +* so that we see a diff of all the new contents added. +*/ +argv_array_push (&cp.args,EMPTY_TREE_SHA1_HEX ); + }else { +/* Use the old commit as the diff base */ +argv_array_push (&cp.args,sha1_to_hex (one)); + }
submodule.c static void print_submodule_summary(struct rev_info *rev, FILE *f, · line 443
Quoted 10 lines
submodule.c +10
-0+ +if (dirty_submodule &DIRTY_SUBMODULE_MODIFIED ) { +/* +* If the submodule has modified contents we want to diff +* against the work tree, so don't add a second parameter. +* This is essentially equivalent of using diff-index instead. +* Note that we can't (easily) show the diff of any untracked +* files. +*/ + }else if (is_null_sha1 (two)) {
It is safer to have ';' inside the empty if(){} block to make sure that one empty statement exists there. It makes the intention of the code clearer, too.
I am debating myself if this is a good thing to do, though. The submodule is a separate project for a reason, and there is a reason why the changes haven't been committed. When asking "what's different between these two superproject states?", should the user really see these uncommitted changes?
Thanks.
Re: [PATCH v6 3/3] diff: add SUBMODULE_DIFF format to display submodule diff
Quoted 11 lines · Junio C Hamano
Quoted 9 lines · Jacob Keller
Documentation/diff-config.txt +0
-1@@ -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
Quoted 9 lines
Documentation/diff-config.txt diff.suppressBlankEmpty:: · line 129
Quoted 4 lines
Documentation/diff-config.txt +2
-0+ 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.It would be much better to describe the default one first and then more involved one next, no? That would also match what the change to "diff-options" in this patch does (can be seen below).
The main thing is that "--submodule" alone means "use the log format" so I think that's why it went first. I can reword this to make it more clear how this works.
Thanks,
Jake
Quoted 53 lines
Quoted 4 lines
Documentation/diff-config.txt +0
-0@@ -2311,6 +2322,15 @@ static void builtin_diff(const char *name_a, two->dirty_submodule, meta, del, add, reset); return;Documentation/diff-config.txt static void builtin_diff(const char *name_a, · line 2334
Quoted 10 lines
Documentation/diff-config.txt +9
-0+ } else if (o->submodule_format == DIFF_SUBMODULE_DIFF && + (!one->mode || S_ISGITLINK(one->mode)) && + (!two->mode || S_ISGITLINK(two->mode))) { + show_submodule_diff(o->file, one->path ? one->path : two->path, + line_prefix, + one->oid.hash, two->oid.hash, + two->dirty_submodule, + meta, a_prefix, b_prefix, reset); + return; }The "either missing or is submodule" check used here is being consistent with the existing "submodule=log" case. Good.
Quoted 35 lines
submodule.c +27
-0@@ -333,6 +333,136 @@ 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, +const char *a_prefix,const char *b_prefix, +const char *reset) +{ +struct strbuf submodule_git_dir =STRBUF_INIT , sb =STRBUF_INIT ; +struct child_process cp =CHILD_PROCESS_INIT ; +const char *git_dir; + +if (dirty_submodule &DIRTY_SUBMODULE_UNTRACKED ) { +fprintf (f,"%sSubmodule %s contains untracked content\n" , + line_prefix, path); + } +if (dirty_submodule &DIRTY_SUBMODULE_MODIFIED ) { +fprintf (f,"%sSubmodule %s contains modified content\n" , + line_prefix, path); + } + +strbuf_addf (&sb,"%s%sSubmodule %s %s.." , + line_prefix, meta, path, +find_unique_abbrev (one,DEFAULT_ABBREV )); +strbuf_addf (&sb,"%s:%s" , +find_unique_abbrev (two,DEFAULT_ABBREV ), + reset); +fwrite (sb.buf, sb.len,1 , f);
Quoted 9 lines
submodule.c static void print_submodule_summary(struct rev_info *rev, FILE *f, · line 367
Quoted 5 lines
submodule.c +5
-0+ +if (is_null_sha1 (one)) +fprintf (f," (new submodule)" ); +if (is_null_sha1 (two)) +fprintf (f," (submodule deleted)" );These messages are in sync with
show_submodule_summary()that is used in --submodule=log codepath. Good.
They're not exactly the same due to some ways of splitting up new lines.
Quoted 9 lines
submodule.c static void print_submodule_summary(struct rev_info *rev, FILE *f, · line 376
Quoted 8 lines
submodule.c +8
-0+/* +* We need to determine the most accurate location to call the sub +* command, and handle the various corner cases involved. We'll first +* attempt to use the path directly if the submodule is checked out. +* Then, if that fails, we'll check the standard module location in +* the git directory. If even this fails, it means we can't do the +* lookup because the module has not been initialized. +*/
Quoted 5 lines
This is more elaborate than what
show_submodule_summary()does, isn't it? Would it make the patch series (and the resulting code) more understandable if you used the same code by refactoring these two? If so, I wonder if it makes sense to split 3/3 into a few separate steps:
The show_submodule_summary just uses "add_submodule_odb" which adds the submodule as an alternate source of objects, if I understand correctly.
Quoted 8 lines
* Update the internal "--submodule=<type>" handling without adding the "--submodule=diff" and
show_submodule_diff()function.* Refactor the determination of the submodule status (i.e. does it even have a clone? where is its repository? etc.) from existing
show_submodule_summary()into a separate helper function.
Quoted 6 lines
* Make that helper function more elaborate like what you do here, and update
show_submodule_summary(). I think the stateshow_submodule_summary()calls "not checked out" corresponds to what you say "not initialized" below, and they should share the same logic to determine that the submodule is in that state, and share the same message, for example.
Makes sense, I might squash that with the above if it's easier.
Quoted 7 lines
* Introduce "--submodule=diff", and
show_submodule_diff()function; the latter would use the helper function prepared in the previous step.perhaps?
That makes more sense. I'll rework the series some more.
Quoted 11 lines
submodule.c static void print_submodule_summary(struct rev_info *rev, FILE *f, · line 443
Quoted 10 lines
submodule.c +10
-0+ +if (dirty_submodule &DIRTY_SUBMODULE_MODIFIED ) { +/* +* If the submodule has modified contents we want to diff +* against the work tree, so don't add a second parameter. +* This is essentially equivalent of using diff-index instead. +* Note that we can't (easily) show the diff of any untracked +* files. +*/ + }else if (is_null_sha1 (two)) {
Quoted 4 lines
It is safer to have ';' inside the empty if(){} block to make sure that one empty statement exists there. It makes the intention of the code clearer, too.
Will do.
Quoted 7 lines
I am debating myself if this is a good thing to do, though. The submodule is a separate project for a reason, and there is a reason why the changes haven't been committed. When asking "what's different between these two superproject states?", should the user really see these uncommitted changes?
Thanks.
Well, the previous submodule code for "log" prints out "submodule has untracked content" and "submodule has modified content" so I figured the diff might want to show that as a diff too? Or maybe we just stick with the messages and only show the difference of what's actually been committed.. I think that's probably ok too.
Regards,
Jake
Re: [PATCH v6 3/3] diff: add SUBMODULE_DIFF format to display submodule diff
Quoted 16 lines · Jacob Keller
Quoted 9 lines
submodule.c static void print_submodule_summary(struct rev_info *rev, FILE *f, · line 442
Quoted 9 lines
submodule.c +9
-0+ +if (dirty_submodule &DIRTY_SUBMODULE_MODIFIED ) { +/* +* If the submodule has modified contents we want to diff +* against the work tree, so don't add a second parameter. +* This is essentially equivalent of using diff-index instead. +* Note that we can't (easily) show the diff of any untracked +* files. +*/ Quoted 6 lines
...
I am debating myself if this is a good thing to do, though. The submodule is a separate project for a reason, and there is a reason why the changes haven't been committed. When asking "what's different between these two superproject states?", should the user really see these uncommitted changes?
Quoted 5 lines
Well, the previous submodule code for "log" prints out "submodule has untracked content" and "submodule has modified content" so I figured the diff might want to show that as a diff too? Or maybe we just stick with the messages and only show the difference of what's actually been committed.. I think that's probably ok too.
I do not have a strong opinion. We only see DIRTY when we are looking at the working tree at the top-level diff (i.e. "git diff HEAD~ HEAD" at the top-level, or "git diff --cached" will not show DIRTY_SUBMODULE_MODIFIED when a submodule has local modifications in its working tree), so in that sense, it probably makes sense to show the more detailed picture of the working tree like your patch does. After all, choosing to use --submodule=diff is a strong sign that the user who says top-level "git diff [<tree>]" wants to see the details of her work-in-progress.
Do you need to handle "git diff -R [<tree>]" at the top-level a bit differently, by the way? If this function gets the full diff_options that is driving the top-level diff, the DIFF_OPT_REVERSE_DIFF bit would tell you that.
Re: [PATCH v6 3/3] diff: add SUBMODULE_DIFF format to display submodule diff
Quoted 34 lines · Junio C Hamano
Quoted 16 lines · Jacob Keller
Quoted 9 lines
submodule.c static void print_submodule_summary(struct rev_info *rev, FILE *f, · line 442
Quoted 9 lines
submodule.c +9
-0+ +if (dirty_submodule &DIRTY_SUBMODULE_MODIFIED ) { +/* +* If the submodule has modified contents we want to diff +* against the work tree, so don't add a second parameter. +* This is essentially equivalent of using diff-index instead. +* Note that we can't (easily) show the diff of any untracked +* files. +*/ Quoted 6 lines
...
I am debating myself if this is a good thing to do, though. The submodule is a separate project for a reason, and there is a reason why the changes haven't been committed. When asking "what's different between these two superproject states?", should the user really see these uncommitted changes?Quoted 5 lines
Well, the previous submodule code for "log" prints out "submodule has untracked content" and "submodule has modified content" so I figured the diff might want to show that as a diff too? Or maybe we just stick with the messages and only show the difference of what's actually been committed.. I think that's probably ok too.
I do not have a strong opinion. We only see DIRTY when we are looking at the working tree at the top-level diff (i.e. "git diff HEAD~ HEAD" at the top-level, or "git diff --cached" will not show
DIRTY_SUBMODULE_MODIFIEDwhen a submodule has local modifications in its working tree), so in that sense, it probably makes sense to show the more detailed picture of the working tree like your patch does. After all, choosing to use --submodule=diff is a strong sign that the user who says top-level "git diff [<tree>]" wants to see the details of her work-in-progress.
Quoted 5 lines
Do you need to handle "git diff -R [<tree>]" at the top-level a bit differently, by the way? If this function gets the full
diff_optionsthat is driving the top-level diff, theDIFF_OPT_REVERSE_DIFFbit would tell you that.
Probably. Unfortunately what I really need is to be able to convert (almost) all diff options from the diff_options structure back into command line flags. This is the primary reason I would prefer to not use a sub-command, but I'm not really sure what's the best way to actually DO that in a safe way.
The sub command brings nice separation between the superproject and its submodules... but it has problem because we can't use C calls directly, so I can't pass the options along myself.
Thoughts on that? Or should we just limit ourselves to only some options get propagated to the submodule?
Thanks,
Jake
Re: [PATCH v6 3/3] diff: add SUBMODULE_DIFF format to display submodule diff
Quoted 2 lines · Jacob Keller
Thoughts on that? Or should we just limit ourselves to only some options get propagated to the submodule?
I think you have to be selective either way. You do not want pathspecs used to limit the top-level paths propagated down when you run a diff or a log in the submodule, for example, and that does not change even if you start using the code in diff-lib.c (after adding the submodule odb as an alternate).