Skip to content

Commit d39d667

Browse files
committed
Merge branch 'js/diff-verbose-submodule'
* js/diff-verbose-submodule: add tests for git diff --submodule Add the --submodule option to the diff option family
2 parents e3de372 + 86140d5 commit d39d667

File tree

7 files changed

+411
-0
lines changed

7 files changed

+411
-0
lines changed

Documentation/diff-options.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ endif::git-format-patch[]
8787
Show only names and status of changed files. See the description
8888
of the `--diff-filter` option on what the status letters mean.
8989

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+
9097
--color::
9198
Show colored diff.
9299

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ LIB_H += sideband.h
456456
LIB_H += sigchain.h
457457
LIB_H += strbuf.h
458458
LIB_H += string-list.h
459+
LIB_H += submodule.h
459460
LIB_H += tag.h
460461
LIB_H += transport.h
461462
LIB_H += tree.h
@@ -554,6 +555,7 @@ LIB_OBJS += sideband.o
554555
LIB_OBJS += sigchain.o
555556
LIB_OBJS += strbuf.o
556557
LIB_OBJS += string-list.o
558+
LIB_OBJS += submodule.o
557559
LIB_OBJS += symlinks.o
558560
LIB_OBJS += tag.o
559561
LIB_OBJS += trace.o

diff.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "utf8.h"
1414
#include "userdiff.h"
1515
#include "sigchain.h"
16+
#include "submodule.h"
1617

1718
#ifdef NO_FAST_WORKING_DIRECTORY
1819
#define FAST_WORKING_DIRECTORY 0
@@ -1557,6 +1558,17 @@ static void builtin_diff(const char *name_a,
15571558
const char *a_prefix, *b_prefix;
15581559
const char *textconv_one = NULL, *textconv_two = NULL;
15591560

1561+
if (DIFF_OPT_TST(o, SUBMODULE_LOG) &&
1562+
(!one->mode || S_ISGITLINK(one->mode)) &&
1563+
(!two->mode || S_ISGITLINK(two->mode))) {
1564+
const char *del = diff_get_color_opt(o, DIFF_FILE_OLD);
1565+
const char *add = diff_get_color_opt(o, DIFF_FILE_NEW);
1566+
show_submodule_summary(o->file, one ? one->path : two->path,
1567+
one->sha1, two->sha1,
1568+
del, add, reset);
1569+
return;
1570+
}
1571+
15601572
if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
15611573
textconv_one = get_textconv(one);
15621574
textconv_two = get_textconv(two);
@@ -2757,6 +2769,12 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
27572769
DIFF_OPT_CLR(options, ALLOW_TEXTCONV);
27582770
else if (!strcmp(arg, "--ignore-submodules"))
27592771
DIFF_OPT_SET(options, IGNORE_SUBMODULES);
2772+
else if (!strcmp(arg, "--submodule"))
2773+
DIFF_OPT_SET(options, SUBMODULE_LOG);
2774+
else if (!prefixcmp(arg, "--submodule=")) {
2775+
if (!strcmp(arg + 12, "log"))
2776+
DIFF_OPT_SET(options, SUBMODULE_LOG);
2777+
}
27602778

27612779
/* misc options */
27622780
else if (!strcmp(arg, "-z"))

diff.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,
6666
#define DIFF_OPT_DIRSTAT_CUMULATIVE (1 << 19)
6767
#define DIFF_OPT_DIRSTAT_BY_FILE (1 << 20)
6868
#define DIFF_OPT_ALLOW_TEXTCONV (1 << 21)
69+
70+
#define DIFF_OPT_SUBMODULE_LOG (1 << 23)
71+
6972
#define DIFF_OPT_TST(opts, flag) ((opts)->flags & DIFF_OPT_##flag)
7073
#define DIFF_OPT_SET(opts, flag) ((opts)->flags |= DIFF_OPT_##flag)
7174
#define DIFF_OPT_CLR(opts, flag) ((opts)->flags &= ~DIFF_OPT_##flag)

submodule.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)