@matheusbsbs / matheus-bagliano / commits / f59a59e22f
Fork 0

Add the --color-words option to the diff options family

With this option, the changed words are shown inline. For example, if a file containing "This is foo" is changed to "This is bar", the diff will now show "This is " in plain text, "foo" in red, and "bar" in green. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jul 28, 2006 at 23:56 UTC f59a59e22f0005d799fe1d40e0c1f7e5c1f68b76
3 files changed +177 -7
Documentation/diff-options.txt
+3
@@ -36,6 +36,9 @@
36 Turn off colored diff, even when the configuration file
37 gives the default to color output.
38
39 +--color-words::
40 + Show colored word diff, i.e. color words which have changed.
41 +
42 --no-renames::
43 Turn off rename detection, even when the configuration
44 file gives the default to do so.
diff.c
+172 -6
@@ -358,12 +358,152 @@ static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
358 return 0;
359 }
360
361 +struct diff_words_buffer {
362 + mmfile_t text;
363 + long alloc;
364 + long current; /* output pointer */
365 + int suppressed_newline;
366 +};
367 +
368 +static void diff_words_append(char *line, unsigned long len,
369 + struct diff_words_buffer *buffer)
370 +{
371 + if (buffer->text.size + len > buffer->alloc) {
372 + buffer->alloc = (buffer->text.size + len) * 3 / 2;
373 + buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc);
374 + }
375 + line++;
376 + len--;
377 + memcpy(buffer->text.ptr + buffer->text.size, line, len);
378 + buffer->text.size += len;
379 +}
380 +
381 +struct diff_words_data {
382 + struct xdiff_emit_state xm;
383 + struct diff_words_buffer minus, plus;
384 +};
385 +
386 +static void print_word(struct diff_words_buffer *buffer, int len, int color,
387 + int suppress_newline)
388 +{
389 + const char *ptr;
390 + int eol = 0;
391 +
392 + if (len == 0)
393 + return;
394 +
395 + ptr = buffer->text.ptr + buffer->current;
396 + buffer->current += len;
397 +
398 + if (ptr[len - 1] == '\n') {
399 + eol = 1;
400 + len--;
401 + }
402 +
403 + fputs(diff_get_color(1, color), stdout);
404 + fwrite(ptr, len, 1, stdout);
405 + fputs(diff_get_color(1, DIFF_RESET), stdout);
406 +
407 + if (eol) {
408 + if (suppress_newline)
409 + buffer->suppressed_newline = 1;
410 + else
411 + putchar('\n');
412 + }
413 +}
414 +
415 +static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
416 +{
417 + struct diff_words_data *diff_words = priv;
418 +
419 + if (diff_words->minus.suppressed_newline) {
420 + if (line[0] != '+')
421 + putchar('\n');
422 + diff_words->minus.suppressed_newline = 0;
423 + }
424 +
425 + len--;
426 + switch (line[0]) {
427 + case '-':
428 + print_word(&diff_words->minus, len, DIFF_FILE_OLD, 1);
429 + break;
430 + case '+':
431 + print_word(&diff_words->plus, len, DIFF_FILE_NEW, 0);
432 + break;
433 + case ' ':
434 + print_word(&diff_words->plus, len, DIFF_PLAIN, 0);
435 + diff_words->minus.current += len;
436 + break;
437 + }
438 +}
439 +
440 +/* this executes the word diff on the accumulated buffers */
441 +static void diff_words_show(struct diff_words_data *diff_words)
442 +{
443 + xpparam_t xpp;
444 + xdemitconf_t xecfg;
445 + xdemitcb_t ecb;
446 + mmfile_t minus, plus;
447 + int i;
448 +
449 + minus.size = diff_words->minus.text.size;
450 + minus.ptr = xmalloc(minus.size);
451 + memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size);
452 + for (i = 0; i < minus.size; i++)
453 + if (isspace(minus.ptr[i]))
454 + minus.ptr[i] = '\n';
455 + diff_words->minus.current = 0;
456 +
457 + plus.size = diff_words->plus.text.size;
458 + plus.ptr = xmalloc(plus.size);
459 + memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size);
460 + for (i = 0; i < plus.size; i++)
461 + if (isspace(plus.ptr[i]))
462 + plus.ptr[i] = '\n';
463 + diff_words->plus.current = 0;
464 +
465 + xpp.flags = XDF_NEED_MINIMAL;
466 + xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
467 + xecfg.flags = 0;
468 + ecb.outf = xdiff_outf;
469 + ecb.priv = diff_words;
470 + diff_words->xm.consume = fn_out_diff_words_aux;
471 + xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
472 +
473 + free(minus.ptr);
474 + free(plus.ptr);
475 + diff_words->minus.text.size = diff_words->plus.text.size = 0;
476 +
477 + if (diff_words->minus.suppressed_newline) {
478 + putchar('\n');
479 + diff_words->minus.suppressed_newline = 0;
480 + }
481 +}
482 +
483 struct emit_callback {
484 struct xdiff_emit_state xm;
485 int nparents, color_diff;
486 const char **label_path;
487 + struct diff_words_data *diff_words;
488 };
489
490 +static void free_diff_words_data(struct emit_callback *ecbdata)
491 +{
492 + if (ecbdata->diff_words) {
493 + /* flush buffers */
494 + if (ecbdata->diff_words->minus.text.size ||
495 + ecbdata->diff_words->plus.text.size)
496 + diff_words_show(ecbdata->diff_words);
497 +
498 + if (ecbdata->diff_words->minus.text.ptr)
499 + free (ecbdata->diff_words->minus.text.ptr);
500 + if (ecbdata->diff_words->plus.text.ptr)
501 + free (ecbdata->diff_words->plus.text.ptr);
502 + free(ecbdata->diff_words);
503 + ecbdata->diff_words = NULL;
504 + }
505 +}
506 +
507 const char *diff_get_color(int diff_use_color, enum color_diff ix)
508 {
509 if (diff_use_color)
@@ -398,12 +538,31 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
538 else {
539 int nparents = ecbdata->nparents;
540 int color = DIFF_PLAIN;
401 - for (i = 0; i < nparents && len; i++) {
402 - if (line[i] == '-')
403 - color = DIFF_FILE_OLD;
404 - else if (line[i] == '+')
405 - color = DIFF_FILE_NEW;
406 - }
541 + if (ecbdata->diff_words && nparents != 1)
542 + /* fall back to normal diff */
543 + free_diff_words_data(ecbdata);
544 + if (ecbdata->diff_words) {
545 + if (line[0] == '-') {
546 + diff_words_append(line, len,
547 + &ecbdata->diff_words->minus);
548 + return;
549 + } else if (line[0] == '+') {
550 + diff_words_append(line, len,
551 + &ecbdata->diff_words->plus);
552 + return;
553 + }
554 + if (ecbdata->diff_words->minus.text.size ||
555 + ecbdata->diff_words->plus.text.size)
556 + diff_words_show(ecbdata->diff_words);
557 + line++;
558 + len--;
559 + } else
560 + for (i = 0; i < nparents && len; i++) {
561 + if (line[i] == '-')
562 + color = DIFF_FILE_OLD;
563 + else if (line[i] == '+')
564 + color = DIFF_FILE_NEW;
565 + }
566 set = diff_get_color(ecbdata->color_diff, color);
567 }
568 if (len > 0 && line[len-1] == '\n')
@@ -836,7 +995,12 @@ static void builtin_diff(const char *name_a,
995 ecb.outf = xdiff_outf;
996 ecb.priv = &ecbdata;
997 ecbdata.xm.consume = fn_out_consume;
998 + if (o->color_diff_words)
999 + ecbdata.diff_words =
1000 + xcalloc(1, sizeof(struct diff_words_data));
1001 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1002 + if (o->color_diff_words)
1003 + free_diff_words_data(&ecbdata);
1004 }
1005
1006 free_ab_and_return:
@@ -1697,6 +1861,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1861 options->xdl_opts |= XDF_IGNORE_WHITESPACE;
1862 else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
1863 options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1864 + else if (!strcmp(arg, "--color-words"))
1865 + options->color_diff = options->color_diff_words = 1;
1866 else if (!strcmp(arg, "--no-renames"))
1867 options->detect_rename = 0;
1868 else
diff.h
+2 -1
@@ -46,7 +46,8 @@ struct diff_options {
46 full_index:1,
47 silent_on_remove:1,
48 find_copies_harder:1,
49 - color_diff:1;
49 + color_diff:1,
50 + color_diff_words:1;
51 int context;
52 int break_opt;
53 int detect_rename;