commit.c 41 KB

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