Skip to content

Commit d8f4790

Browse files
author
Junio C Hamano
committed
diff-tree --cc: denser combined diff output for a merge commit.
Building on the previous '-c' (combined) option, '--cc' option squelches the output further by omitting hunks that consist of difference with solely one parent. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent af3feef commit d8f4790

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

combine-diff.c

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,25 @@ static int interesting(struct sline *sline, unsigned long all_mask)
278278
return ((sline->flag & all_mask) != all_mask || sline->lost_head);
279279
}
280280

281-
static void make_hunks(struct sline *sline, unsigned long cnt, int num_parent)
281+
static unsigned long line_diff_parents(struct sline *sline, unsigned long all_mask)
282+
{
283+
/*
284+
* Look at the line and see from which parents we have difference.
285+
* Lower bits of sline->flag records if the parent had this line,
286+
* so XOR with all_mask gives us on-bits for parents we have
287+
* differences with.
288+
*/
289+
unsigned long parents = (sline->flag ^ all_mask);
290+
if (sline->lost_head) {
291+
struct lline *ll;
292+
for (ll = sline->lost_head; ll; ll = ll->next)
293+
parents |= ll->parent_map;
294+
}
295+
return parents & all_mask;
296+
}
297+
298+
static void make_hunks(struct sline *sline, unsigned long cnt,
299+
int num_parent, int dense)
282300
{
283301
unsigned long all_mask = (1UL<<num_parent) - 1;
284302
unsigned long mark = (1UL<<num_parent);
@@ -302,6 +320,45 @@ static void make_hunks(struct sline *sline, unsigned long cnt, int num_parent)
302320
}
303321
i++;
304322
}
323+
if (!dense)
324+
return;
325+
326+
/* Look at each hunk, and if it contains changes from only
327+
* one parent, mark that uninteresting.
328+
*/
329+
i = 0;
330+
while (i < cnt) {
331+
int j, hunk_end, diffs;
332+
unsigned long parents;
333+
while (i < cnt && !(sline[i].flag & mark))
334+
i++;
335+
if (cnt <= i)
336+
break; /* No more interesting hunks */
337+
for (hunk_end = i + 1; hunk_end < cnt; hunk_end++)
338+
if (!(sline[hunk_end].flag & mark))
339+
break;
340+
/* [i..hunk_end) are interesting. Now is it from
341+
* only one parent?
342+
* If lost lines are only from one parent and
343+
* remaining lines existed in parents other than
344+
* that parent, then the hunk is not that interesting.
345+
*/
346+
parents = 0;
347+
diffs = 0;
348+
for (j = i; j < hunk_end; j++)
349+
parents |= line_diff_parents(sline + j, all_mask);
350+
/* Now, how many bits from [0..num_parent) are on? */
351+
for (j = 0; j < num_parent; j++) {
352+
if (parents & (1UL<<j))
353+
diffs++;
354+
}
355+
if (diffs < 2) {
356+
/* This hunk is not that interesting after all */
357+
for (j = i; j < hunk_end; j++)
358+
sline[j].flag &= ~mark;
359+
}
360+
i = hunk_end;
361+
}
305362
}
306363

307364
static void dump_sline(struct sline *sline, int cnt, int num_parent)
@@ -351,7 +408,8 @@ static void dump_sline(struct sline *sline, int cnt, int num_parent)
351408
}
352409
}
353410

354-
static void show_combined_diff(struct path_list *elem, int num_parent)
411+
static void show_combined_diff(struct path_list *elem, int num_parent,
412+
int dense)
355413
{
356414
unsigned long size, cnt, lno;
357415
char *result, *cp, *ep;
@@ -390,7 +448,7 @@ static void show_combined_diff(struct path_list *elem, int num_parent)
390448
for (i = 0; i < num_parent; i++)
391449
combine_diff(elem->parent_sha1[i], ourtmp, sline, cnt, i);
392450

393-
make_hunks(sline, cnt, num_parent);
451+
make_hunks(sline, cnt, num_parent, dense);
394452

395453
dump_sline(sline, cnt, num_parent);
396454
unlink(ourtmp);
@@ -410,7 +468,8 @@ static void show_combined_diff(struct path_list *elem, int num_parent)
410468
}
411469

412470
int diff_tree_combined_merge(const unsigned char *sha1,
413-
const char *header, int show_empty_merge)
471+
const char *header,
472+
int show_empty_merge, int dense)
414473
{
415474
struct commit *commit = lookup_commit(sha1);
416475
struct diff_options diffopts;
@@ -455,7 +514,7 @@ int diff_tree_combined_merge(const unsigned char *sha1,
455514
else
456515
printf("%s", p->path);
457516
putchar('\n');
458-
show_combined_diff(p, num_parent);
517+
show_combined_diff(p, num_parent, dense);
459518
}
460519
}
461520

diff-tree.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ static int verbose_header = 0;
88
static int ignore_merges = 1;
99
static int show_empty_combined = 0;
1010
static int combine_merges = 0;
11+
static int dense_combined_merges = 0;
1112
static int read_stdin = 0;
1213

1314
static const char *header = NULL;
@@ -121,7 +122,8 @@ static int diff_tree_commit(const unsigned char *commit_sha1)
121122
header = generate_header(sha1, sha1,
122123
commit->buffer);
123124
return diff_tree_combined_merge(sha1, header,
124-
show_empty_combined);
125+
show_empty_combined,
126+
dense_combined_merges);
125127
}
126128
}
127129

@@ -168,7 +170,7 @@ static int diff_tree_stdin(char *line)
168170
}
169171

170172
static const char diff_tree_usage[] =
171-
"git-diff-tree [--stdin] [-m] [-c] [-s] [-v] [--pretty] [-t] [-r] [--root] "
173+
"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
172174
"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
173175
" -r diff recursively\n"
174176
" --root include the initial commit as diff against /dev/null\n"
@@ -235,6 +237,10 @@ int main(int argc, const char **argv)
235237
combine_merges = 1;
236238
continue;
237239
}
240+
if (!strcmp(arg, "--cc")) {
241+
dense_combined_merges = combine_merges = 1;
242+
continue;
243+
}
238244
if (!strcmp(arg, "-v")) {
239245
verbose_header = 1;
240246
header_prefix = "diff-tree ";

diff.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ extern int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
5656
extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new,
5757
const char *base, struct diff_options *opt);
5858

59-
extern int diff_tree_combined_merge(const unsigned char *sha1, const char *, int);
59+
extern int diff_tree_combined_merge(const unsigned char *sha1, const char *, int, int);
6060

6161
extern void diff_addremove(struct diff_options *,
6262
int addremove,

0 commit comments

Comments
 (0)