From patchwork Fri Jun 21 23:18:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13708138 Received: from pb-smtp1.pobox.com (pb-smtp1.pobox.com [64.147.108.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8EA3B6F2F1 for ; Fri, 21 Jun 2024 23:18:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.108.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719011911; cv=none; b=iNmkWPDT7D3BtIgOPQPHTAwON9UiJjFIy+NmUsVdC9SLOXpOrHNr/E17NeudrjD0yyu+yIlXgLE5L8QkEriYO4Vq1tXtXisZ+KvaIAJyO2wlHRQFm5cjDh7oX0VsdgiICt1M7CKipp5cpLNqbxzlmfFDdBCmb4RJxMm0siZn4J8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719011911; c=relaxed/simple; bh=rwJD3pg3SWLZ/tJrkcRqx6duawt60WJC9U4Psd+hk/s=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HqEqpBE5lkolglmozXkOGs6nILKVbbAPW3rOuwBUkwVsePiO/f6gBOzdM8SEpeIv3tQb/38eyUutuzhhFLrzSMVpOj2spPffpFbgKQDbuJqS8Vuih5INYECK5nfPcYHX/5/iweTm+DWq62nCsaQQPkl7Y0bNepZImGNRgoqAPKo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com; spf=pass smtp.mailfrom=pobox.com; dkim=pass (1024-bit key) header.d=pobox.com header.i=@pobox.com header.b=pbXAEMer; arc=none smtp.client-ip=64.147.108.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pobox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=pobox.com header.i=@pobox.com header.b="pbXAEMer" Received: from pb-smtp1.pobox.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id 7718D27E26; Fri, 21 Jun 2024 19:18:29 -0400 (EDT) (envelope-from gitster@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=rwJD3pg3SWLZ/tJrkcRqx6dua wt60WJC9U4Psd+hk/s=; b=pbXAEMerWCEpdEsZjD+oKlm9uPcWRIj9m/XGkXBc9 mTY1aUb6vjrobm8la7WsxLEkJ9C+HVzSmDXNbgBZEZRxgW4P3jj4dPy2L2qK5ifo MuqTDsOWLWW5dOIH51CY6Jgd6L8l8VC6LSKLrHK5KKvQIyiD61LhG/3faDLhIR1f sE= Received: from pb-smtp1.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id 709FA27E25; Fri, 21 Jun 2024 19:18:29 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.125.204.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp1.pobox.com (Postfix) with ESMTPSA id E454527E24; Fri, 21 Jun 2024 19:18:28 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH 1/5] t4204: patch-id supports various input format Date: Fri, 21 Jun 2024 16:18:22 -0700 Message-ID: <20240621231826.3280338-2-gitster@pobox.com> X-Mailer: git-send-email 2.45.2-786-g49444cbe9a In-Reply-To: <20240621231826.3280338-1-gitster@pobox.com> References: <20240621231826.3280338-1-gitster@pobox.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Pobox-Relay-ID: 917ADA9C-3024-11EF-9E2B-5B6DE52EC81B-77302942!pb-smtp1.pobox.com "git patch-id" was first developed to read from "git diff-tree --stdin -p" output. Later it was enhanced to read from "git diff-tree --stdin -p -v", which was the downstream of an early imitation of "git log" ("git rev-list" run in the upstream of a pipe to feed the "diff-tree"). These days, we also read from "git format-patch". Their output begins slightly differently, but the patch-id computed over them for the same commit should be the same. Ensure that we won't accidentally break this expectation. Signed-off-by: Junio C Hamano --- t/t4204-patch-id.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/t/t4204-patch-id.sh b/t/t4204-patch-id.sh index a7fa94ce0a..1627fdda1b 100755 --- a/t/t4204-patch-id.sh +++ b/t/t4204-patch-id.sh @@ -114,6 +114,29 @@ test_expect_success 'patch-id supports git-format-patch output' ' test "$2" = $(git rev-parse HEAD) ' +test_expect_success 'patch-id computes the same for various formats' ' + # This test happens to consider "git log -p -1" output + # the canonical input format, so use it as the norm. + git log -1 -p same >log-p.output && + git patch-id expect && + + # format-patch begins with "From " + git format-patch -1 --stdout same >format-patch.output && + git patch-id actual && + test_cmp actual expect && + + # "diff-tree --stdin -p" begins with "" + same=$(git rev-parse same) && + echo $same | git diff-tree --stdin -p >diff-tree.output && + git patch-id actual && + test_cmp actual expect && + + # "diff-tree --stdin -v -p" begins with "commit " + echo $same | git diff-tree --stdin -p -v >diff-tree-v.output && + git patch-id actual && + test_cmp actual expect +' + test_expect_success 'whitespace is irrelevant in footer' ' get_patch_id main && git checkout same && From patchwork Fri Jun 21 23:18:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13708139 Received: from pb-smtp2.pobox.com (pb-smtp2.pobox.com [64.147.108.71]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B29B6F2F1 for ; Fri, 21 Jun 2024 23:18:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.108.71 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719011917; cv=none; b=O77nfQTxU4//PT3y0Ev8+iwNYkf86O42R4nzF3c1xFD4p+yUQP72j8+G37BWRls9IqY9xUmu10zhGc0TjjJx5TVnLySf6GqM0i5VU4iZys7K1QCk5AjSM0NM7V1gFIM7pSuwsDEOpbFukBKx1KWLcwB5qZLEDOo39kPnFAeyapI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719011917; c=relaxed/simple; bh=PZM0J0P24DxUfn9mym1XagGPVYbBHN5vU/bQu2tccGo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C65xoVCqLt50zOrUVBIWY/8Zmn1LMOh+b7OERRmLA88DLYEXxQX0FlB0nLIezoVLLDtaOcT2eduY5jKe/gRUb9nKNq46KKJPP0eou8iy8X14eTVt9bFkxQaF2rD25eafyz+CpgK61Q9maIVjDPwU91+rmIUpDZVk1FNis6BNmdM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com; spf=pass smtp.mailfrom=pobox.com; dkim=pass (1024-bit key) header.d=pobox.com header.i=@pobox.com header.b=UyYcC6U+; arc=none smtp.client-ip=64.147.108.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pobox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=pobox.com header.i=@pobox.com header.b="UyYcC6U+" Received: from pb-smtp2.pobox.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 2AACF23CA8; Fri, 21 Jun 2024 19:18:31 -0400 (EDT) (envelope-from gitster@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=PZM0J0P24DxUfn9mym1XagGPV YbBHN5vU/bQu2tccGo=; b=UyYcC6U+nDYF/brWMmSMTkej48Q0GsJFaeph//i+v 3euwqxpjjVpmKDLk8xHioHn541e8OkXJCBre7S3i9YzTPsM5z+7n534UbYKcCGus zkiCOhiXZL7G9BqAXW/q9j16jlj6bHHyWFuhTxYpmvIJbu1Bwlti9fe/gLwKR9qR ms= Received: from pb-smtp2.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 2244C23CA7; Fri, 21 Jun 2024 19:18:31 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.125.204.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp2.pobox.com (Postfix) with ESMTPSA id 8949E23CA6; Fri, 21 Jun 2024 19:18:30 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH 2/5] patch-id: call flush_current_id() only when needed Date: Fri, 21 Jun 2024 16:18:23 -0700 Message-ID: <20240621231826.3280338-3-gitster@pobox.com> X-Mailer: git-send-email 2.45.2-786-g49444cbe9a In-Reply-To: <20240621231826.3280338-1-gitster@pobox.com> References: <20240621231826.3280338-1-gitster@pobox.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Pobox-Relay-ID: 927376D4-3024-11EF-AB43-965B910A682E-77302942!pb-smtp2.pobox.com The caller passes a flag that is used to become no-op when calling flush_current_id(). Instead of calling something that becomes a no-op, teach the caller not to call it in the first place. Signed-off-by: Junio C Hamano --- builtin/patch-id.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin/patch-id.c b/builtin/patch-id.c index 3894d2b970..0f262e7a03 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -6,10 +6,9 @@ #include "hex.h" #include "parse-options.h" -static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result) +static void flush_current_id(struct object_id *id, struct object_id *result) { - if (patchlen) - printf("%s %s\n", oid_to_hex(result), oid_to_hex(id)); + printf("%s %s\n", oid_to_hex(result), oid_to_hex(id)); } static int remove_space(char *line) @@ -181,7 +180,8 @@ static void generate_id_list(int stable, int verbatim) oidclr(&oid); while (!feof(stdin)) { patchlen = get_one_patchid(&n, &result, &line_buf, stable, verbatim); - flush_current_id(patchlen, &oid, &result); + if (patchlen) + flush_current_id(&oid, &result); oidcpy(&oid, &n); } strbuf_release(&line_buf); From patchwork Fri Jun 21 23:18:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13708140 Received: from pb-smtp21.pobox.com (pb-smtp21.pobox.com [173.228.157.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A60C16F85E for ; Fri, 21 Jun 2024 23:18:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.228.157.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719011919; cv=none; b=G1o/RKTYk6Esz5Vrx3krB6h3P+IkgHtHj6qka9+TG3ch0PB0ehK1zeYkcfpDcQmaBj2Vzo3fN5WdoPwuLhceLSPeV7lRc+NU+0kIFZHuLWw2/vOYq2TRIeAKNkuzZr63S9V65EJrt1yjA8lLtqLKy1xq+zYwJdr/hAv2xrxfEc0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719011919; c=relaxed/simple; bh=f4iX3F/uNIwfAdib9dw8aT4AvAD92G+YPup5AxuRuNY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qhcL5xIhsKLVbicj5klWCmn+FY2+fyE9tNJVxvtN5s5jwN1v8zt6dGCVQve0U2UVZ/Yumn+xAdl10R+b+rDE3pxJG6U7n/WMfgPQHSFPgkl+DEVN6rEPSe9Gi8hJYnbVGyWG/p2G++B/mNytwvUq2SuaxNil2yjJGEpKQcFjcY0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com; spf=pass smtp.mailfrom=pobox.com; dkim=pass (1024-bit key) header.d=pobox.com header.i=@pobox.com header.b=LIer/AUv; arc=none smtp.client-ip=173.228.157.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pobox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=pobox.com header.i=@pobox.com header.b="LIer/AUv" Received: from pb-smtp21.pobox.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id C102722EEC; Fri, 21 Jun 2024 19:18:36 -0400 (EDT) (envelope-from gitster@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=f4iX3F/uNIwfAdib9dw8aT4Av AD92G+YPup5AxuRuNY=; b=LIer/AUvUl1h9XKCxguysVsjAjR03SPRxiMl60JUE hpmd8kCo6bPsBCC65Gxzn1E7TllhyBOvo+AKfIiqZqJ9i6VM4gCyEdlMTqWD0HK2 d1Tu0jnfJiCWU/HL4VrCbfHI47VIHAkfkzRhcGSA7N7PBRcMlvOKS4kxhD2wlVBS 4Q= Received: from pb-smtp21.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id BA6ED22EEB; Fri, 21 Jun 2024 19:18:36 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.125.204.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp21.pobox.com (Postfix) with ESMTPSA id 856BD22EEA; Fri, 21 Jun 2024 19:18:32 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH 3/5] patch-id: make get_one_patchid() more extensible Date: Fri, 21 Jun 2024 16:18:24 -0700 Message-ID: <20240621231826.3280338-4-gitster@pobox.com> X-Mailer: git-send-email 2.45.2-786-g49444cbe9a In-Reply-To: <20240621231826.3280338-1-gitster@pobox.com> References: <20240621231826.3280338-1-gitster@pobox.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Pobox-Relay-ID: 93A283E2-3024-11EF-BCCC-DFF1FEA446E2-77302942!pb-smtp21.pobox.com We pass two independent Boolean flags (i.e. do we want the stable variant of patch-id? do we want to hash the stuff verbatim?) into the function as two separate parameters. Before adding the third one and make the interface even wider, let's consolidate them into a single flag word. No changes in behaviour. Just a trivial interface change. Signed-off-by: Junio C Hamano --- builtin/patch-id.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/builtin/patch-id.c b/builtin/patch-id.c index 0f262e7a03..128e0997d8 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -58,9 +58,14 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after) return 1; } +#define GOPID_STABLE 01 +#define GOPID_VERBATIM 02 + static int get_one_patchid(struct object_id *next_oid, struct object_id *result, - struct strbuf *line_buf, int stable, int verbatim) + struct strbuf *line_buf, unsigned flags) { + int stable = flags & GOPID_STABLE; + int verbatim = flags & GOPID_VERBATIM; int patchlen = 0, found_next = 0; int before = -1, after = -1; int diff_is_binary = 0; @@ -171,7 +176,7 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result, return patchlen; } -static void generate_id_list(int stable, int verbatim) +static void generate_id_list(unsigned flags) { struct object_id oid, n, result; int patchlen; @@ -179,7 +184,7 @@ static void generate_id_list(int stable, int verbatim) oidclr(&oid); while (!feof(stdin)) { - patchlen = get_one_patchid(&n, &result, &line_buf, stable, verbatim); + patchlen = get_one_patchid(&n, &result, &line_buf, flags); if (patchlen) flush_current_id(&oid, &result); oidcpy(&oid, &n); @@ -218,6 +223,7 @@ int cmd_patch_id(int argc, const char **argv, const char *prefix) /* if nothing is set, default to unstable */ struct patch_id_opts config = {0, 0}; int opts = 0; + unsigned flags = 0; struct option builtin_patch_id_options[] = { OPT_CMDMODE(0, "unstable", &opts, N_("use the unstable patch-id algorithm"), 1), @@ -237,7 +243,11 @@ int cmd_patch_id(int argc, const char **argv, const char *prefix) argc = parse_options(argc, argv, prefix, builtin_patch_id_options, patch_id_usage, 0); - generate_id_list(opts ? opts > 1 : config.stable, - opts ? opts == 3 : config.verbatim); + if (opts ? opts > 1 : config.stable) + flags |= GOPID_STABLE; + if (opts ? opts == 3 : config.verbatim) + flags |= GOPID_VERBATIM; + generate_id_list(flags); + return 0; } From patchwork Fri Jun 21 23:18:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13708142 Received: from pb-smtp20.pobox.com (pb-smtp20.pobox.com [173.228.157.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B70F516F916 for ; Fri, 21 Jun 2024 23:18:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.228.157.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719011927; cv=none; b=EVCkmHkHdJunq2dIGVyhZ75L1JfYyhYhibL8Rv20CghgLWysp+ewid9rcz3kPaUtZomC8yxwbUtx2UqeA3f3gb74i+j8RNdxGWhdB2/4M4w8/mvDcb+nX3nsfHMddAiIKs9RlfB37J7CzAbWBvz5MPE5N0mn41maWVSAZrzuxj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719011927; c=relaxed/simple; bh=vVsxOmrnhlhgecP/kbDCaQbjFkRaZ3EwaTavy1PgHZk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QAT/JfbTmciF/cvMjVmXlGuffaBMsq4Pn7fKk8LrjC+hxZuQeuDDm7VskoT0kTpi5VMMhndVC2iLE9uxHcbViw+y8eO3CNn4604EdKvpnqEAVJkvaHljqulJtXUpKh2gLB3s/NnEWjGLz5LJsdxvHwjM0rIA4tbPcAYt6HZYhSg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com; spf=pass smtp.mailfrom=pobox.com; dkim=pass (1024-bit key) header.d=pobox.com header.i=@pobox.com header.b=BLKrB5oV; arc=none smtp.client-ip=173.228.157.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pobox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=pobox.com header.i=@pobox.com header.b="BLKrB5oV" Received: from pb-smtp20.pobox.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id AAF3339471; Fri, 21 Jun 2024 19:18:41 -0400 (EDT) (envelope-from gitster@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=vVsxOmrnhlhgecP/kbDCaQbjF kRaZ3EwaTavy1PgHZk=; b=BLKrB5oVwbinXM3rIguyRzE1fPc6WzW7dD0vjiKzC Lj8SAFLr2cXE4vBr8diqOjwcJxJoBNZVnwdLGAtD65jSBPgYnv6wihTKT8yiex8S OuJyU1O5faAEkNutIJ5h8ITz87HyXpB3V/aRrMvwsECELzbH3Aa64ZnbIXTPkqX0 ck= Received: from pb-smtp20.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id A3FDA39470; Fri, 21 Jun 2024 19:18:41 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.125.204.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp20.pobox.com (Postfix) with ESMTPSA id 32BDF39457; Fri, 21 Jun 2024 19:18:37 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH 4/5] patch-id: rewrite code that detects the beginning of a patch Date: Fri, 21 Jun 2024 16:18:25 -0700 Message-ID: <20240621231826.3280338-5-gitster@pobox.com> X-Mailer: git-send-email 2.45.2-786-g49444cbe9a In-Reply-To: <20240621231826.3280338-1-gitster@pobox.com> References: <20240621231826.3280338-1-gitster@pobox.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Pobox-Relay-ID: 966973E2-3024-11EF-B701-C38742FD603B-77302942!pb-smtp20.pobox.com The get_one_patchid() function reads input lines until it finds a patch header (the line that begins a patch), whose beginning is one of: (1) an "", which is "git diff-tree --stdin" shows; (2) "commit ", which is "git log" shows; or (3) "From ", which is "git log --format=email" gives. When it finds such a line, it returns to the caller, reporting the it found, and the size of the "patch" it processed. The caller then calls the function again, which then ignores the commit log message, and then processes the lines in the patch part until it hits another "beginning of a patch". The above logic was fairly easy to see until 2bb73ae8 (patch-id: use starts_with() and skip_prefix(), 2016-05-28) reorganized the code, which made another logic that has nothing to do with the "where does the next patch begin?" logic, which came from 2485eab5 (git-patch-id: do not trip over "no newline" markers, 2011-02-17) that ignores the "\ No newline at the end", rolled into the same single if() statement. Let's split it out. The "\ No newline at the end" marker is part of the patch, should not appear before we start reading the patch part, and does not belong to the detection of patch header. Signed-off-by: Junio C Hamano --- builtin/patch-id.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/builtin/patch-id.c b/builtin/patch-id.c index 128e0997d8..a649966f31 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -80,16 +80,19 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result, const char *p = line; int len; - /* Possibly skip over the prefix added by "log" or "format-patch" */ - if (!skip_prefix(line, "commit ", &p) && - !skip_prefix(line, "From ", &p) && - starts_with(line, "\\ ") && 12 < strlen(line)) { + /* + * If we see a line that begins with "", + * "commit " or "From ", it is + * the beginning of a patch. Return to the caller, as + * we are done with the one we have been processing. + */ + if (skip_prefix(line, "commit ", &p)) + ; + else if (skip_prefix(line, "From ", &p)) + ; + if (!get_oid_hex(p, next_oid)) { if (verbatim) the_hash_algo->update_fn(&ctx, line, strlen(line)); - continue; - } - - if (!get_oid_hex(p, next_oid)) { found_next = 1; break; } @@ -130,6 +133,16 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result, break; } + /* + * A hunk about an incomplete line may have this + * marker at the end, which should just be ignored. + */ + if (starts_with(line, "\\ ") && 12 < strlen(line)) { + if (verbatim) + the_hash_algo->update_fn(&ctx, line, strlen(line)); + continue; + } + if (diff_is_binary) { if (starts_with(line, "diff ")) { diff_is_binary = 0; From patchwork Fri Jun 21 23:18:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junio C Hamano X-Patchwork-Id: 13708141 Received: from pb-smtp1.pobox.com (pb-smtp1.pobox.com [64.147.108.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49C5016F28B for ; Fri, 21 Jun 2024 23:18:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.108.70 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719011924; cv=none; b=q7t+1GCaTocmBMkCWcZ9+PE21uijdDaaDytWMMD9eUbh+hSh19bWzw9c07eWUIfLHnfp5zExOxkZ3XaxaGM1I9TQGfDf/3oU82Jm1mb0lfVrT3Ds3TPxBGt07vIfOEtGBrk8jypFZWcDtvJxus7+qJi5wGcxxvYfBgAGr6lTRts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719011924; c=relaxed/simple; bh=yKsVSi6xjE5triRHVgmzr2ERacSpfV/Ppht98r1hPi0=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ReZ6AiZNUifma6c+OVvuekgoj+GcjzSHzgAS87ka3hXVclPjlMuLhiaHdPuxHaHIkFkh6fDu0LSi3HzLGAkFEaczy249bWU2OBGRD0gH2SfjcrA1goAQeHpUfaDsWyaz0zGNmyaK0QicsLWPFFvW0ekasT3Kw01n5FrreOVPxFc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com; spf=pass smtp.mailfrom=pobox.com; dkim=pass (1024-bit key) header.d=pobox.com header.i=@pobox.com header.b=DGViRG8n; arc=none smtp.client-ip=64.147.108.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pobox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=pobox.com header.i=@pobox.com header.b="DGViRG8n" Received: from pb-smtp1.pobox.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id 36D6B27E29; Fri, 21 Jun 2024 19:18:42 -0400 (EDT) (envelope-from gitster@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; s=sasl; bh=yKsVSi6xjE5triRHVgmzr2ERa cSpfV/Ppht98r1hPi0=; b=DGViRG8n7Yr/iQAESB8j1h3CxfL9dSFdIYy65Cvi7 jNSKH1Ddn3zbKElsR0HpqpQ/IRZhKbH0vChDGg9fY3hSXMpGURAuiFoufLqE46wq 5xgKTNdsr5w2h+KXUvfNkq1HwBYZoHUujCoa1YIZK33LxKFQ7k39hN8gk2O6mU59 wQ= Received: from pb-smtp1.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id 2F9F127E28; Fri, 21 Jun 2024 19:18:42 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.125.204.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp1.pobox.com (Postfix) with ESMTPSA id 8ED7127E27; Fri, 21 Jun 2024 19:18:41 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH 5/5] patch-id: tighten code to detect the patch header Date: Fri, 21 Jun 2024 16:18:26 -0700 Message-ID: <20240621231826.3280338-6-gitster@pobox.com> X-Mailer: git-send-email 2.45.2-786-g49444cbe9a In-Reply-To: <20240621231826.3280338-1-gitster@pobox.com> References: <20240621231826.3280338-1-gitster@pobox.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Pobox-Relay-ID: 9905BE08-3024-11EF-8836-5B6DE52EC81B-77302942!pb-smtp1.pobox.com The get_one_patchid() function unconditionally takes a line that matches the patch header (namely, a line that begins with a full object name, possibly prefixed by "commit" or "From" plus a space) as the beginning of a patch. Even when it is *not* looking for one (namely, when the previous call found the patch header and returned, and then we are called again to skip the log message and process the patch whose header was found by the previous invocation). As a consequence, a line in the commit log message that begins with one of these patterns can be mistaken to start another patch, with current message entirely skipped (because we haven't even reached the patch at all). Allow the caller to tell us if it called us already and saw the patch header (in which case we shouldn't be looking for another one, until we see the "diff" part of the patch; instead we simply should be skipping these lines as part of the commit log message), and skip the header processing logic when that is the case. In the helper function, it also needs to flip this "are we looking for a header?" bit, once it finished skipping the commit log message and started processing the patches, as the patch header of the _next_ message is the only clue in the input that the current patch is done. Signed-off-by: Junio C Hamano --- builtin/patch-id.c | 43 ++++++++++++++++++++++++++++++------------- t/t4204-patch-id.sh | 17 +++++++++++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/builtin/patch-id.c b/builtin/patch-id.c index a649966f31..0e6aab1ca2 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -60,12 +60,14 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after) #define GOPID_STABLE 01 #define GOPID_VERBATIM 02 +#define GOPID_FIND_HEADER 04 static int get_one_patchid(struct object_id *next_oid, struct object_id *result, struct strbuf *line_buf, unsigned flags) { int stable = flags & GOPID_STABLE; int verbatim = flags & GOPID_VERBATIM; + int find_header = flags & GOPID_FIND_HEADER; int patchlen = 0, found_next = 0; int before = -1, after = -1; int diff_is_binary = 0; @@ -81,26 +83,39 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result, int len; /* - * If we see a line that begins with "", - * "commit " or "From ", it is - * the beginning of a patch. Return to the caller, as - * we are done with the one we have been processing. + * The caller hasn't seen us find a patch header and + * return to it, or we have started processing patch + * and may encounter the beginning of the next patch. */ - if (skip_prefix(line, "commit ", &p)) - ; - else if (skip_prefix(line, "From ", &p)) - ; - if (!get_oid_hex(p, next_oid)) { - if (verbatim) - the_hash_algo->update_fn(&ctx, line, strlen(line)); - found_next = 1; - break; + if (find_header) { + /* + * If we see a line that begins with "", + * "commit " or "From ", it is + * the beginning of a patch. Return to the caller, as + * we are done with the one we have been processing. + */ + if (skip_prefix(line, "commit ", &p)) + ; + else if (skip_prefix(line, "From ", &p)) + ; + if (!get_oid_hex(p, next_oid)) { + if (verbatim) + the_hash_algo->update_fn(&ctx, line, strlen(line)); + found_next = 1; + break; + } } /* Ignore commit comments */ if (!patchlen && !starts_with(line, "diff ")) continue; + /* + * We are past the commit log message. Prepare to + * stop at the beginning of the next patch header. + */ + find_header = 1; + /* Parsing diff header? */ if (before == -1) { if (starts_with(line, "GIT binary patch") || @@ -196,11 +211,13 @@ static void generate_id_list(unsigned flags) struct strbuf line_buf = STRBUF_INIT; oidclr(&oid); + flags |= GOPID_FIND_HEADER; while (!feof(stdin)) { patchlen = get_one_patchid(&n, &result, &line_buf, flags); if (patchlen) flush_current_id(&oid, &result); oidcpy(&oid, &n); + flags &= ~GOPID_FIND_HEADER; } strbuf_release(&line_buf); } diff --git a/t/t4204-patch-id.sh b/t/t4204-patch-id.sh index 1627fdda1b..b1d98d4110 100755 --- a/t/t4204-patch-id.sh +++ b/t/t4204-patch-id.sh @@ -137,6 +137,23 @@ test_expect_success 'patch-id computes the same for various formats' ' test_cmp actual expect ' +hash=$(git rev-parse same:) +for cruft in "$hash" "commit $hash is bad" "From $hash status" +do + test_expect_success "patch-id with <$cruft> in log message" ' + git format-patch -1 --stdout same >patch-0 && + git patch-id expect && + + { + sed -e "/^$/q" patch-0 && + printf "random message\n%s\n\n" "$cruft" && + sed -e "1,/^$/d" patch-0 + } >patch-cruft && + git patch-id actual && + test_cmp actual expect + ' +done + test_expect_success 'whitespace is irrelevant in footer' ' get_patch_id main && git checkout same &&