trailer: add data structures and basic functions
We will use a doubly linked list to store all information about trailers and their configuration. This way we can easily remove or add trailers to or from trailer lists while traversing the lists in either direction. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
Oct 13, 2014 at 20:16 UTC
9385b5d7065351104c7bfd3d1e53a28207b8b7ed
2 files changed
+70
Makefile
+1
@@ -763,6 +763,7 @@ LIB_OBJS += submodule.o
763
LIB_OBJS += symlinks.o
764
LIB_OBJS += tag.o
765
LIB_OBJS += trace.o
766
+LIB_OBJS += trailer.o
767
LIB_OBJS += transport.o
768
LIB_OBJS += transport-helper.o
769
LIB_OBJS += tree-diff.o
trailer.c
new
+69
@@ -0,0 +1,69 @@
1
+#include "cache.h"
2
+/*
3
+ * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
4
+ */
5
+
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 };
10
+
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;
18
+};
19
+
20
+static struct conf_info default_conf_info;
21
+
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;
28
+};
29
+
30
+static struct trailer_item *first_conf_item;
31
+
32
+static char *separators = ":";
33
+
34
+static int after_or_end(enum action_where where)
35
+{
36
+ return (where == WHERE_AFTER) || (where == WHERE_END);
37
+}
38
+
39
+/*
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.
44
+ */
45
+static size_t token_len_without_separator(const char *token, size_t len)
46
+{
47
+ while (len > 0 && !isalnum(token[len - 1]))
48
+ len--;
49
+ return len;
50
+}
51
+
52
+static int same_token(struct trailer_item *a, struct trailer_item *b)
53
+{
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;
57
+
58
+ return !strncasecmp(a->token, b->token, min_len);
59
+}
60
+
61
+static int same_value(struct trailer_item *a, struct trailer_item *b)
62
+{
63
+ return !strcasecmp(a->value, b->value);
64
+}
65
+
66
+static int same_trailer(struct trailer_item *a, struct trailer_item *b)
67
+{
68
+ return same_token(a, b) && same_value(a, b);
69
+}