git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 4/7] color-words: take an optional regular expression describing words

From
Thomas Rast <trast@student.ethz.ch>
Date
Jan 17, 2009, 16:29 UTC
Message-ID
<1232209788-10408-5-git-send-email-trast@student.ethz.ch>
In-Reply-To
<1232209788-10408-4-git-send-email-trast@student.ethz.ch>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

In some applications, words are not delimited by white space. To allow for that, you can specify a regular expression describing what makes a word with

	git diff --color-words='[A-Za-z0-9]+'
Note that words cannot contain newline characters.

As suggested by Thomas Rast, the words are the exact matches of the regular expression.

Note that a regular expression beginning with a '^' will match only a word at the beginning of the hunk, not a word at the beginning of a line, and is probably not what you want.

This commit contains a quoting fix by Thomas Rast.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 Documentation/diff-options.txt |    6 +++-
 diff.c                         |   64 ++++++++++++++++++++++++++++++++++-----
 diff.h                         |    1 +
 t/t4034-diff-words.sh          |   57 +++++++++++++++++++++++++++++++++++
 4 files changed, 118 insertions(+), 10 deletions(-)
diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
index 43793d7..2c1fa4b 100644
--- a/Documentation/diff-options.txt
+++ b/Documentation/diff-options.txt
@@ -91,8 +91,12 @@ endif::git-format-patch[]
 	Turn off colored diff, even when the configuration file
 	gives the default to color output.
 
---color-words::
+--color-words[=regex]::
 	Show colored word diff, i.e. color words which have changed.
++
+Optionally, you can pass a regular expression that tells Git what the
+words are that you are looking for; The default is to interpret any
+stretch of non-whitespace as a word.
 
 --no-renames::
 	Turn off rename detection, even when the configuration
diff --git a/diff.c b/diff.c
index 37c886a..9fb3d0d 100644
--- a/diff.c
+++ b/diff.c
@@ -333,12 +333,14 @@ static void diff_words_append(char *line, unsigned long len,
 	len--;
 	memcpy(buffer->text.ptr + buffer->text.size, line, len);
 	buffer->text.size += len;
+	buffer->text.ptr[buffer->text.size] = '\0';
 }
 
 struct diff_words_data {
 	struct diff_words_buffer minus, plus;
 	const char *current_plus;
 	FILE *file;
+	regex_t *word_regex;
 };
 
 static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
@@ -382,17 +384,49 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
 	diff_words->current_plus = plus_end;
 }
 
+/* This function starts looking at *begin, and returns 0 iff a word was found. */
+static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
+		int *begin, int *end)
+{
+	if (word_regex && *begin < buffer->size) {
+		regmatch_t match[1];
+		if (!regexec(word_regex, buffer->ptr + *begin, 1, match, 0)) {
+			char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
+					'\n', match[0].rm_eo - match[0].rm_so);
+			*end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
+			*begin += match[0].rm_so;
+			return *begin >= *end;
+		}
+		return -1;
+	}
+
+	/* find the next word */
+	while (*begin < buffer->size && isspace(buffer->ptr[*begin]))
+		(*begin)++;
+	if (*begin >= buffer->size)
+		return -1;
+
+	/* find the end of the word */
+	*end = *begin + 1;
+	while (*end < buffer->size && !isspace(buffer->ptr[*end]))
+		(*end)++;
+
+	return 0;
+}
+
 /*
  * This function splits the words in buffer->text, stores the list with
  * newline separator into out, and saves the offsets of the original words
  * in buffer->orig.
  */
