THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

916 lines
23 KiB

  1. #include "cache.h"
  2. #include "string-list.h"
  3. #include "run-command.h"
  4. #include "commit.h"
  5. #include "tempfile.h"
  6. #include "trailer.h"
  7. /*
  8. * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
  9. */
  10. enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
  11. enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
  12. EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
  13. enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
  14. struct conf_info {
  15. char *name;
  16. char *key;
  17. char *command;
  18. enum action_where where;
  19. enum action_if_exists if_exists;
  20. enum action_if_missing if_missing;
  21. };
  22. static struct conf_info default_conf_info;
  23. struct trailer_item {
  24. struct trailer_item *previous;
  25. struct trailer_item *next;
  26. const char *token;
  27. const char *value;
  28. struct conf_info conf;
  29. };
  30. static struct trailer_item *first_conf_item;
  31. static char *separators = ":";
  32. #define TRAILER_ARG_STRING "$ARG"
  33. static int after_or_end(enum action_where where)
  34. {
  35. return (where == WHERE_AFTER) || (where == WHERE_END);
  36. }
  37. /*
  38. * Return the length of the string not including any final
  39. * punctuation. E.g., the input "Signed-off-by:" would return
  40. * 13, stripping the trailing punctuation but retaining
  41. * internal punctuation.
  42. */
  43. static size_t token_len_without_separator(const char *token, size_t len)
  44. {
  45. while (len > 0 && !isalnum(token[len - 1]))
  46. len--;
  47. return len;
  48. }
  49. static int same_token(struct trailer_item *a, struct trailer_item *b)
  50. {
  51. size_t a_len = token_len_without_separator(a->token, strlen(a->token));
  52. size_t b_len = token_len_without_separator(b->token, strlen(b->token));
  53. size_t min_len = (a_len > b_len) ? b_len : a_len;
  54. return !strncasecmp(a->token, b->token, min_len);
  55. }
  56. static int same_value(struct trailer_item *a, struct trailer_item *b)
  57. {
  58. return !strcasecmp(a->value, b->value);
  59. }
  60. static int same_trailer(struct trailer_item *a, struct trailer_item *b)
  61. {
  62. return same_token(a, b) && same_value(a, b);
  63. }
  64. static inline int contains_only_spaces(const char *str)
  65. {
  66. const char *s = str;
  67. while (*s && isspace(*s))
  68. s++;
  69. return !*s;
  70. }
  71. static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
  72. {
  73. const char *ptr = strstr(sb->buf, a);
  74. if (ptr)
  75. strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
  76. }
  77. static void free_trailer_item(struct trailer_item *item)
  78. {
  79. free(item->conf.name);
  80. free(item->conf.key);
  81. free(item->conf.command);
  82. free((char *)item->token);
  83. free((char *)item->value);
  84. free(item);
  85. }
  86. static char last_non_space_char(const char *s)
  87. {
  88. int i;
  89. for (i = strlen(s) - 1; i >= 0; i--)
  90. if (!isspace(s[i]))
  91. return s[i];
  92. return '\0';
  93. }
  94. static void print_tok_val(FILE *outfile, const char *tok, const char *val)
  95. {
  96. char c = last_non_space_char(tok);
  97. if (!c)
  98. return;
  99. if (strchr(separators, c))
  100. fprintf(outfile, "%s%s\n", tok, val);
  101. else
  102. fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
  103. }
  104. static void print_all(FILE *outfile, struct trailer_item *first, int trim_empty)
  105. {
  106. struct trailer_item *item;
  107. for (item = first; item; item = item->next) {
  108. if (!trim_empty || strlen(item->value) > 0)
  109. print_tok_val(outfile, item->token, item->value);
  110. }
  111. }
  112. static void update_last(struct trailer_item **last)
  113. {
  114. if (*last)
  115. while ((*last)->next != NULL)
  116. *last = (*last)->next;
  117. }
  118. static void update_first(struct trailer_item **first)
  119. {
  120. if (*first)
  121. while ((*first)->previous != NULL)
  122. *first = (*first)->previous;
  123. }
  124. static void add_arg_to_input_list(struct trailer_item *on_tok,
  125. struct trailer_item *arg_tok,
  126. struct trailer_item **first,
  127. struct trailer_item **last)
  128. {
  129. if (after_or_end(arg_tok->conf.where)) {
  130. arg_tok->next = on_tok->next;
  131. on_tok->next = arg_tok;
  132. arg_tok->previous = on_tok;
  133. if (arg_tok->next)
  134. arg_tok->next->previous = arg_tok;
  135. update_last(last);
  136. } else {
  137. arg_tok->previous = on_tok->previous;
  138. on_tok->previous = arg_tok;
  139. arg_tok->next = on_tok;
  140. if (arg_tok->previous)
  141. arg_tok->previous->next = arg_tok;
  142. update_first(first);
  143. }
  144. }
  145. static int check_if_different(struct trailer_item *in_tok,
  146. struct trailer_item *arg_tok,
  147. int check_all)
  148. {
  149. enum action_where where = arg_tok->conf.where;
  150. do {
  151. if (!in_tok)
  152. return 1;
  153. if (same_trailer(in_tok, arg_tok))
  154. return 0;
  155. /*
  156. * if we want to add a trailer after another one,
  157. * we have to check those before this one
  158. */
  159. in_tok = after_or_end(where) ? in_tok->previous : in_tok->next;
  160. } while (check_all);
  161. return 1;
  162. }
  163. static void remove_from_list(struct trailer_item *item,
  164. struct trailer_item **first,
  165. struct trailer_item **last)
  166. {
  167. struct trailer_item *next = item->next;
  168. struct trailer_item *previous = item->previous;
  169. if (next) {
  170. item->next->previous = previous;
  171. item->next = NULL;
  172. } else if (last)
  173. *last = previous;
  174. if (previous) {
  175. item->previous->next = next;
  176. item->previous = NULL;
  177. } else if (first)
  178. *first = next;
  179. }
  180. static struct trailer_item *remove_first(struct trailer_item **first)
  181. {
  182. struct trailer_item *item = *first;
  183. *first = item->next;
  184. if (item->next) {
  185. item->next->previous = NULL;
  186. item->next = NULL;
  187. }
  188. return item;
  189. }
  190. static const char *apply_command(const char *command, const char *arg)
  191. {
  192. struct strbuf cmd = STRBUF_INIT;
  193. struct strbuf buf = STRBUF_INIT;
  194. struct child_process cp = CHILD_PROCESS_INIT;
  195. const char *argv[] = {NULL, NULL};
  196. const char *result;
  197. strbuf_addstr(&cmd, command);
  198. if (arg)
  199. strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
  200. argv[0] = cmd.buf;
  201. cp.argv = argv;
  202. cp.env = local_repo_env;
  203. cp.no_stdin = 1;
  204. cp.use_shell = 1;
  205. if (capture_command(&cp, &buf, 1024)) {
  206. error(_("running trailer command '%s' failed"), cmd.buf);
  207. strbuf_release(&buf);
  208. result = xstrdup("");
  209. } else {
  210. strbuf_trim(&buf);
  211. result = strbuf_detach(&buf, NULL);
  212. }
  213. strbuf_release(&cmd);
  214. return result;
  215. }
  216. static void apply_item_command(struct trailer_item *in_tok, struct trailer_item *arg_tok)
  217. {
  218. if (arg_tok->conf.command) {
  219. const char *arg;
  220. if (arg_tok->value && arg_tok->value[0]) {
  221. arg = arg_tok->value;
  222. } else {
  223. if (in_tok && in_tok->value)
  224. arg = xstrdup(in_tok->value);
  225. else
  226. arg = xstrdup("");
  227. }
  228. arg_tok->value = apply_command(arg_tok->conf.command, arg);
  229. free((char *)arg);
  230. }
  231. }
  232. static void apply_arg_if_exists(struct trailer_item *in_tok,
  233. struct trailer_item *arg_tok,
  234. struct trailer_item *on_tok,
  235. struct trailer_item **in_tok_first,
  236. struct trailer_item **in_tok_last)
  237. {
  238. switch (arg_tok->conf.if_exists) {
  239. case EXISTS_DO_NOTHING:
  240. free_trailer_item(arg_tok);
  241. break;
  242. case EXISTS_REPLACE:
  243. apply_item_command(in_tok, arg_tok);
  244. add_arg_to_input_list(on_tok, arg_tok,
  245. in_tok_first, in_tok_last);
  246. remove_from_list(in_tok, in_tok_first, in_tok_last);
  247. free_trailer_item(in_tok);
  248. break;
  249. case EXISTS_ADD:
  250. apply_item_command(in_tok, arg_tok);
  251. add_arg_to_input_list(on_tok, arg_tok,
  252. in_tok_first, in_tok_last);
  253. break;
  254. case EXISTS_ADD_IF_DIFFERENT:
  255. apply_item_command(in_tok, arg_tok);
  256. if (check_if_different(in_tok, arg_tok, 1))
  257. add_arg_to_input_list(on_tok, arg_tok,
  258. in_tok_first, in_tok_last);
  259. else
  260. free_trailer_item(arg_tok);
  261. break;
  262. case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
  263. apply_item_command(in_tok, arg_tok);
  264. if (check_if_different(on_tok, arg_tok, 0))
  265. add_arg_to_input_list(on_tok, arg_tok,
  266. in_tok_first, in_tok_last);
  267. else
  268. free_trailer_item(arg_tok);
  269. break;
  270. }
  271. }
  272. static void apply_arg_if_missing(struct trailer_item **in_tok_first,
  273. struct trailer_item **in_tok_last,
  274. struct trailer_item *arg_tok)
  275. {
  276. struct trailer_item **in_tok;
  277. enum action_where where;
  278. switch (arg_tok->conf.if_missing) {
  279. case MISSING_DO_NOTHING:
  280. free_trailer_item(arg_tok);
  281. break;
  282. case MISSING_ADD:
  283. where = arg_tok->conf.where;
  284. in_tok = after_or_end(where) ? in_tok_last : in_tok_first;
  285. apply_item_command(NULL, arg_tok);
  286. if (*in_tok) {
  287. add_arg_to_input_list(*in_tok, arg_tok,
  288. in_tok_first, in_tok_last);
  289. } else {
  290. *in_tok_first = arg_tok;
  291. *in_tok_last = arg_tok;
  292. }
  293. break;
  294. }
  295. }
  296. static int find_same_and_apply_arg(struct trailer_item **in_tok_first,
  297. struct trailer_item **in_tok_last,
  298. struct trailer_item *arg_tok)
  299. {
  300. struct trailer_item *in_tok;
  301. struct trailer_item *on_tok;
  302. struct trailer_item *following_tok;
  303. enum action_where where = arg_tok->conf.where;
  304. int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
  305. int backwards = after_or_end(where);
  306. struct trailer_item *start_tok = backwards ? *in_tok_last : *in_tok_first;
  307. for (in_tok = start_tok; in_tok; in_tok = following_tok) {
  308. following_tok = backwards ? in_tok->previous : in_tok->next;
  309. if (!same_token(in_tok, arg_tok))
  310. continue;
  311. on_tok = middle ? in_tok : start_tok;
  312. apply_arg_if_exists(in_tok, arg_tok, on_tok,
  313. in_tok_first, in_tok_last);
  314. return 1;
  315. }
  316. return 0;
  317. }
  318. static void process_trailers_lists(struct trailer_item **in_tok_first,
  319. struct trailer_item **in_tok_last,
  320. struct trailer_item **arg_tok_first)
  321. {
  322. struct trailer_item *arg_tok;
  323. struct trailer_item *next_arg;
  324. if (!*arg_tok_first)
  325. return;
  326. for (arg_tok = *arg_tok_first; arg_tok; arg_tok = next_arg) {
  327. int applied = 0;
  328. next_arg = arg_tok->next;
  329. remove_from_list(arg_tok, arg_tok_first, NULL);
  330. applied = find_same_and_apply_arg(in_tok_first,
  331. in_tok_last,
  332. arg_tok);
  333. if (!applied)
  334. apply_arg_if_missing(in_tok_first,
  335. in_tok_last,
  336. arg_tok);
  337. }
  338. }
  339. static int set_where(struct conf_info *item, const char *value)
  340. {
  341. if (!strcasecmp("after", value))
  342. item->where = WHERE_AFTER;
  343. else if (!strcasecmp("before", value))
  344. item->where = WHERE_BEFORE;
  345. else if (!strcasecmp("end", value))
  346. item->where = WHERE_END;
  347. else if (!strcasecmp("start", value))
  348. item->where = WHERE_START;
  349. else
  350. return -1;
  351. return 0;
  352. }
  353. static int set_if_exists(struct conf_info *item, const char *value)
  354. {
  355. if (!strcasecmp("addIfDifferent", value))
  356. item->if_exists = EXISTS_ADD_IF_DIFFERENT;
  357. else if (!strcasecmp("addIfDifferentNeighbor", value))
  358. item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
  359. else if (!strcasecmp("add", value))
  360. item->if_exists = EXISTS_ADD;
  361. else if (!strcasecmp("replace", value))
  362. item->if_exists = EXISTS_REPLACE;
  363. else if (!strcasecmp("doNothing", value))
  364. item->if_exists = EXISTS_DO_NOTHING;
  365. else
  366. return -1;
  367. return 0;
  368. }
  369. static int set_if_missing(struct conf_info *item, const char *value)
  370. {
  371. if (!strcasecmp("doNothing", value))
  372. item->if_missing = MISSING_DO_NOTHING;
  373. else if (!strcasecmp("add", value))
  374. item->if_missing = MISSING_ADD;
  375. else
  376. return -1;
  377. return 0;
  378. }
  379. static void duplicate_conf(struct conf_info *dst, struct conf_info *src)
  380. {
  381. *dst = *src;
  382. if (src->name)
  383. dst->name = xstrdup(src->name);
  384. if (src->key)
  385. dst->key = xstrdup(src->key);
  386. if (src->command)
  387. dst->command = xstrdup(src->command);
  388. }
  389. static struct trailer_item *get_conf_item(const char *name)
  390. {
  391. struct trailer_item *item;
  392. struct trailer_item *previous;
  393. /* Look up item with same name */
  394. for (previous = NULL, item = first_conf_item;
  395. item;
  396. previous = item, item = item->next) {
  397. if (!strcasecmp(item->conf.name, name))
  398. return item;
  399. }
  400. /* Item does not already exists, create it */
  401. item = xcalloc(sizeof(struct trailer_item), 1);
  402. duplicate_conf(&item->conf, &default_conf_info);
  403. item->conf.name = xstrdup(name);
  404. if (!previous)
  405. first_conf_item = item;
  406. else {
  407. previous->next = item;
  408. item->previous = previous;
  409. }
  410. return item;
  411. }
  412. enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE,
  413. TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
  414. static struct {
  415. const char *name;
  416. enum trailer_info_type type;
  417. } trailer_config_items[] = {
  418. { "key", TRAILER_KEY },
  419. { "command", TRAILER_COMMAND },
  420. { "where", TRAILER_WHERE },
  421. { "ifexists", TRAILER_IF_EXISTS },
  422. { "ifmissing", TRAILER_IF_MISSING }
  423. };
  424. static int git_trailer_default_config(const char *conf_key, const char *value, void *cb)
  425. {
  426. const char *trailer_item, *variable_name;
  427. if (!skip_prefix(conf_key, "trailer.", &trailer_item))
  428. return 0;
  429. variable_name = strrchr(trailer_item, '.');
  430. if (!variable_name) {
  431. if (!strcmp(trailer_item, "where")) {
  432. if (set_where(&default_conf_info, value) < 0)
  433. warning(_("unknown value '%s' for key '%s'"),
  434. value, conf_key);
  435. } else if (!strcmp(trailer_item, "ifexists")) {
  436. if (set_if_exists(&default_conf_info, value) < 0)
  437. warning(_("unknown value '%s' for key '%s'"),
  438. value, conf_key);
  439. } else if (!strcmp(trailer_item, "ifmissing")) {
  440. if (set_if_missing(&default_conf_info, value) < 0)
  441. warning(_("unknown value '%s' for key '%s'"),
  442. value, conf_key);
  443. } else if (!strcmp(trailer_item, "separators")) {
  444. separators = xstrdup(value);
  445. }
  446. }
  447. return 0;
  448. }
  449. static int git_trailer_config(const char *conf_key, const char *value, void *cb)
  450. {
  451. const char *trailer_item, *variable_name;
  452. struct trailer_item *item;
  453. struct conf_info *conf;
  454. char *name = NULL;
  455. enum trailer_info_type type;
  456. int i;
  457. if (!skip_prefix(conf_key, "trailer.", &trailer_item))
  458. return 0;
  459. variable_name = strrchr(trailer_item, '.');
  460. if (!variable_name)
  461. return 0;
  462. variable_name++;
  463. for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
  464. if (strcmp(trailer_config_items[i].name, variable_name))
  465. continue;
  466. name = xstrndup(trailer_item, variable_name - trailer_item - 1);
  467. type = trailer_config_items[i].type;
  468. break;
  469. }
  470. if (!name)
  471. return 0;
  472. item = get_conf_item(name);
  473. conf = &item->conf;
  474. free(name);
  475. switch (type) {
  476. case TRAILER_KEY:
  477. if (conf->key)
  478. warning(_("more than one %s"), conf_key);
  479. conf->key = xstrdup(value);
  480. break;
  481. case TRAILER_COMMAND:
  482. if (conf->command)
  483. warning(_("more than one %s"), conf_key);
  484. conf->command = xstrdup(value);
  485. break;
  486. case TRAILER_WHERE:
  487. if (set_where(conf, value))
  488. warning(_("unknown value '%s' for key '%s'"), value, conf_key);
  489. break;
  490. case TRAILER_IF_EXISTS:
  491. if (set_if_exists(conf, value))
  492. warning(_("unknown value '%s' for key '%s'"), value, conf_key);
  493. break;
  494. case TRAILER_IF_MISSING:
  495. if (set_if_missing(conf, value))
  496. warning(_("unknown value '%s' for key '%s'"), value, conf_key);
  497. break;
  498. default:
  499. die("internal bug in trailer.c");
  500. }
  501. return 0;
  502. }
  503. static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)
  504. {
  505. size_t len;
  506. struct strbuf seps = STRBUF_INIT;
  507. strbuf_addstr(&seps, separators);
  508. strbuf_addch(&seps, '=');
  509. len = strcspn(trailer, seps.buf);
  510. strbuf_release(&seps);
  511. if (len == 0) {
  512. int l = strlen(trailer);
  513. while (l > 0 && isspace(trailer[l - 1]))
  514. l--;
  515. return error(_("empty trailer token in trailer '%.*s'"), l, trailer);
  516. }
  517. if (len < strlen(trailer)) {
  518. strbuf_add(tok, trailer, len);
  519. strbuf_trim(tok);
  520. strbuf_addstr(val, trailer + len + 1);
  521. strbuf_trim(val);
  522. } else {
  523. strbuf_addstr(tok, trailer);
  524. strbuf_trim(tok);
  525. }
  526. return 0;
  527. }
  528. static const char *token_from_item(struct trailer_item *item, char *tok)
  529. {
  530. if (item->conf.key)
  531. return item->conf.key;
  532. if (tok)
  533. return tok;
  534. return item->conf.name;
  535. }
  536. static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
  537. char *tok, char *val)
  538. {
  539. struct trailer_item *new = xcalloc(sizeof(*new), 1);
  540. new->value = val ? val : xstrdup("");
  541. if (conf_item) {
  542. duplicate_conf(&new->conf, &conf_item->conf);
  543. new->token = xstrdup(token_from_item(conf_item, tok));
  544. free(tok);
  545. } else {
  546. duplicate_conf(&new->conf, &default_conf_info);
  547. new->token = tok;
  548. }
  549. return new;
  550. }
  551. static int token_matches_item(const char *tok, struct trailer_item *item, int tok_len)
  552. {
  553. if (!strncasecmp(tok, item->conf.name, tok_len))
  554. return 1;
  555. return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
  556. }
  557. static struct trailer_item *create_trailer_item(const char *string)
  558. {
  559. struct strbuf tok = STRBUF_INIT;
  560. struct strbuf val = STRBUF_INIT;
  561. struct trailer_item *item;
  562. int tok_len;
  563. if (parse_trailer(&tok, &val, string))
  564. return NULL;
  565. tok_len = token_len_without_separator(tok.buf, tok.len);
  566. /* Lookup if the token matches something in the config */
  567. for (item = first_conf_item; item; item = item->next) {
  568. if (token_matches_item(tok.buf, item, tok_len))
  569. return new_trailer_item(item,
  570. strbuf_detach(&tok, NULL),
  571. strbuf_detach(&val, NULL));
  572. }
  573. return new_trailer_item(NULL,
  574. strbuf_detach(&tok, NULL),
  575. strbuf_detach(&val, NULL));
  576. }
  577. static void add_trailer_item(struct trailer_item **first,
  578. struct trailer_item **last,
  579. struct trailer_item *new)
  580. {
  581. if (!new)
  582. return;
  583. if (!*last) {
  584. *first = new;
  585. *last = new;
  586. } else {
  587. (*last)->next = new;
  588. new->previous = *last;
  589. *last = new;
  590. }
  591. }
  592. static struct trailer_item *process_command_line_args(struct string_list *trailers)
  593. {
  594. struct trailer_item *arg_tok_first = NULL;
  595. struct trailer_item *arg_tok_last = NULL;
  596. struct string_list_item *tr;
  597. struct trailer_item *item;
  598. /* Add a trailer item for each configured trailer with a command */
  599. for (item = first_conf_item; item; item = item->next) {
  600. if (item->conf.command) {
  601. struct trailer_item *new = new_trailer_item(item, NULL, NULL);
  602. add_trailer_item(&arg_tok_first, &arg_tok_last, new);
  603. }
  604. }
  605. /* Add a trailer item for each trailer on the command line */
  606. for_each_string_list_item(tr, trailers) {
  607. struct trailer_item *new = create_trailer_item(tr->string);
  608. add_trailer_item(&arg_tok_first, &arg_tok_last, new);
  609. }
  610. return arg_tok_first;
  611. }
  612. static struct strbuf **read_input_file(const char *file)
  613. {
  614. struct strbuf **lines;
  615. struct strbuf sb = STRBUF_INIT;
  616. if (file) {
  617. if (strbuf_read_file(&sb, file, 0) < 0)
  618. die_errno(_("could not read input file '%s'"), file);
  619. } else {
  620. if (strbuf_read(&sb, fileno(stdin), 0) < 0)
  621. die_errno(_("could not read from stdin"));
  622. }
  623. lines = strbuf_split(&sb, '\n');
  624. strbuf_release(&sb);
  625. return lines;
  626. }
  627. /*
  628. * Return the (0 based) index of the start of the patch or the line
  629. * count if there is no patch in the message.
  630. */
  631. static int find_patch_start(struct strbuf **lines, int count)
  632. {
  633. int i;
  634. /* Get the start of the patch part if any */
  635. for (i = 0; i < count; i++) {
  636. if (starts_with(lines[i]->buf, "---"))
  637. return i;
  638. }
  639. return count;
  640. }
  641. /*
  642. * Return the (0 based) index of the first trailer line or count if
  643. * there are no trailers. Trailers are searched only in the lines from
  644. * index (count - 1) down to index 0.
  645. */
  646. static int find_trailer_start(struct strbuf **lines, int count)
  647. {
  648. int start, end_of_title, only_spaces = 1;
  649. /* The first paragraph is the title and cannot be trailers */
  650. for (start = 0; start < count; start++) {
  651. if (lines[start]->buf[0] == comment_line_char)
  652. continue;
  653. if (contains_only_spaces(lines[start]->buf))
  654. break;
  655. }
  656. end_of_title = start;
  657. /*
  658. * Get the start of the trailers by looking starting from the end
  659. * for a line with only spaces before lines with one separator.
  660. */
  661. for (start = count - 1; start >= end_of_title; start--) {
  662. if (lines[start]->buf[0] == comment_line_char)
  663. continue;
  664. if (contains_only_spaces(lines[start]->buf)) {
  665. if (only_spaces)
  666. continue;
  667. return start + 1;
  668. }
  669. if (strcspn(lines[start]->buf, separators) < lines[start]->len) {
  670. if (only_spaces)
  671. only_spaces = 0;
  672. continue;
  673. }
  674. return count;
  675. }
  676. return only_spaces ? count : 0;
  677. }
  678. /* Get the index of the end of the trailers */
  679. static int find_trailer_end(struct strbuf **lines, int patch_start)
  680. {
  681. struct strbuf sb = STRBUF_INIT;
  682. int i, ignore_bytes;
  683. for (i = 0; i < patch_start; i++)
  684. strbuf_addbuf(&sb, lines[i]);
  685. ignore_bytes = ignore_non_trailer(&sb);
  686. strbuf_release(&sb);
  687. for (i = patch_start - 1; i >= 0 && ignore_bytes > 0; i--)
  688. ignore_bytes -= lines[i]->len;
  689. return i + 1;
  690. }
  691. static int has_blank_line_before(struct strbuf **lines, int start)
  692. {
  693. for (;start >= 0; start--) {
  694. if (lines[start]->buf[0] == comment_line_char)
  695. continue;
  696. return contains_only_spaces(lines[start]->buf);
  697. }
  698. return 0;
  699. }
  700. static void print_lines(FILE *outfile, struct strbuf **lines, int start, int end)
  701. {
  702. int i;
  703. for (i = start; lines[i] && i < end; i++)
  704. fprintf(outfile, "%s", lines[i]->buf);
  705. }
  706. static int process_input_file(FILE *outfile,
  707. struct strbuf **lines,
  708. struct trailer_item **in_tok_first,
  709. struct trailer_item **in_tok_last)
  710. {
  711. int count = 0;
  712. int patch_start, trailer_start, trailer_end, i;
  713. /* Get the line count */
  714. while (lines[count])
  715. count++;
  716. patch_start = find_patch_start(lines, count);
  717. trailer_end = find_trailer_end(lines, patch_start);
  718. trailer_start = find_trailer_start(lines, trailer_end);
  719. /* Print lines before the trailers as is */
  720. print_lines(outfile, lines, 0, trailer_start);
  721. if (!has_blank_line_before(lines, trailer_start - 1))
  722. fprintf(outfile, "\n");
  723. /* Parse trailer lines */
  724. for (i = trailer_start; i < trailer_end; i++) {
  725. if (lines[i]->buf[0] != comment_line_char) {
  726. struct trailer_item *new = create_trailer_item(lines[i]->buf);
  727. add_trailer_item(in_tok_first, in_tok_last, new);
  728. }
  729. }
  730. return trailer_end;
  731. }
  732. static void free_all(struct trailer_item **first)
  733. {
  734. while (*first) {
  735. struct trailer_item *item = remove_first(first);
  736. free_trailer_item(item);
  737. }
  738. }
  739. static struct tempfile trailers_tempfile;
  740. static FILE *create_in_place_tempfile(const char *file)
  741. {
  742. struct stat st;
  743. struct strbuf template = STRBUF_INIT;
  744. const char *tail;
  745. FILE *outfile;
  746. if (stat(file, &st))
  747. die_errno(_("could not stat %s"), file);
  748. if (!S_ISREG(st.st_mode))
  749. die(_("file %s is not a regular file"), file);
  750. if (!(st.st_mode & S_IWUSR))
  751. die(_("file %s is not writable by user"), file);
  752. /* Create temporary file in the same directory as the original */
  753. tail = strrchr(file, '/');
  754. if (tail != NULL)
  755. strbuf_add(&template, file, tail - file + 1);
  756. strbuf_addstr(&template, "git-interpret-trailers-XXXXXX");
  757. xmks_tempfile_m(&trailers_tempfile, template.buf, st.st_mode);
  758. strbuf_release(&template);
  759. outfile = fdopen_tempfile(&trailers_tempfile, "w");
  760. if (!outfile)
  761. die_errno(_("could not open temporary file"));
  762. return outfile;
  763. }
  764. void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)
  765. {
  766. struct trailer_item *in_tok_first = NULL;
  767. struct trailer_item *in_tok_last = NULL;
  768. struct trailer_item *arg_tok_first;
  769. struct strbuf **lines;
  770. int trailer_end;
  771. FILE *outfile = stdout;
  772. /* Default config must be setup first */
  773. git_config(git_trailer_default_config, NULL);
  774. git_config(git_trailer_config, NULL);
  775. lines = read_input_file(file);
  776. if (in_place)
  777. outfile = create_in_place_tempfile(file);
  778. /* Print the lines before the trailers */
  779. trailer_end = process_input_file(outfile, lines, &in_tok_first, &in_tok_last);
  780. arg_tok_first = process_command_line_args(trailers);
  781. process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first);
  782. print_all(outfile, in_tok_first, trim_empty);
  783. free_all(&in_tok_first);
  784. /* Print the lines after the trailers as is */
  785. print_lines(outfile, lines, trailer_end, INT_MAX);
  786. if (in_place)
  787. if (rename_tempfile(&trailers_tempfile, file))
  788. die_errno(_("could not rename temporary file to %s"), file);
  789. strbuf_list_free(lines);
  790. }