add: plug a leak on interactive_add
[git/gitster.git] / builtin / fetch-pack.c
blob44c05ee86cc7048e72e455f4c2a28a1d42a1f7fa
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);
34 } else {
35 /* <ref>, clear cruft from get_oid_hex */
36 oidclr(&oid);
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 *ref = 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, &ref, 0, NULL, NULL,
232 args.stateless_rpc);
233 break;
234 case protocol_v1:
235 case protocol_v0:
236 get_remote_heads(&reader, &ref, 0, NULL, &shallow);
237 break;
238 case protocol_unknown_version:
239 BUG("unknown protocol version");
242 ref = fetch_pack(&args, fd, ref, sought, nr_sought,
243 &shallow, pack_lockfiles_ptr, version);
244 if (pack_lockfiles.nr) {
245 int i;
247 printf("lock %s\n", pack_lockfiles.items[0].string);
248 fflush(stdout);
249 for (i = 1; i < pack_lockfiles.nr; i++)
250 warning(_("Lockfile created but not reported: %s"),
251 pack_lockfiles.items[i].string);
253 if (args.check_self_contained_and_connected &&
254 args.self_contained_and_connected) {
255 printf("connectivity-ok\n");
256 fflush(stdout);
258 close(fd[0]);
259 close(fd[1]);
260 if (finish_connect(conn))
261 return 1;
263 ret = !ref;
266 * If the heads to pull were given, we should have consumed
267 * all of them by matching the remote. Otherwise, 'git fetch
268 * remote no-such-ref' would silently succeed without issuing
269 * an error.
271 ret |= report_unmatched_refs(sought, nr_sought);
273 while (ref) {
274 printf("%s %s\n",
275 oid_to_hex(&ref->old_oid), ref->name);
276 ref = ref->next;
279 return ret;