blob: 947da545de0556692e0e424c5736c96e36ecf3db [file] [log] [blame]
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001#include "cache.h"
Stefan Bellera49d2832018-03-23 18:45:21 +01002#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02004#include "lockfile.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07005#include "refs.h"
6#include "pkt-line.h"
7#include "commit.h"
8#include "tag.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07009#include "exec-cmd.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070010#include "pack.h"
11#include "sideband.h"
12#include "fetch-pack.h"
13#include "remote.h"
14#include "run-command.h"
Junio C Hamano47a59182013-07-08 13:56:53 -070015#include "connect.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070016#include "transport.h"
17#include "version.h"
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +070018#include "sha1-array.h"
Jonathan Tanfdb69d32017-05-15 10:32:20 -070019#include "oidset.h"
Jonathan Tan0abe14f2017-08-18 15:20:26 -070020#include "packfile.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070021#include "object-store.h"
Jonathan Tancf1e7c02018-07-02 15:08:43 -070022#include "connected.h"
Jonathan Tanec062832018-06-14 15:54:28 -070023#include "fetch-negotiator.h"
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +000024#include "fsck.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070025
26static int transfer_unpack_limit = -1;
27static int fetch_unpack_limit = -1;
28static int unpack_limit = 100;
29static int prefer_ofs_delta = 1;
30static int no_done;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070031static int deepen_since_ok;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070032static int deepen_not_ok;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070033static int fetch_fsck_objects = -1;
34static int transfer_fsck_objects = -1;
35static int agent_supported;
Jeff Hostetler640d8b72017-12-08 15:58:40 +000036static int server_supports_filtering;
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +070037static struct lock_file shallow_lock;
38static const char *alternate_shallow_file;
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +000039static struct strbuf fsck_msg_types = STRBUF_INIT;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070040
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070041/* Remember to update object flag allocation in object.h */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070042#define COMPLETE (1U << 0)
Jonathan Tanec062832018-06-14 15:54:28 -070043#define ALTERNATE (1U << 1)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070044
45/*
46 * After sending this many "have"s if we do not get any new ACK , we
47 * give up traversing our history.
48 */
49#define MAX_IN_VAIN 256
50
Jonathan Tand30fe892018-06-14 15:54:26 -070051static int multi_ack, use_sideband;
Fredrik Medley7199c092015-05-21 22:23:38 +020052/* Allow specifying sha1 if it is a ref tip. */
53#define ALLOW_TIP_SHA1 01
Fredrik Medley68ee6282015-05-21 22:23:39 +020054/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
55#define ALLOW_REACHABLE_SHA1 02
Fredrik Medley7199c092015-05-21 22:23:38 +020056static unsigned int allow_unadvertised_object_request;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070057
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +070058__attribute__((format (printf, 2, 3)))
59static inline void print_verbose(const struct fetch_pack_args *args,
60 const char *fmt, ...)
61{
62 va_list params;
63
64 if (!args->verbose)
65 return;
66
67 va_start(params, fmt);
68 vfprintf(stderr, fmt, params);
69 va_end(params);
70 fputc('\n', stderr);
71}
72
Jeff King41a078c2017-02-08 15:53:03 -050073struct alternate_object_cache {
74 struct object **items;
75 size_t nr, alloc;
76};
77
Jeff Kingbdf42762018-10-08 11:09:23 -070078static void cache_one_alternate(const struct object_id *oid,
Jeff King41a078c2017-02-08 15:53:03 -050079 void *vcache)
80{
81 struct alternate_object_cache *cache = vcache;
Stefan Beller109cd762018-06-28 18:21:51 -070082 struct object *obj = parse_object(the_repository, oid);
Jeff King41a078c2017-02-08 15:53:03 -050083
84 if (!obj || (obj->flags & ALTERNATE))
85 return;
86
87 obj->flags |= ALTERNATE;
88 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
89 cache->items[cache->nr++] = obj;
90}
91
Jonathan Tanec062832018-06-14 15:54:28 -070092static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
93 void (*cb)(struct fetch_negotiator *,
Jonathan Tand30fe892018-06-14 15:54:26 -070094 struct object *))
Jeff King41a078c2017-02-08 15:53:03 -050095{
96 static int initialized;
97 static struct alternate_object_cache cache;
98 size_t i;
99
100 if (!initialized) {
101 for_each_alternate_ref(cache_one_alternate, &cache);
102 initialized = 1;
103 }
104
105 for (i = 0; i < cache.nr; i++)
Jonathan Tanec062832018-06-14 15:54:28 -0700106 cb(negotiator, cache.items[i]);
Jeff King41a078c2017-02-08 15:53:03 -0500107}
108
Jonathan Tanec062832018-06-14 15:54:28 -0700109static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700110 const char *refname,
111 const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700112{
Stefan Bellera74093d2018-06-28 18:22:05 -0700113 struct object *o = deref_tag(the_repository,
114 parse_object(the_repository, oid),
Stefan Beller109cd762018-06-28 18:21:51 -0700115 refname, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700116
117 if (o && o->type == OBJ_COMMIT)
Jonathan Tanec062832018-06-14 15:54:28 -0700118 negotiator->add_tip(negotiator, (struct commit *)o);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700119
120 return 0;
121}
122
Michael Haggertyb1b49c62015-05-25 18:39:18 +0000123static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
124 int flag, void *cb_data)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700125{
Jonathan Tand30fe892018-06-14 15:54:26 -0700126 return rev_list_insert_ref(cb_data, refname, oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700127}
128
129enum ack_type {
130 NAK = 0,
131 ACK,
132 ACK_continue,
133 ACK_common,
134 ACK_ready
135};
136
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800137static void consume_shallow_list(struct fetch_pack_args *args,
138 struct packet_reader *reader)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700139{
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700140 if (args->stateless_rpc && args->deepen) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700141 /* If we sent a depth we will get back "duplicate"
142 * shallow and unshallow commands every time there
143 * is a block of have lines exchanged.
144 */
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800145 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
146 if (starts_with(reader->line, "shallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700147 continue;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800148 if (starts_with(reader->line, "unshallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700149 continue;
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700150 die(_("git fetch-pack: expected shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700151 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800152 if (reader->status != PACKET_READ_FLUSH)
153 die(_("git fetch-pack: expected a flush packet after shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700154 }
155}
156
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800157static enum ack_type get_ack(struct packet_reader *reader,
158 struct object_id *result_oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700159{
Jeff King74543a02013-02-20 15:02:57 -0500160 int len;
Jeff King82e56762014-06-18 15:56:03 -0400161 const char *arg;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700162
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800163 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
Jeff Kingbc9d4dc2018-02-08 13:47:49 -0500164 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800165 len = reader->pktlen;
166
167 if (!strcmp(reader->line, "NAK"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700168 return NAK;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800169 if (skip_prefix(reader->line, "ACK ", &arg)) {
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000170 const char *p;
171 if (!parse_oid_hex(arg, result_oid, &p)) {
172 len -= p - reader->line;
Jeff King82e56762014-06-18 15:56:03 -0400173 if (len < 1)
Jeff King030e9dd2013-02-20 15:00:28 -0500174 return ACK;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000175 if (strstr(p, "continue"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700176 return ACK_continue;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000177 if (strstr(p, "common"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700178 return ACK_common;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000179 if (strstr(p, "ready"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700180 return ACK_ready;
181 return ACK;
182 }
183 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800184 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700185}
186
187static void send_request(struct fetch_pack_args *args,
188 int fd, struct strbuf *buf)
189{
190 if (args->stateless_rpc) {
191 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
192 packet_flush(fd);
Jeff King37c80012019-03-04 23:11:39 -0500193 } else {
194 if (write_in_full(fd, buf->buf, buf->len) < 0)
195 die_errno(_("unable to write to remote"));
196 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700197}
198
Jonathan Tanec062832018-06-14 15:54:28 -0700199static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700200 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700201{
Jonathan Tanec062832018-06-14 15:54:28 -0700202 rev_list_insert_ref(negotiator, NULL, &obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700203}
204
205#define INITIAL_FLUSH 16
206#define PIPESAFE_FLUSH 32
Jonathan Tanda470982016-07-18 15:21:38 -0700207#define LARGE_FLUSH 16384
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700208
Brandon Williams685fbd32018-03-15 10:31:28 -0700209static int next_flush(int stateless_rpc, int count)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700210{
Brandon Williams685fbd32018-03-15 10:31:28 -0700211 if (stateless_rpc) {
Jonathan Tanda470982016-07-18 15:21:38 -0700212 if (count < LARGE_FLUSH)
213 count <<= 1;
214 else
215 count = count * 11 / 10;
216 } else {
217 if (count < PIPESAFE_FLUSH)
218 count <<= 1;
219 else
220 count += PIPESAFE_FLUSH;
221 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700222 return count;
223}
224
Jonathan Tan3390e422018-07-02 15:39:44 -0700225static void mark_tips(struct fetch_negotiator *negotiator,
226 const struct oid_array *negotiation_tips)
227{
228 int i;
229
230 if (!negotiation_tips) {
231 for_each_ref(rev_list_insert_ref_oid, negotiator);
232 return;
233 }
234
235 for (i = 0; i < negotiation_tips->nr; i++)
236 rev_list_insert_ref(negotiator, NULL,
237 &negotiation_tips->oid[i]);
238 return;
239}
240
Jonathan Tanec062832018-06-14 15:54:28 -0700241static int find_common(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700242 struct fetch_pack_args *args,
brian m. carlson1b283372017-05-01 02:28:54 +0000243 int fd[2], struct object_id *result_oid,
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700244 struct ref *refs)
245{
246 int fetching;
247 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
brian m. carlson1b283372017-05-01 02:28:54 +0000248 const struct object_id *oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700249 unsigned in_vain = 0;
250 int got_continue = 0;
251 int got_ready = 0;
252 struct strbuf req_buf = STRBUF_INIT;
253 size_t state_len = 0;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800254 struct packet_reader reader;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700255
256 if (args->stateless_rpc && multi_ack == 1)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700257 die(_("--stateless-rpc requires multi_ack_detailed"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700258
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800259 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -0800260 PACKET_READ_CHOMP_NEWLINE |
261 PACKET_READ_DIE_ON_ERR_PACKET);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800262
Jonathan Tan12f19a92018-10-03 16:04:52 -0700263 if (!args->no_dependents) {
264 mark_tips(negotiator, args->negotiation_tips);
265 for_each_cached_alternate(negotiator, insert_one_alternate_object);
266 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700267
268 fetching = 0;
269 for ( ; refs ; refs = refs->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000270 struct object_id *remote = &refs->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700271 const char *remote_hex;
272 struct object *o;
273
274 /*
275 * If that object is complete (i.e. it is an ancestor of a
276 * local ref), we tell them we have it but do not have to
277 * tell them about its ancestors, which they already know
278 * about.
279 *
280 * We use lookup_object here because we are only
281 * interested in the case we *know* the object is
282 * reachable and we have already scanned it.
Jonathan Tan12f19a92018-10-03 16:04:52 -0700283 *
284 * Do this only if args->no_dependents is false (if it is true,
285 * we cannot trust the object flags).
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700286 */
Jonathan Tan12f19a92018-10-03 16:04:52 -0700287 if (!args->no_dependents &&
Jeff Kingd0229ab2019-06-20 03:41:14 -0400288 ((o = lookup_object(the_repository, remote)) != NULL) &&
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700289 (o->flags & COMPLETE)) {
290 continue;
291 }
292
brian m. carlson1b283372017-05-01 02:28:54 +0000293 remote_hex = oid_to_hex(remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700294 if (!fetching) {
295 struct strbuf c = STRBUF_INIT;
296 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
297 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
298 if (no_done) strbuf_addstr(&c, " no-done");
299 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
300 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700301 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700302 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
303 if (args->no_progress) strbuf_addstr(&c, " no-progress");
304 if (args->include_tag) strbuf_addstr(&c, " include-tag");
305 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700306 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700307 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700308 if (agent_supported) strbuf_addf(&c, " agent=%s",
309 git_user_agent_sanitized());
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000310 if (args->filter_options.choice)
311 strbuf_addstr(&c, " filter");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700312 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
313 strbuf_release(&c);
314 } else
315 packet_buf_write(&req_buf, "want %s\n", remote_hex);
316 fetching++;
317 }
318
319 if (!fetching) {
320 strbuf_release(&req_buf);
321 packet_flush(fd[1]);
322 return 1;
323 }
324
Stefan Bellerc8813482018-05-17 15:51:46 -0700325 if (is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700326 write_shallow_commits(&req_buf, 1, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700327 if (args->depth > 0)
328 packet_buf_write(&req_buf, "deepen %d", args->depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700329 if (args->deepen_since) {
Johannes Schindelindddbad72017-04-26 21:29:31 +0200330 timestamp_t max_age = approxidate(args->deepen_since);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200331 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700332 }
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700333 if (args->deepen_not) {
334 int i;
335 for (i = 0; i < args->deepen_not->nr; i++) {
336 struct string_list_item *s = args->deepen_not->items + i;
337 packet_buf_write(&req_buf, "deepen-not %s", s->string);
338 }
339 }
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800340 if (server_supports_filtering && args->filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700341 const char *spec =
342 expand_list_objects_filter_spec(&args->filter_options);
343 packet_buf_write(&req_buf, "filter %s", spec);
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800344 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700345 packet_buf_flush(&req_buf);
346 state_len = req_buf.len;
347
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700348 if (args->deepen) {
Jeff Kingae021d82014-06-18 15:47:50 -0400349 const char *arg;
brian m. carlson1b283372017-05-01 02:28:54 +0000350 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700351
352 send_request(args, fd[1], &req_buf);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800353 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
354 if (skip_prefix(reader.line, "shallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000355 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800356 die(_("invalid shallow line: %s"), reader.line);
Stefan Beller19143f12018-05-17 15:51:44 -0700357 register_shallow(the_repository, &oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700358 continue;
359 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800360 if (skip_prefix(reader.line, "unshallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000361 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800362 die(_("invalid unshallow line: %s"), reader.line);
Jeff Kingd0229ab2019-06-20 03:41:14 -0400363 if (!lookup_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800364 die(_("object not found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700365 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -0700366 if (!parse_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800367 die(_("error in object: %s"), reader.line);
brian m. carlsone92b8482017-05-06 22:10:06 +0000368 if (unregister_shallow(&oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800369 die(_("no shallow found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700370 continue;
371 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800372 die(_("expected shallow/unshallow, got %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700373 }
374 } else if (!args->stateless_rpc)
375 send_request(args, fd[1], &req_buf);
376
377 if (!args->stateless_rpc) {
378 /* If we aren't using the stateless-rpc interface
379 * we don't need to retain the headers.
380 */
381 strbuf_setlen(&req_buf, 0);
382 state_len = 0;
383 }
384
385 flushes = 0;
386 retval = -1;
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000387 if (args->no_dependents)
388 goto done;
Jonathan Tanec062832018-06-14 15:54:28 -0700389 while ((oid = negotiator->next(negotiator))) {
brian m. carlson1b283372017-05-01 02:28:54 +0000390 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
391 print_verbose(args, "have %s", oid_to_hex(oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700392 in_vain++;
393 if (flush_at <= ++count) {
394 int ack;
395
396 packet_buf_flush(&req_buf);
397 send_request(args, fd[1], &req_buf);
398 strbuf_setlen(&req_buf, state_len);
399 flushes++;
Brandon Williams685fbd32018-03-15 10:31:28 -0700400 flush_at = next_flush(args->stateless_rpc, count);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700401
402 /*
403 * We keep one window "ahead" of the other side, and
404 * will wait for an ACK only on the next one
405 */
406 if (!args->stateless_rpc && count == INITIAL_FLUSH)
407 continue;
408
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800409 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700410 do {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800411 ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700412 if (ack)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700413 print_verbose(args, _("got %s %d %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000414 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700415 switch (ack) {
416 case ACK:
417 flushes = 0;
418 multi_ack = 0;
419 retval = 0;
420 goto done;
421 case ACK_common:
422 case ACK_ready:
423 case ACK_continue: {
424 struct commit *commit =
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700425 lookup_commit(the_repository,
426 result_oid);
Jonathan Tand093bc72018-06-14 15:54:27 -0700427 int was_common;
Junio C Hamano3a2a1dc2018-08-02 15:30:42 -0700428
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700429 if (!commit)
brian m. carlson1b283372017-05-01 02:28:54 +0000430 die(_("invalid commit %s"), oid_to_hex(result_oid));
Jonathan Tanec062832018-06-14 15:54:28 -0700431 was_common = negotiator->ack(negotiator, commit);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700432 if (args->stateless_rpc
433 && ack == ACK_common
Jonathan Tand093bc72018-06-14 15:54:27 -0700434 && !was_common) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700435 /* We need to replay the have for this object
436 * on the next RPC request so the peer knows
437 * it is in common with us.
438 */
brian m. carlson1b283372017-05-01 02:28:54 +0000439 const char *hex = oid_to_hex(result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700440 packet_buf_write(&req_buf, "have %s\n", hex);
441 state_len = req_buf.len;
Jonathan Tan06b3d382016-09-23 10:41:35 -0700442 /*
443 * Reset in_vain because an ack
444 * for this commit has not been
445 * seen.
446 */
447 in_vain = 0;
448 } else if (!args->stateless_rpc
449 || ack != ACK_common)
450 in_vain = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700451 retval = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700452 got_continue = 1;
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700453 if (ack == ACK_ready)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700454 got_ready = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700455 break;
456 }
457 }
458 } while (ack);
459 flushes--;
460 if (got_continue && MAX_IN_VAIN < in_vain) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700461 print_verbose(args, _("giving up"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700462 break; /* give up */
463 }
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700464 if (got_ready)
465 break;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700466 }
467 }
468done:
469 if (!got_ready || !no_done) {
470 packet_buf_write(&req_buf, "done\n");
471 send_request(args, fd[1], &req_buf);
472 }
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700473 print_verbose(args, _("done"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700474 if (retval != 0) {
475 multi_ack = 0;
476 flushes++;
477 }
478 strbuf_release(&req_buf);
479
Nguyễn Thái Ngọc Duyff62eca2014-02-06 22:10:39 +0700480 if (!got_ready || !no_done)
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800481 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700482 while (flushes || multi_ack) {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800483 int ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700484 if (ack) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700485 print_verbose(args, _("got %s (%d) %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000486 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700487 if (ack == ACK)
488 return 0;
489 multi_ack = 1;
490 continue;
491 }
492 flushes--;
493 }
494 /* it is no error to fetch into a completely empty repo */
495 return count ? retval : 0;
496}
497
498static struct commit_list *complete;
499
brian m. carlson1b283372017-05-01 02:28:54 +0000500static int mark_complete(const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700501{
Stefan Beller109cd762018-06-28 18:21:51 -0700502 struct object *o = parse_object(the_repository, oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700503
504 while (o && o->type == OBJ_TAG) {
505 struct tag *t = (struct tag *) o;
506 if (!t->tagged)
507 break; /* broken repository */
508 o->flags |= COMPLETE;
Stefan Beller109cd762018-06-28 18:21:51 -0700509 o = parse_object(the_repository, &t->tagged->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700510 }
511 if (o && o->type == OBJ_COMMIT) {
512 struct commit *commit = (struct commit *)o;
513 if (!(commit->object.flags & COMPLETE)) {
514 commit->object.flags |= COMPLETE;
Jeff King16445242013-07-02 02:16:23 -0400515 commit_list_insert(commit, &complete);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700516 }
517 }
518 return 0;
519}
520
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000521static int mark_complete_oid(const char *refname, const struct object_id *oid,
522 int flag, void *cb_data)
523{
brian m. carlson1b283372017-05-01 02:28:54 +0000524 return mark_complete(oid);
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000525}
526
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700527static void mark_recent_complete_commits(struct fetch_pack_args *args,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200528 timestamp_t cutoff)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700529{
530 while (complete && cutoff <= complete->item->date) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700531 print_verbose(args, _("Marking %s as complete"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700532 oid_to_hex(&complete->item->object.oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700533 pop_most_recent_commit(&complete, COMPLETE);
534 }
535}
536
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700537static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
538{
539 for (; refs; refs = refs->next)
540 oidset_insert(oids, &refs->old_oid);
541}
542
René Scharfebf732822018-10-04 17:09:06 +0200543static int is_unmatched_ref(const struct ref *ref)
544{
545 struct object_id oid;
546 const char *p;
547 return ref->match_status == REF_NOT_MATCHED &&
548 !parse_oid_hex(ref->name, &oid, &p) &&
549 *p == '\0' &&
550 oideq(&oid, &ref->old_oid);
551}
552
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700553static void filter_refs(struct fetch_pack_args *args,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800554 struct ref **refs,
555 struct ref **sought, int nr_sought)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700556{
557 struct ref *newlist = NULL;
558 struct ref **newtail = &newlist;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700559 struct ref *unmatched = NULL;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700560 struct ref *ref, *next;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700561 struct oidset tip_oids = OIDSET_INIT;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800562 int i;
René Scharfe22a16462018-10-04 17:09:39 +0200563 int strict = !(allow_unadvertised_object_request &
564 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700565
Junio C Hamanof2db8542013-01-29 14:02:15 -0800566 i = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700567 for (ref = *refs; ref; ref = next) {
568 int keep = 0;
569 next = ref->next;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800570
René Scharfe50e19a82014-06-06 19:24:48 +0200571 if (starts_with(ref->name, "refs/") &&
Jeff King34066f02019-04-13 01:57:37 -0400572 check_refname_format(ref->name, 0)) {
573 /*
574 * trash or a peeled value; do not even add it to
575 * unmatched list
576 */
577 free_one_ref(ref);
578 continue;
579 } else {
Junio C Hamanof2db8542013-01-29 14:02:15 -0800580 while (i < nr_sought) {
581 int cmp = strcmp(ref->name, sought[i]->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700582 if (cmp < 0)
583 break; /* definitely do not have it */
584 else if (cmp == 0) {
585 keep = 1; /* definitely have it */
Matt McCutchend56583d2017-02-22 11:05:57 -0500586 sought[i]->match_status = REF_MATCHED;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700587 }
Junio C Hamanof2db8542013-01-29 14:02:15 -0800588 i++;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700589 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700590
Jeff Kinge9502c02018-06-11 01:53:57 -0400591 if (!keep && args->fetch_all &&
592 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
593 keep = 1;
594 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700595
596 if (keep) {
597 *newtail = ref;
598 ref->next = NULL;
599 newtail = &ref->next;
600 } else {
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700601 ref->next = unmatched;
602 unmatched = ref;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700603 }
604 }
605
René Scharfe22a16462018-10-04 17:09:39 +0200606 if (strict) {
607 for (i = 0; i < nr_sought; i++) {
608 ref = sought[i];
609 if (!is_unmatched_ref(ref))
610 continue;
611
612 add_refs_to_oidset(&tip_oids, unmatched);
613 add_refs_to_oidset(&tip_oids, newlist);
614 break;
615 }
616 }
617
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800618 /* Append unmatched requests to the list */
Matt McCutchend56583d2017-02-22 11:05:57 -0500619 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500620 ref = sought[i];
René Scharfebf732822018-10-04 17:09:06 +0200621 if (!is_unmatched_ref(ref))
Matt McCutchend56583d2017-02-22 11:05:57 -0500622 continue;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800623
René Scharfe22a16462018-10-04 17:09:39 +0200624 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500625 ref->match_status = REF_MATCHED;
Jeff Kingc3c17bf2015-03-19 16:37:09 -0400626 *newtail = copy_ref(ref);
627 newtail = &(*newtail)->next;
Matt McCutchend56583d2017-02-22 11:05:57 -0500628 } else {
629 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800630 }
631 }
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700632
633 oidset_clear(&tip_oids);
Jeff King259eddd2019-04-13 01:54:09 -0400634 free_refs(unmatched);
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700635
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700636 *refs = newlist;
637}
638
Jonathan Tanec062832018-06-14 15:54:28 -0700639static void mark_alternate_complete(struct fetch_negotiator *unused,
Jonathan Tand30fe892018-06-14 15:54:26 -0700640 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700641{
brian m. carlson1b283372017-05-01 02:28:54 +0000642 mark_complete(&obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700643}
644
Takuto Ikuta024aa462018-03-14 15:32:42 +0900645struct loose_object_iter {
646 struct oidset *loose_object_set;
647 struct ref *refs;
648};
649
650/*
Jonathan Tan34c29032018-06-06 13:47:07 -0700651 * Mark recent commits available locally and reachable from a local ref as
652 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
653 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
654 * thus do not need COMMON_REF marks).
655 *
656 * The cutoff time for recency is determined by this heuristic: it is the
657 * earliest commit time of the objects in refs that are commits and that we know
658 * the commit time of.
659 */
Jonathan Tanec062832018-06-14 15:54:28 -0700660static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700661 struct fetch_pack_args *args,
Jonathan Tan34c29032018-06-06 13:47:07 -0700662 struct ref **refs)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700663{
664 struct ref *ref;
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000665 int old_save_commit_buffer = save_commit_buffer;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200666 timestamp_t cutoff = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700667
668 save_commit_buffer = 0;
669
670 for (ref = *refs; ref; ref = ref->next) {
671 struct object *o;
672
Jeff King97b2fa02018-11-12 09:55:58 -0500673 if (!has_object_file_with_flags(&ref->old_oid,
674 OBJECT_INFO_QUICK))
Junio C Hamano012a1bb2013-01-26 19:42:09 -0800675 continue;
Stefan Beller109cd762018-06-28 18:21:51 -0700676 o = parse_object(the_repository, &ref->old_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700677 if (!o)
678 continue;
679
680 /* We already have it -- which may mean that we were
681 * in sync with the other side at some time after
682 * that (it is OK if we guess wrong here).
683 */
684 if (o->type == OBJ_COMMIT) {
685 struct commit *commit = (struct commit *)o;
686 if (!cutoff || cutoff < commit->date)
687 cutoff = commit->date;
688 }
689 }
690
Jonathan Tan12f19a92018-10-03 16:04:52 -0700691 if (!args->deepen) {
692 for_each_ref(mark_complete_oid, NULL);
693 for_each_cached_alternate(NULL, mark_alternate_complete);
694 commit_list_sort_by_date(&complete);
695 if (cutoff)
696 mark_recent_complete_commits(args, cutoff);
697 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700698
Jonathan Tan12f19a92018-10-03 16:04:52 -0700699 /*
700 * Mark all complete remote refs as common refs.
701 * Don't mark them common yet; the server has to be told so first.
702 */
703 for (ref = *refs; ref; ref = ref->next) {
704 struct object *o = deref_tag(the_repository,
705 lookup_object(the_repository,
Jeff Kingd0229ab2019-06-20 03:41:14 -0400706 &ref->old_oid),
Jonathan Tan12f19a92018-10-03 16:04:52 -0700707 NULL, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700708
Jonathan Tan12f19a92018-10-03 16:04:52 -0700709 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
710 continue;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700711
Jonathan Tan12f19a92018-10-03 16:04:52 -0700712 negotiator->known_common(negotiator,
713 (struct commit *)o);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700714 }
715
Jonathan Tan34c29032018-06-06 13:47:07 -0700716 save_commit_buffer = old_save_commit_buffer;
717}
718
719/*
720 * Returns 1 if every object pointed to by the given remote refs is available
721 * locally and reachable from a local ref, and 0 otherwise.
722 */
723static int everything_local(struct fetch_pack_args *args,
724 struct ref **refs)
725{
726 struct ref *ref;
727 int retval;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700728
729 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000730 const struct object_id *remote = &ref->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700731 struct object *o;
732
Jeff Kingd0229ab2019-06-20 03:41:14 -0400733 o = lookup_object(the_repository, remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700734 if (!o || !(o->flags & COMPLETE)) {
735 retval = 0;
brian m. carlson1b283372017-05-01 02:28:54 +0000736 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700737 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700738 continue;
739 }
brian m. carlson1b283372017-05-01 02:28:54 +0000740 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700741 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700742 }
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000743
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700744 return retval;
745}
746
747static int sideband_demux(int in, int out, void *data)
748{
749 int *xd = data;
Jeff King9ff18fa2016-02-24 02:44:58 -0500750 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700751
Jeff King9ff18fa2016-02-24 02:44:58 -0500752 ret = recv_sideband("fetch-pack", xd[0], out);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700753 close(out);
754 return ret;
755}
756
757static int get_pack(struct fetch_pack_args *args,
758 int xd[2], char **pack_lockfile)
759{
760 struct async demux;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700761 int do_keep = args->keep_pack;
Jeff King984a43b2015-09-24 17:07:54 -0400762 const char *cmd_name;
763 struct pack_header header;
764 int pass_header = 0;
René Scharfed3180272014-08-19 21:09:35 +0200765 struct child_process cmd = CHILD_PROCESS_INIT;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700766 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700767
768 memset(&demux, 0, sizeof(demux));
769 if (use_sideband) {
770 /* xd[] is talking with upload-pack; subprocess reads from
771 * xd[0], spits out band#2 to stderr, and feeds us band#1
772 * through demux->out.
773 */
774 demux.proc = sideband_demux;
775 demux.data = xd;
776 demux.out = -1;
Jeff Kingdf857572016-04-19 18:50:29 -0400777 demux.isolate_sigpipe = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700778 if (start_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700779 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700780 }
781 else
782 demux.out = xd[0];
783
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700784 if (!args->keep_pack && unpack_limit) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700785
786 if (read_pack_header(demux.out, &header))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700787 die(_("protocol error: bad pack header"));
Jeff King984a43b2015-09-24 17:07:54 -0400788 pass_header = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700789 if (ntohl(header.hdr_entries) < unpack_limit)
790 do_keep = 0;
791 else
792 do_keep = 1;
793 }
794
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700795 if (alternate_shallow_file) {
Jeff King984a43b2015-09-24 17:07:54 -0400796 argv_array_push(&cmd.args, "--shallow-file");
797 argv_array_push(&cmd.args, alternate_shallow_file);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700798 }
799
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000800 if (do_keep || args->from_promisor) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700801 if (pack_lockfile)
802 cmd.out = -1;
Jeff King984a43b2015-09-24 17:07:54 -0400803 cmd_name = "index-pack";
804 argv_array_push(&cmd.args, cmd_name);
805 argv_array_push(&cmd.args, "--stdin");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700806 if (!args->quiet && !args->no_progress)
Jeff King984a43b2015-09-24 17:07:54 -0400807 argv_array_push(&cmd.args, "-v");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700808 if (args->use_thin_pack)
Jeff King984a43b2015-09-24 17:07:54 -0400809 argv_array_push(&cmd.args, "--fix-thin");
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000810 if (do_keep && (args->lock_pack || unpack_limit)) {
René Scharfeda25bdb2017-04-18 17:57:42 -0400811 char hostname[HOST_NAME_MAX + 1];
David Turner5781a9a2017-04-18 17:57:43 -0400812 if (xgethostname(hostname, sizeof(hostname)))
Jeff King984a43b2015-09-24 17:07:54 -0400813 xsnprintf(hostname, sizeof(hostname), "localhost");
814 argv_array_pushf(&cmd.args,
815 "--keep=fetch-pack %"PRIuMAX " on %s",
816 (uintmax_t)getpid(), hostname);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700817 }
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700818 if (args->check_self_contained_and_connected)
Jeff King984a43b2015-09-24 17:07:54 -0400819 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000820 if (args->from_promisor)
821 argv_array_push(&cmd.args, "--promisor");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700822 }
823 else {
Jeff King984a43b2015-09-24 17:07:54 -0400824 cmd_name = "unpack-objects";
825 argv_array_push(&cmd.args, cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700826 if (args->quiet || args->no_progress)
Jeff King984a43b2015-09-24 17:07:54 -0400827 argv_array_push(&cmd.args, "-q");
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700828 args->check_self_contained_and_connected = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700829 }
Jeff King984a43b2015-09-24 17:07:54 -0400830
831 if (pass_header)
832 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
833 ntohl(header.hdr_version),
834 ntohl(header.hdr_entries));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700835 if (fetch_fsck_objects >= 0
836 ? fetch_fsck_objects
837 : transfer_fsck_objects >= 0
838 ? transfer_fsck_objects
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700839 : 0) {
840 if (args->from_promisor)
841 /*
842 * We cannot use --strict in index-pack because it
843 * checks both broken objects and links, but we only
844 * want to check for broken objects.
845 */
846 argv_array_push(&cmd.args, "--fsck-objects");
847 else
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +0000848 argv_array_pushf(&cmd.args, "--strict%s",
849 fsck_msg_types.buf);
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700850 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700851
852 cmd.in = demux.out;
853 cmd.git_cmd = 1;
854 if (start_command(&cmd))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700855 die(_("fetch-pack: unable to fork off %s"), cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700856 if (do_keep && pack_lockfile) {
857 *pack_lockfile = index_pack_lockfile(cmd.out);
858 close(cmd.out);
859 }
860
Jens Lindstrom37cb1dd2013-10-22 15:36:02 +0200861 if (!use_sideband)
862 /* Closed by start_command() */
863 xd[0] = -1;
864
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700865 ret = finish_command(&cmd);
866 if (!ret || (args->check_self_contained_and_connected && ret == 1))
867 args->self_contained_and_connected =
868 args->check_self_contained_and_connected &&
869 ret == 0;
870 else
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700871 die(_("%s failed"), cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700872 if (use_sideband && finish_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700873 die(_("error in sideband demultiplexer"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700874 return 0;
875}
876
Junio C Hamanof2db8542013-01-29 14:02:15 -0800877static int cmp_ref_by_name(const void *a_, const void *b_)
878{
879 const struct ref *a = *((const struct ref **)a_);
880 const struct ref *b = *((const struct ref **)b_);
881 return strcmp(a->name, b->name);
882}
883
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700884static struct ref *do_fetch_pack(struct fetch_pack_args *args,
885 int fd[2],
886 const struct ref *orig_ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800887 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +0700888 struct shallow_info *si,
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700889 char **pack_lockfile)
890{
Derrick Stoleeaaf633c2019-08-13 11:37:48 -0700891 struct repository *r = the_repository;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700892 struct ref *ref = copy_ref_list(orig_ref);
brian m. carlson1b283372017-05-01 02:28:54 +0000893 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700894 const char *agent_feature;
895 int agent_len;
Jonathan Tanec062832018-06-14 15:54:28 -0700896 struct fetch_negotiator negotiator;
Derrick Stoleeaaf633c2019-08-13 11:37:48 -0700897 fetch_negotiator_init(r, &negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700898
899 sort_ref_list(&ref, ref_compare_name);
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200900 QSORT(sought, nr_sought, cmp_ref_by_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700901
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700902 if ((agent_feature = server_feature_value("agent", &agent_len))) {
903 agent_supported = 1;
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700904 if (agent_len)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700905 print_verbose(args, _("Server version is %.*s"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700906 agent_len, agent_feature);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700907 }
Nguyễn Thái Ngọc Duy0e042972019-06-20 18:59:51 +0700908
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700909 if (server_supports("shallow"))
910 print_verbose(args, _("Server supports %s"), "shallow");
Derrick Stoleeaaf633c2019-08-13 11:37:48 -0700911 else if (args->depth > 0 || is_repository_shallow(r))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700912 die(_("Server does not support shallow clients"));
913 if (args->depth > 0 || args->deepen_since || args->deepen_not)
914 args->deepen = 1;
915 if (server_supports("multi_ack_detailed")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700916 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700917 multi_ack = 2;
918 if (server_supports("no-done")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700919 print_verbose(args, _("Server supports %s"), "no-done");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700920 if (args->stateless_rpc)
921 no_done = 1;
922 }
923 }
924 else if (server_supports("multi_ack")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700925 print_verbose(args, _("Server supports %s"), "multi_ack");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700926 multi_ack = 1;
927 }
928 if (server_supports("side-band-64k")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700929 print_verbose(args, _("Server supports %s"), "side-band-64k");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700930 use_sideband = 2;
931 }
932 else if (server_supports("side-band")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700933 print_verbose(args, _("Server supports %s"), "side-band");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700934 use_sideband = 1;
935 }
936 if (server_supports("allow-tip-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700937 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700938 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
939 }
940 if (server_supports("allow-reachable-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700941 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700942 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
943 }
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700944 if (server_supports("thin-pack"))
945 print_verbose(args, _("Server supports %s"), "thin-pack");
946 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700947 args->use_thin_pack = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700948 if (server_supports("no-progress"))
949 print_verbose(args, _("Server supports %s"), "no-progress");
950 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700951 args->no_progress = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700952 if (server_supports("include-tag"))
953 print_verbose(args, _("Server supports %s"), "include-tag");
954 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700955 args->include_tag = 0;
956 if (server_supports("ofs-delta"))
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700957 print_verbose(args, _("Server supports %s"), "ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700958 else
959 prefer_ofs_delta = 0;
960
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700961 if (server_supports("filter")) {
962 server_supports_filtering = 1;
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700963 print_verbose(args, _("Server supports %s"), "filter");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700964 } else if (args->filter_options.choice) {
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700965 warning("filtering not recognized by server, ignoring");
966 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700967
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700968 if (server_supports("deepen-since")) {
969 print_verbose(args, _("Server supports %s"), "deepen-since");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700970 deepen_since_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700971 } else if (args->deepen_since)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700972 die(_("Server does not support --shallow-since"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700973 if (server_supports("deepen-not")) {
974 print_verbose(args, _("Server supports %s"), "deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700975 deepen_not_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700976 } else if (args->deepen_not)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700977 die(_("Server does not support --shallow-exclude"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700978 if (server_supports("deepen-relative"))
979 print_verbose(args, _("Server supports %s"), "deepen-relative");
980 else if (args->deepen_relative)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700981 die(_("Server does not support --deepen"));
982
Jonathan Tan12f19a92018-10-03 16:04:52 -0700983 if (!args->no_dependents) {
984 mark_complete_and_common_ref(&negotiator, args, &ref);
985 filter_refs(args, &ref, sought, nr_sought);
986 if (everything_local(args, &ref)) {
987 packet_flush(fd[1]);
988 goto all_done;
989 }
990 } else {
991 filter_refs(args, &ref, sought, nr_sought);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700992 }
Jonathan Tanec062832018-06-14 15:54:28 -0700993 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700994 if (!args->keep_pack)
995 /* When cloning, it is not unusual to have
996 * no common commit.
997 */
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700998 warning(_("no common commits"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700999
1000 if (args->stateless_rpc)
1001 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001002 if (args->deepen)
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +07001003 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1004 NULL);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001005 else if (si->nr_ours || si->nr_theirs)
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001006 alternate_shallow_file = setup_temporary_shallow(si->shallow);
Nguyễn Thái Ngọc Duy6da8bdc2013-08-26 09:17:26 +07001007 else
1008 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001009 if (get_pack(args, fd, pack_lockfile))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001010 die(_("git fetch-pack: fetch failed."));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001011
1012 all_done:
Jonathan Tanec062832018-06-14 15:54:28 -07001013 negotiator.release(&negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001014 return ref;
1015}
1016
Brandon Williamsf7e20502018-03-15 10:31:29 -07001017static void add_shallow_requests(struct strbuf *req_buf,
1018 const struct fetch_pack_args *args)
1019{
Junio C Hamano00624d62018-07-18 12:20:27 -07001020 if (is_repository_shallow(the_repository))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001021 write_shallow_commits(req_buf, 1, NULL);
1022 if (args->depth > 0)
1023 packet_buf_write(req_buf, "deepen %d", args->depth);
1024 if (args->deepen_since) {
1025 timestamp_t max_age = approxidate(args->deepen_since);
1026 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1027 }
1028 if (args->deepen_not) {
1029 int i;
1030 for (i = 0; i < args->deepen_not->nr; i++) {
1031 struct string_list_item *s = args->deepen_not->items + i;
1032 packet_buf_write(req_buf, "deepen-not %s", s->string);
1033 }
1034 }
Jonathan Tan5056cf42018-12-18 13:24:35 -08001035 if (args->deepen_relative)
1036 packet_buf_write(req_buf, "deepen-relative\n");
Brandon Williamsf7e20502018-03-15 10:31:29 -07001037}
1038
Jonathan Tan12f19a92018-10-03 16:04:52 -07001039static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
Brandon Williams685fbd32018-03-15 10:31:28 -07001040{
Brandon Williams73302052018-06-27 15:30:23 -07001041 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1042
Brandon Williams685fbd32018-03-15 10:31:28 -07001043 for ( ; wants ; wants = wants->next) {
1044 const struct object_id *remote = &wants->old_oid;
Brandon Williams685fbd32018-03-15 10:31:28 -07001045 struct object *o;
1046
1047 /*
1048 * If that object is complete (i.e. it is an ancestor of a
1049 * local ref), we tell them we have it but do not have to
1050 * tell them about its ancestors, which they already know
1051 * about.
1052 *
1053 * We use lookup_object here because we are only
1054 * interested in the case we *know* the object is
1055 * reachable and we have already scanned it.
Jonathan Tan12f19a92018-10-03 16:04:52 -07001056 *
1057 * Do this only if args->no_dependents is false (if it is true,
1058 * we cannot trust the object flags).
Brandon Williams685fbd32018-03-15 10:31:28 -07001059 */
Jonathan Tan12f19a92018-10-03 16:04:52 -07001060 if (!no_dependents &&
Jeff Kingd0229ab2019-06-20 03:41:14 -04001061 ((o = lookup_object(the_repository, remote)) != NULL) &&
Brandon Williams685fbd32018-03-15 10:31:28 -07001062 (o->flags & COMPLETE)) {
1063 continue;
1064 }
1065
Brandon Williams73302052018-06-27 15:30:23 -07001066 if (!use_ref_in_want || wants->exact_oid)
1067 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1068 else
1069 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
Brandon Williams685fbd32018-03-15 10:31:28 -07001070 }
1071}
1072
1073static void add_common(struct strbuf *req_buf, struct oidset *common)
1074{
1075 struct oidset_iter iter;
1076 const struct object_id *oid;
1077 oidset_iter_init(common, &iter);
1078
1079 while ((oid = oidset_iter_next(&iter))) {
1080 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1081 }
1082}
1083
Jonathan Tanec062832018-06-14 15:54:28 -07001084static int add_haves(struct fetch_negotiator *negotiator,
1085 struct strbuf *req_buf,
Jonathan Tand30fe892018-06-14 15:54:26 -07001086 int *haves_to_send, int *in_vain)
Brandon Williams685fbd32018-03-15 10:31:28 -07001087{
1088 int ret = 0;
1089 int haves_added = 0;
1090 const struct object_id *oid;
1091
Jonathan Tanec062832018-06-14 15:54:28 -07001092 while ((oid = negotiator->next(negotiator))) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001093 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1094 if (++haves_added >= *haves_to_send)
1095 break;
1096 }
1097
1098 *in_vain += haves_added;
1099 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1100 /* Send Done */
1101 packet_buf_write(req_buf, "done\n");
1102 ret = 1;
1103 }
1104
1105 /* Increase haves to send on next round */
1106 *haves_to_send = next_flush(1, *haves_to_send);
1107
1108 return ret;
1109}
1110
Jonathan Tanec062832018-06-14 15:54:28 -07001111static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001112 struct fetch_pack_args *args,
Brandon Williams685fbd32018-03-15 10:31:28 -07001113 const struct ref *wants, struct oidset *common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001114 int *haves_to_send, int *in_vain,
1115 int sideband_all)
Brandon Williams685fbd32018-03-15 10:31:28 -07001116{
1117 int ret = 0;
1118 struct strbuf req_buf = STRBUF_INIT;
1119
1120 if (server_supports_v2("fetch", 1))
1121 packet_buf_write(&req_buf, "command=fetch");
1122 if (server_supports_v2("agent", 0))
1123 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
Brandon Williams5e3548e2018-04-23 15:46:24 -07001124 if (args->server_options && args->server_options->nr &&
1125 server_supports_v2("server-option", 1)) {
1126 int i;
1127 for (i = 0; i < args->server_options->nr; i++)
Jonathan Tan5b204b72019-05-22 13:08:22 -07001128 packet_buf_write(&req_buf, "server-option=%s",
Brandon Williams5e3548e2018-04-23 15:46:24 -07001129 args->server_options->items[i].string);
1130 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001131
1132 packet_buf_delim(&req_buf);
1133 if (args->use_thin_pack)
1134 packet_buf_write(&req_buf, "thin-pack");
1135 if (args->no_progress)
1136 packet_buf_write(&req_buf, "no-progress");
1137 if (args->include_tag)
1138 packet_buf_write(&req_buf, "include-tag");
1139 if (prefer_ofs_delta)
1140 packet_buf_write(&req_buf, "ofs-delta");
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001141 if (sideband_all)
1142 packet_buf_write(&req_buf, "sideband-all");
Brandon Williams685fbd32018-03-15 10:31:28 -07001143
Brandon Williamsf7e20502018-03-15 10:31:29 -07001144 /* Add shallow-info and deepen request */
1145 if (server_supports_feature("fetch", "shallow", 0))
1146 add_shallow_requests(&req_buf, args);
Junio C Hamano00624d62018-07-18 12:20:27 -07001147 else if (is_repository_shallow(the_repository) || args->deepen)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001148 die(_("Server does not support shallow requests"));
1149
Jonathan Tanba957102018-05-03 16:46:56 -07001150 /* Add filter */
1151 if (server_supports_feature("fetch", "filter", 0) &&
1152 args->filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001153 const char *spec =
1154 expand_list_objects_filter_spec(&args->filter_options);
Jonathan Tanba957102018-05-03 16:46:56 -07001155 print_verbose(args, _("Server supports filter"));
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001156 packet_buf_write(&req_buf, "filter %s", spec);
Jonathan Tanba957102018-05-03 16:46:56 -07001157 } else if (args->filter_options.choice) {
1158 warning("filtering not recognized by server, ignoring");
1159 }
1160
Brandon Williams685fbd32018-03-15 10:31:28 -07001161 /* add wants */
Jonathan Tan12f19a92018-10-03 16:04:52 -07001162 add_wants(args->no_dependents, wants, &req_buf);
Brandon Williams685fbd32018-03-15 10:31:28 -07001163
Jonathan Tanba957102018-05-03 16:46:56 -07001164 if (args->no_dependents) {
1165 packet_buf_write(&req_buf, "done");
1166 ret = 1;
1167 } else {
1168 /* Add all of the common commits we've found in previous rounds */
1169 add_common(&req_buf, common);
Brandon Williams685fbd32018-03-15 10:31:28 -07001170
Jonathan Tanba957102018-05-03 16:46:56 -07001171 /* Add initial haves */
Jonathan Tanec062832018-06-14 15:54:28 -07001172 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
Jonathan Tanba957102018-05-03 16:46:56 -07001173 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001174
1175 /* Send request */
1176 packet_buf_flush(&req_buf);
Jeff King37c80012019-03-04 23:11:39 -05001177 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1178 die_errno(_("unable to write request to remote"));
Brandon Williams685fbd32018-03-15 10:31:28 -07001179
1180 strbuf_release(&req_buf);
1181 return ret;
1182}
1183
1184/*
1185 * Processes a section header in a server's response and checks if it matches
1186 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1187 * not consumed); if 0, the line will be consumed and the function will die if
1188 * the section header doesn't match what was expected.
1189 */
1190static int process_section_header(struct packet_reader *reader,
1191 const char *section, int peek)
1192{
1193 int ret;
1194
1195 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001196 die(_("error reading section header '%s'"), section);
Brandon Williams685fbd32018-03-15 10:31:28 -07001197
1198 ret = !strcmp(reader->line, section);
1199
1200 if (!peek) {
1201 if (!ret)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001202 die(_("expected '%s', received '%s'"),
Brandon Williams685fbd32018-03-15 10:31:28 -07001203 section, reader->line);
1204 packet_reader_read(reader);
1205 }
1206
1207 return ret;
1208}
1209
Jonathan Tanec062832018-06-14 15:54:28 -07001210static int process_acks(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -07001211 struct packet_reader *reader,
1212 struct oidset *common)
Brandon Williams685fbd32018-03-15 10:31:28 -07001213{
1214 /* received */
1215 int received_ready = 0;
1216 int received_ack = 0;
1217
1218 process_section_header(reader, "acknowledgments", 0);
1219 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1220 const char *arg;
1221
1222 if (!strcmp(reader->line, "NAK"))
1223 continue;
1224
1225 if (skip_prefix(reader->line, "ACK ", &arg)) {
1226 struct object_id oid;
1227 if (!get_oid_hex(arg, &oid)) {
1228 struct commit *commit;
1229 oidset_insert(common, &oid);
Stefan Bellerc1f5eb42018-06-28 18:21:59 -07001230 commit = lookup_commit(the_repository, &oid);
Jonathan Tanec062832018-06-14 15:54:28 -07001231 negotiator->ack(negotiator, commit);
Brandon Williams685fbd32018-03-15 10:31:28 -07001232 }
1233 continue;
1234 }
1235
1236 if (!strcmp(reader->line, "ready")) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001237 received_ready = 1;
1238 continue;
1239 }
1240
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001241 die(_("unexpected acknowledgment line: '%s'"), reader->line);
Brandon Williams685fbd32018-03-15 10:31:28 -07001242 }
1243
1244 if (reader->status != PACKET_READ_FLUSH &&
1245 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001246 die(_("error processing acks: %d"), reader->status);
Brandon Williams685fbd32018-03-15 10:31:28 -07001247
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001248 /*
1249 * If an "acknowledgments" section is sent, a packfile is sent if and
1250 * only if "ready" was sent in this section. The other sections
1251 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1252 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1253 * otherwise.
1254 */
1255 if (received_ready && reader->status != PACKET_READ_DELIM)
1256 die(_("expected packfile to be sent after 'ready'"));
1257 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1258 die(_("expected no other sections to be sent after no 'ready'"));
1259
Brandon Williams685fbd32018-03-15 10:31:28 -07001260 /* return 0 if no common, 1 if there are common, or 2 if ready */
1261 return received_ready ? 2 : (received_ack ? 1 : 0);
1262}
1263
Brandon Williamsf7e20502018-03-15 10:31:29 -07001264static void receive_shallow_info(struct fetch_pack_args *args,
Jonathan Tan13390782019-03-26 12:31:21 -07001265 struct packet_reader *reader,
1266 struct oid_array *shallows,
1267 struct shallow_info *si)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001268{
Jonathan Tan13390782019-03-26 12:31:21 -07001269 int unshallow_received = 0;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001270
Brandon Williamsf7e20502018-03-15 10:31:29 -07001271 process_section_header(reader, "shallow-info", 0);
1272 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1273 const char *arg;
1274 struct object_id oid;
1275
1276 if (skip_prefix(reader->line, "shallow ", &arg)) {
1277 if (get_oid_hex(arg, &oid))
1278 die(_("invalid shallow line: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001279 oid_array_append(shallows, &oid);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001280 continue;
1281 }
1282 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1283 if (get_oid_hex(arg, &oid))
1284 die(_("invalid unshallow line: %s"), reader->line);
Jeff Kingd0229ab2019-06-20 03:41:14 -04001285 if (!lookup_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001286 die(_("object not found: %s"), reader->line);
1287 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -07001288 if (!parse_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001289 die(_("error in object: %s"), reader->line);
1290 if (unregister_shallow(&oid))
1291 die(_("no shallow found: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001292 unshallow_received = 1;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001293 continue;
1294 }
1295 die(_("expected shallow/unshallow, got %s"), reader->line);
1296 }
1297
1298 if (reader->status != PACKET_READ_FLUSH &&
1299 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001300 die(_("error processing shallow info: %d"), reader->status);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001301
Jonathan Tan13390782019-03-26 12:31:21 -07001302 if (args->deepen || unshallow_received) {
1303 /*
1304 * Treat these as shallow lines caused by our depth settings.
1305 * In v0, these lines cannot cause refs to be rejected; do the
1306 * same.
1307 */
1308 int i;
1309
1310 for (i = 0; i < shallows->nr; i++)
1311 register_shallow(the_repository, &shallows->oid[i]);
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001312 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1313 NULL);
1314 args->deepen = 1;
Jonathan Tan13390782019-03-26 12:31:21 -07001315 } else if (shallows->nr) {
1316 /*
1317 * Treat these as shallow lines caused by the remote being
1318 * shallow. In v0, remote refs that reach these objects are
1319 * rejected (unless --update-shallow is set); do the same.
1320 */
1321 prepare_shallow_info(si, shallows);
1322 if (si->nr_ours || si->nr_theirs)
1323 alternate_shallow_file =
1324 setup_temporary_shallow(si->shallow);
1325 else
1326 alternate_shallow_file = NULL;
brian m. carlson380ebab2019-02-06 23:59:37 +00001327 } else {
1328 alternate_shallow_file = NULL;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001329 }
Brandon Williamsf7e20502018-03-15 10:31:29 -07001330}
1331
Jonathan Tanb7643002019-03-27 14:11:10 -07001332static int cmp_name_ref(const void *name, const void *ref)
1333{
1334 return strcmp(name, (*(struct ref **)ref)->name);
1335}
1336
Jonathan Tane2842b32018-08-01 13:13:20 -07001337static void receive_wanted_refs(struct packet_reader *reader,
1338 struct ref **sought, int nr_sought)
Brandon Williams73302052018-06-27 15:30:23 -07001339{
1340 process_section_header(reader, "wanted-refs", 0);
1341 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1342 struct object_id oid;
1343 const char *end;
Jonathan Tanb7643002019-03-27 14:11:10 -07001344 struct ref **found;
Brandon Williams73302052018-06-27 15:30:23 -07001345
1346 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001347 die(_("expected wanted-ref, got '%s'"), reader->line);
Brandon Williams73302052018-06-27 15:30:23 -07001348
Jonathan Tanb7643002019-03-27 14:11:10 -07001349 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1350 cmp_name_ref);
1351 if (!found)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001352 die(_("unexpected wanted-ref: '%s'"), reader->line);
Jonathan Tanb7643002019-03-27 14:11:10 -07001353 oidcpy(&(*found)->old_oid, &oid);
Brandon Williams73302052018-06-27 15:30:23 -07001354 }
1355
1356 if (reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001357 die(_("error processing wanted refs: %d"), reader->status);
Brandon Williams73302052018-06-27 15:30:23 -07001358}
1359
Brandon Williams685fbd32018-03-15 10:31:28 -07001360enum fetch_state {
1361 FETCH_CHECK_LOCAL = 0,
1362 FETCH_SEND_REQUEST,
1363 FETCH_PROCESS_ACKS,
1364 FETCH_GET_PACK,
1365 FETCH_DONE,
1366};
1367
1368static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1369 int fd[2],
1370 const struct ref *orig_ref,
1371 struct ref **sought, int nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001372 struct oid_array *shallows,
1373 struct shallow_info *si,
Brandon Williams685fbd32018-03-15 10:31:28 -07001374 char **pack_lockfile)
1375{
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001376 struct repository *r = the_repository;
Brandon Williams685fbd32018-03-15 10:31:28 -07001377 struct ref *ref = copy_ref_list(orig_ref);
1378 enum fetch_state state = FETCH_CHECK_LOCAL;
1379 struct oidset common = OIDSET_INIT;
1380 struct packet_reader reader;
1381 int in_vain = 0;
1382 int haves_to_send = INITIAL_FLUSH;
Jonathan Tanec062832018-06-14 15:54:28 -07001383 struct fetch_negotiator negotiator;
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001384 fetch_negotiator_init(r, &negotiator);
Brandon Williams685fbd32018-03-15 10:31:28 -07001385 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -08001386 PACKET_READ_CHOMP_NEWLINE |
1387 PACKET_READ_DIE_ON_ERR_PACKET);
Jonathan Tan07c3c2a2019-01-16 11:28:15 -08001388 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1389 server_supports_feature("fetch", "sideband-all", 0)) {
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001390 reader.use_sideband = 1;
1391 reader.me = "fetch-pack";
1392 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001393
1394 while (state != FETCH_DONE) {
1395 switch (state) {
1396 case FETCH_CHECK_LOCAL:
1397 sort_ref_list(&ref, ref_compare_name);
1398 QSORT(sought, nr_sought, cmp_ref_by_name);
1399
1400 /* v2 supports these by default */
1401 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1402 use_sideband = 2;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001403 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1404 args->deepen = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001405
Brandon Williams685fbd32018-03-15 10:31:28 -07001406 /* Filter 'ref' by 'sought' and those that aren't local */
Jonathan Tan12f19a92018-10-03 16:04:52 -07001407 if (!args->no_dependents) {
1408 mark_complete_and_common_ref(&negotiator, args, &ref);
1409 filter_refs(args, &ref, sought, nr_sought);
1410 if (everything_local(args, &ref))
1411 state = FETCH_DONE;
1412 else
1413 state = FETCH_SEND_REQUEST;
Jonathan Tanaf1c90d2018-06-14 15:54:25 -07001414
Jonathan Tan12f19a92018-10-03 16:04:52 -07001415 mark_tips(&negotiator, args->negotiation_tips);
1416 for_each_cached_alternate(&negotiator,
1417 insert_one_alternate_object);
1418 } else {
1419 filter_refs(args, &ref, sought, nr_sought);
1420 state = FETCH_SEND_REQUEST;
1421 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001422 break;
1423 case FETCH_SEND_REQUEST:
Jonathan Tanec062832018-06-14 15:54:28 -07001424 if (send_fetch_request(&negotiator, fd[1], args, ref,
1425 &common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001426 &haves_to_send, &in_vain,
1427 reader.use_sideband))
Brandon Williams685fbd32018-03-15 10:31:28 -07001428 state = FETCH_GET_PACK;
1429 else
1430 state = FETCH_PROCESS_ACKS;
1431 break;
1432 case FETCH_PROCESS_ACKS:
1433 /* Process ACKs/NAKs */
Jonathan Tanec062832018-06-14 15:54:28 -07001434 switch (process_acks(&negotiator, &reader, &common)) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001435 case 2:
1436 state = FETCH_GET_PACK;
1437 break;
1438 case 1:
1439 in_vain = 0;
1440 /* fallthrough */
1441 default:
1442 state = FETCH_SEND_REQUEST;
1443 break;
1444 }
1445 break;
1446 case FETCH_GET_PACK:
Brandon Williamsf7e20502018-03-15 10:31:29 -07001447 /* Check for shallow-info section */
1448 if (process_section_header(&reader, "shallow-info", 1))
Jonathan Tan13390782019-03-26 12:31:21 -07001449 receive_shallow_info(args, &reader, shallows, si);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001450
Brandon Williams73302052018-06-27 15:30:23 -07001451 if (process_section_header(&reader, "wanted-refs", 1))
Jonathan Tane2842b32018-08-01 13:13:20 -07001452 receive_wanted_refs(&reader, sought, nr_sought);
Brandon Williams73302052018-06-27 15:30:23 -07001453
Brandon Williams685fbd32018-03-15 10:31:28 -07001454 /* get the pack */
1455 process_section_header(&reader, "packfile", 0);
1456 if (get_pack(args, fd, pack_lockfile))
1457 die(_("git fetch-pack: fetch failed."));
1458
1459 state = FETCH_DONE;
1460 break;
1461 case FETCH_DONE:
1462 continue;
1463 }
1464 }
1465
Jonathan Tanec062832018-06-14 15:54:28 -07001466 negotiator.release(&negotiator);
Brandon Williams685fbd32018-03-15 10:31:28 -07001467 oidset_clear(&common);
1468 return ref;
1469}
1470
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001471static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1472{
1473 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1474 const char *path;
1475
1476 if (git_config_pathname(&path, var, value))
1477 return 1;
1478 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1479 fsck_msg_types.len ? ',' : '=', path);
1480 free((char *)path);
1481 return 0;
1482 }
1483
1484 if (skip_prefix(var, "fetch.fsck.", &var)) {
1485 if (is_valid_msg_type(var, value))
1486 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1487 fsck_msg_types.len ? ',' : '=', var, value);
1488 else
1489 warning("Skipping unknown msg id '%s'", var);
1490 return 0;
1491 }
1492
1493 return git_default_config(var, value, cb);
1494}
1495
Tanay Abhraf44af512014-08-07 09:21:20 -07001496static void fetch_pack_config(void)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001497{
Tanay Abhraf44af512014-08-07 09:21:20 -07001498 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1499 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1500 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1501 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1502 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001503
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001504 git_config(fetch_pack_config_cb, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001505}
1506
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001507static void fetch_pack_setup(void)
1508{
1509 static int did_setup;
1510 if (did_setup)
1511 return;
Tanay Abhraf44af512014-08-07 09:21:20 -07001512 fetch_pack_config();
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001513 if (0 <= transfer_unpack_limit)
1514 unpack_limit = transfer_unpack_limit;
1515 else if (0 <= fetch_unpack_limit)
1516 unpack_limit = fetch_unpack_limit;
1517 did_setup = 1;
1518}
1519
Junio C Hamanof2db8542013-01-29 14:02:15 -08001520static int remove_duplicates_in_refs(struct ref **ref, int nr)
1521{
1522 struct string_list names = STRING_LIST_INIT_NODUP;
1523 int src, dst;
1524
1525 for (src = dst = 0; src < nr; src++) {
1526 struct string_list_item *item;
1527 item = string_list_insert(&names, ref[src]->name);
1528 if (item->util)
1529 continue; /* already have it */
1530 item->util = ref[src];
1531 if (src != dst)
1532 ref[dst] = ref[src];
1533 dst++;
1534 }
1535 for (src = dst; src < nr; src++)
1536 ref[src] = NULL;
1537 string_list_clear(&names, 0);
1538 return dst;
1539}
1540
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001541static void update_shallow(struct fetch_pack_args *args,
Jonathan Tane2842b32018-08-01 13:13:20 -07001542 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001543 struct shallow_info *si)
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001544{
brian m. carlson910650d2017-03-31 01:40:00 +00001545 struct oid_array ref = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001546 int *status;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001547 int i;
1548
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001549 if (args->deepen && alternate_shallow_file) {
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001550 if (*alternate_shallow_file == '\0') { /* --unshallow */
Stefan Beller102de882018-05-17 15:51:51 -07001551 unlink_or_warn(git_path_shallow(the_repository));
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001552 rollback_lock_file(&shallow_lock);
1553 } else
1554 commit_lock_file(&shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001555 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001556 return;
1557 }
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001558
1559 if (!si->shallow || !si->shallow->nr)
1560 return;
1561
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001562 if (args->cloning) {
1563 /*
1564 * remote is shallow, but this is a clone, there are
1565 * no objects in repo to worry about. Accept any
1566 * shallow points that exist in the pack (iow in repo
1567 * after get_pack() and reprepare_packed_git())
1568 */
brian m. carlson910650d2017-03-31 01:40:00 +00001569 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001570 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001571 for (i = 0; i < si->shallow->nr; i++)
brian m. carlsonee3051b2017-03-26 16:01:37 +00001572 if (has_object_file(&oid[i]))
brian m. carlson910650d2017-03-31 01:40:00 +00001573 oid_array_append(&extra, &oid[i]);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001574 if (extra.nr) {
1575 setup_alternate_shallow(&shallow_lock,
1576 &alternate_shallow_file,
1577 &extra);
1578 commit_lock_file(&shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001579 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001580 }
brian m. carlson910650d2017-03-31 01:40:00 +00001581 oid_array_clear(&extra);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001582 return;
1583 }
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001584
1585 if (!si->nr_ours && !si->nr_theirs)
1586 return;
1587
1588 remove_nonexistent_theirs_shallow(si);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001589 if (!si->nr_ours && !si->nr_theirs)
1590 return;
Jonathan Tane2842b32018-08-01 13:13:20 -07001591 for (i = 0; i < nr_sought; i++)
1592 oid_array_append(&ref, &sought[i]->old_oid);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001593 si->ref = &ref;
1594
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001595 if (args->update_shallow) {
1596 /*
1597 * remote is also shallow, .git/shallow may be updated
1598 * so all refs can be accepted. Make sure we only add
1599 * shallow roots that are actually reachable from new
1600 * refs.
1601 */
brian m. carlson910650d2017-03-31 01:40:00 +00001602 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001603 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001604 assign_shallow_commits_to_refs(si, NULL, NULL);
1605 if (!si->nr_ours && !si->nr_theirs) {
brian m. carlson910650d2017-03-31 01:40:00 +00001606 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001607 return;
1608 }
1609 for (i = 0; i < si->nr_ours; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001610 oid_array_append(&extra, &oid[si->ours[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001611 for (i = 0; i < si->nr_theirs; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001612 oid_array_append(&extra, &oid[si->theirs[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001613 setup_alternate_shallow(&shallow_lock,
1614 &alternate_shallow_file,
1615 &extra);
1616 commit_lock_file(&shallow_lock);
brian m. carlson910650d2017-03-31 01:40:00 +00001617 oid_array_clear(&extra);
1618 oid_array_clear(&ref);
brian m. carlson23311f32019-02-04 00:06:50 +00001619 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001620 return;
1621 }
1622
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001623 /*
1624 * remote is also shallow, check what ref is safe to update
1625 * without updating .git/shallow
1626 */
Jonathan Tane2842b32018-08-01 13:13:20 -07001627 status = xcalloc(nr_sought, sizeof(*status));
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001628 assign_shallow_commits_to_refs(si, NULL, status);
1629 if (si->nr_ours || si->nr_theirs) {
Jonathan Tane2842b32018-08-01 13:13:20 -07001630 for (i = 0; i < nr_sought; i++)
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001631 if (status[i])
Jonathan Tane2842b32018-08-01 13:13:20 -07001632 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001633 }
1634 free(status);
brian m. carlson910650d2017-03-31 01:40:00 +00001635 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001636}
1637
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001638static int iterate_ref_map(void *cb_data, struct object_id *oid)
1639{
1640 struct ref **rm = cb_data;
1641 struct ref *ref = *rm;
1642
1643 if (!ref)
1644 return -1; /* end of the list */
1645 *rm = ref->next;
1646 oidcpy(oid, &ref->old_oid);
1647 return 0;
1648}
1649
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001650struct ref *fetch_pack(struct fetch_pack_args *args,
Jeff King0f804b02019-03-20 04:16:14 -04001651 int fd[],
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001652 const struct ref *ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -08001653 struct ref **sought, int nr_sought,
brian m. carlson910650d2017-03-31 01:40:00 +00001654 struct oid_array *shallow,
Brandon Williams685fbd32018-03-15 10:31:28 -07001655 char **pack_lockfile,
1656 enum protocol_version version)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001657{
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001658 struct ref *ref_cpy;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001659 struct shallow_info si;
Jonathan Tan13390782019-03-26 12:31:21 -07001660 struct oid_array shallows_scratch = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001661
1662 fetch_pack_setup();
Junio C Hamanof2db8542013-01-29 14:02:15 -08001663 if (nr_sought)
1664 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001665
Jonathan Tan4c7f9562018-10-03 16:04:53 -07001666 if (args->no_dependents && !args->filter_options.choice) {
1667 /*
1668 * The protocol does not support requesting that only the
1669 * wanted objects be sent, so approximate this by setting a
1670 * "blob:none" filter if no filter is already set. This works
1671 * for all object types: note that wanted blobs will still be
1672 * sent because they are directly specified as a "want".
1673 *
1674 * NEEDSWORK: Add an option in the protocol to request that
1675 * only the wanted objects be sent, and implement it.
1676 */
1677 parse_list_objects_filter(&args->filter_options, "blob:none");
1678 }
1679
Jonathan Tan01775652018-09-27 12:24:05 -07001680 if (version != protocol_v2 && !ref) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001681 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001682 die(_("no matching remote head"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001683 }
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001684 if (version == protocol_v2) {
1685 if (shallow->nr)
1686 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1687 memset(&si, 0, sizeof(si));
Brandon Williams685fbd32018-03-15 10:31:28 -07001688 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001689 &shallows_scratch, &si,
Brandon Williams685fbd32018-03-15 10:31:28 -07001690 pack_lockfile);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001691 } else {
1692 prepare_shallow_info(&si, shallow);
Brandon Williams685fbd32018-03-15 10:31:28 -07001693 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1694 &si, pack_lockfile);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001695 }
Stefan Bellera49d2832018-03-23 18:45:21 +01001696 reprepare_packed_git(the_repository);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001697
1698 if (!args->cloning && args->deepen) {
1699 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1700 struct ref *iterator = ref_cpy;
1701 opt.shallow_file = alternate_shallow_file;
1702 if (args->deepen)
1703 opt.is_deepening_fetch = 1;
1704 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1705 error(_("remote did not send all necessary objects"));
1706 free_refs(ref_cpy);
1707 ref_cpy = NULL;
1708 rollback_lock_file(&shallow_lock);
1709 goto cleanup;
1710 }
1711 args->connectivity_checked = 1;
1712 }
1713
Jonathan Tane2842b32018-08-01 13:13:20 -07001714 update_shallow(args, sought, nr_sought, &si);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001715cleanup:
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001716 clear_shallow_info(&si);
Jonathan Tan13390782019-03-26 12:31:21 -07001717 oid_array_clear(&shallows_scratch);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001718 return ref_cpy;
1719}
Matt McCutchene860d962017-02-22 11:01:22 -05001720
1721int report_unmatched_refs(struct ref **sought, int nr_sought)
1722{
1723 int i, ret = 0;
1724
1725 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -05001726 if (!sought[i])
Matt McCutchene860d962017-02-22 11:01:22 -05001727 continue;
Matt McCutchend56583d2017-02-22 11:05:57 -05001728 switch (sought[i]->match_status) {
1729 case REF_MATCHED:
1730 continue;
1731 case REF_NOT_MATCHED:
1732 error(_("no such remote ref %s"), sought[i]->name);
1733 break;
1734 case REF_UNADVERTISED_NOT_ALLOWED:
1735 error(_("Server does not allow request for unadvertised object %s"),
1736 sought[i]->name);
1737 break;
1738 }
Matt McCutchene860d962017-02-22 11:01:22 -05001739 ret = 1;
1740 }
1741 return ret;
1742}