]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fetch-pack.c
strmap: enable allocations to come from a mem_pool
[thirdparty/git.git] / builtin / fetch-pack.c
CommitLineData
c2e86add 1#include "builtin.h"
def88e9a 2#include "pkt-line.h"
2d4177c0 3#include "fetch-pack.h"
47a59185
JH
4#include "remote.h"
5#include "connect.h"
fe299ec5 6#include "oid-array.h"
ad6ac124 7#include "protocol.h"
fa740529 8
33b83034 9static const char fetch_pack_usage[] =
9c9b4f2f 10"git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
078b895f 11"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
5610b7c0 12"[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
def88e9a 13
5545f057
JK
14static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
15 const char *name)
f2db854d 16{
5545f057 17 struct ref *ref;
854ecb9c 18 struct object_id oid;
7b5e614e 19 const char *p;
58f2ed05 20
7b5e614e 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 */
4a8d202c
GSF
27 } else {
28 /* <ref>, clear cruft from oid */
29 oidclr(&oid);
30 }
31 } else {
32 /* <ref>, clear cruft from get_oid_hex */
5545f057 33 oidclr(&oid);
4a8d202c 34 }
f2db854d 35
5545f057
JK
36 ref = alloc_ref(name);
37 oidcpy(&ref->old_oid, &oid);
f2db854d
JH
38 (*nr)++;
39 ALLOC_GROW(*sought, *nr, *alloc);
40 (*sought)[*nr - 1] = ref;
41}
42
50ab5fd3
SP
43int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
44{
57e6fc69 45 int i, ret;
ba227857 46 struct ref *ref = NULL;
9d19c6ea 47 const char *dest = NULL;
f2db854d
JH
48 struct ref **sought = NULL;
49 int nr_sought = 0, alloc_sought = 0;
ba227857 50 int fd[2];
9da69a65
JT
51 struct string_list pack_lockfiles = STRING_LIST_INIT_DUP;
52 struct string_list *pack_lockfiles_ptr = NULL;
ba227857 53 struct child_process *conn;
f8eb3036 54 struct fetch_pack_args args;
910650d2 55 struct oid_array shallow = OID_ARRAY_INIT;
a45a2600 56 struct string_list deepen_not = STRING_LIST_INIT_DUP;
ad6ac124 57 struct packet_reader reader;
4316ff30 58 enum protocol_version version;
e28714c5 59
8b4c0103
JT
60 fetch_if_missing = 0;
61
bbc30f99
JK
62 packet_trace_identity("fetch-pack");
63
f8eb3036
NTND
64 memset(&args, 0, sizeof(args));
65 args.uploadpack = "git-upload-pack";
66
ff22ff99 67 for (i = 1; i < argc && *argv[i] == '-'; i++) {
2d4177c0 68 const char *arg = argv[i];
def88e9a 69
45a3e526
NTND
70 if (skip_prefix(arg, "--upload-pack=", &arg)) {
71 args.uploadpack = arg;
ff22ff99
MH
72 continue;
73 }
45a3e526
NTND
74 if (skip_prefix(arg, "--exec=", &arg)) {
75 args.uploadpack = arg;
ff22ff99 76 continue;
def88e9a 77 }
ff22ff99
MH
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;
def88e9a 98 }
ff22ff99
MH
99 if (!strcmp("--stdin", arg)) {
100 args.stdin_refs = 1;
101 continue;
102 }
5610b7c0
TB
103 if (!strcmp("--diag-url", arg)) {
104 args.diag_url = 1;
105 continue;
106 }
ff22ff99
MH
107 if (!strcmp("-v", arg)) {
108 args.verbose = 1;
109 continue;
110 }
45a3e526
NTND
111 if (skip_prefix(arg, "--depth=", &arg)) {
112 args.depth = strtol(arg, NULL, 0);
ff22ff99
MH
113 continue;
114 }
508ea882
NTND
115 if (skip_prefix(arg, "--shallow-since=", &arg)) {
116 args.deepen_since = xstrdup(arg);
117 continue;
118 }
a45a2600
NTND
119 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
120 string_list_append(&deepen_not, arg);
121 continue;
122 }
cccf74e2
NTND
123 if (!strcmp(arg, "--deepen-relative")) {
124 args.deepen_relative = 1;
ff22ff99
MH
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;
9da69a65 137 pack_lockfiles_ptr = &pack_lockfiles;
ff22ff99
MH
138 continue;
139 }
9ba38048
NTND
140 if (!strcmp("--check-self-contained-and-connected", arg)) {
141 args.check_self_contained_and_connected = 1;
142 continue;
143 }
16094885
NTND
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 }
88e2f9ed
JT
152 if (!strcmp("--from-promisor", arg)) {
153 args.from_promisor = 1;
154 continue;
155 }
640d8b72
JH
156 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
157 parse_list_objects_filter(&args.filter_options, arg);
158 continue;
159 }
bc2d0c33 160 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
aa57b871 161 list_objects_filter_set_no_filter(&args.filter_options);
bc2d0c33
JH
162 continue;
163 }
ff22ff99 164 usage(fetch_pack_usage);
def88e9a 165 }
a45a2600
NTND
166 if (deepen_not.nr)
167 args.deepen_not = &deepen_not;
4cc00fcf
MH
168
169 if (i < argc)
170 dest = argv[i++];
171 else
def88e9a 172 usage(fetch_pack_usage);
2d4177c0 173
57e6fc69
MH
174 /*
175 * Copy refs from cmdline to growable list, then append any
176 * refs from the standard input:
177 */
57e6fc69 178 for (; i < argc; i++)
f2db854d 179 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
078b895f 180 if (args.stdin_refs) {
078b895f
IT
181 if (args.stateless_rpc) {
182 /* in stateless RPC mode we use pkt-line to read
183 * from stdin, until we get a flush packet
184 */
078b895f 185 for (;;) {
74543a04
JK
186 char *line = packet_read_line(0, NULL);
187 if (!line)
078b895f 188 break;
e013bdab 189 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
078b895f
IT
190 }
191 }
192 else {
193 /* read from stdin one ref per line, until EOF */
194 struct strbuf line = STRBUF_INIT;
8f309aeb 195 while (strbuf_getline_lf(&line, stdin) != EOF)
f2db854d 196 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
078b895f
IT
197 strbuf_release(&line);
198 }
199 }
200
249b2004
SP
201 if (args.stateless_rpc) {
202 conn = NULL;
203 fd[0] = 0;
204 fd[1] = 1;
ba227857 205 } else {
5610b7c0
TB
206 int flags = args.verbose ? CONNECT_VERBOSE : 0;
207 if (args.diag_url)
208 flags |= CONNECT_DIAG_URL;
9d19c6ea 209 conn = git_connect(fd, dest, args.uploadpack,
5610b7c0
TB
210 flags);
211 if (!conn)
212 return args.diag_url ? 0 : 1;
249b2004 213 }
ad6ac124
BW
214
215 packet_reader_init(&reader, fd[0], NULL, 0,
216 PACKET_READ_CHOMP_NEWLINE |
2d103c31
MS
217 PACKET_READ_GENTLE_ON_EOF |
218 PACKET_READ_DIE_ON_ERR_PACKET);
ad6ac124 219
4316ff30
JT
220 version = discover_version(&reader);
221 switch (version) {
8f6982b4 222 case protocol_v2:
b0df0c16 223 get_remote_refs(fd[1], &reader, &ref, 0, NULL, NULL, args.stateless_rpc);
4316ff30 224 break;
ad6ac124
BW
225 case protocol_v1:
226 case protocol_v0:
227 get_remote_heads(&reader, &ref, 0, NULL, &shallow);
228 break;
229 case protocol_unknown_version:
230 BUG("unknown protocol version");
231 }
249b2004 232
0f804b0b 233 ref = fetch_pack(&args, fd, ref, sought, nr_sought,
9da69a65
JT
234 &shallow, pack_lockfiles_ptr, version);
235 if (pack_lockfiles.nr) {
236 int i;
237
238 printf("lock %s\n", pack_lockfiles.items[0].string);
249b2004 239 fflush(stdout);
9da69a65
JT
240 for (i = 1; i < pack_lockfiles.nr; i++)
241 warning(_("Lockfile created but not reported: %s"),
242 pack_lockfiles.items[i].string);
ba227857 243 }
9ba38048
NTND
244 if (args.check_self_contained_and_connected &&
245 args.self_contained_and_connected) {
246 printf("connectivity-ok\n");
247 fflush(stdout);
248 }
249b2004
SP
249 close(fd[0]);
250 close(fd[1]);
251 if (finish_connect(conn))
7418f1a0 252 return 1;
2d4177c0 253
f2db854d 254 ret = !ref;
b285668d
MH
255
256 /*
257 * If the heads to pull were given, we should have consumed
258 * all of them by matching the remote. Otherwise, 'git fetch
259 * remote no-such-ref' would silently succeed without issuing
260 * an error.
261 */
e860d96b 262 ret |= report_unmatched_refs(sought, nr_sought);
f2db854d 263
2d4177c0
DB
264 while (ref) {
265 printf("%s %s\n",
f4e54d02 266 oid_to_hex(&ref->old_oid), ref->name);
2d4177c0
DB
267 ref = ref->next;
268 }
269
270 return ret;
271}