From patchwork Tue Sep 26 06:22:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13398720 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CB80EE020C for ; Tue, 26 Sep 2023 06:23:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233773AbjIZGXG (ORCPT ); Tue, 26 Sep 2023 02:23:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233747AbjIZGXE (ORCPT ); Tue, 26 Sep 2023 02:23:04 -0400 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7E49E9 for ; Mon, 25 Sep 2023 23:22:57 -0700 (PDT) Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-503065c4b25so12789668e87.1 for ; Mon, 25 Sep 2023 23:22:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695709376; x=1696314176; 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=zWIlzatUj0yB3Mm8UszHb+wk6g8QnOKK3XsDQqGKZnc=; b=keYXD4HjUMg0DhAi+ab3z8te/r4yS7+BlYIYOhf168uVwG0FOXpNTzZL3HwMKuAyYS jQ6qJ00W5DiB3/2vQdj/SNTeKukTjdwt1jq2KVGov1mfIFl/nS0V36fXz5hKfLicXhNM QlDDhTULJ65qQrHYsxMpoBOBniXTxZ2ayh2nBQ86J7/YE+Ds0/WN90VyejfP2/ep10Qe 7vrV+dyd+lKV1b8KxSq/PIFn3WnVj148KiYpoQg8qlWu9GrwMNvXd9t18eK22sfJ3ODX q+guGEgsDsfoHkN0L3sIkN8zorkCc0J4DnbTnAnSfuvqxcAGY3M1hIEgdCoWbSJIu1QE Z6sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695709376; x=1696314176; 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=zWIlzatUj0yB3Mm8UszHb+wk6g8QnOKK3XsDQqGKZnc=; b=F2yBSbQbhBhvip0s6zUoXGI6YlyolhgeGlYRp/HzvQSEtEwACyHYER1qwa363okCrd WJrh/lGhdw2uZS/8gm9pwRo+vzBAhsNn1Uj3a7sN/pBrqTcRfenT0kchsokKy+D/NsjQ Ia2nMe2rNSZ/qEJGKZqVDJkVgPKE9+nNfFSuGIvJZP3KR7zgBRZX82VD7K7gZpzPmaw2 5hlIpL5hPi0VazX/3nRcz3mwUOxZBp9NqwNUQUruqmVL8ATHp3/zRvrvCr1s2Aw1IaAO qCS28DIyMlxuwRSCF6+OHrSaATQ1B4kAUx5y5nLGoOEfRXRMtn5NB1YaksMeEQd9+dq2 Ggyw== X-Gm-Message-State: AOJu0YztndZj7eDJB1VTa+JsO3HaqhkV2TfNUxhkpwY/owY2J9H7R/lz 7SFChlMXw7d8MZ4Axs7NvY5uaQqERqQ= X-Google-Smtp-Source: AGHT+IH/Fjxbljh1pcIDIEj8mGlTLNvY7L2hQPwTh7pyaNFktzThzC5VadqqAfqhfwBe2CTm+h+thA== X-Received: by 2002:a05:6512:33cd:b0:4ff:a8c6:d1aa with SMTP id d13-20020a05651233cd00b004ffa8c6d1aamr8606588lfg.48.1695709375724; Mon, 25 Sep 2023 23:22:55 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f11-20020ac2532b000000b005007fc9ccd0sm2075655lfh.113.2023.09.25.23.22.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 23:22:55 -0700 (PDT) Message-ID: <4ce5cf77005eb8c6da243777b3c29103add7ddbd.1695709372.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 26 Sep 2023 06:22:49 +0000 Subject: [PATCH v4 1/4] commit: ignore_non_trailer computes number of bytes to ignore Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Glen Choo , Christian Couder , Phillip Wood , Linus Arver , Linus Arver Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Linus Arver From: Linus Arver ignore_non_trailer() returns the _number of bytes_ that should be ignored from the end of the log message. It does not by itself "ignore" anything. Rename this function to remove the leading "ignore" verb, to sound more like a quantity than an action. Signed-off-by: Linus Arver --- builtin/commit.c | 2 +- builtin/merge.c | 2 +- commit.c | 2 +- commit.h | 4 ++-- trailer.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index 7da5f924484..d1785d32db1 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -900,7 +900,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, strbuf_stripspace(&sb, '\0'); if (signoff) - append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0); + append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0); if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len) die_errno(_("could not write commit template")); diff --git a/builtin/merge.c b/builtin/merge.c index 545da0c8a11..c654a29fe85 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -870,7 +870,7 @@ static void prepare_to_commit(struct commit_list *remoteheads) _(no_scissors_editor_comment), comment_line_char); } if (signoff) - append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0); + append_signoff(&msg, ignored_log_message_bytes(msg.buf, msg.len), 0); write_merge_heads(remoteheads); write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len); if (run_commit_hook(0 < option_edit, get_index_file(), NULL, diff --git a/commit.c b/commit.c index b3223478bc2..4440fbabb83 100644 --- a/commit.c +++ b/commit.c @@ -1769,7 +1769,7 @@ const char *find_commit_header(const char *msg, const char *key, size_t *out_len * Returns the number of bytes from the tail to ignore, to be fed as * the second parameter to append_signoff(). */ -size_t ignore_non_trailer(const char *buf, size_t len) +size_t ignored_log_message_bytes(const char *buf, size_t len) { size_t boc = 0; size_t bol = 0; diff --git a/commit.h b/commit.h index 28928833c54..1cc872f225f 100644 --- a/commit.h +++ b/commit.h @@ -294,8 +294,8 @@ const char *find_header_mem(const char *msg, size_t len, const char *find_commit_header(const char *msg, const char *key, size_t *out_len); -/* Find the end of the log message, the right place for a new trailer. */ -size_t ignore_non_trailer(const char *buf, size_t len); +/* Find the number of bytes to ignore from the end of a log message. */ +size_t ignored_log_message_bytes(const char *buf, size_t len); typedef int (*each_mergetag_fn)(struct commit *commit, struct commit_extra_header *extra, void *cb_data); diff --git a/trailer.c b/trailer.c index b6de5d9cb2d..3c54b38a85a 100644 --- a/trailer.c +++ b/trailer.c @@ -928,7 +928,7 @@ continue_outer_loop: /* Return the position of the end of the trailers. */ static size_t find_trailer_end(const char *buf, size_t len) { - return len - ignore_non_trailer(buf, len); + return len - ignored_log_message_bytes(buf, len); } static int ends_with_blank_line(const char *buf, size_t len) From patchwork Tue Sep 26 06:22:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13398722 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51437E8181F for ; Tue, 26 Sep 2023 06:23:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233792AbjIZGXK (ORCPT ); Tue, 26 Sep 2023 02:23:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233749AbjIZGXG (ORCPT ); Tue, 26 Sep 2023 02:23:06 -0400 Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [IPv6:2a00:1450:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA48ADF for ; Mon, 25 Sep 2023 23:22:58 -0700 (PDT) Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2c131ddfc95so123918701fa.0 for ; Mon, 25 Sep 2023 23:22:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695709377; x=1696314177; 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=+rwbg0EHFrct7ZzWHjVPCjko4EDWAGutEr1RanG0Sgg=; b=PMTJorQ3v+jev8ChsqcfgWymfMoDS5mgVJoWtNt/ONNcqQCSSmjh3FT/dM17VELLUt 3Jy4zCvdV07H20mnIogkcYs9WzH6E0Pjb6cd/CXUDvrHp0StRuljpR+0yQMxsvxXRJFd 2cxvBcmJKpLoRHW3NKk4+v/+ZXd3tRZ7+/Ti/RWyl76yUXsj/fQVfQoxT1GxWUQExA8J ZX3dEwTtIe2PmcHMWIUUHf+1IxuBft3bSGINMjZ6phssoH9ivMiFI2sRKDcbytAFiFym YoIjNcQxnK+e/rZu7Ct+aCx7MRqGQzoRk+5mfeWUzDgmTKRjLzujEgVFNc716U8WllSl PPkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695709377; x=1696314177; 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=+rwbg0EHFrct7ZzWHjVPCjko4EDWAGutEr1RanG0Sgg=; b=VK6dp1OlqYIVQIIQZ57h3fNxp+IUynhGAZ7OtyMaQh3BgA6NrT+NYbTnm5TEhX+1aX 8iJ0wpao9WBk/UVTPhd+StUu+f/ns1Ow0QfqG3axLEPs/MijmpDosOwH/YvjRnQU6mfi 2oYpucnM7BcMtjbcbOO3Iz6xquI6HMPmz5Ja3IMql5UeZE0prCO9Jpm2Q7UIkW3w6kam Y0BDL67/IdZTWvcJWL9D4W2Td8hbBnAF7VnYmO75JmlmcjCQviOhM8FMgjkliPBlYnr5 DmnHWpnG2VlkD38rgUodJ4ZVDmgYbSnbCaTN1x3RZeahjsGm7S2Cp+9G0pF2og9uLYeU JULg== X-Gm-Message-State: AOJu0YyIPbO/1gBVdVziRbbQfUeTQVC+TnVZxOa1cBXJ5S6hIe30ffV4 1Z/Kiun6qzO1WI9Pkw/bCfFD4cxMpFE= X-Google-Smtp-Source: AGHT+IHYRCMSU73w4CFmx/r4UBYAKdpbJIJqQvXD6J7MdyWU1BJk6Rl+Ca94kOP37ju9wi2ZUg0zRw== X-Received: by 2002:a2e:b7d4:0:b0:2c0:d06:9e65 with SMTP id p20-20020a2eb7d4000000b002c00d069e65mr6637619ljo.8.1695709376865; Mon, 25 Sep 2023 23:22:56 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n3-20020a2e9043000000b002bbacc6c523sm2549587ljg.49.2023.09.25.23.22.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 23:22:56 -0700 (PDT) Message-ID: In-Reply-To: References: Date: Tue, 26 Sep 2023 06:22:50 +0000 Subject: [PATCH v4 2/4] trailer: find the end of the log message Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Glen Choo , Christian Couder , Phillip Wood , Linus Arver , Linus Arver Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Linus Arver From: Linus Arver Previously, trailer_info_get() computed the trailer block end position by (1) checking for the opts->no_divider flag and optionally calling find_patch_start() to find the "patch start" location (patch_start), and (2) calling find_trailer_end() to find the end of the trailer block using patch_start as a guide, saving the return value into "trailer_end". The logic in (1) was awkward because the variable "patch_start" is misleading if there is no patch in the input. The logic in (2) was misleading because it could be the case that no trailers are in the input (yet we are setting a "trailer_end" variable before even searching for trailers, which happens later in find_trailer_start()). The name "find_trailer_end" was misleading because that function did not look for any trailer block itself --- instead it just computed the end position of the log message in the input where the end of the trailer block (if it exists) would be (because trailer blocks must always come after the end of the log message). Combine the logic in (1) and (2) together into find_patch_start() by renaming it to find_end_of_log_message(). The end of the log message is the starting point which find_trailer_start() needs to start searching backward to parse individual trailers (if any). Helped-by: Junio C Hamano Signed-off-by: Linus Arver --- trailer.c | 61 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/trailer.c b/trailer.c index 3c54b38a85a..96cb285a4ea 100644 --- a/trailer.c +++ b/trailer.c @@ -809,21 +809,47 @@ static ssize_t last_line(const char *buf, size_t len) } /* - * Return the position of the start of the patch or the length of str if there - * is no patch in the message. + * Find the end of the log message as an offset from the start of the input + * (where callers of this function are interested in looking for a trailers + * block in the same input). We have to consider two categories of content that + * can come at the end of the input which we want to ignore (because they don't + * belong in the log message): + * + * (1) the "patch part" which begins with a "---" divider and has patch + * information (like the output of git-format-patch), and + * + * (2) any trailing comment lines, blank lines like in the output of "git + * commit -v", or stuff below the "cut" (scissor) line. + * + * As a formula, the situation looks like this: + * + * INPUT = LOG MESSAGE + IGNORED + * + * where IGNORED can be either of the two categories described above. It may be + * that there is nothing to ignore. Now it may be the case that the LOG MESSAGE + * contains a trailer block, but that's not the concern of this function. */ -static size_t find_patch_start(const char *str) +static size_t find_end_of_log_message(const char *input, int no_divider) { + size_t end; + const char *s; - for (s = str; *s; s = next_line(s)) { + /* Assume the naive end of the input is already what we want. */ + end = strlen(input); + + /* Optionally skip over any patch part ("---" line and below). */ + for (s = input; *s; s = next_line(s)) { const char *v; - if (skip_prefix(s, "---", &v) && isspace(*v)) - return s - str; + if (!no_divider && skip_prefix(s, "---", &v) && isspace(*v)) { + end = s - input; + break; + } } - return s - str; + /* Skip over other ignorable bits. */ + return end - ignored_log_message_bytes(input, end); } /* @@ -925,12 +951,6 @@ continue_outer_loop: return len; } -/* Return the position of the end of the trailers. */ -static size_t find_trailer_end(const char *buf, size_t len) -{ - return len - ignored_log_message_bytes(buf, len); -} - static int ends_with_blank_line(const char *buf, size_t len) { ssize_t ll = last_line(buf, len); @@ -1101,7 +1121,7 @@ void process_trailers(const char *file, void trailer_info_get(struct trailer_info *info, const char *str, const struct process_trailer_options *opts) { - int patch_start, trailer_end, trailer_start; + int end_of_log_message, trailer_start; struct strbuf **trailer_lines, **ptr; char **trailer_strings = NULL; size_t nr = 0, alloc = 0; @@ -1109,16 +1129,11 @@ void trailer_info_get(struct trailer_info *info, const char *str, ensure_configured(); - if (opts->no_divider) - patch_start = strlen(str); - else - patch_start = find_patch_start(str); - - trailer_end = find_trailer_end(str, patch_start); - trailer_start = find_trailer_start(str, trailer_end); + end_of_log_message = find_end_of_log_message(str, opts->no_divider); + trailer_start = find_trailer_start(str, end_of_log_message); trailer_lines = strbuf_split_buf(str + trailer_start, - trailer_end - trailer_start, + end_of_log_message - trailer_start, '\n', 0); for (ptr = trailer_lines; *ptr; ptr++) { @@ -1141,7 +1156,7 @@ void trailer_info_get(struct trailer_info *info, const char *str, info->blank_line_before_trailer = ends_with_blank_line(str, trailer_start); info->trailer_start = str + trailer_start; - info->trailer_end = str + trailer_end; + info->trailer_end = str + end_of_log_message; info->trailers = trailer_strings; info->trailer_nr = nr; } From patchwork Tue Sep 26 06:22:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13398723 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A458FEE020C for ; Tue, 26 Sep 2023 06:23:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233795AbjIZGXO (ORCPT ); Tue, 26 Sep 2023 02:23:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233774AbjIZGXH (ORCPT ); Tue, 26 Sep 2023 02:23:07 -0400 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D4CEE6 for ; Mon, 25 Sep 2023 23:23:00 -0700 (PDT) Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-5046bf37ec1so3366257e87.1 for ; Mon, 25 Sep 2023 23:23:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695709378; x=1696314178; 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=zj9fPgAekG4Qh/8P7EF1+8FR/3JIU8xIJkNsOP7+fOE=; b=Ai63mgyXiASHYrC9WJ+UxC3J6rNKYNWXKjqQ9wkhVBRK2+mWA1n5X2Pq8WVNWjGQ+F t8CC4XL7Y988adLZYl+SqCbIEGu+txxGOlL1cIUZw698pTfyDC+tH4S1s9WLrsePanSd GvJSLGUMylFIy2R9U3mVXLbtwZLMKj+JNvIGvo+kYBjrgNY6KBOsWpen5HXfwyqCm/p5 b9C1WieOtFIAafijDG4rWXQK7bYPUQH4AeUQneCSrTP7++r+ncgVYIHktE91n0a51P+7 LSJEXCeXvWC8MeaASFKmbE97ylVWTyLwMuFB1Qa0qWTOhc2OSZxcbIUuDujdtagpZuvC TTZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695709378; x=1696314178; 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=zj9fPgAekG4Qh/8P7EF1+8FR/3JIU8xIJkNsOP7+fOE=; b=qsJlr2Dc6j+ZFudA/uruXjYKFdHGRBpKdLuWBlI36NazKUbUbjvH7lXFashcAg9fGq Vquj82riEImkrOqcC+b7EHl1WNxot7/IHjVR5wc70sGMhdksAf+yLvjgFD/f9bw8LYBQ Av2zrBSIVIxbLODk8BRMJhV/+Tk2Tml0dsAnLP8fFX6LCjVru/qZ1gtU+w7CWEXkouI2 3uuVhCt2xo471j0vJSNwkKB0phLf04QP6GyceJ7kJ6ccbz7JnSGZVheTJhZuEwm1QbOL 93XY+Gtyds43nKunZ3E84AMIBZnQeNH4qT5QFbYjtISUJ7zJulaYgJjD3l7yMpo+3xS7 qkGA== X-Gm-Message-State: AOJu0YxFPU6a4lPN4E6Q6RIgS0A47SuxaOSxntfFP1mDfVUeBl0sD/WU 2jhVP8VK0m7d7CEd2eVVsaCLXuDTN4I= X-Google-Smtp-Source: AGHT+IGen7F8+rwpzNPzc8KeNlsMfp6RawriEOQrvRGi+0JZMcWcmSPbH+sVrchXUouHXr1AQhDheA== X-Received: by 2002:a05:6512:2829:b0:503:385c:4319 with SMTP id cf41-20020a056512282900b00503385c4319mr9685488lfb.19.1695709378001; Mon, 25 Sep 2023 23:22:58 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id i3-20020ac25223000000b004f9c44b3e6dsm2111359lfl.127.2023.09.25.23.22.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 23:22:57 -0700 (PDT) Message-ID: <796e47c1e5fb50a8adb4cf803320de926912a8ad.1695709372.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 26 Sep 2023 06:22:51 +0000 Subject: [PATCH v4 3/4] trailer: use offsets for trailer_start/trailer_end Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Glen Choo , Christian Couder , Phillip Wood , Linus Arver , Linus Arver Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Linus Arver From: Linus Arver Previously these fields in the trailer_info struct were of type "const char *" and pointed to positions in the input string directly (to the start and end positions of the trailer block). Use offsets to make the intended usage less ambiguous. We only need to reference the input string in format_trailer_info(), so update that function to take a pointer to the input. While we're at it, rename trailer_start to trailer_block_start to be more explicit about these offsets (that they are for the entire trailer block including other trailers). Ditto for trailer_end. Signed-off-by: Linus Arver --- sequencer.c | 2 +- trailer.c | 29 ++++++++++++++--------------- trailer.h | 13 ++++++++----- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/sequencer.c b/sequencer.c index d584cac8ed9..8707a92204f 100644 --- a/sequencer.c +++ b/sequencer.c @@ -345,7 +345,7 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, if (ignore_footer) sb->buf[sb->len - ignore_footer] = saved_char; - if (info.trailer_start == info.trailer_end) + if (info.trailer_block_start == info.trailer_block_end) return 0; for (i = 0; i < info.trailer_nr; i++) diff --git a/trailer.c b/trailer.c index 96cb285a4ea..3dc2faa969c 100644 --- a/trailer.c +++ b/trailer.c @@ -856,7 +856,7 @@ static size_t find_end_of_log_message(const char *input, int no_divider) * Return the position of the first trailer line or len if there are no * trailers. */ -static size_t find_trailer_start(const char *buf, size_t len) +static size_t find_trailer_block_start(const char *buf, size_t len) { const char *s; ssize_t end_of_title, l; @@ -1072,7 +1072,6 @@ void process_trailers(const char *file, LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; struct trailer_info info; - size_t trailer_end; FILE *outfile = stdout; ensure_configured(); @@ -1083,11 +1082,10 @@ void process_trailers(const char *file, outfile = create_in_place_tempfile(file); parse_trailers(&info, sb.buf, &head, opts); - trailer_end = info.trailer_end - sb.buf; /* Print the lines before the trailers */ if (!opts->only_trailers) - fwrite(sb.buf, 1, info.trailer_start - sb.buf, outfile); + fwrite(sb.buf, 1, info.trailer_block_start, outfile); if (!opts->only_trailers && !info.blank_line_before_trailer) fprintf(outfile, "\n"); @@ -1109,7 +1107,7 @@ void process_trailers(const char *file, /* Print the lines after the trailers as is */ if (!opts->only_trailers) - fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile); + fwrite(sb.buf + info.trailer_block_end, 1, sb.len - info.trailer_block_end, outfile); if (opts->in_place) if (rename_tempfile(&trailers_tempfile, file)) @@ -1121,7 +1119,7 @@ void process_trailers(const char *file, void trailer_info_get(struct trailer_info *info, const char *str, const struct process_trailer_options *opts) { - int end_of_log_message, trailer_start; + 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; @@ -1130,10 +1128,10 @@ void trailer_info_get(struct trailer_info *info, const char *str, ensure_configured(); end_of_log_message = find_end_of_log_message(str, opts->no_divider); - trailer_start = find_trailer_start(str, end_of_log_message); + trailer_block_start = find_trailer_block_start(str, end_of_log_message); - trailer_lines = strbuf_split_buf(str + trailer_start, - end_of_log_message - trailer_start, + trailer_lines = strbuf_split_buf(str + trailer_block_start, + end_of_log_message - trailer_block_start, '\n', 0); for (ptr = trailer_lines; *ptr; ptr++) { @@ -1154,9 +1152,9 @@ void trailer_info_get(struct trailer_info *info, const char *str, strbuf_list_free(trailer_lines); info->blank_line_before_trailer = ends_with_blank_line(str, - trailer_start); - info->trailer_start = str + trailer_start; - info->trailer_end = str + end_of_log_message; + 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; } @@ -1171,6 +1169,7 @@ void trailer_info_release(struct trailer_info *info) static void format_trailer_info(struct strbuf *out, const struct trailer_info *info, + const char *msg, const struct process_trailer_options *opts) { size_t origlen = out->len; @@ -1180,8 +1179,8 @@ static void format_trailer_info(struct strbuf *out, if (!opts->only_trailers && !opts->unfold && !opts->filter && !opts->separator && !opts->key_only && !opts->value_only && !opts->key_value_separator) { - strbuf_add(out, info->trailer_start, - info->trailer_end - info->trailer_start); + strbuf_add(out, msg + info->trailer_block_start, + info->trailer_block_end - info->trailer_block_start); return; } @@ -1235,7 +1234,7 @@ void format_trailers_from_commit(struct strbuf *out, const char *msg, struct trailer_info info; trailer_info_get(&info, msg, opts); - format_trailer_info(out, &info, opts); + format_trailer_info(out, &info, msg, opts); trailer_info_release(&info); } diff --git a/trailer.h b/trailer.h index ab2cd017567..70d7b8bf1d8 100644 --- a/trailer.h +++ b/trailer.h @@ -32,16 +32,19 @@ 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_start. + * trailer_block_start. */ int blank_line_before_trailer; /* - * Pointers to the start and end of the trailer block found. If there - * is no trailer block found, these 2 pointers point to the end of the - * input string. + * 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, per find_true_end_of_input(). + * + * NOTE: This will be changed so that these point to 0 in the next + * patch if no trailers are found. */ - const char *trailer_start, *trailer_end; + size_t trailer_block_start, trailer_block_end; /* * Array of trailers found. From patchwork Tue Sep 26 06:22:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13398724 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C8D5EE14D8 for ; Tue, 26 Sep 2023 06:23:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233814AbjIZGXW (ORCPT ); Tue, 26 Sep 2023 02:23:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40450 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233783AbjIZGXI (ORCPT ); Tue, 26 Sep 2023 02:23:08 -0400 Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CEEAF0 for ; Mon, 25 Sep 2023 23:23:01 -0700 (PDT) Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-2c12ae20a5cso129259421fa.2 for ; Mon, 25 Sep 2023 23:23:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695709379; x=1696314179; 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=p8hixXzd8jXu6lQmBPmqdQo/d01jD8VxJ5wDmz+8Ee0=; b=Ff8aXIeVKoNKuwFCuWvOJKzTclF7H7re58ZgnNOgMPA19mrCb76VkJIt2TlwZHNBru Ce+qyXvoLrbAMx1g/0vhdoRDVdJdlMoWyUT1xRiZJT+DrOn0vTUjpF6hwVeFDngaBLtg woL5p2tW/0gtxIT3lAZpenfo+ZQTACpC1nPeIuhMVkS2M7SmIZeC/LpW6O9FniKeRZWj VzXK9MOcBrQGdaYwnIygt/8EmUNlkHiGOPLniBOqI7p2zCc2oYRpeif0qhvX6dTSewPD e1vd2w2v1FusFklKQ4qmfV88Uyr8aAtYYxfSfcpLswyyGrElfwMgbqxbjMyEDR+3stz2 Jx2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695709379; x=1696314179; 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=p8hixXzd8jXu6lQmBPmqdQo/d01jD8VxJ5wDmz+8Ee0=; b=PSlOzxbB9QP1O4m4yyolY3oyHUhcpfX8BOLVsTxpxwslj/gEOmLM5sN8r3Ut8N+GgN 91EZ6fG1aty48r4hzHPKlGZq1UzLAzyB70yBNGpUywQiyQD2fRK5GsAzoph6SPKbv8S2 bmQ/9Jr5fbxF2h9nJhrF+tjUrqsFeMQiJ7vuKTlOccl/KielkuN4DXBhwZqr+kAgBPLG GKWFZq6KMK4mDZN0FhRkQCdQBKJWdYj4s5PWgK6bTAywKlWct3147mFNIyP9iMwdgWMR lZVmQVMt98uqDkGZ2ePZKr4+CbiEPIKzRClK+rSZill0LOctyaFaGnDNeOjpLAfqKDGu vRWQ== X-Gm-Message-State: AOJu0Yx+yhMmTnfPDSW2k1lpVzMgnH5ZXGcnRA02wM6pJdW9gX6FjRrA kxn+udDB2JpQuSKxsqkbNwYXoixovPM= X-Google-Smtp-Source: AGHT+IFPzJOzZBsZp1smkKH63dpAL99LaAJsiyqT1KlaqLcvrGS2OLMpzKHCicmxXYpavpk+VAmaeA== X-Received: by 2002:a2e:6e13:0:b0:2bc:b694:6d6e with SMTP id j19-20020a2e6e13000000b002bcb6946d6emr7700417ljc.27.1695709378971; Mon, 25 Sep 2023 23:22:58 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y4-20020a2e95c4000000b002ba586d27a2sm2515306ljh.26.2023.09.25.23.22.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Sep 2023 23:22:58 -0700 (PDT) Message-ID: <64e1bd4e4be6d5f59b17986601aa2b7285362937.1695709372.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 26 Sep 2023 06:22:52 +0000 Subject: [PATCH v4 4/4] trailer: only use trailer_block_* variables if trailers were found Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Glen Choo , Christian Couder , Phillip Wood , Linus Arver , Linus Arver Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Linus Arver From: Linus Arver Previously, these variables were overloaded to act as the end of the log message even if no trailers were found. Remove the overloaded meaning by adding a new end_of_log_message field to the trailer_info struct. The trailer_info struct consumers now only refer to the trailer_block_start and trailer_block_end fields if trailers were found (trailer_nr > 0), and otherwise refer to the end_of_log_message. Signed-off-by: Linus Arver --- trailer.c | 31 +++++++++++++++++++++++-------- trailer.h | 12 +++++++----- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/trailer.c b/trailer.c index 3dc2faa969c..c11839ae365 100644 --- a/trailer.c +++ b/trailer.c @@ -1083,9 +1083,14 @@ void process_trailers(const char *file, parse_trailers(&info, sb.buf, &head, opts); - /* Print the lines before the trailers */ - if (!opts->only_trailers) - fwrite(sb.buf, 1, info.trailer_block_start, outfile); + /* Print the lines before the trailers (if any) as is. */ + if (!opts->only_trailers) { + if (info.trailer_nr) { + fwrite(sb.buf, 1, info.trailer_block_start, outfile); + } else { + fwrite(sb.buf, 1, info.end_of_log_message, outfile); + } + } if (!opts->only_trailers && !info.blank_line_before_trailer) fprintf(outfile, "\n"); @@ -1105,9 +1110,14 @@ void process_trailers(const char *file, free_all(&head); trailer_info_release(&info); - /* 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); + /* Print the lines after the trailers (if any) as is. */ + if (!opts->only_trailers) { + if (info.trailer_nr) { + fwrite(sb.buf + info.trailer_block_end, 1, sb.len - info.trailer_block_end, outfile); + } else { + fwrite(sb.buf + info.end_of_log_message, 1, sb.len - info.end_of_log_message, outfile); + } + } if (opts->in_place) if (rename_tempfile(&trailers_tempfile, file)) @@ -1153,8 +1163,13 @@ void trailer_info_get(struct trailer_info *info, const char *str, 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->trailer_block_start = 0; + info->trailer_block_end = 0; + if (nr) { + info->trailer_block_start = trailer_block_start; + info->trailer_block_end = end_of_log_message; + } + info->end_of_log_message = end_of_log_message; info->trailers = trailer_strings; info->trailer_nr = nr; } diff --git a/trailer.h b/trailer.h index 70d7b8bf1d8..d1e8751952b 100644 --- a/trailer.h +++ b/trailer.h @@ -38,14 +38,16 @@ struct trailer_info { /* * 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, per find_true_end_of_input(). - * - * NOTE: This will be changed so that these point to 0 in the next - * patch if no trailers are found. + * string. If no trailer block is found, these are set to 0. */ size_t trailer_block_start, trailer_block_end; + /* + * Offset to the end of the log message in the input (may not be the + * same as the end of the input). + */ + size_t end_of_log_message; + /* * Array of trailers found. */