transport.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. #include "cache.h"
  2. #include "config.h"
  3. #include "transport.h"
  4. #include "run-command.h"
  5. #include "pkt-line.h"
  6. #include "fetch-pack.h"
  7. #include "remote.h"
  8. #include "connect.h"
  9. #include "send-pack.h"
  10. #include "walker.h"
  11. #include "bundle.h"
  12. #include "dir.h"
  13. #include "refs.h"
  14. #include "refspec.h"
  15. #include "branch.h"
  16. #include "url.h"
  17. #include "submodule.h"
  18. #include "string-list.h"
  19. #include "oid-array.h"
  20. #include "sigchain.h"
  21. #include "transport-internal.h"
  22. #include "protocol.h"
  23. #include "object-store.h"
  24. #include "color.h"
  25. static int transport_use_color = -1;
  26. static char transport_colors[][COLOR_MAXLEN] = {
  27. GIT_COLOR_RESET,
  28. GIT_COLOR_RED /* REJECTED */
  29. };
  30. enum color_transport {
  31. TRANSPORT_COLOR_RESET = 0,
  32. TRANSPORT_COLOR_REJECTED = 1
  33. };
  34. static int transport_color_config(void)
  35. {
  36. const char *keys[] = {
  37. "color.transport.reset",
  38. "color.transport.rejected"
  39. }, *key = "color.transport";
  40. char *value;
  41. int i;
  42. static int initialized;
  43. if (initialized)
  44. return 0;
  45. initialized = 1;
  46. if (!git_config_get_string(key, &value))
  47. transport_use_color = git_config_colorbool(key, value);
  48. if (!want_color_stderr(transport_use_color))
  49. return 0;
  50. for (i = 0; i < ARRAY_SIZE(keys); i++)
  51. if (!git_config_get_string(keys[i], &value)) {
  52. if (!value)
  53. return config_error_nonbool(keys[i]);
  54. if (color_parse(value, transport_colors[i]) < 0)
  55. return -1;
  56. }
  57. return 0;
  58. }
  59. static const char *transport_get_color(enum color_transport ix)
  60. {
  61. if (want_color_stderr(transport_use_color))
  62. return transport_colors[ix];
  63. return "";
  64. }
  65. static void set_upstreams(struct transport *transport, struct ref *refs,
  66. int pretend)
  67. {
  68. struct ref *ref;
  69. for (ref = refs; ref; ref = ref->next) {
  70. const char *localname;
  71. const char *tmp;
  72. const char *remotename;
  73. int flag = 0;
  74. /*
  75. * Check suitability for tracking. Must be successful /
  76. * already up-to-date ref create/modify (not delete).
  77. */
  78. if (ref->status != REF_STATUS_OK &&
  79. ref->status != REF_STATUS_UPTODATE)
  80. continue;
  81. if (!ref->peer_ref)
  82. continue;
  83. if (is_null_oid(&ref->new_oid))
  84. continue;
  85. /* Follow symbolic refs (mainly for HEAD). */
  86. localname = ref->peer_ref->name;
  87. remotename = ref->name;
  88. tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
  89. NULL, &flag);
  90. if (tmp && flag & REF_ISSYMREF &&
  91. starts_with(tmp, "refs/heads/"))
  92. localname = tmp;
  93. /* Both source and destination must be local branches. */
  94. if (!localname || !starts_with(localname, "refs/heads/"))
  95. continue;
  96. if (!remotename || !starts_with(remotename, "refs/heads/"))
  97. continue;
  98. if (!pretend)
  99. install_branch_config(BRANCH_CONFIG_VERBOSE,
  100. localname + 11, transport->remote->name,
  101. remotename);
  102. else
  103. printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
  104. localname + 11, remotename + 11,
  105. transport->remote->name);
  106. }
  107. }
  108. struct bundle_transport_data {
  109. int fd;
  110. struct bundle_header header;
  111. unsigned get_refs_from_bundle_called : 1;
  112. };
  113. static struct ref *get_refs_from_bundle(struct transport *transport,
  114. int for_push,
  115. const struct strvec *ref_prefixes)
  116. {
  117. struct bundle_transport_data *data = transport->data;
  118. struct ref *result = NULL;
  119. int i;
  120. if (for_push)
  121. return NULL;
  122. data->get_refs_from_bundle_called = 1;
  123. if (data->fd > 0)
  124. close(data->fd);
  125. data->fd = read_bundle_header(transport->url, &data->header);
  126. if (data->fd < 0)
  127. die(_("could not read bundle '%s'"), transport->url);
  128. transport->hash_algo = data->header.hash_algo;
  129. for (i = 0; i < data->header.references.nr; i++) {
  130. struct ref_list_entry *e = data->header.references.list + i;
  131. struct ref *ref = alloc_ref(e->name);
  132. oidcpy(&ref->old_oid, &e->oid);
  133. ref->next = result;
  134. result = ref;
  135. }
  136. return result;
  137. }
  138. static int fetch_refs_from_bundle(struct transport *transport,
  139. int nr_heads, struct ref **to_fetch)
  140. {
  141. struct bundle_transport_data *data = transport->data;
  142. int ret;
  143. if (!data->get_refs_from_bundle_called)
  144. get_refs_from_bundle(transport, 0, NULL);
  145. ret = unbundle(the_repository, &data->header, data->fd,
  146. transport->progress ? BUNDLE_VERBOSE : 0);
  147. transport->hash_algo = data->header.hash_algo;
  148. return ret;
  149. }
  150. static int close_bundle(struct transport *transport)
  151. {
  152. struct bundle_transport_data *data = transport->data;
  153. if (data->fd > 0)
  154. close(data->fd);
  155. free(data);
  156. return 0;
  157. }
  158. struct git_transport_data {
  159. struct git_transport_options options;
  160. struct child_process *conn;
  161. int fd[2];
  162. unsigned got_remote_heads : 1;
  163. enum protocol_version version;
  164. struct oid_array extra_have;
  165. struct oid_array shallow;
  166. };
  167. static int set_git_option(struct git_transport_options *opts,
  168. const char *name, const char *value)
  169. {
  170. if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
  171. opts->uploadpack = value;
  172. return 0;
  173. } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
  174. opts->receivepack = value;
  175. return 0;
  176. } else if (!strcmp(name, TRANS_OPT_THIN)) {
  177. opts->thin = !!value;
  178. return 0;
  179. } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
  180. opts->followtags = !!value;
  181. return 0;
  182. } else if (!strcmp(name, TRANS_OPT_KEEP)) {
  183. opts->keep = !!value;
  184. return 0;
  185. } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
  186. opts->update_shallow = !!value;
  187. return 0;
  188. } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
  189. if (!value)
  190. opts->depth = 0;
  191. else {
  192. char *end;
  193. opts->depth = strtol(value, &end, 0);
  194. if (*end)
  195. die(_("transport: invalid depth option '%s'"), value);
  196. }
  197. return 0;
  198. } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
  199. opts->deepen_since = value;
  200. return 0;
  201. } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
  202. opts->deepen_not = (const struct string_list *)value;
  203. return 0;
  204. } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
  205. opts->deepen_relative = !!value;
  206. return 0;
  207. } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
  208. opts->from_promisor = !!value;
  209. return 0;
  210. } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
  211. list_objects_filter_die_if_populated(&opts->filter_options);
  212. parse_list_objects_filter(&opts->filter_options, value);
  213. return 0;
  214. }
  215. return 1;
  216. }
  217. static int connect_setup(struct transport *transport, int for_push)
  218. {
  219. struct git_transport_data *data = transport->data;
  220. int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
  221. if (data->conn)
  222. return 0;
  223. switch (transport->family) {
  224. case TRANSPORT_FAMILY_ALL: break;
  225. case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
  226. case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
  227. }
  228. data->conn = git_connect(data->fd, transport->url,
  229. for_push ? data->options.receivepack :
  230. data->options.uploadpack,
  231. flags);
  232. return 0;
  233. }
  234. static void die_if_server_options(struct transport *transport)
  235. {
  236. if (!transport->server_options || !transport->server_options->nr)
  237. return;
  238. advise(_("see protocol.version in 'git help config' for more details"));
  239. die(_("server options require protocol version 2 or later"));
  240. }
  241. /*
  242. * Obtains the protocol version from the transport and writes it to
  243. * transport->data->version, first connecting if not already connected.
  244. *
  245. * If the protocol version is one that allows skipping the listing of remote
  246. * refs, and must_list_refs is 0, the listing of remote refs is skipped and
  247. * this function returns NULL. Otherwise, this function returns the list of
  248. * remote refs.
  249. */
  250. static struct ref *handshake(struct transport *transport, int for_push,
  251. const struct strvec *ref_prefixes,
  252. int must_list_refs)
  253. {
  254. struct git_transport_data *data = transport->data;
  255. struct ref *refs = NULL;
  256. struct packet_reader reader;
  257. connect_setup(transport, for_push);
  258. packet_reader_init(&reader, data->fd[0], NULL, 0,
  259. PACKET_READ_CHOMP_NEWLINE |
  260. PACKET_READ_GENTLE_ON_EOF |
  261. PACKET_READ_DIE_ON_ERR_PACKET);
  262. data->version = discover_version(&reader);
  263. switch (data->version) {
  264. case protocol_v2:
  265. if (must_list_refs)
  266. get_remote_refs(data->fd[1], &reader, &refs, for_push,
  267. ref_prefixes,
  268. transport->server_options,
  269. transport->stateless_rpc);
  270. break;
  271. case protocol_v1:
  272. case protocol_v0:
  273. die_if_server_options(transport);
  274. get_remote_heads(&reader, &refs,
  275. for_push ? REF_NORMAL : 0,
  276. &data->extra_have,
  277. &data->shallow);
  278. break;
  279. case protocol_unknown_version:
  280. BUG("unknown protocol version");
  281. }
  282. data->got_remote_heads = 1;
  283. transport->hash_algo = reader.hash_algo;
  284. if (reader.line_peeked)
  285. BUG("buffer must be empty at the end of handshake()");
  286. return refs;
  287. }
  288. static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
  289. const struct strvec *ref_prefixes)
  290. {
  291. return handshake(transport, for_push, ref_prefixes, 1);
  292. }
  293. static int fetch_refs_via_pack(struct transport *transport,
  294. int nr_heads, struct ref **to_fetch)
  295. {
  296. int ret = 0;
  297. struct git_transport_data *data = transport->data;
  298. struct ref *refs = NULL;
  299. struct fetch_pack_args args;
  300. struct ref *refs_tmp = NULL;
  301. memset(&args, 0, sizeof(args));
  302. args.uploadpack = data->options.uploadpack;
  303. args.keep_pack = data->options.keep;
  304. args.lock_pack = 1;
  305. args.use_thin_pack = data->options.thin;
  306. args.include_tag = data->options.followtags;
  307. args.verbose = (transport->verbose > 1);
  308. args.quiet = (transport->verbose < 0);
  309. args.no_progress = !transport->progress;
  310. args.depth = data->options.depth;
  311. args.deepen_since = data->options.deepen_since;
  312. args.deepen_not = data->options.deepen_not;
  313. args.deepen_relative = data->options.deepen_relative;
  314. args.check_self_contained_and_connected =
  315. data->options.check_self_contained_and_connected;
  316. args.cloning = transport->cloning;
  317. args.update_shallow = data->options.update_shallow;
  318. args.from_promisor = data->options.from_promisor;
  319. args.filter_options = data->options.filter_options;
  320. args.stateless_rpc = transport->stateless_rpc;
  321. args.server_options = transport->server_options;
  322. args.negotiation_tips = data->options.negotiation_tips;
  323. if (!data->got_remote_heads) {
  324. int i;
  325. int must_list_refs = 0;
  326. for (i = 0; i < nr_heads; i++) {
  327. if (!to_fetch[i]->exact_oid) {
  328. must_list_refs = 1;
  329. break;
  330. }
  331. }
  332. refs_tmp = handshake(transport, 0, NULL, must_list_refs);
  333. }
  334. if (data->version == protocol_unknown_version)
  335. BUG("unknown protocol version");
  336. else if (data->version <= protocol_v1)
  337. die_if_server_options(transport);
  338. refs = fetch_pack(&args, data->fd,
  339. refs_tmp ? refs_tmp : transport->remote_refs,
  340. to_fetch, nr_heads, &data->shallow,
  341. &transport->pack_lockfiles, data->version);
  342. close(data->fd[0]);
  343. close(data->fd[1]);
  344. if (finish_connect(data->conn))
  345. ret = -1;
  346. data->conn = NULL;
  347. data->got_remote_heads = 0;
  348. data->options.self_contained_and_connected =
  349. args.self_contained_and_connected;
  350. data->options.connectivity_checked = args.connectivity_checked;
  351. if (refs == NULL)
  352. ret = -1;
  353. if (report_unmatched_refs(to_fetch, nr_heads))
  354. ret = -1;
  355. free_refs(refs_tmp);
  356. free_refs(refs);
  357. return ret;
  358. }
  359. static int push_had_errors(struct ref *ref)
  360. {
  361. for (; ref; ref = ref->next) {
  362. switch (ref->status) {
  363. case REF_STATUS_NONE:
  364. case REF_STATUS_UPTODATE:
  365. case REF_STATUS_OK:
  366. break;
  367. default:
  368. return 1;
  369. }
  370. }
  371. return 0;
  372. }
  373. int transport_refs_pushed(struct ref *ref)
  374. {
  375. for (; ref; ref = ref->next) {
  376. switch(ref->status) {
  377. case REF_STATUS_NONE:
  378. case REF_STATUS_UPTODATE:
  379. break;
  380. default:
  381. return 1;
  382. }
  383. }
  384. return 0;
  385. }
  386. static void update_one_tracking_ref(struct remote *remote, char *refname,
  387. struct object_id *new_oid, int deletion,
  388. int verbose)
  389. {
  390. struct refspec_item rs;
  391. memset(&rs, 0, sizeof(rs));
  392. rs.src = refname;
  393. rs.dst = NULL;
  394. if (!remote_find_tracking(remote, &rs)) {
  395. if (verbose)
  396. fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
  397. if (deletion)
  398. delete_ref(NULL, rs.dst, NULL, 0);
  399. else
  400. update_ref("update by push", rs.dst, new_oid,
  401. NULL, 0, 0);
  402. free(rs.dst);
  403. }
  404. }
  405. void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
  406. {
  407. char *refname;
  408. struct object_id *new_oid;
  409. struct ref_push_report *report;
  410. if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
  411. return;
  412. report = ref->report;
  413. if (!report)
  414. update_one_tracking_ref(remote, ref->name, &ref->new_oid,
  415. ref->deletion, verbose);
  416. else
  417. for (; report; report = report->next) {
  418. refname = report->ref_name ? (char *)report->ref_name : ref->name;
  419. new_oid = report->new_oid ? report->new_oid : &ref->new_oid;
  420. update_one_tracking_ref(remote, refname, new_oid,
  421. is_null_oid(new_oid), verbose);
  422. }
  423. }
  424. static void print_ref_status(char flag, const char *summary,
  425. struct ref *to, struct ref *from, const char *msg,
  426. struct ref_push_report *report,
  427. int porcelain, int summary_width)
  428. {
  429. const char *to_name;
  430. if (report && report->ref_name)
  431. to_name = report->ref_name;
  432. else
  433. to_name = to->name;
  434. if (porcelain) {
  435. if (from)
  436. fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name);
  437. else
  438. fprintf(stdout, "%c\t:%s\t", flag, to_name);
  439. if (msg)
  440. fprintf(stdout, "%s (%s)\n", summary, msg);
  441. else
  442. fprintf(stdout, "%s\n", summary);
  443. } else {
  444. const char *red = "", *reset = "";
  445. if (push_had_errors(to)) {
  446. red = transport_get_color(TRANSPORT_COLOR_REJECTED);
  447. reset = transport_get_color(TRANSPORT_COLOR_RESET);
  448. }
  449. fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
  450. summary, reset);
  451. if (from)
  452. fprintf(stderr, "%s -> %s",
  453. prettify_refname(from->name),
  454. prettify_refname(to_name));
  455. else
  456. fputs(prettify_refname(to_name), stderr);
  457. if (msg) {
  458. fputs(" (", stderr);
  459. fputs(msg, stderr);
  460. fputc(')', stderr);
  461. }
  462. fputc('\n', stderr);
  463. }
  464. }
  465. static void print_ok_ref_status(struct ref *ref,
  466. struct ref_push_report *report,
  467. int porcelain, int summary_width)
  468. {
  469. struct object_id *old_oid;
  470. struct object_id *new_oid;
  471. const char *ref_name;
  472. int forced_update;
  473. if (report && report->old_oid)
  474. old_oid = report->old_oid;
  475. else
  476. old_oid = &ref->old_oid;
  477. if (report && report->new_oid)
  478. new_oid = report->new_oid;
  479. else
  480. new_oid = &ref->new_oid;
  481. if (report && report->forced_update)
  482. forced_update = report->forced_update;
  483. else
  484. forced_update = ref->forced_update;
  485. if (report && report->ref_name)
  486. ref_name = report->ref_name;
  487. else
  488. ref_name = ref->name;
  489. if (ref->deletion)
  490. print_ref_status('-', "[deleted]", ref, NULL, NULL,
  491. report, porcelain, summary_width);
  492. else if (is_null_oid(old_oid))
  493. print_ref_status('*',
  494. (starts_with(ref_name, "refs/tags/")
  495. ? "[new tag]"
  496. : (starts_with(ref_name, "refs/heads/")
  497. ? "[new branch]"
  498. : "[new reference]")),
  499. ref, ref->peer_ref, NULL,
  500. report, porcelain, summary_width);
  501. else {
  502. struct strbuf quickref = STRBUF_INIT;
  503. char type;
  504. const char *msg;
  505. strbuf_add_unique_abbrev(&quickref, old_oid,
  506. DEFAULT_ABBREV);
  507. if (forced_update) {
  508. strbuf_addstr(&quickref, "...");
  509. type = '+';
  510. msg = "forced update";
  511. } else {
  512. strbuf_addstr(&quickref, "..");
  513. type = ' ';
  514. msg = NULL;
  515. }
  516. strbuf_add_unique_abbrev(&quickref, new_oid,
  517. DEFAULT_ABBREV);
  518. print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
  519. report, porcelain, summary_width);
  520. strbuf_release(&quickref);
  521. }
  522. }
  523. static int print_one_push_report(struct ref *ref, const char *dest, int count,
  524. struct ref_push_report *report,
  525. int porcelain, int summary_width)
  526. {
  527. if (!count) {
  528. char *url = transport_anonymize_url(dest);
  529. fprintf(porcelain ? stdout : stderr, "To %s\n", url);
  530. free(url);
  531. }
  532. switch(ref->status) {
  533. case REF_STATUS_NONE:
  534. print_ref_status('X', "[no match]", ref, NULL, NULL,
  535. report, porcelain, summary_width);
  536. break;
  537. case REF_STATUS_REJECT_NODELETE:
  538. print_ref_status('!', "[rejected]", ref, NULL,
  539. "remote does not support deleting refs",
  540. report, porcelain, summary_width);
  541. break;
  542. case REF_STATUS_UPTODATE:
  543. print_ref_status('=', "[up to date]", ref,
  544. ref->peer_ref, NULL,
  545. report, porcelain, summary_width);
  546. break;
  547. case REF_STATUS_REJECT_NONFASTFORWARD:
  548. print_ref_status('!', "[rejected]", ref, ref->peer_ref,
  549. "non-fast-forward",
  550. report, porcelain, summary_width);
  551. break;
  552. case REF_STATUS_REJECT_ALREADY_EXISTS:
  553. print_ref_status('!', "[rejected]", ref, ref->peer_ref,
  554. "already exists",
  555. report, porcelain, summary_width);
  556. break;
  557. case REF_STATUS_REJECT_FETCH_FIRST:
  558. print_ref_status('!', "[rejected]", ref, ref->peer_ref,
  559. "fetch first",
  560. report, porcelain, summary_width);
  561. break;
  562. case REF_STATUS_REJECT_NEEDS_FORCE:
  563. print_ref_status('!', "[rejected]", ref, ref->peer_ref,
  564. "needs force",
  565. report, porcelain, summary_width);
  566. break;
  567. case REF_STATUS_REJECT_STALE:
  568. print_ref_status('!', "[rejected]", ref, ref->peer_ref,
  569. "stale info",
  570. report, porcelain, summary_width);
  571. break;
  572. case REF_STATUS_REJECT_SHALLOW:
  573. print_ref_status('!', "[rejected]", ref, ref->peer_ref,
  574. "new shallow roots not allowed",
  575. report, porcelain, summary_width);
  576. break;
  577. case REF_STATUS_REMOTE_REJECT:
  578. print_ref_status('!', "[remote rejected]", ref,
  579. ref->deletion ? NULL : ref->peer_ref,
  580. ref->remote_status,
  581. report, porcelain, summary_width);
  582. break;
  583. case REF_STATUS_EXPECTING_REPORT:
  584. print_ref_status('!', "[remote failure]", ref,
  585. ref->deletion ? NULL : ref->peer_ref,
  586. "remote failed to report status",
  587. report, porcelain, summary_width);
  588. break;
  589. case REF_STATUS_ATOMIC_PUSH_FAILED:
  590. print_ref_status('!', "[rejected]", ref, ref->peer_ref,
  591. "atomic push failed",
  592. report, porcelain, summary_width);
  593. break;
  594. case REF_STATUS_OK:
  595. print_ok_ref_status(ref, report, porcelain, summary_width);
  596. break;
  597. }
  598. return 1;
  599. }
  600. static int print_one_push_status(struct ref *ref, const char *dest, int count,
  601. int porcelain, int summary_width)
  602. {
  603. struct ref_push_report *report;
  604. int n = 0;
  605. if (!ref->report)
  606. return print_one_push_report(ref, dest, count,
  607. NULL, porcelain, summary_width);
  608. for (report = ref->report; report; report = report->next)
  609. print_one_push_report(ref, dest, count + n++,
  610. report, porcelain, summary_width);
  611. return n;
  612. }
  613. static int measure_abbrev(const struct object_id *oid, int sofar)
  614. {
  615. char hex[GIT_MAX_HEXSZ + 1];
  616. int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
  617. return (w < sofar) ? sofar : w;
  618. }
  619. int transport_summary_width(const struct ref *refs)
  620. {
  621. int maxw = -1;
  622. for (; refs; refs = refs->next) {
  623. maxw = measure_abbrev(&refs->old_oid, maxw);
  624. maxw = measure_abbrev(&refs->new_oid, maxw);
  625. }
  626. if (maxw < 0)
  627. maxw = FALLBACK_DEFAULT_ABBREV;
  628. return (2 * maxw + 3);
  629. }
  630. void transport_print_push_status(const char *dest, struct ref *refs,
  631. int verbose, int porcelain, unsigned int *reject_reasons)
  632. {
  633. struct ref *ref;
  634. int n = 0;
  635. char *head;
  636. int summary_width = transport_summary_width(refs);
  637. if (transport_color_config() < 0)
  638. warning(_("could not parse transport.color.* config"));
  639. head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
  640. if (verbose) {
  641. for (ref = refs; ref; ref = ref->next)
  642. if (ref->status == REF_STATUS_UPTODATE)
  643. n += print_one_push_status(ref, dest, n,
  644. porcelain, summary_width);
  645. }
  646. for (ref = refs; ref; ref = ref->next)
  647. if (ref->status == REF_STATUS_OK)
  648. n += print_one_push_status(ref, dest, n,
  649. porcelain, summary_width);
  650. *reject_reasons = 0;
  651. for (ref = refs; ref; ref = ref->next) {
  652. if (ref->status != REF_STATUS_NONE &&
  653. ref->status != REF_STATUS_UPTODATE &&
  654. ref->status != REF_STATUS_OK)
  655. n += print_one_push_status(ref, dest, n,
  656. porcelain, summary_width);
  657. if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
  658. if (head != NULL && !strcmp(head, ref->name))
  659. *reject_reasons |= REJECT_NON_FF_HEAD;
  660. else
  661. *reject_reasons |= REJECT_NON_FF_OTHER;
  662. } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
  663. *reject_reasons |= REJECT_ALREADY_EXISTS;
  664. } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
  665. *reject_reasons |= REJECT_FETCH_FIRST;
  666. } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
  667. *reject_reasons |= REJECT_NEEDS_FORCE;
  668. }
  669. }
  670. free(head);
  671. }
  672. static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
  673. {
  674. struct git_transport_data *data = transport->data;
  675. struct send_pack_args args;
  676. int ret = 0;
  677. if (transport_color_config() < 0)
  678. return -1;
  679. if (!data->got_remote_heads)
  680. get_refs_via_connect(transport, 1, NULL);
  681. memset(&args, 0, sizeof(args));
  682. args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
  683. args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
  684. args.use_thin_pack = data->options.thin;
  685. args.verbose = (transport->verbose > 0);
  686. args.quiet = (transport->verbose < 0);
  687. args.progress = transport->progress;
  688. args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
  689. args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
  690. args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
  691. args.push_options = transport->push_options;
  692. args.url = transport->url;
  693. if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
  694. args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
  695. else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
  696. args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
  697. else
  698. args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
  699. switch (data->version) {
  700. case protocol_v2:
  701. die(_("support for protocol v2 not implemented yet"));
  702. break;
  703. case protocol_v1:
  704. case protocol_v0:
  705. ret = send_pack(&args, data->fd, data->conn, remote_refs,
  706. &data->extra_have);
  707. break;
  708. case protocol_unknown_version:
  709. BUG("unknown protocol version");
  710. }
  711. close(data->fd[1]);
  712. close(data->fd[0]);
  713. /*
  714. * Atomic push may abort the connection early and close the pipe,
  715. * which may cause an error for `finish_connect()`. Ignore this error
  716. * for atomic git-push.
  717. */
  718. if (ret || args.atomic)
  719. finish_connect(data->conn);
  720. else
  721. ret = finish_connect(data->conn);
  722. data->conn = NULL;
  723. data->got_remote_heads = 0;
  724. return ret;
  725. }
  726. static int connect_git(struct transport *transport, const char *name,
  727. const char *executable, int fd[2])
  728. {
  729. struct git_transport_data *data = transport->data;
  730. data->conn = git_connect(data->fd, transport->url,
  731. executable, 0);
  732. fd[0] = data->fd[0];
  733. fd[1] = data->fd[1];
  734. return 0;
  735. }
  736. static int disconnect_git(struct transport *transport)
  737. {
  738. struct git_transport_data *data = transport->data;
  739. if (data->conn) {
  740. if (data->got_remote_heads && !transport->stateless_rpc)
  741. packet_flush(data->fd[1]);
  742. close(data->fd[0]);
  743. close(data->fd[1]);
  744. finish_connect(data->conn);
  745. }
  746. free(data);
  747. return 0;
  748. }
  749. static struct transport_vtable taken_over_vtable = {
  750. NULL,
  751. get_refs_via_connect,
  752. fetch_refs_via_pack,
  753. git_transport_push,
  754. NULL,
  755. disconnect_git
  756. };
  757. void transport_take_over(struct transport *transport,
  758. struct child_process *child)
  759. {
  760. struct git_transport_data *data;
  761. if (!transport->smart_options)
  762. BUG("taking over transport requires non-NULL "
  763. "smart_options field.");
  764. data = xcalloc(1, sizeof(*data));
  765. data->options = *transport->smart_options;
  766. data->conn = child;
  767. data->fd[0] = data->conn->out;
  768. data->fd[1] = data->conn->in;
  769. data->got_remote_heads = 0;
  770. transport->data = data;
  771. transport->vtable = &taken_over_vtable;
  772. transport->smart_options = &(data->options);
  773. transport->cannot_reuse = 1;
  774. }
  775. static int is_file(const char *url)
  776. {
  777. struct stat buf;
  778. if (stat(url, &buf))
  779. return 0;
  780. return S_ISREG(buf.st_mode);
  781. }
  782. static int external_specification_len(const char *url)
  783. {
  784. return strchr(url, ':') - url;
  785. }
  786. static const struct string_list *protocol_whitelist(void)
  787. {
  788. static int enabled = -1;
  789. static struct string_list allowed = STRING_LIST_INIT_DUP;
  790. if (enabled < 0) {
  791. const char *v = getenv("GIT_ALLOW_PROTOCOL");
  792. if (v) {
  793. string_list_split(&allowed, v, ':', -1);
  794. string_list_sort(&allowed);
  795. enabled = 1;
  796. } else {
  797. enabled = 0;
  798. }
  799. }
  800. return enabled ? &allowed : NULL;
  801. }
  802. enum protocol_allow_config {
  803. PROTOCOL_ALLOW_NEVER = 0,
  804. PROTOCOL_ALLOW_USER_ONLY,
  805. PROTOCOL_ALLOW_ALWAYS
  806. };
  807. static enum protocol_allow_config parse_protocol_config(const char *key,
  808. const char *value)
  809. {
  810. if (!strcasecmp(value, "always"))
  811. return PROTOCOL_ALLOW_ALWAYS;
  812. else if (!strcasecmp(value, "never"))
  813. return PROTOCOL_ALLOW_NEVER;
  814. else if (!strcasecmp(value, "user"))
  815. return PROTOCOL_ALLOW_USER_ONLY;
  816. die(_("unknown value for config '%s': %s"), key, value);
  817. }
  818. static enum protocol_allow_config get_protocol_config(const char *type)
  819. {
  820. char *key = xstrfmt("protocol.%s.allow", type);
  821. char *value;
  822. /* first check the per-protocol config */
  823. if (!git_config_get_string(key, &value)) {
  824. enum protocol_allow_config ret =
  825. parse_protocol_config(key, value);
  826. free(key);
  827. free(value);
  828. return ret;
  829. }
  830. free(key);
  831. /* if defined, fallback to user-defined default for unknown protocols */
  832. if (!git_config_get_string("protocol.allow", &value)) {
  833. enum protocol_allow_config ret =
  834. parse_protocol_config("protocol.allow", value);
  835. free(value);
  836. return ret;
  837. }
  838. /* fallback to built-in defaults */
  839. /* known safe */
  840. if (!strcmp(type, "http") ||
  841. !strcmp(type, "https") ||
  842. !strcmp(type, "git") ||
  843. !strcmp(type, "ssh") ||
  844. !strcmp(type, "file"))
  845. return PROTOCOL_ALLOW_ALWAYS;
  846. /* known scary; err on the side of caution */
  847. if (!strcmp(type, "ext"))
  848. return PROTOCOL_ALLOW_NEVER;
  849. /* unknown; by default let them be used only directly by the user */
  850. return PROTOCOL_ALLOW_USER_ONLY;
  851. }
  852. int is_transport_allowed(const char *type, int from_user)
  853. {
  854. const struct string_list *whitelist = protocol_whitelist();
  855. if (whitelist)
  856. return string_list_has_string(whitelist, type);
  857. switch (get_protocol_config(type)) {
  858. case PROTOCOL_ALLOW_ALWAYS:
  859. return 1;
  860. case PROTOCOL_ALLOW_NEVER:
  861. return 0;
  862. case PROTOCOL_ALLOW_USER_ONLY:
  863. if (from_user < 0)
  864. from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
  865. return from_user;
  866. }
  867. BUG("invalid protocol_allow_config type");
  868. }
  869. void transport_check_allowed(const char *type)
  870. {
  871. if (!is_transport_allowed(type, -1))
  872. die(_("transport '%s' not allowed"), type);
  873. }
  874. static struct transport_vtable bundle_vtable = {
  875. NULL,
  876. get_refs_from_bundle,
  877. fetch_refs_from_bundle,
  878. NULL,
  879. NULL,
  880. close_bundle
  881. };
  882. static struct transport_vtable builtin_smart_vtable = {
  883. NULL,
  884. get_refs_via_connect,
  885. fetch_refs_via_pack,
  886. git_transport_push,
  887. connect_git,
  888. disconnect_git
  889. };
  890. struct transport *transport_get(struct remote *remote, const char *url)
  891. {
  892. const char *helper;
  893. struct transport *ret = xcalloc(1, sizeof(*ret));
  894. ret->progress = isatty(2);
  895. string_list_init(&ret->pack_lockfiles, 1);
  896. if (!remote)
  897. BUG("No remote provided to transport_get()");
  898. ret->got_remote_refs = 0;
  899. ret->remote = remote;
  900. helper = remote->foreign_vcs;
  901. if (!url && remote->url)
  902. url = remote->url[0];
  903. ret->url = url;
  904. /* maybe it is a foreign URL? */
  905. if (url) {
  906. const char *p = url;
  907. while (is_urlschemechar(p == url, *p))
  908. p++;
  909. if (starts_with(p, "::"))
  910. helper = xstrndup(url, p - url);
  911. }
  912. if (helper) {
  913. transport_helper_init(ret, helper);
  914. } else if (starts_with(url, "rsync:")) {
  915. die(_("git-over-rsync is no longer supported"));
  916. } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
  917. struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
  918. transport_check_allowed("file");
  919. ret->data = data;
  920. ret->vtable = &bundle_vtable;
  921. ret->smart_options = NULL;
  922. } else if (!is_url(url)
  923. || starts_with(url, "file://")
  924. || starts_with(url, "git://")
  925. || starts_with(url, "ssh://")
  926. || starts_with(url, "git+ssh://") /* deprecated - do not use */
  927. || starts_with(url, "ssh+git://") /* deprecated - do not use */
  928. ) {
  929. /*
  930. * These are builtin smart transports; "allowed" transports
  931. * will be checked individually in git_connect.
  932. */
  933. struct git_transport_data *data = xcalloc(1, sizeof(*data));
  934. ret->data = data;
  935. ret->vtable = &builtin_smart_vtable;
  936. ret->smart_options = &(data->options);
  937. data->conn = NULL;
  938. data->got_remote_heads = 0;
  939. } else {
  940. /* Unknown protocol in URL. Pass to external handler. */
  941. int len = external_specification_len(url);
  942. char *handler = xmemdupz(url, len);
  943. transport_helper_init(ret, handler);
  944. }
  945. if (ret->smart_options) {
  946. ret->smart_options->thin = 1;
  947. ret->smart_options->uploadpack = "git-upload-pack";
  948. if (remote->uploadpack)
  949. ret->smart_options->uploadpack = remote->uploadpack;
  950. ret->smart_options->receivepack = "git-receive-pack";
  951. if (remote->receivepack)
  952. ret->smart_options->receivepack = remote->receivepack;
  953. }
  954. ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
  955. return ret;
  956. }
  957. const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
  958. {
  959. return transport->hash_algo;
  960. }
  961. int transport_set_option(struct transport *transport,
  962. const char *name, const char *value)
  963. {
  964. int git_reports = 1, protocol_reports = 1;
  965. if (transport->smart_options)
  966. git_reports = set_git_option(transport->smart_options,
  967. name, value);
  968. if (transport->vtable->set_option)
  969. protocol_reports = transport->vtable->set_option(transport,
  970. name, value);
  971. /* If either report is 0, report 0 (success). */
  972. if (!git_reports || !protocol_reports)
  973. return 0;
  974. /* If either reports -1 (invalid value), report -1. */
  975. if ((git_reports == -1) || (protocol_reports == -1))
  976. return -1;
  977. /* Otherwise if both report unknown, report unknown. */
  978. return 1;
  979. }
  980. void transport_set_verbosity(struct transport *transport, int verbosity,
  981. int force_progress)
  982. {
  983. if (verbosity >= 1)
  984. transport->verbose = verbosity <= 3 ? verbosity : 3;
  985. if (verbosity < 0)
  986. transport->verbose = -1;
  987. /**
  988. * Rules used to determine whether to report progress (processing aborts
  989. * when a rule is satisfied):
  990. *
  991. * . Report progress, if force_progress is 1 (ie. --progress).
  992. * . Don't report progress, if force_progress is 0 (ie. --no-progress).
  993. * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
  994. * . Report progress if isatty(2) is 1.
  995. **/
  996. if (force_progress >= 0)
  997. transport->progress = !!force_progress;
  998. else
  999. transport->progress = verbosity >= 0 && isatty(2);
  1000. }
  1001. static void die_with_unpushed_submodules(struct string_list *needs_pushing)
  1002. {
  1003. int i;
  1004. fprintf(stderr, _("The following submodule paths contain changes that can\n"
  1005. "not be found on any remote:\n"));
  1006. for (i = 0; i < needs_pushing->nr; i++)
  1007. fprintf(stderr, " %s\n", needs_pushing->items[i].string);
  1008. fprintf(stderr, _("\nPlease try\n\n"
  1009. " git push --recurse-submodules=on-demand\n\n"
  1010. "or cd to the path and use\n\n"
  1011. " git push\n\n"
  1012. "to push them to a remote.\n\n"));
  1013. string_list_clear(needs_pushing, 0);
  1014. die(_("Aborting."));
  1015. }
  1016. static int run_pre_push_hook(struct transport *transport,
  1017. struct ref *remote_refs)
  1018. {
  1019. int ret = 0, x;
  1020. struct ref *r;
  1021. struct child_process proc = CHILD_PROCESS_INIT;
  1022. struct strbuf buf;
  1023. const char *argv[4];
  1024. if (!(argv[0] = find_hook("pre-push")))
  1025. return 0;
  1026. argv[1] = transport->remote->name;
  1027. argv[2] = transport->url;
  1028. argv[3] = NULL;
  1029. proc.argv = argv;
  1030. proc.in = -1;
  1031. proc.trace2_hook_name = "pre-push";
  1032. if (start_command(&proc)) {
  1033. finish_command(&proc);
  1034. return -1;
  1035. }
  1036. sigchain_push(SIGPIPE, SIG_IGN);
  1037. strbuf_init(&buf, 256);
  1038. for (r = remote_refs; r; r = r->next) {
  1039. if (!r->peer_ref) continue;
  1040. if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
  1041. if (r->status == REF_STATUS_REJECT_STALE) continue;
  1042. if (r->status == REF_STATUS_UPTODATE) continue;
  1043. strbuf_reset(&buf);
  1044. strbuf_addf( &buf, "%s %s %s %s\n",
  1045. r->peer_ref->name, oid_to_hex(&r->new_oid),
  1046. r->name, oid_to_hex(&r->old_oid));
  1047. if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
  1048. /* We do not mind if a hook does not read all refs. */
  1049. if (errno != EPIPE)
  1050. ret = -1;
  1051. break;
  1052. }
  1053. }
  1054. strbuf_release(&buf);
  1055. x = close(proc.in);
  1056. if (!ret)
  1057. ret = x;
  1058. sigchain_pop(SIGPIPE);
  1059. x = finish_command(&proc);
  1060. if (!ret)
  1061. ret = x;
  1062. return ret;
  1063. }
  1064. int transport_push(struct repository *r,
  1065. struct transport *transport,
  1066. struct refspec *rs, int flags,
  1067. unsigned int *reject_reasons)
  1068. {
  1069. *reject_reasons = 0;
  1070. if (transport_color_config() < 0)
  1071. return -1;
  1072. if (transport->vtable->push_refs) {
  1073. struct ref *remote_refs;
  1074. struct ref *local_refs = get_local_heads();
  1075. int match_flags = MATCH_REFS_NONE;
  1076. int verbose = (transport->verbose > 0);
  1077. int quiet = (transport->verbose < 0);
  1078. int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
  1079. int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
  1080. int push_ret, ret, err;
  1081. struct strvec ref_prefixes = STRVEC_INIT;
  1082. if (check_push_refs(local_refs, rs) < 0)
  1083. return -1;
  1084. refspec_ref_prefixes(rs, &ref_prefixes);
  1085. trace2_region_enter("transport_push", "get_refs_list", r);
  1086. remote_refs = transport->vtable->get_refs_list(transport, 1,
  1087. &ref_prefixes);
  1088. trace2_region_leave("transport_push", "get_refs_list", r);
  1089. strvec_clear(&ref_prefixes);
  1090. if (flags & TRANSPORT_PUSH_ALL)
  1091. match_flags |= MATCH_REFS_ALL;
  1092. if (flags & TRANSPORT_PUSH_MIRROR)
  1093. match_flags |= MATCH_REFS_MIRROR;
  1094. if (flags & TRANSPORT_PUSH_PRUNE)
  1095. match_flags |= MATCH_REFS_PRUNE;
  1096. if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
  1097. match_flags |= MATCH_REFS_FOLLOW_TAGS;
  1098. if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
  1099. return -1;
  1100. if (transport->smart_options &&
  1101. transport->smart_options->cas &&
  1102. !is_empty_cas(transport->smart_options->cas))
  1103. apply_push_cas(transport->smart_options->cas,
  1104. transport->remote, remote_refs);
  1105. set_ref_status_for_push(remote_refs,
  1106. flags & TRANSPORT_PUSH_MIRROR,
  1107. flags & TRANSPORT_PUSH_FORCE);
  1108. if (!(flags & TRANSPORT_PUSH_NO_HOOK))
  1109. if (run_pre_push_hook(transport, remote_refs))
  1110. return -1;
  1111. if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
  1112. TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
  1113. !is_bare_repository()) {
  1114. struct ref *ref = remote_refs;
  1115. struct oid_array commits = OID_ARRAY_INIT;
  1116. trace2_region_enter("transport_push", "push_submodules", r);
  1117. for (; ref; ref = ref->next)
  1118. if (!is_null_oid(&ref->new_oid))
  1119. oid_array_append(&commits,
  1120. &ref->new_oid);
  1121. if (!push_unpushed_submodules(r,
  1122. &commits,
  1123. transport->remote,
  1124. rs,
  1125. transport->push_options,
  1126. pretend)) {
  1127. oid_array_clear(&commits);
  1128. trace2_region_leave("transport_push", "push_submodules", r);
  1129. die(_("failed to push all needed submodules"));
  1130. }
  1131. oid_array_clear(&commits);
  1132. trace2_region_leave("transport_push", "push_submodules", r);
  1133. }
  1134. if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
  1135. ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
  1136. TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
  1137. !pretend)) && !is_bare_repository()) {
  1138. struct ref *ref = remote_refs;
  1139. struct string_list needs_pushing = STRING_LIST_INIT_DUP;
  1140. struct oid_array commits = OID_ARRAY_INIT;
  1141. trace2_region_enter("transport_push", "check_submodules", r);
  1142. for (; ref; ref = ref->next)
  1143. if (!is_null_oid(&ref->new_oid))
  1144. oid_array_append(&commits,
  1145. &ref->new_oid);
  1146. if (find_unpushed_submodules(r,
  1147. &commits,
  1148. transport->remote->name,
  1149. &needs_pushing)) {
  1150. oid_array_clear(&commits);
  1151. trace2_region_leave("transport_push", "check_submodules", r);
  1152. die_with_unpushed_submodules(&needs_pushing);
  1153. }
  1154. string_list_clear(&needs_pushing, 0);
  1155. oid_array_clear(&commits);
  1156. trace2_region_leave("transport_push", "check_submodules", r);
  1157. }
  1158. if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
  1159. trace2_region_enter("transport_push", "push_refs", r);
  1160. push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
  1161. trace2_region_leave("transport_push", "push_refs", r);
  1162. } else
  1163. push_ret = 0;
  1164. err = push_had_errors(remote_refs);
  1165. ret = push_ret | err;
  1166. if (!quiet || err)
  1167. transport_print_push_status(transport->url, remote_refs,
  1168. verbose | porcelain, porcelain,
  1169. reject_reasons);
  1170. if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
  1171. set_upstreams(transport, remote_refs, pretend);
  1172. if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
  1173. TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
  1174. struct ref *ref;
  1175. for (ref = remote_refs; ref; ref = ref->next)
  1176. transport_update_tracking_ref(transport->remote, ref, verbose);
  1177. }
  1178. if (porcelain && !push_ret)
  1179. puts("Done");
  1180. else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
  1181. fprintf(stderr, "Everything up-to-date\n");
  1182. return ret;
  1183. }
  1184. return 1;
  1185. }
  1186. const struct ref *transport_get_remote_refs(struct transport *transport,
  1187. const struct strvec *ref_prefixes)
  1188. {
  1189. if (!transport->got_remote_refs) {
  1190. transport->remote_refs =
  1191. transport->vtable->get_refs_list(transport, 0,
  1192. ref_prefixes);
  1193. transport->got_remote_refs = 1;
  1194. }
  1195. return transport->remote_refs;
  1196. }
  1197. int transport_fetch_refs(struct transport *transport, struct ref *refs)
  1198. {
  1199. int rc;
  1200. int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
  1201. struct ref **heads = NULL;
  1202. struct ref *rm;
  1203. for (rm = refs; rm; rm = rm->next) {
  1204. nr_refs++;
  1205. if (rm->peer_ref &&
  1206. !is_null_oid(&rm->old_oid) &&
  1207. oideq(&rm->peer_ref->old_oid, &rm->old_oid))
  1208. continue;
  1209. ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
  1210. heads[nr_heads++] = rm;
  1211. }
  1212. if (!nr_heads) {
  1213. /*
  1214. * When deepening of a shallow repository is requested,
  1215. * then local and remote refs are likely to still be equal.
  1216. * Just feed them all to the fetch method in that case.
  1217. * This condition shouldn't be met in a non-deepening fetch
  1218. * (see builtin/fetch.c:quickfetch()).
  1219. */
  1220. ALLOC_ARRAY(heads, nr_refs);
  1221. for (rm = refs; rm; rm = rm->next)
  1222. heads[nr_heads++] = rm;
  1223. }
  1224. rc = transport->vtable->fetch(transport, nr_heads, heads);
  1225. free(heads);
  1226. return rc;
  1227. }
  1228. void transport_unlock_pack(struct transport *transport)
  1229. {
  1230. int i;
  1231. for (i = 0; i < transport->pack_lockfiles.nr; i++)
  1232. unlink_or_warn(transport->pack_lockfiles.items[i].string);
  1233. string_list_clear(&transport->pack_lockfiles, 0);
  1234. }
  1235. int transport_connect(struct transport *transport, const char *name,
  1236. const char *exec, int fd[2])
  1237. {
  1238. if (transport->vtable->connect)
  1239. return transport->vtable->connect(transport, name, exec, fd);
  1240. else
  1241. die(_("operation not supported by protocol"));
  1242. }
  1243. int transport_disconnect(struct transport *transport)
  1244. {
  1245. int ret = 0;
  1246. if (transport->vtable->disconnect)
  1247. ret = transport->vtable->disconnect(transport);
  1248. free(transport);
  1249. return ret;
  1250. }
  1251. /*
  1252. * Strip username (and password) from a URL and return
  1253. * it in a newly allocated string.
  1254. */
  1255. char *transport_anonymize_url(const char *url)
  1256. {
  1257. char *scheme_prefix, *anon_part;
  1258. size_t anon_len, prefix_len = 0;
  1259. anon_part = strchr(url, '@');
  1260. if (url_is_local_not_ssh(url) || !anon_part)
  1261. goto literal_copy;
  1262. anon_len = strlen(++anon_part);
  1263. scheme_prefix = strstr(url, "://");
  1264. if (!scheme_prefix) {
  1265. if (!strchr(anon_part, ':'))
  1266. /* cannot be "me@there:/path/name" */
  1267. goto literal_copy;
  1268. } else {
  1269. const char *cp;
  1270. /* make sure scheme is reasonable */
  1271. for (cp = url; cp < scheme_prefix; cp++) {
  1272. switch (*cp) {
  1273. /* RFC 1738 2.1 */
  1274. case '+': case '.': case '-':
  1275. break; /* ok */
  1276. default:
  1277. if (isalnum(*cp))
  1278. break;
  1279. /* it isn't */
  1280. goto literal_copy;
  1281. }
  1282. }
  1283. /* @ past the first slash does not count */
  1284. cp = strchr(scheme_prefix + 3, '/');
  1285. if (cp && cp < anon_part)
  1286. goto literal_copy;
  1287. prefix_len = scheme_prefix - url + 3;
  1288. }
  1289. return xstrfmt("%.*s%.*s", (int)prefix_len, url,
  1290. (int)anon_len, anon_part);
  1291. literal_copy:
  1292. return xstrdup(url);
  1293. }