blob: 04016d1e3250f28ae6cd53a784913f02879769d8 [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"
Jeff King2a4aed42021-11-19 15:58:55 -050028#include "sigchain.h"
René Scharfe6fc9fec2022-07-16 18:59:59 +020029#include "mergesort.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070030
31static int transfer_unpack_limit = -1;
32static int fetch_unpack_limit = -1;
33static int unpack_limit = 100;
34static int prefer_ofs_delta = 1;
35static int no_done;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070036static int deepen_since_ok;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070037static int deepen_not_ok;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070038static int fetch_fsck_objects = -1;
39static int transfer_fsck_objects = -1;
40static int agent_supported;
Jeff Hostetler640d8b72017-12-08 15:58:40 +000041static int server_supports_filtering;
Josh Steadmon1e905bb2020-11-11 15:29:31 -080042static int advertise_sid;
Taylor Blaucac4b8e2020-04-30 13:48:57 -060043static struct shallow_lock shallow_lock;
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +070044static const char *alternate_shallow_file;
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +020045static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +000046static struct strbuf fsck_msg_types = STRBUF_INIT;
Jonathan Tandd4b7322020-06-10 13:57:23 -070047static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070048
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070049/* Remember to update object flag allocation in object.h */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070050#define COMPLETE (1U << 0)
Jonathan Tanec062832018-06-14 15:54:28 -070051#define ALTERNATE (1U << 1)
Jonathan Tan9c1e6572021-05-04 14:16:01 -070052#define COMMON (1U << 6)
53#define REACH_SCRATCH (1U << 7)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070054
55/*
56 * After sending this many "have"s if we do not get any new ACK , we
57 * give up traversing our history.
58 */
59#define MAX_IN_VAIN 256
60
Jonathan Tand30fe892018-06-14 15:54:26 -070061static int multi_ack, use_sideband;
Fredrik Medley7199c092015-05-21 22:23:38 +020062/* Allow specifying sha1 if it is a ref tip. */
63#define ALLOW_TIP_SHA1 01
Fredrik Medley68ee6282015-05-21 22:23:39 +020064/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
65#define ALLOW_REACHABLE_SHA1 02
Fredrik Medley7199c092015-05-21 22:23:38 +020066static unsigned int allow_unadvertised_object_request;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070067
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +070068__attribute__((format (printf, 2, 3)))
69static inline void print_verbose(const struct fetch_pack_args *args,
70 const char *fmt, ...)
71{
72 va_list params;
73
74 if (!args->verbose)
75 return;
76
77 va_start(params, fmt);
78 vfprintf(stderr, fmt, params);
79 va_end(params);
80 fputc('\n', stderr);
81}
82
Jeff King41a078c2017-02-08 15:53:03 -050083struct alternate_object_cache {
84 struct object **items;
85 size_t nr, alloc;
86};
87
Jeff Kingbdf42762018-10-08 11:09:23 -070088static void cache_one_alternate(const struct object_id *oid,
Jeff King41a078c2017-02-08 15:53:03 -050089 void *vcache)
90{
91 struct alternate_object_cache *cache = vcache;
Stefan Beller109cd762018-06-28 18:21:51 -070092 struct object *obj = parse_object(the_repository, oid);
Jeff King41a078c2017-02-08 15:53:03 -050093
94 if (!obj || (obj->flags & ALTERNATE))
95 return;
96
97 obj->flags |= ALTERNATE;
98 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
99 cache->items[cache->nr++] = obj;
100}
101
Jonathan Tanec062832018-06-14 15:54:28 -0700102static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
103 void (*cb)(struct fetch_negotiator *,
Jonathan Tand30fe892018-06-14 15:54:26 -0700104 struct object *))
Jeff King41a078c2017-02-08 15:53:03 -0500105{
106 static int initialized;
107 static struct alternate_object_cache cache;
108 size_t i;
109
110 if (!initialized) {
111 for_each_alternate_ref(cache_one_alternate, &cache);
112 initialized = 1;
113 }
114
115 for (i = 0; i < cache.nr; i++)
Jonathan Tanec062832018-06-14 15:54:28 -0700116 cb(negotiator, cache.items[i]);
Jeff King41a078c2017-02-08 15:53:03 -0500117}
118
Ævar Arnfjörð Bjarmasona6e65fb2022-05-16 20:11:00 +0000119static struct commit *deref_without_lazy_fetch_extended(const struct object_id *oid,
120 int mark_tags_complete,
121 enum object_type *type,
122 unsigned int oi_flags)
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700123{
Ævar Arnfjörð Bjarmasona6e65fb2022-05-16 20:11:00 +0000124 struct object_info info = { .typep = type };
Patrick Steinhardt62b5a352021-09-01 15:09:54 +0200125 struct commit *commit;
126
127 commit = lookup_commit_in_graph(the_repository, oid);
128 if (commit)
129 return commit;
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700130
131 while (1) {
132 if (oid_object_info_extended(the_repository, oid, &info,
Ævar Arnfjörð Bjarmasona6e65fb2022-05-16 20:11:00 +0000133 oi_flags))
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700134 return NULL;
Ævar Arnfjörð Bjarmasona6e65fb2022-05-16 20:11:00 +0000135 if (*type == OBJ_TAG) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700136 struct tag *tag = (struct tag *)
137 parse_object(the_repository, oid);
138
139 if (!tag->tagged)
140 return NULL;
141 if (mark_tags_complete)
142 tag->object.flags |= COMPLETE;
143 oid = &tag->tagged->oid;
144 } else {
145 break;
146 }
147 }
Patrick Steinhardt3e5e6c62021-08-04 15:56:11 +0200148
Ævar Arnfjörð Bjarmasona6e65fb2022-05-16 20:11:00 +0000149 if (*type == OBJ_COMMIT) {
Patrick Steinhardt3e5e6c62021-08-04 15:56:11 +0200150 struct commit *commit = lookup_commit(the_repository, oid);
151 if (!commit || repo_parse_commit(the_repository, commit))
152 return NULL;
153 return commit;
154 }
155
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700156 return NULL;
157}
158
Ævar Arnfjörð Bjarmasona6e65fb2022-05-16 20:11:00 +0000159
160static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
161 int mark_tags_complete)
162{
163 enum object_type type;
164 unsigned flags = OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK;
165 return deref_without_lazy_fetch_extended(oid, mark_tags_complete,
166 &type, flags);
167}
168
Jonathan Tanec062832018-06-14 15:54:28 -0700169static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700170 const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700171{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700172 struct commit *c = deref_without_lazy_fetch(oid, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700173
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700174 if (c)
175 negotiator->add_tip(negotiator, c);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700176 return 0;
177}
178
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200179static int rev_list_insert_ref_oid(const char *refname UNUSED,
Jeff King63e14ee2022-08-19 06:08:32 -0400180 const struct object_id *oid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200181 int flag UNUSED,
Jeff King63e14ee2022-08-19 06:08:32 -0400182 void *cb_data)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700183{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700184 return rev_list_insert_ref(cb_data, oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700185}
186
187enum ack_type {
188 NAK = 0,
189 ACK,
190 ACK_continue,
191 ACK_common,
192 ACK_ready
193};
194
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800195static void consume_shallow_list(struct fetch_pack_args *args,
196 struct packet_reader *reader)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700197{
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700198 if (args->stateless_rpc && args->deepen) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700199 /* If we sent a depth we will get back "duplicate"
200 * shallow and unshallow commands every time there
201 * is a block of have lines exchanged.
202 */
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800203 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
204 if (starts_with(reader->line, "shallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700205 continue;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800206 if (starts_with(reader->line, "unshallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700207 continue;
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700208 die(_("git fetch-pack: expected shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700209 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800210 if (reader->status != PACKET_READ_FLUSH)
211 die(_("git fetch-pack: expected a flush packet after shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700212 }
213}
214
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800215static enum ack_type get_ack(struct packet_reader *reader,
216 struct object_id *result_oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700217{
Jeff King74543a02013-02-20 15:02:57 -0500218 int len;
Jeff King82e56762014-06-18 15:56:03 -0400219 const char *arg;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700220
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800221 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
Jeff Kingbc9d4dc2018-02-08 13:47:49 -0500222 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800223 len = reader->pktlen;
224
225 if (!strcmp(reader->line, "NAK"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700226 return NAK;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800227 if (skip_prefix(reader->line, "ACK ", &arg)) {
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000228 const char *p;
229 if (!parse_oid_hex(arg, result_oid, &p)) {
230 len -= p - reader->line;
Jeff King82e56762014-06-18 15:56:03 -0400231 if (len < 1)
Jeff King030e9dd2013-02-20 15:00:28 -0500232 return ACK;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000233 if (strstr(p, "continue"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700234 return ACK_continue;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000235 if (strstr(p, "common"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700236 return ACK_common;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000237 if (strstr(p, "ready"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700238 return ACK_ready;
239 return ACK;
240 }
241 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800242 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700243}
244
245static void send_request(struct fetch_pack_args *args,
246 int fd, struct strbuf *buf)
247{
248 if (args->stateless_rpc) {
249 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
250 packet_flush(fd);
Jeff King37c80012019-03-04 23:11:39 -0500251 } else {
252 if (write_in_full(fd, buf->buf, buf->len) < 0)
253 die_errno(_("unable to write to remote"));
254 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700255}
256
Jonathan Tanec062832018-06-14 15:54:28 -0700257static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700258 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700259{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700260 rev_list_insert_ref(negotiator, &obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700261}
262
263#define INITIAL_FLUSH 16
264#define PIPESAFE_FLUSH 32
Jonathan Tanda470982016-07-18 15:21:38 -0700265#define LARGE_FLUSH 16384
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700266
Brandon Williams685fbd32018-03-15 10:31:28 -0700267static int next_flush(int stateless_rpc, int count)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700268{
Brandon Williams685fbd32018-03-15 10:31:28 -0700269 if (stateless_rpc) {
Jonathan Tanda470982016-07-18 15:21:38 -0700270 if (count < LARGE_FLUSH)
271 count <<= 1;
272 else
273 count = count * 11 / 10;
274 } else {
275 if (count < PIPESAFE_FLUSH)
276 count <<= 1;
277 else
278 count += PIPESAFE_FLUSH;
279 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700280 return count;
281}
282
Jonathan Tan3390e422018-07-02 15:39:44 -0700283static void mark_tips(struct fetch_negotiator *negotiator,
284 const struct oid_array *negotiation_tips)
285{
286 int i;
287
288 if (!negotiation_tips) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700289 for_each_rawref(rev_list_insert_ref_oid, negotiator);
Jonathan Tan3390e422018-07-02 15:39:44 -0700290 return;
291 }
292
293 for (i = 0; i < negotiation_tips->nr; i++)
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700294 rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]);
Jonathan Tan3390e422018-07-02 15:39:44 -0700295 return;
296}
297
Jonathan Tan10075572022-07-26 09:27:11 -0700298static void send_filter(struct fetch_pack_args *args,
299 struct strbuf *req_buf,
300 int server_supports_filter)
301{
302 if (args->filter_options.choice) {
303 const char *spec =
304 expand_list_objects_filter_spec(&args->filter_options);
305 if (server_supports_filter) {
306 print_verbose(args, _("Server supports filter"));
307 packet_buf_write(req_buf, "filter %s", spec);
308 trace2_data_string("fetch", the_repository,
309 "filter/effective", spec);
310 } else {
311 warning("filtering not recognized by server, ignoring");
312 trace2_data_string("fetch", the_repository,
313 "filter/unsupported", spec);
314 }
315 } else {
316 trace2_data_string("fetch", the_repository,
317 "filter/none", "");
318 }
319}
320
Jonathan Tanec062832018-06-14 15:54:28 -0700321static int find_common(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700322 struct fetch_pack_args *args,
brian m. carlson1b283372017-05-01 02:28:54 +0000323 int fd[2], struct object_id *result_oid,
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700324 struct ref *refs)
325{
326 int fetching;
327 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
Josh Steadmona29263c2022-08-02 15:04:05 -0700328 int negotiation_round = 0, haves = 0;
brian m. carlson1b283372017-05-01 02:28:54 +0000329 const struct object_id *oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700330 unsigned in_vain = 0;
331 int got_continue = 0;
332 int got_ready = 0;
333 struct strbuf req_buf = STRBUF_INIT;
334 size_t state_len = 0;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800335 struct packet_reader reader;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700336
337 if (args->stateless_rpc && multi_ack == 1)
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000338 die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700339
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800340 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -0800341 PACKET_READ_CHOMP_NEWLINE |
342 PACKET_READ_DIE_ON_ERR_PACKET);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800343
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700344 mark_tips(negotiator, args->negotiation_tips);
345 for_each_cached_alternate(negotiator, insert_one_alternate_object);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700346
347 fetching = 0;
348 for ( ; refs ; refs = refs->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000349 struct object_id *remote = &refs->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700350 const char *remote_hex;
351 struct object *o;
352
Robert Coup4dfd0922022-03-28 14:02:06 +0000353 if (!args->refetch) {
354 /*
355 * If that object is complete (i.e. it is an ancestor of a
356 * local ref), we tell them we have it but do not have to
357 * tell them about its ancestors, which they already know
358 * about.
359 *
360 * We use lookup_object here because we are only
361 * interested in the case we *know* the object is
362 * reachable and we have already scanned it.
363 */
364 if (((o = lookup_object(the_repository, remote)) != NULL) &&
365 (o->flags & COMPLETE)) {
366 continue;
367 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700368 }
369
brian m. carlson1b283372017-05-01 02:28:54 +0000370 remote_hex = oid_to_hex(remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700371 if (!fetching) {
372 struct strbuf c = STRBUF_INIT;
373 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
374 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
375 if (no_done) strbuf_addstr(&c, " no-done");
376 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
377 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700378 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700379 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
380 if (args->no_progress) strbuf_addstr(&c, " no-progress");
381 if (args->include_tag) strbuf_addstr(&c, " include-tag");
382 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700383 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700384 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700385 if (agent_supported) strbuf_addf(&c, " agent=%s",
386 git_user_agent_sanitized());
Josh Steadmon1e905bb2020-11-11 15:29:31 -0800387 if (advertise_sid)
388 strbuf_addf(&c, " session-id=%s", trace2_session_id());
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000389 if (args->filter_options.choice)
390 strbuf_addstr(&c, " filter");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700391 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
392 strbuf_release(&c);
393 } else
394 packet_buf_write(&req_buf, "want %s\n", remote_hex);
395 fetching++;
396 }
397
398 if (!fetching) {
399 strbuf_release(&req_buf);
400 packet_flush(fd[1]);
401 return 1;
402 }
403
Stefan Bellerc8813482018-05-17 15:51:46 -0700404 if (is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700405 write_shallow_commits(&req_buf, 1, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700406 if (args->depth > 0)
407 packet_buf_write(&req_buf, "deepen %d", args->depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700408 if (args->deepen_since) {
Johannes Schindelindddbad72017-04-26 21:29:31 +0200409 timestamp_t max_age = approxidate(args->deepen_since);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200410 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700411 }
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700412 if (args->deepen_not) {
413 int i;
414 for (i = 0; i < args->deepen_not->nr; i++) {
415 struct string_list_item *s = args->deepen_not->items + i;
416 packet_buf_write(&req_buf, "deepen-not %s", s->string);
417 }
418 }
Jonathan Tan10075572022-07-26 09:27:11 -0700419 send_filter(args, &req_buf, server_supports_filtering);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700420 packet_buf_flush(&req_buf);
421 state_len = req_buf.len;
422
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700423 if (args->deepen) {
Jeff Kingae021d82014-06-18 15:47:50 -0400424 const char *arg;
brian m. carlson1b283372017-05-01 02:28:54 +0000425 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700426
427 send_request(args, fd[1], &req_buf);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800428 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
429 if (skip_prefix(reader.line, "shallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000430 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800431 die(_("invalid shallow line: %s"), reader.line);
Stefan Beller19143f12018-05-17 15:51:44 -0700432 register_shallow(the_repository, &oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700433 continue;
434 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800435 if (skip_prefix(reader.line, "unshallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000436 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800437 die(_("invalid unshallow line: %s"), reader.line);
Jeff Kingd0229ab2019-06-20 03:41:14 -0400438 if (!lookup_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800439 die(_("object not found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700440 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -0700441 if (!parse_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800442 die(_("error in object: %s"), reader.line);
brian m. carlsone92b8482017-05-06 22:10:06 +0000443 if (unregister_shallow(&oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800444 die(_("no shallow found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700445 continue;
446 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800447 die(_("expected shallow/unshallow, got %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700448 }
449 } else if (!args->stateless_rpc)
450 send_request(args, fd[1], &req_buf);
451
452 if (!args->stateless_rpc) {
453 /* If we aren't using the stateless-rpc interface
454 * we don't need to retain the headers.
455 */
456 strbuf_setlen(&req_buf, 0);
457 state_len = 0;
458 }
459
Josh Steadmon5fc31182019-10-02 16:49:28 -0700460 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700461 flushes = 0;
462 retval = -1;
Jonathan Tanec062832018-06-14 15:54:28 -0700463 while ((oid = negotiator->next(negotiator))) {
brian m. carlson1b283372017-05-01 02:28:54 +0000464 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
465 print_verbose(args, "have %s", oid_to_hex(oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700466 in_vain++;
Josh Steadmona29263c2022-08-02 15:04:05 -0700467 haves++;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700468 if (flush_at <= ++count) {
469 int ack;
470
Josh Steadmona29263c2022-08-02 15:04:05 -0700471 negotiation_round++;
472 trace2_region_enter_printf("negotiation_v0_v1", "round",
473 the_repository, "%d",
474 negotiation_round);
475 trace2_data_intmax("negotiation_v0_v1", the_repository,
476 "haves_added", haves);
477 trace2_data_intmax("negotiation_v0_v1", the_repository,
478 "in_vain", in_vain);
479 haves = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700480 packet_buf_flush(&req_buf);
481 send_request(args, fd[1], &req_buf);
482 strbuf_setlen(&req_buf, state_len);
483 flushes++;
Brandon Williams685fbd32018-03-15 10:31:28 -0700484 flush_at = next_flush(args->stateless_rpc, count);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700485
486 /*
487 * We keep one window "ahead" of the other side, and
488 * will wait for an ACK only on the next one
489 */
490 if (!args->stateless_rpc && count == INITIAL_FLUSH)
491 continue;
492
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800493 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700494 do {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800495 ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700496 if (ack)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700497 print_verbose(args, _("got %s %d %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000498 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700499 switch (ack) {
500 case ACK:
Josh Steadmona29263c2022-08-02 15:04:05 -0700501 trace2_region_leave_printf("negotiation_v0_v1", "round",
502 the_repository, "%d",
503 negotiation_round);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700504 flushes = 0;
505 multi_ack = 0;
506 retval = 0;
507 goto done;
508 case ACK_common:
509 case ACK_ready:
510 case ACK_continue: {
511 struct commit *commit =
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700512 lookup_commit(the_repository,
513 result_oid);
Jonathan Tand093bc72018-06-14 15:54:27 -0700514 int was_common;
Junio C Hamano3a2a1dc2018-08-02 15:30:42 -0700515
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700516 if (!commit)
brian m. carlson1b283372017-05-01 02:28:54 +0000517 die(_("invalid commit %s"), oid_to_hex(result_oid));
Jonathan Tanec062832018-06-14 15:54:28 -0700518 was_common = negotiator->ack(negotiator, commit);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700519 if (args->stateless_rpc
520 && ack == ACK_common
Jonathan Tand093bc72018-06-14 15:54:27 -0700521 && !was_common) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700522 /* We need to replay the have for this object
523 * on the next RPC request so the peer knows
524 * it is in common with us.
525 */
brian m. carlson1b283372017-05-01 02:28:54 +0000526 const char *hex = oid_to_hex(result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700527 packet_buf_write(&req_buf, "have %s\n", hex);
528 state_len = req_buf.len;
Josh Steadmona29263c2022-08-02 15:04:05 -0700529 haves++;
Jonathan Tan06b3d382016-09-23 10:41:35 -0700530 /*
531 * Reset in_vain because an ack
532 * for this commit has not been
533 * seen.
534 */
535 in_vain = 0;
536 } else if (!args->stateless_rpc
537 || ack != ACK_common)
538 in_vain = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700539 retval = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700540 got_continue = 1;
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700541 if (ack == ACK_ready)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700542 got_ready = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700543 break;
544 }
545 }
546 } while (ack);
547 flushes--;
Josh Steadmona29263c2022-08-02 15:04:05 -0700548 trace2_region_leave_printf("negotiation_v0_v1", "round",
549 the_repository, "%d",
550 negotiation_round);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700551 if (got_continue && MAX_IN_VAIN < in_vain) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700552 print_verbose(args, _("giving up"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700553 break; /* give up */
554 }
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700555 if (got_ready)
556 break;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700557 }
558 }
559done:
Josh Steadmon5fc31182019-10-02 16:49:28 -0700560 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
Josh Steadmona29263c2022-08-02 15:04:05 -0700561 trace2_data_intmax("negotiation_v0_v1", the_repository, "total_rounds",
562 negotiation_round);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700563 if (!got_ready || !no_done) {
564 packet_buf_write(&req_buf, "done\n");
565 send_request(args, fd[1], &req_buf);
566 }
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700567 print_verbose(args, _("done"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700568 if (retval != 0) {
569 multi_ack = 0;
570 flushes++;
571 }
572 strbuf_release(&req_buf);
573
Nguyễn Thái Ngọc Duyff62eca2014-02-06 22:10:39 +0700574 if (!got_ready || !no_done)
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800575 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700576 while (flushes || multi_ack) {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800577 int ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700578 if (ack) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700579 print_verbose(args, _("got %s (%d) %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000580 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700581 if (ack == ACK)
582 return 0;
583 multi_ack = 1;
584 continue;
585 }
586 flushes--;
587 }
588 /* it is no error to fetch into a completely empty repo */
589 return count ? retval : 0;
590}
591
592static struct commit_list *complete;
593
brian m. carlson1b283372017-05-01 02:28:54 +0000594static int mark_complete(const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700595{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700596 struct commit *commit = deref_without_lazy_fetch(oid, 1);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700597
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700598 if (commit && !(commit->object.flags & COMPLETE)) {
599 commit->object.flags |= COMPLETE;
600 commit_list_insert(commit, &complete);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700601 }
602 return 0;
603}
604
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200605static int mark_complete_oid(const char *refname UNUSED,
Jeff King63e14ee2022-08-19 06:08:32 -0400606 const struct object_id *oid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200607 int flag UNUSED,
608 void *cb_data UNUSED)
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000609{
brian m. carlson1b283372017-05-01 02:28:54 +0000610 return mark_complete(oid);
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000611}
612
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700613static void mark_recent_complete_commits(struct fetch_pack_args *args,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200614 timestamp_t cutoff)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700615{
616 while (complete && cutoff <= complete->item->date) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700617 print_verbose(args, _("Marking %s as complete"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700618 oid_to_hex(&complete->item->object.oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700619 pop_most_recent_commit(&complete, COMPLETE);
620 }
621}
622
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700623static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
624{
625 for (; refs; refs = refs->next)
626 oidset_insert(oids, &refs->old_oid);
627}
628
René Scharfebf732822018-10-04 17:09:06 +0200629static int is_unmatched_ref(const struct ref *ref)
630{
631 struct object_id oid;
632 const char *p;
633 return ref->match_status == REF_NOT_MATCHED &&
634 !parse_oid_hex(ref->name, &oid, &p) &&
635 *p == '\0' &&
636 oideq(&oid, &ref->old_oid);
637}
638
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700639static void filter_refs(struct fetch_pack_args *args,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800640 struct ref **refs,
641 struct ref **sought, int nr_sought)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700642{
643 struct ref *newlist = NULL;
644 struct ref **newtail = &newlist;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700645 struct ref *unmatched = NULL;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700646 struct ref *ref, *next;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700647 struct oidset tip_oids = OIDSET_INIT;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800648 int i;
René Scharfe22a16462018-10-04 17:09:39 +0200649 int strict = !(allow_unadvertised_object_request &
650 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700651
Junio C Hamanof2db8542013-01-29 14:02:15 -0800652 i = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700653 for (ref = *refs; ref; ref = next) {
654 int keep = 0;
655 next = ref->next;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800656
René Scharfe50e19a82014-06-06 19:24:48 +0200657 if (starts_with(ref->name, "refs/") &&
Jeff King34066f02019-04-13 01:57:37 -0400658 check_refname_format(ref->name, 0)) {
659 /*
660 * trash or a peeled value; do not even add it to
661 * unmatched list
662 */
663 free_one_ref(ref);
664 continue;
665 } else {
Junio C Hamanof2db8542013-01-29 14:02:15 -0800666 while (i < nr_sought) {
667 int cmp = strcmp(ref->name, sought[i]->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700668 if (cmp < 0)
669 break; /* definitely do not have it */
670 else if (cmp == 0) {
671 keep = 1; /* definitely have it */
Matt McCutchend56583d2017-02-22 11:05:57 -0500672 sought[i]->match_status = REF_MATCHED;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700673 }
Junio C Hamanof2db8542013-01-29 14:02:15 -0800674 i++;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700675 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700676
Jeff Kinge9502c02018-06-11 01:53:57 -0400677 if (!keep && args->fetch_all &&
678 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
679 keep = 1;
680 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700681
682 if (keep) {
683 *newtail = ref;
684 ref->next = NULL;
685 newtail = &ref->next;
686 } else {
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700687 ref->next = unmatched;
688 unmatched = ref;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700689 }
690 }
691
René Scharfe22a16462018-10-04 17:09:39 +0200692 if (strict) {
693 for (i = 0; i < nr_sought; i++) {
694 ref = sought[i];
695 if (!is_unmatched_ref(ref))
696 continue;
697
698 add_refs_to_oidset(&tip_oids, unmatched);
699 add_refs_to_oidset(&tip_oids, newlist);
700 break;
701 }
702 }
703
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800704 /* Append unmatched requests to the list */
Matt McCutchend56583d2017-02-22 11:05:57 -0500705 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500706 ref = sought[i];
René Scharfebf732822018-10-04 17:09:06 +0200707 if (!is_unmatched_ref(ref))
Matt McCutchend56583d2017-02-22 11:05:57 -0500708 continue;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800709
René Scharfe22a16462018-10-04 17:09:39 +0200710 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500711 ref->match_status = REF_MATCHED;
Jeff Kingc3c17bf2015-03-19 16:37:09 -0400712 *newtail = copy_ref(ref);
713 newtail = &(*newtail)->next;
Matt McCutchend56583d2017-02-22 11:05:57 -0500714 } else {
715 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800716 }
717 }
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700718
719 oidset_clear(&tip_oids);
Jeff King259eddd2019-04-13 01:54:09 -0400720 free_refs(unmatched);
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700721
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700722 *refs = newlist;
723}
724
Jonathan Tanec062832018-06-14 15:54:28 -0700725static void mark_alternate_complete(struct fetch_negotiator *unused,
Jonathan Tand30fe892018-06-14 15:54:26 -0700726 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700727{
brian m. carlson1b283372017-05-01 02:28:54 +0000728 mark_complete(&obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700729}
730
Takuto Ikuta024aa462018-03-14 15:32:42 +0900731struct loose_object_iter {
732 struct oidset *loose_object_set;
733 struct ref *refs;
734};
735
736/*
Jonathan Tan34c29032018-06-06 13:47:07 -0700737 * Mark recent commits available locally and reachable from a local ref as
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700738 * COMPLETE.
Jonathan Tan34c29032018-06-06 13:47:07 -0700739 *
740 * The cutoff time for recency is determined by this heuristic: it is the
741 * earliest commit time of the objects in refs that are commits and that we know
742 * the commit time of.
743 */
Jonathan Tanec062832018-06-14 15:54:28 -0700744static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700745 struct fetch_pack_args *args,
Jonathan Tan34c29032018-06-06 13:47:07 -0700746 struct ref **refs)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700747{
748 struct ref *ref;
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000749 int old_save_commit_buffer = save_commit_buffer;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200750 timestamp_t cutoff = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700751
Robert Coup4dfd0922022-03-28 14:02:06 +0000752 if (args->refetch)
753 return;
754
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700755 save_commit_buffer = 0;
756
Erik Chen9e5afdf2019-11-19 23:02:09 +0000757 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700758 for (ref = *refs; ref; ref = ref->next) {
Patrick Steinhardt6fd1cc82022-02-10 13:28:09 +0100759 struct commit *commit;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700760
Patrick Steinhardt6fd1cc82022-02-10 13:28:09 +0100761 commit = lookup_commit_in_graph(the_repository, &ref->old_oid);
762 if (!commit) {
763 struct object *o;
764
765 if (!has_object_file_with_flags(&ref->old_oid,
Jonathan Tan6462d5e2019-11-05 10:56:19 -0800766 OBJECT_INFO_QUICK |
Patrick Steinhardt6fd1cc82022-02-10 13:28:09 +0100767 OBJECT_INFO_SKIP_FETCH_OBJECT))
768 continue;
769 o = parse_object(the_repository, &ref->old_oid);
770 if (!o || o->type != OBJ_COMMIT)
771 continue;
772
773 commit = (struct commit *)o;
774 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700775
Erik Chen9e5afdf2019-11-19 23:02:09 +0000776 /*
777 * We already have it -- which may mean that we were
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700778 * in sync with the other side at some time after
779 * that (it is OK if we guess wrong here).
780 */
Patrick Steinhardt6fd1cc82022-02-10 13:28:09 +0100781 if (!cutoff || cutoff < commit->date)
782 cutoff = commit->date;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700783 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000784 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700785
Erik Chen9e5afdf2019-11-19 23:02:09 +0000786 /*
787 * This block marks all local refs as COMPLETE, and then recursively marks all
788 * parents of those refs as COMPLETE.
789 */
790 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700791 if (!args->deepen) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700792 for_each_rawref(mark_complete_oid, NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700793 for_each_cached_alternate(NULL, mark_alternate_complete);
794 commit_list_sort_by_date(&complete);
795 if (cutoff)
796 mark_recent_complete_commits(args, cutoff);
797 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000798 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700799
Jonathan Tan12f19a92018-10-03 16:04:52 -0700800 /*
801 * Mark all complete remote refs as common refs.
802 * Don't mark them common yet; the server has to be told so first.
803 */
Erik Chen9e5afdf2019-11-19 23:02:09 +0000804 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700805 for (ref = *refs; ref; ref = ref->next) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700806 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700807
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700808 if (!c || !(c->object.flags & COMPLETE))
Jonathan Tan12f19a92018-10-03 16:04:52 -0700809 continue;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700810
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700811 negotiator->known_common(negotiator, c);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700812 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000813 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700814
Jonathan Tan34c29032018-06-06 13:47:07 -0700815 save_commit_buffer = old_save_commit_buffer;
816}
817
818/*
819 * Returns 1 if every object pointed to by the given remote refs is available
820 * locally and reachable from a local ref, and 0 otherwise.
821 */
822static int everything_local(struct fetch_pack_args *args,
823 struct ref **refs)
824{
825 struct ref *ref;
826 int retval;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700827
828 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000829 const struct object_id *remote = &ref->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700830 struct object *o;
831
Jeff Kingd0229ab2019-06-20 03:41:14 -0400832 o = lookup_object(the_repository, remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700833 if (!o || !(o->flags & COMPLETE)) {
834 retval = 0;
brian m. carlson1b283372017-05-01 02:28:54 +0000835 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700836 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700837 continue;
838 }
brian m. carlson1b283372017-05-01 02:28:54 +0000839 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700840 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700841 }
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000842
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700843 return retval;
844}
845
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200846static int sideband_demux(int in UNUSED, int out, void *data)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700847{
848 int *xd = data;
Jeff King9ff18fa2016-02-24 02:44:58 -0500849 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700850
Jeff King9ff18fa2016-02-24 02:44:58 -0500851 ret = recv_sideband("fetch-pack", xd[0], out);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700852 close(out);
853 return ret;
854}
855
Christian Couder9d7fa3b2021-01-12 09:21:58 +0100856static void create_promisor_file(const char *keep_name,
857 struct ref **sought, int nr_sought)
Jonathan Tan5374a292019-10-14 17:12:31 -0700858{
859 struct strbuf promisor_name = STRBUF_INIT;
860 int suffix_stripped;
Jonathan Tan5374a292019-10-14 17:12:31 -0700861
862 strbuf_addstr(&promisor_name, keep_name);
863 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
864 if (!suffix_stripped)
865 BUG("name of pack lockfile should end with .keep (was '%s')",
866 keep_name);
867 strbuf_addstr(&promisor_name, ".promisor");
868
Christian Couder33add2a2021-01-12 09:21:59 +0100869 write_promisor_file(promisor_name.buf, sought, nr_sought);
Jonathan Tan5374a292019-10-14 17:12:31 -0700870
871 strbuf_release(&promisor_name);
872}
873
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800874static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids)
875{
876 int len = the_hash_algo->hexsz + 1; /* hash + NL */
877
878 do {
879 char hex_hash[GIT_MAX_HEXSZ + 1];
880 int read_len = read_in_full(fd, hex_hash, len);
881 struct object_id oid;
882 const char *end;
883
884 if (!read_len)
885 return;
886 if (read_len != len)
887 die("invalid length read %d", read_len);
888 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
889 die("invalid hash");
890 oidset_insert(gitmodules_oids, &oid);
891 } while (1);
892}
893
Ævar Arnfjörð Bjarmason1f6cf452022-05-16 20:11:01 +0000894static void add_index_pack_keep_option(struct strvec *args)
895{
896 char hostname[HOST_NAME_MAX + 1];
897
898 if (xgethostname(hostname, sizeof(hostname)))
899 xsnprintf(hostname, sizeof(hostname), "localhost");
900 strvec_pushf(args, "--keep=fetch-pack %"PRIuMAX " on %s",
901 (uintmax_t)getpid(), hostname);
902}
903
Jonathan Tanece9aea2020-08-17 12:48:19 -0700904/*
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800905 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
906 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
907 * stored there. (It must be freed by the caller.)
Jonathan Tanece9aea2020-08-17 12:48:19 -0700908 */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700909static int get_pack(struct fetch_pack_args *args,
Jonathan Tan9da69a62020-06-10 13:57:22 -0700910 int xd[2], struct string_list *pack_lockfiles,
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800911 struct strvec *index_pack_args,
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800912 struct ref **sought, int nr_sought,
913 struct oidset *gitmodules_oids)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700914{
915 struct async demux;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700916 int do_keep = args->keep_pack;
Jeff King984a43b2015-09-24 17:07:54 -0400917 const char *cmd_name;
918 struct pack_header header;
919 int pass_header = 0;
René Scharfed3180272014-08-19 21:09:35 +0200920 struct child_process cmd = CHILD_PROCESS_INIT;
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800921 int fsck_objects = 0;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700922 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700923
924 memset(&demux, 0, sizeof(demux));
925 if (use_sideband) {
926 /* xd[] is talking with upload-pack; subprocess reads from
927 * xd[0], spits out band#2 to stderr, and feeds us band#1
928 * through demux->out.
929 */
930 demux.proc = sideband_demux;
931 demux.data = xd;
932 demux.out = -1;
Jeff Kingdf857572016-04-19 18:50:29 -0400933 demux.isolate_sigpipe = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700934 if (start_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700935 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700936 }
937 else
938 demux.out = xd[0];
939
Jonathan Tan2aec3bc2021-03-04 17:16:20 -0800940 if (!args->keep_pack && unpack_limit && !index_pack_args) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700941
942 if (read_pack_header(demux.out, &header))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700943 die(_("protocol error: bad pack header"));
Jeff King984a43b2015-09-24 17:07:54 -0400944 pass_header = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700945 if (ntohl(header.hdr_entries) < unpack_limit)
946 do_keep = 0;
947 else
948 do_keep = 1;
949 }
950
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700951 if (alternate_shallow_file) {
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400952 strvec_push(&cmd.args, "--shallow-file");
953 strvec_push(&cmd.args, alternate_shallow_file);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700954 }
955
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800956 if (fetch_fsck_objects >= 0
957 ? fetch_fsck_objects
958 : transfer_fsck_objects >= 0
959 ? transfer_fsck_objects
960 : 0)
961 fsck_objects = 1;
962
963 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) {
964 if (pack_lockfiles || fsck_objects)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700965 cmd.out = -1;
Jeff King984a43b2015-09-24 17:07:54 -0400966 cmd_name = "index-pack";
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400967 strvec_push(&cmd.args, cmd_name);
968 strvec_push(&cmd.args, "--stdin");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700969 if (!args->quiet && !args->no_progress)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400970 strvec_push(&cmd.args, "-v");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700971 if (args->use_thin_pack)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400972 strvec_push(&cmd.args, "--fix-thin");
Ævar Arnfjörð Bjarmason1f6cf452022-05-16 20:11:01 +0000973 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit))
974 add_index_pack_keep_option(&cmd.args);
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800975 if (!index_pack_args && args->check_self_contained_and_connected)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400976 strvec_push(&cmd.args, "--check-self-contained-and-connected");
Jonathan Tandd4b7322020-06-10 13:57:23 -0700977 else
978 /*
979 * We cannot perform any connectivity checks because
980 * not all packs have been downloaded; let the caller
981 * have this responsibility.
982 */
983 args->check_self_contained_and_connected = 0;
Jonathan Tan1b03df52020-08-20 10:51:16 -0700984
985 if (args->from_promisor)
986 /*
Christian Couder9d7fa3b2021-01-12 09:21:58 +0100987 * create_promisor_file() may be called afterwards but
Jonathan Tan1b03df52020-08-20 10:51:16 -0700988 * we still need index-pack to know that this is a
989 * promisor pack. For example, if transfer.fsckobjects
990 * is true, index-pack needs to know that .gitmodules
991 * is a promisor object (so that it won't complain if
992 * it is missing).
993 */
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400994 strvec_push(&cmd.args, "--promisor");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700995 }
996 else {
Jeff King984a43b2015-09-24 17:07:54 -0400997 cmd_name = "unpack-objects";
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400998 strvec_push(&cmd.args, cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700999 if (args->quiet || args->no_progress)
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001000 strvec_push(&cmd.args, "-q");
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +07001001 args->check_self_contained_and_connected = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001002 }
Jeff King984a43b2015-09-24 17:07:54 -04001003
1004 if (pass_header)
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001005 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
Jeff Kingf6d89422020-07-28 16:26:31 -04001006 ntohl(header.hdr_version),
Jeff King984a43b2015-09-24 17:07:54 -04001007 ntohl(header.hdr_entries));
Jonathan Tan5476e1e2021-02-22 11:20:09 -08001008 if (fsck_objects) {
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001009 if (args->from_promisor || index_pack_args)
Jonathan Tan98a2ea42018-03-14 11:42:41 -07001010 /*
1011 * We cannot use --strict in index-pack because it
1012 * checks both broken objects and links, but we only
1013 * want to check for broken objects.
1014 */
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001015 strvec_push(&cmd.args, "--fsck-objects");
Jonathan Tan98a2ea42018-03-14 11:42:41 -07001016 else
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001017 strvec_pushf(&cmd.args, "--strict%s",
Jeff Kingf6d89422020-07-28 16:26:31 -04001018 fsck_msg_types.buf);
Jonathan Tan98a2ea42018-03-14 11:42:41 -07001019 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001020
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001021 if (index_pack_args) {
1022 int i;
1023
1024 for (i = 0; i < cmd.args.nr; i++)
1025 strvec_push(index_pack_args, cmd.args.v[i]);
1026 }
1027
Jeff King2a4aed42021-11-19 15:58:55 -05001028 sigchain_push(SIGPIPE, SIG_IGN);
1029
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001030 cmd.in = demux.out;
1031 cmd.git_cmd = 1;
1032 if (start_command(&cmd))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001033 die(_("fetch-pack: unable to fork off %s"), cmd_name);
Jonathan Tan5476e1e2021-02-22 11:20:09 -08001034 if (do_keep && (pack_lockfiles || fsck_objects)) {
1035 int is_well_formed;
1036 char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed);
1037
1038 if (!is_well_formed)
1039 die(_("fetch-pack: invalid index-pack output"));
René Scharfe6031af32020-11-30 20:27:15 +01001040 if (pack_lockfile)
1041 string_list_append_nodup(pack_lockfiles, pack_lockfile);
Jonathan Tan5476e1e2021-02-22 11:20:09 -08001042 parse_gitmodules_oids(cmd.out, gitmodules_oids);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001043 close(cmd.out);
1044 }
1045
Jens Lindstrom37cb1dd2013-10-22 15:36:02 +02001046 if (!use_sideband)
1047 /* Closed by start_command() */
1048 xd[0] = -1;
1049
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +07001050 ret = finish_command(&cmd);
1051 if (!ret || (args->check_self_contained_and_connected && ret == 1))
1052 args->self_contained_and_connected =
1053 args->check_self_contained_and_connected &&
1054 ret == 0;
1055 else
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001056 die(_("%s failed"), cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001057 if (use_sideband && finish_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001058 die(_("error in sideband demultiplexer"));
Jonathan Tan5374a292019-10-14 17:12:31 -07001059
Jeff King2a4aed42021-11-19 15:58:55 -05001060 sigchain_pop(SIGPIPE);
1061
Jonathan Tan5374a292019-10-14 17:12:31 -07001062 /*
1063 * Now that index-pack has succeeded, write the promisor file using the
1064 * obtained .keep filename if necessary
1065 */
Jonathan Tan9da69a62020-06-10 13:57:22 -07001066 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
Christian Couder9d7fa3b2021-01-12 09:21:58 +01001067 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
Jonathan Tan5374a292019-10-14 17:12:31 -07001068
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001069 return 0;
1070}
1071
René Scharfe6fc9fec2022-07-16 18:59:59 +02001072static int ref_compare_name(const struct ref *a, const struct ref *b)
1073{
1074 return strcmp(a->name, b->name);
1075}
1076
1077DEFINE_LIST_SORT(static, sort_ref_list, struct ref, next);
1078
Junio C Hamanof2db8542013-01-29 14:02:15 -08001079static int cmp_ref_by_name(const void *a_, const void *b_)
1080{
1081 const struct ref *a = *((const struct ref **)a_);
1082 const struct ref *b = *((const struct ref **)b_);
1083 return strcmp(a->name, b->name);
1084}
1085
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001086static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1087 int fd[2],
1088 const struct ref *orig_ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -08001089 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001090 struct shallow_info *si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001091 struct string_list *pack_lockfiles)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001092{
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001093 struct repository *r = the_repository;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001094 struct ref *ref = copy_ref_list(orig_ref);
brian m. carlson1b283372017-05-01 02:28:54 +00001095 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001096 const char *agent_feature;
1097 int agent_len;
Jonathan Tan603960b2019-11-12 16:34:20 -08001098 struct fetch_negotiator negotiator_alloc;
1099 struct fetch_negotiator *negotiator;
1100
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001101 negotiator = &negotiator_alloc;
Robert Coup4dfd0922022-03-28 14:02:06 +00001102 if (args->refetch) {
1103 fetch_negotiator_init_noop(negotiator);
1104 } else {
1105 fetch_negotiator_init(r, negotiator);
1106 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001107
1108 sort_ref_list(&ref, ref_compare_name);
René Scharfe9ed0d8d2016-09-29 17:27:31 +02001109 QSORT(sought, nr_sought, cmp_ref_by_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001110
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001111 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1112 agent_supported = 1;
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +07001113 if (agent_len)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001114 print_verbose(args, _("Server version is %.*s"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +07001115 agent_len, agent_feature);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001116 }
Nguyễn Thái Ngọc Duy0e042972019-06-20 18:59:51 +07001117
Josh Steadmon1e905bb2020-11-11 15:29:31 -08001118 if (!server_supports("session-id"))
1119 advertise_sid = 0;
1120
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001121 if (server_supports("shallow"))
1122 print_verbose(args, _("Server supports %s"), "shallow");
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001123 else if (args->depth > 0 || is_repository_shallow(r))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001124 die(_("Server does not support shallow clients"));
1125 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1126 args->deepen = 1;
1127 if (server_supports("multi_ack_detailed")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001128 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001129 multi_ack = 2;
1130 if (server_supports("no-done")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001131 print_verbose(args, _("Server supports %s"), "no-done");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001132 if (args->stateless_rpc)
1133 no_done = 1;
1134 }
1135 }
1136 else if (server_supports("multi_ack")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001137 print_verbose(args, _("Server supports %s"), "multi_ack");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001138 multi_ack = 1;
1139 }
1140 if (server_supports("side-band-64k")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001141 print_verbose(args, _("Server supports %s"), "side-band-64k");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001142 use_sideband = 2;
1143 }
1144 else if (server_supports("side-band")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001145 print_verbose(args, _("Server supports %s"), "side-band");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001146 use_sideband = 1;
1147 }
1148 if (server_supports("allow-tip-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001149 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001150 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1151 }
1152 if (server_supports("allow-reachable-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001153 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001154 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1155 }
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001156 if (server_supports("thin-pack"))
1157 print_verbose(args, _("Server supports %s"), "thin-pack");
1158 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001159 args->use_thin_pack = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001160 if (server_supports("no-progress"))
1161 print_verbose(args, _("Server supports %s"), "no-progress");
1162 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001163 args->no_progress = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001164 if (server_supports("include-tag"))
1165 print_verbose(args, _("Server supports %s"), "include-tag");
1166 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001167 args->include_tag = 0;
1168 if (server_supports("ofs-delta"))
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001169 print_verbose(args, _("Server supports %s"), "ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001170 else
1171 prefer_ofs_delta = 0;
1172
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001173 if (server_supports("filter")) {
1174 server_supports_filtering = 1;
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001175 print_verbose(args, _("Server supports %s"), "filter");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001176 } else if (args->filter_options.choice) {
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001177 warning("filtering not recognized by server, ignoring");
1178 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001179
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001180 if (server_supports("deepen-since")) {
1181 print_verbose(args, _("Server supports %s"), "deepen-since");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001182 deepen_since_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001183 } else if (args->deepen_since)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001184 die(_("Server does not support --shallow-since"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001185 if (server_supports("deepen-not")) {
1186 print_verbose(args, _("Server supports %s"), "deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001187 deepen_not_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001188 } else if (args->deepen_not)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001189 die(_("Server does not support --shallow-exclude"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001190 if (server_supports("deepen-relative"))
1191 print_verbose(args, _("Server supports %s"), "deepen-relative");
1192 else if (args->deepen_relative)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001193 die(_("Server does not support --deepen"));
brian m. carlson48bf1412020-05-25 19:58:59 +00001194 if (!server_supports_hash(the_hash_algo->name, NULL))
1195 die(_("Server does not support this repository's object format"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001196
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001197 mark_complete_and_common_ref(negotiator, args, &ref);
1198 filter_refs(args, &ref, sought, nr_sought);
Robert Coup4dfd0922022-03-28 14:02:06 +00001199 if (!args->refetch && everything_local(args, &ref)) {
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001200 packet_flush(fd[1]);
1201 goto all_done;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001202 }
Jonathan Tan603960b2019-11-12 16:34:20 -08001203 if (find_common(negotiator, args, fd, &oid, ref) < 0)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001204 if (!args->keep_pack)
1205 /* When cloning, it is not unusual to have
1206 * no common commit.
1207 */
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001208 warning(_("no common commits"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001209
1210 if (args->stateless_rpc)
1211 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001212 if (args->deepen)
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +07001213 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1214 NULL);
Li Linchao4fe788b2021-04-01 10:46:59 +00001215 else if (si->nr_ours || si->nr_theirs) {
1216 if (args->reject_shallow_remote)
1217 die(_("source repository is shallow, reject to clone."));
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001218 alternate_shallow_file = setup_temporary_shallow(si->shallow);
Li Linchao4fe788b2021-04-01 10:46:59 +00001219 } else
Nguyễn Thái Ngọc Duy6da8bdc2013-08-26 09:17:26 +07001220 alternate_shallow_file = NULL;
Jonathan Tan5476e1e2021-02-22 11:20:09 -08001221 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001222 &fsck_options.gitmodules_found))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001223 die(_("git fetch-pack: fetch failed."));
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001224 if (fsck_finish(&fsck_options))
1225 die("fsck failed");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001226
1227 all_done:
Jonathan Tan603960b2019-11-12 16:34:20 -08001228 if (negotiator)
1229 negotiator->release(negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001230 return ref;
1231}
1232
Brandon Williamsf7e20502018-03-15 10:31:29 -07001233static void add_shallow_requests(struct strbuf *req_buf,
1234 const struct fetch_pack_args *args)
1235{
Junio C Hamano00624d62018-07-18 12:20:27 -07001236 if (is_repository_shallow(the_repository))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001237 write_shallow_commits(req_buf, 1, NULL);
1238 if (args->depth > 0)
1239 packet_buf_write(req_buf, "deepen %d", args->depth);
1240 if (args->deepen_since) {
1241 timestamp_t max_age = approxidate(args->deepen_since);
1242 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1243 }
1244 if (args->deepen_not) {
1245 int i;
1246 for (i = 0; i < args->deepen_not->nr; i++) {
1247 struct string_list_item *s = args->deepen_not->items + i;
1248 packet_buf_write(req_buf, "deepen-not %s", s->string);
1249 }
1250 }
Jonathan Tan5056cf42018-12-18 13:24:35 -08001251 if (args->deepen_relative)
1252 packet_buf_write(req_buf, "deepen-relative\n");
Brandon Williamsf7e20502018-03-15 10:31:29 -07001253}
1254
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001255static void add_wants(const struct ref *wants, struct strbuf *req_buf)
Brandon Williams685fbd32018-03-15 10:31:28 -07001256{
Brandon Williams73302052018-06-27 15:30:23 -07001257 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1258
Brandon Williams685fbd32018-03-15 10:31:28 -07001259 for ( ; wants ; wants = wants->next) {
1260 const struct object_id *remote = &wants->old_oid;
Brandon Williams685fbd32018-03-15 10:31:28 -07001261 struct object *o;
1262
1263 /*
1264 * If that object is complete (i.e. it is an ancestor of a
1265 * local ref), we tell them we have it but do not have to
1266 * tell them about its ancestors, which they already know
1267 * about.
1268 *
1269 * We use lookup_object here because we are only
1270 * interested in the case we *know* the object is
1271 * reachable and we have already scanned it.
1272 */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001273 if (((o = lookup_object(the_repository, remote)) != NULL) &&
Brandon Williams685fbd32018-03-15 10:31:28 -07001274 (o->flags & COMPLETE)) {
1275 continue;
1276 }
1277
Brandon Williams73302052018-06-27 15:30:23 -07001278 if (!use_ref_in_want || wants->exact_oid)
1279 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1280 else
1281 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
Brandon Williams685fbd32018-03-15 10:31:28 -07001282 }
1283}
1284
1285static void add_common(struct strbuf *req_buf, struct oidset *common)
1286{
1287 struct oidset_iter iter;
1288 const struct object_id *oid;
1289 oidset_iter_init(common, &iter);
1290
1291 while ((oid = oidset_iter_next(&iter))) {
1292 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1293 }
1294}
1295
Jonathan Tanec062832018-06-14 15:54:28 -07001296static int add_haves(struct fetch_negotiator *negotiator,
1297 struct strbuf *req_buf,
Jonathan Tan57c34512021-04-08 18:10:00 -07001298 int *haves_to_send)
Brandon Williams685fbd32018-03-15 10:31:28 -07001299{
Brandon Williams685fbd32018-03-15 10:31:28 -07001300 int haves_added = 0;
1301 const struct object_id *oid;
1302
Jonathan Tanec062832018-06-14 15:54:28 -07001303 while ((oid = negotiator->next(negotiator))) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001304 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1305 if (++haves_added >= *haves_to_send)
1306 break;
1307 }
1308
Brandon Williams685fbd32018-03-15 10:31:28 -07001309 /* Increase haves to send on next round */
1310 *haves_to_send = next_flush(1, *haves_to_send);
1311
Jonathan Tan57c34512021-04-08 18:10:00 -07001312 return haves_added;
Brandon Williams685fbd32018-03-15 10:31:28 -07001313}
1314
Jonathan Tan6871d0c2021-04-08 18:10:01 -07001315static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1316 const struct string_list *server_options)
1317{
1318 const char *hash_name;
1319
Jeff Kinga31cfe32022-12-13 05:52:58 -05001320 ensure_server_supports_v2("fetch");
1321 packet_buf_write(req_buf, "command=fetch");
1322 if (server_supports_v2("agent"))
Jonathan Tan6871d0c2021-04-08 18:10:01 -07001323 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
Jeff Kinga31cfe32022-12-13 05:52:58 -05001324 if (advertise_sid && server_supports_v2("session-id"))
Jonathan Tan6871d0c2021-04-08 18:10:01 -07001325 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
Jeff Kinga31cfe32022-12-13 05:52:58 -05001326 if (server_options && server_options->nr) {
Jonathan Tan6871d0c2021-04-08 18:10:01 -07001327 int i;
Jeff Kinga31cfe32022-12-13 05:52:58 -05001328 ensure_server_supports_v2("server-option");
Jonathan Tan6871d0c2021-04-08 18:10:01 -07001329 for (i = 0; i < server_options->nr; i++)
1330 packet_buf_write(req_buf, "server-option=%s",
1331 server_options->items[i].string);
1332 }
1333
1334 if (server_feature_v2("object-format", &hash_name)) {
1335 int hash_algo = hash_algo_by_name(hash_name);
1336 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1337 die(_("mismatched algorithms: client %s; server %s"),
1338 the_hash_algo->name, hash_name);
1339 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1340 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1341 die(_("the server does not support algorithm '%s'"),
1342 the_hash_algo->name);
1343 }
1344 packet_buf_delim(req_buf);
1345}
1346
Jonathan Tanec062832018-06-14 15:54:28 -07001347static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001348 struct fetch_pack_args *args,
Brandon Williams685fbd32018-03-15 10:31:28 -07001349 const struct ref *wants, struct oidset *common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001350 int *haves_to_send, int *in_vain,
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001351 int sideband_all, int seen_ack)
Brandon Williams685fbd32018-03-15 10:31:28 -07001352{
Jonathan Tan57c34512021-04-08 18:10:00 -07001353 int haves_added;
1354 int done_sent = 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001355 struct strbuf req_buf = STRBUF_INIT;
1356
Jonathan Tan6871d0c2021-04-08 18:10:01 -07001357 write_fetch_command_and_capabilities(&req_buf, args->server_options);
Brandon Williams685fbd32018-03-15 10:31:28 -07001358
Brandon Williams685fbd32018-03-15 10:31:28 -07001359 if (args->use_thin_pack)
1360 packet_buf_write(&req_buf, "thin-pack");
1361 if (args->no_progress)
1362 packet_buf_write(&req_buf, "no-progress");
1363 if (args->include_tag)
1364 packet_buf_write(&req_buf, "include-tag");
1365 if (prefer_ofs_delta)
1366 packet_buf_write(&req_buf, "ofs-delta");
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001367 if (sideband_all)
1368 packet_buf_write(&req_buf, "sideband-all");
Brandon Williams685fbd32018-03-15 10:31:28 -07001369
Brandon Williamsf7e20502018-03-15 10:31:29 -07001370 /* Add shallow-info and deepen request */
1371 if (server_supports_feature("fetch", "shallow", 0))
1372 add_shallow_requests(&req_buf, args);
Junio C Hamano00624d62018-07-18 12:20:27 -07001373 else if (is_repository_shallow(the_repository) || args->deepen)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001374 die(_("Server does not support shallow requests"));
1375
Jonathan Tanba957102018-05-03 16:46:56 -07001376 /* Add filter */
Jonathan Tan10075572022-07-26 09:27:11 -07001377 send_filter(args, &req_buf,
1378 server_supports_feature("fetch", "filter", 0));
Jonathan Tanba957102018-05-03 16:46:56 -07001379
Jonathan Tandd4b7322020-06-10 13:57:23 -07001380 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1381 int i;
1382 struct strbuf to_send = STRBUF_INIT;
1383
1384 for (i = 0; i < uri_protocols.nr; i++) {
1385 const char *s = uri_protocols.items[i].string;
1386
1387 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1388 if (to_send.len)
1389 strbuf_addch(&to_send, ',');
1390 strbuf_addstr(&to_send, s);
1391 }
1392 }
1393 if (to_send.len) {
1394 packet_buf_write(&req_buf, "packfile-uris %s",
1395 to_send.buf);
1396 strbuf_release(&to_send);
1397 }
1398 }
1399
Brandon Williams685fbd32018-03-15 10:31:28 -07001400 /* add wants */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001401 add_wants(wants, &req_buf);
Brandon Williams685fbd32018-03-15 10:31:28 -07001402
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001403 /* Add all of the common commits we've found in previous rounds */
1404 add_common(&req_buf, common);
Brandon Williams685fbd32018-03-15 10:31:28 -07001405
Jonathan Tan57c34512021-04-08 18:10:00 -07001406 haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1407 *in_vain += haves_added;
Josh Steadmona29263c2022-08-02 15:04:05 -07001408 trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added);
1409 trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain);
Jonathan Tan57c34512021-04-08 18:10:00 -07001410 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1411 /* Send Done */
1412 packet_buf_write(&req_buf, "done\n");
1413 done_sent = 1;
1414 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001415
1416 /* Send request */
1417 packet_buf_flush(&req_buf);
Jeff King37c80012019-03-04 23:11:39 -05001418 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1419 die_errno(_("unable to write request to remote"));
Brandon Williams685fbd32018-03-15 10:31:28 -07001420
1421 strbuf_release(&req_buf);
Jonathan Tan57c34512021-04-08 18:10:00 -07001422 return done_sent;
Brandon Williams685fbd32018-03-15 10:31:28 -07001423}
1424
1425/*
1426 * Processes a section header in a server's response and checks if it matches
1427 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1428 * not consumed); if 0, the line will be consumed and the function will die if
1429 * the section header doesn't match what was expected.
1430 */
1431static int process_section_header(struct packet_reader *reader,
1432 const char *section, int peek)
1433{
Jonathan Tan7709acf2022-05-16 04:02:20 -07001434 int ret = 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001435
Jonathan Tan7709acf2022-05-16 04:02:20 -07001436 if (packet_reader_peek(reader) == PACKET_READ_NORMAL &&
1437 !strcmp(reader->line, section))
1438 ret = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001439
1440 if (!peek) {
Jonathan Tan7709acf2022-05-16 04:02:20 -07001441 if (!ret) {
1442 if (reader->line)
1443 die(_("expected '%s', received '%s'"),
1444 section, reader->line);
1445 else
1446 die(_("expected '%s'"), section);
1447 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001448 packet_reader_read(reader);
1449 }
1450
1451 return ret;
1452}
1453
Jonathan Tan81025702021-04-08 18:09:59 -07001454static int process_ack(struct fetch_negotiator *negotiator,
1455 struct packet_reader *reader,
1456 struct object_id *common_oid,
1457 int *received_ready)
Brandon Williams685fbd32018-03-15 10:31:28 -07001458{
Brandon Williams685fbd32018-03-15 10:31:28 -07001459 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1460 const char *arg;
1461
1462 if (!strcmp(reader->line, "NAK"))
1463 continue;
1464
1465 if (skip_prefix(reader->line, "ACK ", &arg)) {
Jonathan Tan81025702021-04-08 18:09:59 -07001466 if (!get_oid_hex(arg, common_oid)) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001467 struct commit *commit;
Jonathan Tan81025702021-04-08 18:09:59 -07001468 commit = lookup_commit(the_repository, common_oid);
Jonathan Tan603960b2019-11-12 16:34:20 -08001469 if (negotiator)
1470 negotiator->ack(negotiator, commit);
Brandon Williams685fbd32018-03-15 10:31:28 -07001471 }
Jonathan Tan81025702021-04-08 18:09:59 -07001472 return 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001473 }
1474
1475 if (!strcmp(reader->line, "ready")) {
Jonathan Tan81025702021-04-08 18:09:59 -07001476 *received_ready = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001477 continue;
1478 }
1479
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001480 die(_("unexpected acknowledgment line: '%s'"), reader->line);
Brandon Williams685fbd32018-03-15 10:31:28 -07001481 }
1482
1483 if (reader->status != PACKET_READ_FLUSH &&
1484 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001485 die(_("error processing acks: %d"), reader->status);
Brandon Williams685fbd32018-03-15 10:31:28 -07001486
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001487 /*
1488 * If an "acknowledgments" section is sent, a packfile is sent if and
1489 * only if "ready" was sent in this section. The other sections
1490 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1491 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1492 * otherwise.
1493 */
Jonathan Tan81025702021-04-08 18:09:59 -07001494 if (*received_ready && reader->status != PACKET_READ_DELIM)
Bagas Sanjaya3d3c23b2021-12-22 14:58:06 +07001495 /*
1496 * TRANSLATORS: The parameter will be 'ready', a protocol
1497 * keyword.
1498 */
1499 die(_("expected packfile to be sent after '%s'"), "ready");
Jonathan Tan81025702021-04-08 18:09:59 -07001500 if (!*received_ready && reader->status != PACKET_READ_FLUSH)
Bagas Sanjaya3d3c23b2021-12-22 14:58:06 +07001501 /*
1502 * TRANSLATORS: The parameter will be 'ready', a protocol
1503 * keyword.
1504 */
1505 die(_("expected no other sections to be sent after no '%s'"), "ready");
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001506
Jonathan Tan81025702021-04-08 18:09:59 -07001507 return 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001508}
1509
Brandon Williamsf7e20502018-03-15 10:31:29 -07001510static void receive_shallow_info(struct fetch_pack_args *args,
Jonathan Tan13390782019-03-26 12:31:21 -07001511 struct packet_reader *reader,
1512 struct oid_array *shallows,
1513 struct shallow_info *si)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001514{
Jonathan Tan13390782019-03-26 12:31:21 -07001515 int unshallow_received = 0;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001516
Brandon Williamsf7e20502018-03-15 10:31:29 -07001517 process_section_header(reader, "shallow-info", 0);
1518 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1519 const char *arg;
1520 struct object_id oid;
1521
1522 if (skip_prefix(reader->line, "shallow ", &arg)) {
1523 if (get_oid_hex(arg, &oid))
1524 die(_("invalid shallow line: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001525 oid_array_append(shallows, &oid);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001526 continue;
1527 }
1528 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1529 if (get_oid_hex(arg, &oid))
1530 die(_("invalid unshallow line: %s"), reader->line);
Jeff Kingd0229ab2019-06-20 03:41:14 -04001531 if (!lookup_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001532 die(_("object not found: %s"), reader->line);
1533 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -07001534 if (!parse_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001535 die(_("error in object: %s"), reader->line);
1536 if (unregister_shallow(&oid))
1537 die(_("no shallow found: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001538 unshallow_received = 1;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001539 continue;
1540 }
1541 die(_("expected shallow/unshallow, got %s"), reader->line);
1542 }
1543
1544 if (reader->status != PACKET_READ_FLUSH &&
1545 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001546 die(_("error processing shallow info: %d"), reader->status);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001547
Jonathan Tan13390782019-03-26 12:31:21 -07001548 if (args->deepen || unshallow_received) {
1549 /*
1550 * Treat these as shallow lines caused by our depth settings.
1551 * In v0, these lines cannot cause refs to be rejected; do the
1552 * same.
1553 */
1554 int i;
1555
1556 for (i = 0; i < shallows->nr; i++)
1557 register_shallow(the_repository, &shallows->oid[i]);
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001558 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1559 NULL);
1560 args->deepen = 1;
Jonathan Tan13390782019-03-26 12:31:21 -07001561 } else if (shallows->nr) {
1562 /*
1563 * Treat these as shallow lines caused by the remote being
1564 * shallow. In v0, remote refs that reach these objects are
1565 * rejected (unless --update-shallow is set); do the same.
1566 */
1567 prepare_shallow_info(si, shallows);
Li Linchao4fe788b2021-04-01 10:46:59 +00001568 if (si->nr_ours || si->nr_theirs) {
1569 if (args->reject_shallow_remote)
1570 die(_("source repository is shallow, reject to clone."));
Jonathan Tan13390782019-03-26 12:31:21 -07001571 alternate_shallow_file =
1572 setup_temporary_shallow(si->shallow);
Li Linchao4fe788b2021-04-01 10:46:59 +00001573 } else
Jonathan Tan13390782019-03-26 12:31:21 -07001574 alternate_shallow_file = NULL;
brian m. carlson380ebab2019-02-06 23:59:37 +00001575 } else {
1576 alternate_shallow_file = NULL;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001577 }
Brandon Williamsf7e20502018-03-15 10:31:29 -07001578}
1579
Jonathan Tanb7643002019-03-27 14:11:10 -07001580static int cmp_name_ref(const void *name, const void *ref)
1581{
1582 return strcmp(name, (*(struct ref **)ref)->name);
1583}
1584
Jonathan Tane2842b32018-08-01 13:13:20 -07001585static void receive_wanted_refs(struct packet_reader *reader,
1586 struct ref **sought, int nr_sought)
Brandon Williams73302052018-06-27 15:30:23 -07001587{
1588 process_section_header(reader, "wanted-refs", 0);
1589 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1590 struct object_id oid;
1591 const char *end;
Jonathan Tanb7643002019-03-27 14:11:10 -07001592 struct ref **found;
Brandon Williams73302052018-06-27 15:30:23 -07001593
1594 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001595 die(_("expected wanted-ref, got '%s'"), reader->line);
Brandon Williams73302052018-06-27 15:30:23 -07001596
Jonathan Tanb7643002019-03-27 14:11:10 -07001597 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1598 cmp_name_ref);
1599 if (!found)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001600 die(_("unexpected wanted-ref: '%s'"), reader->line);
Jonathan Tanb7643002019-03-27 14:11:10 -07001601 oidcpy(&(*found)->old_oid, &oid);
Brandon Williams73302052018-06-27 15:30:23 -07001602 }
1603
1604 if (reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001605 die(_("error processing wanted refs: %d"), reader->status);
Brandon Williams73302052018-06-27 15:30:23 -07001606}
1607
Jonathan Tandd4b7322020-06-10 13:57:23 -07001608static void receive_packfile_uris(struct packet_reader *reader,
1609 struct string_list *uris)
1610{
1611 process_section_header(reader, "packfile-uris", 0);
1612 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1613 if (reader->pktlen < the_hash_algo->hexsz ||
1614 reader->line[the_hash_algo->hexsz] != ' ')
1615 die("expected '<hash> <uri>', got: %s\n", reader->line);
1616
1617 string_list_append(uris, reader->line);
1618 }
1619 if (reader->status != PACKET_READ_DELIM)
1620 die("expected DELIM");
1621}
1622
Brandon Williams685fbd32018-03-15 10:31:28 -07001623enum fetch_state {
1624 FETCH_CHECK_LOCAL = 0,
1625 FETCH_SEND_REQUEST,
1626 FETCH_PROCESS_ACKS,
1627 FETCH_GET_PACK,
1628 FETCH_DONE,
1629};
1630
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001631static void do_check_stateless_delimiter(int stateless_rpc,
Denton Liub0df0c12020-05-19 06:54:00 -04001632 struct packet_reader *reader)
1633{
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001634 check_stateless_delimiter(stateless_rpc, reader,
Denton Liub0df0c12020-05-19 06:54:00 -04001635 _("git fetch-pack: expected response end packet"));
1636}
1637
Brandon Williams685fbd32018-03-15 10:31:28 -07001638static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1639 int fd[2],
1640 const struct ref *orig_ref,
1641 struct ref **sought, int nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001642 struct oid_array *shallows,
1643 struct shallow_info *si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001644 struct string_list *pack_lockfiles)
Brandon Williams685fbd32018-03-15 10:31:28 -07001645{
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001646 struct repository *r = the_repository;
Brandon Williams685fbd32018-03-15 10:31:28 -07001647 struct ref *ref = copy_ref_list(orig_ref);
1648 enum fetch_state state = FETCH_CHECK_LOCAL;
1649 struct oidset common = OIDSET_INIT;
1650 struct packet_reader reader;
Josh Steadmon5fc31182019-10-02 16:49:28 -07001651 int in_vain = 0, negotiation_started = 0;
Josh Steadmona29263c2022-08-02 15:04:05 -07001652 int negotiation_round = 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001653 int haves_to_send = INITIAL_FLUSH;
Jonathan Tan603960b2019-11-12 16:34:20 -08001654 struct fetch_negotiator negotiator_alloc;
1655 struct fetch_negotiator *negotiator;
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001656 int seen_ack = 0;
Jonathan Tan81025702021-04-08 18:09:59 -07001657 struct object_id common_oid;
1658 int received_ready = 0;
Jonathan Tandd4b7322020-06-10 13:57:23 -07001659 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1660 int i;
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001661 struct strvec index_pack_args = STRVEC_INIT;
Jonathan Tan603960b2019-11-12 16:34:20 -08001662
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001663 negotiator = &negotiator_alloc;
Robert Coup4dfd0922022-03-28 14:02:06 +00001664 if (args->refetch)
1665 fetch_negotiator_init_noop(negotiator);
1666 else
1667 fetch_negotiator_init(r, negotiator);
Jonathan Tan603960b2019-11-12 16:34:20 -08001668
Brandon Williams685fbd32018-03-15 10:31:28 -07001669 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -08001670 PACKET_READ_CHOMP_NEWLINE |
1671 PACKET_READ_DIE_ON_ERR_PACKET);
Jonathan Tan07c3c2a2019-01-16 11:28:15 -08001672 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1673 server_supports_feature("fetch", "sideband-all", 0)) {
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001674 reader.use_sideband = 1;
1675 reader.me = "fetch-pack";
1676 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001677
1678 while (state != FETCH_DONE) {
1679 switch (state) {
1680 case FETCH_CHECK_LOCAL:
1681 sort_ref_list(&ref, ref_compare_name);
1682 QSORT(sought, nr_sought, cmp_ref_by_name);
1683
1684 /* v2 supports these by default */
1685 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1686 use_sideband = 2;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001687 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1688 args->deepen = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001689
Brandon Williams685fbd32018-03-15 10:31:28 -07001690 /* Filter 'ref' by 'sought' and those that aren't local */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001691 mark_complete_and_common_ref(negotiator, args, &ref);
1692 filter_refs(args, &ref, sought, nr_sought);
Robert Coup4dfd0922022-03-28 14:02:06 +00001693 if (!args->refetch && everything_local(args, &ref))
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001694 state = FETCH_DONE;
1695 else
Jonathan Tan12f19a92018-10-03 16:04:52 -07001696 state = FETCH_SEND_REQUEST;
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001697
1698 mark_tips(negotiator, args->negotiation_tips);
1699 for_each_cached_alternate(negotiator,
1700 insert_one_alternate_object);
Brandon Williams685fbd32018-03-15 10:31:28 -07001701 break;
1702 case FETCH_SEND_REQUEST:
Josh Steadmon5fc31182019-10-02 16:49:28 -07001703 if (!negotiation_started) {
1704 negotiation_started = 1;
1705 trace2_region_enter("fetch-pack",
1706 "negotiation_v2",
1707 the_repository);
1708 }
Josh Steadmona29263c2022-08-02 15:04:05 -07001709 negotiation_round++;
1710 trace2_region_enter_printf("negotiation_v2", "round",
1711 the_repository, "%d",
1712 negotiation_round);
Jonathan Tan603960b2019-11-12 16:34:20 -08001713 if (send_fetch_request(negotiator, fd[1], args, ref,
Jonathan Tanec062832018-06-14 15:54:28 -07001714 &common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001715 &haves_to_send, &in_vain,
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001716 reader.use_sideband,
Josh Steadmona29263c2022-08-02 15:04:05 -07001717 seen_ack)) {
1718 trace2_region_leave_printf("negotiation_v2", "round",
1719 the_repository, "%d",
1720 negotiation_round);
Brandon Williams685fbd32018-03-15 10:31:28 -07001721 state = FETCH_GET_PACK;
Josh Steadmona29263c2022-08-02 15:04:05 -07001722 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001723 else
1724 state = FETCH_PROCESS_ACKS;
1725 break;
1726 case FETCH_PROCESS_ACKS:
1727 /* Process ACKs/NAKs */
Jonathan Tan81025702021-04-08 18:09:59 -07001728 process_section_header(&reader, "acknowledgments", 0);
1729 while (process_ack(negotiator, &reader, &common_oid,
1730 &received_ready)) {
1731 in_vain = 0;
1732 seen_ack = 1;
1733 oidset_insert(&common, &common_oid);
1734 }
Josh Steadmona29263c2022-08-02 15:04:05 -07001735 trace2_region_leave_printf("negotiation_v2", "round",
1736 the_repository, "%d",
1737 negotiation_round);
Jonathan Tan81025702021-04-08 18:09:59 -07001738 if (received_ready) {
Denton Liub0df0c12020-05-19 06:54:00 -04001739 /*
1740 * Don't check for response delimiter; get_pack() will
1741 * read the rest of this response.
1742 */
Brandon Williams685fbd32018-03-15 10:31:28 -07001743 state = FETCH_GET_PACK;
Jonathan Tan81025702021-04-08 18:09:59 -07001744 } else {
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001745 do_check_stateless_delimiter(args->stateless_rpc, &reader);
Brandon Williams685fbd32018-03-15 10:31:28 -07001746 state = FETCH_SEND_REQUEST;
Brandon Williams685fbd32018-03-15 10:31:28 -07001747 }
1748 break;
1749 case FETCH_GET_PACK:
Josh Steadmon5fc31182019-10-02 16:49:28 -07001750 trace2_region_leave("fetch-pack",
1751 "negotiation_v2",
1752 the_repository);
Josh Steadmona29263c2022-08-02 15:04:05 -07001753 trace2_data_intmax("negotiation_v2", the_repository,
1754 "total_rounds", negotiation_round);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001755 /* Check for shallow-info section */
1756 if (process_section_header(&reader, "shallow-info", 1))
Jonathan Tan13390782019-03-26 12:31:21 -07001757 receive_shallow_info(args, &reader, shallows, si);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001758
Brandon Williams73302052018-06-27 15:30:23 -07001759 if (process_section_header(&reader, "wanted-refs", 1))
Jonathan Tane2842b32018-08-01 13:13:20 -07001760 receive_wanted_refs(&reader, sought, nr_sought);
Brandon Williams73302052018-06-27 15:30:23 -07001761
Jonathan Tandd4b7322020-06-10 13:57:23 -07001762 /* get the pack(s) */
Ivan Frade88e9b1e2021-11-10 23:51:28 +00001763 if (git_env_bool("GIT_TRACE_REDACT", 1))
1764 reader.options |= PACKET_READ_REDACT_URI_PATH;
Jonathan Tandd4b7322020-06-10 13:57:23 -07001765 if (process_section_header(&reader, "packfile-uris", 1))
1766 receive_packfile_uris(&reader, &packfile_uris);
Ivan Frade88e9b1e2021-11-10 23:51:28 +00001767 /* We don't expect more URIs. Reset to avoid expensive URI check. */
1768 reader.options &= ~PACKET_READ_REDACT_URI_PATH;
1769
Brandon Williams685fbd32018-03-15 10:31:28 -07001770 process_section_header(&reader, "packfile", 0);
Jeff Kingae1a7ee2021-05-19 12:11:05 -04001771
1772 /*
1773 * this is the final request we'll make of the server;
1774 * do a half-duplex shutdown to indicate that they can
1775 * hang up as soon as the pack is sent.
1776 */
1777 close(fd[1]);
1778 fd[1] = -1;
1779
Jonathan Tandd4b7322020-06-10 13:57:23 -07001780 if (get_pack(args, fd, pack_lockfiles,
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001781 packfile_uris.nr ? &index_pack_args : NULL,
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001782 sought, nr_sought, &fsck_options.gitmodules_found))
Brandon Williams685fbd32018-03-15 10:31:28 -07001783 die(_("git fetch-pack: fetch failed."));
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001784 do_check_stateless_delimiter(args->stateless_rpc, &reader);
Brandon Williams685fbd32018-03-15 10:31:28 -07001785
1786 state = FETCH_DONE;
1787 break;
1788 case FETCH_DONE:
1789 continue;
1790 }
1791 }
1792
Jonathan Tandd4b7322020-06-10 13:57:23 -07001793 for (i = 0; i < packfile_uris.nr; i++) {
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001794 int j;
Jonathan Tandd4b7322020-06-10 13:57:23 -07001795 struct child_process cmd = CHILD_PROCESS_INIT;
1796 char packname[GIT_MAX_HEXSZ + 1];
1797 const char *uri = packfile_uris.items[i].string +
1798 the_hash_algo->hexsz + 1;
1799
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001800 strvec_push(&cmd.args, "http-fetch");
1801 strvec_pushf(&cmd.args, "--packfile=%.*s",
Jeff Kingf6d89422020-07-28 16:26:31 -04001802 (int) the_hash_algo->hexsz,
1803 packfile_uris.items[i].string);
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001804 for (j = 0; j < index_pack_args.nr; j++)
1805 strvec_pushf(&cmd.args, "--index-pack-arg=%s",
1806 index_pack_args.v[j]);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001807 strvec_push(&cmd.args, uri);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001808 cmd.git_cmd = 1;
1809 cmd.no_stdin = 1;
1810 cmd.out = -1;
1811 if (start_command(&cmd))
1812 die("fetch-pack: unable to spawn http-fetch");
1813
1814 if (read_in_full(cmd.out, packname, 5) < 0 ||
1815 memcmp(packname, "keep\t", 5))
1816 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1817
1818 if (read_in_full(cmd.out, packname,
1819 the_hash_algo->hexsz + 1) < 0 ||
1820 packname[the_hash_algo->hexsz] != '\n')
1821 die("fetch-pack: expected hash then LF at end of http-fetch output");
1822
1823 packname[the_hash_algo->hexsz] = '\0';
1824
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001825 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
Jonathan Tan5476e1e2021-02-22 11:20:09 -08001826
Jonathan Tandd4b7322020-06-10 13:57:23 -07001827 close(cmd.out);
1828
1829 if (finish_command(&cmd))
1830 die("fetch-pack: unable to finish http-fetch");
1831
1832 if (memcmp(packfile_uris.items[i].string, packname,
1833 the_hash_algo->hexsz))
1834 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1835 uri, (int) the_hash_algo->hexsz,
1836 packfile_uris.items[i].string);
1837
1838 string_list_append_nodup(pack_lockfiles,
1839 xstrfmt("%s/pack/pack-%s.keep",
1840 get_object_directory(),
1841 packname));
1842 }
1843 string_list_clear(&packfile_uris, 0);
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001844 strvec_clear(&index_pack_args);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001845
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001846 if (fsck_finish(&fsck_options))
1847 die("fsck failed");
Brandon Williams685fbd32018-03-15 10:31:28 -07001848
Jonathan Tan603960b2019-11-12 16:34:20 -08001849 if (negotiator)
1850 negotiator->release(negotiator);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001851
Brandon Williams685fbd32018-03-15 10:31:28 -07001852 oidset_clear(&common);
1853 return ref;
1854}
1855
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001856static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1857{
1858 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1859 const char *path;
1860
1861 if (git_config_pathname(&path, var, value))
1862 return 1;
1863 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1864 fsck_msg_types.len ? ',' : '=', path);
1865 free((char *)path);
1866 return 0;
1867 }
1868
1869 if (skip_prefix(var, "fetch.fsck.", &var)) {
1870 if (is_valid_msg_type(var, value))
1871 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1872 fsck_msg_types.len ? ',' : '=', var, value);
1873 else
1874 warning("Skipping unknown msg id '%s'", var);
1875 return 0;
1876 }
1877
1878 return git_default_config(var, value, cb);
1879}
1880
Tanay Abhraf44af512014-08-07 09:21:20 -07001881static void fetch_pack_config(void)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001882{
Tanay Abhraf44af512014-08-07 09:21:20 -07001883 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1884 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1885 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1886 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1887 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
Josh Steadmon1e905bb2020-11-11 15:29:31 -08001888 git_config_get_bool("transfer.advertisesid", &advertise_sid);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001889 if (!uri_protocols.nr) {
1890 char *str;
1891
1892 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1893 string_list_split(&uri_protocols, str, ',', -1);
1894 free(str);
1895 }
1896 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001897
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001898 git_config(fetch_pack_config_cb, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001899}
1900
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001901static void fetch_pack_setup(void)
1902{
1903 static int did_setup;
1904 if (did_setup)
1905 return;
Tanay Abhraf44af512014-08-07 09:21:20 -07001906 fetch_pack_config();
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001907 if (0 <= transfer_unpack_limit)
1908 unpack_limit = transfer_unpack_limit;
1909 else if (0 <= fetch_unpack_limit)
1910 unpack_limit = fetch_unpack_limit;
1911 did_setup = 1;
1912}
1913
Junio C Hamanof2db8542013-01-29 14:02:15 -08001914static int remove_duplicates_in_refs(struct ref **ref, int nr)
1915{
1916 struct string_list names = STRING_LIST_INIT_NODUP;
1917 int src, dst;
1918
1919 for (src = dst = 0; src < nr; src++) {
1920 struct string_list_item *item;
1921 item = string_list_insert(&names, ref[src]->name);
1922 if (item->util)
1923 continue; /* already have it */
1924 item->util = ref[src];
1925 if (src != dst)
1926 ref[dst] = ref[src];
1927 dst++;
1928 }
1929 for (src = dst; src < nr; src++)
1930 ref[src] = NULL;
1931 string_list_clear(&names, 0);
1932 return dst;
1933}
1934
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001935static void update_shallow(struct fetch_pack_args *args,
Jonathan Tane2842b32018-08-01 13:13:20 -07001936 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001937 struct shallow_info *si)
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001938{
brian m. carlson910650d2017-03-31 01:40:00 +00001939 struct oid_array ref = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001940 int *status;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001941 int i;
1942
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001943 if (args->deepen && alternate_shallow_file) {
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001944 if (*alternate_shallow_file == '\0') { /* --unshallow */
Stefan Beller102de882018-05-17 15:51:51 -07001945 unlink_or_warn(git_path_shallow(the_repository));
Taylor Blau37b9dca2020-04-22 18:25:45 -06001946 rollback_shallow_file(the_repository, &shallow_lock);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001947 } else
Taylor Blau37b9dca2020-04-22 18:25:45 -06001948 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001949 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001950 return;
1951 }
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001952
1953 if (!si->shallow || !si->shallow->nr)
1954 return;
1955
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001956 if (args->cloning) {
1957 /*
1958 * remote is shallow, but this is a clone, there are
1959 * no objects in repo to worry about. Accept any
1960 * shallow points that exist in the pack (iow in repo
1961 * after get_pack() and reprepare_packed_git())
1962 */
brian m. carlson910650d2017-03-31 01:40:00 +00001963 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001964 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001965 for (i = 0; i < si->shallow->nr; i++)
brian m. carlsonee3051b2017-03-26 16:01:37 +00001966 if (has_object_file(&oid[i]))
brian m. carlson910650d2017-03-31 01:40:00 +00001967 oid_array_append(&extra, &oid[i]);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001968 if (extra.nr) {
1969 setup_alternate_shallow(&shallow_lock,
1970 &alternate_shallow_file,
1971 &extra);
Taylor Blau37b9dca2020-04-22 18:25:45 -06001972 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001973 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001974 }
brian m. carlson910650d2017-03-31 01:40:00 +00001975 oid_array_clear(&extra);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001976 return;
1977 }
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001978
1979 if (!si->nr_ours && !si->nr_theirs)
1980 return;
1981
1982 remove_nonexistent_theirs_shallow(si);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001983 if (!si->nr_ours && !si->nr_theirs)
1984 return;
Jonathan Tane2842b32018-08-01 13:13:20 -07001985 for (i = 0; i < nr_sought; i++)
1986 oid_array_append(&ref, &sought[i]->old_oid);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001987 si->ref = &ref;
1988
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001989 if (args->update_shallow) {
1990 /*
1991 * remote is also shallow, .git/shallow may be updated
1992 * so all refs can be accepted. Make sure we only add
1993 * shallow roots that are actually reachable from new
1994 * refs.
1995 */
brian m. carlson910650d2017-03-31 01:40:00 +00001996 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001997 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001998 assign_shallow_commits_to_refs(si, NULL, NULL);
1999 if (!si->nr_ours && !si->nr_theirs) {
brian m. carlson910650d2017-03-31 01:40:00 +00002000 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07002001 return;
2002 }
2003 for (i = 0; i < si->nr_ours; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00002004 oid_array_append(&extra, &oid[si->ours[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07002005 for (i = 0; i < si->nr_theirs; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00002006 oid_array_append(&extra, &oid[si->theirs[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07002007 setup_alternate_shallow(&shallow_lock,
2008 &alternate_shallow_file,
2009 &extra);
Taylor Blau37b9dca2020-04-22 18:25:45 -06002010 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson910650d2017-03-31 01:40:00 +00002011 oid_array_clear(&extra);
2012 oid_array_clear(&ref);
brian m. carlson23311f32019-02-04 00:06:50 +00002013 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07002014 return;
2015 }
2016
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07002017 /*
2018 * remote is also shallow, check what ref is safe to update
2019 * without updating .git/shallow
2020 */
René Scharfeca56dad2021-03-13 17:17:22 +01002021 CALLOC_ARRAY(status, nr_sought);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07002022 assign_shallow_commits_to_refs(si, NULL, status);
2023 if (si->nr_ours || si->nr_theirs) {
Jonathan Tane2842b32018-08-01 13:13:20 -07002024 for (i = 0; i < nr_sought; i++)
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07002025 if (status[i])
Jonathan Tane2842b32018-08-01 13:13:20 -07002026 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07002027 }
2028 free(status);
brian m. carlson910650d2017-03-31 01:40:00 +00002029 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07002030}
2031
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +02002032static const struct object_id *iterate_ref_map(void *cb_data)
Jonathan Tancf1e7c02018-07-02 15:08:43 -07002033{
2034 struct ref **rm = cb_data;
2035 struct ref *ref = *rm;
2036
2037 if (!ref)
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +02002038 return NULL;
Jonathan Tancf1e7c02018-07-02 15:08:43 -07002039 *rm = ref->next;
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +02002040 return &ref->old_oid;
Jonathan Tancf1e7c02018-07-02 15:08:43 -07002041}
2042
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07002043struct ref *fetch_pack(struct fetch_pack_args *args,
Jeff King0f804b02019-03-20 04:16:14 -04002044 int fd[],
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07002045 const struct ref *ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -08002046 struct ref **sought, int nr_sought,
brian m. carlson910650d2017-03-31 01:40:00 +00002047 struct oid_array *shallow,
Jonathan Tan9da69a62020-06-10 13:57:22 -07002048 struct string_list *pack_lockfiles,
Brandon Williams685fbd32018-03-15 10:31:28 -07002049 enum protocol_version version)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07002050{
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07002051 struct ref *ref_cpy;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07002052 struct shallow_info si;
Jonathan Tan13390782019-03-26 12:31:21 -07002053 struct oid_array shallows_scratch = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07002054
2055 fetch_pack_setup();
Junio C Hamanof2db8542013-01-29 14:02:15 -08002056 if (nr_sought)
2057 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07002058
Jonathan Tan01775652018-09-27 12:24:05 -07002059 if (version != protocol_v2 && !ref) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07002060 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07002061 die(_("no matching remote head"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07002062 }
Jonathan Tan1e7d4402019-03-26 12:31:20 -07002063 if (version == protocol_v2) {
2064 if (shallow->nr)
2065 BUG("Protocol V2 does not provide shallows at this point in the fetch");
2066 memset(&si, 0, sizeof(si));
Brandon Williams685fbd32018-03-15 10:31:28 -07002067 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07002068 &shallows_scratch, &si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07002069 pack_lockfiles);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07002070 } else {
2071 prepare_shallow_info(&si, shallow);
Brandon Williams685fbd32018-03-15 10:31:28 -07002072 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
Jonathan Tan9da69a62020-06-10 13:57:22 -07002073 &si, pack_lockfiles);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07002074 }
Stefan Bellera49d2832018-03-23 18:45:21 +01002075 reprepare_packed_git(the_repository);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07002076
2077 if (!args->cloning && args->deepen) {
2078 struct check_connected_options opt = CHECK_CONNECTED_INIT;
2079 struct ref *iterator = ref_cpy;
2080 opt.shallow_file = alternate_shallow_file;
2081 if (args->deepen)
2082 opt.is_deepening_fetch = 1;
2083 if (check_connected(iterate_ref_map, &iterator, &opt)) {
2084 error(_("remote did not send all necessary objects"));
2085 free_refs(ref_cpy);
2086 ref_cpy = NULL;
Taylor Blau37b9dca2020-04-22 18:25:45 -06002087 rollback_shallow_file(the_repository, &shallow_lock);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07002088 goto cleanup;
2089 }
2090 args->connectivity_checked = 1;
2091 }
2092
Jonathan Tane2842b32018-08-01 13:13:20 -07002093 update_shallow(args, sought, nr_sought, &si);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07002094cleanup:
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07002095 clear_shallow_info(&si);
Jonathan Tan13390782019-03-26 12:31:21 -07002096 oid_array_clear(&shallows_scratch);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07002097 return ref_cpy;
2098}
Matt McCutchene860d962017-02-22 11:01:22 -05002099
Jonathan Tan9c1e6572021-05-04 14:16:01 -07002100static int add_to_object_array(const struct object_id *oid, void *data)
2101{
2102 struct object_array *a = data;
2103
2104 add_object_array(lookup_object(the_repository, oid), "", a);
2105 return 0;
2106}
2107
2108static void clear_common_flag(struct oidset *s)
2109{
2110 struct oidset_iter iter;
2111 const struct object_id *oid;
2112 oidset_iter_init(s, &iter);
2113
2114 while ((oid = oidset_iter_next(&iter))) {
2115 struct object *obj = lookup_object(the_repository, oid);
2116 obj->flags &= ~COMMON;
2117 }
2118}
2119
2120void negotiate_using_fetch(const struct oid_array *negotiation_tips,
2121 const struct string_list *server_options,
2122 int stateless_rpc,
2123 int fd[],
2124 struct oidset *acked_commits)
2125{
2126 struct fetch_negotiator negotiator;
2127 struct packet_reader reader;
2128 struct object_array nt_object_array = OBJECT_ARRAY_INIT;
2129 struct strbuf req_buf = STRBUF_INIT;
2130 int haves_to_send = INITIAL_FLUSH;
2131 int in_vain = 0;
2132 int seen_ack = 0;
2133 int last_iteration = 0;
Josh Steadmona29263c2022-08-02 15:04:05 -07002134 int negotiation_round = 0;
Jonathan Tan9c1e6572021-05-04 14:16:01 -07002135 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2136
2137 fetch_negotiator_init(the_repository, &negotiator);
2138 mark_tips(&negotiator, negotiation_tips);
2139
2140 packet_reader_init(&reader, fd[0], NULL, 0,
2141 PACKET_READ_CHOMP_NEWLINE |
2142 PACKET_READ_DIE_ON_ERR_PACKET);
2143
2144 oid_array_for_each((struct oid_array *) negotiation_tips,
2145 add_to_object_array,
2146 &nt_object_array);
2147
Josh Steadmona29263c2022-08-02 15:04:05 -07002148 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository);
Jonathan Tan9c1e6572021-05-04 14:16:01 -07002149 while (!last_iteration) {
2150 int haves_added;
2151 struct object_id common_oid;
2152 int received_ready = 0;
2153
Josh Steadmona29263c2022-08-02 15:04:05 -07002154 negotiation_round++;
2155
2156 trace2_region_enter_printf("negotiate_using_fetch", "round",
2157 the_repository, "%d",
2158 negotiation_round);
Jonathan Tan9c1e6572021-05-04 14:16:01 -07002159 strbuf_reset(&req_buf);
2160 write_fetch_command_and_capabilities(&req_buf, server_options);
2161
2162 packet_buf_write(&req_buf, "wait-for-done");
2163
2164 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2165 in_vain += haves_added;
2166 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2167 last_iteration = 1;
2168
Josh Steadmona29263c2022-08-02 15:04:05 -07002169 trace2_data_intmax("negotiate_using_fetch", the_repository,
2170 "haves_added", haves_added);
2171 trace2_data_intmax("negotiate_using_fetch", the_repository,
2172 "in_vain", in_vain);
2173
Jonathan Tan9c1e6572021-05-04 14:16:01 -07002174 /* Send request */
2175 packet_buf_flush(&req_buf);
2176 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2177 die_errno(_("unable to write request to remote"));
2178
2179 /* Process ACKs/NAKs */
2180 process_section_header(&reader, "acknowledgments", 0);
2181 while (process_ack(&negotiator, &reader, &common_oid,
2182 &received_ready)) {
2183 struct commit *commit = lookup_commit(the_repository,
2184 &common_oid);
2185 if (commit) {
2186 timestamp_t generation;
2187
2188 parse_commit_or_die(commit);
2189 commit->object.flags |= COMMON;
2190 generation = commit_graph_generation(commit);
2191 if (generation < min_generation)
2192 min_generation = generation;
2193 }
2194 in_vain = 0;
2195 seen_ack = 1;
2196 oidset_insert(acked_commits, &common_oid);
2197 }
2198 if (received_ready)
2199 die(_("unexpected 'ready' from remote"));
2200 else
2201 do_check_stateless_delimiter(stateless_rpc, &reader);
2202 if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2203 REACH_SCRATCH, 0,
2204 min_generation))
2205 last_iteration = 1;
Josh Steadmona29263c2022-08-02 15:04:05 -07002206 trace2_region_leave_printf("negotiation", "round",
2207 the_repository, "%d",
2208 negotiation_round);
Jonathan Tan9c1e6572021-05-04 14:16:01 -07002209 }
Josh Steadmona29263c2022-08-02 15:04:05 -07002210 trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository);
2211 trace2_data_intmax("negotiate_using_fetch", the_repository,
2212 "total_rounds", negotiation_round);
Jonathan Tan9c1e6572021-05-04 14:16:01 -07002213 clear_common_flag(acked_commits);
2214 strbuf_release(&req_buf);
2215}
2216
Matt McCutchene860d962017-02-22 11:01:22 -05002217int report_unmatched_refs(struct ref **sought, int nr_sought)
2218{
2219 int i, ret = 0;
2220
2221 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -05002222 if (!sought[i])
Matt McCutchene860d962017-02-22 11:01:22 -05002223 continue;
Matt McCutchend56583d2017-02-22 11:05:57 -05002224 switch (sought[i]->match_status) {
2225 case REF_MATCHED:
2226 continue;
2227 case REF_NOT_MATCHED:
2228 error(_("no such remote ref %s"), sought[i]->name);
2229 break;
2230 case REF_UNADVERTISED_NOT_ALLOWED:
2231 error(_("Server does not allow request for unadvertised object %s"),
2232 sought[i]->name);
2233 break;
2234 }
Matt McCutchene860d962017-02-22 11:01:22 -05002235 ret = 1;
2236 }
2237 return ret;
2238}