blob: 795d2fccfd9579500b798ac667feef2ee549f4b4 [file] [log] [blame]
Christian Couderb1d78d72014-10-13 20:16:28 +02001#ifndef TRAILER_H
2#define TRAILER_H
3
Paolo Bonzini51166b82017-08-01 11:03:31 +02004#include "list.h"
Jeff Kingf0939a02020-09-27 04:40:01 -04005#include "strbuf.h"
Elijah Newrenef3ca952018-08-15 10:54:05 -07006
Paolo Bonzini52fc3192017-07-24 10:22:43 +02007enum trailer_where {
Paolo Bonzini0ea52922017-08-01 11:03:32 +02008 WHERE_DEFAULT,
Paolo Bonzini52fc3192017-07-24 10:22:43 +02009 WHERE_END,
10 WHERE_AFTER,
11 WHERE_BEFORE,
12 WHERE_START
13};
14enum trailer_if_exists {
Paolo Bonzini0ea52922017-08-01 11:03:32 +020015 EXISTS_DEFAULT,
Paolo Bonzini52fc3192017-07-24 10:22:43 +020016 EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
17 EXISTS_ADD_IF_DIFFERENT,
18 EXISTS_ADD,
19 EXISTS_REPLACE,
20 EXISTS_DO_NOTHING
21};
22enum trailer_if_missing {
Paolo Bonzini0ea52922017-08-01 11:03:32 +020023 MISSING_DEFAULT,
Paolo Bonzini52fc3192017-07-24 10:22:43 +020024 MISSING_ADD,
25 MISSING_DO_NOTHING
26};
27
28int trailer_set_where(enum trailer_where *item, const char *value);
29int trailer_set_if_exists(enum trailer_if_exists *item, const char *value);
30int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
31
Jonathan Tane8c352c2016-11-02 10:29:19 -070032struct trailer_info {
33 /*
34 * True if there is a blank line before the location pointed to by
35 * trailer_start.
36 */
37 int blank_line_before_trailer;
38
39 /*
40 * Pointers to the start and end of the trailer block found. If there
41 * is no trailer block found, these 2 pointers point to the end of the
42 * input string.
43 */
44 const char *trailer_start, *trailer_end;
45
46 /*
47 * Array of trailers found.
48 */
49 char **trailers;
50 size_t trailer_nr;
51};
52
Paolo Bonzini51166b82017-08-01 11:03:31 +020053/*
54 * A list that represents newly-added trailers, such as those provided
55 * with the --trailer command line option of git-interpret-trailers.
56 */
57struct new_trailer_item {
58 struct list_head list;
59
60 const char *text;
Paolo Bonzini0ea52922017-08-01 11:03:32 +020061
62 enum trailer_where where;
63 enum trailer_if_exists if_exists;
64 enum trailer_if_missing if_missing;
Paolo Bonzini51166b82017-08-01 11:03:31 +020065};
66
Jeff King8abc8982017-08-10 14:03:58 -040067struct process_trailer_options {
68 int in_place;
69 int trim_empty;
Jeff King56c493e2017-08-15 06:23:21 -040070 int only_trailers;
Jeff Kingfdbdb642017-08-15 06:23:25 -040071 int only_input;
Jeff King00002392017-08-15 06:23:29 -040072 int unfold;
Jeff King1688c9a2018-08-22 20:49:56 -040073 int no_divider;
Ævar Arnfjörð Bjarmason9d87d5a2020-12-09 16:52:07 +010074 int key_only;
Anders Waldenborgd9b936d2019-01-28 22:33:35 +010075 int value_only;
Anders Waldenborg0b691d82019-01-28 22:33:37 +010076 const struct strbuf *separator;
Ævar Arnfjörð Bjarmason058761f2020-12-09 16:52:08 +010077 const struct strbuf *key_value_separator;
Anders Waldenborg250bea02019-01-28 22:33:34 +010078 int (*filter)(const struct strbuf *, void *);
79 void *filter_data;
Jeff King8abc8982017-08-10 14:03:58 -040080};
81
82#define PROCESS_TRAILER_OPTIONS_INIT {0}
83
84void process_trailers(const char *file,
85 const struct process_trailer_options *opts,
Paolo Bonzini51166b82017-08-01 11:03:31 +020086 struct list_head *new_trailer_head);
Christian Couderb1d78d72014-10-13 20:16:28 +020087
Jeff King00a21f52018-08-22 20:46:23 -040088void trailer_info_get(struct trailer_info *info, const char *str,
89 const struct process_trailer_options *opts);
Jonathan Tane8c352c2016-11-02 10:29:19 -070090
91void trailer_info_release(struct trailer_info *info);
92
Jeff Kinga388b102017-08-15 06:23:56 -040093/*
94 * Format the trailers from the commit msg "msg" into the strbuf "out".
95 * Note two caveats about "opts":
96 *
97 * - this is primarily a helper for pretty.c, and not
98 * all of the flags are supported.
99 *
100 * - this differs from process_trailers slightly in that we always format
101 * only the trailer block itself, even if the "only_trailers" option is not
102 * set.
103 */
104void format_trailers_from_commit(struct strbuf *out, const char *msg,
105 const struct process_trailer_options *opts);
106
Jeff Kingf0939a02020-09-27 04:40:01 -0400107/*
108 * An interface for iterating over the trailers found in a particular commit
109 * message. Use like:
110 *
111 * struct trailer_iterator iter;
112 * trailer_iterator_init(&iter, msg);
113 * while (trailer_iterator_advance(&iter))
114 * ... do something with iter.key and iter.val ...
115 * trailer_iterator_release(&iter);
116 */
117struct trailer_iterator {
118 struct strbuf key;
119 struct strbuf val;
120
121 /* private */
122 struct trailer_info info;
123 size_t cur;
124};
125
126/*
127 * Initialize "iter" in preparation for walking over the trailers in the commit
128 * message "msg". The "msg" pointer must remain valid until the iterator is
129 * released.
130 *
131 * After initializing, note that key/val will not yet point to any trailer.
132 * Call advance() to parse the first one (if any).
133 */
134void trailer_iterator_init(struct trailer_iterator *iter, const char *msg);
135
136/*
137 * Advance to the next trailer of the iterator. Returns 0 if there is no such
138 * trailer, and 1 otherwise. The key and value of the trailer can be
139 * fetched from the iter->key and iter->value fields (which are valid
140 * only until the next advance).
141 */
142int trailer_iterator_advance(struct trailer_iterator *iter);
143
144/*
145 * Release all resources associated with the trailer iteration.
146 */
147void trailer_iterator_release(struct trailer_iterator *iter);
148
Christian Couderb1d78d72014-10-13 20:16:28 +0200149#endif /* TRAILER_H */