Skip to content

Commit 9385b5d

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ce1d3a9 commit 9385b5d

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ LIB_OBJS += submodule.o
763763
LIB_OBJS += symlinks.o
764764
LIB_OBJS += tag.o
765765
LIB_OBJS += trace.o
766+
LIB_OBJS += trailer.o
766767
LIB_OBJS += transport.o
767768
LIB_OBJS += transport-helper.o
768769
LIB_OBJS += tree-diff.o

trailer.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "cache.h"
2+
/*
3+
* Copyright (c) 2013, 2014 Christian Couder <[email protected]>
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+
}

0 commit comments

Comments
 (0)