combine-diff: show parent line numbers as well.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jan 30, 2006 at 22:33 UTC
f16706cc59af32d0255e0a5ec9d04475bc976ede
1 file changed
+59
-11
combine-diff.c
+59
-11
@@ -86,6 +86,7 @@ struct sline {
86
* bit N is used for "interesting" lines, including context.
87
*/
88
unsigned long flag;
89
+ unsigned long *p_lno;
90
};
91
92
static char *grab_blob(const unsigned char *sha1, unsigned long *size)
@@ -210,14 +211,15 @@ static void append_lost(struct sline *sline, int n, const char *line)
211
}
212
213
static void combine_diff(const unsigned char *parent, const char *ourtmp,
213
- struct sline *sline, int cnt, int n)
214
+ struct sline *sline, int cnt, int n, int num_parent)
215
{
216
FILE *in;
217
char parent_tmp[TMPPATHLEN];
218
char cmd[TMPPATHLEN * 2 + 1024];
219
char line[MAXLINELEN];
219
- unsigned int lno, ob, on, nb, nn;
220
- unsigned long pmask = ~(1UL << n);
220
+ unsigned int lno, ob, on, nb, nn, p_lno;
221
+ unsigned long nmask = (1UL << n);
222
+ unsigned long pmask = ~nmask;
223
struct sline *lost_bucket = NULL;
224
225
write_temp_blob(parent_tmp, parent);
@@ -225,7 +227,7 @@ static void combine_diff(const unsigned char *parent, const char *ourtmp,
227
parent_tmp, ourtmp);
228
in = popen(cmd, "r");
229
if (!in)
228
- return;
230
+ die("cannot spawn %s", cmd);
231
232
lno = 1;
233
while (fgets(line, sizeof(line), in) != NULL) {
@@ -235,16 +237,25 @@ static void combine_diff(const unsigned char *parent, const char *ourtmp,
237
&ob, &on, &nb, &nn))
238
break;
239
lno = nb;
238
- if (!nb) {
240
+ if (!nb)
241
/* @@ -1,2 +0,0 @@ to remove the
242
* first two lines...
243
*/
244
nb = 1;
243
- }
245
if (nn == 0)
246
+ /* @@ -X,Y +N,0 @@ removed Y lines
247
+ * that would have come *after* line N
248
+ * in the result. Our lost buckets hang
249
+ * to the line after the removed lines,
250
+ */
251
lost_bucket = &sline[nb];
252
else
253
lost_bucket = &sline[nb-1];
254
+ if (!sline[nb-1].p_lno)
255
+ sline[nb-1].p_lno =
256
+ xcalloc(num_parent,
257
+ sizeof(unsigned long));
258
+ sline[nb-1].p_lno[n] = ob;
259
continue;
260
}
261
if (!lost_bucket)
@@ -261,6 +272,29 @@ static void combine_diff(const unsigned char *parent, const char *ourtmp,
272
}
273
fclose(in);
274
unlink(parent_tmp);
275
+
276
+ /* Assign line numbers for this parent.
277
+ *
278
+ * sline[lno].p_lno[n] records the first line number
279
+ * (counting from 1) for parent N if the final hunk display
280
+ * started by showing sline[lno] (possibly showing the lost
281
+ * lines attached to it first).
282
+ */
283
+ for (lno = 0, p_lno = 1; lno < cnt; lno++) {
284
+ struct lline *ll;
285
+ sline[lno].p_lno[n] = p_lno;
286
+
287
+ /* How many lines would this sline advance the p_lno? */
288
+ ll = sline[lno].lost_head;
289
+ while (ll) {
290
+ if (ll->parent_map & nmask)
291
+ p_lno++; /* '-' means parent had it */
292
+ ll = ll->next;
293
+ }
294
+ if (sline[lno].flag & nmask)
295
+ p_lno++; /* no '+' means parent had it */
296
+ }
297
+ sline[lno].p_lno[n] = p_lno; /* trailer */
298
}
299
300
static unsigned long context = 3;
@@ -466,11 +500,18 @@ static int make_hunks(struct sline *sline, unsigned long cnt,
500
return has_interesting;
501
}
502
469
-static void dump_sline(struct sline *sline, int cnt, int num_parent)
503
+static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, unsigned long cnt, int n)
504
+{
505
+ l0 = sline[l0].p_lno[n];
506
+ l1 = sline[l1].p_lno[n];
507
+ printf("-%lu,%lu ", l0, l1-l0);
508
+}
509
+
510
+static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
511
{
512
unsigned long mark = (1UL<<num_parent);
513
int i;
473
- int lno = 0;
514
+ unsigned long lno = 0;
515
516
while (1) {
517
struct sline *sl = &sline[lno];
@@ -483,7 +524,9 @@ static void dump_sline(struct sline *sline, int cnt, int num_parent)
524
if (!(sline[hunk_end].flag & mark))
525
break;
526
for (i = 0; i <= num_parent; i++) putchar(combine_marker);
486
- printf(" +%d,%d ", lno+1, hunk_end-lno);
527
+ printf(" +%lu,%lu ", lno+1, hunk_end-lno);
528
+ for (i = 0; i < num_parent; i++)
529
+ show_parent_lno(sline, lno, hunk_end, cnt, i);
530
for (i = 0; i <= num_parent; i++) putchar(combine_marker);
531
putchar('\n');
532
while (lno < hunk_end) {
@@ -525,6 +568,7 @@ static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
568
569
for (lno = 0; lno < cnt; lno++) {
570
struct lline *ll = sline->lost_head;
571
+ sline->p_lno[i] = sline->p_lno[j];
572
while (ll) {
573
if (ll->parent_map & jmask)
574
ll->parent_map |= imask;
@@ -590,7 +634,7 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
634
if (result[size-1] != '\n')
635
cnt++; /* incomplete line */
636
593
- sline = xcalloc(cnt, sizeof(*sline));
637
+ sline = xcalloc(cnt+1, sizeof(*sline));
638
ep = result;
639
sline[0].bol = result;
640
for (lno = 0, cp = result; cp - result < size; cp++) {
@@ -609,6 +653,10 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
653
sline[cnt-1].flag = (1UL<<num_parent) - 1;
654
}
655
656
+ sline[0].p_lno = xcalloc((cnt+1) * num_parent, sizeof(unsigned long));
657
+ for (lno = 0; lno < cnt; lno++)
658
+ sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
659
+
660
for (i = 0; i < num_parent; i++) {
661
int j;
662
for (j = 0; j < i; j++) {
@@ -620,7 +668,7 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
668
}
669
if (i <= j)
670
combine_diff(elem->parent_sha1[i], ourtmp, sline,
623
- cnt, i);
671
+ cnt, i, num_parent);
672
}
673
674
show_hunks = make_hunks(sline, cnt, num_parent, dense);