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.

1584 lines
39KB

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