deps/libgit2/src/libgit2/trailer.c

filter: Statementstmt 74.5% 149 / 200 Branchbran 54.1% 78 / 144 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.9% 227 / 344 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/refs.c ↑ Summary deps/libgit2/src/libgit2/filter.c »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #include "array.h"
8 #include "common.h"
9 #include "git2/message.h"
10  
11 #include <stddef.h>
12 #include <string.h>
13 #include <ctype.h>
14  
15 #define COMMENT_LINE_CHAR '#'
16 #define TRAILER_SEPARATORS ":"
17  
18 static const char *const git_generated_prefixes[] = {
19 "Signed-off-by: ",
20 "(cherry picked from commit ",
21 NULL
22 };
23  
24 8 static int is_blank_line(const char *str)
25 {
26 8 const char *s = str;
27 8 ▶ while (*s && *s != '\n' && isspace(*s))
Branch: 3 branches
BranchTrueFalseTotal
while (*s && *s != '\n' && isspace(*s)) 8 0 8
while (*s && *s != '\n' && isspace(*s)) 6 2 8
while (*s && *s != '\n' && isspace(*s)) 0 6 6
28 0 s++;
29 8 ▶ return !*s || *s == '\n';
Branch: 2 branches
BranchTrueFalseTotal
return !*s || *s == '\n'; 8 0 8
return !*s || *s == '\n'; 2 6 8
30 }
31  
32 10 static const char *next_line(const char *str)
33 {
34 10 const char *nl = strchr(str, '\n');
35  
36 10 ▶ if (nl) {
Branch: 1 branch
BranchTrueFalseTotal
if (nl) { 10 0 10
37 10 return nl + 1;
38 } else {
39 /* return pointer to the NUL terminator: */
40 0 return str + strlen(str);
41 }
42 }
43  
44 /*
45 * Return the position of the start of the last line. If len is 0, return 0.
46 */
47 6 static bool last_line(size_t *out, const char *buf, size_t len)
48 {
49 size_t i;
50  
51 6 *out = 0;
52  
53 6 ▶ if (len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) 0 6 6
54 0 return false;
55 6 ▶ if (len == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (len == 1) 0 6 6
56 0 return true;
57  
58 /*
59 * Skip the last character (in addition to the null terminator),
60 * because if the last character is a newline, it is considered as part
61 * of the last line anyway.
62 */
63 6 i = len - 2;
64  
65 84 ▶ for (; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (; i > 0; i--) { 83 1 84
66 83 ▶ if (buf[i] == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[i] == '\n') { 5 78 83
67 5 *out = i + 1;
68 5 return true;
69 }
70 }
71 1 return true;
72 }
73  
74 /*
75 * If the given line is of the form
76 * "<token><optional whitespace><separator>..." or "<separator>...", sets out
77 * to the location of the separator and returns true. Otherwise, returns
78 * false. The optional whitespace is allowed there primarily to allow things
79 * like "Bug #43" where <token> is "Bug" and <separator> is "#".
80 *
81 * The separator-starts-line case (in which this function returns true and
82 * sets out to 0) is distinguished from the non-well-formed-line case (in
83 * which this function returns false) because some callers of this function
84 * need such a distinction.
85 */
86 2 static bool find_separator(size_t *out, const char *line, const char *separators)
87 {
88 2 int whitespace_found = 0;
89 const char *c;
90 4 ▶ for (c = line; *c; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = line; *c; c++) { 4 0 4
91 4 ▶ if (strchr(separators, *c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(separators, *c)) { 2 2 4
92 2 *out = c - line;
93 2 return true;
94 }
95  
96 2 ▶ if (!whitespace_found && (isalnum(*c) || *c == '-'))
Branch: 3 branches
BranchTrueFalseTotal
if (!whitespace_found && (isalnum(*c) || *c == '-')) 2 0 2
if (!whitespace_found && (isalnum(*c) || *c == '-')) 0 2 2
if (!whitespace_found && (isalnum(*c) || *c == '-')) 0 0 0
97 2 continue;
98 0 ▶ if (c != line && (*c == ' ' || *c == '\t')) {
Branch: 3 branches
BranchTrueFalseTotal
if (c != line && (*c == ' ' || *c == '\t')) { 0 0 0
if (c != line && (*c == ' ' || *c == '\t')) { 0 0 0
if (c != line && (*c == ' ' || *c == '\t')) { 0 0 0
99 0 whitespace_found = 1;
100 0 continue;
101 }
102 0 break;
103 }
104 0 return false;
105 }
106  
107 /*
108 * Inspect the given string and determine the true "end" of the log message, in
109 * order to find where to put a new Signed-off-by: line. Ignored are
110 * trailing comment lines and blank lines. To support "git commit -s
111 * --amend" on an existing commit, we also ignore "Conflicts:". To
112 * support "git commit -v", we truncate at cut lines.
113 *
114 * Returns the number of bytes from the tail to ignore, to be fed as
115 * the second parameter to append_signoff().
116 */
117 2 static size_t ignore_non_trailer(const char *buf, size_t len)
118 {
119 2 size_t boc = 0, bol = 0;
120 2 int in_old_conflicts_block = 0;
121 2 size_t cutoff = len;
122  
123 9 ▶ while (bol < cutoff) {
Branch: 1 branch
BranchTrueFalseTotal
while (bol < cutoff) { 7 2 9
124 7 const char *next_line = memchr(buf + bol, '\n', len - bol);
125  
126 7 ▶ if (!next_line)
Branch: 1 branch
BranchTrueFalseTotal
if (!next_line) 0 7 7
127 0 next_line = buf + len;
128 else
129 7 next_line++;
130  
131 7 ▶ if (buf[bol] == COMMENT_LINE_CHAR || buf[bol] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (buf[bol] == COMMENT_LINE_CHAR || buf[bol] == '\n') { 7 0 7
if (buf[bol] == COMMENT_LINE_CHAR || buf[bol] == '\n') { 1 6 7
132 /* is this the first of the run of comments? */
133 1 ▶ if (!boc)
Branch: 1 branch
BranchTrueFalseTotal
if (!boc) 1 0 1
134 1 boc = bol;
135 /* otherwise, it is just continuing */
136 6 ▶ } else if (git__prefixcmp(buf + bol, "Conflicts:\n") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (git__prefixcmp(buf + bol, "Conflicts:\n") == 0) { 0 6 6
137 0 in_old_conflicts_block = 1;
138 0 ▶ if (!boc)
Branch: 1 branch
BranchTrueFalseTotal
if (!boc) 0 0 0
139 0 boc = bol;
140 6 ▶ } else if (in_old_conflicts_block && buf[bol] == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
} else if (in_old_conflicts_block && buf[bol] == '\t') { 0 6 6
} else if (in_old_conflicts_block && buf[bol] == '\t') { 0 0 0
141 ; /* a pathname in the conflicts block */
142 6 ▶ } else if (boc) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (boc) { 1 5 6
143 /* the previous was not trailing comment */
144 1 boc = 0;
145 1 in_old_conflicts_block = 0;
146 }
147 7 bol = next_line - buf;
148 }
149 2 ▶ return boc ? len - boc : len - cutoff;
Branch: 1 branch
BranchTrueFalseTotal
return boc ? len - boc : len - cutoff; 0 2 2
150 }
151  
152 /*
153 * Return the position of the start of the patch or the length of str if there
154 * is no patch in the message.
155 */
156 2 static size_t find_patch_start(const char *str)
157 {
158 const char *s;
159  
160 9 ▶ for (s = str; *s; s = next_line(s)) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = str; *s; s = next_line(s)) { 7 2 9
161 7 ▶ if (git__prefixcmp(s, "---") == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixcmp(s, "---") == 0) 0 7 7
162 0 return s - str;
163 }
164  
165 2 return s - str;
166 }
167  
168 /*
169 * Return the position of the first trailer line or len if there are no
170 * trailers.
171 */
172 2 static size_t find_trailer_start(const char *buf, size_t len)
173 {
174 const char *s;
175 size_t end_of_title, l;
176 2 int only_spaces = 1;
177 2 int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
178 /*
179 * Number of possible continuation lines encountered. This will be
180 * reset to 0 if we encounter a trailer (since those lines are to be
181 * considered continuations of that trailer), and added to
182 * non_trailer_lines if we encounter a non-trailer (since those lines
183 * are to be considered non-trailers).
184 */
185 2 int possible_continuation_lines = 0;
186  
187 /* The first paragraph is the title and cannot be trailers */
188 4 ▶ for (s = buf; s < buf + len; s = next_line(s)) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = buf; s < buf + len; s = next_line(s)) { 3 1 4
189 3 ▶ if (s[0] == COMMENT_LINE_CHAR)
Branch: 1 branch
BranchTrueFalseTotal
if (s[0] == COMMENT_LINE_CHAR) 0 3 3
190 0 continue;
191 3 ▶ if (is_blank_line(s))
Branch: 1 branch
BranchTrueFalseTotal
if (is_blank_line(s)) 1 2 3
192 1 break;
193 }
194 2 end_of_title = s - buf;
195  
196 /*
197 * Get the start of the trailers by looking starting from the end for a
198 * blank line before a set of non-blank lines that (i) are all
199 * trailers, or (ii) contains at least one Git-generated trailer and
200 * consists of at least 25% trailers.
201 */
202 2 l = len;
203 6 ▶ while (last_line(&l, buf, l) && l >= end_of_title) {
Branch: 2 branches
BranchTrueFalseTotal
while (last_line(&l, buf, l) && l >= end_of_title) { 6 0 6
while (last_line(&l, buf, l) && l >= end_of_title) { 5 1 6
204 5 const char *bol = buf + l;
205 const char *const *p;
206 5 size_t separator_pos = 0;
207  
208 5 ▶ if (bol[0] == COMMENT_LINE_CHAR) {
Branch: 1 branch
BranchTrueFalseTotal
if (bol[0] == COMMENT_LINE_CHAR) { 0 5 5
209 0 non_trailer_lines += possible_continuation_lines;
210 0 possible_continuation_lines = 0;
211 0 continue;
212 }
213 5 ▶ if (is_blank_line(bol)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_blank_line(bol)) { 1 4 5
214 1 ▶ if (only_spaces)
Branch: 1 branch
BranchTrueFalseTotal
if (only_spaces) 0 1 1
215 0 continue;
216 1 non_trailer_lines += possible_continuation_lines;
217 1 ▶ if (recognized_prefix &&
Branch: 1 branch
BranchTrueFalseTotal
if (recognized_prefix && 1 0 1
218 1 ▶ trailer_lines * 3 >= non_trailer_lines)
Branch: 1 branch
BranchTrueFalseTotal
trailer_lines * 3 >= non_trailer_lines) 1 0 1
219 1 return next_line(bol) - buf;
220 0 ▶ else if (trailer_lines && !non_trailer_lines)
Branch: 2 branches
BranchTrueFalseTotal
else if (trailer_lines && !non_trailer_lines) 0 0 0
else if (trailer_lines && !non_trailer_lines) 0 0 0
221 0 return next_line(bol) - buf;
222 0 return len;
223 }
224 4 only_spaces = 0;
225  
226 8 ▶ for (p = git_generated_prefixes; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = git_generated_prefixes; *p; p++) { 6 2 8
227 6 ▶ if (git__prefixcmp(bol, *p) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git__prefixcmp(bol, *p) == 0) { 2 4 6
228 2 trailer_lines++;
229 2 possible_continuation_lines = 0;
230 2 recognized_prefix = 1;
231 2 goto continue_outer_loop;
232 }
233 }
234  
235 2 find_separator(&separator_pos, bol, TRAILER_SEPARATORS);
236 2 ▶ if (separator_pos >= 1 && !isspace(bol[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (separator_pos >= 1 && !isspace(bol[0])) { 2 0 2
if (separator_pos >= 1 && !isspace(bol[0])) { 2 0 2
237 2 trailer_lines++;
238 2 possible_continuation_lines = 0;
239 2 ▶ if (recognized_prefix)
Branch: 1 branch
BranchTrueFalseTotal
if (recognized_prefix) 0 2 2
240 0 continue;
241 0 ▶ } else if (isspace(bol[0]))
Branch: 1 branch
BranchTrueFalseTotal
} else if (isspace(bol[0])) 0 0 0
242 0 possible_continuation_lines++;
243 else {
244 0 non_trailer_lines++;
245 0 non_trailer_lines += possible_continuation_lines;
246 0 possible_continuation_lines = 0;
247 }
248 4 continue_outer_loop:
249 ;
250 }
251  
252 1 return len;
253 }
254  
255 /* Return the position of the end of the trailers. */
256 2 static size_t find_trailer_end(const char *buf, size_t len)
257 {
258 2 return len - ignore_non_trailer(buf, len);
259 }
260  
261 2 static char *extract_trailer_block(const char *message, size_t *len)
262 {
263 2 size_t patch_start = find_patch_start(message);
264 2 size_t trailer_end = find_trailer_end(message, patch_start);
265 2 size_t trailer_start = find_trailer_start(message, trailer_end);
266  
267 2 size_t trailer_len = trailer_end - trailer_start;
268  
269 2 char *buffer = git__malloc(trailer_len + 1);
270 2 ▶ if (buffer == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (buffer == NULL) 0 2 2
271 0 return NULL;
272  
273 2 memcpy(buffer, message + trailer_start, trailer_len);
274 2 buffer[trailer_len] = 0;
275  
276 2 *len = trailer_len;
277  
278 2 return buffer;
279 }
280  
281 enum trailer_state {
282 S_START = 0,
283 S_KEY = 1,
284 S_KEY_WS = 2,
285 S_SEP_WS = 3,
286 S_VALUE = 4,
287 S_VALUE_NL = 5,
288 S_VALUE_END = 6,
289 S_IGNORE = 7
290 };
291  
292 #define NEXT(st) { state = (st); ptr++; continue; }
293 #define GOTO(st) { state = (st); continue; }
294  
295 typedef git_array_t(git_message_trailer) git_array_trailer_t;
296  
297 2 int git_message_trailers(git_message_trailer_array *trailer_arr, const char *message)
298 {
299 2 enum trailer_state state = S_START;
300 2 int rc = 0;
301 char *ptr;
302 2 char *key = NULL;
303 2 char *value = NULL;
304 2 git_array_trailer_t arr = GIT_ARRAY_INIT;
305  
306 size_t trailer_len;
307 2 char *trailer = extract_trailer_block(message, &trailer_len);
308 2 ▶ if (trailer == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (trailer == NULL) 0 2 2
309 0 return -1;
310  
311 2 for (ptr = trailer;;) {
312 83 switch (state) {
313 6 case S_START: {
314 6 ▶ if (*ptr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*ptr == 0) { 2 4 6
315 2 goto ret;
316 }
317  
318 4 key = ptr;
319 4 GOTO(S_KEY);
320 }
321 32 case S_KEY: {
322 32 ▶ if (*ptr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*ptr == 0) { 0 32 32
323 0 goto ret;
324 }
325  
326 32 ▶ if (isalnum(*ptr) || *ptr == '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (isalnum(*ptr) || *ptr == '-') { 8 24 32
if (isalnum(*ptr) || *ptr == '-') { 4 4 8
327 /* legal key character */
328 28 NEXT(S_KEY);
329 }
330  
331 4 ▶ if (*ptr == ' ' || *ptr == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
if (*ptr == ' ' || *ptr == '\t') { 4 0 4
if (*ptr == ' ' || *ptr == '\t') { 0 4 4
332 /* optional whitespace before separator */
333 0 *ptr = 0;
334 0 NEXT(S_KEY_WS);
335 }
336  
337 4 ▶ if (strchr(TRAILER_SEPARATORS, *ptr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(TRAILER_SEPARATORS, *ptr)) { 4 0 4
338 4 *ptr = 0;
339 4 NEXT(S_SEP_WS);
340 }
341  
342 /* illegal character */
343 0 GOTO(S_IGNORE);
344 }
345 0 case S_KEY_WS: {
346 0 ▶ if (*ptr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*ptr == 0) { 0 0 0
347 0 goto ret;
348 }
349  
350 0 ▶ if (*ptr == ' ' || *ptr == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
if (*ptr == ' ' || *ptr == '\t') { 0 0 0
if (*ptr == ' ' || *ptr == '\t') { 0 0 0
351 0 NEXT(S_KEY_WS);
352 }
353  
354 0 ▶ if (strchr(TRAILER_SEPARATORS, *ptr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(TRAILER_SEPARATORS, *ptr)) { 0 0 0
355 0 NEXT(S_SEP_WS);
356 }
357  
358 /* illegal character */
359 0 GOTO(S_IGNORE);
360 }
361 8 case S_SEP_WS: {
362 8 ▶ if (*ptr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*ptr == 0) { 0 8 8
363 0 goto ret;
364 }
365  
366 8 ▶ if (*ptr == ' ' || *ptr == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
if (*ptr == ' ' || *ptr == '\t') { 4 4 8
if (*ptr == ' ' || *ptr == '\t') { 0 4 4
367 4 NEXT(S_SEP_WS);
368 }
369  
370 4 value = ptr;
371 4 NEXT(S_VALUE);
372 }
373 29 case S_VALUE: {
374 29 ▶ if (*ptr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*ptr == 0) { 0 29 29
375 0 GOTO(S_VALUE_END);
376 }
377  
378 29 ▶ if (*ptr == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*ptr == '\n') { 4 25 29
379 4 NEXT(S_VALUE_NL);
380 }
381  
382 25 NEXT(S_VALUE);
383 }
384 4 case S_VALUE_NL: {
385 4 ▶ if (*ptr == ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (*ptr == ' ') { 0 4 4
386 /* continuation; */
387 0 NEXT(S_VALUE);
388 }
389  
390 4 ptr[-1] = 0;
391 4 GOTO(S_VALUE_END);
392 }
393 8 case S_VALUE_END: {
394 4 ▶ git_message_trailer *t = git_array_alloc(arr);
Branch: 2 branches
BranchTrueFalseTotal
git_message_trailer *t = git_array_alloc(arr); 1 3 4
git_message_trailer *t = git_array_alloc(arr); 1 0 1
395  
396 4 t->key = key;
397 4 t->value = value;
398  
399 4 key = NULL;
400 4 value = NULL;
401  
402 4 GOTO(S_START);
403 }
404 0 case S_IGNORE: {
405 0 ▶ if (*ptr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*ptr == 0) { 0 0 0
406 0 goto ret;
407 }
408  
409 0 ▶ if (*ptr == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*ptr == '\n') { 0 0 0
410 0 NEXT(S_START);
411 }
412  
413 0 NEXT(S_IGNORE);
414 }
415 }
416 }
417  
418 2 ret:
419 2 trailer_arr->_trailer_block = trailer;
420 2 trailer_arr->trailers = arr.ptr;
421 2 trailer_arr->count = arr.size;
422  
423 2 return rc;
424 }
425  
426 2 void git_message_trailer_array_free(git_message_trailer_array *arr)
427 {
428 2 git__free(arr->_trailer_block);
429 2 git__free(arr->trailers);
430 2 }
« deps/libgit2/src/libgit2/refs.c ↑ Summary deps/libgit2/src/libgit2/filter.c »