fetch-pack.c 53 KB

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