trailer: streamline trailer item create and add
Currently, creation and addition (to a list) of trailer items are spread across multiple functions. Streamline this by only having 2 functions: one to parse the user-supplied string, and one to add the parsed information to a list. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Tan committed
Oct 20, 2016 at 14:39 UTC
63ab3f348458212c3e5d493465acd615303fb45a
1 file changed
+60
-70
trailer.c
+60
-70
@@ -500,10 +500,31 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
500
return 0;
501
}
502
503
-static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)
503
+static const char *token_from_item(struct trailer_item *item, char *tok)
504
+{
505
+ if (item->conf.key)
506
+ return item->conf.key;
507
+ if (tok)
508
+ return tok;
509
+ return item->conf.name;
510
+}
511
+
512
+static int token_matches_item(const char *tok, struct trailer_item *item, int tok_len)
513
+{
514
+ if (!strncasecmp(tok, item->conf.name, tok_len))
515
+ return 1;
516
+ return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
517
+}
518
+
519
+static int parse_trailer(struct strbuf *tok, struct strbuf *val,
520
+ const struct conf_info **conf, const char *trailer)
521
{
522
size_t len;
523
struct strbuf seps = STRBUF_INIT;
524
+ struct trailer_item *item;
525
+ int tok_len;
526
+ struct list_head *pos;
527
+
528
strbuf_addstr(&seps, separators);
529
strbuf_addch(&seps, '=');
530
len = strcspn(trailer, seps.buf);
@@ -523,74 +544,31 @@ static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tra
544
strbuf_addstr(tok, trailer);
545
strbuf_trim(tok);
546
}
526
- return 0;
527
-}
528
-
529
-static const char *token_from_item(struct trailer_item *item, char *tok)
530
-{
531
- if (item->conf.key)
532
- return item->conf.key;
533
- if (tok)
534
- return tok;
535
- return item->conf.name;
536
-}
537
-
538
-static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
539
- char *tok, char *val)
540
-{
541
- struct trailer_item *new = xcalloc(sizeof(*new), 1);
542
- new->value = val ? val : xstrdup("");
543
-
544
- if (conf_item) {
545
- duplicate_conf(&new->conf, &conf_item->conf);
546
- new->token = xstrdup(token_from_item(conf_item, tok));
547
- free(tok);
548
- } else {
549
- duplicate_conf(&new->conf, &default_conf_info);
550
- new->token = tok;
551
- }
552
-
553
- return new;
554
-}
555
-
556
-static int token_matches_item(const char *tok, struct trailer_item *item, int tok_len)
557
-{
558
- if (!strncasecmp(tok, item->conf.name, tok_len))
559
- return 1;
560
- return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
561
-}
562
-
563
-static struct trailer_item *create_trailer_item(const char *string)
564
-{
565
- struct strbuf tok = STRBUF_INIT;
566
- struct strbuf val = STRBUF_INIT;
567
- struct trailer_item *item;
568
- int tok_len;
569
- struct list_head *pos;
570
-
571
- if (parse_trailer(&tok, &val, string))
572
- return NULL;
573
-
574
- tok_len = token_len_without_separator(tok.buf, tok.len);
547
548
/* Lookup if the token matches something in the config */
549
+ tok_len = token_len_without_separator(tok->buf, tok->len);
550
+ *conf = &default_conf_info;
551
list_for_each(pos, &conf_head) {
552
item = list_entry(pos, struct trailer_item, list);
579
- if (token_matches_item(tok.buf, item, tok_len))
580
- return new_trailer_item(item,
581
- strbuf_detach(&tok, NULL),
582
- strbuf_detach(&val, NULL));
553
+ if (token_matches_item(tok->buf, item, tok_len)) {
554
+ char *tok_buf = strbuf_detach(tok, NULL);
555
+ *conf = &item->conf;
556
+ strbuf_addstr(tok, token_from_item(item, tok_buf));
557
+ free(tok_buf);
558
+ break;
559
+ }
560
}
561
585
- return new_trailer_item(NULL,
586
- strbuf_detach(&tok, NULL),
587
- strbuf_detach(&val, NULL));
562
+ return 0;
563
}
564
590
-static void add_trailer_item(struct list_head *head, struct trailer_item *new)
565
+static void add_trailer_item(struct list_head *head, char *tok, char *val,
566
+ const struct conf_info *conf)
567
{
592
- if (!new)
593
- return;
568
+ struct trailer_item *new = xcalloc(sizeof(*new), 1);
569
+ new->token = tok;
570
+ new->value = val;
571
+ duplicate_conf(&new->conf, conf);
572
list_add_tail(&new->list, head);
573
}
574
@@ -599,21 +577,28 @@ static void process_command_line_args(struct list_head *arg_head,
577
{
578
struct string_list_item *tr;
579
struct trailer_item *item;
580
+ struct strbuf tok = STRBUF_INIT;
581
+ struct strbuf val = STRBUF_INIT;
582
+ const struct conf_info *conf;
583
struct list_head *pos;
584
585
/* Add a trailer item for each configured trailer with a command */
586
list_for_each(pos, &conf_head) {
587
item = list_entry(pos, struct trailer_item, list);
607
- if (item->conf.command) {
608
- struct trailer_item *new = new_trailer_item(item, NULL, NULL);
609
- add_trailer_item(arg_head, new);
610
- }
588
+ if (item->conf.command)
589
+ add_trailer_item(arg_head,
590
+ xstrdup(token_from_item(item, NULL)),
591
+ xstrdup(""),
592
+ &item->conf);
593
}
594
595
/* Add a trailer item for each trailer on the command line */
596
for_each_string_list_item(tr, trailers) {
615
- struct trailer_item *new = create_trailer_item(tr->string);
616
- add_trailer_item(arg_head, new);
597
+ if (!parse_trailer(&tok, &val, &conf, tr->string))
598
+ add_trailer_item(arg_head,
599
+ strbuf_detach(&tok, NULL),
600
+ strbuf_detach(&val, NULL),
601
+ conf);
602
}
603
}
604
@@ -734,6 +719,9 @@ static int process_input_file(FILE *outfile,
719
{
720
int count = 0;
721
int patch_start, trailer_start, trailer_end, i;
722
+ struct strbuf tok = STRBUF_INIT;
723
+ struct strbuf val = STRBUF_INIT;
724
+ const struct conf_info *conf;
725
726
/* Get the line count */
727
while (lines[count])
@@ -751,10 +739,12 @@ static int process_input_file(FILE *outfile,
739
740
/* Parse trailer lines */
741
for (i = trailer_start; i < trailer_end; i++) {
754
- if (lines[i]->buf[0] != comment_line_char) {
755
- struct trailer_item *new = create_trailer_item(lines[i]->buf);
756
- add_trailer_item(head, new);
757
- }
742
+ if (lines[i]->buf[0] != comment_line_char &&
743
+ !parse_trailer(&tok, &val, &conf, lines[i]->buf))
744
+ add_trailer_item(head,
745
+ strbuf_detach(&tok, NULL),
746
+ strbuf_detach(&val, NULL),
747
+ conf);
748
}
749
750
return trailer_end;