send-pack.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "builtin.h"
  2. #include "config.h"
  3. #include "commit.h"
  4. #include "refs.h"
  5. #include "pkt-line.h"
  6. #include "sideband.h"
  7. #include "run-command.h"
  8. #include "remote.h"
  9. #include "connect.h"
  10. #include "send-pack.h"
  11. #include "quote.h"
  12. #include "transport.h"
  13. #include "version.h"
  14. #include "oid-array.h"
  15. #include "gpg-interface.h"
  16. #include "gettext.h"
  17. #include "protocol.h"
  18. static const char * const send_pack_usage[] = {
  19. N_("git send-pack [--all | --mirror] [--dry-run] [--force] "
  20. "[--receive-pack=<git-receive-pack>] [--verbose] [--thin] [--atomic] "
  21. "[<host>:]<directory> [<ref>...]\n"
  22. " --all and explicit <ref> specification are mutually exclusive."),
  23. NULL,
  24. };
  25. static struct send_pack_args args;
  26. static void print_helper_status(struct ref *ref)
  27. {
  28. struct strbuf buf = STRBUF_INIT;
  29. struct ref_push_report *report;
  30. for (; ref; ref = ref->next) {
  31. const char *msg = NULL;
  32. const char *res;
  33. int count = 0;
  34. switch(ref->status) {
  35. case REF_STATUS_NONE:
  36. res = "error";
  37. msg = "no match";
  38. break;
  39. case REF_STATUS_OK:
  40. res = "ok";
  41. break;
  42. case REF_STATUS_UPTODATE:
  43. res = "ok";
  44. msg = "up to date";
  45. break;
  46. case REF_STATUS_REJECT_NONFASTFORWARD:
  47. res = "error";
  48. msg = "non-fast forward";
  49. break;
  50. case REF_STATUS_REJECT_FETCH_FIRST:
  51. res = "error";
  52. msg = "fetch first";
  53. break;
  54. case REF_STATUS_REJECT_NEEDS_FORCE:
  55. res = "error";
  56. msg = "needs force";
  57. break;
  58. case REF_STATUS_REJECT_STALE:
  59. res = "error";
  60. msg = "stale info";
  61. break;
  62. case REF_STATUS_REJECT_ALREADY_EXISTS:
  63. res = "error";
  64. msg = "already exists";
  65. break;
  66. case REF_STATUS_REJECT_NODELETE:
  67. case REF_STATUS_REMOTE_REJECT:
  68. res = "error";
  69. break;
  70. case REF_STATUS_EXPECTING_REPORT:
  71. default:
  72. continue;
  73. }
  74. strbuf_reset(&buf);
  75. strbuf_addf(&buf, "%s %s", res, ref->name);
  76. if (ref->remote_status)
  77. msg = ref->remote_status;
  78. if (msg) {
  79. strbuf_addch(&buf, ' ');
  80. quote_two_c_style(&buf, "", msg, 0);
  81. }
  82. strbuf_addch(&buf, '\n');
  83. if (ref->status == REF_STATUS_OK) {
  84. for (report = ref->report; report; report = report->next) {
  85. if (count++ > 0)
  86. strbuf_addf(&buf, "ok %s\n", ref->name);
  87. if (report->ref_name)
  88. strbuf_addf(&buf, "option refname %s\n",
  89. report->ref_name);
  90. if (report->old_oid)
  91. strbuf_addf(&buf, "option old-oid %s\n",
  92. oid_to_hex(report->old_oid));
  93. if (report->new_oid)
  94. strbuf_addf(&buf, "option new-oid %s\n",
  95. oid_to_hex(report->new_oid));
  96. if (report->forced_update)
  97. strbuf_addstr(&buf, "option forced-update\n");
  98. }
  99. }
  100. write_or_die(1, buf.buf, buf.len);
  101. }
  102. strbuf_release(&buf);
  103. }
  104. static int send_pack_config(const char *k, const char *v, void *cb)
  105. {
  106. git_gpg_config(k, v, NULL);
  107. if (!strcmp(k, "push.gpgsign")) {
  108. const char *value;
  109. if (!git_config_get_value("push.gpgsign", &value)) {
  110. switch (git_parse_maybe_bool(value)) {
  111. case 0:
  112. args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
  113. break;
  114. case 1:
  115. args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
  116. break;
  117. default:
  118. if (value && !strcasecmp(value, "if-asked"))
  119. args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
  120. else
  121. return error("Invalid value for '%s'", k);
  122. }
  123. }
  124. }
  125. return git_default_config(k, v, cb);
  126. }
  127. int cmd_send_pack(int argc, const char **argv, const char *prefix)
  128. {
  129. struct refspec rs = REFSPEC_INIT_PUSH;
  130. const char *remote_name = NULL;
  131. struct remote *remote = NULL;
  132. const char *dest = NULL;
  133. int fd[2];
  134. struct child_process *conn;
  135. struct oid_array extra_have = OID_ARRAY_INIT;
  136. struct oid_array shallow = OID_ARRAY_INIT;
  137. struct ref *remote_refs, *local_refs;
  138. int ret;
  139. int helper_status = 0;
  140. int send_all = 0;
  141. int verbose = 0;
  142. const char *receivepack = "git-receive-pack";
  143. unsigned dry_run = 0;
  144. unsigned send_mirror = 0;
  145. unsigned force_update = 0;
  146. unsigned quiet = 0;
  147. int push_cert = 0;
  148. struct string_list push_options = STRING_LIST_INIT_NODUP;
  149. unsigned use_thin_pack = 0;
  150. unsigned atomic = 0;
  151. unsigned stateless_rpc = 0;
  152. int flags;
  153. unsigned int reject_reasons;
  154. int progress = -1;
  155. int from_stdin = 0;
  156. struct push_cas_option cas = {0};
  157. struct packet_reader reader;
  158. struct option options[] = {
  159. OPT__VERBOSITY(&verbose),
  160. OPT_STRING(0, "receive-pack", &receivepack, "receive-pack", N_("receive pack program")),
  161. OPT_STRING(0, "exec", &receivepack, "receive-pack", N_("receive pack program")),
  162. OPT_STRING(0, "remote", &remote_name, "remote", N_("remote name")),
  163. OPT_BOOL(0, "all", &send_all, N_("push all refs")),
  164. OPT_BOOL('n' , "dry-run", &dry_run, N_("dry run")),
  165. OPT_BOOL(0, "mirror", &send_mirror, N_("mirror all refs")),
  166. OPT_BOOL('f', "force", &force_update, N_("force updates")),
  167. OPT_CALLBACK_F(0, "signed", &push_cert, "(yes|no|if-asked)", N_("GPG sign the push"),
  168. PARSE_OPT_OPTARG, option_parse_push_signed),
  169. OPT_STRING_LIST(0, "push-option", &push_options,
  170. N_("server-specific"),
  171. N_("option to transmit")),
  172. OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
  173. OPT_BOOL(0, "thin", &use_thin_pack, N_("use thin pack")),
  174. OPT_BOOL(0, "atomic", &atomic, N_("request atomic transaction on remote side")),
  175. OPT_BOOL(0, "stateless-rpc", &stateless_rpc, N_("use stateless RPC protocol")),
  176. OPT_BOOL(0, "stdin", &from_stdin, N_("read refs from stdin")),
  177. OPT_BOOL(0, "helper-status", &helper_status, N_("print status from remote helper")),
  178. OPT_CALLBACK_F(0, CAS_OPT_NAME, &cas, N_("<refname>:<expect>"),
  179. N_("require old value of ref to be at this value"),
  180. PARSE_OPT_OPTARG, parseopt_push_cas_option),
  181. OPT_END()
  182. };
  183. git_config(send_pack_config, NULL);
  184. argc = parse_options(argc, argv, prefix, options, send_pack_usage, 0);
  185. if (argc > 0) {
  186. dest = argv[0];
  187. refspec_appendn(&rs, argv + 1, argc - 1);
  188. }
  189. if (!dest)
  190. usage_with_options(send_pack_usage, options);
  191. args.verbose = verbose;
  192. args.dry_run = dry_run;
  193. args.send_mirror = send_mirror;
  194. args.force_update = force_update;
  195. args.quiet = quiet;
  196. args.push_cert = push_cert;
  197. args.progress = progress;
  198. args.use_thin_pack = use_thin_pack;
  199. args.atomic = atomic;
  200. args.stateless_rpc = stateless_rpc;
  201. args.push_options = push_options.nr ? &push_options : NULL;
  202. if (from_stdin) {
  203. if (args.stateless_rpc) {
  204. const char *buf;
  205. while ((buf = packet_read_line(0, NULL)))
  206. refspec_append(&rs, buf);
  207. } else {
  208. struct strbuf line = STRBUF_INIT;
  209. while (strbuf_getline(&line, stdin) != EOF)
  210. refspec_append(&rs, line.buf);
  211. strbuf_release(&line);
  212. }
  213. }
  214. /*
  215. * --all and --mirror are incompatible; neither makes sense
  216. * with any refspecs.
  217. */
  218. if ((rs.nr > 0 && (send_all || args.send_mirror)) ||
  219. (send_all && args.send_mirror))
  220. usage_with_options(send_pack_usage, options);
  221. if (remote_name) {
  222. remote = remote_get(remote_name);
  223. if (!remote_has_url(remote, dest)) {
  224. die("Destination %s is not a uri for %s",
  225. dest, remote_name);
  226. }
  227. }
  228. if (progress == -1)
  229. progress = !args.quiet && isatty(2);
  230. args.progress = progress;
  231. if (args.stateless_rpc) {
  232. conn = NULL;
  233. fd[0] = 0;
  234. fd[1] = 1;
  235. } else {
  236. conn = git_connect(fd, dest, receivepack,
  237. args.verbose ? CONNECT_VERBOSE : 0);
  238. }
  239. packet_reader_init(&reader, fd[0], NULL, 0,
  240. PACKET_READ_CHOMP_NEWLINE |
  241. PACKET_READ_GENTLE_ON_EOF |
  242. PACKET_READ_DIE_ON_ERR_PACKET);
  243. switch (discover_version(&reader)) {
  244. case protocol_v2:
  245. die("support for protocol v2 not implemented yet");
  246. break;
  247. case protocol_v1:
  248. case protocol_v0:
  249. get_remote_heads(&reader, &remote_refs, REF_NORMAL,
  250. &extra_have, &shallow);
  251. break;
  252. case protocol_unknown_version:
  253. BUG("unknown protocol version");
  254. }
  255. local_refs = get_local_heads();
  256. flags = MATCH_REFS_NONE;
  257. if (send_all)
  258. flags |= MATCH_REFS_ALL;
  259. if (args.send_mirror)
  260. flags |= MATCH_REFS_MIRROR;
  261. /* match them up */
  262. if (match_push_refs(local_refs, &remote_refs, &rs, flags))
  263. return -1;
  264. if (!is_empty_cas(&cas))
  265. apply_push_cas(&cas, remote, remote_refs);
  266. set_ref_status_for_push(remote_refs, args.send_mirror,
  267. args.force_update);
  268. ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
  269. if (helper_status)
  270. print_helper_status(remote_refs);
  271. close(fd[1]);
  272. close(fd[0]);
  273. ret |= finish_connect(conn);
  274. if (!helper_status)
  275. transport_print_push_status(dest, remote_refs, args.verbose, 0, &reject_reasons);
  276. if (!args.dry_run && remote) {
  277. struct ref *ref;
  278. for (ref = remote_refs; ref; ref = ref->next)
  279. transport_update_tracking_ref(remote, ref, args.verbose);
  280. }
  281. if (!ret && !transport_refs_pushed(remote_refs))
  282. fprintf(stderr, "Everything up-to-date\n");
  283. return ret;
  284. }