cocci: avoid "should ... be a metavariable" warnings
[git/debian.git] / builtin / fetch-pack.c
blobafe679368deec2a0288de1606259b73847ce434b
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"
9 static const char fetch_pack_usage[] =
10 "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
11 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
12 "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
14 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
15 const char *name)
17 struct ref *ref;
18 struct object_id oid;
19 const char *p;
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 */
27 } else {
28 /* <ref>, clear cruft from oid */
29 oidclr(&oid);
31 } else {
32 /* <ref>, clear cruft from get_oid_hex */
33 oidclr(&oid);
36 ref = alloc_ref(name);
37 oidcpy(&ref->old_oid, &oid);
38 (*nr)++;
39 ALLOC_GROW(*sought, *nr, *alloc);
40 (*sought)[*nr - 1] = ref;
43 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
45 int i, ret;
46 struct ref *ref = NULL;
47 const char *dest = NULL;
48 struct ref **sought = NULL;
49 int nr_sought = 0, alloc_sought = 0;
50 int fd[2];
51 struct string_list pack_lockfiles = STRING_LIST_INIT_DUP;
52 struct string_list *pack_lockfiles_ptr = NULL;
53 struct child_process *conn;
54 struct fetch_pack_args args;
55 struct oid_array shallow = OID_ARRAY_INIT;
56 struct string_list deepen_not = STRING_LIST_INIT_DUP;
57 struct packet_reader reader;
58 enum protocol_version version;
60 fetch_if_missing = 0;
62 packet_trace_identity("fetch-pack");
64 memset(&args, 0, sizeof(args));
65 list_objects_filter_init(&args.filter_options);
66 args.uploadpack = "git-upload-pack";
68 for (i = 1; i < argc && *argv[i] == '-'; i++) {
69 const char *arg = argv[i];
71 if (skip_prefix(arg, "--upload-pack=", &arg)) {
72 args.uploadpack = arg;
73 continue;
75 if (skip_prefix(arg, "--exec=", &arg)) {
76 args.uploadpack = arg;
77 continue;
79 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
80 args.quiet = 1;
81 continue;
83 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
84 args.lock_pack = args.keep_pack;
85 args.keep_pack = 1;
86 continue;
88 if (!strcmp("--thin", arg)) {
89 args.use_thin_pack = 1;
90 continue;
92 if (!strcmp("--include-tag", arg)) {
93 args.include_tag = 1;
94 continue;
96 if (!strcmp("--all", arg)) {
97 args.fetch_all = 1;
98 continue;
100 if (!strcmp("--stdin", arg)) {
101 args.stdin_refs = 1;
102 continue;
104 if (!strcmp("--diag-url", arg)) {
105 args.diag_url = 1;
106 continue;
108 if (!strcmp("-v", arg)) {
109 args.verbose = 1;
110 continue;
112 if (skip_prefix(arg, "--depth=", &arg)) {
113 args.depth = strtol(arg, NULL, 0);
114 continue;
116 if (skip_prefix(arg, "--shallow-since=", &arg)) {
117 args.deepen_since = xstrdup(arg);
118 continue;
120 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
121 string_list_append(&deepen_not, arg);
122 continue;
124 if (!strcmp(arg, "--deepen-relative")) {
125 args.deepen_relative = 1;
126 continue;
128 if (!strcmp("--no-progress", arg)) {
129 args.no_progress = 1;
130 continue;
132 if (!strcmp("--stateless-rpc", arg)) {
133 args.stateless_rpc = 1;
134 continue;
136 if (!strcmp("--lock-pack", arg)) {
137 args.lock_pack = 1;
138 pack_lockfiles_ptr = &pack_lockfiles;
139 continue;
141 if (!strcmp("--check-self-contained-and-connected", arg)) {
142 args.check_self_contained_and_connected = 1;
143 continue;
145 if (!strcmp("--cloning", arg)) {
146 args.cloning = 1;
147 continue;
149 if (!strcmp("--update-shallow", arg)) {
150 args.update_shallow = 1;
151 continue;
153 if (!strcmp("--from-promisor", arg)) {
154 args.from_promisor = 1;
155 continue;
157 if (!strcmp("--refetch", arg)) {
158 args.refetch = 1;
159 continue;
161 if (skip_prefix(arg, ("--filter="), &arg)) {
162 parse_list_objects_filter(&args.filter_options, arg);
163 continue;
165 if (!strcmp(arg, ("--no-filter"))) {
166 list_objects_filter_set_no_filter(&args.filter_options);
167 continue;
169 usage(fetch_pack_usage);
171 if (deepen_not.nr)
172 args.deepen_not = &deepen_not;
174 if (i < argc)
175 dest = argv[i++];
176 else
177 usage(fetch_pack_usage);
180 * Copy refs from cmdline to growable list, then append any
181 * refs from the standard input:
183 for (; i < argc; i++)
184 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
185 if (args.stdin_refs) {
186 if (args.stateless_rpc) {
187 /* in stateless RPC mode we use pkt-line to read
188 * from stdin, until we get a flush packet
190 for (;;) {
191 char *line = packet_read_line(0, NULL);
192 if (!line)
193 break;
194 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
197 else {
198 /* read from stdin one ref per line, until EOF */
199 struct strbuf line = STRBUF_INIT;
200 while (strbuf_getline_lf(&line, stdin) != EOF)
201 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
202 strbuf_release(&line);
206 if (args.stateless_rpc) {
207 conn = NULL;
208 fd[0] = 0;
209 fd[1] = 1;
210 } else {
211 int flags = args.verbose ? CONNECT_VERBOSE : 0;
212 if (args.diag_url)
213 flags |= CONNECT_DIAG_URL;
214 conn = git_connect(fd, dest, args.uploadpack,
215 flags);
216 if (!conn)
217 return args.diag_url ? 0 : 1;
220 packet_reader_init(&reader, fd[0], NULL, 0,
221 PACKET_READ_CHOMP_NEWLINE |
222 PACKET_READ_GENTLE_ON_EOF |
223 PACKET_READ_DIE_ON_ERR_PACKET);
225 version = discover_version(&reader);
226 switch (version) {
227 case protocol_v2:
228 get_remote_refs(fd[1], &reader, &ref, 0, NULL, NULL,
229 args.stateless_rpc);
230 break;
231 case protocol_v1:
232 case protocol_v0:
233 get_remote_heads(&reader, &ref, 0, NULL, &shallow);
234 break;
235 case protocol_unknown_version:
236 BUG("unknown protocol version");
239 ref = fetch_pack(&args, fd, ref, sought, nr_sought,
240 &shallow, pack_lockfiles_ptr, version);
241 if (pack_lockfiles.nr) {
242 int i;
244 printf("lock %s\n", pack_lockfiles.items[0].string);
245 fflush(stdout);
246 for (i = 1; i < pack_lockfiles.nr; i++)
247 warning(_("Lockfile created but not reported: %s"),
248 pack_lockfiles.items[i].string);
250 if (args.check_self_contained_and_connected &&
251 args.self_contained_and_connected) {
252 printf("connectivity-ok\n");
253 fflush(stdout);
255 close(fd[0]);
256 close(fd[1]);
257 if (finish_connect(conn))
258 return 1;
260 ret = !ref;
263 * If the heads to pull were given, we should have consumed
264 * all of them by matching the remote. Otherwise, 'git fetch
265 * remote no-such-ref' would silently succeed without issuing
266 * an error.
268 ret |= report_unmatched_refs(sought, nr_sought);
270 while (ref) {
271 printf("%s %s\n",
272 oid_to_hex(&ref->old_oid), ref->name);
273 ref = ref->next;
276 return ret;