From patchwork Thu May 2 04:54:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13651267 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA5FA24A0E for ; Thu, 2 May 2024 04:54:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625674; cv=none; b=K20RMgyMyVkmXE9XOrF7YMc9ayFd3wjzzABr1E+5tcbc+eeL6zd+qRMqBDzSnR5WHNvVWg8UCY0kDCaG7BpDIQvGdb6CUu//lqR2MJaSWmIK0azDKFmPxiU2biZ4LvZnaI066JIRSMNfg3V1HLSryTgU8CkxSnwq7rmo9IUcKNI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625674; c=relaxed/simple; bh=yTTNOJw+bgqYx6sq4o7FfuHJXJwSHwSeBLlGpNLuJpA=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=tyhXkkvxa/sa6qJMw/QYOS49LjXc4xi1+deb981tyra3HzY7d+kTqZFkSMRRwgGQQqgBSplFEyc2/7Xhe+jh6giZJafQkTSqJSoCHadq2RCaVLOFkjsYyfL6BFH4dpbKI3Mi/pxWce5nyvZtKSddrVC8zS9BG0WEzqy2zYcydcg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WCfZ/yce; arc=none smtp.client-ip=209.85.221.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WCfZ/yce" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-34dc9065606so687336f8f.1 for ; Wed, 01 May 2024 21:54:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714625671; x=1715230471; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=Bgga1YobWZAZi6qF0+Oew5+ux5Rz6Z1eug/vhXlcR34=; b=WCfZ/ycexUMLbElm+SuNyESdPcpmye9S2gGoVnvmofbue6sTvszdWXd38eOq2BMnZV /SCz4i6OMfynCtoZs82GZXjGP603czOqIT0dazRX6QnhM2U/KzGXhMvCAJoj0832ik0c psa643l6nDdThNI9V5bMLrPadPBlKrZVr3lOfpOGggD5TCYdotm+Rph8vg03cOAgWsRH xEBhjlz34B9EBy9Xb2HNokJJgOr4j8uW85zMO4uFQwHSGEHNbt1fdcOdEnBm35wufusf 6ox9QSQHzGSNvU5xSiveiVnLDW5JkdKrJHkDV+QR05zmmd9dODlg+uv6o/nb4w2tn3tg +Q1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714625671; x=1715230471; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Bgga1YobWZAZi6qF0+Oew5+ux5Rz6Z1eug/vhXlcR34=; b=m3OBjTCT8GplxjtysUU21Sluxl3Czx+Ty3SmlOBlMM+fNnxWcB7q0rLojDfGwhRyvO ceRn/2gsYlBZgpCqqZFiHICzz6o57nX1/vMsLlTYwhgw8Isrq2DgCYFk846L63V5RaLR Dy53f1IwrSflTQ+0VrFs56KVTvnNtL21OUU5xfpiBsdZGCKxIWH6xf85D5K2KJUUd4XZ kRs5G+34uR4FpbRfvy0Q6wMTsNIpdh6v2GrX8+Qg12iLAnbr+DZd2u3W3ZIXBN38QPQE C6BZybNZRNzSkEx2rnr7cefjEfnZv7xfwQ6P+aW65LkpaKlcS29rc0HpTwEmKpFFpj5g JAqQ== X-Gm-Message-State: AOJu0YxaGmet70Y33XDd5HOSi9qVLTh18iFDR58AkrWCT9d5BGEnsfVK Qe/ecp4rOuPhGN47Ux94WAjmC0Qz6du+JaRJXNT5MHGAaGWNMrQ/o0CLuQ== X-Google-Smtp-Source: AGHT+IHccp7iz/CNfOvmz66ReZSBlF+nRx9QoXebCb9tR6wkeaXtNNFw+6MZ4vRrN3mx7Nrk/GonjQ== X-Received: by 2002:adf:a30d:0:b0:34c:c53e:f2ca with SMTP id c13-20020adfa30d000000b0034cc53ef2camr1072167wrb.14.1714625670746; Wed, 01 May 2024 21:54:30 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j30-20020a5d6e5e000000b0034db5f648a8sm244105wrz.77.2024.05.01.21.54.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:54:30 -0700 (PDT) Message-Id: <8a9f71442d8a5ae6bb0cc9e2d098e85b7e4a4764.1714625667.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 02 May 2024 04:54:18 +0000 Subject: [PATCH v4 01/10] Makefile: sort UNIT_TEST_PROGRAMS Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Christian Couder , Kristoffer Haugsbakk , Linus Arver , Linus Arver , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Signed-off-by: Linus Arver --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 4e255c81f22..d3a3f16f076 100644 --- a/Makefile +++ b/Makefile @@ -1343,10 +1343,10 @@ THIRD_PARTY_SOURCES += sha1collisiondetection/% THIRD_PARTY_SOURCES += sha1dc/% UNIT_TEST_PROGRAMS += t-basic -UNIT_TEST_PROGRAMS += t-mem-pool -UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-ctype +UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-prio-queue +UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o From patchwork Thu May 2 04:54:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13651269 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D72912E75 for ; Thu, 2 May 2024 04:54:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625675; cv=none; b=QB+31m/Fw0bdzcgrBE8ShR0CZ+81QwwM5BB2RJjq0yf+9nTU3ZbBHiZCHCr/XixdJ7116WzQ8ui0ui6bQHJRcrH/UtDS+TynCzAMCc2QDG0C7NH2uXUk9abfjBC2TBCtUep9I8J7H1EhG0wp/nBCfTerT4kT74pG7eGslKzV980= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625675; c=relaxed/simple; bh=g6JeJcLryUUQ1Z+s9u08hvV+Ey/BjT/kf7oFT8phA2k=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=szebRBy4weohEs1uNJWifTK5AUDNZzn6WxRA1MLG3RQpAo7AMwrGd6VIHw1xMzInPyYoID4iDOJ+FGcSGy+rnNgPvKpaDL70fimaK++ed8WHyrBWDWqO6OC6Vl/2KyoI+2AGSgagKzuKxYWmtk+y6Mo/YXaje7JiXZK0+gv1sbY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hmDIxHpR; arc=none smtp.client-ip=209.85.221.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hmDIxHpR" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-34e040ed031so450057f8f.0 for ; Wed, 01 May 2024 21:54:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714625672; x=1715230472; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=tFyr8DwpcDs6uJW2CB97JjAvFQRz2zEA8znyg8RYS+U=; b=hmDIxHpRjtUqGLmAe5sLGQRissTfRg4JQ9gw6q5z/r4Ebm4QSb4AbhXSPJoT8IhOCo v7lWr/kQ8HS9lAGQ5P4I/hbHPHfu8C2FbnYr+ky2VHqOYMKUFQ69GKHln7TwRspnYK/A g4Oe3j+oT4bqEjZzzuzWklairncRiCAJcoWJCkP1R/SdJ2YY74Hmvu700p0IFQeYgack 8k6Q7paExDAoBp7O+f5VkkyuKCD61cqd/KeycBp4CcqSE2s6xE79pFPk8/QlANExBiy7 40/CpEUaMxe23Pz24PZ5R47ZkDXvDFBJXvSwwGhRBjvRrdZlFoX4/3A18ws9OURlUsfJ Iifw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714625672; x=1715230472; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tFyr8DwpcDs6uJW2CB97JjAvFQRz2zEA8znyg8RYS+U=; b=N12Jxqg5gLV3KFhaR+QTOLs9sljpTHjksTm+xNzM8cJanH6ShyU4lqIRw+IIoLNpc3 92BvyrBGn01AISVUHgS94mkWFZ+Fiqe5oVGavnMdzjBqUEX4NPww1TYCJAkRbSnElPfi vy0KTfPALvxe5CCJsMLWthu4xutmQco8vsRp75q9yw5VbJKlApaLkEsC5vOKAB82iQYp 3o139TGX0kmGrJpAhbghbqW9HIaI4iunkVYBuUEdYFyi8H3e7q4xQlUu61EI+c/A/Om3 dO98EKTe3m+0Q8PT0bFJ48czsulWv0NXTZr1zjkhSTsdqqjCI2hfhId82STkHLxqrhMC G1mA== X-Gm-Message-State: AOJu0Yy42CrPJtSBSMY3L7PV40Tx+qMCg7oV0HzFysjtWiv0HuYNZqAU wGQs57D6kQn1CTl+hhTBpWvPac/FoFi0QuJUErQO51SWUcPYTyIrIWHe5Q== X-Google-Smtp-Source: AGHT+IG5E7MzIEkYxowJMzpd+aj6jHqhauUSXHx6D2CzWs+yWVrjJ81GXUCNXS21hCSXl0m1F7s+GA== X-Received: by 2002:a5d:6047:0:b0:34c:f8ad:f782 with SMTP id j7-20020a5d6047000000b0034cf8adf782mr2556485wrt.40.1714625671766; Wed, 01 May 2024 21:54:31 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id o10-20020a5d47ca000000b0034d9e5411ebsm248618wrc.45.2024.05.01.21.54.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:54:31 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Thu, 02 May 2024 04:54:19 +0000 Subject: [PATCH v4 02/10] trailer: add unit tests for trailer iterator Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Christian Couder , Kristoffer Haugsbakk , Linus Arver , Linus Arver , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Test the number of trailers found by the iterator (to be more precise, the parsing mechanism which the iterator just walks over) when given some some arbitrary log message. We test the iterator because it is a public interface function exposed by the trailer API (we generally don't want to test internal implementation details which are, unlike the API, subject to drastic changes). Signed-off-by: Linus Arver --- Makefile | 1 + t/unit-tests/t-trailer.c | 174 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 t/unit-tests/t-trailer.c diff --git a/Makefile b/Makefile index d3a3f16f076..5418ddd03be 100644 --- a/Makefile +++ b/Makefile @@ -1347,6 +1347,7 @@ UNIT_TEST_PROGRAMS += t-ctype UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-prio-queue UNIT_TEST_PROGRAMS += t-strbuf +UNIT_TEST_PROGRAMS += t-trailer UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o diff --git a/t/unit-tests/t-trailer.c b/t/unit-tests/t-trailer.c new file mode 100644 index 00000000000..c1f897235c7 --- /dev/null +++ b/t/unit-tests/t-trailer.c @@ -0,0 +1,174 @@ +#include "test-lib.h" +#include "trailer.h" + +static void t_trailer_iterator(const char *msg, size_t num_expected_trailers) +{ + struct trailer_iterator iter; + size_t i = 0; + + trailer_iterator_init(&iter, msg); + while (trailer_iterator_advance(&iter)) + i++; + trailer_iterator_release(&iter); + + check_uint(i, ==, num_expected_trailers); +} + +static void run_t_trailer_iterator(void) +{ + static struct test_cases { + const char *name; + const char *msg; + size_t num_expected_trailers; + } tc[] = { + { + "empty input", + "", + 0 + }, + { + "no newline at beginning", + "Fixes: x\n" + "Acked-by: x\n" + "Reviewed-by: x\n", + 0 + }, + { + "newline at beginning", + "\n" + "Fixes: x\n" + "Acked-by: x\n" + "Reviewed-by: x\n", + 3 + }, + { + "without body text", + "subject: foo bar\n" + "\n" + "Fixes: x\n" + "Acked-by: x\n" + "Reviewed-by: x\n", + 3 + }, + { + "with body text, without divider", + "my subject\n" + "\n" + "my body which is long\n" + "and contains some special\n" + "chars like : = ? !\n" + "hello\n" + "\n" + "Fixes: x\n" + "Acked-by: x\n" + "Reviewed-by: x\n" + "Signed-off-by: x\n", + 4 + }, + { + "with body text, without divider (second trailer block)", + "my subject\n" + "\n" + "my body which is long\n" + "and contains some special\n" + "chars like : = ? !\n" + "hello\n" + "\n" + "Fixes: x\n" + "Acked-by: x\n" + "Reviewed-by: x\n" + "Signed-off-by: x\n" + "\n" + /* + * Because this is the last trailer block, it takes + * precedence over the first one encountered above. + */ + "Helped-by: x\n" + "Signed-off-by: x\n", + 2 + }, + { + "with body text, with divider", + "my subject\n" + "\n" + "my body which is long\n" + "and contains some special\n" + "chars like : = ? !\n" + "hello\n" + "\n" + "---\n" + "\n" + /* + * This trailer still counts because the iterator + * always ignores the divider. + */ + "Signed-off-by: x\n", + 1 + }, + { + "with non-trailer lines in trailer block", + "subject: foo bar\n" + "\n" + /* + * Even though this trailer block has a non-trailer line + * in it, it's still a valid trailer block because it's + * at least 25% trailers and is Git-generated (see + * git_generated_prefixes[] in trailer.c). + */ + "not a trailer line\n" + "not a trailer line\n" + "not a trailer line\n" + "Signed-off-by: x\n", + 1 + }, + { + "with non-trailer lines (one too many) in trailer block", + "subject: foo bar\n" + "\n" + /* + * This block has only 20% trailers, so it's below the + * 25% threshold. + */ + "not a trailer line\n" + "not a trailer line\n" + "not a trailer line\n" + "not a trailer line\n" + "Signed-off-by: x\n", + 0 + }, + { + "with non-trailer lines (only 1) in trailer block, but no Git-generated trailers", + "subject: foo bar\n" + "\n" + /* + * This block has only 1 non-trailer out of 10 (IOW, 90% + * trailers) but is not considered a trailer block + * because the 25% threshold only applies to cases where + * there was a Git-generated trailer. + */ + "Reviewed-by: x\n" + "Reviewed-by: x\n" + "Reviewed-by: x\n" + "Helped-by: x\n" + "Helped-by: x\n" + "Helped-by: x\n" + "Acked-by: x\n" + "Acked-by: x\n" + "Acked-by: x\n" + "not a trailer line\n", + 0 + }, + }; + + for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) { + TEST(t_trailer_iterator(tc[i].msg, + tc[i].num_expected_trailers), + "%s", tc[i].name); + } +} + +int cmd_main(int argc, const char **argv) +{ + run_t_trailer_iterator(); + return test_done(); +} From patchwork Thu May 2 04:54:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13651270 Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C8C5A24A0E for ; Thu, 2 May 2024 04:54:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625677; cv=none; b=TD+vFZJ+Np4XVbWuXLE0IgVmCIGDPlyzUvFQnK5/gkXz7jpBq/cX+ODrnjamNTVx+FQlDv73msEc02Nxeyp1TWa8EDvaETCDUV8ScRIgnYK/6YNQRo3cwUQB1QK//H/JvQGjL5gQHRyIuOgNk7cxXO+4yk6eYuQXcZbajXZXWQQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625677; c=relaxed/simple; bh=0dq7EZz69LBcz5cHGHAl5MdUisMDkm7R8Z3I/8TkhYc=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=O12aMyiXVm2OfaeUXdDDNwBjrTrF0lqVsF4EHWNngXBQ0FIBtoMNNYUJKvcYzj6/g/DlvqVaxfThfyF23nMHnqSvDt/4K1PFr+ER01uKVu7Nwv6tk79plMVjP3rOczM313kqI+L0O0mUSghMvriGScHXuuuAecIYKlJP7+HHp94= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WqclUX4V; arc=none smtp.client-ip=209.85.208.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WqclUX4V" Received: by mail-lj1-f178.google.com with SMTP id 38308e7fff4ca-2dd6c14d000so91688511fa.0 for ; Wed, 01 May 2024 21:54:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714625673; x=1715230473; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=NVtPJ0vp+RWJgaMX0JHtnOVHtN9uHmXYYn1fiQs8ZC4=; b=WqclUX4VLcDarLljfOGpqOKrXuCStx82Z/f+bcxfr4aaRAaCbeeGziyMNQmW2gWIcB m5hSAhos5Vnt5mHfDImQipWJCiwpUPSeJV9/QUqWgt0/VMvpvR4Au1hzf7kq3ROygYq1 57EBs9G0Gs0zQ6CQAH/ugnwvfLYiOA9PMGRoz5Mm0NalM5QIHjYTNQAZYkAJOEYF+1CM zfr+EMDASWO+4cqHnO47ohg5HGx+l7sUJ8vowLDxycjDyWUs7VyUnQFGGJ62Fd+NpGQz o1FhAtSZ4ahdi48ICFH+lse3i6jZ7Qqo9izAJ5QsQ0ta3kwJ2rzJ9Z5ja8IzHmOJoFWu 1EfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714625673; x=1715230473; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NVtPJ0vp+RWJgaMX0JHtnOVHtN9uHmXYYn1fiQs8ZC4=; b=UcDPIiGw+LVrs/fhAKcMB5kKP6HeBNMut/S3skP8oP6a5bSO5vcRsr8S1CWz2Qq9/h nvZPRZyJfk5l2yHs66uKHx5qZYCfRtugFWePoG6TRlIDtvPowqcOLSOgk55miEcPXU8q eH05K9wbc2qIEksgGM8lRvAgJWDn3eQ8bvJ2PnU8tmWkVZbmp81vLW4yE08xJzz2tY0D DPj+YbAYUxShDweB+Yr6WH7u/2i/2jWTxWXrEq+5Df+3JrQn1MZYP4KDAI/hMSXPoSMR shD0j8ErWM8IFIyFdDoU8JlZkLQWOXNQcNPQBpT7aoD2YUVPLT3wHrM7F7PmTxigzHxA RzaQ== X-Gm-Message-State: AOJu0Yy6DL9Co85lYt6+/zWvolsroHF+MB7skmMoAjtP1rhysmgJA+0A SjTCsKrj1IKWQJ3tSBi+HbiVyfYiiA6uY5cq8HMK2XW5wuwSTX9Lc4B8zg== X-Google-Smtp-Source: AGHT+IH178GrwQ5lKIc2vODGHui0ZGTlQ9Xtp4fESA4mJcoeYP6z5/zl//jqzD5Jd2Ihx2A2b1atJQ== X-Received: by 2002:a2e:ba14:0:b0:2dc:b467:cb32 with SMTP id p20-20020a2eba14000000b002dcb467cb32mr497211lja.14.1714625673203; Wed, 01 May 2024 21:54:33 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id r9-20020a05600c35c900b0041bf5b9fb93sm511167wmq.5.2024.05.01.21.54.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:54:32 -0700 (PDT) Message-Id: <4aeb48050b14e44ec65cfa651a4d98587a6cd860.1714625668.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 02 May 2024 04:54:20 +0000 Subject: [PATCH v4 03/10] trailer: teach iterator about non-trailer lines Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Christian Couder , Kristoffer Haugsbakk , Linus Arver , Linus Arver , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Previously the iterator did not iterate over non-trailer lines. This was somewhat unfortunate, because trailer blocks could have non-trailer lines in them since 146245063e (trailer: allow non-trailers in trailer block, 2016-10-21), which was before the iterator was created in f0939a0eb1 (trailer: add interface for iterating over commit trailers, 2020-09-27). So if trailer API users wanted to iterate over all lines in a trailer block (including non-trailer lines), they could not use the iterator and were forced to use the lower-level trailer_info struct directly (which provides a raw string array that includes all lines in the trailer block). Change the iterator's behavior so that we also iterate over non-trailer lines, instead of skipping over them. The new "raw" member of the iterator allows API users to access previously inaccessible non-trailer lines. Reword the variable "trailer" to just "line" because this variable can now hold both trailer lines _and_ non-trailer lines. The new "raw" member is important because anyone currently not using the iterator is using trailer_info's raw string array directly to access lines to check what the combined key + value looks like. If we didn't provide a "raw" member here, iterator users would have to re-construct the unparsed line by concatenating the key and value back together again --- which places an undue burden for iterator users. The next commit demonstrates the use of the iterator in sequencer.c as an example of where "raw" will be useful, so that it can start using the iterator. For the existing use of the iterator in builtin/shortlog.c, we don't have to change the code there because that code does trailer_iterator_init(&iter, body); while (trailer_iterator_advance(&iter)) { const char *value = iter.val.buf; if (!string_list_has_string(&log->trailers, iter.key.buf)) continue; ... and the if (!string_list_has_string(&log->trailers, iter.key.buf)) condition already skips over non-trailer lines (iter.key.buf is empty for non-trailer lines, making the comparison still work even with this commit). Rename "num_expected_trailers" to "num_expected" in t/unit-tests/t-trailer.c because the items we iterate over now include non-trailer lines. Signed-off-by: Linus Arver --- t/unit-tests/t-trailer.c | 16 +++++++++++----- trailer.c | 12 +++++------- trailer.h | 7 +++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/t/unit-tests/t-trailer.c b/t/unit-tests/t-trailer.c index c1f897235c7..4f640d2a4b8 100644 --- a/t/unit-tests/t-trailer.c +++ b/t/unit-tests/t-trailer.c @@ -1,7 +1,7 @@ #include "test-lib.h" #include "trailer.h" -static void t_trailer_iterator(const char *msg, size_t num_expected_trailers) +static void t_trailer_iterator(const char *msg, size_t num_expected) { struct trailer_iterator iter; size_t i = 0; @@ -11,7 +11,7 @@ static void t_trailer_iterator(const char *msg, size_t num_expected_trailers) i++; trailer_iterator_release(&iter); - check_uint(i, ==, num_expected_trailers); + check_uint(i, ==, num_expected); } static void run_t_trailer_iterator(void) @@ -19,7 +19,7 @@ static void run_t_trailer_iterator(void) static struct test_cases { const char *name; const char *msg; - size_t num_expected_trailers; + size_t num_expected; } tc[] = { { "empty input", @@ -119,7 +119,13 @@ static void run_t_trailer_iterator(void) "not a trailer line\n" "not a trailer line\n" "Signed-off-by: x\n", - 1 + /* + * Even though there is only really 1 real "trailer" + * (Signed-off-by), we still have 4 trailer objects + * because we still want to iterate through the entire + * block. + */ + 4 }, { "with non-trailer lines (one too many) in trailer block", @@ -162,7 +168,7 @@ static void run_t_trailer_iterator(void) for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) { TEST(t_trailer_iterator(tc[i].msg, - tc[i].num_expected_trailers), + tc[i].num_expected), "%s", tc[i].name); } } diff --git a/trailer.c b/trailer.c index 3e4dab9c065..4700c441442 100644 --- a/trailer.c +++ b/trailer.c @@ -1146,17 +1146,15 @@ void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) int trailer_iterator_advance(struct trailer_iterator *iter) { - while (iter->internal.cur < iter->internal.info.trailer_nr) { - char *trailer = iter->internal.info.trailers[iter->internal.cur++]; - int separator_pos = find_separator(trailer, separators); - - if (separator_pos < 1) - continue; /* not a real trailer */ + if (iter->internal.cur < iter->internal.info.trailer_nr) { + char *line = iter->internal.info.trailers[iter->internal.cur++]; + int separator_pos = find_separator(line, separators); + iter->raw = line; strbuf_reset(&iter->key); strbuf_reset(&iter->val); parse_trailer(&iter->key, &iter->val, NULL, - trailer, separator_pos); + line, separator_pos); /* Always unfold values during iteration. */ unfold_value(&iter->val); return 1; diff --git a/trailer.h b/trailer.h index 9f42aa75994..7e36da7d13c 100644 --- a/trailer.h +++ b/trailer.h @@ -125,6 +125,13 @@ void format_trailers_from_commit(const struct process_trailer_options *, * trailer_iterator_release(&iter); */ struct trailer_iterator { + /* + * Raw line (e.g., "foo: bar baz") before being parsed as a trailer + * key/val pair as part of a trailer block (as the "key" and "val" + * fields below). If a line fails to parse as a trailer, then the "key" + * will be the entire line and "val" will be the empty string. + */ + const char *raw; struct strbuf key; struct strbuf val; From patchwork Thu May 2 04:54:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13651271 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A6DF286A6 for ; Thu, 2 May 2024 04:54:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625677; cv=none; b=TtxVSrTy4BZjG0YkCpIX79UbKtrkbzliMAQagAd9L/ZfLjPfkY7gYN9KufYcLoOBcq1EvC313SUkq3U/E4ka2LkRmm1vKdDWEU6cCshJRB/fWEbUgujBGtHsQ/Y4rww5S6t2Wexs2qHnj/M3ry5uHQqDBM0NwhV3Ujrp/VZKZug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625677; c=relaxed/simple; bh=oEQBb/9zguig5IXXgeMlUxPxea0HsztQ9iTJrBw+gEo=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=tI+yrETIYItHF8QB2uBvRS2TDRF40Z4XOxZNuVcIRPR/mDUEKKbN4HhhIZ19dbz2Bo14OSaYTqPFmN4qC8G5lPjUANOblUJDav9lJDFLsB1aNs+jRUneJ4FMVk7PE7hVqTQzkkCNxofqL4yl1dxeU798JDWnnyq7ioh7aZ7VWws= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=c5h6ccQg; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="c5h6ccQg" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-41ba1ba5592so40144555e9.1 for ; Wed, 01 May 2024 21:54:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714625674; x=1715230474; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=wu37bVUqp7rA7s17b6iU3IeqALt8CAL1m5L5YOMUILE=; b=c5h6ccQgyr64jFq2SXFgucewrRgPd75KgwPZ5cxgXZOQNg9UHhF4t8gUSYgZ8dWMCV Cc55sEW4JQ2UzHiDMK98Nxy4N0yGJ2IvCIbUi505q7RV9RY43eB3fpsT90M5oeWsWBoj +u6f2y4OA95fKRLDlEbfRNr35fsNa+6YsazJvBZm2LvwXrLKxOHfC8oEwTVd+dhpg3he NzeE4AjRdoYKhogDq5x6RQH+kvT8UeGUAX7rmy+6cAjBiSKl/HKgAWSSwOq9OSYc7b6E pVNwcd0eG//j86lqRkqKyt1Ql8fZAkDNTRdsOUIm07hFRmxgyanHdJmbRiN/gAgiaIFb V9gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714625674; x=1715230474; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wu37bVUqp7rA7s17b6iU3IeqALt8CAL1m5L5YOMUILE=; b=kSMbHlm5ISru4wuoCjYgYNO6c0FQzSBca27mgTSJ41kOQulUivZJ/d1j0tCY4zE0Ka u6G+WuiwClG9w0P8Z3regqnwMTjLISq2lEafZdSvYxPB0ugejTyycdF7il3UqOviAJx+ Mk51xOPEv/w7wHvbyqrg8j7PTRfkuf2gLrQtqan9R9tuNwAK/fbN5IHpi0ObqFo/IYxf 3dvdpFzxIqZ5ScLNSpsNHYVdiXT+u8s1ebtFiz1ytdIzmCvcV8uSd/vlX5f2pjH1wOSU PaVBTY1NpvulQ9ECz/+CKda7joh51C5sFKVibRWZ3h8Qebvj3W0SPs6E9mOJvv+U9Ia6 AyRQ== X-Gm-Message-State: AOJu0YxpIc83IGkQy96X4yhqOPmRl/+FYaa5tZ4Rg5T6Nt0EJahNR9Ol yzYkmeUu/c4tMhpSQ77D1h2yFqWPJKFlUyibC3x3d2xX7Jz4quNAQAWImg== X-Google-Smtp-Source: AGHT+IFYGTAcWIMEnjQ/Sx8tKfo65aCtM16SwQZQKUFEgLe3A9jlrbOMdvWVbiHmUS5Xj8BsahQTYg== X-Received: by 2002:a05:600c:3c9a:b0:41a:908c:b841 with SMTP id bg26-20020a05600c3c9a00b0041a908cb841mr779764wmb.32.1714625674037; Wed, 01 May 2024 21:54:34 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m9-20020a05600c4f4900b0041bf7da4200sm498455wmq.33.2024.05.01.21.54.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:54:33 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Thu, 02 May 2024 04:54:21 +0000 Subject: [PATCH v4 04/10] sequencer: use the trailer iterator Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Christian Couder , Kristoffer Haugsbakk , Linus Arver , Linus Arver , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Instead of calling "trailer_info_get()", which is a low-level function in the trailers implementation (trailer.c), call trailer_iterator_advance(), which was specifically designed for public consumption in f0939a0eb1 (trailer: add interface for iterating over commit trailers, 2020-09-27). Avoiding "trailer_info_get()" means we don't have to worry about options like "no_divider" (relevant for parsing trailers). We also don't have to check for things like "info.trailer_start == info.trailer_end" to see whether there were any trailers (instead we can just check to see whether the iterator advanced at all). Note how we have to use "iter.raw" in order to get the same behavior as before when we iterated over the unparsed string array (char **trailers) in trailer_info. Signed-off-by: Linus Arver --- sequencer.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/sequencer.c b/sequencer.c index ea1441e6174..4c1f6c675e7 100644 --- a/sequencer.c +++ b/sequencer.c @@ -319,35 +319,32 @@ static const char *get_todo_path(const struct replay_opts *opts) static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, size_t ignore_footer) { - struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; - struct trailer_info info; - size_t i; + struct trailer_iterator iter; + size_t i = 0; int found_sob = 0, found_sob_last = 0; char saved_char; - opts.no_divider = 1; - if (ignore_footer) { saved_char = sb->buf[sb->len - ignore_footer]; sb->buf[sb->len - ignore_footer] = '\0'; } - trailer_info_get(&opts, sb->buf, &info); + trailer_iterator_init(&iter, sb->buf); if (ignore_footer) sb->buf[sb->len - ignore_footer] = saved_char; - if (info.trailer_block_start == info.trailer_block_end) - return 0; + while (trailer_iterator_advance(&iter)) { + i++; + if (sob && !strncmp(iter.raw, sob->buf, sob->len)) + found_sob = i; + } + trailer_iterator_release(&iter); - for (i = 0; i < info.trailer_nr; i++) - if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) { - found_sob = 1; - if (i == info.trailer_nr - 1) - found_sob_last = 1; - } + if (!i) + return 0; - trailer_info_release(&info); + found_sob_last = (int)i == found_sob; if (found_sob_last) return 3; From patchwork Thu May 2 04:54:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13651272 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67F4612E75 for ; Thu, 2 May 2024 04:54:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625679; cv=none; b=Yl7g/Rx4gWgy+tijIeVmw79v+kSjkiZl0CBwsNQtxmxzWvqyUAOYZMty818JoBJlVVZaG+MFwWV0TK1HrqdmRMFJGBFJ3pHCnTJQQaes/zhoDWWJbPKF9nVdgkxAvFTcbCNMvuQg2XqDg0i0yas8WGp5lBP8FFgO+njx092zQ/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625679; c=relaxed/simple; bh=1gS6Qf9nAbSzGm6Baz/stRHb7AwJhNW2IjtWOYmElqI=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=YPJxtTb2ws8dDXHd4U96nCOons6nXZbO9PMQCJip7EzasnvUvLjSWWwmLb455q2GZYvxONxhAHdmDDdXwS72GJ9G6Grgu3/jrS1Whd/H2VPBzoK6GiowapPSHhN6R2xEFrmNYZGH3C9iLQaPAC74AYxC8YIEz4mkktmAa8SA0pg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VfDZnjH2; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VfDZnjH2" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-41b9dff6be8so39386855e9.3 for ; Wed, 01 May 2024 21:54:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714625675; x=1715230475; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=OmgpG1H0BarOmPn+1t/bIn9KW+Z0AyYuKOS83PwezFk=; b=VfDZnjH2+54TXU1xjEn0fU7yNvJcu9TDhPEJTarJqFfJ/H4qTcVERHFIeoN9U0qcTe 8viQR+y/5J2bKvlcyF5J7GPQ8p0KBv1zY9YnQBbKGce6+aREbFEVfEUCMU2zcY2BGFpt 1pI+UdAYeaEYUhhFfSBK8z17Bm/QFTOsDgkDyf0g6FL4C/s5NPgM6sK5LO9mBTiA2W++ Q1R/hNH2G2G3sT4qbCuELKDpDo+GbZRhFmDQPPmI+BYNuPJy28XgQtpI1DCshFv+ZXpl mdMQBY6DpWYUHCecN+TMVgeTll8cNbpDWKNyHwQoiV6KlH5jvLRTwrpfIxvuFk0y1ZWW sAWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714625675; x=1715230475; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OmgpG1H0BarOmPn+1t/bIn9KW+Z0AyYuKOS83PwezFk=; b=otFYY8wwGhBUtmEypLBot8nEjhzdmybZsG8WhEJCkJtuDlKi8AV9yg/ohtGHM0gwyd y4o8wGP4IpxbnMYWZLeqjbPBsSHXg9UAGPxb/25NM7VF8G5aD2R8hDbWGWkgnu20aCQ2 v5Kdh2CGxFCmkNn9Nb1QE9arFsK9RPvjwH+jHUFWxpKFQuNrBOlcLqu1jIeAkY+D31MC 2bgKDjVxaPaHJtwWVKbzQLgaC2Az/cQsgYawnI8mRP/cLIeJZQzhFe7+K9JaVyY7409Y 6yvASA1CMsB8xl3JwQLgLKp74pvZKiQRepXOhuEZCEFAZgUZ6gTAS6Qon0yRmqvzqmwb 1q8A== X-Gm-Message-State: AOJu0Yzl1mwlse8NOvXEx0dr72tqTAhXL5u8wMLUCIt2NiZeqnVXk+xD A01ySVx0DZo7//PjrTjT2CRCIcO/YnvjU+ooIpoJ9I7pAn0N7VWVy7TSNA== X-Google-Smtp-Source: AGHT+IG48BNMxUOnRJi4qZqkb8SVbuS6au/0uU8TWJXJk1MK52xL9cAbw3Ef01cCs7/Wiug503IfkQ== X-Received: by 2002:a05:600c:474e:b0:41b:f28a:a0c6 with SMTP id w14-20020a05600c474e00b0041bf28aa0c6mr3176234wmo.38.1714625675297; Wed, 01 May 2024 21:54:35 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id r13-20020a05600c35cd00b0041bf45c0665sm4233155wmq.15.2024.05.01.21.54.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:54:34 -0700 (PDT) Message-Id: <44df42ca503a6007e897f8b5b7c6a8c7d7c27f6e.1714625668.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 02 May 2024 04:54:22 +0000 Subject: [PATCH v4 05/10] interpret-trailers: access trailer_info with new helpers Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Christian Couder , Kristoffer Haugsbakk , Linus Arver , Linus Arver , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Instead of directly accessing trailer_info members, access them indirectly through new helper functions exposed by the trailer API. This is the first of two preparatory commits which will allow us to use the so-called "pimpl" (pointer to implementation) idiom for the trailer API, by making the trailer_info struct private to the trailer implementation (and thus hidden from the API). Helped-by: Christian Couder Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 12 ++++++------ trailer.c | 21 +++++++++++++++++++++ trailer.h | 4 ++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 11f4ce9e4a2..f3240682e35 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -141,7 +141,7 @@ static void interpret_trailers(const struct process_trailer_options *opts, LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; struct strbuf trailer_block = STRBUF_INIT; - struct trailer_info info; + struct trailer_info *info = trailer_info_new(); FILE *outfile = stdout; trailer_config_init(); @@ -151,13 +151,13 @@ static void interpret_trailers(const struct process_trailer_options *opts, if (opts->in_place) outfile = create_in_place_tempfile(file); - parse_trailers(opts, &info, sb.buf, &head); + parse_trailers(opts, info, sb.buf, &head); /* Print the lines before the trailers */ if (!opts->only_trailers) - fwrite(sb.buf, 1, info.trailer_block_start, outfile); + fwrite(sb.buf, 1, trailer_block_start(info), outfile); - if (!opts->only_trailers && !info.blank_line_before_trailer) + if (!opts->only_trailers && !blank_line_before_trailer_block(info)) fprintf(outfile, "\n"); @@ -178,8 +178,8 @@ static void interpret_trailers(const struct process_trailer_options *opts, /* Print the lines after the trailers as is */ if (!opts->only_trailers) - fwrite(sb.buf + info.trailer_block_end, 1, sb.len - info.trailer_block_end, outfile); - trailer_info_release(&info); + fwrite(sb.buf + trailer_block_end(info), 1, sb.len - trailer_block_end(info), outfile); + trailer_info_release(info); if (opts->in_place) if (rename_tempfile(&trailers_tempfile, file)) diff --git a/trailer.c b/trailer.c index 4700c441442..95b4c9b8f19 100644 --- a/trailer.c +++ b/trailer.c @@ -952,6 +952,12 @@ static void unfold_value(struct strbuf *val) strbuf_release(&out); } +struct trailer_info *trailer_info_new(void) +{ + struct trailer_info *info = xcalloc(1, sizeof(*info)); + return info; +} + /* * Parse trailers in "str", populating the trailer info and "head" * linked list structure. @@ -1000,6 +1006,21 @@ void free_trailers(struct list_head *trailers) } } +size_t trailer_block_start(struct trailer_info *info) +{ + return info->trailer_block_start; +} + +size_t trailer_block_end(struct trailer_info *info) +{ + return info->trailer_block_end; +} + +int blank_line_before_trailer_block(struct trailer_info *info) +{ + return info->blank_line_before_trailer; +} + void trailer_info_get(const struct process_trailer_options *opts, const char *str, struct trailer_info *info) diff --git a/trailer.h b/trailer.h index 7e36da7d13c..9ba96721398 100644 --- a/trailer.h +++ b/trailer.h @@ -97,6 +97,10 @@ void parse_trailers(const struct process_trailer_options *, void trailer_info_get(const struct process_trailer_options *, const char *str, struct trailer_info *); +size_t trailer_block_start(struct trailer_info *); +size_t trailer_block_end(struct trailer_info *); +int blank_line_before_trailer_block(struct trailer_info *); +struct trailer_info *trailer_info_new(void); void trailer_info_release(struct trailer_info *info); From patchwork Thu May 2 04:54:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13651273 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 79A3A2D047 for ; Thu, 2 May 2024 04:54:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625680; cv=none; b=iUK33Xvom10PtqQ59bGz6olCjqIztfPRxtkU8w7oKJ4kWmwWlZpuVs7kZ7zGMuDHF4FP1u1Lsu8I+ivKPWIBusGG9oSKrHkAMHnB9EOriHISyJ0Y83aT8j2NlgECM9dIFwC2TF2M+5NKyHFsfFgGxyZo66ird5F/m6L6Owg5ukc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625680; c=relaxed/simple; bh=WA3qy4XSE4VL6jtaIEj/rpUOZxgQP8ybPTieY9ejBcY=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=oQhX0Tex95SABNy0w8eGLfVExFVZkqIiVRxVPgmmDtJZLJbgnEx1MEoG26KYALJTvwbCAe3TNgbp+vMxN7RGAYSJhKEJD+uFJox0PbQPwhLK5LWTszKn9b5cU5m+E63li8chcvxHWFjjPjda//n4t+p9tnwJB1wZU1KKzPc/G5c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=l5R4zFQ4; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="l5R4zFQ4" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-41b794510cdso49143645e9.2 for ; Wed, 01 May 2024 21:54:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714625676; x=1715230476; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=IvEPhzygcqCzWhKD1yYdWR0bGjbS+NZOV4zmlXp5GfU=; b=l5R4zFQ4dKDCyX5AV51iJDlA2y0Ep6OWuiy0+q+YuZihGU9CxvK4Z1jpXPVDURI8JM 9uEKqXmSLm49REYkXVcJGln2NOhf/ospK+KI8ZedrjFFuW4ZgT131zIi3Dw4LoV7raun D3I6Q4sC2kPP87nAmXQgHiD1ZTqTr6nMuZ+ZxNUmqP/6ADBEcXrEB/6xqDUat+DjAUlN sYFg5/Wm93wRhP1I5xYxwdBqXpwhVeNLbxDi22au5b04biPye1EAwVBTdDJkugo8TnpY ihq96RRXeRoJ66LwrvdHGJTNGZ3Hi7a+Gn1DSfBuc6Ht/g5wcQIJhDMnSGicHgGuf63j OZLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714625676; x=1715230476; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IvEPhzygcqCzWhKD1yYdWR0bGjbS+NZOV4zmlXp5GfU=; b=BQts5WAIoNIe/BkOb5I20QR1xnjAgBe+4u3Zo99Z+GzE4wtmRBVQsbXJIuz6988ClJ Tuo3SLBDHed2lMq0oTvZhDlzXKXWAK4wEny7rzzueNCF/NVEo4ppNVOsyVFuoB0T+dJa RPfCxNxsuhMyIR1Gng8Y/AZee1WzqLEcdCOnu+Lj3ii4Xc23fjXVV2Xu+TJagf+0lXSK R9YNeV4XWmWpxKI4j9929L2D3UuEopotkYPWSoUqwCovzOe/CGXUf7SzrNtp0rqInVRI IADz9tf38bb6lBH/4mnDzpQFdURv1YeTVRqBKddm+gD5NcQ6pJop+Om1X12k42FMBJZs Nt/A== X-Gm-Message-State: AOJu0YysdPevTDM57v5MUaRkvhz4pXTKJXsFBpK5ewrxJmlfIN8lMApN FiiYbBvDYmWloGReU2olzkD4V+e1JfVkkPAfc6BoWQNe0garUNRYGeEReA== X-Google-Smtp-Source: AGHT+IEJpChL2ait5Tnld2bqTdMLOFT/lM5q3LnGFJ1wU66QmlQ9BKIKanNgoRFUmdv42Vy/C8YA3g== X-Received: by 2002:a5d:474e:0:b0:34a:cc2:1a34 with SMTP id o14-20020a5d474e000000b0034a0cc21a34mr2397077wrs.42.1714625676535; Wed, 01 May 2024 21:54:36 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id h18-20020a056000001200b0034c78001f6asm238653wrx.109.2024.05.01.21.54.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:54:35 -0700 (PDT) Message-Id: <9ed7cef9d294bc4cc8920975451e610974bb75e0.1714625668.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 02 May 2024 04:54:23 +0000 Subject: [PATCH v4 06/10] trailer: make parse_trailers() return trailer_info pointer Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Christian Couder , Kristoffer Haugsbakk , Linus Arver , Linus Arver , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver This is the second and final preparatory commit for making the trailer_info struct private to the trailer implementation. Make trailer_info_get() do the actual work of allocating a new trailer_info struct, and return a pointer to it. Because parse_trailers() wraps around trailer_info_get(), it too can return this pointer to the caller. From the trailer API user's perspective, the call to trailer_info_new() can be replaced with parse_trailers(); do so in interpret-trailers. Because trailer_info_new() is no longer called by interpret-trailers, remove this function from the trailer API. With this change, we no longer allocate trailer_info on the stack --- all uses of it are via a pointer where the actual data is always allocated at runtime through trailer_info_new(). Make trailer_info_release() free this dynamically allocated memory. Finally, due to the way the function signatures of parse_trailers() and trailer_info_get() have changed, update the callsites in format_trailers_from_commit() and trailer_iterator_init() accordingly. Helped-by: Christian Couder Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 4 ++-- trailer.c | 41 +++++++++++++++++++----------------- trailer.h | 17 ++++++--------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index f3240682e35..6bf8cec005a 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -141,7 +141,7 @@ static void interpret_trailers(const struct process_trailer_options *opts, LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; struct strbuf trailer_block = STRBUF_INIT; - struct trailer_info *info = trailer_info_new(); + struct trailer_info *info; FILE *outfile = stdout; trailer_config_init(); @@ -151,7 +151,7 @@ static void interpret_trailers(const struct process_trailer_options *opts, if (opts->in_place) outfile = create_in_place_tempfile(file); - parse_trailers(opts, info, sb.buf, &head); + info = parse_trailers(opts, sb.buf, &head); /* Print the lines before the trailers */ if (!opts->only_trailers) diff --git a/trailer.c b/trailer.c index 95b4c9b8f19..9179dd802c6 100644 --- a/trailer.c +++ b/trailer.c @@ -952,7 +952,7 @@ static void unfold_value(struct strbuf *val) strbuf_release(&out); } -struct trailer_info *trailer_info_new(void) +static struct trailer_info *trailer_info_new(void) { struct trailer_info *info = xcalloc(1, sizeof(*info)); return info; @@ -962,16 +962,16 @@ struct trailer_info *trailer_info_new(void) * Parse trailers in "str", populating the trailer info and "head" * linked list structure. */ -void parse_trailers(const struct process_trailer_options *opts, - struct trailer_info *info, - const char *str, - struct list_head *head) +struct trailer_info *parse_trailers(const struct process_trailer_options *opts, + const char *str, + struct list_head *head) { + struct trailer_info *info; struct strbuf tok = STRBUF_INIT; struct strbuf val = STRBUF_INIT; size_t i; - trailer_info_get(opts, str, info); + info = trailer_info_get(opts, str); for (i = 0; i < info->trailer_nr; i++) { int separator_pos; @@ -995,6 +995,8 @@ void parse_trailers(const struct process_trailer_options *opts, strbuf_detach(&val, NULL)); } } + + return info; } void free_trailers(struct list_head *trailers) @@ -1021,10 +1023,10 @@ int blank_line_before_trailer_block(struct trailer_info *info) return info->blank_line_before_trailer; } -void trailer_info_get(const struct process_trailer_options *opts, - const char *str, - struct trailer_info *info) +struct trailer_info *trailer_info_get(const struct process_trailer_options *opts, + const char *str) { + struct trailer_info *info = trailer_info_new(); size_t end_of_log_message = 0, trailer_block_start = 0; struct strbuf **trailer_lines, **ptr; char **trailer_strings = NULL; @@ -1063,6 +1065,8 @@ void trailer_info_get(const struct process_trailer_options *opts, info->trailer_block_end = end_of_log_message; info->trailers = trailer_strings; info->trailer_nr = nr; + + return info; } void trailer_info_release(struct trailer_info *info) @@ -1071,6 +1075,7 @@ void trailer_info_release(struct trailer_info *info) for (i = 0; i < info->trailer_nr; i++) free(info->trailers[i]); free(info->trailers); + free(info); } void format_trailers(const struct process_trailer_options *opts, @@ -1138,21 +1143,19 @@ void format_trailers_from_commit(const struct process_trailer_options *opts, struct strbuf *out) { LIST_HEAD(trailer_objects); - struct trailer_info info; - - parse_trailers(opts, &info, msg, &trailer_objects); + struct trailer_info *info = parse_trailers(opts, msg, &trailer_objects); /* If we want the whole block untouched, we can take the fast path. */ if (!opts->only_trailers && !opts->unfold && !opts->filter && !opts->separator && !opts->key_only && !opts->value_only && !opts->key_value_separator) { - strbuf_add(out, msg + info.trailer_block_start, - info.trailer_block_end - info.trailer_block_start); + strbuf_add(out, msg + info->trailer_block_start, + info->trailer_block_end - info->trailer_block_start); } else format_trailers(opts, &trailer_objects, out); free_trailers(&trailer_objects); - trailer_info_release(&info); + trailer_info_release(info); } void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) @@ -1161,14 +1164,14 @@ void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) strbuf_init(&iter->key, 0); strbuf_init(&iter->val, 0); opts.no_divider = 1; - trailer_info_get(&opts, msg, &iter->internal.info); + iter->internal.info = trailer_info_get(&opts, msg); iter->internal.cur = 0; } int trailer_iterator_advance(struct trailer_iterator *iter) { - if (iter->internal.cur < iter->internal.info.trailer_nr) { - char *line = iter->internal.info.trailers[iter->internal.cur++]; + if (iter->internal.cur < iter->internal.info->trailer_nr) { + char *line = iter->internal.info->trailers[iter->internal.cur++]; int separator_pos = find_separator(line, separators); iter->raw = line; @@ -1185,7 +1188,7 @@ int trailer_iterator_advance(struct trailer_iterator *iter) void trailer_iterator_release(struct trailer_iterator *iter) { - trailer_info_release(&iter->internal.info); + trailer_info_release(iter->internal.info); strbuf_release(&iter->val); strbuf_release(&iter->key); } diff --git a/trailer.h b/trailer.h index 9ba96721398..c8c0018c54d 100644 --- a/trailer.h +++ b/trailer.h @@ -89,18 +89,15 @@ void parse_trailers_from_command_line_args(struct list_head *arg_head, void process_trailers_lists(struct list_head *head, struct list_head *arg_head); -void parse_trailers(const struct process_trailer_options *, - struct trailer_info *, - const char *str, - struct list_head *head); - -void trailer_info_get(const struct process_trailer_options *, - const char *str, - struct trailer_info *); +struct trailer_info *parse_trailers(const struct process_trailer_options *, + const char *str, + struct list_head *head); +struct trailer_info *trailer_info_get(const struct process_trailer_options *, + const char *str); + size_t trailer_block_start(struct trailer_info *); size_t trailer_block_end(struct trailer_info *); int blank_line_before_trailer_block(struct trailer_info *); -struct trailer_info *trailer_info_new(void); void trailer_info_release(struct trailer_info *info); @@ -141,7 +138,7 @@ struct trailer_iterator { /* private */ struct { - struct trailer_info info; + struct trailer_info *info; size_t cur; } internal; }; From patchwork Thu May 2 04:54:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13651274 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0952D208A5 for ; Thu, 2 May 2024 04:54:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625681; cv=none; b=MJ0tgvbws58J6JmdYu3/jxgMSjJq9gW1PcuEFEGIhZA5y+opjB89SBOhvrqgBSofCAltQXLchNhBBGIt83oTCOhCTYcjvTbxFiqSQIbVACUt6P/Un/QvB99uNrzBXguwDdNTwxKbPce3Y7BvkQznF4+RTb4MROr/Vd6WuKMRzrk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625681; c=relaxed/simple; bh=uEHr4ZS7X/bZIOYID1msplF47dMxYIqUkI03R2KsVBo=; h=Message-Id:In-Reply-To:References:From:Date:Subject:MIME-Version: Content-Type:To:Cc; b=BOtRq5LpjR9MJ9FUPce1CRY6prklA93qtqcoOAZGlll/DLKPz06wlrU7Pg38O3/rAENcWOepZhD3iA6lvwwIjndhMoU3wU02VRGolNjjChfvwa7dadngeTEiBUyfudfJz/9fTz/VcbsOb+aq5RRG2YL7rrvxvHxN10xeJyjQVKY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gQJr+pyU; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gQJr+pyU" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-41b794510cdso49143755e9.2 for ; Wed, 01 May 2024 21:54:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714625678; x=1715230478; darn=vger.kernel.org; h=cc:to:fcc:content-transfer-encoding:mime-version:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=kAnslTvdkZPeM+iwo8vx31r70Wcrsa/u5WxTuhuHa0g=; b=gQJr+pyUzOHR+G4hac+tYOOW212omNqxFBLDSMjJ7za2f5Dr9XUIcswoaGE1lf1eKb N/RAprEeEM4YK1tKEu3lg7QwHs1T6bD12/3TOAYaLw++KuL1fbz0RwH7JEmbpmeKqql6 LVYpaUi85bnIQGqA6TPZZbJlLQVOEHpvyEyXqs4plG6HFTECT1bq1mUr6Sz8tjiuUJml PmBUDbCRrwKVo/65YXNFx5ZTPJtHGflQ3RDs2VVo6uPwdt7oULO+ZmQpyvkr/2tpoJ2r B2dECknKhRcrn8Cl76lTGy9Jgx1tRWld6jPK0CiBBpzDMMQNyg2ppWq42BZ9lPHcXULs TZLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714625678; x=1715230478; h=cc:to:fcc:content-transfer-encoding:mime-version:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kAnslTvdkZPeM+iwo8vx31r70Wcrsa/u5WxTuhuHa0g=; b=XTtry6hvdyvifH0LQw7vwwB2LhzxST4mUGlrykp8DNRVCU0B0AvJpmfFnWdERqEXHi tnp/dyGmCR6z1j2bmTT7OEjXsLlCiESRmHvkYTj/JSqDhZ2x45+n2V44aXEscyHNnESc 2nAG8GQsfzqj+Pdu+qFMAw0gRUgebf/yJzVhm8PIG4fw1hOXdM817tbmPBgMax9+hEOO hQMzwXEMALqN9kYMlg7hsLJQNyewrHiaRB5rDL4Kh6qXUZZ13Txuas6Qz/9ceXPG9fJ6 UpnDpyMDScCB0kNw5M8xOzNhB3j4psVZazvzGpK4ii8i1R8MKGk9UkpAyQr1VwkdkIck WCrg== X-Gm-Message-State: AOJu0YxDBPS1wiOQ+HSCG4rlqtS2B7/BDJlewf9jfw0q2Wnc0xRrf+73 XyGzdl88KHmVH0cZuHngPJMuCDpml3Q3KysvuuxJH8Xxkgs+SkP01DXIAQ== X-Google-Smtp-Source: AGHT+IGO2XZJ0m6Ti9fxLKvX9VODqKA3Nb51+CbCzhKNsDc0iPNm0lNKtFBlnPE+NgJ6M1hyHSgVzQ== X-Received: by 2002:a05:600c:4511:b0:418:3eb7:e8c6 with SMTP id t17-20020a05600c451100b004183eb7e8c6mr2874092wmo.5.1714625677654; Wed, 01 May 2024 21:54:37 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u17-20020a05600c19d100b004186eb69a55sm507512wmq.25.2024.05.01.21.54.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:54:36 -0700 (PDT) Message-Id: <246ac9a5d075870e07fe1485b0687f035facd3b8.1714625668.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 02 May 2024 04:54:24 +0000 Subject: [PATCH v4 07/10] trailer: make trailer_info struct private Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Fcc: Sent To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Christian Couder , Kristoffer Haugsbakk , Linus Arver , Linus Arver , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver In 13211ae23f (trailer: separate public from internal portion of trailer_iterator, 2023-09-09) we moved trailer_info behind an anonymous struct to discourage use by trailer.h API users. However it still left open the possibility of external use of trailer_info itself. Now that there are no external users of trailer_info, we can make this struct private. Make this struct private by putting its definition inside trailer.c. This has two benefits: (1) it makes the surface area of the public facing interface (trailer.h) smaller, and (2) external API users are unable to peer inside this struct (because it is only ever exposed as an opaque pointer). There are a few disadvantages: (A) every time the member of the struct is accessed an extra pointer dereference must be done, and (B) for users of trailer_info outside trailer.c, this struct can no longer be allocated on the stack and may only be allocated on the heap (because its definition is hidden away in trailer.c) and appropriately deallocated by the user, and (C) without good documentation on the API, the opaque struct is hostile to programmers by going opposite to the "Show me your data structures, and I won't usually need your code; it'll be obvious." mantra [2]. (The disadvantages have already been observed in the two preparatory commits that precede this one.) This commit believes that the benefits outweigh the disadvantages for designing APIs, as explained below. Making trailer_info private exposes existing deficiencies in the API. This is because users of this struct had full access to its internals, so there wasn't much need to actually design it to be "complete" in the sense that API users only needed to use what was provided by the API. For example, the location of the trailer block (start/end offsets relative to the start of the input text) was accessible by looking at these struct members directly. Now that the struct is private, we have to expose new API functions to allow clients to access this information (see builtin/interpret-trailers.c). The idea in this commit to hide implementation details behind an "opaque pointer" is also known as the "pimpl" (pointer to implementation) idiom in C++ and is a common pattern in that language (where, for example, abstract classes only have pointers to concrete classes). However, the original inspiration to use this idiom does not come from C++, but instead the book "C Interfaces and Implementations: Techniques for Creating Reusable Software" [1]. This book recommends opaque pointers as a good design principle for designing C libraries, using the term "interface" as the functions defined in *.h (header) files and "implementation" as the corresponding *.c file which define the interfaces. The book says this about opaque pointers: ... clients can manipulate such pointers freely, but they can’t dereference them; that is, they can’t look at the innards of the structure pointed to by them. Only the implementation has that privilege. Opaque pointers hide representation details and help catch errors. In our case, "struct trailer_info" is now hidden from clients, and the ways in which this opaque pointer can be used is limited to the richness of . In other words, exclusively controls exactly how "trailer_info" pointers are to be used. [1] Hanson, David R. "C Interfaces and Implementations: Techniques for Creating Reusable Software". Addison Wesley, 1997. p. 22 [2] Raymond, Eric S. "The Cathedral and the Bazaar: Musings on Linux and Open Source by an Accidental Revolutionary". O'Reilly, 1999. Helped-by: Junio C Hamano Helped-by: Christian Couder Signed-off-by: Linus Arver --- trailer.c | 21 +++++++++++++++++++++ trailer.h | 23 ++--------------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/trailer.c b/trailer.c index 9179dd802c6..6167b707ae0 100644 --- a/trailer.c +++ b/trailer.c @@ -11,6 +11,27 @@ * Copyright (c) 2013, 2014 Christian Couder */ +struct trailer_info { + /* + * True if there is a blank line before the location pointed to by + * trailer_block_start. + */ + int blank_line_before_trailer; + + /* + * Offsets to the trailer block start and end positions in the input + * string. If no trailer block is found, these are both set to the + * "true" end of the input (find_end_of_log_message()). + */ + size_t trailer_block_start, trailer_block_end; + + /* + * Array of trailers found. + */ + char **trailers; + size_t trailer_nr; +}; + struct conf_info { char *name; char *key; diff --git a/trailer.h b/trailer.h index c8c0018c54d..4e0a6789d7f 100644 --- a/trailer.h +++ b/trailer.h @@ -4,6 +4,8 @@ #include "list.h" #include "strbuf.h" +struct trailer_info; + enum trailer_where { WHERE_DEFAULT, WHERE_END, @@ -29,27 +31,6 @@ int trailer_set_where(enum trailer_where *item, const char *value); int trailer_set_if_exists(enum trailer_if_exists *item, const char *value); int trailer_set_if_missing(enum trailer_if_missing *item, const char *value); -struct trailer_info { - /* - * True if there is a blank line before the location pointed to by - * trailer_block_start. - */ - int blank_line_before_trailer; - - /* - * Offsets to the trailer block start and end positions in the input - * string. If no trailer block is found, these are both set to the - * "true" end of the input (find_end_of_log_message()). - */ - size_t trailer_block_start, trailer_block_end; - - /* - * Array of trailers found. - */ - char **trailers; - size_t trailer_nr; -}; - /* * A list that represents newly-added trailers, such as those provided * with the --trailer command line option of git-interpret-trailers. From patchwork Thu May 2 04:54:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13651275 Received: from mail-lj1-f172.google.com (mail-lj1-f172.google.com [209.85.208.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 135DE208B8 for ; Thu, 2 May 2024 04:54:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625683; cv=none; b=Q9/ddnOXUItlcV8LcJleQTMKsZqeD7vpxg88Ua3jeUW4Hnxqs0uaFdPdWi6qOM5f1RbiPbSj4wgzAW9MeI/weZhJcFf7XlYGoiVY8/5ESpO+Z+8krTdtKeO5dbuJcw18C4Nj2mDKozVgiR3Ckv7PAxHnjnGoWrOvjEWVrDXdBeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625683; c=relaxed/simple; bh=QqjBcXEgpyPQ71DrqyWwmGYHoR76c+5PLG9LmQyvn2w=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=l8OBwTiqTUm/9GGGCjLJPTWXBjIPzIM35X2SESecrf5pRU4Lb/mKJqANoawvx10Zs3azLPkfs8tU//UeW2RD67Fv9aE2RoHHQSeqIKSTHpVpSXUNXRfiSBE/TtURhFypLaTdpOSnwfJGSpnCRxpBJ660M9K/at+h1n7DPQ1YyxY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=a8dJYa0i; arc=none smtp.client-ip=209.85.208.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="a8dJYa0i" Received: by mail-lj1-f172.google.com with SMTP id 38308e7fff4ca-2e1fa824504so501791fa.0 for ; Wed, 01 May 2024 21:54:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714625679; x=1715230479; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=KtlMUDRS1Qk/riYvm9ztz9w3nrPRqIvEExLXVjjshmg=; b=a8dJYa0iU10wXW7nu4XLWqdLEZ/8/4tWNgupaGiftOhc0h+DiDG5WX0XZVRqvaaJtw WrpHGVOga5sYLprebEWBhyK5vQRMQnnBcLcUVIndyHqBvfGOPm3UV69E4oPHdKWEFSth Bv8OEFPtOeK6cUIk5eUyCONK45NZ716x/TfUmqdxY0C6srLDEMavkgQQHGbsoNnJ6vAM 1iMbAp/Xna9P5nyPjSfkh8c6noUnSV5yqN+1o7RTt3HsNAP1WX+PLGrug2NvhoOJnudg yEDqqa2u/gH+HS2IAywE7WXfrrsX3o2uV3YPm9yAOse+EOHQeV7Eu0T7yZhcusZklHU2 SPbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714625679; x=1715230479; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KtlMUDRS1Qk/riYvm9ztz9w3nrPRqIvEExLXVjjshmg=; b=sv95QUS1oWqLA2sASAH5CLHHsqBHaohcifuv3T7mM9aL0I+sGUCZR6fIjfI8xMZPI7 tARhs6qZNoR2Svi8qktQtoGzgqSOLSo7YD2QNEjo3+y4DjrltmunrePD9NLE59QDxQSM P7CYJKvDwj6vA9zX7YF8f2UCS3dB1LAmn+dJzIoYxbHUIKJwjZlfB5UzwwKJa+KjtJDU Xj2wQNAET1M6BAAeriu0XnYzYRv/e7XhGC9kLWqD0v+6vUBgEXCzuvGpgR06ieKUPR4r eZtWQUdRwcQ2IalbzTzYqFUO4Mcp1tlar5FHAXhW7sn99ZGNoM3mJWvtjHunDO+qs7A7 4syg== X-Gm-Message-State: AOJu0YygRibGxf+QmbZEx38XpRbAKB81h0Ru80xE0/Km4zk00x7Go73h ZTE58StiLGcGKIKwFoUJw2JWohmXWLzpDo6smz0kAc1JtNhwLcozbQKsfQ== X-Google-Smtp-Source: AGHT+IFK1SCeQQxIZ1etzxvQICWbacdY5PRrFjFC2k9pNysxWiYEnE9LCjToaKkx7Qvu5xEKJxFsbg== X-Received: by 2002:a05:651c:1213:b0:2da:c3a:2546 with SMTP id i19-20020a05651c121300b002da0c3a2546mr2804431lja.21.1714625679316; Wed, 01 May 2024 21:54:39 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n20-20020a05600c501400b0041aa79f27a0sm498520wmr.38.2024.05.01.21.54.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:54:38 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Thu, 02 May 2024 04:54:25 +0000 Subject: [PATCH v4 08/10] trailer: retire trailer_info_get() from API Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Christian Couder , Kristoffer Haugsbakk , Linus Arver , Linus Arver , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Make trailer_info_get() "static" to be file-scoped to trailer.c, because no one outside of trailer.c uses it. Remove its declaration from . We have to also reposition it to be above parse_trailers(), which depends on it. Signed-off-by: Linus Arver --- trailer.c | 92 +++++++++++++++++++++++++++---------------------------- trailer.h | 2 -- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/trailer.c b/trailer.c index 6167b707ae0..33b6aa7e8bd 100644 --- a/trailer.c +++ b/trailer.c @@ -979,6 +979,52 @@ static struct trailer_info *trailer_info_new(void) return info; } +static struct trailer_info *trailer_info_get(const struct process_trailer_options *opts, + const char *str) +{ + struct trailer_info *info = trailer_info_new(); + size_t end_of_log_message = 0, trailer_block_start = 0; + struct strbuf **trailer_lines, **ptr; + char **trailer_strings = NULL; + size_t nr = 0, alloc = 0; + char **last = NULL; + + trailer_config_init(); + + end_of_log_message = find_end_of_log_message(str, opts->no_divider); + trailer_block_start = find_trailer_block_start(str, end_of_log_message); + + trailer_lines = strbuf_split_buf(str + trailer_block_start, + end_of_log_message - trailer_block_start, + '\n', + 0); + for (ptr = trailer_lines; *ptr; ptr++) { + if (last && isspace((*ptr)->buf[0])) { + struct strbuf sb = STRBUF_INIT; + strbuf_attach(&sb, *last, strlen(*last), strlen(*last)); + strbuf_addbuf(&sb, *ptr); + *last = strbuf_detach(&sb, NULL); + continue; + } + ALLOC_GROW(trailer_strings, nr + 1, alloc); + trailer_strings[nr] = strbuf_detach(*ptr, NULL); + last = find_separator(trailer_strings[nr], separators) >= 1 + ? &trailer_strings[nr] + : NULL; + nr++; + } + strbuf_list_free(trailer_lines); + + info->blank_line_before_trailer = ends_with_blank_line(str, + trailer_block_start); + info->trailer_block_start = trailer_block_start; + info->trailer_block_end = end_of_log_message; + info->trailers = trailer_strings; + info->trailer_nr = nr; + + return info; +} + /* * Parse trailers in "str", populating the trailer info and "head" * linked list structure. @@ -1044,52 +1090,6 @@ int blank_line_before_trailer_block(struct trailer_info *info) return info->blank_line_before_trailer; } -struct trailer_info *trailer_info_get(const struct process_trailer_options *opts, - const char *str) -{ - struct trailer_info *info = trailer_info_new(); - size_t end_of_log_message = 0, trailer_block_start = 0; - struct strbuf **trailer_lines, **ptr; - char **trailer_strings = NULL; - size_t nr = 0, alloc = 0; - char **last = NULL; - - trailer_config_init(); - - end_of_log_message = find_end_of_log_message(str, opts->no_divider); - trailer_block_start = find_trailer_block_start(str, end_of_log_message); - - trailer_lines = strbuf_split_buf(str + trailer_block_start, - end_of_log_message - trailer_block_start, - '\n', - 0); - for (ptr = trailer_lines; *ptr; ptr++) { - if (last && isspace((*ptr)->buf[0])) { - struct strbuf sb = STRBUF_INIT; - strbuf_attach(&sb, *last, strlen(*last), strlen(*last)); - strbuf_addbuf(&sb, *ptr); - *last = strbuf_detach(&sb, NULL); - continue; - } - ALLOC_GROW(trailer_strings, nr + 1, alloc); - trailer_strings[nr] = strbuf_detach(*ptr, NULL); - last = find_separator(trailer_strings[nr], separators) >= 1 - ? &trailer_strings[nr] - : NULL; - nr++; - } - strbuf_list_free(trailer_lines); - - info->blank_line_before_trailer = ends_with_blank_line(str, - trailer_block_start); - info->trailer_block_start = trailer_block_start; - info->trailer_block_end = end_of_log_message; - info->trailers = trailer_strings; - info->trailer_nr = nr; - - return info; -} - void trailer_info_release(struct trailer_info *info) { size_t i; diff --git a/trailer.h b/trailer.h index 4e0a6789d7f..b0ec7658c67 100644 --- a/trailer.h +++ b/trailer.h @@ -73,8 +73,6 @@ void process_trailers_lists(struct list_head *head, struct trailer_info *parse_trailers(const struct process_trailer_options *, const char *str, struct list_head *head); -struct trailer_info *trailer_info_get(const struct process_trailer_options *, - const char *str); size_t trailer_block_start(struct trailer_info *); size_t trailer_block_end(struct trailer_info *); From patchwork Thu May 2 04:54:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13651276 Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F124208A5 for ; Thu, 2 May 2024 04:54:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625685; cv=none; b=lhAulL3wvVYda1lzhn8uVU6BZ64lepa51fiDywsnqoXEp2WhxK0EG+I/Kja80xX0rgdf6IP4t08jQBdsdXaJD112o1Awk/ln4InZWSZD12Dktv+rtzKjLyUL2bKpBQLmm6YYjImAOjSEgsbk3CvELXh5uqaClK29FFiYD2FXONw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625685; c=relaxed/simple; bh=yYeQ7AAR3Yfte32t8sr7Txt2T8nA3wjmDCJC29ojBw8=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=aYWV1NsKZ0LrnU4j+0D24sqNJsvvgAnXw9aZ1PzyKPba1pkQGVavROEWyPzcpvHy3K1HHNUfadBpuYLD+xd6QYddsOpH1gnvsPXGz3pj63R5cFMNosBwLFw8FdXZuIfNMyfl26LpLzmWUHt/kffXOO6Cv56WX2zNPEyYIPOjlAY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mEuU8nsM; arc=none smtp.client-ip=209.85.208.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mEuU8nsM" Received: by mail-lj1-f182.google.com with SMTP id 38308e7fff4ca-2df9af57b5eso72669641fa.2 for ; Wed, 01 May 2024 21:54:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714625681; x=1715230481; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=CHy3w/jmb5rVyFsJuV+a2Zva6f4ZiFgRqNbnWmxj/vg=; b=mEuU8nsMXtlTeHIwxY3PzFvwzdNkxrKISHNunuFfZ19SRhjnBDFjT/i82U5j7pasRK Cnq5x7zr+p16Amoh8L66jGvA3qAfDI5xpwbswF/5tgbKO6tJLLt1tRVAwW5N/dhRYKRA gR6TWp4Lm7W9MH8quMyVTUKNqUOKJm1g4mKPDyq/gSaj9/G6ILmOTCW8smSaLTkhKcyl ffPw9knLitolOjGKIovoKU1ODJNFe3HIYX/lPCbVSYOdUV+ea00zj4u7s1qWhUOaKefX ntnJPGoZNA7Y+NgWLXqYGF7z0GxsL/7NWkljzLafx8wFm3YV2MJkpFOKDzVUSLYHfn3i U6Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714625681; x=1715230481; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CHy3w/jmb5rVyFsJuV+a2Zva6f4ZiFgRqNbnWmxj/vg=; b=m3ASpe4UwF8WzmlTTjtqneiCC2IrIPscrsrSryXSn4OPhrTVXnok09uBamao6Y6KQo HyGDNrYUp+Z/5Qn4SWMnAi5XAtSkZNUngkYDzE1p7y6w0L6Fync20wjMBERFGPsU72ND S7Gr06CWCqnyJGlwCzaPCB346iDiNB6nSjuaXRpRBXTNf2lNRC1JNu21jSoalgFlml5+ WtEHLEGLwOa9OlwhA6QkaDgA8s3K/3PRObQDeSUESLf8RGimhS6TK9SHcNajkwgeDGCo Y5ixJStmyGqjECp1P28IItIeFVpK1qqjKi8cZcA7e/u6WV6bDNvJg9pnZWk+xx3qBOtV yVSw== X-Gm-Message-State: AOJu0YyGXC0cOB4v1x3s8IcshlZqHyKFTLy6CQnt6fEgx2pyps5ApAn2 qu5nk6vQOQqchG/MJ8mYalF42SvLbVGaNlKdoVXjqw5oqKnX9VuEYL+iNw== X-Google-Smtp-Source: AGHT+IEOypnWKRFznf5MUEJGExBEy8xl2jNGs7KIndCO50bpBRRjJt6+tMxJ/WVSz+25ssoG/vNQWQ== X-Received: by 2002:a05:651c:515:b0:2e0:2891:c75e with SMTP id o21-20020a05651c051500b002e02891c75emr834829ljp.51.1714625680975; Wed, 01 May 2024 21:54:40 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p9-20020a05600c1d8900b0041563096e15sm4275084wms.5.2024.05.01.21.54.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:54:39 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Thu, 02 May 2024 04:54:26 +0000 Subject: [PATCH v4 09/10] trailer: document parse_trailers() usage Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Christian Couder , Kristoffer Haugsbakk , Linus Arver , Linus Arver , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Explain how to use parse_trailers(), because earlier we made the trailer_info struct opaque. That is, because clients can no longer peek inside it, we should give them guidance about how the (pointer to the) opaque struct can still be useful to them. Rename "head" struct to "trailer_objects" to make the wording of the new comments a bit easier to read (because "head" itself doesn't really have any domain-specific meaning here). Signed-off-by: Linus Arver --- trailer.c | 8 ++++---- trailer.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/trailer.c b/trailer.c index 33b6aa7e8bd..406745264aa 100644 --- a/trailer.c +++ b/trailer.c @@ -1026,12 +1026,12 @@ static struct trailer_info *trailer_info_get(const struct process_trailer_option } /* - * Parse trailers in "str", populating the trailer info and "head" + * Parse trailers in "str", populating the trailer info and "trailer_objects" * linked list structure. */ struct trailer_info *parse_trailers(const struct process_trailer_options *opts, const char *str, - struct list_head *head) + struct list_head *trailer_objects) { struct trailer_info *info; struct strbuf tok = STRBUF_INIT; @@ -1051,13 +1051,13 @@ struct trailer_info *parse_trailers(const struct process_trailer_options *opts, separator_pos); if (opts->unfold) unfold_value(&val); - add_trailer_item(head, + add_trailer_item(trailer_objects, strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL)); } else if (!opts->only_trailers) { strbuf_addstr(&val, trailer); strbuf_strip_suffix(&val, "\n"); - add_trailer_item(head, + add_trailer_item(trailer_objects, NULL, strbuf_detach(&val, NULL)); } diff --git a/trailer.h b/trailer.h index b0ec7658c67..82104912d70 100644 --- a/trailer.h +++ b/trailer.h @@ -70,14 +70,63 @@ void parse_trailers_from_command_line_args(struct list_head *arg_head, void process_trailers_lists(struct list_head *head, struct list_head *arg_head); +/* + * Given some input string "str", return a pointer to an opaque trailer_info + * structure. Also populate the trailer_objects list with parsed trailer + * objects. Internally this calls trailer_info_get() to get the opaque pointer, + * but does some extra work to populate the trailer_objects linked list. + * + * The opaque trailer_info pointer can be used to check the position of the + * trailer block as offsets relative to the beginning of "str" in + * trailer_block_start() and trailer_block_end(). + * blank_line_before_trailer_block() returns 1 if there is a blank line just + * before the trailer block. All of these functions are useful for preserving + * the input before and after the trailer block, if we were to write out the + * original input (but with the trailer block itself modified); see + * builtin/interpret-trailers.c for an example. + * + * For iterating through the parsed trailer block (if you don't care about the + * position of the trailer block itself in the context of the larger string text + * from which it was parsed), please see trailer_iterator_init() which uses the + * trailer_info struct internally. + * + * Lastly, callers should call trailer_info_release() when they are done using + * the opaque pointer. + * + * NOTE: Callers should treat both trailer_info and trailer_objects as + * read-only items, because there is some overlap between the two (trailer_info + * has "char **trailers" string array, and trailer_objects will have the same + * data but as a linked list of trailer_item objects). This API does not perform + * any synchronization between the two. In the future we should be able to + * reduce the duplication and use just the linked list. + */ struct trailer_info *parse_trailers(const struct process_trailer_options *, const char *str, - struct list_head *head); + struct list_head *trailer_objects); +/* + * Return the offset of the start of the trailer block. That is, 0 is the start + * of the input ("str" in parse_trailers()) and some other positive number + * indicates how many bytes we have to skip over before we get to the beginning + * of the trailer block. + */ size_t trailer_block_start(struct trailer_info *); + +/* + * Return the end of the trailer block, again relative to the start of the + * input. + */ size_t trailer_block_end(struct trailer_info *); + +/* + * Return 1 if the trailer block had an extra newline (blank line) just before + * it. + */ int blank_line_before_trailer_block(struct trailer_info *); +/* + * Free trailer_info struct. + */ void trailer_info_release(struct trailer_info *info); void trailer_config_init(void); From patchwork Thu May 2 04:54:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13651277 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52815374C2 for ; Thu, 2 May 2024 04:54:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625686; cv=none; b=k6KRdgN8RDCIkGL+FeIfb9DvLXRTxuYKW9EzEmQvyIee4644iT5LaGxzuKVjKbqP7nKJcxsecUq03yHzRp2DgJ1pBcFA7KII2Rx22ufalmDSHMb+/RnMgRT5u4jpP/wYuspnAiNsIOLMlUqMFdqCF2b4M/zRWQWjGgbj8yKxGUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714625686; c=relaxed/simple; bh=8T+J6GkH4hjFOh0kdgWkfVP8Kv50fcLvA1QOL8zQ3xA=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=Sn/fGSpKZheTjNRaD5EUC4HxGaGbvBx264nOe1l0lns+NvMRpaQY24gw0GVTw6yzDHmD0gI70O5VMtXaG7WDdk2Xfh/NEhsXNYlLefOSD3oVo38MLIBdKWOEchyxctjVw05Agvi4CtBjsRe6oIcoqurDN8Yf1aoOxNOYJQV76y4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ihjgDsMP; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ihjgDsMP" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-41bca450fa3so37388835e9.2 for ; Wed, 01 May 2024 21:54:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714625682; x=1715230482; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=dyL7WRUnzbYgLmQBOx3pRS9AZoBS024alSRNjZT/554=; b=ihjgDsMPr2zHKI/bVuSlOUKjlkw5pIClN2eFliW68FZLc/FXd3N3kQ5JCrsmnVH1Nr UCAvVOga9+Li1dC1TVKtb/9MyIxXlRdGunFwkppWmbu/suD+HGUvwtT+AsYLfW1oSOTz 03pdM5oFyseLURRkLaAoCfDF9cQgJfNvnvKwG8XJajef+FLzTWVa9Q0njVwR7h0qQ5sH ylZ7uIH0pA273ARDut0hPjZ6KSXBjynxxWues6KwxkG39c3+w1IJn5I9jZRArJJqZbr+ shwRn0vUgSAcieHzlNP1mkSqE4/pzMien8yGVU3YPlCy+9Li3n+X0bv3XE+rs2hn7Wi+ nJfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714625682; x=1715230482; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dyL7WRUnzbYgLmQBOx3pRS9AZoBS024alSRNjZT/554=; b=AumPV7KGdJAQHOZ7iVq2LgKQ+vcHoV4Y+ycs2LVNfIuP4N5Pi3Gcbo7tSTXB/BofZt 5lljKOEof489gFhihekfotBOP3iv+/0wRDm18pOsWJozTIYMWiYOuw40T4tTIbwX79cM BM4I9t1xAXFxddGMQDDVXT0z7HrbDItUuUVbmXgyLSIqqz1svXG/IZQthnI9EiDQ55UD Hg1ip+QSMQuR+hKj3OoE5V1tJxijrW4WczJcMRgv+eySQKsYPF3Hl4BaMoWMOqYLYTaO A9KkkU/3oSVaARzcME6Z4Wgr34hAJqX7Yaym4IH09xsZCEvadl5JxuH9e2shc3JB4lVc BVVg== X-Gm-Message-State: AOJu0YxocKK4ZSWpiIj9oz4eM5rV+9xk7MoIZeRM9JHVUYdbGkRc9tAV ESHg4vqbFLk7i6BXkp/gd499/+v+wzhYci8FsmnQl05kWyAtNlfCfJZPqA== X-Google-Smtp-Source: AGHT+IFbPYcakF4+2o5mNGZuFz8YEzIo4JH0Z2j7bgb0MNyyMiPSzH3FykFwW7r1zcfZoSJhl0v/dg== X-Received: by 2002:a05:600c:510b:b0:416:2471:e102 with SMTP id o11-20020a05600c510b00b004162471e102mr3146389wms.37.1714625682310; Wed, 01 May 2024 21:54:42 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j14-20020a5d452e000000b0034dae1488ccsm248164wra.53.2024.05.01.21.54.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:54:41 -0700 (PDT) Message-Id: <310b632ddfdeae3251c18690b1fe0b0113a602b6.1714625668.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 02 May 2024 04:54:27 +0000 Subject: [PATCH v4 10/10] trailer unit tests: inspect iterator contents Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Christian Couder , Kristoffer Haugsbakk , Linus Arver , Linus Arver , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Previously we only checked whether we would iterate a certain (expected) number of times. Also check the parsed "raw", "key" and "val" fields during each iteration. Helped-by: Junio C Hamano Signed-off-by: Linus Arver --- t/unit-tests/t-trailer.c | 161 +++++++++++++++++++++++++++++++++++---- 1 file changed, 148 insertions(+), 13 deletions(-) diff --git a/t/unit-tests/t-trailer.c b/t/unit-tests/t-trailer.c index 4f640d2a4b8..2ecca359d96 100644 --- a/t/unit-tests/t-trailer.c +++ b/t/unit-tests/t-trailer.c @@ -1,14 +1,27 @@ #include "test-lib.h" #include "trailer.h" -static void t_trailer_iterator(const char *msg, size_t num_expected) +struct contents { + const char *raw; + const char *key; + const char *val; +}; + +static void t_trailer_iterator(const char *msg, size_t num_expected, + struct contents *contents) { struct trailer_iterator iter; size_t i = 0; trailer_iterator_init(&iter, msg); - while (trailer_iterator_advance(&iter)) + while (trailer_iterator_advance(&iter)) { + if (num_expected) { + check_str(iter.raw, contents[i].raw); + check_str(iter.key.buf, contents[i].key); + check_str(iter.val.buf, contents[i].val); + } i++; + } trailer_iterator_release(&iter); check_uint(i, ==, num_expected); @@ -16,22 +29,26 @@ static void t_trailer_iterator(const char *msg, size_t num_expected) static void run_t_trailer_iterator(void) { + static struct test_cases { const char *name; const char *msg; size_t num_expected; + struct contents contents[10]; } tc[] = { { "empty input", "", - 0 + 0, + {{0}}, }, { "no newline at beginning", "Fixes: x\n" "Acked-by: x\n" "Reviewed-by: x\n", - 0 + 0, + {{0}}, }, { "newline at beginning", @@ -39,7 +56,27 @@ static void run_t_trailer_iterator(void) "Fixes: x\n" "Acked-by: x\n" "Reviewed-by: x\n", - 3 + 3, + { + { + .raw = "Fixes: x\n", + .key = "Fixes", + .val = "x", + }, + { + .raw = "Acked-by: x\n", + .key = "Acked-by", + .val = "x", + }, + { + .raw = "Reviewed-by: x\n", + .key = "Reviewed-by", + .val = "x", + }, + { + 0 + }, + }, }, { "without body text", @@ -48,7 +85,27 @@ static void run_t_trailer_iterator(void) "Fixes: x\n" "Acked-by: x\n" "Reviewed-by: x\n", - 3 + 3, + { + { + .raw = "Fixes: x\n", + .key = "Fixes", + .val = "x", + }, + { + .raw = "Acked-by: x\n", + .key = "Acked-by", + .val = "x", + }, + { + .raw = "Reviewed-by: x\n", + .key = "Reviewed-by", + .val = "x", + }, + { + 0 + }, + }, }, { "with body text, without divider", @@ -63,7 +120,32 @@ static void run_t_trailer_iterator(void) "Acked-by: x\n" "Reviewed-by: x\n" "Signed-off-by: x\n", - 4 + 4, + { + { + .raw = "Fixes: x\n", + .key = "Fixes", + .val = "x", + }, + { + .raw = "Acked-by: x\n", + .key = "Acked-by", + .val = "x", + }, + { + .raw = "Reviewed-by: x\n", + .key = "Reviewed-by", + .val = "x", + }, + { + .raw = "Signed-off-by: x\n", + .key = "Signed-off-by", + .val = "x", + }, + { + 0 + }, + }, }, { "with body text, without divider (second trailer block)", @@ -85,7 +167,22 @@ static void run_t_trailer_iterator(void) */ "Helped-by: x\n" "Signed-off-by: x\n", - 2 + 2, + { + { + .raw = "Helped-by: x\n", + .key = "Helped-by", + .val = "x", + }, + { + .raw = "Signed-off-by: x\n", + .key = "Signed-off-by", + .val = "x", + }, + { + 0 + }, + }, }, { "with body text, with divider", @@ -103,7 +200,17 @@ static void run_t_trailer_iterator(void) * always ignores the divider. */ "Signed-off-by: x\n", - 1 + 1, + { + { + .raw = "Signed-off-by: x\n", + .key = "Signed-off-by", + .val = "x", + }, + { + 0 + }, + }, }, { "with non-trailer lines in trailer block", @@ -125,7 +232,32 @@ static void run_t_trailer_iterator(void) * because we still want to iterate through the entire * block. */ - 4 + 4, + { + { + .raw = "not a trailer line\n", + .key = "not a trailer line", + .val = "", + }, + { + .raw = "not a trailer line\n", + .key = "not a trailer line", + .val = "", + }, + { + .raw = "not a trailer line\n", + .key = "not a trailer line", + .val = "", + }, + { + .raw = "Signed-off-by: x\n", + .key = "Signed-off-by", + .val = "x", + }, + { + 0 + }, + }, }, { "with non-trailer lines (one too many) in trailer block", @@ -140,7 +272,8 @@ static void run_t_trailer_iterator(void) "not a trailer line\n" "not a trailer line\n" "Signed-off-by: x\n", - 0 + 0, + {{0}}, }, { "with non-trailer lines (only 1) in trailer block, but no Git-generated trailers", @@ -162,13 +295,15 @@ static void run_t_trailer_iterator(void) "Acked-by: x\n" "Acked-by: x\n" "not a trailer line\n", - 0 + 0, + {{0}}, }, }; for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) { TEST(t_trailer_iterator(tc[i].msg, - tc[i].num_expected), + tc[i].num_expected, + tc[i].contents), "%s", tc[i].name); } }