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.

2519 lines
62KB

  1. /*
  2. Format of STDIN stream:
  3. stream ::= cmd*;
  4. cmd ::= new_blob
  5. | new_commit
  6. | new_tag
  7. | reset_branch
  8. | checkpoint
  9. | progress
  10. ;
  11. new_blob ::= 'blob' lf
  12. mark?
  13. file_content;
  14. file_content ::= data;
  15. new_commit ::= 'commit' sp ref_str lf
  16. mark?
  17. ('author' sp name '<' email '>' when lf)?
  18. 'committer' sp name '<' email '>' when lf
  19. commit_msg
  20. ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
  21. ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
  22. file_change*
  23. lf?;
  24. commit_msg ::= data;
  25. file_change ::= file_clr
  26. | file_del
  27. | file_rnm
  28. | file_cpy
  29. | file_obm
  30. | file_inm;
  31. file_clr ::= 'deleteall' lf;
  32. file_del ::= 'D' sp path_str lf;
  33. file_rnm ::= 'R' sp path_str sp path_str lf;
  34. file_cpy ::= 'C' sp path_str sp path_str lf;
  35. file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
  36. file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
  37. data;
  38. new_tag ::= 'tag' sp tag_str lf
  39. 'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
  40. 'tagger' sp name '<' email '>' when lf
  41. tag_msg;
  42. tag_msg ::= data;
  43. reset_branch ::= 'reset' sp ref_str lf
  44. ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
  45. lf?;
  46. checkpoint ::= 'checkpoint' lf
  47. lf?;
  48. progress ::= 'progress' sp not_lf* lf
  49. lf?;
  50. # note: the first idnum in a stream should be 1 and subsequent
  51. # idnums should not have gaps between values as this will cause
  52. # the stream parser to reserve space for the gapped values. An
  53. # idnum can be updated in the future to a new object by issuing
  54. # a new mark directive with the old idnum.
  55. #
  56. mark ::= 'mark' sp idnum lf;
  57. data ::= (delimited_data | exact_data)
  58. lf?;
  59. # note: delim may be any string but must not contain lf.
  60. # data_line may contain any data but must not be exactly
  61. # delim.
  62. delimited_data ::= 'data' sp '<<' delim lf
  63. (data_line lf)*
  64. delim lf;
  65. # note: declen indicates the length of binary_data in bytes.
  66. # declen does not include the lf preceeding the binary data.
  67. #
  68. exact_data ::= 'data' sp declen lf
  69. binary_data;
  70. # note: quoted strings are C-style quoting supporting \c for
  71. # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
  72. # is the signed byte value in octal. Note that the only
  73. # characters which must actually be escaped to protect the
  74. # stream formatting is: \, " and LF. Otherwise these values
  75. # are UTF8.
  76. #
  77. ref_str ::= ref;
  78. sha1exp_str ::= sha1exp;
  79. tag_str ::= tag;
  80. path_str ::= path | '"' quoted(path) '"' ;
  81. mode ::= '100644' | '644'
  82. | '100755' | '755'
  83. | '120000'
  84. ;
  85. declen ::= # unsigned 32 bit value, ascii base10 notation;
  86. bigint ::= # unsigned integer value, ascii base10 notation;
  87. binary_data ::= # file content, not interpreted;
  88. when ::= raw_when | rfc2822_when;
  89. raw_when ::= ts sp tz;
  90. rfc2822_when ::= # Valid RFC 2822 date and time;
  91. sp ::= # ASCII space character;
  92. lf ::= # ASCII newline (LF) character;
  93. # note: a colon (':') must precede the numerical value assigned to
  94. # an idnum. This is to distinguish it from a ref or tag name as
  95. # GIT does not permit ':' in ref or tag strings.
  96. #
  97. idnum ::= ':' bigint;
  98. path ::= # GIT style file path, e.g. "a/b/c";
  99. ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
  100. tag ::= # GIT tag name, e.g. "FIREFOX_1_5";
  101. sha1exp ::= # Any valid GIT SHA1 expression;
  102. hexsha1 ::= # SHA1 in hexadecimal format;
  103. # note: name and email are UTF8 strings, however name must not
  104. # contain '<' or lf and email must not contain any of the
  105. # following: '<', '>', lf.
  106. #
  107. name ::= # valid GIT author/committer name;
  108. email ::= # valid GIT author/committer email;
  109. ts ::= # time since the epoch in seconds, ascii base10 notation;
  110. tz ::= # GIT style timezone;
  111. # note: comments may appear anywhere in the input, except
  112. # within a data command. Any form of the data command
  113. # always escapes the related input from comment processing.
  114. #
  115. # In case it is not clear, the '#' that starts the comment
  116. # must be the first character on that the line (an lf have
  117. # preceeded it).
  118. #
  119. comment ::= '#' not_lf* lf;
  120. not_lf ::= # Any byte that is not ASCII newline (LF);
  121. */
  122. #include "builtin.h"
  123. #include "cache.h"
  124. #include "object.h"
  125. #include "blob.h"
  126. #include "tree.h"
  127. #include "commit.h"
  128. #include "delta.h"
  129. #include "pack.h"
  130. #include "refs.h"
  131. #include "csum-file.h"
  132. #include "quote.h"
  133. #define PACK_ID_BITS 16
  134. #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
  135. #define DEPTH_BITS 13
  136. #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
  137. struct object_entry
  138. {
  139. struct object_entry *next;
  140. uint32_t offset;
  141. uint32_t type : TYPE_BITS,
  142. pack_id : PACK_ID_BITS,
  143. depth : DEPTH_BITS;
  144. unsigned char sha1[20];
  145. };
  146. struct object_entry_pool
  147. {
  148. struct object_entry_pool *next_pool;
  149. struct object_entry *next_free;
  150. struct object_entry *end;
  151. struct object_entry entries[FLEX_ARRAY]; /* more */
  152. };
  153. struct mark_set
  154. {
  155. union {
  156. struct object_entry *marked[1024];
  157. struct mark_set *sets[1024];
  158. } data;
  159. unsigned int shift;
  160. };
  161. struct last_object
  162. {
  163. struct strbuf data;
  164. uint32_t offset;
  165. unsigned int depth;
  166. unsigned no_swap : 1;
  167. };
  168. struct mem_pool
  169. {
  170. struct mem_pool *next_pool;
  171. char *next_free;
  172. char *end;
  173. uintmax_t space[FLEX_ARRAY]; /* more */
  174. };
  175. struct atom_str
  176. {
  177. struct atom_str *next_atom;
  178. unsigned short str_len;
  179. char str_dat[FLEX_ARRAY]; /* more */
  180. };
  181. struct tree_content;
  182. struct tree_entry
  183. {
  184. struct tree_content *tree;
  185. struct atom_str* name;
  186. struct tree_entry_ms
  187. {
  188. uint16_t mode;
  189. unsigned char sha1[20];
  190. } versions[2];
  191. };
  192. struct tree_content
  193. {
  194. unsigned int entry_capacity; /* must match avail_tree_content */
  195. unsigned int entry_count;
  196. unsigned int delta_depth;
  197. struct tree_entry *entries[FLEX_ARRAY]; /* more */
  198. };
  199. struct avail_tree_content
  200. {
  201. unsigned int entry_capacity; /* must match tree_content */
  202. struct avail_tree_content *next_avail;
  203. };
  204. struct branch
  205. {
  206. struct branch *table_next_branch;
  207. struct branch *active_next_branch;
  208. const char *name;
  209. struct tree_entry branch_tree;
  210. uintmax_t last_commit;
  211. unsigned active : 1;
  212. unsigned pack_id : PACK_ID_BITS;
  213. unsigned char sha1[20];
  214. };
  215. struct tag
  216. {
  217. struct tag *next_tag;
  218. const char *name;
  219. unsigned int pack_id;
  220. unsigned char sha1[20];
  221. };
  222. struct hash_list
  223. {
  224. struct hash_list *next;
  225. unsigned char sha1[20];
  226. };
  227. typedef enum {
  228. WHENSPEC_RAW = 1,
  229. WHENSPEC_RFC2822,
  230. WHENSPEC_NOW,
  231. } whenspec_type;
  232. struct recent_command
  233. {
  234. struct recent_command *prev;
  235. struct recent_command *next;
  236. char *buf;
  237. };
  238. /* Configured limits on output */
  239. static unsigned long max_depth = 10;
  240. static off_t max_packsize = (1LL << 32) - 1;
  241. static int force_update;
  242. static int pack_compression_level = Z_DEFAULT_COMPRESSION;
  243. static int pack_compression_seen;
  244. /* Stats and misc. counters */
  245. static uintmax_t alloc_count;
  246. static uintmax_t marks_set_count;
  247. static uintmax_t object_count_by_type[1 << TYPE_BITS];
  248. static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
  249. static uintmax_t delta_count_by_type[1 << TYPE_BITS];
  250. static unsigned long object_count;
  251. static unsigned long branch_count;
  252. static unsigned long branch_load_count;
  253. static int failure;
  254. static FILE *pack_edges;
  255. /* Memory pools */
  256. static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
  257. static size_t total_allocd;
  258. static struct mem_pool *mem_pool;
  259. /* Atom management */
  260. static unsigned int atom_table_sz = 4451;
  261. static unsigned int atom_cnt;
  262. static struct atom_str **atom_table;
  263. /* The .pack file being generated */
  264. static unsigned int pack_id;
  265. static struct packed_git *pack_data;
  266. static struct packed_git **all_packs;
  267. static unsigned long pack_size;
  268. /* Table of objects we've written. */
  269. static unsigned int object_entry_alloc = 5000;
  270. static struct object_entry_pool *blocks;
  271. static struct object_entry *object_table[1 << 16];
  272. static struct mark_set *marks;
  273. static const char* mark_file;
  274. /* Our last blob */
  275. static struct last_object last_blob = { STRBUF_INIT, 0, 0, 0 };
  276. /* Tree management */
  277. static unsigned int tree_entry_alloc = 1000;
  278. static void *avail_tree_entry;
  279. static unsigned int avail_tree_table_sz = 100;
  280. static struct avail_tree_content **avail_tree_table;
  281. static struct strbuf old_tree = STRBUF_INIT;
  282. static struct strbuf new_tree = STRBUF_INIT;
  283. /* Branch data */
  284. static unsigned long max_active_branches = 5;
  285. static unsigned long cur_active_branches;
  286. static unsigned long branch_table_sz = 1039;
  287. static struct branch **branch_table;
  288. static struct branch *active_branches;
  289. /* Tag data */
  290. static struct tag *first_tag;
  291. static struct tag *last_tag;
  292. /* Input stream parsing */
  293. static whenspec_type whenspec = WHENSPEC_RAW;
  294. static struct strbuf command_buf = STRBUF_INIT;
  295. static int unread_command_buf;
  296. static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
  297. static struct recent_command *cmd_tail = &cmd_hist;
  298. static struct recent_command *rc_free;
  299. static unsigned int cmd_save = 100;
  300. static uintmax_t next_mark;
  301. static struct strbuf new_data = STRBUF_INIT;
  302. static void write_branch_report(FILE *rpt, struct branch *b)
  303. {
  304. fprintf(rpt, "%s:\n", b->name);
  305. fprintf(rpt, " status :");
  306. if (b->active)
  307. fputs(" active", rpt);
  308. if (b->branch_tree.tree)
  309. fputs(" loaded", rpt);
  310. if (is_null_sha1(b->branch_tree.versions[1].sha1))
  311. fputs(" dirty", rpt);
  312. fputc('\n', rpt);
  313. fprintf(rpt, " tip commit : %s\n", sha1_to_hex(b->sha1));
  314. fprintf(rpt, " old tree : %s\n", sha1_to_hex(b->branch_tree.versions[0].sha1));
  315. fprintf(rpt, " cur tree : %s\n", sha1_to_hex(b->branch_tree.versions[1].sha1));
  316. fprintf(rpt, " commit clock: %" PRIuMAX "\n", b->last_commit);
  317. fputs(" last pack : ", rpt);
  318. if (b->pack_id < MAX_PACK_ID)
  319. fprintf(rpt, "%u", b->pack_id);
  320. fputc('\n', rpt);
  321. fputc('\n', rpt);
  322. }
  323. static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *);
  324. static void write_crash_report(const char *err)
  325. {
  326. char *loc = git_path("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
  327. FILE *rpt = fopen(loc, "w");
  328. struct branch *b;
  329. unsigned long lu;
  330. struct recent_command *rc;
  331. if (!rpt) {
  332. error("can't write crash report %s: %s", loc, strerror(errno));
  333. return;
  334. }
  335. fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
  336. fprintf(rpt, "fast-import crash report:\n");
  337. fprintf(rpt, " fast-import process: %"PRIuMAX"\n", (uintmax_t) getpid());
  338. fprintf(rpt, " parent process : %"PRIuMAX"\n", (uintmax_t) getppid());
  339. fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_LOCAL));
  340. fputc('\n', rpt);
  341. fputs("fatal: ", rpt);
  342. fputs(err, rpt);
  343. fputc('\n', rpt);
  344. fputc('\n', rpt);
  345. fputs("Most Recent Commands Before Crash\n", rpt);
  346. fputs("---------------------------------\n", rpt);
  347. for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
  348. if (rc->next == &cmd_hist)
  349. fputs("* ", rpt);
  350. else
  351. fputs(" ", rpt);
  352. fputs(rc->buf, rpt);
  353. fputc('\n', rpt);
  354. }
  355. fputc('\n', rpt);
  356. fputs("Active Branch LRU\n", rpt);
  357. fputs("-----------------\n", rpt);
  358. fprintf(rpt, " active_branches = %lu cur, %lu max\n",
  359. cur_active_branches,
  360. max_active_branches);
  361. fputc('\n', rpt);
  362. fputs(" pos clock name\n", rpt);
  363. fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
  364. for (b = active_branches, lu = 0; b; b = b->active_next_branch)
  365. fprintf(rpt, " %2lu) %6" PRIuMAX" %s\n",
  366. ++lu, b->last_commit, b->name);
  367. fputc('\n', rpt);
  368. fputs("Inactive Branches\n", rpt);
  369. fputs("-----------------\n", rpt);
  370. for (lu = 0; lu < branch_table_sz; lu++) {
  371. for (b = branch_table[lu]; b; b = b->table_next_branch)
  372. write_branch_report(rpt, b);
  373. }
  374. if (first_tag) {
  375. struct tag *tg;
  376. fputc('\n', rpt);
  377. fputs("Annotated Tags\n", rpt);
  378. fputs("--------------\n", rpt);
  379. for (tg = first_tag; tg; tg = tg->next_tag) {
  380. fputs(sha1_to_hex(tg->sha1), rpt);
  381. fputc(' ', rpt);
  382. fputs(tg->name, rpt);
  383. fputc('\n', rpt);
  384. }
  385. }
  386. fputc('\n', rpt);
  387. fputs("Marks\n", rpt);
  388. fputs("-----\n", rpt);
  389. if (mark_file)
  390. fprintf(rpt, " exported to %s\n", mark_file);
  391. else
  392. dump_marks_helper(rpt, 0, marks);
  393. fputc('\n', rpt);
  394. fputs("-------------------\n", rpt);
  395. fputs("END OF CRASH REPORT\n", rpt);
  396. fclose(rpt);
  397. }
  398. static void end_packfile(void);
  399. static void unkeep_all_packs(void);
  400. static void dump_marks(void);
  401. static NORETURN void die_nicely(const char *err, va_list params)
  402. {
  403. static int zombie;
  404. char message[2 * PATH_MAX];
  405. vsnprintf(message, sizeof(message), err, params);
  406. fputs("fatal: ", stderr);
  407. fputs(message, stderr);
  408. fputc('\n', stderr);
  409. if (!zombie) {
  410. zombie = 1;
  411. write_crash_report(message);
  412. end_packfile();
  413. unkeep_all_packs();
  414. dump_marks();
  415. }
  416. exit(128);
  417. }
  418. static void alloc_objects(unsigned int cnt)
  419. {
  420. struct object_entry_pool *b;
  421. b = xmalloc(sizeof(struct object_entry_pool)
  422. + cnt * sizeof(struct object_entry));
  423. b->next_pool = blocks;
  424. b->next_free = b->entries;
  425. b->end = b->entries + cnt;
  426. blocks = b;
  427. alloc_count += cnt;
  428. }
  429. static struct object_entry *new_object(unsigned char *sha1)
  430. {
  431. struct object_entry *e;
  432. if (blocks->next_free == blocks->end)
  433. alloc_objects(object_entry_alloc);
  434. e = blocks->next_free++;
  435. hashcpy(e->sha1, sha1);
  436. return e;
  437. }
  438. static struct object_entry *find_object(unsigned char *sha1)
  439. {
  440. unsigned int h = sha1[0] << 8 | sha1[1];
  441. struct object_entry *e;
  442. for (e = object_table[h]; e; e = e->next)
  443. if (!hashcmp(sha1, e->sha1))
  444. return e;
  445. return NULL;
  446. }
  447. static struct object_entry *insert_object(unsigned char *sha1)
  448. {
  449. unsigned int h = sha1[0] << 8 | sha1[1];
  450. struct object_entry *e = object_table[h];
  451. struct object_entry *p = NULL;
  452. while (e) {
  453. if (!hashcmp(sha1, e->sha1))
  454. return e;
  455. p = e;
  456. e = e->next;
  457. }
  458. e = new_object(sha1);
  459. e->next = NULL;
  460. e->offset = 0;
  461. if (p)
  462. p->next = e;
  463. else
  464. object_table[h] = e;
  465. return e;
  466. }
  467. static unsigned int hc_str(const char *s, size_t len)
  468. {
  469. unsigned int r = 0;
  470. while (len-- > 0)
  471. r = r * 31 + *s++;
  472. return r;
  473. }
  474. static void *pool_alloc(size_t len)
  475. {
  476. struct mem_pool *p;
  477. void *r;
  478. for (p = mem_pool; p; p = p->next_pool)
  479. if ((p->end - p->next_free >= len))
  480. break;
  481. if (!p) {
  482. if (len >= (mem_pool_alloc/2)) {
  483. total_allocd += len;
  484. return xmalloc(len);
  485. }
  486. total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
  487. p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
  488. p->next_pool = mem_pool;
  489. p->next_free = (char *) p->space;
  490. p->end = p->next_free + mem_pool_alloc;
  491. mem_pool = p;
  492. }
  493. r = p->next_free;
  494. /* round out to a 'uintmax_t' alignment */
  495. if (len & (sizeof(uintmax_t) - 1))
  496. len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
  497. p->next_free += len;
  498. return r;
  499. }
  500. static void *pool_calloc(size_t count, size_t size)
  501. {
  502. size_t len = count * size;
  503. void *r = pool_alloc(len);
  504. memset(r, 0, len);
  505. return r;
  506. }
  507. static char *pool_strdup(const char *s)
  508. {
  509. char *r = pool_alloc(strlen(s) + 1);
  510. strcpy(r, s);
  511. return r;
  512. }
  513. static void insert_mark(uintmax_t idnum, struct object_entry *oe)
  514. {
  515. struct mark_set *s = marks;
  516. while ((idnum >> s->shift) >= 1024) {
  517. s = pool_calloc(1, sizeof(struct mark_set));
  518. s->shift = marks->shift + 10;
  519. s->data.sets[0] = marks;
  520. marks = s;
  521. }
  522. while (s->shift) {
  523. uintmax_t i = idnum >> s->shift;
  524. idnum -= i << s->shift;
  525. if (!s->data.sets[i]) {
  526. s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
  527. s->data.sets[i]->shift = s->shift - 10;
  528. }
  529. s = s->data.sets[i];
  530. }
  531. if (!s->data.marked[idnum])
  532. marks_set_count++;
  533. s->data.marked[idnum] = oe;
  534. }
  535. static struct object_entry *find_mark(uintmax_t idnum)
  536. {
  537. uintmax_t orig_idnum = idnum;
  538. struct mark_set *s = marks;
  539. struct object_entry *oe = NULL;
  540. if ((idnum >> s->shift) < 1024) {
  541. while (s && s->shift) {
  542. uintmax_t i = idnum >> s->shift;
  543. idnum -= i << s->shift;
  544. s = s->data.sets[i];
  545. }
  546. if (s)
  547. oe = s->data.marked[idnum];
  548. }
  549. if (!oe)
  550. die("mark :%" PRIuMAX " not declared", orig_idnum);
  551. return oe;
  552. }
  553. static struct atom_str *to_atom(const char *s, unsigned short len)
  554. {
  555. unsigned int hc = hc_str(s, len) % atom_table_sz;
  556. struct atom_str *c;
  557. for (c = atom_table[hc]; c; c = c->next_atom)
  558. if (c->str_len == len && !strncmp(s, c->str_dat, len))
  559. return c;
  560. c = pool_alloc(sizeof(struct atom_str) + len + 1);
  561. c->str_len = len;
  562. strncpy(c->str_dat, s, len);
  563. c->str_dat[len] = 0;
  564. c->next_atom = atom_table[hc];
  565. atom_table[hc] = c;
  566. atom_cnt++;
  567. return c;
  568. }
  569. static struct branch *lookup_branch(const char *name)
  570. {
  571. unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
  572. struct branch *b;
  573. for (b = branch_table[hc]; b; b = b->table_next_branch)
  574. if (!strcmp(name, b->name))
  575. return b;
  576. return NULL;
  577. }
  578. static struct branch *new_branch(const char *name)
  579. {
  580. unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
  581. struct branch* b = lookup_branch(name);
  582. if (b)
  583. die("Invalid attempt to create duplicate branch: %s", name);
  584. switch (check_ref_format(name)) {
  585. case 0: break; /* its valid */
  586. case CHECK_REF_FORMAT_ONELEVEL:
  587. break; /* valid, but too few '/', allow anyway */
  588. default:
  589. die("Branch name doesn't conform to GIT standards: %s", name);
  590. }
  591. b = pool_calloc(1, sizeof(struct branch));
  592. b->name = pool_strdup(name);
  593. b->table_next_branch = branch_table[hc];
  594. b->branch_tree.versions[0].mode = S_IFDIR;
  595. b->branch_tree.versions[1].mode = S_IFDIR;
  596. b->active = 0;
  597. b->pack_id = MAX_PACK_ID;
  598. branch_table[hc] = b;
  599. branch_count++;
  600. return b;
  601. }
  602. static unsigned int hc_entries(unsigned int cnt)
  603. {
  604. cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
  605. return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
  606. }
  607. static struct tree_content *new_tree_content(unsigned int cnt)
  608. {
  609. struct avail_tree_content *f, *l = NULL;
  610. struct tree_content *t;
  611. unsigned int hc = hc_entries(cnt);
  612. for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
  613. if (f->entry_capacity >= cnt)
  614. break;
  615. if (f) {
  616. if (l)
  617. l->next_avail = f->next_avail;
  618. else
  619. avail_tree_table[hc] = f->next_avail;
  620. } else {
  621. cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
  622. f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
  623. f->entry_capacity = cnt;
  624. }
  625. t = (struct tree_content*)f;
  626. t->entry_count = 0;
  627. t->delta_depth = 0;
  628. return t;
  629. }
  630. static void release_tree_entry(struct tree_entry *e);
  631. static void release_tree_content(struct tree_content *t)
  632. {
  633. struct avail_tree_content *f = (struct avail_tree_content*)t;
  634. unsigned int hc = hc_entries(f->entry_capacity);
  635. f->next_avail = avail_tree_table[hc];
  636. avail_tree_table[hc] = f;
  637. }
  638. static void release_tree_content_recursive(struct tree_content *t)
  639. {
  640. unsigned int i;
  641. for (i = 0; i < t->entry_count; i++)
  642. release_tree_entry(t->entries[i]);
  643. release_tree_content(t);
  644. }
  645. static struct tree_content *grow_tree_content(
  646. struct tree_content *t,
  647. int amt)
  648. {
  649. struct tree_content *r = new_tree_content(t->entry_count + amt);
  650. r->entry_count = t->entry_count;
  651. r->delta_depth = t->delta_depth;
  652. memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
  653. release_tree_content(t);
  654. return r;
  655. }
  656. static struct tree_entry *new_tree_entry(void)
  657. {
  658. struct tree_entry *e;
  659. if (!avail_tree_entry) {
  660. unsigned int n = tree_entry_alloc;
  661. total_allocd += n * sizeof(struct tree_entry);
  662. avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
  663. while (n-- > 1) {
  664. *((void**)e) = e + 1;
  665. e++;
  666. }
  667. *((void**)e) = NULL;
  668. }
  669. e = avail_tree_entry;
  670. avail_tree_entry = *((void**)e);
  671. return e;
  672. }
  673. static void release_tree_entry(struct tree_entry *e)
  674. {
  675. if (e->tree)
  676. release_tree_content_recursive(e->tree);
  677. *((void**)e) = avail_tree_entry;
  678. avail_tree_entry = e;
  679. }
  680. static struct tree_content *dup_tree_content(struct tree_content *s)
  681. {
  682. struct tree_content *d;
  683. struct tree_entry *a, *b;
  684. unsigned int i;
  685. if (!s)
  686. return NULL;
  687. d = new_tree_content(s->entry_count);
  688. for (i = 0; i < s->entry_count; i++) {
  689. a = s->entries[i];
  690. b = new_tree_entry();
  691. memcpy(b, a, sizeof(*a));
  692. if (a->tree && is_null_sha1(b->versions[1].sha1))
  693. b->tree = dup_tree_content(a->tree);
  694. else
  695. b->tree = NULL;
  696. d->entries[i] = b;
  697. }
  698. d->entry_count = s->entry_count;
  699. d->delta_depth = s->delta_depth;
  700. return d;
  701. }
  702. static void start_packfile(void)
  703. {
  704. static char tmpfile[PATH_MAX];
  705. struct packed_git *p;
  706. struct pack_header hdr;
  707. int pack_fd;
  708. snprintf(tmpfile, sizeof(tmpfile),
  709. "%s/tmp_pack_XXXXXX", get_object_directory());
  710. pack_fd = xmkstemp(tmpfile);
  711. p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
  712. strcpy(p->pack_name, tmpfile);
  713. p->pack_fd = pack_fd;
  714. hdr.hdr_signature = htonl(PACK_SIGNATURE);
  715. hdr.hdr_version = htonl(2);
  716. hdr.hdr_entries = 0;
  717. write_or_die(p->pack_fd, &hdr, sizeof(hdr));
  718. pack_data = p;
  719. pack_size = sizeof(hdr);
  720. object_count = 0;
  721. all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
  722. all_packs[pack_id] = p;
  723. }
  724. static int oecmp (const void *a_, const void *b_)
  725. {
  726. struct object_entry *a = *((struct object_entry**)a_);
  727. struct object_entry *b = *((struct object_entry**)b_);
  728. return hashcmp(a->sha1, b->sha1);
  729. }
  730. static char *create_index(void)
  731. {
  732. static char tmpfile[PATH_MAX];
  733. SHA_CTX ctx;
  734. struct sha1file *f;
  735. struct object_entry **idx, **c, **last, *e;
  736. struct object_entry_pool *o;
  737. uint32_t array[256];
  738. int i, idx_fd;
  739. /* Build the sorted table of object IDs. */
  740. idx = xmalloc(object_count * sizeof(struct object_entry*));
  741. c = idx;
  742. for (o = blocks; o; o = o->next_pool)
  743. for (e = o->next_free; e-- != o->entries;)
  744. if (pack_id == e->pack_id)
  745. *c++ = e;
  746. last = idx + object_count;
  747. if (c != last)
  748. die("internal consistency error creating the index");
  749. qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
  750. /* Generate the fan-out array. */
  751. c = idx;
  752. for (i = 0; i < 256; i++) {
  753. struct object_entry **next = c;;
  754. while (next < last) {
  755. if ((*next)->sha1[0] != i)
  756. break;
  757. next++;
  758. }
  759. array[i] = htonl(next - idx);
  760. c = next;
  761. }
  762. snprintf(tmpfile, sizeof(tmpfile),
  763. "%s/tmp_idx_XXXXXX", get_object_directory());
  764. idx_fd = xmkstemp(tmpfile);
  765. f = sha1fd(idx_fd, tmpfile);
  766. sha1write(f, array, 256 * sizeof(int));
  767. SHA1_Init(&ctx);
  768. for (c = idx; c != last; c++) {
  769. uint32_t offset = htonl((*c)->offset);
  770. sha1write(f, &offset, 4);
  771. sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
  772. SHA1_Update(&ctx, (*c)->sha1, 20);
  773. }
  774. sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
  775. sha1close(f, NULL, CSUM_FSYNC);
  776. free(idx);
  777. SHA1_Final(pack_data->sha1, &ctx);
  778. return tmpfile;
  779. }
  780. static char *keep_pack(char *curr_index_name)
  781. {
  782. static char name[PATH_MAX];
  783. static const char *keep_msg = "fast-import";
  784. int keep_fd;
  785. chmod(pack_data->pack_name, 0444);
  786. chmod(curr_index_name, 0444);
  787. snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
  788. get_object_directory(), sha1_to_hex(pack_data->sha1));
  789. keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
  790. if (keep_fd < 0)
  791. die("cannot create keep file");
  792. write_or_die(keep_fd, keep_msg, strlen(keep_msg));
  793. if (close(keep_fd))
  794. die("failed to write keep file");
  795. snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
  796. get_object_directory(), sha1_to_hex(pack_data->sha1));
  797. if (move_temp_to_file(pack_data->pack_name, name))
  798. die("cannot store pack file");
  799. snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
  800. get_object_directory(), sha1_to_hex(pack_data->sha1));
  801. if (move_temp_to_file(curr_index_name, name))
  802. die("cannot store index file");
  803. return name;
  804. }
  805. static void unkeep_all_packs(void)
  806. {
  807. static char name[PATH_MAX];
  808. int k;
  809. for (k = 0; k < pack_id; k++) {
  810. struct packed_git *p = all_packs[k];
  811. snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
  812. get_object_directory(), sha1_to_hex(p->sha1));
  813. unlink(name);
  814. }
  815. }
  816. static void end_packfile(void)
  817. {
  818. struct packed_git *old_p = pack_data, *new_p;
  819. if (object_count) {
  820. char *idx_name;
  821. int i;
  822. struct branch *b;
  823. struct tag *t;
  824. close_pack_windows(pack_data);
  825. fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
  826. pack_data->pack_name, object_count);
  827. close(pack_data->pack_fd);
  828. idx_name = keep_pack(create_index());
  829. /* Register the packfile with core git's machinary. */
  830. new_p = add_packed_git(idx_name, strlen(idx_name), 1);
  831. if (!new_p)
  832. die("core git rejected index %s", idx_name);
  833. all_packs[pack_id] = new_p;
  834. install_packed_git(new_p);
  835. /* Print the boundary */
  836. if (pack_edges) {
  837. fprintf(pack_edges, "%s:", new_p->pack_name);
  838. for (i = 0; i < branch_table_sz; i++) {
  839. for (b = branch_table[i]; b; b = b->table_next_branch) {
  840. if (b->pack_id == pack_id)
  841. fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
  842. }
  843. }
  844. for (t = first_tag; t; t = t->next_tag) {
  845. if (t->pack_id == pack_id)
  846. fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
  847. }
  848. fputc('\n', pack_edges);
  849. fflush(pack_edges);
  850. }
  851. pack_id++;
  852. }
  853. else
  854. unlink(old_p->pack_name);
  855. free(old_p);
  856. /* We can't carry a delta across packfiles. */
  857. strbuf_release(&last_blob.data);
  858. last_blob.offset = 0;
  859. last_blob.depth = 0;
  860. }
  861. static void cycle_packfile(void)
  862. {
  863. end_packfile();
  864. start_packfile();
  865. }
  866. static size_t encode_header(
  867. enum object_type type,
  868. size_t size,
  869. unsigned char *hdr)
  870. {
  871. int n = 1;
  872. unsigned char c;
  873. if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
  874. die("bad type %d", type);
  875. c = (type << 4) | (size & 15);
  876. size >>= 4;
  877. while (size) {
  878. *hdr++ = c | 0x80;
  879. c = size & 0x7f;
  880. size >>= 7;
  881. n++;
  882. }
  883. *hdr = c;
  884. return n;
  885. }
  886. static int store_object(
  887. enum object_type type,
  888. struct strbuf *dat,
  889. struct last_object *last,
  890. unsigned char *sha1out,
  891. uintmax_t mark)
  892. {
  893. void *out, *delta;
  894. struct object_entry *e;
  895. unsigned char hdr[96];
  896. unsigned char sha1[20];
  897. unsigned long hdrlen, deltalen;
  898. SHA_CTX c;
  899. z_stream s;
  900. hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
  901. (unsigned long)dat->len) + 1;
  902. SHA1_Init(&c);
  903. SHA1_Update(&c, hdr, hdrlen);
  904. SHA1_Update(&c, dat->buf, dat->len);
  905. SHA1_Final(sha1, &c);
  906. if (sha1out)
  907. hashcpy(sha1out, sha1);
  908. e = insert_object(sha1);
  909. if (mark)
  910. insert_mark(mark, e);
  911. if (e->offset) {
  912. duplicate_count_by_type[type]++;
  913. return 1;
  914. } else if (find_sha1_pack(sha1, packed_git)) {
  915. e->type = type;
  916. e->pack_id = MAX_PACK_ID;
  917. e->offset = 1; /* just not zero! */
  918. duplicate_count_by_type[type]++;
  919. return 1;
  920. }
  921. if (last && last->data.buf && last->depth < max_depth) {
  922. delta = diff_delta(last->data.buf, last->data.len,
  923. dat->buf, dat->len,
  924. &deltalen, 0);
  925. if (delta && deltalen >= dat->len) {
  926. free(delta);
  927. delta = NULL;
  928. }
  929. } else
  930. delta = NULL;
  931. memset(&s, 0, sizeof(s));
  932. deflateInit(&s, pack_compression_level);
  933. if (delta) {
  934. s.next_in = delta;
  935. s.avail_in = deltalen;
  936. } else {
  937. s.next_in = (void *)dat->buf;
  938. s.avail_in = dat->len;
  939. }
  940. s.avail_out = deflateBound(&s, s.avail_in);
  941. s.next_out = out = xmalloc(s.avail_out);
  942. while (deflate(&s, Z_FINISH) == Z_OK)
  943. /* nothing */;
  944. deflateEnd(&s);
  945. /* Determine if we should auto-checkpoint. */
  946. if ((pack_size + 60 + s.total_out) > max_packsize
  947. || (pack_size + 60 + s.total_out) < pack_size) {
  948. /* This new object needs to *not* have the current pack_id. */
  949. e->pack_id = pack_id + 1;
  950. cycle_packfile();
  951. /* We cannot carry a delta into the new pack. */
  952. if (delta) {
  953. free(delta);
  954. delta = NULL;
  955. memset(&s, 0, sizeof(s));
  956. deflateInit(&s, pack_compression_level);
  957. s.next_in = (void *)dat->buf;
  958. s.avail_in = dat->len;
  959. s.avail_out = deflateBound(&s, s.avail_in);
  960. s.next_out = out = xrealloc(out, s.avail_out);
  961. while (deflate(&s, Z_FINISH) == Z_OK)
  962. /* nothing */;
  963. deflateEnd(&s);
  964. }
  965. }
  966. e->type = type;
  967. e->pack_id = pack_id;
  968. e->offset = pack_size;
  969. object_count++;
  970. object_count_by_type[type]++;
  971. if (delta) {
  972. unsigned long ofs = e->offset - last->offset;
  973. unsigned pos = sizeof(hdr) - 1;
  974. delta_count_by_type[type]++;
  975. e->depth = last->depth + 1;
  976. hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
  977. write_or_die(pack_data->pack_fd, hdr, hdrlen);
  978. pack_size += hdrlen;
  979. hdr[pos] = ofs & 127;
  980. while (ofs >>= 7)
  981. hdr[--pos] = 128 | (--ofs & 127);
  982. write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
  983. pack_size += sizeof(hdr) - pos;
  984. } else {
  985. e->depth = 0;
  986. hdrlen = encode_header(type, dat->len, hdr);
  987. write_or_die(pack_data->pack_fd, hdr, hdrlen);
  988. pack_size += hdrlen;
  989. }
  990. write_or_die(pack_data->pack_fd, out, s.total_out);
  991. pack_size += s.total_out;
  992. free(out);
  993. free(delta);
  994. if (last) {
  995. if (last->no_swap) {
  996. last->data = *dat;
  997. } else {
  998. strbuf_swap(&last->data, dat);
  999. }
  1000. last->offset = e->offset;
  1001. last->depth = e->depth;
  1002. }
  1003. return 0;
  1004. }
  1005. /* All calls must be guarded by find_object() or find_mark() to
  1006. * ensure the 'struct object_entry' passed was written by this
  1007. * process instance. We unpack the entry by the offset, avoiding
  1008. * the need for the corresponding .idx file. This unpacking rule
  1009. * works because we only use OBJ_REF_DELTA within the packfiles
  1010. * created by fast-import.
  1011. *
  1012. * oe must not be NULL. Such an oe usually comes from giving
  1013. * an unknown SHA-1 to find_object() or an undefined mark to
  1014. * find_mark(). Callers must test for this condition and use
  1015. * the standard read_sha1_file() when it happens.
  1016. *
  1017. * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
  1018. * find_mark(), where the mark was reloaded from an existing marks
  1019. * file and is referencing an object that this fast-import process
  1020. * instance did not write out to a packfile. Callers must test for
  1021. * this condition and use read_sha1_file() instead.
  1022. */
  1023. static void *gfi_unpack_entry(
  1024. struct object_entry *oe,
  1025. unsigned long *sizep)
  1026. {
  1027. enum object_type type;
  1028. struct packed_git *p = all_packs[oe->pack_id];
  1029. if (p == pack_data && p->pack_size < (pack_size + 20)) {
  1030. /* The object is stored in the packfile we are writing to
  1031. * and we have modified it since the last time we scanned
  1032. * back to read a previously written object. If an old
  1033. * window covered [p->pack_size, p->pack_size + 20) its
  1034. * data is stale and is not valid. Closing all windows
  1035. * and updating the packfile length ensures we can read
  1036. * the newly written data.
  1037. */
  1038. close_pack_windows(p);
  1039. /* We have to offer 20 bytes additional on the end of
  1040. * the packfile as the core unpacker code assumes the
  1041. * footer is present at the file end and must promise
  1042. * at least 20 bytes within any window it maps. But
  1043. * we don't actually create the footer here.
  1044. */
  1045. p->pack_size = pack_size + 20;
  1046. }
  1047. return unpack_entry(p, oe->offset, &type, sizep);
  1048. }
  1049. static const char *get_mode(const char *str, uint16_t *modep)
  1050. {
  1051. unsigned char c;
  1052. uint16_t mode = 0;
  1053. while ((c = *str++) != ' ') {
  1054. if (c < '0' || c > '7')
  1055. return NULL;
  1056. mode = (mode << 3) + (c - '0');
  1057. }
  1058. *modep = mode;
  1059. return str;
  1060. }
  1061. static void load_tree(struct tree_entry *root)
  1062. {
  1063. unsigned char* sha1 = root->versions[1].sha1;
  1064. struct object_entry *myoe;
  1065. struct tree_content *t;
  1066. unsigned long size;
  1067. char *buf;
  1068. const char *c;
  1069. root->tree = t = new_tree_content(8);
  1070. if (is_null_sha1(sha1))
  1071. return;
  1072. myoe = find_object(sha1);
  1073. if (myoe && myoe->pack_id != MAX_PACK_ID) {
  1074. if (myoe->type != OBJ_TREE)
  1075. die("Not a tree: %s", sha1_to_hex(sha1));
  1076. t->delta_depth = myoe->depth;
  1077. buf = gfi_unpack_entry(myoe, &size);
  1078. if (!buf)
  1079. die("Can't load tree %s", sha1_to_hex(sha1));
  1080. } else {
  1081. enum object_type type;
  1082. buf = read_sha1_file(sha1, &type, &size);
  1083. if (!buf || type != OBJ_TREE)
  1084. die("Can't load tree %s", sha1_to_hex(sha1));
  1085. }
  1086. c = buf;
  1087. while (c != (buf + size)) {
  1088. struct tree_entry *e = new_tree_entry();
  1089. if (t->entry_count == t->entry_capacity)
  1090. root->tree = t = grow_tree_content(t, t->entry_count);
  1091. t->entries[t->entry_count++] = e;
  1092. e->tree = NULL;
  1093. c = get_mode(c, &e->versions[1].mode);
  1094. if (!c)
  1095. die("Corrupt mode in %s", sha1_to_hex(sha1));
  1096. e->versions[0].mode = e->versions[1].mode;
  1097. e->name = to_atom(c, strlen(c));
  1098. c += e->name->str_len + 1;
  1099. hashcpy(e->versions[0].sha1, (unsigned char*)c);
  1100. hashcpy(e->versions[1].sha1, (unsigned char*)c);
  1101. c += 20;
  1102. }
  1103. free(buf);
  1104. }
  1105. static int tecmp0 (const void *_a, const void *_b)
  1106. {
  1107. struct tree_entry *a = *((struct tree_entry**)_a);
  1108. struct tree_entry *b = *((struct tree_entry**)_b);
  1109. return base_name_compare(
  1110. a->name->str_dat, a->name->str_len, a->versions[0].mode,
  1111. b->name->str_dat, b->name->str_len, b->versions[0].mode);
  1112. }
  1113. static int tecmp1 (const void *_a, const void *_b)
  1114. {
  1115. struct tree_entry *a = *((struct tree_entry**)_a);
  1116. struct tree_entry *b = *((struct tree_entry**)_b);
  1117. return base_name_compare(
  1118. a->name->str_dat, a->name->str_len, a->versions[1].mode,
  1119. b->name->str_dat, b->name->str_len, b->versions[1].mode);
  1120. }
  1121. static void mktree(struct tree_content *t, int v, struct strbuf *b)
  1122. {
  1123. size_t maxlen = 0;
  1124. unsigned int i;
  1125. if (!v)
  1126. qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
  1127. else
  1128. qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
  1129. for (i = 0; i < t->entry_count; i++) {
  1130. if (t->entries[i]->versions[v].mode)
  1131. maxlen += t->entries[i]->name->str_len + 34;
  1132. }
  1133. strbuf_reset(b);
  1134. strbuf_grow(b, maxlen);
  1135. for (i = 0; i < t->entry_count; i++) {
  1136. struct tree_entry *e = t->entries[i];
  1137. if (!e->versions[v].mode)
  1138. continue;
  1139. strbuf_addf(b, "%o %s%c", (unsigned int)e->versions[v].mode,
  1140. e->name->str_dat, '\0');
  1141. strbuf_add(b, e->versions[v].sha1, 20);
  1142. }
  1143. }
  1144. static void store_tree(struct tree_entry *root)
  1145. {
  1146. struct tree_content *t = root->tree;
  1147. unsigned int i, j, del;
  1148. struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
  1149. struct object_entry *le;
  1150. if (!is_null_sha1(root->versions[1].sha1))
  1151. return;
  1152. for (i = 0; i < t->entry_count; i++) {
  1153. if (t->entries[i]->tree)
  1154. store_tree(t->entries[i]);
  1155. }
  1156. le = find_object(root->versions[0].sha1);
  1157. if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) {
  1158. mktree(t, 0, &old_tree);
  1159. lo.data = old_tree;
  1160. lo.offset = le->offset;
  1161. lo.depth = t->delta_depth;
  1162. }
  1163. mktree(t, 1, &new_tree);
  1164. store_object(OBJ_TREE, &new_tree, &lo, root->versions[1].sha1, 0);
  1165. t->delta_depth = lo.depth;
  1166. for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
  1167. struct tree_entry *e = t->entries[i];
  1168. if (e->versions[1].mode) {
  1169. e->versions[0].mode = e->versions[1].mode;
  1170. hashcpy(e->versions[0].sha1, e->versions[1].sha1);
  1171. t->entries[j++] = e;
  1172. } else {
  1173. release_tree_entry(e);
  1174. del++;
  1175. }
  1176. }
  1177. t->entry_count -= del;
  1178. }
  1179. static int tree_content_set(
  1180. struct tree_entry *root,
  1181. const char *p,
  1182. const unsigned char *sha1,
  1183. const uint16_t mode,
  1184. struct tree_content *subtree)
  1185. {
  1186. struct tree_content *t = root->tree;
  1187. const char *slash1;
  1188. unsigned int i, n;
  1189. struct tree_entry *e;
  1190. slash1 = strchr(p, '/');
  1191. if (slash1)
  1192. n = slash1 - p;
  1193. else
  1194. n = strlen(p);
  1195. if (!n)
  1196. die("Empty path component found in input");
  1197. if (!slash1 && !S_ISDIR(mode) && subtree)
  1198. die("Non-directories cannot have subtrees");
  1199. for (i = 0; i < t->entry_count; i++) {
  1200. e = t->entries[i];
  1201. if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
  1202. if (!slash1) {
  1203. if (!S_ISDIR(mode)
  1204. && e->versions[1].mode == mode
  1205. && !hashcmp(e->versions[1].sha1, sha1))
  1206. return 0;
  1207. e->versions[1].mode = mode;
  1208. hashcpy(e->versions[1].sha1, sha1);
  1209. if (e->tree)
  1210. release_tree_content_recursive(e->tree);
  1211. e->tree = subtree;
  1212. hashclr(root->versions[1].sha1);
  1213. return 1;
  1214. }
  1215. if (!S_ISDIR(e->versions[1].mode)) {
  1216. e->tree = new_tree_content(8);
  1217. e->versions[1].mode = S_IFDIR;
  1218. }
  1219. if (!e->tree)
  1220. load_tree(e);
  1221. if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
  1222. hashclr(root->versions[1].sha1);
  1223. return 1;
  1224. }
  1225. return 0;
  1226. }
  1227. }
  1228. if (t->entry_count == t->entry_capacity)
  1229. root->tree = t = grow_tree_content(t, t->entry_count);
  1230. e = new_tree_entry();
  1231. e->name = to_atom(p, n);
  1232. e->versions[0].mode = 0;
  1233. hashclr(e->versions[0].sha1);
  1234. t->entries[t->entry_count++] = e;
  1235. if (slash1) {
  1236. e->tree = new_tree_content(8);
  1237. e->versions[1].mode = S_IFDIR;
  1238. tree_content_set(e, slash1 + 1, sha1, mode, subtree);
  1239. } else {
  1240. e->tree = subtree;
  1241. e->versions[1].mode = mode;
  1242. hashcpy(e->versions[1].sha1, sha1);
  1243. }
  1244. hashclr(root->versions[1].sha1);
  1245. return 1;
  1246. }
  1247. static int tree_content_remove(
  1248. struct tree_entry *root,
  1249. const char *p,
  1250. struct tree_entry *backup_leaf)
  1251. {
  1252. struct tree_content *t = root->tree;
  1253. const char *slash1;
  1254. unsigned int i, n;
  1255. struct tree_entry *e;
  1256. slash1 = strchr(p, '/');
  1257. if (slash1)
  1258. n = slash1 - p;
  1259. else
  1260. n = strlen(p);
  1261. for (i = 0; i < t->entry_count; i++) {
  1262. e = t->entries[i];
  1263. if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
  1264. if (!slash1 || !S_ISDIR(e->versions[1].mode))
  1265. goto del_entry;
  1266. if (!e->tree)
  1267. load_tree(e);
  1268. if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
  1269. for (n = 0; n < e->tree->entry_count; n++) {
  1270. if (e->tree->entries[n]->versions[1].mode) {
  1271. hashclr(root->versions[1].sha1);
  1272. return 1;
  1273. }
  1274. }
  1275. backup_leaf = NULL;
  1276. goto del_entry;
  1277. }
  1278. return 0;
  1279. }
  1280. }
  1281. return 0;
  1282. del_entry:
  1283. if (backup_leaf)
  1284. memcpy(backup_leaf, e, sizeof(*backup_leaf));
  1285. else if (e->tree)
  1286. release_tree_content_recursive(e->tree);
  1287. e->tree = NULL;
  1288. e->versions[1].mode = 0;
  1289. hashclr(e->versions[1].sha1);
  1290. hashclr(root->versions[1].sha1);
  1291. return 1;
  1292. }
  1293. static int tree_content_get(
  1294. struct tree_entry *root,
  1295. const char *p,
  1296. struct tree_entry *leaf)
  1297. {
  1298. struct tree_content *t = root->tree;
  1299. const char *slash1;
  1300. unsigned int i, n;
  1301. struct tree_entry *e;
  1302. slash1 = strchr(p, '/');
  1303. if (slash1)
  1304. n = slash1 - p;
  1305. else
  1306. n = strlen(p);
  1307. for (i = 0; i < t->entry_count; i++) {
  1308. e = t->entries[i];
  1309. if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
  1310. if (!slash1) {
  1311. memcpy(leaf, e, sizeof(*leaf));
  1312. if (e->tree && is_null_sha1(e->versions[1].sha1))
  1313. leaf->tree = dup_tree_content(e->tree);
  1314. else
  1315. leaf->tree = NULL;
  1316. return 1;
  1317. }
  1318. if (!S_ISDIR(e->versions[1].mode))
  1319. return 0;
  1320. if (!e->tree)
  1321. load_tree(e);
  1322. return tree_content_get(e, slash1 + 1, leaf);
  1323. }
  1324. }
  1325. return 0;
  1326. }
  1327. static int update_branch(struct branch *b)
  1328. {
  1329. static const char *msg = "fast-import";
  1330. struct ref_lock *lock;
  1331. unsigned char old_sha1[20];
  1332. if (is_null_sha1(b->sha1))
  1333. return 0;
  1334. if (read_ref(b->name, old_sha1))
  1335. hashclr(old_sha1);
  1336. lock = lock_any_ref_for_update(b->name, old_sha1, 0);
  1337. if (!lock)
  1338. return error("Unable to lock %s", b->name);
  1339. if (!force_update && !is_null_sha1(old_sha1)) {
  1340. struct commit *old_cmit, *new_cmit;
  1341. old_cmit = lookup_commit_reference_gently(old_sha1, 0);
  1342. new_cmit = lookup_commit_reference_gently(b->sha1, 0);
  1343. if (!old_cmit || !new_cmit) {
  1344. unlock_ref(lock);
  1345. return error("Branch %s is missing commits.", b->name);
  1346. }
  1347. if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
  1348. unlock_ref(lock);
  1349. warning("Not updating %s"
  1350. " (new tip %s does not contain %s)",
  1351. b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
  1352. return -1;
  1353. }
  1354. }
  1355. if (write_ref_sha1(lock, b->sha1, msg) < 0)
  1356. return error("Unable to update %s", b->name);
  1357. return 0;
  1358. }
  1359. static void dump_branches(void)
  1360. {
  1361. unsigned int i;
  1362. struct branch *b;
  1363. for (i = 0; i < branch_table_sz; i++) {
  1364. for (b = branch_table[i]; b; b = b->table_next_branch)
  1365. failure |= update_branch(b);
  1366. }
  1367. }
  1368. static void dump_tags(void)
  1369. {
  1370. static const char *msg = "fast-import";
  1371. struct tag *t;
  1372. struct ref_lock *lock;
  1373. char ref_name[PATH_MAX];
  1374. for (t = first_tag; t; t = t->next_tag) {
  1375. sprintf(ref_name, "tags/%s", t->name);
  1376. lock = lock_ref_sha1(ref_name, NULL);
  1377. if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
  1378. failure |= error("Unable to update %s", ref_name);
  1379. }
  1380. }
  1381. static void dump_marks_helper(FILE *f,
  1382. uintmax_t base,
  1383. struct mark_set *m)
  1384. {
  1385. uintmax_t k;
  1386. if (m->shift) {
  1387. for (k = 0; k < 1024; k++) {
  1388. if (m->data.sets[k])
  1389. dump_marks_helper(f, (base + k) << m->shift,
  1390. m->data.sets[k]);
  1391. }
  1392. } else {
  1393. for (k = 0; k < 1024; k++) {
  1394. if (m->data.marked[k])
  1395. fprintf(f, ":%" PRIuMAX " %s\n", base + k,
  1396. sha1_to_hex(m->data.marked[k]->sha1));
  1397. }
  1398. }
  1399. }
  1400. static void dump_marks(void)
  1401. {
  1402. static struct lock_file mark_lock;
  1403. int mark_fd;
  1404. FILE *f;
  1405. if (!mark_file)
  1406. return;
  1407. mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
  1408. if (mark_fd < 0) {
  1409. failure |= error("Unable to write marks file %s: %s",
  1410. mark_file, strerror(errno));
  1411. return;
  1412. }
  1413. f = fdopen(mark_fd, "w");
  1414. if (!f) {
  1415. int saved_errno = errno;
  1416. rollback_lock_file(&mark_lock);
  1417. failure |= error("Unable to write marks file %s: %s",
  1418. mark_file, strerror(saved_errno));
  1419. return;
  1420. }
  1421. /*
  1422. * Since the lock file was fdopen()'ed, it should not be close()'ed.
  1423. * Assign -1 to the lock file descriptor so that commit_lock_file()
  1424. * won't try to close() it.
  1425. */
  1426. mark_lock.fd = -1;
  1427. dump_marks_helper(f, 0, marks);
  1428. if (ferror(f) || fclose(f)) {
  1429. int saved_errno = errno;
  1430. rollback_lock_file(&mark_lock);
  1431. failure |= error("Unable to write marks file %s: %s",
  1432. mark_file, strerror(saved_errno));
  1433. return;
  1434. }
  1435. if (commit_lock_file(&mark_lock)) {
  1436. int saved_errno = errno;
  1437. rollback_lock_file(&mark_lock);
  1438. failure |= error("Unable to commit marks file %s: %s",
  1439. mark_file, strerror(saved_errno));
  1440. return;
  1441. }
  1442. }
  1443. static int read_next_command(void)
  1444. {
  1445. static int stdin_eof = 0;
  1446. if (stdin_eof) {
  1447. unread_command_buf = 0;
  1448. return EOF;
  1449. }
  1450. do {
  1451. if (unread_command_buf) {
  1452. unread_command_buf = 0;
  1453. } else {
  1454. struct recent_command *rc;
  1455. strbuf_detach(&command_buf, NULL);
  1456. stdin_eof = strbuf_getline(&command_buf, stdin, '\n');
  1457. if (stdin_eof)
  1458. return EOF;
  1459. rc = rc_free;
  1460. if (rc)
  1461. rc_free = rc->next;
  1462. else {
  1463. rc = cmd_hist.next;
  1464. cmd_hist.next = rc->next;
  1465. cmd_hist.next->prev = &cmd_hist;
  1466. free(rc->buf);
  1467. }
  1468. rc->buf = command_buf.buf;
  1469. rc->prev = cmd_tail;
  1470. rc->next = cmd_hist.prev;
  1471. rc->prev->next = rc;
  1472. cmd_tail = rc;
  1473. }
  1474. } while (command_buf.buf[0] == '#');
  1475. return 0;
  1476. }
  1477. static void skip_optional_lf(void)
  1478. {
  1479. int term_char = fgetc(stdin);
  1480. if (term_char != '\n' && term_char != EOF)
  1481. ungetc(term_char, stdin);
  1482. }
  1483. static void parse_mark(void)
  1484. {
  1485. if (!prefixcmp(command_buf.buf, "mark :")) {
  1486. next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
  1487. read_next_command();
  1488. }
  1489. else
  1490. next_mark = 0;
  1491. }
  1492. static void parse_data(struct strbuf *sb)
  1493. {
  1494. strbuf_reset(sb);
  1495. if (prefixcmp(command_buf.buf, "data "))
  1496. die("Expected 'data n' command, found: %s", command_buf.buf);
  1497. if (!prefixcmp(command_buf.buf + 5, "<<")) {
  1498. char *term = xstrdup(command_buf.buf + 5 + 2);
  1499. size_t term_len = command_buf.len - 5 - 2;
  1500. strbuf_detach(&command_buf, NULL);
  1501. for (;;) {
  1502. if (strbuf_getline(&command_buf, stdin, '\n') == EOF)
  1503. die("EOF in data (terminator '%s' not found)", term);
  1504. if (term_len == command_buf.len
  1505. && !strcmp(term, command_buf.buf))
  1506. break;
  1507. strbuf_addbuf(sb, &command_buf);
  1508. strbuf_addch(sb, '\n');
  1509. }
  1510. free(term);
  1511. }
  1512. else {
  1513. size_t n = 0, length;
  1514. length = strtoul(command_buf.buf + 5, NULL, 10);
  1515. while (n < length) {
  1516. size_t s = strbuf_fread(sb, length - n, stdin);
  1517. if (!s && feof(stdin))
  1518. die("EOF in data (%lu bytes remaining)",
  1519. (unsigned long)(length - n));
  1520. n += s;
  1521. }
  1522. }
  1523. skip_optional_lf();
  1524. }
  1525. static int validate_raw_date(const char *src, char *result, int maxlen)
  1526. {
  1527. const char *orig_src = src;
  1528. char *endp, sign;
  1529. strtoul(src, &endp, 10);
  1530. if (endp == src || *endp != ' ')
  1531. return -1;
  1532. src = endp + 1;
  1533. if (*src != '-' && *src != '+')
  1534. return -1;
  1535. sign = *src;
  1536. strtoul(src + 1, &endp, 10);
  1537. if (endp == src || *endp || (endp - orig_src) >= maxlen)
  1538. return -1;
  1539. strcpy(result, orig_src);
  1540. return 0;
  1541. }
  1542. static char *parse_ident(const char *buf)
  1543. {
  1544. const char *gt;
  1545. size_t name_len;
  1546. char *ident;
  1547. gt = strrchr(buf, '>');
  1548. if (!gt)
  1549. die("Missing > in ident string: %s", buf);
  1550. gt++;
  1551. if (*gt != ' ')
  1552. die("Missing space after > in ident string: %s", buf);
  1553. gt++;
  1554. name_len = gt - buf;
  1555. ident = xmalloc(name_len + 24);
  1556. strncpy(ident, buf, name_len);
  1557. switch (whenspec) {
  1558. case WHENSPEC_RAW:
  1559. if (validate_raw_date(gt, ident + name_len, 24) < 0)
  1560. die("Invalid raw date \"%s\" in ident: %s", gt, buf);
  1561. break;
  1562. case WHENSPEC_RFC2822:
  1563. if (parse_date(gt, ident + name_len, 24) < 0)
  1564. die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
  1565. break;
  1566. case WHENSPEC_NOW:
  1567. if (strcmp("now", gt))
  1568. die("Date in ident must be 'now': %s", buf);
  1569. datestamp(ident + name_len, 24);
  1570. break;
  1571. }
  1572. return ident;
  1573. }
  1574. static void parse_new_blob(void)
  1575. {
  1576. static struct strbuf buf = STRBUF_INIT;
  1577. read_next_command();
  1578. parse_mark();
  1579. parse_data(&buf);
  1580. store_object(OBJ_BLOB, &buf, &last_blob, NULL, next_mark);
  1581. }
  1582. static void unload_one_branch(void)
  1583. {
  1584. while (cur_active_branches
  1585. && cur_active_branches >= max_active_branches) {
  1586. uintmax_t min_commit = ULONG_MAX;
  1587. struct branch *e, *l = NULL, *p = NULL;
  1588. for (e = active_branches; e; e = e->active_next_branch) {
  1589. if (e->last_commit < min_commit) {
  1590. p = l;
  1591. min_commit = e->last_commit;
  1592. }
  1593. l = e;
  1594. }
  1595. if (p) {
  1596. e = p->active_next_branch;
  1597. p->active_next_branch = e->active_next_branch;
  1598. } else {
  1599. e = active_branches;
  1600. active_branches = e->active_next_branch;
  1601. }
  1602. e->active = 0;
  1603. e->active_next_branch = NULL;
  1604. if (e->branch_tree.tree) {
  1605. release_tree_content_recursive(e->branch_tree.tree);
  1606. e->branch_tree.tree = NULL;
  1607. }
  1608. cur_active_branches--;
  1609. }
  1610. }
  1611. static void load_branch(struct branch *b)
  1612. {
  1613. load_tree(&b->branch_tree);
  1614. if (!b->active) {
  1615. b->active = 1;
  1616. b->active_next_branch = active_branches;
  1617. active_branches = b;
  1618. cur_active_branches++;
  1619. branch_load_count++;
  1620. }
  1621. }
  1622. static void file_change_m(struct branch *b)
  1623. {
  1624. const char *p = command_buf.buf + 2;
  1625. static struct strbuf uq = STRBUF_INIT;
  1626. const char *endp;
  1627. struct object_entry *oe = oe;
  1628. unsigned char sha1[20];
  1629. uint16_t mode, inline_data = 0;
  1630. p = get_mode(p, &mode);
  1631. if (!p)
  1632. die("Corrupt mode: %s", command_buf.buf);
  1633. switch (mode) {
  1634. case S_IFREG | 0644:
  1635. case S_IFREG | 0755:
  1636. case S_IFLNK:
  1637. case S_IFGITLINK:
  1638. case 0644:
  1639. case 0755:
  1640. /* ok */
  1641. break;
  1642. default:
  1643. die("Corrupt mode: %s", command_buf.buf);
  1644. }
  1645. if (*p == ':') {
  1646. char *x;
  1647. oe = find_mark(strtoumax(p + 1, &x, 10));
  1648. hashcpy(sha1, oe->sha1);
  1649. p = x;
  1650. } else if (!prefixcmp(p, "inline")) {
  1651. inline_data = 1;
  1652. p += 6;
  1653. } else {
  1654. if (get_sha1_hex(p, sha1))
  1655. die("Invalid SHA1: %s", command_buf.buf);
  1656. oe = find_object(sha1);
  1657. p += 40;
  1658. }
  1659. if (*p++ != ' ')
  1660. die("Missing space after SHA1: %s", command_buf.buf);
  1661. strbuf_reset(&uq);
  1662. if (!unquote_c_style(&uq, p, &endp)) {
  1663. if (*endp)
  1664. die("Garbage after path in: %s", command_buf.buf);
  1665. p = uq.buf;
  1666. }
  1667. if (S_ISGITLINK(mode)) {
  1668. if (inline_data)
  1669. die("Git links cannot be specified 'inline': %s",
  1670. command_buf.buf);
  1671. else if (oe) {
  1672. if (oe->type != OBJ_COMMIT)
  1673. die("Not a commit (actually a %s): %s",
  1674. typename(oe->type), command_buf.buf);
  1675. }
  1676. /*
  1677. * Accept the sha1 without checking; it expected to be in
  1678. * another repository.
  1679. */
  1680. } else if (inline_data) {
  1681. static struct strbuf buf = STRBUF_INIT;
  1682. if (p != uq.buf) {
  1683. strbuf_addstr(&uq, p);
  1684. p = uq.buf;
  1685. }
  1686. read_next_command();
  1687. parse_data(&buf);
  1688. store_object(OBJ_BLOB, &buf, &last_blob, sha1, 0);
  1689. } else if (oe) {
  1690. if (oe->type != OBJ_BLOB)
  1691. die("Not a blob (actually a %s): %s",
  1692. typename(oe->type), command_buf.buf);
  1693. } else {
  1694. enum object_type type = sha1_object_info(sha1, NULL);
  1695. if (type < 0)
  1696. die("Blob not found: %s", command_buf.buf);
  1697. if (type != OBJ_BLOB)
  1698. die("Not a blob (actually a %s): %s",
  1699. typename(type), command_buf.buf);
  1700. }
  1701. tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
  1702. }
  1703. static void file_change_d(struct branch *b)
  1704. {
  1705. const char *p = command_buf.buf + 2;
  1706. static struct strbuf uq = STRBUF_INIT;
  1707. const char *endp;
  1708. strbuf_reset(&uq);
  1709. if (!unquote_c_style(&uq, p, &endp)) {
  1710. if (*endp)
  1711. die("Garbage after path in: %s", command_buf.buf);
  1712. p = uq.buf;
  1713. }
  1714. tree_content_remove(&b->branch_tree, p, NULL);
  1715. }
  1716. static void file_change_cr(struct branch *b, int rename)
  1717. {
  1718. const char *s, *d;
  1719. static struct strbuf s_uq = STRBUF_INIT;
  1720. static struct strbuf d_uq = STRBUF_INIT;
  1721. const char *endp;
  1722. struct tree_entry leaf;
  1723. s = command_buf.buf + 2;
  1724. strbuf_reset(&s_uq);
  1725. if (!unquote_c_style(&s_uq, s, &endp)) {
  1726. if (*endp != ' ')
  1727. die("Missing space after source: %s", command_buf.buf);
  1728. } else {
  1729. endp = strchr(s, ' ');
  1730. if (!endp)
  1731. die("Missing space after source: %s", command_buf.buf);
  1732. strbuf_add(&s_uq, s, endp - s);
  1733. }
  1734. s = s_uq.buf;
  1735. endp++;
  1736. if (!*endp)
  1737. die("Missing dest: %s", command_buf.buf);
  1738. d = endp;
  1739. strbuf_reset(&d_uq);
  1740. if (!unquote_c_style(&d_uq, d, &endp)) {
  1741. if (*endp)
  1742. die("Garbage after dest in: %s", command_buf.buf);
  1743. d = d_uq.buf;
  1744. }
  1745. memset(&leaf, 0, sizeof(leaf));
  1746. if (rename)
  1747. tree_content_remove(&b->branch_tree, s, &leaf);
  1748. else
  1749. tree_content_get(&b->branch_tree, s, &leaf);
  1750. if (!leaf.versions[1].mode)
  1751. die("Path %s not in branch", s);
  1752. tree_content_set(&b->branch_tree, d,
  1753. leaf.versions[1].sha1,
  1754. leaf.versions[1].mode,
  1755. leaf.tree);
  1756. }
  1757. static void file_change_deleteall(struct branch *b)
  1758. {
  1759. release_tree_content_recursive(b->branch_tree.tree);
  1760. hashclr(b->branch_tree.versions[0].sha1);
  1761. hashclr(b->branch_tree.versions[1].sha1);
  1762. load_tree(&b->branch_tree);
  1763. }
  1764. static void parse_from_commit(struct branch *b, char *buf, unsigned long size)
  1765. {
  1766. if (!buf || size < 46)
  1767. die("Not a valid commit: %s", sha1_to_hex(b->sha1));
  1768. if (memcmp("tree ", buf, 5)
  1769. || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
  1770. die("The commit %s is corrupt", sha1_to_hex(b->sha1));
  1771. hashcpy(b->branch_tree.versions[0].sha1,
  1772. b->branch_tree.versions[1].sha1);
  1773. }
  1774. static void parse_from_existing(struct branch *b)
  1775. {
  1776. if (is_null_sha1(b->sha1)) {
  1777. hashclr(b->branch_tree.versions[0].sha1);
  1778. hashclr(b->branch_tree.versions[1].sha1);
  1779. } else {
  1780. unsigned long size;
  1781. char *buf;
  1782. buf = read_object_with_reference(b->sha1,
  1783. commit_type, &size, b->sha1);
  1784. parse_from_commit(b, buf, size);
  1785. free(buf);
  1786. }
  1787. }
  1788. static int parse_from(struct branch *b)
  1789. {
  1790. const char *from;
  1791. struct branch *s;
  1792. if (prefixcmp(command_buf.buf, "from "))
  1793. return 0;
  1794. if (b->branch_tree.tree) {
  1795. release_tree_content_recursive(b->branch_tree.tree);
  1796. b->branch_tree.tree = NULL;
  1797. }
  1798. from = strchr(command_buf.buf, ' ') + 1;
  1799. s = lookup_branch(from);
  1800. if (b == s)
  1801. die("Can't create a branch from itself: %s", b->name);
  1802. else if (s) {
  1803. unsigned char *t = s->branch_tree.versions[1].sha1;
  1804. hashcpy(b->sha1, s->sha1);
  1805. hashcpy(b->branch_tree.versions[0].sha1, t);
  1806. hashcpy(b->branch_tree.versions[1].sha1, t);
  1807. } else if (*from == ':') {
  1808. uintmax_t idnum = strtoumax(from + 1, NULL, 10);
  1809. struct object_entry *oe = find_mark(idnum);
  1810. if (oe->type != OBJ_COMMIT)
  1811. die("Mark :%" PRIuMAX " not a commit", idnum);
  1812. hashcpy(b->sha1, oe->sha1);
  1813. if (oe->pack_id != MAX_PACK_ID) {
  1814. unsigned long size;
  1815. char *buf = gfi_unpack_entry(oe, &size);
  1816. parse_from_commit(b, buf, size);
  1817. free(buf);
  1818. } else
  1819. parse_from_existing(b);
  1820. } else if (!get_sha1(from, b->sha1))
  1821. parse_from_existing(b);
  1822. else
  1823. die("Invalid ref name or SHA1 expression: %s", from);
  1824. read_next_command();
  1825. return 1;
  1826. }
  1827. static struct hash_list *parse_merge(unsigned int *count)
  1828. {
  1829. struct hash_list *list = NULL, *n, *e = e;
  1830. const char *from;
  1831. struct branch *s;
  1832. *count = 0;
  1833. while (!prefixcmp(command_buf.buf, "merge ")) {
  1834. from = strchr(command_buf.buf, ' ') + 1;
  1835. n = xmalloc(sizeof(*n));
  1836. s = lookup_branch(from);
  1837. if (s)
  1838. hashcpy(n->sha1, s->sha1);
  1839. else if (*from == ':') {
  1840. uintmax_t idnum = strtoumax(from + 1, NULL, 10);
  1841. struct object_entry *oe = find_mark(idnum);
  1842. if (oe->type != OBJ_COMMIT)
  1843. die("Mark :%" PRIuMAX " not a commit", idnum);
  1844. hashcpy(n->sha1, oe->sha1);
  1845. } else if (!get_sha1(from, n->sha1)) {
  1846. unsigned long size;
  1847. char *buf = read_object_with_reference(n->sha1,
  1848. commit_type, &size, n->sha1);
  1849. if (!buf || size < 46)
  1850. die("Not a valid commit: %s", from);
  1851. free(buf);
  1852. } else
  1853. die("Invalid ref name or SHA1 expression: %s", from);
  1854. n->next = NULL;
  1855. if (list)
  1856. e->next = n;
  1857. else
  1858. list = n;
  1859. e = n;
  1860. (*count)++;
  1861. read_next_command();
  1862. }
  1863. return list;
  1864. }
  1865. static void parse_new_commit(void)
  1866. {
  1867. static struct strbuf msg = STRBUF_INIT;
  1868. struct branch *b;
  1869. char *sp;
  1870. char *author = NULL;
  1871. char *committer = NULL;
  1872. struct hash_list *merge_list = NULL;
  1873. unsigned int merge_count;
  1874. /* Obtain the branch name from the rest of our command */
  1875. sp = strchr(command_buf.buf, ' ') + 1;
  1876. b = lookup_branch(sp);
  1877. if (!b)
  1878. b = new_branch(sp);
  1879. read_next_command();
  1880. parse_mark();
  1881. if (!prefixcmp(command_buf.buf, "author ")) {
  1882. author = parse_ident(command_buf.buf + 7);
  1883. read_next_command();
  1884. }
  1885. if (!prefixcmp(command_buf.buf, "committer ")) {
  1886. committer = parse_ident(command_buf.buf + 10);
  1887. read_next_command();
  1888. }
  1889. if (!committer)
  1890. die("Expected committer but didn't get one");
  1891. parse_data(&msg);
  1892. read_next_command();
  1893. parse_from(b);
  1894. merge_list = parse_merge(&merge_count);
  1895. /* ensure the branch is active/loaded */
  1896. if (!b->branch_tree.tree || !max_active_branches) {
  1897. unload_one_branch();
  1898. load_branch(b);
  1899. }
  1900. /* file_change* */
  1901. while (command_buf.len > 0) {
  1902. if (!prefixcmp(command_buf.buf, "M "))
  1903. file_change_m(b);
  1904. else if (!prefixcmp(command_buf.buf, "D "))
  1905. file_change_d(b);
  1906. else if (!prefixcmp(command_buf.buf, "R "))
  1907. file_change_cr(b, 1);
  1908. else if (!prefixcmp(command_buf.buf, "C "))
  1909. file_change_cr(b, 0);
  1910. else if (!strcmp("deleteall", command_buf.buf))
  1911. file_change_deleteall(b);
  1912. else {
  1913. unread_command_buf = 1;
  1914. break;
  1915. }
  1916. if (read_next_command() == EOF)
  1917. break;
  1918. }
  1919. /* build the tree and the commit */
  1920. store_tree(&b->branch_tree);
  1921. hashcpy(b->branch_tree.versions[0].sha1,
  1922. b->branch_tree.versions[1].sha1);
  1923. strbuf_reset(&new_data);
  1924. strbuf_addf(&new_data, "tree %s\n",
  1925. sha1_to_hex(b->branch_tree.versions[1].sha1));
  1926. if (!is_null_sha1(b->sha1))
  1927. strbuf_addf(&new_data, "parent %s\n", sha1_to_hex(b->sha1));
  1928. while (merge_list) {
  1929. struct hash_list *next = merge_list->next;
  1930. strbuf_addf(&new_data, "parent %s\n", sha1_to_hex(merge_list->sha1));
  1931. free(merge_list);
  1932. merge_list = next;
  1933. }
  1934. strbuf_addf(&new_data,
  1935. "author %s\n"
  1936. "committer %s\n"
  1937. "\n",
  1938. author ? author : committer, committer);
  1939. strbuf_addbuf(&new_data, &msg);
  1940. free(author);
  1941. free(committer);
  1942. if (!store_object(OBJ_COMMIT, &new_data, NULL, b->sha1, next_mark))
  1943. b->pack_id = pack_id;
  1944. b->last_commit = object_count_by_type[OBJ_COMMIT];
  1945. }
  1946. static void parse_new_tag(void)
  1947. {
  1948. static struct strbuf msg = STRBUF_INIT;
  1949. char *sp;
  1950. const char *from;
  1951. char *tagger;
  1952. struct branch *s;
  1953. struct tag *t;
  1954. uintmax_t from_mark = 0;
  1955. unsigned char sha1[20];
  1956. /* Obtain the new tag name from the rest of our command */
  1957. sp = strchr(command_buf.buf, ' ') + 1;
  1958. t = pool_alloc(sizeof(struct tag));
  1959. t->next_tag = NULL;
  1960. t->name = pool_strdup(sp);
  1961. if (last_tag)
  1962. last_tag->next_tag = t;
  1963. else
  1964. first_tag = t;
  1965. last_tag = t;
  1966. read_next_command();
  1967. /* from ... */
  1968. if (prefixcmp(command_buf.buf, "from "))
  1969. die("Expected from command, got %s", command_buf.buf);
  1970. from = strchr(command_buf.buf, ' ') + 1;
  1971. s = lookup_branch(from);
  1972. if (s) {
  1973. hashcpy(sha1, s->sha1);
  1974. } else if (*from == ':') {
  1975. struct object_entry *oe;
  1976. from_mark = strtoumax(from + 1, NULL, 10);
  1977. oe = find_mark(from_mark);
  1978. if (oe->type != OBJ_COMMIT)
  1979. die("Mark :%" PRIuMAX " not a commit", from_mark);
  1980. hashcpy(sha1, oe->sha1);
  1981. } else if (!get_sha1(from, sha1)) {
  1982. unsigned long size;
  1983. char *buf;
  1984. buf = read_object_with_reference(sha1,
  1985. commit_type, &size, sha1);
  1986. if (!buf || size < 46)
  1987. die("Not a valid commit: %s", from);
  1988. free(buf);
  1989. } else
  1990. die("Invalid ref name or SHA1 expression: %s", from);
  1991. read_next_command();
  1992. /* tagger ... */
  1993. if (prefixcmp(command_buf.buf, "tagger "))
  1994. die("Expected tagger command, got %s", command_buf.buf);
  1995. tagger = parse_ident(command_buf.buf + 7);
  1996. /* tag payload/message */
  1997. read_next_command();
  1998. parse_data(&msg);
  1999. /* build the tag object */
  2000. strbuf_reset(&new_data);
  2001. strbuf_addf(&new_data,
  2002. "object %s\n"
  2003. "type %s\n"
  2004. "tag %s\n"
  2005. "tagger %s\n"
  2006. "\n",
  2007. sha1_to_hex(sha1), commit_type, t->name, tagger);
  2008. strbuf_addbuf(&new_data, &msg);
  2009. free(tagger);
  2010. if (store_object(OBJ_TAG, &new_data, NULL, t->sha1, 0))
  2011. t->pack_id = MAX_PACK_ID;
  2012. else
  2013. t->pack_id = pack_id;
  2014. }
  2015. static void parse_reset_branch(void)
  2016. {
  2017. struct branch *b;
  2018. char *sp;
  2019. /* Obtain the branch name from the rest of our command */
  2020. sp = strchr(command_buf.buf, ' ') + 1;
  2021. b = lookup_branch(sp);
  2022. if (b) {
  2023. hashclr(b->sha1);
  2024. hashclr(b->branch_tree.versions[0].sha1);
  2025. hashclr(b->branch_tree.versions[1].sha1);
  2026. if (b->branch_tree.tree) {
  2027. release_tree_content_recursive(b->branch_tree.tree);
  2028. b->branch_tree.tree = NULL;
  2029. }
  2030. }
  2031. else
  2032. b = new_branch(sp);
  2033. read_next_command();
  2034. parse_from(b);
  2035. if (command_buf.len > 0)
  2036. unread_command_buf = 1;
  2037. }
  2038. static void parse_checkpoint(void)
  2039. {
  2040. if (object_count) {
  2041. cycle_packfile();
  2042. dump_branches();
  2043. dump_tags();
  2044. dump_marks();
  2045. }
  2046. skip_optional_lf();
  2047. }
  2048. static void parse_progress(void)
  2049. {
  2050. fwrite(command_buf.buf, 1, command_buf.len, stdout);
  2051. fputc('\n', stdout);
  2052. fflush(stdout);
  2053. skip_optional_lf();
  2054. }
  2055. static void import_marks(const char *input_file)
  2056. {
  2057. char line[512];
  2058. FILE *f = fopen(input_file, "r");
  2059. if (!f)
  2060. die("cannot read %s: %s", input_file, strerror(errno));
  2061. while (fgets(line, sizeof(line), f)) {
  2062. uintmax_t mark;
  2063. char *end;
  2064. unsigned char sha1[20];
  2065. struct object_entry *e;
  2066. end = strchr(line, '\n');
  2067. if (line[0] != ':' || !end)
  2068. die("corrupt mark line: %s", line);
  2069. *end = 0;
  2070. mark = strtoumax(line + 1, &end, 10);
  2071. if (!mark || end == line + 1
  2072. || *end != ' ' || get_sha1(end + 1, sha1))
  2073. die("corrupt mark line: %s", line);
  2074. e = find_object(sha1);
  2075. if (!e) {
  2076. enum object_type type = sha1_object_info(sha1, NULL);
  2077. if (type < 0)
  2078. die("object not found: %s", sha1_to_hex(sha1));
  2079. e = insert_object(sha1);
  2080. e->type = type;
  2081. e->pack_id = MAX_PACK_ID;
  2082. e->offset = 1; /* just not zero! */
  2083. }
  2084. insert_mark(mark, e);
  2085. }
  2086. fclose(f);
  2087. }
  2088. static int git_pack_config(const char *k, const char *v, void *cb)
  2089. {
  2090. if (!strcmp(k, "pack.depth")) {
  2091. max_depth = git_config_int(k, v);
  2092. if (max_depth > MAX_DEPTH)
  2093. max_depth = MAX_DEPTH;
  2094. return 0;
  2095. }
  2096. if (!strcmp(k, "pack.compression")) {
  2097. int level = git_config_int(k, v);
  2098. if (level == -1)
  2099. level = Z_DEFAULT_COMPRESSION;
  2100. else if (level < 0 || level > Z_BEST_COMPRESSION)
  2101. die("bad pack compression level %d", level);
  2102. pack_compression_level = level;
  2103. pack_compression_seen = 1;
  2104. return 0;
  2105. }
  2106. return git_default_config(k, v, cb);
  2107. }
  2108. static const char fast_import_usage[] =
  2109. "git fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
  2110. int main(int argc, const char **argv)
  2111. {
  2112. unsigned int i, show_stats = 1;
  2113. setup_git_directory();
  2114. git_config(git_pack_config, NULL);
  2115. if (!pack_compression_seen && core_compression_seen)
  2116. pack_compression_level = core_compression_level;
  2117. alloc_objects(object_entry_alloc);
  2118. strbuf_init(&command_buf, 0);
  2119. atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
  2120. branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
  2121. avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
  2122. marks = pool_calloc(1, sizeof(struct mark_set));
  2123. for (i = 1; i < argc; i++) {
  2124. const char *a = argv[i];
  2125. if (*a != '-' || !strcmp(a, "--"))
  2126. break;
  2127. else if (!prefixcmp(a, "--date-format=")) {
  2128. const char *fmt = a + 14;
  2129. if (!strcmp(fmt, "raw"))
  2130. whenspec = WHENSPEC_RAW;
  2131. else if (!strcmp(fmt, "rfc2822"))
  2132. whenspec = WHENSPEC_RFC2822;
  2133. else if (!strcmp(fmt, "now"))
  2134. whenspec = WHENSPEC_NOW;
  2135. else
  2136. die("unknown --date-format argument %s", fmt);
  2137. }
  2138. else if (!prefixcmp(a, "--max-pack-size="))
  2139. max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
  2140. else if (!prefixcmp(a, "--depth=")) {
  2141. max_depth = strtoul(a + 8, NULL, 0);
  2142. if (max_depth > MAX_DEPTH)
  2143. die("--depth cannot exceed %u", MAX_DEPTH);
  2144. }
  2145. else if (!prefixcmp(a, "--active-branches="))
  2146. max_active_branches = strtoul(a + 18, NULL, 0);
  2147. else if (!prefixcmp(a, "--import-marks="))
  2148. import_marks(a + 15);
  2149. else if (!prefixcmp(a, "--export-marks="))
  2150. mark_file = a + 15;
  2151. else if (!prefixcmp(a, "--export-pack-edges=")) {
  2152. if (pack_edges)
  2153. fclose(pack_edges);
  2154. pack_edges = fopen(a + 20, "a");
  2155. if (!pack_edges)
  2156. die("Cannot open %s: %s", a + 20, strerror(errno));
  2157. } else if (!strcmp(a, "--force"))
  2158. force_update = 1;
  2159. else if (!strcmp(a, "--quiet"))
  2160. show_stats = 0;
  2161. else if (!strcmp(a, "--stats"))
  2162. show_stats = 1;
  2163. else
  2164. die("unknown option %s", a);
  2165. }
  2166. if (i != argc)
  2167. usage(fast_import_usage);
  2168. rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
  2169. for (i = 0; i < (cmd_save - 1); i++)
  2170. rc_free[i].next = &rc_free[i + 1];
  2171. rc_free[cmd_save - 1].next = NULL;
  2172. prepare_packed_git();
  2173. start_packfile();
  2174. set_die_routine(die_nicely);
  2175. while (read_next_command() != EOF) {
  2176. if (!strcmp("blob", command_buf.buf))
  2177. parse_new_blob();
  2178. else if (!prefixcmp(command_buf.buf, "commit "))
  2179. parse_new_commit();
  2180. else if (!prefixcmp(command_buf.buf, "tag "))
  2181. parse_new_tag();
  2182. else if (!prefixcmp(command_buf.buf, "reset "))
  2183. parse_reset_branch();
  2184. else if (!strcmp("checkpoint", command_buf.buf))
  2185. parse_checkpoint();
  2186. else if (!prefixcmp(command_buf.buf, "progress "))
  2187. parse_progress();
  2188. else
  2189. die("Unsupported command: %s", command_buf.buf);
  2190. }
  2191. end_packfile();
  2192. dump_branches();
  2193. dump_tags();
  2194. unkeep_all_packs();
  2195. dump_marks();
  2196. if (pack_edges)
  2197. fclose(pack_edges);
  2198. if (show_stats) {
  2199. uintmax_t total_count = 0, duplicate_count = 0;
  2200. for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
  2201. total_count += object_count_by_type[i];
  2202. for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
  2203. duplicate_count += duplicate_count_by_type[i];
  2204. fprintf(stderr, "%s statistics:\n", argv[0]);
  2205. fprintf(stderr, "---------------------------------------------------------------------\n");
  2206. fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
  2207. fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
  2208. fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
  2209. fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
  2210. fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
  2211. fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
  2212. fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
  2213. fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
  2214. fprintf(stderr, " atoms: %10u\n", atom_cnt);
  2215. fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
  2216. fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024));
  2217. fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
  2218. fprintf(stderr, "---------------------------------------------------------------------\n");
  2219. pack_report();
  2220. fprintf(stderr, "---------------------------------------------------------------------\n");
  2221. fprintf(stderr, "\n");
  2222. }
  2223. return failure ? 1 : 0;
  2224. }