-static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out)
+static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
+		regex_t *word_regex)
 {
 	int i, j;
+	long alloc = 0;
 
 	out->size = 0;
-	out->ptr = xmalloc(buffer->text.size);
+	out->ptr = NULL;
 
 	/* fake an empty "0th" word */
 	ALLOC_GROW(buffer->orig, 1, buffer->orig_alloc);
@@ -400,11 +434,8 @@ static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out)
 	buffer->orig_nr = 1;
 
 	for (i = 0; i < buffer->text.size; i++) {
-		if (isspace(buffer->text.ptr[i]))
-			continue;
-		for (j = i + 1; j < buffer->text.size &&
-				!isspace(buffer->text.ptr[j]); j++)
-			; /* find the end of the word */
+		if (find_word_boundaries(&buffer->text, word_regex, &i, &j))
+			return;
 
 		/* store original boundaries */
 		ALLOC_GROW(buffer->orig, buffer->orig_nr + 1,
@@ -414,6 +445,7 @@ static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out)
 		buffer->orig_nr++;
 
 		/* store one word */
+		ALLOC_GROW(out->ptr, out->size + j - i + 1, alloc);
 		memcpy(out->ptr + out->size, buffer->text.ptr + i, j - i);
 		out->ptr[out->size + j - i] = '\n';
 		out->size += j - i + 1;
@@ -443,9 +475,10 @@ static void diff_words_show(struct diff_words_data *diff_words)
 
 	memset(&xpp, 0, sizeof(xpp));
 	memset(&xecfg, 0, sizeof(xecfg));
-	diff_words_fill(&diff_words->minus, &minus);
-	diff_words_fill(&diff_words->plus, &plus);
+	diff_words_fill(&diff_words->minus, &minus, diff_words->word_regex);
+	diff_words_fill(&diff_words->plus, &plus, diff_words->word_regex);
 	xpp.flags = XDF_NEED_MINIMAL;
+	/* as only the hunk header will be parsed, we need a 0-context */
 	xecfg.ctxlen = 0;
 	xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
 		      &xpp, &xecfg, &ecb);
@@ -484,6 +517,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
 		free (ecbdata->diff_words->minus.orig);
 		free (ecbdata->diff_words->plus.text.ptr);
 		free (ecbdata->diff_words->plus.orig);
+		free(ecbdata->diff_words->word_regex);
 		free(ecbdata->diff_words);
 		ecbdata->diff_words = NULL;
 	}
@@ -1506,6 +1540,14 @@ static void builtin_diff(const char *name_a,
 			ecbdata.diff_words =
 				xcalloc(1, sizeof(struct diff_words_data));
 			ecbdata.diff_words->file = o->file;
+			if (o->word_regex) {
+				ecbdata.diff_words->word_regex = (regex_t *)
+					xmalloc(sizeof(regex_t));
+				if (regcomp(ecbdata.diff_words->word_regex,
+						o->word_regex, REG_EXTENDED))
+					die ("Invalid regular expression: %s",
+							o->word_regex);
+			}
 		}
 		xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
 			      &xpp, &xecfg, &ecb);
@@ -2517,6 +2559,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 		DIFF_OPT_CLR(options, COLOR_DIFF);
 	else if (!strcmp(arg, "--color-words"))
 		options->flags |= DIFF_OPT_COLOR_DIFF | DIFF_OPT_COLOR_DIFF_WORDS;
+	else if (!prefixcmp(arg, "--color-words=")) {
+		options->flags |= DIFF_OPT_COLOR_DIFF | DIFF_OPT_COLOR_DIFF_WORDS;
+		options->word_regex = arg + 14;
+	}
 	else if (!strcmp(arg, "--exit-code"))
 		DIFF_OPT_SET(options, EXIT_WITH_STATUS);
 	else if (!strcmp(arg, "--quiet"))
