From patchwork Fri Apr 26 00:26:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13644040 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 3F645365 for ; Fri, 26 Apr 2024 00:26:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091176; cv=none; b=LsReIVCh7rtnaZOVdUjTEDM9xoBPmaBFOGdd5KYHHHa051d00vOSn2Q1JESNYThSfo/G7Fk0pbJqaji9TNMwcjopT9Jg6etmzjyb0VoPhp7qlQXFRQ0zc6cRIaJ/ZVb+OaNREJMjwTGCgSA9uvZ1MtMhl9jJA0UTocV8wdSx4kY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091176; c=relaxed/simple; bh=MLOxizPMH13V/nmWD73mPqJzfltd5DqNenMqtilm0m4=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=AS8R+/P7YV3iSP0jbqutNsm/J3G2ljxpFhimxERznlKqP/lKiQjJv5GQScmGe0I8eAIITQK26qD888srVtZ7CRA34TpTFeR0YWoEDlXRipZAA2Z2RS7jy1snD/6bDmOTCOeigUTGNYu0fV0PorFmNwsQSvrgSQMIDzovaTD+Ufw= 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=ODUsYcan; arc=none smtp.client-ip=209.85.128.41 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="ODUsYcan" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-41b794510cdso3706165e9.2 for ; Thu, 25 Apr 2024 17:26:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714091173; x=1714695973; 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=ODUsYcanwc5MxC83+uWHucpOYQ0PaIhZxeBymS8MCrwAV6i/gzODDYhLs+mBntt32p kUYuJPtwk5+Tpf9z4cb4pwOBgSb2+KTLgnE49MtrPyL6RbXL+He0uQ23Dqt0PsCS6dcI SgaOXBhbGK73qNBqdnYHXtSfu3Nn0fyv9R7kmpApDmbjF0IREmg+77fvB25Dlq418zRH jHJHtrau1H3N7bKCwLIE6re0mWCOGChlKCPxshUyDqlB9ur6ydsTgQTEqf3q9Ir1tZm2 Us7qI++9cpCg0dv3JsRwvZ3hj8gCrPiErGxaPZ209WoFT4uW53ye8SSaieI9PTCJg1Mu hGUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714091173; x=1714695973; 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=b4BAhEfg/pBHHTcuEEwC7RUCqOgsmYfFfP88WKo4c4ong/aE2p2oqDxuKXkr3WiFbl Xb/cWMsV0/cEnij/XDk6xYgTGuvcNVGMy0dwqiMOr5f5UoYxTS75bjArFsYBC5JF42rb lbbulp1ZTXz1+KeHoiwU3gOaNwPSUDOa2O2bvQ+v/AbFBDyFXtXvExjSq2M1s+nNDOuc zDf4XDbmSaz3/BK01guN9X9yPcKXuNn9DVBezFeFOM7fS66xN7B1rGqJv2LRDGxwkY5f 3bRbmAN0Ve6FJ4m6/CNNFCoTKPMITznXXyjSDCQlxFgQwPIkiosSVOz47YYiynTasfqM nutQ== X-Gm-Message-State: AOJu0YwQBkl6gXvbhY/AR7NU2GkeiuQnjOokmVXSFagoXr4sFXbYfs5f OVWIUqePGS1DP1feIpzwjTHZw76UVhGxbuPdDCDxOHJjeL+5EXuSfCvvMw== X-Google-Smtp-Source: AGHT+IFIddlU/SSX6js9aWkPYZW/QmXUMwHWHgz4zP65YG6bG8lqno5s2E0J1LmnWzyOLoXxwRM0AA== X-Received: by 2002:a05:600c:a4f:b0:41b:39a3:cbfa with SMTP id c15-20020a05600c0a4f00b0041b39a3cbfamr855284wmq.12.1714091173014; Thu, 25 Apr 2024 17:26:13 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id g9-20020a05600c310900b0041aa79f27a0sm11967859wmo.38.2024.04.25.17.26.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:26:12 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Fri, 26 Apr 2024 00:26:01 +0000 Subject: [PATCH v3 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 Fri Apr 26 00:26:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13644041 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (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 8FEDC37B for ; Fri, 26 Apr 2024 00:26:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091177; cv=none; b=YPFtL+lUK3tF++nlEaYa2A1yrkcpU9P1tUQ1YeeuMZTYWjXH0ZAci7LBQz/NxJWJ1Jl7MVbr9Gc40Ttw9Ky+DJY3WwTuwGdwdpXSmtLalg4uR19CsAl7w+8Je8I7HqoTyrItrkrZf/rsLe/YSb1QQnlOR/kJi+aYt85o6VOYmoY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091177; c=relaxed/simple; bh=5kDmCkE6aSdCZQlUEGCZKs0Wga1/nqp2UJuivy9Yij0=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=TvHk9fPT/TKQad/KsBANjfiEhE2nq8AOx0VbJXawGCwZwyeCdav64EbqW6hNJTOBSXcBMyPpSAGQDOGPoWxVFl3hjXts2ErJ8CeLeLrXjlE6Q8BtlnotYZzFDMIxEPDOhGqucGfMLMNMnBy8E/WWABQ/jdA+nIzI3+I92tVwXRA= 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=HWFAwuMb; arc=none smtp.client-ip=209.85.128.47 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="HWFAwuMb" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-41b498ed12dso11420185e9.1 for ; Thu, 25 Apr 2024 17:26:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714091174; x=1714695974; 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=S1mzvCb7c3aBNOXmdvRmGBEIZtu18iN0xvOe07d65Kk=; b=HWFAwuMbzzhzo9DY2gc3Rd/nY/FCDclZWIh+k2Pr54siiAqFn3sWLF1YMQ8uQKiJQ+ 40AzkUw+MVd3bhfG1GvzimGIwIq02dOKm/eVWdpPkjvOMq7eaXxmvzuBbApPlIwW8gUB blVMGZ9tf3Jl2FtWLPYsznGGpILWcvoVG+SCkhgZDqvicPuo6xC8cj/hrPQimL9Ibh9L yKdSBhowTkiSYsFm1CBBgjSE34mX7BMmyAfuLPEeCIl2Z59ywJOaYINrAqheSAl5eF/R 8QJ577SFz/1jNVhRGbpYY7R74E4Mp9L/feAAio3dgduHBtvLOoLwsJ++hMiRw2zuWVd3 f3jQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714091174; x=1714695974; 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=S1mzvCb7c3aBNOXmdvRmGBEIZtu18iN0xvOe07d65Kk=; b=mcxxlWAqDyEMno/hGLZtFTH1jBMkkHdJl9dZ2zyefSOOhBUApDXCHf1Y8iS59Gz7hO m6ZsTC6sFyHQBX9JDygfhTm2IC0XS4/PYfUQPtiml8Rk5q8rc3Rfv34QbUWMBPSc5kXQ xYFfW4m6YjD3zN/7mUeOwUzbGDCxiGRmpvB16Ak3QThJvBxKR/Pjt9/s7DQHq6rlYK+T tnNgV5QDaiVg6PxfLtsBRT6CDIFj4IrF9HfV5/t6xGDkZRsJDfCeMegMFOms+clXqvLx m4EnME1/OLNn10Qyo3PQM57xtUxqfAHcr+OUNhroINghpJG08HvlvEkKPWYxLKDl2Tis oewQ== X-Gm-Message-State: AOJu0YzY3w3e1905VXpmJECPCn5mf7fJsvByI875gsKWifL1AZ35YSMU Pirq0pcBKZDpS8lqWYRbGQen1CHNICH3+E7x/C1bV1wPtyPWvfrzNxieMw== X-Google-Smtp-Source: AGHT+IHXpl69UjmbqlkvUcosB0TDw2a82109JNPMwispMd/3J+lwF4xLHtiSlcd8z00Gc+k99RBchw== X-Received: by 2002:a05:600c:1910:b0:41a:e5f5:99f8 with SMTP id j16-20020a05600c191000b0041ae5f599f8mr920492wmq.18.1714091173679; Thu, 25 Apr 2024 17:26:13 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t6-20020a05600c450600b0041a3e724fc5sm17187349wmo.19.2024.04.25.17.26.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:26:13 -0700 (PDT) Message-Id: <4ad0fbbb33cab9d5841689cc5660befe6921d515.1714091170.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Apr 2024 00:26:02 +0000 Subject: [PATCH v3 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 Fri Apr 26 00:26:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13644042 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 9826C394 for ; Fri, 26 Apr 2024 00:26:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091178; cv=none; b=lfqwIHW3NFJTcL2CEoNumfOAaV1EPc48LqhHQogfl+TFgyIh3r3PWbuIeurqHt0vXDJgKJ8v4RXlu8IlPtbV0dFvgyW9voLodO8U4afnNpn87xeQO4O/JMnGJiHiCkAyva3JDumPiZ25ZZsdcv2DOwGuoTE8CLEZQRajcujeHFE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091178; c=relaxed/simple; bh=pn6lJAactYJsBSPTIFSgV7vIlupiXwHHFTTCX2GvoxQ=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=ADogUnCpJtfkoO2OeLWV6w+QXx/5ygaBJao6WqWiXC/qewCNWl5cB7O3QhMKoECdF/nDL7ASweiqMjqX0Ytmpi00WiXcmWfuUFL+itTPsaBBeY2bDO6eG3NwpuR5aDSLgT2JRoqIdoewNQYJrpsl0bU5ZHYzk5maQqmjjaWoJcY= 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=Ef4C8E7P; arc=none smtp.client-ip=209.85.128.45 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="Ef4C8E7P" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-41b39d38cf6so12350255e9.1 for ; Thu, 25 Apr 2024 17:26:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714091175; x=1714695975; 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=I7/iBf6uowCR02s0pWxLwpTzzI1CCRdM3LPVm9XghlE=; b=Ef4C8E7P2Mh5LifiMfEQfY05jdUrMvOQzJ1Jx3WLYimcS0gn6mEVa7FUWvAtd9FJpX TIMghXNZ/OHtcDD4jvtGj2+5sl0Cwa3+P0t9RnrNx4XqJhBlF6UpQMLEg9itRe1LYeBo 4s5+EyMSkSRc8ihsjBUYpmKgn5gqncQh6G6SV9vwoYa+JmrwkREKr76tdwqDXWQaVrZi aocjST8RxQIuEqyeYaBLgF4GpWWc3WNDJl1K+C3WJSbbagU/OW/Zu1ej9IoVwB3F+4Xn qSkFkQLWHZCTYXp+J96S2OeDEPnfSnwQbiq2wJDkL9oMxZkgqWlVB9Xzo4a49QRCaj+F A4kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714091175; x=1714695975; 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=I7/iBf6uowCR02s0pWxLwpTzzI1CCRdM3LPVm9XghlE=; b=NMiWcMEHABhK8RNxCxiY0uQW6IvlX/Xn/FuxLgfpkIrgxSJOmE/RbpdAVMrIu+BsID BpMj/CobWy0IYSLmOnKVLAoLlP6DRNrt/GHB+/eOGgQdOc2ugk/HGxlQAZcGE9Zq/iss KphYnbOz+gVZerzN0rX4Qq5kFPRYecEU6BWvVVCEywtTHMb+VrR7+wf467pHKhORQYae +eBnO/ot3dlHM2EMj8ehRbD0Iqf9ZqQfdvldAJgudzzxcToTdZNXxfbqAAo1GUD7XpJw qLBDw44Yh+9uiTTXOxz5ITos54XP9XUccJx5cxq9c1z8N8YPo4anetZXOabnU0UfpE2W F29g== X-Gm-Message-State: AOJu0Yzb0E+XtpF2Uacjm5YXhcqOQKx6IaaQJj5JL70OFFmpt6JF/UGj zcmTJmk3NzSpAFEl1XoHww+Rr6tV6VZcCgcoJRU6bsq+pFc/lhfacG9+Yg== X-Google-Smtp-Source: AGHT+IF/ewagsng/QHBWVidOl0u4Um0kjDgZyf7rqPMHHBE6CMtTelgfDaTwaXovGFbjvTnhtsSRxA== X-Received: by 2002:a05:600c:474c:b0:418:fe93:22d0 with SMTP id w12-20020a05600c474c00b00418fe9322d0mr726770wmo.11.1714091174675; Thu, 25 Apr 2024 17:26:14 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id q2-20020adfab02000000b00349d8717feasm21005614wrc.56.2024.04.25.17.26.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:26:14 -0700 (PDT) Message-Id: <9077d5a315d0d7272266856bf75a75b0a24df91d.1714091170.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Apr 2024 00:26:03 +0000 Subject: [PATCH v3 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_objects" 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 | 8 ++++++++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/t/unit-tests/t-trailer.c b/t/unit-tests/t-trailer.c index c1f897235c7..262e2838273 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_objects) { 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_objects); } 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_objects; } 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_objects), "%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..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 26 00:26:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13644043 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (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 1B1501869 for ; Fri, 26 Apr 2024 00:26:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091179; cv=none; b=I3e3NjC45/UwAn1TuGXdOjEXJ/0i+2tPguL8KDrX/yupMZeEfYU13xMi+nkvDBz10UCbFTLKNP/+yVQc0NST9qq6ALTscFcZnbFpvO0FubLO7uWDjwStSSCm4t8eyx88DpbQrav3eVRpAjP9mbafkGr/KHhGlmo2lTolhMjaYec= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091179; c=relaxed/simple; bh=9kAoMDOFAxLoPOZrzD7YL4t/gUJSopvjOz6CeQeZXkI=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=nQNANU4xsKuxv3f1SRtoP2jP2Hp6NFC7Uf/oj6v08ia91VPcONIVmUIXIoHMn4SQCpIzfgZJER4RvGzNDZMU1OVJsOE1B6sycrqXkfp2WxBtq2OMvfLLzqwyeFQedw+n8LzGRqwSuANs5K3iyTCoBaOUOuAzTs2PB6XIkMuuu9M= 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=cgeUCvW/; arc=none smtp.client-ip=209.85.128.50 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="cgeUCvW/" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-418e4cd2196so12804225e9.1 for ; Thu, 25 Apr 2024 17:26:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714091176; x=1714695976; 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=cgeUCvW/eoKa+ebCLtnClNSmpgSwpM7UKP0phKUBpOoyrgkCFCrgnPHaWIcWZv6k90 RTKV2Z43wdaCt9gtz/38PGvI7PuOBId3VdOns+fb7v+9dyNNHjbjk3RPZBdbfR299yhI KXBTbOKB6W+niaBOp/f50bkn3kRELW+oyUXcKvlZdVzi4/zr9gNcF+JuKQuuJYq9YgbF WEHjAEtYOPeqnbEKVxL8K06IggVTVFqKqyOqCRdVtwYStSDIBVijl+euZEl89DN4hfOY eZ6ln88L2I5/n3vbPLMFHYADS38sLXg4sR5Vm7Yf3TyzEaKl9fnl2uulr6yn/9rdLfg4 Phwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714091176; x=1714695976; 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=fXKosHB5RTm5SdGtmnw4YD0HfFelOwBVxkg5qePTv7HNUuiyBMcwxgiaXuRs9oQ93P J+i1esVmX39Hh6eL9407XmElnSaSDO/x5N0Lak2klefuiF3MphGNb3IPnks3+L4tGkPn 6wh9UEzooH29ipcMRfC/ZR4QQA9kWV+G/baV+iOvTKchXrfkJWDLi3TuL7KIXL5whjf0 CbRGpRjON64dAANNerrtKhVQymELCDdbzeP8+pVDR/swWaQS6M9fQ0vahccDQifdvdEi gV5+pDUo4CP1VUYMnYcyVKaDfkJvBL7+bS43V5l6tQzv2GxO9SgN//55c7Q9zgp9ryhW +lAg== X-Gm-Message-State: AOJu0Yyffr8Uy6k9/aTxjKhFw6aavjLXPVbexy8p9fRyBP1Jz4+16sJa fK20RS5yKHHfaHRh7NReAEIfmee+Fr7vlgAiBOe2wkmift+DtS4AXGnMAw== X-Google-Smtp-Source: AGHT+IGtbgWHo3lYWEaJj/z/aaEmMO4DiwMb6U8GppiH1l+0jjSmiWSK3vrYKydU4aJ+fi6iYGMt3Q== X-Received: by 2002:a05:600c:3b85:b0:418:2a04:d8cb with SMTP id n5-20020a05600c3b8500b004182a04d8cbmr765104wms.14.1714091175644; Thu, 25 Apr 2024 17:26:15 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p6-20020a05600c468600b00418accde252sm29248506wmo.30.2024.04.25.17.26.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:26:14 -0700 (PDT) Message-Id: <4a1d18da574de8d0dbcde62b2d33c05026da1ec1.1714091170.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Apr 2024 00:26:04 +0000 Subject: [PATCH v3 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 Fri Apr 26 00:26:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13644044 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 6B24F2904 for ; Fri, 26 Apr 2024 00:26:19 +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=1714091181; cv=none; b=sRvCyAbqvItw+j09eH1zR8PL3ZJiOpb/Nb79FAwWwzGUBG668vVmkmJwq+vpFA4ZJgHK1ILmQnS1LppyybOt9SD/ctAYXnJ+6FkXRY9mRlU26zCL6yZROB5MY5dEJpywBLIemYng6MGlGREypwFIKAI7DS9K3XrWoMPN21Sk9WE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091181; c=relaxed/simple; bh=es/8xvwikbdf4BTLJ8Qn7X1Yb95TjQhL82KXUPLxrrs=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=cqha0nxXwvuDRaGTyMaM7A4VtcW9ZpsGMw/3si665/8leocgYQIzFbRunudqsHS7tsx56yfMsBX0V+eSX6td+Fc10yTKq7yk5MIXBvzLlGMTthylOccQC/oqs2N5BWI6JlGwOxBLhNORfum4jbc5dGJ75ycQOQJdoOBLH7M7fZQ= 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=OLvtddnL; 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="OLvtddnL" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-41b2119da94so13385685e9.0 for ; Thu, 25 Apr 2024 17:26:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714091177; x=1714695977; 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=OLvtddnLnR0/8CnpexI4wAcGIK0WFJ8VEUhSSm8JWdrfwxhZw8/pJ8GLKYzx4ngk0G ccz6hztGYBpyIFWeggBBvH4n35J7JYk8bs7cGR+RqxyBWU4G2jaPkmZ+C97P9qX1oAFS ngQD5LLJlq6DqG2jPp+uWrD/2KXDaGZS+3XnAHsmLtzZIYqJMBQQ0K2EXHWZcqrllbSl ACKC75P0mIVA5d6BUIAwF4kQTikmOQsGjj6YZCRZk6Kru+Rdeyn/b2mj9J/1adOi0xkx sAhDmU5BWDZe6qJyxwtESNN63neo8recPhF9qOcMfOhLQ6PSvUlmpZXdudZI7x/0p8Kg jwLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714091177; x=1714695977; 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=dlXAjc/NotxHBEt306D0jq+sZ0RAUPZxJ3joVhMmDRusInpRSZ7Bt6vFzMXcP3Dpjq d656EWRI5OhCunngCzL/FnQYxUzR9dAKGtSWxSxJKusLirqLAvtWh+1DGaNOLnrjBW+i sCdHxUJ1DId9hNI+VIjZqHxOvfewAvNqygXo8SsdXNXF+B9UBK/TpE4u7QiF15Fh5nTT bD4AejQG7bJaLOo2RvmFJanAMudY2IzlQBIGqGkMwX3VkdLe9vWXI+T3mVdOPrt/BeWm S+tccvieZzvc/Vi0EaqnF5GIMgOD+PDxDoyI7fg05oSTDRpxuy4HMW6aCk/6Miv9Dk2T 7ZHw== X-Gm-Message-State: AOJu0YwTbeQB9jqLSkK4OmsI4SuF4yQvZMEZfLp2gudTrXqHPwGlhxrq IoA+RsL6ghxPyUa9KIa6AI2sgWlxqICvX5DjyRBTcGF4L+E903bYGOKoaw== X-Google-Smtp-Source: AGHT+IE4mpDizJdD1EVGPwQW8lN2yr4H0S+HYLyNUN/aax536oCHMxV45ysmbdyCV9Kk+d3SfOE5XQ== X-Received: by 2002:adf:f10b:0:b0:33e:7c9a:9368 with SMTP id r11-20020adff10b000000b0033e7c9a9368mr593961wro.63.1714091177095; Thu, 25 Apr 2024 17:26:17 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p5-20020a5d48c5000000b0034afaa9ca47sm12718356wrs.1.2024.04.25.17.26.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:26:15 -0700 (PDT) Message-Id: <460979ba9643c5b7eb0fb93a51ba4817396bcbf7.1714091170.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Apr 2024 00:26:05 +0000 Subject: [PATCH v3 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 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 26 00:26:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13644045 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 66D674428 for ; Fri, 26 Apr 2024 00:26:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091182; cv=none; b=W3HdnBW47l2zwOFiYZ0uu4XSSwIh1sGtpban4LjK1TXrtUwrODY+m2G6oB4f0g03si5NElM6gex5tQKBxDaslRZ8x4sSqTH0S1mGMjHHniZ3ef2p66fx+JVzAsMmAvm+RdaUUWOVvVc9JQiJCuo5zE7bt7Prdr1EnQVxac6f15g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091182; c=relaxed/simple; bh=4uNz/BNgPU9bXhIVD83KXZWLdRC44l+OQ1r4zFrYzCs=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=h8c+sITxbnNQy3mk/C7T6p8fyUZ1Fyz6yaZT77gX+EVz9sx+B0KtNqp9OxFrUvdZnVc2BObZLM5KC40wDXNZVs3Itmmj9dxf2oKMMnMnKEY5F3egQyXciq5ZqfQbxjYHsHQUNozTta6JjsTvxHmseQG8sMcRGqF03Kdnuqg7dOs= 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=nDXVYptL; arc=none smtp.client-ip=209.85.128.45 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="nDXVYptL" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-41b3692b508so11845725e9.1 for ; Thu, 25 Apr 2024 17:26:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714091178; x=1714695978; 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=nDXVYptLGyvotpeoBQ5O43XGT97+dCivDCJUz5ghz6bEysW3bSbLfun8ZExoos+6lL j3s0vAne7luhEQqOmWo7MvUqNGfMY+6qeYHvX4ZXYc/I3jRMP9qZM+042rptwYiAqVeC XQuiLgY10Ad0xveE7ILDXMa9Axhqt2bA4j/Pj4gXxhSEi5G5Viqn+DXpRnmGba4hMTkR Z9cJIDfvIkT0nxdaza2Ct2+k7I7JYy0eNdLpWSzBlClL5q+wn5ssVVUC6dFse4NxPc4E 7/2nLWzN7nxSsN33Izn/hFHR8zbrvmFp+d79ncIMGnGx3ZZ6NQQHDm6eBzIdFuJRzv1C Rj5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714091178; x=1714695978; 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=qvmVs1+fyBMQwwnwWJCCYtvNN7uVi5GdxrnnyObSWBUbwWPPQtuuXJY+W/eie0K0Z+ MYMcc+0e9oylJ0o6z1zDJtYa6rBScfjHl12LBt259c3ygBd+VHR8dyiy+gkNmTb5w7Cd frkvFzD+uC+Fb5jE2Tu9dV9AwS3o1JOBV6bBbJyGZrSlefFYyysKoDrUSR6o5kbYDjJl 11T4dxPVDrg//jgXgu5uElNSW5K/wXhuaxE9AQztkA+Ew1B4gdFJAy0bCrzcSud4XVPP 1HyQMRoow1tDJaC/biD4VT83IQy+4OdGLC1n61Zz64sARn1W0FzwCIi3reY52U0TAQhE 9J2Q== X-Gm-Message-State: AOJu0Yy7vl6/FfxU/znLmeEVDchFZsaVNo0TWBI1ryc0j3womqcxZVK/ dBCfx/gQhUEFmeEjps7O4JKFLbCjtETvmq1RZwtkpprLAgo4yHEg8PR+Lg== X-Google-Smtp-Source: AGHT+IGqJeA2A5HpMi4LbF5bh0Vcs5qB/EDI6pIF4eiKYavfGywlnwVHbbRWFUnzDwYpCTIN/ThAQg== X-Received: by 2002:a05:600c:a54:b0:41a:aad8:e393 with SMTP id c20-20020a05600c0a5400b0041aaad8e393mr708505wmq.30.1714091178442; Thu, 25 Apr 2024 17:26:18 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id hg16-20020a05600c539000b0041aa8ad46d6sm11558306wmb.16.2024.04.25.17.26.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:26:17 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Fri, 26 Apr 2024 00:26:06 +0000 Subject: [PATCH v3 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 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 26 00:26:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13644046 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.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 6ECA04A2F for ; Fri, 26 Apr 2024 00:26:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091183; cv=none; b=EfIszyPpshd/18YGHWKBnk6vML6OND1fMsLu13EWqTEoAnsCq4FDVRW9dwC6AcHnlIVArBovdk4f2JmXHh6km19esyE8FghlTM7UL2pkPkdMSwn8U812nPJKQaD4rV4G7bKQObhPgZmNecpvvNa72hGQaZguOK8hlrw3T4FYSOA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091183; c=relaxed/simple; bh=cKifk+a+kcLT5ki9XCXaz3NFjqM+4wskDzsWFcb8FwQ=; h=Message-Id:In-Reply-To:References:From:Date:Subject:MIME-Version: Content-Type:To:Cc; b=De6zH0xkpClDD689t5yOmhN4MPO4USRwePmEqtwBTLU2xKb0hUkx3ySKqw3QZH7JR/LNRRV6nYzqeRvytzXzLG7Q8gPYjywxXe/oZhnmldZHeAemtWso6DbOpqaBj9E2J22DmmKOb0IwQp5KQfG02rnKh3uXHvZ1CCok9QRCdyU= 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=VEqwxUZO; arc=none smtp.client-ip=209.85.221.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="VEqwxUZO" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-34a32ba1962so1361059f8f.2 for ; Thu, 25 Apr 2024 17:26:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714091179; x=1714695979; 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=ABhG4+2mPlET/KZ1W+ROLPBP4uDqCNh673WDJascm8M=; b=VEqwxUZOJToTAVQtWNx4GmbL0Nz8XlrCsWptWqs6vpaiw3DA9HvYxFPLIyPmniW1dM 0444pyWoTyo7PDeW9gb9c62Ued+IvWpHb0JOWo5kmEYzfiHryBKXvUiUsA3SNnqqt1xK LKg6A50GvR+o6DV3vovVgpoDcJYCySBwOtiAABj2cwuHyRlb7OyqHXJNmaTvMP81KW+h U4vtuswmZlz/53g64KqRVAd3bdwOZ0+ZrWUfD77e/Bj+QeZa5dsRL5RxLvYsDh6UZlgZ L8uqje0xPX+9nGGRzllBrhf7HmJdJvrl4QChNInjW2Bjf12jYkhbKOhlNZZDYT4kuQ4U g5Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714091179; x=1714695979; 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=ABhG4+2mPlET/KZ1W+ROLPBP4uDqCNh673WDJascm8M=; b=L1z8LjEd9vKXDsYT2wVlMztGc4UY5Vz+s2WgINJvzFQPwj3m/jDp1qiqCQ4dODS/Tq lwZqlnkzNxnDL4OH/0aPo15aOi1ThpCuV9Ocr8DxGFiORNVfRXltE1uRajA7OZAF87xu tNXhQ9cdniEEinBv4t6QEy5THibafkHiIkEe0k2M1FhmDgB5vYyzOxox0JFry0KkfvPy zXo/jgJobPlckoGbY1EilPNXPizWWJrMaUepUxgrKjlVKq5+IztcQQR/RxxN6S2889B2 Y5GdYrHl/scVoUgDxUWp3Mvbrh+aHFSdCiuTG31gt3qsOO7neb15goNYHij+9A59GWFK wGMg== X-Gm-Message-State: AOJu0YwOqgOA2+2JqE5ow7TunTqt4dmyWZyy5lRGr2emsLd4mOvQuVYg a1DgW7zzNsdAbM70Hpdc92TqsM7I7dBjlpvBzO8wVEn4wqxYELOtk0xPvw== X-Google-Smtp-Source: AGHT+IG27CZXhTqwD/5mWvJZChttRktgMn5Ea9+bjsP46yjIK88InenO3jyJX9dptP2jvBrHk11p3g== X-Received: by 2002:a5d:5987:0:b0:33e:726d:e963 with SMTP id n7-20020a5d5987000000b0033e726de963mr798257wri.17.1714091179480; Thu, 25 Apr 2024 17:26:19 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n4-20020a5d4844000000b00349f098f4a6sm20892447wrs.53.2024.04.25.17.26.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:26:18 -0700 (PDT) Message-Id: <49c66c48cc1793744e7565f7ac1ef887c76da4a0.1714091170.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Apr 2024 00:26:07 +0000 Subject: [PATCH v3 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 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 26 00:26:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13644047 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 9C6BC6AAD for ; Fri, 26 Apr 2024 00:26:22 +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=1714091184; cv=none; b=lkp4WIwCMtmnqhiq/MXAsUR0U4lvAA2vCPuOVauhacHNjXXX6YPE4DEPB3UnXMGqiRmT5VCrSvQsM/jpbAK0IKLuTMnxOHZG14Qg0QdJpaUI0fDPnL+tOBC2g0rkge7e4qVYJE7Cy5s/VzS9nBCsNYqyDVt/iRqsZKC7GjQlW6w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091184; c=relaxed/simple; bh=yX6rb+UiEB1astWPY1iRzrTuD7QPl8KVlv1oC88H+4s=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=bHCYrXJog+cIN+cBHKks09Q24H+aHa74KOqOPYX4ozrx/KY03lii0QrItjV7x73xMe+vgz+XNjeRrzj0kiQX/QdigESdAnfkzcIIzC9BFgtr4qoucsQ/IcprEgZss53xYj+75s1A+zVT+AYumP9oXp5kn3m4E7G0o6LUm80Jeog= 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=UIDwx7ak; 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="UIDwx7ak" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-41b39d38cf6so12350705e9.1 for ; Thu, 25 Apr 2024 17:26:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714091180; x=1714695980; 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=UIDwx7akgoxxERQHYhsryr/GFXjlvj/b7wZ/wdQ58gXnuPHLmE4d7wCmvqInOOAaop l92w+yIP5hOPlRJocJt3KO84kQMHC2FhmwkciWa0Kf/nIazDjOQB4g4WNp51S2fW5N6N GFAdmtNYkT0Piw2zwNkQS6WHl7wMUCPtMwC269IaKpVSCycR07dFgPgVM2onEJsRTjO6 iSUCFr2wp2GW1fknWcesZvPSTm/3z7JaqkGBhpj8RnyodhvRJxRUr1jEN/vq4IcM6oqN UKaA6eX79ERNIayU2zEec+iCh90B9vpllEPwyFNWWvoY8RTa/kDcjrMgln3HwuSRAn7t m8LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714091180; x=1714695980; 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=bl6425nFWxMaeL1VGHHO5Uli/nh5UW8Dm/3c5/sr/FvV4ri80DXMKOPcrMbBB28kfh zfNhrAMFHqWBANX8xwZzokmYgY8NG2C+xfQ03TkwVZ/w2YbsWlkP8iEVHYoovsho9pvb VblKwB6X/Qoot6YB+/gtYt+Oxh0zeIl3K4SoJrApMzD7K4oxddm8ddUssckmSre2tIxf /+oxjCM2LcabSIE1ABV26jQGBQN3VdZ3O40ZDh/9u3VDeGOsTxocSqqIFo5bXUsUoFTh O54FSVmiNoTwRomdv+jeMgwMuVRIAc3TnDza2YysTd+S1TkOP9y0JhN2y6vHXszh6iS0 drFA== X-Gm-Message-State: AOJu0YyuoXpl4mf0/yXDg5r81qMCeKAlt9zsYLJsLVT7EoPg4oJkkYZA ccVQfmWV2GycSr5Ea0C4vi5dwf8lL8tKzeKl1meEUa+uVxgvMPSe+wLoRg== X-Google-Smtp-Source: AGHT+IFqElc0+T7H2pQE7uY090rMjOtHAdL1OMLgLw+wmRggiXV/su98VoLfr9hHTO0XadoEvU7BeQ== X-Received: by 2002:a05:600c:4448:b0:418:ee30:3f92 with SMTP id v8-20020a05600c444800b00418ee303f92mr771242wmn.25.1714091180477; Thu, 25 Apr 2024 17:26:20 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d4-20020a5d6dc4000000b00341ce80ea66sm20914431wrz.82.2024.04.25.17.26.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:26:19 -0700 (PDT) Message-Id: <56e1cca4b7b63725c3db60157b863ea36453d02a.1714091170.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Apr 2024 00:26:08 +0000 Subject: [PATCH v3 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 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 *); From patchwork Fri Apr 26 00:26:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13644049 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 DB0CAA94B for ; Fri, 26 Apr 2024 00:26:24 +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=1714091186; cv=none; b=AJKroBpzD7hKz62kzVLJwVBEFA9INwx/MXFVZZY3qerX7+rc0O2x4PeB7XvjVjS7qQhwJ5XpqIhOkodxkePn2fyNy0Z2C8CcH/0i6RhTW0iAt3xUugi8/sqtxCUFWPyNw8JeBvK+nTnJoI61UzGyi94V7iPr1lKmZ/dtGxxWnmw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091186; c=relaxed/simple; bh=M2IGualKqbmz1Ic5SwZXBIEQDju0L9R0gDiDDtzgrBk=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=Sksj5V8iAEUY2hIG3P2LqN97GTWykhIeknYniC5zf5cRBn6iS7E2uBFTWkWpPORe9CdqzRBe6eV3i18YUK9eSe+7M5Wvrj0lDZzJi0oaYUNAtbhCESjOeuXEx3o61cQOb3om11huG2UGtvOU7+vX8hPH2GyZe6PSulzrP8IiwqY= 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=AUMMzqa6; 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="AUMMzqa6" Received: by mail-lj1-f172.google.com with SMTP id 38308e7fff4ca-2d895e2c6efso22717051fa.0 for ; Thu, 25 Apr 2024 17:26:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714091182; x=1714695982; 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=Syadgd5b0jP+Rx/VwdK+bsvYAR8drTA/fM/+OqxR0bU=; b=AUMMzqa6ktuemciVIvfzhEj2EQskTQMMnvfQzsX/4M8hN9LdbrjcDXp9Wa6pDUb0BX XCAEACDAKmbGsyv+cDWjmU1kTMhTxRxtK5qBThrJDjw2TtNphjNBhsVaS+ZqI5GZg21l z9Fwmd1pznQlj88Ri5iTC4LGcUpSBrn4hG/RlLpi3wg3CwuV6PaP2NSdQ0bGvGN2RIAx WA6e5E+Z/EyQgrS0bxF0B0lQ5nRifbSSkAlaV4y1SOJXKNquVPCyvBJQZ/lSypH5pVWN lk2TkOa5z3uazPUdPlEOf+4ZBWBdX7PekFXnC0FIArQqQGE5rzDXxmxYwb1v1yv0qz0o 9mZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714091182; x=1714695982; 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=Syadgd5b0jP+Rx/VwdK+bsvYAR8drTA/fM/+OqxR0bU=; b=g7iZWsnkO+QC4UvFLJdHNLWcrRvNDfu2pN2R+G6ZUbKLQk1/jNoNj4kM4iVsqwbrkk gvpec1xh8miBuNypgmcHVTc3uTGczdlkDkT8pF4IKOdvpnsLHUvoQkA6VpXpNZxG6ENS Sgt/7VMYMalDJllMCXyLac7cGBQnJodK8Yfyxt98nGhQtwZqiA3fcrtwQXZwF5uyf4XC Yze6XA06Z1kxKZYdYsX59GuiJtSY+ClBz7s4j/xDqLDKodGJWHMuMoqDfB5mN/AyJ8Ep zRz6+paGL6BasnXTuN/kewvYP0xp/Vp9MBFoKb15cb8eSds00MVr97t3uHCGxhl9Ezuk YWvg== X-Gm-Message-State: AOJu0YzaP1CMzsWve3jrIxkF4zoIwa39GvW9K67BF3YP0SgZDrOIYTVq HTiuabDkvVfg3TSqNUCe8Eib+oOn1IoBKrC/zlaow5MyXGkRz6q1corafQ== X-Google-Smtp-Source: AGHT+IFE6lftTONE+0DfqUnGbQU6kie4Gp7WTw20sg7EEtd0ZCubz4H0Mfe2iHNP2aXX438AR81sww== X-Received: by 2002:a2e:2407:0:b0:2de:4b8d:ee31 with SMTP id k7-20020a2e2407000000b002de4b8dee31mr467008ljk.37.1714091181932; Thu, 25 Apr 2024 17:26:21 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id ay28-20020a05600c1e1c00b0041b4f563a17sm3581586wmb.39.2024.04.25.17.26.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:26:20 -0700 (PDT) Message-Id: <35304837e08aa1ecf6bebb47aa31813a80f2a2f4.1714091170.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Apr 2024 00:26:09 +0000 Subject: [PATCH v3 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 1b7422fa2b0..647d48aa2de 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 Fri Apr 26 00:26:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13644048 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (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 E2FD6A94C for ; Fri, 26 Apr 2024 00:26:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091186; cv=none; b=RxZi41HXM0YxU9xWA9DpaRt1gNRbGILHKeqsz6Qpcr0gbDTGuapqJAD05W1+pY2R1iHjLieaUjZNUbIPTbLqvN9N8nnk0laO3/RStAeoQZSvWIyxMiol40Q7CJHglT9ZXYVktGjEmtVORwBofGV1NiJ0ejBO3g8sQLp9IWCh+A4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714091186; c=relaxed/simple; bh=XEwo4h2UPF0xHe+PphdJ8pueiFCV9X2Wlxv5hau+1Po=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=AGiNu2Xg2VXBJb/2Q9cIxEJ4HlG4DleJGV4CCwcNQAOgZlCkRCJOu30d+D89xN1YwelCn5NVAnYgRrPdfUkOc2pwt6Uv7U6KbZvDIxO9vBEoye6+noGvH8GKVp/3q0z5N9AR/EI/x/U6Vub6nBeS7REiESrmxDdn9zuaDYcLEvc= 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=JAGG4dqd; arc=none smtp.client-ip=209.85.128.41 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="JAGG4dqd" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-41a0979b9aeso10746745e9.3 for ; Thu, 25 Apr 2024 17:26:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714091182; x=1714695982; 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=KG2dQWLoDXnXfNteqRoRZCSu+qEJ8/rYfxAqyh9y6pc=; b=JAGG4dqd1xzfZM8fer89E7E/HlNgB6PpOMzFODW8Ii6aY1yNmmfzrxnZdgxsdkejDO hAiyRV8ArULXLPh142ewzlogVlctyLBxpTklcDPuWE/6vlejjfmsrloO3mAD2QpIV3B8 njpkcOeP93jlge+3LYXMCpcDIM+90GsGUU0PJfYHAqtl3U+xHEy/22VgavW8liE66PiM qApkj54644K5DZj3F+MbnPsojlj7VqdJ8ruYXl40yCtTUJeZz4MEWLvwZ2INbvP/5LCm 0AfACl1mKfG33UQCWUSxdw6FI5fVpJUJOrkUHh5ItLr4ylm7WT05M8PD4xvjEPC0DrK2 UekQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714091182; x=1714695982; 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=KG2dQWLoDXnXfNteqRoRZCSu+qEJ8/rYfxAqyh9y6pc=; b=WctkDw7BfFY/glcrfuCGgaXsYjrvKgiZwqLdcXn0qetWeFWTGsAR9pLFdXgDvdh5LH JA9foO9EUr0k7gkdsGsLAspVjGiNn9VWW6H9/J+cdy+SmNaIyLYmb9mAD9GzWCtkp+FY 1THoz9VDK5SPe4o2us5Fne+U+zGK/6my8fEIPGpt0RhFS1Emf68QI44d5rwTjchva88O Bu2UbSOtQv4epmBkcXuuR1gtfdQR73J3rm65rYqoR1vQA5EGUmNGy3UiyOY+voEtZSYc b7rN36Svvb8ccOT6pyZOUo50m8PFBkeRcmFActUqKfBskEOlMfe1pWHjsnW8xc0cyzm1 asPQ== X-Gm-Message-State: AOJu0Yy2/SMRxdWyVB5QeGMJv6szZTHIP/8A7P9XMBCAcNgOyN/3/BYO jn1FOwTiUnr5xi+6HtUsquZhcJd476zu2F5YHRzL5OZ0NJVxwqtMJXR/Rw== X-Google-Smtp-Source: AGHT+IF3czRREvayrS9COVbX9Iz1cfCDEX0qEHTUPtdDwgdSvbFuJT4zUKy9HVgphz8cAprwwec8vw== X-Received: by 2002:a05:600c:5252:b0:41b:13d5:7da9 with SMTP id fc18-20020a05600c525200b0041b13d57da9mr743043wmb.38.1714091182585; Thu, 25 Apr 2024 17:26:22 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d4-20020a5d6dc4000000b00341ce80ea66sm20914498wrz.82.2024.04.25.17.26.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 17:26:22 -0700 (PDT) Message-Id: <4d53707f83612111a150c326bf60ec04db96f852.1714091170.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Apr 2024 00:26:10 +0000 Subject: [PATCH v3 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 262e2838273..2abba913d00 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_objects) +struct trailer_assertions { + const char *raw; + const char *key; + const char *val; +}; + +static void t_trailer_iterator(const char *msg, size_t num_expected_objects, + struct trailer_assertions *trailer_assertions) { 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_objects) { + check_str(iter.raw, trailer_assertions[i].raw); + check_str(iter.key.buf, trailer_assertions[i].key); + check_str(iter.val.buf, trailer_assertions[i].val); + } i++; + } trailer_iterator_release(&iter); check_uint(i, ==, num_expected_objects); @@ -16,22 +29,26 @@ static void t_trailer_iterator(const char *msg, size_t num_expected_objects) static void run_t_trailer_iterator(void) { + static struct test_cases { const char *name; const char *msg; size_t num_expected_objects; + struct trailer_assertions trailer_assertions[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_objects), + tc[i].num_expected_objects, + tc[i].trailer_assertions), "%s", tc[i].name); } }