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