@matheusbsbs / matheus-bagliano / commits / 752c0c2492
Fork 0

Add the --submodule option to the diff option family

When you use the option --submodule=log you can see the submodule summaries inlined in the diff, instead of not-quite-helpful SHA-1 pairs. The format imitates what "git submodule summary" shows. To do that, <path>/.git/objects/ is added to the alternate object databases (if that directory exists). This option was requested by Jens Lehmann at the GitTogether in Berlin. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Oct 19, 2009 at 14:38 UTC 752c0c24926aacbceca0d27de6ad22cbb7dd0709
6 files changed +151
Documentation/diff-options.txt
+7
@@ -87,6 +87,13 @@ endif::git-format-patch[]
87 Show only names and status of changed files. See the description
88 of the `--diff-filter` option on what the status letters mean.
89
90 +--submodule[=<format>]::
91 + Chose the output format for submodule differences. <format> can be one of
92 + 'short' and 'log'. 'short' just shows pairs of commit names, this format
93 + is used when this option is not given. 'log' is the default value for this
94 + option and lists the commits in that commit range like the 'summary'
95 + option of linkgit:git-submodule[1] does.
96 +
97 --color::
98 Show colored diff.
99
Makefile
+2
@@ -448,6 +448,7 @@ LIB_H += sideband.h
448 LIB_H += sigchain.h
449 LIB_H += strbuf.h
450 LIB_H += string-list.h
451 +LIB_H += submodule.h
452 LIB_H += tag.h
453 LIB_H += transport.h
454 LIB_H += tree.h
@@ -546,6 +547,7 @@ LIB_OBJS += sideband.o
547 LIB_OBJS += sigchain.o
548 LIB_OBJS += strbuf.o
549 LIB_OBJS += string-list.o
550 +LIB_OBJS += submodule.o
551 LIB_OBJS += symlinks.o
552 LIB_OBJS += tag.o
553 LIB_OBJS += trace.o
diff.c
+18
@@ -13,6 +13,7 @@
13 #include "utf8.h"
14 #include "userdiff.h"
15 #include "sigchain.h"
16 +#include "submodule.h"
17
18 #ifdef NO_FAST_WORKING_DIRECTORY
19 #define FAST_WORKING_DIRECTORY 0
@@ -1453,6 +1454,17 @@ static void builtin_diff(const char *name_a,
1454 const char *a_prefix, *b_prefix;
1455 const char *textconv_one = NULL, *textconv_two = NULL;
1456
1457 + if (DIFF_OPT_TST(o, SUBMODULE_LOG) &&
1458 + (!one->mode || S_ISGITLINK(one->mode)) &&
1459 + (!two->mode || S_ISGITLINK(two->mode))) {
1460 + const char *del = diff_get_color_opt(o, DIFF_FILE_OLD);
1461 + const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
1462 + show_submodule_summary(o->file, one ? one->path : two->path,
1463 + one->sha1, two->sha1,
1464 + del, add, reset);
1465 + return;
1466 + }
1467 +
1468 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
1469 textconv_one = get_textconv(one);
1470 textconv_two = get_textconv(two);
@@ -2640,6 +2652,12 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2652 DIFF_OPT_CLR(options, ALLOW_TEXTCONV);
2653 else if (!strcmp(arg, "--ignore-submodules"))
2654 DIFF_OPT_SET(options, IGNORE_SUBMODULES);
2655 + else if (!strcmp(arg, "--submodule"))
2656 + DIFF_OPT_SET(options, SUBMODULE_LOG);
2657 + else if (!prefixcmp(arg, "--submodule=")) {
2658 + if (!strcmp(arg + 12, "log"))
2659 + DIFF_OPT_SET(options, SUBMODULE_LOG);
2660 + }
2661
2662 /* misc options */
2663 else if (!strcmp(arg, "-z"))
diff.h
+3
@@ -66,6 +66,9 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
66 #define DIFF_OPT_DIRSTAT_CUMULATIVE (1 << 19)
67 #define DIFF_OPT_DIRSTAT_BY_FILE (1 << 20)
68 #define DIFF_OPT_ALLOW_TEXTCONV (1 << 21)
69 +
70 +#define DIFF_OPT_SUBMODULE_LOG (1 << 23)
71 +
72 #define DIFF_OPT_TST(opts, flag) ((opts)->flags & DIFF_OPT_##flag)
73 #define DIFF_OPT_SET(opts, flag) ((opts)->flags |= DIFF_OPT_##flag)
74 #define DIFF_OPT_CLR(opts, flag) ((opts)->flags &= ~DIFF_OPT_##flag)
submodule.c new
+113
@@ -0,0 +1,113 @@
1 +#include "cache.h"
2 +#include "submodule.h"
3 +#include "dir.h"
4 +#include "diff.h"
5 +#include "commit.h"
6 +#include "revision.h"
7 +
8 +int add_submodule_odb(const char *path)
9 +{
10 + struct strbuf objects_directory = STRBUF_INIT;
11 + struct alternate_object_database *alt_odb;
12 +
13 + strbuf_addf(&objects_directory, "%s/.git/objects/", path);
14 + if (!is_directory(objects_directory.buf))
15 + return -1;
16 +
17 + /* avoid adding it twice */
18 + for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
19 + if (alt_odb->name - alt_odb->base == objects_directory.len &&
20 + !strncmp(alt_odb->base, objects_directory.buf,
21 + objects_directory.len))
22 + return 0;
23 +
24 + alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
25 + alt_odb->next = alt_odb_list;
26 + strcpy(alt_odb->base, objects_directory.buf);
27 + alt_odb->name = alt_odb->base + objects_directory.len;
28 + alt_odb->name[2] = '/';
29 + alt_odb->name[40] = '\0';
30 + alt_odb->name[41] = '\0';
31 + alt_odb_list = alt_odb;
32 + prepare_alt_odb();
33 + return 0;
34 +}
35 +
36 +void show_submodule_summary(FILE *f, const char *path,
37 + unsigned char one[20], unsigned char two[20],
38 + const char *del, const char *add, const char *reset)
39 +{
40 + struct rev_info rev;
41 + struct commit *commit, *left = left, *right;
42 + struct commit_list *merge_bases, *list;
43 + const char *message = NULL;
44 + struct strbuf sb = STRBUF_INIT;
45 + static const char *format = " %m %s";
46 + int fast_forward = 0, fast_backward = 0;
47 +
48 + if (is_null_sha1(two))
49 + message = "(submodule deleted)";
50 + else if (add_submodule_odb(path))
51 + message = "(not checked out)";
52 + else if (is_null_sha1(one))
53 + message = "(new submodule)";
54 + else if (!(left = lookup_commit_reference(one)) ||
55 + !(right = lookup_commit_reference(two)))
56 + message = "(commits not present)";
57 +
58 + if (!message) {
59 + init_revisions(&rev, NULL);
60 + setup_revisions(0, NULL, &rev, NULL);
61 + rev.left_right = 1;
62 + rev.first_parent_only = 1;
63 + left->object.flags |= SYMMETRIC_LEFT;
64 + add_pending_object(&rev, &left->object, path);
65 + add_pending_object(&rev, &right->object, path);
66 + merge_bases = get_merge_bases(left, right, 1);
67 + if (merge_bases) {
68 + if (merge_bases->item == left)
69 + fast_forward = 1;
70 + else if (merge_bases->item == right)
71 + fast_backward = 1;
72 + }
73 + for (list = merge_bases; list; list = list->next) {
74 + list->item->object.flags |= UNINTERESTING;
75 + add_pending_object(&rev, &list->item->object,
76 + sha1_to_hex(list->item->object.sha1));
77 + }
78 + if (prepare_revision_walk(&rev))
79 + message = "(revision walker failed)";
80 + }
81 +
82 + strbuf_addf(&sb, "Submodule %s %s..", path,
83 + find_unique_abbrev(one, DEFAULT_ABBREV));
84 + if (!fast_backward && !fast_forward)
85 + strbuf_addch(&sb, '.');
86 + strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
87 + if (message)
88 + strbuf_addf(&sb, " %s\n", message);
89 + else
90 + strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
91 + fwrite(sb.buf, sb.len, 1, f);
92 +
93 + if (!message) {
94 + while ((commit = get_revision(&rev))) {
95 + strbuf_setlen(&sb, 0);
96 + if (commit->object.flags & SYMMETRIC_LEFT) {
97 + if (del)
98 + strbuf_addstr(&sb, del);
99 + }
100 + else if (add)
101 + strbuf_addstr(&sb, add);
102 + format_commit_message(commit, format, &sb,
103 + rev.date_mode);
104 + if (reset)
105 + strbuf_addstr(&sb, reset);
106 + strbuf_addch(&sb, '\n');
107 + fprintf(f, "%s", sb.buf);
108 + }
109 + clear_commit_marks(left, ~0);
110 + clear_commit_marks(right, ~0);
111 + }
112 + strbuf_release(&sb);
113 +}
submodule.h new
+8
@@ -0,0 +1,8 @@
1 +#ifndef SUBMODULE_H
2 +#define SUBMODULE_H
3 +
4 +void show_submodule_summary(FILE *f, const char *path,
5 + unsigned char one[20], unsigned char two[20],
6 + const char *del, const char *add, const char *reset);
7 +
8 +#endif