diff --git a/diff.h b/diff.h
index 4d5a327..23cd90c 100644
--- a/diff.h
+++ b/diff.h
@@ -98,6 +98,7 @@ struct diff_options {
 
 	int stat_width;
 	int stat_name_width;
+	const char *word_regex;
 
 	/* this is set by diffcore for DIFF_FORMAT_PATCH */
 	int found_changes;
diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh
index b22195f..4873486 100755
--- a/t/t4034-diff-words.sh
+++ b/t/t4034-diff-words.sh
@@ -63,4 +63,61 @@ test_expect_success 'word diff with runs of whitespace' '
 
 '
 
+cat > expect <<\EOF
+<WHITE>diff --git a/pre b/post<RESET>
+<WHITE>index 330b04f..5ed8eff 100644<RESET>
+<WHITE>--- a/pre<RESET>
+<WHITE>+++ b/post<RESET>
+<BROWN>@@ -1,3 +1,7 @@<RESET>
+h(4),<GREEN>hh<RESET>[44]
+<RESET>
+a = b + c<RESET>
+
+<GREEN>aa = a<RESET>
+
+<GREEN>aeff = aeff * ( aaa<RESET> )
+EOF
+
+test_expect_success 'word diff with a regular expression' '
+
+	word_diff --color-words="[a-z]+"
+
+'
+
+echo 'aaa (aaa)' > pre
+echo 'aaa (aaa) aaa' > post
+
+cat > expect <<\EOF
+<WHITE>diff --git a/pre b/post<RESET>
+<WHITE>index c29453b..be22f37 100644<RESET>
+<WHITE>--- a/pre<RESET>
+<WHITE>+++ b/post<RESET>
+<BROWN>@@ -1 +1 @@<RESET>
+aaa (aaa) <GREEN>aaa<RESET>
+EOF
+
+test_expect_success 'test parsing words for newline' '
+
+	word_diff --color-words="a+"
+
+'
+
+echo '(:' > pre
+echo '(' > post
+
+cat > expect <<\EOF
+<WHITE>diff --git a/pre b/post<RESET>
+<WHITE>index 289cb9d..2d06f37 100644<RESET>
+<WHITE>--- a/pre<RESET>
+<WHITE>+++ b/post<RESET>
+<BROWN>@@ -1 +1 @@<RESET>
+(<RED>:<RESET>
+EOF
+
+test_expect_success 'test when words are only removed at the end' '
+
+	word_diff --color-words=.
+
+'
+
 test_done
-- 
1.6.1.315.g92577
Previous: Thomas RastNext: Thomas Rast
Message 48 of 109 in “refactor the --color-words to make it more hackable”
  1. 0/4 refactor the --color-words to make it more hackableJohannes Schindelin, Jan 11, 2009
  2. 1/4 Add color_fwrite(), a function coloring each line individuallyJohannes Schindelin, Jan 11, 2009
  3. Junio C HamanoJan 11, 2009
  4. Johannes SchindelinJan 11, 2009
  5. 1/4 Add color_fwrite(), a function coloring each line individuallyJohannes Schindelin, Jan 11, 2009
  6. Jakub NarebskiJan 12, 2009
  7. 2/4 color-words: refactor word splitting and use ALLOC_GROW()Johannes Schindelin, Jan 11, 2009
  8. 3/4 color-words: refactor to allow for 0-character word boundariesJohannes Schindelin, Jan 11, 2009
  9. Junio C HamanoJan 11, 2009
  10. Johannes SchindelinJan 11, 2009
  11. Thomas RastJan 12, 2009
  12. Junio C HamanoJan 12, 2009
  13. 0/4 color-words improvementsJohannes Schindelin, Jan 14, 2009
  14. 1/4 Add color_fwrite_lines(), a function coloring each line individuallyJohannes Schindelin, Jan 14, 2009
  15. 2/4 color-words: refactor word splitting and use ALLOC_GROW()Johannes Schindelin, Jan 14, 2009
  16. 3/4 color-words: change algorithm to allow for 0-character word boundariesJohannes Schindelin, Jan 14, 2009
  17. Johannes SchindelinJan 14, 2009
  18. 4/4 color-words: take an optional regular expression describing wordsJohannes Schindelin, Jan 14, 2009
  19. Thomas RastJan 14, 2009
  20. Teemu LikonenJan 14, 2009
  21. Teemu LikonenJan 14, 2009
  22. Johannes SchindelinJan 14, 2009
  23. Johannes SchindelinJan 14, 2009
  24. Thomas RastJan 14, 2009
  25. Johannes SchindelinJan 14, 2009
  26. Thomas RastJan 14, 2009
  27. Boyd Stephen Smith Jr.Jan 14, 2009
  28. Teemu LikonenJan 15, 2009
  29. Johannes SchindelinJan 15, 2009
  30. Teemu LikonenJan 15, 2009
  31. Thomas RastJan 15, 2009
  32. Junio C HamanoJan 15, 2009
  33. Johannes SchindelinJan 15, 2009
  34. Santi BéjarJan 16, 2009
  35. Junio C HamanoJan 16, 2009
  36. Boyd Stephen Smith Jr.Jan 16, 2009
  37. Johannes SchindelinJan 16, 2009
  38. Santi BéjarJan 16, 2009
  39. Johannes SchindelinJan 16, 2009
  40. Santi BéjarJan 16, 2009
  41. Johannes SchindelinJan 16, 2009
  42. Thomas RastJan 16, 2009
  43. Johannes SchindelinJan 16, 2009
  44. 0/7 customizable --color-wordsThomas Rast, Jan 17, 2009
  45. 1/7 Add color_fwrite_lines(), a function coloring each line individuallyThomas Rast, Jan 17, 2009
  46. 2/7 color-words: refactor word splitting and use ALLOC_GROW()Thomas Rast, Jan 17, 2009
  47. 3/7 color-words: change algorithm to allow for 0-character word boundariesThomas Rast, Jan 17, 2009
  48. 4/7 color-words: take an optional regular expression describing wordsThomas Rast, Jan 17, 2009
  49. 5/7 color-words: enable REG_NEWLINE to help userThomas Rast, Jan 17, 2009
  50. 6/7 color-words: expand docs with precise semanticsThomas Rast, Jan 17, 2009
  51. 7/7 color-words: make regex configurable via attributesThomas Rast, Jan 17, 2009
  52. Santi BéjarJan 18, 2009
  53. Santi BéjarJan 18, 2009
  54. Santi BéjarJan 19, 2009
  55. Johannes SchindelinJan 19, 2009
  56. Add tests for diff.color-words configuration option.Boyd Stephen Smith Jr., Jan 20, 2009
  57. diff: Support diff.color-words config optionBoyd Stephen Smith Jr., Jan 20, 2009
  58. Junio C HamanoJan 20, 2009
  59. Markus HeidelbergJan 20, 2009
  60. Boyd Stephen Smith Jr.Jan 20, 2009
  61. Johannes SchindelinJan 20, 2009
  62. Junio C HamanoJan 21, 2009
  63. Markus HeidelbergJan 21, 2009
  64. Johannes SchindelinJan 20, 2009
  65. Boyd Stephen Smith Jr.Jan 20, 2009
  66. Johannes SchindelinJan 20, 2009
  67. Junio C HamanoJan 20, 2009
  68. Johannes SchindelinJan 20, 2009
  69. Junio C HamanoJan 20, 2009
  70. Johannes SchindelinJan 20, 2009
  71. color-words: Support diff.color-words config optionBoyd Stephen Smith Jr., Jan 21, 2009
  72. Change the spelling of "wordregex".Boyd Stephen Smith Jr., Jan 21, 2009
  73. Johannes SchindelinJan 21, 2009
  74. Thomas RastJan 21, 2009
  75. Boyd Stephen Smith Jr.Jan 21, 2009
  76. Johannes SchindelinJan 21, 2009
  77. Boyd Stephen Smith Jr.Jan 21, 2009
  78. Junio C HamanoJan 21, 2009
  79. Jakub NarebskiJan 20, 2009
  80. Johannes SchindelinJan 20, 2009
  81. Boyd Stephen Smith Jr.Jan 20, 2009
  82. Johannes SchindelinJan 20, 2009
  83. Boyd Stephen Smith Jr.Jan 16, 2009
  84. color-words: make regex configurable via attributesThomas Rast, Jan 14, 2009
  85. Johannes SchindelinJan 14, 2009
  86. Thomas RastJan 14, 2009
  87. 3/4 color-words: change algorithm to allow for 0-character word boundariesJohannes Schindelin, Jan 14, 2009
  88. 4/4 color-words: take an optional regular expression describing wordsJohannes Schindelin, Jan 14, 2009
  89. Thomas RastJan 15, 2009
  90. Johannes SchindelinJan 15, 2009
  91. Johannes SchindelinJan 15, 2009
  92. Thomas RastJan 15, 2009
  93. Thomas RastJan 15, 2009
  94. Johannes SchindelinJan 15, 2009
  95. 1/4 color-words: fix quoting in t4034Thomas Rast, Jan 14, 2009
  96. 2/4 color-words: enable REG_NEWLINE to help userThomas Rast, Jan 14, 2009
  97. 3/4 color-words: expand docs with precise semanticsThomas Rast, Jan 14, 2009
  98. 4/4 color-words: make regex configurable via attributesThomas Rast, Jan 14, 2009
  99. Johannes SchindelinJan 15, 2009
  100. Johannes SchindelinJan 15, 2009
  101. Johannes SchindelinJan 14, 2009
  102. Thomas RastJan 14, 2009
  103. Johannes SchindelinJan 14, 2009
  104. Thomas RastJan 14, 2009
  105. 4/4 color-words: take an optional regular expression describing wordsJohannes Schindelin, Jan 11, 2009
  106. Thomas RastJan 11, 2009
  107. Johannes SchindelinJan 11, 2009
  108. Thomas RastJan 12, 2009
  109. Santi BéjarJan 14, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.