]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fetch-pack.c
Merge branch 'ms/send-email-validate-fix'
[thirdparty/git.git] / builtin / fetch-pack.c
CommitLineData
c2e86add 1#include "builtin.h"
f394e093 2#include "gettext.h"
41771fa4 3#include "hex.h"
87bed179 4#include "object-file.h"
def88e9a 5#include "pkt-line.h"
2d4177c0 6#include "fetch-pack.h"
47a59185
JH
7#include "remote.h"
8#include "connect.h"
fe299ec5 9#include "oid-array.h"
ad6ac124 10#include "protocol.h"
fa740529 11
33b83034 12static const char fetch_pack_usage[] =
9c9b4f2f 13"git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
078b895f 14"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
5610b7c0 15"[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
def88e9a 16
5545f057
JK
17static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
18 const char *name)
f2db854d 19{
5545f057 20 struct ref *ref;
854ecb9c 21 struct object_id oid;
7b5e614e 22 const char *p;
58f2ed05 23
7b5e614e 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 */
4a8d202c
GSF
30 } else {
31 /* <ref>, clear cruft from oid */
32 oidclr(&oid);
33 }
34 } else {
35 /* <ref>, clear cruft from get_oid_hex */
5545f057 36 oidclr(&oid);
4a8d202c 37 }
f2db854d 38
5545f057
JK
39 ref = alloc_ref(name);
40 oidcpy(&ref->old_oid, &oid);
f2db854d
JH
41 (*nr)++;
42 ALLOC_GROW(*sought, *nr, *alloc);
43 (*sought)[*nr - 1] = ref;
44}
45
5247b762 46int cmd_fetch_pack(int argc, const char **argv, const char *prefix UNUSED)
50ab5fd3 47{
57e6fc69 48 int i, ret;
ba227857 49 struct ref *ref = NULL;
9d19c6ea 50 const char *dest = NULL;
f2db854d
JH
51 struct ref **sought = NULL;
52 int nr_sought = 0, alloc_sought = 0;
ba227857 53 int fd[2];
9da69a65
JT
54 struct string_list pack_lockfiles = STRING_LIST_INIT_DUP;
55 struct string_list *pack_lockfiles_ptr = NULL;
ba227857 56 struct child_process *conn;
f8eb3036 57 struct fetch_pack_args args;
910650d2 58 struct oid_array shallow = OID_ARRAY_INIT;
a45a2600 59 struct string_list deepen_not = STRING_LIST_INIT_DUP;
ad6ac124 60 struct packet_reader reader;
4316ff30 61 enum protocol_version version;
e28714c5 62
8b4c0103
JT
63 fetch_if_missing = 0;
64
bbc30f99
JK
65 packet_trace_identity("fetch-pack");
66
f8eb3036 67 memset(&args, 0, sizeof(args));
2a01bded 68 list_objects_filter_init(&args.filter_options);
f8eb3036
NTND
69 args.uploadpack = "git-upload-pack";
70
ff22ff99 71 for (i = 1; i < argc && *argv[i] == '-'; i++) {
2d4177c0 72 const char *arg = argv[i];
def88e9a 73
45a3e526
NTND
74 if (skip_prefix(arg, "--upload-pack=", &arg)) {
75 args.uploadpack = arg;
ff22ff99
MH
76 continue;
77 }
45a3e526
NTND
78 if (skip_prefix(arg, "--exec=", &arg)) {
79 args.uploadpack = arg;
ff22ff99 80 continue;
def88e9a 81 }
ff22ff99
MH
82 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
83 args.quiet = 1;
84 continue;
85 }
86 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
87 args.lock_pack = args.keep_pack;
88 args.keep_pack = 1;
89 continue;
90 }
91 if (!strcmp("--thin", arg)) {
92 args.use_thin_pack = 1;
93 continue;
94 }
95 if (!strcmp("--include-tag", arg)) {
96 args.include_tag = 1;
97 continue;
98 }
99 if (!strcmp("--all", arg)) {
100 args.fetch_all = 1;
101 continue;
def88e9a 102 }
ff22ff99
MH
103 if (!strcmp("--stdin", arg)) {
104 args.stdin_refs = 1;
105 continue;
106 }
5610b7c0
TB
107 if (!strcmp("--diag-url", arg)) {
108 args.diag_url = 1;
109 continue;
110 }
ff22ff99
MH
111 if (!strcmp("-v", arg)) {
112 args.verbose = 1;
113 continue;
114 }
45a3e526
NTND
115 if (skip_prefix(arg, "--depth=", &arg)) {
116 args.depth = strtol(arg, NULL, 0);
ff22ff99
MH
117 continue;
118 }
508ea882
NTND
119 if (skip_prefix(arg, "--shallow-since=", &arg)) {
120 args.deepen_since = xstrdup(arg);
121 continue;
122 }
a45a2600
NTND
123 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
124 string_list_append(&deepen_not, arg);
125 continue;
126 }
cccf74e2
NTND
127 if (!strcmp(arg, "--deepen-relative")) {
128 args.deepen_relative = 1;
ff22ff99
MH
129 continue;
130 }
131 if (!strcmp("--no-progress", arg)) {
132 args.no_progress = 1;
133 continue;
134 }
135 if (!strcmp("--stateless-rpc", arg)) {
136 args.stateless_rpc = 1;
137 continue;
138 }
139 if (!strcmp("--lock-pack", arg)) {
140 args.lock_pack = 1;
9da69a65 141 pack_lockfiles_ptr = &pack_lockfiles;
ff22ff99
MH
142 continue;
143 }
9ba38048
NTND
144 if (!strcmp("--check-self-contained-and-connected", arg)) {
145 args.check_self_contained_and_connected = 1;
146 continue;
147 }
16094885
NTND
148 if (!strcmp("--cloning", arg)) {
149 args.cloning = 1;
150 continue;
151 }
152 if (!strcmp("--update-shallow", arg)) {
153 args.update_shallow = 1;
154 continue;
155 }
88e2f9ed
JT
156 if (!strcmp("--from-promisor", arg)) {
157 args.from_promisor = 1;
158 continue;
159 }
869a0eb4
RC
160 if (!strcmp("--refetch", arg)) {
161 args.refetch = 1;
162 continue;
163 }
cc910442 164 if (skip_prefix(arg, ("--filter="), &arg)) {
640d8b72
JH
165 parse_list_objects_filter(&args.filter_options, arg);
166 continue;
167 }
cc910442 168 if (!strcmp(arg, ("--no-filter"))) {
aa57b871 169 list_objects_filter_set_no_filter(&args.filter_options);
bc2d0c33
JH
170 continue;
171 }
ff22ff99 172 usage(fetch_pack_usage);
def88e9a 173 }
a45a2600
NTND
174 if (deepen_not.nr)
175 args.deepen_not = &deepen_not;
4cc00fcf
MH
176
177 if (i < argc)
178 dest = argv[i++];
179 else
def88e9a 180 usage(fetch_pack_usage);
2d4177c0 181
57e6fc69
MH
182 /*
183 * Copy refs from cmdline to growable list, then append any
184 * refs from the standard input:
185 */
57e6fc69 186 for (; i < argc; i++)
f2db854d 187 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
078b895f 188 if (args.stdin_refs) {
078b895f
IT
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
192 */
078b895f 193 for (;;) {
74543a04
JK
194 char *line = packet_read_line(0, NULL);
195 if (!line)
078b895f 196 break;
e013bdab 197 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
078b895f
IT
198 }
199 }
200 else {
201 /* read from stdin one ref per line, until EOF */
202 struct strbuf line = STRBUF_INIT;
8f309aeb 203 while (strbuf_getline_lf(&line, stdin) != EOF)
f2db854d 204 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
078b895f
IT
205 strbuf_release(&line);
206 }
207 }
208
249b2004
SP
209 if (args.stateless_rpc) {
210 conn = NULL;
211 fd[0] = 0;
212 fd[1] = 1;
ba227857 213 } else {
5610b7c0
TB
214 int flags = args.verbose ? CONNECT_VERBOSE : 0;
215 if (args.diag_url)
216 flags |= CONNECT_DIAG_URL;
eaa0fd65
JK
217 conn = git_connect(fd, dest, "git-upload-pack",
218 args.uploadpack, flags);
5610b7c0
TB
219 if (!conn)
220 return args.diag_url ? 0 : 1;
249b2004 221 }
ad6ac124
BW
222
223 packet_reader_init(&reader, fd[0], NULL, 0,
224 PACKET_READ_CHOMP_NEWLINE |
2d103c31
MS
225 PACKET_READ_GENTLE_ON_EOF |
226 PACKET_READ_DIE_ON_ERR_PACKET);
ad6ac124 227
4316ff30
JT
228 version = discover_version(&reader);
229 switch (version) {
8f6982b4 230 case protocol_v2:
39835409
JT
231 get_remote_refs(fd[1], &reader, &ref, 0, NULL, NULL,
232 args.stateless_rpc);
4316ff30 233 break;
ad6ac124
BW
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");
240 }
249b2004 241
0f804b0b 242 ref = fetch_pack(&args, fd, ref, sought, nr_sought,
9da69a65
JT
243 &shallow, pack_lockfiles_ptr, version);
244 if (pack_lockfiles.nr) {
245 int i;
246
247 printf("lock %s\n", pack_lockfiles.items[0].string);
249b2004 248 fflush(stdout);
9da69a65
JT
249 for (i = 1; i < pack_lockfiles.nr; i++)
250 warning(_("Lockfile created but not reported: %s"),
251 pack_lockfiles.items[i].string);
ba227857 252 }
9ba38048
NTND
253 if (args.check_self_contained_and_connected &&
254 args.self_contained_and_connected) {
255 printf("connectivity-ok\n");
256 fflush(stdout);
257 }
249b2004
SP
258 close(fd[0]);
259 close(fd[1]);
260 if (finish_connect(conn))
7418f1a0 261 return 1;
2d4177c0 262
f2db854d 263 ret = !ref;
b285668d
MH
264
265 /*
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.
270 */
e860d96b 271 ret |= report_unmatched_refs(sought, nr_sought);
f2db854d 272
2d4177c0
DB
273 while (ref) {
274 printf("%s %s\n",
f4e54d02 275 oid_to_hex(&ref->old_oid), ref->name);
2d4177c0
DB
276 ref = ref->next;
277 }
278
279 return ret;
280}