combine-diff: cleanup.
The flag on the surviving lines meant "this parent is not different" while the parent_map flag on the lost lines meant "this parent is different", which was confusing. So swap the meaning of on-bit in the flag. Also more heavily comment the code. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Feb 2, 2006 at 15:17 UTC
46dc941246af6e845eb7835afdb8cd20eed835b7
1 file changed
+65
-37
combine-diff.c
+65
-37
@@ -81,8 +81,8 @@ struct sline {
81
struct lline *lost_head, **lost_tail;
82
char *bol;
83
int len;
84
- /* bit 0 up to (N-1) are on if the parent does _not_
85
- * have this line (i.e. we changed it).
84
+ /* bit 0 up to (N-1) are on if the parent has this line (i.e.
85
+ * we did not change it).
86
* bit N is used for "interesting" lines, including context.
87
*/
88
unsigned long flag;
@@ -219,7 +219,6 @@ static void combine_diff(const unsigned char *parent, const char *ourtmp,
219
char line[MAXLINELEN];
220
unsigned int lno, ob, on, nb, nn, p_lno;
221
unsigned long nmask = (1UL << n);
222
- unsigned long pmask = ~nmask;
222
struct sline *lost_bucket = NULL;
223
224
write_temp_blob(parent_tmp, parent);
@@ -265,7 +264,7 @@ static void combine_diff(const unsigned char *parent, const char *ourtmp,
264
append_lost(lost_bucket, n, line+1);
265
break;
266
case '+':
268
- sline[lno-1].flag &= pmask;
267
+ sline[lno-1].flag |= nmask;
268
lno++;
269
break;
270
}
@@ -291,7 +290,7 @@ static void combine_diff(const unsigned char *parent, const char *ourtmp,
290
p_lno++; /* '-' means parent had it */
291
ll = ll->next;
292
}
294
- if (sline[lno].flag & nmask)
293
+ if (!(sline[lno].flag & nmask))
294
p_lno++; /* no '+' means parent had it */
295
}
296
sline[lno].p_lno[n] = p_lno; /* trailer */
@@ -302,7 +301,10 @@ static char combine_marker = '@';
301
302
static int interesting(struct sline *sline, unsigned long all_mask)
303
{
305
- return ((sline->flag & all_mask) != all_mask || sline->lost_head);
304
+ /* If some parents lost lines here, or if we have added to
305
+ * some parent, it is interesting.
306
+ */
307
+ return ((sline->flag & all_mask) || sline->lost_head);
308
}
309
310
static unsigned long adjust_hunk_tail(struct sline *sline,
@@ -310,28 +312,35 @@ static unsigned long adjust_hunk_tail(struct sline *sline,
312
unsigned long hunk_begin,
313
unsigned long i)
314
{
313
- /* i points at the first uninteresting line.
314
- * If the last line of the hunk was interesting
315
- * only because it has some deletion, then
316
- * it is not all that interesting for the
317
- * purpose of giving trailing context lines.
315
+ /* i points at the first uninteresting line. If the last line
316
+ * of the hunk was interesting only because it has some
317
+ * deletion, then it is not all that interesting for the
318
+ * purpose of giving trailing context lines. This is because
319
+ * we output '-' line and then unmodified sline[i-1] itself in
320
+ * that case which gives us one extra context line.
321
*/
319
- if ((hunk_begin + 1 <= i) &&
320
- ((sline[i-1].flag & all_mask) == all_mask))
322
+ if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
323
i--;
324
return i;
325
}
326
325
-static unsigned long next_interesting(struct sline *sline,
326
- unsigned long mark,
327
- unsigned long i,
328
- unsigned long cnt,
329
- int uninteresting)
327
+static unsigned long find_next(struct sline *sline,
328
+ unsigned long mark,
329
+ unsigned long i,
330
+ unsigned long cnt,
331
+ int uninteresting)
332
{
333
+ /* We have examined up to i-1 and are about to look at i.
334
+ * Find next interesting or uninteresting line. Here,
335
+ * "interesting" does not mean interesting(), but marked by
336
+ * the give_context() function below (i.e. it includes context
337
+ * lines that are not interesting to interesting() function
338
+ * that are surrounded by interesting() ones.
339
+ */
340
while (i < cnt)
332
- if (uninteresting ?
333
- !(sline[i].flag & mark) :
334
- (sline[i].flag & mark))
341
+ if (uninteresting
342
+ ? !(sline[i].flag & mark)
343
+ : (sline[i].flag & mark))
344
return i;
345
else
346
i++;
@@ -344,23 +353,37 @@ static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
353
unsigned long mark = (1UL<<num_parent);
354
unsigned long i;
355
347
- i = next_interesting(sline, mark, 0, cnt, 0);
356
+ /* Two groups of interesting lines may have a short gap of
357
+ * unintersting lines. Connect such groups to give them a
358
+ * bit of context.
359
+ *
360
+ * We first start from what the interesting() function says,
361
+ * and mark them with "mark", and paint context lines with the
362
+ * mark. So interesting() would still say false for such context
363
+ * lines but they are treated as "interesting" in the end.
364
+ */
365
+ i = find_next(sline, mark, 0, cnt, 0);
366
if (cnt <= i)
367
return 0;
368
369
while (i < cnt) {
370
unsigned long j = (context < i) ? (i - context) : 0;
371
unsigned long k;
372
+
373
+ /* Paint a few lines before the first interesting line. */
374
while (j < i)
375
sline[j++].flag |= mark;
376
377
again:
358
- j = next_interesting(sline, mark, i, cnt, 1);
378
+ /* we know up to i is to be included. where does the
379
+ * next uninteresting one start?
380
+ */
381
+ j = find_next(sline, mark, i, cnt, 1);
382
if (cnt <= j)
383
break; /* the rest are all interesting */
384
385
/* lookahead context lines */
363
- k = next_interesting(sline, mark, j, cnt, 0);
386
+ k = find_next(sline, mark, j, cnt, 0);
387
j = adjust_hunk_tail(sline, all_mask, i, j);
388
389
if (k < j + context) {
@@ -374,7 +397,8 @@ static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
397
}
398
399
/* j is the first uninteresting line and there is
377
- * no overlap beyond it within context lines.
400
+ * no overlap beyond it within context lines. Paint
401
+ * the trailing edge a bit.
402
*/
403
i = k;
404
k = (j + context < cnt) ? j + context : cnt;
@@ -461,7 +485,7 @@ static int make_hunks(struct sline *sline, unsigned long cnt,
485
same_diff = 0;
486
has_interesting = 0;
487
for (j = i; j < hunk_end && !has_interesting; j++) {
464
- unsigned long this_diff = ~sline[j].flag & all_mask;
488
+ unsigned long this_diff = sline[j].flag & all_mask;
489
struct lline *ll = sline[j].lost_head;
490
if (this_diff) {
491
/* This has some changes. Is it the
@@ -532,6 +556,7 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
556
while (lno < hunk_end) {
557
struct lline *ll;
558
int j;
559
+ unsigned long p_mask;
560
sl = &sline[lno++];
561
ll = sl->lost_head;
562
while (ll) {
@@ -544,11 +569,13 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
569
puts(ll->line);
570
ll = ll->next;
571
}
572
+ p_mask = 1;
573
for (j = 0; j < num_parent; j++) {
548
- if ((1UL<<j) & sl->flag)
549
- putchar(' ');
550
- else
574
+ if (p_mask & sl->flag)
575
putchar('+');
576
+ else
577
+ putchar(' ');
578
+ p_mask <<= 1;
579
}
580
printf("%.*s\n", sl->len, sl->bol);
581
}
@@ -574,8 +601,8 @@ static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
601
ll->parent_map |= imask;
602
ll = ll->next;
603
}
577
- if (!(sline->flag & jmask))
578
- sline->flag &= ~imask;
604
+ if (sline->flag & jmask)
605
+ sline->flag |= imask;
606
sline++;
607
}
608
}
@@ -641,7 +668,7 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
668
if (*cp == '\n') {
669
sline[lno].lost_tail = &sline[lno].lost_head;
670
sline[lno].len = cp - sline[lno].bol;
644
- sline[lno].flag = (1UL<<num_parent) - 1;
671
+ sline[lno].flag = 0;
672
lno++;
673
if (lno < cnt)
674
sline[lno].bol = cp + 1;
@@ -650,7 +677,7 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
677
if (result[size-1] != '\n') {
678
sline[cnt-1].lost_tail = &sline[cnt-1].lost_head;
679
sline[cnt-1].len = size - (sline[cnt-1].bol - result);
653
- sline[cnt-1].flag = (1UL<<num_parent) - 1;
680
+ sline[cnt-1].flag = 0;
681
}
682
683
sline[0].p_lno = xcalloc((cnt+1) * num_parent, sizeof(unsigned long));
@@ -673,11 +700,11 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
700
701
show_hunks = make_hunks(sline, cnt, num_parent, dense);
702
676
- if (header && (show_hunks || show_empty)) {
677
- shown_header++;
678
- puts(header);
679
- }
703
if (show_hunks) {
704
+ if (header) {
705
+ shown_header++;
706
+ puts(header);
707
+ }
708
printf("diff --%s ", dense ? "cc" : "combined");
709
if (quote_c_style(elem->path, NULL, NULL, 0))
710
quote_c_style(elem->path, NULL, stdout, 0);
@@ -709,6 +736,7 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
736
}
737
}
738
}
739
+ free(sline[0].p_lno);
740
free(sline);
741
return shown_header;
742
}