Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1 | #include "builtin.h" |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 2 | #include "pkt-line.h" |
Daniel Barkalow | 2d4177c | 2007-09-10 23:03:00 -0400 | [diff] [blame] | 3 | #include "fetch-pack.h" |
Junio C Hamano | 47a5918 | 2013-07-08 13:56:53 -0700 | [diff] [blame] | 4 | #include "remote.h" |
| 5 | #include "connect.h" |
Jeff King | fe299ec | 2020-03-30 10:03:46 -0400 | [diff] [blame] | 6 | #include "oid-array.h" |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 7 | #include "protocol.h" |
Shawn O. Pearce | fa74052 | 2007-09-19 00:49:35 -0400 | [diff] [blame] | 8 | |
Junio C Hamano | 33b8303 | 2005-08-12 02:08:29 -0700 | [diff] [blame] | 9 | static const char fetch_pack_usage[] = |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 10 | "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] " |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 11 | "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] " |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 12 | "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]"; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 13 | |
Jeff King | 5545f05 | 2016-02-22 17:44:50 -0500 | [diff] [blame] | 14 | static void add_sought_entry(struct ref ***sought, int *nr, int *alloc, |
| 15 | const char *name) |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 16 | { |
Jeff King | 5545f05 | 2016-02-22 17:44:50 -0500 | [diff] [blame] | 17 | struct ref *ref; |
brian m. carlson | 854ecb9 | 2015-11-10 02:22:21 +0000 | [diff] [blame] | 18 | struct object_id oid; |
brian m. carlson | 7b5e614 | 2018-10-15 00:01:52 +0000 | [diff] [blame] | 19 | const char *p; |
Nguyễn Thái Ngọc Duy | 58f2ed0 | 2013-12-05 20:02:49 +0700 | [diff] [blame] | 20 | |
brian m. carlson | 7b5e614 | 2018-10-15 00:01:52 +0000 | [diff] [blame] | 21 | if (!parse_oid_hex(name, &oid, &p)) { |
| 22 | if (*p == ' ') { |
| 23 | /* <oid> <ref>, find refname */ |
| 24 | name = p + 1; |
| 25 | } else if (*p == '\0') { |
| 26 | ; /* <oid>, leave oid as name */ |
Gabriel Souza Franco | 4a8d202 | 2016-02-29 23:12:56 -0300 | [diff] [blame] | 27 | } else { |
| 28 | /* <ref>, clear cruft from oid */ |
| 29 | oidclr(&oid); |
| 30 | } |
| 31 | } else { |
| 32 | /* <ref>, clear cruft from get_oid_hex */ |
Jeff King | 5545f05 | 2016-02-22 17:44:50 -0500 | [diff] [blame] | 33 | oidclr(&oid); |
Gabriel Souza Franco | 4a8d202 | 2016-02-29 23:12:56 -0300 | [diff] [blame] | 34 | } |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 35 | |
Jeff King | 5545f05 | 2016-02-22 17:44:50 -0500 | [diff] [blame] | 36 | ref = alloc_ref(name); |
| 37 | oidcpy(&ref->old_oid, &oid); |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 38 | (*nr)++; |
| 39 | ALLOC_GROW(*sought, *nr, *alloc); |
| 40 | (*sought)[*nr - 1] = ref; |
| 41 | } |
| 42 | |
Daniel Barkalow | 2d4177c | 2007-09-10 23:03:00 -0400 | [diff] [blame] | 43 | int cmd_fetch_pack(int argc, const char **argv, const char *prefix) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 44 | { |
Michael Haggerty | 57e6fc69 | 2012-05-21 09:59:59 +0200 | [diff] [blame] | 45 | int i, ret; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 46 | struct ref *ref = NULL; |
Michael Haggerty | 9d19c6e | 2012-05-21 09:59:56 +0200 | [diff] [blame] | 47 | const char *dest = NULL; |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 48 | struct ref **sought = NULL; |
| 49 | int nr_sought = 0, alloc_sought = 0; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 50 | int fd[2]; |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 51 | char *pack_lockfile = NULL; |
| 52 | char **pack_lockfile_ptr = NULL; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 53 | struct child_process *conn; |
Nguyễn Thái Ngọc Duy | f8eb303 | 2012-10-26 22:53:54 +0700 | [diff] [blame] | 54 | struct fetch_pack_args args; |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 55 | struct oid_array shallow = OID_ARRAY_INIT; |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 56 | struct string_list deepen_not = STRING_LIST_INIT_DUP; |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 57 | struct packet_reader reader; |
Jonathan Tan | 4316ff3 | 2018-12-17 23:40:54 +0100 | [diff] [blame] | 58 | enum protocol_version version; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 59 | |
Jonathan Tan | 8b4c010 | 2017-12-08 15:27:14 +0000 | [diff] [blame] | 60 | fetch_if_missing = 0; |
| 61 | |
Jeff King | bbc30f9 | 2011-02-24 09:30:19 -0500 | [diff] [blame] | 62 | packet_trace_identity("fetch-pack"); |
| 63 | |
Nguyễn Thái Ngọc Duy | f8eb303 | 2012-10-26 22:53:54 +0700 | [diff] [blame] | 64 | memset(&args, 0, sizeof(args)); |
| 65 | args.uploadpack = "git-upload-pack"; |
| 66 | |
Michael Haggerty | ff22ff9 | 2012-05-21 09:59:58 +0200 | [diff] [blame] | 67 | for (i = 1; i < argc && *argv[i] == '-'; i++) { |
Daniel Barkalow | 2d4177c | 2007-09-10 23:03:00 -0400 | [diff] [blame] | 68 | const char *arg = argv[i]; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 69 | |
Nguyễn Thái Ngọc Duy | 45a3e52 | 2016-06-12 17:53:53 +0700 | [diff] [blame] | 70 | if (skip_prefix(arg, "--upload-pack=", &arg)) { |
| 71 | args.uploadpack = arg; |
Michael Haggerty | ff22ff9 | 2012-05-21 09:59:58 +0200 | [diff] [blame] | 72 | continue; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 73 | } |
Nguyễn Thái Ngọc Duy | 45a3e52 | 2016-06-12 17:53:53 +0700 | [diff] [blame] | 74 | if (skip_prefix(arg, "--exec=", &arg)) { |
| 75 | args.uploadpack = arg; |
Michael Haggerty | ff22ff9 | 2012-05-21 09:59:58 +0200 | [diff] [blame] | 76 | continue; |
| 77 | } |
| 78 | if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) { |
| 79 | args.quiet = 1; |
| 80 | continue; |
| 81 | } |
| 82 | if (!strcmp("--keep", arg) || !strcmp("-k", arg)) { |
| 83 | args.lock_pack = args.keep_pack; |
| 84 | args.keep_pack = 1; |
| 85 | continue; |
| 86 | } |
| 87 | if (!strcmp("--thin", arg)) { |
| 88 | args.use_thin_pack = 1; |
| 89 | continue; |
| 90 | } |
| 91 | if (!strcmp("--include-tag", arg)) { |
| 92 | args.include_tag = 1; |
| 93 | continue; |
| 94 | } |
| 95 | if (!strcmp("--all", arg)) { |
| 96 | args.fetch_all = 1; |
| 97 | continue; |
| 98 | } |
| 99 | if (!strcmp("--stdin", arg)) { |
| 100 | args.stdin_refs = 1; |
| 101 | continue; |
| 102 | } |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 103 | if (!strcmp("--diag-url", arg)) { |
| 104 | args.diag_url = 1; |
| 105 | continue; |
| 106 | } |
Michael Haggerty | ff22ff9 | 2012-05-21 09:59:58 +0200 | [diff] [blame] | 107 | if (!strcmp("-v", arg)) { |
| 108 | args.verbose = 1; |
| 109 | continue; |
| 110 | } |
Nguyễn Thái Ngọc Duy | 45a3e52 | 2016-06-12 17:53:53 +0700 | [diff] [blame] | 111 | if (skip_prefix(arg, "--depth=", &arg)) { |
| 112 | args.depth = strtol(arg, NULL, 0); |
Michael Haggerty | ff22ff9 | 2012-05-21 09:59:58 +0200 | [diff] [blame] | 113 | continue; |
| 114 | } |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 115 | if (skip_prefix(arg, "--shallow-since=", &arg)) { |
| 116 | args.deepen_since = xstrdup(arg); |
| 117 | continue; |
| 118 | } |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 119 | if (skip_prefix(arg, "--shallow-exclude=", &arg)) { |
| 120 | string_list_append(&deepen_not, arg); |
| 121 | continue; |
| 122 | } |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 123 | if (!strcmp(arg, "--deepen-relative")) { |
| 124 | args.deepen_relative = 1; |
Michael Haggerty | ff22ff9 | 2012-05-21 09:59:58 +0200 | [diff] [blame] | 125 | continue; |
| 126 | } |
| 127 | if (!strcmp("--no-progress", arg)) { |
| 128 | args.no_progress = 1; |
| 129 | continue; |
| 130 | } |
| 131 | if (!strcmp("--stateless-rpc", arg)) { |
| 132 | args.stateless_rpc = 1; |
| 133 | continue; |
| 134 | } |
| 135 | if (!strcmp("--lock-pack", arg)) { |
| 136 | args.lock_pack = 1; |
| 137 | pack_lockfile_ptr = &pack_lockfile; |
| 138 | continue; |
| 139 | } |
Nguyễn Thái Ngọc Duy | 9ba3804 | 2013-07-21 15:18:05 +0700 | [diff] [blame] | 140 | if (!strcmp("--check-self-contained-and-connected", arg)) { |
| 141 | args.check_self_contained_and_connected = 1; |
| 142 | continue; |
| 143 | } |
Nguyễn Thái Ngọc Duy | 1609488 | 2013-12-05 20:02:50 +0700 | [diff] [blame] | 144 | if (!strcmp("--cloning", arg)) { |
| 145 | args.cloning = 1; |
| 146 | continue; |
| 147 | } |
| 148 | if (!strcmp("--update-shallow", arg)) { |
| 149 | args.update_shallow = 1; |
| 150 | continue; |
| 151 | } |
Jonathan Tan | 88e2f9e | 2017-12-05 16:58:49 +0000 | [diff] [blame] | 152 | if (!strcmp("--from-promisor", arg)) { |
| 153 | args.from_promisor = 1; |
| 154 | continue; |
| 155 | } |
| 156 | if (!strcmp("--no-dependents", arg)) { |
| 157 | args.no_dependents = 1; |
| 158 | continue; |
| 159 | } |
Jeff Hostetler | 640d8b7 | 2017-12-08 15:58:40 +0000 | [diff] [blame] | 160 | if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) { |
| 161 | parse_list_objects_filter(&args.filter_options, arg); |
| 162 | continue; |
| 163 | } |
Jeff Hostetler | bc2d0c3 | 2017-12-08 15:58:41 +0000 | [diff] [blame] | 164 | if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) { |
Jeff Hostetler | aa57b87 | 2017-12-08 15:58:50 +0000 | [diff] [blame] | 165 | list_objects_filter_set_no_filter(&args.filter_options); |
Jeff Hostetler | bc2d0c3 | 2017-12-08 15:58:41 +0000 | [diff] [blame] | 166 | continue; |
| 167 | } |
Michael Haggerty | ff22ff9 | 2012-05-21 09:59:58 +0200 | [diff] [blame] | 168 | usage(fetch_pack_usage); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 169 | } |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 170 | if (deepen_not.nr) |
| 171 | args.deepen_not = &deepen_not; |
Michael Haggerty | 4cc00fc | 2012-05-21 09:59:57 +0200 | [diff] [blame] | 172 | |
| 173 | if (i < argc) |
| 174 | dest = argv[i++]; |
| 175 | else |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 176 | usage(fetch_pack_usage); |
Daniel Barkalow | 2d4177c | 2007-09-10 23:03:00 -0400 | [diff] [blame] | 177 | |
Michael Haggerty | 57e6fc69 | 2012-05-21 09:59:59 +0200 | [diff] [blame] | 178 | /* |
| 179 | * Copy refs from cmdline to growable list, then append any |
| 180 | * refs from the standard input: |
| 181 | */ |
Michael Haggerty | 57e6fc69 | 2012-05-21 09:59:59 +0200 | [diff] [blame] | 182 | for (; i < argc; i++) |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 183 | add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]); |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 184 | if (args.stdin_refs) { |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 185 | if (args.stateless_rpc) { |
| 186 | /* in stateless RPC mode we use pkt-line to read |
| 187 | * from stdin, until we get a flush packet |
| 188 | */ |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 189 | for (;;) { |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 190 | char *line = packet_read_line(0, NULL); |
| 191 | if (!line) |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 192 | break; |
Junio C Hamano | e013bda | 2013-04-01 08:59:37 -0700 | [diff] [blame] | 193 | add_sought_entry(&sought, &nr_sought, &alloc_sought, line); |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | else { |
| 197 | /* read from stdin one ref per line, until EOF */ |
| 198 | struct strbuf line = STRBUF_INIT; |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 199 | while (strbuf_getline_lf(&line, stdin) != EOF) |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 200 | add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf); |
Ivan Todoroski | 078b895 | 2012-04-02 17:13:48 +0200 | [diff] [blame] | 201 | strbuf_release(&line); |
| 202 | } |
| 203 | } |
| 204 | |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 205 | if (args.stateless_rpc) { |
| 206 | conn = NULL; |
| 207 | fd[0] = 0; |
| 208 | fd[1] = 1; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 209 | } else { |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 210 | int flags = args.verbose ? CONNECT_VERBOSE : 0; |
| 211 | if (args.diag_url) |
| 212 | flags |= CONNECT_DIAG_URL; |
Michael Haggerty | 9d19c6e | 2012-05-21 09:59:56 +0200 | [diff] [blame] | 213 | conn = git_connect(fd, dest, args.uploadpack, |
Torsten Bögershausen | 5610b7c | 2013-11-28 20:49:17 +0100 | [diff] [blame] | 214 | flags); |
| 215 | if (!conn) |
| 216 | return args.diag_url ? 0 : 1; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 217 | } |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 218 | |
| 219 | packet_reader_init(&reader, fd[0], NULL, 0, |
| 220 | PACKET_READ_CHOMP_NEWLINE | |
Masaya Suzuki | 2d103c3 | 2018-12-29 13:19:15 -0800 | [diff] [blame] | 221 | PACKET_READ_GENTLE_ON_EOF | |
| 222 | PACKET_READ_DIE_ON_ERR_PACKET); |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 223 | |
Jonathan Tan | 4316ff3 | 2018-12-17 23:40:54 +0100 | [diff] [blame] | 224 | version = discover_version(&reader); |
| 225 | switch (version) { |
Brandon Williams | 8f6982b | 2018-03-14 11:31:47 -0700 | [diff] [blame] | 226 | case protocol_v2: |
Jonathan Tan | 4316ff3 | 2018-12-17 23:40:54 +0100 | [diff] [blame] | 227 | get_remote_refs(fd[1], &reader, &ref, 0, NULL, NULL); |
| 228 | break; |
Brandon Williams | ad6ac12 | 2018-03-14 11:31:45 -0700 | [diff] [blame] | 229 | case protocol_v1: |
| 230 | case protocol_v0: |
| 231 | get_remote_heads(&reader, &ref, 0, NULL, &shallow); |
| 232 | break; |
| 233 | case protocol_unknown_version: |
| 234 | BUG("unknown protocol version"); |
| 235 | } |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 236 | |
Jeff King | 0f804b0 | 2019-03-20 04:16:14 -0400 | [diff] [blame] | 237 | ref = fetch_pack(&args, fd, ref, sought, nr_sought, |
Jonathan Tan | 4316ff3 | 2018-12-17 23:40:54 +0100 | [diff] [blame] | 238 | &shallow, pack_lockfile_ptr, version); |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 239 | if (pack_lockfile) { |
| 240 | printf("lock %s\n", pack_lockfile); |
| 241 | fflush(stdout); |
| 242 | } |
Nguyễn Thái Ngọc Duy | 9ba3804 | 2013-07-21 15:18:05 +0700 | [diff] [blame] | 243 | if (args.check_self_contained_and_connected && |
| 244 | args.self_contained_and_connected) { |
| 245 | printf("connectivity-ok\n"); |
| 246 | fflush(stdout); |
| 247 | } |
Shawn O. Pearce | 249b200 | 2009-10-30 17:47:42 -0700 | [diff] [blame] | 248 | close(fd[0]); |
| 249 | close(fd[1]); |
| 250 | if (finish_connect(conn)) |
Michael Haggerty | 7418f1a | 2012-09-09 08:19:46 +0200 | [diff] [blame] | 251 | return 1; |
Junio C Hamano | 9e5d2b4 | 2005-11-06 00:09:59 -0800 | [diff] [blame] | 252 | |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 253 | ret = !ref; |
Michael Haggerty | b285668 | 2012-09-09 08:19:48 +0200 | [diff] [blame] | 254 | |
| 255 | /* |
| 256 | * If the heads to pull were given, we should have consumed |
| 257 | * all of them by matching the remote. Otherwise, 'git fetch |
| 258 | * remote no-such-ref' would silently succeed without issuing |
| 259 | * an error. |
| 260 | */ |
Matt McCutchen | e860d96 | 2017-02-22 11:01:22 -0500 | [diff] [blame] | 261 | ret |= report_unmatched_refs(sought, nr_sought); |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 262 | |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 263 | while (ref) { |
| 264 | printf("%s %s\n", |
brian m. carlson | f4e54d0 | 2015-11-10 02:22:20 +0000 | [diff] [blame] | 265 | oid_to_hex(&ref->old_oid), ref->name); |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 266 | ref = ref->next; |
| 267 | } |
Junio C Hamano | 9e5d2b4 | 2005-11-06 00:09:59 -0800 | [diff] [blame] | 268 | |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 269 | return ret; |
| 270 | } |