trailers: export action enums and corresponding lookup functions
Separate the mechanical changes out of the next patch. The functions are changed to take a pointer to enum, because struct conf_info is not going to be public. Set the default values explicitly in default_conf_info, since they are not anymore close to default_conf_info and it's not obvious which constant has value 0. With the next patches, in fact, the values will not be zero anymore! Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Paolo Bonzini committed
Jul 24, 2017 at 10:22 UTC
52fc319d4dfdbf67fe298908d8519be271ca4eb4
2 files changed
+55
-32
trailer.c
+33
-32
@@ -10,18 +10,13 @@
10
* Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
11
*/
12
13
-enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
14
-enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
15
- EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
16
-enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
17
-
13
struct conf_info {
14
char *name;
15
char *key;
16
char *command;
22
- enum action_where where;
23
- enum action_if_exists if_exists;
24
- enum action_if_missing if_missing;
17
+ enum trailer_where where;
18
+ enum trailer_if_exists if_exists;
19
+ enum trailer_if_missing if_missing;
20
};
21
22
static struct conf_info default_conf_info;
@@ -63,7 +58,7 @@ static const char *git_generated_prefixes[] = {
58
pos != (head); \
59
pos = is_reverse ? pos->prev : pos->next)
60
66
-static int after_or_end(enum action_where where)
61
+static int after_or_end(enum trailer_where where)
62
{
63
return (where == WHERE_AFTER) || (where == WHERE_END);
64
}
@@ -201,7 +196,7 @@ static int check_if_different(struct trailer_item *in_tok,
196
int check_all,
197
struct list_head *head)
198
{
204
- enum action_where where = arg_tok->conf.where;
199
+ enum trailer_where where = arg_tok->conf.where;
200
struct list_head *next_head;
201
do {
202
if (same_trailer(in_tok, arg_tok))
@@ -306,7 +301,7 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
301
static void apply_arg_if_missing(struct list_head *head,
302
struct arg_item *arg_tok)
303
{
309
- enum action_where where;
304
+ enum trailer_where where;
305
struct trailer_item *to_add;
306
307
switch (arg_tok->conf.if_missing) {
@@ -331,7 +326,7 @@ static int find_same_and_apply_arg(struct list_head *head,
326
struct trailer_item *in_tok;
327
struct trailer_item *on_tok;
328
334
- enum action_where where = arg_tok->conf.where;
329
+ enum trailer_where where = arg_tok->conf.where;
330
int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
331
int backwards = after_or_end(where);
332
struct trailer_item *start_tok;
@@ -373,44 +368,44 @@ static void process_trailers_lists(struct list_head *head,
368
}
369
}
370
376
-static int set_where(struct conf_info *item, const char *value)
371
+int trailer_set_where(enum trailer_where *item, const char *value)
372
{
373
if (!strcasecmp("after", value))
379
- item->where = WHERE_AFTER;
374
+ *item = WHERE_AFTER;
375
else if (!strcasecmp("before", value))
381
- item->where = WHERE_BEFORE;
376
+ *item = WHERE_BEFORE;
377
else if (!strcasecmp("end", value))
383
- item->where = WHERE_END;
378
+ *item = WHERE_END;
379
else if (!strcasecmp("start", value))
385
- item->where = WHERE_START;
380
+ *item = WHERE_START;
381
else
382
return -1;
383
return 0;
384
}
385
391
-static int set_if_exists(struct conf_info *item, const char *value)
386
+int trailer_set_if_exists(enum trailer_if_exists *item, const char *value)
387
{
388
if (!strcasecmp("addIfDifferent", value))
394
- item->if_exists = EXISTS_ADD_IF_DIFFERENT;
389
+ *item = EXISTS_ADD_IF_DIFFERENT;
390
else if (!strcasecmp("addIfDifferentNeighbor", value))
396
- item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
391
+ *item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
392
else if (!strcasecmp("add", value))
398
- item->if_exists = EXISTS_ADD;
393
+ *item = EXISTS_ADD;
394
else if (!strcasecmp("replace", value))
400
- item->if_exists = EXISTS_REPLACE;
395
+ *item = EXISTS_REPLACE;
396
else if (!strcasecmp("doNothing", value))
402
- item->if_exists = EXISTS_DO_NOTHING;
397
+ *item = EXISTS_DO_NOTHING;
398
else
399
return -1;
400
return 0;
401
}
402
408
-static int set_if_missing(struct conf_info *item, const char *value)
403
+int trailer_set_if_missing(enum trailer_if_missing *item, const char *value)
404
{
405
if (!strcasecmp("doNothing", value))
411
- item->if_missing = MISSING_DO_NOTHING;
406
+ *item = MISSING_DO_NOTHING;
407
else if (!strcasecmp("add", value))
413
- item->if_missing = MISSING_ADD;
408
+ *item = MISSING_ADD;
409
else
410
return -1;
411
return 0;
@@ -470,15 +465,18 @@ static int git_trailer_default_config(const char *conf_key, const char *value, v
465
variable_name = strrchr(trailer_item, '.');
466
if (!variable_name) {
467
if (!strcmp(trailer_item, "where")) {
473
- if (set_where(&default_conf_info, value) < 0)
468
+ if (trailer_set_where(&default_conf_info.where,
469
+ value) < 0)
470
warning(_("unknown value '%s' for key '%s'"),
471
value, conf_key);
472
} else if (!strcmp(trailer_item, "ifexists")) {
477
- if (set_if_exists(&default_conf_info, value) < 0)
473
+ if (trailer_set_if_exists(&default_conf_info.if_exists,
474
+ value) < 0)
475
warning(_("unknown value '%s' for key '%s'"),
476
value, conf_key);
477
} else if (!strcmp(trailer_item, "ifmissing")) {
481
- if (set_if_missing(&default_conf_info, value) < 0)
478
+ if (trailer_set_if_missing(&default_conf_info.if_missing,
479
+ value) < 0)
480
warning(_("unknown value '%s' for key '%s'"),
481
value, conf_key);
482
} else if (!strcmp(trailer_item, "separators")) {
@@ -532,15 +530,15 @@ static int git_trailer_config(const char *conf_key, const char *value, void *cb)
530
conf->command = xstrdup(value);
531
break;
532
case TRAILER_WHERE:
535
- if (set_where(conf, value))
533
+ if (trailer_set_where(&conf->where, value))
534
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
535
break;
536
case TRAILER_IF_EXISTS:
539
- if (set_if_exists(conf, value))
537
+ if (trailer_set_if_exists(&conf->if_exists, value))
538
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
539
break;
540
case TRAILER_IF_MISSING:
543
- if (set_if_missing(conf, value))
541
+ if (trailer_set_if_missing(&conf->if_missing, value))
542
warning(_("unknown value '%s' for key '%s'"), value, conf_key);
543
break;
544
default:
@@ -555,6 +553,9 @@ static void ensure_configured(void)
553
return;
554
555
/* Default config must be setup first */
556
+ default_conf_info.where = WHERE_END;
557
+ default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
558
+ default_conf_info.if_missing = MISSING_ADD;
559
git_config(git_trailer_default_config, NULL);
560
git_config(git_trailer_config, NULL);
561
configured = 1;
trailer.h
+22
@@ -1,6 +1,28 @@
1
#ifndef TRAILER_H
2
#define TRAILER_H
3
4
+enum trailer_where {
5
+ WHERE_END,
6
+ WHERE_AFTER,
7
+ WHERE_BEFORE,
8
+ WHERE_START
9
+};
10
+enum trailer_if_exists {
11
+ EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
12
+ EXISTS_ADD_IF_DIFFERENT,
13
+ EXISTS_ADD,
14
+ EXISTS_REPLACE,
15
+ EXISTS_DO_NOTHING
16
+};
17
+enum trailer_if_missing {
18
+ MISSING_ADD,
19
+ MISSING_DO_NOTHING
20
+};
21
+
22
+int trailer_set_where(enum trailer_where *item, const char *value);
23
+int trailer_set_if_exists(enum trailer_if_exists *item, const char *value);
24
+int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
25
+
26
struct trailer_info {
27
/*
28
* True if there is a blank line before the location pointed to by