From patchwork Wed Jan 10 06:51:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13515686 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 A3CDA24A14 for ; Wed, 10 Jan 2024 06:51:31 +0000 (UTC) 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="XVWeVRTS" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-40e4f71288bso15864825e9.1 for ; Tue, 09 Jan 2024 22:51:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704869489; x=1705474289; 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=QDu0A81Wnf88vB97RoIFzf86XD+FgkHNeSEcwAyLyvM=; b=XVWeVRTSqbtxd6m5VM8EHib6YVz87nmRMDC0yDo8VbF0Tzy3Vn9G9679gVYTF41fCr a9h/2I7N38BOw+d1LddQX3BCDIPCPXVaQnZ4GkxxXB7RPuS1a+KfidEMhaWXKjDFbkES VNvgq7wDA2/6a7GP3is0OyPOVuhyuM6oTpevkom/NTESi1dp46f+SyTXg/kXm3rRGlKK EET0pYjViQlPSfdMkDS2Sp+oiHwa/p4N34q8VrH5ZITF39bIZhT1MBjzZsoAj/Ha0WER J+KQpfOoteJbctxrM0IrieQcKOwVww/h13cR3rZfkjDC/IWmjLbNjEze7MEVYsZHqY4U 4bnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704869489; x=1705474289; 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=QDu0A81Wnf88vB97RoIFzf86XD+FgkHNeSEcwAyLyvM=; b=S4MrShdLcrJZ2uLk/1o2F50sFgREoTlWUcTPrc0tkpWf/7ItefgQB0Etq7J3G9zSxG 6mDk9PC/um7YdNlbf7H8k850jpHCHr7xT3X1ni2/a8jKdliY+t7TiIT5oXHfBsednJhb gEC4sn6GqgUmheQZWFnawEry5uVzChJuYujGIL0TGk/lBjCSvP9+JtuMFm1fTnP2Z+O6 fc3Ts+K8ZhHSkq3lzWuBB6h7/qGDPYLFn4TlaewsrD+J5rx/BRuuE9WsHS0vV6YT9EV8 NtfhatXbqv2McfNNvJYdAxagQzRtOY+3xSZRU3NrK9uP1S0Jn0WWdYRxS62baJX0oRL7 PYuA== X-Gm-Message-State: AOJu0Yx+MlGuHg2Qg9KzILgp90eO5TgLCPnB4BMAS2YILj8ni3TbM3sb 5ghBP5isqCOIl+zQtiykumujqNDpPt0= X-Google-Smtp-Source: AGHT+IFXLlh1TBxlIPEojcjiOY2P4rklTCap9wk0Ozi5Vp0SknGWdQYqIMf9ogTOzY9DoFrbPilSow== X-Received: by 2002:a05:600c:190c:b0:40e:3538:a5ff with SMTP id j12-20020a05600c190c00b0040e3538a5ffmr171897wmq.118.1704869489136; Tue, 09 Jan 2024 22:51:29 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id r20-20020a05600c35d400b0040d8eca092esm988759wmq.47.2024.01.09.22.51.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 22:51:28 -0800 (PST) Message-ID: <2dc3279b37f3aa81ba20e3dd08b029ca655ac3d8.1704869487.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 10 Jan 2024 06:51:17 +0000 Subject: [PATCH 01/10] trailer: move process_trailers() to interpret-trailers.c 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: Emily Shaffer , Junio C Hamano , Christian Couder , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver The interpret-trailers.c builtin is the only place we need to call process_trailers(). As it stands, process_trailers() is inherently tied to how the builtin behaves, so move its definition there. Delete the corresponding declaration from trailer.h, which then forces us to expose the working innards of that function. This enriches trailer.h to include a more granular API, which can then be unit-tested in the future (because process_trailers() by itself does too many things to be able to be easily unit-tested). Take this opportunity to demote some file-handling functions out of the trailer API implementation, as these have nothing to do with trailers. While we're at it, rename process_trailers() to interpret_trailers() in the builtin for consistency with the existing cmd_interpret_trailers(), which wraps around this function. Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 98 +++++++++++++++++++++++++++- trailer.c | 120 ++++------------------------------- trailer.h | 20 +++++- 3 files changed, 126 insertions(+), 112 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 033bd1556cf..444f8fb70c9 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -9,6 +9,7 @@ #include "gettext.h" #include "parse-options.h" #include "string-list.h" +#include "tempfile.h" #include "trailer.h" #include "config.h" @@ -91,6 +92,99 @@ static int parse_opt_parse(const struct option *opt, const char *arg, return 0; } +static struct tempfile *trailers_tempfile; + +static FILE *create_in_place_tempfile(const char *file) +{ + struct stat st; + struct strbuf filename_template = STRBUF_INIT; + const char *tail; + FILE *outfile; + + if (stat(file, &st)) + die_errno(_("could not stat %s"), file); + if (!S_ISREG(st.st_mode)) + die(_("file %s is not a regular file"), file); + if (!(st.st_mode & S_IWUSR)) + die(_("file %s is not writable by user"), file); + + /* Create temporary file in the same directory as the original */ + tail = strrchr(file, '/'); + if (tail) + strbuf_add(&filename_template, file, tail - file + 1); + strbuf_addstr(&filename_template, "git-interpret-trailers-XXXXXX"); + + trailers_tempfile = xmks_tempfile_m(filename_template.buf, st.st_mode); + strbuf_release(&filename_template); + outfile = fdopen_tempfile(trailers_tempfile, "w"); + if (!outfile) + die_errno(_("could not open temporary file")); + + return outfile; +} + +static void read_input_file(struct strbuf *sb, const char *file) +{ + if (file) { + if (strbuf_read_file(sb, file, 0) < 0) + die_errno(_("could not read input file '%s'"), file); + } else { + if (strbuf_read(sb, fileno(stdin), 0) < 0) + die_errno(_("could not read from stdin")); + } +} + +static void interpret_trailers(const char *file, + const struct process_trailer_options *opts, + struct list_head *new_trailer_head) +{ + LIST_HEAD(head); + struct strbuf sb = STRBUF_INIT; + struct trailer_info info; + FILE *outfile = stdout; + + ensure_configured(); + + read_input_file(&sb, file); + + if (opts->in_place) + outfile = create_in_place_tempfile(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); + + if (!opts->only_trailers && !info.blank_line_before_trailer) + fprintf(outfile, "\n"); + + + if (!opts->only_input) { + LIST_HEAD(config_head); + LIST_HEAD(arg_head); + parse_trailers_from_config(&config_head); + parse_trailers_from_command_line_args(&arg_head, new_trailer_head); + list_splice(&config_head, &arg_head); + process_trailers_lists(&head, &arg_head); + } + + print_all(outfile, &head, opts); + + 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); + + if (opts->in_place) + if (rename_tempfile(&trailers_tempfile, file)) + die_errno(_("could not rename temporary file to %s"), file); + + strbuf_release(&sb); +} + int cmd_interpret_trailers(int argc, const char **argv, const char *prefix) { struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; @@ -132,11 +226,11 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix) if (argc) { int i; for (i = 0; i < argc; i++) - process_trailers(argv[i], &opts, &trailers); + interpret_trailers(argv[i], &opts, &trailers); } else { if (opts.in_place) die(_("no input file given for in-place editing")); - process_trailers(NULL, &opts, &trailers); + interpret_trailers(NULL, &opts, &trailers); } new_trailers_clear(&trailers); diff --git a/trailer.c b/trailer.c index 3a0710a4583..9d70c9946bd 100644 --- a/trailer.c +++ b/trailer.c @@ -5,7 +5,6 @@ #include "string-list.h" #include "run-command.h" #include "commit.h" -#include "tempfile.h" #include "trailer.h" #include "list.h" /* @@ -163,8 +162,8 @@ static void print_tok_val(FILE *outfile, const char *tok, const char *val) fprintf(outfile, "%s%c %s\n", tok, separators[0], val); } -static void print_all(FILE *outfile, struct list_head *head, - const struct process_trailer_options *opts) +void print_all(FILE *outfile, struct list_head *head, + const struct process_trailer_options *opts) { struct list_head *pos; struct trailer_item *item; @@ -366,8 +365,8 @@ static int find_same_and_apply_arg(struct list_head *head, return 0; } -static void process_trailers_lists(struct list_head *head, - struct list_head *arg_head) +void process_trailers_lists(struct list_head *head, + struct list_head *arg_head) { struct list_head *pos, *p; struct arg_item *arg_tok; @@ -589,7 +588,7 @@ static int git_trailer_config(const char *conf_key, const char *value, return 0; } -static void ensure_configured(void) +void ensure_configured(void) { if (configured) return; @@ -719,7 +718,7 @@ static void add_arg_item(struct list_head *arg_head, char *tok, char *val, list_add_tail(&new_item->list, arg_head); } -static void parse_trailers_from_config(struct list_head *config_head) +void parse_trailers_from_config(struct list_head *config_head) { struct arg_item *item; struct list_head *pos; @@ -735,8 +734,8 @@ static void parse_trailers_from_config(struct list_head *config_head) } } -static void parse_trailers_from_command_line_args(struct list_head *arg_head, - struct list_head *new_trailer_head) +void parse_trailers_from_command_line_args(struct list_head *arg_head, + struct list_head *new_trailer_head) { struct strbuf tok = STRBUF_INIT; struct strbuf val = STRBUF_INIT; @@ -775,17 +774,6 @@ static void parse_trailers_from_command_line_args(struct list_head *arg_head, free(cl_separators); } -static void read_input_file(struct strbuf *sb, const char *file) -{ - if (file) { - if (strbuf_read_file(sb, file, 0) < 0) - die_errno(_("could not read input file '%s'"), file); - } else { - if (strbuf_read(sb, fileno(stdin), 0) < 0) - die_errno(_("could not read from stdin")); - } -} - static const char *next_line(const char *str) { const char *nl = strchrnul(str, '\n'); @@ -1000,10 +988,10 @@ static void unfold_value(struct strbuf *val) * Parse trailers in "str", populating the trailer info and "head" * linked list structure. */ -static void parse_trailers(struct trailer_info *info, - const char *str, - struct list_head *head, - const struct process_trailer_options *opts) +void parse_trailers(struct trailer_info *info, + const char *str, + struct list_head *head, + const struct process_trailer_options *opts) { struct strbuf tok = STRBUF_INIT; struct strbuf val = STRBUF_INIT; @@ -1035,7 +1023,7 @@ static void parse_trailers(struct trailer_info *info, } } -static void free_all(struct list_head *head) +void free_all(struct list_head *head) { struct list_head *pos, *p; list_for_each_safe(pos, p, head) { @@ -1044,88 +1032,6 @@ static void free_all(struct list_head *head) } } -static struct tempfile *trailers_tempfile; - -static FILE *create_in_place_tempfile(const char *file) -{ - struct stat st; - struct strbuf filename_template = STRBUF_INIT; - const char *tail; - FILE *outfile; - - if (stat(file, &st)) - die_errno(_("could not stat %s"), file); - if (!S_ISREG(st.st_mode)) - die(_("file %s is not a regular file"), file); - if (!(st.st_mode & S_IWUSR)) - die(_("file %s is not writable by user"), file); - - /* Create temporary file in the same directory as the original */ - tail = strrchr(file, '/'); - if (tail) - strbuf_add(&filename_template, file, tail - file + 1); - strbuf_addstr(&filename_template, "git-interpret-trailers-XXXXXX"); - - trailers_tempfile = xmks_tempfile_m(filename_template.buf, st.st_mode); - strbuf_release(&filename_template); - outfile = fdopen_tempfile(trailers_tempfile, "w"); - if (!outfile) - die_errno(_("could not open temporary file")); - - return outfile; -} - -void process_trailers(const char *file, - const struct process_trailer_options *opts, - struct list_head *new_trailer_head) -{ - LIST_HEAD(head); - struct strbuf sb = STRBUF_INIT; - struct trailer_info info; - FILE *outfile = stdout; - - ensure_configured(); - - read_input_file(&sb, file); - - if (opts->in_place) - outfile = create_in_place_tempfile(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); - - if (!opts->only_trailers && !info.blank_line_before_trailer) - fprintf(outfile, "\n"); - - - if (!opts->only_input) { - LIST_HEAD(config_head); - LIST_HEAD(arg_head); - parse_trailers_from_config(&config_head); - parse_trailers_from_command_line_args(&arg_head, new_trailer_head); - list_splice(&config_head, &arg_head); - process_trailers_lists(&head, &arg_head); - } - - print_all(outfile, &head, opts); - - 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); - - if (opts->in_place) - if (rename_tempfile(&trailers_tempfile, file)) - die_errno(_("could not rename temporary file to %s"), file); - - strbuf_release(&sb); -} - void trailer_info_get(struct trailer_info *info, const char *str, const struct process_trailer_options *opts) { diff --git a/trailer.h b/trailer.h index 1644cd05f60..b3e4a5e127d 100644 --- a/trailer.h +++ b/trailer.h @@ -81,15 +81,29 @@ struct process_trailer_options { #define PROCESS_TRAILER_OPTIONS_INIT {0} -void process_trailers(const char *file, - const struct process_trailer_options *opts, - struct list_head *new_trailer_head); +void parse_trailers_from_config(struct list_head *config_head); + +void parse_trailers_from_command_line_args(struct list_head *arg_head, + struct list_head *new_trailer_head); + +void process_trailers_lists(struct list_head *head, + struct list_head *arg_head); + +void parse_trailers(struct trailer_info *info, + const char *str, + struct list_head *head, + const struct process_trailer_options *opts); void trailer_info_get(struct trailer_info *info, const char *str, const struct process_trailer_options *opts); void trailer_info_release(struct trailer_info *info); +void ensure_configured(void); +void print_all(FILE *outfile, struct list_head *head, + const struct process_trailer_options *opts); +void free_all(struct list_head *head); + /* * Format the trailers from the commit msg "msg" into the strbuf "out". * Note two caveats about "opts": From patchwork Wed Jan 10 06:51:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13515687 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DE15C364C0 for ; Wed, 10 Jan 2024 06:51:32 +0000 (UTC) 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="cYGhsrZQ" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-40e43e489e4so42398985e9.1 for ; Tue, 09 Jan 2024 22:51:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704869490; x=1705474290; 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=+yOekFhHVVrew6cuTGAq2PPlVC/kLbfWTfBwN4Z1f8g=; b=cYGhsrZQpMLV6FCi4paCYYLjP1QJsWbIJndfC9Va3aPqLvm1WjLwTCZuj6ZRKHVfGv 4C6qVbWOT1Aqjwy7utfhP0MbulC3fKood9Ejnk7riXRDvgVOKzlKi2K2e8Dtrmxtm5C7 eBp9qs7cIs3tsXVAscbYD7MhY6XH7376zVfSY8s/JGr2tjMrvCzJCQU+M5JQUv2XhrGl MoPOES2lusdOhcFr4T62vO+ByDcGmydWXWWqhCBD2858ieMAf6snrF8OkJyMmtTGhQtj puzJKdYa2mONO9JuHdVUxFJg8ehpD7bAZipwyCR0q604g38W3OI01SloJkSdEaDoMbF+ LYzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704869490; x=1705474290; 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=+yOekFhHVVrew6cuTGAq2PPlVC/kLbfWTfBwN4Z1f8g=; b=FSrxWAVBSrUtdSJHENXzvIvTH+NFtEZg8l0dYF8IVe4/+3TQeTc1MTSRB8o43JpSdw B7mS84avqY7u4b+bQEqGXO7QBf/fAwC75Hr5boxXn89Paywwx4ezIauaCeFMkx84plgA c8BEP4N7c2N3Mc9xHWwinaSm25mZndbORczFxBqNShfXx1o5uXRFXbcu95YWOq4iafGc gkTOnqFoqDImEX9j/Dul1sBtxq2y65yqoErihGDQ12qfwkgkajFESv+N4PYK1Bg7/jjS Xdm7avfVNJ9bkakPjzKQijFOtO+H5Xw0BmNipqBKPP6+Dwi1gFKqCZSbXSIVOvjDRbg2 2NPQ== X-Gm-Message-State: AOJu0Yx8uilQRjMvWFm6FXtI0aJCiYJHn0UI0bMaxrfBITIlViwAFRP+ cAXM+BgbSlyi/8KPXq6rwI2d3g5DqLQ= X-Google-Smtp-Source: AGHT+IFloJtW67+rOeg78LR4BOahuIioT10vmro2VOqz+zfIi++K6CHyW63sygeNIu3bFbO3Em1VAA== X-Received: by 2002:a05:600c:4445:b0:40e:4386:11c6 with SMTP id v5-20020a05600c444500b0040e438611c6mr268696wmn.13.1704869490291; Tue, 09 Jan 2024 22:51:30 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n16-20020a05600c4f9000b0040d62f97e3csm1006449wmq.10.2024.01.09.22.51.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 22:51:29 -0800 (PST) Message-ID: <5f64718abfc2e61b4e259de700c137bc817fbb1c.1704869487.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 10 Jan 2024 06:51:18 +0000 Subject: [PATCH 02/10] trailer: include "trailer" term in API functions 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: Emily Shaffer , Junio C Hamano , Christian Couder , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver These functions are exposed to clients and so they should include "trailer" in their names for easier identification, just like all the other functions already exposed by trailer.h. Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 7 ++++--- trailer.c | 10 +++++----- trailer.h | 10 +++++----- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 444f8fb70c9..adb74276281 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -143,7 +143,7 @@ static void interpret_trailers(const char *file, struct trailer_info info; FILE *outfile = stdout; - ensure_configured(); + trailer_config_init(); read_input_file(&sb, file); @@ -169,9 +169,10 @@ static void interpret_trailers(const char *file, process_trailers_lists(&head, &arg_head); } - print_all(outfile, &head, opts); + /* Print trailer block. */ + format_trailers(outfile, &head, opts); - free_all(&head); + free_trailers(&head); trailer_info_release(&info); /* Print the lines after the trailers as is */ diff --git a/trailer.c b/trailer.c index 9d70c9946bd..0ce7e9079ca 100644 --- a/trailer.c +++ b/trailer.c @@ -162,8 +162,8 @@ static void print_tok_val(FILE *outfile, const char *tok, const char *val) fprintf(outfile, "%s%c %s\n", tok, separators[0], val); } -void print_all(FILE *outfile, struct list_head *head, - const struct process_trailer_options *opts) +void format_trailers(FILE *outfile, struct list_head *head, + const struct process_trailer_options *opts) { struct list_head *pos; struct trailer_item *item; @@ -588,7 +588,7 @@ static int git_trailer_config(const char *conf_key, const char *value, return 0; } -void ensure_configured(void) +void trailer_config_init(void) { if (configured) return; @@ -1023,7 +1023,7 @@ void parse_trailers(struct trailer_info *info, } } -void free_all(struct list_head *head) +void free_trailers(struct list_head *head) { struct list_head *pos, *p; list_for_each_safe(pos, p, head) { @@ -1041,7 +1041,7 @@ void trailer_info_get(struct trailer_info *info, const char *str, size_t nr = 0, alloc = 0; char **last = NULL; - ensure_configured(); + 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); diff --git a/trailer.h b/trailer.h index b3e4a5e127d..0e4f0ece9b3 100644 --- a/trailer.h +++ b/trailer.h @@ -99,11 +99,11 @@ void trailer_info_get(struct trailer_info *info, const char *str, void trailer_info_release(struct trailer_info *info); -void ensure_configured(void); -void print_all(FILE *outfile, struct list_head *head, - const struct process_trailer_options *opts); -void free_all(struct list_head *head); +void trailer_config_init(void); +void free_trailers(struct list_head *trailers); +void format_trailers(FILE *outfile, struct list_head *head, + const struct process_trailer_options *opts); /* * Format the trailers from the commit msg "msg" into the strbuf "out". * Note two caveats about "opts": @@ -111,7 +111,7 @@ void free_all(struct list_head *head); * - this is primarily a helper for pretty.c, and not * all of the flags are supported. * - * - this differs from process_trailers slightly in that we always format + * - this differs from format_trailers slightly in that we always format * only the trailer block itself, even if the "only_trailers" option is not * set. */ From patchwork Wed Jan 10 06:51:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13515688 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76E82364CF for ; Wed, 10 Jan 2024 06:51:33 +0000 (UTC) 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="Rr6IDkwp" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-33677fb38a3so3793096f8f.0 for ; Tue, 09 Jan 2024 22:51:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704869491; x=1705474291; 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=WbYL5BD29oUaC8ih/ccgcKd52IkYxSzaOeYYLFNhE9U=; b=Rr6IDkwpQnCyOFtAqec/S62SUZIQNFnXENTNu74YI2gk3Oswpdd+3dBCJeWiai8GSI 0i7D1yyU8GqYJbONGRAxBRpBpkrzCb5O0Af+68V57dGpYhgz1VjCf00tF0Gr1K14Kh6J Kj738npIxFeeeasiepZhj/D/gX2YVezChaWSnVIFcTq4ekHgiyph/L0IOTcOkVXZrKDK OpQBs8ZJei0uPSPW3ce2odLxnZ00MlMcUSUJWf428Y3aSs09/6h25zz1imoa+9A1caUb YTg8Zyye5Lkpjh4lB6fxRAJsJUlFqxuAry+9zUuvtdqTCkaV9qQbS5rvlYeK+VCgzTx9 DyzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704869491; x=1705474291; 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=WbYL5BD29oUaC8ih/ccgcKd52IkYxSzaOeYYLFNhE9U=; b=q17n8zOxbIQsS4ZEFZSl20qbk75c6xW/fIKKRW9sKQhqutCebCbz19FCT5UD6TRLR5 NeCYEPAVYEyraOj+zCfysLTjpEZ9lBd9Od7fE9jw7GBnYcHgyNsYTo63PYtZwUmqYmnF 7ExnUs1cVOZ/v0FzRguH+nyb4XxfIHORtE+2ThX+nQ8kfz7ugsIcUFpZ7jA6PWWZgu70 epQZKj2bZxnlXgXA6PQln0jpUpYyUZzAKiYAB9GDSeur962sYWGFRbyT7FpYzlvCR1KX I5Jlgu0Zc+xD/cActRvTaWs/QDsl79kCDGM8AQ50v9v6NqO53QtozpuJy/xUI6EVCoJ8 wLzA== X-Gm-Message-State: AOJu0YzPVQaAhQRz0jMVpALagZ3zJbX4dw4gVfLTpNPkyZ+W5KO+zHVr 3+qNp30/raSThCcnPzNdzAYn8EKzV6s= X-Google-Smtp-Source: AGHT+IFwI7thYF7n+CMVs9/WcvPYnyM9IGWBDerhnldiqHy5ZC15VzsAAi/MDV54f1Z458q+Q/Bxqw== X-Received: by 2002:a05:6000:2c1:b0:336:ca94:3e00 with SMTP id o1-20020a05600002c100b00336ca943e00mr282570wry.69.1704869490851; Tue, 09 Jan 2024 22:51:30 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f16-20020adffcd0000000b003373ef060d5sm4070464wrs.113.2024.01.09.22.51.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 22:51:30 -0800 (PST) Message-ID: In-Reply-To: References: Date: Wed, 10 Jan 2024 06:51:19 +0000 Subject: [PATCH 03/10] trailer: unify trailer formatting machinery 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: Emily Shaffer , Junio C Hamano , Christian Couder , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Currently have two functions for formatting trailers exposed in trailer.h: void format_trailers(FILE *outfile, struct list_head *head, const struct process_trailer_options *opts); void format_trailers_from_commit(struct strbuf *out, const char *msg, const struct process_trailer_options *opts); and previously these functions, although similar enough (even taking the same process_trailer_options struct pointer), did not build on each other. Make format_trailers_from_commit() rely on format_trailers(). Teach format_trailers() to process trailers with the additional process_trailer_options fields like opts->key_only which is only used by format_trailers_from_commit() and not builtin/interpret-trailers.c. This will allow us to delete the format_trailer_info() and print_tok_val() functions in the next patch. They are not deleted here in order to keep the diff small. Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 5 +- pretty.c | 2 +- ref-filter.c | 2 +- trailer.c | 105 +++++++++++++++++++++++++++++------ trailer.h | 21 +++---- 5 files changed, 102 insertions(+), 33 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index adb74276281..934833a4645 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -140,6 +140,7 @@ static void interpret_trailers(const char *file, { LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; + struct strbuf trailer_block = STRBUF_INIT; struct trailer_info info; FILE *outfile = stdout; @@ -170,7 +171,9 @@ static void interpret_trailers(const char *file, } /* Print trailer block. */ - format_trailers(outfile, &head, opts); + format_trailers(&head, opts, &trailer_block); + fwrite(trailer_block.buf, 1, trailer_block.len, outfile); + strbuf_release(&trailer_block); free_trailers(&head); trailer_info_release(&info); diff --git a/pretty.c b/pretty.c index cf964b060cd..f0721a5214f 100644 --- a/pretty.c +++ b/pretty.c @@ -1759,7 +1759,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */ goto trailer_out; } if (*arg == ')') { - format_trailers_from_commit(sb, msg + c->subject_off, &opts); + format_trailers_from_commit(msg + c->subject_off, &opts, sb); ret = arg - placeholder + 1; } trailer_out: diff --git a/ref-filter.c b/ref-filter.c index 35b989e1dfe..7fb13818686 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1985,7 +1985,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp struct strbuf s = STRBUF_INIT; /* Format the trailer info according to the trailer_opts given */ - format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts); + format_trailers_from_commit(subpos, &atom->u.contents.trailer_opts, &s); v->s = strbuf_detach(&s, NULL); } else if (atom->u.contents.option == C_BARE) diff --git a/trailer.c b/trailer.c index 0ce7e9079ca..315d90ee1ab 100644 --- a/trailer.c +++ b/trailer.c @@ -162,19 +162,6 @@ static void print_tok_val(FILE *outfile, const char *tok, const char *val) fprintf(outfile, "%s%c %s\n", tok, separators[0], val); } -void format_trailers(FILE *outfile, struct list_head *head, - const struct process_trailer_options *opts) -{ - struct list_head *pos; - struct trailer_item *item; - list_for_each(pos, head) { - item = list_entry(pos, struct trailer_item, list); - if ((!opts->trim_empty || strlen(item->value) > 0) && - (!opts->only_trailers || item->token)) - print_tok_val(outfile, item->token, item->value); - } -} - static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok) { struct trailer_item *new_item = xcalloc(1, sizeof(*new_item)); @@ -984,6 +971,78 @@ static void unfold_value(struct strbuf *val) strbuf_release(&out); } +void format_trailers(struct list_head *head, + const struct process_trailer_options *opts, + struct strbuf *out) +{ + struct list_head *pos; + struct trailer_item *item; + int need_separator = 0; + + list_for_each(pos, head) { + item = list_entry(pos, struct trailer_item, list); + if (item->token) { + char c; + + struct strbuf tok = STRBUF_INIT; + struct strbuf val = STRBUF_INIT; + strbuf_addstr(&tok, item->token); + strbuf_addstr(&val, item->value); + + /* + * Skip key/value pairs where the value was empty. This + * can happen from trailers specified without a + * separator, like `--trailer "Reviewed-by"` (no + * corresponding value). + */ + if (opts->trim_empty && !strlen(item->value)) + continue; + + if (!opts->filter || opts->filter(&tok, opts->filter_data)) { + if (opts->unfold) + unfold_value(&val); + + if (opts->separator && need_separator) + strbuf_addbuf(out, opts->separator); + if (!opts->value_only) + strbuf_addbuf(out, &tok); + if (!opts->key_only && !opts->value_only) { + if (opts->key_value_separator) + strbuf_addbuf(out, opts->key_value_separator); + else { + c = last_non_space_char(tok.buf); + if (c) { + if (!strchr(separators, c)) + strbuf_addf(out, "%c ", separators[0]); + } + } + } + if (!opts->key_only) + strbuf_addbuf(out, &val); + if (!opts->separator) + strbuf_addch(out, '\n'); + + need_separator = 1; + } + + strbuf_release(&tok); + strbuf_release(&val); + } else if (!opts->only_trailers) { + if (opts->separator && need_separator) { + strbuf_addbuf(out, opts->separator); + } + strbuf_addstr(out, item->value); + if (opts->separator) + strbuf_rtrim(out); + else + strbuf_addch(out, '\n'); + + need_separator = 1; + } + + } +} + /* * Parse trailers in "str", populating the trailer info and "head" * linked list structure. @@ -1144,13 +1203,25 @@ static void format_trailer_info(struct strbuf *out, } -void format_trailers_from_commit(struct strbuf *out, const char *msg, - const struct process_trailer_options *opts) +void format_trailers_from_commit(const char *msg, + const struct process_trailer_options *opts, + struct strbuf *out) { + LIST_HEAD(head); struct trailer_info info; - trailer_info_get(&info, msg, opts); - format_trailer_info(out, &info, msg, opts); + parse_trailers(&info, msg, &head, opts); + + /* 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); + } else + format_trailers(&head, opts, out); + + free_trailers(&head); trailer_info_release(&info); } diff --git a/trailer.h b/trailer.h index 0e4f0ece9b3..50f70556302 100644 --- a/trailer.h +++ b/trailer.h @@ -102,21 +102,16 @@ void trailer_info_release(struct trailer_info *info); void trailer_config_init(void); void free_trailers(struct list_head *trailers); -void format_trailers(FILE *outfile, struct list_head *head, - const struct process_trailer_options *opts); +void format_trailers(struct list_head *head, + const struct process_trailer_options *opts, + struct strbuf *out); /* - * Format the trailers from the commit msg "msg" into the strbuf "out". - * Note two caveats about "opts": - * - * - this is primarily a helper for pretty.c, and not - * all of the flags are supported. - * - * - this differs from format_trailers slightly in that we always format - * only the trailer block itself, even if the "only_trailers" option is not - * set. + * Convenience function to format the trailers from the commit msg "msg" into + * the strbuf "out". Reuses format_trailers internally. */ -void format_trailers_from_commit(struct strbuf *out, const char *msg, - const struct process_trailer_options *opts); +void format_trailers_from_commit(const char *msg, + const struct process_trailer_options *opts, + struct strbuf *out); /* * An interface for iterating over the trailers found in a particular commit From patchwork Wed Jan 10 06:51:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13515689 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 5DD61374C4 for ; Wed, 10 Jan 2024 06:51:34 +0000 (UTC) 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="N6zLws6Z" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-40e585ec8ebso609485e9.3 for ; Tue, 09 Jan 2024 22:51:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704869492; x=1705474292; 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=W+XtGcq4JfiHzuMRg7zikG1tVAdUtgFV7VlIuM6fVgo=; b=N6zLws6ZB/aAF5OGMmPJioBtA6w1z05SJo177bTplk/06D9WUaEMiJS9dEybFP5efY VhRL1Mbc1c28WpGmqXFcoppQsbaztEtdpAUxFT37krYdbY3Fk2BeL2vW3Gnn3qqdyClN 74RJk4UKiwWv/hkn/WriflIzDXc9AZ+LTGeo5VfAtGrTEQrBoe3cgZJt16Wpj3Fj423G QMeTuaom8x00FMMA2TF1DbmIqtWbAgq+C2y7B7YENf7I4Awmg1psl+0Zx3zSul9Dd8x7 Q9zkT2Bz4PX69vkbhBTcz+4pfDze7jrkXYAm/v+Jc9J/lgJpUwVLx1AGuDfzf2yC1UL/ R8/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704869492; x=1705474292; 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=W+XtGcq4JfiHzuMRg7zikG1tVAdUtgFV7VlIuM6fVgo=; b=kzF6tUJCmhL4Q/LoYqkm4e4A25S10RJHL7eczJn7lbjD2ayzdlM2k6YUfiOshSdUcT VgO0GIxvXe2+y6Lr/knrymQRqkALJbTRb+w9hUH/HKDTgPVxFyCqRgv/q6/PtxKK/EjL kypAyu49HHOGFP+ugT5G1rQHvdFcQuRAdjZn2fsSo+xpQFO83UZRM4W+3HMhZ3TWMCGC yy82w7VNvTUj6YyBLaG89foooH7vEWirZrZCEVsh0Z6MCYA1BsTFahrJdYF1PsWErKCG 3Vn/HwZPDiC30olA/ONcQBB6MkpXs/TWhHtKhbNj+chplyUiHrX/9c9QQVhOloJJA0Br nJUw== X-Gm-Message-State: AOJu0Yzp/+WdcBsRWiQvPluH8nPHQlk+7k8UuhuDrJaYsmNU/SQPKmZ4 P4MXej46kCBcQxshsG/NXKntdDIvnAQ= X-Google-Smtp-Source: AGHT+IH09q1Yd25U4WVrY+4Ftyh3n+L8s1DLJdZn89YwiauFgmMB/f778kroQM+AWmNOQ08eGcntmw== X-Received: by 2002:a05:600c:540a:b0:40e:5332:1934 with SMTP id he10-20020a05600c540a00b0040e53321934mr257442wmb.15.1704869492128; Tue, 09 Jan 2024 22:51:32 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id l17-20020a05600c1d1100b0040e4a2b36bfsm997377wms.22.2024.01.09.22.51.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 22:51:31 -0800 (PST) Message-ID: <8d86461475765ac04ad0ed207e46598eb90a45ba.1704869487.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 10 Jan 2024 06:51:20 +0000 Subject: [PATCH 04/10] trailer: delete obsolete formatting functions 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: Emily Shaffer , Junio C Hamano , Christian Couder , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Signed-off-by: Linus Arver --- trailer.c | 79 ------------------------------------------------------- 1 file changed, 79 deletions(-) diff --git a/trailer.c b/trailer.c index 315d90ee1ab..132f22b3dd7 100644 --- a/trailer.c +++ b/trailer.c @@ -144,24 +144,6 @@ static char last_non_space_char(const char *s) return '\0'; } -static void print_tok_val(FILE *outfile, const char *tok, const char *val) -{ - char c; - - if (!tok) { - fprintf(outfile, "%s\n", val); - return; - } - - c = last_non_space_char(tok); - if (!c) - return; - if (strchr(separators, c)) - fprintf(outfile, "%s%s\n", tok, val); - else - fprintf(outfile, "%s%c %s\n", tok, separators[0], val); -} - static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok) { struct trailer_item *new_item = xcalloc(1, sizeof(*new_item)); @@ -1142,67 +1124,6 @@ void trailer_info_release(struct trailer_info *info) free(info->trailers); } -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; - size_t i; - - /* 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); - return; - } - - for (i = 0; i < info->trailer_nr; i++) { - char *trailer = info->trailers[i]; - ssize_t separator_pos = find_separator(trailer, separators); - - if (separator_pos >= 1) { - struct strbuf tok = STRBUF_INIT; - struct strbuf val = STRBUF_INIT; - - parse_trailer(&tok, &val, NULL, trailer, separator_pos); - if (!opts->filter || opts->filter(&tok, opts->filter_data)) { - if (opts->unfold) - unfold_value(&val); - - if (opts->separator && out->len != origlen) - strbuf_addbuf(out, opts->separator); - if (!opts->value_only) - strbuf_addbuf(out, &tok); - if (!opts->key_only && !opts->value_only) { - if (opts->key_value_separator) - strbuf_addbuf(out, opts->key_value_separator); - else - strbuf_addstr(out, ": "); - } - if (!opts->key_only) - strbuf_addbuf(out, &val); - if (!opts->separator) - strbuf_addch(out, '\n'); - } - strbuf_release(&tok); - strbuf_release(&val); - - } else if (!opts->only_trailers) { - if (opts->separator && out->len != origlen) { - strbuf_addbuf(out, opts->separator); - } - strbuf_addstr(out, trailer); - if (opts->separator) { - strbuf_rtrim(out); - } - } - } - -} - void format_trailers_from_commit(const char *msg, const struct process_trailer_options *opts, struct strbuf *out) From patchwork Wed Jan 10 06:51:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13515690 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 3BB7238DF1 for ; Wed, 10 Jan 2024 06:51:35 +0000 (UTC) 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="J5OI6VWB" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-40d604b4b30so28166055e9.1 for ; Tue, 09 Jan 2024 22:51:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704869493; x=1705474293; 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=a7COct+8+WEna7hs7utinyouT7wLiyPcBuZMs7kZSJU=; b=J5OI6VWBeyH/QdYaysNzSlkjXQj88C4Iv+n1CqTngl3dtQVBodLgRPu/j/D3ccFtSV h5L9fvBRB9qoju/Ge20xConY6t2zPm9juCgrVKnVTC5mhKZjJr8g61UnA/sSM3ZTkevG cu9QMqOD1B/zyq+wvy/l+nJexmhy6Ym4OEMgwujNk7V7Ipo6cJ73QEdW4dm9aJhNqqTA 1kDjXnItswqdoOoHVZpgpg/XWXnm9EBjEhBb1uRleCwHWlSjw3tEJP2xjZxCb4je6bJF s32YNoUxOrQQz8GTjU6stQWPixn3JfbUQR67ILQigb+q6gTLrrcK2IJKltMz84xdwVNQ RvKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704869493; x=1705474293; 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=a7COct+8+WEna7hs7utinyouT7wLiyPcBuZMs7kZSJU=; b=u6lC3ct+6xlSIQyglNiq6ve0dZmWB9I3lnxzJAV32ldXiK/WL3DPJpHtIFpPpRL0d0 n9uKSR2wF1iZejP0hGe9qEFNZCPUioSTGBESLVxoX/eWVZZHg1ZH3aCHriSlYzfj391k E6FSGNjpHznQvqcgSX3a0RuGQ70FGFENVa0Dr7+fTufrj8xgjUdLpJqvwamPnrSCPZkD q9xo06h0U2535g/lb3NGarEG+74iws4ZEUGWCRlTi7EwoasAUjJELmfDgI59+0EOBU1r YTJAkTAPpowa3LULAsvDa2q6GDqwMDy0ho+e4rzOHfpCCXyfg/n+8alvw/O/zTll+JVm FULw== X-Gm-Message-State: AOJu0YzX95b/DxtUKQBUgKyW1jlIaDPkaeTGscQ9nJSeoyTfGpJfgzWr 2Z4ZDQRaGIoeX9S6WYAmNqXQWOR7KQQ= X-Google-Smtp-Source: AGHT+IG3KmD3J9I63f9Pl22PTkNYsZmZdHgNgetijtyDPPcxCucMJIdyCqIot8LPL3RpxAqTUdp9+Q== X-Received: by 2002:a05:600c:1551:b0:40e:4eed:dab8 with SMTP id f17-20020a05600c155100b0040e4eeddab8mr302021wmg.129.1704869493140; Tue, 09 Jan 2024 22:51:33 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u13-20020a05600c19cd00b0040e34ca648bsm1031528wmq.0.2024.01.09.22.51.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 22:51:32 -0800 (PST) Message-ID: In-Reply-To: References: Date: Wed, 10 Jan 2024 06:51:21 +0000 Subject: [PATCH 05/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: Emily Shaffer , Junio C Hamano , Christian Couder , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver This patch allows for the removal of "trailer_info_get()" from the trailer.h API, which will be in the next patch. 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). Also, teach the iterator about non-trailer lines, by adding a new field called "raw" to hold both trailer and non-trailer lines. This is necessary because a "trailer block" is a list of trailer lines of at least 25% trailers (see 146245063e (trailer: allow non-trailers in trailer block, 2016-10-21)), such that it may hold non-trailer lines. Signed-off-by: Linus Arver --- builtin/shortlog.c | 7 +++++-- sequencer.c | 35 +++++++++++++++-------------------- trailer.c | 20 ++++++++++++-------- trailer.h | 13 +++++++++++++ 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/builtin/shortlog.c b/builtin/shortlog.c index 1307ed2b88a..dc8fd5a5532 100644 --- a/builtin/shortlog.c +++ b/builtin/shortlog.c @@ -172,7 +172,7 @@ static void insert_records_from_trailers(struct shortlog *log, const char *oneline) { struct trailer_iterator iter; - const char *commit_buffer, *body; + const char *commit_buffer, *body, *value; struct strbuf ident = STRBUF_INIT; if (!log->trailers.nr) @@ -190,7 +190,10 @@ static void insert_records_from_trailers(struct shortlog *log, trailer_iterator_init(&iter, body); while (trailer_iterator_advance(&iter)) { - const char *value = iter.val.buf; + if (!iter.is_trailer) + continue; + + value = iter.val.buf; if (!string_list_has_string(&log->trailers, iter.key.buf)) continue; diff --git a/sequencer.c b/sequencer.c index 3cc88d8a800..d199869cda9 100644 --- a/sequencer.c +++ b/sequencer.c @@ -319,37 +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; - int found_sob = 0, found_sob_last = 0; - char saved_char; - - opts.no_divider = 1; + struct trailer_iterator iter; + size_t i = 0, found_sob = 0; + char saved_char = sb->buf[sb->len - ignore_footer]; if (ignore_footer) { - saved_char = sb->buf[sb->len - ignore_footer]; sb->buf[sb->len - ignore_footer] = '\0'; } - trailer_info_get(&info, sb->buf, &opts); + trailer_iterator_init(&iter, sb->buf); + while (trailer_iterator_advance(&iter)) { + i++; + if (sob && + iter.is_trailer && + !strncmp(iter.raw.buf, sob->buf, sob->len)) { + found_sob = i; + } + } + trailer_iterator_release(&iter); if (ignore_footer) sb->buf[sb->len - ignore_footer] = saved_char; - if (info.trailer_block_start == info.trailer_block_end) + if (!i) return 0; - 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; - } - - trailer_info_release(&info); - - if (found_sob_last) + if (found_sob == i) return 3; if (found_sob) return 2; diff --git a/trailer.c b/trailer.c index 132f22b3dd7..593717fd56c 100644 --- a/trailer.c +++ b/trailer.c @@ -1151,6 +1151,7 @@ void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; strbuf_init(&iter->key, 0); strbuf_init(&iter->val, 0); + strbuf_init(&iter->raw, 0); opts.no_divider = 1; trailer_info_get(&iter->internal.info, msg, &opts); iter->internal.cur = 0; @@ -1158,17 +1159,19 @@ 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 */ - + char *line; + int separator_pos; + if (iter->internal.cur < iter->internal.info.trailer_nr) { + line = iter->internal.info.trailers[iter->internal.cur++]; + separator_pos = find_separator(line, separators); + iter->is_trailer = (separator_pos > 0); + + strbuf_reset(&iter->raw); + strbuf_addstr(&iter->raw, line); strbuf_reset(&iter->key); strbuf_reset(&iter->val); parse_trailer(&iter->key, &iter->val, NULL, - trailer, separator_pos); + line, separator_pos); unfold_value(&iter->val); return 1; } @@ -1180,4 +1183,5 @@ void trailer_iterator_release(struct trailer_iterator *iter) trailer_info_release(&iter->internal.info); strbuf_release(&iter->val); strbuf_release(&iter->key); + strbuf_release(&iter->raw); } diff --git a/trailer.h b/trailer.h index 50f70556302..d50c9fd79b2 100644 --- a/trailer.h +++ b/trailer.h @@ -127,6 +127,19 @@ struct trailer_iterator { struct strbuf key; struct strbuf val; + /* + * Raw line (e.g., "foo: bar baz") before being parsed as a trailer + * key/val pair. This field can contain non-trailer lines because it's + * valid for a trailer block to contain such lines (i.e., we only + * require 25% of the lines in a trailer block to be trailer lines). + */ + struct strbuf raw; + + /* + * 1 if the raw line was parsed as a separate key/val pair. + */ + int is_trailer; + /* private */ struct { struct trailer_info info; From patchwork Wed Jan 10 06:51:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13515691 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D252039859 for ; Wed, 10 Jan 2024 06:51:35 +0000 (UTC) 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="Wp7yTZXj" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-40e4d515cdeso18695715e9.1 for ; Tue, 09 Jan 2024 22:51:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704869494; x=1705474294; 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=uEnUMG/yXVsvQZSyKlNAw4PZRUucAC+c6HSSXZX3PW8=; b=Wp7yTZXjxjVB2gQAMXPn7QgcRTU5a/F8LjBDXAUYjXQ9F6QuyrNmk8cFKp8jh2nOKk xOti50OMrpLN99WYe2m5a09SBsV6otDsRlEALrxBQBt1ma2l8YE7SHPCVFNk0rOJkkEA pibtNymigokI1Rs1I1Ad2UQSyaqewHu1Dte7J4ocLOtBGr9amrm3ZyqQYHYOmwBtmeZJ PivOOWbe3/O18+eqFxUNBtXudzM0MQMiAkEPv26AmBr+/IKpShBYZRsTcXjXEdg2CTcB 5M8BFGopo/it1unz75PAdRmNmw72m2GU39NMt9y9oRUVmEbSHxcYeI8mJik0ejQJEmzh J13w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704869494; x=1705474294; 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=uEnUMG/yXVsvQZSyKlNAw4PZRUucAC+c6HSSXZX3PW8=; b=fwZyWKz5jXVAPKfTAiUUQOw62xcqc4zdWnHihqVfYI9IVQjAhpRNnZVob03HX5RkjZ NVqQZ8/HCx4w1CEDBRyOyGc46yaj6gWCTSej2hFLg2i5PLSiVHqEMjCBIq7vMGlGUFlb A1IbkbXG5Nq1o02soG/SmFdTjV7X7jiypv9Bho2ak5Am2+hGX/KH3HUpn+UJ0d9kUQGy BVeIy+3AT5vKGZ9d1yeh8xuegACRkkZ/QURoiF2nN5Ubx/5lSJhlkOXNG+c/ioG9MfxY ppPG2PaGemTdVL5FVVXOXpmxlJ8OZXI3EKc4gNVcWrRoQhS/u8yruWSyAkKYPSNHU4EW SNXg== X-Gm-Message-State: AOJu0YzjEUmEYN2QOTWCIDNoWEgAi6Z4cGceRHABy1/OoE15PIUTO6mB B5EmBvzwhgumHQm1WFtUrGJP7FMHF4c= X-Google-Smtp-Source: AGHT+IGBl6TMnST2HCwf4lFkRu8HHSM4I/cY3Uo8YvAH74IZ7Lvm9wnpPRTEcuzo1E6idlD2U2Ae3A== X-Received: by 2002:a05:600c:520a:b0:40e:4239:506d with SMTP id fb10-20020a05600c520a00b0040e4239506dmr288377wmb.175.1704869493641; Tue, 09 Jan 2024 22:51:33 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id ek10-20020a05600c3eca00b0040d81ca11casm992599wmb.28.2024.01.09.22.51.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 22:51:33 -0800 (PST) Message-ID: <0cbe96421c7bf573e8ddc97b2a0aecc894095399.1704869487.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 10 Jan 2024 06:51:22 +0000 Subject: [PATCH 06/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: Emily Shaffer , Junio C Hamano , Christian Couder , 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). This change exposes some deficiencies in the API, mainly with regard to information about the location of the trailer block that was parsed. Expose new API functions to access this information (needed by builtin/interpret-trailers.c). The idea in this patch 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 the trailer.h file. In other words, trailer.h 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 Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 13 +-- trailer.c | 154 +++++++++++++++++++++++------------ trailer.h | 37 ++------- 3 files changed, 117 insertions(+), 87 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 934833a4645..0838a57e157 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -141,7 +141,7 @@ static void interpret_trailers(const char *file, LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; struct strbuf trailer_block = STRBUF_INIT; - struct trailer_info info; + struct trailer_info *info; FILE *outfile = stdout; trailer_config_init(); @@ -151,13 +151,13 @@ static void interpret_trailers(const char *file, if (opts->in_place) outfile = create_in_place_tempfile(file); - parse_trailers(&info, sb.buf, &head, opts); + info = parse_trailers(sb.buf, &head, opts); /* 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"); @@ -176,11 +176,12 @@ static void interpret_trailers(const char *file, strbuf_release(&trailer_block); free_trailers(&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); + 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 593717fd56c..0c66e2d3812 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; @@ -1025,20 +1046,72 @@ void format_trailers(struct list_head *head, } } +static struct trailer_info *trailer_info_new(void) +{ + struct trailer_info *info = xcalloc(1, sizeof(*info)); + return info; +} + +static struct trailer_info *trailer_info_get(const char *str, + const struct process_trailer_options *opts) +{ + 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. */ -void parse_trailers(struct trailer_info *info, - const char *str, - struct list_head *head, - const struct process_trailer_options *opts) +struct trailer_info *parse_trailers(const char *str, + struct list_head *head, + const struct process_trailer_options *opts) { + struct trailer_info *info; struct strbuf tok = STRBUF_INIT; struct strbuf val = STRBUF_INIT; size_t i; - trailer_info_get(info, str, opts); + info = trailer_info_get(str, opts); for (i = 0; i < info->trailer_nr; i++) { int separator_pos; @@ -1062,6 +1135,8 @@ void parse_trailers(struct trailer_info *info, strbuf_detach(&val, NULL)); } } + + return info; } void free_trailers(struct list_head *head) @@ -1073,47 +1148,19 @@ void free_trailers(struct list_head *head) } } -void trailer_info_get(struct trailer_info *info, const char *str, - const struct process_trailer_options *opts) +size_t trailer_block_start(struct trailer_info *info) { - 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); + return info->trailer_block_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++) { - 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); +size_t trailer_block_end(struct trailer_info *info) +{ + return info->trailer_block_end; +} - 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; +int blank_line_before_trailer_block(struct trailer_info *info) +{ + return info->blank_line_before_trailer; } void trailer_info_release(struct trailer_info *info) @@ -1122,6 +1169,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_from_commit(const char *msg, @@ -1129,31 +1177,31 @@ void format_trailers_from_commit(const char *msg, struct strbuf *out) { LIST_HEAD(head); - struct trailer_info info; - - parse_trailers(&info, msg, &head, opts); + struct trailer_info *info = parse_trailers(msg, &head, opts); /* 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(&head, opts, out); free_trailers(&head); - trailer_info_release(&info); + trailer_info_release(info); } void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) { struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; + struct trailer_info *internal = trailer_info_new(); strbuf_init(&iter->key, 0); strbuf_init(&iter->val, 0); strbuf_init(&iter->raw, 0); opts.no_divider = 1; - trailer_info_get(&iter->internal.info, msg, &opts); + iter->internal.info = internal; + iter->internal.info = trailer_info_get(msg, &opts); iter->internal.cur = 0; } @@ -1161,8 +1209,8 @@ int trailer_iterator_advance(struct trailer_iterator *iter) { char *line; int separator_pos; - if (iter->internal.cur < iter->internal.info.trailer_nr) { - line = iter->internal.info.trailers[iter->internal.cur++]; + if (iter->internal.cur < iter->internal.info->trailer_nr) { + line = iter->internal.info->trailers[iter->internal.cur++]; separator_pos = find_separator(line, separators); iter->is_trailer = (separator_pos > 0); @@ -1180,7 +1228,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); strbuf_release(&iter->raw); diff --git a/trailer.h b/trailer.h index d50c9fd79b2..b06da1a7d3a 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. @@ -89,13 +70,13 @@ 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(struct trailer_info *info, - const char *str, - struct list_head *head, - const struct process_trailer_options *opts); +struct trailer_info *parse_trailers(const char *str, + struct list_head *head, + const struct process_trailer_options *opts); -void trailer_info_get(struct trailer_info *info, const char *str, - const struct process_trailer_options *opts); +size_t trailer_block_start(struct trailer_info *info); +size_t trailer_block_end(struct trailer_info *info); +int blank_line_before_trailer_block(struct trailer_info *info); void trailer_info_release(struct trailer_info *info); @@ -142,7 +123,7 @@ struct trailer_iterator { /* private */ struct { - struct trailer_info info; + struct trailer_info *info; size_t cur; } internal; }; From patchwork Wed Jan 10 06:51:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13515692 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 9521939FC9 for ; Wed, 10 Jan 2024 06:51:36 +0000 (UTC) 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="dzkGpp7I" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-40e461c1f5bso32560045e9.3 for ; Tue, 09 Jan 2024 22:51:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704869494; x=1705474294; 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=OqlRNHRCuFB8NQk2NyQLb8saiGR6AYUgzXfLJm6t8a8=; b=dzkGpp7ImvhTTz0eYfAqOHaVPEn4Et54UroZyKaEWBwzLEGQLFPbv5L72ehIOapU/t jfxo0nlUntqJrlCTFs7FGqKkR38J9MnxPyveplFMOAihYZdg0h2/iXBg/Wi96U+VsAAw K8APg/SioPzZ2McSjlofheWUMI0tHMRFxrH9/HhFrUCYNbR/+LlzjqXO8wx/Dmv4bEIp ZzaYA9BuI+4pu2H6DxIc7dFVVPa8L+U15zWE16tu3amB8T7iT5AIv6m605YlE/8ko/oB dkxHl1ok3ONiQOY0/ziH9HRuAnpIDJDVaw58tYsXthgvMJAAPqwr1k0pw4cScL/l47+k glcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704869494; x=1705474294; 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=OqlRNHRCuFB8NQk2NyQLb8saiGR6AYUgzXfLJm6t8a8=; b=eG/V5hu5v4P3wv+2cNKshEmccdODgFQ+KF2+XWuRZIeWJqXH/hXb+bpTtzbLtQE2/f lTQtXN1i3WNgZhS7pD+O/YNLX+rU51OJN9R4AJcvBXIZ0LSXVCEiH2QVuSlg5pIREW2k QkSPOQqOmV3jvDlajRQh4TFvAvN5LZsRV20NBc13QiIbosz5SDMjnuI64dnTwAFMbIah w7fmXGlC+78dhbtTML+V+MLcFcHewYut/Kk9hiU451S+8xg1ZyF9cWzf3X7VTzuSwzfR bdgqNjPSSSVG8pCpDqPmHnQBPoS33qhx9p/AwurgUbaP9OKdq4E7RR1T21Bbc+NC+UE4 kkPQ== X-Gm-Message-State: AOJu0YzPOzBtzvkVVhyAQR55OqrAKVTISJJucoyukphDj0S53jL9cNBz 025Jh1HB/r+WKlliwFTrhHYVsTMaYEo= X-Google-Smtp-Source: AGHT+IFlNb8+k3NZetxf6p6NL6jsMJqHBHX4TcDqM34LqM9FP015t13GsWUyEOp/1LBR9WriChX/iw== X-Received: by 2002:a05:600c:3548:b0:40e:3d34:3eb6 with SMTP id i8-20020a05600c354800b0040e3d343eb6mr252915wmq.0.1704869494168; Tue, 09 Jan 2024 22:51:34 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id fl14-20020a05600c0b8e00b0040e3488f16dsm1006079wmb.12.2024.01.09.22.51.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 22:51:34 -0800 (PST) Message-ID: <9183990583f9a591c79301a20fa327462bb50cf9.1704869487.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 10 Jan 2024 06:51:23 +0000 Subject: [PATCH 07/10] trailer: spread usage of "trailer_block" language 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: Emily Shaffer , Junio C Hamano , Christian Couder , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Deprecate the "trailer_info" struct name and replace it with "trailer_block". The main reason is to help readability, because "trailer_info" on the surface sounds like it's about a single trailer when in reality it is a collection of contiguous lines, at least 25% of which are trailers. Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 26 +++++----- trailer.c | 99 ++++++++++++++++++------------------ trailer.h | 18 +++---- 3 files changed, 71 insertions(+), 72 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 0838a57e157..42d9ca07a56 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -140,8 +140,8 @@ static void interpret_trailers(const char *file, { LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; - struct strbuf trailer_block = STRBUF_INIT; - struct trailer_info *info; + struct strbuf tb = STRBUF_INIT; + struct trailer_block *trailer_block; FILE *outfile = stdout; trailer_config_init(); @@ -151,13 +151,13 @@ static void interpret_trailers(const char *file, if (opts->in_place) outfile = create_in_place_tempfile(file); - info = parse_trailers(sb.buf, &head, opts); + trailer_block = parse_trailers(sb.buf, opts, &head); - /* Print the lines before the trailers */ + /* Print the lines before the trailer block */ if (!opts->only_trailers) - fwrite(sb.buf, 1, trailer_block_start(info), outfile); + fwrite(sb.buf, 1, trailer_block_start(trailer_block), outfile); - if (!opts->only_trailers && !blank_line_before_trailer_block(info)) + if (!opts->only_trailers && !blank_line_before_trailer_block(trailer_block)) fprintf(outfile, "\n"); @@ -171,17 +171,17 @@ static void interpret_trailers(const char *file, } /* Print trailer block. */ - format_trailers(&head, opts, &trailer_block); - fwrite(trailer_block.buf, 1, trailer_block.len, outfile); - strbuf_release(&trailer_block); + format_trailers(&head, opts, &tb); + fwrite(tb.buf, 1, tb.len, outfile); + strbuf_release(&tb); free_trailers(&head); - /* Print the lines after the trailers as is */ + /* Print the lines after the trailer block as is */ if (!opts->only_trailers) - fwrite(sb.buf + trailer_block_end(info), 1, sb.len - trailer_block_end(info), outfile); - - trailer_info_release(info); + fwrite(sb.buf + trailer_block_end(trailer_block), + 1, sb.len - trailer_block_end(trailer_block), outfile); + trailer_block_release(trailer_block); if (opts->in_place) if (rename_tempfile(&trailers_tempfile, file)) diff --git a/trailer.c b/trailer.c index 0c66e2d3812..360e76376b8 100644 --- a/trailer.c +++ b/trailer.c @@ -11,19 +11,20 @@ * Copyright (c) 2013, 2014 Christian Couder */ -struct trailer_info { +struct trailer_block { /* * True if there is a blank line before the location pointed to by - * trailer_block_start. + * "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()). + * The locations of the start and end positions of the trailer block + * found, as offsets from the beginning of the source text from which + * this trailer block was parsed. If no trailer block is found, these + * are both set to 0. */ - size_t trailer_block_start, trailer_block_end; + size_t start, end; /* * Array of trailers found. @@ -1046,16 +1047,16 @@ void format_trailers(struct list_head *head, } } -static struct trailer_info *trailer_info_new(void) +static struct trailer_block *trailer_block_new(void) { - struct trailer_info *info = xcalloc(1, sizeof(*info)); - return info; + struct trailer_block *trailer_block = xcalloc(1, sizeof(*trailer_block)); + return trailer_block; } -static struct trailer_info *trailer_info_get(const char *str, - const struct process_trailer_options *opts) +static struct trailer_block *trailer_block_get(const char *str, + const struct process_trailer_options *opts) { - struct trailer_info *info = trailer_info_new(); + struct trailer_block *trailer_block = trailer_block_new(); size_t end_of_log_message = 0, trailer_block_start = 0; struct strbuf **trailer_lines, **ptr; char **trailer_strings = NULL; @@ -1088,34 +1089,34 @@ static struct trailer_info *trailer_info_get(const char *str, } 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; + trailer_block->blank_line_before_trailer = ends_with_blank_line(str, + trailer_block_start); + trailer_block->start = trailer_block_start; + trailer_block->end = end_of_log_message; + trailer_block->trailers = trailer_strings; + trailer_block->trailer_nr = nr; - return info; + return trailer_block; } /* - * Parse trailers in "str", populating the trailer info and "head" - * linked list structure. + * Parse trailers in "str", populating the trailer_block info and "head" linked + * list structure. */ -struct trailer_info *parse_trailers(const char *str, - struct list_head *head, - const struct process_trailer_options *opts) +struct trailer_block *parse_trailers(const char *str, + const struct process_trailer_options *opts, + struct list_head *head) { - struct trailer_info *info; + struct trailer_block *trailer_block; struct strbuf tok = STRBUF_INIT; struct strbuf val = STRBUF_INIT; size_t i; - info = trailer_info_get(str, opts); + trailer_block = trailer_block_get(str, opts); - for (i = 0; i < info->trailer_nr; i++) { + for (i = 0; i < trailer_block->trailer_nr; i++) { int separator_pos; - char *trailer = info->trailers[i]; + char *trailer = trailer_block->trailers[i]; if (trailer[0] == comment_line_char) continue; separator_pos = find_separator(trailer, separators); @@ -1136,7 +1137,7 @@ struct trailer_info *parse_trailers(const char *str, } } - return info; + return trailer_block; } void free_trailers(struct list_head *head) @@ -1148,28 +1149,28 @@ void free_trailers(struct list_head *head) } } -size_t trailer_block_start(struct trailer_info *info) +size_t trailer_block_start(struct trailer_block *trailer_block) { - return info->trailer_block_start; + return trailer_block->start; } -size_t trailer_block_end(struct trailer_info *info) +size_t trailer_block_end(struct trailer_block *trailer_block) { - return info->trailer_block_end; + return trailer_block->end; } -int blank_line_before_trailer_block(struct trailer_info *info) +int blank_line_before_trailer_block(struct trailer_block *trailer_block) { - return info->blank_line_before_trailer; + return trailer_block->blank_line_before_trailer; } -void trailer_info_release(struct trailer_info *info) +void trailer_block_release(struct trailer_block *trailer_block) { size_t i; - for (i = 0; i < info->trailer_nr; i++) - free(info->trailers[i]); - free(info->trailers); - free(info); + for (i = 0; i < trailer_block->trailer_nr; i++) + free(trailer_block->trailers[i]); + free(trailer_block->trailers); + free(trailer_block); } void format_trailers_from_commit(const char *msg, @@ -1177,31 +1178,29 @@ void format_trailers_from_commit(const char *msg, struct strbuf *out) { LIST_HEAD(head); - struct trailer_info *info = parse_trailers(msg, &head, opts); + struct trailer_block *trailer_block = parse_trailers(msg, opts, &head); /* 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 + trailer_block->start, + trailer_block->end - trailer_block->start); } else format_trailers(&head, opts, out); free_trailers(&head); - trailer_info_release(info); + trailer_block_release(trailer_block); } void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) { struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; - struct trailer_info *internal = trailer_info_new(); strbuf_init(&iter->key, 0); strbuf_init(&iter->val, 0); strbuf_init(&iter->raw, 0); opts.no_divider = 1; - iter->internal.info = internal; - iter->internal.info = trailer_info_get(msg, &opts); + iter->internal.trailer_block = trailer_block_get(msg, &opts); iter->internal.cur = 0; } @@ -1209,8 +1208,8 @@ int trailer_iterator_advance(struct trailer_iterator *iter) { char *line; int separator_pos; - if (iter->internal.cur < iter->internal.info->trailer_nr) { - line = iter->internal.info->trailers[iter->internal.cur++]; + if (iter->internal.cur < iter->internal.trailer_block->trailer_nr) { + line = iter->internal.trailer_block->trailers[iter->internal.cur++]; separator_pos = find_separator(line, separators); iter->is_trailer = (separator_pos > 0); @@ -1228,7 +1227,7 @@ int trailer_iterator_advance(struct trailer_iterator *iter) void trailer_iterator_release(struct trailer_iterator *iter) { - trailer_info_release(iter->internal.info); + trailer_block_release(iter->internal.trailer_block); strbuf_release(&iter->val); strbuf_release(&iter->key); strbuf_release(&iter->raw); diff --git a/trailer.h b/trailer.h index b06da1a7d3a..5c8503ade78 100644 --- a/trailer.h +++ b/trailer.h @@ -4,7 +4,7 @@ #include "list.h" #include "strbuf.h" -struct trailer_info; +struct trailer_block; enum trailer_where { WHERE_DEFAULT, @@ -70,15 +70,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); -struct trailer_info *parse_trailers(const char *str, - struct list_head *head, - const struct process_trailer_options *opts); +struct trailer_block *parse_trailers(const char *str, + const struct process_trailer_options *opts, + struct list_head *head); -size_t trailer_block_start(struct trailer_info *info); -size_t trailer_block_end(struct trailer_info *info); -int blank_line_before_trailer_block(struct trailer_info *info); +size_t trailer_block_start(struct trailer_block *trailer_block); +size_t trailer_block_end(struct trailer_block *trailer_block); +int blank_line_before_trailer_block(struct trailer_block *trailer_block); -void trailer_info_release(struct trailer_info *info); +void trailer_block_release(struct trailer_block *trailer_block); void trailer_config_init(void); void free_trailers(struct list_head *trailers); @@ -123,7 +123,7 @@ struct trailer_iterator { /* private */ struct { - struct trailer_info *info; + struct trailer_block *trailer_block; size_t cur; } internal; }; From patchwork Wed Jan 10 06:51:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13515693 Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 B01F13A1BF for ; Wed, 10 Jan 2024 06:51:37 +0000 (UTC) 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="fmuz/iNT" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-40e5508ecb9so8381215e9.3 for ; Tue, 09 Jan 2024 22:51:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704869495; x=1705474295; 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=smVvMc5pEUpKMKNz7Dao5GDVMmWhSganQOLMSnlURps=; b=fmuz/iNTHpaqZ8q4dE1yOZZJSlw9qn+bKoD4jIlnUNnbGeYJjXtl/VR5MvDYEy/TZd Go2zdrxWMhMKM0wCG3aP1r7Y3/aEdFVtrAkAok/8A90jSKTcZ3aoYuHHJpiC094b3+B2 ODPUOVr4n9YVZNU9uujvTVPmpTG54uYSq8+RHxbaGp9ULzI3qwDkeP0ZcTCTocuAs+dz kjHk/6M0SUOu3wqCB1uStFE2OYW3wV9hrjWCd0mdohTv9YwnpoLmrY2UofHu315wCQeV O6ordQ14x0u7s/hYACCcd/mdGJKHKQKi2RJ6kAjJfBpQZkrN0S8IzTGVB0o9jN5oLfkq OT6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704869495; x=1705474295; 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=smVvMc5pEUpKMKNz7Dao5GDVMmWhSganQOLMSnlURps=; b=u1UsttHY4nwCWxh1HQBSMYry1AM6rxxcZdOx6l237KUy6tMdLjZwPId11hmZouAZC0 GBit00gs5Gr0ntyRUBx9PtA1ow2C8tz0M8zDCyP7kS+dv5mPxKOj1vM/xvQM2C9Q8M8e T0+HR90oWUo9DnUnnGkoKkXYG6fpHyZWsM1sxCCQKBBAuvQrxhtlbKV4YY40VrulZ5+i K5G7hDRYZFpMAj3HfZp7xGwNuifVwj22fHxnLHxyOH0C5LCywForCaOoUtB0FnsoIRbL d4nk0uBY5sJrtEFVYlrEXlREvinRwO6r8RaQjKHyMb4295ltXcuPkFzMuDEdCZDCYVYm +9EQ== X-Gm-Message-State: AOJu0YxB3o378nSOXz6ZvaMmywBSSXGdzdUDP9d9OeLvF2Wmcer/G5xS XjD5roKaQ0G3il9vwGEqwrNbgWA0VRE= X-Google-Smtp-Source: AGHT+IFkPLGfI250qUsiplT/INksDbtUacUabUQTUt0M1JthoXVsU0UP/54p5yDm//tKXve/KgV3Ow== X-Received: by 2002:a7b:c414:0:b0:40e:4a88:2de2 with SMTP id k20-20020a7bc414000000b0040e4a882de2mr228850wmi.155.1704869495285; Tue, 09 Jan 2024 22:51:35 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j28-20020a05600c1c1c00b0040d91912f2csm1019038wms.1.2024.01.09.22.51.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 22:51:34 -0800 (PST) Message-ID: <406725df46a81f485d7a74c11488e625d3026dc5.1704869487.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 10 Jan 2024 06:51:24 +0000 Subject: [PATCH 08/10] trailer: prepare to move parse_trailers_from_command_line_args() to builtin 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: Emily Shaffer , Junio C Hamano , Christian Couder , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver Expose more functions in the trailer.h API, in preparation for moving out parse_trailers_from_command_line_args() to interpret-trailer.c, because the trailer API should not be concerned with command line arguments (as it has nothing to do with trailers themselves). The interpret-trailers builtin is the only user of the above function. Signed-off-by: Linus Arver --- trailer.c | 66 +++++++++++++++++++++++++++---------------------------- trailer.h | 10 +++++++++ 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/trailer.c b/trailer.c index 360e76376b8..e2d541372a3 100644 --- a/trailer.c +++ b/trailer.c @@ -33,7 +33,7 @@ struct trailer_block { size_t trailer_nr; }; -struct conf_info { +struct trailer_conf { char *name; char *key; char *command; @@ -43,7 +43,7 @@ struct conf_info { enum trailer_if_missing if_missing; }; -static struct conf_info default_conf_info; +static struct trailer_conf default_trailer_conf; struct trailer_item { struct list_head list; @@ -59,7 +59,7 @@ struct arg_item { struct list_head list; char *token; char *value; - struct conf_info conf; + struct trailer_conf conf; }; static LIST_HEAD(conf_head); @@ -210,7 +210,7 @@ static int check_if_different(struct trailer_item *in_tok, return 1; } -static char *apply_command(struct conf_info *conf, const char *arg) +static char *apply_command(struct trailer_conf *conf, const char *arg) { struct strbuf cmd = STRBUF_INIT; struct strbuf buf = STRBUF_INIT; @@ -424,7 +424,8 @@ int trailer_set_if_missing(enum trailer_if_missing *item, const char *value) return 0; } -static void duplicate_conf(struct conf_info *dst, const struct conf_info *src) +void duplicate_trailer_conf(struct trailer_conf *dst, + const struct trailer_conf *src) { *dst = *src; dst->name = xstrdup_or_null(src->name); @@ -447,7 +448,7 @@ static struct arg_item *get_conf_item(const char *name) /* Item does not already exists, create it */ CALLOC_ARRAY(item, 1); - duplicate_conf(&item->conf, &default_conf_info); + duplicate_trailer_conf(&item->conf, &default_trailer_conf); item->conf.name = xstrdup(name); list_add_tail(&item->list, &conf_head); @@ -482,17 +483,17 @@ static int git_trailer_default_config(const char *conf_key, const char *value, variable_name = strrchr(trailer_item, '.'); if (!variable_name) { if (!strcmp(trailer_item, "where")) { - if (trailer_set_where(&default_conf_info.where, + if (trailer_set_where(&default_trailer_conf.where, value) < 0) warning(_("unknown value '%s' for key '%s'"), value, conf_key); } else if (!strcmp(trailer_item, "ifexists")) { - if (trailer_set_if_exists(&default_conf_info.if_exists, + if (trailer_set_if_exists(&default_trailer_conf.if_exists, value) < 0) warning(_("unknown value '%s' for key '%s'"), value, conf_key); } else if (!strcmp(trailer_item, "ifmissing")) { - if (trailer_set_if_missing(&default_conf_info.if_missing, + if (trailer_set_if_missing(&default_trailer_conf.if_missing, value) < 0) warning(_("unknown value '%s' for key '%s'"), value, conf_key); @@ -511,7 +512,7 @@ static int git_trailer_config(const char *conf_key, const char *value, { const char *trailer_item, *variable_name; struct arg_item *item; - struct conf_info *conf; + struct trailer_conf *conf; char *name = NULL; enum trailer_info_type type; int i; @@ -585,9 +586,9 @@ void trailer_config_init(void) return; /* Default config must be setup first */ - default_conf_info.where = WHERE_END; - default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR; - default_conf_info.if_missing = MISSING_ADD; + default_trailer_conf.where = WHERE_END; + default_trailer_conf.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR; + default_trailer_conf.if_missing = MISSING_ADD; git_config(git_trailer_default_config, NULL); git_config(git_trailer_config, NULL); configured = 1; @@ -620,7 +621,7 @@ static int token_matches_item(const char *tok, struct arg_item *item, size_t tok * distinguished from the non-well-formed-line case (in which this function * returns -1) because some callers of this function need such a distinction. */ -static ssize_t find_separator(const char *line, const char *separators) +ssize_t find_separator(const char *line, const char *separators) { int whitespace_found = 0; const char *c; @@ -645,28 +646,28 @@ static ssize_t find_separator(const char *line, const char *separators) * * If separator_pos is -1, interpret the whole trailer as a token. */ -static void parse_trailer(struct strbuf *tok, struct strbuf *val, - const struct conf_info **conf, const char *trailer, - ssize_t separator_pos) +void parse_trailer(const char *line, ssize_t separator_pos, + struct strbuf *tok, struct strbuf *val, + const struct trailer_conf **conf) { struct arg_item *item; size_t tok_len; struct list_head *pos; if (separator_pos != -1) { - strbuf_add(tok, trailer, separator_pos); + strbuf_add(tok, line, separator_pos); strbuf_trim(tok); - strbuf_addstr(val, trailer + separator_pos + 1); + strbuf_addstr(val, line + separator_pos + 1); strbuf_trim(val); } else { - strbuf_addstr(tok, trailer); + strbuf_addstr(tok, line); strbuf_trim(tok); } /* Lookup if the token matches something in the config */ tok_len = token_len_without_separator(tok->buf, tok->len); if (conf) - *conf = &default_conf_info; + *conf = &default_trailer_conf; list_for_each(pos, &conf_head) { item = list_entry(pos, struct arg_item, list); if (token_matches_item(tok->buf, item, tok_len)) { @@ -691,13 +692,13 @@ static struct trailer_item *add_trailer_item(struct list_head *head, char *tok, } static void add_arg_item(struct list_head *arg_head, char *tok, char *val, - const struct conf_info *conf, + const struct trailer_conf *conf, const struct new_trailer_item *new_trailer_item) { struct arg_item *new_item = xcalloc(1, sizeof(*new_item)); new_item->token = tok; new_item->value = val; - duplicate_conf(&new_item->conf, conf); + duplicate_trailer_conf(&new_item->conf, conf); if (new_trailer_item) { if (new_trailer_item->where != WHERE_DEFAULT) new_item->conf.where = new_trailer_item->where; @@ -730,7 +731,7 @@ void parse_trailers_from_command_line_args(struct list_head *arg_head, { struct strbuf tok = STRBUF_INIT; struct strbuf val = STRBUF_INIT; - const struct conf_info *conf; + const struct trailer_conf *conf; struct list_head *pos; /* @@ -753,8 +754,7 @@ void parse_trailers_from_command_line_args(struct list_head *arg_head, (int) sb.len, sb.buf); strbuf_release(&sb); } else { - parse_trailer(&tok, &val, &conf, tr->text, - separator_pos); + parse_trailer(tr->text, separator_pos, &tok, &val, &conf); add_arg_item(arg_head, strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL), @@ -1116,20 +1116,19 @@ struct trailer_block *parse_trailers(const char *str, for (i = 0; i < trailer_block->trailer_nr; i++) { int separator_pos; - char *trailer = trailer_block->trailers[i]; - if (trailer[0] == comment_line_char) + char *line = trailer_block->trailers[i]; + if (line[0] == comment_line_char) continue; - separator_pos = find_separator(trailer, separators); + separator_pos = find_separator(line, separators); if (separator_pos >= 1) { - parse_trailer(&tok, &val, NULL, trailer, - separator_pos); + parse_trailer(line, separator_pos, &tok, &val, NULL); if (opts->unfold) unfold_value(&val); add_trailer_item(head, strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL)); } else if (!opts->only_trailers) { - strbuf_addstr(&val, trailer); + strbuf_addstr(&val, line); strbuf_strip_suffix(&val, "\n"); add_trailer_item(head, NULL, @@ -1217,8 +1216,7 @@ int trailer_iterator_advance(struct trailer_iterator *iter) strbuf_addstr(&iter->raw, line); strbuf_reset(&iter->key); strbuf_reset(&iter->val); - parse_trailer(&iter->key, &iter->val, NULL, - line, separator_pos); + parse_trailer(line, separator_pos, &iter->key, &iter->val, NULL); unfold_value(&iter->val); return 1; } diff --git a/trailer.h b/trailer.h index 5c8503ade78..fe49a9bad52 100644 --- a/trailer.h +++ b/trailer.h @@ -5,6 +5,7 @@ #include "strbuf.h" struct trailer_block; +struct trailer_conf; enum trailer_where { WHERE_DEFAULT, @@ -45,6 +46,9 @@ struct new_trailer_item { enum trailer_if_missing if_missing; }; +void duplicate_trailer_conf(struct trailer_conf *dst, + const struct trailer_conf *src); + struct process_trailer_options { int in_place; int trim_empty; @@ -70,6 +74,12 @@ 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); +ssize_t find_separator(const char *line, const char *separators); + +void parse_trailer(const char *line, ssize_t separator_pos, + struct strbuf *tok, struct strbuf *val, + const struct trailer_conf **conf); + struct trailer_block *parse_trailers(const char *str, const struct process_trailer_options *opts, struct list_head *head); From patchwork Wed Jan 10 06:51:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13515694 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66D223A8DF for ; Wed, 10 Jan 2024 06:51:38 +0000 (UTC) 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="J75vVYrA" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-3374eb61cbcso3520832f8f.0 for ; Tue, 09 Jan 2024 22:51:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704869496; x=1705474296; 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=/aZhmT3uau0XpsAwhvnxAek5hhs7aYCkKYZurr84SVI=; b=J75vVYrA2EY7tC+YNjzK/PK0DxeFqw44EQU8dlStZnStYk3/xtIlbyLpzfp7RDzf0R KQ7VASIJYwF09+9MNAq6sBpXahckrwG7P4I+7aU2c3ylvbZHWUw6QQz4DTJgEknERN1V csqvyaTetKQhoV6NKHvNcCeOl79gjvDuqkkIjqQKiwiuBgV19vcMIJyngAMsnSdOMFXr 24WgJ6ageagrozu3BvFM7TK3EqscYhrwJ8v1t6VCEZwPUIfecH24s66RmB/xMxnUDOqb ZOQ1ogYUKRg71ktUImaxd+aGXRHHHrqbxpNr+72oYZ6DJMdRob62Zo9PkP+ELENRFXDb LL+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704869496; x=1705474296; 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=/aZhmT3uau0XpsAwhvnxAek5hhs7aYCkKYZurr84SVI=; b=rLMdBIDgMkfLQDvIyb7mb4yOP2iyDasZa73XclfQSu3Jbl1+HInY+Bc6nV0KacX7ME eT3s97j+7GzFw9yt+Wc+vtLu3bYHGAj90tp1PXCP6fyU1oCtvTfsIP3soKdSyJEE/Nd5 HLVb8vQGdgMjlgLy0hhMp6/UGxeki+Fv7SXX4+wkaXcXigxAWvsJjlRZp2iYN05ekDTN Hz0qStBPricbwQkE00kcCqk88cwJS9QsqtNhbrh/GEl07KjI+lPm8DOVpNyNGMtM9rgy ZOAJLE8K/OAgPZ6xaBwx0jUmQDDlHtoxMhISw1ncMOKsmvvCl6mArk+y7D8RNMlnpt0o B26w== X-Gm-Message-State: AOJu0YzyE0y1EEEcSia5RoC6ydenoCEoKXHRVwNB898muHeXKwPOoP0h tDHHm3nmDIfTj7YzlSJGRJGoWPP2LZo= X-Google-Smtp-Source: AGHT+IHHUmmAKkUxjINimCywulK53Kqyz6s3+tnO/NOGngJ53ZFlvKqXbI32UD7oAKHru+8UvQX3Kw== X-Received: by 2002:a05:600c:a41:b0:40e:4133:44da with SMTP id c1-20020a05600c0a4100b0040e413344damr268378wmq.26.1704869495763; Tue, 09 Jan 2024 22:51:35 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id g14-20020a05600c4ece00b0040d5f3ef2a2sm1001065wmq.16.2024.01.09.22.51.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 22:51:35 -0800 (PST) Message-ID: <8a99d0fca21eca41d62dcd682c8b4ae545633bf7.1704869487.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 10 Jan 2024 06:51:25 +0000 Subject: [PATCH 09/10] trailer: move arg handling to interpret-trailers.c 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: Emily Shaffer , Junio C Hamano , Christian Couder , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver We don't move the "arg_item" struct to interpret-trailers.c, because it is now a struct that contains information about trailers that should be injected into the input text's own trailers. We will rename this language as such in a follow-up patch to keep the diff here small. Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 88 ++++++++++++++++++++++-------------- trailer.c | 63 +++++++++++++++++++------- trailer.h | 12 +++++ 3 files changed, 113 insertions(+), 50 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 42d9ca07a56..4da4eac3b46 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -45,23 +45,17 @@ static int option_parse_if_missing(const struct option *opt, return trailer_set_if_missing(opt->value, arg); } -static void new_trailers_clear(struct list_head *trailers) -{ - struct list_head *pos, *tmp; - struct new_trailer_item *item; - - list_for_each_safe(pos, tmp, trailers) { - item = list_entry(pos, struct new_trailer_item, list); - list_del(pos); - free(item); - } -} +static char *cl_separators; static int option_parse_trailer(const struct option *opt, const char *arg, int unset) { struct list_head *trailers = opt->value; - struct new_trailer_item *item; + struct strbuf tok = STRBUF_INIT; + struct strbuf val = STRBUF_INIT; + const struct trailer_conf *conf; + struct trailer_conf *conf_current = new_trailer_conf(); + ssize_t separator_pos; if (unset) { new_trailers_clear(trailers); @@ -71,12 +65,31 @@ static int option_parse_trailer(const struct option *opt, if (!arg) return -1; - item = xmalloc(sizeof(*item)); - item->text = arg; - item->where = where; - item->if_exists = if_exists; - item->if_missing = if_missing; - list_add_tail(&item->list, trailers); + separator_pos = find_separator(arg, cl_separators); + if (separator_pos) { + parse_trailer(arg, separator_pos, &tok, &val, &conf); + duplicate_trailer_conf(conf_current, conf); + + /* + * Override conf_current with settings specified via CLI flags. + */ + trailer_conf_set(where, if_exists, if_missing, conf_current); + + add_arg_item(strbuf_detach(&tok, NULL), + strbuf_detach(&val, NULL), + conf_current, + trailers); + } else { + struct strbuf sb = STRBUF_INIT; + strbuf_addstr(&sb, arg); + strbuf_trim(&sb); + error(_("empty trailer token in trailer '%.*s'"), + (int) sb.len, sb.buf); + strbuf_release(&sb); + } + + free(conf_current); + return 0; } @@ -136,7 +149,7 @@ static void read_input_file(struct strbuf *sb, const char *file) static void interpret_trailers(const char *file, const struct process_trailer_options *opts, - struct list_head *new_trailer_head) + struct list_head *arg_trailers) { LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; @@ -144,8 +157,6 @@ static void interpret_trailers(const char *file, struct trailer_block *trailer_block; FILE *outfile = stdout; - trailer_config_init(); - read_input_file(&sb, file); if (opts->in_place) @@ -162,12 +173,7 @@ static void interpret_trailers(const char *file, if (!opts->only_input) { - LIST_HEAD(config_head); - LIST_HEAD(arg_head); - parse_trailers_from_config(&config_head); - parse_trailers_from_command_line_args(&arg_head, new_trailer_head); - list_splice(&config_head, &arg_head); - process_trailers_lists(&head, &arg_head); + process_trailers_lists(&head, arg_trailers); } /* Print trailer block. */ @@ -193,7 +199,8 @@ static void interpret_trailers(const char *file, int cmd_interpret_trailers(int argc, const char **argv, const char *prefix) { struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; - LIST_HEAD(trailers); + LIST_HEAD(configured_trailers); + LIST_HEAD(arg_trailers); struct option options[] = { OPT_BOOL(0, "in-place", &opts.in_place, N_("edit files in place")), @@ -212,33 +219,48 @@ int cmd_interpret_trailers(int argc, const char **argv, const char *prefix) OPT_CALLBACK_F(0, "parse", &opts, NULL, N_("alias for --only-trailers --only-input --unfold"), PARSE_OPT_NOARG | PARSE_OPT_NONEG, parse_opt_parse), OPT_BOOL(0, "no-divider", &opts.no_divider, N_("do not treat \"---\" as the end of input")), - OPT_CALLBACK(0, "trailer", &trailers, N_("trailer"), + OPT_CALLBACK(0, "trailer", &arg_trailers, N_("trailer"), N_("trailer(s) to add"), option_parse_trailer), OPT_END() }; git_config(git_default_config, NULL); + trailer_config_init(); + + if (!opts.only_input) { + parse_trailers_from_config(&configured_trailers); + } + + /* + * In command-line arguments, '=' is accepted (in addition to the + * separators that are defined). + */ + cl_separators = xstrfmt("=%s", default_separators()); argc = parse_options(argc, argv, prefix, options, git_interpret_trailers_usage, 0); - if (opts.only_input && !list_empty(&trailers)) + free(cl_separators); + + if (opts.only_input && !list_empty(&arg_trailers)) usage_msg_opt( _("--trailer with --only-input does not make sense"), git_interpret_trailers_usage, options); + list_splice(&configured_trailers, &arg_trailers); + if (argc) { int i; for (i = 0; i < argc; i++) - interpret_trailers(argv[i], &opts, &trailers); + interpret_trailers(argv[i], &opts, &arg_trailers); } else { if (opts.in_place) die(_("no input file given for in-place editing")); - interpret_trailers(NULL, &opts, &trailers); + interpret_trailers(NULL, &opts, &arg_trailers); } - new_trailers_clear(&trailers); + new_trailers_clear(&arg_trailers); return 0; } diff --git a/trailer.c b/trailer.c index e2d541372a3..0a86e0d5afa 100644 --- a/trailer.c +++ b/trailer.c @@ -66,6 +66,11 @@ static LIST_HEAD(conf_head); static char *separators = ":"; +const char *default_separators(void) +{ + return separators; +} + static int configured; #define TRAILER_ARG_STRING "$ARG" @@ -424,6 +429,25 @@ int trailer_set_if_missing(enum trailer_if_missing *item, const char *value) return 0; } +void trailer_conf_set(enum trailer_where where, + enum trailer_if_exists if_exists, + enum trailer_if_missing if_missing, + struct trailer_conf *conf) +{ + if (where != WHERE_DEFAULT) + conf->where = where; + if (if_exists != EXISTS_DEFAULT) + conf->if_exists = if_exists; + if (if_missing != MISSING_DEFAULT) + conf->if_missing = if_missing; +} + +struct trailer_conf *new_trailer_conf(void) +{ + struct trailer_conf *new = xcalloc(1, sizeof(*new)); + return new; +} + void duplicate_trailer_conf(struct trailer_conf *dst, const struct trailer_conf *src) { @@ -642,6 +666,9 @@ ssize_t find_separator(const char *line, const char *separators) /* * Obtain the token, value, and conf from the given trailer. * + * The conf needs special handling. We first read hardcoded defaults, and + * override them if we find a matching trailer configuration in the config. + * * separator_pos must not be 0, since the token cannot be an empty string. * * If separator_pos is -1, interpret the whole trailer as a token. @@ -691,22 +718,14 @@ static struct trailer_item *add_trailer_item(struct list_head *head, char *tok, return new_item; } -static void add_arg_item(struct list_head *arg_head, char *tok, char *val, - const struct trailer_conf *conf, - const struct new_trailer_item *new_trailer_item) +void add_arg_item(char *tok, char *val, const struct trailer_conf *conf, + struct list_head *arg_head) + { struct arg_item *new_item = xcalloc(1, sizeof(*new_item)); new_item->token = tok; new_item->value = val; duplicate_trailer_conf(&new_item->conf, conf); - if (new_trailer_item) { - if (new_trailer_item->where != WHERE_DEFAULT) - new_item->conf.where = new_trailer_item->where; - if (new_trailer_item->if_exists != EXISTS_DEFAULT) - new_item->conf.if_exists = new_trailer_item->if_exists; - if (new_trailer_item->if_missing != MISSING_DEFAULT) - new_item->conf.if_missing = new_trailer_item->if_missing; - } list_add_tail(&new_item->list, arg_head); } @@ -719,10 +738,10 @@ void parse_trailers_from_config(struct list_head *config_head) list_for_each(pos, &conf_head) { item = list_entry(pos, struct arg_item, list); if (item->conf.command) - add_arg_item(config_head, - xstrdup(token_from_item(item, NULL)), + add_arg_item(xstrdup(token_from_item(item, NULL)), xstrdup(""), - &item->conf, NULL); + &item->conf, + config_head); } } @@ -755,10 +774,10 @@ void parse_trailers_from_command_line_args(struct list_head *arg_head, strbuf_release(&sb); } else { parse_trailer(tr->text, separator_pos, &tok, &val, &conf); - add_arg_item(arg_head, - strbuf_detach(&tok, NULL), + add_arg_item(strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL), - conf, tr); + conf, + arg_head); } } @@ -1148,6 +1167,16 @@ void free_trailers(struct list_head *head) } } +void new_trailers_clear(struct list_head *trailers) +{ + struct list_head *pos, *p; + + list_for_each_safe(pos, p, trailers) { + list_del(pos); + free_arg_item(list_entry(pos, struct arg_item, list)); + } +} + size_t trailer_block_start(struct trailer_block *trailer_block) { return trailer_block->start; diff --git a/trailer.h b/trailer.h index fe49a9bad52..9b86acfe2d4 100644 --- a/trailer.h +++ b/trailer.h @@ -46,9 +46,20 @@ struct new_trailer_item { enum trailer_if_missing if_missing; }; +void trailer_conf_set(enum trailer_where where, + enum trailer_if_exists if_exists, + enum trailer_if_missing if_missing, + struct trailer_conf *conf); + +struct trailer_conf *new_trailer_conf(void); void duplicate_trailer_conf(struct trailer_conf *dst, const struct trailer_conf *src); +const char *default_separators(void); + +void add_arg_item(char *tok, char *val, const struct trailer_conf *conf, + struct list_head *arg_head); + struct process_trailer_options { int in_place; int trim_empty; @@ -92,6 +103,7 @@ void trailer_block_release(struct trailer_block *trailer_block); void trailer_config_init(void); void free_trailers(struct list_head *trailers); +void new_trailers_clear(struct list_head *trailers); void format_trailers(struct list_head *head, const struct process_trailer_options *opts, From patchwork Wed Jan 10 06:51:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13515695 Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (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 DDCED3C464 for ; Wed, 10 Jan 2024 06:51:38 +0000 (UTC) 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="ONUvcP6E" Received: by mail-wr1-f49.google.com with SMTP id ffacd0b85a97d-3368abe1093so3240312f8f.2 for ; Tue, 09 Jan 2024 22:51:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704869497; x=1705474297; 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=NJXVDZAV8/lEGcLz+j4d3giw9C8brSkT4KkN7KU9WGM=; b=ONUvcP6EYno0rm8Qb9+zdbGC9btUyF/9BMQfi4Nxlb00cvlcalg5V43FaNb9emaZmV ss22SKKxXS7VW/SlAOnNIhkq+j+Wbr5O7Mtr/8ufyikdiqjCkWN7RyPIhkqGYM/t6TUZ aFqcKTrZ+mvAArfD7Yz8zCu/e3nP4Fvxh+4Z3uq3mEX0PoAajzeo3eccvTGWgkxmSGGj modOTfUW/U3LtWT07PoyiN+IfX8FbpIdFxGhsUUZByjHUP/pLF7Q7Iem3fIrRVC6SUMw yKxO9BPlJDCdcpXa2IIew2x7wlmQ+AteIEpr/+EbizWvLnAZsoEHrCzA64IOO9onwUAr v6+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704869497; x=1705474297; 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=NJXVDZAV8/lEGcLz+j4d3giw9C8brSkT4KkN7KU9WGM=; b=wWNi+hFTu1zJajQkYcB7R/X2EA/jHSekrUmyADwBIVmbJ72XV/38A0va7cYSsh9/zB J8/xKCVczO5V1Ra++oNqbwUzxwEC5bsXuK2qnSPEjvOC7DkKdWjBwtz1yLzgt8B8FYul prGhO9VeUSgxwxNVzA3a9xtk37+/F/g/mKIf/cn4o4gCOGF/wvINLRtc9PvjlidxP2ou C2XbcEU+ua84GLNSk+iXGZR7b2MMOvMThxdXijaXeLkMpahn480sH5V0b5SZacsEokYN oV3zn34/B3XNrtP8QH1t97hqLmY9U4JEvm6yJFdlVba6dktGvMmqsxF764x4+ftqiC3j HDug== X-Gm-Message-State: AOJu0YyjDDkrS2XxhtbMjDsVaY875/Bxf/CXf/Uyt7YO5bwgbbGlvEhZ DXOs7RCw6/eWKTTKLhr11e+1z8HfxII= X-Google-Smtp-Source: AGHT+IFQfABBT/RjnKIzbhT+WOsNAMuBOSIBPMuQpFa9OOZmDldDW7NpJcMI8tAf9Bo7IBYmr6CgCA== X-Received: by 2002:a05:6000:82:b0:337:68ab:617e with SMTP id m2-20020a056000008200b0033768ab617emr230234wrx.8.1704869497057; Tue, 09 Jan 2024 22:51:37 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j15-20020adfb30f000000b003375c8f796bsm4177275wrd.0.2024.01.09.22.51.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 22:51:36 -0800 (PST) Message-ID: <243eee730e4aae48fd53ba066c829a65cd5546f3.1704869487.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Wed, 10 Jan 2024 06:51:26 +0000 Subject: [PATCH 10/10] trailer: delete obsolete argument handling code 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: Emily Shaffer , Junio C Hamano , Christian Couder , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver This commit was not squashed with its parent in order to keep the diff separate (to make the additive changes in the parent easier to read). Signed-off-by: Linus Arver --- trailer.c | 39 --------------------------------------- trailer.h | 17 ----------------- 2 files changed, 56 deletions(-) diff --git a/trailer.c b/trailer.c index 0a86e0d5afa..27bb2195f53 100644 --- a/trailer.c +++ b/trailer.c @@ -745,45 +745,6 @@ void parse_trailers_from_config(struct list_head *config_head) } } -void parse_trailers_from_command_line_args(struct list_head *arg_head, - struct list_head *new_trailer_head) -{ - struct strbuf tok = STRBUF_INIT; - struct strbuf val = STRBUF_INIT; - const struct trailer_conf *conf; - struct list_head *pos; - - /* - * In command-line arguments, '=' is accepted (in addition to the - * separators that are defined). - */ - char *cl_separators = xstrfmt("=%s", separators); - - /* Add an arg item for each trailer on the command line */ - list_for_each(pos, new_trailer_head) { - struct new_trailer_item *tr = - list_entry(pos, struct new_trailer_item, list); - ssize_t separator_pos = find_separator(tr->text, cl_separators); - - if (separator_pos == 0) { - struct strbuf sb = STRBUF_INIT; - strbuf_addstr(&sb, tr->text); - strbuf_trim(&sb); - error(_("empty trailer token in trailer '%.*s'"), - (int) sb.len, sb.buf); - strbuf_release(&sb); - } else { - parse_trailer(tr->text, separator_pos, &tok, &val, &conf); - add_arg_item(strbuf_detach(&tok, NULL), - strbuf_detach(&val, NULL), - conf, - arg_head); - } - } - - free(cl_separators); -} - static const char *next_line(const char *str) { const char *nl = strchrnul(str, '\n'); diff --git a/trailer.h b/trailer.h index 9b86acfe2d4..8a89e95c171 100644 --- a/trailer.h +++ b/trailer.h @@ -32,20 +32,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); -/* - * A list that represents newly-added trailers, such as those provided - * with the --trailer command line option of git-interpret-trailers. - */ -struct new_trailer_item { - struct list_head list; - - const char *text; - - enum trailer_where where; - enum trailer_if_exists if_exists; - enum trailer_if_missing if_missing; -}; - void trailer_conf_set(enum trailer_where where, enum trailer_if_exists if_exists, enum trailer_if_missing if_missing, @@ -79,9 +65,6 @@ struct process_trailer_options { void parse_trailers_from_config(struct list_head *config_head); -void parse_trailers_from_command_line_args(struct list_head *arg_head, - struct list_head *new_trailer_head); - void process_trailers_lists(struct list_head *head, struct list_head *arg_head);