From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christian Couder Subject: [PATCH v16 06/11] trailer: put all the processing together and print Date: Mon, 13 Oct 2014 20:16:28 +0200 Message-ID: <20141013181634.27329.50950.chriscool@tuxfamily.org> References: <20141013181428.27329.86081.chriscool@tuxfamily.org> Cc: git@vger.kernel.org, Johan Herland , Josh Triplett , Thomas Rast , Michael Haggerty , Dan Carpenter , Greg Kroah-Hartman , Jeff King , Eric Sunshine , Ramsay Jones , Jonathan Nieder , Marc Branchaud , Michael S Tsirkin To: Junio C Hamano X-From: git-owner@vger.kernel.org Mon Oct 13 20:20:28 2014 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XdkEJ-0007jJ-2m for gcvg-git-2@plane.gmane.org; Mon, 13 Oct 2014 20:20:23 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754606AbaJMSUL (ORCPT ); Mon, 13 Oct 2014 14:20:11 -0400 Received: from [194.158.98.15] ([194.158.98.15]:33963 "EHLO mail-2y.bbox.fr" rhost-flags-FAIL-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1754782AbaJMSTd (ORCPT ); Mon, 13 Oct 2014 14:19:33 -0400 Received: from [127.0.1.1] (cha92-h01-128-78-31-246.dsl.sta.abo.bbox.fr [128.78.31.246]) by mail-2y.bbox.fr (Postfix) with ESMTP id A87C5145; Mon, 13 Oct 2014 20:19:11 +0200 (CEST) X-git-sha1: 073b95b00542ed227c8c33175b9b16e1c23c32ea X-Mailer: git-mail-commits v0.5.2 In-Reply-To: <20141013181428.27329.86081.chriscool@tuxfamily.org> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This patch adds the process_trailers() function that calls all the previously added processing functions and then prints the results on the standard output. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- trailer.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ trailer.h | 6 ++++++ 2 files changed, 75 insertions(+) create mode 100644 trailer.h diff --git a/trailer.c b/trailer.c index 4f0de3b..b0be0d7 100644 --- a/trailer.c +++ b/trailer.c @@ -1,5 +1,6 @@ #include "cache.h" #include "string-list.h" +#include "trailer.h" /* * Copyright (c) 2013, 2014 Christian Couder */ @@ -87,6 +88,35 @@ static void free_trailer_item(struct trailer_item *item) free(item); } +static char last_non_space_char(const char *s) +{ + int i; + for (i = strlen(s) - 1; i >= 0; i--) + if (!isspace(s[i])) + return s[i]; + return '\0'; +} + +static void print_tok_val(const char *tok, const char *val) +{ + char c = last_non_space_char(tok); + if (!c) + return; + if (strchr(separators, c)) + printf("%s%s\n", tok, val); + else + printf("%s%c %s\n", tok, separators[0], val); +} + +static void print_all(struct trailer_item *first, int trim_empty) +{ + struct trailer_item *item; + for (item = first; item; item = item->next) { + if (!trim_empty || strlen(item->value) > 0) + print_tok_val(item->token, item->value); + } +} + static void update_last(struct trailer_item **last) { if (*last) @@ -697,3 +727,42 @@ static int process_input_file(struct strbuf **lines, return patch_start; } + +static void free_all(struct trailer_item **first) +{ + while (*first) { + struct trailer_item *item = remove_first(first); + free_trailer_item(item); + } +} + +void process_trailers(const char *file, int trim_empty, struct string_list *trailers) +{ + struct trailer_item *in_tok_first = NULL; + struct trailer_item *in_tok_last = NULL; + struct trailer_item *arg_tok_first; + struct strbuf **lines; + int patch_start; + + /* Default config must be setup first */ + git_config(git_trailer_default_config, NULL); + git_config(git_trailer_config, NULL); + + lines = read_input_file(file); + + /* Print the lines before the trailers */ + patch_start = process_input_file(lines, &in_tok_first, &in_tok_last); + + arg_tok_first = process_command_line_args(trailers); + + process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first); + + print_all(in_tok_first, trim_empty); + + free_all(&in_tok_first); + + /* Print the lines after the trailers as is */ + print_lines(lines, patch_start, INT_MAX); + + strbuf_list_free(lines); +} diff --git a/trailer.h b/trailer.h new file mode 100644 index 0000000..8eb25d5 --- /dev/null +++ b/trailer.h @@ -0,0 +1,6 @@ +#ifndef TRAILER_H +#define TRAILER_H + +void process_trailers(const char *file, int trim_empty, struct string_list *trailers); + +#endif /* TRAILER_H */ -- 2.1.0.rc0.248.gb91fdbc