]> git.ipfire.org Git - thirdparty/git.git/blob - trailer.h
CodingGuidelines: quote assigned value in 'local var=$val'
[thirdparty/git.git] / trailer.h
1 #ifndef TRAILER_H
2 #define TRAILER_H
3
4 #include "list.h"
5 #include "strbuf.h"
6
7 enum trailer_where {
8 WHERE_DEFAULT,
9 WHERE_END,
10 WHERE_AFTER,
11 WHERE_BEFORE,
12 WHERE_START
13 };
14 enum trailer_if_exists {
15 EXISTS_DEFAULT,
16 EXISTS_ADD_IF_DIFFERENT_NEIGHBOR,
17 EXISTS_ADD_IF_DIFFERENT,
18 EXISTS_ADD,
19 EXISTS_REPLACE,
20 EXISTS_DO_NOTHING
21 };
22 enum trailer_if_missing {
23 MISSING_DEFAULT,
24 MISSING_ADD,
25 MISSING_DO_NOTHING
26 };
27
28 int trailer_set_where(enum trailer_where *item, const char *value);
29 int trailer_set_if_exists(enum trailer_if_exists *item, const char *value);
30 int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
31
32 struct trailer_info {
33 /*
34 * True if there is a blank line before the location pointed to by
35 * trailer_block_start.
36 */
37 int blank_line_before_trailer;
38
39 /*
40 * Offsets to the trailer block start and end positions in the input
41 * string. If no trailer block is found, these are both set to the
42 * "true" end of the input (find_end_of_log_message()).
43 */
44 size_t trailer_block_start, trailer_block_end;
45
46 /*
47 * Array of trailers found.
48 */
49 char **trailers;
50 size_t trailer_nr;
51 };
52
53 /*
54 * A list that represents newly-added trailers, such as those provided
55 * with the --trailer command line option of git-interpret-trailers.
56 */
57 struct new_trailer_item {
58 struct list_head list;
59
60 const char *text;
61
62 enum trailer_where where;
63 enum trailer_if_exists if_exists;
64 enum trailer_if_missing if_missing;
65 };
66
67 struct process_trailer_options {
68 int in_place;
69 int trim_empty;
70 int only_trailers;
71 int only_input;
72 int unfold;
73 int no_divider;
74 int key_only;
75 int value_only;
76 const struct strbuf *separator;
77 const struct strbuf *key_value_separator;
78 int (*filter)(const struct strbuf *, void *);
79 void *filter_data;
80 };
81
82 #define PROCESS_TRAILER_OPTIONS_INIT {0}
83
84 void parse_trailers_from_config(struct list_head *config_head);
85
86 void parse_trailers_from_command_line_args(struct list_head *arg_head,
87 struct list_head *new_trailer_head);
88
89 void process_trailers_lists(struct list_head *head,
90 struct list_head *arg_head);
91
92 void parse_trailers(const struct process_trailer_options *,
93 struct trailer_info *,
94 const char *str,
95 struct list_head *head);
96
97 void trailer_info_get(const struct process_trailer_options *,
98 const char *str,
99 struct trailer_info *);
100
101 void trailer_info_release(struct trailer_info *info);
102
103 void trailer_config_init(void);
104 void format_trailers(const struct process_trailer_options *,
105 struct list_head *trailers,
106 struct strbuf *out);
107 void free_trailers(struct list_head *);
108
109 /*
110 * Format the trailers from the commit msg "msg" into the strbuf "out".
111 * Note two caveats about "opts":
112 *
113 * - this is primarily a helper for pretty.c, and not
114 * all of the flags are supported.
115 *
116 * - this differs from process_trailers slightly in that we always format
117 * only the trailer block itself, even if the "only_trailers" option is not
118 * set.
119 */
120 void format_trailers_from_commit(const struct process_trailer_options *opts,
121 const char *msg,
122 struct strbuf *out);
123
124 /*
125 * An interface for iterating over the trailers found in a particular commit
126 * message. Use like:
127 *
128 * struct trailer_iterator iter;
129 * trailer_iterator_init(&iter, msg);
130 * while (trailer_iterator_advance(&iter))
131 * ... do something with iter.key and iter.val ...
132 * trailer_iterator_release(&iter);
133 */
134 struct trailer_iterator {
135 struct strbuf key;
136 struct strbuf val;
137
138 /* private */
139 struct {
140 struct trailer_info info;
141 size_t cur;
142 } internal;
143 };
144
145 /*
146 * Initialize "iter" in preparation for walking over the trailers in the commit
147 * message "msg". The "msg" pointer must remain valid until the iterator is
148 * released.
149 *
150 * After initializing, note that key/val will not yet point to any trailer.
151 * Call advance() to parse the first one (if any).
152 */
153 void trailer_iterator_init(struct trailer_iterator *iter, const char *msg);
154
155 /*
156 * Advance to the next trailer of the iterator. Returns 0 if there is no such
157 * trailer, and 1 otherwise. The key and value of the trailer can be
158 * fetched from the iter->key and iter->value fields (which are valid
159 * only until the next advance).
160 */
161 int trailer_iterator_advance(struct trailer_iterator *iter);
162
163 /*
164 * Release all resources associated with the trailer iteration.
165 */
166 void trailer_iterator_release(struct trailer_iterator *iter);
167
168 #endif /* TRAILER_H */