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.

1754 lines
48KB

  1. #include "cache.h"
  2. #include "repository.h"
  3. #include "config.h"
  4. #include "lockfile.h"
  5. #include "refs.h"
  6. #include "pkt-line.h"
  7. #include "commit.h"
  8. #include "tag.h"
  9. #include "exec-cmd.h"
  10. #include "pack.h"
  11. #include "sideband.h"
  12. #include "fetch-pack.h"
  13. #include "remote.h"
  14. #include "run-command.h"
  15. #include "connect.h"
  16. #include "transport.h"
  17. #include "version.h"
  18. #include "sha1-array.h"
  19. #include "oidset.h"
  20. #include "packfile.h"
  21. #include "object-store.h"
  22. #include "connected.h"
  23. #include "fetch-negotiator.h"
  24. #include "fsck.h"
  25. static int transfer_unpack_limit = -1;
  26. static int fetch_unpack_limit = -1;
  27. static int unpack_limit = 100;
  28. static int prefer_ofs_delta = 1;
  29. static int no_done;
  30. static int deepen_since_ok;
  31. static int deepen_not_ok;
  32. static int fetch_fsck_objects = -1;
  33. static int transfer_fsck_objects = -1;
  34. static int agent_supported;
  35. static int server_supports_filtering;
  36. static struct lock_file shallow_lock;
  37. static const char *alternate_shallow_file;
  38. static struct strbuf fsck_msg_types = STRBUF_INIT;
  39. /* Remember to update object flag allocation in object.h */
  40. #define COMPLETE (1U << 0)
  41. #define ALTERNATE (1U << 1)
  42. /*
  43. * After sending this many "have"s if we do not get any new ACK , we
  44. * give up traversing our history.
  45. */
  46. #define MAX_IN_VAIN 256
  47. static int multi_ack, use_sideband;
  48. /* Allow specifying sha1 if it is a ref tip. */
  49. #define ALLOW_TIP_SHA1 01
  50. /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
  51. #define ALLOW_REACHABLE_SHA1 02
  52. static unsigned int allow_unadvertised_object_request;
  53. __attribute__((format (printf, 2, 3)))
  54. static inline void print_verbose(const struct fetch_pack_args *args,
  55. const char *fmt, ...)
  56. {
  57. va_list params;
  58. if (!args->verbose)
  59. return;
  60. va_start(params, fmt);
  61. vfprintf(stderr, fmt, params);
  62. va_end(params);
  63. fputc('\n', stderr);
  64. }
  65. struct alternate_object_cache {
  66. struct object **items;
  67. size_t nr, alloc;
  68. };
  69. static void cache_one_alternate(const struct object_id *oid,
  70. void *vcache)
  71. {
  72. struct alternate_object_cache *cache = vcache;
  73. struct object *obj = parse_object(the_repository, oid);
  74. if (!obj || (obj->flags & ALTERNATE))
  75. return;
  76. obj->flags |= ALTERNATE;
  77. ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
  78. cache->items[cache->nr++] = obj;
  79. }
  80. static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
  81. void (*cb)(struct fetch_negotiator *,
  82. struct object *))
  83. {
  84. static int initialized;
  85. static struct alternate_object_cache cache;
  86. size_t i;
  87. if (!initialized) {
  88. for_each_alternate_ref(cache_one_alternate, &cache);
  89. initialized = 1;
  90. }
  91. for (i = 0; i < cache.nr; i++)
  92. cb(negotiator, cache.items[i]);
  93. }
  94. static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
  95. const char *refname,
  96. const struct object_id *oid)
  97. {
  98. struct object *o = deref_tag(the_repository,
  99. parse_object(the_repository, oid),
  100. refname, 0);
  101. if (o && o->type == OBJ_COMMIT)
  102. negotiator->add_tip(negotiator, (struct commit *)o);
  103. return 0;
  104. }
  105. static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
  106. int flag, void *cb_data)
  107. {
  108. return rev_list_insert_ref(cb_data, refname, oid);
  109. }
  110. enum ack_type {
  111. NAK = 0,
  112. ACK,
  113. ACK_continue,
  114. ACK_common,
  115. ACK_ready
  116. };
  117. static void consume_shallow_list(struct fetch_pack_args *args,
  118. struct packet_reader *reader)
  119. {
  120. if (args->stateless_rpc && args->deepen) {
  121. /* If we sent a depth we will get back "duplicate"
  122. * shallow and unshallow commands every time there
  123. * is a block of have lines exchanged.
  124. */
  125. while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
  126. if (starts_with(reader->line, "shallow "))
  127. continue;
  128. if (starts_with(reader->line, "unshallow "))
  129. continue;
  130. die(_("git fetch-pack: expected shallow list"));
  131. }
  132. if (reader->status != PACKET_READ_FLUSH)
  133. die(_("git fetch-pack: expected a flush packet after shallow list"));
  134. }
  135. }
  136. static enum ack_type get_ack(struct packet_reader *reader,
  137. struct object_id *result_oid)
  138. {
  139. int len;
  140. const char *arg;
  141. if (packet_reader_read(reader) != PACKET_READ_NORMAL)
  142. die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
  143. len = reader->pktlen;
  144. if (!strcmp(reader->line, "NAK"))
  145. return NAK;
  146. if (skip_prefix(reader->line, "ACK ", &arg)) {
  147. const char *p;
  148. if (!parse_oid_hex(arg, result_oid, &p)) {
  149. len -= p - reader->line;
  150. if (len < 1)
  151. return ACK;
  152. if (strstr(p, "continue"))
  153. return ACK_continue;
  154. if (strstr(p, "common"))
  155. return ACK_common;
  156. if (strstr(p, "ready"))
  157. return ACK_ready;
  158. return ACK;
  159. }
  160. }
  161. die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
  162. }
  163. static void send_request(struct fetch_pack_args *args,
  164. int fd, struct strbuf *buf)
  165. {
  166. if (args->stateless_rpc) {
  167. send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
  168. packet_flush(fd);
  169. } else {
  170. if (write_in_full(fd, buf->buf, buf->len) < 0)
  171. die_errno(_("unable to write to remote"));
  172. }
  173. }
  174. static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
  175. struct object *obj)
  176. {
  177. rev_list_insert_ref(negotiator, NULL, &obj->oid);
  178. }
  179. #define INITIAL_FLUSH 16
  180. #define PIPESAFE_FLUSH 32
  181. #define LARGE_FLUSH 16384
  182. static int next_flush(int stateless_rpc, int count)
  183. {
  184. if (stateless_rpc) {
  185. if (count < LARGE_FLUSH)
  186. count <<= 1;
  187. else
  188. count = count * 11 / 10;
  189. } else {
  190. if (count < PIPESAFE_FLUSH)
  191. count <<= 1;
  192. else
  193. count += PIPESAFE_FLUSH;
  194. }
  195. return count;
  196. }
  197. static void mark_tips(struct fetch_negotiator *negotiator,
  198. const struct oid_array *negotiation_tips)
  199. {
  200. int i;
  201. if (!negotiation_tips) {
  202. for_each_ref(rev_list_insert_ref_oid, negotiator);
  203. return;
  204. }
  205. for (i = 0; i < negotiation_tips->nr; i++)
  206. rev_list_insert_ref(negotiator, NULL,
  207. &negotiation_tips->oid[i]);
  208. return;
  209. }
  210. static int find_common(struct fetch_negotiator *negotiator,
  211. struct fetch_pack_args *args,
  212. int fd[2], struct object_id *result_oid,
  213. struct ref *refs)
  214. {
  215. int fetching;
  216. int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
  217. const struct object_id *oid;
  218. unsigned in_vain = 0;
  219. int got_continue = 0;
  220. int got_ready = 0;
  221. struct strbuf req_buf = STRBUF_INIT;
  222. size_t state_len = 0;
  223. struct packet_reader reader;
  224. if (args->stateless_rpc && multi_ack == 1)
  225. die(_("--stateless-rpc requires multi_ack_detailed"));
  226. packet_reader_init(&reader, fd[0], NULL, 0,
  227. PACKET_READ_CHOMP_NEWLINE |
  228. PACKET_READ_DIE_ON_ERR_PACKET);
  229. if (!args->no_dependents) {
  230. mark_tips(negotiator, args->negotiation_tips);
  231. for_each_cached_alternate(negotiator, insert_one_alternate_object);
  232. }
  233. fetching = 0;
  234. for ( ; refs ; refs = refs->next) {
  235. struct object_id *remote = &refs->old_oid;
  236. const char *remote_hex;
  237. struct object *o;
  238. /*
  239. * If that object is complete (i.e. it is an ancestor of a
  240. * local ref), we tell them we have it but do not have to
  241. * tell them about its ancestors, which they already know
  242. * about.
  243. *
  244. * We use lookup_object here because we are only
  245. * interested in the case we *know* the object is
  246. * reachable and we have already scanned it.
  247. *
  248. * Do this only if args->no_dependents is false (if it is true,
  249. * we cannot trust the object flags).
  250. */
  251. if (!args->no_dependents &&
  252. ((o = lookup_object(the_repository, remote)) != NULL) &&
  253. (o->flags & COMPLETE)) {
  254. continue;
  255. }
  256. remote_hex = oid_to_hex(remote);
  257. if (!fetching) {
  258. struct strbuf c = STRBUF_INIT;
  259. if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
  260. if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
  261. if (no_done) strbuf_addstr(&c, " no-done");
  262. if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
  263. if (use_sideband == 1) strbuf_addstr(&c, " side-band");
  264. if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
  265. if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
  266. if (args->no_progress) strbuf_addstr(&c, " no-progress");
  267. if (args->include_tag) strbuf_addstr(&c, " include-tag");
  268. if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
  269. if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
  270. if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
  271. if (agent_supported) strbuf_addf(&c, " agent=%s",
  272. git_user_agent_sanitized());
  273. if (args->filter_options.choice)
  274. strbuf_addstr(&c, " filter");
  275. packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
  276. strbuf_release(&c);
  277. } else
  278. packet_buf_write(&req_buf, "want %s\n", remote_hex);
  279. fetching++;
  280. }
  281. if (!fetching) {
  282. strbuf_release(&req_buf);
  283. packet_flush(fd[1]);
  284. return 1;
  285. }
  286. if (is_repository_shallow(the_repository))
  287. write_shallow_commits(&req_buf, 1, NULL);
  288. if (args->depth > 0)
  289. packet_buf_write(&req_buf, "deepen %d", args->depth);
  290. if (args->deepen_since) {
  291. timestamp_t max_age = approxidate(args->deepen_since);
  292. packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
  293. }
  294. if (args->deepen_not) {
  295. int i;
  296. for (i = 0; i < args->deepen_not->nr; i++) {
  297. struct string_list_item *s = args->deepen_not->items + i;
  298. packet_buf_write(&req_buf, "deepen-not %s", s->string);
  299. }
  300. }
  301. if (server_supports_filtering && args->filter_options.choice) {
  302. const char *spec =
  303. expand_list_objects_filter_spec(&args->filter_options);
  304. packet_buf_write(&req_buf, "filter %s", spec);
  305. }
  306. packet_buf_flush(&req_buf);
  307. state_len = req_buf.len;
  308. if (args->deepen) {
  309. const char *arg;
  310. struct object_id oid;
  311. send_request(args, fd[1], &req_buf);
  312. while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
  313. if (skip_prefix(reader.line, "shallow ", &arg)) {
  314. if (get_oid_hex(arg, &oid))
  315. die(_("invalid shallow line: %s"), reader.line);
  316. register_shallow(the_repository, &oid);
  317. continue;
  318. }
  319. if (skip_prefix(reader.line, "unshallow ", &arg)) {
  320. if (get_oid_hex(arg, &oid))
  321. die(_("invalid unshallow line: %s"), reader.line);
  322. if (!lookup_object(the_repository, &oid))
  323. die(_("object not found: %s"), reader.line);
  324. /* make sure that it is parsed as shallow */
  325. if (!parse_object(the_repository, &oid))
  326. die(_("error in object: %s"), reader.line);
  327. if (unregister_shallow(&oid))
  328. die(_("no shallow found: %s"), reader.line);
  329. continue;
  330. }
  331. die(_("expected shallow/unshallow, got %s"), reader.line);
  332. }
  333. } else if (!args->stateless_rpc)
  334. send_request(args, fd[1], &req_buf);
  335. if (!args->stateless_rpc) {
  336. /* If we aren't using the stateless-rpc interface
  337. * we don't need to retain the headers.
  338. */
  339. strbuf_setlen(&req_buf, 0);
  340. state_len = 0;
  341. }
  342. trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
  343. flushes = 0;
  344. retval = -1;
  345. if (args->no_dependents)
  346. goto done;
  347. while ((oid = negotiator->next(negotiator))) {
  348. packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
  349. print_verbose(args, "have %s", oid_to_hex(oid));
  350. in_vain++;
  351. if (flush_at <= ++count) {
  352. int ack;
  353. packet_buf_flush(&req_buf);
  354. send_request(args, fd[1], &req_buf);
  355. strbuf_setlen(&req_buf, state_len);
  356. flushes++;
  357. flush_at = next_flush(args->stateless_rpc, count);
  358. /*
  359. * We keep one window "ahead" of the other side, and
  360. * will wait for an ACK only on the next one
  361. */
  362. if (!args->stateless_rpc && count == INITIAL_FLUSH)
  363. continue;
  364. consume_shallow_list(args, &reader);
  365. do {
  366. ack = get_ack(&reader, result_oid);
  367. if (ack)
  368. print_verbose(args, _("got %s %d %s"), "ack",
  369. ack, oid_to_hex(result_oid));
  370. switch (ack) {
  371. case ACK:
  372. flushes = 0;
  373. multi_ack = 0;
  374. retval = 0;
  375. goto done;
  376. case ACK_common:
  377. case ACK_ready:
  378. case ACK_continue: {
  379. struct commit *commit =
  380. lookup_commit(the_repository,
  381. result_oid);
  382. int was_common;
  383. if (!commit)
  384. die(_("invalid commit %s"), oid_to_hex(result_oid));
  385. was_common = negotiator->ack(negotiator, commit);
  386. if (args->stateless_rpc
  387. && ack == ACK_common
  388. && !was_common) {
  389. /* We need to replay the have for this object
  390. * on the next RPC request so the peer knows
  391. * it is in common with us.
  392. */
  393. const char *hex = oid_to_hex(result_oid);
  394. packet_buf_write(&req_buf, "have %s\n", hex);
  395. state_len = req_buf.len;
  396. /*
  397. * Reset in_vain because an ack
  398. * for this commit has not been
  399. * seen.
  400. */
  401. in_vain = 0;
  402. } else if (!args->stateless_rpc
  403. || ack != ACK_common)
  404. in_vain = 0;
  405. retval = 0;
  406. got_continue = 1;
  407. if (ack == ACK_ready)
  408. got_ready = 1;
  409. break;
  410. }
  411. }
  412. } while (ack);
  413. flushes--;
  414. if (got_continue && MAX_IN_VAIN < in_vain) {
  415. print_verbose(args, _("giving up"));
  416. break; /* give up */
  417. }
  418. if (got_ready)
  419. break;
  420. }
  421. }
  422. done:
  423. trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
  424. if (!got_ready || !no_done) {
  425. packet_buf_write(&req_buf, "done\n");
  426. send_request(args, fd[1], &req_buf);
  427. }
  428. print_verbose(args, _("done"));
  429. if (retval != 0) {
  430. multi_ack = 0;
  431. flushes++;
  432. }
  433. strbuf_release(&req_buf);
  434. if (!got_ready || !no_done)
  435. consume_shallow_list(args, &reader);
  436. while (flushes || multi_ack) {
  437. int ack = get_ack(&reader, result_oid);
  438. if (ack) {
  439. print_verbose(args, _("got %s (%d) %s"), "ack",
  440. ack, oid_to_hex(result_oid));
  441. if (ack == ACK)
  442. return 0;
  443. multi_ack = 1;
  444. continue;
  445. }
  446. flushes--;
  447. }
  448. /* it is no error to fetch into a completely empty repo */
  449. return count ? retval : 0;
  450. }
  451. static struct commit_list *complete;
  452. static int mark_complete(const struct object_id *oid)
  453. {
  454. struct object *o = parse_object(the_repository, oid);
  455. while (o && o->type == OBJ_TAG) {
  456. struct tag *t = (struct tag *) o;
  457. if (!t->tagged)
  458. break; /* broken repository */
  459. o->flags |= COMPLETE;
  460. o = parse_object(the_repository, &t->tagged->oid);
  461. }
  462. if (o && o->type == OBJ_COMMIT) {
  463. struct commit *commit = (struct commit *)o;
  464. if (!(commit->object.flags & COMPLETE)) {
  465. commit->object.flags |= COMPLETE;
  466. commit_list_insert(commit, &complete);
  467. }
  468. }
  469. return 0;
  470. }
  471. static int mark_complete_oid(const char *refname, const struct object_id *oid,
  472. int flag, void *cb_data)
  473. {
  474. return mark_complete(oid);
  475. }
  476. static void mark_recent_complete_commits(struct fetch_pack_args *args,
  477. timestamp_t cutoff)
  478. {
  479. while (complete && cutoff <= complete->item->date) {
  480. print_verbose(args, _("Marking %s as complete"),
  481. oid_to_hex(&complete->item->object.oid));
  482. pop_most_recent_commit(&complete, COMPLETE);
  483. }
  484. }
  485. static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
  486. {
  487. for (; refs; refs = refs->next)
  488. oidset_insert(oids, &refs->old_oid);
  489. }
  490. static int is_unmatched_ref(const struct ref *ref)
  491. {
  492. struct object_id oid;
  493. const char *p;
  494. return ref->match_status == REF_NOT_MATCHED &&
  495. !parse_oid_hex(ref->name, &oid, &p) &&
  496. *p == '\0' &&
  497. oideq(&oid, &ref->old_oid);
  498. }
  499. static void filter_refs(struct fetch_pack_args *args,
  500. struct ref **refs,
  501. struct ref **sought, int nr_sought)
  502. {
  503. struct ref *newlist = NULL;
  504. struct ref **newtail = &newlist;
  505. struct ref *unmatched = NULL;
  506. struct ref *ref, *next;
  507. struct oidset tip_oids = OIDSET_INIT;
  508. int i;
  509. int strict = !(allow_unadvertised_object_request &
  510. (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
  511. i = 0;
  512. for (ref = *refs; ref; ref = next) {
  513. int keep = 0;
  514. next = ref->next;
  515. if (starts_with(ref->name, "refs/") &&
  516. check_refname_format(ref->name, 0)) {
  517. /*
  518. * trash or a peeled value; do not even add it to
  519. * unmatched list
  520. */
  521. free_one_ref(ref);
  522. continue;
  523. } else {
  524. while (i < nr_sought) {
  525. int cmp = strcmp(ref->name, sought[i]->name);
  526. if (cmp < 0)
  527. break; /* definitely do not have it */
  528. else if (cmp == 0) {
  529. keep = 1; /* definitely have it */
  530. sought[i]->match_status = REF_MATCHED;
  531. }
  532. i++;
  533. }
  534. if (!keep && args->fetch_all &&
  535. (!args->deepen || !starts_with(ref->name, "refs/tags/")))
  536. keep = 1;
  537. }
  538. if (keep) {
  539. *newtail = ref;
  540. ref->next = NULL;
  541. newtail = &ref->next;
  542. } else {
  543. ref->next = unmatched;
  544. unmatched = ref;
  545. }
  546. }
  547. if (strict) {
  548. for (i = 0; i < nr_sought; i++) {
  549. ref = sought[i];
  550. if (!is_unmatched_ref(ref))
  551. continue;
  552. add_refs_to_oidset(&tip_oids, unmatched);
  553. add_refs_to_oidset(&tip_oids, newlist);
  554. break;
  555. }
  556. }
  557. /* Append unmatched requests to the list */
  558. for (i = 0; i < nr_sought; i++) {
  559. ref = sought[i];
  560. if (!is_unmatched_ref(ref))
  561. continue;
  562. if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
  563. ref->match_status = REF_MATCHED;
  564. *newtail = copy_ref(ref);
  565. newtail = &(*newtail)->next;
  566. } else {
  567. ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
  568. }
  569. }
  570. oidset_clear(&tip_oids);
  571. free_refs(unmatched);
  572. *refs = newlist;
  573. }
  574. static void mark_alternate_complete(struct fetch_negotiator *unused,
  575. struct object *obj)
  576. {
  577. mark_complete(&obj->oid);
  578. }
  579. struct loose_object_iter {
  580. struct oidset *loose_object_set;
  581. struct ref *refs;
  582. };
  583. /*
  584. * Mark recent commits available locally and reachable from a local ref as
  585. * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
  586. * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
  587. * thus do not need COMMON_REF marks).
  588. *
  589. * The cutoff time for recency is determined by this heuristic: it is the
  590. * earliest commit time of the objects in refs that are commits and that we know
  591. * the commit time of.
  592. */
  593. static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
  594. struct fetch_pack_args *args,
  595. struct ref **refs)
  596. {
  597. struct ref *ref;
  598. int old_save_commit_buffer = save_commit_buffer;
  599. timestamp_t cutoff = 0;
  600. save_commit_buffer = 0;
  601. for (ref = *refs; ref; ref = ref->next) {
  602. struct object *o;
  603. if (!has_object_file_with_flags(&ref->old_oid,
  604. OBJECT_INFO_QUICK))
  605. continue;
  606. o = parse_object(the_repository, &ref->old_oid);
  607. if (!o)
  608. continue;
  609. /* We already have it -- which may mean that we were
  610. * in sync with the other side at some time after
  611. * that (it is OK if we guess wrong here).
  612. */
  613. if (o->type == OBJ_COMMIT) {
  614. struct commit *commit = (struct commit *)o;
  615. if (!cutoff || cutoff < commit->date)
  616. cutoff = commit->date;
  617. }
  618. }
  619. if (!args->deepen) {
  620. for_each_ref(mark_complete_oid, NULL);
  621. for_each_cached_alternate(NULL, mark_alternate_complete);
  622. commit_list_sort_by_date(&complete);
  623. if (cutoff)
  624. mark_recent_complete_commits(args, cutoff);
  625. }
  626. /*
  627. * Mark all complete remote refs as common refs.
  628. * Don't mark them common yet; the server has to be told so first.
  629. */
  630. for (ref = *refs; ref; ref = ref->next) {
  631. struct object *o = deref_tag(the_repository,
  632. lookup_object(the_repository,
  633. &ref->old_oid),
  634. NULL, 0);
  635. if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
  636. continue;
  637. negotiator->known_common(negotiator,
  638. (struct commit *)o);
  639. }
  640. save_commit_buffer = old_save_commit_buffer;
  641. }
  642. /*
  643. * Returns 1 if every object pointed to by the given remote refs is available
  644. * locally and reachable from a local ref, and 0 otherwise.
  645. */
  646. static int everything_local(struct fetch_pack_args *args,
  647. struct ref **refs)
  648. {
  649. struct ref *ref;
  650. int retval;
  651. for (retval = 1, ref = *refs; ref ; ref = ref->next) {
  652. const struct object_id *remote = &ref->old_oid;
  653. struct object *o;
  654. o = lookup_object(the_repository, remote);
  655. if (!o || !(o->flags & COMPLETE)) {
  656. retval = 0;
  657. print_verbose(args, "want %s (%s)", oid_to_hex(remote),
  658. ref->name);
  659. continue;
  660. }
  661. print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
  662. ref->name);
  663. }
  664. return retval;
  665. }
  666. static int sideband_demux(int in, int out, void *data)
  667. {
  668. int *xd = data;
  669. int ret;
  670. ret = recv_sideband("fetch-pack", xd[0], out);
  671. close(out);
  672. return ret;
  673. }
  674. static int get_pack(struct fetch_pack_args *args,
  675. int xd[2], char **pack_lockfile)
  676. {
  677. struct async demux;
  678. int do_keep = args->keep_pack;
  679. const char *cmd_name;
  680. struct pack_header header;
  681. int pass_header = 0;
  682. struct child_process cmd = CHILD_PROCESS_INIT;
  683. int ret;
  684. memset(&demux, 0, sizeof(demux));
  685. if (use_sideband) {
  686. /* xd[] is talking with upload-pack; subprocess reads from
  687. * xd[0], spits out band#2 to stderr, and feeds us band#1
  688. * through demux->out.
  689. */
  690. demux.proc = sideband_demux;
  691. demux.data = xd;
  692. demux.out = -1;
  693. demux.isolate_sigpipe = 1;
  694. if (start_async(&demux))
  695. die(_("fetch-pack: unable to fork off sideband demultiplexer"));
  696. }
  697. else
  698. demux.out = xd[0];
  699. if (!args->keep_pack && unpack_limit) {
  700. if (read_pack_header(demux.out, &header))
  701. die(_("protocol error: bad pack header"));
  702. pass_header = 1;
  703. if (ntohl(header.hdr_entries) < unpack_limit)
  704. do_keep = 0;
  705. else
  706. do_keep = 1;
  707. }
  708. if (alternate_shallow_file) {
  709. argv_array_push(&cmd.args, "--shallow-file");
  710. argv_array_push(&cmd.args, alternate_shallow_file);
  711. }
  712. if (do_keep || args->from_promisor) {
  713. if (pack_lockfile)
  714. cmd.out = -1;
  715. cmd_name = "index-pack";
  716. argv_array_push(&cmd.args, cmd_name);
  717. argv_array_push(&cmd.args, "--stdin");
  718. if (!args->quiet && !args->no_progress)
  719. argv_array_push(&cmd.args, "-v");
  720. if (args->use_thin_pack)
  721. argv_array_push(&cmd.args, "--fix-thin");
  722. if (do_keep && (args->lock_pack || unpack_limit)) {
  723. char hostname[HOST_NAME_MAX + 1];
  724. if (xgethostname(hostname, sizeof(hostname)))
  725. xsnprintf(hostname, sizeof(hostname), "localhost");
  726. argv_array_pushf(&cmd.args,
  727. "--keep=fetch-pack %"PRIuMAX " on %s",
  728. (uintmax_t)getpid(), hostname);
  729. }
  730. if (args->check_self_contained_and_connected)
  731. argv_array_push(&cmd.args, "--check-self-contained-and-connected");
  732. if (args->from_promisor)
  733. argv_array_push(&cmd.args, "--promisor");
  734. }
  735. else {
  736. cmd_name = "unpack-objects";
  737. argv_array_push(&cmd.args, cmd_name);
  738. if (args->quiet || args->no_progress)
  739. argv_array_push(&cmd.args, "-q");
  740. args->check_self_contained_and_connected = 0;
  741. }
  742. if (pass_header)
  743. argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
  744. ntohl(header.hdr_version),
  745. ntohl(header.hdr_entries));
  746. if (fetch_fsck_objects >= 0
  747. ? fetch_fsck_objects
  748. : transfer_fsck_objects >= 0
  749. ? transfer_fsck_objects
  750. : 0) {
  751. if (args->from_promisor)
  752. /*
  753. * We cannot use --strict in index-pack because it
  754. * checks both broken objects and links, but we only
  755. * want to check for broken objects.
  756. */
  757. argv_array_push(&cmd.args, "--fsck-objects");
  758. else
  759. argv_array_pushf(&cmd.args, "--strict%s",
  760. fsck_msg_types.buf);
  761. }
  762. cmd.in = demux.out;
  763. cmd.git_cmd = 1;
  764. if (start_command(&cmd))
  765. die(_("fetch-pack: unable to fork off %s"), cmd_name);
  766. if (do_keep && pack_lockfile) {
  767. *pack_lockfile = index_pack_lockfile(cmd.out);
  768. close(cmd.out);
  769. }
  770. if (!use_sideband)
  771. /* Closed by start_command() */
  772. xd[0] = -1;
  773. ret = finish_command(&cmd);
  774. if (!ret || (args->check_self_contained_and_connected && ret == 1))
  775. args->self_contained_and_connected =
  776. args->check_self_contained_and_connected &&
  777. ret == 0;
  778. else
  779. die(_("%s failed"), cmd_name);
  780. if (use_sideband && finish_async(&demux))
  781. die(_("error in sideband demultiplexer"));
  782. return 0;
  783. }
  784. static int cmp_ref_by_name(const void *a_, const void *b_)
  785. {
  786. const struct ref *a = *((const struct ref **)a_);
  787. const struct ref *b = *((const struct ref **)b_);
  788. return strcmp(a->name, b->name);
  789. }
  790. static struct ref *do_fetch_pack(struct fetch_pack_args *args,
  791. int fd[2],
  792. const struct ref *orig_ref,
  793. struct ref **sought, int nr_sought,
  794. struct shallow_info *si,
  795. char **pack_lockfile)
  796. {
  797. struct repository *r = the_repository;
  798. struct ref *ref = copy_ref_list(orig_ref);
  799. struct object_id oid;
  800. const char *agent_feature;
  801. int agent_len;
  802. struct fetch_negotiator negotiator;
  803. fetch_negotiator_init(r, &negotiator);
  804. sort_ref_list(&ref, ref_compare_name);
  805. QSORT(sought, nr_sought, cmp_ref_by_name);
  806. if ((agent_feature = server_feature_value("agent", &agent_len))) {
  807. agent_supported = 1;
  808. if (agent_len)
  809. print_verbose(args, _("Server version is %.*s"),
  810. agent_len, agent_feature);
  811. }
  812. if (server_supports("shallow"))
  813. print_verbose(args, _("Server supports %s"), "shallow");
  814. else if (args->depth > 0 || is_repository_shallow(r))
  815. die(_("Server does not support shallow clients"));
  816. if (args->depth > 0 || args->deepen_since || args->deepen_not)
  817. args->deepen = 1;
  818. if (server_supports("multi_ack_detailed")) {
  819. print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
  820. multi_ack = 2;
  821. if (server_supports("no-done")) {
  822. print_verbose(args, _("Server supports %s"), "no-done");
  823. if (args->stateless_rpc)
  824. no_done = 1;
  825. }
  826. }
  827. else if (server_supports("multi_ack")) {
  828. print_verbose(args, _("Server supports %s"), "multi_ack");
  829. multi_ack = 1;
  830. }
  831. if (server_supports("side-band-64k")) {
  832. print_verbose(args, _("Server supports %s"), "side-band-64k");
  833. use_sideband = 2;
  834. }
  835. else if (server_supports("side-band")) {
  836. print_verbose(args, _("Server supports %s"), "side-band");
  837. use_sideband = 1;
  838. }
  839. if (server_supports("allow-tip-sha1-in-want")) {
  840. print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
  841. allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
  842. }
  843. if (server_supports("allow-reachable-sha1-in-want")) {
  844. print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
  845. allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
  846. }
  847. if (server_supports("thin-pack"))
  848. print_verbose(args, _("Server supports %s"), "thin-pack");
  849. else
  850. args->use_thin_pack = 0;
  851. if (server_supports("no-progress"))
  852. print_verbose(args, _("Server supports %s"), "no-progress");
  853. else
  854. args->no_progress = 0;
  855. if (server_supports("include-tag"))
  856. print_verbose(args, _("Server supports %s"), "include-tag");
  857. else
  858. args->include_tag = 0;
  859. if (server_supports("ofs-delta"))
  860. print_verbose(args, _("Server supports %s"), "ofs-delta");
  861. else
  862. prefer_ofs_delta = 0;
  863. if (server_supports("filter")) {
  864. server_supports_filtering = 1;
  865. print_verbose(args, _("Server supports %s"), "filter");
  866. } else if (args->filter_options.choice) {
  867. warning("filtering not recognized by server, ignoring");
  868. }
  869. if (server_supports("deepen-since")) {
  870. print_verbose(args, _("Server supports %s"), "deepen-since");
  871. deepen_since_ok = 1;
  872. } else if (args->deepen_since)
  873. die(_("Server does not support --shallow-since"));
  874. if (server_supports("deepen-not")) {
  875. print_verbose(args, _("Server supports %s"), "deepen-not");
  876. deepen_not_ok = 1;
  877. } else if (args->deepen_not)
  878. die(_("Server does not support --shallow-exclude"));
  879. if (server_supports("deepen-relative"))
  880. print_verbose(args, _("Server supports %s"), "deepen-relative");
  881. else if (args->deepen_relative)
  882. die(_("Server does not support --deepen"));
  883. if (!args->no_dependents) {
  884. mark_complete_and_common_ref(&negotiator, args, &ref);
  885. filter_refs(args, &ref, sought, nr_sought);
  886. if (everything_local(args, &ref)) {
  887. packet_flush(fd[1]);
  888. goto all_done;
  889. }
  890. } else {
  891. filter_refs(args, &ref, sought, nr_sought);
  892. }
  893. if (find_common(&negotiator, args, fd, &oid, ref) < 0)
  894. if (!args->keep_pack)
  895. /* When cloning, it is not unusual to have
  896. * no common commit.
  897. */
  898. warning(_("no common commits"));
  899. if (args->stateless_rpc)
  900. packet_flush(fd[1]);
  901. if (args->deepen)
  902. setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
  903. NULL);
  904. else if (si->nr_ours || si->nr_theirs)
  905. alternate_shallow_file = setup_temporary_shallow(si->shallow);
  906. else
  907. alternate_shallow_file = NULL;
  908. if (get_pack(args, fd, pack_lockfile))
  909. die(_("git fetch-pack: fetch failed."));
  910. all_done:
  911. negotiator.release(&negotiator);
  912. return ref;
  913. }
  914. static void add_shallow_requests(struct strbuf *req_buf,
  915. const struct fetch_pack_args *args)
  916. {
  917. if (is_repository_shallow(the_repository))
  918. write_shallow_commits(req_buf, 1, NULL);
  919. if (args->depth > 0)
  920. packet_buf_write(req_buf, "deepen %d", args->depth);
  921. if (args->deepen_since) {
  922. timestamp_t max_age = approxidate(args->deepen_since);
  923. packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
  924. }
  925. if (args->deepen_not) {
  926. int i;
  927. for (i = 0; i < args->deepen_not->nr; i++) {
  928. struct string_list_item *s = args->deepen_not->items + i;
  929. packet_buf_write(req_buf, "deepen-not %s", s->string);
  930. }
  931. }
  932. if (args->deepen_relative)
  933. packet_buf_write(req_buf, "deepen-relative\n");
  934. }
  935. static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
  936. {
  937. int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
  938. for ( ; wants ; wants = wants->next) {
  939. const struct object_id *remote = &wants->old_oid;
  940. struct object *o;
  941. /*
  942. * If that object is complete (i.e. it is an ancestor of a
  943. * local ref), we tell them we have it but do not have to
  944. * tell them about its ancestors, which they already know
  945. * about.
  946. *
  947. * We use lookup_object here because we are only
  948. * interested in the case we *know* the object is
  949. * reachable and we have already scanned it.
  950. *
  951. * Do this only if args->no_dependents is false (if it is true,
  952. * we cannot trust the object flags).
  953. */
  954. if (!no_dependents &&
  955. ((o = lookup_object(the_repository, remote)) != NULL) &&
  956. (o->flags & COMPLETE)) {
  957. continue;
  958. }
  959. if (!use_ref_in_want || wants->exact_oid)
  960. packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
  961. else
  962. packet_buf_write(req_buf, "want-ref %s\n", wants->name);
  963. }
  964. }
  965. static void add_common(struct strbuf *req_buf, struct oidset *common)
  966. {
  967. struct oidset_iter iter;
  968. const struct object_id *oid;
  969. oidset_iter_init(common, &iter);
  970. while ((oid = oidset_iter_next(&iter))) {
  971. packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
  972. }
  973. }
  974. static int add_haves(struct fetch_negotiator *negotiator,
  975. struct strbuf *req_buf,
  976. int *haves_to_send, int *in_vain)
  977. {
  978. int ret = 0;
  979. int haves_added = 0;
  980. const struct object_id *oid;
  981. while ((oid = negotiator->next(negotiator))) {
  982. packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
  983. if (++haves_added >= *haves_to_send)
  984. break;
  985. }
  986. *in_vain += haves_added;
  987. if (!haves_added || *in_vain >= MAX_IN_VAIN) {
  988. /* Send Done */
  989. packet_buf_write(req_buf, "done\n");
  990. ret = 1;
  991. }
  992. /* Increase haves to send on next round */
  993. *haves_to_send = next_flush(1, *haves_to_send);
  994. return ret;
  995. }
  996. static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
  997. struct fetch_pack_args *args,
  998. const struct ref *wants, struct oidset *common,
  999. int *haves_to_send, int *in_vain,
  1000. int sideband_all)
  1001. {
  1002. int ret = 0;
  1003. struct strbuf req_buf = STRBUF_INIT;
  1004. if (server_supports_v2("fetch", 1))
  1005. packet_buf_write(&req_buf, "command=fetch");
  1006. if (server_supports_v2("agent", 0))
  1007. packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
  1008. if (args->server_options && args->server_options->nr &&
  1009. server_supports_v2("server-option", 1)) {
  1010. int i;
  1011. for (i = 0; i < args->server_options->nr; i++)
  1012. packet_buf_write(&req_buf, "server-option=%s",
  1013. args->server_options->items[i].string);
  1014. }
  1015. packet_buf_delim(&req_buf);
  1016. if (args->use_thin_pack)
  1017. packet_buf_write(&req_buf, "thin-pack");
  1018. if (args->no_progress)
  1019. packet_buf_write(&req_buf, "no-progress");
  1020. if (args->include_tag)
  1021. packet_buf_write(&req_buf, "include-tag");
  1022. if (prefer_ofs_delta)
  1023. packet_buf_write(&req_buf, "ofs-delta");
  1024. if (sideband_all)
  1025. packet_buf_write(&req_buf, "sideband-all");
  1026. /* Add shallow-info and deepen request */
  1027. if (server_supports_feature("fetch", "shallow", 0))
  1028. add_shallow_requests(&req_buf, args);
  1029. else if (is_repository_shallow(the_repository) || args->deepen)
  1030. die(_("Server does not support shallow requests"));
  1031. /* Add filter */
  1032. if (server_supports_feature("fetch", "filter", 0) &&
  1033. args->filter_options.choice) {
  1034. const char *spec =
  1035. expand_list_objects_filter_spec(&args->filter_options);
  1036. print_verbose(args, _("Server supports filter"));
  1037. packet_buf_write(&req_buf, "filter %s", spec);
  1038. } else if (args->filter_options.choice) {
  1039. warning("filtering not recognized by server, ignoring");
  1040. }
  1041. /* add wants */
  1042. add_wants(args->no_dependents, wants, &req_buf);
  1043. if (args->no_dependents) {
  1044. packet_buf_write(&req_buf, "done");
  1045. ret = 1;
  1046. } else {
  1047. /* Add all of the common commits we've found in previous rounds */
  1048. add_common(&req_buf, common);
  1049. /* Add initial haves */
  1050. ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
  1051. }
  1052. /* Send request */
  1053. packet_buf_flush(&req_buf);
  1054. if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
  1055. die_errno(_("unable to write request to remote"));
  1056. strbuf_release(&req_buf);
  1057. return ret;
  1058. }
  1059. /*
  1060. * Processes a section header in a server's response and checks if it matches
  1061. * `section`. If the value of `peek` is 1, the header line will be peeked (and
  1062. * not consumed); if 0, the line will be consumed and the function will die if
  1063. * the section header doesn't match what was expected.
  1064. */
  1065. static int process_section_header(struct packet_reader *reader,
  1066. const char *section, int peek)
  1067. {
  1068. int ret;
  1069. if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
  1070. die(_("error reading section header '%s'"), section);
  1071. ret = !strcmp(reader->line, section);
  1072. if (!peek) {
  1073. if (!ret)
  1074. die(_("expected '%s', received '%s'"),
  1075. section, reader->line);
  1076. packet_reader_read(reader);
  1077. }
  1078. return ret;
  1079. }
  1080. static int process_acks(struct fetch_negotiator *negotiator,
  1081. struct packet_reader *reader,
  1082. struct oidset *common)
  1083. {
  1084. /* received */
  1085. int received_ready = 0;
  1086. int received_ack = 0;
  1087. process_section_header(reader, "acknowledgments", 0);
  1088. while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
  1089. const char *arg;
  1090. if (!strcmp(reader->line, "NAK"))
  1091. continue;
  1092. if (skip_prefix(reader->line, "ACK ", &arg)) {
  1093. struct object_id oid;
  1094. if (!get_oid_hex(arg, &oid)) {
  1095. struct commit *commit;
  1096. oidset_insert(common, &oid);
  1097. commit = lookup_commit(the_repository, &oid);
  1098. negotiator->ack(negotiator, commit);
  1099. }
  1100. continue;
  1101. }
  1102. if (!strcmp(reader->line, "ready")) {
  1103. received_ready = 1;
  1104. continue;
  1105. }
  1106. die(_("unexpected acknowledgment line: '%s'"), reader->line);
  1107. }
  1108. if (reader->status != PACKET_READ_FLUSH &&
  1109. reader->status != PACKET_READ_DELIM)
  1110. die(_("error processing acks: %d"), reader->status);
  1111. /*
  1112. * If an "acknowledgments" section is sent, a packfile is sent if and
  1113. * only if "ready" was sent in this section. The other sections
  1114. * ("shallow-info" and "wanted-refs") are sent only if a packfile is
  1115. * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
  1116. * otherwise.
  1117. */
  1118. if (received_ready && reader->status != PACKET_READ_DELIM)
  1119. die(_("expected packfile to be sent after 'ready'"));
  1120. if (!received_ready && reader->status != PACKET_READ_FLUSH)
  1121. die(_("expected no other sections to be sent after no 'ready'"));
  1122. /* return 0 if no common, 1 if there are common, or 2 if ready */
  1123. return received_ready ? 2 : (received_ack ? 1 : 0);
  1124. }
  1125. static void receive_shallow_info(struct fetch_pack_args *args,
  1126. struct packet_reader *reader,
  1127. struct oid_array *shallows,
  1128. struct shallow_info *si)
  1129. {
  1130. int unshallow_received = 0;
  1131. process_section_header(reader, "shallow-info", 0);
  1132. while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
  1133. const char *arg;
  1134. struct object_id oid;
  1135. if (skip_prefix(reader->line, "shallow ", &arg)) {
  1136. if (get_oid_hex(arg, &oid))
  1137. die(_("invalid shallow line: %s"), reader->line);
  1138. oid_array_append(shallows, &oid);
  1139. continue;
  1140. }
  1141. if (skip_prefix(reader->line, "unshallow ", &arg)) {
  1142. if (get_oid_hex(arg, &oid))
  1143. die(_("invalid unshallow line: %s"), reader->line);
  1144. if (!lookup_object(the_repository, &oid))
  1145. die(_("object not found: %s"), reader->line);
  1146. /* make sure that it is parsed as shallow */
  1147. if (!parse_object(the_repository, &oid))
  1148. die(_("error in object: %s"), reader->line);
  1149. if (unregister_shallow(&oid))
  1150. die(_("no shallow found: %s"), reader->line);
  1151. unshallow_received = 1;
  1152. continue;
  1153. }
  1154. die(_("expected shallow/unshallow, got %s"), reader->line);
  1155. }
  1156. if (reader->status != PACKET_READ_FLUSH &&
  1157. reader->status != PACKET_READ_DELIM)
  1158. die(_("error processing shallow info: %d"), reader->status);
  1159. if (args->deepen || unshallow_received) {
  1160. /*
  1161. * Treat these as shallow lines caused by our depth settings.
  1162. * In v0, these lines cannot cause refs to be rejected; do the
  1163. * same.
  1164. */
  1165. int i;
  1166. for (i = 0; i < shallows->nr; i++)
  1167. register_shallow(the_repository, &shallows->oid[i]);
  1168. setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
  1169. NULL);
  1170. args->deepen = 1;
  1171. } else if (shallows->nr) {
  1172. /*
  1173. * Treat these as shallow lines caused by the remote being
  1174. * shallow. In v0, remote refs that reach these objects are
  1175. * rejected (unless --update-shallow is set); do the same.
  1176. */
  1177. prepare_shallow_info(si, shallows);
  1178. if (si->nr_ours || si->nr_theirs)
  1179. alternate_shallow_file =
  1180. setup_temporary_shallow(si->shallow);
  1181. else
  1182. alternate_shallow_file = NULL;
  1183. } else {
  1184. alternate_shallow_file = NULL;
  1185. }
  1186. }
  1187. static int cmp_name_ref(const void *name, const void *ref)
  1188. {
  1189. return strcmp(name, (*(struct ref **)ref)->name);
  1190. }
  1191. static void receive_wanted_refs(struct packet_reader *reader,
  1192. struct ref **sought, int nr_sought)
  1193. {
  1194. process_section_header(reader, "wanted-refs", 0);
  1195. while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
  1196. struct object_id oid;
  1197. const char *end;
  1198. struct ref **found;
  1199. if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
  1200. die(_("expected wanted-ref, got '%s'"), reader->line);
  1201. found = bsearch(end, sought, nr_sought, sizeof(*sought),
  1202. cmp_name_ref);
  1203. if (!found)
  1204. die(_("unexpected wanted-ref: '%s'"), reader->line);
  1205. oidcpy(&(*found)->old_oid, &oid);
  1206. }
  1207. if (reader->status != PACKET_READ_DELIM)
  1208. die(_("error processing wanted refs: %d"), reader->status);
  1209. }
  1210. enum fetch_state {
  1211. FETCH_CHECK_LOCAL = 0,
  1212. FETCH_SEND_REQUEST,
  1213. FETCH_PROCESS_ACKS,
  1214. FETCH_GET_PACK,
  1215. FETCH_DONE,
  1216. };
  1217. static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
  1218. int fd[2],
  1219. const struct ref *orig_ref,
  1220. struct ref **sought, int nr_sought,
  1221. struct oid_array *shallows,
  1222. struct shallow_info *si,
  1223. char **pack_lockfile)
  1224. {
  1225. struct repository *r = the_repository;
  1226. struct ref *ref = copy_ref_list(orig_ref);
  1227. enum fetch_state state = FETCH_CHECK_LOCAL;
  1228. struct oidset common = OIDSET_INIT;
  1229. struct packet_reader reader;
  1230. int in_vain = 0, negotiation_started = 0;
  1231. int haves_to_send = INITIAL_FLUSH;
  1232. struct fetch_negotiator negotiator;
  1233. fetch_negotiator_init(r, &negotiator);
  1234. packet_reader_init(&reader, fd[0], NULL, 0,
  1235. PACKET_READ_CHOMP_NEWLINE |
  1236. PACKET_READ_DIE_ON_ERR_PACKET);
  1237. if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
  1238. server_supports_feature("fetch", "sideband-all", 0)) {
  1239. reader.use_sideband = 1;
  1240. reader.me = "fetch-pack";
  1241. }
  1242. while (state != FETCH_DONE) {
  1243. switch (state) {
  1244. case FETCH_CHECK_LOCAL:
  1245. sort_ref_list(&ref, ref_compare_name);
  1246. QSORT(sought, nr_sought, cmp_ref_by_name);
  1247. /* v2 supports these by default */
  1248. allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
  1249. use_sideband = 2;
  1250. if (args->depth > 0 || args->deepen_since || args->deepen_not)
  1251. args->deepen = 1;
  1252. /* Filter 'ref' by 'sought' and those that aren't local */
  1253. if (!args->no_dependents) {
  1254. mark_complete_and_common_ref(&negotiator, args, &ref);
  1255. filter_refs(args, &ref, sought, nr_sought);
  1256. if (everything_local(args, &ref))
  1257. state = FETCH_DONE;
  1258. else
  1259. state = FETCH_SEND_REQUEST;
  1260. mark_tips(&negotiator, args->negotiation_tips);
  1261. for_each_cached_alternate(&negotiator,
  1262. insert_one_alternate_object);
  1263. } else {
  1264. filter_refs(args, &ref, sought, nr_sought);
  1265. state = FETCH_SEND_REQUEST;
  1266. }
  1267. break;
  1268. case FETCH_SEND_REQUEST:
  1269. if (!negotiation_started) {
  1270. negotiation_started = 1;
  1271. trace2_region_enter("fetch-pack",
  1272. "negotiation_v2",
  1273. the_repository);
  1274. }
  1275. if (send_fetch_request(&negotiator, fd[1], args, ref,
  1276. &common,
  1277. &haves_to_send, &in_vain,
  1278. reader.use_sideband))
  1279. state = FETCH_GET_PACK;
  1280. else
  1281. state = FETCH_PROCESS_ACKS;
  1282. break;
  1283. case FETCH_PROCESS_ACKS:
  1284. /* Process ACKs/NAKs */
  1285. switch (process_acks(&negotiator, &reader, &common)) {
  1286. case 2:
  1287. state = FETCH_GET_PACK;
  1288. break;
  1289. case 1:
  1290. in_vain = 0;
  1291. /* fallthrough */
  1292. default:
  1293. state = FETCH_SEND_REQUEST;
  1294. break;
  1295. }
  1296. break;
  1297. case FETCH_GET_PACK:
  1298. trace2_region_leave("fetch-pack",
  1299. "negotiation_v2",
  1300. the_repository);
  1301. /* Check for shallow-info section */
  1302. if (process_section_header(&reader, "shallow-info", 1))
  1303. receive_shallow_info(args, &reader, shallows, si);
  1304. if (process_section_header(&reader, "wanted-refs", 1))
  1305. receive_wanted_refs(&reader, sought, nr_sought);
  1306. /* get the pack */
  1307. process_section_header(&reader, "packfile", 0);
  1308. if (get_pack(args, fd, pack_lockfile))
  1309. die(_("git fetch-pack: fetch failed."));
  1310. state = FETCH_DONE;
  1311. break;
  1312. case FETCH_DONE:
  1313. continue;
  1314. }
  1315. }
  1316. negotiator.release(&negotiator);
  1317. oidset_clear(&common);
  1318. return ref;
  1319. }
  1320. static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
  1321. {
  1322. if (strcmp(var, "fetch.fsck.skiplist") == 0) {
  1323. const char *path;
  1324. if (git_config_pathname(&path, var, value))
  1325. return 1;
  1326. strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
  1327. fsck_msg_types.len ? ',' : '=', path);
  1328. free((char *)path);
  1329. return 0;
  1330. }
  1331. if (skip_prefix(var, "fetch.fsck.", &var)) {
  1332. if (is_valid_msg_type(var, value))
  1333. strbuf_addf(&fsck_msg_types, "%c%s=%s",
  1334. fsck_msg_types.len ? ',' : '=', var, value);
  1335. else
  1336. warning("Skipping unknown msg id '%s'", var);
  1337. return 0;
  1338. }
  1339. return git_default_config(var, value, cb);
  1340. }
  1341. static void fetch_pack_config(void)
  1342. {
  1343. git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
  1344. git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
  1345. git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
  1346. git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
  1347. git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
  1348. git_config(fetch_pack_config_cb, NULL);
  1349. }
  1350. static void fetch_pack_setup(void)
  1351. {
  1352. static int did_setup;
  1353. if (did_setup)
  1354. return;
  1355. fetch_pack_config();
  1356. if (0 <= transfer_unpack_limit)
  1357. unpack_limit = transfer_unpack_limit;
  1358. else if (0 <= fetch_unpack_limit)
  1359. unpack_limit = fetch_unpack_limit;
  1360. did_setup = 1;
  1361. }
  1362. static int remove_duplicates_in_refs(struct ref **ref, int nr)
  1363. {
  1364. struct string_list names = STRING_LIST_INIT_NODUP;
  1365. int src, dst;
  1366. for (src = dst = 0; src < nr; src++) {
  1367. struct string_list_item *item;
  1368. item = string_list_insert(&names, ref[src]->name);
  1369. if (item->util)
  1370. continue; /* already have it */
  1371. item->util = ref[src];
  1372. if (src != dst)
  1373. ref[dst] = ref[src];
  1374. dst++;
  1375. }
  1376. for (src = dst; src < nr; src++)
  1377. ref[src] = NULL;
  1378. string_list_clear(&names, 0);
  1379. return dst;
  1380. }
  1381. static void update_shallow(struct fetch_pack_args *args,
  1382. struct ref **sought, int nr_sought,
  1383. struct shallow_info *si)
  1384. {
  1385. struct oid_array ref = OID_ARRAY_INIT;
  1386. int *status;
  1387. int i;
  1388. if (args->deepen && alternate_shallow_file) {
  1389. if (*alternate_shallow_file == '\0') { /* --unshallow */
  1390. unlink_or_warn(git_path_shallow(the_repository));
  1391. rollback_lock_file(&shallow_lock);
  1392. } else
  1393. commit_lock_file(&shallow_lock);
  1394. alternate_shallow_file = NULL;
  1395. return;
  1396. }
  1397. if (!si->shallow || !si->shallow->nr)
  1398. return;
  1399. if (args->cloning) {
  1400. /*
  1401. * remote is shallow, but this is a clone, there are
  1402. * no objects in repo to worry about. Accept any
  1403. * shallow points that exist in the pack (iow in repo
  1404. * after get_pack() and reprepare_packed_git())
  1405. */
  1406. struct oid_array extra = OID_ARRAY_INIT;
  1407. struct object_id *oid = si->shallow->oid;
  1408. for (i = 0; i < si->shallow->nr; i++)
  1409. if (has_object_file(&oid[i]))
  1410. oid_array_append(&extra, &oid[i]);
  1411. if (extra.nr) {
  1412. setup_alternate_shallow(&shallow_lock,
  1413. &alternate_shallow_file,
  1414. &extra);
  1415. commit_lock_file(&shallow_lock);
  1416. alternate_shallow_file = NULL;
  1417. }
  1418. oid_array_clear(&extra);
  1419. return;
  1420. }
  1421. if (!si->nr_ours && !si->nr_theirs)
  1422. return;
  1423. remove_nonexistent_theirs_shallow(si);
  1424. if (!si->nr_ours && !si->nr_theirs)
  1425. return;
  1426. for (i = 0; i < nr_sought; i++)
  1427. oid_array_append(&ref, &sought[i]->old_oid);
  1428. si->ref = &ref;
  1429. if (args->update_shallow) {
  1430. /*
  1431. * remote is also shallow, .git/shallow may be updated
  1432. * so all refs can be accepted. Make sure we only add
  1433. * shallow roots that are actually reachable from new
  1434. * refs.
  1435. */
  1436. struct oid_array extra = OID_ARRAY_INIT;
  1437. struct object_id *oid = si->shallow->oid;
  1438. assign_shallow_commits_to_refs(si, NULL, NULL);
  1439. if (!si->nr_ours && !si->nr_theirs) {
  1440. oid_array_clear(&ref);
  1441. return;
  1442. }
  1443. for (i = 0; i < si->nr_ours; i++)
  1444. oid_array_append(&extra, &oid[si->ours[i]]);
  1445. for (i = 0; i < si->nr_theirs; i++)
  1446. oid_array_append(&extra, &oid[si->theirs[i]]);
  1447. setup_alternate_shallow(&shallow_lock,
  1448. &alternate_shallow_file,
  1449. &extra);
  1450. commit_lock_file(&shallow_lock);
  1451. oid_array_clear(&extra);
  1452. oid_array_clear(&ref);
  1453. alternate_shallow_file = NULL;
  1454. return;
  1455. }
  1456. /*
  1457. * remote is also shallow, check what ref is safe to update
  1458. * without updating .git/shallow
  1459. */
  1460. status = xcalloc(nr_sought, sizeof(*status));
  1461. assign_shallow_commits_to_refs(si, NULL, status);
  1462. if (si->nr_ours || si->nr_theirs) {
  1463. for (i = 0; i < nr_sought; i++)
  1464. if (status[i])
  1465. sought[i]->status = REF_STATUS_REJECT_SHALLOW;
  1466. }
  1467. free(status);
  1468. oid_array_clear(&ref);
  1469. }
  1470. static int iterate_ref_map(void *cb_data, struct object_id *oid)
  1471. {
  1472. struct ref **rm = cb_data;
  1473. struct ref *ref = *rm;
  1474. if (!ref)
  1475. return -1; /* end of the list */
  1476. *rm = ref->next;
  1477. oidcpy(oid, &ref->old_oid);
  1478. return 0;
  1479. }
  1480. struct ref *fetch_pack(struct fetch_pack_args *args,
  1481. int fd[],
  1482. const struct ref *ref,
  1483. struct ref **sought, int nr_sought,
  1484. struct oid_array *shallow,
  1485. char **pack_lockfile,
  1486. enum protocol_version version)
  1487. {
  1488. struct ref *ref_cpy;
  1489. struct shallow_info si;
  1490. struct oid_array shallows_scratch = OID_ARRAY_INIT;
  1491. fetch_pack_setup();
  1492. if (nr_sought)
  1493. nr_sought = remove_duplicates_in_refs(sought, nr_sought);
  1494. if (args->no_dependents && !args->filter_options.choice) {
  1495. /*
  1496. * The protocol does not support requesting that only the
  1497. * wanted objects be sent, so approximate this by setting a
  1498. * "blob:none" filter if no filter is already set. This works
  1499. * for all object types: note that wanted blobs will still be
  1500. * sent because they are directly specified as a "want".
  1501. *
  1502. * NEEDSWORK: Add an option in the protocol to request that
  1503. * only the wanted objects be sent, and implement it.
  1504. */
  1505. parse_list_objects_filter(&args->filter_options, "blob:none");
  1506. }
  1507. if (version != protocol_v2 && !ref) {
  1508. packet_flush(fd[1]);
  1509. die(_("no matching remote head"));
  1510. }
  1511. if (version == protocol_v2) {
  1512. if (shallow->nr)
  1513. BUG("Protocol V2 does not provide shallows at this point in the fetch");
  1514. memset(&si, 0, sizeof(si));
  1515. ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
  1516. &shallows_scratch, &si,
  1517. pack_lockfile);
  1518. } else {
  1519. prepare_shallow_info(&si, shallow);
  1520. ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
  1521. &si, pack_lockfile);
  1522. }
  1523. reprepare_packed_git(the_repository);
  1524. if (!args->cloning && args->deepen) {
  1525. struct check_connected_options opt = CHECK_CONNECTED_INIT;
  1526. struct ref *iterator = ref_cpy;
  1527. opt.shallow_file = alternate_shallow_file;
  1528. if (args->deepen)
  1529. opt.is_deepening_fetch = 1;
  1530. if (check_connected(iterate_ref_map, &iterator, &opt)) {
  1531. error(_("remote did not send all necessary objects"));
  1532. free_refs(ref_cpy);
  1533. ref_cpy = NULL;
  1534. rollback_lock_file(&shallow_lock);
  1535. goto cleanup;
  1536. }
  1537. args->connectivity_checked = 1;
  1538. }
  1539. update_shallow(args, sought, nr_sought, &si);
  1540. cleanup:
  1541. clear_shallow_info(&si);
  1542. oid_array_clear(&shallows_scratch);
  1543. return ref_cpy;
  1544. }
  1545. int report_unmatched_refs(struct ref **sought, int nr_sought)
  1546. {
  1547. int i, ret = 0;
  1548. for (i = 0; i < nr_sought; i++) {
  1549. if (!sought[i])
  1550. continue;
  1551. switch (sought[i]->match_status) {
  1552. case REF_MATCHED:
  1553. continue;
  1554. case REF_NOT_MATCHED:
  1555. error(_("no such remote ref %s"), sought[i]->name);
  1556. break;
  1557. case REF_UNADVERTISED_NOT_ALLOWED:
  1558. error(_("Server does not allow request for unadvertised object %s"),
  1559. sought[i]->name);
  1560. break;
  1561. }
  1562. ret = 1;
  1563. }
  1564. return ret;
  1565. }