diff --no-index: fix -R with stdin
[git/debian.git] / builtin / fetch-pack.c
blob3ba0fe5a39614b3785f80ad75cd0f3a494547f37
1 #include "builtin.h"
2 #include "alloc.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "object-file.h"
6 #include "pkt-line.h"
7 #include "fetch-pack.h"
8 #include "remote.h"
9 #include "connect.h"
10 #include "oid-array.h"
11 #include "protocol.h"
13 static const char fetch_pack_usage[] =
14 "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
15 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
16 "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
18 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
19 const char *name)
21 struct ref *ref;
22 struct object_id oid;
23 const char *p;
25 if (!parse_oid_hex(name, &oid, &p)) {
26 if (*p == ' ') {
27 /* <oid> <ref>, find refname */
28 name = p + 1;
29 } else if (*p == '\0') {
30 ; /* <oid>, leave oid as name */
31 } else {
32 /* <ref>, clear cruft from oid */
33 oidclr(&oid);
35 } else {
36 /* <ref>, clear cruft from get_oid_hex */
37 oidclr(&oid);
40 ref = alloc_ref(name);
41 oidcpy(&ref->old_oid, &oid);
42 (*nr)++;
43 ALLOC_GROW(*sought, *nr, *alloc);
44 (*sought)[*nr - 1] = ref;
47 int cmd_fetch_pack(int argc, const char **argv, const char *prefix UNUSED)
49 int i, ret;
50 struct ref *ref = NULL;
51 const char *dest = NULL;
52 struct ref **sought = NULL;
53 int nr_sought = 0, alloc_sought = 0;
54 int fd[2];
55 struct string_list pack_lockfiles = STRING_LIST_INIT_DUP;
56 struct string_list *pack_lockfiles_ptr = NULL;
57 struct child_process *conn;
58 struct fetch_pack_args args;
59 struct oid_array shallow = OID_ARRAY_INIT;
60 struct string_list deepen_not = STRING_LIST_INIT_DUP;
61 struct packet_reader reader;
62 enum protocol_version version;
64 fetch_if_missing = 0;
66 packet_trace_identity("fetch-pack");
68 memset(&args, 0, sizeof(args));
69 list_objects_filter_init(&args.filter_options);
70 args.uploadpack = "git-upload-pack";
72 for (i = 1; i < argc && *argv[i] == '-'; i++) {
73 const char *arg = argv[i];
75 if (skip_prefix(arg, "--upload-pack=", &arg)) {
76 args.uploadpack = arg;
77 continue;
79 if (skip_prefix(arg, "--exec=", &arg)) {
80 args.uploadpack = arg;
81 continue;
83 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
84 args.quiet = 1;
85 continue;
87 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
88 args.lock_pack = args.keep_pack;
89 args.keep_pack = 1;
90 continue;
92 if (!strcmp("--thin", arg)) {
93 args.use_thin_pack = 1;
94 continue;
96 if (!strcmp("--include-tag", arg)) {
97 args.include_tag = 1;
98 continue;
100 if (!strcmp("--all", arg)) {
101 args.fetch_all = 1;
102 continue;
104 if (!strcmp("--stdin", arg)) {
105 args.stdin_refs = 1;
106 continue;
108 if (!strcmp("--diag-url", arg)) {
109 args.diag_url = 1;
110 continue;
112 if (!strcmp("-v", arg)) {
113 args.verbose = 1;
114 continue;
116 if (skip_prefix(arg, "--depth=", &arg)) {
117 args.depth = strtol(arg, NULL, 0);
118 continue;
120 if (skip_prefix(arg, "--shallow-since=", &arg)) {
121 args.deepen_since = xstrdup(arg);
122 continue;
124 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
125 string_list_append(&deepen_not, arg);
126 continue;
128 if (!strcmp(arg, "--deepen-relative")) {
129 args.deepen_relative = 1;
130 continue;
132 if (!strcmp("--no-progress", arg)) {
133 args.no_progress = 1;
134 continue;
136 if (!strcmp("--stateless-rpc", arg)) {
137 args.stateless_rpc = 1;
138 continue;
140 if (!strcmp("--lock-pack", arg)) {
141 args.lock_pack = 1;
142 pack_lockfiles_ptr = &pack_lockfiles;
143 continue;
145 if (!strcmp("--check-self-contained-and-connected", arg)) {
146 args.check_self_contained_and_connected = 1;
147 continue;
149 if (!strcmp("--cloning", arg)) {
150 args.cloning = 1;
151 continue;
153 if (!strcmp("--update-shallow", arg)) {
154 args.update_shallow = 1;
155 continue;
157 if (!strcmp("--from-promisor", arg)) {
158 args.from_promisor = 1;
159 continue;
161 if (!strcmp("--refetch", arg)) {
162 args.refetch = 1;
163 continue;
165 if (skip_prefix(arg, ("--filter="), &arg)) {
166 parse_list_objects_filter(&args.filter_options, arg);
167 continue;
169 if (!strcmp(arg, ("--no-filter"))) {
170 list_objects_filter_set_no_filter(&args.filter_options);
171 continue;
173 usage(fetch_pack_usage);
175 if (deepen_not.nr)
176 args.deepen_not = &deepen_not;
178 if (i < argc)
179 dest = argv[i++];
180 else
181 usage(fetch_pack_usage);
184 * Copy refs from cmdline to growable list, then append any
185 * refs from the standard input:
187 for (; i < argc; i++)
188 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
189 if (args.stdin_refs) {
190 if (args.stateless_rpc) {
191 /* in stateless RPC mode we use pkt-line to read
192 * from stdin, until we get a flush packet
194 for (;;) {
195 char *line = packet_read_line(0, NULL);
196 if (!line)
197 break;
198 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
201 else {
202 /* read from stdin one ref per line, until EOF */
203 struct strbuf line = STRBUF_INIT;
204 while (strbuf_getline_lf(&line, stdin) != EOF)
205 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
206 strbuf_release(&line);
210 if (args.stateless_rpc) {
211 conn = NULL;
212 fd[0] = 0;
213 fd[1] = 1;
214 } else {
215 int flags = args.verbose ? CONNECT_VERBOSE : 0;
216 if (args.diag_url)
217 flags |= CONNECT_DIAG_URL;
218 conn = git_connect(fd, dest, "git-upload-pack",
219 args.uploadpack, flags);
220 if (!conn)
221 return args.diag_url ? 0 : 1;
224 packet_reader_init(&reader, fd[0], NULL, 0,
225 PACKET_READ_CHOMP_NEWLINE |
226 PACKET_READ_GENTLE_ON_EOF |
227 PACKET_READ_DIE_ON_ERR_PACKET);
229 version = discover_version(&reader);
230 switch (version) {
231 case protocol_v2:
232 get_remote_refs(fd[1], &reader, &ref, 0, NULL, NULL,
233 args.stateless_rpc);
234 break;
235 case protocol_v1:
236 case protocol_v0:
237 get_remote_heads(&reader, &ref, 0, NULL, &shallow);
238 break;
239 case protocol_unknown_version:
240 BUG("unknown protocol version");
243 ref = fetch_pack(&args, fd, ref, sought, nr_sought,
244 &shallow, pack_lockfiles_ptr, version);
245 if (pack_lockfiles.nr) {
246 int i;
248 printf("lock %s\n", pack_lockfiles.items[0].string);
249 fflush(stdout);
250 for (i = 1; i < pack_lockfiles.nr; i++)
251 warning(_("Lockfile created but not reported: %s"),
252 pack_lockfiles.items[i].string);
254 if (args.check_self_contained_and_connected &&
255 args.self_contained_and_connected) {
256 printf("connectivity-ok\n");
257 fflush(stdout);
259 close(fd[0]);
260 close(fd[1]);
261 if (finish_connect(conn))
262 return 1;
264 ret = !ref;
267 * If the heads to pull were given, we should have consumed
268 * all of them by matching the remote. Otherwise, 'git fetch
269 * remote no-such-ref' would silently succeed without issuing
270 * an error.
272 ret |= report_unmatched_refs(sought, nr_sought);
274 while (ref) {
275 printf("%s %s\n",
276 oid_to_hex(&ref->old_oid), ref->name);
277 ref = ref->next;
280 return ret;