From patchwork Sat Mar 16 06:27: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: 13594373 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 A161C8F49 for ; Sat, 16 Mar 2024 06:27:12 +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=1710570434; cv=none; b=uEITZP4Hudh8SCqR81jI2iDkFBOmCViuh1B0wVsD7M/ymMYK+d83nJmOrKEEAv5IJ+0SYRcuCwSlguqnt2Sh/5aWe4Yg9T8onZbULBFuB4xeZiSmCpECSHgQ9uVc7T42yxIURcyImm91xtb45yPyAxqTqi4nfIBtLoGWd8kRdd4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710570434; c=relaxed/simple; bh=80X67DI7aF5hrHdqSseuDB3it+OyxHl9Gy50onQDU8Q=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=S8llW3iNUk/0WHCPWhJ6E69eJZkuNLttmLSHV0Qyk0FNkRHeV/Rb9MK42zSONduwSR2o9dpUt+57PsVF9CMyVqbK1aFI1Bj6kmWPO19wtkvwD14UVOm6pmULXj0Bg5z2D407S5Znwj+9KG2V+xhu0NCmvV8KCKocbqO5sstHAWA= 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=HUK9r4QR; 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="HUK9r4QR" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-413ef770363so23862015e9.2 for ; Fri, 15 Mar 2024 23:27:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710570430; x=1711175230; 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=hKLrpgwqPaFMYDoMpIo/JUVy7Y0gCAs/fTuse1xiu6U=; b=HUK9r4QRaCMKv61gCq1F0km8s7mthPea4/sDP+uicnv6XtoBzGVua9Fr4md9IgRioM QgaBsaX50k3sUVTN0Vnm4On3VjDtDGotXlY0Pr2eYz6cvk5ay/bwJWq9JTX1+kLQhC8H 9U1JTHyBRZz3/guc3Ww1Gcw+1gxpleaoZ3zNOW63/RlV03KU0c7jyr/GijSXBEitbrL7 gK3SZhfKvK/C0AFMbCDnsJNS7gz43mN/1KKOfb7WoS/9L3a+lIOZeKqA4foh7bHJCxKs LhicP7D3DDaULLzP2FcUQtlFHEqcxwDQY1DwWoFzRNkIuHPYP8IPPu/qeXRRKiyP1hju Oqyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710570430; x=1711175230; 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=hKLrpgwqPaFMYDoMpIo/JUVy7Y0gCAs/fTuse1xiu6U=; b=R2xGs5btfzNJsWuTlaQt2JTNKcSkqD5bLY68n5HB790so2Aqdj3blgXte3eI0qy9QK EBv0KasJemvQns9oDxq/XW3GN2MoKQcYGLEKl/KtBDMf8sPkPkD9b/rZfQgsMmQU5wDh N6VoQDjuZUmSPn6FmjJt3oC9ZK+ZHOrXq/KPZMzkgvL8EMkU5ETBkwEj1wLb1W4oL2wx bKJRXCEsNBMcXVbUtwbVapcoDn3105pfceAGKzb8orVy2Tbm/cxVbysSn5VJ7iNTXs78 9KLLztSrZe27ZSD3mH1Z23HAIQQnKwOkMrs/Z9CL5fU3BM+cvU2a2MRN1AM38ZlolrKp H6Yw== X-Gm-Message-State: AOJu0Yxbdba2fop78DabS6XXqnVbDTT/zgYjBk57kMIT9bC2smn+oc4m o3pz3+5/DCiMDIBomQzN4VHuAPEz0eex6dUCa3jHXlEAGV9y+jKp5OLfXNWu X-Google-Smtp-Source: AGHT+IHucGmsuXDuykohiAuKNmryihn5FMW2dvaHaUHXlRSnceGdR4oHwadHbnrGMPIGWw8euYlErg== X-Received: by 2002:a05:600c:3556:b0:412:beee:36b3 with SMTP id i22-20020a05600c355600b00412beee36b3mr1102601wmq.7.1710570429898; Fri, 15 Mar 2024 23:27:09 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id h2-20020a05600c350200b00413177c3f1dsm7741904wmq.18.2024.03.15.23.27.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 23:27:09 -0700 (PDT) Message-ID: <32ad0397737375b826d8af893e14a04628e9e803.1710570428.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sat, 16 Mar 2024 06:27:03 +0000 Subject: [PATCH 1/6] 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 --- trailer.c | 12 +++++------- trailer.h | 8 ++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) 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 Sat Mar 16 06:27: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: 13594374 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 20177CA62 for ; Sat, 16 Mar 2024 06:27:13 +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=1710570435; cv=none; b=P0U8W1EvHkr7mzi2MU366uOsURpPp38S/Px0mTCESeSJlKf3ABASiTuHCIOpldKBDC+2IXMdDhdEMg0degvJD5Bt50C1yA7BYKTUbQWMhDZcCg5wNjzJ5eQ7WtjiChJN6dRF9KLsU9d1o1WKS1Xx83TSkPesUXFGvSi8WxwBAIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710570435; c=relaxed/simple; bh=9kAoMDOFAxLoPOZrzD7YL4t/gUJSopvjOz6CeQeZXkI=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=qplAnmS80Uhlr8+OJ3u9ixxiBl7yuhxQ53oo01CcvpPq0xQkhDB8AOjPiP0h3Kt381Tg5cfzg+OQlMdK93ZiWMWePOducZ4gfLrRPrtUNl9msO9JwyKVx+8U4pFsjDOy9IHJEu0dzM3CNKzkU7tQ4HWDa4me+qQibM9+K006qyI= 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=RzX38lqK; 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="RzX38lqK" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-41400332550so13924335e9.1 for ; Fri, 15 Mar 2024 23:27:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710570432; x=1711175232; 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=RzX38lqKJKjlTVxWwYdYFckkiFACuI0EDG082B7JbvSt78W0fWHKHcdoNp3qIHOrYK 2Iemn2PTyLVP9cAHYTOEPWKWXRJD3eD2bEzb5CQ92V+wY1d2WWZ0pmLC+g8vwLHb0eev jjUcOsjQVx/f22IRgQvW3OH8tFsfKbOEzGhm+y8jRQkQ5vDJrAYyvDiME1+PNzdWYZhq HJhvi9Dvx/saFK5eW5dswwb5mCjxDlE8Fog+YaM4f13DF14oQNEgAu2gGhoYyU4ktdt8 NKfNqsIn57Zoms6tUnvXimgPEHKyi5n1aEjYKdehotEiSsURfH6+2Hcv1FpEZzl/sTP4 11Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710570432; x=1711175232; 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=jXN/Fcb/ch6iwi/hz+gaM9vhWszjKKQdW2CepVlZPaw7cBGmNCs/W1nzgeZWnF/yAR NNg+/zVQnf+SEENav2EPYJuviMFWis4Z9AKuMt5XubNG28+bhdeM7kqINTjpeTLXPjZE KKffhZSGmtQl9gUhgCEt86NlXxMlusARxVI1B0tROc0PpbggG+pbx7BH2b88U33RXUvb Sk7MrgOWKXn/vLlp0lerckFzqq9QKAzwLZP8JHSUPXSSigAjH+sPSKtyDjG786pyXQ4h maSy4lJxrFPbHSgkMnX/CdllsamTzUIcNgjRNKMYYoAYz3cL6/UCsfHcDr9yb13/eNrS cVAg== X-Gm-Message-State: AOJu0Ywj93H+WvzruFbVPZPyYkkkLObWRJ0/Sa8k4rCt/N1UrFR9YlnI ckKr2WP5RTLVIz/YUUWp5fMNyY+tWHo7/2MiHub8BUAxNXTcSOS499JhsK/Z X-Google-Smtp-Source: AGHT+IE7s0vBQVoDpZuYpOEXOWelVX4dPXBdAY9Pgs4Z/xiBRG6v7VklU50+mepePPsuR7w/+9FMIA== X-Received: by 2002:a5d:6683:0:b0:33e:c56f:b5e7 with SMTP id l3-20020a5d6683000000b0033ec56fb5e7mr751835wru.31.1710570431630; Fri, 15 Mar 2024 23:27:11 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id bh21-20020a05600c3d1500b004140924b256sm163451wmb.48.2024.03.15.23.27.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 23:27:10 -0700 (PDT) Message-ID: In-Reply-To: References: Date: Sat, 16 Mar 2024 06:27:04 +0000 Subject: [PATCH 2/6] 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 Sat Mar 16 06:27: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: 13594375 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 71614E576 for ; Sat, 16 Mar 2024 06:27:15 +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=1710570437; cv=none; b=ZUwiQHFvHdX3Og5oluQXNIc7aKDngCbP2sYfTY+YrTr5G+48A5MexuklXBt2JcJd0SqIHVkRbSPrxddjhjb3NywY6Gp90vRT3DkNNUwXEvN4BSUBv/yggcJT/pGkdv+NK1yrL6r5Gs/V4wRzfbOrZCNIMRiMTXYZNAaPUT73Uss= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710570437; c=relaxed/simple; bh=es/8xvwikbdf4BTLJ8Qn7X1Yb95TjQhL82KXUPLxrrs=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=iHxVn4Vi5WK6njlTwKpD5uTthrJxww3VSCPR0dJxswFSNjBbUkilcXlCFqOl9Aoyuu4b9k6h+qdky4ZGLQBUYqT27XrQt0ExnENKiNdH4HW+sNaAOkMz12q7toO9LZMpusEw/wevhyNxIWeuyC55t1QcESEIPxISAl3HeZoUDGk= 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=hu0CW+tn; 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="hu0CW+tn" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-414006a0115so13618205e9.3 for ; Fri, 15 Mar 2024 23:27:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710570433; x=1711175233; 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=hu0CW+tnR0GwzlaFHQPwA2sWtUf9Id1E7r3nMhh6eKDbYTAt0pHNQ0bFpfwS6/GSOS DFJXBTtxDL81nndPDPoH/p3P8tAh1SRo2HaiKqOP659R2ENfUvggI0naX/LuYHb6IPWl CjBoLI5ROdUqp3uqvu4qYtvoV9Z5odq8JSxc57Xp3Ptht/KCkFNkPzjGVaIYzJl8jAJe DZWTDc9u4z90rlwlg9+jlZ6Fwv4kplSA45bk0EniqGYaDpe7c9t5Ko8iTyOlFV4LKjvy rbo/f70wWewZdfJhuPBKMWVBcKEhkT4Us5by1ho6xJ3M2SoIXZ/+ABHO8z2flVfCaxXW cLJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710570433; x=1711175233; 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=vLJG9hPtxeLsW96CHzwubKc2yLfXAFtSzcquPlrocfNQnbSsWYYiMpF7Jm3Ay5wkNO mxWUUYD5joIfGbvHYtpTqN2JhsBE9FqLFEu0yWyGnFVaITgIRzFgZi4mU7/S9SzNCXQ2 tUj09bu3BIfmTVrnyAFmgnB2rrgYTmbApEce3KBhflEO55om2D5U1ylTE9RoqabFQIqh 6day6NEHRdttUww6LsKarD8EDZFouvrkguTZ5mnPNiEsdgb/gupHl6q6xnlnE27MIv4c peQGE4UFXmOi06Q7bzQD3JO0Xt6Kqdo0xrhdXYdSAgFxjKaA8lJjNUwG/g12HG9iwd7F 7EUg== X-Gm-Message-State: AOJu0YyVH6167qvhuXmIzkpgn8KPrdtBeUWGoX2ld6hR7xDZF+gwHYvg ANA9/UlWT4ZN1M3JibKDpat3pievGS3iyNLZCa3Kj1eb3VQZQ5Fgzy3GaIx1 X-Google-Smtp-Source: AGHT+IFyOYnFhPnRV4Tjb07+G+LH39lqa2WtAd3i2yQ2crb2SOUJryq6w5LUBU6t16x6hdST70YBsA== X-Received: by 2002:a05:600c:364a:b0:413:3110:2d06 with SMTP id y10-20020a05600c364a00b0041331102d06mr852361wmq.16.1710570433118; Fri, 15 Mar 2024 23:27:13 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m36-20020a05600c3b2400b00412b6fbb9b5sm11158521wms.8.2024.03.15.23.27.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 23:27:12 -0700 (PDT) Message-ID: <872e67286c8407da28ae704b9900ab63b7c407c1.1710570428.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sat, 16 Mar 2024 06:27:05 +0000 Subject: [PATCH 3/6] 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 Sat Mar 16 06:27: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: 13594376 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.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 E293CF510 for ; Sat, 16 Mar 2024 06:27:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710570438; cv=none; b=R38NDDTPlqQiS04mhIVkzTvRyBeJPeFZRIvqTm3/xA1O/AldQx3XA35qaa9XvsPOH3vooquj8khRn4EI1+e4NTiVaS7h2dnHoCPvra43ccBLMTZmM0B9njDa7+GkK3ilAGH9vdtYKscUTAX1F6Pz7GfzcokI4H3sK82Yu9G9uCk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710570438; c=relaxed/simple; bh=4uNz/BNgPU9bXhIVD83KXZWLdRC44l+OQ1r4zFrYzCs=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=pxwlt7l9+n9VZqFarmJFSH2vjT4BR8GgSRvF9nA4AgO854WrtVtEzUZu3RApLSebB2VOqNxhzOwv6AtRnZ8v/u43f56qeqSV2UCUisppwlS5t4RDvLTfviRsy6zh6yy7xUGSg8tKHvIbWJMerJud7PEmE5febMjdSMU1u4FuDSU= 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=hAd/YkCK; arc=none smtp.client-ip=209.85.221.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="hAd/YkCK" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-33fd8a2a3e6so33490f8f.0 for ; Fri, 15 Mar 2024 23:27:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710570434; x=1711175234; 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=hAd/YkCKUsCoXCgBiETQg6Qx+VUE+SJyGsEx0tVVBBrNVQVHOuyIczqywBfJPFMiPQ xrbhBe39SPRK6pPKBj2pl2ErIihYwvxnogx2CCEtur/Jy6vcpAwpenK1gOQcTcQjW/u9 NRha8M2cC9hihC0Y7T5AI/P+7DksWCTN5QozINaYcb2GJLuRivHxJ0o+0TPLCRJeI3M4 Yw/o8bgMKAXRrujSkC2zPpOP99kQNAJ2mRvneJqU7cYivG8P3LskL8Y5qnfyR/2GfEhU TGu0c9ity76AjlvQxWj/+f4cvimkHhaWDSXZG6G3C341Btiqfx4dnbsfIh8shomB+4N7 zb0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710570434; x=1711175234; 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=Cki+mFzqpWdQEuZ4xoRqIylQYFyJc2GQofnHRu0Mx6A4z9UTZVPBzL4h9GkSii/+vz 4uQlokcoiJzxzqR9WWU73mkGD1VMJNUdMmiUgwKoZyrKpiuWRTrlSOososazpdfaKcu4 1Eb+L0y7qO7erXPJWxtRRIeXkCHLl1UNEbKvsDbE04puMCodf2cTV4hSB4geH+Y6PwRi JMtPzaP/1NJKSU0YYLK3jEYa2IAiBnroSY/OI19kcVnXu+TdrRtD4hKJoNWDXMxSyHFN Xqj4S9qv47agzGU7sUjxBSaWp/9vl24LZH6lH6sVF4YVWGIFGYQpoIKunUIwQlg1iqhZ S8AA== X-Gm-Message-State: AOJu0YzwEXSdDSukKpVnaD+Om/MzdfYTOYvHk3pngb2eFEAVWvjN1EH0 4F4C/U9h188Z9MRQAWp2ILm53lOpzijXR7dgnQylFXJi0wIPhjUPobCTL7sb X-Google-Smtp-Source: AGHT+IEtPOP/BAKeF9wLksjg1DzLN1SR05Xx8TFuyF0u2trZpKVmauRedQr5BaE2JfiytKD96A6X7A== X-Received: by 2002:a5d:66cf:0:b0:33d:9f58:8803 with SMTP id k15-20020a5d66cf000000b0033d9f588803mr4556129wrw.18.1710570434484; Fri, 15 Mar 2024 23:27:14 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f5-20020adffcc5000000b0033e7b05edf3sm4708686wrs.44.2024.03.15.23.27.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 23:27:13 -0700 (PDT) Message-ID: In-Reply-To: References: Date: Sat, 16 Mar 2024 06:27:06 +0000 Subject: [PATCH 4/6] 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 Sat Mar 16 06:27: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: 13594377 Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.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 52532107A9 for ; Sat, 16 Mar 2024 06:27:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710570440; cv=none; b=f3sFTB46tyAod+F1P5zSi1c8lcW87je5oUeoWwuX3IgTnrVZ2Wd24n40YYWWF/wQCJcuNCiKm+P6Kff206MfhcN+P+u3tirXPErx0IruTghS1biEX5qi9I+7OFAX5LN32WLwHdJBheW0DpcvO78r545mj5smuNkkcCqd6CjgC/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710570440; c=relaxed/simple; bh=jfbRNkQyvPEiLKTtOYB84JTK+sM4Ui6G4u+TROtkgDI=; h=Message-ID:In-Reply-To:References:From:Date:Subject:MIME-Version: Content-Type:To:Cc; b=FQnmc+zruE6RrgtGeMV5L7SY7rGdaYwT8/g4VfnQMfUP4Q7peJEkC17DyWB+AaIgYx0tZ7qphlGfAL4gnjEMRQsSDYzq7+MJ37l+BEDh7SCgTUp0bYnow5axiOrn0gygepSNfGa9kPhgnU+KuiJwoNvxO9zVAaDhOj04siFPycs= 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=EWkdu89T; arc=none smtp.client-ip=209.85.167.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="EWkdu89T" Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-513d9176cf9so1629189e87.1 for ; Fri, 15 Mar 2024 23:27:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710570436; x=1711175236; 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=EWkdu89TOE4nn3X0ortQZP0MQeRWETCwqAWeNVlmr725D+z70I29fFNnu6PWmGO/o6 Qfw+1iL9bJ/+jUSs/wr2p3mcJQyaGpHxqTkFoTNdj7WWmVRgrjaqhSdjVT7VivI/KlDg 0PYlSYL0zOefZg7swh+j2QAK2XNKSQeMDerEvx5vQmFgfn1oRkqwNMHo7WqLIKQ2kRjq 3B6LpJjARjo/i4nSa+GMLUW0O8bH4NicFvcK88+o7kdTkM5YEgFevCxe62yDxqzYqeoM 2xYplPBtTzTXvasGaM4hzFzKEJZlPKJTuz4Od75irJdfRFd+2aQklGDPSDK5985jNCL/ SeIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710570436; x=1711175236; 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=dJNF2ye6t7RqtomalZ5ruaWdk16c335bihBXDThJYdG1F44bZgMNn6BcWto0wSsEBl O/Gf7qt5hwQVhtRZXiWUvjWCnek3Xd35NL5fQMxI52p9FQVeyRj136ld8i1AtxxYJJ5/ pDClXpuwdp1NiHpA0V47AmWurl2ELsZnVN17/vOE2M5nJy3s0y2ekJdj3bvnYnfnouR0 4jxZ41QbczaZh/dlXmthGZfMh4cAt8JaQg+K1Vorlqwh5MTdIhTE7AYFN1TXsE7+MFnL OxIhFzTeFlTRQcxUlDrFPi2wT199DC16Uqb4q1Bw91iLgEuXGTUVVC8LD9g6er/CDhRm /6rg== X-Gm-Message-State: AOJu0YyhWkSuZXfzd+PAtVlE8GQrSYqsvKG8SACG7A8Q++Xq9HwJg00s 2ORXt+EeuOy04K1z7adMbEpE+vrvKjlJdMtP3xCG83mw8H6ort6v4SBv4JGS X-Google-Smtp-Source: AGHT+IFBR5CW1HNuBWpJMPzYA7TD4fWD6NITtxEYCVUrYCL11z+OVweXCTj7LHwx3yWpq3xHj/Tr+w== X-Received: by 2002:a19:f504:0:b0:512:e58c:7bf1 with SMTP id j4-20020a19f504000000b00512e58c7bf1mr3271568lfb.40.1710570435922; Fri, 15 Mar 2024 23:27:15 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t20-20020a05600c199400b004132ae838absm7968575wmq.43.2024.03.15.23.27.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 23:27:14 -0700 (PDT) Message-ID: In-Reply-To: References: Date: Sat, 16 Mar 2024 06:27:07 +0000 Subject: [PATCH 5/6] 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 Sat Mar 16 06:27: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: 13594378 Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.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 7759510A0D for ; Sat, 16 Mar 2024 06:27:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710570441; cv=none; b=TmWiVJjxPq3/03MbrbRpQJGRmizl9LEPmFP2yBvjvnW6IAPVoLeCulMU6byGSj1eyUlZSCmbzn4noDV6aBn+ZVKqKO2dAoQ2QtMQkfUpBt358fkvzACxM1G3YMANO7M42GqsQfMgQJS1Stx6G4LLOgDzns+ZhlszQYv4tlO28ts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710570441; c=relaxed/simple; bh=yX6rb+UiEB1astWPY1iRzrTuD7QPl8KVlv1oC88H+4s=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=h8yL+xbt5CcXffdctXm+g5nwggmmMCiO4fnH84eJ13AIdQXYD54oHsCRB4in6tD13aOqcHPNg3h/qAFhHc5T93j9krT0Hw90QvG1rgpcO5V+WqzUF0a5+N0eh3g66s09+pb2HvBgMuTQZEQqeeRhMYI1pBu9NuaKRsIwwJOanGs= 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=TnGu0q6+; arc=none smtp.client-ip=209.85.221.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="TnGu0q6+" Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-33ec8f13c62so1755527f8f.0 for ; Fri, 15 Mar 2024 23:27:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710570436; x=1711175236; 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=TnGu0q6+SbBh0vs6v1a/HO4R/J9yL//Pfe88WnkDzi83jzwUp9+iuGOxuGfDeEiyAK agBmMpqmqMr87/BQsgBCID5+8+KTatO1yMbv9CcsHwUwc79JSbeuGMWTuGVmWVIZtaAW +nXH9OZYACnZTo5f1wEekUortEPTd/2+1Kk6STwAerpZQGg1baed10Xj5Ve+6tmlmM68 pWvTWIVb2HAbCYEM3jYch9Iyxj8fplxVUTboT6GO59txnFDEwObxpF9Oq7Eah4xVsySv IWQux7MG5GH5nA9u2HTte7P+1Sns0MI3qZsm+v4NvhXTnMsoBm/CU0oUwELZ8qnL0Q/Z OCKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710570436; x=1711175236; 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=t38dR5X10KXEKAiXi2CKjbLk+4RnAToj3HCeBv7SYH06x2zAZwJ/QQv4VBfmp/AOTa //py6bEndvtGYp7eapz7CQqqlsmcrx/YNZVC7zPew2m9lyfXQHmgFyP9co9K03NDVDCx +rAp0XDbKQRietzGZJ52tk0uofPmfWVQlQRJ9JqCVdsVqv9+PCX74Y2RimCTxAq+AN9E ET4xkVlv/tD+APrrWHpYiOWAMfE1vnDMGb/7MRha8o8bEFxB5FPBrnVW6WbAlu1H2Jfc XcPyQc6ldetfpvb9QMj2ZOxjLfpAcNolBJCPNfjBTojt0vaihz4rDmtKbV/du+CFM7A1 JDdQ== X-Gm-Message-State: AOJu0YwllHY7J3wJ0bCjTIBo7k3lp9a4BySeYz2FhZ/82j8fX2q0bV3i MTg+2X8E2+Re+vRf/qbpne8EAVpCvlLk88bG4ZLN5mM9gvVs6tMkZja4c6QC X-Google-Smtp-Source: AGHT+IFvIcs+2MevlA91A13W7X5VH+KQYPCszhZcfjdvn9hwVMg829xhyxPnFZJsHOxgFNghVTDzDA== X-Received: by 2002:a05:6000:bc7:b0:33e:c6a1:1a39 with SMTP id dm7-20020a0560000bc700b0033ec6a11a39mr3673642wrb.65.1710570436597; Fri, 15 Mar 2024 23:27:16 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n8-20020a5d6b88000000b0033ec8739918sm4688904wrx.41.2024.03.15.23.27.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 23:27:16 -0700 (PDT) Message-ID: <19de7c64171668503b3676bee9fa9e8660671b17.1710570428.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Sat, 16 Mar 2024 06:27:08 +0000 Subject: [PATCH 6/6] 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 *);