From patchwork Fri Apr 19 05:22: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: 13635693 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 00C3E8F62 for ; Fri, 19 Apr 2024 05:22:38 +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=1713504160; cv=none; b=AZzJB4andIZ1A5FY/RZTZAsZ9G3AfMb0O+4xMclfDXB0X0BeFAWQGhcOcQ2fK3zTgNpnm63GKBy26nexnymo1vE//W56PI7uHxuVXP321yWaSLFcKk9+c1c/oPY+JDce80BkyrYnFStmFtOS0uVK+9po/R7aPF4JtRmtBFmGXNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504160; c=relaxed/simple; bh=MLOxizPMH13V/nmWD73mPqJzfltd5DqNenMqtilm0m4=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=hB1vHaxwB0Jy/MvLxx1OVCZoxKw0JJPsLC2T2rrWSj/k3CpPD2IPk83Z0+In8LHVmJFP56xIF0prNO/kQ98xg7nAzIcYhophp6VBjAO1HcJ1m11/MPfBw0hB+f/PKuWpuTndBJu5xIiFMNmW+kHwYLSaMXb3E5AQ4+oQw03wEU4= 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=O5eQLQ3a; 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="O5eQLQ3a" Received: by mail-lj1-f178.google.com with SMTP id 38308e7fff4ca-2d895138d0eso13641721fa.1 for ; Thu, 18 Apr 2024 22:22:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713504156; x=1714108956; 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=LodBV6FiAEjtGgTccQmp9l61Rlj/gUAFYBcrwkt2Ubk=; b=O5eQLQ3a/PJV+7g0kdALZsp0kICLfCVC5z2NeMsDf2Giwn29BLoaASEFpvcRHinzab JugM0zWg6YQ+bVV97jQxR/KJsBS0B9NWB5XA4+H+8B0cxkLbZXjaziYp450UViIdiQj8 dbEbYTIc093SoxmTUIjYHsTE6ipOaBjslooXW5sFqatLe50uK176+xsfjo31ncEreJrt CCpo/yRcvAkdE4wvJleL2svGCFRQFr/oIaFfJPfpP4r60YXNnHMlgOSNklNUPeQZM7ak Gb58eNuxaJ/bIYzCOQ0ZziQAf2wCZprdoTIRe+zJ0NQesAVr2WPOQIqLM2S0OditUfUL A9Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713504156; x=1714108956; 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=LodBV6FiAEjtGgTccQmp9l61Rlj/gUAFYBcrwkt2Ubk=; b=FsJQkxEtLyILzZp7jCSKf3DxlQz850I0cdEkeWrdga/nWOPMKAeHnx8OEjcpqKvZSX NiRYFhTNp2+nR1bcVU2NeGNcomS7YMhFBgxM9UyYm3DjihLf0sdfO6EJ1v3xnQQt14M6 7olYKGxQk2J1J3csYmaYG116+6igmrinrHjABdSFnGSLmSP5ptFHjInsgjO+AeZjGby4 SEfwbQ0ZCCuzlGMCml5v1cVSHdF0lFhdgs6pglz56HTUgTQquLNa8cdNUSKsPlr9EEZk S/FPtJi2rHQL70p7bXov3JUOTvW9Bp3NqVlYKxamWxhsuOXispj1j6YFMv2U+tYxh5tD t4Gg== X-Gm-Message-State: AOJu0YwEJZTvDyDZq3cwAd1YtUJfFVAsoi9AIkN4f2U7LzjhiSWJIPbw 8hGqnCo0bs8ZejWMKNgYuyTHQ4z0ZqIx0ahbex9yqKiIhiIiMcmM/L4m/Q== X-Google-Smtp-Source: AGHT+IFXVOf7RPy8Mou3E0WJBMAnIFMkLAgWl3zj7PLGiMv03D2arw97wKsfFo/8hbsT2kGPuiY+ow== X-Received: by 2002:a2e:330b:0:b0:2dc:9b50:eabb with SMTP id d11-20020a2e330b000000b002dc9b50eabbmr488117ljc.4.1713504156070; Thu, 18 Apr 2024 22:22:36 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id v26-20020aa7d65a000000b005701d5535b4sm1662658edr.51.2024.04.18.22.22.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 22:22:35 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Fri, 19 Apr 2024 05:22:26 +0000 Subject: [PATCH v2 1/8] 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 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 Fri Apr 19 05:22: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: 13635694 Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.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 6F5F1B676 for ; Fri, 19 Apr 2024 05:22:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504162; cv=none; b=lYsSoInFmlCGzJccwmLx98em9JvzAiDnnoWXm8ZXIqj0R8pQJBStVNsMBqFtmTJRlAtPrDBz4RuylXae4pj29Un4jIWSHr9TcQ/8aTe2TTjp/odD3/8a320dX+3YB/brxIBTFuG4YLjc4DK7FgFI3QnNW8DHNMBpyFy5m6Neimk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504162; c=relaxed/simple; bh=wj9dMchDfB7R2zikuPNhoW1Y61vQzfq+pHOwYVpDRhw=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=e+3bIVZt0okouCuiKhyLENr7grF3aW4d54lIga5gYka7Qzp2Hwd96idzDpozGJW1VNn7eD/e3HXwmgxQLEK4VNUl69qEvVyyICs8uCtHUscPR9sY+EIF605eeU+gTBH+PJZfT9ghYkv9Ba87IXMhnIXqH8RVGGDlOh2CuFWiPt8= 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=T09YnAq3; arc=none smtp.client-ip=209.85.218.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="T09YnAq3" Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-a51beae2f13so180293666b.1 for ; Thu, 18 Apr 2024 22:22:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713504158; x=1714108958; 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=JUZDovbc7FZmvnnROI4CKNpCadCTv6oxoXIe8y7i6iU=; b=T09YnAq3FO/08ZjjDgti7LbiUWFJDCAxAM0ZpHYHIWplrGGe+3/PGfTwLToZ7ugAgJ FB9O1gizITb/6z/COAqIetRdf2pdGczrn6pB8vNagLezss0QN1TuYq1b3HTsnnFzOgM7 VFfK35RBYQa5ohkLnnLtPajFh7CI/wMc3V+bhLRh9K8FClbNtko8YuT5+cwTOPdCjWQQ 7et6MZH+ccOYq1jf8H9+c3Hgog/ioz+KduhGS+6FYRod3x0BMqVtX8LTDCGy4RrnCtej fG2hEZ8TRayAGCaOC5/l98n4DC/vpjLE9KLNY4ULIHk0J/JC3ku3pRqdtGdvFtPjewQs XVNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713504158; x=1714108958; 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=JUZDovbc7FZmvnnROI4CKNpCadCTv6oxoXIe8y7i6iU=; b=oQ1pSwY9DpV/xcJkQUwCizIv8wlf7LvABWFWrkyIuWRMS9DsyEveb9o8yIQ9O59WkN SmJ2exnc68dRpKftiWNS27N+UyDg1VyTN0l8zqCrMp5nmZ9Tm31/feOXAK5fV1hwIEf9 YP7nwu76bwj7+vvCFkyrKeSQgMvWm8p5Zx2r1oyH7N8MniW/qyVS4rusry1Tqj2Qn0oB PrQZdoBxR0x8tS2yyRByQj5gUj6aiA1qW8KNATwsrqUbFihli7h3rCiCnob3YkgQP5BG w8bkZUH2hmmgiFOtOq8Qrj3i5dD4z5OdwGyZdUQTfwhXz+BAkzhKjO+Nr1YNHGnlgQbA VB1w== X-Gm-Message-State: AOJu0YxS3qNNr/R8n3jjpqVQ4jxgtXZGmjj8+/2MLyeGB/XicYppgTgA yP/PBkyX3Hq9c1W50z+h4/M70yV87Fmc8ED6fsg8Djur3ML8lMKjcjZG6w== X-Google-Smtp-Source: AGHT+IHdjfi78re8oyQjj9FSqN/ZIK1qpz9uUzWf6hREe1S4djqhz6QHN6bGoFoLn1Z+jB4mdJ3ihQ== X-Received: by 2002:a17:906:6690:b0:a51:ddc6:f9c0 with SMTP id z16-20020a170906669000b00a51ddc6f9c0mr588984ejo.45.1713504157879; Thu, 18 Apr 2024 22:22:37 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id jp4-20020a170906f74400b00a4734125fd2sm1714523ejb.31.2024.04.18.22.22.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 22:22:36 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Fri, 19 Apr 2024 05:22:27 +0000 Subject: [PATCH v2 2/8] 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 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 | 175 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 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..147a51b66b9 --- /dev/null +++ b/t/unit-tests/t-trailer.c @@ -0,0 +1,175 @@ +#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. + */ + "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 because the + * 25% threshold only applies to cases where there was a + * Git-generated trailer (see git_generated_prefixes[] + * in trailer.c). + */ + "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 Fri Apr 19 05:22:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13635695 Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) (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 EF359BE65 for ; Fri, 19 Apr 2024 05:22:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504162; cv=none; b=na0lIZ7cXV+K0TUMT7Klk2Xn2kPMDk+7qaRcHAsL8Y5T18ytAEJ/FG2b4wY5NcpJMkS8O+wZTwhzGZK48fqUJLW1lwQMNRoF+I2X77e3IsK7pC7zOEvM/betSXCH+SD7QR8mer+KRg/1XIPaQtxdY9BiRFOpinuBXc5XH8mVVQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504162; c=relaxed/simple; bh=bb7jkEEZNjgSidkcGYWlD/iy6gxqsOxLaonybRIpcfc=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=NcW+bkGV7HZRRngEbfKFl2pT7kx4tTPDLuA1mQkx1Yp5JLKeGA6HH1gVbucj2l4+S2yntuIaqH+ZnHG3sp+eYwTvGBfcCmvU+DFWB2nd+PumYYp5lhRVGMhkFPNWyY1orpdw0VI4r6QR7up83Jc8mcEGuL4Ic5hb5fGkWL+eDRo= 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=aTcnG7Yp; arc=none smtp.client-ip=209.85.208.42 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="aTcnG7Yp" Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-571be483ccaso1771230a12.2 for ; Thu, 18 Apr 2024 22:22:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713504159; x=1714108959; 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=ygbPtQMCV4vO9JumpWIKOn60QEJGrJYWuGHbfZ2sPsU=; b=aTcnG7YpSoKS3rfrFX4rB0N86LME5mDAPYUDJYvwp9//Qn2v7rCC4/nJsg7nDtU0rH EtyQ4fSL7c9Aetdh3xltLmjYDZnm79QGBncLbvM5yCVhTd5sM7CoNWMux7Tn3ekwy250 0J0nU8ngduy7QEKQgIIyprDJqmpJSwEXzvidriDXf3mnbhfO3CgAJ0G+tf6iXWoDs9bC sIDXxBETQOY88uRi7dQNQ+2dySBDtAHjSFAmxIuiInNn9VJu1CorRlLKywQR9424G+9H GtcE0AD9d7A4zOMZzZsI8Ftgf0lBBnxr6lJo/YpXM5OJQQ6YRJihZQbZFWIMJGiubnLy 0rGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713504159; x=1714108959; 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=ygbPtQMCV4vO9JumpWIKOn60QEJGrJYWuGHbfZ2sPsU=; b=FnDo5epuSOaV48L6osR5FTfwMq2Gd4Mrzy3tkGI9SX10Lz7j2On73SwfwL0aPu8cl8 vQopfRmPfLKf307/fy9Qa36e5v9Wz881V35nrw5TIEiUbzQGpKGzByVWNzDmeuvvKHMB C7BV5O8IS2Kr760XcLxCByqbblXb8kTxlWPpMTITWSCvMAeeZYm2Yc74gubOLqZBc9eU dvXnO+JtESel8edY3b/ppgt1vDLWB74DdnVPcZpIbLAhNWzGBdeha6RWL0Ngn5x9wAO5 fXnsLJ31l/GlLW5nssSmnQne+57WeO7ivrJvM64lenr57t3kYdR+8YkMLS8BRZIqrbAc cWkw== X-Gm-Message-State: AOJu0YztovjyOWz1j6ZatTdFeW6FCiv1vVr32YQP9Spqi89LPX/5iOWK SAK34+lpQZS0KqkZeazknXOSiKu9k9XsZ9Vn/9eNLcQUIhUC181g4iq94Q== X-Google-Smtp-Source: AGHT+IGxglU4+X5Sxs9KZJ/WLr9MKtsf91YoNAHucgYblQbpeU0Kod17m5/hnHkwAmvsK/ku4Xl1Iw== X-Received: by 2002:a50:8a8a:0:b0:56e:246b:2896 with SMTP id j10-20020a508a8a000000b0056e246b2896mr726489edj.3.1713504158759; Thu, 18 Apr 2024 22:22:38 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id e6-20020a056402148600b005702c757af2sm1666490edv.30.2024.04.18.22.22.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 22:22:38 -0700 (PDT) Message-Id: <5520a98e2967401977dbc16f0d1ca77a8001178e.1713504153.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 19 Apr 2024 05:22:28 +0000 Subject: [PATCH v2 3/8] 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 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). Signed-off-by: Linus Arver --- t/unit-tests/t-trailer.c | 8 +++++++- trailer.c | 12 +++++------- trailer.h | 8 ++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/t/unit-tests/t-trailer.c b/t/unit-tests/t-trailer.c index 147a51b66b9..50c696b969b 100644 --- a/t/unit-tests/t-trailer.c +++ b/t/unit-tests/t-trailer.c @@ -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", 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..ebafa3657e4 100644 --- a/trailer.h +++ b/trailer.h @@ -125,6 +125,14 @@ 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. A trailer block can be + * either 100% trailer lines, or mixed in with non-trailer lines (in + * which case at least 25% must be trailer lines). + */ + const char *raw; + struct strbuf key; struct strbuf val; From patchwork Fri Apr 19 05:22:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13635696 Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) (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 2273A8F62 for ; Fri, 19 Apr 2024 05:22:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504163; cv=none; b=tAIWE2aRX8qKasFfR5DME1EmOjYGu5sg6POx6xoORCgvsVfLsr02wxBq4kUpAMIQLSoh/WZ6qEMVBZm+3mGzt6VZlAPvK2it6fDJetiWZol5NsLz5s+8Rc2vQGoSHq8w5O3o+sxAKWEaiIfYMYjjl74PK+3T4xPRENJgHRb5G+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504163; c=relaxed/simple; bh=9kAoMDOFAxLoPOZrzD7YL4t/gUJSopvjOz6CeQeZXkI=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=bjm8B8w3Gv74BDmdLpMy8C08n6iWs4OWkd/IHQU8yK08SByF8BGorpabR2C19NedoCyO0tAOIy40ksTgEptE7Xxzt6fImRUox3PZZG47aoByvpuMT/M2NXJaN+zpHxmE5wUTC/c+9k+jLUg4sgR+a5a4QHZ4DRC0aT3vuVPbnoI= 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=EvzXPFSQ; arc=none smtp.client-ip=209.85.208.42 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="EvzXPFSQ" Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-56fffd131a8so848559a12.0 for ; Thu, 18 Apr 2024 22:22:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713504160; x=1714108960; 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=MOGKGuTkdwRSPqSSaqCCsiOdFMxgqW/ZfvgOAqeO+ic=; b=EvzXPFSQYfz9Uu/EaAtkqOpPFyoJAjFDRCB6m79Z4aoyw6Ot+wGTbJm6IuELW+7FG+ YvzQEnBrrHZOU2N3wMxtCbDT+ImB8TZlnHqXSsYGtE6m3v7BZtV3zYWFqBjWlW0ByksK UvhD9W5bxO916Gz2fYVMXItWPhwN+kx1LpuW5Ada6HrZGF0lnHpEio0/IuxGEM/YxGiA Y7WBaB0RfVdOfOMvS9nJNPuzMUFk2LADslVhX0xQ2wqT0k5YyMGoZ/7nD3vIXmKUKYoZ Lvfn1zCRkVuPbGehMHNm9KZ/Vif9iR7lH+7YMwgFNTviUYRbrQ5/QWJcVW8f68gsoiPJ VcxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713504160; x=1714108960; 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=MOGKGuTkdwRSPqSSaqCCsiOdFMxgqW/ZfvgOAqeO+ic=; b=B+G64yxWTd71SJs7M/0x0zmYXZgIwFOe+2Ce5bhijhJg3R+mCcc/0AU4ZL6YZZ7JVS HCs3V9x0pFyb/kaulOIG6Cl6hNNvtskZHBjVrGjaglodZppR6UZ2UYecRFBZm8+tMGcb zAUYXo6EDUzSXRpxdOnEncXr2bB0yXljsIMFtJqgu7Lh8RK2K5l5CxqhsUMDwQO3K+sO 6g7xXR5XcH6sGev8CCrE5EIsqsJqQ11Colz/FntvugOQ/0SkIRYBIFkupsDTsWX5ghtv kWrwQ7W1ZSG5Fj8beqOh+Goh6D08BGQY5A0zXMn6jxp+Zgxmla1i5u9FE7x46bK9+q4c aGSQ== X-Gm-Message-State: AOJu0YzZChgm3f+Vdq2+MxPllKIuo/c+YATAdcjgLjRIL4A1rR199j26 3kBECmmTZHJv8/T6g+fqf4deZYkqYNQGLYSKZW51wmQSIzE0wVNY1oQsBQ== X-Google-Smtp-Source: AGHT+IFH6FxUVWmm3EXgFrWEXIatmqhTzEkCy2AWjjkq0Trq603oHj/f3UC+SwXc5ABJ5ig7Rdy2lg== X-Received: by 2002:a50:cd1d:0:b0:56e:d54:6d63 with SMTP id z29-20020a50cd1d000000b0056e0d546d63mr777158edi.15.1713504160034; Thu, 18 Apr 2024 22:22:40 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id s12-20020aa7c54c000000b0057025ea16f2sm1670260edr.39.2024.04.18.22.22.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 22:22:39 -0700 (PDT) Message-Id: <84897cf5c83eb67c023603016b49fb7b56870aa3.1713504153.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 19 Apr 2024 05:22:29 +0000 Subject: [PATCH v2 4/8] 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 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 Fri Apr 19 05:22:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13635697 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.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 352ACDDD5 for ; Fri, 19 Apr 2024 05:22:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504164; cv=none; b=nZp8UHT0mNWooz1mrRKVwr0SEIStl9ZYDa8BoGmtN/7Tm9ZEY723jdOua6VKZ3kw//wZvpog8207Rb8dW9Y8a7+bX+GgXR8BG0dosNOWAkpI4H7De/fU4SbRITeM+kHVQ/6do+fUFMHK23T0Yz87U/7Q3uiTC4g+GnIn5AJnAT0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504164; c=relaxed/simple; bh=es/8xvwikbdf4BTLJ8Qn7X1Yb95TjQhL82KXUPLxrrs=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=oOHZLUBZ94XuLxkQtL6erA5n1p+qHNIarkQXuU1KubkOUWoMW/C8sfMYPqlHaROBk1Mr3xBP8iRnZ79SKzMgSjwZfUa4xrAIgG3ia7qlCmU8qOlcVZY2HiSCmGMrax3w4F7yTd+dqmNRzT4HZUAOuWGK6FyApJYea08SO2I8z5g= 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=agEOBht9; arc=none smtp.client-ip=209.85.208.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="agEOBht9" Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-56e78970853so4316135a12.0 for ; Thu, 18 Apr 2024 22:22:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713504161; x=1714108961; 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=P5jundgRnTRZ1UeFsaoiYa52OHNNlsJEIZK+0TSBpDI=; b=agEOBht90C4kSNZNcE8elB32Ztn0UzO2sMhwMhM3r+CdRFPxkyWZPR0gaMkQh/0/tE szhc79/ibBhN7NlBelrQ2sdPXW5HfZ1t8qrGRyuxWM+LJCCGtUu0bPuxwbMuMawNFHBG F9yvoDAwggxAopjs2ymEkC59v4PABGVFM2jPCs8xYO5GyOgSFwHu4xfwOej600WLM/bf EqAoPnOUDfcho/6KivNCtoLC0tB3mpQVaNdXB2Z7gLyxSR+oZLASk5oWVf5B4SvFnh7L GnNABjujA/jQM6aEVa/hyy+UuLCcDv8/a/L3vdCGL+vz9hHTJ4NbBXcXnLcdDqMyg87S eSLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713504161; x=1714108961; 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=P5jundgRnTRZ1UeFsaoiYa52OHNNlsJEIZK+0TSBpDI=; b=vnz+/dLrGxz+ftDyXKv3dg1pRXUTFuf4sryV7YZFi4Cn9q96V8MfdHviH/+Ircbbde z+25ABShqeQyB/BbjFEDsIuP2ZijbpBCZSzCD1W3vK2x8+4+/mQBPXLRtCD+9Uo2tnsa vRylpMwOxfKI3u49Xy4d5lyonz8afZxCUpDpnqbXB3ZGeb7p2D7Z0ov9pM4O2ERstvMl Nzz6/EEGo9iDNbIWXyoCgSjvzqM/uOig7q9pbAQNF2YnsWj4WIKFt4Bqyqct9MAZu2Xd 5PSttc+P7mY9j6JKDCCoHk+bTkTNxm7OE78dtP38T57giZIsvzgkyGVS8cOZaSd+602E S9zg== X-Gm-Message-State: AOJu0Yx1EdMWIUVjPyoN1ofHaJEheqy0u9YVRMJtc9LCkBkJE3Nunf+C FeOi0TvNXqxOyXXbQOCJ9HV2xFg90IEx8PcGTxBYRK+LwT0QnheQyLFJWg== X-Google-Smtp-Source: AGHT+IFW5xWDm93nipAThpSUZk7CUh9J9hUqPuFSScF5sbjcDHP4YS1yRBWiD47eqkWeUo8rv4HnYA== X-Received: by 2002:a17:906:c812:b0:a52:a25:2077 with SMTP id cx18-20020a170906c81200b00a520a252077mr3580599ejb.14.1713504161225; Thu, 18 Apr 2024 22:22:41 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id k26-20020a1709063e1a00b00a4e533085aesm1699780eji.129.2024.04.18.22.22.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 22:22:40 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Fri, 19 Apr 2024 05:22:30 +0000 Subject: [PATCH v2 5/8] 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 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 ebafa3657e4..9ac4be853c5 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 Fri Apr 19 05:22:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13635699 Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (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 A75AF10A35 for ; Fri, 19 Apr 2024 05:22:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504167; cv=none; b=iZMIVtwQrZyqrF2ut1ICLB4VXousQKLMFdDIJZisK1FDA1bfBPfXVGmtVuvwJgNNQN+N6117PdcFAVwphN32X/2/faBiYvsrWb8NrRkVTUA1mW+cW5t7bfyZg2couk5kfbMhpEq6bj2u0FvMuT+n+bzPEbpddHlpi0wIBUmBhb8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504167; c=relaxed/simple; bh=4uNz/BNgPU9bXhIVD83KXZWLdRC44l+OQ1r4zFrYzCs=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=Pcvgcc43Uba2bA8BPjraLq9ttbKhOpsjQ06Ln1H8Kt9UJU5sLzorpT/K+hOf37oD9q5JIKBuYmzQ+DvCpCWH65Ctcv8pDI7b64Lapa873O9kQUlPq37euvWC9SSWXHFM9spK254nVl7+Uly13tenGM9RCvEpabH2YgZbSBh0R64= 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=C/E4GkIv; arc=none smtp.client-ip=209.85.208.43 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="C/E4GkIv" Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-571d6d1943fso53032a12.2 for ; Thu, 18 Apr 2024 22:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713504163; x=1714108963; 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=tYH3cOb+6VK9nymkrwkTOMX8VDcmssy9ePiYCbDT9xU=; b=C/E4GkIvTkm5dSb90sa184PuYhFlh2pSGbAYN+T2PJC7dvm7XZOmegD2Rs08YMrbwD S5BQbV8ThVwfeyFlT5oWQmbMcTUrkquSmyyMYUCz1VqVHshl6zYW3JMzqm25E3fvKGOQ MBbiH1JHOYZEZPKQ2Yd2KfXxp+VG+oZbCmKdDYz567EwMlhe2wU9UCr35MSFIAFJ1vJO EaJ8WHg9ZrNbrBhMn4hnl+6otP1qZYQievQNKSyZbDu3bAwLu9k8SlM854V0OI3g4Dqm poUxtReE2H6uBF32Z9KpQdZP0CTTHlRLM4hcOAH96IK6WZ36bngouEqx4D8TJmW8CeKO zvFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713504163; x=1714108963; 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=tYH3cOb+6VK9nymkrwkTOMX8VDcmssy9ePiYCbDT9xU=; b=n8eiQL4vGMFl0KuV3W9milGvbSzumGuJc28SOcXQa2F77O5WD+d4BdRgI6Ky5mMAQR pB8eOyRawAtvUJ8OIhwoeUO0QG/w0I3XOOT5JenjcDG4Zpfar29wIdISo8KcXriMwFfS xxDZk38kyCX9J8/dHup7lQfZfIRLeY5jaeRfPLCSH804oEQe1jxLudNzkokyCHYD4HXn RmZpero+e4LMSmfGekuF/BwvmQB0D/lvBNYq5J8f7P5u2T90/LmXnfLNProx9FyWW6pn UwgNCgPDNi/2VIOSZ0+7xggwAewP18hgu5mKujzv/tjVb7kZdbJtdrt4mehZvwa6aWRA tzfg== X-Gm-Message-State: AOJu0YxoOE23WxoU565mkR37CfOu7ZIy69wPsheg/VnkidELWqBc3KPI o80dG4AtPIMOWPkJVLgkZxMRkApq2N/Jgq1FueYNnwy6jL9bndXuyXY6Qw== X-Google-Smtp-Source: AGHT+IEWR+ZQVDh/n2w6gMVLv2lUroct9kjzX2izMmS0hcvntNRp3ZMKdcYazZwWE14enVoBgWhk5g== X-Received: by 2002:a17:906:c290:b0:a52:1e53:febf with SMTP id r16-20020a170906c29000b00a521e53febfmr709405ejz.69.1713504162505; Thu, 18 Apr 2024 22:22:42 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id q21-20020a170906771500b00a51d408d446sm1729623ejm.26.2024.04.18.22.22.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 22:22:41 -0700 (PDT) Message-Id: <093f68f365801bc8801f29dc3e6eaa9b07fcd57d.1713504153.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 19 Apr 2024 05:22:31 +0000 Subject: [PATCH v2 6/8] 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 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 9ac4be853c5..b32213a9e23 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); @@ -142,7 +139,7 @@ struct trailer_iterator { /* private */ struct { - struct trailer_info info; + struct trailer_info *info; size_t cur; } internal; }; From patchwork Fri Apr 19 05:22:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13635698 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) (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 5A0BC10A28 for ; Fri, 19 Apr 2024 05:22:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504167; cv=none; b=i6zNzAP33Y1JMynKBEFBXF/PFFaXfP4dlbP/ga6g0ixzfZnBTeO5zUbVu3rvq60v6Nt84YiVwE/0gHpT/kgHJxqCWntfgHbgVLPLMGNzbVl3qB+nWoJmoSMOEckbUiQlMomcBE0zAWmEzuWjTprZkWoaO6skEcyCJ0uZnz1X0Vs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504167; c=relaxed/simple; bh=jfbRNkQyvPEiLKTtOYB84JTK+sM4Ui6G4u+TROtkgDI=; h=Message-Id:In-Reply-To:References:From:Date:Subject:MIME-Version: Content-Type:To:Cc; b=byJ0UAXrA+GJ4Dw0RUEuKKD5RMYJNFsF/DbhaVg0fdwSzd9dUFCQQvfgxnnhWT4qGUzWp3vo7AfzJuxPKVZjB0BWOpE9TG1nygruRM0ZrTMG7LY0xrhnrhZ0gFFfzR+14hxjSx7MRBVWEo6cYcDA7RiLrUjVTMjDvHoObjBRWvQ= 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=JcZvZHIs; arc=none smtp.client-ip=209.85.218.43 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="JcZvZHIs" Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a557044f2ddso159780466b.2 for ; Thu, 18 Apr 2024 22:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713504163; x=1714108963; 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=H1m6zhjdREPDDft/nFZ1Z7dW98zExW0HvemFKuR7ZvE=; b=JcZvZHIsQhlFezxwoiZRdVu9TyUYYG5tJQ8EdxmrfivDDCsyD4SCCJFOTlmZBzgVSl GYieaERkTiraZn78r1xVMVyAhGuWlqXMABrED2EkYo/fraKHwvhoSBgRPvlSQc5UoBeT EuEFu26a56QRVPPi08uxPplpYObRid2JPAno2dDAzAJcMnAj2VX8RoHJqKkkEmMVt/3k UBBSrnCu3GALYeBhnX8K6GU774QfEROm/KqyWHdu8cJHmRmmLP6JlCFfpmoosa84462H x24pR+NveXfDfcL8ieoxBR9w3ByMEpH+SL2+95Wt12UKlj30/dFmCqHOZ6GwG4T0qwWL cBeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713504163; x=1714108963; 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=H1m6zhjdREPDDft/nFZ1Z7dW98zExW0HvemFKuR7ZvE=; b=GTTIT0RA+izhCiKfEZlMk13yfWz0PS47ZN4y8t4H9/GNJpSfqYcfbgkicxRvh0YFbO uSaZ1/pBMnPywtHb8xj+b3CN5CmheCqldf4JqxTfC2HNFHld9F7gVvHfVL55uXx0Mg9q GKQKg36csPQ11S3dPxGZifrgHgAFMQeuaFELQZoTu6mudaj6uEGoEeJcxh1o7Uzfnnx7 CmW043yIPqm/pqGt307xyfXzncOAtNQqvJReqoRDSxxnQe4ewQt2AuDssXUTNZC+1yZ9 SC5Woo4OGfafrCG0XjcNoD7lZrFQVZgW4M8Qiq+DNU9awgqbz9leLzf/O09NTulaSPTZ jRpg== X-Gm-Message-State: AOJu0Yyr8Aj8zgmhoSMpVnHUl29I2j6ObXBpsxuHbNnlZuO9KiL4eb7t 6+hl+BA7DZm6hRVfFoKaoPiJXr3atUIeqyfv3esCdiCw1eIgcFfCwWi6rw== X-Google-Smtp-Source: AGHT+IGj+Iub6ivOMfOi/PQdC2GXYswUKo04ELWdLggGUEck0HaGTnARyNbcLSD3KPmH/HcFiMXnWA== X-Received: by 2002:a17:906:f148:b0:a55:5e89:a158 with SMTP id gw8-20020a170906f14800b00a555e89a158mr713016ejb.68.1713504163412; Thu, 18 Apr 2024 22:22:43 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u7-20020a17090657c700b00a4e07760215sm1727687ejr.69.2024.04.18.22.22.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 22:22:42 -0700 (PDT) Message-Id: <0e9ae049b8861fecf49c097e8d52e734f7a9c9b3.1713504153.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 19 Apr 2024 05:22:32 +0000 Subject: [PATCH v2 7/8] 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 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 couple 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. (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 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 b32213a9e23..a63e97a2663 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 Fri Apr 19 05:22:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13635700 Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) (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 33183B666 for ; Fri, 19 Apr 2024 05:22:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504168; cv=none; b=OKAjPkRPOJW4j8Pz8PMWzTLmoy78zV9iXVQfLzO+IsKz/LaAon5bAARzikgXNRBf/FVEeiTZOFM3QsqVrgEjbiKF3D0x8qRGAiUh+3bALu0/vHcrJ+bZ+hIZqEofQ9YJIg8yhxpUVpjdeBFJ1HiUvcLVALE+BOo8LoCpOVTSE/I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713504168; c=relaxed/simple; bh=yX6rb+UiEB1astWPY1iRzrTuD7QPl8KVlv1oC88H+4s=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=s1ZeoG1rGWc8e/+d3ArqB9bMuJN4hoH9Et2L80E8Y4IkNaQHI+SCDvhv9AZjXYpLsJqassLq8Ypzc781vATUY3bx0tXnnUwG2F1/tjKJ+iUZ5dAaJfQzMV+OTJ6qZZEAd8TdqXBsKlLsPS+pGjne+w15vuDIxSCxg6iu5+cVsD0= 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=icc9x0qW; arc=none smtp.client-ip=209.85.218.44 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="icc9x0qW" Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-a555b203587so174395266b.3 for ; Thu, 18 Apr 2024 22:22:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713504165; x=1714108965; 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=yELVRQ8ZDtDc4lZA2exN72FHqFJ0qjVHYEiiVMabbG4=; b=icc9x0qW0COGYoF/poeBMTfeV+Or1aWZQ1z/PD9O8Jela4is/ubOZ/tCCRVlfWjZl8 wr+vVbI2OsR3mdy3QWcv/DSP3iGihCRyjznuNIN92dcdLaBn/JPWDjNrRmHHFGXm+8PR DfKwMRPmUZi/qO9ZT3JQm7K/kwizo3/o+P2d3CvnZPrVKD0xjxCeqNrC6cku4LqVSQP8 ih5dsqoPKFVXaXmms0ROreTHf02YSZBkquilk8f96ufivCuWdL9Kv0bMHeX4fEHDac4E Vg6BkIIBjSbxnMtQA+1WcJG/Eu2UKCvfMV/+W43ti1SKEkfAPIjjwhRBxNeUi73EGtwI HHkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713504165; x=1714108965; 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=yELVRQ8ZDtDc4lZA2exN72FHqFJ0qjVHYEiiVMabbG4=; b=e1Kn8b8koqiWJnZg8wv03l++gvquX1Vbpln4jf89NJYOjpKdd+Xi0YdXwl8teir/iY He9oqYiuT6WlKjQhEv0yi5Z+xVXW3rhzCxKrQ48AoG8jQXM0bXlWkj6YB1X4dC3fAs9M dH6HKMWA++2+Wm+TRleVZK0rz7hM3lwBqHaDuLb+0t3CDsU6y5XHqvxiYikNkACaFs5v jRW89+p2ppjU26x5k38XRHTUg8apLdW4xH6RDb9vWh0km7J85h5j2ZitU2I/KZwV6IJF x9Y96ikYU6nY9Tc0xe04mWOPFuU6ExrhR6I9YB1Dlhb/ae+sDIUAhK/qgYdn2aUpq7To QvHg== X-Gm-Message-State: AOJu0Yxu7sTsgJ0JiIB8lawWOyQOt9DFUR9mEGN0SUvas+sf9Vr8zxll bL7p8qWzHZn4qw42KaZYoM4oYYyyqtaueQyu5anrFFRhXkWc8VMheSHhyA== X-Google-Smtp-Source: AGHT+IHmBm4QYzJhJ2YQwLelzeTx0TjyyUQI9lyqBovy+P2qHpP4Ongl2+K6xrptEwL9HVvzBMGo2w== X-Received: by 2002:a17:906:aad8:b0:a52:6e71:e770 with SMTP id kt24-20020a170906aad800b00a526e71e770mr852049ejb.11.1713504164788; Thu, 18 Apr 2024 22:22:44 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id v22-20020a1709064e9600b00a55865b2760sm328411eju.30.2024.04.18.22.22.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 22:22:43 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Fri, 19 Apr 2024 05:22:33 +0000 Subject: [PATCH v2 8/8] 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 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 a63e97a2663..1b7422fa2b0 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 *);