From patchwork Tue Jul 30 01:17:34 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: 13746144 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 C0F818BE8 for ; Tue, 30 Jul 2024 01:17: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=1722302267; cv=none; b=fL/nUA6FJTBKGzgiuVKltso3QiCxQ36RRIwzqpR7pXjYzENStQPy0RhfePRlqn+7rcWNBHpHgWhlx38FnmuFV+hmEXZY27RaSaYEWX/siE+pIBgkCpZhk1zBoMQvncT0wao0xyyJkSJA+Yu7ZzWxwAV4s3/Ti217dewhGppwHPg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722302267; c=relaxed/simple; bh=sIm/uZmLRU8NrUA2POFrmOidfpI8ely9odWo7sFRetc=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ik84QNtsmSCZ/4g0441ut5Mwz2L9V7NFYyXbLyZfsK0VT9DpDESGG2XpLY8YF3X4GW+BZtwqOhfxPPpYIojvukozR1qwJZ2lBrIm5Mu+WfgP/fSXh7FyQI0yTCiJGrXzly8Nv4tFbQrRVBq0jM7DNB8YhDytN5R/wBrKWd8bZYI= 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=C3ur/07Q; 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="C3ur/07Q" Received: from pb-smtp20.pobox.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id 2AED530A4D; Mon, 29 Jul 2024 21:17:45 -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=sIm/uZmLRU8NrUA2POFrmOidf pI8ely9odWo7sFRetc=; b=C3ur/07Qqv+SMGe9E1jdg3ID2FdnRE93GK63qXUfu SJsD39wOVblviwPKBeGXoBNt3EmoknU1GPwQP0XONnlJVGb9XG1zKzMFjtwM2d8F T4RJSXu5AccJ03CuJ1XRLK2FsvSx5rQKQ9Qmj7KibqcGsNnstI8VrBM+IBfKwXTk aA= Received: from pb-smtp20.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id 2412830A4C; Mon, 29 Jul 2024 21:17:45 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.125.139.61]) (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 8910830A4B; Mon, 29 Jul 2024 21:17:41 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH v2 1/5] t4204: patch-id supports various input format Date: Mon, 29 Jul 2024 18:17:34 -0700 Message-ID: <20240730011738.4032377-2-gitster@pobox.com> X-Mailer: git-send-email 2.46.0-71-g1aa693ace8 In-Reply-To: <20240730011738.4032377-1-gitster@pobox.com> References: <20240621231826.3280338-1-gitster@pobox.com> <20240730011738.4032377-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: 8479F688-4E11-11EF-9D07-92D9AF168FA5-77302942!pb-smtp20.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 Tue Jul 30 01:17:35 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: 13746145 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 920298C0B for ; Tue, 30 Jul 2024 01:17:46 +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=1722302268; cv=none; b=cHm0soCDPOpkM8eKXO4RpLPCDv7T/tCOvm5R7IYKENDvkF5Jtb9D3gPHYY0Sk8z5mQUNUamPWUiBMO0My+oGs+IDFREeuTqo1llzuKEOFuOCeTxq7hCeHHPZSnDF3LG44vzggkDcTWRTBf4MS4tOYPmM7NY892DIezEvzcB8a/8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722302268; c=relaxed/simple; bh=UcgcfI/FE1M35x1BylLs9zx58w+hlce1WdVJZHPIwYI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZeatBRPVr1y0z4gplZ3+aI+Dl163X7/WGaw5hXx73Ciy7K86+0mgj6SDza0jDNAuGpdA7zjXDWyYX8+MFM1KbntEdctI1FOlI9L84UAUYaFP34Asj5DZ4fBhf4IOmhfFJlo/wHdkVfw9O0BgCLkEsTlA7IJZjSnz/gp0/86YUyE= 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=yNcAUDc2; 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="yNcAUDc2" Received: from pb-smtp1.pobox.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id 8D2802CEBE; Mon, 29 Jul 2024 21:17:45 -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=UcgcfI/FE1M35x1BylLs9zx58 w+hlce1WdVJZHPIwYI=; b=yNcAUDc29064ygXMWyDE+wZwhwU1ym/5QhR3LouN0 eXlZCwPojKBsO1eAtzIeI05Jxnbp5OLHOiw7EYuyEszXVjAJaAOtziyIOEUWs444 ZOt/7li/RvjeMDJayLilkTEflvSgrl7rebPVzPmGHv/q3kD8cYMLwlHEFs0VtFzK bc= Received: from pb-smtp1.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp1.pobox.com (Postfix) with ESMTP id 8538A2CEBD; Mon, 29 Jul 2024 21:17:45 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.125.139.61]) (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 F19F12CEBC; Mon, 29 Jul 2024 21:17:44 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH v2 2/5] patch-id: call flush_current_id() only when needed Date: Mon, 29 Jul 2024 18:17:35 -0700 Message-ID: <20240730011738.4032377-3-gitster@pobox.com> X-Mailer: git-send-email 2.46.0-71-g1aa693ace8 In-Reply-To: <20240730011738.4032377-1-gitster@pobox.com> References: <20240621231826.3280338-1-gitster@pobox.com> <20240730011738.4032377-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: 8684FD38-4E11-11EF-ACE6-34EEED2EC81B-77302942!pb-smtp1.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 Tue Jul 30 01:17:36 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: 13746147 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 1B8EAC2C6 for ; Tue, 30 Jul 2024 01:17:50 +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=1722302272; cv=none; b=GKcj0TyEIVQdm0zCwP6yAIfQLoY8GSuq7UE5g6Geb6rbNMva3RGt4empriXlTjmFkxUvPwFgIcHjPpefvV+yqRmIWXthXGRe2bTRnzLhai4UZT5WS562KIpPVZNKNRwn/SXg1pAgGZrBecr+PJyb+tLCduU9krHnd1+nHzg1LHk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722302272; c=relaxed/simple; bh=DAhTwaJlv04Kj9cz5l4czl0JNwykokjSC6CzbWcv9BM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=h/EeC18tMp5x4t6PADlT4/4ocScxlSnXcL8v1qWUaNat4QZnccZU+5zO6SeEBIg9jz7r6mAGO5b/ZAhNbVtl72LMj2HimIOYuoPCikRjpZmaOuPrWNlwoUpy8J54V27ynm/vKuvAn53d7CAlv8PhjEDvH8PU7lmSdhHS5goVLHw= 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=M1vo1GeH; 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="M1vo1GeH" Received: from pb-smtp21.pobox.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id 8B13D3B20C; Mon, 29 Jul 2024 21:17:50 -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=DAhTwaJlv04Kj9cz5l4czl0JN wykokjSC6CzbWcv9BM=; b=M1vo1GeHxXrNgdBeAUx0tU2vJHnWM+L74MQaGopN6 ODo/VH5G0x2/bY6PvsEm4XWeL6J0yetRFygcHWPQ9oh6mq10z763/C6z5VKvO18r J2cGXUZjSODk7Yrrkxy2b4hBKcu0NFN6W+rbPgoTKK9Xhr+q0d5KSXiX1lzgozUH us= Received: from pb-smtp21.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp21.pobox.com (Postfix) with ESMTP id 845CD3B20B; Mon, 29 Jul 2024 21:17:50 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.125.139.61]) (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 1BB4A3B20A; Mon, 29 Jul 2024 21:17:47 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH v2 3/5] patch-id: make get_one_patchid() more extensible Date: Mon, 29 Jul 2024 18:17:36 -0700 Message-ID: <20240730011738.4032377-4-gitster@pobox.com> X-Mailer: git-send-email 2.46.0-71-g1aa693ace8 In-Reply-To: <20240730011738.4032377-1-gitster@pobox.com> References: <20240621231826.3280338-1-gitster@pobox.com> <20240730011738.4032377-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: 87C8B0E0-4E11-11EF-8FCD-9625FCCAB05B-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 | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/builtin/patch-id.c b/builtin/patch-id.c index 0f262e7a03..1d3b7ff12b 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -58,9 +58,19 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after) return 1; } +/* + * flag bits to control get_one_patchid()'s behaviour. + */ +enum { + GOPID_STABLE = (1<<0), /* --stable */ + GOPID_VERBATIM = (1<<1), /* --verbatim */ +}; + 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 +181,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 +189,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 +228,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 +248,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 Tue Jul 30 01:17:37 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: 13746148 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 4B0C428EB for ; Tue, 30 Jul 2024 01:17:52 +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=1722302273; cv=none; b=d/zKifoLP3Szjxke1t9i3LLbMRNkBJZ5KNGxVdqcH9jspRa21MI4TwWCLDTJifVGBYjl57UluL7dMiR63+UEpsTaaKgZTDgaXGmtIkfAPkoTS3jz9jeXz8aq3N2I7nlft3uVU/TKVjxLHZXtCAK/ZcjpxR+SXhgOmakRjeJvK3s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722302273; c=relaxed/simple; bh=k+gfZd9AzS/qBbbr/ccflSHOvUawzGy/6BdwE7sZYaQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=inLYOMser9DkXsZbJGuW2/7u74UIMUeOliWXC9jYXTq8GFeg1z/YgNe6/KTbWDuXJpPtG3PgwbhDVj2i+8Uo9faU5EXTXHLXzht0QvRvvr9OxwIB71pW+OroFCDuQCoYbLbSfIJlPIW7KT8ZgFeERHs/tXyvKpBSHWOKDqwpU3E= 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=oRENUmI+; 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="oRENUmI+" Received: from pb-smtp2.pobox.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 12EE227F54; Mon, 29 Jul 2024 21:17:51 -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=k+gfZd9AzS/qBbbr/ccflSHOv UawzGy/6BdwE7sZYaQ=; b=oRENUmI+aSoLUCcQ1L8dgZ0R4gKvltQOMCKr+Xm0f ke4nWH0xyXnS8ZC7zpcGGfncwZ6IDzVVV0YMyPuqtfZXmj/G9VlndbUFC+12ToSZ lIQ4cpR3LNEgXI4in8kfjPlHyaRDwA/H6fvRiUO22UzyXHu+TynRSoXJyQRPpVtZ CY= Received: from pb-smtp2.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 0B80C27F53; Mon, 29 Jul 2024 21:17:51 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.125.139.61]) (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 6B55C27F52; Mon, 29 Jul 2024 21:17:50 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH v2 4/5] patch-id: rewrite code that detects the beginning of a patch Date: Mon, 29 Jul 2024 18:17:37 -0700 Message-ID: <20240730011738.4032377-5-gitster@pobox.com> X-Mailer: git-send-email 2.46.0-71-g1aa693ace8 In-Reply-To: <20240730011738.4032377-1-gitster@pobox.com> References: <20240621231826.3280338-1-gitster@pobox.com> <20240730011738.4032377-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: 89C4BACE-4E11-11EF-B9C5-BAC1940A682E-77302942!pb-smtp2.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 what "git diff-tree --stdin" shows; (2) "commit ", which is what "git log" shows; or (3) "From ", which is what "git log --format=email" shows. 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 1d3b7ff12b..a2fdc0505d 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -85,16 +85,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; } @@ -135,6 +138,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 Tue Jul 30 01:17:38 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: 13746149 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 32D7DDDCD for ; Tue, 30 Jul 2024 01:17:56 +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=1722302277; cv=none; b=GZIZbXG1aZpyfy1mlg1C7rSqAmNCZhpQ+ryo6adYB2NoNIRtRGbmY9FWWE7O2KPfirYtiXlUcXoXC45/g6uSqJR1l11+eXaMqRQorfxJ9UWxmeruE19rhIBvK0kInKpeQ//3bhiYsRn8MUWhjo+AbQH6wNgSF6C3LB2fvufJOFs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722302277; c=relaxed/simple; bh=XVGOSQ+UIm/0pOdHM4vpIKXj2vxbUX1IrFWisDUQ6Lo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VVv43YEdAbg0HdkbwfK1Pz9HjVgKK0BROLAixooDkFkOiWFPvG19+kqcMtuQ4IvP40aDMxcYxZ7QmDZCOIhoAbVsN3HdLtkT0x2WKKvBvVABdcFyIR2wlyO002MeZ8Y4mamDQ4CHDp6n1/kfPn3muYHG4ZuwqK+9ycAfMKovy9I= 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=CsAwzmXf; 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="CsAwzmXf" Received: from pb-smtp20.pobox.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id E472A30A50; Mon, 29 Jul 2024 21:17:55 -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=XVGOSQ+UIm/0pOdHM4vpIKXj2 vxbUX1IrFWisDUQ6Lo=; b=CsAwzmXfD7t2wiUuu+45qw9I/5PLfHdVgPy2v3kWv yAptzE1uFO2/L2uQFx9I0+3MEYcv2XTaHI/2OQegF7Oj942OfgUiG4z+hS/E/z7e DpDNoRgC5K9DyWoiUwvgrYYifcpid7GR/620Hga9ydsZbi5ietR8ZhsNBdyJpcIK FI= Received: from pb-smtp20.sea.icgroup.com (unknown [127.0.0.1]) by pb-smtp20.pobox.com (Postfix) with ESMTP id DCF4D30A4F; Mon, 29 Jul 2024 21:17:55 -0400 (EDT) (envelope-from gitster@pobox.com) Received: from pobox.com (unknown [34.125.139.61]) (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 67CFE30A4E; Mon, 29 Jul 2024 21:17:52 -0400 (EDT) (envelope-from gitster@pobox.com) From: Junio C Hamano To: git@vger.kernel.org Subject: [PATCH v2 5/5] patch-id: tighten code to detect the patch header Date: Mon, 29 Jul 2024 18:17:38 -0700 Message-ID: <20240730011738.4032377-6-gitster@pobox.com> X-Mailer: git-send-email 2.46.0-71-g1aa693ace8 In-Reply-To: <20240730011738.4032377-1-gitster@pobox.com> References: <20240621231826.3280338-1-gitster@pobox.com> <20240730011738.4032377-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: 8AF3778C-4E11-11EF-B989-92D9AF168FA5-77302942!pb-smtp20.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 | 49 +++++++++++++++++++++++++++++++++------------ t/t4204-patch-id.sh | 17 ++++++++++++++++ 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/builtin/patch-id.c b/builtin/patch-id.c index a2fdc0505d..f2e8feb6d3 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -60,10 +60,17 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after) /* * flag bits to control get_one_patchid()'s behaviour. + * + * STABLE/VERBATIM are given from the command line option as + * --stable/--verbatim. FIND_HEADER conveys the internal state + * maintained by the caller to allow the function to avoid mistaking + * lines of log message before seeing the "diff" part as the beginning + * of the next patch. */ enum { GOPID_STABLE = (1<<0), /* --stable */ GOPID_VERBATIM = (1<<1), /* --verbatim */ + GOPID_FIND_HEADER = (1<<2), /* stop at the beginning of patch message */ }; static int get_one_patchid(struct object_id *next_oid, struct object_id *result, @@ -71,6 +78,7 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result, { 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; @@ -86,26 +94,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") || @@ -201,11 +222,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 &&