THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1402 lines
37KB

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