fetch-pack.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "builtin.h"
  2. #include "pkt-line.h"
  3. #include "fetch-pack.h"
  4. #include "remote.h"
  5. #include "connect.h"
  6. #include "oid-array.h"
  7. #include "protocol.h"
  8. static const char fetch_pack_usage[] =
  9. "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
  10. "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
  11. "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
  12. static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
  13. const char *name)
  14. {
  15. struct ref *ref;
  16. struct object_id oid;
  17. const char *p;
  18. if (!parse_oid_hex(name, &oid, &p)) {
  19. if (*p == ' ') {
  20. /* <oid> <ref>, find refname */
  21. name = p + 1;
  22. } else if (*p == '\0') {
  23. ; /* <oid>, leave oid as name */
  24. } else {
  25. /* <ref>, clear cruft from oid */
  26. oidclr(&oid);
  27. }
  28. } else {
  29. /* <ref>, clear cruft from get_oid_hex */
  30. oidclr(&oid);
  31. }
  32. ref = alloc_ref(name);
  33. oidcpy(&ref->old_oid, &oid);
  34. (*nr)++;
  35. ALLOC_GROW(*sought, *nr, *alloc);
  36. (*sought)[*nr - 1] = ref;
  37. }
  38. int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
  39. {
  40. int i, ret;
  41. struct ref *ref = NULL;
  42. const char *dest = NULL;
  43. struct ref **sought = NULL;
  44. int nr_sought = 0, alloc_sought = 0;
  45. int fd[2];
  46. struct string_list pack_lockfiles = STRING_LIST_INIT_DUP;
  47. struct string_list *pack_lockfiles_ptr = NULL;
  48. struct child_process *conn;
  49. struct fetch_pack_args args;
  50. struct oid_array shallow = OID_ARRAY_INIT;
  51. struct string_list deepen_not = STRING_LIST_INIT_DUP;
  52. struct packet_reader reader;
  53. enum protocol_version version;
  54. fetch_if_missing = 0;
  55. packet_trace_identity("fetch-pack");
  56. memset(&args, 0, sizeof(args));
  57. args.uploadpack = "git-upload-pack";
  58. for (i = 1; i < argc && *argv[i] == '-'; i++) {
  59. const char *arg = argv[i];
  60. if (skip_prefix(arg, "--upload-pack=", &arg)) {
  61. args.uploadpack = arg;
  62. continue;
  63. }
  64. if (skip_prefix(arg, "--exec=", &arg)) {
  65. args.uploadpack = arg;
  66. continue;
  67. }
  68. if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
  69. args.quiet = 1;
  70. continue;
  71. }
  72. if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
  73. args.lock_pack = args.keep_pack;
  74. args.keep_pack = 1;
  75. continue;
  76. }
  77. if (!strcmp("--thin", arg)) {
  78. args.use_thin_pack = 1;
  79. continue;
  80. }
  81. if (!strcmp("--include-tag", arg)) {
  82. args.include_tag = 1;
  83. continue;
  84. }
  85. if (!strcmp("--all", arg)) {
  86. args.fetch_all = 1;
  87. continue;
  88. }
  89. if (!strcmp("--stdin", arg)) {
  90. args.stdin_refs = 1;
  91. continue;
  92. }
  93. if (!strcmp("--diag-url", arg)) {
  94. args.diag_url = 1;
  95. continue;
  96. }
  97. if (!strcmp("-v", arg)) {
  98. args.verbose = 1;
  99. continue;
  100. }
  101. if (skip_prefix(arg, "--depth=", &arg)) {
  102. args.depth = strtol(arg, NULL, 0);
  103. continue;
  104. }
  105. if (skip_prefix(arg, "--shallow-since=", &arg)) {
  106. args.deepen_since = xstrdup(arg);
  107. continue;
  108. }
  109. if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
  110. string_list_append(&deepen_not, arg);
  111. continue;
  112. }
  113. if (!strcmp(arg, "--deepen-relative")) {
  114. args.deepen_relative = 1;
  115. continue;
  116. }
  117. if (!strcmp("--no-progress", arg)) {
  118. args.no_progress = 1;
  119. continue;
  120. }
  121. if (!strcmp("--stateless-rpc", arg)) {
  122. args.stateless_rpc = 1;
  123. continue;
  124. }
  125. if (!strcmp("--lock-pack", arg)) {
  126. args.lock_pack = 1;
  127. pack_lockfiles_ptr = &pack_lockfiles;
  128. continue;
  129. }
  130. if (!strcmp("--check-self-contained-and-connected", arg)) {
  131. args.check_self_contained_and_connected = 1;
  132. continue;
  133. }
  134. if (!strcmp("--cloning", arg)) {
  135. args.cloning = 1;
  136. continue;
  137. }
  138. if (!strcmp("--update-shallow", arg)) {
  139. args.update_shallow = 1;
  140. continue;
  141. }
  142. if (!strcmp("--from-promisor", arg)) {
  143. args.from_promisor = 1;
  144. continue;
  145. }
  146. if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
  147. parse_list_objects_filter(&args.filter_options, arg);
  148. continue;
  149. }
  150. if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
  151. list_objects_filter_set_no_filter(&args.filter_options);
  152. continue;
  153. }
  154. usage(fetch_pack_usage);
  155. }
  156. if (deepen_not.nr)
  157. args.deepen_not = &deepen_not;
  158. if (i < argc)
  159. dest = argv[i++];
  160. else
  161. usage(fetch_pack_usage);
  162. /*
  163. * Copy refs from cmdline to growable list, then append any
  164. * refs from the standard input:
  165. */
  166. for (; i < argc; i++)
  167. add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
  168. if (args.stdin_refs) {
  169. if (args.stateless_rpc) {
  170. /* in stateless RPC mode we use pkt-line to read
  171. * from stdin, until we get a flush packet
  172. */
  173. for (;;) {
  174. char *line = packet_read_line(0, NULL);
  175. if (!line)
  176. break;
  177. add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
  178. }
  179. }
  180. else {
  181. /* read from stdin one ref per line, until EOF */
  182. struct strbuf line = STRBUF_INIT;
  183. while (strbuf_getline_lf(&line, stdin) != EOF)
  184. add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
  185. strbuf_release(&line);
  186. }
  187. }
  188. if (args.stateless_rpc) {
  189. conn = NULL;
  190. fd[0] = 0;
  191. fd[1] = 1;
  192. } else {
  193. int flags = args.verbose ? CONNECT_VERBOSE : 0;
  194. if (args.diag_url)
  195. flags |= CONNECT_DIAG_URL;
  196. conn = git_connect(fd, dest, args.uploadpack,
  197. flags);
  198. if (!conn)
  199. return args.diag_url ? 0 : 1;
  200. }
  201. packet_reader_init(&reader, fd[0], NULL, 0,
  202. PACKET_READ_CHOMP_NEWLINE |
  203. PACKET_READ_GENTLE_ON_EOF |
  204. PACKET_READ_DIE_ON_ERR_PACKET);
  205. version = discover_version(&reader);
  206. switch (version) {
  207. case protocol_v2:
  208. get_remote_refs(fd[1], &reader, &ref, 0, NULL, NULL, args.stateless_rpc);
  209. break;
  210. case protocol_v1:
  211. case protocol_v0:
  212. get_remote_heads(&reader, &ref, 0, NULL, &shallow);
  213. break;
  214. case protocol_unknown_version:
  215. BUG("unknown protocol version");
  216. }
  217. ref = fetch_pack(&args, fd, ref, sought, nr_sought,
  218. &shallow, pack_lockfiles_ptr, version);
  219. if (pack_lockfiles.nr) {
  220. int i;
  221. printf("lock %s\n", pack_lockfiles.items[0].string);
  222. fflush(stdout);
  223. for (i = 1; i < pack_lockfiles.nr; i++)
  224. warning(_("Lockfile created but not reported: %s"),
  225. pack_lockfiles.items[i].string);
  226. }
  227. if (args.check_self_contained_and_connected &&
  228. args.self_contained_and_connected) {
  229. printf("connectivity-ok\n");
  230. fflush(stdout);
  231. }
  232. close(fd[0]);
  233. close(fd[1]);
  234. if (finish_connect(conn))
  235. return 1;
  236. ret = !ref;
  237. /*
  238. * If the heads to pull were given, we should have consumed
  239. * all of them by matching the remote. Otherwise, 'git fetch
  240. * remote no-such-ref' would silently succeed without issuing
  241. * an error.
  242. */
  243. ret |= report_unmatched_refs(sought, nr_sought);
  244. while (ref) {
  245. printf("%s %s\n",
  246. oid_to_hex(&ref->old_oid), ref->name);
  247. ref = ref->next;
  248. }
  249. return ret;
  250. }