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.

3508 lines
88KB

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