trailer: add data structures and basic functions
[git/mingw.git] / trailer.c
blobac323b12010ead397105d32b1bc94256fcb0ff8c
1 #include "cache.h"
2 /*
3 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
4 */
6 enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
7 enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
8 EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
9 enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
11 struct conf_info {
12 char *name;
13 char *key;
14 char *command;
15 enum action_where where;
16 enum action_if_exists if_exists;
17 enum action_if_missing if_missing;
20 static struct conf_info default_conf_info;
22 struct trailer_item {
23 struct trailer_item *previous;
24 struct trailer_item *next;
25 const char *token;
26 const char *value;
27 struct conf_info conf;
30 static struct trailer_item *first_conf_item;
32 static char *separators = ":";
34 static int after_or_end(enum action_where where)
36 return (where == WHERE_AFTER) || (where == WHERE_END);
40 * Return the length of the string not including any final
41 * punctuation. E.g., the input "Signed-off-by:" would return
42 * 13, stripping the trailing punctuation but retaining
43 * internal punctuation.
45 static size_t token_len_without_separator(const char *token, size_t len)
47 while (len > 0 && !isalnum(token[len - 1]))
48 len--;
49 return len;
52 static int same_token(struct trailer_item *a, struct trailer_item *b)
54 size_t a_len = token_len_without_separator(a->token, strlen(a->token));
55 size_t b_len = token_len_without_separator(b->token, strlen(b->token));
56 size_t min_len = (a_len > b_len) ? b_len : a_len;
58 return !strncasecmp(a->token, b->token, min_len);
61 static int same_value(struct trailer_item *a, struct trailer_item *b)
63 return !strcasecmp(a->value, b->value);
66 static int same_trailer(struct trailer_item *a, struct trailer_item *b)
68 return same_token(a, b) && same_value(a, b);