bundle.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. #include "cache.h"
  2. #include "lockfile.h"
  3. #include "bundle.h"
  4. #include "object-store.h"
  5. #include "repository.h"
  6. #include "object.h"
  7. #include "commit.h"
  8. #include "diff.h"
  9. #include "revision.h"
  10. #include "list-objects.h"
  11. #include "run-command.h"
  12. #include "refs.h"
  13. #include "strvec.h"
  14. static const char v2_bundle_signature[] = "# v2 git bundle\n";
  15. static const char v3_bundle_signature[] = "# v3 git bundle\n";
  16. static struct {
  17. int version;
  18. const char *signature;
  19. } bundle_sigs[] = {
  20. { 2, v2_bundle_signature },
  21. { 3, v3_bundle_signature },
  22. };
  23. static void add_to_ref_list(const struct object_id *oid, const char *name,
  24. struct ref_list *list)
  25. {
  26. ALLOC_GROW(list->list, list->nr + 1, list->alloc);
  27. oidcpy(&list->list[list->nr].oid, oid);
  28. list->list[list->nr].name = xstrdup(name);
  29. list->nr++;
  30. }
  31. static int parse_capability(struct bundle_header *header, const char *capability)
  32. {
  33. const char *arg;
  34. if (skip_prefix(capability, "object-format=", &arg)) {
  35. int algo = hash_algo_by_name(arg);
  36. if (algo == GIT_HASH_UNKNOWN)
  37. return error(_("unrecognized bundle hash algorithm: %s"), arg);
  38. header->hash_algo = &hash_algos[algo];
  39. return 0;
  40. }
  41. return error(_("unknown capability '%s'"), capability);
  42. }
  43. static int parse_bundle_signature(struct bundle_header *header, const char *line)
  44. {
  45. int i;
  46. for (i = 0; i < ARRAY_SIZE(bundle_sigs); i++) {
  47. if (!strcmp(line, bundle_sigs[i].signature)) {
  48. header->version = bundle_sigs[i].version;
  49. return 0;
  50. }
  51. }
  52. return -1;
  53. }
  54. static int parse_bundle_header(int fd, struct bundle_header *header,
  55. const char *report_path)
  56. {
  57. struct strbuf buf = STRBUF_INIT;
  58. int status = 0;
  59. /* The bundle header begins with the signature */
  60. if (strbuf_getwholeline_fd(&buf, fd, '\n') ||
  61. parse_bundle_signature(header, buf.buf)) {
  62. if (report_path)
  63. error(_("'%s' does not look like a v2 or v3 bundle file"),
  64. report_path);
  65. status = -1;
  66. goto abort;
  67. }
  68. header->hash_algo = the_hash_algo;
  69. /* The bundle header ends with an empty line */
  70. while (!strbuf_getwholeline_fd(&buf, fd, '\n') &&
  71. buf.len && buf.buf[0] != '\n') {
  72. struct object_id oid;
  73. int is_prereq = 0;
  74. const char *p;
  75. strbuf_rtrim(&buf);
  76. if (header->version == 3 && *buf.buf == '@') {
  77. if (parse_capability(header, buf.buf + 1)) {
  78. status = -1;
  79. break;
  80. }
  81. continue;
  82. }
  83. if (*buf.buf == '-') {
  84. is_prereq = 1;
  85. strbuf_remove(&buf, 0, 1);
  86. }
  87. /*
  88. * Tip lines have object name, SP, and refname.
  89. * Prerequisites have object name that is optionally
  90. * followed by SP and subject line.
  91. */
  92. if (parse_oid_hex_algop(buf.buf, &oid, &p, header->hash_algo) ||
  93. (*p && !isspace(*p)) ||
  94. (!is_prereq && !*p)) {
  95. if (report_path)
  96. error(_("unrecognized header: %s%s (%d)"),
  97. (is_prereq ? "-" : ""), buf.buf, (int)buf.len);
  98. status = -1;
  99. break;
  100. } else {
  101. if (is_prereq)
  102. add_to_ref_list(&oid, "", &header->prerequisites);
  103. else
  104. add_to_ref_list(&oid, p + 1, &header->references);
  105. }
  106. }
  107. abort:
  108. if (status) {
  109. close(fd);
  110. fd = -1;
  111. }
  112. strbuf_release(&buf);
  113. return fd;
  114. }
  115. int read_bundle_header(const char *path, struct bundle_header *header)
  116. {
  117. int fd = open(path, O_RDONLY);
  118. if (fd < 0)
  119. return error(_("could not open '%s'"), path);
  120. return parse_bundle_header(fd, header, path);
  121. }
  122. int is_bundle(const char *path, int quiet)
  123. {
  124. struct bundle_header header;
  125. int fd = open(path, O_RDONLY);
  126. if (fd < 0)
  127. return 0;
  128. memset(&header, 0, sizeof(header));
  129. fd = parse_bundle_header(fd, &header, quiet ? NULL : path);
  130. if (fd >= 0)
  131. close(fd);
  132. return (fd >= 0);
  133. }
  134. static int list_refs(struct ref_list *r, int argc, const char **argv)
  135. {
  136. int i;
  137. for (i = 0; i < r->nr; i++) {
  138. if (argc > 1) {
  139. int j;
  140. for (j = 1; j < argc; j++)
  141. if (!strcmp(r->list[i].name, argv[j]))
  142. break;
  143. if (j == argc)
  144. continue;
  145. }
  146. printf("%s %s\n", oid_to_hex(&r->list[i].oid),
  147. r->list[i].name);
  148. }
  149. return 0;
  150. }
  151. /* Remember to update object flag allocation in object.h */
  152. #define PREREQ_MARK (1u<<16)
  153. int verify_bundle(struct repository *r,
  154. struct bundle_header *header,
  155. int verbose)
  156. {
  157. /*
  158. * Do fast check, then if any prereqs are missing then go line by line
  159. * to be verbose about the errors
  160. */
  161. struct ref_list *p = &header->prerequisites;
  162. struct rev_info revs;
  163. const char *argv[] = {NULL, "--all", NULL};
  164. struct commit *commit;
  165. int i, ret = 0, req_nr;
  166. const char *message = _("Repository lacks these prerequisite commits:");
  167. if (!r || !r->objects || !r->objects->odb)
  168. return error(_("need a repository to verify a bundle"));
  169. repo_init_revisions(r, &revs, NULL);
  170. for (i = 0; i < p->nr; i++) {
  171. struct ref_list_entry *e = p->list + i;
  172. struct object *o = parse_object(r, &e->oid);
  173. if (o) {
  174. o->flags |= PREREQ_MARK;
  175. add_pending_object(&revs, o, e->name);
  176. continue;
  177. }
  178. if (++ret == 1)
  179. error("%s", message);
  180. error("%s %s", oid_to_hex(&e->oid), e->name);
  181. }
  182. if (revs.pending.nr != p->nr)
  183. return ret;
  184. req_nr = revs.pending.nr;
  185. setup_revisions(2, argv, &revs, NULL);
  186. if (prepare_revision_walk(&revs))
  187. die(_("revision walk setup failed"));
  188. i = req_nr;
  189. while (i && (commit = get_revision(&revs)))
  190. if (commit->object.flags & PREREQ_MARK)
  191. i--;
  192. for (i = 0; i < p->nr; i++) {
  193. struct ref_list_entry *e = p->list + i;
  194. struct object *o = parse_object(r, &e->oid);
  195. assert(o); /* otherwise we'd have returned early */
  196. if (o->flags & SHOWN)
  197. continue;
  198. if (++ret == 1)
  199. error("%s", message);
  200. error("%s %s", oid_to_hex(&e->oid), e->name);
  201. }
  202. /* Clean up objects used, as they will be reused. */
  203. for (i = 0; i < p->nr; i++) {
  204. struct ref_list_entry *e = p->list + i;
  205. commit = lookup_commit_reference_gently(r, &e->oid, 1);
  206. if (commit)
  207. clear_commit_marks(commit, ALL_REV_FLAGS);
  208. }
  209. if (verbose) {
  210. struct ref_list *r;
  211. r = &header->references;
  212. printf_ln(Q_("The bundle contains this ref:",
  213. "The bundle contains these %d refs:",
  214. r->nr),
  215. r->nr);
  216. list_refs(r, 0, NULL);
  217. r = &header->prerequisites;
  218. if (!r->nr) {
  219. printf_ln(_("The bundle records a complete history."));
  220. } else {
  221. printf_ln(Q_("The bundle requires this ref:",
  222. "The bundle requires these %d refs:",
  223. r->nr),
  224. r->nr);
  225. list_refs(r, 0, NULL);
  226. }
  227. }
  228. return ret;
  229. }
  230. int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
  231. {
  232. return list_refs(&header->references, argc, argv);
  233. }
  234. static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
  235. {
  236. unsigned long size;
  237. enum object_type type;
  238. char *buf = NULL, *line, *lineend;
  239. timestamp_t date;
  240. int result = 1;
  241. if (revs->max_age == -1 && revs->min_age == -1)
  242. goto out;
  243. buf = read_object_file(&tag->oid, &type, &size);
  244. if (!buf)
  245. goto out;
  246. line = memmem(buf, size, "\ntagger ", 8);
  247. if (!line++)
  248. goto out;
  249. lineend = memchr(line, '\n', buf + size - line);
  250. line = memchr(line, '>', lineend ? lineend - line : buf + size - line);
  251. if (!line++)
  252. goto out;
  253. date = parse_timestamp(line, NULL, 10);
  254. result = (revs->max_age == -1 || revs->max_age < date) &&
  255. (revs->min_age == -1 || revs->min_age > date);
  256. out:
  257. free(buf);
  258. return result;
  259. }
  260. /* Write the pack data to bundle_fd */
  261. static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options)
  262. {
  263. struct child_process pack_objects = CHILD_PROCESS_INIT;
  264. int i;
  265. strvec_pushl(&pack_objects.args,
  266. "pack-objects",
  267. "--stdout", "--thin", "--delta-base-offset",
  268. NULL);
  269. strvec_pushv(&pack_objects.args, pack_options->v);
  270. pack_objects.in = -1;
  271. pack_objects.out = bundle_fd;
  272. pack_objects.git_cmd = 1;
  273. /*
  274. * start_command() will close our descriptor if it's >1. Duplicate it
  275. * to avoid surprising the caller.
  276. */
  277. if (pack_objects.out > 1) {
  278. pack_objects.out = dup(pack_objects.out);
  279. if (pack_objects.out < 0) {
  280. error_errno(_("unable to dup bundle descriptor"));
  281. child_process_clear(&pack_objects);
  282. return -1;
  283. }
  284. }
  285. if (start_command(&pack_objects))
  286. return error(_("Could not spawn pack-objects"));
  287. for (i = 0; i < revs->pending.nr; i++) {
  288. struct object *object = revs->pending.objects[i].item;
  289. if (object->flags & UNINTERESTING)
  290. write_or_die(pack_objects.in, "^", 1);
  291. write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
  292. write_or_die(pack_objects.in, "\n", 1);
  293. }
  294. close(pack_objects.in);
  295. if (finish_command(&pack_objects))
  296. return error(_("pack-objects died"));
  297. return 0;
  298. }
  299. static int compute_and_write_prerequisites(int bundle_fd,
  300. struct rev_info *revs,
  301. int argc, const char **argv)
  302. {
  303. struct child_process rls = CHILD_PROCESS_INIT;
  304. struct strbuf buf = STRBUF_INIT;
  305. FILE *rls_fout;
  306. int i;
  307. strvec_pushl(&rls.args,
  308. "rev-list", "--boundary", "--pretty=oneline",
  309. NULL);
  310. for (i = 1; i < argc; i++)
  311. strvec_push(&rls.args, argv[i]);
  312. rls.out = -1;
  313. rls.git_cmd = 1;
  314. if (start_command(&rls))
  315. return -1;
  316. rls_fout = xfdopen(rls.out, "r");
  317. while (strbuf_getwholeline(&buf, rls_fout, '\n') != EOF) {
  318. struct object_id oid;
  319. if (buf.len > 0 && buf.buf[0] == '-') {
  320. write_or_die(bundle_fd, buf.buf, buf.len);
  321. if (!get_oid_hex(buf.buf + 1, &oid)) {
  322. struct object *object = parse_object_or_die(&oid,
  323. buf.buf);
  324. object->flags |= UNINTERESTING;
  325. add_pending_object(revs, object, buf.buf);
  326. }
  327. } else if (!get_oid_hex(buf.buf, &oid)) {
  328. struct object *object = parse_object_or_die(&oid,
  329. buf.buf);
  330. object->flags |= SHOWN;
  331. }
  332. }
  333. strbuf_release(&buf);
  334. fclose(rls_fout);
  335. if (finish_command(&rls))
  336. return error(_("rev-list died"));
  337. return 0;
  338. }
  339. /*
  340. * Write out bundle refs based on the tips already
  341. * parsed into revs.pending. As a side effect, may
  342. * manipulate revs.pending to include additional
  343. * necessary objects (like tags).
  344. *
  345. * Returns the number of refs written, or negative
  346. * on error.
  347. */
  348. static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
  349. {
  350. int i;
  351. int ref_count = 0;
  352. for (i = 0; i < revs->pending.nr; i++) {
  353. struct object_array_entry *e = revs->pending.objects + i;
  354. struct object_id oid;
  355. char *ref;
  356. const char *display_ref;
  357. int flag;
  358. if (e->item->flags & UNINTERESTING)
  359. continue;
  360. if (dwim_ref(e->name, strlen(e->name), &oid, &ref, 0) != 1)
  361. goto skip_write_ref;
  362. if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag))
  363. flag = 0;
  364. display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
  365. if (e->item->type == OBJ_TAG &&
  366. !is_tag_in_date_range(e->item, revs)) {
  367. e->item->flags |= UNINTERESTING;
  368. goto skip_write_ref;
  369. }
  370. /*
  371. * Make sure the refs we wrote out is correct; --max-count and
  372. * other limiting options could have prevented all the tips
  373. * from getting output.
  374. *
  375. * Non commit objects such as tags and blobs do not have
  376. * this issue as they are not affected by those extra
  377. * constraints.
  378. */
  379. if (!(e->item->flags & SHOWN) && e->item->type == OBJ_COMMIT) {
  380. warning(_("ref '%s' is excluded by the rev-list options"),
  381. e->name);
  382. goto skip_write_ref;
  383. }
  384. /*
  385. * If you run "git bundle create bndl v1.0..v2.0", the
  386. * name of the positive ref is "v2.0" but that is the
  387. * commit that is referenced by the tag, and not the tag
  388. * itself.
  389. */
  390. if (!oideq(&oid, &e->item->oid)) {
  391. /*
  392. * Is this the positive end of a range expressed
  393. * in terms of a tag (e.g. v2.0 from the range
  394. * "v1.0..v2.0")?
  395. */
  396. struct commit *one = lookup_commit_reference(revs->repo, &oid);
  397. struct object *obj;
  398. if (e->item == &(one->object)) {
  399. /*
  400. * Need to include e->name as an
  401. * independent ref to the pack-objects
  402. * input, so that the tag is included
  403. * in the output; otherwise we would
  404. * end up triggering "empty bundle"
  405. * error.
  406. */
  407. obj = parse_object_or_die(&oid, e->name);
  408. obj->flags |= SHOWN;
  409. add_pending_object(revs, obj, e->name);
  410. }
  411. goto skip_write_ref;
  412. }
  413. ref_count++;
  414. write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
  415. write_or_die(bundle_fd, " ", 1);
  416. write_or_die(bundle_fd, display_ref, strlen(display_ref));
  417. write_or_die(bundle_fd, "\n", 1);
  418. skip_write_ref:
  419. free(ref);
  420. }
  421. /* end header */
  422. write_or_die(bundle_fd, "\n", 1);
  423. return ref_count;
  424. }
  425. int create_bundle(struct repository *r, const char *path,
  426. int argc, const char **argv, struct strvec *pack_options, int version)
  427. {
  428. struct lock_file lock = LOCK_INIT;
  429. int bundle_fd = -1;
  430. int bundle_to_stdout;
  431. int ref_count = 0;
  432. struct rev_info revs;
  433. int min_version = the_hash_algo == &hash_algos[GIT_HASH_SHA1] ? 2 : 3;
  434. bundle_to_stdout = !strcmp(path, "-");
  435. if (bundle_to_stdout)
  436. bundle_fd = 1;
  437. else
  438. bundle_fd = hold_lock_file_for_update(&lock, path,
  439. LOCK_DIE_ON_ERROR);
  440. if (version == -1)
  441. version = min_version;
  442. if (version < 2 || version > 3) {
  443. die(_("unsupported bundle version %d"), version);
  444. } else if (version < min_version) {
  445. die(_("cannot write bundle version %d with algorithm %s"), version, the_hash_algo->name);
  446. } else if (version == 2) {
  447. write_or_die(bundle_fd, v2_bundle_signature, strlen(v2_bundle_signature));
  448. } else {
  449. const char *capability = "@object-format=";
  450. write_or_die(bundle_fd, v3_bundle_signature, strlen(v3_bundle_signature));
  451. write_or_die(bundle_fd, capability, strlen(capability));
  452. write_or_die(bundle_fd, the_hash_algo->name, strlen(the_hash_algo->name));
  453. write_or_die(bundle_fd, "\n", 1);
  454. }
  455. /* init revs to list objects for pack-objects later */
  456. save_commit_buffer = 0;
  457. repo_init_revisions(r, &revs, NULL);
  458. /* write prerequisites */
  459. if (compute_and_write_prerequisites(bundle_fd, &revs, argc, argv))
  460. goto err;
  461. argc = setup_revisions(argc, argv, &revs, NULL);
  462. if (argc > 1) {
  463. error(_("unrecognized argument: %s"), argv[1]);
  464. goto err;
  465. }
  466. object_array_remove_duplicates(&revs.pending);
  467. ref_count = write_bundle_refs(bundle_fd, &revs);
  468. if (!ref_count)
  469. die(_("Refusing to create empty bundle."));
  470. else if (ref_count < 0)
  471. goto err;
  472. /* write pack */
  473. if (write_pack_data(bundle_fd, &revs, pack_options))
  474. goto err;
  475. if (!bundle_to_stdout) {
  476. if (commit_lock_file(&lock))
  477. die_errno(_("cannot create '%s'"), path);
  478. }
  479. return 0;
  480. err:
  481. rollback_lock_file(&lock);
  482. return -1;
  483. }
  484. int unbundle(struct repository *r, struct bundle_header *header,
  485. int bundle_fd, int flags)
  486. {
  487. const char *argv_index_pack[] = {"index-pack",
  488. "--fix-thin", "--stdin", NULL, NULL};
  489. struct child_process ip = CHILD_PROCESS_INIT;
  490. if (flags & BUNDLE_VERBOSE)
  491. argv_index_pack[3] = "-v";
  492. if (verify_bundle(r, header, 0))
  493. return -1;
  494. ip.argv = argv_index_pack;
  495. ip.in = bundle_fd;
  496. ip.no_stdout = 1;
  497. ip.git_cmd = 1;
  498. if (run_command(&ip))
  499. return error(_("index-pack died"));
  500. return 0;
  501. }