blob: c135635e34a9000a948186c0e316865d14dc89c0 [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"
Jeff Kingfe299ec2020-03-30 10:03:46 -040018#include "oid-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"
Taylor Blau120ad2b2020-04-30 13:48:50 -060025#include "shallow.h"
Jonathan Tan9c1e6572021-05-04 14:16:01 -070026#include "commit-reach.h"
27#include "commit-graph.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070028
29static int transfer_unpack_limit = -1;
30static int fetch_unpack_limit = -1;
31static int unpack_limit = 100;
32static int prefer_ofs_delta = 1;
33static int no_done;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070034static int deepen_since_ok;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070035static int deepen_not_ok;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070036static int fetch_fsck_objects = -1;
37static int transfer_fsck_objects = -1;
38static int agent_supported;
Jeff Hostetler640d8b72017-12-08 15:58:40 +000039static int server_supports_filtering;
Josh Steadmon1e905bb2020-11-11 15:29:31 -080040static int advertise_sid;
Taylor Blaucac4b8e2020-04-30 13:48:57 -060041static struct shallow_lock shallow_lock;
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +070042static const char *alternate_shallow_file;
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +020043static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +000044static struct strbuf fsck_msg_types = STRBUF_INIT;
Jonathan Tandd4b7322020-06-10 13:57:23 -070045static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070046
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070047/* Remember to update object flag allocation in object.h */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070048#define COMPLETE (1U << 0)
Jonathan Tanec062832018-06-14 15:54:28 -070049#define ALTERNATE (1U << 1)
Jonathan Tan9c1e6572021-05-04 14:16:01 -070050#define COMMON (1U << 6)
51#define REACH_SCRATCH (1U << 7)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070052
53/*
54 * After sending this many "have"s if we do not get any new ACK , we
55 * give up traversing our history.
56 */
57#define MAX_IN_VAIN 256
58
Jonathan Tand30fe892018-06-14 15:54:26 -070059static int multi_ack, use_sideband;
Fredrik Medley7199c092015-05-21 22:23:38 +020060/* Allow specifying sha1 if it is a ref tip. */
61#define ALLOW_TIP_SHA1 01
Fredrik Medley68ee6282015-05-21 22:23:39 +020062/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
63#define ALLOW_REACHABLE_SHA1 02
Fredrik Medley7199c092015-05-21 22:23:38 +020064static unsigned int allow_unadvertised_object_request;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070065
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +070066__attribute__((format (printf, 2, 3)))
67static inline void print_verbose(const struct fetch_pack_args *args,
68 const char *fmt, ...)
69{
70 va_list params;
71
72 if (!args->verbose)
73 return;
74
75 va_start(params, fmt);
76 vfprintf(stderr, fmt, params);
77 va_end(params);
78 fputc('\n', stderr);
79}
80
Jeff King41a078c2017-02-08 15:53:03 -050081struct alternate_object_cache {
82 struct object **items;
83 size_t nr, alloc;
84};
85
Jeff Kingbdf42762018-10-08 11:09:23 -070086static void cache_one_alternate(const struct object_id *oid,
Jeff King41a078c2017-02-08 15:53:03 -050087 void *vcache)
88{
89 struct alternate_object_cache *cache = vcache;
Stefan Beller109cd762018-06-28 18:21:51 -070090 struct object *obj = parse_object(the_repository, oid);
Jeff King41a078c2017-02-08 15:53:03 -050091
92 if (!obj || (obj->flags & ALTERNATE))
93 return;
94
95 obj->flags |= ALTERNATE;
96 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
97 cache->items[cache->nr++] = obj;
98}
99
Jonathan Tanec062832018-06-14 15:54:28 -0700100static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
101 void (*cb)(struct fetch_negotiator *,
Jonathan Tand30fe892018-06-14 15:54:26 -0700102 struct object *))
Jeff King41a078c2017-02-08 15:53:03 -0500103{
104 static int initialized;
105 static struct alternate_object_cache cache;
106 size_t i;
107
108 if (!initialized) {
109 for_each_alternate_ref(cache_one_alternate, &cache);
110 initialized = 1;
111 }
112
113 for (i = 0; i < cache.nr; i++)
Jonathan Tanec062832018-06-14 15:54:28 -0700114 cb(negotiator, cache.items[i]);
Jeff King41a078c2017-02-08 15:53:03 -0500115}
116
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700117static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
118 int mark_tags_complete)
119{
120 enum object_type type;
121 struct object_info info = { .typep = &type };
122
123 while (1) {
124 if (oid_object_info_extended(the_repository, oid, &info,
125 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK))
126 return NULL;
127 if (type == OBJ_TAG) {
128 struct tag *tag = (struct tag *)
129 parse_object(the_repository, oid);
130
131 if (!tag->tagged)
132 return NULL;
133 if (mark_tags_complete)
134 tag->object.flags |= COMPLETE;
135 oid = &tag->tagged->oid;
136 } else {
137 break;
138 }
139 }
140 if (type == OBJ_COMMIT)
141 return (struct commit *) parse_object(the_repository, oid);
142 return NULL;
143}
144
Jonathan Tanec062832018-06-14 15:54:28 -0700145static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700146 const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700147{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700148 struct commit *c = deref_without_lazy_fetch(oid, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700149
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700150 if (c)
151 negotiator->add_tip(negotiator, c);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700152 return 0;
153}
154
Michael Haggertyb1b49c62015-05-25 18:39:18 +0000155static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
156 int flag, void *cb_data)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700157{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700158 return rev_list_insert_ref(cb_data, oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700159}
160
161enum ack_type {
162 NAK = 0,
163 ACK,
164 ACK_continue,
165 ACK_common,
166 ACK_ready
167};
168
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800169static void consume_shallow_list(struct fetch_pack_args *args,
170 struct packet_reader *reader)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700171{
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700172 if (args->stateless_rpc && args->deepen) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700173 /* If we sent a depth we will get back "duplicate"
174 * shallow and unshallow commands every time there
175 * is a block of have lines exchanged.
176 */
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800177 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
178 if (starts_with(reader->line, "shallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700179 continue;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800180 if (starts_with(reader->line, "unshallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700181 continue;
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700182 die(_("git fetch-pack: expected shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700183 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800184 if (reader->status != PACKET_READ_FLUSH)
185 die(_("git fetch-pack: expected a flush packet after shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700186 }
187}
188
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800189static enum ack_type get_ack(struct packet_reader *reader,
190 struct object_id *result_oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700191{
Jeff King74543a02013-02-20 15:02:57 -0500192 int len;
Jeff King82e56762014-06-18 15:56:03 -0400193 const char *arg;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700194
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800195 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
Jeff Kingbc9d4dc2018-02-08 13:47:49 -0500196 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800197 len = reader->pktlen;
198
199 if (!strcmp(reader->line, "NAK"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700200 return NAK;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800201 if (skip_prefix(reader->line, "ACK ", &arg)) {
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000202 const char *p;
203 if (!parse_oid_hex(arg, result_oid, &p)) {
204 len -= p - reader->line;
Jeff King82e56762014-06-18 15:56:03 -0400205 if (len < 1)
Jeff King030e9dd2013-02-20 15:00:28 -0500206 return ACK;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000207 if (strstr(p, "continue"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700208 return ACK_continue;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000209 if (strstr(p, "common"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700210 return ACK_common;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000211 if (strstr(p, "ready"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700212 return ACK_ready;
213 return ACK;
214 }
215 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800216 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700217}
218
219static void send_request(struct fetch_pack_args *args,
220 int fd, struct strbuf *buf)
221{
222 if (args->stateless_rpc) {
223 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
224 packet_flush(fd);
Jeff King37c80012019-03-04 23:11:39 -0500225 } else {
226 if (write_in_full(fd, buf->buf, buf->len) < 0)
227 die_errno(_("unable to write to remote"));
228 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700229}
230
Jonathan Tanec062832018-06-14 15:54:28 -0700231static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700232 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700233{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700234 rev_list_insert_ref(negotiator, &obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700235}
236
237#define INITIAL_FLUSH 16
238#define PIPESAFE_FLUSH 32
Jonathan Tanda470982016-07-18 15:21:38 -0700239#define LARGE_FLUSH 16384
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700240
Brandon Williams685fbd32018-03-15 10:31:28 -0700241static int next_flush(int stateless_rpc, int count)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700242{
Brandon Williams685fbd32018-03-15 10:31:28 -0700243 if (stateless_rpc) {
Jonathan Tanda470982016-07-18 15:21:38 -0700244 if (count < LARGE_FLUSH)
245 count <<= 1;
246 else
247 count = count * 11 / 10;
248 } else {
249 if (count < PIPESAFE_FLUSH)
250 count <<= 1;
251 else
252 count += PIPESAFE_FLUSH;
253 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700254 return count;
255}
256
Jonathan Tan3390e422018-07-02 15:39:44 -0700257static void mark_tips(struct fetch_negotiator *negotiator,
258 const struct oid_array *negotiation_tips)
259{
260 int i;
261
262 if (!negotiation_tips) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700263 for_each_rawref(rev_list_insert_ref_oid, negotiator);
Jonathan Tan3390e422018-07-02 15:39:44 -0700264 return;
265 }
266
267 for (i = 0; i < negotiation_tips->nr; i++)
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700268 rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]);
Jonathan Tan3390e422018-07-02 15:39:44 -0700269 return;
270}
271
Jonathan Tanec062832018-06-14 15:54:28 -0700272static int find_common(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700273 struct fetch_pack_args *args,
brian m. carlson1b283372017-05-01 02:28:54 +0000274 int fd[2], struct object_id *result_oid,
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700275 struct ref *refs)
276{
277 int fetching;
278 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
brian m. carlson1b283372017-05-01 02:28:54 +0000279 const struct object_id *oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700280 unsigned in_vain = 0;
281 int got_continue = 0;
282 int got_ready = 0;
283 struct strbuf req_buf = STRBUF_INIT;
284 size_t state_len = 0;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800285 struct packet_reader reader;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700286
287 if (args->stateless_rpc && multi_ack == 1)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700288 die(_("--stateless-rpc requires multi_ack_detailed"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700289
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800290 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -0800291 PACKET_READ_CHOMP_NEWLINE |
292 PACKET_READ_DIE_ON_ERR_PACKET);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800293
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700294 mark_tips(negotiator, args->negotiation_tips);
295 for_each_cached_alternate(negotiator, insert_one_alternate_object);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700296
297 fetching = 0;
298 for ( ; refs ; refs = refs->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000299 struct object_id *remote = &refs->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700300 const char *remote_hex;
301 struct object *o;
302
303 /*
304 * If that object is complete (i.e. it is an ancestor of a
305 * local ref), we tell them we have it but do not have to
306 * tell them about its ancestors, which they already know
307 * about.
308 *
309 * We use lookup_object here because we are only
310 * interested in the case we *know* the object is
311 * reachable and we have already scanned it.
312 */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700313 if (((o = lookup_object(the_repository, remote)) != NULL) &&
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700314 (o->flags & COMPLETE)) {
315 continue;
316 }
317
brian m. carlson1b283372017-05-01 02:28:54 +0000318 remote_hex = oid_to_hex(remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700319 if (!fetching) {
320 struct strbuf c = STRBUF_INIT;
321 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
322 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
323 if (no_done) strbuf_addstr(&c, " no-done");
324 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
325 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700326 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700327 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
328 if (args->no_progress) strbuf_addstr(&c, " no-progress");
329 if (args->include_tag) strbuf_addstr(&c, " include-tag");
330 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700331 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700332 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700333 if (agent_supported) strbuf_addf(&c, " agent=%s",
334 git_user_agent_sanitized());
Josh Steadmon1e905bb2020-11-11 15:29:31 -0800335 if (advertise_sid)
336 strbuf_addf(&c, " session-id=%s", trace2_session_id());
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000337 if (args->filter_options.choice)
338 strbuf_addstr(&c, " filter");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700339 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
340 strbuf_release(&c);
341 } else
342 packet_buf_write(&req_buf, "want %s\n", remote_hex);
343 fetching++;
344 }
345
346 if (!fetching) {
347 strbuf_release(&req_buf);
348 packet_flush(fd[1]);
349 return 1;
350 }
351
Stefan Bellerc8813482018-05-17 15:51:46 -0700352 if (is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700353 write_shallow_commits(&req_buf, 1, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700354 if (args->depth > 0)
355 packet_buf_write(&req_buf, "deepen %d", args->depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700356 if (args->deepen_since) {
Johannes Schindelindddbad72017-04-26 21:29:31 +0200357 timestamp_t max_age = approxidate(args->deepen_since);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200358 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700359 }
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700360 if (args->deepen_not) {
361 int i;
362 for (i = 0; i < args->deepen_not->nr; i++) {
363 struct string_list_item *s = args->deepen_not->items + i;
364 packet_buf_write(&req_buf, "deepen-not %s", s->string);
365 }
366 }
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800367 if (server_supports_filtering && args->filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700368 const char *spec =
369 expand_list_objects_filter_spec(&args->filter_options);
370 packet_buf_write(&req_buf, "filter %s", spec);
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800371 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700372 packet_buf_flush(&req_buf);
373 state_len = req_buf.len;
374
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700375 if (args->deepen) {
Jeff Kingae021d82014-06-18 15:47:50 -0400376 const char *arg;
brian m. carlson1b283372017-05-01 02:28:54 +0000377 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700378
379 send_request(args, fd[1], &req_buf);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800380 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
381 if (skip_prefix(reader.line, "shallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000382 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800383 die(_("invalid shallow line: %s"), reader.line);
Stefan Beller19143f12018-05-17 15:51:44 -0700384 register_shallow(the_repository, &oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700385 continue;
386 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800387 if (skip_prefix(reader.line, "unshallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000388 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800389 die(_("invalid unshallow line: %s"), reader.line);
Jeff Kingd0229ab2019-06-20 03:41:14 -0400390 if (!lookup_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800391 die(_("object not found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700392 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -0700393 if (!parse_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800394 die(_("error in object: %s"), reader.line);
brian m. carlsone92b8482017-05-06 22:10:06 +0000395 if (unregister_shallow(&oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800396 die(_("no shallow found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700397 continue;
398 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800399 die(_("expected shallow/unshallow, got %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700400 }
401 } else if (!args->stateless_rpc)
402 send_request(args, fd[1], &req_buf);
403
404 if (!args->stateless_rpc) {
405 /* If we aren't using the stateless-rpc interface
406 * we don't need to retain the headers.
407 */
408 strbuf_setlen(&req_buf, 0);
409 state_len = 0;
410 }
411
Josh Steadmon5fc31182019-10-02 16:49:28 -0700412 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700413 flushes = 0;
414 retval = -1;
Jonathan Tanec062832018-06-14 15:54:28 -0700415 while ((oid = negotiator->next(negotiator))) {
brian m. carlson1b283372017-05-01 02:28:54 +0000416 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
417 print_verbose(args, "have %s", oid_to_hex(oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700418 in_vain++;
419 if (flush_at <= ++count) {
420 int ack;
421
422 packet_buf_flush(&req_buf);
423 send_request(args, fd[1], &req_buf);
424 strbuf_setlen(&req_buf, state_len);
425 flushes++;
Brandon Williams685fbd32018-03-15 10:31:28 -0700426 flush_at = next_flush(args->stateless_rpc, count);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700427
428 /*
429 * We keep one window "ahead" of the other side, and
430 * will wait for an ACK only on the next one
431 */
432 if (!args->stateless_rpc && count == INITIAL_FLUSH)
433 continue;
434
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800435 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700436 do {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800437 ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700438 if (ack)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700439 print_verbose(args, _("got %s %d %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000440 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700441 switch (ack) {
442 case ACK:
443 flushes = 0;
444 multi_ack = 0;
445 retval = 0;
446 goto done;
447 case ACK_common:
448 case ACK_ready:
449 case ACK_continue: {
450 struct commit *commit =
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700451 lookup_commit(the_repository,
452 result_oid);
Jonathan Tand093bc72018-06-14 15:54:27 -0700453 int was_common;
Junio C Hamano3a2a1dc2018-08-02 15:30:42 -0700454
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700455 if (!commit)
brian m. carlson1b283372017-05-01 02:28:54 +0000456 die(_("invalid commit %s"), oid_to_hex(result_oid));
Jonathan Tanec062832018-06-14 15:54:28 -0700457 was_common = negotiator->ack(negotiator, commit);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700458 if (args->stateless_rpc
459 && ack == ACK_common
Jonathan Tand093bc72018-06-14 15:54:27 -0700460 && !was_common) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700461 /* We need to replay the have for this object
462 * on the next RPC request so the peer knows
463 * it is in common with us.
464 */
brian m. carlson1b283372017-05-01 02:28:54 +0000465 const char *hex = oid_to_hex(result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700466 packet_buf_write(&req_buf, "have %s\n", hex);
467 state_len = req_buf.len;
Jonathan Tan06b3d382016-09-23 10:41:35 -0700468 /*
469 * Reset in_vain because an ack
470 * for this commit has not been
471 * seen.
472 */
473 in_vain = 0;
474 } else if (!args->stateless_rpc
475 || ack != ACK_common)
476 in_vain = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700477 retval = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700478 got_continue = 1;
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700479 if (ack == ACK_ready)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700480 got_ready = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700481 break;
482 }
483 }
484 } while (ack);
485 flushes--;
486 if (got_continue && MAX_IN_VAIN < in_vain) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700487 print_verbose(args, _("giving up"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700488 break; /* give up */
489 }
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700490 if (got_ready)
491 break;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700492 }
493 }
494done:
Josh Steadmon5fc31182019-10-02 16:49:28 -0700495 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700496 if (!got_ready || !no_done) {
497 packet_buf_write(&req_buf, "done\n");
498 send_request(args, fd[1], &req_buf);
499 }
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700500 print_verbose(args, _("done"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700501 if (retval != 0) {
502 multi_ack = 0;
503 flushes++;
504 }
505 strbuf_release(&req_buf);
506
Nguyễn Thái Ngọc Duyff62eca2014-02-06 22:10:39 +0700507 if (!got_ready || !no_done)
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800508 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700509 while (flushes || multi_ack) {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800510 int ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700511 if (ack) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700512 print_verbose(args, _("got %s (%d) %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000513 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700514 if (ack == ACK)
515 return 0;
516 multi_ack = 1;
517 continue;
518 }
519 flushes--;
520 }
521 /* it is no error to fetch into a completely empty repo */
522 return count ? retval : 0;
523}
524
525static struct commit_list *complete;
526
brian m. carlson1b283372017-05-01 02:28:54 +0000527static int mark_complete(const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700528{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700529 struct commit *commit = deref_without_lazy_fetch(oid, 1);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700530
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700531 if (commit && !(commit->object.flags & COMPLETE)) {
532 commit->object.flags |= COMPLETE;
533 commit_list_insert(commit, &complete);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700534 }
535 return 0;
536}
537
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000538static int mark_complete_oid(const char *refname, const struct object_id *oid,
539 int flag, void *cb_data)
540{
brian m. carlson1b283372017-05-01 02:28:54 +0000541 return mark_complete(oid);
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000542}
543
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700544static void mark_recent_complete_commits(struct fetch_pack_args *args,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200545 timestamp_t cutoff)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700546{
547 while (complete && cutoff <= complete->item->date) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700548 print_verbose(args, _("Marking %s as complete"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700549 oid_to_hex(&complete->item->object.oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700550 pop_most_recent_commit(&complete, COMPLETE);
551 }
552}
553
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700554static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
555{
556 for (; refs; refs = refs->next)
557 oidset_insert(oids, &refs->old_oid);
558}
559
René Scharfebf732822018-10-04 17:09:06 +0200560static int is_unmatched_ref(const struct ref *ref)
561{
562 struct object_id oid;
563 const char *p;
564 return ref->match_status == REF_NOT_MATCHED &&
565 !parse_oid_hex(ref->name, &oid, &p) &&
566 *p == '\0' &&
567 oideq(&oid, &ref->old_oid);
568}
569
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700570static void filter_refs(struct fetch_pack_args *args,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800571 struct ref **refs,
572 struct ref **sought, int nr_sought)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700573{
574 struct ref *newlist = NULL;
575 struct ref **newtail = &newlist;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700576 struct ref *unmatched = NULL;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700577 struct ref *ref, *next;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700578 struct oidset tip_oids = OIDSET_INIT;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800579 int i;
René Scharfe22a16462018-10-04 17:09:39 +0200580 int strict = !(allow_unadvertised_object_request &
581 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700582
Junio C Hamanof2db8542013-01-29 14:02:15 -0800583 i = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700584 for (ref = *refs; ref; ref = next) {
585 int keep = 0;
586 next = ref->next;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800587
René Scharfe50e19a82014-06-06 19:24:48 +0200588 if (starts_with(ref->name, "refs/") &&
Jeff King34066f02019-04-13 01:57:37 -0400589 check_refname_format(ref->name, 0)) {
590 /*
591 * trash or a peeled value; do not even add it to
592 * unmatched list
593 */
594 free_one_ref(ref);
595 continue;
596 } else {
Junio C Hamanof2db8542013-01-29 14:02:15 -0800597 while (i < nr_sought) {
598 int cmp = strcmp(ref->name, sought[i]->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700599 if (cmp < 0)
600 break; /* definitely do not have it */
601 else if (cmp == 0) {
602 keep = 1; /* definitely have it */
Matt McCutchend56583d2017-02-22 11:05:57 -0500603 sought[i]->match_status = REF_MATCHED;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700604 }
Junio C Hamanof2db8542013-01-29 14:02:15 -0800605 i++;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700606 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700607
Jeff Kinge9502c02018-06-11 01:53:57 -0400608 if (!keep && args->fetch_all &&
609 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
610 keep = 1;
611 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700612
613 if (keep) {
614 *newtail = ref;
615 ref->next = NULL;
616 newtail = &ref->next;
617 } else {
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700618 ref->next = unmatched;
619 unmatched = ref;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700620 }
621 }
622
René Scharfe22a16462018-10-04 17:09:39 +0200623 if (strict) {
624 for (i = 0; i < nr_sought; i++) {
625 ref = sought[i];
626 if (!is_unmatched_ref(ref))
627 continue;
628
629 add_refs_to_oidset(&tip_oids, unmatched);
630 add_refs_to_oidset(&tip_oids, newlist);
631 break;
632 }
633 }
634
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800635 /* Append unmatched requests to the list */
Matt McCutchend56583d2017-02-22 11:05:57 -0500636 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500637 ref = sought[i];
René Scharfebf732822018-10-04 17:09:06 +0200638 if (!is_unmatched_ref(ref))
Matt McCutchend56583d2017-02-22 11:05:57 -0500639 continue;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800640
René Scharfe22a16462018-10-04 17:09:39 +0200641 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500642 ref->match_status = REF_MATCHED;
Jeff Kingc3c17bf2015-03-19 16:37:09 -0400643 *newtail = copy_ref(ref);
644 newtail = &(*newtail)->next;
Matt McCutchend56583d2017-02-22 11:05:57 -0500645 } else {
646 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800647 }
648 }
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700649
650 oidset_clear(&tip_oids);
Jeff King259eddd2019-04-13 01:54:09 -0400651 free_refs(unmatched);
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700652
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700653 *refs = newlist;
654}
655
Jonathan Tanec062832018-06-14 15:54:28 -0700656static void mark_alternate_complete(struct fetch_negotiator *unused,
Jonathan Tand30fe892018-06-14 15:54:26 -0700657 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700658{
brian m. carlson1b283372017-05-01 02:28:54 +0000659 mark_complete(&obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700660}
661
Takuto Ikuta024aa462018-03-14 15:32:42 +0900662struct loose_object_iter {
663 struct oidset *loose_object_set;
664 struct ref *refs;
665};
666
667/*
Jonathan Tan34c29032018-06-06 13:47:07 -0700668 * Mark recent commits available locally and reachable from a local ref as
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700669 * COMPLETE.
Jonathan Tan34c29032018-06-06 13:47:07 -0700670 *
671 * The cutoff time for recency is determined by this heuristic: it is the
672 * earliest commit time of the objects in refs that are commits and that we know
673 * the commit time of.
674 */
Jonathan Tanec062832018-06-14 15:54:28 -0700675static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700676 struct fetch_pack_args *args,
Jonathan Tan34c29032018-06-06 13:47:07 -0700677 struct ref **refs)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700678{
679 struct ref *ref;
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000680 int old_save_commit_buffer = save_commit_buffer;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200681 timestamp_t cutoff = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700682
683 save_commit_buffer = 0;
684
Erik Chen9e5afdf2019-11-19 23:02:09 +0000685 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700686 for (ref = *refs; ref; ref = ref->next) {
687 struct object *o;
688
Jeff King97b2fa02018-11-12 09:55:58 -0500689 if (!has_object_file_with_flags(&ref->old_oid,
Jonathan Tan6462d5e2019-11-05 10:56:19 -0800690 OBJECT_INFO_QUICK |
691 OBJECT_INFO_SKIP_FETCH_OBJECT))
Junio C Hamano012a1bb2013-01-26 19:42:09 -0800692 continue;
Stefan Beller109cd762018-06-28 18:21:51 -0700693 o = parse_object(the_repository, &ref->old_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700694 if (!o)
695 continue;
696
Erik Chen9e5afdf2019-11-19 23:02:09 +0000697 /*
698 * We already have it -- which may mean that we were
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700699 * in sync with the other side at some time after
700 * that (it is OK if we guess wrong here).
701 */
702 if (o->type == OBJ_COMMIT) {
703 struct commit *commit = (struct commit *)o;
704 if (!cutoff || cutoff < commit->date)
705 cutoff = commit->date;
706 }
707 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000708 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700709
Erik Chen9e5afdf2019-11-19 23:02:09 +0000710 /*
711 * This block marks all local refs as COMPLETE, and then recursively marks all
712 * parents of those refs as COMPLETE.
713 */
714 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700715 if (!args->deepen) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700716 for_each_rawref(mark_complete_oid, NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700717 for_each_cached_alternate(NULL, mark_alternate_complete);
718 commit_list_sort_by_date(&complete);
719 if (cutoff)
720 mark_recent_complete_commits(args, cutoff);
721 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000722 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700723
Jonathan Tan12f19a92018-10-03 16:04:52 -0700724 /*
725 * Mark all complete remote refs as common refs.
726 * Don't mark them common yet; the server has to be told so first.
727 */
Erik Chen9e5afdf2019-11-19 23:02:09 +0000728 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700729 for (ref = *refs; ref; ref = ref->next) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700730 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700731
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700732 if (!c || !(c->object.flags & COMPLETE))
Jonathan Tan12f19a92018-10-03 16:04:52 -0700733 continue;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700734
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700735 negotiator->known_common(negotiator, c);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700736 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000737 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700738
Jonathan Tan34c29032018-06-06 13:47:07 -0700739 save_commit_buffer = old_save_commit_buffer;
740}
741
742/*
743 * Returns 1 if every object pointed to by the given remote refs is available
744 * locally and reachable from a local ref, and 0 otherwise.
745 */
746static int everything_local(struct fetch_pack_args *args,
747 struct ref **refs)
748{
749 struct ref *ref;
750 int retval;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700751
752 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000753 const struct object_id *remote = &ref->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700754 struct object *o;
755
Jeff Kingd0229ab2019-06-20 03:41:14 -0400756 o = lookup_object(the_repository, remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700757 if (!o || !(o->flags & COMPLETE)) {
758 retval = 0;
brian m. carlson1b283372017-05-01 02:28:54 +0000759 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700760 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700761 continue;
762 }
brian m. carlson1b283372017-05-01 02:28:54 +0000763 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700764 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700765 }
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000766
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700767 return retval;
768}
769
770static int sideband_demux(int in, int out, void *data)
771{
772 int *xd = data;
Jeff King9ff18fa2016-02-24 02:44:58 -0500773 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700774
Jeff King9ff18fa2016-02-24 02:44:58 -0500775 ret = recv_sideband("fetch-pack", xd[0], out);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700776 close(out);
777 return ret;
778}
779
Christian Couder9d7fa3b2021-01-12 09:21:58 +0100780static void create_promisor_file(const char *keep_name,
781 struct ref **sought, int nr_sought)
Jonathan Tan5374a292019-10-14 17:12:31 -0700782{
783 struct strbuf promisor_name = STRBUF_INIT;
784 int suffix_stripped;
Jonathan Tan5374a292019-10-14 17:12:31 -0700785
786 strbuf_addstr(&promisor_name, keep_name);
787 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
788 if (!suffix_stripped)
789 BUG("name of pack lockfile should end with .keep (was '%s')",
790 keep_name);
791 strbuf_addstr(&promisor_name, ".promisor");
792
Christian Couder33add2a2021-01-12 09:21:59 +0100793 write_promisor_file(promisor_name.buf, sought, nr_sought);
Jonathan Tan5374a292019-10-14 17:12:31 -0700794
795 strbuf_release(&promisor_name);
796}
797
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800798static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids)
799{
800 int len = the_hash_algo->hexsz + 1; /* hash + NL */
801
802 do {
803 char hex_hash[GIT_MAX_HEXSZ + 1];
804 int read_len = read_in_full(fd, hex_hash, len);
805 struct object_id oid;
806 const char *end;
807
808 if (!read_len)
809 return;
810 if (read_len != len)
811 die("invalid length read %d", read_len);
812 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
813 die("invalid hash");
814 oidset_insert(gitmodules_oids, &oid);
815 } while (1);
816}
817
Jonathan Tanece9aea2020-08-17 12:48:19 -0700818/*
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800819 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
820 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
821 * stored there. (It must be freed by the caller.)
Jonathan Tanece9aea2020-08-17 12:48:19 -0700822 */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700823static int get_pack(struct fetch_pack_args *args,
Jonathan Tan9da69a62020-06-10 13:57:22 -0700824 int xd[2], struct string_list *pack_lockfiles,
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800825 struct strvec *index_pack_args,
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800826 struct ref **sought, int nr_sought,
827 struct oidset *gitmodules_oids)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700828{
829 struct async demux;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700830 int do_keep = args->keep_pack;
Jeff King984a43b2015-09-24 17:07:54 -0400831 const char *cmd_name;
832 struct pack_header header;
833 int pass_header = 0;
René Scharfed3180272014-08-19 21:09:35 +0200834 struct child_process cmd = CHILD_PROCESS_INIT;
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800835 int fsck_objects = 0;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700836 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700837
838 memset(&demux, 0, sizeof(demux));
839 if (use_sideband) {
840 /* xd[] is talking with upload-pack; subprocess reads from
841 * xd[0], spits out band#2 to stderr, and feeds us band#1
842 * through demux->out.
843 */
844 demux.proc = sideband_demux;
845 demux.data = xd;
846 demux.out = -1;
Jeff Kingdf857572016-04-19 18:50:29 -0400847 demux.isolate_sigpipe = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700848 if (start_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700849 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700850 }
851 else
852 demux.out = xd[0];
853
Jonathan Tan2aec3bc2021-03-04 17:16:20 -0800854 if (!args->keep_pack && unpack_limit && !index_pack_args) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700855
856 if (read_pack_header(demux.out, &header))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700857 die(_("protocol error: bad pack header"));
Jeff King984a43b2015-09-24 17:07:54 -0400858 pass_header = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700859 if (ntohl(header.hdr_entries) < unpack_limit)
860 do_keep = 0;
861 else
862 do_keep = 1;
863 }
864
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700865 if (alternate_shallow_file) {
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400866 strvec_push(&cmd.args, "--shallow-file");
867 strvec_push(&cmd.args, alternate_shallow_file);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700868 }
869
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800870 if (fetch_fsck_objects >= 0
871 ? fetch_fsck_objects
872 : transfer_fsck_objects >= 0
873 ? transfer_fsck_objects
874 : 0)
875 fsck_objects = 1;
876
877 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) {
878 if (pack_lockfiles || fsck_objects)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700879 cmd.out = -1;
Jeff King984a43b2015-09-24 17:07:54 -0400880 cmd_name = "index-pack";
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400881 strvec_push(&cmd.args, cmd_name);
882 strvec_push(&cmd.args, "--stdin");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700883 if (!args->quiet && !args->no_progress)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400884 strvec_push(&cmd.args, "-v");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700885 if (args->use_thin_pack)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400886 strvec_push(&cmd.args, "--fix-thin");
Jonathan Tan2aec3bc2021-03-04 17:16:20 -0800887 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit)) {
René Scharfeda25bdb2017-04-18 17:57:42 -0400888 char hostname[HOST_NAME_MAX + 1];
David Turner5781a9a2017-04-18 17:57:43 -0400889 if (xgethostname(hostname, sizeof(hostname)))
Jeff King984a43b2015-09-24 17:07:54 -0400890 xsnprintf(hostname, sizeof(hostname), "localhost");
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400891 strvec_pushf(&cmd.args,
Jeff Kingf6d89422020-07-28 16:26:31 -0400892 "--keep=fetch-pack %"PRIuMAX " on %s",
893 (uintmax_t)getpid(), hostname);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700894 }
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800895 if (!index_pack_args && args->check_self_contained_and_connected)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400896 strvec_push(&cmd.args, "--check-self-contained-and-connected");
Jonathan Tandd4b7322020-06-10 13:57:23 -0700897 else
898 /*
899 * We cannot perform any connectivity checks because
900 * not all packs have been downloaded; let the caller
901 * have this responsibility.
902 */
903 args->check_self_contained_and_connected = 0;
Jonathan Tan1b03df52020-08-20 10:51:16 -0700904
905 if (args->from_promisor)
906 /*
Christian Couder9d7fa3b2021-01-12 09:21:58 +0100907 * create_promisor_file() may be called afterwards but
Jonathan Tan1b03df52020-08-20 10:51:16 -0700908 * we still need index-pack to know that this is a
909 * promisor pack. For example, if transfer.fsckobjects
910 * is true, index-pack needs to know that .gitmodules
911 * is a promisor object (so that it won't complain if
912 * it is missing).
913 */
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400914 strvec_push(&cmd.args, "--promisor");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700915 }
916 else {
Jeff King984a43b2015-09-24 17:07:54 -0400917 cmd_name = "unpack-objects";
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400918 strvec_push(&cmd.args, cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700919 if (args->quiet || args->no_progress)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400920 strvec_push(&cmd.args, "-q");
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700921 args->check_self_contained_and_connected = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700922 }
Jeff King984a43b2015-09-24 17:07:54 -0400923
924 if (pass_header)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400925 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
Jeff Kingf6d89422020-07-28 16:26:31 -0400926 ntohl(header.hdr_version),
Jeff King984a43b2015-09-24 17:07:54 -0400927 ntohl(header.hdr_entries));
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800928 if (fsck_objects) {
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800929 if (args->from_promisor || index_pack_args)
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700930 /*
931 * We cannot use --strict in index-pack because it
932 * checks both broken objects and links, but we only
933 * want to check for broken objects.
934 */
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400935 strvec_push(&cmd.args, "--fsck-objects");
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700936 else
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400937 strvec_pushf(&cmd.args, "--strict%s",
Jeff Kingf6d89422020-07-28 16:26:31 -0400938 fsck_msg_types.buf);
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700939 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700940
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800941 if (index_pack_args) {
942 int i;
943
944 for (i = 0; i < cmd.args.nr; i++)
945 strvec_push(index_pack_args, cmd.args.v[i]);
946 }
947
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700948 cmd.in = demux.out;
949 cmd.git_cmd = 1;
950 if (start_command(&cmd))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700951 die(_("fetch-pack: unable to fork off %s"), cmd_name);
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800952 if (do_keep && (pack_lockfiles || fsck_objects)) {
953 int is_well_formed;
954 char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed);
955
956 if (!is_well_formed)
957 die(_("fetch-pack: invalid index-pack output"));
René Scharfe6031af32020-11-30 20:27:15 +0100958 if (pack_lockfile)
959 string_list_append_nodup(pack_lockfiles, pack_lockfile);
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800960 parse_gitmodules_oids(cmd.out, gitmodules_oids);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700961 close(cmd.out);
962 }
963
Jens Lindstrom37cb1dd2013-10-22 15:36:02 +0200964 if (!use_sideband)
965 /* Closed by start_command() */
966 xd[0] = -1;
967
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700968 ret = finish_command(&cmd);
969 if (!ret || (args->check_self_contained_and_connected && ret == 1))
970 args->self_contained_and_connected =
971 args->check_self_contained_and_connected &&
972 ret == 0;
973 else
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700974 die(_("%s failed"), cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700975 if (use_sideband && finish_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700976 die(_("error in sideband demultiplexer"));
Jonathan Tan5374a292019-10-14 17:12:31 -0700977
978 /*
979 * Now that index-pack has succeeded, write the promisor file using the
980 * obtained .keep filename if necessary
981 */
Jonathan Tan9da69a62020-06-10 13:57:22 -0700982 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
Christian Couder9d7fa3b2021-01-12 09:21:58 +0100983 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
Jonathan Tan5374a292019-10-14 17:12:31 -0700984
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700985 return 0;
986}
987
Junio C Hamanof2db8542013-01-29 14:02:15 -0800988static int cmp_ref_by_name(const void *a_, const void *b_)
989{
990 const struct ref *a = *((const struct ref **)a_);
991 const struct ref *b = *((const struct ref **)b_);
992 return strcmp(a->name, b->name);
993}
994
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700995static struct ref *do_fetch_pack(struct fetch_pack_args *args,
996 int fd[2],
997 const struct ref *orig_ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800998 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +0700999 struct shallow_info *si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001000 struct string_list *pack_lockfiles)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001001{
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001002 struct repository *r = the_repository;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001003 struct ref *ref = copy_ref_list(orig_ref);
brian m. carlson1b283372017-05-01 02:28:54 +00001004 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001005 const char *agent_feature;
1006 int agent_len;
Jonathan Tan603960b2019-11-12 16:34:20 -08001007 struct fetch_negotiator negotiator_alloc;
1008 struct fetch_negotiator *negotiator;
1009
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001010 negotiator = &negotiator_alloc;
1011 fetch_negotiator_init(r, negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001012
1013 sort_ref_list(&ref, ref_compare_name);
René Scharfe9ed0d8d2016-09-29 17:27:31 +02001014 QSORT(sought, nr_sought, cmp_ref_by_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001015
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001016 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1017 agent_supported = 1;
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +07001018 if (agent_len)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001019 print_verbose(args, _("Server version is %.*s"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +07001020 agent_len, agent_feature);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001021 }
Nguyễn Thái Ngọc Duy0e042972019-06-20 18:59:51 +07001022
Josh Steadmon1e905bb2020-11-11 15:29:31 -08001023 if (!server_supports("session-id"))
1024 advertise_sid = 0;
1025
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001026 if (server_supports("shallow"))
1027 print_verbose(args, _("Server supports %s"), "shallow");
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001028 else if (args->depth > 0 || is_repository_shallow(r))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001029 die(_("Server does not support shallow clients"));
1030 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1031 args->deepen = 1;
1032 if (server_supports("multi_ack_detailed")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001033 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001034 multi_ack = 2;
1035 if (server_supports("no-done")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001036 print_verbose(args, _("Server supports %s"), "no-done");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001037 if (args->stateless_rpc)
1038 no_done = 1;
1039 }
1040 }
1041 else if (server_supports("multi_ack")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001042 print_verbose(args, _("Server supports %s"), "multi_ack");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001043 multi_ack = 1;
1044 }
1045 if (server_supports("side-band-64k")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001046 print_verbose(args, _("Server supports %s"), "side-band-64k");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001047 use_sideband = 2;
1048 }
1049 else if (server_supports("side-band")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001050 print_verbose(args, _("Server supports %s"), "side-band");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001051 use_sideband = 1;
1052 }
1053 if (server_supports("allow-tip-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001054 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001055 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1056 }
1057 if (server_supports("allow-reachable-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001058 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001059 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1060 }
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001061 if (server_supports("thin-pack"))
1062 print_verbose(args, _("Server supports %s"), "thin-pack");
1063 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001064 args->use_thin_pack = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001065 if (server_supports("no-progress"))
1066 print_verbose(args, _("Server supports %s"), "no-progress");
1067 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001068 args->no_progress = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001069 if (server_supports("include-tag"))
1070 print_verbose(args, _("Server supports %s"), "include-tag");
1071 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001072 args->include_tag = 0;
1073 if (server_supports("ofs-delta"))
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001074 print_verbose(args, _("Server supports %s"), "ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001075 else
1076 prefer_ofs_delta = 0;
1077
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001078 if (server_supports("filter")) {
1079 server_supports_filtering = 1;
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001080 print_verbose(args, _("Server supports %s"), "filter");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001081 } else if (args->filter_options.choice) {
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001082 warning("filtering not recognized by server, ignoring");
1083 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001084
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001085 if (server_supports("deepen-since")) {
1086 print_verbose(args, _("Server supports %s"), "deepen-since");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001087 deepen_since_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001088 } else if (args->deepen_since)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001089 die(_("Server does not support --shallow-since"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001090 if (server_supports("deepen-not")) {
1091 print_verbose(args, _("Server supports %s"), "deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001092 deepen_not_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001093 } else if (args->deepen_not)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001094 die(_("Server does not support --shallow-exclude"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001095 if (server_supports("deepen-relative"))
1096 print_verbose(args, _("Server supports %s"), "deepen-relative");
1097 else if (args->deepen_relative)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001098 die(_("Server does not support --deepen"));
brian m. carlson48bf1412020-05-25 19:58:59 +00001099 if (!server_supports_hash(the_hash_algo->name, NULL))
1100 die(_("Server does not support this repository's object format"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001101
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001102 mark_complete_and_common_ref(negotiator, args, &ref);
1103 filter_refs(args, &ref, sought, nr_sought);
1104 if (everything_local(args, &ref)) {
1105 packet_flush(fd[1]);
1106 goto all_done;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001107 }
Jonathan Tan603960b2019-11-12 16:34:20 -08001108 if (find_common(negotiator, args, fd, &oid, ref) < 0)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001109 if (!args->keep_pack)
1110 /* When cloning, it is not unusual to have
1111 * no common commit.
1112 */
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001113 warning(_("no common commits"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001114
1115 if (args->stateless_rpc)
1116 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001117 if (args->deepen)
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +07001118 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1119 NULL);
Li Linchao4fe788b2021-04-01 10:46:59 +00001120 else if (si->nr_ours || si->nr_theirs) {
1121 if (args->reject_shallow_remote)
1122 die(_("source repository is shallow, reject to clone."));
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001123 alternate_shallow_file = setup_temporary_shallow(si->shallow);
Li Linchao4fe788b2021-04-01 10:46:59 +00001124 } else
Nguyễn Thái Ngọc Duy6da8bdc2013-08-26 09:17:26 +07001125 alternate_shallow_file = NULL;
Jonathan Tan5476e1e2021-02-22 11:20:09 -08001126 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001127 &fsck_options.gitmodules_found))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001128 die(_("git fetch-pack: fetch failed."));
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001129 if (fsck_finish(&fsck_options))
1130 die("fsck failed");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001131
1132 all_done:
Jonathan Tan603960b2019-11-12 16:34:20 -08001133 if (negotiator)
1134 negotiator->release(negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001135 return ref;
1136}
1137
Brandon Williamsf7e20502018-03-15 10:31:29 -07001138static void add_shallow_requests(struct strbuf *req_buf,
1139 const struct fetch_pack_args *args)
1140{
Junio C Hamano00624d62018-07-18 12:20:27 -07001141 if (is_repository_shallow(the_repository))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001142 write_shallow_commits(req_buf, 1, NULL);
1143 if (args->depth > 0)
1144 packet_buf_write(req_buf, "deepen %d", args->depth);
1145 if (args->deepen_since) {
1146 timestamp_t max_age = approxidate(args->deepen_since);
1147 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1148 }
1149 if (args->deepen_not) {
1150 int i;
1151 for (i = 0; i < args->deepen_not->nr; i++) {
1152 struct string_list_item *s = args->deepen_not->items + i;
1153 packet_buf_write(req_buf, "deepen-not %s", s->string);
1154 }
1155 }
Jonathan Tan5056cf42018-12-18 13:24:35 -08001156 if (args->deepen_relative)
1157 packet_buf_write(req_buf, "deepen-relative\n");
Brandon Williamsf7e20502018-03-15 10:31:29 -07001158}
1159
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001160static void add_wants(const struct ref *wants, struct strbuf *req_buf)
Brandon Williams685fbd32018-03-15 10:31:28 -07001161{
Brandon Williams73302052018-06-27 15:30:23 -07001162 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1163
Brandon Williams685fbd32018-03-15 10:31:28 -07001164 for ( ; wants ; wants = wants->next) {
1165 const struct object_id *remote = &wants->old_oid;
Brandon Williams685fbd32018-03-15 10:31:28 -07001166 struct object *o;
1167
1168 /*
1169 * If that object is complete (i.e. it is an ancestor of a
1170 * local ref), we tell them we have it but do not have to
1171 * tell them about its ancestors, which they already know
1172 * about.
1173 *
1174 * We use lookup_object here because we are only
1175 * interested in the case we *know* the object is
1176 * reachable and we have already scanned it.
1177 */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001178 if (((o = lookup_object(the_repository, remote)) != NULL) &&
Brandon Williams685fbd32018-03-15 10:31:28 -07001179 (o->flags & COMPLETE)) {
1180 continue;
1181 }
1182
Brandon Williams73302052018-06-27 15:30:23 -07001183 if (!use_ref_in_want || wants->exact_oid)
1184 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1185 else
1186 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
Brandon Williams685fbd32018-03-15 10:31:28 -07001187 }
1188}
1189
1190static void add_common(struct strbuf *req_buf, struct oidset *common)
1191{
1192 struct oidset_iter iter;
1193 const struct object_id *oid;
1194 oidset_iter_init(common, &iter);
1195
1196 while ((oid = oidset_iter_next(&iter))) {
1197 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1198 }
1199}
1200
Jonathan Tanec062832018-06-14 15:54:28 -07001201static int add_haves(struct fetch_negotiator *negotiator,
1202 struct strbuf *req_buf,
Jonathan Tan57c34512021-04-08 18:10:00 -07001203 int *haves_to_send)
Brandon Williams685fbd32018-03-15 10:31:28 -07001204{
Brandon Williams685fbd32018-03-15 10:31:28 -07001205 int haves_added = 0;
1206 const struct object_id *oid;
1207
Jonathan Tanec062832018-06-14 15:54:28 -07001208 while ((oid = negotiator->next(negotiator))) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001209 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1210 if (++haves_added >= *haves_to_send)
1211 break;
1212 }
1213
Brandon Williams685fbd32018-03-15 10:31:28 -07001214 /* Increase haves to send on next round */
1215 *haves_to_send = next_flush(1, *haves_to_send);
1216
Jonathan Tan57c34512021-04-08 18:10:00 -07001217 return haves_added;
Brandon Williams685fbd32018-03-15 10:31:28 -07001218}
1219
Jonathan Tan6871d0c2021-04-08 18:10:01 -07001220static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1221 const struct string_list *server_options)
1222{
1223 const char *hash_name;
1224
1225 if (server_supports_v2("fetch", 1))
1226 packet_buf_write(req_buf, "command=fetch");
1227 if (server_supports_v2("agent", 0))
1228 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1229 if (advertise_sid && server_supports_v2("session-id", 0))
1230 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1231 if (server_options && server_options->nr &&
1232 server_supports_v2("server-option", 1)) {
1233 int i;
1234 for (i = 0; i < server_options->nr; i++)
1235 packet_buf_write(req_buf, "server-option=%s",
1236 server_options->items[i].string);
1237 }
1238
1239 if (server_feature_v2("object-format", &hash_name)) {
1240 int hash_algo = hash_algo_by_name(hash_name);
1241 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1242 die(_("mismatched algorithms: client %s; server %s"),
1243 the_hash_algo->name, hash_name);
1244 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1245 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1246 die(_("the server does not support algorithm '%s'"),
1247 the_hash_algo->name);
1248 }
1249 packet_buf_delim(req_buf);
1250}
1251
Jonathan Tanec062832018-06-14 15:54:28 -07001252static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001253 struct fetch_pack_args *args,
Brandon Williams685fbd32018-03-15 10:31:28 -07001254 const struct ref *wants, struct oidset *common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001255 int *haves_to_send, int *in_vain,
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001256 int sideband_all, int seen_ack)
Brandon Williams685fbd32018-03-15 10:31:28 -07001257{
Jonathan Tan57c34512021-04-08 18:10:00 -07001258 int haves_added;
1259 int done_sent = 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001260 struct strbuf req_buf = STRBUF_INIT;
1261
Jonathan Tan6871d0c2021-04-08 18:10:01 -07001262 write_fetch_command_and_capabilities(&req_buf, args->server_options);
Brandon Williams685fbd32018-03-15 10:31:28 -07001263
Brandon Williams685fbd32018-03-15 10:31:28 -07001264 if (args->use_thin_pack)
1265 packet_buf_write(&req_buf, "thin-pack");
1266 if (args->no_progress)
1267 packet_buf_write(&req_buf, "no-progress");
1268 if (args->include_tag)
1269 packet_buf_write(&req_buf, "include-tag");
1270 if (prefer_ofs_delta)
1271 packet_buf_write(&req_buf, "ofs-delta");
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001272 if (sideband_all)
1273 packet_buf_write(&req_buf, "sideband-all");
Brandon Williams685fbd32018-03-15 10:31:28 -07001274
Brandon Williamsf7e20502018-03-15 10:31:29 -07001275 /* Add shallow-info and deepen request */
1276 if (server_supports_feature("fetch", "shallow", 0))
1277 add_shallow_requests(&req_buf, args);
Junio C Hamano00624d62018-07-18 12:20:27 -07001278 else if (is_repository_shallow(the_repository) || args->deepen)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001279 die(_("Server does not support shallow requests"));
1280
Jonathan Tanba957102018-05-03 16:46:56 -07001281 /* Add filter */
1282 if (server_supports_feature("fetch", "filter", 0) &&
1283 args->filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001284 const char *spec =
1285 expand_list_objects_filter_spec(&args->filter_options);
Jonathan Tanba957102018-05-03 16:46:56 -07001286 print_verbose(args, _("Server supports filter"));
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001287 packet_buf_write(&req_buf, "filter %s", spec);
Jonathan Tanba957102018-05-03 16:46:56 -07001288 } else if (args->filter_options.choice) {
1289 warning("filtering not recognized by server, ignoring");
1290 }
1291
Jonathan Tandd4b7322020-06-10 13:57:23 -07001292 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1293 int i;
1294 struct strbuf to_send = STRBUF_INIT;
1295
1296 for (i = 0; i < uri_protocols.nr; i++) {
1297 const char *s = uri_protocols.items[i].string;
1298
1299 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1300 if (to_send.len)
1301 strbuf_addch(&to_send, ',');
1302 strbuf_addstr(&to_send, s);
1303 }
1304 }
1305 if (to_send.len) {
1306 packet_buf_write(&req_buf, "packfile-uris %s",
1307 to_send.buf);
1308 strbuf_release(&to_send);
1309 }
1310 }
1311
Brandon Williams685fbd32018-03-15 10:31:28 -07001312 /* add wants */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001313 add_wants(wants, &req_buf);
Brandon Williams685fbd32018-03-15 10:31:28 -07001314
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001315 /* Add all of the common commits we've found in previous rounds */
1316 add_common(&req_buf, common);
Brandon Williams685fbd32018-03-15 10:31:28 -07001317
Jonathan Tan57c34512021-04-08 18:10:00 -07001318 haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1319 *in_vain += haves_added;
1320 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1321 /* Send Done */
1322 packet_buf_write(&req_buf, "done\n");
1323 done_sent = 1;
1324 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001325
1326 /* Send request */
1327 packet_buf_flush(&req_buf);
Jeff King37c80012019-03-04 23:11:39 -05001328 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1329 die_errno(_("unable to write request to remote"));
Brandon Williams685fbd32018-03-15 10:31:28 -07001330
1331 strbuf_release(&req_buf);
Jonathan Tan57c34512021-04-08 18:10:00 -07001332 return done_sent;
Brandon Williams685fbd32018-03-15 10:31:28 -07001333}
1334
1335/*
1336 * Processes a section header in a server's response and checks if it matches
1337 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1338 * not consumed); if 0, the line will be consumed and the function will die if
1339 * the section header doesn't match what was expected.
1340 */
1341static int process_section_header(struct packet_reader *reader,
1342 const char *section, int peek)
1343{
1344 int ret;
1345
1346 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001347 die(_("error reading section header '%s'"), section);
Brandon Williams685fbd32018-03-15 10:31:28 -07001348
1349 ret = !strcmp(reader->line, section);
1350
1351 if (!peek) {
1352 if (!ret)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001353 die(_("expected '%s', received '%s'"),
Brandon Williams685fbd32018-03-15 10:31:28 -07001354 section, reader->line);
1355 packet_reader_read(reader);
1356 }
1357
1358 return ret;
1359}
1360
Jonathan Tan81025702021-04-08 18:09:59 -07001361static int process_ack(struct fetch_negotiator *negotiator,
1362 struct packet_reader *reader,
1363 struct object_id *common_oid,
1364 int *received_ready)
Brandon Williams685fbd32018-03-15 10:31:28 -07001365{
Brandon Williams685fbd32018-03-15 10:31:28 -07001366 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1367 const char *arg;
1368
1369 if (!strcmp(reader->line, "NAK"))
1370 continue;
1371
1372 if (skip_prefix(reader->line, "ACK ", &arg)) {
Jonathan Tan81025702021-04-08 18:09:59 -07001373 if (!get_oid_hex(arg, common_oid)) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001374 struct commit *commit;
Jonathan Tan81025702021-04-08 18:09:59 -07001375 commit = lookup_commit(the_repository, common_oid);
Jonathan Tan603960b2019-11-12 16:34:20 -08001376 if (negotiator)
1377 negotiator->ack(negotiator, commit);
Brandon Williams685fbd32018-03-15 10:31:28 -07001378 }
Jonathan Tan81025702021-04-08 18:09:59 -07001379 return 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001380 }
1381
1382 if (!strcmp(reader->line, "ready")) {
Jonathan Tan81025702021-04-08 18:09:59 -07001383 *received_ready = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001384 continue;
1385 }
1386
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001387 die(_("unexpected acknowledgment line: '%s'"), reader->line);
Brandon Williams685fbd32018-03-15 10:31:28 -07001388 }
1389
1390 if (reader->status != PACKET_READ_FLUSH &&
1391 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001392 die(_("error processing acks: %d"), reader->status);
Brandon Williams685fbd32018-03-15 10:31:28 -07001393
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001394 /*
1395 * If an "acknowledgments" section is sent, a packfile is sent if and
1396 * only if "ready" was sent in this section. The other sections
1397 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1398 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1399 * otherwise.
1400 */
Jonathan Tan81025702021-04-08 18:09:59 -07001401 if (*received_ready && reader->status != PACKET_READ_DELIM)
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001402 die(_("expected packfile to be sent after 'ready'"));
Jonathan Tan81025702021-04-08 18:09:59 -07001403 if (!*received_ready && reader->status != PACKET_READ_FLUSH)
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001404 die(_("expected no other sections to be sent after no 'ready'"));
1405
Jonathan Tan81025702021-04-08 18:09:59 -07001406 return 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001407}
1408
Brandon Williamsf7e20502018-03-15 10:31:29 -07001409static void receive_shallow_info(struct fetch_pack_args *args,
Jonathan Tan13390782019-03-26 12:31:21 -07001410 struct packet_reader *reader,
1411 struct oid_array *shallows,
1412 struct shallow_info *si)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001413{
Jonathan Tan13390782019-03-26 12:31:21 -07001414 int unshallow_received = 0;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001415
Brandon Williamsf7e20502018-03-15 10:31:29 -07001416 process_section_header(reader, "shallow-info", 0);
1417 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1418 const char *arg;
1419 struct object_id oid;
1420
1421 if (skip_prefix(reader->line, "shallow ", &arg)) {
1422 if (get_oid_hex(arg, &oid))
1423 die(_("invalid shallow line: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001424 oid_array_append(shallows, &oid);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001425 continue;
1426 }
1427 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1428 if (get_oid_hex(arg, &oid))
1429 die(_("invalid unshallow line: %s"), reader->line);
Jeff Kingd0229ab2019-06-20 03:41:14 -04001430 if (!lookup_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001431 die(_("object not found: %s"), reader->line);
1432 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -07001433 if (!parse_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001434 die(_("error in object: %s"), reader->line);
1435 if (unregister_shallow(&oid))
1436 die(_("no shallow found: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001437 unshallow_received = 1;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001438 continue;
1439 }
1440 die(_("expected shallow/unshallow, got %s"), reader->line);
1441 }
1442
1443 if (reader->status != PACKET_READ_FLUSH &&
1444 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001445 die(_("error processing shallow info: %d"), reader->status);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001446
Jonathan Tan13390782019-03-26 12:31:21 -07001447 if (args->deepen || unshallow_received) {
1448 /*
1449 * Treat these as shallow lines caused by our depth settings.
1450 * In v0, these lines cannot cause refs to be rejected; do the
1451 * same.
1452 */
1453 int i;
1454
1455 for (i = 0; i < shallows->nr; i++)
1456 register_shallow(the_repository, &shallows->oid[i]);
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001457 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1458 NULL);
1459 args->deepen = 1;
Jonathan Tan13390782019-03-26 12:31:21 -07001460 } else if (shallows->nr) {
1461 /*
1462 * Treat these as shallow lines caused by the remote being
1463 * shallow. In v0, remote refs that reach these objects are
1464 * rejected (unless --update-shallow is set); do the same.
1465 */
1466 prepare_shallow_info(si, shallows);
Li Linchao4fe788b2021-04-01 10:46:59 +00001467 if (si->nr_ours || si->nr_theirs) {
1468 if (args->reject_shallow_remote)
1469 die(_("source repository is shallow, reject to clone."));
Jonathan Tan13390782019-03-26 12:31:21 -07001470 alternate_shallow_file =
1471 setup_temporary_shallow(si->shallow);
Li Linchao4fe788b2021-04-01 10:46:59 +00001472 } else
Jonathan Tan13390782019-03-26 12:31:21 -07001473 alternate_shallow_file = NULL;
brian m. carlson380ebab2019-02-06 23:59:37 +00001474 } else {
1475 alternate_shallow_file = NULL;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001476 }
Brandon Williamsf7e20502018-03-15 10:31:29 -07001477}
1478
Jonathan Tanb7643002019-03-27 14:11:10 -07001479static int cmp_name_ref(const void *name, const void *ref)
1480{
1481 return strcmp(name, (*(struct ref **)ref)->name);
1482}
1483
Jonathan Tane2842b32018-08-01 13:13:20 -07001484static void receive_wanted_refs(struct packet_reader *reader,
1485 struct ref **sought, int nr_sought)
Brandon Williams73302052018-06-27 15:30:23 -07001486{
1487 process_section_header(reader, "wanted-refs", 0);
1488 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1489 struct object_id oid;
1490 const char *end;
Jonathan Tanb7643002019-03-27 14:11:10 -07001491 struct ref **found;
Brandon Williams73302052018-06-27 15:30:23 -07001492
1493 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001494 die(_("expected wanted-ref, got '%s'"), reader->line);
Brandon Williams73302052018-06-27 15:30:23 -07001495
Jonathan Tanb7643002019-03-27 14:11:10 -07001496 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1497 cmp_name_ref);
1498 if (!found)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001499 die(_("unexpected wanted-ref: '%s'"), reader->line);
Jonathan Tanb7643002019-03-27 14:11:10 -07001500 oidcpy(&(*found)->old_oid, &oid);
Brandon Williams73302052018-06-27 15:30:23 -07001501 }
1502
1503 if (reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001504 die(_("error processing wanted refs: %d"), reader->status);
Brandon Williams73302052018-06-27 15:30:23 -07001505}
1506
Jonathan Tandd4b7322020-06-10 13:57:23 -07001507static void receive_packfile_uris(struct packet_reader *reader,
1508 struct string_list *uris)
1509{
1510 process_section_header(reader, "packfile-uris", 0);
1511 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1512 if (reader->pktlen < the_hash_algo->hexsz ||
1513 reader->line[the_hash_algo->hexsz] != ' ')
1514 die("expected '<hash> <uri>', got: %s\n", reader->line);
1515
1516 string_list_append(uris, reader->line);
1517 }
1518 if (reader->status != PACKET_READ_DELIM)
1519 die("expected DELIM");
1520}
1521
Brandon Williams685fbd32018-03-15 10:31:28 -07001522enum fetch_state {
1523 FETCH_CHECK_LOCAL = 0,
1524 FETCH_SEND_REQUEST,
1525 FETCH_PROCESS_ACKS,
1526 FETCH_GET_PACK,
1527 FETCH_DONE,
1528};
1529
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001530static void do_check_stateless_delimiter(int stateless_rpc,
Denton Liub0df0c12020-05-19 06:54:00 -04001531 struct packet_reader *reader)
1532{
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001533 check_stateless_delimiter(stateless_rpc, reader,
Denton Liub0df0c12020-05-19 06:54:00 -04001534 _("git fetch-pack: expected response end packet"));
1535}
1536
Brandon Williams685fbd32018-03-15 10:31:28 -07001537static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1538 int fd[2],
1539 const struct ref *orig_ref,
1540 struct ref **sought, int nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001541 struct oid_array *shallows,
1542 struct shallow_info *si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001543 struct string_list *pack_lockfiles)
Brandon Williams685fbd32018-03-15 10:31:28 -07001544{
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001545 struct repository *r = the_repository;
Brandon Williams685fbd32018-03-15 10:31:28 -07001546 struct ref *ref = copy_ref_list(orig_ref);
1547 enum fetch_state state = FETCH_CHECK_LOCAL;
1548 struct oidset common = OIDSET_INIT;
1549 struct packet_reader reader;
Josh Steadmon5fc31182019-10-02 16:49:28 -07001550 int in_vain = 0, negotiation_started = 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001551 int haves_to_send = INITIAL_FLUSH;
Jonathan Tan603960b2019-11-12 16:34:20 -08001552 struct fetch_negotiator negotiator_alloc;
1553 struct fetch_negotiator *negotiator;
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001554 int seen_ack = 0;
Jonathan Tan81025702021-04-08 18:09:59 -07001555 struct object_id common_oid;
1556 int received_ready = 0;
Jonathan Tandd4b7322020-06-10 13:57:23 -07001557 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1558 int i;
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001559 struct strvec index_pack_args = STRVEC_INIT;
Jonathan Tan603960b2019-11-12 16:34:20 -08001560
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001561 negotiator = &negotiator_alloc;
1562 fetch_negotiator_init(r, negotiator);
Jonathan Tan603960b2019-11-12 16:34:20 -08001563
Brandon Williams685fbd32018-03-15 10:31:28 -07001564 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -08001565 PACKET_READ_CHOMP_NEWLINE |
1566 PACKET_READ_DIE_ON_ERR_PACKET);
Jonathan Tan07c3c2a2019-01-16 11:28:15 -08001567 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1568 server_supports_feature("fetch", "sideband-all", 0)) {
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001569 reader.use_sideband = 1;
1570 reader.me = "fetch-pack";
1571 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001572
1573 while (state != FETCH_DONE) {
1574 switch (state) {
1575 case FETCH_CHECK_LOCAL:
1576 sort_ref_list(&ref, ref_compare_name);
1577 QSORT(sought, nr_sought, cmp_ref_by_name);
1578
1579 /* v2 supports these by default */
1580 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1581 use_sideband = 2;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001582 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1583 args->deepen = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001584
Brandon Williams685fbd32018-03-15 10:31:28 -07001585 /* Filter 'ref' by 'sought' and those that aren't local */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001586 mark_complete_and_common_ref(negotiator, args, &ref);
1587 filter_refs(args, &ref, sought, nr_sought);
1588 if (everything_local(args, &ref))
1589 state = FETCH_DONE;
1590 else
Jonathan Tan12f19a92018-10-03 16:04:52 -07001591 state = FETCH_SEND_REQUEST;
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001592
1593 mark_tips(negotiator, args->negotiation_tips);
1594 for_each_cached_alternate(negotiator,
1595 insert_one_alternate_object);
Brandon Williams685fbd32018-03-15 10:31:28 -07001596 break;
1597 case FETCH_SEND_REQUEST:
Josh Steadmon5fc31182019-10-02 16:49:28 -07001598 if (!negotiation_started) {
1599 negotiation_started = 1;
1600 trace2_region_enter("fetch-pack",
1601 "negotiation_v2",
1602 the_repository);
1603 }
Jonathan Tan603960b2019-11-12 16:34:20 -08001604 if (send_fetch_request(negotiator, fd[1], args, ref,
Jonathan Tanec062832018-06-14 15:54:28 -07001605 &common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001606 &haves_to_send, &in_vain,
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001607 reader.use_sideband,
1608 seen_ack))
Brandon Williams685fbd32018-03-15 10:31:28 -07001609 state = FETCH_GET_PACK;
1610 else
1611 state = FETCH_PROCESS_ACKS;
1612 break;
1613 case FETCH_PROCESS_ACKS:
1614 /* Process ACKs/NAKs */
Jonathan Tan81025702021-04-08 18:09:59 -07001615 process_section_header(&reader, "acknowledgments", 0);
1616 while (process_ack(negotiator, &reader, &common_oid,
1617 &received_ready)) {
1618 in_vain = 0;
1619 seen_ack = 1;
1620 oidset_insert(&common, &common_oid);
1621 }
1622 if (received_ready) {
Denton Liub0df0c12020-05-19 06:54:00 -04001623 /*
1624 * Don't check for response delimiter; get_pack() will
1625 * read the rest of this response.
1626 */
Brandon Williams685fbd32018-03-15 10:31:28 -07001627 state = FETCH_GET_PACK;
Jonathan Tan81025702021-04-08 18:09:59 -07001628 } else {
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001629 do_check_stateless_delimiter(args->stateless_rpc, &reader);
Brandon Williams685fbd32018-03-15 10:31:28 -07001630 state = FETCH_SEND_REQUEST;
Brandon Williams685fbd32018-03-15 10:31:28 -07001631 }
1632 break;
1633 case FETCH_GET_PACK:
Josh Steadmon5fc31182019-10-02 16:49:28 -07001634 trace2_region_leave("fetch-pack",
1635 "negotiation_v2",
1636 the_repository);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001637 /* Check for shallow-info section */
1638 if (process_section_header(&reader, "shallow-info", 1))
Jonathan Tan13390782019-03-26 12:31:21 -07001639 receive_shallow_info(args, &reader, shallows, si);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001640
Brandon Williams73302052018-06-27 15:30:23 -07001641 if (process_section_header(&reader, "wanted-refs", 1))
Jonathan Tane2842b32018-08-01 13:13:20 -07001642 receive_wanted_refs(&reader, sought, nr_sought);
Brandon Williams73302052018-06-27 15:30:23 -07001643
Jonathan Tandd4b7322020-06-10 13:57:23 -07001644 /* get the pack(s) */
1645 if (process_section_header(&reader, "packfile-uris", 1))
1646 receive_packfile_uris(&reader, &packfile_uris);
Brandon Williams685fbd32018-03-15 10:31:28 -07001647 process_section_header(&reader, "packfile", 0);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001648 if (get_pack(args, fd, pack_lockfiles,
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001649 packfile_uris.nr ? &index_pack_args : NULL,
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001650 sought, nr_sought, &fsck_options.gitmodules_found))
Brandon Williams685fbd32018-03-15 10:31:28 -07001651 die(_("git fetch-pack: fetch failed."));
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001652 do_check_stateless_delimiter(args->stateless_rpc, &reader);
Brandon Williams685fbd32018-03-15 10:31:28 -07001653
1654 state = FETCH_DONE;
1655 break;
1656 case FETCH_DONE:
1657 continue;
1658 }
1659 }
1660
Jonathan Tandd4b7322020-06-10 13:57:23 -07001661 for (i = 0; i < packfile_uris.nr; i++) {
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001662 int j;
Jonathan Tandd4b7322020-06-10 13:57:23 -07001663 struct child_process cmd = CHILD_PROCESS_INIT;
1664 char packname[GIT_MAX_HEXSZ + 1];
1665 const char *uri = packfile_uris.items[i].string +
1666 the_hash_algo->hexsz + 1;
1667
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001668 strvec_push(&cmd.args, "http-fetch");
1669 strvec_pushf(&cmd.args, "--packfile=%.*s",
Jeff Kingf6d89422020-07-28 16:26:31 -04001670 (int) the_hash_algo->hexsz,
1671 packfile_uris.items[i].string);
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001672 for (j = 0; j < index_pack_args.nr; j++)
1673 strvec_pushf(&cmd.args, "--index-pack-arg=%s",
1674 index_pack_args.v[j]);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001675 strvec_push(&cmd.args, uri);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001676 cmd.git_cmd = 1;
1677 cmd.no_stdin = 1;
1678 cmd.out = -1;
1679 if (start_command(&cmd))
1680 die("fetch-pack: unable to spawn http-fetch");
1681
1682 if (read_in_full(cmd.out, packname, 5) < 0 ||
1683 memcmp(packname, "keep\t", 5))
1684 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1685
1686 if (read_in_full(cmd.out, packname,
1687 the_hash_algo->hexsz + 1) < 0 ||
1688 packname[the_hash_algo->hexsz] != '\n')
1689 die("fetch-pack: expected hash then LF at end of http-fetch output");
1690
1691 packname[the_hash_algo->hexsz] = '\0';
1692
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001693 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
Jonathan Tan5476e1e2021-02-22 11:20:09 -08001694
Jonathan Tandd4b7322020-06-10 13:57:23 -07001695 close(cmd.out);
1696
1697 if (finish_command(&cmd))
1698 die("fetch-pack: unable to finish http-fetch");
1699
1700 if (memcmp(packfile_uris.items[i].string, packname,
1701 the_hash_algo->hexsz))
1702 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1703 uri, (int) the_hash_algo->hexsz,
1704 packfile_uris.items[i].string);
1705
1706 string_list_append_nodup(pack_lockfiles,
1707 xstrfmt("%s/pack/pack-%s.keep",
1708 get_object_directory(),
1709 packname));
1710 }
1711 string_list_clear(&packfile_uris, 0);
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001712 strvec_clear(&index_pack_args);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001713
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001714 if (fsck_finish(&fsck_options))
1715 die("fsck failed");
Brandon Williams685fbd32018-03-15 10:31:28 -07001716
Jonathan Tan603960b2019-11-12 16:34:20 -08001717 if (negotiator)
1718 negotiator->release(negotiator);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001719
Brandon Williams685fbd32018-03-15 10:31:28 -07001720 oidset_clear(&common);
1721 return ref;
1722}
1723
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001724static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1725{
1726 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1727 const char *path;
1728
1729 if (git_config_pathname(&path, var, value))
1730 return 1;
1731 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1732 fsck_msg_types.len ? ',' : '=', path);
1733 free((char *)path);
1734 return 0;
1735 }
1736
1737 if (skip_prefix(var, "fetch.fsck.", &var)) {
1738 if (is_valid_msg_type(var, value))
1739 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1740 fsck_msg_types.len ? ',' : '=', var, value);
1741 else
1742 warning("Skipping unknown msg id '%s'", var);
1743 return 0;
1744 }
1745
1746 return git_default_config(var, value, cb);
1747}
1748
Tanay Abhraf44af512014-08-07 09:21:20 -07001749static void fetch_pack_config(void)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001750{
Tanay Abhraf44af512014-08-07 09:21:20 -07001751 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1752 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1753 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1754 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1755 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
Josh Steadmon1e905bb2020-11-11 15:29:31 -08001756 git_config_get_bool("transfer.advertisesid", &advertise_sid);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001757 if (!uri_protocols.nr) {
1758 char *str;
1759
1760 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1761 string_list_split(&uri_protocols, str, ',', -1);
1762 free(str);
1763 }
1764 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001765
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001766 git_config(fetch_pack_config_cb, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001767}
1768
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001769static void fetch_pack_setup(void)
1770{
1771 static int did_setup;
1772 if (did_setup)
1773 return;
Tanay Abhraf44af512014-08-07 09:21:20 -07001774 fetch_pack_config();
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001775 if (0 <= transfer_unpack_limit)
1776 unpack_limit = transfer_unpack_limit;
1777 else if (0 <= fetch_unpack_limit)
1778 unpack_limit = fetch_unpack_limit;
1779 did_setup = 1;
1780}
1781
Junio C Hamanof2db8542013-01-29 14:02:15 -08001782static int remove_duplicates_in_refs(struct ref **ref, int nr)
1783{
1784 struct string_list names = STRING_LIST_INIT_NODUP;
1785 int src, dst;
1786
1787 for (src = dst = 0; src < nr; src++) {
1788 struct string_list_item *item;
1789 item = string_list_insert(&names, ref[src]->name);
1790 if (item->util)
1791 continue; /* already have it */
1792 item->util = ref[src];
1793 if (src != dst)
1794 ref[dst] = ref[src];
1795 dst++;
1796 }
1797 for (src = dst; src < nr; src++)
1798 ref[src] = NULL;
1799 string_list_clear(&names, 0);
1800 return dst;
1801}
1802
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001803static void update_shallow(struct fetch_pack_args *args,
Jonathan Tane2842b32018-08-01 13:13:20 -07001804 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001805 struct shallow_info *si)
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001806{
brian m. carlson910650d2017-03-31 01:40:00 +00001807 struct oid_array ref = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001808 int *status;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001809 int i;
1810
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001811 if (args->deepen && alternate_shallow_file) {
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001812 if (*alternate_shallow_file == '\0') { /* --unshallow */
Stefan Beller102de882018-05-17 15:51:51 -07001813 unlink_or_warn(git_path_shallow(the_repository));
Taylor Blau37b9dca2020-04-22 18:25:45 -06001814 rollback_shallow_file(the_repository, &shallow_lock);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001815 } else
Taylor Blau37b9dca2020-04-22 18:25:45 -06001816 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001817 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001818 return;
1819 }
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001820
1821 if (!si->shallow || !si->shallow->nr)
1822 return;
1823
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001824 if (args->cloning) {
1825 /*
1826 * remote is shallow, but this is a clone, there are
1827 * no objects in repo to worry about. Accept any
1828 * shallow points that exist in the pack (iow in repo
1829 * after get_pack() and reprepare_packed_git())
1830 */
brian m. carlson910650d2017-03-31 01:40:00 +00001831 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001832 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001833 for (i = 0; i < si->shallow->nr; i++)
brian m. carlsonee3051b2017-03-26 16:01:37 +00001834 if (has_object_file(&oid[i]))
brian m. carlson910650d2017-03-31 01:40:00 +00001835 oid_array_append(&extra, &oid[i]);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001836 if (extra.nr) {
1837 setup_alternate_shallow(&shallow_lock,
1838 &alternate_shallow_file,
1839 &extra);
Taylor Blau37b9dca2020-04-22 18:25:45 -06001840 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001841 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001842 }
brian m. carlson910650d2017-03-31 01:40:00 +00001843 oid_array_clear(&extra);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001844 return;
1845 }
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001846
1847 if (!si->nr_ours && !si->nr_theirs)
1848 return;
1849
1850 remove_nonexistent_theirs_shallow(si);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001851 if (!si->nr_ours && !si->nr_theirs)
1852 return;
Jonathan Tane2842b32018-08-01 13:13:20 -07001853 for (i = 0; i < nr_sought; i++)
1854 oid_array_append(&ref, &sought[i]->old_oid);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001855 si->ref = &ref;
1856
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001857 if (args->update_shallow) {
1858 /*
1859 * remote is also shallow, .git/shallow may be updated
1860 * so all refs can be accepted. Make sure we only add
1861 * shallow roots that are actually reachable from new
1862 * refs.
1863 */
brian m. carlson910650d2017-03-31 01:40:00 +00001864 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001865 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001866 assign_shallow_commits_to_refs(si, NULL, NULL);
1867 if (!si->nr_ours && !si->nr_theirs) {
brian m. carlson910650d2017-03-31 01:40:00 +00001868 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001869 return;
1870 }
1871 for (i = 0; i < si->nr_ours; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001872 oid_array_append(&extra, &oid[si->ours[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001873 for (i = 0; i < si->nr_theirs; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001874 oid_array_append(&extra, &oid[si->theirs[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001875 setup_alternate_shallow(&shallow_lock,
1876 &alternate_shallow_file,
1877 &extra);
Taylor Blau37b9dca2020-04-22 18:25:45 -06001878 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson910650d2017-03-31 01:40:00 +00001879 oid_array_clear(&extra);
1880 oid_array_clear(&ref);
brian m. carlson23311f32019-02-04 00:06:50 +00001881 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001882 return;
1883 }
1884
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001885 /*
1886 * remote is also shallow, check what ref is safe to update
1887 * without updating .git/shallow
1888 */
René Scharfeca56dad2021-03-13 17:17:22 +01001889 CALLOC_ARRAY(status, nr_sought);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001890 assign_shallow_commits_to_refs(si, NULL, status);
1891 if (si->nr_ours || si->nr_theirs) {
Jonathan Tane2842b32018-08-01 13:13:20 -07001892 for (i = 0; i < nr_sought; i++)
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001893 if (status[i])
Jonathan Tane2842b32018-08-01 13:13:20 -07001894 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001895 }
1896 free(status);
brian m. carlson910650d2017-03-31 01:40:00 +00001897 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001898}
1899
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001900static int iterate_ref_map(void *cb_data, struct object_id *oid)
1901{
1902 struct ref **rm = cb_data;
1903 struct ref *ref = *rm;
1904
1905 if (!ref)
1906 return -1; /* end of the list */
1907 *rm = ref->next;
1908 oidcpy(oid, &ref->old_oid);
1909 return 0;
1910}
1911
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001912struct ref *fetch_pack(struct fetch_pack_args *args,
Jeff King0f804b02019-03-20 04:16:14 -04001913 int fd[],
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001914 const struct ref *ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -08001915 struct ref **sought, int nr_sought,
brian m. carlson910650d2017-03-31 01:40:00 +00001916 struct oid_array *shallow,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001917 struct string_list *pack_lockfiles,
Brandon Williams685fbd32018-03-15 10:31:28 -07001918 enum protocol_version version)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001919{
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001920 struct ref *ref_cpy;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001921 struct shallow_info si;
Jonathan Tan13390782019-03-26 12:31:21 -07001922 struct oid_array shallows_scratch = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001923
1924 fetch_pack_setup();
Junio C Hamanof2db8542013-01-29 14:02:15 -08001925 if (nr_sought)
1926 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001927
Jonathan Tan01775652018-09-27 12:24:05 -07001928 if (version != protocol_v2 && !ref) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001929 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001930 die(_("no matching remote head"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001931 }
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001932 if (version == protocol_v2) {
1933 if (shallow->nr)
1934 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1935 memset(&si, 0, sizeof(si));
Brandon Williams685fbd32018-03-15 10:31:28 -07001936 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001937 &shallows_scratch, &si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001938 pack_lockfiles);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001939 } else {
1940 prepare_shallow_info(&si, shallow);
Brandon Williams685fbd32018-03-15 10:31:28 -07001941 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001942 &si, pack_lockfiles);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001943 }
Stefan Bellera49d2832018-03-23 18:45:21 +01001944 reprepare_packed_git(the_repository);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001945
1946 if (!args->cloning && args->deepen) {
1947 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1948 struct ref *iterator = ref_cpy;
1949 opt.shallow_file = alternate_shallow_file;
1950 if (args->deepen)
1951 opt.is_deepening_fetch = 1;
1952 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1953 error(_("remote did not send all necessary objects"));
1954 free_refs(ref_cpy);
1955 ref_cpy = NULL;
Taylor Blau37b9dca2020-04-22 18:25:45 -06001956 rollback_shallow_file(the_repository, &shallow_lock);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001957 goto cleanup;
1958 }
1959 args->connectivity_checked = 1;
1960 }
1961
Jonathan Tane2842b32018-08-01 13:13:20 -07001962 update_shallow(args, sought, nr_sought, &si);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001963cleanup:
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001964 clear_shallow_info(&si);
Jonathan Tan13390782019-03-26 12:31:21 -07001965 oid_array_clear(&shallows_scratch);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001966 return ref_cpy;
1967}
Matt McCutchene860d962017-02-22 11:01:22 -05001968
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001969static int add_to_object_array(const struct object_id *oid, void *data)
1970{
1971 struct object_array *a = data;
1972
1973 add_object_array(lookup_object(the_repository, oid), "", a);
1974 return 0;
1975}
1976
1977static void clear_common_flag(struct oidset *s)
1978{
1979 struct oidset_iter iter;
1980 const struct object_id *oid;
1981 oidset_iter_init(s, &iter);
1982
1983 while ((oid = oidset_iter_next(&iter))) {
1984 struct object *obj = lookup_object(the_repository, oid);
1985 obj->flags &= ~COMMON;
1986 }
1987}
1988
1989void negotiate_using_fetch(const struct oid_array *negotiation_tips,
1990 const struct string_list *server_options,
1991 int stateless_rpc,
1992 int fd[],
1993 struct oidset *acked_commits)
1994{
1995 struct fetch_negotiator negotiator;
1996 struct packet_reader reader;
1997 struct object_array nt_object_array = OBJECT_ARRAY_INIT;
1998 struct strbuf req_buf = STRBUF_INIT;
1999 int haves_to_send = INITIAL_FLUSH;
2000 int in_vain = 0;
2001 int seen_ack = 0;
2002 int last_iteration = 0;
2003 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2004
2005 fetch_negotiator_init(the_repository, &negotiator);
2006 mark_tips(&negotiator, negotiation_tips);
2007
2008 packet_reader_init(&reader, fd[0], NULL, 0,
2009 PACKET_READ_CHOMP_NEWLINE |
2010 PACKET_READ_DIE_ON_ERR_PACKET);
2011
2012 oid_array_for_each((struct oid_array *) negotiation_tips,
2013 add_to_object_array,
2014 &nt_object_array);
2015
2016 while (!last_iteration) {
2017 int haves_added;
2018 struct object_id common_oid;
2019 int received_ready = 0;
2020
2021 strbuf_reset(&req_buf);
2022 write_fetch_command_and_capabilities(&req_buf, server_options);
2023
2024 packet_buf_write(&req_buf, "wait-for-done");
2025
2026 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2027 in_vain += haves_added;
2028 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2029 last_iteration = 1;
2030
2031 /* Send request */
2032 packet_buf_flush(&req_buf);
2033 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2034 die_errno(_("unable to write request to remote"));
2035
2036 /* Process ACKs/NAKs */
2037 process_section_header(&reader, "acknowledgments", 0);
2038 while (process_ack(&negotiator, &reader, &common_oid,
2039 &received_ready)) {
2040 struct commit *commit = lookup_commit(the_repository,
2041 &common_oid);
2042 if (commit) {
2043 timestamp_t generation;
2044
2045 parse_commit_or_die(commit);
2046 commit->object.flags |= COMMON;
2047 generation = commit_graph_generation(commit);
2048 if (generation < min_generation)
2049 min_generation = generation;
2050 }
2051 in_vain = 0;
2052 seen_ack = 1;
2053 oidset_insert(acked_commits, &common_oid);
2054 }
2055 if (received_ready)
2056 die(_("unexpected 'ready' from remote"));
2057 else
2058 do_check_stateless_delimiter(stateless_rpc, &reader);
2059 if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2060 REACH_SCRATCH, 0,
2061 min_generation))
2062 last_iteration = 1;
2063 }
2064 clear_common_flag(acked_commits);
2065 strbuf_release(&req_buf);
2066}
2067
Matt McCutchene860d962017-02-22 11:01:22 -05002068int report_unmatched_refs(struct ref **sought, int nr_sought)
2069{
2070 int i, ret = 0;
2071
2072 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -05002073 if (!sought[i])
Matt McCutchene860d962017-02-22 11:01:22 -05002074 continue;
Matt McCutchend56583d2017-02-22 11:05:57 -05002075 switch (sought[i]->match_status) {
2076 case REF_MATCHED:
2077 continue;
2078 case REF_NOT_MATCHED:
2079 error(_("no such remote ref %s"), sought[i]->name);
2080 break;
2081 case REF_UNADVERTISED_NOT_ALLOWED:
2082 error(_("Server does not allow request for unadvertised object %s"),
2083 sought[i]->name);
2084 break;
2085 }
Matt McCutchene860d962017-02-22 11:01:22 -05002086 ret = 1;
2087 }
2088 return ret;
2089}