Skip to content

Commit 0495404

Browse files
committed
diff --check: detect leftover conflict markers
This teaches "diff --check" to detect and complain if the change adds lines that look like leftover conflict markers. We should be able to remove the old Perl script used in the sample pre-commit hook and modernize the script with this facility. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 877f23c commit 0495404

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

diff.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,35 @@ struct checkdiff_t {
11431143
int trailing_blanks_start;
11441144
};
11451145

1146+
static int is_conflict_marker(const char *line, unsigned long len)
1147+
{
1148+
char firstchar;
1149+
int cnt;
1150+
1151+
if (len < 8)
1152+
return 0;
1153+
firstchar = line[0];
1154+
switch (firstchar) {
1155+
case '=': case '>': case '<':
1156+
break;
1157+
default:
1158+
return 0;
1159+
}
1160+
for (cnt = 1; cnt < 7; cnt++)
1161+
if (line[cnt] != firstchar)
1162+
return 0;
1163+
/* line[0] thru line[6] are same as firstchar */
1164+
if (firstchar == '=') {
1165+
/* divider between ours and theirs? */
1166+
if (len != 8 || line[7] != '\n')
1167+
return 0;
1168+
} else if (len < 8 || !isspace(line[7])) {
1169+
/* not divider before ours nor after theirs */
1170+
return 0;
1171+
}
1172+
return 1;
1173+
}
1174+
11461175
static void checkdiff_consume(void *priv, char *line, unsigned long len)
11471176
{
11481177
struct checkdiff_t *data = priv;
@@ -1159,6 +1188,12 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
11591188
data->trailing_blanks_start = 0;
11601189
else if (!data->trailing_blanks_start)
11611190
data->trailing_blanks_start = data->lineno;
1191+
if (is_conflict_marker(line + 1, len - 1)) {
1192+
data->status |= 1;
1193+
fprintf(data->o->file,
1194+
"%s:%d: leftover conflict marker\n",
1195+
data->filename, data->lineno);
1196+
}
11621197
bad = ws_check(line + 1, len - 1, data->ws_rule);
11631198
if (!bad)
11641199
return;

t/t4017-diff-retval.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,18 @@ test_expect_success 'check should test not just the last line' '
113113
114114
'
115115

116+
test_expect_success 'check detects leftover conflict markers' '
117+
git reset --hard &&
118+
git checkout HEAD^ &&
119+
echo binary >>b &&
120+
git commit -m "side" b &&
121+
test_must_fail git merge master &&
122+
git add b && (
123+
git --no-pager diff --cached --check >test.out
124+
test $? = 2
125+
) &&
126+
test "$(grep "conflict marker" test.out | wc -l)" = 3 &&
127+
git reset --hard
128+
'
129+
116130
test_done

0 commit comments

Comments
 (0)