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.

1631 lines
40KB

  1. #include "cache.h"
  2. #include "tag.h"
  3. #include "commit.h"
  4. #include "commit-graph.h"
  5. #include "repository.h"
  6. #include "object-store.h"
  7. #include "pkt-line.h"
  8. #include "utf8.h"
  9. #include "diff.h"
  10. #include "revision.h"
  11. #include "notes.h"
  12. #include "alloc.h"
  13. #include "gpg-interface.h"
  14. #include "mergesort.h"
  15. #include "commit-slab.h"
  16. #include "prio-queue.h"
  17. #include "sha1-lookup.h"
  18. #include "wt-status.h"
  19. #include "advice.h"
  20. #include "refs.h"
  21. #include "commit-reach.h"
  22. #include "run-command.h"
  23. static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
  24. int save_commit_buffer = 1;
  25. const char *commit_type = "commit";
  26. struct commit *lookup_commit_reference_gently(struct repository *r,
  27. const struct object_id *oid, int quiet)
  28. {
  29. struct object *obj = deref_tag(r,
  30. parse_object(r, oid),
  31. NULL, 0);
  32. if (!obj)
  33. return NULL;
  34. return object_as_type(r, obj, OBJ_COMMIT, quiet);
  35. }
  36. struct commit *lookup_commit_reference(struct repository *r, const struct object_id *oid)
  37. {
  38. return lookup_commit_reference_gently(r, oid, 0);
  39. }
  40. struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
  41. {
  42. struct commit *c = lookup_commit_reference(the_repository, oid);
  43. if (!c)
  44. die(_("could not parse %s"), ref_name);
  45. if (!oideq(oid, &c->object.oid)) {
  46. warning(_("%s %s is not a commit!"),
  47. ref_name, oid_to_hex(oid));
  48. }
  49. return c;
  50. }
  51. struct commit *lookup_commit(struct repository *r, const struct object_id *oid)
  52. {
  53. struct object *obj = lookup_object(r, oid);
  54. if (!obj)
  55. return create_object(r, oid, alloc_commit_node(r));
  56. return object_as_type(r, obj, OBJ_COMMIT, 0);
  57. }
  58. struct commit *lookup_commit_reference_by_name(const char *name)
  59. {
  60. struct object_id oid;
  61. struct commit *commit;
  62. if (get_oid_committish(name, &oid))
  63. return NULL;
  64. commit = lookup_commit_reference(the_repository, &oid);
  65. if (parse_commit(commit))
  66. return NULL;
  67. return commit;
  68. }
  69. static timestamp_t parse_commit_date(const char *buf, const char *tail)
  70. {
  71. const char *dateptr;
  72. if (buf + 6 >= tail)
  73. return 0;
  74. if (memcmp(buf, "author", 6))
  75. return 0;
  76. while (buf < tail && *buf++ != '\n')
  77. /* nada */;
  78. if (buf + 9 >= tail)
  79. return 0;
  80. if (memcmp(buf, "committer", 9))
  81. return 0;
  82. while (buf < tail && *buf++ != '>')
  83. /* nada */;
  84. if (buf >= tail)
  85. return 0;
  86. dateptr = buf;
  87. while (buf < tail && *buf++ != '\n')
  88. /* nada */;
  89. if (buf >= tail)
  90. return 0;
  91. /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
  92. return parse_timestamp(dateptr, NULL, 10);
  93. }
  94. static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
  95. {
  96. struct commit_graft **commit_graft_table = table;
  97. return commit_graft_table[index]->oid.hash;
  98. }
  99. static int commit_graft_pos(struct repository *r, const unsigned char *sha1)
  100. {
  101. return sha1_pos(sha1, r->parsed_objects->grafts,
  102. r->parsed_objects->grafts_nr,
  103. commit_graft_sha1_access);
  104. }
  105. int register_commit_graft(struct repository *r, struct commit_graft *graft,
  106. int ignore_dups)
  107. {
  108. int pos = commit_graft_pos(r, graft->oid.hash);
  109. if (0 <= pos) {
  110. if (ignore_dups)
  111. free(graft);
  112. else {
  113. free(r->parsed_objects->grafts[pos]);
  114. r->parsed_objects->grafts[pos] = graft;
  115. }
  116. return 1;
  117. }
  118. pos = -pos - 1;
  119. ALLOC_GROW(r->parsed_objects->grafts,
  120. r->parsed_objects->grafts_nr + 1,
  121. r->parsed_objects->grafts_alloc);
  122. r->parsed_objects->grafts_nr++;
  123. if (pos < r->parsed_objects->grafts_nr)
  124. memmove(r->parsed_objects->grafts + pos + 1,
  125. r->parsed_objects->grafts + pos,
  126. (r->parsed_objects->grafts_nr - pos - 1) *
  127. sizeof(*r->parsed_objects->grafts));
  128. r->parsed_objects->grafts[pos] = graft;
  129. return 0;
  130. }
  131. struct commit_graft *read_graft_line(struct strbuf *line)
  132. {
  133. /* The format is just "Commit Parent1 Parent2 ...\n" */
  134. int i, phase;
  135. const char *tail = NULL;
  136. struct commit_graft *graft = NULL;
  137. struct object_id dummy_oid, *oid;
  138. strbuf_rtrim(line);
  139. if (!line->len || line->buf[0] == '#')
  140. return NULL;
  141. /*
  142. * phase 0 verifies line, counts hashes in line and allocates graft
  143. * phase 1 fills graft
  144. */
  145. for (phase = 0; phase < 2; phase++) {
  146. oid = graft ? &graft->oid : &dummy_oid;
  147. if (parse_oid_hex(line->buf, oid, &tail))
  148. goto bad_graft_data;
  149. for (i = 0; *tail != '\0'; i++) {
  150. oid = graft ? &graft->parent[i] : &dummy_oid;
  151. if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
  152. goto bad_graft_data;
  153. }
  154. if (!graft) {
  155. graft = xmalloc(st_add(sizeof(*graft),
  156. st_mult(sizeof(struct object_id), i)));
  157. graft->nr_parent = i;
  158. }
  159. }
  160. return graft;
  161. bad_graft_data:
  162. error("bad graft data: %s", line->buf);
  163. assert(!graft);
  164. return NULL;
  165. }
  166. static int read_graft_file(struct repository *r, const char *graft_file)
  167. {
  168. FILE *fp = fopen_or_warn(graft_file, "r");
  169. struct strbuf buf = STRBUF_INIT;
  170. if (!fp)
  171. return -1;
  172. if (advice_graft_file_deprecated)
  173. advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
  174. "and will be removed in a future Git version.\n"
  175. "\n"
  176. "Please use \"git replace --convert-graft-file\"\n"
  177. "to convert the grafts into replace refs.\n"
  178. "\n"
  179. "Turn this message off by running\n"
  180. "\"git config advice.graftFileDeprecated false\""));
  181. while (!strbuf_getwholeline(&buf, fp, '\n')) {
  182. /* The format is just "Commit Parent1 Parent2 ...\n" */
  183. struct commit_graft *graft = read_graft_line(&buf);
  184. if (!graft)
  185. continue;
  186. if (register_commit_graft(r, graft, 1))
  187. error("duplicate graft data: %s", buf.buf);
  188. }
  189. fclose(fp);
  190. strbuf_release(&buf);
  191. return 0;
  192. }
  193. void prepare_commit_graft(struct repository *r)
  194. {
  195. char *graft_file;
  196. if (r->parsed_objects->commit_graft_prepared)
  197. return;
  198. if (!startup_info->have_repository)
  199. return;
  200. graft_file = get_graft_file(r);
  201. read_graft_file(r, graft_file);
  202. /* make sure shallows are read */
  203. is_repository_shallow(r);
  204. r->parsed_objects->commit_graft_prepared = 1;
  205. }
  206. struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid)
  207. {
  208. int pos;
  209. prepare_commit_graft(r);
  210. pos = commit_graft_pos(r, oid->hash);
  211. if (pos < 0)
  212. return NULL;
  213. return r->parsed_objects->grafts[pos];
  214. }
  215. int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
  216. {
  217. int i, ret;
  218. for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++)
  219. ret = fn(the_repository->parsed_objects->grafts[i], cb_data);
  220. return ret;
  221. }
  222. int unregister_shallow(const struct object_id *oid)
  223. {
  224. int pos = commit_graft_pos(the_repository, oid->hash);
  225. if (pos < 0)
  226. return -1;
  227. if (pos + 1 < the_repository->parsed_objects->grafts_nr)
  228. MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
  229. the_repository->parsed_objects->grafts + pos + 1,
  230. the_repository->parsed_objects->grafts_nr - pos - 1);
  231. the_repository->parsed_objects->grafts_nr--;
  232. return 0;
  233. }
  234. struct commit_buffer {
  235. void *buffer;
  236. unsigned long size;
  237. };
  238. define_commit_slab(buffer_slab, struct commit_buffer);
  239. struct buffer_slab *allocate_commit_buffer_slab(void)
  240. {
  241. struct buffer_slab *bs = xmalloc(sizeof(*bs));
  242. init_buffer_slab(bs);
  243. return bs;
  244. }
  245. void free_commit_buffer_slab(struct buffer_slab *bs)
  246. {
  247. clear_buffer_slab(bs);
  248. free(bs);
  249. }
  250. void set_commit_buffer(struct repository *r, struct commit *commit, void *buffer, unsigned long size)
  251. {
  252. struct commit_buffer *v = buffer_slab_at(
  253. r->parsed_objects->buffer_slab, commit);
  254. v->buffer = buffer;
  255. v->size = size;
  256. }
  257. const void *get_cached_commit_buffer(struct repository *r, const struct commit *commit, unsigned long *sizep)
  258. {
  259. struct commit_buffer *v = buffer_slab_peek(
  260. r->parsed_objects->buffer_slab, commit);
  261. if (!v) {
  262. if (sizep)
  263. *sizep = 0;
  264. return NULL;
  265. }
  266. if (sizep)
  267. *sizep = v->size;
  268. return v->buffer;
  269. }
  270. const void *repo_get_commit_buffer(struct repository *r,
  271. const struct commit *commit,
  272. unsigned long *sizep)
  273. {
  274. const void *ret = get_cached_commit_buffer(r, commit, sizep);
  275. if (!ret) {
  276. enum object_type type;
  277. unsigned long size;
  278. ret = repo_read_object_file(r, &commit->object.oid, &type, &size);
  279. if (!ret)
  280. die("cannot read commit object %s",
  281. oid_to_hex(&commit->object.oid));
  282. if (type != OBJ_COMMIT)
  283. die("expected commit for %s, got %s",
  284. oid_to_hex(&commit->object.oid), type_name(type));
  285. if (sizep)
  286. *sizep = size;
  287. }
  288. return ret;
  289. }
  290. void repo_unuse_commit_buffer(struct repository *r,
  291. const struct commit *commit,
  292. const void *buffer)
  293. {
  294. struct commit_buffer *v = buffer_slab_peek(
  295. r->parsed_objects->buffer_slab, commit);
  296. if (!(v && v->buffer == buffer))
  297. free((void *)buffer);
  298. }
  299. void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit)
  300. {
  301. struct commit_buffer *v = buffer_slab_peek(
  302. pool->buffer_slab, commit);
  303. if (v) {
  304. FREE_AND_NULL(v->buffer);
  305. v->size = 0;
  306. }
  307. }
  308. static inline void set_commit_tree(struct commit *c, struct tree *t)
  309. {
  310. c->maybe_tree = t;
  311. }
  312. struct tree *repo_get_commit_tree(struct repository *r,
  313. const struct commit *commit)
  314. {
  315. if (commit->maybe_tree || !commit->object.parsed)
  316. return commit->maybe_tree;
  317. if (commit->graph_pos != COMMIT_NOT_FROM_GRAPH)
  318. return get_commit_tree_in_graph(r, commit);
  319. return NULL;
  320. }
  321. struct object_id *get_commit_tree_oid(const struct commit *commit)
  322. {
  323. struct tree *tree = get_commit_tree(commit);
  324. return tree ? &tree->object.oid : NULL;
  325. }
  326. void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)
  327. {
  328. set_commit_tree(c, NULL);
  329. free_commit_buffer(pool, c);
  330. c->index = 0;
  331. free_commit_list(c->parents);
  332. c->object.parsed = 0;
  333. }
  334. const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
  335. {
  336. struct commit_buffer *v = buffer_slab_peek(
  337. the_repository->parsed_objects->buffer_slab, commit);
  338. void *ret;
  339. if (!v) {
  340. if (sizep)
  341. *sizep = 0;
  342. return NULL;
  343. }
  344. ret = v->buffer;
  345. if (sizep)
  346. *sizep = v->size;
  347. v->buffer = NULL;
  348. v->size = 0;
  349. return ret;
  350. }
  351. int parse_commit_buffer(struct repository *r, struct commit *item, const void *buffer, unsigned long size, int check_graph)
  352. {
  353. const char *tail = buffer;
  354. const char *bufptr = buffer;
  355. struct object_id parent;
  356. struct commit_list **pptr;
  357. struct commit_graft *graft;
  358. const int tree_entry_len = the_hash_algo->hexsz + 5;
  359. const int parent_entry_len = the_hash_algo->hexsz + 7;
  360. struct tree *tree;
  361. if (item->object.parsed)
  362. return 0;
  363. if (item->parents) {
  364. /*
  365. * Presumably this is leftover from an earlier failed parse;
  366. * clear it out in preparation for us re-parsing (we'll hit the
  367. * same error, but that's good, since it lets our caller know
  368. * the result cannot be trusted.
  369. */
  370. free_commit_list(item->parents);
  371. item->parents = NULL;
  372. }
  373. tail += size;
  374. if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
  375. bufptr[tree_entry_len] != '\n')
  376. return error("bogus commit object %s", oid_to_hex(&item->object.oid));
  377. if (get_oid_hex(bufptr + 5, &parent) < 0)
  378. return error("bad tree pointer in commit %s",
  379. oid_to_hex(&item->object.oid));
  380. tree = lookup_tree(r, &parent);
  381. if (!tree)
  382. return error("bad tree pointer %s in commit %s",
  383. oid_to_hex(&parent),
  384. oid_to_hex(&item->object.oid));
  385. set_commit_tree(item, tree);
  386. bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
  387. pptr = &item->parents;
  388. graft = lookup_commit_graft(r, &item->object.oid);
  389. while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
  390. struct commit *new_parent;
  391. if (tail <= bufptr + parent_entry_len + 1 ||
  392. get_oid_hex(bufptr + 7, &parent) ||
  393. bufptr[parent_entry_len] != '\n')
  394. return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
  395. bufptr += parent_entry_len + 1;
  396. /*
  397. * The clone is shallow if nr_parent < 0, and we must
  398. * not traverse its real parents even when we unhide them.
  399. */
  400. if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
  401. continue;
  402. new_parent = lookup_commit(r, &parent);
  403. if (!new_parent)
  404. return error("bad parent %s in commit %s",
  405. oid_to_hex(&parent),
  406. oid_to_hex(&item->object.oid));
  407. pptr = &commit_list_insert(new_parent, pptr)->next;
  408. }
  409. if (graft) {
  410. int i;
  411. struct commit *new_parent;
  412. for (i = 0; i < graft->nr_parent; i++) {
  413. new_parent = lookup_commit(r,
  414. &graft->parent[i]);
  415. if (!new_parent)
  416. return error("bad graft parent %s in commit %s",
  417. oid_to_hex(&graft->parent[i]),
  418. oid_to_hex(&item->object.oid));
  419. pptr = &commit_list_insert(new_parent, pptr)->next;
  420. }
  421. }
  422. item->date = parse_commit_date(bufptr, tail);
  423. if (check_graph)
  424. load_commit_graph_info(r, item);
  425. item->object.parsed = 1;
  426. return 0;
  427. }
  428. int repo_parse_commit_internal(struct repository *r,
  429. struct commit *item,
  430. int quiet_on_missing,
  431. int use_commit_graph)
  432. {
  433. enum object_type type;
  434. void *buffer;
  435. unsigned long size;
  436. int ret;
  437. if (!item)
  438. return -1;
  439. if (item->object.parsed)
  440. return 0;
  441. if (use_commit_graph && parse_commit_in_graph(r, item))
  442. return 0;
  443. buffer = repo_read_object_file(r, &item->object.oid, &type, &size);
  444. if (!buffer)
  445. return quiet_on_missing ? -1 :
  446. error("Could not read %s",
  447. oid_to_hex(&item->object.oid));
  448. if (type != OBJ_COMMIT) {
  449. free(buffer);
  450. return error("Object %s not a commit",
  451. oid_to_hex(&item->object.oid));
  452. }
  453. ret = parse_commit_buffer(r, item, buffer, size, 0);
  454. if (save_commit_buffer && !ret) {
  455. set_commit_buffer(r, item, buffer, size);
  456. return 0;
  457. }
  458. free(buffer);
  459. return ret;
  460. }
  461. int repo_parse_commit_gently(struct repository *r,
  462. struct commit *item, int quiet_on_missing)
  463. {
  464. return repo_parse_commit_internal(r, item, quiet_on_missing, 1);
  465. }
  466. void parse_commit_or_die(struct commit *item)
  467. {
  468. if (parse_commit(item))
  469. die("unable to parse commit %s",
  470. item ? oid_to_hex(&item->object.oid) : "(null)");
  471. }
  472. int find_commit_subject(const char *commit_buffer, const char **subject)
  473. {
  474. const char *eol;
  475. const char *p = commit_buffer;
  476. while (*p && (*p != '\n' || p[1] != '\n'))
  477. p++;
  478. if (*p) {
  479. p = skip_blank_lines(p + 2);
  480. eol = strchrnul(p, '\n');
  481. } else
  482. eol = p;
  483. *subject = p;
  484. return eol - p;
  485. }
  486. struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
  487. {
  488. struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
  489. new_list->item = item;
  490. new_list->next = *list_p;
  491. *list_p = new_list;
  492. return new_list;
  493. }
  494. unsigned commit_list_count(const struct commit_list *l)
  495. {
  496. unsigned c = 0;
  497. for (; l; l = l->next )
  498. c++;
  499. return c;
  500. }
  501. struct commit_list *copy_commit_list(struct commit_list *list)
  502. {
  503. struct commit_list *head = NULL;
  504. struct commit_list **pp = &head;
  505. while (list) {
  506. pp = commit_list_append(list->item, pp);
  507. list = list->next;
  508. }
  509. return head;
  510. }
  511. void free_commit_list(struct commit_list *list)
  512. {
  513. while (list)
  514. pop_commit(&list);
  515. }
  516. struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
  517. {
  518. struct commit_list **pp = list;
  519. struct commit_list *p;
  520. while ((p = *pp) != NULL) {
  521. if (p->item->date < item->date) {
  522. break;
  523. }
  524. pp = &p->next;
  525. }
  526. return commit_list_insert(item, pp);
  527. }
  528. static int commit_list_compare_by_date(const void *a, const void *b)
  529. {
  530. timestamp_t a_date = ((const struct commit_list *)a)->item->date;
  531. timestamp_t b_date = ((const struct commit_list *)b)->item->date;
  532. if (a_date < b_date)
  533. return 1;
  534. if (a_date > b_date)
  535. return -1;
  536. return 0;
  537. }
  538. static void *commit_list_get_next(const void *a)
  539. {
  540. return ((const struct commit_list *)a)->next;
  541. }
  542. static void commit_list_set_next(void *a, void *next)
  543. {
  544. ((struct commit_list *)a)->next = next;
  545. }
  546. void commit_list_sort_by_date(struct commit_list **list)
  547. {
  548. *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
  549. commit_list_compare_by_date);
  550. }
  551. struct commit *pop_most_recent_commit(struct commit_list **list,
  552. unsigned int mark)
  553. {
  554. struct commit *ret = pop_commit(list);
  555. struct commit_list *parents = ret->parents;
  556. while (parents) {
  557. struct commit *commit = parents->item;
  558. if (!parse_commit(commit) && !(commit->object.flags & mark)) {
  559. commit->object.flags |= mark;
  560. commit_list_insert_by_date(commit, list);
  561. }
  562. parents = parents->next;
  563. }
  564. return ret;
  565. }
  566. static void clear_commit_marks_1(struct commit_list **plist,
  567. struct commit *commit, unsigned int mark)
  568. {
  569. while (commit) {
  570. struct commit_list *parents;
  571. if (!(mark & commit->object.flags))
  572. return;
  573. commit->object.flags &= ~mark;
  574. parents = commit->parents;
  575. if (!parents)
  576. return;
  577. while ((parents = parents->next))
  578. commit_list_insert(parents->item, plist);
  579. commit = commit->parents->item;
  580. }
  581. }
  582. void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
  583. {
  584. struct commit_list *list = NULL;
  585. while (nr--) {
  586. clear_commit_marks_1(&list, *commit, mark);
  587. commit++;
  588. }
  589. while (list)
  590. clear_commit_marks_1(&list, pop_commit(&list), mark);
  591. }
  592. void clear_commit_marks(struct commit *commit, unsigned int mark)
  593. {
  594. clear_commit_marks_many(1, &commit, mark);
  595. }
  596. struct commit *pop_commit(struct commit_list **stack)
  597. {
  598. struct commit_list *top = *stack;
  599. struct commit *item = top ? top->item : NULL;
  600. if (top) {
  601. *stack = top->next;
  602. free(top);
  603. }
  604. return item;
  605. }
  606. /*
  607. * Topological sort support
  608. */
  609. /* count number of children that have not been emitted */
  610. define_commit_slab(indegree_slab, int);
  611. define_commit_slab(author_date_slab, timestamp_t);
  612. void record_author_date(struct author_date_slab *author_date,
  613. struct commit *commit)
  614. {
  615. const char *buffer = get_commit_buffer(commit, NULL);
  616. struct ident_split ident;
  617. const char *ident_line;
  618. size_t ident_len;
  619. char *date_end;
  620. timestamp_t date;
  621. ident_line = find_commit_header(buffer, "author", &ident_len);
  622. if (!ident_line)
  623. goto fail_exit; /* no author line */
  624. if (split_ident_line(&ident, ident_line, ident_len) ||
  625. !ident.date_begin || !ident.date_end)
  626. goto fail_exit; /* malformed "author" line */
  627. date = parse_timestamp(ident.date_begin, &date_end, 10);
  628. if (date_end != ident.date_end)
  629. goto fail_exit; /* malformed date */
  630. *(author_date_slab_at(author_date, commit)) = date;
  631. fail_exit:
  632. unuse_commit_buffer(commit, buffer);
  633. }
  634. int compare_commits_by_author_date(const void *a_, const void *b_,
  635. void *cb_data)
  636. {
  637. const struct commit *a = a_, *b = b_;
  638. struct author_date_slab *author_date = cb_data;
  639. timestamp_t a_date = *(author_date_slab_at(author_date, a));
  640. timestamp_t b_date = *(author_date_slab_at(author_date, b));
  641. /* newer commits with larger date first */
  642. if (a_date < b_date)
  643. return 1;
  644. else if (a_date > b_date)
  645. return -1;
  646. return 0;
  647. }
  648. int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused)
  649. {
  650. const struct commit *a = a_, *b = b_;
  651. /* newer commits first */
  652. if (a->generation < b->generation)
  653. return 1;
  654. else if (a->generation > b->generation)
  655. return -1;
  656. /* use date as a heuristic when generations are equal */
  657. if (a->date < b->date)
  658. return 1;
  659. else if (a->date > b->date)
  660. return -1;
  661. return 0;
  662. }
  663. int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
  664. {
  665. const struct commit *a = a_, *b = b_;
  666. /* newer commits with larger date first */
  667. if (a->date < b->date)
  668. return 1;
  669. else if (a->date > b->date)
  670. return -1;
  671. return 0;
  672. }
  673. /*
  674. * Performs an in-place topological sort on the list supplied.
  675. */
  676. void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
  677. {
  678. struct commit_list *next, *orig = *list;
  679. struct commit_list **pptr;
  680. struct indegree_slab indegree;
  681. struct prio_queue queue;
  682. struct commit *commit;
  683. struct author_date_slab author_date;
  684. if (!orig)
  685. return;
  686. *list = NULL;
  687. init_indegree_slab(&indegree);
  688. memset(&queue, '\0', sizeof(queue));
  689. switch (sort_order) {
  690. default: /* REV_SORT_IN_GRAPH_ORDER */
  691. queue.compare = NULL;
  692. break;
  693. case REV_SORT_BY_COMMIT_DATE:
  694. queue.compare = compare_commits_by_commit_date;
  695. break;
  696. case REV_SORT_BY_AUTHOR_DATE:
  697. init_author_date_slab(&author_date);
  698. queue.compare = compare_commits_by_author_date;
  699. queue.cb_data = &author_date;
  700. break;
  701. }
  702. /* Mark them and clear the indegree */
  703. for (next = orig; next; next = next->next) {
  704. struct commit *commit = next->item;
  705. *(indegree_slab_at(&indegree, commit)) = 1;
  706. /* also record the author dates, if needed */
  707. if (sort_order == REV_SORT_BY_AUTHOR_DATE)
  708. record_author_date(&author_date, commit);
  709. }
  710. /* update the indegree */
  711. for (next = orig; next; next = next->next) {
  712. struct commit_list *parents = next->item->parents;
  713. while (parents) {
  714. struct commit *parent = parents->item;
  715. int *pi = indegree_slab_at(&indegree, parent);
  716. if (*pi)
  717. (*pi)++;
  718. parents = parents->next;
  719. }
  720. }
  721. /*
  722. * find the tips
  723. *
  724. * tips are nodes not reachable from any other node in the list
  725. *
  726. * the tips serve as a starting set for the work queue.
  727. */
  728. for (next = orig; next; next = next->next) {
  729. struct commit *commit = next->item;
  730. if (*(indegree_slab_at(&indegree, commit)) == 1)
  731. prio_queue_put(&queue, commit);
  732. }
  733. /*
  734. * This is unfortunate; the initial tips need to be shown
  735. * in the order given from the revision traversal machinery.
  736. */
  737. if (sort_order == REV_SORT_IN_GRAPH_ORDER)
  738. prio_queue_reverse(&queue);
  739. /* We no longer need the commit list */
  740. free_commit_list(orig);
  741. pptr = list;
  742. *list = NULL;
  743. while ((commit = prio_queue_get(&queue)) != NULL) {
  744. struct commit_list *parents;
  745. for (parents = commit->parents; parents ; parents = parents->next) {
  746. struct commit *parent = parents->item;
  747. int *pi = indegree_slab_at(&indegree, parent);
  748. if (!*pi)
  749. continue;
  750. /*
  751. * parents are only enqueued for emission
  752. * when all their children have been emitted thereby
  753. * guaranteeing topological order.
  754. */
  755. if (--(*pi) == 1)
  756. prio_queue_put(&queue, parent);
  757. }
  758. /*
  759. * all children of commit have already been
  760. * emitted. we can emit it now.
  761. */
  762. *(indegree_slab_at(&indegree, commit)) = 0;
  763. pptr = &commit_list_insert(commit, pptr)->next;
  764. }
  765. clear_indegree_slab(&indegree);
  766. clear_prio_queue(&queue);
  767. if (sort_order == REV_SORT_BY_AUTHOR_DATE)
  768. clear_author_date_slab(&author_date);
  769. }
  770. struct rev_collect {
  771. struct commit **commit;
  772. int nr;
  773. int alloc;
  774. unsigned int initial : 1;
  775. };
  776. static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
  777. {
  778. struct commit *commit;
  779. if (is_null_oid(oid))
  780. return;
  781. commit = lookup_commit(the_repository, oid);
  782. if (!commit ||
  783. (commit->object.flags & TMP_MARK) ||
  784. parse_commit(commit))
  785. return;
  786. ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc);
  787. revs->commit[revs->nr++] = commit;
  788. commit->object.flags |= TMP_MARK;
  789. }
  790. static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
  791. const char *ident, timestamp_t timestamp,
  792. int tz, const char *message, void *cbdata)
  793. {
  794. struct rev_collect *revs = cbdata;
  795. if (revs->initial) {
  796. revs->initial = 0;
  797. add_one_commit(ooid, revs);
  798. }
  799. add_one_commit(noid, revs);
  800. return 0;
  801. }
  802. struct commit *get_fork_point(const char *refname, struct commit *commit)
  803. {
  804. struct object_id oid;
  805. struct rev_collect revs;
  806. struct commit_list *bases;
  807. int i;
  808. struct commit *ret = NULL;
  809. memset(&revs, 0, sizeof(revs));
  810. revs.initial = 1;
  811. for_each_reflog_ent(refname, collect_one_reflog_ent, &revs);
  812. if (!revs.nr && !get_oid(refname, &oid))
  813. add_one_commit(&oid, &revs);
  814. for (i = 0; i < revs.nr; i++)
  815. revs.commit[i]->object.flags &= ~TMP_MARK;
  816. bases = get_merge_bases_many(commit, revs.nr, revs.commit);
  817. /*
  818. * There should be one and only one merge base, when we found
  819. * a common ancestor among reflog entries.
  820. */
  821. if (!bases || bases->next)
  822. goto cleanup_return;
  823. /* And the found one must be one of the reflog entries */
  824. for (i = 0; i < revs.nr; i++)
  825. if (&bases->item->object == &revs.commit[i]->object)
  826. break; /* found */
  827. if (revs.nr <= i)
  828. goto cleanup_return;
  829. ret = bases->item;
  830. cleanup_return:
  831. free_commit_list(bases);
  832. return ret;
  833. }
  834. static const char gpg_sig_header[] = "gpgsig";
  835. static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
  836. static int do_sign_commit(struct strbuf *buf, const char *keyid)
  837. {
  838. struct strbuf sig = STRBUF_INIT;
  839. int inspos, copypos;
  840. const char *eoh;
  841. /* find the end of the header */
  842. eoh = strstr(buf->buf, "\n\n");
  843. if (!eoh)
  844. inspos = buf->len;
  845. else
  846. inspos = eoh - buf->buf + 1;
  847. if (!keyid || !*keyid)
  848. keyid = get_signing_key();
  849. if (sign_buffer(buf, &sig, keyid)) {
  850. strbuf_release(&sig);
  851. return -1;
  852. }
  853. for (copypos = 0; sig.buf[copypos]; ) {
  854. const char *bol = sig.buf + copypos;
  855. const char *eol = strchrnul(bol, '\n');
  856. int len = (eol - bol) + !!*eol;
  857. if (!copypos) {
  858. strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
  859. inspos += gpg_sig_header_len;
  860. }
  861. strbuf_insert(buf, inspos++, " ", 1);
  862. strbuf_insert(buf, inspos, bol, len);
  863. inspos += len;
  864. copypos += len;
  865. }
  866. strbuf_release(&sig);
  867. return 0;
  868. }
  869. int parse_signed_commit(const struct commit *commit,
  870. struct strbuf *payload, struct strbuf *signature)
  871. {
  872. unsigned long size;
  873. const char *buffer = get_commit_buffer(commit, &size);
  874. int in_signature, saw_signature = -1;
  875. const char *line, *tail;
  876. line = buffer;
  877. tail = buffer + size;
  878. in_signature = 0;
  879. saw_signature = 0;
  880. while (line < tail) {
  881. const char *sig = NULL;
  882. const char *next = memchr(line, '\n', tail - line);
  883. next = next ? next + 1 : tail;
  884. if (in_signature && line[0] == ' ')
  885. sig = line + 1;
  886. else if (starts_with(line, gpg_sig_header) &&
  887. line[gpg_sig_header_len] == ' ')
  888. sig = line + gpg_sig_header_len + 1;
  889. if (sig) {
  890. strbuf_add(signature, sig, next - sig);
  891. saw_signature = 1;
  892. in_signature = 1;
  893. } else {
  894. if (*line == '\n')
  895. /* dump the whole remainder of the buffer */
  896. next = tail;
  897. strbuf_add(payload, line, next - line);
  898. in_signature = 0;
  899. }
  900. line = next;
  901. }
  902. unuse_commit_buffer(commit, buffer);
  903. return saw_signature;
  904. }
  905. int remove_signature(struct strbuf *buf)
  906. {
  907. const char *line = buf->buf;
  908. const char *tail = buf->buf + buf->len;
  909. int in_signature = 0;
  910. const char *sig_start = NULL;
  911. const char *sig_end = NULL;
  912. while (line < tail) {
  913. const char *next = memchr(line, '\n', tail - line);
  914. next = next ? next + 1 : tail;
  915. if (in_signature && line[0] == ' ')
  916. sig_end = next;
  917. else if (starts_with(line, gpg_sig_header) &&
  918. line[gpg_sig_header_len] == ' ') {
  919. sig_start = line;
  920. sig_end = next;
  921. in_signature = 1;
  922. } else {
  923. if (*line == '\n')
  924. /* dump the whole remainder of the buffer */
  925. next = tail;
  926. in_signature = 0;
  927. }
  928. line = next;
  929. }
  930. if (sig_start)
  931. strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
  932. return sig_start != NULL;
  933. }
  934. static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
  935. {
  936. struct merge_remote_desc *desc;
  937. struct commit_extra_header *mergetag;
  938. char *buf;
  939. unsigned long size, len;
  940. enum object_type type;
  941. desc = merge_remote_util(parent);
  942. if (!desc || !desc->obj)
  943. return;
  944. buf = read_object_file(&desc->obj->oid, &type, &size);
  945. if (!buf || type != OBJ_TAG)
  946. goto free_return;
  947. len = parse_signature(buf, size);
  948. if (size == len)
  949. goto free_return;
  950. /*
  951. * We could verify this signature and either omit the tag when
  952. * it does not validate, but the integrator may not have the
  953. * public key of the signer of the tag he is merging, while a
  954. * later auditor may have it while auditing, so let's not run
  955. * verify-signed-buffer here for now...
  956. *
  957. * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
  958. * warn("warning: signed tag unverified.");
  959. */
  960. mergetag = xcalloc(1, sizeof(*mergetag));
  961. mergetag->key = xstrdup("mergetag");
  962. mergetag->value = buf;
  963. mergetag->len = size;
  964. **tail = mergetag;
  965. *tail = &mergetag->next;
  966. return;
  967. free_return:
  968. free(buf);
  969. }
  970. int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
  971. {
  972. struct strbuf payload = STRBUF_INIT;
  973. struct strbuf signature = STRBUF_INIT;
  974. int ret = 1;
  975. sigc->result = 'N';
  976. if (parse_signed_commit(commit, &payload, &signature) <= 0)
  977. goto out;
  978. ret = check_signature(payload.buf, payload.len, signature.buf,
  979. signature.len, sigc);
  980. out:
  981. strbuf_release(&payload);
  982. strbuf_release(&signature);
  983. return ret;
  984. }
  985. void verify_merge_signature(struct commit *commit, int verbosity)
  986. {
  987. char hex[GIT_MAX_HEXSZ + 1];
  988. struct signature_check signature_check;
  989. memset(&signature_check, 0, sizeof(signature_check));
  990. check_commit_signature(commit, &signature_check);
  991. find_unique_abbrev_r(hex, &commit->object.oid, DEFAULT_ABBREV);
  992. switch (signature_check.result) {
  993. case 'G':
  994. break;
  995. case 'U':
  996. die(_("Commit %s has an untrusted GPG signature, "
  997. "allegedly by %s."), hex, signature_check.signer);
  998. case 'B':
  999. die(_("Commit %s has a bad GPG signature "
  1000. "allegedly by %s."), hex, signature_check.signer);
  1001. default: /* 'N' */
  1002. die(_("Commit %s does not have a GPG signature."), hex);
  1003. }
  1004. if (verbosity >= 0 && signature_check.result == 'G')
  1005. printf(_("Commit %s has a good GPG signature by %s\n"),
  1006. hex, signature_check.signer);
  1007. signature_check_clear(&signature_check);
  1008. }
  1009. void append_merge_tag_headers(struct commit_list *parents,
  1010. struct commit_extra_header ***tail)
  1011. {
  1012. while (parents) {
  1013. struct commit *parent = parents->item;
  1014. handle_signed_tag(parent, tail);
  1015. parents = parents->next;
  1016. }
  1017. }
  1018. static void add_extra_header(struct strbuf *buffer,
  1019. struct commit_extra_header *extra)
  1020. {
  1021. strbuf_addstr(buffer, extra->key);
  1022. if (extra->len)
  1023. strbuf_add_lines(buffer, " ", extra->value, extra->len);
  1024. else
  1025. strbuf_addch(buffer, '\n');
  1026. }
  1027. struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
  1028. const char **exclude)
  1029. {
  1030. struct commit_extra_header *extra = NULL;
  1031. unsigned long size;
  1032. const char *buffer = get_commit_buffer(commit, &size);
  1033. extra = read_commit_extra_header_lines(buffer, size, exclude);
  1034. unuse_commit_buffer(commit, buffer);
  1035. return extra;
  1036. }
  1037. int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
  1038. {
  1039. struct commit_extra_header *extra, *to_free;
  1040. int res = 0;
  1041. to_free = read_commit_extra_headers(commit, NULL);
  1042. for (extra = to_free; !res && extra; extra = extra->next) {
  1043. if (strcmp(extra->key, "mergetag"))
  1044. continue; /* not a merge tag */
  1045. res = fn(commit, extra, data);
  1046. }
  1047. free_commit_extra_headers(to_free);
  1048. return res;
  1049. }
  1050. static inline int standard_header_field(const char *field, size_t len)
  1051. {
  1052. return ((len == 4 && !memcmp(field, "tree", 4)) ||
  1053. (len == 6 && !memcmp(field, "parent", 6)) ||
  1054. (len == 6 && !memcmp(field, "author", 6)) ||
  1055. (len == 9 && !memcmp(field, "committer", 9)) ||
  1056. (len == 8 && !memcmp(field, "encoding", 8)));
  1057. }
  1058. static int excluded_header_field(const char *field, size_t len, const char **exclude)
  1059. {
  1060. if (!exclude)
  1061. return 0;
  1062. while (*exclude) {
  1063. size_t xlen = strlen(*exclude);
  1064. if (len == xlen && !memcmp(field, *exclude, xlen))
  1065. return 1;
  1066. exclude++;
  1067. }
  1068. return 0;
  1069. }
  1070. static struct commit_extra_header *read_commit_extra_header_lines(
  1071. const char *buffer, size_t size,
  1072. const char **exclude)
  1073. {
  1074. struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
  1075. const char *line, *next, *eof, *eob;
  1076. struct strbuf buf = STRBUF_INIT;
  1077. for (line = buffer, eob = line + size;
  1078. line < eob && *line != '\n';
  1079. line = next) {
  1080. next = memchr(line, '\n', eob - line);
  1081. next = next ? next + 1 : eob;
  1082. if (*line == ' ') {
  1083. /* continuation */
  1084. if (it)
  1085. strbuf_add(&buf, line + 1, next - (line + 1));
  1086. continue;
  1087. }
  1088. if (it)
  1089. it->value = strbuf_detach(&buf, &it->len);
  1090. strbuf_reset(&buf);
  1091. it = NULL;
  1092. eof = memchr(line, ' ', next - line);
  1093. if (!eof)
  1094. eof = next;
  1095. else if (standard_header_field(line, eof - line) ||
  1096. excluded_header_field(line, eof - line, exclude))
  1097. continue;
  1098. it = xcalloc(1, sizeof(*it));
  1099. it->key = xmemdupz(line, eof-line);
  1100. *tail = it;
  1101. tail = &it->next;
  1102. if (eof + 1 < next)
  1103. strbuf_add(&buf, eof + 1, next - (eof + 1));
  1104. }
  1105. if (it)
  1106. it->value = strbuf_detach(&buf, &it->len);
  1107. return extra;
  1108. }
  1109. void free_commit_extra_headers(struct commit_extra_header *extra)
  1110. {
  1111. while (extra) {
  1112. struct commit_extra_header *next = extra->next;
  1113. free(extra->key);
  1114. free(extra->value);
  1115. free(extra);
  1116. extra = next;
  1117. }
  1118. }
  1119. int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
  1120. struct commit_list *parents, struct object_id *ret,
  1121. const char *author, const char *sign_commit)
  1122. {
  1123. struct commit_extra_header *extra = NULL, **tail = &extra;
  1124. int result;
  1125. append_merge_tag_headers(parents, &tail);
  1126. result = commit_tree_extended(msg, msg_len, tree, parents, ret,
  1127. author, sign_commit, extra);
  1128. free_commit_extra_headers(extra);
  1129. return result;
  1130. }
  1131. static int find_invalid_utf8(const char *buf, int len)
  1132. {
  1133. int offset = 0;
  1134. static const unsigned int max_codepoint[] = {
  1135. 0x7f, 0x7ff, 0xffff, 0x10ffff
  1136. };
  1137. while (len) {
  1138. unsigned char c = *buf++;
  1139. int bytes, bad_offset;
  1140. unsigned int codepoint;
  1141. unsigned int min_val, max_val;
  1142. len--;
  1143. offset++;
  1144. /* Simple US-ASCII? No worries. */
  1145. if (c < 0x80)
  1146. continue;
  1147. bad_offset = offset-1;
  1148. /*
  1149. * Count how many more high bits set: that's how
  1150. * many more bytes this sequence should have.
  1151. */
  1152. bytes = 0;
  1153. while (c & 0x40) {
  1154. c <<= 1;
  1155. bytes++;
  1156. }
  1157. /*
  1158. * Must be between 1 and 3 more bytes. Longer sequences result in
  1159. * codepoints beyond U+10FFFF, which are guaranteed never to exist.
  1160. */
  1161. if (bytes < 1 || 3 < bytes)
  1162. return bad_offset;
  1163. /* Do we *have* that many bytes? */
  1164. if (len < bytes)
  1165. return bad_offset;
  1166. /*
  1167. * Place the encoded bits at the bottom of the value and compute the
  1168. * valid range.
  1169. */
  1170. codepoint = (c & 0x7f) >> bytes;
  1171. min_val = max_codepoint[bytes-1] + 1;
  1172. max_val = max_codepoint[bytes];
  1173. offset += bytes;
  1174. len -= bytes;
  1175. /* And verify that they are good continuation bytes */
  1176. do {
  1177. codepoint <<= 6;
  1178. codepoint |= *buf & 0x3f;
  1179. if ((*buf++ & 0xc0) != 0x80)
  1180. return bad_offset;
  1181. } while (--bytes);
  1182. /* Reject codepoints that are out of range for the sequence length. */
  1183. if (codepoint < min_val || codepoint > max_val)
  1184. return bad_offset;
  1185. /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
  1186. if ((codepoint & 0x1ff800) == 0xd800)
  1187. return bad_offset;
  1188. /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
  1189. if ((codepoint & 0xfffe) == 0xfffe)
  1190. return bad_offset;
  1191. /* So are anything in the range U+FDD0..U+FDEF. */
  1192. if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
  1193. return bad_offset;
  1194. }
  1195. return -1;
  1196. }
  1197. /*
  1198. * This verifies that the buffer is in proper utf8 format.
  1199. *
  1200. * If it isn't, it assumes any non-utf8 characters are Latin1,
  1201. * and does the conversion.
  1202. */
  1203. static int verify_utf8(struct strbuf *buf)
  1204. {
  1205. int ok = 1;
  1206. long pos = 0;
  1207. for (;;) {
  1208. int bad;
  1209. unsigned char c;
  1210. unsigned char replace[2];
  1211. bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
  1212. if (bad < 0)
  1213. return ok;
  1214. pos += bad;
  1215. ok = 0;
  1216. c = buf->buf[pos];
  1217. strbuf_remove(buf, pos, 1);
  1218. /* We know 'c' must be in the range 128-255 */
  1219. replace[0] = 0xc0 + (c >> 6);
  1220. replace[1] = 0x80 + (c & 0x3f);
  1221. strbuf_insert(buf, pos, replace, 2);
  1222. pos += 2;
  1223. }
  1224. }
  1225. static const char commit_utf8_warn[] =
  1226. N_("Warning: commit message did not conform to UTF-8.\n"
  1227. "You may want to amend it after fixing the message, or set the config\n"
  1228. "variable i18n.commitencoding to the encoding your project uses.\n");
  1229. int commit_tree_extended(const char *msg, size_t msg_len,
  1230. const struct object_id *tree,
  1231. struct commit_list *parents, struct object_id *ret,
  1232. const char *author, const char *sign_commit,
  1233. struct commit_extra_header *extra)
  1234. {
  1235. int result;
  1236. int encoding_is_utf8;
  1237. struct strbuf buffer;
  1238. assert_oid_type(tree, OBJ_TREE);
  1239. if (memchr(msg, '\0', msg_len))
  1240. return error("a NUL byte in commit log message not allowed.");
  1241. /* Not having i18n.commitencoding is the same as having utf-8 */
  1242. encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
  1243. strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
  1244. strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
  1245. /*
  1246. * NOTE! This ordering means that the same exact tree merged with a
  1247. * different order of parents will be a _different_ changeset even
  1248. * if everything else stays the same.
  1249. */
  1250. while (parents) {
  1251. struct commit *parent = pop_commit(&parents);
  1252. strbuf_addf(&buffer, "parent %s\n",
  1253. oid_to_hex(&parent->object.oid));
  1254. }
  1255. /* Person/date information */
  1256. if (!author)
  1257. author = git_author_info(IDENT_STRICT);
  1258. strbuf_addf(&buffer, "author %s\n", author);
  1259. strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
  1260. if (!encoding_is_utf8)
  1261. strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
  1262. while (extra) {
  1263. add_extra_header(&buffer, extra);
  1264. extra = extra->next;
  1265. }
  1266. strbuf_addch(&buffer, '\n');
  1267. /* And add the comment */
  1268. strbuf_add(&buffer, msg, msg_len);
  1269. /* And check the encoding */
  1270. if (encoding_is_utf8 && !verify_utf8(&buffer))
  1271. fprintf(stderr, _(commit_utf8_warn));
  1272. if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
  1273. result = -1;
  1274. goto out;
  1275. }
  1276. result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
  1277. out:
  1278. strbuf_release(&buffer);
  1279. return result;
  1280. }
  1281. define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
  1282. static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
  1283. struct merge_remote_desc *merge_remote_util(struct commit *commit)
  1284. {
  1285. return *merge_desc_slab_at(&merge_desc_slab, commit);
  1286. }
  1287. void set_merge_remote_desc(struct commit *commit,
  1288. const char *name, struct object *obj)
  1289. {
  1290. struct merge_remote_desc *desc;
  1291. FLEX_ALLOC_STR(desc, name, name);
  1292. desc->obj = obj;
  1293. *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
  1294. }
  1295. struct commit *get_merge_parent(const char *name)
  1296. {
  1297. struct object *obj;
  1298. struct commit *commit;
  1299. struct object_id oid;
  1300. if (get_oid(name, &oid))
  1301. return NULL;
  1302. obj = parse_object(the_repository, &oid);
  1303. commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
  1304. if (commit && !merge_remote_util(commit))
  1305. set_merge_remote_desc(commit, name, obj);
  1306. return commit;
  1307. }
  1308. /*
  1309. * Append a commit to the end of the commit_list.
  1310. *
  1311. * next starts by pointing to the variable that holds the head of an
  1312. * empty commit_list, and is updated to point to the "next" field of
  1313. * the last item on the list as new commits are appended.
  1314. *
  1315. * Usage example:
  1316. *
  1317. * struct commit_list *list;
  1318. * struct commit_list **next = &list;
  1319. *
  1320. * next = commit_list_append(c1, next);
  1321. * next = commit_list_append(c2, next);
  1322. * assert(commit_list_count(list) == 2);
  1323. * return list;
  1324. */
  1325. struct commit_list **commit_list_append(struct commit *commit,
  1326. struct commit_list **next)
  1327. {
  1328. struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
  1329. new_commit->item = commit;
  1330. *next = new_commit;
  1331. new_commit->next = NULL;
  1332. return &new_commit->next;
  1333. }
  1334. const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
  1335. {
  1336. int key_len = strlen(key);
  1337. const char *line = msg;
  1338. while (line) {
  1339. const char *eol = strchrnul(line, '\n');
  1340. if (line == eol)
  1341. return NULL;
  1342. if (eol - line > key_len &&
  1343. !strncmp(line, key, key_len) &&
  1344. line[key_len] == ' ') {
  1345. *out_len = eol - line - key_len - 1;
  1346. return line + key_len + 1;
  1347. }
  1348. line = *eol ? eol + 1 : NULL;
  1349. }
  1350. return NULL;
  1351. }
  1352. /*
  1353. * Inspect the given string and determine the true "end" of the log message, in
  1354. * order to find where to put a new Signed-off-by: line. Ignored are
  1355. * trailing comment lines and blank lines. To support "git commit -s
  1356. * --amend" on an existing commit, we also ignore "Conflicts:". To
  1357. * support "git commit -v", we truncate at cut lines.
  1358. *
  1359. * Returns the number of bytes from the tail to ignore, to be fed as
  1360. * the second parameter to append_signoff().
  1361. */
  1362. size_t ignore_non_trailer(const char *buf, size_t len)
  1363. {
  1364. size_t boc = 0;
  1365. size_t bol = 0;
  1366. int in_old_conflicts_block = 0;
  1367. size_t cutoff = wt_status_locate_end(buf, len);
  1368. while (bol < cutoff) {
  1369. const char *next_line = memchr(buf + bol, '\n', len - bol);
  1370. if (!next_line)
  1371. next_line = buf + len;
  1372. else
  1373. next_line++;
  1374. if (buf[bol] == comment_line_char || buf[bol] == '\n') {
  1375. /* is this the first of the run of comments? */
  1376. if (!boc)
  1377. boc = bol;
  1378. /* otherwise, it is just continuing */
  1379. } else if (starts_with(buf + bol, "Conflicts:\n")) {
  1380. in_old_conflicts_block = 1;
  1381. if (!boc)
  1382. boc = bol;
  1383. } else if (in_old_conflicts_block && buf[bol] == '\t') {
  1384. ; /* a pathname in the conflicts block */
  1385. } else if (boc) {
  1386. /* the previous was not trailing comment */
  1387. boc = 0;
  1388. in_old_conflicts_block = 0;
  1389. }
  1390. bol = next_line - buf;
  1391. }
  1392. return boc ? len - boc : len - cutoff;
  1393. }
  1394. int run_commit_hook(int editor_is_used, const char *index_file,
  1395. const char *name, ...)
  1396. {
  1397. struct argv_array hook_env = ARGV_ARRAY_INIT;
  1398. va_list args;
  1399. int ret;
  1400. argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
  1401. /*
  1402. * Let the hook know that no editor will be launched.
  1403. */
  1404. if (!editor_is_used)
  1405. argv_array_push(&hook_env, "GIT_EDITOR=:");
  1406. va_start(args, name);
  1407. ret = run_hook_ve(hook_env.argv,name, args);
  1408. va_end(args);
  1409. argv_array_clear(&hook_env);
  1410. return ret;
  1411. }