]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fetch-pack.c
Merge branch 'js/rebase-autostash-fix'
[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"
16094885 6#include "sha1-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];
249b2004
SP
51 char *pack_lockfile = NULL;
52 char **pack_lockfile_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;
e28714c5 58
8b4c0103
JT
59 fetch_if_missing = 0;
60
bbc30f99
JK
61 packet_trace_identity("fetch-pack");
62
f8eb3036
NTND
63 memset(&args, 0, sizeof(args));
64 args.uploadpack = "git-upload-pack";
65
ff22ff99 66 for (i = 1; i < argc && *argv[i] == '-'; i++) {
2d4177c0 67 const char *arg = argv[i];
def88e9a 68
45a3e526
NTND
69 if (skip_prefix(arg, "--upload-pack=", &arg)) {
70 args.uploadpack = arg;
ff22ff99
MH
71 continue;
72 }
45a3e526
NTND
73 if (skip_prefix(arg, "--exec=", &arg)) {
74 args.uploadpack = arg;
ff22ff99 75 continue;
def88e9a 76 }
ff22ff99
MH
77 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
78 args.quiet = 1;
79 continue;
80 }
81 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
82 args.lock_pack = args.keep_pack;
83 args.keep_pack = 1;
84 continue;
85 }
86 if (!strcmp("--thin", arg)) {
87 args.use_thin_pack = 1;
88 continue;
89 }
90 if (!strcmp("--include-tag", arg)) {
91 args.include_tag = 1;
92 continue;
93 }
94 if (!strcmp("--all", arg)) {
95 args.fetch_all = 1;
96 continue;
def88e9a 97 }
ff22ff99
MH
98 if (!strcmp("--stdin", arg)) {
99 args.stdin_refs = 1;
100 continue;
101 }
5610b7c0
TB
102 if (!strcmp("--diag-url", arg)) {
103 args.diag_url = 1;
104 continue;
105 }
ff22ff99
MH
106 if (!strcmp("-v", arg)) {
107 args.verbose = 1;
108 continue;
109 }
45a3e526
NTND
110 if (skip_prefix(arg, "--depth=", &arg)) {
111 args.depth = strtol(arg, NULL, 0);
ff22ff99
MH
112 continue;
113 }
508ea882
NTND
114 if (skip_prefix(arg, "--shallow-since=", &arg)) {
115 args.deepen_since = xstrdup(arg);
116 continue;
117 }
a45a2600
NTND
118 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
119 string_list_append(&deepen_not, arg);
120 continue;
121 }
cccf74e2
NTND
122 if (!strcmp(arg, "--deepen-relative")) {
123 args.deepen_relative = 1;
ff22ff99
MH
124 continue;
125 }
126 if (!strcmp("--no-progress", arg)) {
127 args.no_progress = 1;
128 continue;
129 }
130 if (!strcmp("--stateless-rpc", arg)) {
131 args.stateless_rpc = 1;
132 continue;
133 }
134 if (!strcmp("--lock-pack", arg)) {
135 args.lock_pack = 1;
136 pack_lockfile_ptr = &pack_lockfile;
137 continue;
138 }
9ba38048
NTND
139 if (!strcmp("--check-self-contained-and-connected", arg)) {
140 args.check_self_contained_and_connected = 1;
141 continue;
142 }
16094885
NTND
143 if (!strcmp("--cloning", arg)) {
144 args.cloning = 1;
145 continue;
146 }
147 if (!strcmp("--update-shallow", arg)) {
148 args.update_shallow = 1;
149 continue;
150 }
88e2f9ed
JT
151 if (!strcmp("--from-promisor", arg)) {
152 args.from_promisor = 1;
153 continue;
154 }
155 if (!strcmp("--no-dependents", arg)) {
156 args.no_dependents = 1;
157 continue;
158 }
640d8b72
JH
159 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
160 parse_list_objects_filter(&args.filter_options, arg);
161 continue;
162 }
bc2d0c33 163 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
aa57b871 164 list_objects_filter_set_no_filter(&args.filter_options);
bc2d0c33
JH
165 continue;
166 }
ff22ff99 167 usage(fetch_pack_usage);
def88e9a 168 }
a45a2600
NTND
169 if (deepen_not.nr)
170 args.deepen_not = &deepen_not;
4cc00fcf
MH
171
172 if (i < argc)
173 dest = argv[i++];
174 else
def88e9a 175 usage(fetch_pack_usage);
2d4177c0 176
57e6fc69
MH
177 /*
178 * Copy refs from cmdline to growable list, then append any
179 * refs from the standard input:
180 */
57e6fc69 181 for (; i < argc; i++)
f2db854d 182 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
078b895f 183 if (args.stdin_refs) {
078b895f
IT
184 if (args.stateless_rpc) {
185 /* in stateless RPC mode we use pkt-line to read
186 * from stdin, until we get a flush packet
187 */
078b895f 188 for (;;) {
74543a04
JK
189 char *line = packet_read_line(0, NULL);
190 if (!line)
078b895f 191 break;
e013bdab 192 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
078b895f
IT
193 }
194 }
195 else {
196 /* read from stdin one ref per line, until EOF */
197 struct strbuf line = STRBUF_INIT;
8f309aeb 198 while (strbuf_getline_lf(&line, stdin) != EOF)
f2db854d 199 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
078b895f
IT
200 strbuf_release(&line);
201 }
202 }
203
249b2004
SP
204 if (args.stateless_rpc) {
205 conn = NULL;
206 fd[0] = 0;
207 fd[1] = 1;
ba227857 208 } else {
5610b7c0
TB
209 int flags = args.verbose ? CONNECT_VERBOSE : 0;
210 if (args.diag_url)
211 flags |= CONNECT_DIAG_URL;
9d19c6ea 212 conn = git_connect(fd, dest, args.uploadpack,
5610b7c0
TB
213 flags);
214 if (!conn)
215 return args.diag_url ? 0 : 1;
249b2004 216 }
ad6ac124
BW
217
218 packet_reader_init(&reader, fd[0], NULL, 0,
219 PACKET_READ_CHOMP_NEWLINE |
220 PACKET_READ_GENTLE_ON_EOF);
221
222 switch (discover_version(&reader)) {
8f6982b4
BW
223 case protocol_v2:
224 die("support for protocol v2 not implemented yet");
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
16094885 233 ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
685fbd32 234 &shallow, pack_lockfile_ptr, protocol_v0);
249b2004
SP
235 if (pack_lockfile) {
236 printf("lock %s\n", pack_lockfile);
237 fflush(stdout);
ba227857 238 }
9ba38048
NTND
239 if (args.check_self_contained_and_connected &&
240 args.self_contained_and_connected) {
241 printf("connectivity-ok\n");
242 fflush(stdout);
243 }
249b2004
SP
244 close(fd[0]);
245 close(fd[1]);
246 if (finish_connect(conn))
7418f1a0 247 return 1;
2d4177c0 248
f2db854d 249 ret = !ref;
b285668d
MH
250
251 /*
252 * If the heads to pull were given, we should have consumed
253 * all of them by matching the remote. Otherwise, 'git fetch
254 * remote no-such-ref' would silently succeed without issuing
255 * an error.
256 */
e860d96b 257 ret |= report_unmatched_refs(sought, nr_sought);
f2db854d 258
2d4177c0
DB
259 while (ref) {
260 printf("%s %s\n",
f4e54d02 261 oid_to_hex(&ref->old_oid), ref->name);
2d4177c0
DB
262 ref = ref->next;
263 }
264
265 return ret;
266}