git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Christian Holtje <docwhat@gmail.com>
Cc: Alex Riesen <raa.lkml@gmail.com>, git@vger.kernel.org
Subject: [PATCH 5/5] diff --check: detect leftover conflict markers
Date: Thu, 26 Jun 2008 15:37:21 -0700	[thread overview]
Message-ID: <7vwskbomam.fsf_-_@gitster.siamese.dyndns.org> (raw)
In-Reply-To: <7vk5gbq10p.fsf@gitster.siamese.dyndns.org> (Junio C. Hamano's message of "Thu, 26 Jun 2008 15:33:58 -0700")

This teaches "diff --check" to detect and complain if new lines
have 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>
---
 diff.c                 |   35 +++++++++++++++++++++++++++++++++++
 t/t4017-diff-retval.sh |   14 ++++++++++++++
 2 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/diff.c b/diff.c
index f31c721..d515b06 100644
--- a/diff.c
+++ b/diff.c
@@ -1143,6 +1143,35 @@ struct checkdiff_t {
 	int trailing_blanks_start;
 };
 
+static int is_conflict_marker(const char *line, unsigned long len)
+{
+	char firstchar;
+	int cnt;
+
+	if (len < 8)
+		return 0;
+	firstchar = line[0];
+	switch (firstchar) {
+	case '=': case '>': case '<':
+		break;
+	default:
+		return 0;
+	}
+	for (cnt = 1; cnt < 7; cnt++)
+		if (line[cnt] != firstchar)
+			return 0;
+	/* line[0] thru line[6] are same as firstchar */
+	if (firstchar == '=') {
+		/* divider between ours and theirs? */
+		if (len != 8 || line[7] != '\n')
+			return 0;
+	} else if (len < 8 || !isspace(line[7])) {
+		/* not divider before ours nor after theirs */
+		return 0;
+	}
+	return 1;
+}
+
 static void checkdiff_consume(void *priv, char *line, unsigned long len)
 {
 	struct checkdiff_t *data = priv;
@@ -1159,6 +1188,12 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 			data->trailing_blanks_start = 0;
 		else if (!data->trailing_blanks_start)
 			data->trailing_blanks_start = data->lineno;
+		if (is_conflict_marker(line + 1, len - 1)) {
+			data->status |= 1;
+			fprintf(data->o->file,
+				"%s:%d: leftover conflict marker\n",
+				data->filename, data->lineno);
+		}
 		bad = ws_check(line + 1, len - 1, data->ws_rule);
 		if (!bad)
 			return;
diff --git a/t/t4017-diff-retval.sh b/t/t4017-diff-retval.sh
index 0d0fb87..d748d45 100755
--- a/t/t4017-diff-retval.sh
+++ b/t/t4017-diff-retval.sh
@@ -113,4 +113,18 @@ test_expect_success 'check should test not just the last line' '
 
 '
 
+test_expect_success 'check detects leftover conflict markers' '
+	git reset --hard &&
+	git checkout HEAD^ &&
+	echo binary >>b &&
+	git commit -m "side" b &&
+	test_must_fail git merge master &&
+	git add b && (
+		git --no-pager diff --cached --check >test.out
+		test $? = 2
+	) &&
+	test "$(grep "conflict marker" test.out | wc -l)" = 3 &&
+	git reset --hard
+'
+
 test_done
-- 
1.5.6.1.78.gde8d9

  parent reply	other threads:[~2008-06-26 22:39 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-24 19:21 [PATCH v2] pre-commit hook should ignore carriage returns at EOL Christian Holtje
2008-06-25 18:14 ` Alex Riesen
2008-06-25 18:47   ` Christian Holtje
2008-06-25 19:14     ` Junio C Hamano
2008-06-26  2:41       ` Christian Holtje
2008-06-26 22:33         ` Junio C Hamano
2008-06-26 22:34           ` [PATCH 1/5] diff --check: explain why we do not care whether old side is binary Junio C Hamano
2008-06-26 22:35           ` [PATCH 2/5] check_and_emit_line(): rename and refactor Junio C Hamano
2008-06-26 22:36           ` [PATCH 3/5] checkdiff: pass diff_options to the callback Junio C Hamano
2008-06-26 22:36           ` [PATCH 4/5] Teach "diff --check" about a new blank lines at end Junio C Hamano
2008-06-26 22:37           ` Junio C Hamano [this message]
2008-06-26 23:01           ` [PATCH v2] pre-commit hook should ignore carriage returns at EOL Junio C Hamano
2008-06-26 23:06             ` Junio C Hamano
2008-06-27  4:24               ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7vwskbomam.fsf_-_@gitster.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=docwhat@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=raa.lkml@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).