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.

3477 lines
87KB

  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "repository.h"
  4. #include "config.h"
  5. #include "lockfile.h"
  6. #include "object.h"
  7. #include "blob.h"
  8. #include "tree.h"
  9. #include "commit.h"
  10. #include "delta.h"
  11. #include "pack.h"
  12. #include "refs.h"
  13. #include "csum-file.h"
  14. #include "quote.h"
  15. #include "dir.h"
  16. #include "run-command.h"
  17. #include "packfile.h"
  18. #include "object-store.h"
  19. #include "mem-pool.h"
  20. #include "commit-reach.h"
  21. #define PACK_ID_BITS 16
  22. #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
  23. #define DEPTH_BITS 13
  24. #define MAX_DEPTH ((1<<DEPTH_BITS)-1)
  25. /*
  26. * We abuse the setuid bit on directories to mean "do not delta".
  27. */
  28. #define NO_DELTA S_ISUID
  29. /*
  30. * The amount of additional space required in order to write an object into the
  31. * current pack. This is the hash lengths at the end of the pack, plus the
  32. * length of one object ID.
  33. */
  34. #define PACK_SIZE_THRESHOLD (the_hash_algo->rawsz * 3)
  35. struct object_entry {
  36. struct pack_idx_entry idx;
  37. struct object_entry *next;
  38. uint32_t type : TYPE_BITS,
  39. pack_id : PACK_ID_BITS,
  40. depth : DEPTH_BITS;
  41. };
  42. struct object_entry_pool {
  43. struct object_entry_pool *next_pool;
  44. struct object_entry *next_free;
  45. struct object_entry *end;
  46. struct object_entry entries[FLEX_ARRAY]; /* more */
  47. };
  48. struct mark_set {
  49. union {
  50. struct object_entry *marked[1024];
  51. struct mark_set *sets[1024];
  52. } data;
  53. unsigned int shift;
  54. };
  55. struct last_object {
  56. struct strbuf data;
  57. off_t offset;
  58. unsigned int depth;
  59. unsigned no_swap : 1;
  60. };
  61. struct atom_str {
  62. struct atom_str *next_atom;
  63. unsigned short str_len;
  64. char str_dat[FLEX_ARRAY]; /* more */
  65. };
  66. struct tree_content;
  67. struct tree_entry {
  68. struct tree_content *tree;
  69. struct atom_str *name;
  70. struct tree_entry_ms {
  71. uint16_t mode;
  72. struct object_id oid;
  73. } versions[2];
  74. };
  75. struct tree_content {
  76. unsigned int entry_capacity; /* must match avail_tree_content */
  77. unsigned int entry_count;
  78. unsigned int delta_depth;
  79. struct tree_entry *entries[FLEX_ARRAY]; /* more */
  80. };
  81. struct avail_tree_content {
  82. unsigned int entry_capacity; /* must match tree_content */
  83. struct avail_tree_content *next_avail;
  84. };
  85. struct branch {
  86. struct branch *table_next_branch;
  87. struct branch *active_next_branch;
  88. const char *name;
  89. struct tree_entry branch_tree;
  90. uintmax_t last_commit;
  91. uintmax_t num_notes;
  92. unsigned active : 1;
  93. unsigned delete : 1;
  94. unsigned pack_id : PACK_ID_BITS;
  95. struct object_id oid;
  96. };
  97. struct tag {
  98. struct tag *next_tag;
  99. const char *name;
  100. unsigned int pack_id;
  101. struct object_id oid;
  102. };
  103. struct hash_list {
  104. struct hash_list *next;
  105. struct object_id oid;
  106. };
  107. typedef enum {
  108. WHENSPEC_RAW = 1,
  109. WHENSPEC_RFC2822,
  110. WHENSPEC_NOW
  111. } whenspec_type;
  112. struct recent_command {
  113. struct recent_command *prev;
  114. struct recent_command *next;
  115. char *buf;
  116. };
  117. /* Configured limits on output */
  118. static unsigned long max_depth = 50;
  119. static off_t max_packsize;
  120. static int unpack_limit = 100;
  121. static int force_update;
  122. /* Stats and misc. counters */
  123. static uintmax_t alloc_count;
  124. static uintmax_t marks_set_count;
  125. static uintmax_t object_count_by_type[1 << TYPE_BITS];
  126. static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
  127. static uintmax_t delta_count_by_type[1 << TYPE_BITS];
  128. static uintmax_t delta_count_attempts_by_type[1 << TYPE_BITS];
  129. static unsigned long object_count;
  130. static unsigned long branch_count;
  131. static unsigned long branch_load_count;
  132. static int failure;
  133. static FILE *pack_edges;
  134. static unsigned int show_stats = 1;
  135. static int global_argc;
  136. static const char **global_argv;
  137. /* Memory pools */
  138. static struct mem_pool fi_mem_pool = {NULL, 2*1024*1024 -
  139. sizeof(struct mp_block), 0 };
  140. /* Atom management */
  141. static unsigned int atom_table_sz = 4451;
  142. static unsigned int atom_cnt;
  143. static struct atom_str **atom_table;
  144. /* The .pack file being generated */
  145. static struct pack_idx_option pack_idx_opts;
  146. static unsigned int pack_id;
  147. static struct hashfile *pack_file;
  148. static struct packed_git *pack_data;
  149. static struct packed_git **all_packs;
  150. static off_t pack_size;
  151. /* Table of objects we've written. */
  152. static unsigned int object_entry_alloc = 5000;
  153. static struct object_entry_pool *blocks;
  154. static struct object_entry *object_table[1 << 16];
  155. static struct mark_set *marks;
  156. static const char *export_marks_file;
  157. static const char *import_marks_file;
  158. static int import_marks_file_from_stream;
  159. static int import_marks_file_ignore_missing;
  160. static int import_marks_file_done;
  161. static int relative_marks_paths;
  162. /* Our last blob */
  163. static struct last_object last_blob = { STRBUF_INIT, 0, 0, 0 };
  164. /* Tree management */
  165. static unsigned int tree_entry_alloc = 1000;
  166. static void *avail_tree_entry;
  167. static unsigned int avail_tree_table_sz = 100;
  168. static struct avail_tree_content **avail_tree_table;
  169. static size_t tree_entry_allocd;
  170. static struct strbuf old_tree = STRBUF_INIT;
  171. static struct strbuf new_tree = STRBUF_INIT;
  172. /* Branch data */
  173. static unsigned long max_active_branches = 5;
  174. static unsigned long cur_active_branches;
  175. static unsigned long branch_table_sz = 1039;
  176. static struct branch **branch_table;
  177. static struct branch *active_branches;
  178. /* Tag data */
  179. static struct tag *first_tag;
  180. static struct tag *last_tag;
  181. /* Input stream parsing */
  182. static whenspec_type whenspec = WHENSPEC_RAW;
  183. static struct strbuf command_buf = STRBUF_INIT;
  184. static int unread_command_buf;
  185. static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
  186. static struct recent_command *cmd_tail = &cmd_hist;
  187. static struct recent_command *rc_free;
  188. static unsigned int cmd_save = 100;
  189. static uintmax_t next_mark;
  190. static struct strbuf new_data = STRBUF_INIT;
  191. static int seen_data_command;
  192. static int require_explicit_termination;
  193. /* Signal handling */
  194. static volatile sig_atomic_t checkpoint_requested;
  195. /* Where to write output of cat-blob commands */
  196. static int cat_blob_fd = STDOUT_FILENO;
  197. static void parse_argv(void);
  198. static void parse_get_mark(const char *p);
  199. static void parse_cat_blob(const char *p);
  200. static void parse_ls(const char *p, struct branch *b);
  201. static void write_branch_report(FILE *rpt, struct branch *b)
  202. {
  203. fprintf(rpt, "%s:\n", b->name);
  204. fprintf(rpt, " status :");
  205. if (b->active)
  206. fputs(" active", rpt);
  207. if (b->branch_tree.tree)
  208. fputs(" loaded", rpt);
  209. if (is_null_oid(&b->branch_tree.versions[1].oid))
  210. fputs(" dirty", rpt);
  211. fputc('\n', rpt);
  212. fprintf(rpt, " tip commit : %s\n", oid_to_hex(&b->oid));
  213. fprintf(rpt, " old tree : %s\n",
  214. oid_to_hex(&b->branch_tree.versions[0].oid));
  215. fprintf(rpt, " cur tree : %s\n",
  216. oid_to_hex(&b->branch_tree.versions[1].oid));
  217. fprintf(rpt, " commit clock: %" PRIuMAX "\n", b->last_commit);
  218. fputs(" last pack : ", rpt);
  219. if (b->pack_id < MAX_PACK_ID)
  220. fprintf(rpt, "%u", b->pack_id);
  221. fputc('\n', rpt);
  222. fputc('\n', rpt);
  223. }
  224. static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *);
  225. static void write_crash_report(const char *err)
  226. {
  227. char *loc = git_pathdup("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
  228. FILE *rpt = fopen(loc, "w");
  229. struct branch *b;
  230. unsigned long lu;
  231. struct recent_command *rc;
  232. if (!rpt) {
  233. error_errno("can't write crash report %s", loc);
  234. free(loc);
  235. return;
  236. }
  237. fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
  238. fprintf(rpt, "fast-import crash report:\n");
  239. fprintf(rpt, " fast-import process: %"PRIuMAX"\n", (uintmax_t) getpid());
  240. fprintf(rpt, " parent process : %"PRIuMAX"\n", (uintmax_t) getppid());
  241. fprintf(rpt, " at %s\n", show_date(time(NULL), 0, DATE_MODE(ISO8601)));
  242. fputc('\n', rpt);
  243. fputs("fatal: ", rpt);
  244. fputs(err, rpt);
  245. fputc('\n', rpt);
  246. fputc('\n', rpt);
  247. fputs("Most Recent Commands Before Crash\n", rpt);
  248. fputs("---------------------------------\n", rpt);
  249. for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
  250. if (rc->next == &cmd_hist)
  251. fputs("* ", rpt);
  252. else
  253. fputs(" ", rpt);
  254. fputs(rc->buf, rpt);
  255. fputc('\n', rpt);
  256. }
  257. fputc('\n', rpt);
  258. fputs("Active Branch LRU\n", rpt);
  259. fputs("-----------------\n", rpt);
  260. fprintf(rpt, " active_branches = %lu cur, %lu max\n",
  261. cur_active_branches,
  262. max_active_branches);
  263. fputc('\n', rpt);
  264. fputs(" pos clock name\n", rpt);
  265. fputs(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
  266. for (b = active_branches, lu = 0; b; b = b->active_next_branch)
  267. fprintf(rpt, " %2lu) %6" PRIuMAX" %s\n",
  268. ++lu, b->last_commit, b->name);
  269. fputc('\n', rpt);
  270. fputs("Inactive Branches\n", rpt);
  271. fputs("-----------------\n", rpt);
  272. for (lu = 0; lu < branch_table_sz; lu++) {
  273. for (b = branch_table[lu]; b; b = b->table_next_branch)
  274. write_branch_report(rpt, b);
  275. }
  276. if (first_tag) {
  277. struct tag *tg;
  278. fputc('\n', rpt);
  279. fputs("Annotated Tags\n", rpt);
  280. fputs("--------------\n", rpt);
  281. for (tg = first_tag; tg; tg = tg->next_tag) {
  282. fputs(oid_to_hex(&tg->oid), rpt);
  283. fputc(' ', rpt);
  284. fputs(tg->name, rpt);
  285. fputc('\n', rpt);
  286. }
  287. }
  288. fputc('\n', rpt);
  289. fputs("Marks\n", rpt);
  290. fputs("-----\n", rpt);
  291. if (export_marks_file)
  292. fprintf(rpt, " exported to %s\n", export_marks_file);
  293. else
  294. dump_marks_helper(rpt, 0, marks);
  295. fputc('\n', rpt);
  296. fputs("-------------------\n", rpt);
  297. fputs("END OF CRASH REPORT\n", rpt);
  298. fclose(rpt);
  299. free(loc);
  300. }
  301. static void end_packfile(void);
  302. static void unkeep_all_packs(void);
  303. static void dump_marks(void);
  304. static NORETURN void die_nicely(const char *err, va_list params)
  305. {
  306. static int zombie;
  307. char message[2 * PATH_MAX];
  308. vsnprintf(message, sizeof(message), err, params);
  309. fputs("fatal: ", stderr);
  310. fputs(message, stderr);
  311. fputc('\n', stderr);
  312. if (!zombie) {
  313. zombie = 1;
  314. write_crash_report(message);
  315. end_packfile();
  316. unkeep_all_packs();
  317. dump_marks();
  318. }
  319. exit(128);
  320. }
  321. #ifndef SIGUSR1 /* Windows, for example */
  322. static void set_checkpoint_signal(void)
  323. {
  324. }
  325. #else
  326. static void checkpoint_signal(int signo)
  327. {
  328. checkpoint_requested = 1;
  329. }
  330. static void set_checkpoint_signal(void)
  331. {
  332. struct sigaction sa;
  333. memset(&sa, 0, sizeof(sa));
  334. sa.sa_handler = checkpoint_signal;
  335. sigemptyset(&sa.sa_mask);
  336. sa.sa_flags = SA_RESTART;
  337. sigaction(SIGUSR1, &sa, NULL);
  338. }
  339. #endif
  340. static void alloc_objects(unsigned int cnt)
  341. {
  342. struct object_entry_pool *b;
  343. b = xmalloc(sizeof(struct object_entry_pool)
  344. + cnt * sizeof(struct object_entry));
  345. b->next_pool = blocks;
  346. b->next_free = b->entries;
  347. b->end = b->entries + cnt;
  348. blocks = b;
  349. alloc_count += cnt;
  350. }
  351. static struct object_entry *new_object(struct object_id *oid)
  352. {
  353. struct object_entry *e;
  354. if (blocks->next_free == blocks->end)
  355. alloc_objects(object_entry_alloc);
  356. e = blocks->next_free++;
  357. oidcpy(&e->idx.oid, oid);
  358. return e;
  359. }
  360. static struct object_entry *find_object(struct object_id *oid)
  361. {
  362. unsigned int h = oid->hash[0] << 8 | oid->hash[1];
  363. struct object_entry *e;
  364. for (e = object_table[h]; e; e = e->next)
  365. if (oideq(oid, &e->idx.oid))
  366. return e;
  367. return NULL;
  368. }
  369. static struct object_entry *insert_object(struct object_id *oid)
  370. {
  371. unsigned int h = oid->hash[0] << 8 | oid->hash[1];
  372. struct object_entry *e = object_table[h];
  373. while (e) {
  374. if (oideq(oid, &e->idx.oid))
  375. return e;
  376. e = e->next;
  377. }
  378. e = new_object(oid);
  379. e->next = object_table[h];
  380. e->idx.offset = 0;
  381. object_table[h] = e;
  382. return e;
  383. }
  384. static void invalidate_pack_id(unsigned int id)
  385. {
  386. unsigned int h;
  387. unsigned long lu;
  388. struct tag *t;
  389. for (h = 0; h < ARRAY_SIZE(object_table); h++) {
  390. struct object_entry *e;
  391. for (e = object_table[h]; e; e = e->next)
  392. if (e->pack_id == id)
  393. e->pack_id = MAX_PACK_ID;
  394. }
  395. for (lu = 0; lu < branch_table_sz; lu++) {
  396. struct branch *b;
  397. for (b = branch_table[lu]; b; b = b->table_next_branch)
  398. if (b->pack_id == id)
  399. b->pack_id = MAX_PACK_ID;
  400. }
  401. for (t = first_tag; t; t = t->next_tag)
  402. if (t->pack_id == id)
  403. t->pack_id = MAX_PACK_ID;
  404. }
  405. static unsigned int hc_str(const char *s, size_t len)
  406. {
  407. unsigned int r = 0;
  408. while (len-- > 0)
  409. r = r * 31 + *s++;
  410. return r;
  411. }
  412. static char *pool_strdup(const char *s)
  413. {
  414. size_t len = strlen(s) + 1;
  415. char *r = mem_pool_alloc(&fi_mem_pool, len);
  416. memcpy(r, s, len);
  417. return r;
  418. }
  419. static void insert_mark(uintmax_t idnum, struct object_entry *oe)
  420. {
  421. struct mark_set *s = marks;
  422. while ((idnum >> s->shift) >= 1024) {
  423. s = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
  424. s->shift = marks->shift + 10;
  425. s->data.sets[0] = marks;
  426. marks = s;
  427. }
  428. while (s->shift) {
  429. uintmax_t i = idnum >> s->shift;
  430. idnum -= i << s->shift;
  431. if (!s->data.sets[i]) {
  432. s->data.sets[i] = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
  433. s->data.sets[i]->shift = s->shift - 10;
  434. }
  435. s = s->data.sets[i];
  436. }
  437. if (!s->data.marked[idnum])
  438. marks_set_count++;
  439. s->data.marked[idnum] = oe;
  440. }
  441. static struct object_entry *find_mark(uintmax_t idnum)
  442. {
  443. uintmax_t orig_idnum = idnum;
  444. struct mark_set *s = marks;
  445. struct object_entry *oe = NULL;
  446. if ((idnum >> s->shift) < 1024) {
  447. while (s && s->shift) {
  448. uintmax_t i = idnum >> s->shift;
  449. idnum -= i << s->shift;
  450. s = s->data.sets[i];
  451. }
  452. if (s)
  453. oe = s->data.marked[idnum];
  454. }
  455. if (!oe)
  456. die("mark :%" PRIuMAX " not declared", orig_idnum);
  457. return oe;
  458. }
  459. static struct atom_str *to_atom(const char *s, unsigned short len)
  460. {
  461. unsigned int hc = hc_str(s, len) % atom_table_sz;
  462. struct atom_str *c;
  463. for (c = atom_table[hc]; c; c = c->next_atom)
  464. if (c->str_len == len && !strncmp(s, c->str_dat, len))
  465. return c;
  466. c = mem_pool_alloc(&fi_mem_pool, sizeof(struct atom_str) + len + 1);
  467. c->str_len = len;
  468. memcpy(c->str_dat, s, len);
  469. c->str_dat[len] = 0;
  470. c->next_atom = atom_table[hc];
  471. atom_table[hc] = c;
  472. atom_cnt++;
  473. return c;
  474. }
  475. static struct branch *lookup_branch(const char *name)
  476. {
  477. unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
  478. struct branch *b;
  479. for (b = branch_table[hc]; b; b = b->table_next_branch)
  480. if (!strcmp(name, b->name))
  481. return b;
  482. return NULL;
  483. }
  484. static struct branch *new_branch(const char *name)
  485. {
  486. unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
  487. struct branch *b = lookup_branch(name);
  488. if (b)
  489. die("Invalid attempt to create duplicate branch: %s", name);
  490. if (check_refname_format(name, REFNAME_ALLOW_ONELEVEL))
  491. die("Branch name doesn't conform to GIT standards: %s", name);
  492. b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch));
  493. b->name = pool_strdup(name);
  494. b->table_next_branch = branch_table[hc];
  495. b->branch_tree.versions[0].mode = S_IFDIR;
  496. b->branch_tree.versions[1].mode = S_IFDIR;
  497. b->num_notes = 0;
  498. b->active = 0;
  499. b->pack_id = MAX_PACK_ID;
  500. branch_table[hc] = b;
  501. branch_count++;
  502. return b;
  503. }
  504. static unsigned int hc_entries(unsigned int cnt)
  505. {
  506. cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
  507. return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
  508. }
  509. static struct tree_content *new_tree_content(unsigned int cnt)
  510. {
  511. struct avail_tree_content *f, *l = NULL;
  512. struct tree_content *t;
  513. unsigned int hc = hc_entries(cnt);
  514. for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
  515. if (f->entry_capacity >= cnt)
  516. break;
  517. if (f) {
  518. if (l)
  519. l->next_avail = f->next_avail;
  520. else
  521. avail_tree_table[hc] = f->next_avail;
  522. } else {
  523. cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
  524. f = mem_pool_alloc(&fi_mem_pool, sizeof(*t) + sizeof(t->entries[0]) * cnt);
  525. f->entry_capacity = cnt;
  526. }
  527. t = (struct tree_content*)f;
  528. t->entry_count = 0;
  529. t->delta_depth = 0;
  530. return t;
  531. }
  532. static void release_tree_entry(struct tree_entry *e);
  533. static void release_tree_content(struct tree_content *t)
  534. {
  535. struct avail_tree_content *f = (struct avail_tree_content*)t;
  536. unsigned int hc = hc_entries(f->entry_capacity);
  537. f->next_avail = avail_tree_table[hc];
  538. avail_tree_table[hc] = f;
  539. }
  540. static void release_tree_content_recursive(struct tree_content *t)
  541. {
  542. unsigned int i;
  543. for (i = 0; i < t->entry_count; i++)
  544. release_tree_entry(t->entries[i]);
  545. release_tree_content(t);
  546. }
  547. static struct tree_content *grow_tree_content(
  548. struct tree_content *t,
  549. int amt)
  550. {
  551. struct tree_content *r = new_tree_content(t->entry_count + amt);
  552. r->entry_count = t->entry_count;
  553. r->delta_depth = t->delta_depth;
  554. COPY_ARRAY(r->entries, t->entries, t->entry_count);
  555. release_tree_content(t);
  556. return r;
  557. }
  558. static struct tree_entry *new_tree_entry(void)
  559. {
  560. struct tree_entry *e;
  561. if (!avail_tree_entry) {
  562. unsigned int n = tree_entry_alloc;
  563. tree_entry_allocd += n * sizeof(struct tree_entry);
  564. ALLOC_ARRAY(e, n);
  565. avail_tree_entry = e;
  566. while (n-- > 1) {
  567. *((void**)e) = e + 1;
  568. e++;
  569. }
  570. *((void**)e) = NULL;
  571. }
  572. e = avail_tree_entry;
  573. avail_tree_entry = *((void**)e);
  574. return e;
  575. }
  576. static void release_tree_entry(struct tree_entry *e)
  577. {
  578. if (e->tree)
  579. release_tree_content_recursive(e->tree);
  580. *((void**)e) = avail_tree_entry;
  581. avail_tree_entry = e;
  582. }
  583. static struct tree_content *dup_tree_content(struct tree_content *s)
  584. {
  585. struct tree_content *d;
  586. struct tree_entry *a, *b;
  587. unsigned int i;
  588. if (!s)
  589. return NULL;
  590. d = new_tree_content(s->entry_count);
  591. for (i = 0; i < s->entry_count; i++) {
  592. a = s->entries[i];
  593. b = new_tree_entry();
  594. memcpy(b, a, sizeof(*a));
  595. if (a->tree && is_null_oid(&b->versions[1].oid))
  596. b->tree = dup_tree_content(a->tree);
  597. else
  598. b->tree = NULL;
  599. d->entries[i] = b;
  600. }
  601. d->entry_count = s->entry_count;
  602. d->delta_depth = s->delta_depth;
  603. return d;
  604. }
  605. static void start_packfile(void)
  606. {
  607. struct strbuf tmp_file = STRBUF_INIT;
  608. struct packed_git *p;
  609. struct pack_header hdr;
  610. int pack_fd;
  611. pack_fd = odb_mkstemp(&tmp_file, "pack/tmp_pack_XXXXXX");
  612. FLEX_ALLOC_STR(p, pack_name, tmp_file.buf);
  613. strbuf_release(&tmp_file);
  614. p->pack_fd = pack_fd;
  615. p->do_not_close = 1;
  616. pack_file = hashfd(pack_fd, p->pack_name);
  617. hdr.hdr_signature = htonl(PACK_SIGNATURE);
  618. hdr.hdr_version = htonl(2);
  619. hdr.hdr_entries = 0;
  620. hashwrite(pack_file, &hdr, sizeof(hdr));
  621. pack_data = p;
  622. pack_size = sizeof(hdr);
  623. object_count = 0;
  624. REALLOC_ARRAY(all_packs, pack_id + 1);
  625. all_packs[pack_id] = p;
  626. }
  627. static const char *create_index(void)
  628. {
  629. const char *tmpfile;
  630. struct pack_idx_entry **idx, **c, **last;
  631. struct object_entry *e;
  632. struct object_entry_pool *o;
  633. /* Build the table of object IDs. */
  634. ALLOC_ARRAY(idx, object_count);
  635. c = idx;
  636. for (o = blocks; o; o = o->next_pool)
  637. for (e = o->next_free; e-- != o->entries;)
  638. if (pack_id == e->pack_id)
  639. *c++ = &e->idx;
  640. last = idx + object_count;
  641. if (c != last)
  642. die("internal consistency error creating the index");
  643. tmpfile = write_idx_file(NULL, idx, object_count, &pack_idx_opts,
  644. pack_data->hash);
  645. free(idx);
  646. return tmpfile;
  647. }
  648. static char *keep_pack(const char *curr_index_name)
  649. {
  650. static const char *keep_msg = "fast-import";
  651. struct strbuf name = STRBUF_INIT;
  652. int keep_fd;
  653. odb_pack_name(&name, pack_data->hash, "keep");
  654. keep_fd = odb_pack_keep(name.buf);
  655. if (keep_fd < 0)
  656. die_errno("cannot create keep file");
  657. write_or_die(keep_fd, keep_msg, strlen(keep_msg));
  658. if (close(keep_fd))
  659. die_errno("failed to write keep file");
  660. odb_pack_name(&name, pack_data->hash, "pack");
  661. if (finalize_object_file(pack_data->pack_name, name.buf))
  662. die("cannot store pack file");
  663. odb_pack_name(&name, pack_data->hash, "idx");
  664. if (finalize_object_file(curr_index_name, name.buf))
  665. die("cannot store index file");
  666. free((void *)curr_index_name);
  667. return strbuf_detach(&name, NULL);
  668. }
  669. static void unkeep_all_packs(void)
  670. {
  671. struct strbuf name = STRBUF_INIT;
  672. int k;
  673. for (k = 0; k < pack_id; k++) {
  674. struct packed_git *p = all_packs[k];
  675. odb_pack_name(&name, p->hash, "keep");
  676. unlink_or_warn(name.buf);
  677. }
  678. strbuf_release(&name);
  679. }
  680. static int loosen_small_pack(const struct packed_git *p)
  681. {
  682. struct child_process unpack = CHILD_PROCESS_INIT;
  683. if (lseek(p->pack_fd, 0, SEEK_SET) < 0)
  684. die_errno("Failed seeking to start of '%s'", p->pack_name);
  685. unpack.in = p->pack_fd;
  686. unpack.git_cmd = 1;
  687. unpack.stdout_to_stderr = 1;
  688. argv_array_push(&unpack.args, "unpack-objects");
  689. if (!show_stats)
  690. argv_array_push(&unpack.args, "-q");
  691. return run_command(&unpack);
  692. }
  693. static void end_packfile(void)
  694. {
  695. static int running;
  696. if (running || !pack_data)
  697. return;
  698. running = 1;
  699. clear_delta_base_cache();
  700. if (object_count) {
  701. struct packed_git *new_p;
  702. struct object_id cur_pack_oid;
  703. char *idx_name;
  704. int i;
  705. struct branch *b;
  706. struct tag *t;
  707. close_pack_windows(pack_data);
  708. finalize_hashfile(pack_file, cur_pack_oid.hash, 0);
  709. fixup_pack_header_footer(pack_data->pack_fd, pack_data->hash,
  710. pack_data->pack_name, object_count,
  711. cur_pack_oid.hash, pack_size);
  712. if (object_count <= unpack_limit) {
  713. if (!loosen_small_pack(pack_data)) {
  714. invalidate_pack_id(pack_id);
  715. goto discard_pack;
  716. }
  717. }
  718. close(pack_data->pack_fd);
  719. idx_name = keep_pack(create_index());
  720. /* Register the packfile with core git's machinery. */
  721. new_p = add_packed_git(idx_name, strlen(idx_name), 1);
  722. if (!new_p)
  723. die("core git rejected index %s", idx_name);
  724. all_packs[pack_id] = new_p;
  725. install_packed_git(the_repository, new_p);
  726. free(idx_name);
  727. /* Print the boundary */
  728. if (pack_edges) {
  729. fprintf(pack_edges, "%s:", new_p->pack_name);
  730. for (i = 0; i < branch_table_sz; i++) {
  731. for (b = branch_table[i]; b; b = b->table_next_branch) {
  732. if (b->pack_id == pack_id)
  733. fprintf(pack_edges, " %s",
  734. oid_to_hex(&b->oid));
  735. }
  736. }
  737. for (t = first_tag; t; t = t->next_tag) {
  738. if (t->pack_id == pack_id)
  739. fprintf(pack_edges, " %s",
  740. oid_to_hex(&t->oid));
  741. }
  742. fputc('\n', pack_edges);
  743. fflush(pack_edges);
  744. }
  745. pack_id++;
  746. }
  747. else {
  748. discard_pack:
  749. close(pack_data->pack_fd);
  750. unlink_or_warn(pack_data->pack_name);
  751. }
  752. FREE_AND_NULL(pack_data);
  753. running = 0;
  754. /* We can't carry a delta across packfiles. */
  755. strbuf_release(&last_blob.data);
  756. last_blob.offset = 0;
  757. last_blob.depth = 0;
  758. }
  759. static void cycle_packfile(void)
  760. {
  761. end_packfile();
  762. start_packfile();
  763. }
  764. static int store_object(
  765. enum object_type type,
  766. struct strbuf *dat,
  767. struct last_object *last,
  768. struct object_id *oidout,
  769. uintmax_t mark)
  770. {
  771. void *out, *delta;
  772. struct object_entry *e;
  773. unsigned char hdr[96];
  774. struct object_id oid;
  775. unsigned long hdrlen, deltalen;
  776. git_hash_ctx c;
  777. git_zstream s;
  778. hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu",
  779. type_name(type), (unsigned long)dat->len) + 1;
  780. the_hash_algo->init_fn(&c);
  781. the_hash_algo->update_fn(&c, hdr, hdrlen);
  782. the_hash_algo->update_fn(&c, dat->buf, dat->len);
  783. the_hash_algo->final_fn(oid.hash, &c);
  784. if (oidout)
  785. oidcpy(oidout, &oid);
  786. e = insert_object(&oid);
  787. if (mark)
  788. insert_mark(mark, e);
  789. if (e->idx.offset) {
  790. duplicate_count_by_type[type]++;
  791. return 1;
  792. } else if (find_sha1_pack(oid.hash,
  793. get_all_packs(the_repository))) {
  794. e->type = type;
  795. e->pack_id = MAX_PACK_ID;
  796. e->idx.offset = 1; /* just not zero! */
  797. duplicate_count_by_type[type]++;
  798. return 1;
  799. }
  800. if (last && last->data.len && last->data.buf && last->depth < max_depth
  801. && dat->len > the_hash_algo->rawsz) {
  802. delta_count_attempts_by_type[type]++;
  803. delta = diff_delta(last->data.buf, last->data.len,
  804. dat->buf, dat->len,
  805. &deltalen, dat->len - the_hash_algo->rawsz);
  806. } else
  807. delta = NULL;
  808. git_deflate_init(&s, pack_compression_level);
  809. if (delta) {
  810. s.next_in = delta;
  811. s.avail_in = deltalen;
  812. } else {
  813. s.next_in = (void *)dat->buf;
  814. s.avail_in = dat->len;
  815. }
  816. s.avail_out = git_deflate_bound(&s, s.avail_in);
  817. s.next_out = out = xmalloc(s.avail_out);
  818. while (git_deflate(&s, Z_FINISH) == Z_OK)
  819. ; /* nothing */
  820. git_deflate_end(&s);
  821. /* Determine if we should auto-checkpoint. */
  822. if ((max_packsize
  823. && (pack_size + PACK_SIZE_THRESHOLD + s.total_out) > max_packsize)
  824. || (pack_size + PACK_SIZE_THRESHOLD + s.total_out) < pack_size) {
  825. /* This new object needs to *not* have the current pack_id. */
  826. e->pack_id = pack_id + 1;
  827. cycle_packfile();
  828. /* We cannot carry a delta into the new pack. */
  829. if (delta) {
  830. FREE_AND_NULL(delta);
  831. git_deflate_init(&s, pack_compression_level);
  832. s.next_in = (void *)dat->buf;
  833. s.avail_in = dat->len;
  834. s.avail_out = git_deflate_bound(&s, s.avail_in);
  835. s.next_out = out = xrealloc(out, s.avail_out);
  836. while (git_deflate(&s, Z_FINISH) == Z_OK)
  837. ; /* nothing */
  838. git_deflate_end(&s);
  839. }
  840. }
  841. e->type = type;
  842. e->pack_id = pack_id;
  843. e->idx.offset = pack_size;
  844. object_count++;
  845. object_count_by_type[type]++;
  846. crc32_begin(pack_file);
  847. if (delta) {
  848. off_t ofs = e->idx.offset - last->offset;
  849. unsigned pos = sizeof(hdr) - 1;
  850. delta_count_by_type[type]++;
  851. e->depth = last->depth + 1;
  852. hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr),
  853. OBJ_OFS_DELTA, deltalen);
  854. hashwrite(pack_file, hdr, hdrlen);
  855. pack_size += hdrlen;
  856. hdr[pos] = ofs & 127;
  857. while (ofs >>= 7)
  858. hdr[--pos] = 128 | (--ofs & 127);
  859. hashwrite(pack_file, hdr + pos, sizeof(hdr) - pos);
  860. pack_size += sizeof(hdr) - pos;
  861. } else {
  862. e->depth = 0;
  863. hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr),
  864. type, dat->len);
  865. hashwrite(pack_file, hdr, hdrlen);
  866. pack_size += hdrlen;
  867. }
  868. hashwrite(pack_file, out, s.total_out);
  869. pack_size += s.total_out;
  870. e->idx.crc32 = crc32_end(pack_file);
  871. free(out);
  872. free(delta);
  873. if (last) {
  874. if (last->no_swap) {
  875. last->data = *dat;
  876. } else {
  877. strbuf_swap(&last->data, dat);
  878. }
  879. last->offset = e->idx.offset;
  880. last->depth = e->depth;
  881. }
  882. return 0;
  883. }
  884. static void truncate_pack(struct hashfile_checkpoint *checkpoint)
  885. {
  886. if (hashfile_truncate(pack_file, checkpoint))
  887. die_errno("cannot truncate pack to skip duplicate");
  888. pack_size = checkpoint->offset;
  889. }
  890. static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
  891. {
  892. size_t in_sz = 64 * 1024, out_sz = 64 * 1024;
  893. unsigned char *in_buf = xmalloc(in_sz);
  894. unsigned char *out_buf = xmalloc(out_sz);
  895. struct object_entry *e;
  896. struct object_id oid;
  897. unsigned long hdrlen;
  898. off_t offset;
  899. git_hash_ctx c;
  900. git_zstream s;
  901. struct hashfile_checkpoint checkpoint;
  902. int status = Z_OK;
  903. /* Determine if we should auto-checkpoint. */
  904. if ((max_packsize
  905. && (pack_size + PACK_SIZE_THRESHOLD + len) > max_packsize)
  906. || (pack_size + PACK_SIZE_THRESHOLD + len) < pack_size)
  907. cycle_packfile();
  908. hashfile_checkpoint(pack_file, &checkpoint);
  909. offset = checkpoint.offset;
  910. hdrlen = xsnprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1;
  911. the_hash_algo->init_fn(&c);
  912. the_hash_algo->update_fn(&c, out_buf, hdrlen);
  913. crc32_begin(pack_file);
  914. git_deflate_init(&s, pack_compression_level);
  915. hdrlen = encode_in_pack_object_header(out_buf, out_sz, OBJ_BLOB, len);
  916. s.next_out = out_buf + hdrlen;
  917. s.avail_out = out_sz - hdrlen;
  918. while (status != Z_STREAM_END) {
  919. if (0 < len && !s.avail_in) {
  920. size_t cnt = in_sz < len ? in_sz : (size_t)len;
  921. size_t n = fread(in_buf, 1, cnt, stdin);
  922. if (!n && feof(stdin))
  923. die("EOF in data (%" PRIuMAX " bytes remaining)", len);
  924. the_hash_algo->update_fn(&c, in_buf, n);
  925. s.next_in = in_buf;
  926. s.avail_in = n;
  927. len -= n;
  928. }
  929. status = git_deflate(&s, len ? 0 : Z_FINISH);
  930. if (!s.avail_out || status == Z_STREAM_END) {
  931. size_t n = s.next_out - out_buf;
  932. hashwrite(pack_file, out_buf, n);
  933. pack_size += n;
  934. s.next_out = out_buf;
  935. s.avail_out = out_sz;
  936. }
  937. switch (status) {
  938. case Z_OK:
  939. case Z_BUF_ERROR:
  940. case Z_STREAM_END:
  941. continue;
  942. default:
  943. die("unexpected deflate failure: %d", status);
  944. }
  945. }
  946. git_deflate_end(&s);
  947. the_hash_algo->final_fn(oid.hash, &c);
  948. if (oidout)
  949. oidcpy(oidout, &oid);
  950. e = insert_object(&oid);
  951. if (mark)
  952. insert_mark(mark, e);
  953. if (e->idx.offset) {
  954. duplicate_count_by_type[OBJ_BLOB]++;
  955. truncate_pack(&checkpoint);
  956. } else if (find_sha1_pack(oid.hash,
  957. get_all_packs(the_repository))) {
  958. e->type = OBJ_BLOB;
  959. e->pack_id = MAX_PACK_ID;
  960. e->idx.offset = 1; /* just not zero! */
  961. duplicate_count_by_type[OBJ_BLOB]++;
  962. truncate_pack(&checkpoint);
  963. } else {
  964. e->depth = 0;
  965. e->type = OBJ_BLOB;
  966. e->pack_id = pack_id;
  967. e->idx.offset = offset;
  968. e->idx.crc32 = crc32_end(pack_file);
  969. object_count++;
  970. object_count_by_type[OBJ_BLOB]++;
  971. }
  972. free(in_buf);
  973. free(out_buf);
  974. }
  975. /* All calls must be guarded by find_object() or find_mark() to
  976. * ensure the 'struct object_entry' passed was written by this
  977. * process instance. We unpack the entry by the offset, avoiding
  978. * the need for the corresponding .idx file. This unpacking rule
  979. * works because we only use OBJ_REF_DELTA within the packfiles
  980. * created by fast-import.
  981. *
  982. * oe must not be NULL. Such an oe usually comes from giving
  983. * an unknown SHA-1 to find_object() or an undefined mark to
  984. * find_mark(). Callers must test for this condition and use
  985. * the standard read_sha1_file() when it happens.
  986. *
  987. * oe->pack_id must not be MAX_PACK_ID. Such an oe is usually from
  988. * find_mark(), where the mark was reloaded from an existing marks
  989. * file and is referencing an object that this fast-import process
  990. * instance did not write out to a packfile. Callers must test for
  991. * this condition and use read_sha1_file() instead.
  992. */
  993. static void *gfi_unpack_entry(
  994. struct object_entry *oe,
  995. unsigned long *sizep)
  996. {
  997. enum object_type type;
  998. struct packed_git *p = all_packs[oe->pack_id];
  999. if (p == pack_data && p->pack_size < (pack_size + the_hash_algo->rawsz)) {
  1000. /* The object is stored in the packfile we are writing to
  1001. * and we have modified it since the last time we scanned
  1002. * back to read a previously written object. If an old
  1003. * window covered [p->pack_size, p->pack_size + rawsz) its
  1004. * data is stale and is not valid. Closing all windows
  1005. * and updating the packfile length ensures we can read
  1006. * the newly written data.
  1007. */
  1008. close_pack_windows(p);
  1009. hashflush(pack_file);
  1010. /* We have to offer rawsz bytes additional on the end of
  1011. * the packfile as the core unpacker code assumes the
  1012. * footer is present at the file end and must promise
  1013. * at least rawsz bytes within any window it maps. But
  1014. * we don't actually create the footer here.
  1015. */
  1016. p->pack_size = pack_size + the_hash_algo->rawsz;
  1017. }
  1018. return unpack_entry(the_repository, p, oe->idx.offset, &type, sizep);
  1019. }
  1020. static const char *get_mode(const char *str, uint16_t *modep)
  1021. {
  1022. unsigned char c;
  1023. uint16_t mode = 0;
  1024. while ((c = *str++) != ' ') {
  1025. if (c < '0' || c > '7')
  1026. return NULL;
  1027. mode = (mode << 3) + (c - '0');
  1028. }
  1029. *modep = mode;
  1030. return str;
  1031. }
  1032. static void load_tree(struct tree_entry *root)
  1033. {
  1034. struct object_id *oid = &root->versions[1].oid;
  1035. struct object_entry *myoe;
  1036. struct tree_content *t;
  1037. unsigned long size;
  1038. char *buf;
  1039. const char *c;
  1040. root->tree = t = new_tree_content(8);
  1041. if (is_null_oid(oid))
  1042. return;
  1043. myoe = find_object(oid);
  1044. if (myoe && myoe->pack_id != MAX_PACK_ID) {
  1045. if (myoe->type != OBJ_TREE)
  1046. die("Not a tree: %s", oid_to_hex(oid));
  1047. t->delta_depth = myoe->depth;
  1048. buf = gfi_unpack_entry(myoe, &size);
  1049. if (!buf)
  1050. die("Can't load tree %s", oid_to_hex(oid));
  1051. } else {
  1052. enum object_type type;
  1053. buf = read_object_file(oid, &type, &size);
  1054. if (!buf || type != OBJ_TREE)
  1055. die("Can't load tree %s", oid_to_hex(oid));
  1056. }
  1057. c = buf;
  1058. while (c != (buf + size)) {
  1059. struct tree_entry *e = new_tree_entry();
  1060. if (t->entry_count == t->entry_capacity)
  1061. root->tree = t = grow_tree_content(t, t->entry_count);
  1062. t->entries[t->entry_count++] = e;
  1063. e->tree = NULL;
  1064. c = get_mode(c, &e->versions[1].mode);
  1065. if (!c)
  1066. die("Corrupt mode in %s", oid_to_hex(oid));
  1067. e->versions[0].mode = e->versions[1].mode;
  1068. e->name = to_atom(c, strlen(c));
  1069. c += e->name->str_len + 1;
  1070. hashcpy(e->versions[0].oid.hash, (unsigned char *)c);
  1071. hashcpy(e->versions[1].oid.hash, (unsigned char *)c);
  1072. c += the_hash_algo->rawsz;
  1073. }
  1074. free(buf);
  1075. }
  1076. static int tecmp0 (const void *_a, const void *_b)
  1077. {
  1078. struct tree_entry *a = *((struct tree_entry**)_a);
  1079. struct tree_entry *b = *((struct tree_entry**)_b);
  1080. return base_name_compare(
  1081. a->name->str_dat, a->name->str_len, a->versions[0].mode,
  1082. b->name->str_dat, b->name->str_len, b->versions[0].mode);
  1083. }
  1084. static int tecmp1 (const void *_a, const void *_b)
  1085. {
  1086. struct tree_entry *a = *((struct tree_entry**)_a);
  1087. struct tree_entry *b = *((struct tree_entry**)_b);
  1088. return base_name_compare(
  1089. a->name->str_dat, a->name->str_len, a->versions[1].mode,
  1090. b->name->str_dat, b->name->str_len, b->versions[1].mode);
  1091. }
  1092. static void mktree(struct tree_content *t, int v, struct strbuf *b)
  1093. {
  1094. size_t maxlen = 0;
  1095. unsigned int i;
  1096. if (!v)
  1097. QSORT(t->entries, t->entry_count, tecmp0);
  1098. else
  1099. QSORT(t->entries, t->entry_count, tecmp1);
  1100. for (i = 0; i < t->entry_count; i++) {
  1101. if (t->entries[i]->versions[v].mode)
  1102. maxlen += t->entries[i]->name->str_len + 34;
  1103. }
  1104. strbuf_reset(b);
  1105. strbuf_grow(b, maxlen);
  1106. for (i = 0; i < t->entry_count; i++) {
  1107. struct tree_entry *e = t->entries[i];
  1108. if (!e->versions[v].mode)
  1109. continue;
  1110. strbuf_addf(b, "%o %s%c",
  1111. (unsigned int)(e->versions[v].mode & ~NO_DELTA),
  1112. e->name->str_dat, '\0');
  1113. strbuf_add(b, e->versions[v].oid.hash, the_hash_algo->rawsz);
  1114. }
  1115. }
  1116. static void store_tree(struct tree_entry *root)
  1117. {
  1118. struct tree_content *t;
  1119. unsigned int i, j, del;
  1120. struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
  1121. struct object_entry *le = NULL;
  1122. if (!is_null_oid(&root->versions[1].oid))
  1123. return;
  1124. if (!root->tree)
  1125. load_tree(root);
  1126. t = root->tree;
  1127. for (i = 0; i < t->entry_count; i++) {
  1128. if (t->entries[i]->tree)
  1129. store_tree(t->entries[i]);
  1130. }
  1131. if (!(root->versions[0].mode & NO_DELTA))
  1132. le = find_object(&root->versions[0].oid);
  1133. if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) {
  1134. mktree(t, 0, &old_tree);
  1135. lo.data = old_tree;
  1136. lo.offset = le->idx.offset;
  1137. lo.depth = t->delta_depth;
  1138. }
  1139. mktree(t, 1, &new_tree);
  1140. store_object(OBJ_TREE, &new_tree, &lo, &root->versions[1].oid, 0);
  1141. t->delta_depth = lo.depth;
  1142. for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
  1143. struct tree_entry *e = t->entries[i];
  1144. if (e->versions[1].mode) {
  1145. e->versions[0].mode = e->versions[1].mode;
  1146. oidcpy(&e->versions[0].oid, &e->versions[1].oid);
  1147. t->entries[j++] = e;
  1148. } else {
  1149. release_tree_entry(e);
  1150. del++;
  1151. }
  1152. }
  1153. t->entry_count -= del;
  1154. }
  1155. static void tree_content_replace(
  1156. struct tree_entry *root,
  1157. const struct object_id *oid,
  1158. const uint16_t mode,
  1159. struct tree_content *newtree)
  1160. {
  1161. if (!S_ISDIR(mode))
  1162. die("Root cannot be a non-directory");
  1163. oidclr(&root->versions[0].oid);
  1164. oidcpy(&root->versions[1].oid, oid);
  1165. if (root->tree)
  1166. release_tree_content_recursive(root->tree);
  1167. root->tree = newtree;
  1168. }
  1169. static int tree_content_set(
  1170. struct tree_entry *root,
  1171. const char *p,
  1172. const struct object_id *oid,
  1173. const uint16_t mode,
  1174. struct tree_content *subtree)
  1175. {
  1176. struct tree_content *t;
  1177. const char *slash1;
  1178. unsigned int i, n;
  1179. struct tree_entry *e;
  1180. slash1 = strchrnul(p, '/');
  1181. n = slash1 - p;
  1182. if (!n)
  1183. die("Empty path component found in input");
  1184. if (!*slash1 && !S_ISDIR(mode) && subtree)
  1185. die("Non-directories cannot have subtrees");
  1186. if (!root->tree)
  1187. load_tree(root);
  1188. t = root->tree;
  1189. for (i = 0; i < t->entry_count; i++) {
  1190. e = t->entries[i];
  1191. if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
  1192. if (!*slash1) {
  1193. if (!S_ISDIR(mode)
  1194. && e->versions[1].mode == mode
  1195. && oideq(&e->versions[1].oid, oid))
  1196. return 0;
  1197. e->versions[1].mode = mode;
  1198. oidcpy(&e->versions[1].oid, oid);
  1199. if (e->tree)
  1200. release_tree_content_recursive(e->tree);
  1201. e->tree = subtree;
  1202. /*
  1203. * We need to leave e->versions[0].sha1 alone
  1204. * to avoid modifying the preimage tree used
  1205. * when writing out the parent directory.
  1206. * But after replacing the subdir with a
  1207. * completely different one, it's not a good
  1208. * delta base any more, and besides, we've
  1209. * thrown away the tree entries needed to
  1210. * make a delta against it.
  1211. *
  1212. * So let's just explicitly disable deltas
  1213. * for the subtree.
  1214. */
  1215. if (S_ISDIR(e->versions[0].mode))
  1216. e->versions[0].mode |= NO_DELTA;
  1217. oidclr(&root->versions[1].oid);
  1218. return 1;
  1219. }
  1220. if (!S_ISDIR(e->versions[1].mode)) {
  1221. e->tree = new_tree_content(8);
  1222. e->versions[1].mode = S_IFDIR;
  1223. }
  1224. if (!e->tree)
  1225. load_tree(e);
  1226. if (tree_content_set(e, slash1 + 1, oid, mode, subtree)) {
  1227. oidclr(&root->versions[1].oid);
  1228. return 1;
  1229. }
  1230. return 0;
  1231. }
  1232. }
  1233. if (t->entry_count == t->entry_capacity)
  1234. root->tree = t = grow_tree_content(t, t->entry_count);
  1235. e = new_tree_entry();
  1236. e->name = to_atom(p, n);
  1237. e->versions[0].mode = 0;
  1238. oidclr(&e->versions[0].oid);
  1239. t->entries[t->entry_count++] = e;
  1240. if (*slash1) {
  1241. e->tree = new_tree_content(8);
  1242. e->versions[1].mode = S_IFDIR;
  1243. tree_content_set(e, slash1 + 1, oid, mode, subtree);
  1244. } else {
  1245. e->tree = subtree;
  1246. e->versions[1].mode = mode;
  1247. oidcpy(&e->versions[1].oid, oid);
  1248. }
  1249. oidclr(&root->versions[1].oid);
  1250. return 1;
  1251. }
  1252. static int tree_content_remove(
  1253. struct tree_entry *root,
  1254. const char *p,
  1255. struct tree_entry *backup_leaf,
  1256. int allow_root)
  1257. {
  1258. struct tree_content *t;
  1259. const char *slash1;
  1260. unsigned int i, n;
  1261. struct tree_entry *e;
  1262. slash1 = strchrnul(p, '/');
  1263. n = slash1 - p;
  1264. if (!root->tree)
  1265. load_tree(root);
  1266. if (!*p && allow_root) {
  1267. e = root;
  1268. goto del_entry;
  1269. }
  1270. t = root->tree;
  1271. for (i = 0; i < t->entry_count; i++) {
  1272. e = t->entries[i];
  1273. if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
  1274. if (*slash1 && !S_ISDIR(e->versions[1].mode))
  1275. /*
  1276. * If p names a file in some subdirectory, and a
  1277. * file or symlink matching the name of the
  1278. * parent directory of p exists, then p cannot
  1279. * exist and need not be deleted.
  1280. */
  1281. return 1;
  1282. if (!*slash1 || !S_ISDIR(e->versions[1].mode))
  1283. goto del_entry;
  1284. if (!e->tree)
  1285. load_tree(e);
  1286. if (tree_content_remove(e, slash1 + 1, backup_leaf, 0)) {
  1287. for (n = 0; n < e->tree->entry_count; n++) {
  1288. if (e->tree->entries[n]->versions[1].mode) {
  1289. oidclr(&root->versions[1].oid);
  1290. return 1;
  1291. }
  1292. }
  1293. backup_leaf = NULL;
  1294. goto del_entry;
  1295. }
  1296. return 0;
  1297. }
  1298. }
  1299. return 0;
  1300. del_entry:
  1301. if (backup_leaf)
  1302. memcpy(backup_leaf, e, sizeof(*backup_leaf));
  1303. else if (e->tree)
  1304. release_tree_content_recursive(e->tree);
  1305. e->tree = NULL;
  1306. e->versions[1].mode = 0;
  1307. oidclr(&e->versions[1].oid);
  1308. oidclr(&root->versions[1].oid);
  1309. return 1;
  1310. }
  1311. static int tree_content_get(
  1312. struct tree_entry *root,
  1313. const char *p,
  1314. struct tree_entry *leaf,
  1315. int allow_root)
  1316. {
  1317. struct tree_content *t;
  1318. const char *slash1;
  1319. unsigned int i, n;
  1320. struct tree_entry *e;
  1321. slash1 = strchrnul(p, '/');
  1322. n = slash1 - p;
  1323. if (!n && !allow_root)
  1324. die("Empty path component found in input");
  1325. if (!root->tree)
  1326. load_tree(root);
  1327. if (!n) {
  1328. e = root;
  1329. goto found_entry;
  1330. }
  1331. t = root->tree;
  1332. for (i = 0; i < t->entry_count; i++) {
  1333. e = t->entries[i];
  1334. if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
  1335. if (!*slash1)
  1336. goto found_entry;
  1337. if (!S_ISDIR(e->versions[1].mode))
  1338. return 0;
  1339. if (!e->tree)
  1340. load_tree(e);
  1341. return tree_content_get(e, slash1 + 1, leaf, 0);
  1342. }
  1343. }
  1344. return 0;
  1345. found_entry:
  1346. memcpy(leaf, e, sizeof(*leaf));
  1347. if (e->tree && is_null_oid(&e->versions[1].oid))
  1348. leaf->tree = dup_tree_content(e->tree);
  1349. else
  1350. leaf->tree = NULL;
  1351. return 1;
  1352. }
  1353. static int update_branch(struct branch *b)
  1354. {
  1355. static const char *msg = "fast-import";
  1356. struct ref_transaction *transaction;
  1357. struct object_id old_oid;
  1358. struct strbuf err = STRBUF_INIT;
  1359. if (is_null_oid(&b->oid)) {
  1360. if (b->delete)
  1361. delete_ref(NULL, b->name, NULL, 0);
  1362. return 0;
  1363. }
  1364. if (read_ref(b->name, &old_oid))
  1365. oidclr(&old_oid);
  1366. if (!force_update && !is_null_oid(&old_oid)) {
  1367. struct commit *old_cmit, *new_cmit;
  1368. old_cmit = lookup_commit_reference_gently(the_repository,
  1369. &old_oid, 0);
  1370. new_cmit = lookup_commit_reference_gently(the_repository,
  1371. &b->oid, 0);
  1372. if (!old_cmit || !new_cmit)
  1373. return error("Branch %s is missing commits.", b->name);
  1374. if (!in_merge_bases(old_cmit, new_cmit)) {
  1375. warning("Not updating %s"
  1376. " (new tip %s does not contain %s)",
  1377. b->name, oid_to_hex(&b->oid),
  1378. oid_to_hex(&old_oid));
  1379. return -1;
  1380. }
  1381. }
  1382. transaction = ref_transaction_begin(&err);
  1383. if (!transaction ||
  1384. ref_transaction_update(transaction, b->name, &b->oid, &old_oid,
  1385. 0, msg, &err) ||
  1386. ref_transaction_commit(transaction, &err)) {
  1387. ref_transaction_free(transaction);
  1388. error("%s", err.buf);
  1389. strbuf_release(&err);
  1390. return -1;
  1391. }
  1392. ref_transaction_free(transaction);
  1393. strbuf_release(&err);
  1394. return 0;
  1395. }
  1396. static void dump_branches(void)
  1397. {
  1398. unsigned int i;
  1399. struct branch *b;
  1400. for (i = 0; i < branch_table_sz; i++) {
  1401. for (b = branch_table[i]; b; b = b->table_next_branch)
  1402. failure |= update_branch(b);
  1403. }
  1404. }
  1405. static void dump_tags(void)
  1406. {
  1407. static const char *msg = "fast-import";
  1408. struct tag *t;
  1409. struct strbuf ref_name = STRBUF_INIT;
  1410. struct strbuf err = STRBUF_INIT;
  1411. struct ref_transaction *transaction;
  1412. transaction = ref_transaction_begin(&err);
  1413. if (!transaction) {
  1414. failure |= error("%s", err.buf);
  1415. goto cleanup;
  1416. }
  1417. for (t = first_tag; t; t = t->next_tag) {
  1418. strbuf_reset(&ref_name);
  1419. strbuf_addf(&ref_name, "refs/tags/%s", t->name);
  1420. if (ref_transaction_update(transaction, ref_name.buf,
  1421. &t->oid, NULL, 0, msg, &err)) {
  1422. failure |= error("%s", err.buf);
  1423. goto cleanup;
  1424. }
  1425. }
  1426. if (ref_transaction_commit(transaction, &err))
  1427. failure |= error("%s", err.buf);
  1428. cleanup:
  1429. ref_transaction_free(transaction);
  1430. strbuf_release(&ref_name);
  1431. strbuf_release(&err);
  1432. }
  1433. static void dump_marks_helper(FILE *f,
  1434. uintmax_t base,
  1435. struct mark_set *m)
  1436. {
  1437. uintmax_t k;
  1438. if (m->shift) {
  1439. for (k = 0; k < 1024; k++) {
  1440. if (m->data.sets[k])
  1441. dump_marks_helper(f, base + (k << m->shift),
  1442. m->data.sets[k]);
  1443. }
  1444. } else {
  1445. for (k = 0; k < 1024; k++) {
  1446. if (m->data.marked[k])
  1447. fprintf(f, ":%" PRIuMAX " %s\n", base + k,
  1448. oid_to_hex(&m->data.marked[k]->idx.oid));
  1449. }
  1450. }
  1451. }
  1452. static void dump_marks(void)
  1453. {
  1454. struct lock_file mark_lock = LOCK_INIT;
  1455. FILE *f;
  1456. if (!export_marks_file || (import_marks_file && !import_marks_file_done))
  1457. return;
  1458. if (hold_lock_file_for_update(&mark_lock, export_marks_file, 0) < 0) {
  1459. failure |= error_errno("Unable to write marks file %s",
  1460. export_marks_file);
  1461. return;
  1462. }
  1463. f = fdopen_lock_file(&mark_lock, "w");
  1464. if (!f) {
  1465. int saved_errno = errno;
  1466. rollback_lock_file(&mark_lock);
  1467. failure |= error("Unable to write marks file %s: %s",
  1468. export_marks_file, strerror(saved_errno));
  1469. return;
  1470. }
  1471. dump_marks_helper(f, 0, marks);
  1472. if (commit_lock_file(&mark_lock)) {
  1473. failure |= error_errno("Unable to write file %s",
  1474. export_marks_file);
  1475. return;
  1476. }
  1477. }
  1478. static void read_marks(void)
  1479. {
  1480. char line[512];
  1481. FILE *f = fopen(import_marks_file, "r");
  1482. if (f)
  1483. ;
  1484. else if (import_marks_file_ignore_missing && errno == ENOENT)
  1485. goto done; /* Marks file does not exist */
  1486. else
  1487. die_errno("cannot read '%s'", import_marks_file);
  1488. while (fgets(line, sizeof(line), f)) {
  1489. uintmax_t mark;
  1490. char *end;
  1491. struct object_id oid;
  1492. struct object_entry *e;
  1493. end = strchr(line, '\n');
  1494. if (line[0] != ':' || !end)
  1495. die("corrupt mark line: %s", line);
  1496. *end = 0;
  1497. mark = strtoumax(line + 1, &end, 10);
  1498. if (!mark || end == line + 1
  1499. || *end != ' ' || get_oid_hex(end + 1, &oid))
  1500. die("corrupt mark line: %s", line);
  1501. e = find_object(&oid);
  1502. if (!e) {
  1503. enum object_type type = oid_object_info(the_repository,
  1504. &oid, NULL);
  1505. if (type < 0)
  1506. die("object not found: %s", oid_to_hex(&oid));
  1507. e = insert_object(&oid);
  1508. e->type = type;
  1509. e->pack_id = MAX_PACK_ID;
  1510. e->idx.offset = 1; /* just not zero! */
  1511. }
  1512. insert_mark(mark, e);
  1513. }
  1514. fclose(f);
  1515. done:
  1516. import_marks_file_done = 1;
  1517. }
  1518. static int read_next_command(void)
  1519. {
  1520. static int stdin_eof = 0;
  1521. if (stdin_eof) {
  1522. unread_command_buf = 0;
  1523. return EOF;
  1524. }
  1525. for (;;) {
  1526. if (unread_command_buf) {
  1527. unread_command_buf = 0;
  1528. } else {
  1529. struct recent_command *rc;
  1530. stdin_eof = strbuf_getline_lf(&command_buf, stdin);
  1531. if (stdin_eof)
  1532. return EOF;
  1533. if (!seen_data_command
  1534. && !starts_with(command_buf.buf, "feature ")
  1535. && !starts_with(command_buf.buf, "option ")) {
  1536. parse_argv();
  1537. }
  1538. rc = rc_free;
  1539. if (rc)
  1540. rc_free = rc->next;
  1541. else {
  1542. rc = cmd_hist.next;
  1543. cmd_hist.next = rc->next;
  1544. cmd_hist.next->prev = &cmd_hist;
  1545. free(rc->buf);
  1546. }
  1547. rc->buf = xstrdup(command_buf.buf);
  1548. rc->prev = cmd_tail;
  1549. rc->next = cmd_hist.prev;
  1550. rc->prev->next = rc;
  1551. cmd_tail = rc;
  1552. }
  1553. if (command_buf.buf[0] == '#')
  1554. continue;
  1555. return 0;
  1556. }
  1557. }
  1558. static void skip_optional_lf(void)
  1559. {
  1560. int term_char = fgetc(stdin);
  1561. if (term_char != '\n' && term_char != EOF)
  1562. ungetc(term_char, stdin);
  1563. }
  1564. static void parse_mark(void)
  1565. {
  1566. const char *v;
  1567. if (skip_prefix(command_buf.buf, "mark :", &v)) {
  1568. next_mark = strtoumax(v, NULL, 10);
  1569. read_next_command();
  1570. }
  1571. else
  1572. next_mark = 0;
  1573. }
  1574. static void parse_original_identifier(void)
  1575. {
  1576. const char *v;
  1577. if (skip_prefix(command_buf.buf, "original-oid ", &v))
  1578. read_next_command();
  1579. }
  1580. static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
  1581. {
  1582. const char *data;
  1583. strbuf_reset(sb);
  1584. if (!skip_prefix(command_buf.buf, "data ", &data))
  1585. die("Expected 'data n' command, found: %s", command_buf.buf);
  1586. if (skip_prefix(data, "<<", &data)) {
  1587. char *term = xstrdup(data);
  1588. size_t term_len = command_buf.len - (data - command_buf.buf);
  1589. for (;;) {
  1590. if (strbuf_getline_lf(&command_buf, stdin) == EOF)
  1591. die("EOF in data (terminator '%s' not found)", term);
  1592. if (term_len == command_buf.len
  1593. && !strcmp(term, command_buf.buf))
  1594. break;
  1595. strbuf_addbuf(sb, &command_buf);
  1596. strbuf_addch(sb, '\n');
  1597. }
  1598. free(term);
  1599. }
  1600. else {
  1601. uintmax_t len = strtoumax(data, NULL, 10);
  1602. size_t n = 0, length = (size_t)len;
  1603. if (limit && limit < len) {
  1604. *len_res = len;
  1605. return 0;
  1606. }
  1607. if (length < len)
  1608. die("data is too large to use in this context");
  1609. while (n < length) {
  1610. size_t s = strbuf_fread(sb, length - n, stdin);
  1611. if (!s && feof(stdin))
  1612. die("EOF in data (%lu bytes remaining)",
  1613. (unsigned long)(length - n));
  1614. n += s;
  1615. }
  1616. }
  1617. skip_optional_lf();
  1618. return 1;
  1619. }
  1620. static int validate_raw_date(const char *src, struct strbuf *result)
  1621. {
  1622. const char *orig_src = src;
  1623. char *endp;
  1624. unsigned long num;
  1625. errno = 0;
  1626. num = strtoul(src, &endp, 10);
  1627. /* NEEDSWORK: perhaps check for reasonable values? */
  1628. if (errno || endp == src || *endp != ' ')
  1629. return -1;
  1630. src = endp + 1;
  1631. if (*src != '-' && *src != '+')
  1632. return -1;
  1633. num = strtoul(src + 1, &endp, 10);
  1634. if (errno || endp == src + 1 || *endp || 1400 < num)
  1635. return -1;
  1636. strbuf_addstr(result, orig_src);
  1637. return 0;
  1638. }
  1639. static char *parse_ident(const char *buf)
  1640. {
  1641. const char *ltgt;
  1642. size_t name_len;
  1643. struct strbuf ident = STRBUF_INIT;
  1644. /* ensure there is a space delimiter even if there is no name */
  1645. if (*buf == '<')
  1646. --buf;
  1647. ltgt = buf + strcspn(buf, "<>");
  1648. if (*ltgt != '<')
  1649. die("Missing < in ident string: %s", buf);
  1650. if (ltgt != buf && ltgt[-1] != ' ')
  1651. die("Missing space before < in ident string: %s", buf);
  1652. ltgt = ltgt + 1 + strcspn(ltgt + 1, "<>");
  1653. if (*ltgt != '>')
  1654. die("Missing > in ident string: %s", buf);
  1655. ltgt++;
  1656. if (*ltgt != ' ')
  1657. die("Missing space after > in ident string: %s", buf);
  1658. ltgt++;
  1659. name_len = ltgt - buf;
  1660. strbuf_add(&ident, buf, name_len);
  1661. switch (whenspec) {
  1662. case WHENSPEC_RAW:
  1663. if (validate_raw_date(ltgt, &ident) < 0)
  1664. die("Invalid raw date \"%s\" in ident: %s", ltgt, buf);
  1665. break;
  1666. case WHENSPEC_RFC2822:
  1667. if (parse_date(ltgt, &ident) < 0)
  1668. die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt, buf);
  1669. break;
  1670. case WHENSPEC_NOW:
  1671. if (strcmp("now", ltgt))
  1672. die("Date in ident must be 'now': %s", buf);
  1673. datestamp(&ident);
  1674. break;
  1675. }
  1676. return strbuf_detach(&ident, NULL);
  1677. }
  1678. static void parse_and_store_blob(
  1679. struct last_object *last,
  1680. struct object_id *oidout,
  1681. uintmax_t mark)
  1682. {
  1683. static struct strbuf buf = STRBUF_INIT;
  1684. uintmax_t len;
  1685. if (parse_data(&buf, big_file_threshold, &len))
  1686. store_object(OBJ_BLOB, &buf, last, oidout, mark);
  1687. else {
  1688. if (last) {
  1689. strbuf_release(&last->data);
  1690. last->offset = 0;
  1691. last->depth = 0;
  1692. }
  1693. stream_blob(len, oidout, mark);
  1694. skip_optional_lf();
  1695. }
  1696. }
  1697. static void parse_new_blob(void)
  1698. {
  1699. read_next_command();
  1700. parse_mark();
  1701. parse_original_identifier();
  1702. parse_and_store_blob(&last_blob, NULL, next_mark);
  1703. }
  1704. static void unload_one_branch(void)
  1705. {
  1706. while (cur_active_branches
  1707. && cur_active_branches >= max_active_branches) {
  1708. uintmax_t min_commit = ULONG_MAX;
  1709. struct branch *e, *l = NULL, *p = NULL;
  1710. for (e = active_branches; e; e = e->active_next_branch) {
  1711. if (e->last_commit < min_commit) {
  1712. p = l;
  1713. min_commit = e->last_commit;
  1714. }
  1715. l = e;
  1716. }
  1717. if (p) {
  1718. e = p->active_next_branch;
  1719. p->active_next_branch = e->active_next_branch;
  1720. } else {
  1721. e = active_branches;
  1722. active_branches = e->active_next_branch;
  1723. }
  1724. e->active = 0;
  1725. e->active_next_branch = NULL;
  1726. if (e->branch_tree.tree) {
  1727. release_tree_content_recursive(e->branch_tree.tree);
  1728. e->branch_tree.tree = NULL;
  1729. }
  1730. cur_active_branches--;
  1731. }
  1732. }
  1733. static void load_branch(struct branch *b)
  1734. {
  1735. load_tree(&b->branch_tree);
  1736. if (!b->active) {
  1737. b->active = 1;
  1738. b->active_next_branch = active_branches;
  1739. active_branches = b;
  1740. cur_active_branches++;
  1741. branch_load_count++;
  1742. }
  1743. }
  1744. static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes)
  1745. {
  1746. unsigned char fanout = 0;
  1747. while ((num_notes >>= 8))
  1748. fanout++;
  1749. return fanout;
  1750. }
  1751. static void construct_path_with_fanout(const char *hex_sha1,
  1752. unsigned char fanout, char *path)
  1753. {
  1754. unsigned int i = 0, j = 0;
  1755. if (fanout >= the_hash_algo->rawsz)
  1756. die("Too large fanout (%u)", fanout);
  1757. while (fanout) {
  1758. path[i++] = hex_sha1[j++];
  1759. path[i++] = hex_sha1[j++];
  1760. path[i++] = '/';
  1761. fanout--;
  1762. }
  1763. memcpy(path + i, hex_sha1 + j, the_hash_algo->hexsz - j);
  1764. path[i + the_hash_algo->hexsz - j] = '\0';
  1765. }
  1766. static uintmax_t do_change_note_fanout(
  1767. struct tree_entry *orig_root, struct tree_entry *root,
  1768. char *hex_oid, unsigned int hex_oid_len,
  1769. char *fullpath, unsigned int fullpath_len,
  1770. unsigned char fanout)
  1771. {
  1772. struct tree_content *t;
  1773. struct tree_entry *e, leaf;
  1774. unsigned int i, tmp_hex_oid_len, tmp_fullpath_len;
  1775. uintmax_t num_notes = 0;
  1776. struct object_id oid;
  1777. /* hex oid + '/' between each pair of hex digits + NUL */
  1778. char realpath[GIT_MAX_HEXSZ + ((GIT_MAX_HEXSZ / 2) - 1) + 1];
  1779. const unsigned hexsz = the_hash_algo->hexsz;
  1780. if (!root->tree)
  1781. load_tree(root);
  1782. t = root->tree;
  1783. for (i = 0; t && i < t->entry_count; i++) {
  1784. e = t->entries[i];
  1785. tmp_hex_oid_len = hex_oid_len + e->name->str_len;
  1786. tmp_fullpath_len = fullpath_len;
  1787. /*
  1788. * We're interested in EITHER existing note entries (entries
  1789. * with exactly 40 hex chars in path, not including directory
  1790. * separators), OR directory entries that may contain note
  1791. * entries (with < 40 hex chars in path).
  1792. * Also, each path component in a note entry must be a multiple
  1793. * of 2 chars.
  1794. */
  1795. if (!e->versions[1].mode ||
  1796. tmp_hex_oid_len > hexsz ||
  1797. e->name->str_len % 2)
  1798. continue;
  1799. /* This _may_ be a note entry, or a subdir containing notes */
  1800. memcpy(hex_oid + hex_oid_len, e->name->str_dat,
  1801. e->name->str_len);
  1802. if (tmp_fullpath_len)
  1803. fullpath[tmp_fullpath_len++] = '/';
  1804. memcpy(fullpath + tmp_fullpath_len, e->name->str_dat,
  1805. e->name->str_len);
  1806. tmp_fullpath_len += e->name->str_len;
  1807. fullpath[tmp_fullpath_len] = '\0';
  1808. if (tmp_hex_oid_len == hexsz && !get_oid_hex(hex_oid, &oid)) {
  1809. /* This is a note entry */
  1810. if (fanout == 0xff) {
  1811. /* Counting mode, no rename */
  1812. num_notes++;
  1813. continue;
  1814. }
  1815. construct_path_with_fanout(hex_oid, fanout, realpath);
  1816. if (!strcmp(fullpath, realpath)) {
  1817. /* Note entry is in correct location */
  1818. num_notes++;
  1819. continue;
  1820. }
  1821. /* Rename fullpath to realpath */
  1822. if (!tree_content_remove(orig_root, fullpath, &leaf, 0))
  1823. die("Failed to remove path %s", fullpath);
  1824. tree_content_set(orig_root, realpath,
  1825. &leaf.versions[1].oid,
  1826. leaf.versions[1].mode,
  1827. leaf.tree);
  1828. } else if (S_ISDIR(e->versions[1].mode)) {
  1829. /* This is a subdir that may contain note entries */
  1830. num_notes += do_change_note_fanout(orig_root, e,
  1831. hex_oid, tmp_hex_oid_len,
  1832. fullpath, tmp_fullpath_len, fanout);
  1833. }
  1834. /* The above may have reallocated the current tree_content */
  1835. t = root->tree;
  1836. }
  1837. return num_notes;
  1838. }
  1839. static uintmax_t change_note_fanout(struct tree_entry *root,
  1840. unsigned char fanout)
  1841. {
  1842. /*
  1843. * The size of path is due to one slash between every two hex digits,
  1844. * plus the terminating NUL. Note that there is no slash at the end, so
  1845. * the number of slashes is one less than half the number of hex
  1846. * characters.
  1847. */
  1848. char hex_oid[GIT_MAX_HEXSZ], path[GIT_MAX_HEXSZ + (GIT_MAX_HEXSZ / 2) - 1 + 1];
  1849. return do_change_note_fanout(root, root, hex_oid, 0, path, 0, fanout);
  1850. }
  1851. /*
  1852. * Given a pointer into a string, parse a mark reference:
  1853. *
  1854. * idnum ::= ':' bigint;
  1855. *
  1856. * Return the first character after the value in *endptr.
  1857. *
  1858. * Complain if the following character is not what is expected,
  1859. * either a space or end of the string.
  1860. */
  1861. static uintmax_t parse_mark_ref(const char *p, char **endptr)
  1862. {
  1863. uintmax_t mark;
  1864. assert(*p == ':');
  1865. p++;
  1866. mark = strtoumax(p, endptr, 10);
  1867. if (*endptr == p)
  1868. die("No value after ':' in mark: %s", command_buf.buf);
  1869. return mark;
  1870. }
  1871. /*
  1872. * Parse the mark reference, and complain if this is not the end of
  1873. * the string.
  1874. */
  1875. static uintmax_t parse_mark_ref_eol(const char *p)
  1876. {
  1877. char *end;
  1878. uintmax_t mark;
  1879. mark = parse_mark_ref(p, &end);
  1880. if (*end != '\0')
  1881. die("Garbage after mark: %s", command_buf.buf);
  1882. return mark;
  1883. }
  1884. /*
  1885. * Parse the mark reference, demanding a trailing space. Return a
  1886. * pointer to the space.
  1887. */
  1888. static uintmax_t parse_mark_ref_space(const char **p)
  1889. {
  1890. uintmax_t mark;
  1891. char *end;
  1892. mark = parse_mark_ref(*p, &end);
  1893. if (*end++ != ' ')
  1894. die("Missing space after mark: %s", command_buf.buf);
  1895. *p = end;
  1896. return mark;
  1897. }
  1898. static void file_change_m(const char *p, struct branch *b)
  1899. {
  1900. static struct strbuf uq = STRBUF_INIT;
  1901. const char *endp;
  1902. struct object_entry *oe;
  1903. struct object_id oid;
  1904. uint16_t mode, inline_data = 0;
  1905. p = get_mode(p, &mode);
  1906. if (!p)
  1907. die("Corrupt mode: %s", command_buf.buf);
  1908. switch (mode) {
  1909. case 0644:
  1910. case 0755:
  1911. mode |= S_IFREG;
  1912. case S_IFREG | 0644:
  1913. case S_IFREG | 0755:
  1914. case S_IFLNK:
  1915. case S_IFDIR:
  1916. case S_IFGITLINK:
  1917. /* ok */
  1918. break;
  1919. default:
  1920. die("Corrupt mode: %s", command_buf.buf);
  1921. }
  1922. if (*p == ':') {
  1923. oe = find_mark(parse_mark_ref_space(&p));
  1924. oidcpy(&oid, &oe->idx.oid);
  1925. } else if (skip_prefix(p, "inline ", &p)) {
  1926. inline_data = 1;
  1927. oe = NULL; /* not used with inline_data, but makes gcc happy */
  1928. } else {
  1929. if (parse_oid_hex(p, &oid, &p))
  1930. die("Invalid dataref: %s", command_buf.buf);
  1931. oe = find_object(&oid);
  1932. if (*p++ != ' ')
  1933. die("Missing space after SHA1: %s", command_buf.buf);
  1934. }
  1935. strbuf_reset(&uq);
  1936. if (!unquote_c_style(&uq, p, &endp)) {
  1937. if (*endp)
  1938. die("Garbage after path in: %s", command_buf.buf);
  1939. p = uq.buf;
  1940. }
  1941. /* Git does not track empty, non-toplevel directories. */
  1942. if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *p) {
  1943. tree_content_remove(&b->branch_tree, p, NULL, 0);
  1944. return;
  1945. }
  1946. if (S_ISGITLINK(mode)) {
  1947. if (inline_data)
  1948. die("Git links cannot be specified 'inline': %s",
  1949. command_buf.buf);
  1950. else if (oe) {
  1951. if (oe->type != OBJ_COMMIT)
  1952. die("Not a commit (actually a %s): %s",
  1953. type_name(oe->type), command_buf.buf);
  1954. }
  1955. /*
  1956. * Accept the sha1 without checking; it expected to be in
  1957. * another repository.
  1958. */
  1959. } else if (inline_data) {
  1960. if (S_ISDIR(mode))
  1961. die("Directories cannot be specified 'inline': %s",
  1962. command_buf.buf);
  1963. if (p != uq.buf) {
  1964. strbuf_addstr(&uq, p);
  1965. p = uq.buf;
  1966. }
  1967. while (read_next_command() != EOF) {
  1968. const char *v;
  1969. if (skip_prefix(command_buf.buf, "cat-blob ", &v))
  1970. parse_cat_blob(v);
  1971. else {
  1972. parse_and_store_blob(&last_blob, &oid, 0);
  1973. break;
  1974. }
  1975. }
  1976. } else {
  1977. enum object_type expected = S_ISDIR(mode) ?
  1978. OBJ_TREE: OBJ_BLOB;
  1979. enum object_type type = oe ? oe->type :
  1980. oid_object_info(the_repository, &oid,
  1981. NULL);
  1982. if (type < 0)
  1983. die("%s not found: %s",
  1984. S_ISDIR(mode) ? "Tree" : "Blob",
  1985. command_buf.buf);
  1986. if (type != expected)
  1987. die("Not a %s (actually a %s): %s",
  1988. type_name(expected), type_name(type),
  1989. command_buf.buf);
  1990. }
  1991. if (!*p) {
  1992. tree_content_replace(&b->branch_tree, &oid, mode, NULL);
  1993. return;
  1994. }
  1995. tree_content_set(&b->branch_tree, p, &oid, mode, NULL);
  1996. }
  1997. static void file_change_d(const char *p, struct branch *b)
  1998. {
  1999. static struct strbuf uq = STRBUF_INIT;
  2000. const char *endp;
  2001. strbuf_reset(&uq);
  2002. if (!unquote_c_style(&uq, p, &endp)) {
  2003. if (*endp)
  2004. die("Garbage after path in: %s", command_buf.buf);
  2005. p = uq.buf;
  2006. }
  2007. tree_content_remove(&b->branch_tree, p, NULL, 1);
  2008. }
  2009. static void file_change_cr(const char *s, struct branch *b, int rename)
  2010. {
  2011. const char *d;
  2012. static struct strbuf s_uq = STRBUF_INIT;
  2013. static struct strbuf d_uq = STRBUF_INIT;
  2014. const char *endp;
  2015. struct tree_entry leaf;
  2016. strbuf_reset(&s_uq);
  2017. if (!unquote_c_style(&s_uq, s, &endp)) {
  2018. if (*endp != ' ')
  2019. die("Missing space after source: %s", command_buf.buf);
  2020. } else {
  2021. endp = strchr(s, ' ');
  2022. if (!endp)
  2023. die("Missing space after source: %s", command_buf.buf);
  2024. strbuf_add(&s_uq, s, endp - s);
  2025. }
  2026. s = s_uq.buf;
  2027. endp++;
  2028. if (!*endp)
  2029. die("Missing dest: %s", command_buf.buf);
  2030. d = endp;
  2031. strbuf_reset(&d_uq);
  2032. if (!unquote_c_style(&d_uq, d, &endp)) {
  2033. if (*endp)
  2034. die("Garbage after dest in: %s", command_buf.buf);
  2035. d = d_uq.buf;
  2036. }
  2037. memset(&leaf, 0, sizeof(leaf));
  2038. if (rename)
  2039. tree_content_remove(&b->branch_tree, s, &leaf, 1);
  2040. else
  2041. tree_content_get(&b->branch_tree, s, &leaf, 1);
  2042. if (!leaf.versions[1].mode)
  2043. die("Path %s not in branch", s);
  2044. if (!*d) { /* C "path/to/subdir" "" */
  2045. tree_content_replace(&b->branch_tree,
  2046. &leaf.versions[1].oid,
  2047. leaf.versions[1].mode,
  2048. leaf.tree);
  2049. return;
  2050. }
  2051. tree_content_set(&b->branch_tree, d,
  2052. &leaf.versions[1].oid,
  2053. leaf.versions[1].mode,
  2054. leaf.tree);
  2055. }
  2056. static void note_change_n(const char *p, struct branch *b, unsigned char *old_fanout)
  2057. {
  2058. static struct strbuf uq = STRBUF_INIT;
  2059. struct object_entry *oe;
  2060. struct branch *s;
  2061. struct object_id oid, commit_oid;
  2062. char path[GIT_MAX_RAWSZ * 3];
  2063. uint16_t inline_data = 0;
  2064. unsigned char new_fanout;
  2065. /*
  2066. * When loading a branch, we don't traverse its tree to count the real
  2067. * number of notes (too expensive to do this for all non-note refs).
  2068. * This means that recently loaded notes refs might incorrectly have
  2069. * b->num_notes == 0, and consequently, old_fanout might be wrong.
  2070. *
  2071. * Fix this by traversing the tree and counting the number of notes
  2072. * when b->num_notes == 0. If the notes tree is truly empty, the
  2073. * calculation should not take long.
  2074. */
  2075. if (b->num_notes == 0 && *old_fanout == 0) {
  2076. /* Invoke change_note_fanout() in "counting mode". */
  2077. b->num_notes = change_note_fanout(&b->branch_tree, 0xff);
  2078. *old_fanout = convert_num_notes_to_fanout(b->num_notes);
  2079. }
  2080. /* Now parse the notemodify command. */
  2081. /* <dataref> or 'inline' */
  2082. if (*p == ':') {
  2083. oe = find_mark(parse_mark_ref_space(&p));
  2084. oidcpy(&oid, &oe->idx.oid);
  2085. } else if (skip_prefix(p, "inline ", &p)) {
  2086. inline_data = 1;
  2087. oe = NULL; /* not used with inline_data, but makes gcc happy */
  2088. } else {
  2089. if (parse_oid_hex(p, &oid, &p))
  2090. die("Invalid dataref: %s", command_buf.buf);
  2091. oe = find_object(&oid);
  2092. if (*p++ != ' ')
  2093. die("Missing space after SHA1: %s", command_buf.buf);
  2094. }
  2095. /* <commit-ish> */
  2096. s = lookup_branch(p);
  2097. if (s) {
  2098. if (is_null_oid(&s->oid))
  2099. die("Can't add a note on empty branch.");
  2100. oidcpy(&commit_oid, &s->oid);
  2101. } else if (*p == ':') {
  2102. uintmax_t commit_mark = parse_mark_ref_eol(p);
  2103. struct object_entry *commit_oe = find_mark(commit_mark);
  2104. if (commit_oe->type != OBJ_COMMIT)
  2105. die("Mark :%" PRIuMAX " not a commit", commit_mark);
  2106. oidcpy(&commit_oid, &commit_oe->idx.oid);
  2107. } else if (!get_oid(p, &commit_oid)) {
  2108. unsigned long size;
  2109. char *buf = read_object_with_reference(the_repository,
  2110. &commit_oid,
  2111. commit_type, &size,
  2112. &commit_oid);
  2113. if (!buf || size < the_hash_algo->hexsz + 6)
  2114. die("Not a valid commit: %s", p);
  2115. free(buf);
  2116. } else
  2117. die("Invalid ref name or SHA1 expression: %s", p);
  2118. if (inline_data) {
  2119. if (p != uq.buf) {
  2120. strbuf_addstr(&uq, p);
  2121. p = uq.buf;
  2122. }
  2123. read_next_command();
  2124. parse_and_store_blob(&last_blob, &oid, 0);
  2125. } else if (oe) {
  2126. if (oe->type != OBJ_BLOB)
  2127. die("Not a blob (actually a %s): %s",
  2128. type_name(oe->type), command_buf.buf);
  2129. } else if (!is_null_oid(&oid)) {
  2130. enum object_type type = oid_object_info(the_repository, &oid,
  2131. NULL);
  2132. if (type < 0)
  2133. die("Blob not found: %s", command_buf.buf);
  2134. if (type != OBJ_BLOB)
  2135. die("Not a blob (actually a %s): %s",
  2136. type_name(type), command_buf.buf);
  2137. }
  2138. construct_path_with_fanout(oid_to_hex(&commit_oid), *old_fanout, path);
  2139. if (tree_content_remove(&b->branch_tree, path, NULL, 0))
  2140. b->num_notes--;
  2141. if (is_null_oid(&oid))
  2142. return; /* nothing to insert */
  2143. b->num_notes++;
  2144. new_fanout = convert_num_notes_to_fanout(b->num_notes);
  2145. construct_path_with_fanout(oid_to_hex(&commit_oid), new_fanout, path);
  2146. tree_content_set(&b->branch_tree, path, &oid, S_IFREG | 0644, NULL);
  2147. }
  2148. static void file_change_deleteall(struct branch *b)
  2149. {
  2150. release_tree_content_recursive(b->branch_tree.tree);
  2151. oidclr(&b->branch_tree.versions[0].oid);
  2152. oidclr(&b->branch_tree.versions[1].oid);
  2153. load_tree(&b->branch_tree);
  2154. b->num_notes = 0;
  2155. }
  2156. static void parse_from_commit(struct branch *b, char *buf, unsigned long size)
  2157. {
  2158. if (!buf || size < the_hash_algo->hexsz + 6)
  2159. die("Not a valid commit: %s", oid_to_hex(&b->oid));
  2160. if (memcmp("tree ", buf, 5)
  2161. || get_oid_hex(buf + 5, &b->branch_tree.versions[1].oid))
  2162. die("The commit %s is corrupt", oid_to_hex(&b->oid));
  2163. oidcpy(&b->branch_tree.versions[0].oid,
  2164. &b->branch_tree.versions[1].oid);
  2165. }
  2166. static void parse_from_existing(struct branch *b)
  2167. {
  2168. if (is_null_oid(&b->oid)) {
  2169. oidclr(&b->branch_tree.versions[0].oid);
  2170. oidclr(&b->branch_tree.versions[1].oid);
  2171. } else {
  2172. unsigned long size;
  2173. char *buf;
  2174. buf = read_object_with_reference(the_repository,
  2175. &b->oid, commit_type, &size,
  2176. &b->oid);
  2177. parse_from_commit(b, buf, size);
  2178. free(buf);
  2179. }
  2180. }
  2181. static int parse_objectish(struct branch *b, const char *objectish)
  2182. {
  2183. struct branch *s;
  2184. struct object_id oid;
  2185. oidcpy(&oid, &b->branch_tree.versions[1].oid);
  2186. s = lookup_branch(objectish);
  2187. if (b == s)
  2188. die("Can't create a branch from itself: %s", b->name);
  2189. else if (s) {
  2190. struct object_id *t = &s->branch_tree.versions[1].oid;
  2191. oidcpy(&b->oid, &s->oid);
  2192. oidcpy(&b->branch_tree.versions[0].oid, t);
  2193. oidcpy(&b->branch_tree.versions[1].oid, t);
  2194. } else if (*objectish == ':') {
  2195. uintmax_t idnum = parse_mark_ref_eol(objectish);
  2196. struct object_entry *oe = find_mark(idnum);
  2197. if (oe->type != OBJ_COMMIT)
  2198. die("Mark :%" PRIuMAX " not a commit", idnum);
  2199. if (!oideq(&b->oid, &oe->idx.oid)) {
  2200. oidcpy(&b->oid, &oe->idx.oid);
  2201. if (oe->pack_id != MAX_PACK_ID) {
  2202. unsigned long size;
  2203. char *buf = gfi_unpack_entry(oe, &size);
  2204. parse_from_commit(b, buf, size);
  2205. free(buf);
  2206. } else
  2207. parse_from_existing(b);
  2208. }
  2209. } else if (!get_oid(objectish, &b->oid)) {
  2210. parse_from_existing(b);
  2211. if (is_null_oid(&b->oid))
  2212. b->delete = 1;
  2213. }
  2214. else
  2215. die("Invalid ref name or SHA1 expression: %s", objectish);
  2216. if (b->branch_tree.tree && !oideq(&oid, &b->branch_tree.versions[1].oid)) {
  2217. release_tree_content_recursive(b->branch_tree.tree);
  2218. b->branch_tree.tree = NULL;
  2219. }
  2220. read_next_command();
  2221. return 1;
  2222. }
  2223. static int parse_from(struct branch *b)
  2224. {
  2225. const char *from;
  2226. if (!skip_prefix(command_buf.buf, "from ", &from))
  2227. return 0;
  2228. return parse_objectish(b, from);
  2229. }
  2230. static int parse_objectish_with_prefix(struct branch *b, const char *prefix)
  2231. {
  2232. const char *base;
  2233. if (!skip_prefix(command_buf.buf, prefix, &base))
  2234. return 0;
  2235. return parse_objectish(b, base);
  2236. }
  2237. static struct hash_list *parse_merge(unsigned int *count)
  2238. {
  2239. struct hash_list *list = NULL, **tail = &list, *n;
  2240. const char *from;
  2241. struct branch *s;
  2242. *count = 0;
  2243. while (skip_prefix(command_buf.buf, "merge ", &from)) {
  2244. n = xmalloc(sizeof(*n));
  2245. s = lookup_branch(from);
  2246. if (s)
  2247. oidcpy(&n->oid, &s->oid);
  2248. else if (*from == ':') {
  2249. uintmax_t idnum = parse_mark_ref_eol(from);
  2250. struct object_entry *oe = find_mark(idnum);
  2251. if (oe->type != OBJ_COMMIT)
  2252. die("Mark :%" PRIuMAX " not a commit", idnum);
  2253. oidcpy(&n->oid, &oe->idx.oid);
  2254. } else if (!get_oid(from, &n->oid)) {
  2255. unsigned long size;
  2256. char *buf = read_object_with_reference(the_repository,
  2257. &n->oid,
  2258. commit_type,
  2259. &size, &n->oid);
  2260. if (!buf || size < the_hash_algo->hexsz + 6)
  2261. die("Not a valid commit: %s", from);
  2262. free(buf);
  2263. } else
  2264. die("Invalid ref name or SHA1 expression: %s", from);
  2265. n->next = NULL;
  2266. *tail = n;
  2267. tail = &n->next;
  2268. (*count)++;
  2269. read_next_command();
  2270. }
  2271. return list;
  2272. }
  2273. static void parse_new_commit(const char *arg)
  2274. {
  2275. static struct strbuf msg = STRBUF_INIT;
  2276. struct branch *b;
  2277. char *author = NULL;
  2278. char *committer = NULL;
  2279. char *encoding = NULL;
  2280. struct hash_list *merge_list = NULL;
  2281. unsigned int merge_count;
  2282. unsigned char prev_fanout, new_fanout;
  2283. const char *v;
  2284. b = lookup_branch(arg);
  2285. if (!b)
  2286. b = new_branch(arg);
  2287. read_next_command();
  2288. parse_mark();
  2289. parse_original_identifier();
  2290. if (skip_prefix(command_buf.buf, "author ", &v)) {
  2291. author = parse_ident(v);
  2292. read_next_command();
  2293. }
  2294. if (skip_prefix(command_buf.buf, "committer ", &v)) {
  2295. committer = parse_ident(v);
  2296. read_next_command();
  2297. }
  2298. if (!committer)
  2299. die("Expected committer but didn't get one");
  2300. if (skip_prefix(command_buf.buf, "encoding ", &v)) {
  2301. encoding = xstrdup(v);
  2302. read_next_command();
  2303. }
  2304. parse_data(&msg, 0, NULL);
  2305. read_next_command();
  2306. parse_from(b);
  2307. merge_list = parse_merge(&merge_count);
  2308. /* ensure the branch is active/loaded */
  2309. if (!b->branch_tree.tree || !max_active_branches) {
  2310. unload_one_branch();
  2311. load_branch(b);
  2312. }
  2313. prev_fanout = convert_num_notes_to_fanout(b->num_notes);
  2314. /* file_change* */
  2315. while (command_buf.len > 0) {
  2316. if (skip_prefix(command_buf.buf, "M ", &v))
  2317. file_change_m(v, b);
  2318. else if (skip_prefix(command_buf.buf, "D ", &v))
  2319. file_change_d(v, b);
  2320. else if (skip_prefix(command_buf.buf, "R ", &v))
  2321. file_change_cr(v, b, 1);
  2322. else if (skip_prefix(command_buf.buf, "C ", &v))
  2323. file_change_cr(v, b, 0);
  2324. else if (skip_prefix(command_buf.buf, "N ", &v))
  2325. note_change_n(v, b, &prev_fanout);
  2326. else if (!strcmp("deleteall", command_buf.buf))
  2327. file_change_deleteall(b);
  2328. else if (skip_prefix(command_buf.buf, "ls ", &v))
  2329. parse_ls(v, b);
  2330. else if (skip_prefix(command_buf.buf, "cat-blob ", &v))
  2331. parse_cat_blob(v);
  2332. else {
  2333. unread_command_buf = 1;
  2334. break;
  2335. }
  2336. if (read_next_command() == EOF)
  2337. break;
  2338. }
  2339. new_fanout = convert_num_notes_to_fanout(b->num_notes);
  2340. if (new_fanout != prev_fanout)
  2341. b->num_notes = change_note_fanout(&b->branch_tree, new_fanout);
  2342. /* build the tree and the commit */
  2343. store_tree(&b->branch_tree);
  2344. oidcpy(&b->branch_tree.versions[0].oid,
  2345. &b->branch_tree.versions[1].oid);
  2346. strbuf_reset(&new_data);
  2347. strbuf_addf(&new_data, "tree %s\n",
  2348. oid_to_hex(&b->branch_tree.versions[1].oid));
  2349. if (!is_null_oid(&b->oid))
  2350. strbuf_addf(&new_data, "parent %s\n",
  2351. oid_to_hex(&b->oid));
  2352. while (merge_list) {
  2353. struct hash_list *next = merge_list->next;
  2354. strbuf_addf(&new_data, "parent %s\n",
  2355. oid_to_hex(&merge_list->oid));
  2356. free(merge_list);
  2357. merge_list = next;
  2358. }
  2359. strbuf_addf(&new_data,
  2360. "author %s\n"
  2361. "committer %s\n",
  2362. author ? author : committer, committer);
  2363. if (encoding)
  2364. strbuf_addf(&new_data,
  2365. "encoding %s\n",
  2366. encoding);
  2367. strbuf_addch(&new_data, '\n');
  2368. strbuf_addbuf(&new_data, &msg);
  2369. free(author);
  2370. free(committer);
  2371. free(encoding);
  2372. if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark))
  2373. b->pack_id = pack_id;
  2374. b->last_commit = object_count_by_type[OBJ_COMMIT];
  2375. }
  2376. static void parse_new_tag(const char *arg)
  2377. {
  2378. static struct strbuf msg = STRBUF_INIT;
  2379. const char *from;
  2380. char *tagger;
  2381. struct branch *s;
  2382. struct tag *t;
  2383. uintmax_t from_mark = 0;
  2384. struct object_id oid;
  2385. enum object_type type;
  2386. const char *v;
  2387. t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag));
  2388. memset(t, 0, sizeof(struct tag));
  2389. t->name = pool_strdup(arg);
  2390. if (last_tag)
  2391. last_tag->next_tag = t;
  2392. else
  2393. first_tag = t;
  2394. last_tag = t;
  2395. read_next_command();
  2396. parse_mark();
  2397. /* from ... */
  2398. if (!skip_prefix(command_buf.buf, "from ", &from))
  2399. die("Expected from command, got %s", command_buf.buf);
  2400. s = lookup_branch(from);
  2401. if (s) {
  2402. if (is_null_oid(&s->oid))
  2403. die("Can't tag an empty branch.");
  2404. oidcpy(&oid, &s->oid);
  2405. type = OBJ_COMMIT;
  2406. } else if (*from == ':') {
  2407. struct object_entry *oe;
  2408. from_mark = parse_mark_ref_eol(from);
  2409. oe = find_mark(from_mark);
  2410. type = oe->type;
  2411. oidcpy(&oid, &oe->idx.oid);
  2412. } else if (!get_oid(from, &oid)) {
  2413. struct object_entry *oe = find_object(&oid);
  2414. if (!oe) {
  2415. type = oid_object_info(the_repository, &oid, NULL);
  2416. if (type < 0)
  2417. die("Not a valid object: %s", from);
  2418. } else
  2419. type = oe->type;
  2420. } else
  2421. die("Invalid ref name or SHA1 expression: %s", from);
  2422. read_next_command();
  2423. /* original-oid ... */
  2424. parse_original_identifier();
  2425. /* tagger ... */
  2426. if (skip_prefix(command_buf.buf, "tagger ", &v)) {
  2427. tagger = parse_ident(v);
  2428. read_next_command();
  2429. } else
  2430. tagger = NULL;
  2431. /* tag payload/message */
  2432. parse_data(&msg, 0, NULL);
  2433. /* build the tag object */
  2434. strbuf_reset(&new_data);
  2435. strbuf_addf(&new_data,
  2436. "object %s\n"
  2437. "type %s\n"
  2438. "tag %s\n",
  2439. oid_to_hex(&oid), type_name(type), t->name);
  2440. if (tagger)
  2441. strbuf_addf(&new_data,
  2442. "tagger %s\n", tagger);
  2443. strbuf_addch(&new_data, '\n');
  2444. strbuf_addbuf(&new_data, &msg);
  2445. free(tagger);
  2446. if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, next_mark))
  2447. t->pack_id = MAX_PACK_ID;
  2448. else
  2449. t->pack_id = pack_id;
  2450. }
  2451. static void parse_reset_branch(const char *arg)
  2452. {
  2453. struct branch *b;
  2454. const char *tag_name;
  2455. b = lookup_branch(arg);
  2456. if (b) {
  2457. oidclr(&b->oid);
  2458. oidclr(&b->branch_tree.versions[0].oid);
  2459. oidclr(&b->branch_tree.versions[1].oid);
  2460. if (b->branch_tree.tree) {
  2461. release_tree_content_recursive(b->branch_tree.tree);
  2462. b->branch_tree.tree = NULL;
  2463. }
  2464. }
  2465. else
  2466. b = new_branch(arg);
  2467. read_next_command();
  2468. parse_from(b);
  2469. if (b->delete && skip_prefix(b->name, "refs/tags/", &tag_name)) {
  2470. /*
  2471. * Elsewhere, we call dump_branches() before dump_tags(),
  2472. * and dump_branches() will handle ref deletions first, so
  2473. * in order to make sure the deletion actually takes effect,
  2474. * we need to remove the tag from our list of tags to update.
  2475. *
  2476. * NEEDSWORK: replace list of tags with hashmap for faster
  2477. * deletion?
  2478. */
  2479. struct tag *t, *prev = NULL;
  2480. for (t = first_tag; t; t = t->next_tag) {
  2481. if (!strcmp(t->name, tag_name))
  2482. break;
  2483. prev = t;
  2484. }
  2485. if (t) {
  2486. if (prev)
  2487. prev->next_tag = t->next_tag;
  2488. else
  2489. first_tag = t->next_tag;
  2490. if (!t->next_tag)
  2491. last_tag = prev;
  2492. /* There is no mem_pool_free(t) function to call. */
  2493. }
  2494. }
  2495. if (command_buf.len > 0)
  2496. unread_command_buf = 1;
  2497. }
  2498. static void cat_blob_write(const char *buf, unsigned long size)
  2499. {
  2500. if (write_in_full(cat_blob_fd, buf, size) < 0)
  2501. die_errno("Write to frontend failed");
  2502. }
  2503. static void cat_blob(struct object_entry *oe, struct object_id *oid)
  2504. {
  2505. struct strbuf line = STRBUF_INIT;
  2506. unsigned long size;
  2507. enum object_type type = 0;
  2508. char *buf;
  2509. if (!oe || oe->pack_id == MAX_PACK_ID) {
  2510. buf = read_object_file(oid, &type, &size);
  2511. } else {
  2512. type = oe->type;
  2513. buf = gfi_unpack_entry(oe, &size);
  2514. }
  2515. /*
  2516. * Output based on batch_one_object() from cat-file.c.
  2517. */
  2518. if (type <= 0) {
  2519. strbuf_reset(&line);
  2520. strbuf_addf(&line, "%s missing\n", oid_to_hex(oid));
  2521. cat_blob_write(line.buf, line.len);
  2522. strbuf_release(&line);
  2523. free(buf);
  2524. return;
  2525. }
  2526. if (!buf)
  2527. die("Can't read object %s", oid_to_hex(oid));
  2528. if (type != OBJ_BLOB)
  2529. die("Object %s is a %s but a blob was expected.",
  2530. oid_to_hex(oid), type_name(type));
  2531. strbuf_reset(&line);
  2532. strbuf_addf(&line, "%s %s %"PRIuMAX"\n", oid_to_hex(oid),
  2533. type_name(type), (uintmax_t)size);
  2534. cat_blob_write(line.buf, line.len);
  2535. strbuf_release(&line);
  2536. cat_blob_write(buf, size);
  2537. cat_blob_write("\n", 1);
  2538. if (oe && oe->pack_id == pack_id) {
  2539. last_blob.offset = oe->idx.offset;
  2540. strbuf_attach(&last_blob.data, buf, size, size);
  2541. last_blob.depth = oe->depth;
  2542. } else
  2543. free(buf);
  2544. }
  2545. static void parse_get_mark(const char *p)
  2546. {
  2547. struct object_entry *oe;
  2548. char output[GIT_MAX_HEXSZ + 2];
  2549. /* get-mark SP <object> LF */
  2550. if (*p != ':')
  2551. die("Not a mark: %s", p);
  2552. oe = find_mark(parse_mark_ref_eol(p));
  2553. if (!oe)
  2554. die("Unknown mark: %s", command_buf.buf);
  2555. xsnprintf(output, sizeof(output), "%s\n", oid_to_hex(&oe->idx.oid));
  2556. cat_blob_write(output, the_hash_algo->hexsz + 1);
  2557. }
  2558. static void parse_cat_blob(const char *p)
  2559. {
  2560. struct object_entry *oe;
  2561. struct object_id oid;
  2562. /* cat-blob SP <object> LF */
  2563. if (*p == ':') {
  2564. oe = find_mark(parse_mark_ref_eol(p));
  2565. if (!oe)
  2566. die("Unknown mark: %s", command_buf.buf);
  2567. oidcpy(&oid, &oe->idx.oid);
  2568. } else {
  2569. if (parse_oid_hex(p, &oid, &p))
  2570. die("Invalid dataref: %s", command_buf.buf);
  2571. if (*p)
  2572. die("Garbage after SHA1: %s", command_buf.buf);
  2573. oe = find_object(&oid);
  2574. }
  2575. cat_blob(oe, &oid);
  2576. }
  2577. static struct object_entry *dereference(struct object_entry *oe,
  2578. struct object_id *oid)
  2579. {
  2580. unsigned long size;
  2581. char *buf = NULL;
  2582. const unsigned hexsz = the_hash_algo->hexsz;
  2583. if (!oe) {
  2584. enum object_type type = oid_object_info(the_repository, oid,
  2585. NULL);
  2586. if (type < 0)
  2587. die("object not found: %s", oid_to_hex(oid));
  2588. /* cache it! */
  2589. oe = insert_object(oid);
  2590. oe->type = type;
  2591. oe->pack_id = MAX_PACK_ID;
  2592. oe->idx.offset = 1;
  2593. }
  2594. switch (oe->type) {
  2595. case OBJ_TREE: /* easy case. */
  2596. return oe;
  2597. case OBJ_COMMIT:
  2598. case OBJ_TAG:
  2599. break;
  2600. default:
  2601. die("Not a tree-ish: %s", command_buf.buf);
  2602. }
  2603. if (oe->pack_id != MAX_PACK_ID) { /* in a pack being written */
  2604. buf = gfi_unpack_entry(oe, &size);
  2605. } else {
  2606. enum object_type unused;
  2607. buf = read_object_file(oid, &unused, &size);
  2608. }
  2609. if (!buf)
  2610. die("Can't load object %s", oid_to_hex(oid));
  2611. /* Peel one layer. */
  2612. switch (oe->type) {
  2613. case OBJ_TAG:
  2614. if (size < hexsz + strlen("object ") ||
  2615. get_oid_hex(buf + strlen("object "), oid))
  2616. die("Invalid SHA1 in tag: %s", command_buf.buf);
  2617. break;
  2618. case OBJ_COMMIT:
  2619. if (size < hexsz + strlen("tree ") ||
  2620. get_oid_hex(buf + strlen("tree "), oid))
  2621. die("Invalid SHA1 in commit: %s", command_buf.buf);
  2622. }
  2623. free(buf);
  2624. return find_object(oid);
  2625. }
  2626. static struct object_entry *parse_treeish_dataref(const char **p)
  2627. {
  2628. struct object_id oid;
  2629. struct object_entry *e;
  2630. if (**p == ':') { /* <mark> */
  2631. e = find_mark(parse_mark_ref_space(p));
  2632. if (!e)
  2633. die("Unknown mark: %s", command_buf.buf);
  2634. oidcpy(&oid, &e->idx.oid);
  2635. } else { /* <sha1> */
  2636. if (parse_oid_hex(*p, &oid, p))
  2637. die("Invalid dataref: %s", command_buf.buf);
  2638. e = find_object(&oid);
  2639. if (*(*p)++ != ' ')
  2640. die("Missing space after tree-ish: %s", command_buf.buf);
  2641. }
  2642. while (!e || e->type != OBJ_TREE)
  2643. e = dereference(e, &oid);
  2644. return e;
  2645. }
  2646. static void print_ls(int mode, const unsigned char *hash, const char *path)
  2647. {
  2648. static struct strbuf line = STRBUF_INIT;
  2649. /* See show_tree(). */
  2650. const char *type =
  2651. S_ISGITLINK(mode) ? commit_type :
  2652. S_ISDIR(mode) ? tree_type :
  2653. blob_type;
  2654. if (!mode) {
  2655. /* missing SP path LF */
  2656. strbuf_reset(&line);
  2657. strbuf_addstr(&line, "missing ");
  2658. quote_c_style(path, &line, NULL, 0);
  2659. strbuf_addch(&line, '\n');
  2660. } else {
  2661. /* mode SP type SP object_name TAB path LF */
  2662. strbuf_reset(&line);
  2663. strbuf_addf(&line, "%06o %s %s\t",
  2664. mode & ~NO_DELTA, type, hash_to_hex(hash));
  2665. quote_c_style(path, &line, NULL, 0);
  2666. strbuf_addch(&line, '\n');
  2667. }
  2668. cat_blob_write(line.buf, line.len);
  2669. }
  2670. static void parse_ls(const char *p, struct branch *b)
  2671. {
  2672. struct tree_entry *root = NULL;
  2673. struct tree_entry leaf = {NULL};
  2674. /* ls SP (<tree-ish> SP)? <path> */
  2675. if (*p == '"') {
  2676. if (!b)
  2677. die("Not in a commit: %s", command_buf.buf);
  2678. root = &b->branch_tree;
  2679. } else {
  2680. struct object_entry *e = parse_treeish_dataref(&p);
  2681. root = new_tree_entry();
  2682. oidcpy(&root->versions[1].oid, &e->idx.oid);
  2683. if (!is_null_oid(&root->versions[1].oid))
  2684. root->versions[1].mode = S_IFDIR;
  2685. load_tree(root);
  2686. }
  2687. if (*p == '"') {
  2688. static struct strbuf uq = STRBUF_INIT;
  2689. const char *endp;
  2690. strbuf_reset(&uq);
  2691. if (unquote_c_style(&uq, p, &endp))
  2692. die("Invalid path: %s", command_buf.buf);
  2693. if (*endp)
  2694. die("Garbage after path in: %s", command_buf.buf);
  2695. p = uq.buf;
  2696. }
  2697. tree_content_get(root, p, &leaf, 1);
  2698. /*
  2699. * A directory in preparation would have a sha1 of zero
  2700. * until it is saved. Save, for simplicity.
  2701. */
  2702. if (S_ISDIR(leaf.versions[1].mode))
  2703. store_tree(&leaf);
  2704. print_ls(leaf.versions[1].mode, leaf.versions[1].oid.hash, p);
  2705. if (leaf.tree)
  2706. release_tree_content_recursive(leaf.tree);
  2707. if (!b || root != &b->branch_tree)
  2708. release_tree_entry(root);
  2709. }
  2710. static void checkpoint(void)
  2711. {
  2712. checkpoint_requested = 0;
  2713. if (object_count) {
  2714. cycle_packfile();
  2715. }
  2716. dump_branches();
  2717. dump_tags();
  2718. dump_marks();
  2719. }
  2720. static void parse_checkpoint(void)
  2721. {
  2722. checkpoint_requested = 1;
  2723. skip_optional_lf();
  2724. }
  2725. static void parse_progress(void)
  2726. {
  2727. fwrite(command_buf.buf, 1, command_buf.len, stdout);
  2728. fputc('\n', stdout);
  2729. fflush(stdout);
  2730. skip_optional_lf();
  2731. }
  2732. static void parse_alias(void)
  2733. {
  2734. struct object_entry *e;
  2735. struct branch b;
  2736. skip_optional_lf();
  2737. read_next_command();
  2738. /* mark ... */
  2739. parse_mark();
  2740. if (!next_mark)
  2741. die(_("Expected 'mark' command, got %s"), command_buf.buf);
  2742. /* to ... */
  2743. memset(&b, 0, sizeof(b));
  2744. if (!parse_objectish_with_prefix(&b, "to "))
  2745. die(_("Expected 'to' command, got %s"), command_buf.buf);
  2746. e = find_object(&b.oid);
  2747. assert(e);
  2748. insert_mark(next_mark, e);
  2749. }
  2750. static char* make_fast_import_path(const char *path)
  2751. {
  2752. if (!relative_marks_paths || is_absolute_path(path))
  2753. return xstrdup(path);
  2754. return git_pathdup("info/fast-import/%s", path);
  2755. }
  2756. static void option_import_marks(const char *marks,
  2757. int from_stream, int ignore_missing)
  2758. {
  2759. if (import_marks_file) {
  2760. if (from_stream)
  2761. die("Only one import-marks command allowed per stream");
  2762. /* read previous mark file */
  2763. if(!import_marks_file_from_stream)
  2764. read_marks();
  2765. }
  2766. import_marks_file = make_fast_import_path(marks);
  2767. safe_create_leading_directories_const(import_marks_file);
  2768. import_marks_file_from_stream = from_stream;
  2769. import_marks_file_ignore_missing = ignore_missing;
  2770. }
  2771. static void option_date_format(const char *fmt)
  2772. {
  2773. if (!strcmp(fmt, "raw"))
  2774. whenspec = WHENSPEC_RAW;
  2775. else if (!strcmp(fmt, "rfc2822"))
  2776. whenspec = WHENSPEC_RFC2822;
  2777. else if (!strcmp(fmt, "now"))
  2778. whenspec = WHENSPEC_NOW;
  2779. else
  2780. die("unknown --date-format argument %s", fmt);
  2781. }
  2782. static unsigned long ulong_arg(const char *option, const char *arg)
  2783. {
  2784. char *endptr;
  2785. unsigned long rv = strtoul(arg, &endptr, 0);
  2786. if (strchr(arg, '-') || endptr == arg || *endptr)
  2787. die("%s: argument must be a non-negative integer", option);
  2788. return rv;
  2789. }
  2790. static void option_depth(const char *depth)
  2791. {
  2792. max_depth = ulong_arg("--depth", depth);
  2793. if (max_depth > MAX_DEPTH)
  2794. die("--depth cannot exceed %u", MAX_DEPTH);
  2795. }
  2796. static void option_active_branches(const char *branches)
  2797. {
  2798. max_active_branches = ulong_arg("--active-branches", branches);
  2799. }
  2800. static void option_export_marks(const char *marks)
  2801. {
  2802. export_marks_file = make_fast_import_path(marks);
  2803. safe_create_leading_directories_const(export_marks_file);
  2804. }
  2805. static void option_cat_blob_fd(const char *fd)
  2806. {
  2807. unsigned long n = ulong_arg("--cat-blob-fd", fd);
  2808. if (n > (unsigned long) INT_MAX)
  2809. die("--cat-blob-fd cannot exceed %d", INT_MAX);
  2810. cat_blob_fd = (int) n;
  2811. }
  2812. static void option_export_pack_edges(const char *edges)
  2813. {
  2814. if (pack_edges)
  2815. fclose(pack_edges);
  2816. pack_edges = xfopen(edges, "a");
  2817. }
  2818. static int parse_one_option(const char *option)
  2819. {
  2820. if (skip_prefix(option, "max-pack-size=", &option)) {
  2821. unsigned long v;
  2822. if (!git_parse_ulong(option, &v))
  2823. return 0;
  2824. if (v < 8192) {
  2825. warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v);
  2826. v *= 1024 * 1024;
  2827. } else if (v < 1024 * 1024) {
  2828. warning("minimum max-pack-size is 1 MiB");
  2829. v = 1024 * 1024;
  2830. }
  2831. max_packsize = v;
  2832. } else if (skip_prefix(option, "big-file-threshold=", &option)) {
  2833. unsigned long v;
  2834. if (!git_parse_ulong(option, &v))
  2835. return 0;
  2836. big_file_threshold = v;
  2837. } else if (skip_prefix(option, "depth=", &option)) {
  2838. option_depth(option);
  2839. } else if (skip_prefix(option, "active-branches=", &option)) {
  2840. option_active_branches(option);
  2841. } else if (skip_prefix(option, "export-pack-edges=", &option)) {
  2842. option_export_pack_edges(option);
  2843. } else if (starts_with(option, "quiet")) {
  2844. show_stats = 0;
  2845. } else if (starts_with(option, "stats")) {
  2846. show_stats = 1;
  2847. } else {
  2848. return 0;
  2849. }
  2850. return 1;
  2851. }
  2852. static int parse_one_feature(const char *feature, int from_stream)
  2853. {
  2854. const char *arg;
  2855. if (skip_prefix(feature, "date-format=", &arg)) {
  2856. option_date_format(arg);
  2857. } else if (skip_prefix(feature, "import-marks=", &arg)) {
  2858. option_import_marks(arg, from_stream, 0);
  2859. } else if (skip_prefix(feature, "import-marks-if-exists=", &arg)) {
  2860. option_import_marks(arg, from_stream, 1);
  2861. } else if (skip_prefix(feature, "export-marks=", &arg)) {
  2862. option_export_marks(arg);
  2863. } else if (!strcmp(feature, "alias")) {
  2864. ; /* Don't die - this feature is supported */
  2865. } else if (!strcmp(feature, "get-mark")) {
  2866. ; /* Don't die - this feature is supported */
  2867. } else if (!strcmp(feature, "cat-blob")) {
  2868. ; /* Don't die - this feature is supported */
  2869. } else if (!strcmp(feature, "relative-marks")) {
  2870. relative_marks_paths = 1;
  2871. } else if (!strcmp(feature, "no-relative-marks")) {
  2872. relative_marks_paths = 0;
  2873. } else if (!strcmp(feature, "done")) {
  2874. require_explicit_termination = 1;
  2875. } else if (!strcmp(feature, "force")) {
  2876. force_update = 1;
  2877. } else if (!strcmp(feature, "notes") || !strcmp(feature, "ls")) {
  2878. ; /* do nothing; we have the feature */
  2879. } else {
  2880. return 0;
  2881. }
  2882. return 1;
  2883. }
  2884. static void parse_feature(const char *feature)
  2885. {
  2886. if (seen_data_command)
  2887. die("Got feature command '%s' after data command", feature);
  2888. if (parse_one_feature(feature, 1))
  2889. return;
  2890. die("This version of fast-import does not support feature %s.", feature);
  2891. }
  2892. static void parse_option(const char *option)
  2893. {
  2894. if (seen_data_command)
  2895. die("Got option command '%s' after data command", option);
  2896. if (parse_one_option(option))
  2897. return;
  2898. die("This version of fast-import does not support option: %s", option);
  2899. }
  2900. static void git_pack_config(void)
  2901. {
  2902. int indexversion_value;
  2903. int limit;
  2904. unsigned long packsizelimit_value;
  2905. if (!git_config_get_ulong("pack.depth", &max_depth)) {
  2906. if (max_depth > MAX_DEPTH)
  2907. max_depth = MAX_DEPTH;
  2908. }
  2909. if (!git_config_get_int("pack.indexversion", &indexversion_value)) {
  2910. pack_idx_opts.version = indexversion_value;
  2911. if (pack_idx_opts.version > 2)
  2912. git_die_config("pack.indexversion",
  2913. "bad pack.indexversion=%"PRIu32, pack_idx_opts.version);
  2914. }
  2915. if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value))
  2916. max_packsize = packsizelimit_value;
  2917. if (!git_config_get_int("fastimport.unpacklimit", &limit))
  2918. unpack_limit = limit;
  2919. else if (!git_config_get_int("transfer.unpacklimit", &limit))
  2920. unpack_limit = limit;
  2921. git_config(git_default_config, NULL);
  2922. }
  2923. static const char fast_import_usage[] =
  2924. "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
  2925. static void parse_argv(void)
  2926. {
  2927. unsigned int i;
  2928. for (i = 1; i < global_argc; i++) {
  2929. const char *a = global_argv[i];
  2930. if (*a != '-' || !strcmp(a, "--"))
  2931. break;
  2932. if (!skip_prefix(a, "--", &a))
  2933. die("unknown option %s", a);
  2934. if (parse_one_option(a))
  2935. continue;
  2936. if (parse_one_feature(a, 0))
  2937. continue;
  2938. if (skip_prefix(a, "cat-blob-fd=", &a)) {
  2939. option_cat_blob_fd(a);
  2940. continue;
  2941. }
  2942. die("unknown option --%s", a);
  2943. }
  2944. if (i != global_argc)
  2945. usage(fast_import_usage);
  2946. seen_data_command = 1;
  2947. if (import_marks_file)
  2948. read_marks();
  2949. }
  2950. int cmd_main(int argc, const char **argv)
  2951. {
  2952. unsigned int i;
  2953. if (argc == 2 && !strcmp(argv[1], "-h"))
  2954. usage(fast_import_usage);
  2955. setup_git_directory();
  2956. reset_pack_idx_option(&pack_idx_opts);
  2957. git_pack_config();
  2958. alloc_objects(object_entry_alloc);
  2959. strbuf_init(&command_buf, 0);
  2960. atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
  2961. branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
  2962. avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
  2963. marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set));
  2964. global_argc = argc;
  2965. global_argv = argv;
  2966. rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free));
  2967. for (i = 0; i < (cmd_save - 1); i++)
  2968. rc_free[i].next = &rc_free[i + 1];
  2969. rc_free[cmd_save - 1].next = NULL;
  2970. start_packfile();
  2971. set_die_routine(die_nicely);
  2972. set_checkpoint_signal();
  2973. while (read_next_command() != EOF) {
  2974. const char *v;
  2975. if (!strcmp("blob", command_buf.buf))
  2976. parse_new_blob();
  2977. else if (skip_prefix(command_buf.buf, "commit ", &v))
  2978. parse_new_commit(v);
  2979. else if (skip_prefix(command_buf.buf, "tag ", &v))
  2980. parse_new_tag(v);
  2981. else if (skip_prefix(command_buf.buf, "reset ", &v))
  2982. parse_reset_branch(v);
  2983. else if (skip_prefix(command_buf.buf, "ls ", &v))
  2984. parse_ls(v, NULL);
  2985. else if (skip_prefix(command_buf.buf, "cat-blob ", &v))
  2986. parse_cat_blob(v);
  2987. else if (skip_prefix(command_buf.buf, "get-mark ", &v))
  2988. parse_get_mark(v);
  2989. else if (!strcmp("checkpoint", command_buf.buf))
  2990. parse_checkpoint();
  2991. else if (!strcmp("done", command_buf.buf))
  2992. break;
  2993. else if (!strcmp("alias", command_buf.buf))
  2994. parse_alias();
  2995. else if (starts_with(command_buf.buf, "progress "))
  2996. parse_progress();
  2997. else if (skip_prefix(command_buf.buf, "feature ", &v))
  2998. parse_feature(v);
  2999. else if (skip_prefix(command_buf.buf, "option git ", &v))
  3000. parse_option(v);
  3001. else if (starts_with(command_buf.buf, "option "))
  3002. /* ignore non-git options*/;
  3003. else
  3004. die("Unsupported command: %s", command_buf.buf);
  3005. if (checkpoint_requested)
  3006. checkpoint();
  3007. }
  3008. /* argv hasn't been parsed yet, do so */
  3009. if (!seen_data_command)
  3010. parse_argv();
  3011. if (require_explicit_termination && feof(stdin))
  3012. die("stream ends early");
  3013. end_packfile();
  3014. dump_branches();
  3015. dump_tags();
  3016. unkeep_all_packs();
  3017. dump_marks();
  3018. if (pack_edges)
  3019. fclose(pack_edges);
  3020. if (show_stats) {
  3021. uintmax_t total_count = 0, duplicate_count = 0;
  3022. for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
  3023. total_count += object_count_by_type[i];
  3024. for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
  3025. duplicate_count += duplicate_count_by_type[i];
  3026. fprintf(stderr, "%s statistics:\n", argv[0]);
  3027. fprintf(stderr, "---------------------------------------------------------------------\n");
  3028. fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
  3029. fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
  3030. fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB], delta_count_attempts_by_type[OBJ_BLOB]);
  3031. fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE], delta_count_attempts_by_type[OBJ_TREE]);
  3032. fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT], delta_count_attempts_by_type[OBJ_COMMIT]);
  3033. fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas of %10" PRIuMAX" attempts)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG], delta_count_attempts_by_type[OBJ_TAG]);
  3034. fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
  3035. fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
  3036. fprintf(stderr, " atoms: %10u\n", atom_cnt);
  3037. fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (tree_entry_allocd + fi_mem_pool.pool_alloc + alloc_count*sizeof(struct object_entry))/1024);
  3038. fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd + fi_mem_pool.pool_alloc) /1024));
  3039. fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
  3040. fprintf(stderr, "---------------------------------------------------------------------\n");
  3041. pack_report();
  3042. fprintf(stderr, "---------------------------------------------------------------------\n");
  3043. fprintf(stderr, "\n");
  3044. }
  3045. return failure ? 1 : 0;
  3046. }