Skip to content

Commit 002a9ec

Browse files
committed
Merge branch 'js/maint-diff-color-words'
* js/maint-diff-color-words: diff --color-words: bit of clean-up diff --color-words -U0: fix the location of hunk headers t4034-diff-words: add a test for word diff without context Conflicts: diff.c
2 parents 934c042 + 76fd282 commit 002a9ec

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

diff.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -686,14 +686,18 @@ static void diff_words_show(struct diff_words_data *diff_words)
686686
diff_words->minus.text.size = diff_words->plus.text.size = 0;
687687
}
688688

689+
/* In "color-words" mode, show word-diff of words accumulated in the buffer */
690+
static void diff_words_flush(struct emit_callback *ecbdata)
691+
{
692+
if (ecbdata->diff_words->minus.text.size ||
693+
ecbdata->diff_words->plus.text.size)
694+
diff_words_show(ecbdata->diff_words);
695+
}
696+
689697
static void free_diff_words_data(struct emit_callback *ecbdata)
690698
{
691699
if (ecbdata->diff_words) {
692-
/* flush buffers */
693-
if (ecbdata->diff_words->minus.text.size ||
694-
ecbdata->diff_words->plus.text.size)
695-
diff_words_show(ecbdata->diff_words);
696-
700+
diff_words_flush(ecbdata);
697701
free (ecbdata->diff_words->minus.text.ptr);
698702
free (ecbdata->diff_words->minus.orig);
699703
free (ecbdata->diff_words->plus.text.ptr);
@@ -773,6 +777,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
773777
}
774778

775779
if (line[0] == '@') {
780+
if (ecbdata->diff_words)
781+
diff_words_flush(ecbdata);
776782
len = sane_truncate_line(ecbdata, line, len);
777783
find_lno(line, ecbdata);
778784
emit_line(ecbdata->file,
@@ -798,9 +804,7 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
798804
&ecbdata->diff_words->plus);
799805
return;
800806
}
801-
if (ecbdata->diff_words->minus.text.size ||
802-
ecbdata->diff_words->plus.text.size)
803-
diff_words_show(ecbdata->diff_words);
807+
diff_words_flush(ecbdata);
804808
line++;
805809
len--;
806810
emit_line(ecbdata->file, plain, reset, line, len);

t/t4034-diff-words.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ test_expect_success 'word diff with runs of whitespace' '
6363
6464
'
6565

66+
cat > expect <<\EOF
67+
<WHITE>diff --git a/pre b/post<RESET>
68+
<WHITE>index 330b04f..5ed8eff 100644<RESET>
69+
<WHITE>--- a/pre<RESET>
70+
<WHITE>+++ b/post<RESET>
71+
<BROWN>@@ -1 +1 @@<RESET>
72+
<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
73+
<BROWN>@@ -3,0 +4,4 @@ a = b + c<RESET>
74+
75+
<GREEN>aa = a<RESET>
76+
77+
<GREEN>aeff = aeff * ( aaa )<RESET>
78+
EOF
79+
80+
test_expect_success 'word diff without context' '
81+
82+
word_diff --color-words --unified=0
83+
84+
'
85+
6686
cat > expect <<\EOF
6787
<WHITE>diff --git a/pre b/post<RESET>
6888
<WHITE>index 330b04f..5ed8eff 100644<RESET>

0 commit comments

Comments
 (0)