From patchwork Fri Jan 26 22:38:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13533433 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66EBD5A114 for ; Fri, 26 Jan 2024 22:39:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308744; cv=none; b=YDJaj5HZcqnAwhVYFYJ9Nphee7vP8x8CCq20z29aLrBZESrDxKSY93xkSY/wySc22hrHUm3GuA0wCy0QLNo5QJxZedjEREWCA1AQ7mYKrhjMFhKxxmGfbltWGndyTb+cuZU7aKLT9N4/LFgcuy9vqZGzeAkuGRxELHzHAQJLCvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308744; c=relaxed/simple; bh=KMW1tqjXZbth0lWetno8Vmnfqr0/CaaAJxAZ8SfCCzU=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=oOBR5qI6UGN05LtKflPTUctuCkhvEJOjy+rXwai8Zd3AodTkSmVUUK0+CdxTaBlaURtFW7L+R4rI9yS80FaddQHfmnbT+FCBPbdFWfkt/Rs0eKBcZP2n1sM0VUzXtq54B3kBOWSgc5wbqelE1YFE5o8WgGanHpo6lQJXD6rVX+8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DeCtEDTq; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DeCtEDTq" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-40e9ef9853bso13754365e9.1 for ; Fri, 26 Jan 2024 14:39:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706308740; x=1706913540; 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=dMYYWuGCHsrryvcyEzP3kmo0QRvFaMyqF3XKpAYLOvo=; b=DeCtEDTqdHksgZzrtuclvWbjhxKcA/FOVtlWnvH8PrXegBCDzg8HCurC7LHXovFGcr +LSK+p2pwIJlSPUNqJ2d0TEmHPPs7KTbQIMiXJKV1RP/Qr3ofqWRNgIsXa8nSmS0lwpz CR9s4D8ZxgS++tjnsGN8cAJ6p7FKuzf4kMyrdvWkq7cZN0ZOHWCsr/slykX4YYkwOYlE q1x9E7RgC300UyZg/x5H062XBsMQJsN1icx/MM4NUs8mXdgZeDs/u8Rq5WVL8mnS0Qb+ vXx2RtDEeZ7DAPM6LUc5RW8bF8kTSQkX2c5+Cnza8BqRUtm294ZyK4wHoGfuo45cKolN xx9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706308740; x=1706913540; 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=dMYYWuGCHsrryvcyEzP3kmo0QRvFaMyqF3XKpAYLOvo=; b=uwhdhDpBMj4hl+y4sEf+U5bm7953aPFY7R5Qtz5gsqQCvAKfnVlf4hNw8HcOb2Wz2j g4/DcIgAx4ktszdjSxCn0FF3JBnI6potnjFQyCyXWN9OGOV77SSkgz42ZoXuDTrxTCHA XdWZTsU8s+sYDTN4cRxSFsbY7YsV7LJUTm34qsyZrhuzJZeMu22qfpCWm73aX2NljDyd wYXwRRKAPU2VI7l8/3ZR37x9xM2nfO2R4FZXeP18bXgkmLWNuCQSYtb5d5Pi54QWqXnR T/Z8EfinHCITmFhArtNUVfiVzxDxxrrxSt/1UxwzyBhN+7gKmJEd7jS4gLoULTVeCpSY 8L8w== X-Gm-Message-State: AOJu0YyWoJgTexulcDdcZUlYSiziOVeYfgEgTEBxalG25N5Qt6/gCmiR 3eme1o7IlRi7IceIEz3UupX5CrXrYBZtqKTXXZhzZs+FQMzROqfNJytkFx4Z X-Google-Smtp-Source: AGHT+IGgle1El6pzSuZGE21IwdmBrLVjDuDZkj/URaL0lYmleWUYoEZlflA8iNXbumFGZ2ILFKL6BA== X-Received: by 2002:a1c:7c0b:0:b0:40e:e4d3:473e with SMTP id x11-20020a1c7c0b000000b0040ee4d3473emr319778wmc.64.1706308740134; Fri, 26 Jan 2024 14:39:00 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j22-20020a05600c1c1600b0040e7306f655sm3034010wms.22.2024.01.26.14.38.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 14:38:59 -0800 (PST) Message-ID: In-Reply-To: References: Date: Fri, 26 Jan 2024 22:38:48 +0000 Subject: [PATCH v2 01/10] trailer: prepare to expose functions as part of API Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver In the next patch, we will move "process_trailers" from trailer.c to builtin/interpret-trailers.c. That move will necessitate the growth of the trailer.h API, forcing us to expose some additional functions in trailer.h. Rename relevant functions so that they include the term "trailer" in their name, so that clients of the API will be able to easily identify them by their "trailer" moniker, just like all the other functions already exposed by trailer.h. The the opportunity to start putting trailer processions options (opts) as the first parameter. This will be the pattern going forward in this series. Helped-by: Junio C Hamano Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 4 ++-- trailer.c | 26 +++++++++++++------------- trailer.h | 6 +++--- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 033bd1556cf..85a3413baf5 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -132,11 +132,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(&opts, &trailers, argv[i]); } else { if (opts.in_place) die(_("no input file given for in-place editing")); - process_trailers(NULL, &opts, &trailers); + interpret_trailers(&opts, &trailers, NULL); } new_trailers_clear(&trailers); diff --git a/trailer.c b/trailer.c index 3a0710a4583..66b6660f5a4 100644 --- a/trailer.c +++ b/trailer.c @@ -163,12 +163,12 @@ 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) +static void format_trailers(const struct process_trailer_options *opts, + struct list_head *trailers, FILE *outfile) { struct list_head *pos; struct trailer_item *item; - list_for_each(pos, head) { + list_for_each(pos, trailers) { item = list_entry(pos, struct trailer_item, list); if ((!opts->trim_empty || strlen(item->value) > 0) && (!opts->only_trailers || item->token)) @@ -589,7 +589,7 @@ static int git_trailer_config(const char *conf_key, const char *value, return 0; } -static void ensure_configured(void) +static void trailer_config_init(void) { if (configured) return; @@ -1035,10 +1035,10 @@ static void parse_trailers(struct trailer_info *info, } } -static void free_all(struct list_head *head) +static void free_trailers(struct list_head *trailers) { struct list_head *pos, *p; - list_for_each_safe(pos, p, head) { + list_for_each_safe(pos, p, trailers) { list_del(pos); free_trailer_item(list_entry(pos, struct trailer_item, list)); } @@ -1075,16 +1075,16 @@ static FILE *create_in_place_tempfile(const char *file) return outfile; } -void process_trailers(const char *file, - const struct process_trailer_options *opts, - struct list_head *new_trailer_head) +void interpret_trailers(const struct process_trailer_options *opts, + struct list_head *new_trailer_head, + const char *file) { LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; struct trailer_info info; FILE *outfile = stdout; - ensure_configured(); + trailer_config_init(); read_input_file(&sb, file); @@ -1110,9 +1110,9 @@ void process_trailers(const char *file, process_trailers_lists(&head, &arg_head); } - print_all(outfile, &head, opts); + format_trailers(opts, &head, outfile); - free_all(&head); + free_trailers(&head); trailer_info_release(&info); /* Print the lines after the trailers as is */ @@ -1135,7 +1135,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 1644cd05f60..37033e631a1 100644 --- a/trailer.h +++ b/trailer.h @@ -81,9 +81,9 @@ 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 interpret_trailers(const struct process_trailer_options *opts, + struct list_head *new_trailer_head, + const char *file); void trailer_info_get(struct trailer_info *info, const char *str, const struct process_trailer_options *opts); From patchwork Fri Jan 26 22:38:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13533434 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 60F135A783 for ; Fri, 26 Jan 2024 22:39:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308745; cv=none; b=Ydg9RPN3dIGnDIfpC4YZM6hgDR5djMLdJcdkGRJ+fcfBGpbqop7RZU7OoZ6lRAcrDX/dB7KnfXlk/TzMtbh1MAnazbf/5yT3FS4j1tj3z1qm0ENmUYnLPxtFYi9f0A7ekY8h9VXgb4HgTzp9gwS8JgYatJ142v8nUKjf4pPJYpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308745; c=relaxed/simple; bh=O5jwe+h8ffItyEhBoUzHBQ/oWD++hIkdugUqqPZKEBc=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=X+MWQ4iITUE4ucMEICMgd5N6RlSv5BkET46fExc33ZBqO97I/8fbbRXU1ZFqtAtlnocFNVDcoBQ/5VOjNAlF7g5X/xKQHxHYX3MJ7BX9RRuieGHTMnIyp4bYYYlC0lMIVYB7fR3Mp1PNA8MzJq8Vci/YdKKNkBbuDYYg1A+asZQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CNqxJ7OA; arc=none smtp.client-ip=209.85.221.54 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="CNqxJ7OA" Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-3394b892691so535298f8f.1 for ; Fri, 26 Jan 2024 14:39:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706308741; x=1706913541; 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=kZmfdHp82bUljryEBfwEldFiToycrWOOk+xAtbl3FT0=; b=CNqxJ7OA5MzMmB6pjmEto3KwEOqO18BUuYkZA95sJ9okIiYvsWqJ76d+6HUVhTx8Aw 6TCVbtUr1uZSh5O19yypagildR/Ww6yyUsS8s1kcHDK/55vXyjkGwCDLN+Hgc0DMuEUU O0ft0zYXRXcd22wixIFZjzYmoQ6Wljrq+HLnh5HyvvmKztEmT36q7rx6S3Pw8jXZj3v8 xX0bs54aBCGlepiwiirbyueS5/DBl7REy2MnjVuwb0G4zzCgpukqVw5ibvd/wyezV19F z2nCSPuHr86/PwgW6ZAkQJoGU7ETTOOsZXM9VW0qSEpQEV07FTXDNKkxVglpUob1bpBu 8K9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706308741; x=1706913541; 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=kZmfdHp82bUljryEBfwEldFiToycrWOOk+xAtbl3FT0=; b=odT65szoPaFu2drg3OT1OiKL1ACKm1t/RAN5vlP7r2LWTh2UfbQ1isj33Wfa/abqMs 8fqfyO7ke+uMxhmCpRvF2P30ACPDDHxM1sQFZdWqLMmYaS3lY1diO2h8sDr3n1lE8+B9 9mInjr/1mo+NbXyRQFN1PzSyDEaz0mDylnuKmHdFlH0IL+wRfy3YyWiQzc4mxSaoE2ZG NxPaXS6skN0T+gBfNpGwJFB3caKtc8iuIl48HuWKU3b0FFKjVxIKYpc5xmAyfRa+KW3L 266o49OCa2f6mn5D3QTxKdCCatOvyTAZLb5+TBF6tlkNhMASO/ptVZ3/KMNcajFhT1WC H8sA== X-Gm-Message-State: AOJu0Yzjj6MYaNX7ZcrTTjVDI1bg6Mv+FQm2ttYFvyBWsN6BuEJ9ZXx1 EebBNJhPdl/ahOQ1Rlox7RrLutccB7N60Dzst98+OFnyEzAzB9alqettjRRN X-Google-Smtp-Source: AGHT+IFPASiS63sC5cSn9Ob/582uXkwgnuuyFKOVPSaHXN8vD9zUIoTw5tJWZIksw2iNJDa91Vceuw== X-Received: by 2002:adf:fcc7:0:b0:339:2827:d560 with SMTP id f7-20020adffcc7000000b003392827d560mr957366wrs.17.1706308741168; Fri, 26 Jan 2024 14:39:01 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id q14-20020adff78e000000b0033925aa222dsm2106956wrp.57.2024.01.26.14.39.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 14:39:00 -0800 (PST) Message-ID: In-Reply-To: References: Date: Fri, 26 Jan 2024 22:38:49 +0000 Subject: [PATCH v2 02/10] trailer: move interpret_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: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , Linus Arver , Linus Arver From: Linus Arver From: Linus Arver The interpret-trailers.c builtin is the only place we need to call interpret_trailers(), 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 with a more granular API, which can then be unit-tested in the future (because interpret_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. Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 94 +++++++++++++++++++++++++++ trailer.c | 120 ++++------------------------------- trailer.h | 20 +++++- 3 files changed, 124 insertions(+), 110 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 85a3413baf5..8556acde4aa 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 struct process_trailer_options *opts, + struct list_head *new_trailer_head, + const char *file) +{ + LIST_HEAD(head); + struct strbuf sb = STRBUF_INIT; + struct trailer_info info; + FILE *outfile = stdout; + + trailer_config_init(); + + read_input_file(&sb, file); + + if (opts->in_place) + outfile = create_in_place_tempfile(file); + + parse_trailers(opts, &info, sb.buf, &head); + + /* Print the lines before the trailers */ + if (!opts->only_trailers) + fwrite(sb.buf, 1, info.trailer_block_start, outfile); + + 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); + } + + format_trailers(opts, &head, outfile); + + 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); + + 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; diff --git a/trailer.c b/trailer.c index 66b6660f5a4..d3899195876 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 format_trailers(const struct process_trailer_options *opts, - struct list_head *trailers, FILE *outfile) +void format_trailers(const struct process_trailer_options *opts, + struct list_head *trailers, FILE *outfile) { 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 trailer_config_init(void) +void trailer_config_init(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(const struct process_trailer_options *opts, + struct trailer_info *info, + const char *str, + struct list_head *head) { 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_trailers(struct list_head *trailers) +void free_trailers(struct list_head *trailers) { struct list_head *pos, *p; list_for_each_safe(pos, p, trailers) { @@ -1044,88 +1032,6 @@ static void free_trailers(struct list_head *trailers) } } -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 interpret_trailers(const struct process_trailer_options *opts, - struct list_head *new_trailer_head, - const char *file) -{ - LIST_HEAD(head); - struct strbuf sb = STRBUF_INIT; - struct trailer_info info; - FILE *outfile = stdout; - - trailer_config_init(); - - 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); - } - - format_trailers(opts, &head, outfile); - - 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); - - 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 37033e631a1..4f603e03ceb 100644 --- a/trailer.h +++ b/trailer.h @@ -81,15 +81,29 @@ struct process_trailer_options { #define PROCESS_TRAILER_OPTIONS_INIT {0} -void interpret_trailers(const struct process_trailer_options *opts, - struct list_head *new_trailer_head, - const char *file); +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(const struct process_trailer_options *opts, + struct trailer_info *info, + const char *str, + struct list_head *head); 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 trailer_config_init(void); +void format_trailers(const struct process_trailer_options *opts, + struct list_head *trailers, FILE *outfile); +void free_trailers(struct list_head *trailers); + /* * Format the trailers from the commit msg "msg" into the strbuf "out". * Note two caveats about "opts": From patchwork Fri Jan 26 22:38:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13533436 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CE6D5A78F for ; Fri, 26 Jan 2024 22:39:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308746; cv=none; b=P0tEEo+SWlBgsJx3DvAoltmp8rR4KLuhi03oo3aP+B7bNiia+tOcKoQrqmWkjGugBGVV+K6+mBT/mTZZLSMqnvGADOfYlTaXk2hztw60J4IgPTBSiIR9sQOgoi2UQZNgW+L3ux3OgYONAPNHXdn6DYomPCwwoB5ERQBpOPfJJ2M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308746; c=relaxed/simple; bh=kBFEdNiqzbM7lRdU4NHsD3q9dSujwy2MVPpxLZmPeLw=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=UU4L2qR4RtIkWQFXLEIpB8ahbUpVtz9VImlRUT92wza6LnRdR2x3ClEPu/SyC/In0cjgXgqFop080o6j9JjtPZ27CTm/KD7JYfF9mv4o3LzvhGx23tp470Ljhao7pxghAG81rccA/99dh7xt+ho1WM1uLls+Pr3fdtbhcOLjGj4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NVUMNVCc; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NVUMNVCc" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-33921b8988fso976795f8f.3 for ; Fri, 26 Jan 2024 14:39:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706308742; x=1706913542; 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=8XtF5Gk3QFBRNRpa1+EO6YCah2g8wGYKVIZusohDWNw=; b=NVUMNVCcI7MmAYU535DcqQFV7aFIt4tpUxcZfajIP6+7tQ2Se/mRYlm8SZc1AdhrxE /dwdrwMYCwi27E+2jv9fObIBbgfSWFinx2INvtLNId9sEMPRsau8108e5ltzLQ1qcMsu XCfJlxIcX9rBdf2RJbZOIqvoWhgfKTIpVuXxHnwZYzk00qdlkJ4sD+uU2J1uzOJz1r64 KvRhaGUADYTPnORoeGqfX+/+eqmcqM4ikUTGH1aBtvmIFeX6qHkGlrS9XXN1qJs0Tr7I Mi8+4rfwavlrZSuWhA9RGEGy9vIRjkt+ah8vF7bttSb4zAvq5f1CHVBQIBdxgaNoNzrV Jlgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706308742; x=1706913542; 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=8XtF5Gk3QFBRNRpa1+EO6YCah2g8wGYKVIZusohDWNw=; b=MHtORUffw/haYfwwXZzGdxFFkig0AyXCfHtsYq3gdOHRZkkwlBRT2sJNjAtLl4uCGK W15OX0qJxHlhiThT7bFxGm/0ZdAHUyk1ptskUiGuiMRJXea34/UDz2HGqsUmFm6duWLY B5MLEtp3ko3H/msIgXMR+gkbUWPLDqOhlnrKS2irf9rsGFOutaip40cGUPtGEsXjlvk6 beNS5WJRbzXNz6Z6eJtSzSdQDBfTlPBSfYOLj12lhPg3B7HH1SCrQOwpR/+7+fKEpWxf tLi24/p6B1fMcsDOQmxChH5aWsbFr6xzukUTqM4hgUPEnWVgeoiuTgk2ZZcxt8lWvDzy oANQ== X-Gm-Message-State: AOJu0YyfVHLtF3yPdE6IyEgnEbqJKp23fxb0nTjz1atSAI6GW9MXO/jN /CxgP7H+N/eH0Z4kfjYQiKeAX8mhSpUyFbY/IYdlBX8y8d7j16zi3drCsWAs X-Google-Smtp-Source: AGHT+IGfn6d+EoqZYvKvi4LNidcDpsOxxRcWEOKJFUkJ5yE81+YrvJMG7tSg8Rmasv1LkWDPglL7Lw== X-Received: by 2002:adf:e6cc:0:b0:339:437f:9a8c with SMTP id y12-20020adfe6cc000000b00339437f9a8cmr293018wrm.133.1706308742167; Fri, 26 Jan 2024 14:39:02 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y14-20020a5d470e000000b00337cd6b1890sm2109415wrq.80.2024.01.26.14.39.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 14:39:01 -0800 (PST) Message-ID: <9b7747d550e87457195c40a49347bc749a7290d0.1706308737.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Jan 2024 22:38:50 +0000 Subject: [PATCH v2 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: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , 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. While we're at it, reorder parameters to put the trailer processing options first, and the out parameter (strbuf we write into) at the end. This unification 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. Helped-by: Junio C Hamano Signed-off-by: Linus Arver --- builtin/interpret-trailers.c | 6 +- pretty.c | 2 +- ref-filter.c | 2 +- trailer.c | 105 +++++++++++++++++++++++++++++------ trailer.h | 19 +++---- 5 files changed, 102 insertions(+), 32 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 8556acde4aa..5352ee65bd1 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -140,6 +140,7 @@ static void interpret_trailers(const struct process_trailer_options *opts, { LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; + struct strbuf trailer_block = STRBUF_INIT; struct trailer_info info; FILE *outfile = stdout; @@ -169,7 +170,10 @@ static void interpret_trailers(const struct process_trailer_options *opts, process_trailers_lists(&head, &arg_head); } - format_trailers(opts, &head, outfile); + /* Print trailer block. */ + format_trailers(opts, &head, &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..bdbed4295aa 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(&opts, msg + c->subject_off, sb); ret = arg - placeholder + 1; } trailer_out: diff --git a/ref-filter.c b/ref-filter.c index 35b989e1dfe..d358953b0ce 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(&atom->u.contents.trailer_opts, subpos, &s); v->s = strbuf_detach(&s, NULL); } else if (atom->u.contents.option == C_BARE) diff --git a/trailer.c b/trailer.c index d3899195876..7692bf9eb40 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(const struct process_trailer_options *opts, - struct list_head *trailers, FILE *outfile) -{ - struct list_head *pos; - struct trailer_item *item; - list_for_each(pos, trailers) { - 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(const struct process_trailer_options *opts, + struct list_head *trailers, + struct strbuf *out) +{ + struct list_head *pos; + struct trailer_item *item; + int need_separator = 0; + + list_for_each(pos, trailers) { + 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 struct process_trailer_options *opts, + const char *msg, + 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(opts, &info, msg, &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); + } else + format_trailers(opts, &head, out); + + free_trailers(&head); trailer_info_release(&info); } diff --git a/trailer.h b/trailer.h index 4f603e03ceb..c309b01323d 100644 --- a/trailer.h +++ b/trailer.h @@ -101,22 +101,17 @@ void trailer_info_release(struct trailer_info *info); void trailer_config_init(void); void format_trailers(const struct process_trailer_options *opts, - struct list_head *trailers, FILE *outfile); + struct list_head *trailers, + struct strbuf *out); void free_trailers(struct list_head *trailers); /* - * 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 process_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 struct process_trailer_options *opts, + const char *msg, + struct strbuf *out); /* * An interface for iterating over the trailers found in a particular commit From patchwork Fri Jan 26 22:38:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13533437 Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60D765A7B8 for ; Fri, 26 Jan 2024 22:39:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308748; cv=none; b=dJcdpwE8OVCRNTr2zfMoBSYuYYqJu4IYPmywJgY7HS/n06VMnlKP20IR6Awqw1OuVgApse3BuR2AWNyYVgk+kiLNMKcjmYxM5II901ngyltEf966VzvbemDNv02eKlOMcJzDCFKWgZhawZDTcRIb3qzYlk+Rw9xa1ASdfqQOkbQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308748; c=relaxed/simple; bh=BeCA9NimrzHxV5yv+yfyAVz592dcO/1kGWixHYKqjv8=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=d6165uRULHI/Ehjawl2h4auJKcuY2hC4r6aGdTJlKaLHK0yz/VMOY1Y6yVyOiTjHE5hDbdk8alWLgaT+Z7IY2bwrX7uIaOQByrtz0OmrybJ7PRnZ2B2PmfmFqyf7a0UYSnImUSCtAoRWOtF5jZUDtzYENMiwKmx17DUMUlxj7Ak= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XsG+XJK8; arc=none smtp.client-ip=209.85.167.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XsG+XJK8" Received: by mail-lf1-f41.google.com with SMTP id 2adb3069b0e04-51022133a84so1213194e87.3 for ; Fri, 26 Jan 2024 14:39:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706308743; x=1706913543; 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=cWcfZGdnOoF8T+7DEPxIEnkTNfcTKabTkpAgfF5BPdI=; b=XsG+XJK8irGtMQyzxeU0HS02O/xsKeC2HQNI+b9nvdKNGWT4qqUEa7Q6PcrAOs4d5a Lre8kwJ3R9MDGw1NHDwDX5I6q91C37sQ8x6OemebWL3ZLQHG/5Zy9BPbpEW3PmAhjtf+ nsu9EX+UFGJJ3wiG3o6DhpnV7BEt2y6NM0P9z2jzsqgEvOIkwER64z/iFHOz75p9Zh2g zfQg+TG7gwdh87QkP2vMo9g6N9JD7fkY/2OOpxGtYtHwhg0lPGAWummX8utbNBPBWEBJ XqCUKT/injvcXFQ1j58VsvmM6poD9k9kznT5cRBvc7FNcQeNeEJVE5Zpaxwes4j9cjny sGyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706308743; x=1706913543; 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=cWcfZGdnOoF8T+7DEPxIEnkTNfcTKabTkpAgfF5BPdI=; b=EYgJp8kZE8S93HTBtQSpiW7lDCmd0W7H6iA3vNWvLr+aVGALtFfZf3rdjgbA8mPylK YAguRtdtkM4wxZy9OrXTwqgFg1QFTiFTYPgF16MWwYVwFF3Y4fRyWxBZWDIMnRtvx99F yrXev5nlm+jRFCmZ94XNxjr+f+P/49eeETBF5J3Fq4DbdHUSpH1l9+8CWzg1k/YfPOiV t1PPDKVq52OwKJUWn0Wfg5EQNPCS0gO87ogvRowPKjPJ3dlbTGA1pLKBqJM7RFwuNT1j Iwq5RqT8CdKxI0zaZBtodI7nzu43kgxTTExj1TUdfchG4CKx7As9AdrWxGW6Q6HdpcDh 555A== X-Gm-Message-State: AOJu0Yx6PRsxDCcauqYlgWBQBgUsDyTC8chmnxdOotfBRltZDXqfdE+q wxP9ZO4Gv9vRfI3s5fI84ZNn6Dx8Wfx5LmUvjNYcX63xb0M5AAaaV6/vTJiP X-Google-Smtp-Source: AGHT+IHlMQu3zHXG8vf0dfXpE3wXcz99bxmcb0ANiRr9/BJWpFCP49UKXeWNi24t7TrMEbFaBM9uJA== X-Received: by 2002:a05:6512:3147:b0:50e:7749:7b99 with SMTP id s7-20020a056512314700b0050e77497b99mr605284lfi.37.1706308743305; Fri, 26 Jan 2024 14:39:03 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t6-20020a0560001a4600b00337be1c7d2fsm2112463wry.58.2024.01.26.14.39.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 14:39:02 -0800 (PST) Message-ID: In-Reply-To: References: Date: Fri, 26 Jan 2024 22:38:51 +0000 Subject: [PATCH v2 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: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , 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 7692bf9eb40..71ea2bb67f8 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 struct process_trailer_options *opts, const char *msg, struct strbuf *out) From patchwork Fri Jan 26 22:38:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13533438 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91C3F5B1E6 for ; Fri, 26 Jan 2024 22:39:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308748; cv=none; b=JrSbtCxU5pLfnVNd0eHneS0uBCNUAzXtXw8S0kHCQ6fac1WMNuh2PhYEWQy+Vi1uYQCR/u965Ws+nZwRPIRKraT2glsyfp0klgh9JhSEG53j/iTlr6Qg3h4UmZIRWtiIpX7QytmVXQ0vMbQkfXjvVI36u5SMECRW5OhafJbl5IA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308748; c=relaxed/simple; bh=NrqDl9jO7w0p1uWSKnU1dungX5O33wIeWGNlllaJ/uY=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=el/30B3Ap8bhSifCvk1MwmOWk5HTJn2DzUtYqHw5kP3E0+bJbvYn7AXl70oYPjKAWSh/ROXwdSIXa03j1j5hqoOz1KXTqNQU9iTM5f1oV6RG2karMyfKwJZwEShh9kzFwM59NepBxSiIDiIBasXS1L/q4YvP6bDxeSQ6cn/7240= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TVW9azS+; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TVW9azS+" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-33924df7245so1182316f8f.0 for ; Fri, 26 Jan 2024 14:39:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706308744; x=1706913544; 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=k3OQPNBRoI4s9xk+w81XwIFGbiMy8piXg1fxcmhx8Hc=; b=TVW9azS+ubVB4DmWvQ71BZjE3Gah3XGHMyBwlLKBb2Ifd3qxHgsMqBmxyz1zPMfidq qm7tVHqQR5SuRl5fKXCtTi1EwqNdLIrb/oiVN0bOK0wa4czfQXX1t2DNhSC4O5zIpqoU LWnqTgQewaGNeEc661zOjxWHCecEes+IOvZ7GHQTwk/ZDAuzCIu0tj7GHu7jPIMgxiTu JNP3qR4Dp7bsuMhD6U17aKb72+CTSIYvCL50BmHgX3A315/mJDlV63sxrQYfC4FU2PXI R31g2AjLb0JragkHMg0IYRNfoOSfE61MoIfl9gyCSfpvmxns/Cla2TW7fM1TbC6JDBzg bdrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706308744; x=1706913544; 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=k3OQPNBRoI4s9xk+w81XwIFGbiMy8piXg1fxcmhx8Hc=; b=Z8DZ7KtkdoBbZsyiFdxgpxw8QfLedTuxYr5xuCECf2ZCZqFGOr2wmAJLboVM4vUTp3 75iT71yFHT4YWpdjCq7sCCpLpPXRuJ8fGBUUS+N1Mo/yQaVa1q/5C1swwmE8YKO5FYa1 QCpOQiGbyb5Kl0XdnlluwNTZWMTylAZwxqP27yrcOQbs+9SSrKyKCoUF/8h9R9WXeBYZ 6e9u4lH7NdJ8Ryp07KHWJ6jdzd64tBOUGm9hr+JirbplM2Aja6H4TCCKg5awIJifZduH 43kI3ODpz5WilWl1zglDqUDXNRlr4XjlYai7QcCRvv41m2i6HcgyEFJUXfq72PoN4DT+ i5LA== X-Gm-Message-State: AOJu0YxG9foenjb+lsU7S+xApj5S+pRBGN9sdNCddsHKsyfxJA8jtW3s MSPU/X6fl6yXmdvQLmLO28DkBY1gMVTVp8kbHJPcISTMAaMsVaV/wdcaP2Lk X-Google-Smtp-Source: AGHT+IHtiRWHqvuiA4HKqBRkSgAiD6UB9BTy1YhOSv6LXXRsqN9H8ggj53SO1v1xEq+yfcPL9KJ7VQ== X-Received: by 2002:adf:e544:0:b0:339:5570:59e7 with SMTP id z4-20020adfe544000000b00339557059e7mr287989wrm.49.1706308743912; Fri, 26 Jan 2024 14:39:03 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id z5-20020adff745000000b003395642bc9bsm2104951wrp.117.2024.01.26.14.39.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 14:39:03 -0800 (PST) Message-ID: <5ba842b5005ce2099a4de2884d48bfc894ce4c2c.1706308737.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Jan 2024 22:38:52 +0000 Subject: [PATCH v2 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: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , 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 | 17 +++++++++-------- trailer.h | 13 +++++++++++++ 4 files changed, 42 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..bc7c82c5271 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, 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 71ea2bb67f8..5bcc9b0006c 100644 --- a/trailer.c +++ b/trailer.c @@ -1158,17 +1158,18 @@ 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); + + 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; } diff --git a/trailer.h b/trailer.h index c309b01323d..a47f18b1525 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 as part of a trailer block. A trailer block can be + * either 100% trailer lines, or mixed in with non-trailer lines (in + * which case at least 25% must be trailer lines). + */ + const char *raw; + + /* + * 1 if the raw line was parsed as a trailer line (key/val pair). + */ + int is_trailer; + /* private */ struct { struct trailer_info info; From patchwork Fri Jan 26 22:38:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13533439 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 6D79D5B5B0 for ; Fri, 26 Jan 2024 22:39:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308749; cv=none; b=LrvoBNF3r/VtbBjGL7Ya8NcxHbl7DSZLPfzUIEjzMP/mlFbTM+y+ISHrFIog4yohFTKL8bqKhfZCTv9Jpzx33ROEo68Jm3sjBJY9hRCwmWPjV9enXd3r6K2KT996j62bRkAryX1lveeJUrZ2fG8QYxdDXCwDlkQu8e5sPfAPm4g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308749; c=relaxed/simple; bh=+0TFUqA9sz6Ky6tbgIil3IGsgCN14v/W6mHcEs8v4A0=; h=Message-ID:In-Reply-To:References:From:Date:Subject:MIME-Version: Content-Type:To:Cc; b=be6i3UOn6W8dxUVQnx1gQ/QthNT6ExR3Nfd9yhZq8UPQGwvZQIPdAUsin9tWNSQ8rylLI8i82O0TS0UgfsYXsoMpUPDDklYYSXDnJzfAh4uwXucZlR7YJwdvaNe7E9YsNYu0TRN2DtFGMMDXxrXQoN6bqAV2sfjDyuLOSrE1DLQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LJiiduPN; arc=none smtp.client-ip=209.85.128.49 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="LJiiduPN" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-40e80046264so18767905e9.0 for ; Fri, 26 Jan 2024 14:39:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706308745; x=1706913545; 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=oTV4xRooiqYa2WvtjacIyyvfkiL/cL4magg3QWfhGIs=; b=LJiiduPNmAwIaAx80/5ppJv2I49i2Jwo+cPplWpq50VLBoZ/y50LmiuaU6JpAoM0AI Q5DnTASofET51u0A3mQuVKibD2KGErFn5UdmSH38USKtYAffoFLlyoSO48wWGESojISk P+GpgtPP6LPfpjOF58UXNdpd14xgus5t8EwomUHZy0Datw4ol+7BGAJZJshNkqQkLPib g3ksBrlirFDX9wXbJlRrwxAvIby+Yo+ucTrfwqzt6ZcbJnesZ7BWPJIx8cTjWfT67/Ye kVPthYo+WrKDYyuBrqP8SPWHgRFBhlsKmNDvO+SYq5SHBQedE0s8Bh+csebKfRbcB1pj 3HTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706308745; x=1706913545; 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=oTV4xRooiqYa2WvtjacIyyvfkiL/cL4magg3QWfhGIs=; b=RlClPrhtB6A1vwVIqEo7fxlHePluBeASQYWln2P7bOxLU1xGGrmRRkv8IpbHINzRt4 3yoi5ricBX0lg3P5ptFksb0kKS23eXjpKZmBxS5kPty6cmjaC8MkMR8om6mS70Y1OiE2 L3lex1PRJ+FwzLFiAMrWBA8GnRrdJgC7OU2rFWRQAZDL6tu+ah4ihtU+cdEntLSPv1Bx QEKY5NF7e9L7FUVjF6y5OEtOi2zysMLv+L5P7Z9Emu5i6mdWsQ543MUiejr5bl0HImtN aeRI3qc0fH8cNUjTHl5IzI870R6JUlopXcu1OcNWlV0NocUFlJ9Uuo5phHCZhFhZdIQm Hyow== X-Gm-Message-State: AOJu0YyOHsMVEpwf+Y5Zd+el2iSvX5FChWvdOJdZZrjKRRagUCAsxph4 KoljoGOdQoE5UMF+a81y2lpyQYQGGDhyiXjkaW3MW6AhtkUSP9GpWOxZuuPf X-Google-Smtp-Source: AGHT+IHl3IMiSvZi26DX0gfXM0iBE1c7t15vK5dj6Oru7+A0EPBhiLnb7nCD119CSf5Eqb7zsKLbAg== X-Received: by 2002:a05:600c:35d4:b0:40e:cd3e:2ff3 with SMTP id r20-20020a05600c35d400b0040ecd3e2ff3mr336712wmq.132.1706308745195; Fri, 26 Jan 2024 14:39:05 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id cc9-20020a5d5c09000000b00339443be964sm862685wrb.91.2024.01.26.14.39.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 14:39:04 -0800 (PST) Message-ID: In-Reply-To: References: Date: Fri, 26 Jan 2024 22:38:53 +0000 Subject: [PATCH v2 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: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , 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 5352ee65bd1..9e6ed6b65e2 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -141,7 +141,7 @@ static void interpret_trailers(const struct process_trailer_options *opts, LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; struct strbuf trailer_block = STRBUF_INIT; - struct trailer_info info; + struct trailer_info *info; FILE *outfile = stdout; trailer_config_init(); @@ -151,13 +151,13 @@ static void interpret_trailers(const struct process_trailer_options *opts, if (opts->in_place) outfile = create_in_place_tempfile(file); - parse_trailers(opts, &info, sb.buf, &head); + info = parse_trailers(opts, sb.buf, &head); /* Print the lines before the trailers */ if (!opts->only_trailers) - fwrite(sb.buf, 1, info.trailer_block_start, outfile); + fwrite(sb.buf, 1, trailer_block_start(info), outfile); - if (!opts->only_trailers && !info.blank_line_before_trailer) + if (!opts->only_trailers && !blank_line_before_trailer_block(info)) fprintf(outfile, "\n"); @@ -176,11 +176,12 @@ static void interpret_trailers(const struct process_trailer_options *opts, 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 5bcc9b0006c..63774cd068d 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(const struct process_trailer_options *opts, } } +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 struct process_trailer_options *opts, + const char *str) +{ + struct trailer_info *info = trailer_info_new(); + size_t end_of_log_message = 0, trailer_block_start = 0; + struct strbuf **trailer_lines, **ptr; + char **trailer_strings = NULL; + size_t nr = 0, alloc = 0; + char **last = NULL; + + trailer_config_init(); + + end_of_log_message = find_end_of_log_message(str, opts->no_divider); + trailer_block_start = find_trailer_block_start(str, end_of_log_message); + + trailer_lines = strbuf_split_buf(str + trailer_block_start, + end_of_log_message - trailer_block_start, + '\n', + 0); + for (ptr = trailer_lines; *ptr; ptr++) { + if (last && isspace((*ptr)->buf[0])) { + struct strbuf sb = STRBUF_INIT; + strbuf_attach(&sb, *last, strlen(*last), strlen(*last)); + strbuf_addbuf(&sb, *ptr); + *last = strbuf_detach(&sb, NULL); + continue; + } + ALLOC_GROW(trailer_strings, nr + 1, alloc); + trailer_strings[nr] = strbuf_detach(*ptr, NULL); + last = find_separator(trailer_strings[nr], separators) >= 1 + ? &trailer_strings[nr] + : NULL; + nr++; + } + strbuf_list_free(trailer_lines); + + info->blank_line_before_trailer = ends_with_blank_line(str, + trailer_block_start); + info->trailer_block_start = trailer_block_start; + info->trailer_block_end = end_of_log_message; + info->trailers = trailer_strings; + info->trailer_nr = nr; + + return info; +} + /* * Parse trailers in "str", populating the trailer info and "head" * linked list structure. */ -void parse_trailers(const struct process_trailer_options *opts, - struct trailer_info *info, - const char *str, - struct list_head *head) +struct trailer_info *parse_trailers(const struct process_trailer_options *opts, + const char *str, + struct list_head *head) { + struct trailer_info *info; struct strbuf tok = STRBUF_INIT; struct strbuf val = STRBUF_INIT; size_t i; - trailer_info_get(info, str, opts); + info = trailer_info_get(opts, str); for (i = 0; i < info->trailer_nr; i++) { int separator_pos; @@ -1062,6 +1135,8 @@ void parse_trailers(const struct process_trailer_options *opts, strbuf_detach(&val, NULL)); } } + + return info; } void free_trailers(struct list_head *trailers) @@ -1073,47 +1148,19 @@ void free_trailers(struct list_head *trailers) } } -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 struct process_trailer_options *opts, @@ -1129,30 +1177,30 @@ void format_trailers_from_commit(const struct process_trailer_options *opts, struct strbuf *out) { LIST_HEAD(head); - struct trailer_info info; - - parse_trailers(opts, &info, msg, &head); + struct trailer_info *info = parse_trailers(opts, msg, &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 + info->trailer_block_start, + info->trailer_block_end - info->trailer_block_start); } else format_trailers(opts, &head, 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); opts.no_divider = 1; - trailer_info_get(&iter->internal.info, msg, &opts); + iter->internal.info = internal; + iter->internal.info = trailer_info_get(&opts, msg); iter->internal.cur = 0; } @@ -1160,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.info->trailer_nr) { + line = iter->internal.info->trailers[iter->internal.cur++]; separator_pos = find_separator(line, separators); iter->is_trailer = (separator_pos > 0); @@ -1178,7 +1226,7 @@ int trailer_iterator_advance(struct trailer_iterator *iter) void trailer_iterator_release(struct trailer_iterator *iter) { - trailer_info_release(&iter->internal.info); + trailer_info_release(iter->internal.info); strbuf_release(&iter->val); strbuf_release(&iter->key); } diff --git a/trailer.h b/trailer.h index a47f18b1525..fff16a29a55 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(const struct process_trailer_options *opts, - struct trailer_info *info, - const char *str, - struct list_head *head); +struct trailer_info *parse_trailers(const struct process_trailer_options *opts, + const char *str, + struct list_head *head); -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 Fri Jan 26 22:38:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13533440 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 2F5395B5BC for ; Fri, 26 Jan 2024 22:39:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308750; cv=none; b=pxNgn8MhQSlhtrfJ/j+pgyAJTsgPF1kuIsZtr8P+2sTSLCVxyiBLKPG6KzfeuMUaY/nXyCROTS3gpdF7FRe5v0hAEqT6c213ge1LXKKiKAqF5G+g7yYnN9Q6YUAyegeKP4jKYNONIUDFqVzDSqcX6+dPElTKYRCQgD8zigNtaDs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308750; c=relaxed/simple; bh=WnrF3q81kl0a6dY8O4tnodfk/D+W3toR4KqH9+IiM40=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=DIcgcrZCvyGvM6S68YVVJYfBQxTdrnweUMj1ux6MMkz+MlVmwkLnXKHJdIIsZMQ8VYUZMjjgSP5Qr6aV+FchnUCNc8sjewCZqQ721P635/PqkZCV2KGdWAyIScmZPqTK4ZyEW1f8DiwxT5kbgkjjjkkZDEboyhPzgcg0Yj8bqeg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PVyJ4oVU; arc=none smtp.client-ip=209.85.128.53 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="PVyJ4oVU" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-40e7e2e04f0so14512415e9.1 for ; Fri, 26 Jan 2024 14:39:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706308746; x=1706913546; 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=Jd936NTPFGJQqJWUkCbeNYWJP/6lN7xzfBcmne0KTvw=; b=PVyJ4oVUk1jLs7VZaU2KIcwv8WAeosZ1ClTPdpq/pGjC/UympF7DvG0DWmeT9PBqnu WuDTBOfH19/vlMODVo4smOzpoZZ0bcyBT8isb8oC9A/jgrLcICJyUGTk79PxQfa5LGaA /h8WilCNImahlIgXPu756uyKVIpq612DQnw6Xp1XkbukuFAWJ+FDUI8EfuNceda7p6WY 5p9uLUd7Aj+0zu/nzbKgOQbSVKeq0NfZsSeez3YDlmNbQ+TVVK/d6e/dlIQPo3VKf1vj +H6qyVtJzIJ35+mfKTHm6xKicy+uwB/iJ2Psb61TTwpVBPsx0n4QZc46abQEkF8UDlES 8Qdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706308746; x=1706913546; 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=Jd936NTPFGJQqJWUkCbeNYWJP/6lN7xzfBcmne0KTvw=; b=rhVOW617qez4bPKPQxFm8LXCUcKNMLyVh1YxewG6DX2tODZkl/5pEmS4mLlzBEseXH 8L0KNm2iXBgBab0dDxtSbztokS8O1gSAXRRKWB/KzohVRUFb3F04p+6TzJ+nfnoYtM6Y Dvj5QSHfm21EG9ASCiINHw1zbVXcKPjGAfu+fQQx0CTj9mF8vcokp7qkB2+fZa3eyuq0 HhUergO1i4PxtMEWiDa5hmfJhYLATRBBHQ/sxgrut0yzQThGbP0uYmkC1zWAHSOHKl8G IfwEetriy1t4dvBjy57fR5F5VC+XTwvtcStjvjaQpYByJUo5VLp5qqIbSnvdye5p9pXM BLQw== X-Gm-Message-State: AOJu0YxPrsY/d9+OdUK8ssO3zue/gdq8mDmVF1xtFAptOi17JQ3mrMs9 uueMvJI0EHlQyhaUcIbObE3CSjMaQs97WyxbA8SM+9SvsFMqyWk2HFwufvOH X-Google-Smtp-Source: AGHT+IEs5uk6bs6IImBbQu6pUcYtLVpmFungh9SU1BW5IQzS/6Iwlh6StxEmW8K4w3MgFTdDSh+YiA== X-Received: by 2002:a05:600c:2301:b0:40e:e896:c6c7 with SMTP id 1-20020a05600c230100b0040ee896c6c7mr342115wmo.124.1706308745763; Fri, 26 Jan 2024 14:39:05 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p9-20020a05600c468900b0040ee4f38968sm1964750wmo.2.2024.01.26.14.39.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 14:39:05 -0800 (PST) Message-ID: <291aa83af5583b696eaa003625b3c4c27e1ae8ba.1706308737.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Jan 2024 22:38:54 +0000 Subject: [PATCH v2 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: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , 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 9e6ed6b65e2..9e41fa20b5f 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -140,8 +140,8 @@ static void interpret_trailers(const struct process_trailer_options *opts, { LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; - struct strbuf trailer_block = STRBUF_INIT; - struct trailer_info *info; + struct strbuf tb = STRBUF_INIT; + struct trailer_block *trailer_block; FILE *outfile = stdout; trailer_config_init(); @@ -151,13 +151,13 @@ static void interpret_trailers(const struct process_trailer_options *opts, if (opts->in_place) outfile = create_in_place_tempfile(file); - info = parse_trailers(opts, sb.buf, &head); + trailer_block = parse_trailers(opts, sb.buf, &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 struct process_trailer_options *opts, } /* Print trailer block. */ - format_trailers(opts, &head, &trailer_block); - fwrite(trailer_block.buf, 1, trailer_block.len, outfile); - strbuf_release(&trailer_block); + format_trailers(opts, &head, &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 63774cd068d..e2a48bea0ae 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(const struct process_trailer_options *opts, } } -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 struct process_trailer_options *opts, - const char *str) +static struct trailer_block *trailer_block_get(const struct process_trailer_options *opts, + const char *str) { - 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 struct process_trailer_option } 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 struct process_trailer_options *opts, - const char *str, - struct list_head *head) +struct trailer_block *parse_trailers(const struct process_trailer_options *opts, + const char *str, + 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(opts, str); + trailer_block = trailer_block_get(opts, str); - 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 struct process_trailer_options *opts, } } - return info; + return trailer_block; } void free_trailers(struct list_head *trailers) @@ -1148,28 +1149,28 @@ void free_trailers(struct list_head *trailers) } } -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 struct process_trailer_options *opts, @@ -1177,30 +1178,28 @@ void format_trailers_from_commit(const struct process_trailer_options *opts, struct strbuf *out) { LIST_HEAD(head); - struct trailer_info *info = parse_trailers(opts, msg, &head); + struct trailer_block *trailer_block = parse_trailers(opts, msg, &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(opts, &head, 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); opts.no_divider = 1; - iter->internal.info = internal; - iter->internal.info = trailer_info_get(&opts, msg); + iter->internal.trailer_block = trailer_block_get(&opts, msg); iter->internal.cur = 0; } @@ -1208,8 +1207,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); @@ -1226,7 +1225,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); } diff --git a/trailer.h b/trailer.h index fff16a29a55..be14dd17185 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 struct process_trailer_options *opts, - const char *str, - struct list_head *head); +struct trailer_block *parse_trailers(const struct process_trailer_options *opts, + const char *str, + 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 format_trailers(const struct process_trailer_options *opts, @@ -123,7 +123,7 @@ struct trailer_iterator { /* private */ struct { - struct trailer_info *info; + struct trailer_block *trailer_block; size_t cur; } internal; }; From patchwork Fri Jan 26 22:38:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13533441 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 CACAB5B5CA for ; Fri, 26 Jan 2024 22:39:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308750; cv=none; b=EkPv6bTGBcj40rVqZGss1RlkQMKWsFoW/5CRXyL4mk2hkW8TOto0xDsPGdGmeJCo8Z5mYE9BUICVCoBqZvBDae0X33p38egx5tSDTXJSZoiFfBeor+cqpSfRD58+E/igitmLlEoGBAksToPVXjZvLnrZU6TRcqu0jyv3gPbSf3s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308750; c=relaxed/simple; bh=PtCCuLonOCRt0Ru8sNJNdxyyZ378SkCCDwPTcLZcpTs=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=SfF8XYtwEFdMg0fJi9kXMmfsKqZTDytA6ygwqtDdWgoqCdfVWdfT7AGgjZ82B2XwRgnkcjil6JzTI1PApbCMO70izTCW4SHi5zniuJjd2N9wZ9UeoGEZC8vg+GsFRTvECWhFE8uIiiGTWCa8n7Ke+gUMpAuGIC+cESiDFhn1tns= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=S43ACXrH; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S43ACXrH" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-33921b8988fso976856f8f.3 for ; Fri, 26 Jan 2024 14:39:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706308746; x=1706913546; 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=yRk+3C8IALaEfI7ZIID5BEGl7Wn4rKwWqU0IKt/GCHc=; b=S43ACXrHY1ewrQLlqE+yLF3pmFfZlzoXcGpbVMYPEmK47RoF0s4qA0xSrtAFghFG5N iMRGI+aS19Y8GcWGgVdWCYI84bSLwuIX4r1GjhDo+roVPpP/k7qNy1Ex9aGxtSsB8+JY gi90w4rgtg+YZdfKOe++t9ypgBOUyyj/hKULmw3m6eJXWHhavZpTatWSSSv24FdOGIMe +ydEFR1miZ30Q7g/uU1B1FnPhOiv0+8dacvJpS7plivph8eFvQK/BT5WTvK2vy5cmpgz SdrRJSRBID58sTDUxgEzEoYkR6IzBWR53TnbQja8h0J+L21u84vbvqqghcMWjv35ejfx jeUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706308746; x=1706913546; 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=yRk+3C8IALaEfI7ZIID5BEGl7Wn4rKwWqU0IKt/GCHc=; b=mXgzcxfAxZTIeVwbAwE632N2OE0EvcESQCp5w9EWOmHPofUZZNvYm39r3QSLRa/EiL +fCCOnMfbDflDsDNww6wBa8bd/WT/G60yA88Ul5jS/eIkUW8JCovA8MWg0W9xAU8ZCag hQ2NUYkgz4pcUvfoX+ytgQajD2CSaJ9Qa0g0sBwsW8CoPxrVNPMAT4VGaAub18B8PYHf a33FjxfffEFzCwXsZo+5C92AWNoDEGvQwooYyY4tVzzN47hjn0opC+CkjbsNrHM6yIb1 JALjdBkQe9EWz/SKqIuIwc2Cllvhq9f1jCrJoFZWCnC1qUb2Ba9lRsyasnUgVyZlqU6/ 8hTA== X-Gm-Message-State: AOJu0YyepkF+jZnlJgdEOTy6Xl5UpJuZe6+60kzsT7lgNvqB98qAwt28 nmRKBpfxfmPjY/Sa7YPjeKw40szm4cPCUefwfUOnl0g5IrCO0BQOY2IhP12t X-Google-Smtp-Source: AGHT+IFBWH0avXxowxYwEM5XfIqHclQrXkABCKVb7Pk3atBrm9BnYV5o+FBel16WENlS3S4Q1E+xYg== X-Received: by 2002:a5d:4742:0:b0:337:5997:a4f0 with SMTP id o2-20020a5d4742000000b003375997a4f0mr186735wrs.9.1706308746370; Fri, 26 Jan 2024 14:39:06 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p8-20020a5d4588000000b00337d735c193sm2125721wrq.49.2024.01.26.14.39.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 14:39:06 -0800 (PST) Message-ID: <64ee07d0b53cf46d8f1aa1b7d26528f35a26b662.1706308737.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Jan 2024 22:38:55 +0000 Subject: [PATCH v2 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: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , 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 they have nothing to do with trailers themselves). The interpret-trailers builtin is the only caller of this function. Also rename "conf_info" to "trailer_conf" for readability, dropping the low-value "_info" suffix as we did earlier in this series for "trailer_info" to "trailer_block". Helped-by: Josh Steadmon 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 e2a48bea0ae..d362b9a604e 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 struct process_trailer_options *opts, 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, @@ -1215,8 +1214,7 @@ int trailer_iterator_advance(struct trailer_iterator *iter) 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 be14dd17185..c6aa96dd6be 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 struct process_trailer_options *opts, const char *str, struct list_head *head); From patchwork Fri Jan 26 22:38:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13533442 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDF7C5BAC5 for ; Fri, 26 Jan 2024 22:39:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308751; cv=none; b=lnJXFxHOi8r19CjCz3UMuu3dgUIpuN4JtUPQlEAbOg1pnNJUwiXr8GyUO4K1j6AbmDCvC19CJDSM7C/EGJtdADNJWiHlm/VDY20WYW9Op+kLeqpEYm7xbIBySIDvCfvaxLPY4J+7DZ179RbocMb6hXkIGgnYiXw0o1aBo75nPNU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308751; c=relaxed/simple; bh=oN2oky9mumCxLBV1B7pWYY0gU5lRdDWuLMfKZo9din4=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=BTxw0jx7qgg6vl5CT3QtyHiPzbv2Hu9rkHSjMGHTJb6O/7Q35er2TZzad6Gqa+BFN4z7a6MGVBtDrmqHXE8j2UWzsxr1j6w9gxrY2Py05hjYlF4mGj/h42d0GDI3hgnxdsFPJlGuOqEtamCUCDCM6Nc+SV4xi4YDu9sgJrlji/E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mdmNii1R; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mdmNii1R" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-40e7e2e04f0so14512735e9.1 for ; Fri, 26 Jan 2024 14:39:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706308748; x=1706913548; 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=ybg+brw47oXuSv6Jhnj/AdUtbgd9VH1MR8nNSBdOk90=; b=mdmNii1RPNeRl10SG8WM5UGuliXob1Pu33S6VtoI5SqixSIiJfnIHEX15gl1V28g1g uQVtpG7RlJ9YUgneRbbs+kqjvJIvEDooqz27oO6i/TVKYU+ucgI6yZCoyuUOQLotAdZ6 VWSXpTLbAnJUfFBa8BFoHJZTLDZ1WYBNc/7q+gJVnUsuHv85XhqnmYU+4MYaCs09tQrF mHA0fvkuLe47pvVMLLMIoMxm73mn4e4fy1K43Q6gzZVAzEUyYX6yJRGADVFHcQHayUd/ OOzcrjBcVYLbNN2g5HcArw7g0kUVa8w165bTTU+8Cg7lhlliOiFVGeeXMEwxi4/HyfUv hlfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706308748; x=1706913548; 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=ybg+brw47oXuSv6Jhnj/AdUtbgd9VH1MR8nNSBdOk90=; b=nxB7C/N4WBf/BcDdJKYx9MWhCTamnF5oRAFYNafhutI8rj8PjimlMWAhS9Q5jiakKg p+dmChdJrfp29df+PdHWVGkSiWoMBHWitoxKUe10mWtMO4rq1gprQ12VBbiJN51WVSXY dQ/3uxUJiqOVa6/NnmOFA8DfiwIeCTdkEG4yjO8VHAFbq7XQp/3vCDgOrkyIYNtu4JVY u3kx90R4926tYJRVSe/49iTgos3+njPB5yf7SW2VWvkVEMSRsPB41Pen3pwvC9DOR9Bw HRWeAOQT7zYmvnEa+tuqaksMvKEckkyyFfSH1kH6x0SBGKhfBW/6iGRw5G1R/p1vW9dK RGIg== X-Gm-Message-State: AOJu0YzxIipgtaj1VjsdQmPH/z8HWnX550cnCtF27FAch3QdT2S60ag6 97YNtYSp+A6vQkZNRpQqg1x3Uya9p/kn2BI/WDISkrqb9gnkJ5LszC6QC4Mu X-Google-Smtp-Source: AGHT+IHAs+Qo+thLnRGzcz3i3UyBLZOZrBxcS1XyJGAk55T/ZRBXrKq0glNuhK0fKAX7cPPPrsFhaQ== X-Received: by 2002:a05:600c:1c91:b0:40e:e743:87 with SMTP id k17-20020a05600c1c9100b0040ee7430087mr372542wms.49.1706308747570; Fri, 26 Jan 2024 14:39:07 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c4-20020a05600c0a4400b0040e621feca9sm3007045wmq.17.2024.01.26.14.39.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 14:39:06 -0800 (PST) Message-ID: <1b4bdde65bcb375ce9ef2345814330df92e6d932.1706308737.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Fri, 26 Jan 2024 22:38:56 +0000 Subject: [PATCH v2 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: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , 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 | 13 +++++- 3 files changed, 113 insertions(+), 51 deletions(-) diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 9e41fa20b5f..ad9b9a9f9ef 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; } @@ -135,7 +148,7 @@ static void read_input_file(struct strbuf *sb, const char *file) } static void interpret_trailers(const struct process_trailer_options *opts, - struct list_head *new_trailer_head, + struct list_head *arg_trailers, const char *file) { LIST_HEAD(head); @@ -144,8 +157,6 @@ static void interpret_trailers(const struct process_trailer_options *opts, 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 struct process_trailer_options *opts, 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 struct process_trailer_options *opts, 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(&opts, &trailers, argv[i]); + interpret_trailers(&opts, &arg_trailers, argv[i]); } else { if (opts.in_place) die(_("no input file given for in-place editing")); - interpret_trailers(&opts, &trailers, NULL); + interpret_trailers(&opts, &arg_trailers, NULL); } - new_trailers_clear(&trailers); + new_trailers_clear(&arg_trailers); return 0; } diff --git a/trailer.c b/trailer.c index d362b9a604e..1865d04bdf2 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 *trailers) } } +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 c6aa96dd6be..e01437160cf 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; @@ -95,7 +106,7 @@ void format_trailers(const struct process_trailer_options *opts, struct list_head *trailers, struct strbuf *out); void free_trailers(struct list_head *trailers); - +void new_trailers_clear(struct list_head *new_trailers); /* * Convenience function to format the trailers from the commit msg "msg" into * the strbuf "out". Reuses format_trailers internally. From patchwork Fri Jan 26 22:38:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Arver X-Patchwork-Id: 13533443 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 457025BAC4 for ; Fri, 26 Jan 2024 22:39:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308752; cv=none; b=uQhhPezSynZ6SfXzhVSjCDMO3CeCm93nwAYARPmp6/E66DWFFGvRTxdIz5PW/slh6u2A/m8rfXGGtMC2xmZZfPesseWxVSpOip3GrXL8XNrQPpcfYgsr2amB6/ThiNRc72ulRsAgCVofbgFiEMH7KByxayBJNhk3+9yFoOTVtTY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706308752; c=relaxed/simple; bh=9OWE2jYEz2zBDeQHs8Xq4vCE0xUCJd6dLtTfQvVL2GU=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=tE/y4G3eE455rc/hmC9S9qweojYoOhf61a3HkJOgPlEcYDECZA1CoxwIJiFfOQWn8HDzh9lDFXq6UyEr2m6RZZBk/30yu6pamL7t6/y9aPivEPkMxwmFW9ll2pD/P6DjxF/KhYaKAude1+p4ijUGIv8RTRBys2goj/tI/TCG3+k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dBnfqiBh; arc=none smtp.client-ip=209.85.128.48 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="dBnfqiBh" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-40e9ef9853bso13754675e9.1 for ; Fri, 26 Jan 2024 14:39:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706308748; x=1706913548; 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=QJEUR8ttb2FW4F01SMStunvlwDlg5leFii8d3URYToA=; b=dBnfqiBh/RX4/lHnaLOcSTbB3o3TjweGxZck1nJAKrhpBI4AB6c2Yw9Yjo/YLTGnxY 3aaoHKNo042WpgmZe9IT4AOfYFuJwez652/iLZqpnHxDPpOXdzoaRVFxkF2n4mJoJ89K L/zXMKmOrsQbhp3KMnTu0tzwwL4vdI21S+rybOqC47M76iCCwrYeOiPS8wOfOvtHX9lM a0iDNeGDZC5V+7JRCm7LBqvmhJGcms7KILPumjE7Cl97ThBidv5zrUFI0JnoY6eesNrA 8lO3j8ZoNnf88D1Eeth5Hy3c8PUo9nzJY8e7Exp2OUJPkmE2YIVqjNc44XSem4LL15Xg Ng5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706308748; x=1706913548; 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=QJEUR8ttb2FW4F01SMStunvlwDlg5leFii8d3URYToA=; b=oTOfupEDc+RARyfQ4AV6SWmxlrTd7SlOgVp73s8UJMb7z5ghTGqRVncWkSntBjHaER Xcmsk46pyvDWnQqLc7vrHg6qTLI6Dz0USBfyBZeMFrQYm3M+eDeRkEM7P/qAb5wxcxoZ G2Ow3h8PhMIfv6eBZzwEid7jJifpz66uEOs28rJmDUYBXszByc5jfgPeir8V/IS5zXQl hNiGoMygGEI6sTgXmQar020AmAU4dsjeWuyYlMq24h6QO2ruBJIlmcdYvHmwXGJtksQC jwH05TJSSovEdLbqOzmSWqeZHXxIfl3AiWiCfx3tD3N7H6lrbRwNbxV18rIrB2p2ehOK 6M0g== X-Gm-Message-State: AOJu0YwLvIiusYEndzQpwycYSLvQX3OsL6y3Rwc42WxTpgpTkVcGik/4 WkJBkjxMo2kgvIdvC7wJo/ANYcgJ+G5gQPLI2vUGu0mAF3WCbpDVvS20AJb2 X-Google-Smtp-Source: AGHT+IFMMDmB+42gLo5Oq6q+3/QkCUWxIIdq4i4XpP/50vp1rALiqR8Xp9iaIUszkbYqMpqoLIm04A== X-Received: by 2002:a05:600c:3650:b0:40e:e1c5:122a with SMTP id y16-20020a05600c365000b0040ee1c5122amr294177wmq.60.1706308748138; Fri, 26 Jan 2024 14:39:08 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p9-20020a05600c468900b0040ee4f38968sm1964812wmo.2.2024.01.26.14.39.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 14:39:07 -0800 (PST) Message-ID: In-Reply-To: References: Date: Fri, 26 Jan 2024 22:38:57 +0000 Subject: [PATCH v2 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: Christian Couder , Junio C Hamano , Emily Shaffer , Josh Steadmon , "Randall S. Becker" , 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 1865d04bdf2..8016908039f 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 e01437160cf..a71b770c564 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);