@matheusbsbs / matheus-bagliano / commits / 049540435f
Fork 0

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>

Junio C Hamano committed Jun 26, 2008 at 15:37 UTC 049540435fa5f7f583b8f5af257322b17eac7375
2 files changed +49
diff.c
+35
@@ -1143,6 +1143,35 @@ struct checkdiff_t {
1143 int trailing_blanks_start;
1144 };
1145
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 +
1175 static void checkdiff_consume(void *priv, char *line, unsigned long len)
1176 {
1177 struct checkdiff_t *data = priv;
@@ -1159,6 +1188,12 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
1188 data->trailing_blanks_start = 0;
1189 else if (!data->trailing_blanks_start)
1190 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 + }
1197 bad = ws_check(line + 1, len - 1, data->ws_rule);
1198 if (!bad)
1199 return;
t/t4017-diff-retval.sh
+14
@@ -113,4 +113,18 @@ test_expect_success 'check should test not just the last line' '
113
114 '
115
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 +
130 test_done