b2374ddbbf58bb077423c32885a051c3405f6485
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
9static 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
14static 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
20 if (!get_oid_hex(name, &oid)) {
21 if (name[GIT_SHA1_HEXSZ] == ' ') {
22 /* <sha1> <ref>, find refname */
23 name += GIT_SHA1_HEXSZ + 1;
24 } else if (name[GIT_SHA1_HEXSZ] == '\0') {
25 ; /* <sha1>, leave sha1 as name */
26 } else {
27 /* <ref>, clear cruft from oid */
28 oidclr(&oid);
29 }
30 } else {
31 /* <ref>, clear cruft from get_oid_hex */
32 oidclr(&oid);
33 }
34
35 ref = alloc_ref(name);
36 oidcpy(&ref->old_oid, &oid);
37 (*nr)++;
38 ALLOC_GROW(*sought, *nr, *alloc);
39 (*sought)[*nr - 1] = ref;
40}
41
42int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
43{
44 int i, ret;
45 struct ref *ref = NULL;
46 const char *dest = NULL;
47 struct ref **sought = NULL;
48 int nr_sought = 0, alloc_sought = 0;
49 int fd[2];
50 char *pack_lockfile = NULL;
51 char **pack_lockfile_ptr = NULL;
52 struct child_process *conn;
53 struct fetch_pack_args args;
54 struct oid_array shallow = OID_ARRAY_INIT;
55 struct string_list deepen_not = STRING_LIST_INIT_DUP;
56 struct packet_reader reader;
57
58 packet_trace_identity("fetch-pack");
59
60 memset(&args, 0, sizeof(args));
61 args.uploadpack = "git-upload-pack";
62
63 for (i = 1; i < argc && *argv[i] == '-'; i++) {
64 const char *arg = argv[i];
65
66 if (skip_prefix(arg, "--upload-pack=", &arg)) {
67 args.uploadpack = arg;
68 continue;
69 }
70 if (skip_prefix(arg, "--exec=", &arg)) {
71 args.uploadpack = arg;
72 continue;
73 }
74 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
75 args.quiet = 1;
76 continue;
77 }
78 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
79 args.lock_pack = args.keep_pack;
80 args.keep_pack = 1;
81 continue;
82 }
83 if (!strcmp("--thin", arg)) {
84 args.use_thin_pack = 1;
85 continue;
86 }
87 if (!strcmp("--include-tag", arg)) {
88 args.include_tag = 1;
89 continue;
90 }
91 if (!strcmp("--all", arg)) {
92 args.fetch_all = 1;
93 continue;
94 }
95 if (!strcmp("--stdin", arg)) {
96 args.stdin_refs = 1;
97 continue;
98 }
99 if (!strcmp("--diag-url", arg)) {
100 args.diag_url = 1;
101 continue;
102 }
103 if (!strcmp("-v", arg)) {
104 args.verbose = 1;
105 continue;
106 }
107 if (skip_prefix(arg, "--depth=", &arg)) {
108 args.depth = strtol(arg, NULL, 0);
109 continue;
110 }
111 if (skip_prefix(arg, "--shallow-since=", &arg)) {
112 args.deepen_since = xstrdup(arg);
113 continue;
114 }
115 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
116 string_list_append(&deepen_not, arg);
117 continue;
118 }
119 if (!strcmp(arg, "--deepen-relative")) {
120 args.deepen_relative = 1;
121 continue;
122 }
123 if (!strcmp("--no-progress", arg)) {
124 args.no_progress = 1;
125 continue;
126 }
127 if (!strcmp("--stateless-rpc", arg)) {
128 args.stateless_rpc = 1;
129 continue;
130 }
131 if (!strcmp("--lock-pack", arg)) {
132 args.lock_pack = 1;
133 pack_lockfile_ptr = &pack_lockfile;
134 continue;
135 }
136 if (!strcmp("--check-self-contained-and-connected", arg)) {
137 args.check_self_contained_and_connected = 1;
138 continue;
139 }
140 if (!strcmp("--cloning", arg)) {
141 args.cloning = 1;
142 continue;
143 }
144 if (!strcmp("--update-shallow", arg)) {
145 args.update_shallow = 1;
146 continue;
147 }
148 usage(fetch_pack_usage);
149 }
150 if (deepen_not.nr)
151 args.deepen_not = &deepen_not;
152
153 if (i < argc)
154 dest = argv[i++];
155 else
156 usage(fetch_pack_usage);
157
158 /*
159 * Copy refs from cmdline to growable list, then append any
160 * refs from the standard input:
161 */
162 for (; i < argc; i++)
163 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
164 if (args.stdin_refs) {
165 if (args.stateless_rpc) {
166 /* in stateless RPC mode we use pkt-line to read
167 * from stdin, until we get a flush packet
168 */
169 for (;;) {
170 char *line = packet_read_line(0, NULL);
171 if (!line)
172 break;
173 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
174 }
175 }
176 else {
177 /* read from stdin one ref per line, until EOF */
178 struct strbuf line = STRBUF_INIT;
179 while (strbuf_getline_lf(&line, stdin) != EOF)
180 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
181 strbuf_release(&line);
182 }
183 }
184
185 if (args.stateless_rpc) {
186 conn = NULL;
187 fd[0] = 0;
188 fd[1] = 1;
189 } else {
190 int flags = args.verbose ? CONNECT_VERBOSE : 0;
191 if (args.diag_url)
192 flags |= CONNECT_DIAG_URL;
193 conn = git_connect(fd, dest, args.uploadpack,
194 flags);
195 if (!conn)
196 return args.diag_url ? 0 : 1;
197 }
198
199 packet_reader_init(&reader, fd[0], NULL, 0,
200 PACKET_READ_CHOMP_NEWLINE |
201 PACKET_READ_GENTLE_ON_EOF);
202
203 switch (discover_version(&reader)) {
204 case protocol_v2:
205 die("support for protocol v2 not implemented yet");
206 case protocol_v1:
207 case protocol_v0:
208 get_remote_heads(&reader, &ref, 0, NULL, &shallow);
209 break;
210 case protocol_unknown_version:
211 BUG("unknown protocol version");
212 }
213
214 ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
215 &shallow, pack_lockfile_ptr);
216 if (pack_lockfile) {
217 printf("lock %s\n", pack_lockfile);
218 fflush(stdout);
219 }
220 if (args.check_self_contained_and_connected &&
221 args.self_contained_and_connected) {
222 printf("connectivity-ok\n");
223 fflush(stdout);
224 }
225 close(fd[0]);
226 close(fd[1]);
227 if (finish_connect(conn))
228 return 1;
229
230 ret = !ref;
231
232 /*
233 * If the heads to pull were given, we should have consumed
234 * all of them by matching the remote. Otherwise, 'git fetch
235 * remote no-such-ref' would silently succeed without issuing
236 * an error.
237 */
238 ret |= report_unmatched_refs(sought, nr_sought);
239
240 while (ref) {
241 printf("%s %s\n",
242 oid_to_hex(&ref->old_oid), ref->name);
243 ref = ref->next;
244 }
245
246 return ret;
247}