upload-pack.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. #include "cache.h"
  2. #include "config.h"
  3. #include "refs.h"
  4. #include "pkt-line.h"
  5. #include "sideband.h"
  6. #include "repository.h"
  7. #include "object-store.h"
  8. #include "tag.h"
  9. #include "object.h"
  10. #include "commit.h"
  11. #include "diff.h"
  12. #include "revision.h"
  13. #include "list-objects.h"
  14. #include "list-objects-filter.h"
  15. #include "list-objects-filter-options.h"
  16. #include "run-command.h"
  17. #include "connect.h"
  18. #include "sigchain.h"
  19. #include "version.h"
  20. #include "string-list.h"
  21. #include "strvec.h"
  22. #include "prio-queue.h"
  23. #include "protocol.h"
  24. #include "quote.h"
  25. #include "upload-pack.h"
  26. #include "serve.h"
  27. #include "commit-graph.h"
  28. #include "commit-reach.h"
  29. #include "shallow.h"
  30. /* Remember to update object flag allocation in object.h */
  31. #define THEY_HAVE (1u << 11)
  32. #define OUR_REF (1u << 12)
  33. #define WANTED (1u << 13)
  34. #define COMMON_KNOWN (1u << 14)
  35. #define SHALLOW (1u << 16)
  36. #define NOT_SHALLOW (1u << 17)
  37. #define CLIENT_SHALLOW (1u << 18)
  38. #define HIDDEN_REF (1u << 19)
  39. #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \
  40. NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF)
  41. /* Enum for allowed unadvertised object request (UOR) */
  42. enum allow_uor {
  43. /* Allow specifying sha1 if it is a ref tip. */
  44. ALLOW_TIP_SHA1 = 0x01,
  45. /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
  46. ALLOW_REACHABLE_SHA1 = 0x02,
  47. /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
  48. ALLOW_ANY_SHA1 = 0x07
  49. };
  50. /*
  51. * Please annotate, and if possible group together, fields used only
  52. * for protocol v0 or only for protocol v2.
  53. */
  54. struct upload_pack_data {
  55. struct string_list symref; /* v0 only */
  56. struct object_array want_obj;
  57. struct object_array have_obj;
  58. struct oid_array haves; /* v2 only */
  59. struct string_list wanted_refs; /* v2 only */
  60. struct object_array shallows;
  61. struct string_list deepen_not;
  62. struct object_array extra_edge_obj;
  63. int depth;
  64. timestamp_t deepen_since;
  65. int deepen_rev_list;
  66. int deepen_relative;
  67. int keepalive;
  68. int shallow_nr;
  69. timestamp_t oldest_have;
  70. unsigned int timeout; /* v0 only */
  71. enum {
  72. NO_MULTI_ACK = 0,
  73. MULTI_ACK = 1,
  74. MULTI_ACK_DETAILED = 2
  75. } multi_ack; /* v0 only */
  76. /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */
  77. int use_sideband;
  78. struct string_list uri_protocols;
  79. enum allow_uor allow_uor;
  80. struct list_objects_filter_options filter_options;
  81. struct string_list allowed_filters;
  82. struct packet_writer writer;
  83. const char *pack_objects_hook;
  84. unsigned stateless_rpc : 1; /* v0 only */
  85. unsigned no_done : 1; /* v0 only */
  86. unsigned daemon_mode : 1; /* v0 only */
  87. unsigned filter_capability_requested : 1; /* v0 only */
  88. unsigned use_thin_pack : 1;
  89. unsigned use_ofs_delta : 1;
  90. unsigned no_progress : 1;
  91. unsigned use_include_tag : 1;
  92. unsigned allow_filter : 1;
  93. unsigned allow_filter_fallback : 1;
  94. unsigned long tree_filter_max_depth;
  95. unsigned done : 1; /* v2 only */
  96. unsigned allow_ref_in_want : 1; /* v2 only */
  97. unsigned allow_sideband_all : 1; /* v2 only */
  98. };
  99. static void upload_pack_data_init(struct upload_pack_data *data)
  100. {
  101. struct string_list symref = STRING_LIST_INIT_DUP;
  102. struct string_list wanted_refs = STRING_LIST_INIT_DUP;
  103. struct object_array want_obj = OBJECT_ARRAY_INIT;
  104. struct object_array have_obj = OBJECT_ARRAY_INIT;
  105. struct oid_array haves = OID_ARRAY_INIT;
  106. struct object_array shallows = OBJECT_ARRAY_INIT;
  107. struct string_list deepen_not = STRING_LIST_INIT_DUP;
  108. struct string_list uri_protocols = STRING_LIST_INIT_DUP;
  109. struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
  110. struct string_list allowed_filters = STRING_LIST_INIT_DUP;
  111. memset(data, 0, sizeof(*data));
  112. data->symref = symref;
  113. data->wanted_refs = wanted_refs;
  114. data->want_obj = want_obj;
  115. data->have_obj = have_obj;
  116. data->haves = haves;
  117. data->shallows = shallows;
  118. data->deepen_not = deepen_not;
  119. data->uri_protocols = uri_protocols;
  120. data->extra_edge_obj = extra_edge_obj;
  121. data->allowed_filters = allowed_filters;
  122. data->allow_filter_fallback = 1;
  123. data->tree_filter_max_depth = ULONG_MAX;
  124. packet_writer_init(&data->writer, 1);
  125. data->keepalive = 5;
  126. }
  127. static void upload_pack_data_clear(struct upload_pack_data *data)
  128. {
  129. string_list_clear(&data->symref, 1);
  130. string_list_clear(&data->wanted_refs, 1);
  131. object_array_clear(&data->want_obj);
  132. object_array_clear(&data->have_obj);
  133. oid_array_clear(&data->haves);
  134. object_array_clear(&data->shallows);
  135. string_list_clear(&data->deepen_not, 0);
  136. object_array_clear(&data->extra_edge_obj);
  137. list_objects_filter_release(&data->filter_options);
  138. string_list_clear(&data->allowed_filters, 1);
  139. free((char *)data->pack_objects_hook);
  140. }
  141. static void reset_timeout(unsigned int timeout)
  142. {
  143. alarm(timeout);
  144. }
  145. static void send_client_data(int fd, const char *data, ssize_t sz,
  146. int use_sideband)
  147. {
  148. if (use_sideband) {
  149. send_sideband(1, fd, data, sz, use_sideband);
  150. return;
  151. }
  152. if (fd == 3)
  153. /* emergency quit */
  154. fd = 2;
  155. if (fd == 2) {
  156. /* XXX: are we happy to lose stuff here? */
  157. xwrite(fd, data, sz);
  158. return;
  159. }
  160. write_or_die(fd, data, sz);
  161. }
  162. static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
  163. {
  164. FILE *fp = cb_data;
  165. if (graft->nr_parent == -1)
  166. fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
  167. return 0;
  168. }
  169. struct output_state {
  170. char buffer[8193];
  171. int used;
  172. unsigned packfile_uris_started : 1;
  173. unsigned packfile_started : 1;
  174. };
  175. static int relay_pack_data(int pack_objects_out, struct output_state *os,
  176. int use_sideband, int write_packfile_line)
  177. {
  178. /*
  179. * We keep the last byte to ourselves
  180. * in case we detect broken rev-list, so that we
  181. * can leave the stream corrupted. This is
  182. * unfortunate -- unpack-objects would happily
  183. * accept a valid packdata with trailing garbage,
  184. * so appending garbage after we pass all the
  185. * pack data is not good enough to signal
  186. * breakage to downstream.
  187. */
  188. ssize_t readsz;
  189. readsz = xread(pack_objects_out, os->buffer + os->used,
  190. sizeof(os->buffer) - os->used);
  191. if (readsz < 0) {
  192. return readsz;
  193. }
  194. os->used += readsz;
  195. while (!os->packfile_started) {
  196. char *p;
  197. if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) {
  198. os->packfile_started = 1;
  199. if (write_packfile_line) {
  200. if (os->packfile_uris_started)
  201. packet_delim(1);
  202. packet_write_fmt(1, "\1packfile\n");
  203. }
  204. break;
  205. }
  206. if ((p = memchr(os->buffer, '\n', os->used))) {
  207. if (!os->packfile_uris_started) {
  208. os->packfile_uris_started = 1;
  209. if (!write_packfile_line)
  210. BUG("packfile_uris requires sideband-all");
  211. packet_write_fmt(1, "\1packfile-uris\n");
  212. }
  213. *p = '\0';
  214. packet_write_fmt(1, "\1%s\n", os->buffer);
  215. os->used -= p - os->buffer + 1;
  216. memmove(os->buffer, p + 1, os->used);
  217. } else {
  218. /*
  219. * Incomplete line.
  220. */
  221. return readsz;
  222. }
  223. }
  224. if (os->used > 1) {
  225. send_client_data(1, os->buffer, os->used - 1, use_sideband);
  226. os->buffer[0] = os->buffer[os->used - 1];
  227. os->used = 1;
  228. } else {
  229. send_client_data(1, os->buffer, os->used, use_sideband);
  230. os->used = 0;
  231. }
  232. return readsz;
  233. }
  234. static void create_pack_file(struct upload_pack_data *pack_data,
  235. const struct string_list *uri_protocols)
  236. {
  237. struct child_process pack_objects = CHILD_PROCESS_INIT;
  238. struct output_state output_state = { { 0 } };
  239. char progress[128];
  240. char abort_msg[] = "aborting due to possible repository "
  241. "corruption on the remote side.";
  242. ssize_t sz;
  243. int i;
  244. FILE *pipe_fd;
  245. if (!pack_data->pack_objects_hook)
  246. pack_objects.git_cmd = 1;
  247. else {
  248. strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
  249. strvec_push(&pack_objects.args, "git");
  250. pack_objects.use_shell = 1;
  251. }
  252. if (pack_data->shallow_nr) {
  253. strvec_push(&pack_objects.args, "--shallow-file");
  254. strvec_push(&pack_objects.args, "");
  255. }
  256. strvec_push(&pack_objects.args, "pack-objects");
  257. strvec_push(&pack_objects.args, "--revs");
  258. if (pack_data->use_thin_pack)
  259. strvec_push(&pack_objects.args, "--thin");
  260. strvec_push(&pack_objects.args, "--stdout");
  261. if (pack_data->shallow_nr)
  262. strvec_push(&pack_objects.args, "--shallow");
  263. if (!pack_data->no_progress)
  264. strvec_push(&pack_objects.args, "--progress");
  265. if (pack_data->use_ofs_delta)
  266. strvec_push(&pack_objects.args, "--delta-base-offset");
  267. if (pack_data->use_include_tag)
  268. strvec_push(&pack_objects.args, "--include-tag");
  269. if (pack_data->filter_options.choice) {
  270. const char *spec =
  271. expand_list_objects_filter_spec(&pack_data->filter_options);
  272. if (pack_objects.use_shell) {
  273. struct strbuf buf = STRBUF_INIT;
  274. sq_quote_buf(&buf, spec);
  275. strvec_pushf(&pack_objects.args, "--filter=%s", buf.buf);
  276. strbuf_release(&buf);
  277. } else {
  278. strvec_pushf(&pack_objects.args, "--filter=%s", spec);
  279. }
  280. }
  281. if (uri_protocols) {
  282. for (i = 0; i < uri_protocols->nr; i++)
  283. strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
  284. uri_protocols->items[i].string);
  285. }
  286. pack_objects.in = -1;
  287. pack_objects.out = -1;
  288. pack_objects.err = -1;
  289. if (start_command(&pack_objects))
  290. die("git upload-pack: unable to fork git-pack-objects");
  291. pipe_fd = xfdopen(pack_objects.in, "w");
  292. if (pack_data->shallow_nr)
  293. for_each_commit_graft(write_one_shallow, pipe_fd);
  294. for (i = 0; i < pack_data->want_obj.nr; i++)
  295. fprintf(pipe_fd, "%s\n",
  296. oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
  297. fprintf(pipe_fd, "--not\n");
  298. for (i = 0; i < pack_data->have_obj.nr; i++)
  299. fprintf(pipe_fd, "%s\n",
  300. oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
  301. for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
  302. fprintf(pipe_fd, "%s\n",
  303. oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
  304. fprintf(pipe_fd, "\n");
  305. fflush(pipe_fd);
  306. fclose(pipe_fd);
  307. /* We read from pack_objects.err to capture stderr output for
  308. * progress bar, and pack_objects.out to capture the pack data.
  309. */
  310. while (1) {
  311. struct pollfd pfd[2];
  312. int pe, pu, pollsize, polltimeout;
  313. int ret;
  314. reset_timeout(pack_data->timeout);
  315. pollsize = 0;
  316. pe = pu = -1;
  317. if (0 <= pack_objects.out) {
  318. pfd[pollsize].fd = pack_objects.out;
  319. pfd[pollsize].events = POLLIN;
  320. pu = pollsize;
  321. pollsize++;
  322. }
  323. if (0 <= pack_objects.err) {
  324. pfd[pollsize].fd = pack_objects.err;
  325. pfd[pollsize].events = POLLIN;
  326. pe = pollsize;
  327. pollsize++;
  328. }
  329. if (!pollsize)
  330. break;
  331. polltimeout = pack_data->keepalive < 0
  332. ? -1
  333. : 1000 * pack_data->keepalive;
  334. ret = poll(pfd, pollsize, polltimeout);
  335. if (ret < 0) {
  336. if (errno != EINTR) {
  337. error_errno("poll failed, resuming");
  338. sleep(1);
  339. }
  340. continue;
  341. }
  342. if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
  343. /* Status ready; we ship that in the side-band
  344. * or dump to the standard error.
  345. */
  346. sz = xread(pack_objects.err, progress,
  347. sizeof(progress));
  348. if (0 < sz)
  349. send_client_data(2, progress, sz,
  350. pack_data->use_sideband);
  351. else if (sz == 0) {
  352. close(pack_objects.err);
  353. pack_objects.err = -1;
  354. }
  355. else
  356. goto fail;
  357. /* give priority to status messages */
  358. continue;
  359. }
  360. if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
  361. int result = relay_pack_data(pack_objects.out,
  362. &output_state,
  363. pack_data->use_sideband,
  364. !!uri_protocols);
  365. if (result == 0) {
  366. close(pack_objects.out);
  367. pack_objects.out = -1;
  368. } else if (result < 0) {
  369. goto fail;
  370. }
  371. }
  372. /*
  373. * We hit the keepalive timeout without saying anything; send
  374. * an empty message on the data sideband just to let the other
  375. * side know we're still working on it, but don't have any data
  376. * yet.
  377. *
  378. * If we don't have a sideband channel, there's no room in the
  379. * protocol to say anything, so those clients are just out of
  380. * luck.
  381. */
  382. if (!ret && pack_data->use_sideband) {
  383. static const char buf[] = "0005\1";
  384. write_or_die(1, buf, 5);
  385. }
  386. }
  387. if (finish_command(&pack_objects)) {
  388. error("git upload-pack: git-pack-objects died with error.");
  389. goto fail;
  390. }
  391. /* flush the data */
  392. if (output_state.used > 0) {
  393. send_client_data(1, output_state.buffer, output_state.used,
  394. pack_data->use_sideband);
  395. fprintf(stderr, "flushed.\n");
  396. }
  397. if (pack_data->use_sideband)
  398. packet_flush(1);
  399. return;
  400. fail:
  401. send_client_data(3, abort_msg, sizeof(abort_msg),
  402. pack_data->use_sideband);
  403. die("git upload-pack: %s", abort_msg);
  404. }
  405. static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid)
  406. {
  407. int we_knew_they_have = 0;
  408. struct object *o = parse_object(the_repository, oid);
  409. if (!o)
  410. die("oops (%s)", oid_to_hex(oid));
  411. if (o->type == OBJ_COMMIT) {
  412. struct commit_list *parents;
  413. struct commit *commit = (struct commit *)o;
  414. if (o->flags & THEY_HAVE)
  415. we_knew_they_have = 1;
  416. else
  417. o->flags |= THEY_HAVE;
  418. if (!data->oldest_have || (commit->date < data->oldest_have))
  419. data->oldest_have = commit->date;
  420. for (parents = commit->parents;
  421. parents;
  422. parents = parents->next)
  423. parents->item->object.flags |= THEY_HAVE;
  424. }
  425. if (!we_knew_they_have) {
  426. add_object_array(o, NULL, &data->have_obj);
  427. return 1;
  428. }
  429. return 0;
  430. }
  431. static int got_oid(struct upload_pack_data *data,
  432. const char *hex, struct object_id *oid)
  433. {
  434. if (get_oid_hex(hex, oid))
  435. die("git upload-pack: expected SHA1 object, got '%s'", hex);
  436. if (!has_object_file_with_flags(oid,
  437. OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
  438. return -1;
  439. return do_got_oid(data, oid);
  440. }
  441. static int ok_to_give_up(struct upload_pack_data *data)
  442. {
  443. uint32_t min_generation = GENERATION_NUMBER_ZERO;
  444. if (!data->have_obj.nr)
  445. return 0;
  446. return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE,
  447. COMMON_KNOWN, data->oldest_have,
  448. min_generation);
  449. }
  450. static int get_common_commits(struct upload_pack_data *data,
  451. struct packet_reader *reader)
  452. {
  453. struct object_id oid;
  454. char last_hex[GIT_MAX_HEXSZ + 1];
  455. int got_common = 0;
  456. int got_other = 0;
  457. int sent_ready = 0;
  458. save_commit_buffer = 0;
  459. for (;;) {
  460. const char *arg;
  461. reset_timeout(data->timeout);
  462. if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
  463. if (data->multi_ack == MULTI_ACK_DETAILED
  464. && got_common
  465. && !got_other
  466. && ok_to_give_up(data)) {
  467. sent_ready = 1;
  468. packet_write_fmt(1, "ACK %s ready\n", last_hex);
  469. }
  470. if (data->have_obj.nr == 0 || data->multi_ack)
  471. packet_write_fmt(1, "NAK\n");
  472. if (data->no_done && sent_ready) {
  473. packet_write_fmt(1, "ACK %s\n", last_hex);
  474. return 0;
  475. }
  476. if (data->stateless_rpc)
  477. exit(0);
  478. got_common = 0;
  479. got_other = 0;
  480. continue;
  481. }
  482. if (skip_prefix(reader->line, "have ", &arg)) {
  483. switch (got_oid(data, arg, &oid)) {
  484. case -1: /* they have what we do not */
  485. got_other = 1;
  486. if (data->multi_ack
  487. && ok_to_give_up(data)) {
  488. const char *hex = oid_to_hex(&oid);
  489. if (data->multi_ack == MULTI_ACK_DETAILED) {
  490. sent_ready = 1;
  491. packet_write_fmt(1, "ACK %s ready\n", hex);
  492. } else
  493. packet_write_fmt(1, "ACK %s continue\n", hex);
  494. }
  495. break;
  496. default:
  497. got_common = 1;
  498. oid_to_hex_r(last_hex, &oid);
  499. if (data->multi_ack == MULTI_ACK_DETAILED)
  500. packet_write_fmt(1, "ACK %s common\n", last_hex);
  501. else if (data->multi_ack)
  502. packet_write_fmt(1, "ACK %s continue\n", last_hex);
  503. else if (data->have_obj.nr == 1)
  504. packet_write_fmt(1, "ACK %s\n", last_hex);
  505. break;
  506. }
  507. continue;
  508. }
  509. if (!strcmp(reader->line, "done")) {
  510. if (data->have_obj.nr > 0) {
  511. if (data->multi_ack)
  512. packet_write_fmt(1, "ACK %s\n", last_hex);
  513. return 0;
  514. }
  515. packet_write_fmt(1, "NAK\n");
  516. return -1;
  517. }
  518. die("git upload-pack: expected SHA1 list, got '%s'", reader->line);
  519. }
  520. }
  521. static int is_our_ref(struct object *o, enum allow_uor allow_uor)
  522. {
  523. int allow_hidden_ref = (allow_uor &
  524. (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
  525. return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
  526. }
  527. /*
  528. * on successful case, it's up to the caller to close cmd->out
  529. */
  530. static int do_reachable_revlist(struct child_process *cmd,
  531. struct object_array *src,
  532. struct object_array *reachable,
  533. enum allow_uor allow_uor)
  534. {
  535. static const char *argv[] = {
  536. "rev-list", "--stdin", NULL,
  537. };
  538. struct object *o;
  539. FILE *cmd_in = NULL;
  540. int i;
  541. cmd->argv = argv;
  542. cmd->git_cmd = 1;
  543. cmd->no_stderr = 1;
  544. cmd->in = -1;
  545. cmd->out = -1;
  546. /*
  547. * If the next rev-list --stdin encounters an unknown commit,
  548. * it terminates, which will cause SIGPIPE in the write loop
  549. * below.
  550. */
  551. sigchain_push(SIGPIPE, SIG_IGN);
  552. if (start_command(cmd))
  553. goto error;
  554. cmd_in = xfdopen(cmd->in, "w");
  555. for (i = get_max_object_index(); 0 < i; ) {
  556. o = get_indexed_object(--i);
  557. if (!o)
  558. continue;
  559. if (reachable && o->type == OBJ_COMMIT)
  560. o->flags &= ~TMP_MARK;
  561. if (!is_our_ref(o, allow_uor))
  562. continue;
  563. if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
  564. goto error;
  565. }
  566. for (i = 0; i < src->nr; i++) {
  567. o = src->objects[i].item;
  568. if (is_our_ref(o, allow_uor)) {
  569. if (reachable)
  570. add_object_array(o, NULL, reachable);
  571. continue;
  572. }
  573. if (reachable && o->type == OBJ_COMMIT)
  574. o->flags |= TMP_MARK;
  575. if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
  576. goto error;
  577. }
  578. if (ferror(cmd_in) || fflush(cmd_in))
  579. goto error;
  580. fclose(cmd_in);
  581. cmd->in = -1;
  582. sigchain_pop(SIGPIPE);
  583. return 0;
  584. error:
  585. sigchain_pop(SIGPIPE);
  586. if (cmd_in)
  587. fclose(cmd_in);
  588. if (cmd->out >= 0)
  589. close(cmd->out);
  590. return -1;
  591. }
  592. static int get_reachable_list(struct upload_pack_data *data,
  593. struct object_array *reachable)
  594. {
  595. struct child_process cmd = CHILD_PROCESS_INIT;
  596. int i;
  597. struct object *o;
  598. char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
  599. const unsigned hexsz = the_hash_algo->hexsz;
  600. if (do_reachable_revlist(&cmd, &data->shallows, reachable,
  601. data->allow_uor) < 0)
  602. return -1;
  603. while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
  604. struct object_id oid;
  605. const char *p;
  606. if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n')
  607. break;
  608. o = lookup_object(the_repository, &oid);
  609. if (o && o->type == OBJ_COMMIT) {
  610. o->flags &= ~TMP_MARK;
  611. }
  612. }
  613. for (i = get_max_object_index(); 0 < i; i--) {
  614. o = get_indexed_object(i - 1);
  615. if (o && o->type == OBJ_COMMIT &&
  616. (o->flags & TMP_MARK)) {
  617. add_object_array(o, NULL, reachable);
  618. o->flags &= ~TMP_MARK;
  619. }
  620. }
  621. close(cmd.out);
  622. if (finish_command(&cmd))
  623. return -1;
  624. return 0;
  625. }
  626. static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
  627. {
  628. struct child_process cmd = CHILD_PROCESS_INIT;
  629. char buf[1];
  630. int i;
  631. if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0)
  632. return 1;
  633. /*
  634. * The commits out of the rev-list are not ancestors of
  635. * our ref.
  636. */
  637. i = read_in_full(cmd.out, buf, 1);
  638. if (i)
  639. goto error;
  640. close(cmd.out);
  641. cmd.out = -1;
  642. /*
  643. * rev-list may have died by encountering a bad commit
  644. * in the history, in which case we do want to bail out
  645. * even when it showed no commit.
  646. */
  647. if (finish_command(&cmd))
  648. goto error;
  649. /* All the non-tip ones are ancestors of what we advertised */
  650. return 0;
  651. error:
  652. if (cmd.out >= 0)
  653. close(cmd.out);
  654. return 1;
  655. }
  656. static void check_non_tip(struct upload_pack_data *data)
  657. {
  658. int i;
  659. /*
  660. * In the normal in-process case without
  661. * uploadpack.allowReachableSHA1InWant,
  662. * non-tip requests can never happen.
  663. */
  664. if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1))
  665. goto error;
  666. if (!has_unreachable(&data->want_obj, data->allow_uor))
  667. /* All the non-tip ones are ancestors of what we advertised */
  668. return;
  669. error:
  670. /* Pick one of them (we know there at least is one) */
  671. for (i = 0; i < data->want_obj.nr; i++) {
  672. struct object *o = data->want_obj.objects[i].item;
  673. if (!is_our_ref(o, data->allow_uor)) {
  674. packet_writer_error(&data->writer,
  675. "upload-pack: not our ref %s",
  676. oid_to_hex(&o->oid));
  677. die("git upload-pack: not our ref %s",
  678. oid_to_hex(&o->oid));
  679. }
  680. }
  681. }
  682. static void send_shallow(struct upload_pack_data *data,
  683. struct commit_list *result)
  684. {
  685. while (result) {
  686. struct object *object = &result->item->object;
  687. if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
  688. packet_writer_write(&data->writer, "shallow %s",
  689. oid_to_hex(&object->oid));
  690. register_shallow(the_repository, &object->oid);
  691. data->shallow_nr++;
  692. }
  693. result = result->next;
  694. }
  695. }
  696. static void send_unshallow(struct upload_pack_data *data)
  697. {
  698. int i;
  699. for (i = 0; i < data->shallows.nr; i++) {
  700. struct object *object = data->shallows.objects[i].item;
  701. if (object->flags & NOT_SHALLOW) {
  702. struct commit_list *parents;
  703. packet_writer_write(&data->writer, "unshallow %s",
  704. oid_to_hex(&object->oid));
  705. object->flags &= ~CLIENT_SHALLOW;
  706. /*
  707. * We want to _register_ "object" as shallow, but we
  708. * also need to traverse object's parents to deepen a
  709. * shallow clone. Unregister it for now so we can
  710. * parse and add the parents to the want list, then
  711. * re-register it.
  712. */
  713. unregister_shallow(&object->oid);
  714. object->parsed = 0;
  715. parse_commit_or_die((struct commit *)object);
  716. parents = ((struct commit *)object)->parents;
  717. while (parents) {
  718. add_object_array(&parents->item->object,
  719. NULL, &data->want_obj);
  720. parents = parents->next;
  721. }
  722. add_object_array(object, NULL, &data->extra_edge_obj);
  723. }
  724. /* make sure commit traversal conforms to client */
  725. register_shallow(the_repository, &object->oid);
  726. }
  727. }
  728. static int check_ref(const char *refname_full, const struct object_id *oid,
  729. int flag, void *cb_data);
  730. static void deepen(struct upload_pack_data *data, int depth)
  731. {
  732. if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
  733. int i;
  734. for (i = 0; i < data->shallows.nr; i++) {
  735. struct object *object = data->shallows.objects[i].item;
  736. object->flags |= NOT_SHALLOW;
  737. }
  738. } else if (data->deepen_relative) {
  739. struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
  740. struct commit_list *result;
  741. /*
  742. * Checking for reachable shallows requires that our refs be
  743. * marked with OUR_REF.
  744. */
  745. head_ref_namespaced(check_ref, NULL);
  746. for_each_namespaced_ref(check_ref, NULL);
  747. get_reachable_list(data, &reachable_shallows);
  748. result = get_shallow_commits(&reachable_shallows,
  749. depth + 1,
  750. SHALLOW, NOT_SHALLOW);
  751. send_shallow(data, result);
  752. free_commit_list(result);
  753. object_array_clear(&reachable_shallows);
  754. } else {
  755. struct commit_list *result;
  756. result = get_shallow_commits(&data->want_obj, depth,
  757. SHALLOW, NOT_SHALLOW);
  758. send_shallow(data, result);
  759. free_commit_list(result);
  760. }
  761. send_unshallow(data);
  762. }
  763. static void deepen_by_rev_list(struct upload_pack_data *data,
  764. int ac,
  765. const char **av)
  766. {
  767. struct commit_list *result;
  768. disable_commit_graph(the_repository);
  769. result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
  770. send_shallow(data, result);
  771. free_commit_list(result);
  772. send_unshallow(data);
  773. }
  774. /* Returns 1 if a shallow list is sent or 0 otherwise */
  775. static int send_shallow_list(struct upload_pack_data *data)
  776. {
  777. int ret = 0;
  778. if (data->depth > 0 && data->deepen_rev_list)
  779. die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
  780. if (data->depth > 0) {
  781. deepen(data, data->depth);
  782. ret = 1;
  783. } else if (data->deepen_rev_list) {
  784. struct strvec av = STRVEC_INIT;
  785. int i;
  786. strvec_push(&av, "rev-list");
  787. if (data->deepen_since)
  788. strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
  789. if (data->deepen_not.nr) {
  790. strvec_push(&av, "--not");
  791. for (i = 0; i < data->deepen_not.nr; i++) {
  792. struct string_list_item *s = data->deepen_not.items + i;
  793. strvec_push(&av, s->string);
  794. }
  795. strvec_push(&av, "--not");
  796. }
  797. for (i = 0; i < data->want_obj.nr; i++) {
  798. struct object *o = data->want_obj.objects[i].item;
  799. strvec_push(&av, oid_to_hex(&o->oid));
  800. }
  801. deepen_by_rev_list(data, av.nr, av.v);
  802. strvec_clear(&av);
  803. ret = 1;
  804. } else {
  805. if (data->shallows.nr > 0) {
  806. int i;
  807. for (i = 0; i < data->shallows.nr; i++)
  808. register_shallow(the_repository,
  809. &data->shallows.objects[i].item->oid);
  810. }
  811. }
  812. data->shallow_nr += data->shallows.nr;
  813. return ret;
  814. }
  815. static int process_shallow(const char *line, struct object_array *shallows)
  816. {
  817. const char *arg;
  818. if (skip_prefix(line, "shallow ", &arg)) {
  819. struct object_id oid;
  820. struct object *object;
  821. if (get_oid_hex(arg, &oid))
  822. die("invalid shallow line: %s", line);
  823. object = parse_object(the_repository, &oid);
  824. if (!object)
  825. return 1;
  826. if (object->type != OBJ_COMMIT)
  827. die("invalid shallow object %s", oid_to_hex(&oid));
  828. if (!(object->flags & CLIENT_SHALLOW)) {
  829. object->flags |= CLIENT_SHALLOW;
  830. add_object_array(object, NULL, shallows);
  831. }
  832. return 1;
  833. }
  834. return 0;
  835. }
  836. static int process_deepen(const char *line, int *depth)
  837. {
  838. const char *arg;
  839. if (skip_prefix(line, "deepen ", &arg)) {
  840. char *end = NULL;
  841. *depth = (int)strtol(arg, &end, 0);
  842. if (!end || *end || *depth <= 0)
  843. die("Invalid deepen: %s", line);
  844. return 1;
  845. }
  846. return 0;
  847. }
  848. static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
  849. {
  850. const char *arg;
  851. if (skip_prefix(line, "deepen-since ", &arg)) {
  852. char *end = NULL;
  853. *deepen_since = parse_timestamp(arg, &end, 0);
  854. if (!end || *end || !deepen_since ||
  855. /* revisions.c's max_age -1 is special */
  856. *deepen_since == -1)
  857. die("Invalid deepen-since: %s", line);
  858. *deepen_rev_list = 1;
  859. return 1;
  860. }
  861. return 0;
  862. }
  863. static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
  864. {
  865. const char *arg;
  866. if (skip_prefix(line, "deepen-not ", &arg)) {
  867. char *ref = NULL;
  868. struct object_id oid;
  869. if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1)
  870. die("git upload-pack: ambiguous deepen-not: %s", line);
  871. string_list_append(deepen_not, ref);
  872. free(ref);
  873. *deepen_rev_list = 1;
  874. return 1;
  875. }
  876. return 0;
  877. }
  878. NORETURN __attribute__((format(printf,2,3)))
  879. static void send_err_and_die(struct upload_pack_data *data,
  880. const char *fmt, ...)
  881. {
  882. struct strbuf buf = STRBUF_INIT;
  883. va_list ap;
  884. va_start(ap, fmt);
  885. strbuf_vaddf(&buf, fmt, ap);
  886. va_end(ap);
  887. packet_writer_error(&data->writer, "%s", buf.buf);
  888. die("%s", buf.buf);
  889. }
  890. static void check_one_filter(struct upload_pack_data *data,
  891. struct list_objects_filter_options *opts)
  892. {
  893. const char *key = list_object_filter_config_name(opts->choice);
  894. struct string_list_item *item = string_list_lookup(&data->allowed_filters,
  895. key);
  896. int allowed;
  897. if (item)
  898. allowed = (intptr_t)item->util;
  899. else
  900. allowed = data->allow_filter_fallback;
  901. if (!allowed)
  902. send_err_and_die(data, "filter '%s' not supported", key);
  903. if (opts->choice == LOFC_TREE_DEPTH &&
  904. opts->tree_exclude_depth > data->tree_filter_max_depth)
  905. send_err_and_die(data,
  906. "tree filter allows max depth %lu, but got %lu",
  907. data->tree_filter_max_depth,
  908. opts->tree_exclude_depth);
  909. }
  910. static void check_filter_recurse(struct upload_pack_data *data,
  911. struct list_objects_filter_options *opts)
  912. {
  913. size_t i;
  914. check_one_filter(data, opts);
  915. if (opts->choice != LOFC_COMBINE)
  916. return;
  917. for (i = 0; i < opts->sub_nr; i++)
  918. check_filter_recurse(data, &opts->sub[i]);
  919. }
  920. static void die_if_using_banned_filter(struct upload_pack_data *data)
  921. {
  922. check_filter_recurse(data, &data->filter_options);
  923. }
  924. static void receive_needs(struct upload_pack_data *data,
  925. struct packet_reader *reader)
  926. {
  927. int has_non_tip = 0;
  928. data->shallow_nr = 0;
  929. for (;;) {
  930. struct object *o;
  931. const char *features;
  932. struct object_id oid_buf;
  933. const char *arg;
  934. reset_timeout(data->timeout);
  935. if (packet_reader_read(reader) != PACKET_READ_NORMAL)
  936. break;
  937. if (process_shallow(reader->line, &data->shallows))
  938. continue;
  939. if (process_deepen(reader->line, &data->depth))
  940. continue;
  941. if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list))
  942. continue;
  943. if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list))
  944. continue;
  945. if (skip_prefix(reader->line, "filter ", &arg)) {
  946. if (!data->filter_capability_requested)
  947. die("git upload-pack: filtering capability not negotiated");
  948. list_objects_filter_die_if_populated(&data->filter_options);
  949. parse_list_objects_filter(&data->filter_options, arg);
  950. die_if_using_banned_filter(data);
  951. continue;
  952. }
  953. if (!skip_prefix(reader->line, "want ", &arg) ||
  954. parse_oid_hex(arg, &oid_buf, &features))
  955. die("git upload-pack: protocol error, "
  956. "expected to get object ID, not '%s'", reader->line);
  957. if (parse_feature_request(features, "deepen-relative"))
  958. data->deepen_relative = 1;
  959. if (parse_feature_request(features, "multi_ack_detailed"))
  960. data->multi_ack = MULTI_ACK_DETAILED;
  961. else if (parse_feature_request(features, "multi_ack"))
  962. data->multi_ack = MULTI_ACK;
  963. if (parse_feature_request(features, "no-done"))
  964. data->no_done = 1;
  965. if (parse_feature_request(features, "thin-pack"))
  966. data->use_thin_pack = 1;
  967. if (parse_feature_request(features, "ofs-delta"))
  968. data->use_ofs_delta = 1;
  969. if (parse_feature_request(features, "side-band-64k"))
  970. data->use_sideband = LARGE_PACKET_MAX;
  971. else if (parse_feature_request(features, "side-band"))
  972. data->use_sideband = DEFAULT_PACKET_MAX;
  973. if (parse_feature_request(features, "no-progress"))
  974. data->no_progress = 1;
  975. if (parse_feature_request(features, "include-tag"))
  976. data->use_include_tag = 1;
  977. if (data->allow_filter &&
  978. parse_feature_request(features, "filter"))
  979. data->filter_capability_requested = 1;
  980. o = parse_object(the_repository, &oid_buf);
  981. if (!o) {
  982. packet_writer_error(&data->writer,
  983. "upload-pack: not our ref %s",
  984. oid_to_hex(&oid_buf));
  985. die("git upload-pack: not our ref %s",
  986. oid_to_hex(&oid_buf));
  987. }
  988. if (!(o->flags & WANTED)) {
  989. o->flags |= WANTED;
  990. if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
  991. || is_our_ref(o, data->allow_uor)))
  992. has_non_tip = 1;
  993. add_object_array(o, NULL, &data->want_obj);
  994. }
  995. }
  996. /*
  997. * We have sent all our refs already, and the other end
  998. * should have chosen out of them. When we are operating
  999. * in the stateless RPC mode, however, their choice may
  1000. * have been based on the set of older refs advertised
  1001. * by another process that handled the initial request.
  1002. */
  1003. if (has_non_tip)
  1004. check_non_tip(data);
  1005. if (!data->use_sideband && data->daemon_mode)
  1006. data->no_progress = 1;
  1007. if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0)
  1008. return;
  1009. if (send_shallow_list(data))
  1010. packet_flush(1);
  1011. }
  1012. /* return non-zero if the ref is hidden, otherwise 0 */
  1013. static int mark_our_ref(const char *refname, const char *refname_full,
  1014. const struct object_id *oid)
  1015. {
  1016. struct object *o = lookup_unknown_object(oid);
  1017. if (ref_is_hidden(refname, refname_full)) {
  1018. o->flags |= HIDDEN_REF;
  1019. return 1;
  1020. }
  1021. o->flags |= OUR_REF;
  1022. return 0;
  1023. }
  1024. static int check_ref(const char *refname_full, const struct object_id *oid,
  1025. int flag, void *cb_data)
  1026. {
  1027. const char *refname = strip_namespace(refname_full);
  1028. mark_our_ref(refname, refname_full, oid);
  1029. return 0;
  1030. }
  1031. static void format_symref_info(struct strbuf *buf, struct string_list *symref)
  1032. {
  1033. struct string_list_item *item;
  1034. if (!symref->nr)
  1035. return;
  1036. for_each_string_list_item(item, symref)
  1037. strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
  1038. }
  1039. static int send_ref(const char *refname, const struct object_id *oid,
  1040. int flag, void *cb_data)
  1041. {
  1042. static const char *capabilities = "multi_ack thin-pack side-band"
  1043. " side-band-64k ofs-delta shallow deepen-since deepen-not"
  1044. " deepen-relative no-progress include-tag multi_ack_detailed";
  1045. const char *refname_nons = strip_namespace(refname);
  1046. struct object_id peeled;
  1047. struct upload_pack_data *data = cb_data;
  1048. if (mark_our_ref(refname_nons, refname, oid))
  1049. return 0;
  1050. if (capabilities) {
  1051. struct strbuf symref_info = STRBUF_INIT;
  1052. format_symref_info(&symref_info, &data->symref);
  1053. packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s object-format=%s agent=%s\n",
  1054. oid_to_hex(oid), refname_nons,
  1055. 0, capabilities,
  1056. (data->allow_uor & ALLOW_TIP_SHA1) ?
  1057. " allow-tip-sha1-in-want" : "",
  1058. (data->allow_uor & ALLOW_REACHABLE_SHA1) ?
  1059. " allow-reachable-sha1-in-want" : "",
  1060. data->stateless_rpc ? " no-done" : "",
  1061. symref_info.buf,
  1062. data->allow_filter ? " filter" : "",
  1063. the_hash_algo->name,
  1064. git_user_agent_sanitized());
  1065. strbuf_release(&symref_info);
  1066. } else {
  1067. packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
  1068. }
  1069. capabilities = NULL;
  1070. if (!peel_ref(refname, &peeled))
  1071. packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
  1072. return 0;
  1073. }
  1074. static int find_symref(const char *refname, const struct object_id *oid,
  1075. int flag, void *cb_data)
  1076. {
  1077. const char *symref_target;
  1078. struct string_list_item *item;
  1079. if ((flag & REF_ISSYMREF) == 0)
  1080. return 0;
  1081. symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
  1082. if (!symref_target || (flag & REF_ISSYMREF) == 0)
  1083. die("'%s' is a symref but it is not?", refname);
  1084. item = string_list_append(cb_data, strip_namespace(refname));
  1085. item->util = xstrdup(strip_namespace(symref_target));
  1086. return 0;
  1087. }
  1088. static int parse_object_filter_config(const char *var, const char *value,
  1089. struct upload_pack_data *data)
  1090. {
  1091. struct strbuf buf = STRBUF_INIT;
  1092. const char *sub, *key;
  1093. size_t sub_len;
  1094. if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
  1095. return 0;
  1096. if (!sub) {
  1097. if (!strcmp(key, "allow"))
  1098. data->allow_filter_fallback = git_config_bool(var, value);
  1099. return 0;
  1100. }
  1101. strbuf_add(&buf, sub, sub_len);
  1102. if (!strcmp(key, "allow"))
  1103. string_list_insert(&data->allowed_filters, buf.buf)->util =
  1104. (void *)(intptr_t)git_config_bool(var, value);
  1105. else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
  1106. if (!value) {
  1107. strbuf_release(&buf);
  1108. return config_error_nonbool(var);
  1109. }
  1110. string_list_insert(&data->allowed_filters, buf.buf)->util =
  1111. (void *)(intptr_t)1;
  1112. data->tree_filter_max_depth = git_config_ulong(var, value);
  1113. }
  1114. strbuf_release(&buf);
  1115. return 0;
  1116. }
  1117. static int upload_pack_config(const char *var, const char *value, void *cb_data)
  1118. {
  1119. struct upload_pack_data *data = cb_data;
  1120. if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
  1121. if (git_config_bool(var, value))
  1122. data->allow_uor |= ALLOW_TIP_SHA1;
  1123. else
  1124. data->allow_uor &= ~ALLOW_TIP_SHA1;
  1125. } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
  1126. if (git_config_bool(var, value))
  1127. data->allow_uor |= ALLOW_REACHABLE_SHA1;
  1128. else
  1129. data->allow_uor &= ~ALLOW_REACHABLE_SHA1;
  1130. } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
  1131. if (git_config_bool(var, value))
  1132. data->allow_uor |= ALLOW_ANY_SHA1;
  1133. else
  1134. data->allow_uor &= ~ALLOW_ANY_SHA1;
  1135. } else if (!strcmp("uploadpack.keepalive", var)) {
  1136. data->keepalive = git_config_int(var, value);
  1137. if (!data->keepalive)
  1138. data->keepalive = -1;
  1139. } else if (!strcmp("uploadpack.allowfilter", var)) {
  1140. data->allow_filter = git_config_bool(var, value);
  1141. } else if (!strcmp("uploadpack.allowrefinwant", var)) {
  1142. data->allow_ref_in_want = git_config_bool(var, value);
  1143. } else if (!strcmp("uploadpack.allowsidebandall", var)) {
  1144. data->allow_sideband_all = git_config_bool(var, value);
  1145. } else if (!strcmp("core.precomposeunicode", var)) {
  1146. precomposed_unicode = git_config_bool(var, value);
  1147. }
  1148. if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
  1149. current_config_scope() != CONFIG_SCOPE_WORKTREE) {
  1150. if (!strcmp("uploadpack.packobjectshook", var))
  1151. return git_config_string(&data->pack_objects_hook, var, value);
  1152. }
  1153. parse_object_filter_config(var, value, data);
  1154. return parse_hide_refs_config(var, value, "uploadpack");
  1155. }
  1156. void upload_pack(struct upload_pack_options *options)
  1157. {
  1158. struct packet_reader reader;
  1159. struct upload_pack_data data;
  1160. upload_pack_data_init(&data);
  1161. git_config(upload_pack_config, &data);
  1162. data.stateless_rpc = options->stateless_rpc;
  1163. data.daemon_mode = options->daemon_mode;
  1164. data.timeout = options->timeout;
  1165. head_ref_namespaced(find_symref, &data.symref);
  1166. if (options->advertise_refs || !data.stateless_rpc) {
  1167. reset_timeout(data.timeout);
  1168. head_ref_namespaced(send_ref, &data);
  1169. for_each_namespaced_ref(send_ref, &data);
  1170. advertise_shallow_grafts(1);
  1171. packet_flush(1);
  1172. } else {
  1173. head_ref_namespaced(check_ref, NULL);
  1174. for_each_namespaced_ref(check_ref, NULL);
  1175. }
  1176. if (!options->advertise_refs) {
  1177. packet_reader_init(&reader, 0, NULL, 0,
  1178. PACKET_READ_CHOMP_NEWLINE |
  1179. PACKET_READ_DIE_ON_ERR_PACKET);
  1180. receive_needs(&data, &reader);
  1181. if (data.want_obj.nr) {
  1182. get_common_commits(&data, &reader);
  1183. create_pack_file(&data, NULL);
  1184. }
  1185. }
  1186. upload_pack_data_clear(&data);
  1187. }
  1188. static int parse_want(struct packet_writer *writer, const char *line,
  1189. struct object_array *want_obj)
  1190. {
  1191. const char *arg;
  1192. if (skip_prefix(line, "want ", &arg)) {
  1193. struct object_id oid;
  1194. struct object *o;
  1195. if (get_oid_hex(arg, &oid))
  1196. die("git upload-pack: protocol error, "
  1197. "expected to get oid, not '%s'", line);
  1198. o = parse_object(the_repository, &oid);
  1199. if (!o) {
  1200. packet_writer_error(writer,
  1201. "upload-pack: not our ref %s",
  1202. oid_to_hex(&oid));
  1203. die("git upload-pack: not our ref %s",
  1204. oid_to_hex(&oid));
  1205. }
  1206. if (!(o->flags & WANTED)) {
  1207. o->flags |= WANTED;
  1208. add_object_array(o, NULL, want_obj);
  1209. }
  1210. return 1;
  1211. }
  1212. return 0;
  1213. }
  1214. static int parse_want_ref(struct packet_writer *writer, const char *line,
  1215. struct string_list *wanted_refs,
  1216. struct object_array *want_obj)
  1217. {
  1218. const char *arg;
  1219. if (skip_prefix(line, "want-ref ", &arg)) {
  1220. struct object_id oid;
  1221. struct string_list_item *item;
  1222. struct object *o;
  1223. if (read_ref(arg, &oid)) {
  1224. packet_writer_error(writer, "unknown ref %s", arg);
  1225. die("unknown ref %s", arg);
  1226. }
  1227. item = string_list_append(wanted_refs, arg);
  1228. item->util = oiddup(&oid);
  1229. o = parse_object_or_die(&oid, arg);
  1230. if (!(o->flags & WANTED)) {
  1231. o->flags |= WANTED;
  1232. add_object_array(o, NULL, want_obj);
  1233. }
  1234. return 1;
  1235. }
  1236. return 0;
  1237. }
  1238. static int parse_have(const char *line, struct oid_array *haves)
  1239. {
  1240. const char *arg;
  1241. if (skip_prefix(line, "have ", &arg)) {
  1242. struct object_id oid;
  1243. if (get_oid_hex(arg, &oid))
  1244. die("git upload-pack: expected SHA1 object, got '%s'", arg);
  1245. oid_array_append(haves, &oid);
  1246. return 1;
  1247. }
  1248. return 0;
  1249. }
  1250. static void process_args(struct packet_reader *request,
  1251. struct upload_pack_data *data)
  1252. {
  1253. while (packet_reader_read(request) == PACKET_READ_NORMAL) {
  1254. const char *arg = request->line;
  1255. const char *p;
  1256. /* process want */
  1257. if (parse_want(&data->writer, arg, &data->want_obj))
  1258. continue;
  1259. if (data->allow_ref_in_want &&
  1260. parse_want_ref(&data->writer, arg, &data->wanted_refs,
  1261. &data->want_obj))
  1262. continue;
  1263. /* process have line */
  1264. if (parse_have(arg, &data->haves))
  1265. continue;
  1266. /* process args like thin-pack */
  1267. if (!strcmp(arg, "thin-pack")) {
  1268. data->use_thin_pack = 1;
  1269. continue;
  1270. }
  1271. if (!strcmp(arg, "ofs-delta")) {
  1272. data->use_ofs_delta = 1;
  1273. continue;
  1274. }
  1275. if (!strcmp(arg, "no-progress")) {
  1276. data->no_progress = 1;
  1277. continue;
  1278. }
  1279. if (!strcmp(arg, "include-tag")) {
  1280. data->use_include_tag = 1;
  1281. continue;
  1282. }
  1283. if (!strcmp(arg, "done")) {
  1284. data->done = 1;
  1285. continue;
  1286. }
  1287. /* Shallow related arguments */
  1288. if (process_shallow(arg, &data->shallows))
  1289. continue;
  1290. if (process_deepen(arg, &data->depth))
  1291. continue;
  1292. if (process_deepen_since(arg, &data->deepen_since,
  1293. &data->deepen_rev_list))
  1294. continue;
  1295. if (process_deepen_not(arg, &data->deepen_not,
  1296. &data->deepen_rev_list))
  1297. continue;
  1298. if (!strcmp(arg, "deepen-relative")) {
  1299. data->deepen_relative = 1;
  1300. continue;
  1301. }
  1302. if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
  1303. list_objects_filter_die_if_populated(&data->filter_options);
  1304. parse_list_objects_filter(&data->filter_options, p);
  1305. die_if_using_banned_filter(data);
  1306. continue;
  1307. }
  1308. if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
  1309. data->allow_sideband_all) &&
  1310. !strcmp(arg, "sideband-all")) {
  1311. data->writer.use_sideband = 1;
  1312. continue;
  1313. }
  1314. if (skip_prefix(arg, "packfile-uris ", &p)) {
  1315. string_list_split(&data->uri_protocols, p, ',', -1);
  1316. continue;
  1317. }
  1318. /* ignore unknown lines maybe? */
  1319. die("unexpected line: '%s'", arg);
  1320. }
  1321. if (data->uri_protocols.nr && !data->writer.use_sideband)
  1322. string_list_clear(&data->uri_protocols, 0);
  1323. if (request->status != PACKET_READ_FLUSH)
  1324. die(_("expected flush after fetch arguments"));
  1325. }
  1326. static int process_haves(struct upload_pack_data *data, struct oid_array *common)
  1327. {
  1328. int i;
  1329. /* Process haves */
  1330. for (i = 0; i < data->haves.nr; i++) {
  1331. const struct object_id *oid = &data->haves.oid[i];
  1332. if (!has_object_file_with_flags(oid,
  1333. OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
  1334. continue;
  1335. oid_array_append(common, oid);
  1336. do_got_oid(data, oid);
  1337. }
  1338. return 0;
  1339. }
  1340. static int send_acks(struct upload_pack_data *data, struct oid_array *acks)
  1341. {
  1342. int i;
  1343. packet_writer_write(&data->writer, "acknowledgments\n");
  1344. /* Send Acks */
  1345. if (!acks->nr)
  1346. packet_writer_write(&data->writer, "NAK\n");
  1347. for (i = 0; i < acks->nr; i++) {
  1348. packet_writer_write(&data->writer, "ACK %s\n",
  1349. oid_to_hex(&acks->oid[i]));
  1350. }
  1351. if (ok_to_give_up(data)) {
  1352. /* Send Ready */
  1353. packet_writer_write(&data->writer, "ready\n");
  1354. return 1;
  1355. }
  1356. return 0;
  1357. }
  1358. static int process_haves_and_send_acks(struct upload_pack_data *data)
  1359. {
  1360. struct oid_array common = OID_ARRAY_INIT;
  1361. int ret = 0;
  1362. process_haves(data, &common);
  1363. if (data->done) {
  1364. ret = 1;
  1365. } else if (send_acks(data, &common)) {
  1366. packet_writer_delim(&data->writer);
  1367. ret = 1;
  1368. } else {
  1369. /* Add Flush */
  1370. packet_writer_flush(&data->writer);
  1371. ret = 0;
  1372. }
  1373. oid_array_clear(&data->haves);
  1374. oid_array_clear(&common);
  1375. return ret;
  1376. }
  1377. static void send_wanted_ref_info(struct upload_pack_data *data)
  1378. {
  1379. const struct string_list_item *item;
  1380. if (!data->wanted_refs.nr)
  1381. return;
  1382. packet_writer_write(&data->writer, "wanted-refs\n");
  1383. for_each_string_list_item(item, &data->wanted_refs) {
  1384. packet_writer_write(&data->writer, "%s %s\n",
  1385. oid_to_hex(item->util),
  1386. item->string);
  1387. }
  1388. packet_writer_delim(&data->writer);
  1389. }
  1390. static void send_shallow_info(struct upload_pack_data *data)
  1391. {
  1392. /* No shallow info needs to be sent */
  1393. if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
  1394. !is_repository_shallow(the_repository))
  1395. return;
  1396. packet_writer_write(&data->writer, "shallow-info\n");
  1397. if (!send_shallow_list(data) &&
  1398. is_repository_shallow(the_repository))
  1399. deepen(data, INFINITE_DEPTH);
  1400. packet_delim(1);
  1401. }
  1402. enum fetch_state {
  1403. FETCH_PROCESS_ARGS = 0,
  1404. FETCH_SEND_ACKS,
  1405. FETCH_SEND_PACK,
  1406. FETCH_DONE,
  1407. };
  1408. int upload_pack_v2(struct repository *r, struct strvec *keys,
  1409. struct packet_reader *request)
  1410. {
  1411. enum fetch_state state = FETCH_PROCESS_ARGS;
  1412. struct upload_pack_data data;
  1413. clear_object_flags(ALL_FLAGS);
  1414. upload_pack_data_init(&data);
  1415. data.use_sideband = LARGE_PACKET_MAX;
  1416. git_config(upload_pack_config, &data);
  1417. while (state != FETCH_DONE) {
  1418. switch (state) {
  1419. case FETCH_PROCESS_ARGS:
  1420. process_args(request, &data);
  1421. if (!data.want_obj.nr) {
  1422. /*
  1423. * Request didn't contain any 'want' lines,
  1424. * guess they didn't want anything.
  1425. */
  1426. state = FETCH_DONE;
  1427. } else if (data.haves.nr) {
  1428. /*
  1429. * Request had 'have' lines, so lets ACK them.
  1430. */
  1431. state = FETCH_SEND_ACKS;
  1432. } else {
  1433. /*
  1434. * Request had 'want's but no 'have's so we can
  1435. * immedietly go to construct and send a pack.
  1436. */
  1437. state = FETCH_SEND_PACK;
  1438. }
  1439. break;
  1440. case FETCH_SEND_ACKS:
  1441. if (process_haves_and_send_acks(&data))
  1442. state = FETCH_SEND_PACK;
  1443. else
  1444. state = FETCH_DONE;
  1445. break;
  1446. case FETCH_SEND_PACK:
  1447. send_wanted_ref_info(&data);
  1448. send_shallow_info(&data);
  1449. if (data.uri_protocols.nr) {
  1450. create_pack_file(&data, &data.uri_protocols);
  1451. } else {
  1452. packet_writer_write(&data.writer, "packfile\n");
  1453. create_pack_file(&data, NULL);
  1454. }
  1455. state = FETCH_DONE;
  1456. break;
  1457. case FETCH_DONE:
  1458. continue;
  1459. }
  1460. }
  1461. upload_pack_data_clear(&data);
  1462. return 0;
  1463. }
  1464. int upload_pack_advertise(struct repository *r,
  1465. struct strbuf *value)
  1466. {
  1467. if (value) {
  1468. int allow_filter_value;
  1469. int allow_ref_in_want;
  1470. int allow_sideband_all_value;
  1471. char *str = NULL;
  1472. strbuf_addstr(value, "shallow");
  1473. if (!repo_config_get_bool(the_repository,
  1474. "uploadpack.allowfilter",
  1475. &allow_filter_value) &&
  1476. allow_filter_value)
  1477. strbuf_addstr(value, " filter");
  1478. if (!repo_config_get_bool(the_repository,
  1479. "uploadpack.allowrefinwant",
  1480. &allow_ref_in_want) &&
  1481. allow_ref_in_want)
  1482. strbuf_addstr(value, " ref-in-want");
  1483. if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) ||
  1484. (!repo_config_get_bool(the_repository,
  1485. "uploadpack.allowsidebandall",
  1486. &allow_sideband_all_value) &&
  1487. allow_sideband_all_value))
  1488. strbuf_addstr(value, " sideband-all");
  1489. if (!repo_config_get_string(the_repository,
  1490. "uploadpack.blobpackfileuri",
  1491. &str) &&
  1492. str) {
  1493. strbuf_addstr(value, " packfile-uris");
  1494. free(str);
  1495. }
  1496. }
  1497. return 1;
  1498. }