winansi: use FLEX_ARRAY to avoid compiler warning
[git] / builtin / fetch-pack.c
1 #include "builtin.h"
2 #include "pkt-line.h"
3 #include "fetch-pack.h"
4 #include "remote.h"
5 #include "connect.h"
6 #include "sha1-array.h"
7 #include "protocol.h"
8
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>...]";
13
14 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
15                              const char *name)
16 {
17         struct ref *ref;
18         struct object_id oid;
19         const char *p;
20
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);
30                 }
31         } else {
32                 /* <ref>, clear cruft from get_oid_hex */
33                 oidclr(&oid);
34         }
35
36         ref = alloc_ref(name);
37         oidcpy(&ref->old_oid, &oid);
38         (*nr)++;
39         ALLOC_GROW(*sought, *nr, *alloc);
40         (*sought)[*nr - 1] = ref;
41 }
42
43 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
44 {
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         char *pack_lockfile = NULL;
52         char **pack_lockfile_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;
59
60         fetch_if_missing = 0;
61
62         packet_trace_identity("fetch-pack");
63
64         memset(&args, 0, sizeof(args));
65         args.uploadpack = "git-upload-pack";
66
67         for (i = 1; i < argc && *argv[i] == '-'; i++) {
68                 const char *arg = argv[i];
69
70                 if (skip_prefix(arg, "--upload-pack=", &arg)) {
71                         args.uploadpack = arg;
72                         continue;
73                 }
74                 if (skip_prefix(arg, "--exec=", &arg)) {
75                         args.uploadpack = arg;
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                 }
103                 if (!strcmp("--diag-url", arg)) {
104                         args.diag_url = 1;
105                         continue;
106                 }
107                 if (!strcmp("-v", arg)) {
108                         args.verbose = 1;
109                         continue;
110                 }
111                 if (skip_prefix(arg, "--depth=", &arg)) {
112                         args.depth = strtol(arg, NULL, 0);
113                         continue;
114                 }
115                 if (skip_prefix(arg, "--shallow-since=", &arg)) {
116                         args.deepen_since = xstrdup(arg);
117                         continue;
118                 }
119                 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
120                         string_list_append(&deepen_not, arg);
121                         continue;
122                 }
123                 if (!strcmp(arg, "--deepen-relative")) {
124                         args.deepen_relative = 1;
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                 }
140                 if (!strcmp("--check-self-contained-and-connected", arg)) {
141                         args.check_self_contained_and_connected = 1;
142                         continue;
143                 }
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                 }
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                 }
160                 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
161                         parse_list_objects_filter(&args.filter_options, arg);
162                         continue;
163                 }
164                 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
165                         list_objects_filter_set_no_filter(&args.filter_options);
166                         continue;
167                 }
168                 usage(fetch_pack_usage);
169         }
170         if (deepen_not.nr)
171                 args.deepen_not = &deepen_not;
172
173         if (i < argc)
174                 dest = argv[i++];
175         else
176                 usage(fetch_pack_usage);
177
178         /*
179          * Copy refs from cmdline to growable list, then append any
180          * refs from the standard input:
181          */
182         for (; i < argc; i++)
183                 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
184         if (args.stdin_refs) {
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                          */
189                         for (;;) {
190                                 char *line = packet_read_line(0, NULL);
191                                 if (!line)
192                                         break;
193                                 add_sought_entry(&sought, &nr_sought,  &alloc_sought, line);
194                         }
195                 }
196                 else {
197                         /* read from stdin one ref per line, until EOF */
198                         struct strbuf line = STRBUF_INIT;
199                         while (strbuf_getline_lf(&line, stdin) != EOF)
200                                 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
201                         strbuf_release(&line);
202                 }
203         }
204
205         if (args.stateless_rpc) {
206                 conn = NULL;
207                 fd[0] = 0;
208                 fd[1] = 1;
209         } else {
210                 int flags = args.verbose ? CONNECT_VERBOSE : 0;
211                 if (args.diag_url)
212                         flags |= CONNECT_DIAG_URL;
213                 conn = git_connect(fd, dest, args.uploadpack,
214                                    flags);
215                 if (!conn)
216                         return args.diag_url ? 0 : 1;
217         }
218
219         packet_reader_init(&reader, fd[0], NULL, 0,
220                            PACKET_READ_CHOMP_NEWLINE |
221                            PACKET_READ_GENTLE_ON_EOF |
222                            PACKET_READ_DIE_ON_ERR_PACKET);
223
224         version = discover_version(&reader);
225         switch (version) {
226         case protocol_v2:
227                 get_remote_refs(fd[1], &reader, &ref, 0, NULL, NULL);
228                 break;
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         }
236
237         ref = fetch_pack(&args, fd, ref, sought, nr_sought,
238                          &shallow, pack_lockfile_ptr, version);
239         if (pack_lockfile) {
240                 printf("lock %s\n", pack_lockfile);
241                 fflush(stdout);
242         }
243         if (args.check_self_contained_and_connected &&
244             args.self_contained_and_connected) {
245                 printf("connectivity-ok\n");
246                 fflush(stdout);
247         }
248         close(fd[0]);
249         close(fd[1]);
250         if (finish_connect(conn))
251                 return 1;
252
253         ret = !ref;
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          */
261         ret |= report_unmatched_refs(sought, nr_sought);
262
263         while (ref) {
264                 printf("%s %s\n",
265                        oid_to_hex(&ref->old_oid), ref->name);
266                 ref = ref->next;
267         }
268
269         return ret;
270 }