blob: 65be043f2afafdb37b4ce4d6fe8fbd3ac0eb2eaf [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;
Jonathan Tan42cc7482018-07-16 11:44:01 -070039static char *negotiation_algorithm;
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +000040static struct strbuf fsck_msg_types = STRBUF_INIT;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070041
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070042/* Remember to update object flag allocation in object.h */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070043#define COMPLETE (1U << 0)
Jonathan Tanec062832018-06-14 15:54:28 -070044#define ALTERNATE (1U << 1)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070045
46/*
47 * After sending this many "have"s if we do not get any new ACK , we
48 * give up traversing our history.
49 */
50#define MAX_IN_VAIN 256
51
Jonathan Tand30fe892018-06-14 15:54:26 -070052static int multi_ack, use_sideband;
Fredrik Medley7199c092015-05-21 22:23:38 +020053/* Allow specifying sha1 if it is a ref tip. */
54#define ALLOW_TIP_SHA1 01
Fredrik Medley68ee6282015-05-21 22:23:39 +020055/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
56#define ALLOW_REACHABLE_SHA1 02
Fredrik Medley7199c092015-05-21 22:23:38 +020057static unsigned int allow_unadvertised_object_request;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070058
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +070059__attribute__((format (printf, 2, 3)))
60static inline void print_verbose(const struct fetch_pack_args *args,
61 const char *fmt, ...)
62{
63 va_list params;
64
65 if (!args->verbose)
66 return;
67
68 va_start(params, fmt);
69 vfprintf(stderr, fmt, params);
70 va_end(params);
71 fputc('\n', stderr);
72}
73
Jeff King41a078c2017-02-08 15:53:03 -050074struct alternate_object_cache {
75 struct object **items;
76 size_t nr, alloc;
77};
78
Jeff Kingbdf42762018-10-08 11:09:23 -070079static void cache_one_alternate(const struct object_id *oid,
Jeff King41a078c2017-02-08 15:53:03 -050080 void *vcache)
81{
82 struct alternate_object_cache *cache = vcache;
Stefan Beller109cd762018-06-28 18:21:51 -070083 struct object *obj = parse_object(the_repository, oid);
Jeff King41a078c2017-02-08 15:53:03 -050084
85 if (!obj || (obj->flags & ALTERNATE))
86 return;
87
88 obj->flags |= ALTERNATE;
89 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
90 cache->items[cache->nr++] = obj;
91}
92
Jonathan Tanec062832018-06-14 15:54:28 -070093static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
94 void (*cb)(struct fetch_negotiator *,
Jonathan Tand30fe892018-06-14 15:54:26 -070095 struct object *))
Jeff King41a078c2017-02-08 15:53:03 -050096{
97 static int initialized;
98 static struct alternate_object_cache cache;
99 size_t i;
100
101 if (!initialized) {
102 for_each_alternate_ref(cache_one_alternate, &cache);
103 initialized = 1;
104 }
105
106 for (i = 0; i < cache.nr; i++)
Jonathan Tanec062832018-06-14 15:54:28 -0700107 cb(negotiator, cache.items[i]);
Jeff King41a078c2017-02-08 15:53:03 -0500108}
109
Jonathan Tanec062832018-06-14 15:54:28 -0700110static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700111 const char *refname,
112 const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700113{
Stefan Bellera74093d2018-06-28 18:22:05 -0700114 struct object *o = deref_tag(the_repository,
115 parse_object(the_repository, oid),
Stefan Beller109cd762018-06-28 18:21:51 -0700116 refname, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700117
118 if (o && o->type == OBJ_COMMIT)
Jonathan Tanec062832018-06-14 15:54:28 -0700119 negotiator->add_tip(negotiator, (struct commit *)o);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700120
121 return 0;
122}
123
Michael Haggertyb1b49c62015-05-25 18:39:18 +0000124static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
125 int flag, void *cb_data)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700126{
Jonathan Tand30fe892018-06-14 15:54:26 -0700127 return rev_list_insert_ref(cb_data, refname, oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700128}
129
130enum ack_type {
131 NAK = 0,
132 ACK,
133 ACK_continue,
134 ACK_common,
135 ACK_ready
136};
137
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800138static void consume_shallow_list(struct fetch_pack_args *args,
139 struct packet_reader *reader)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700140{
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700141 if (args->stateless_rpc && args->deepen) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700142 /* If we sent a depth we will get back "duplicate"
143 * shallow and unshallow commands every time there
144 * is a block of have lines exchanged.
145 */
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800146 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
147 if (starts_with(reader->line, "shallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700148 continue;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800149 if (starts_with(reader->line, "unshallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700150 continue;
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700151 die(_("git fetch-pack: expected shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700152 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800153 if (reader->status != PACKET_READ_FLUSH)
154 die(_("git fetch-pack: expected a flush packet after shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700155 }
156}
157
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800158static enum ack_type get_ack(struct packet_reader *reader,
159 struct object_id *result_oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700160{
Jeff King74543a02013-02-20 15:02:57 -0500161 int len;
Jeff King82e56762014-06-18 15:56:03 -0400162 const char *arg;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700163
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800164 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
Jeff Kingbc9d4dc2018-02-08 13:47:49 -0500165 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800166 len = reader->pktlen;
167
168 if (!strcmp(reader->line, "NAK"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700169 return NAK;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800170 if (skip_prefix(reader->line, "ACK ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000171 if (!get_oid_hex(arg, result_oid)) {
Jeff King82e56762014-06-18 15:56:03 -0400172 arg += 40;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800173 len -= arg - reader->line;
Jeff King82e56762014-06-18 15:56:03 -0400174 if (len < 1)
Jeff King030e9dd2013-02-20 15:00:28 -0500175 return ACK;
Jeff King82e56762014-06-18 15:56:03 -0400176 if (strstr(arg, "continue"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700177 return ACK_continue;
Jeff King82e56762014-06-18 15:56:03 -0400178 if (strstr(arg, "common"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700179 return ACK_common;
Jeff King82e56762014-06-18 15:56:03 -0400180 if (strstr(arg, "ready"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700181 return ACK_ready;
182 return ACK;
183 }
184 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800185 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700186}
187
188static void send_request(struct fetch_pack_args *args,
189 int fd, struct strbuf *buf)
190{
191 if (args->stateless_rpc) {
192 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
193 packet_flush(fd);
Jeff King37c80012019-03-04 23:11:39 -0500194 } else {
195 if (write_in_full(fd, buf->buf, buf->len) < 0)
196 die_errno(_("unable to write to remote"));
197 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700198}
199
Jonathan Tanec062832018-06-14 15:54:28 -0700200static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700201 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700202{
Jonathan Tanec062832018-06-14 15:54:28 -0700203 rev_list_insert_ref(negotiator, NULL, &obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700204}
205
206#define INITIAL_FLUSH 16
207#define PIPESAFE_FLUSH 32
Jonathan Tanda470982016-07-18 15:21:38 -0700208#define LARGE_FLUSH 16384
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700209
Brandon Williams685fbd32018-03-15 10:31:28 -0700210static int next_flush(int stateless_rpc, int count)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700211{
Brandon Williams685fbd32018-03-15 10:31:28 -0700212 if (stateless_rpc) {
Jonathan Tanda470982016-07-18 15:21:38 -0700213 if (count < LARGE_FLUSH)
214 count <<= 1;
215 else
216 count = count * 11 / 10;
217 } else {
218 if (count < PIPESAFE_FLUSH)
219 count <<= 1;
220 else
221 count += PIPESAFE_FLUSH;
222 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700223 return count;
224}
225
Jonathan Tan3390e422018-07-02 15:39:44 -0700226static void mark_tips(struct fetch_negotiator *negotiator,
227 const struct oid_array *negotiation_tips)
228{
229 int i;
230
231 if (!negotiation_tips) {
232 for_each_ref(rev_list_insert_ref_oid, negotiator);
233 return;
234 }
235
236 for (i = 0; i < negotiation_tips->nr; i++)
237 rev_list_insert_ref(negotiator, NULL,
238 &negotiation_tips->oid[i]);
239 return;
240}
241
Jonathan Tanec062832018-06-14 15:54:28 -0700242static int find_common(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700243 struct fetch_pack_args *args,
brian m. carlson1b283372017-05-01 02:28:54 +0000244 int fd[2], struct object_id *result_oid,
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700245 struct ref *refs)
246{
247 int fetching;
248 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
brian m. carlson1b283372017-05-01 02:28:54 +0000249 const struct object_id *oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700250 unsigned in_vain = 0;
251 int got_continue = 0;
252 int got_ready = 0;
253 struct strbuf req_buf = STRBUF_INIT;
254 size_t state_len = 0;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800255 struct packet_reader reader;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700256
257 if (args->stateless_rpc && multi_ack == 1)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700258 die(_("--stateless-rpc requires multi_ack_detailed"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700259
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800260 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -0800261 PACKET_READ_CHOMP_NEWLINE |
262 PACKET_READ_DIE_ON_ERR_PACKET);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800263
Jonathan Tan12f19a92018-10-03 16:04:52 -0700264 if (!args->no_dependents) {
265 mark_tips(negotiator, args->negotiation_tips);
266 for_each_cached_alternate(negotiator, insert_one_alternate_object);
267 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700268
269 fetching = 0;
270 for ( ; refs ; refs = refs->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000271 struct object_id *remote = &refs->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700272 const char *remote_hex;
273 struct object *o;
274
275 /*
276 * If that object is complete (i.e. it is an ancestor of a
277 * local ref), we tell them we have it but do not have to
278 * tell them about its ancestors, which they already know
279 * about.
280 *
281 * We use lookup_object here because we are only
282 * interested in the case we *know* the object is
283 * reachable and we have already scanned it.
Jonathan Tan12f19a92018-10-03 16:04:52 -0700284 *
285 * Do this only if args->no_dependents is false (if it is true,
286 * we cannot trust the object flags).
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700287 */
Jonathan Tan12f19a92018-10-03 16:04:52 -0700288 if (!args->no_dependents &&
Jeff Kingd0229ab2019-06-20 03:41:14 -0400289 ((o = lookup_object(the_repository, remote)) != NULL) &&
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700290 (o->flags & COMPLETE)) {
291 continue;
292 }
293
brian m. carlson1b283372017-05-01 02:28:54 +0000294 remote_hex = oid_to_hex(remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700295 if (!fetching) {
296 struct strbuf c = STRBUF_INIT;
297 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
298 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
299 if (no_done) strbuf_addstr(&c, " no-done");
300 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
301 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700302 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700303 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
304 if (args->no_progress) strbuf_addstr(&c, " no-progress");
305 if (args->include_tag) strbuf_addstr(&c, " include-tag");
306 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700307 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700308 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700309 if (agent_supported) strbuf_addf(&c, " agent=%s",
310 git_user_agent_sanitized());
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000311 if (args->filter_options.choice)
312 strbuf_addstr(&c, " filter");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700313 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
314 strbuf_release(&c);
315 } else
316 packet_buf_write(&req_buf, "want %s\n", remote_hex);
317 fetching++;
318 }
319
320 if (!fetching) {
321 strbuf_release(&req_buf);
322 packet_flush(fd[1]);
323 return 1;
324 }
325
Stefan Bellerc8813482018-05-17 15:51:46 -0700326 if (is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700327 write_shallow_commits(&req_buf, 1, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700328 if (args->depth > 0)
329 packet_buf_write(&req_buf, "deepen %d", args->depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700330 if (args->deepen_since) {
Johannes Schindelindddbad72017-04-26 21:29:31 +0200331 timestamp_t max_age = approxidate(args->deepen_since);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200332 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700333 }
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700334 if (args->deepen_not) {
335 int i;
336 for (i = 0; i < args->deepen_not->nr; i++) {
337 struct string_list_item *s = args->deepen_not->items + i;
338 packet_buf_write(&req_buf, "deepen-not %s", s->string);
339 }
340 }
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800341 if (server_supports_filtering && args->filter_options.choice) {
342 struct strbuf expanded_filter_spec = STRBUF_INIT;
343 expand_list_objects_filter_spec(&args->filter_options,
344 &expanded_filter_spec);
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000345 packet_buf_write(&req_buf, "filter %s",
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800346 expanded_filter_spec.buf);
347 strbuf_release(&expanded_filter_spec);
348 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700349 packet_buf_flush(&req_buf);
350 state_len = req_buf.len;
351
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700352 if (args->deepen) {
Jeff Kingae021d82014-06-18 15:47:50 -0400353 const char *arg;
brian m. carlson1b283372017-05-01 02:28:54 +0000354 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700355
356 send_request(args, fd[1], &req_buf);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800357 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
358 if (skip_prefix(reader.line, "shallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000359 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800360 die(_("invalid shallow line: %s"), reader.line);
Stefan Beller19143f12018-05-17 15:51:44 -0700361 register_shallow(the_repository, &oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700362 continue;
363 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800364 if (skip_prefix(reader.line, "unshallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000365 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800366 die(_("invalid unshallow line: %s"), reader.line);
Jeff Kingd0229ab2019-06-20 03:41:14 -0400367 if (!lookup_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800368 die(_("object not found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700369 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -0700370 if (!parse_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800371 die(_("error in object: %s"), reader.line);
brian m. carlsone92b8482017-05-06 22:10:06 +0000372 if (unregister_shallow(&oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800373 die(_("no shallow found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700374 continue;
375 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800376 die(_("expected shallow/unshallow, got %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700377 }
378 } else if (!args->stateless_rpc)
379 send_request(args, fd[1], &req_buf);
380
381 if (!args->stateless_rpc) {
382 /* If we aren't using the stateless-rpc interface
383 * we don't need to retain the headers.
384 */
385 strbuf_setlen(&req_buf, 0);
386 state_len = 0;
387 }
388
389 flushes = 0;
390 retval = -1;
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000391 if (args->no_dependents)
392 goto done;
Jonathan Tanec062832018-06-14 15:54:28 -0700393 while ((oid = negotiator->next(negotiator))) {
brian m. carlson1b283372017-05-01 02:28:54 +0000394 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
395 print_verbose(args, "have %s", oid_to_hex(oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700396 in_vain++;
397 if (flush_at <= ++count) {
398 int ack;
399
400 packet_buf_flush(&req_buf);
401 send_request(args, fd[1], &req_buf);
402 strbuf_setlen(&req_buf, state_len);
403 flushes++;
Brandon Williams685fbd32018-03-15 10:31:28 -0700404 flush_at = next_flush(args->stateless_rpc, count);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700405
406 /*
407 * We keep one window "ahead" of the other side, and
408 * will wait for an ACK only on the next one
409 */
410 if (!args->stateless_rpc && count == INITIAL_FLUSH)
411 continue;
412
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800413 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700414 do {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800415 ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700416 if (ack)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700417 print_verbose(args, _("got %s %d %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000418 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700419 switch (ack) {
420 case ACK:
421 flushes = 0;
422 multi_ack = 0;
423 retval = 0;
424 goto done;
425 case ACK_common:
426 case ACK_ready:
427 case ACK_continue: {
428 struct commit *commit =
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700429 lookup_commit(the_repository,
430 result_oid);
Jonathan Tand093bc72018-06-14 15:54:27 -0700431 int was_common;
Junio C Hamano3a2a1dc2018-08-02 15:30:42 -0700432
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700433 if (!commit)
brian m. carlson1b283372017-05-01 02:28:54 +0000434 die(_("invalid commit %s"), oid_to_hex(result_oid));
Jonathan Tanec062832018-06-14 15:54:28 -0700435 was_common = negotiator->ack(negotiator, commit);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700436 if (args->stateless_rpc
437 && ack == ACK_common
Jonathan Tand093bc72018-06-14 15:54:27 -0700438 && !was_common) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700439 /* We need to replay the have for this object
440 * on the next RPC request so the peer knows
441 * it is in common with us.
442 */
brian m. carlson1b283372017-05-01 02:28:54 +0000443 const char *hex = oid_to_hex(result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700444 packet_buf_write(&req_buf, "have %s\n", hex);
445 state_len = req_buf.len;
Jonathan Tan06b3d382016-09-23 10:41:35 -0700446 /*
447 * Reset in_vain because an ack
448 * for this commit has not been
449 * seen.
450 */
451 in_vain = 0;
452 } else if (!args->stateless_rpc
453 || ack != ACK_common)
454 in_vain = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700455 retval = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700456 got_continue = 1;
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700457 if (ack == ACK_ready)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700458 got_ready = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700459 break;
460 }
461 }
462 } while (ack);
463 flushes--;
464 if (got_continue && MAX_IN_VAIN < in_vain) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700465 print_verbose(args, _("giving up"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700466 break; /* give up */
467 }
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700468 if (got_ready)
469 break;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700470 }
471 }
472done:
473 if (!got_ready || !no_done) {
474 packet_buf_write(&req_buf, "done\n");
475 send_request(args, fd[1], &req_buf);
476 }
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700477 print_verbose(args, _("done"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700478 if (retval != 0) {
479 multi_ack = 0;
480 flushes++;
481 }
482 strbuf_release(&req_buf);
483
Nguyễn Thái Ngọc Duyff62eca2014-02-06 22:10:39 +0700484 if (!got_ready || !no_done)
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800485 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700486 while (flushes || multi_ack) {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800487 int ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700488 if (ack) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700489 print_verbose(args, _("got %s (%d) %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000490 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700491 if (ack == ACK)
492 return 0;
493 multi_ack = 1;
494 continue;
495 }
496 flushes--;
497 }
498 /* it is no error to fetch into a completely empty repo */
499 return count ? retval : 0;
500}
501
502static struct commit_list *complete;
503
brian m. carlson1b283372017-05-01 02:28:54 +0000504static int mark_complete(const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700505{
Stefan Beller109cd762018-06-28 18:21:51 -0700506 struct object *o = parse_object(the_repository, oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700507
508 while (o && o->type == OBJ_TAG) {
509 struct tag *t = (struct tag *) o;
510 if (!t->tagged)
511 break; /* broken repository */
512 o->flags |= COMPLETE;
Stefan Beller109cd762018-06-28 18:21:51 -0700513 o = parse_object(the_repository, &t->tagged->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700514 }
515 if (o && o->type == OBJ_COMMIT) {
516 struct commit *commit = (struct commit *)o;
517 if (!(commit->object.flags & COMPLETE)) {
518 commit->object.flags |= COMPLETE;
Jeff King16445242013-07-02 02:16:23 -0400519 commit_list_insert(commit, &complete);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700520 }
521 }
522 return 0;
523}
524
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000525static int mark_complete_oid(const char *refname, const struct object_id *oid,
526 int flag, void *cb_data)
527{
brian m. carlson1b283372017-05-01 02:28:54 +0000528 return mark_complete(oid);
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000529}
530
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700531static void mark_recent_complete_commits(struct fetch_pack_args *args,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200532 timestamp_t cutoff)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700533{
534 while (complete && cutoff <= complete->item->date) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700535 print_verbose(args, _("Marking %s as complete"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700536 oid_to_hex(&complete->item->object.oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700537 pop_most_recent_commit(&complete, COMPLETE);
538 }
539}
540
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700541static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
542{
543 for (; refs; refs = refs->next)
544 oidset_insert(oids, &refs->old_oid);
545}
546
René Scharfebf732822018-10-04 17:09:06 +0200547static int is_unmatched_ref(const struct ref *ref)
548{
549 struct object_id oid;
550 const char *p;
551 return ref->match_status == REF_NOT_MATCHED &&
552 !parse_oid_hex(ref->name, &oid, &p) &&
553 *p == '\0' &&
554 oideq(&oid, &ref->old_oid);
555}
556
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700557static void filter_refs(struct fetch_pack_args *args,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800558 struct ref **refs,
559 struct ref **sought, int nr_sought)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700560{
561 struct ref *newlist = NULL;
562 struct ref **newtail = &newlist;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700563 struct ref *unmatched = NULL;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700564 struct ref *ref, *next;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700565 struct oidset tip_oids = OIDSET_INIT;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800566 int i;
René Scharfe22a16462018-10-04 17:09:39 +0200567 int strict = !(allow_unadvertised_object_request &
568 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700569
Junio C Hamanof2db8542013-01-29 14:02:15 -0800570 i = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700571 for (ref = *refs; ref; ref = next) {
572 int keep = 0;
573 next = ref->next;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800574
René Scharfe50e19a82014-06-06 19:24:48 +0200575 if (starts_with(ref->name, "refs/") &&
Jeff King34066f02019-04-13 01:57:37 -0400576 check_refname_format(ref->name, 0)) {
577 /*
578 * trash or a peeled value; do not even add it to
579 * unmatched list
580 */
581 free_one_ref(ref);
582 continue;
583 } else {
Junio C Hamanof2db8542013-01-29 14:02:15 -0800584 while (i < nr_sought) {
585 int cmp = strcmp(ref->name, sought[i]->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700586 if (cmp < 0)
587 break; /* definitely do not have it */
588 else if (cmp == 0) {
589 keep = 1; /* definitely have it */
Matt McCutchend56583d2017-02-22 11:05:57 -0500590 sought[i]->match_status = REF_MATCHED;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700591 }
Junio C Hamanof2db8542013-01-29 14:02:15 -0800592 i++;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700593 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700594
Jeff Kinge9502c02018-06-11 01:53:57 -0400595 if (!keep && args->fetch_all &&
596 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
597 keep = 1;
598 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700599
600 if (keep) {
601 *newtail = ref;
602 ref->next = NULL;
603 newtail = &ref->next;
604 } else {
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700605 ref->next = unmatched;
606 unmatched = ref;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700607 }
608 }
609
René Scharfe22a16462018-10-04 17:09:39 +0200610 if (strict) {
611 for (i = 0; i < nr_sought; i++) {
612 ref = sought[i];
613 if (!is_unmatched_ref(ref))
614 continue;
615
616 add_refs_to_oidset(&tip_oids, unmatched);
617 add_refs_to_oidset(&tip_oids, newlist);
618 break;
619 }
620 }
621
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800622 /* Append unmatched requests to the list */
Matt McCutchend56583d2017-02-22 11:05:57 -0500623 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500624 ref = sought[i];
René Scharfebf732822018-10-04 17:09:06 +0200625 if (!is_unmatched_ref(ref))
Matt McCutchend56583d2017-02-22 11:05:57 -0500626 continue;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800627
René Scharfe22a16462018-10-04 17:09:39 +0200628 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500629 ref->match_status = REF_MATCHED;
Jeff Kingc3c17bf2015-03-19 16:37:09 -0400630 *newtail = copy_ref(ref);
631 newtail = &(*newtail)->next;
Matt McCutchend56583d2017-02-22 11:05:57 -0500632 } else {
633 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800634 }
635 }
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700636
637 oidset_clear(&tip_oids);
Jeff King259eddd2019-04-13 01:54:09 -0400638 free_refs(unmatched);
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700639
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700640 *refs = newlist;
641}
642
Jonathan Tanec062832018-06-14 15:54:28 -0700643static void mark_alternate_complete(struct fetch_negotiator *unused,
Jonathan Tand30fe892018-06-14 15:54:26 -0700644 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700645{
brian m. carlson1b283372017-05-01 02:28:54 +0000646 mark_complete(&obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700647}
648
Takuto Ikuta024aa462018-03-14 15:32:42 +0900649struct loose_object_iter {
650 struct oidset *loose_object_set;
651 struct ref *refs;
652};
653
654/*
Jonathan Tan34c29032018-06-06 13:47:07 -0700655 * Mark recent commits available locally and reachable from a local ref as
656 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
657 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
658 * thus do not need COMMON_REF marks).
659 *
660 * The cutoff time for recency is determined by this heuristic: it is the
661 * earliest commit time of the objects in refs that are commits and that we know
662 * the commit time of.
663 */
Jonathan Tanec062832018-06-14 15:54:28 -0700664static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700665 struct fetch_pack_args *args,
Jonathan Tan34c29032018-06-06 13:47:07 -0700666 struct ref **refs)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700667{
668 struct ref *ref;
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000669 int old_save_commit_buffer = save_commit_buffer;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200670 timestamp_t cutoff = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700671
672 save_commit_buffer = 0;
673
674 for (ref = *refs; ref; ref = ref->next) {
675 struct object *o;
676
Jeff King97b2fa02018-11-12 09:55:58 -0500677 if (!has_object_file_with_flags(&ref->old_oid,
678 OBJECT_INFO_QUICK))
Junio C Hamano012a1bb2013-01-26 19:42:09 -0800679 continue;
Stefan Beller109cd762018-06-28 18:21:51 -0700680 o = parse_object(the_repository, &ref->old_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700681 if (!o)
682 continue;
683
684 /* We already have it -- which may mean that we were
685 * in sync with the other side at some time after
686 * that (it is OK if we guess wrong here).
687 */
688 if (o->type == OBJ_COMMIT) {
689 struct commit *commit = (struct commit *)o;
690 if (!cutoff || cutoff < commit->date)
691 cutoff = commit->date;
692 }
693 }
694
Jonathan Tan12f19a92018-10-03 16:04:52 -0700695 if (!args->deepen) {
696 for_each_ref(mark_complete_oid, NULL);
697 for_each_cached_alternate(NULL, mark_alternate_complete);
698 commit_list_sort_by_date(&complete);
699 if (cutoff)
700 mark_recent_complete_commits(args, cutoff);
701 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700702
Jonathan Tan12f19a92018-10-03 16:04:52 -0700703 /*
704 * Mark all complete remote refs as common refs.
705 * Don't mark them common yet; the server has to be told so first.
706 */
707 for (ref = *refs; ref; ref = ref->next) {
708 struct object *o = deref_tag(the_repository,
709 lookup_object(the_repository,
Jeff Kingd0229ab2019-06-20 03:41:14 -0400710 &ref->old_oid),
Jonathan Tan12f19a92018-10-03 16:04:52 -0700711 NULL, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700712
Jonathan Tan12f19a92018-10-03 16:04:52 -0700713 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
714 continue;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700715
Jonathan Tan12f19a92018-10-03 16:04:52 -0700716 negotiator->known_common(negotiator,
717 (struct commit *)o);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700718 }
719
Jonathan Tan34c29032018-06-06 13:47:07 -0700720 save_commit_buffer = old_save_commit_buffer;
721}
722
723/*
724 * Returns 1 if every object pointed to by the given remote refs is available
725 * locally and reachable from a local ref, and 0 otherwise.
726 */
727static int everything_local(struct fetch_pack_args *args,
728 struct ref **refs)
729{
730 struct ref *ref;
731 int retval;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700732
733 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000734 const struct object_id *remote = &ref->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700735 struct object *o;
736
Jeff Kingd0229ab2019-06-20 03:41:14 -0400737 o = lookup_object(the_repository, remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700738 if (!o || !(o->flags & COMPLETE)) {
739 retval = 0;
brian m. carlson1b283372017-05-01 02:28:54 +0000740 print_verbose(args, "want %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 continue;
743 }
brian m. carlson1b283372017-05-01 02:28:54 +0000744 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700745 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700746 }
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000747
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700748 return retval;
749}
750
751static int sideband_demux(int in, int out, void *data)
752{
753 int *xd = data;
Jeff King9ff18fa2016-02-24 02:44:58 -0500754 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700755
Jeff King9ff18fa2016-02-24 02:44:58 -0500756 ret = recv_sideband("fetch-pack", xd[0], out);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700757 close(out);
758 return ret;
759}
760
761static int get_pack(struct fetch_pack_args *args,
762 int xd[2], char **pack_lockfile)
763{
764 struct async demux;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700765 int do_keep = args->keep_pack;
Jeff King984a43b2015-09-24 17:07:54 -0400766 const char *cmd_name;
767 struct pack_header header;
768 int pass_header = 0;
René Scharfed3180272014-08-19 21:09:35 +0200769 struct child_process cmd = CHILD_PROCESS_INIT;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700770 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700771
772 memset(&demux, 0, sizeof(demux));
773 if (use_sideband) {
774 /* xd[] is talking with upload-pack; subprocess reads from
775 * xd[0], spits out band#2 to stderr, and feeds us band#1
776 * through demux->out.
777 */
778 demux.proc = sideband_demux;
779 demux.data = xd;
780 demux.out = -1;
Jeff Kingdf857572016-04-19 18:50:29 -0400781 demux.isolate_sigpipe = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700782 if (start_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700783 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700784 }
785 else
786 demux.out = xd[0];
787
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700788 if (!args->keep_pack && unpack_limit) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700789
790 if (read_pack_header(demux.out, &header))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700791 die(_("protocol error: bad pack header"));
Jeff King984a43b2015-09-24 17:07:54 -0400792 pass_header = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700793 if (ntohl(header.hdr_entries) < unpack_limit)
794 do_keep = 0;
795 else
796 do_keep = 1;
797 }
798
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700799 if (alternate_shallow_file) {
Jeff King984a43b2015-09-24 17:07:54 -0400800 argv_array_push(&cmd.args, "--shallow-file");
801 argv_array_push(&cmd.args, alternate_shallow_file);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700802 }
803
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000804 if (do_keep || args->from_promisor) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700805 if (pack_lockfile)
806 cmd.out = -1;
Jeff King984a43b2015-09-24 17:07:54 -0400807 cmd_name = "index-pack";
808 argv_array_push(&cmd.args, cmd_name);
809 argv_array_push(&cmd.args, "--stdin");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700810 if (!args->quiet && !args->no_progress)
Jeff King984a43b2015-09-24 17:07:54 -0400811 argv_array_push(&cmd.args, "-v");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700812 if (args->use_thin_pack)
Jeff King984a43b2015-09-24 17:07:54 -0400813 argv_array_push(&cmd.args, "--fix-thin");
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000814 if (do_keep && (args->lock_pack || unpack_limit)) {
René Scharfeda25bdb2017-04-18 17:57:42 -0400815 char hostname[HOST_NAME_MAX + 1];
David Turner5781a9a2017-04-18 17:57:43 -0400816 if (xgethostname(hostname, sizeof(hostname)))
Jeff King984a43b2015-09-24 17:07:54 -0400817 xsnprintf(hostname, sizeof(hostname), "localhost");
818 argv_array_pushf(&cmd.args,
819 "--keep=fetch-pack %"PRIuMAX " on %s",
820 (uintmax_t)getpid(), hostname);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700821 }
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700822 if (args->check_self_contained_and_connected)
Jeff King984a43b2015-09-24 17:07:54 -0400823 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000824 if (args->from_promisor)
825 argv_array_push(&cmd.args, "--promisor");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700826 }
827 else {
Jeff King984a43b2015-09-24 17:07:54 -0400828 cmd_name = "unpack-objects";
829 argv_array_push(&cmd.args, cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700830 if (args->quiet || args->no_progress)
Jeff King984a43b2015-09-24 17:07:54 -0400831 argv_array_push(&cmd.args, "-q");
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700832 args->check_self_contained_and_connected = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700833 }
Jeff King984a43b2015-09-24 17:07:54 -0400834
835 if (pass_header)
836 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
837 ntohl(header.hdr_version),
838 ntohl(header.hdr_entries));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700839 if (fetch_fsck_objects >= 0
840 ? fetch_fsck_objects
841 : transfer_fsck_objects >= 0
842 ? transfer_fsck_objects
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700843 : 0) {
844 if (args->from_promisor)
845 /*
846 * We cannot use --strict in index-pack because it
847 * checks both broken objects and links, but we only
848 * want to check for broken objects.
849 */
850 argv_array_push(&cmd.args, "--fsck-objects");
851 else
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +0000852 argv_array_pushf(&cmd.args, "--strict%s",
853 fsck_msg_types.buf);
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700854 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700855
856 cmd.in = demux.out;
857 cmd.git_cmd = 1;
858 if (start_command(&cmd))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700859 die(_("fetch-pack: unable to fork off %s"), cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700860 if (do_keep && pack_lockfile) {
861 *pack_lockfile = index_pack_lockfile(cmd.out);
862 close(cmd.out);
863 }
864
Jens Lindstrom37cb1dd2013-10-22 15:36:02 +0200865 if (!use_sideband)
866 /* Closed by start_command() */
867 xd[0] = -1;
868
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700869 ret = finish_command(&cmd);
870 if (!ret || (args->check_self_contained_and_connected && ret == 1))
871 args->self_contained_and_connected =
872 args->check_self_contained_and_connected &&
873 ret == 0;
874 else
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700875 die(_("%s failed"), cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700876 if (use_sideband && finish_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700877 die(_("error in sideband demultiplexer"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700878 return 0;
879}
880
Junio C Hamanof2db8542013-01-29 14:02:15 -0800881static int cmp_ref_by_name(const void *a_, const void *b_)
882{
883 const struct ref *a = *((const struct ref **)a_);
884 const struct ref *b = *((const struct ref **)b_);
885 return strcmp(a->name, b->name);
886}
887
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700888static struct ref *do_fetch_pack(struct fetch_pack_args *args,
889 int fd[2],
890 const struct ref *orig_ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800891 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +0700892 struct shallow_info *si,
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700893 char **pack_lockfile)
894{
895 struct ref *ref = copy_ref_list(orig_ref);
brian m. carlson1b283372017-05-01 02:28:54 +0000896 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700897 const char *agent_feature;
898 int agent_len;
Jonathan Tanec062832018-06-14 15:54:28 -0700899 struct fetch_negotiator negotiator;
Jonathan Tan42cc7482018-07-16 11:44:01 -0700900 fetch_negotiator_init(&negotiator, negotiation_algorithm);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700901
902 sort_ref_list(&ref, ref_compare_name);
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200903 QSORT(sought, nr_sought, cmp_ref_by_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700904
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700905 if ((agent_feature = server_feature_value("agent", &agent_len))) {
906 agent_supported = 1;
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700907 if (agent_len)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700908 print_verbose(args, _("Server version is %.*s"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700909 agent_len, agent_feature);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700910 }
Nguyễn Thái Ngọc Duy0e042972019-06-20 18:59:51 +0700911
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700912 if (server_supports("shallow"))
913 print_verbose(args, _("Server supports %s"), "shallow");
914 else if (args->depth > 0 || is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700915 die(_("Server does not support shallow clients"));
916 if (args->depth > 0 || args->deepen_since || args->deepen_not)
917 args->deepen = 1;
918 if (server_supports("multi_ack_detailed")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700919 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700920 multi_ack = 2;
921 if (server_supports("no-done")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700922 print_verbose(args, _("Server supports %s"), "no-done");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700923 if (args->stateless_rpc)
924 no_done = 1;
925 }
926 }
927 else if (server_supports("multi_ack")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700928 print_verbose(args, _("Server supports %s"), "multi_ack");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700929 multi_ack = 1;
930 }
931 if (server_supports("side-band-64k")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700932 print_verbose(args, _("Server supports %s"), "side-band-64k");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700933 use_sideband = 2;
934 }
935 else if (server_supports("side-band")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700936 print_verbose(args, _("Server supports %s"), "side-band");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700937 use_sideband = 1;
938 }
939 if (server_supports("allow-tip-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700940 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700941 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
942 }
943 if (server_supports("allow-reachable-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700944 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700945 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
946 }
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700947 if (server_supports("thin-pack"))
948 print_verbose(args, _("Server supports %s"), "thin-pack");
949 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700950 args->use_thin_pack = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700951 if (server_supports("no-progress"))
952 print_verbose(args, _("Server supports %s"), "no-progress");
953 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700954 args->no_progress = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700955 if (server_supports("include-tag"))
956 print_verbose(args, _("Server supports %s"), "include-tag");
957 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700958 args->include_tag = 0;
959 if (server_supports("ofs-delta"))
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700960 print_verbose(args, _("Server supports %s"), "ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700961 else
962 prefer_ofs_delta = 0;
963
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700964 if (server_supports("filter")) {
965 server_supports_filtering = 1;
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700966 print_verbose(args, _("Server supports %s"), "filter");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700967 } else if (args->filter_options.choice) {
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700968 warning("filtering not recognized by server, ignoring");
969 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700970
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700971 if (server_supports("deepen-since")) {
972 print_verbose(args, _("Server supports %s"), "deepen-since");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700973 deepen_since_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700974 } else if (args->deepen_since)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700975 die(_("Server does not support --shallow-since"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700976 if (server_supports("deepen-not")) {
977 print_verbose(args, _("Server supports %s"), "deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700978 deepen_not_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700979 } else if (args->deepen_not)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700980 die(_("Server does not support --shallow-exclude"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700981 if (server_supports("deepen-relative"))
982 print_verbose(args, _("Server supports %s"), "deepen-relative");
983 else if (args->deepen_relative)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700984 die(_("Server does not support --deepen"));
985
Jonathan Tan12f19a92018-10-03 16:04:52 -0700986 if (!args->no_dependents) {
987 mark_complete_and_common_ref(&negotiator, args, &ref);
988 filter_refs(args, &ref, sought, nr_sought);
989 if (everything_local(args, &ref)) {
990 packet_flush(fd[1]);
991 goto all_done;
992 }
993 } else {
994 filter_refs(args, &ref, sought, nr_sought);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700995 }
Jonathan Tanec062832018-06-14 15:54:28 -0700996 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700997 if (!args->keep_pack)
998 /* When cloning, it is not unusual to have
999 * no common commit.
1000 */
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001001 warning(_("no common commits"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001002
1003 if (args->stateless_rpc)
1004 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001005 if (args->deepen)
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +07001006 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1007 NULL);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001008 else if (si->nr_ours || si->nr_theirs)
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001009 alternate_shallow_file = setup_temporary_shallow(si->shallow);
Nguyễn Thái Ngọc Duy6da8bdc2013-08-26 09:17:26 +07001010 else
1011 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001012 if (get_pack(args, fd, pack_lockfile))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001013 die(_("git fetch-pack: fetch failed."));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001014
1015 all_done:
Jonathan Tanec062832018-06-14 15:54:28 -07001016 negotiator.release(&negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001017 return ref;
1018}
1019
Brandon Williamsf7e20502018-03-15 10:31:29 -07001020static void add_shallow_requests(struct strbuf *req_buf,
1021 const struct fetch_pack_args *args)
1022{
Junio C Hamano00624d62018-07-18 12:20:27 -07001023 if (is_repository_shallow(the_repository))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001024 write_shallow_commits(req_buf, 1, NULL);
1025 if (args->depth > 0)
1026 packet_buf_write(req_buf, "deepen %d", args->depth);
1027 if (args->deepen_since) {
1028 timestamp_t max_age = approxidate(args->deepen_since);
1029 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1030 }
1031 if (args->deepen_not) {
1032 int i;
1033 for (i = 0; i < args->deepen_not->nr; i++) {
1034 struct string_list_item *s = args->deepen_not->items + i;
1035 packet_buf_write(req_buf, "deepen-not %s", s->string);
1036 }
1037 }
Jonathan Tan5056cf42018-12-18 13:24:35 -08001038 if (args->deepen_relative)
1039 packet_buf_write(req_buf, "deepen-relative\n");
Brandon Williamsf7e20502018-03-15 10:31:29 -07001040}
1041
Jonathan Tan12f19a92018-10-03 16:04:52 -07001042static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf)
Brandon Williams685fbd32018-03-15 10:31:28 -07001043{
Brandon Williams73302052018-06-27 15:30:23 -07001044 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1045
Brandon Williams685fbd32018-03-15 10:31:28 -07001046 for ( ; wants ; wants = wants->next) {
1047 const struct object_id *remote = &wants->old_oid;
Brandon Williams685fbd32018-03-15 10:31:28 -07001048 struct object *o;
1049
1050 /*
1051 * If that object is complete (i.e. it is an ancestor of a
1052 * local ref), we tell them we have it but do not have to
1053 * tell them about its ancestors, which they already know
1054 * about.
1055 *
1056 * We use lookup_object here because we are only
1057 * interested in the case we *know* the object is
1058 * reachable and we have already scanned it.
Jonathan Tan12f19a92018-10-03 16:04:52 -07001059 *
1060 * Do this only if args->no_dependents is false (if it is true,
1061 * we cannot trust the object flags).
Brandon Williams685fbd32018-03-15 10:31:28 -07001062 */
Jonathan Tan12f19a92018-10-03 16:04:52 -07001063 if (!no_dependents &&
Jeff Kingd0229ab2019-06-20 03:41:14 -04001064 ((o = lookup_object(the_repository, remote)) != NULL) &&
Brandon Williams685fbd32018-03-15 10:31:28 -07001065 (o->flags & COMPLETE)) {
1066 continue;
1067 }
1068
Brandon Williams73302052018-06-27 15:30:23 -07001069 if (!use_ref_in_want || wants->exact_oid)
1070 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1071 else
1072 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
Brandon Williams685fbd32018-03-15 10:31:28 -07001073 }
1074}
1075
1076static void add_common(struct strbuf *req_buf, struct oidset *common)
1077{
1078 struct oidset_iter iter;
1079 const struct object_id *oid;
1080 oidset_iter_init(common, &iter);
1081
1082 while ((oid = oidset_iter_next(&iter))) {
1083 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1084 }
1085}
1086
Jonathan Tanec062832018-06-14 15:54:28 -07001087static int add_haves(struct fetch_negotiator *negotiator,
1088 struct strbuf *req_buf,
Jonathan Tand30fe892018-06-14 15:54:26 -07001089 int *haves_to_send, int *in_vain)
Brandon Williams685fbd32018-03-15 10:31:28 -07001090{
1091 int ret = 0;
1092 int haves_added = 0;
1093 const struct object_id *oid;
1094
Jonathan Tanec062832018-06-14 15:54:28 -07001095 while ((oid = negotiator->next(negotiator))) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001096 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1097 if (++haves_added >= *haves_to_send)
1098 break;
1099 }
1100
1101 *in_vain += haves_added;
1102 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1103 /* Send Done */
1104 packet_buf_write(req_buf, "done\n");
1105 ret = 1;
1106 }
1107
1108 /* Increase haves to send on next round */
1109 *haves_to_send = next_flush(1, *haves_to_send);
1110
1111 return ret;
1112}
1113
Jonathan Tanec062832018-06-14 15:54:28 -07001114static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
Jonathan Tand30fe892018-06-14 15:54:26 -07001115 const struct fetch_pack_args *args,
Brandon Williams685fbd32018-03-15 10:31:28 -07001116 const struct ref *wants, struct oidset *common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001117 int *haves_to_send, int *in_vain,
1118 int sideband_all)
Brandon Williams685fbd32018-03-15 10:31:28 -07001119{
1120 int ret = 0;
1121 struct strbuf req_buf = STRBUF_INIT;
1122
1123 if (server_supports_v2("fetch", 1))
1124 packet_buf_write(&req_buf, "command=fetch");
1125 if (server_supports_v2("agent", 0))
1126 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
Brandon Williams5e3548e2018-04-23 15:46:24 -07001127 if (args->server_options && args->server_options->nr &&
1128 server_supports_v2("server-option", 1)) {
1129 int i;
1130 for (i = 0; i < args->server_options->nr; i++)
Jonathan Tan5b204b72019-05-22 13:08:22 -07001131 packet_buf_write(&req_buf, "server-option=%s",
Brandon Williams5e3548e2018-04-23 15:46:24 -07001132 args->server_options->items[i].string);
1133 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001134
1135 packet_buf_delim(&req_buf);
1136 if (args->use_thin_pack)
1137 packet_buf_write(&req_buf, "thin-pack");
1138 if (args->no_progress)
1139 packet_buf_write(&req_buf, "no-progress");
1140 if (args->include_tag)
1141 packet_buf_write(&req_buf, "include-tag");
1142 if (prefer_ofs_delta)
1143 packet_buf_write(&req_buf, "ofs-delta");
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001144 if (sideband_all)
1145 packet_buf_write(&req_buf, "sideband-all");
Brandon Williams685fbd32018-03-15 10:31:28 -07001146
Brandon Williamsf7e20502018-03-15 10:31:29 -07001147 /* Add shallow-info and deepen request */
1148 if (server_supports_feature("fetch", "shallow", 0))
1149 add_shallow_requests(&req_buf, args);
Junio C Hamano00624d62018-07-18 12:20:27 -07001150 else if (is_repository_shallow(the_repository) || args->deepen)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001151 die(_("Server does not support shallow requests"));
1152
Jonathan Tanba957102018-05-03 16:46:56 -07001153 /* Add filter */
1154 if (server_supports_feature("fetch", "filter", 0) &&
1155 args->filter_options.choice) {
Josh Steadmon87c2d9d2019-01-07 16:17:09 -08001156 struct strbuf expanded_filter_spec = STRBUF_INIT;
Jonathan Tanba957102018-05-03 16:46:56 -07001157 print_verbose(args, _("Server supports filter"));
Josh Steadmon87c2d9d2019-01-07 16:17:09 -08001158 expand_list_objects_filter_spec(&args->filter_options,
1159 &expanded_filter_spec);
Jonathan Tanba957102018-05-03 16:46:56 -07001160 packet_buf_write(&req_buf, "filter %s",
Josh Steadmon87c2d9d2019-01-07 16:17:09 -08001161 expanded_filter_spec.buf);
1162 strbuf_release(&expanded_filter_spec);
Jonathan Tanba957102018-05-03 16:46:56 -07001163 } else if (args->filter_options.choice) {
1164 warning("filtering not recognized by server, ignoring");
1165 }
1166
Brandon Williams685fbd32018-03-15 10:31:28 -07001167 /* add wants */
Jonathan Tan12f19a92018-10-03 16:04:52 -07001168 add_wants(args->no_dependents, wants, &req_buf);
Brandon Williams685fbd32018-03-15 10:31:28 -07001169
Jonathan Tanba957102018-05-03 16:46:56 -07001170 if (args->no_dependents) {
1171 packet_buf_write(&req_buf, "done");
1172 ret = 1;
1173 } else {
1174 /* Add all of the common commits we've found in previous rounds */
1175 add_common(&req_buf, common);
Brandon Williams685fbd32018-03-15 10:31:28 -07001176
Jonathan Tanba957102018-05-03 16:46:56 -07001177 /* Add initial haves */
Jonathan Tanec062832018-06-14 15:54:28 -07001178 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
Jonathan Tanba957102018-05-03 16:46:56 -07001179 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001180
1181 /* Send request */
1182 packet_buf_flush(&req_buf);
Jeff King37c80012019-03-04 23:11:39 -05001183 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1184 die_errno(_("unable to write request to remote"));
Brandon Williams685fbd32018-03-15 10:31:28 -07001185
1186 strbuf_release(&req_buf);
1187 return ret;
1188}
1189
1190/*
1191 * Processes a section header in a server's response and checks if it matches
1192 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1193 * not consumed); if 0, the line will be consumed and the function will die if
1194 * the section header doesn't match what was expected.
1195 */
1196static int process_section_header(struct packet_reader *reader,
1197 const char *section, int peek)
1198{
1199 int ret;
1200
1201 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001202 die(_("error reading section header '%s'"), section);
Brandon Williams685fbd32018-03-15 10:31:28 -07001203
1204 ret = !strcmp(reader->line, section);
1205
1206 if (!peek) {
1207 if (!ret)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001208 die(_("expected '%s', received '%s'"),
Brandon Williams685fbd32018-03-15 10:31:28 -07001209 section, reader->line);
1210 packet_reader_read(reader);
1211 }
1212
1213 return ret;
1214}
1215
Jonathan Tanec062832018-06-14 15:54:28 -07001216static int process_acks(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -07001217 struct packet_reader *reader,
1218 struct oidset *common)
Brandon Williams685fbd32018-03-15 10:31:28 -07001219{
1220 /* received */
1221 int received_ready = 0;
1222 int received_ack = 0;
1223
1224 process_section_header(reader, "acknowledgments", 0);
1225 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1226 const char *arg;
1227
1228 if (!strcmp(reader->line, "NAK"))
1229 continue;
1230
1231 if (skip_prefix(reader->line, "ACK ", &arg)) {
1232 struct object_id oid;
1233 if (!get_oid_hex(arg, &oid)) {
1234 struct commit *commit;
1235 oidset_insert(common, &oid);
Stefan Bellerc1f5eb42018-06-28 18:21:59 -07001236 commit = lookup_commit(the_repository, &oid);
Jonathan Tanec062832018-06-14 15:54:28 -07001237 negotiator->ack(negotiator, commit);
Brandon Williams685fbd32018-03-15 10:31:28 -07001238 }
1239 continue;
1240 }
1241
1242 if (!strcmp(reader->line, "ready")) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001243 received_ready = 1;
1244 continue;
1245 }
1246
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001247 die(_("unexpected acknowledgment line: '%s'"), reader->line);
Brandon Williams685fbd32018-03-15 10:31:28 -07001248 }
1249
1250 if (reader->status != PACKET_READ_FLUSH &&
1251 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001252 die(_("error processing acks: %d"), reader->status);
Brandon Williams685fbd32018-03-15 10:31:28 -07001253
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001254 /*
1255 * If an "acknowledgments" section is sent, a packfile is sent if and
1256 * only if "ready" was sent in this section. The other sections
1257 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1258 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1259 * otherwise.
1260 */
1261 if (received_ready && reader->status != PACKET_READ_DELIM)
1262 die(_("expected packfile to be sent after 'ready'"));
1263 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1264 die(_("expected no other sections to be sent after no 'ready'"));
1265
Brandon Williams685fbd32018-03-15 10:31:28 -07001266 /* return 0 if no common, 1 if there are common, or 2 if ready */
1267 return received_ready ? 2 : (received_ack ? 1 : 0);
1268}
1269
Brandon Williamsf7e20502018-03-15 10:31:29 -07001270static void receive_shallow_info(struct fetch_pack_args *args,
Jonathan Tan13390782019-03-26 12:31:21 -07001271 struct packet_reader *reader,
1272 struct oid_array *shallows,
1273 struct shallow_info *si)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001274{
Jonathan Tan13390782019-03-26 12:31:21 -07001275 int unshallow_received = 0;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001276
Brandon Williamsf7e20502018-03-15 10:31:29 -07001277 process_section_header(reader, "shallow-info", 0);
1278 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1279 const char *arg;
1280 struct object_id oid;
1281
1282 if (skip_prefix(reader->line, "shallow ", &arg)) {
1283 if (get_oid_hex(arg, &oid))
1284 die(_("invalid shallow line: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001285 oid_array_append(shallows, &oid);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001286 continue;
1287 }
1288 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1289 if (get_oid_hex(arg, &oid))
1290 die(_("invalid unshallow line: %s"), reader->line);
Jeff Kingd0229ab2019-06-20 03:41:14 -04001291 if (!lookup_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001292 die(_("object not found: %s"), reader->line);
1293 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -07001294 if (!parse_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001295 die(_("error in object: %s"), reader->line);
1296 if (unregister_shallow(&oid))
1297 die(_("no shallow found: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001298 unshallow_received = 1;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001299 continue;
1300 }
1301 die(_("expected shallow/unshallow, got %s"), reader->line);
1302 }
1303
1304 if (reader->status != PACKET_READ_FLUSH &&
1305 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001306 die(_("error processing shallow info: %d"), reader->status);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001307
Jonathan Tan13390782019-03-26 12:31:21 -07001308 if (args->deepen || unshallow_received) {
1309 /*
1310 * Treat these as shallow lines caused by our depth settings.
1311 * In v0, these lines cannot cause refs to be rejected; do the
1312 * same.
1313 */
1314 int i;
1315
1316 for (i = 0; i < shallows->nr; i++)
1317 register_shallow(the_repository, &shallows->oid[i]);
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001318 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1319 NULL);
1320 args->deepen = 1;
Jonathan Tan13390782019-03-26 12:31:21 -07001321 } else if (shallows->nr) {
1322 /*
1323 * Treat these as shallow lines caused by the remote being
1324 * shallow. In v0, remote refs that reach these objects are
1325 * rejected (unless --update-shallow is set); do the same.
1326 */
1327 prepare_shallow_info(si, shallows);
1328 if (si->nr_ours || si->nr_theirs)
1329 alternate_shallow_file =
1330 setup_temporary_shallow(si->shallow);
1331 else
1332 alternate_shallow_file = NULL;
brian m. carlson380ebab2019-02-06 23:59:37 +00001333 } else {
1334 alternate_shallow_file = NULL;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001335 }
Brandon Williamsf7e20502018-03-15 10:31:29 -07001336}
1337
Jonathan Tanb7643002019-03-27 14:11:10 -07001338static int cmp_name_ref(const void *name, const void *ref)
1339{
1340 return strcmp(name, (*(struct ref **)ref)->name);
1341}
1342
Jonathan Tane2842b32018-08-01 13:13:20 -07001343static void receive_wanted_refs(struct packet_reader *reader,
1344 struct ref **sought, int nr_sought)
Brandon Williams73302052018-06-27 15:30:23 -07001345{
1346 process_section_header(reader, "wanted-refs", 0);
1347 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1348 struct object_id oid;
1349 const char *end;
Jonathan Tanb7643002019-03-27 14:11:10 -07001350 struct ref **found;
Brandon Williams73302052018-06-27 15:30:23 -07001351
1352 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001353 die(_("expected wanted-ref, got '%s'"), reader->line);
Brandon Williams73302052018-06-27 15:30:23 -07001354
Jonathan Tanb7643002019-03-27 14:11:10 -07001355 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1356 cmp_name_ref);
1357 if (!found)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001358 die(_("unexpected wanted-ref: '%s'"), reader->line);
Jonathan Tanb7643002019-03-27 14:11:10 -07001359 oidcpy(&(*found)->old_oid, &oid);
Brandon Williams73302052018-06-27 15:30:23 -07001360 }
1361
1362 if (reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001363 die(_("error processing wanted refs: %d"), reader->status);
Brandon Williams73302052018-06-27 15:30:23 -07001364}
1365
Brandon Williams685fbd32018-03-15 10:31:28 -07001366enum fetch_state {
1367 FETCH_CHECK_LOCAL = 0,
1368 FETCH_SEND_REQUEST,
1369 FETCH_PROCESS_ACKS,
1370 FETCH_GET_PACK,
1371 FETCH_DONE,
1372};
1373
1374static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1375 int fd[2],
1376 const struct ref *orig_ref,
1377 struct ref **sought, int nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001378 struct oid_array *shallows,
1379 struct shallow_info *si,
Brandon Williams685fbd32018-03-15 10:31:28 -07001380 char **pack_lockfile)
1381{
1382 struct ref *ref = copy_ref_list(orig_ref);
1383 enum fetch_state state = FETCH_CHECK_LOCAL;
1384 struct oidset common = OIDSET_INIT;
1385 struct packet_reader reader;
1386 int in_vain = 0;
1387 int haves_to_send = INITIAL_FLUSH;
Jonathan Tanec062832018-06-14 15:54:28 -07001388 struct fetch_negotiator negotiator;
Jonathan Tan42cc7482018-07-16 11:44:01 -07001389 fetch_negotiator_init(&negotiator, negotiation_algorithm);
Brandon Williams685fbd32018-03-15 10:31:28 -07001390 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -08001391 PACKET_READ_CHOMP_NEWLINE |
1392 PACKET_READ_DIE_ON_ERR_PACKET);
Jonathan Tan07c3c2a2019-01-16 11:28:15 -08001393 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1394 server_supports_feature("fetch", "sideband-all", 0)) {
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001395 reader.use_sideband = 1;
1396 reader.me = "fetch-pack";
1397 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001398
1399 while (state != FETCH_DONE) {
1400 switch (state) {
1401 case FETCH_CHECK_LOCAL:
1402 sort_ref_list(&ref, ref_compare_name);
1403 QSORT(sought, nr_sought, cmp_ref_by_name);
1404
1405 /* v2 supports these by default */
1406 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1407 use_sideband = 2;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001408 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1409 args->deepen = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001410
Brandon Williams685fbd32018-03-15 10:31:28 -07001411 /* Filter 'ref' by 'sought' and those that aren't local */
Jonathan Tan12f19a92018-10-03 16:04:52 -07001412 if (!args->no_dependents) {
1413 mark_complete_and_common_ref(&negotiator, args, &ref);
1414 filter_refs(args, &ref, sought, nr_sought);
1415 if (everything_local(args, &ref))
1416 state = FETCH_DONE;
1417 else
1418 state = FETCH_SEND_REQUEST;
Jonathan Tanaf1c90d2018-06-14 15:54:25 -07001419
Jonathan Tan12f19a92018-10-03 16:04:52 -07001420 mark_tips(&negotiator, args->negotiation_tips);
1421 for_each_cached_alternate(&negotiator,
1422 insert_one_alternate_object);
1423 } else {
1424 filter_refs(args, &ref, sought, nr_sought);
1425 state = FETCH_SEND_REQUEST;
1426 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001427 break;
1428 case FETCH_SEND_REQUEST:
Jonathan Tanec062832018-06-14 15:54:28 -07001429 if (send_fetch_request(&negotiator, fd[1], args, ref,
1430 &common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001431 &haves_to_send, &in_vain,
1432 reader.use_sideband))
Brandon Williams685fbd32018-03-15 10:31:28 -07001433 state = FETCH_GET_PACK;
1434 else
1435 state = FETCH_PROCESS_ACKS;
1436 break;
1437 case FETCH_PROCESS_ACKS:
1438 /* Process ACKs/NAKs */
Jonathan Tanec062832018-06-14 15:54:28 -07001439 switch (process_acks(&negotiator, &reader, &common)) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001440 case 2:
1441 state = FETCH_GET_PACK;
1442 break;
1443 case 1:
1444 in_vain = 0;
1445 /* fallthrough */
1446 default:
1447 state = FETCH_SEND_REQUEST;
1448 break;
1449 }
1450 break;
1451 case FETCH_GET_PACK:
Brandon Williamsf7e20502018-03-15 10:31:29 -07001452 /* Check for shallow-info section */
1453 if (process_section_header(&reader, "shallow-info", 1))
Jonathan Tan13390782019-03-26 12:31:21 -07001454 receive_shallow_info(args, &reader, shallows, si);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001455
Brandon Williams73302052018-06-27 15:30:23 -07001456 if (process_section_header(&reader, "wanted-refs", 1))
Jonathan Tane2842b32018-08-01 13:13:20 -07001457 receive_wanted_refs(&reader, sought, nr_sought);
Brandon Williams73302052018-06-27 15:30:23 -07001458
Brandon Williams685fbd32018-03-15 10:31:28 -07001459 /* get the pack */
1460 process_section_header(&reader, "packfile", 0);
1461 if (get_pack(args, fd, pack_lockfile))
1462 die(_("git fetch-pack: fetch failed."));
1463
1464 state = FETCH_DONE;
1465 break;
1466 case FETCH_DONE:
1467 continue;
1468 }
1469 }
1470
Jonathan Tanec062832018-06-14 15:54:28 -07001471 negotiator.release(&negotiator);
Brandon Williams685fbd32018-03-15 10:31:28 -07001472 oidset_clear(&common);
1473 return ref;
1474}
1475
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001476static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1477{
1478 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1479 const char *path;
1480
1481 if (git_config_pathname(&path, var, value))
1482 return 1;
1483 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1484 fsck_msg_types.len ? ',' : '=', path);
1485 free((char *)path);
1486 return 0;
1487 }
1488
1489 if (skip_prefix(var, "fetch.fsck.", &var)) {
1490 if (is_valid_msg_type(var, value))
1491 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1492 fsck_msg_types.len ? ',' : '=', var, value);
1493 else
1494 warning("Skipping unknown msg id '%s'", var);
1495 return 0;
1496 }
1497
1498 return git_default_config(var, value, cb);
1499}
1500
Tanay Abhraf44af512014-08-07 09:21:20 -07001501static void fetch_pack_config(void)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001502{
Tanay Abhraf44af512014-08-07 09:21:20 -07001503 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1504 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1505 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1506 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1507 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
Jonathan Tan42cc7482018-07-16 11:44:01 -07001508 git_config_get_string("fetch.negotiationalgorithm",
1509 &negotiation_algorithm);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001510
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001511 git_config(fetch_pack_config_cb, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001512}
1513
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001514static void fetch_pack_setup(void)
1515{
1516 static int did_setup;
1517 if (did_setup)
1518 return;
Tanay Abhraf44af512014-08-07 09:21:20 -07001519 fetch_pack_config();
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001520 if (0 <= transfer_unpack_limit)
1521 unpack_limit = transfer_unpack_limit;
1522 else if (0 <= fetch_unpack_limit)
1523 unpack_limit = fetch_unpack_limit;
1524 did_setup = 1;
1525}
1526
Junio C Hamanof2db8542013-01-29 14:02:15 -08001527static int remove_duplicates_in_refs(struct ref **ref, int nr)
1528{
1529 struct string_list names = STRING_LIST_INIT_NODUP;
1530 int src, dst;
1531
1532 for (src = dst = 0; src < nr; src++) {
1533 struct string_list_item *item;
1534 item = string_list_insert(&names, ref[src]->name);
1535 if (item->util)
1536 continue; /* already have it */
1537 item->util = ref[src];
1538 if (src != dst)
1539 ref[dst] = ref[src];
1540 dst++;
1541 }
1542 for (src = dst; src < nr; src++)
1543 ref[src] = NULL;
1544 string_list_clear(&names, 0);
1545 return dst;
1546}
1547
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001548static void update_shallow(struct fetch_pack_args *args,
Jonathan Tane2842b32018-08-01 13:13:20 -07001549 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001550 struct shallow_info *si)
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001551{
brian m. carlson910650d2017-03-31 01:40:00 +00001552 struct oid_array ref = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001553 int *status;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001554 int i;
1555
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001556 if (args->deepen && alternate_shallow_file) {
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001557 if (*alternate_shallow_file == '\0') { /* --unshallow */
Stefan Beller102de882018-05-17 15:51:51 -07001558 unlink_or_warn(git_path_shallow(the_repository));
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001559 rollback_lock_file(&shallow_lock);
1560 } else
1561 commit_lock_file(&shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001562 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001563 return;
1564 }
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001565
1566 if (!si->shallow || !si->shallow->nr)
1567 return;
1568
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001569 if (args->cloning) {
1570 /*
1571 * remote is shallow, but this is a clone, there are
1572 * no objects in repo to worry about. Accept any
1573 * shallow points that exist in the pack (iow in repo
1574 * after get_pack() and reprepare_packed_git())
1575 */
brian m. carlson910650d2017-03-31 01:40:00 +00001576 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001577 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001578 for (i = 0; i < si->shallow->nr; i++)
brian m. carlsonee3051b2017-03-26 16:01:37 +00001579 if (has_object_file(&oid[i]))
brian m. carlson910650d2017-03-31 01:40:00 +00001580 oid_array_append(&extra, &oid[i]);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001581 if (extra.nr) {
1582 setup_alternate_shallow(&shallow_lock,
1583 &alternate_shallow_file,
1584 &extra);
1585 commit_lock_file(&shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001586 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001587 }
brian m. carlson910650d2017-03-31 01:40:00 +00001588 oid_array_clear(&extra);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001589 return;
1590 }
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001591
1592 if (!si->nr_ours && !si->nr_theirs)
1593 return;
1594
1595 remove_nonexistent_theirs_shallow(si);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001596 if (!si->nr_ours && !si->nr_theirs)
1597 return;
Jonathan Tane2842b32018-08-01 13:13:20 -07001598 for (i = 0; i < nr_sought; i++)
1599 oid_array_append(&ref, &sought[i]->old_oid);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001600 si->ref = &ref;
1601
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001602 if (args->update_shallow) {
1603 /*
1604 * remote is also shallow, .git/shallow may be updated
1605 * so all refs can be accepted. Make sure we only add
1606 * shallow roots that are actually reachable from new
1607 * refs.
1608 */
brian m. carlson910650d2017-03-31 01:40:00 +00001609 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001610 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001611 assign_shallow_commits_to_refs(si, NULL, NULL);
1612 if (!si->nr_ours && !si->nr_theirs) {
brian m. carlson910650d2017-03-31 01:40:00 +00001613 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001614 return;
1615 }
1616 for (i = 0; i < si->nr_ours; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001617 oid_array_append(&extra, &oid[si->ours[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001618 for (i = 0; i < si->nr_theirs; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001619 oid_array_append(&extra, &oid[si->theirs[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001620 setup_alternate_shallow(&shallow_lock,
1621 &alternate_shallow_file,
1622 &extra);
1623 commit_lock_file(&shallow_lock);
brian m. carlson910650d2017-03-31 01:40:00 +00001624 oid_array_clear(&extra);
1625 oid_array_clear(&ref);
brian m. carlson23311f32019-02-04 00:06:50 +00001626 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001627 return;
1628 }
1629
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001630 /*
1631 * remote is also shallow, check what ref is safe to update
1632 * without updating .git/shallow
1633 */
Jonathan Tane2842b32018-08-01 13:13:20 -07001634 status = xcalloc(nr_sought, sizeof(*status));
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001635 assign_shallow_commits_to_refs(si, NULL, status);
1636 if (si->nr_ours || si->nr_theirs) {
Jonathan Tane2842b32018-08-01 13:13:20 -07001637 for (i = 0; i < nr_sought; i++)
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001638 if (status[i])
Jonathan Tane2842b32018-08-01 13:13:20 -07001639 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001640 }
1641 free(status);
brian m. carlson910650d2017-03-31 01:40:00 +00001642 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001643}
1644
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001645static int iterate_ref_map(void *cb_data, struct object_id *oid)
1646{
1647 struct ref **rm = cb_data;
1648 struct ref *ref = *rm;
1649
1650 if (!ref)
1651 return -1; /* end of the list */
1652 *rm = ref->next;
1653 oidcpy(oid, &ref->old_oid);
1654 return 0;
1655}
1656
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001657struct ref *fetch_pack(struct fetch_pack_args *args,
Jeff King0f804b02019-03-20 04:16:14 -04001658 int fd[],
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001659 const struct ref *ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -08001660 struct ref **sought, int nr_sought,
brian m. carlson910650d2017-03-31 01:40:00 +00001661 struct oid_array *shallow,
Brandon Williams685fbd32018-03-15 10:31:28 -07001662 char **pack_lockfile,
1663 enum protocol_version version)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001664{
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001665 struct ref *ref_cpy;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001666 struct shallow_info si;
Jonathan Tan13390782019-03-26 12:31:21 -07001667 struct oid_array shallows_scratch = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001668
1669 fetch_pack_setup();
Junio C Hamanof2db8542013-01-29 14:02:15 -08001670 if (nr_sought)
1671 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001672
Jonathan Tan4c7f9562018-10-03 16:04:53 -07001673 if (args->no_dependents && !args->filter_options.choice) {
1674 /*
1675 * The protocol does not support requesting that only the
1676 * wanted objects be sent, so approximate this by setting a
1677 * "blob:none" filter if no filter is already set. This works
1678 * for all object types: note that wanted blobs will still be
1679 * sent because they are directly specified as a "want".
1680 *
1681 * NEEDSWORK: Add an option in the protocol to request that
1682 * only the wanted objects be sent, and implement it.
1683 */
1684 parse_list_objects_filter(&args->filter_options, "blob:none");
1685 }
1686
Jonathan Tan01775652018-09-27 12:24:05 -07001687 if (version != protocol_v2 && !ref) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001688 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001689 die(_("no matching remote head"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001690 }
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001691 if (version == protocol_v2) {
1692 if (shallow->nr)
1693 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1694 memset(&si, 0, sizeof(si));
Brandon Williams685fbd32018-03-15 10:31:28 -07001695 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001696 &shallows_scratch, &si,
Brandon Williams685fbd32018-03-15 10:31:28 -07001697 pack_lockfile);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001698 } else {
1699 prepare_shallow_info(&si, shallow);
Brandon Williams685fbd32018-03-15 10:31:28 -07001700 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1701 &si, pack_lockfile);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001702 }
Stefan Bellera49d2832018-03-23 18:45:21 +01001703 reprepare_packed_git(the_repository);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001704
1705 if (!args->cloning && args->deepen) {
1706 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1707 struct ref *iterator = ref_cpy;
1708 opt.shallow_file = alternate_shallow_file;
1709 if (args->deepen)
1710 opt.is_deepening_fetch = 1;
1711 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1712 error(_("remote did not send all necessary objects"));
1713 free_refs(ref_cpy);
1714 ref_cpy = NULL;
1715 rollback_lock_file(&shallow_lock);
1716 goto cleanup;
1717 }
1718 args->connectivity_checked = 1;
1719 }
1720
Jonathan Tane2842b32018-08-01 13:13:20 -07001721 update_shallow(args, sought, nr_sought, &si);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001722cleanup:
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001723 clear_shallow_info(&si);
Jonathan Tan13390782019-03-26 12:31:21 -07001724 oid_array_clear(&shallows_scratch);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001725 return ref_cpy;
1726}
Matt McCutchene860d962017-02-22 11:01:22 -05001727
1728int report_unmatched_refs(struct ref **sought, int nr_sought)
1729{
1730 int i, ret = 0;
1731
1732 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -05001733 if (!sought[i])
Matt McCutchene860d962017-02-22 11:01:22 -05001734 continue;
Matt McCutchend56583d2017-02-22 11:05:57 -05001735 switch (sought[i]->match_status) {
1736 case REF_MATCHED:
1737 continue;
1738 case REF_NOT_MATCHED:
1739 error(_("no such remote ref %s"), sought[i]->name);
1740 break;
1741 case REF_UNADVERTISED_NOT_ALLOWED:
1742 error(_("Server does not allow request for unadvertised object %s"),
1743 sought[i]->name);
1744 break;
1745 }
Matt McCutchene860d962017-02-22 11:01:22 -05001746 ret = 1;
1747 }
1748 return ret;
1749}