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