blob: a9604f35a3ea9055732d48e39b63a39f041f18f3 [file] [log] [blame]
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001#include "cache.h"
Stefan Bellera49d2832018-03-23 18:45:21 +01002#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02004#include "lockfile.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07005#include "refs.h"
6#include "pkt-line.h"
7#include "commit.h"
8#include "tag.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07009#include "exec-cmd.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070010#include "pack.h"
11#include "sideband.h"
12#include "fetch-pack.h"
13#include "remote.h"
14#include "run-command.h"
Junio C Hamano47a59182013-07-08 13:56:53 -070015#include "connect.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070016#include "transport.h"
17#include "version.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040018#include "oid-array.h"
Jonathan Tanfdb69d32017-05-15 10:32:20 -070019#include "oidset.h"
Jonathan Tan0abe14f2017-08-18 15:20:26 -070020#include "packfile.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070021#include "object-store.h"
Jonathan Tancf1e7c02018-07-02 15:08:43 -070022#include "connected.h"
Jonathan Tanec062832018-06-14 15:54:28 -070023#include "fetch-negotiator.h"
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +000024#include "fsck.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060025#include "shallow.h"
Jonathan Tan9c1e6572021-05-04 14:16:01 -070026#include "commit-reach.h"
27#include "commit-graph.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070028
29static int transfer_unpack_limit = -1;
30static int fetch_unpack_limit = -1;
31static int unpack_limit = 100;
32static int prefer_ofs_delta = 1;
33static int no_done;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070034static int deepen_since_ok;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070035static int deepen_not_ok;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070036static int fetch_fsck_objects = -1;
37static int transfer_fsck_objects = -1;
38static int agent_supported;
Jeff Hostetler640d8b72017-12-08 15:58:40 +000039static int server_supports_filtering;
Josh Steadmon1e905bb2020-11-11 15:29:31 -080040static int advertise_sid;
Taylor Blaucac4b8e2020-04-30 13:48:57 -060041static struct shallow_lock shallow_lock;
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +070042static const char *alternate_shallow_file;
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +020043static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +000044static struct strbuf fsck_msg_types = STRBUF_INIT;
Jonathan Tandd4b7322020-06-10 13:57:23 -070045static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070046
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070047/* Remember to update object flag allocation in object.h */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070048#define COMPLETE (1U << 0)
Jonathan Tanec062832018-06-14 15:54:28 -070049#define ALTERNATE (1U << 1)
Jonathan Tan9c1e6572021-05-04 14:16:01 -070050#define COMMON (1U << 6)
51#define REACH_SCRATCH (1U << 7)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070052
53/*
54 * After sending this many "have"s if we do not get any new ACK , we
55 * give up traversing our history.
56 */
57#define MAX_IN_VAIN 256
58
Jonathan Tand30fe892018-06-14 15:54:26 -070059static int multi_ack, use_sideband;
Fredrik Medley7199c092015-05-21 22:23:38 +020060/* Allow specifying sha1 if it is a ref tip. */
61#define ALLOW_TIP_SHA1 01
Fredrik Medley68ee6282015-05-21 22:23:39 +020062/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
63#define ALLOW_REACHABLE_SHA1 02
Fredrik Medley7199c092015-05-21 22:23:38 +020064static unsigned int allow_unadvertised_object_request;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070065
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +070066__attribute__((format (printf, 2, 3)))
67static inline void print_verbose(const struct fetch_pack_args *args,
68 const char *fmt, ...)
69{
70 va_list params;
71
72 if (!args->verbose)
73 return;
74
75 va_start(params, fmt);
76 vfprintf(stderr, fmt, params);
77 va_end(params);
78 fputc('\n', stderr);
79}
80
Jeff King41a078c2017-02-08 15:53:03 -050081struct alternate_object_cache {
82 struct object **items;
83 size_t nr, alloc;
84};
85
Jeff Kingbdf42762018-10-08 11:09:23 -070086static void cache_one_alternate(const struct object_id *oid,
Jeff King41a078c2017-02-08 15:53:03 -050087 void *vcache)
88{
89 struct alternate_object_cache *cache = vcache;
Stefan Beller109cd762018-06-28 18:21:51 -070090 struct object *obj = parse_object(the_repository, oid);
Jeff King41a078c2017-02-08 15:53:03 -050091
92 if (!obj || (obj->flags & ALTERNATE))
93 return;
94
95 obj->flags |= ALTERNATE;
96 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
97 cache->items[cache->nr++] = obj;
98}
99
Jonathan Tanec062832018-06-14 15:54:28 -0700100static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
101 void (*cb)(struct fetch_negotiator *,
Jonathan Tand30fe892018-06-14 15:54:26 -0700102 struct object *))
Jeff King41a078c2017-02-08 15:53:03 -0500103{
104 static int initialized;
105 static struct alternate_object_cache cache;
106 size_t i;
107
108 if (!initialized) {
109 for_each_alternate_ref(cache_one_alternate, &cache);
110 initialized = 1;
111 }
112
113 for (i = 0; i < cache.nr; i++)
Jonathan Tanec062832018-06-14 15:54:28 -0700114 cb(negotiator, cache.items[i]);
Jeff King41a078c2017-02-08 15:53:03 -0500115}
116
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700117static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
118 int mark_tags_complete)
119{
120 enum object_type type;
121 struct object_info info = { .typep = &type };
Patrick Steinhardt62b5a352021-09-01 15:09:54 +0200122 struct commit *commit;
123
124 commit = lookup_commit_in_graph(the_repository, oid);
125 if (commit)
126 return commit;
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700127
128 while (1) {
129 if (oid_object_info_extended(the_repository, oid, &info,
130 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK))
131 return NULL;
132 if (type == OBJ_TAG) {
133 struct tag *tag = (struct tag *)
134 parse_object(the_repository, oid);
135
136 if (!tag->tagged)
137 return NULL;
138 if (mark_tags_complete)
139 tag->object.flags |= COMPLETE;
140 oid = &tag->tagged->oid;
141 } else {
142 break;
143 }
144 }
Patrick Steinhardt3e5e6c62021-08-04 15:56:11 +0200145
146 if (type == OBJ_COMMIT) {
147 struct commit *commit = lookup_commit(the_repository, oid);
148 if (!commit || repo_parse_commit(the_repository, commit))
149 return NULL;
150 return commit;
151 }
152
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700153 return NULL;
154}
155
Jonathan Tanec062832018-06-14 15:54:28 -0700156static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700157 const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700158{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700159 struct commit *c = deref_without_lazy_fetch(oid, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700160
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700161 if (c)
162 negotiator->add_tip(negotiator, c);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700163 return 0;
164}
165
Michael Haggertyb1b49c62015-05-25 18:39:18 +0000166static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
167 int flag, void *cb_data)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700168{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700169 return rev_list_insert_ref(cb_data, oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700170}
171
172enum ack_type {
173 NAK = 0,
174 ACK,
175 ACK_continue,
176 ACK_common,
177 ACK_ready
178};
179
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800180static void consume_shallow_list(struct fetch_pack_args *args,
181 struct packet_reader *reader)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700182{
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700183 if (args->stateless_rpc && args->deepen) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700184 /* If we sent a depth we will get back "duplicate"
185 * shallow and unshallow commands every time there
186 * is a block of have lines exchanged.
187 */
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800188 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
189 if (starts_with(reader->line, "shallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700190 continue;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800191 if (starts_with(reader->line, "unshallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700192 continue;
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700193 die(_("git fetch-pack: expected shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700194 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800195 if (reader->status != PACKET_READ_FLUSH)
196 die(_("git fetch-pack: expected a flush packet after shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700197 }
198}
199
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800200static enum ack_type get_ack(struct packet_reader *reader,
201 struct object_id *result_oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700202{
Jeff King74543a02013-02-20 15:02:57 -0500203 int len;
Jeff King82e56762014-06-18 15:56:03 -0400204 const char *arg;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700205
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800206 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
Jeff Kingbc9d4dc2018-02-08 13:47:49 -0500207 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800208 len = reader->pktlen;
209
210 if (!strcmp(reader->line, "NAK"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700211 return NAK;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800212 if (skip_prefix(reader->line, "ACK ", &arg)) {
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000213 const char *p;
214 if (!parse_oid_hex(arg, result_oid, &p)) {
215 len -= p - reader->line;
Jeff King82e56762014-06-18 15:56:03 -0400216 if (len < 1)
Jeff King030e9dd2013-02-20 15:00:28 -0500217 return ACK;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000218 if (strstr(p, "continue"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700219 return ACK_continue;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000220 if (strstr(p, "common"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700221 return ACK_common;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000222 if (strstr(p, "ready"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700223 return ACK_ready;
224 return ACK;
225 }
226 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800227 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700228}
229
230static void send_request(struct fetch_pack_args *args,
231 int fd, struct strbuf *buf)
232{
233 if (args->stateless_rpc) {
234 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
235 packet_flush(fd);
Jeff King37c80012019-03-04 23:11:39 -0500236 } else {
237 if (write_in_full(fd, buf->buf, buf->len) < 0)
238 die_errno(_("unable to write to remote"));
239 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700240}
241
Jonathan Tanec062832018-06-14 15:54:28 -0700242static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700243 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700244{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700245 rev_list_insert_ref(negotiator, &obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700246}
247
248#define INITIAL_FLUSH 16
249#define PIPESAFE_FLUSH 32
Jonathan Tanda470982016-07-18 15:21:38 -0700250#define LARGE_FLUSH 16384
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700251
Brandon Williams685fbd32018-03-15 10:31:28 -0700252static int next_flush(int stateless_rpc, int count)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700253{
Brandon Williams685fbd32018-03-15 10:31:28 -0700254 if (stateless_rpc) {
Jonathan Tanda470982016-07-18 15:21:38 -0700255 if (count < LARGE_FLUSH)
256 count <<= 1;
257 else
258 count = count * 11 / 10;
259 } else {
260 if (count < PIPESAFE_FLUSH)
261 count <<= 1;
262 else
263 count += PIPESAFE_FLUSH;
264 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700265 return count;
266}
267
Jonathan Tan3390e422018-07-02 15:39:44 -0700268static void mark_tips(struct fetch_negotiator *negotiator,
269 const struct oid_array *negotiation_tips)
270{
271 int i;
272
273 if (!negotiation_tips) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700274 for_each_rawref(rev_list_insert_ref_oid, negotiator);
Jonathan Tan3390e422018-07-02 15:39:44 -0700275 return;
276 }
277
278 for (i = 0; i < negotiation_tips->nr; i++)
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700279 rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]);
Jonathan Tan3390e422018-07-02 15:39:44 -0700280 return;
281}
282
Jonathan Tanec062832018-06-14 15:54:28 -0700283static int find_common(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700284 struct fetch_pack_args *args,
brian m. carlson1b283372017-05-01 02:28:54 +0000285 int fd[2], struct object_id *result_oid,
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700286 struct ref *refs)
287{
288 int fetching;
289 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
brian m. carlson1b283372017-05-01 02:28:54 +0000290 const struct object_id *oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700291 unsigned in_vain = 0;
292 int got_continue = 0;
293 int got_ready = 0;
294 struct strbuf req_buf = STRBUF_INIT;
295 size_t state_len = 0;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800296 struct packet_reader reader;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700297
298 if (args->stateless_rpc && multi_ack == 1)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700299 die(_("--stateless-rpc requires multi_ack_detailed"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700300
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800301 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -0800302 PACKET_READ_CHOMP_NEWLINE |
303 PACKET_READ_DIE_ON_ERR_PACKET);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800304
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700305 mark_tips(negotiator, args->negotiation_tips);
306 for_each_cached_alternate(negotiator, insert_one_alternate_object);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700307
308 fetching = 0;
309 for ( ; refs ; refs = refs->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000310 struct object_id *remote = &refs->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700311 const char *remote_hex;
312 struct object *o;
313
314 /*
315 * If that object is complete (i.e. it is an ancestor of a
316 * local ref), we tell them we have it but do not have to
317 * tell them about its ancestors, which they already know
318 * about.
319 *
320 * We use lookup_object here because we are only
321 * interested in the case we *know* the object is
322 * reachable and we have already scanned it.
323 */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700324 if (((o = lookup_object(the_repository, remote)) != NULL) &&
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700325 (o->flags & COMPLETE)) {
326 continue;
327 }
328
brian m. carlson1b283372017-05-01 02:28:54 +0000329 remote_hex = oid_to_hex(remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700330 if (!fetching) {
331 struct strbuf c = STRBUF_INIT;
332 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
333 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
334 if (no_done) strbuf_addstr(&c, " no-done");
335 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
336 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700337 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700338 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
339 if (args->no_progress) strbuf_addstr(&c, " no-progress");
340 if (args->include_tag) strbuf_addstr(&c, " include-tag");
341 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700342 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700343 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700344 if (agent_supported) strbuf_addf(&c, " agent=%s",
345 git_user_agent_sanitized());
Josh Steadmon1e905bb2020-11-11 15:29:31 -0800346 if (advertise_sid)
347 strbuf_addf(&c, " session-id=%s", trace2_session_id());
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000348 if (args->filter_options.choice)
349 strbuf_addstr(&c, " filter");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700350 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
351 strbuf_release(&c);
352 } else
353 packet_buf_write(&req_buf, "want %s\n", remote_hex);
354 fetching++;
355 }
356
357 if (!fetching) {
358 strbuf_release(&req_buf);
359 packet_flush(fd[1]);
360 return 1;
361 }
362
Stefan Bellerc8813482018-05-17 15:51:46 -0700363 if (is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700364 write_shallow_commits(&req_buf, 1, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700365 if (args->depth > 0)
366 packet_buf_write(&req_buf, "deepen %d", args->depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700367 if (args->deepen_since) {
Johannes Schindelindddbad72017-04-26 21:29:31 +0200368 timestamp_t max_age = approxidate(args->deepen_since);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200369 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700370 }
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700371 if (args->deepen_not) {
372 int i;
373 for (i = 0; i < args->deepen_not->nr; i++) {
374 struct string_list_item *s = args->deepen_not->items + i;
375 packet_buf_write(&req_buf, "deepen-not %s", s->string);
376 }
377 }
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800378 if (server_supports_filtering && args->filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700379 const char *spec =
380 expand_list_objects_filter_spec(&args->filter_options);
381 packet_buf_write(&req_buf, "filter %s", spec);
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800382 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700383 packet_buf_flush(&req_buf);
384 state_len = req_buf.len;
385
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700386 if (args->deepen) {
Jeff Kingae021d82014-06-18 15:47:50 -0400387 const char *arg;
brian m. carlson1b283372017-05-01 02:28:54 +0000388 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700389
390 send_request(args, fd[1], &req_buf);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800391 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
392 if (skip_prefix(reader.line, "shallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000393 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800394 die(_("invalid shallow line: %s"), reader.line);
Stefan Beller19143f12018-05-17 15:51:44 -0700395 register_shallow(the_repository, &oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700396 continue;
397 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800398 if (skip_prefix(reader.line, "unshallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000399 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800400 die(_("invalid unshallow line: %s"), reader.line);
Jeff Kingd0229ab2019-06-20 03:41:14 -0400401 if (!lookup_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800402 die(_("object not found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700403 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -0700404 if (!parse_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800405 die(_("error in object: %s"), reader.line);
brian m. carlsone92b8482017-05-06 22:10:06 +0000406 if (unregister_shallow(&oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800407 die(_("no shallow found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700408 continue;
409 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800410 die(_("expected shallow/unshallow, got %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700411 }
412 } else if (!args->stateless_rpc)
413 send_request(args, fd[1], &req_buf);
414
415 if (!args->stateless_rpc) {
416 /* If we aren't using the stateless-rpc interface
417 * we don't need to retain the headers.
418 */
419 strbuf_setlen(&req_buf, 0);
420 state_len = 0;
421 }
422
Josh Steadmon5fc31182019-10-02 16:49:28 -0700423 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700424 flushes = 0;
425 retval = -1;
Jonathan Tanec062832018-06-14 15:54:28 -0700426 while ((oid = negotiator->next(negotiator))) {
brian m. carlson1b283372017-05-01 02:28:54 +0000427 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
428 print_verbose(args, "have %s", oid_to_hex(oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700429 in_vain++;
430 if (flush_at <= ++count) {
431 int ack;
432
433 packet_buf_flush(&req_buf);
434 send_request(args, fd[1], &req_buf);
435 strbuf_setlen(&req_buf, state_len);
436 flushes++;
Brandon Williams685fbd32018-03-15 10:31:28 -0700437 flush_at = next_flush(args->stateless_rpc, count);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700438
439 /*
440 * We keep one window "ahead" of the other side, and
441 * will wait for an ACK only on the next one
442 */
443 if (!args->stateless_rpc && count == INITIAL_FLUSH)
444 continue;
445
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800446 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700447 do {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800448 ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700449 if (ack)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700450 print_verbose(args, _("got %s %d %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000451 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700452 switch (ack) {
453 case ACK:
454 flushes = 0;
455 multi_ack = 0;
456 retval = 0;
457 goto done;
458 case ACK_common:
459 case ACK_ready:
460 case ACK_continue: {
461 struct commit *commit =
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700462 lookup_commit(the_repository,
463 result_oid);
Jonathan Tand093bc72018-06-14 15:54:27 -0700464 int was_common;
Junio C Hamano3a2a1dc2018-08-02 15:30:42 -0700465
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700466 if (!commit)
brian m. carlson1b283372017-05-01 02:28:54 +0000467 die(_("invalid commit %s"), oid_to_hex(result_oid));
Jonathan Tanec062832018-06-14 15:54:28 -0700468 was_common = negotiator->ack(negotiator, commit);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700469 if (args->stateless_rpc
470 && ack == ACK_common
Jonathan Tand093bc72018-06-14 15:54:27 -0700471 && !was_common) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700472 /* We need to replay the have for this object
473 * on the next RPC request so the peer knows
474 * it is in common with us.
475 */
brian m. carlson1b283372017-05-01 02:28:54 +0000476 const char *hex = oid_to_hex(result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700477 packet_buf_write(&req_buf, "have %s\n", hex);
478 state_len = req_buf.len;
Jonathan Tan06b3d382016-09-23 10:41:35 -0700479 /*
480 * Reset in_vain because an ack
481 * for this commit has not been
482 * seen.
483 */
484 in_vain = 0;
485 } else if (!args->stateless_rpc
486 || ack != ACK_common)
487 in_vain = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700488 retval = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700489 got_continue = 1;
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700490 if (ack == ACK_ready)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700491 got_ready = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700492 break;
493 }
494 }
495 } while (ack);
496 flushes--;
497 if (got_continue && MAX_IN_VAIN < in_vain) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700498 print_verbose(args, _("giving up"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700499 break; /* give up */
500 }
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700501 if (got_ready)
502 break;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700503 }
504 }
505done:
Josh Steadmon5fc31182019-10-02 16:49:28 -0700506 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700507 if (!got_ready || !no_done) {
508 packet_buf_write(&req_buf, "done\n");
509 send_request(args, fd[1], &req_buf);
510 }
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700511 print_verbose(args, _("done"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700512 if (retval != 0) {
513 multi_ack = 0;
514 flushes++;
515 }
516 strbuf_release(&req_buf);
517
Nguyễn Thái Ngọc Duyff62eca2014-02-06 22:10:39 +0700518 if (!got_ready || !no_done)
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800519 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700520 while (flushes || multi_ack) {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800521 int ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700522 if (ack) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700523 print_verbose(args, _("got %s (%d) %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000524 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700525 if (ack == ACK)
526 return 0;
527 multi_ack = 1;
528 continue;
529 }
530 flushes--;
531 }
532 /* it is no error to fetch into a completely empty repo */
533 return count ? retval : 0;
534}
535
536static struct commit_list *complete;
537
brian m. carlson1b283372017-05-01 02:28:54 +0000538static int mark_complete(const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700539{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700540 struct commit *commit = deref_without_lazy_fetch(oid, 1);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700541
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700542 if (commit && !(commit->object.flags & COMPLETE)) {
543 commit->object.flags |= COMPLETE;
544 commit_list_insert(commit, &complete);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700545 }
546 return 0;
547}
548
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000549static int mark_complete_oid(const char *refname, const struct object_id *oid,
550 int flag, void *cb_data)
551{
brian m. carlson1b283372017-05-01 02:28:54 +0000552 return mark_complete(oid);
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000553}
554
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700555static void mark_recent_complete_commits(struct fetch_pack_args *args,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200556 timestamp_t cutoff)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700557{
558 while (complete && cutoff <= complete->item->date) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700559 print_verbose(args, _("Marking %s as complete"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700560 oid_to_hex(&complete->item->object.oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700561 pop_most_recent_commit(&complete, COMPLETE);
562 }
563}
564
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700565static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
566{
567 for (; refs; refs = refs->next)
568 oidset_insert(oids, &refs->old_oid);
569}
570
René Scharfebf732822018-10-04 17:09:06 +0200571static int is_unmatched_ref(const struct ref *ref)
572{
573 struct object_id oid;
574 const char *p;
575 return ref->match_status == REF_NOT_MATCHED &&
576 !parse_oid_hex(ref->name, &oid, &p) &&
577 *p == '\0' &&
578 oideq(&oid, &ref->old_oid);
579}
580
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700581static void filter_refs(struct fetch_pack_args *args,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800582 struct ref **refs,
583 struct ref **sought, int nr_sought)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700584{
585 struct ref *newlist = NULL;
586 struct ref **newtail = &newlist;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700587 struct ref *unmatched = NULL;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700588 struct ref *ref, *next;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700589 struct oidset tip_oids = OIDSET_INIT;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800590 int i;
René Scharfe22a16462018-10-04 17:09:39 +0200591 int strict = !(allow_unadvertised_object_request &
592 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700593
Junio C Hamanof2db8542013-01-29 14:02:15 -0800594 i = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700595 for (ref = *refs; ref; ref = next) {
596 int keep = 0;
597 next = ref->next;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800598
René Scharfe50e19a82014-06-06 19:24:48 +0200599 if (starts_with(ref->name, "refs/") &&
Jeff King34066f02019-04-13 01:57:37 -0400600 check_refname_format(ref->name, 0)) {
601 /*
602 * trash or a peeled value; do not even add it to
603 * unmatched list
604 */
605 free_one_ref(ref);
606 continue;
607 } else {
Junio C Hamanof2db8542013-01-29 14:02:15 -0800608 while (i < nr_sought) {
609 int cmp = strcmp(ref->name, sought[i]->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700610 if (cmp < 0)
611 break; /* definitely do not have it */
612 else if (cmp == 0) {
613 keep = 1; /* definitely have it */
Matt McCutchend56583d2017-02-22 11:05:57 -0500614 sought[i]->match_status = REF_MATCHED;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700615 }
Junio C Hamanof2db8542013-01-29 14:02:15 -0800616 i++;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700617 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700618
Jeff Kinge9502c02018-06-11 01:53:57 -0400619 if (!keep && args->fetch_all &&
620 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
621 keep = 1;
622 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700623
624 if (keep) {
625 *newtail = ref;
626 ref->next = NULL;
627 newtail = &ref->next;
628 } else {
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700629 ref->next = unmatched;
630 unmatched = ref;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700631 }
632 }
633
René Scharfe22a16462018-10-04 17:09:39 +0200634 if (strict) {
635 for (i = 0; i < nr_sought; i++) {
636 ref = sought[i];
637 if (!is_unmatched_ref(ref))
638 continue;
639
640 add_refs_to_oidset(&tip_oids, unmatched);
641 add_refs_to_oidset(&tip_oids, newlist);
642 break;
643 }
644 }
645
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800646 /* Append unmatched requests to the list */
Matt McCutchend56583d2017-02-22 11:05:57 -0500647 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500648 ref = sought[i];
René Scharfebf732822018-10-04 17:09:06 +0200649 if (!is_unmatched_ref(ref))
Matt McCutchend56583d2017-02-22 11:05:57 -0500650 continue;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800651
René Scharfe22a16462018-10-04 17:09:39 +0200652 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500653 ref->match_status = REF_MATCHED;
Jeff Kingc3c17bf2015-03-19 16:37:09 -0400654 *newtail = copy_ref(ref);
655 newtail = &(*newtail)->next;
Matt McCutchend56583d2017-02-22 11:05:57 -0500656 } else {
657 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800658 }
659 }
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700660
661 oidset_clear(&tip_oids);
Jeff King259eddd2019-04-13 01:54:09 -0400662 free_refs(unmatched);
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700663
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700664 *refs = newlist;
665}
666
Jonathan Tanec062832018-06-14 15:54:28 -0700667static void mark_alternate_complete(struct fetch_negotiator *unused,
Jonathan Tand30fe892018-06-14 15:54:26 -0700668 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700669{
brian m. carlson1b283372017-05-01 02:28:54 +0000670 mark_complete(&obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700671}
672
Takuto Ikuta024aa462018-03-14 15:32:42 +0900673struct loose_object_iter {
674 struct oidset *loose_object_set;
675 struct ref *refs;
676};
677
678/*
Jonathan Tan34c29032018-06-06 13:47:07 -0700679 * Mark recent commits available locally and reachable from a local ref as
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700680 * COMPLETE.
Jonathan Tan34c29032018-06-06 13:47:07 -0700681 *
682 * The cutoff time for recency is determined by this heuristic: it is the
683 * earliest commit time of the objects in refs that are commits and that we know
684 * the commit time of.
685 */
Jonathan Tanec062832018-06-14 15:54:28 -0700686static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700687 struct fetch_pack_args *args,
Jonathan Tan34c29032018-06-06 13:47:07 -0700688 struct ref **refs)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700689{
690 struct ref *ref;
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000691 int old_save_commit_buffer = save_commit_buffer;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200692 timestamp_t cutoff = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700693
694 save_commit_buffer = 0;
695
Erik Chen9e5afdf2019-11-19 23:02:09 +0000696 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700697 for (ref = *refs; ref; ref = ref->next) {
698 struct object *o;
699
Jeff King97b2fa02018-11-12 09:55:58 -0500700 if (!has_object_file_with_flags(&ref->old_oid,
Jonathan Tan6462d5e2019-11-05 10:56:19 -0800701 OBJECT_INFO_QUICK |
702 OBJECT_INFO_SKIP_FETCH_OBJECT))
Junio C Hamano012a1bb2013-01-26 19:42:09 -0800703 continue;
Stefan Beller109cd762018-06-28 18:21:51 -0700704 o = parse_object(the_repository, &ref->old_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700705 if (!o)
706 continue;
707
Erik Chen9e5afdf2019-11-19 23:02:09 +0000708 /*
709 * We already have it -- which may mean that we were
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700710 * in sync with the other side at some time after
711 * that (it is OK if we guess wrong here).
712 */
713 if (o->type == OBJ_COMMIT) {
714 struct commit *commit = (struct commit *)o;
715 if (!cutoff || cutoff < commit->date)
716 cutoff = commit->date;
717 }
718 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000719 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700720
Erik Chen9e5afdf2019-11-19 23:02:09 +0000721 /*
722 * This block marks all local refs as COMPLETE, and then recursively marks all
723 * parents of those refs as COMPLETE.
724 */
725 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700726 if (!args->deepen) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700727 for_each_rawref(mark_complete_oid, NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700728 for_each_cached_alternate(NULL, mark_alternate_complete);
729 commit_list_sort_by_date(&complete);
730 if (cutoff)
731 mark_recent_complete_commits(args, cutoff);
732 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000733 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700734
Jonathan Tan12f19a92018-10-03 16:04:52 -0700735 /*
736 * Mark all complete remote refs as common refs.
737 * Don't mark them common yet; the server has to be told so first.
738 */
Erik Chen9e5afdf2019-11-19 23:02:09 +0000739 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700740 for (ref = *refs; ref; ref = ref->next) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700741 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700742
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700743 if (!c || !(c->object.flags & COMPLETE))
Jonathan Tan12f19a92018-10-03 16:04:52 -0700744 continue;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700745
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700746 negotiator->known_common(negotiator, c);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700747 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000748 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700749
Jonathan Tan34c29032018-06-06 13:47:07 -0700750 save_commit_buffer = old_save_commit_buffer;
751}
752
753/*
754 * Returns 1 if every object pointed to by the given remote refs is available
755 * locally and reachable from a local ref, and 0 otherwise.
756 */
757static int everything_local(struct fetch_pack_args *args,
758 struct ref **refs)
759{
760 struct ref *ref;
761 int retval;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700762
763 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000764 const struct object_id *remote = &ref->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700765 struct object *o;
766
Jeff Kingd0229ab2019-06-20 03:41:14 -0400767 o = lookup_object(the_repository, remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700768 if (!o || !(o->flags & COMPLETE)) {
769 retval = 0;
brian m. carlson1b283372017-05-01 02:28:54 +0000770 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700771 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700772 continue;
773 }
brian m. carlson1b283372017-05-01 02:28:54 +0000774 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700775 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700776 }
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000777
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700778 return retval;
779}
780
781static int sideband_demux(int in, int out, void *data)
782{
783 int *xd = data;
Jeff King9ff18fa2016-02-24 02:44:58 -0500784 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700785
Jeff King9ff18fa2016-02-24 02:44:58 -0500786 ret = recv_sideband("fetch-pack", xd[0], out);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700787 close(out);
788 return ret;
789}
790
Christian Couder9d7fa3b2021-01-12 09:21:58 +0100791static void create_promisor_file(const char *keep_name,
792 struct ref **sought, int nr_sought)
Jonathan Tan5374a292019-10-14 17:12:31 -0700793{
794 struct strbuf promisor_name = STRBUF_INIT;
795 int suffix_stripped;
Jonathan Tan5374a292019-10-14 17:12:31 -0700796
797 strbuf_addstr(&promisor_name, keep_name);
798 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
799 if (!suffix_stripped)
800 BUG("name of pack lockfile should end with .keep (was '%s')",
801 keep_name);
802 strbuf_addstr(&promisor_name, ".promisor");
803
Christian Couder33add2a2021-01-12 09:21:59 +0100804 write_promisor_file(promisor_name.buf, sought, nr_sought);
Jonathan Tan5374a292019-10-14 17:12:31 -0700805
806 strbuf_release(&promisor_name);
807}
808
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800809static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids)
810{
811 int len = the_hash_algo->hexsz + 1; /* hash + NL */
812
813 do {
814 char hex_hash[GIT_MAX_HEXSZ + 1];
815 int read_len = read_in_full(fd, hex_hash, len);
816 struct object_id oid;
817 const char *end;
818
819 if (!read_len)
820 return;
821 if (read_len != len)
822 die("invalid length read %d", read_len);
823 if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n')
824 die("invalid hash");
825 oidset_insert(gitmodules_oids, &oid);
826 } while (1);
827}
828
Jonathan Tanece9aea2020-08-17 12:48:19 -0700829/*
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800830 * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args.
831 * The strings to pass as the --index-pack-arg arguments to http-fetch will be
832 * stored there. (It must be freed by the caller.)
Jonathan Tanece9aea2020-08-17 12:48:19 -0700833 */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700834static int get_pack(struct fetch_pack_args *args,
Jonathan Tan9da69a62020-06-10 13:57:22 -0700835 int xd[2], struct string_list *pack_lockfiles,
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800836 struct strvec *index_pack_args,
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800837 struct ref **sought, int nr_sought,
838 struct oidset *gitmodules_oids)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700839{
840 struct async demux;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700841 int do_keep = args->keep_pack;
Jeff King984a43b2015-09-24 17:07:54 -0400842 const char *cmd_name;
843 struct pack_header header;
844 int pass_header = 0;
René Scharfed3180272014-08-19 21:09:35 +0200845 struct child_process cmd = CHILD_PROCESS_INIT;
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800846 int fsck_objects = 0;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700847 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700848
849 memset(&demux, 0, sizeof(demux));
850 if (use_sideband) {
851 /* xd[] is talking with upload-pack; subprocess reads from
852 * xd[0], spits out band#2 to stderr, and feeds us band#1
853 * through demux->out.
854 */
855 demux.proc = sideband_demux;
856 demux.data = xd;
857 demux.out = -1;
Jeff Kingdf857572016-04-19 18:50:29 -0400858 demux.isolate_sigpipe = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700859 if (start_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700860 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700861 }
862 else
863 demux.out = xd[0];
864
Jonathan Tan2aec3bc2021-03-04 17:16:20 -0800865 if (!args->keep_pack && unpack_limit && !index_pack_args) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700866
867 if (read_pack_header(demux.out, &header))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700868 die(_("protocol error: bad pack header"));
Jeff King984a43b2015-09-24 17:07:54 -0400869 pass_header = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700870 if (ntohl(header.hdr_entries) < unpack_limit)
871 do_keep = 0;
872 else
873 do_keep = 1;
874 }
875
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700876 if (alternate_shallow_file) {
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400877 strvec_push(&cmd.args, "--shallow-file");
878 strvec_push(&cmd.args, alternate_shallow_file);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700879 }
880
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800881 if (fetch_fsck_objects >= 0
882 ? fetch_fsck_objects
883 : transfer_fsck_objects >= 0
884 ? transfer_fsck_objects
885 : 0)
886 fsck_objects = 1;
887
888 if (do_keep || args->from_promisor || index_pack_args || fsck_objects) {
889 if (pack_lockfiles || fsck_objects)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700890 cmd.out = -1;
Jeff King984a43b2015-09-24 17:07:54 -0400891 cmd_name = "index-pack";
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400892 strvec_push(&cmd.args, cmd_name);
893 strvec_push(&cmd.args, "--stdin");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700894 if (!args->quiet && !args->no_progress)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400895 strvec_push(&cmd.args, "-v");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700896 if (args->use_thin_pack)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400897 strvec_push(&cmd.args, "--fix-thin");
Jonathan Tan2aec3bc2021-03-04 17:16:20 -0800898 if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit)) {
René Scharfeda25bdb2017-04-18 17:57:42 -0400899 char hostname[HOST_NAME_MAX + 1];
David Turner5781a9a2017-04-18 17:57:43 -0400900 if (xgethostname(hostname, sizeof(hostname)))
Jeff King984a43b2015-09-24 17:07:54 -0400901 xsnprintf(hostname, sizeof(hostname), "localhost");
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400902 strvec_pushf(&cmd.args,
Jeff Kingf6d89422020-07-28 16:26:31 -0400903 "--keep=fetch-pack %"PRIuMAX " on %s",
904 (uintmax_t)getpid(), hostname);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700905 }
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800906 if (!index_pack_args && args->check_self_contained_and_connected)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400907 strvec_push(&cmd.args, "--check-self-contained-and-connected");
Jonathan Tandd4b7322020-06-10 13:57:23 -0700908 else
909 /*
910 * We cannot perform any connectivity checks because
911 * not all packs have been downloaded; let the caller
912 * have this responsibility.
913 */
914 args->check_self_contained_and_connected = 0;
Jonathan Tan1b03df52020-08-20 10:51:16 -0700915
916 if (args->from_promisor)
917 /*
Christian Couder9d7fa3b2021-01-12 09:21:58 +0100918 * create_promisor_file() may be called afterwards but
Jonathan Tan1b03df52020-08-20 10:51:16 -0700919 * we still need index-pack to know that this is a
920 * promisor pack. For example, if transfer.fsckobjects
921 * is true, index-pack needs to know that .gitmodules
922 * is a promisor object (so that it won't complain if
923 * it is missing).
924 */
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400925 strvec_push(&cmd.args, "--promisor");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700926 }
927 else {
Jeff King984a43b2015-09-24 17:07:54 -0400928 cmd_name = "unpack-objects";
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400929 strvec_push(&cmd.args, cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700930 if (args->quiet || args->no_progress)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400931 strvec_push(&cmd.args, "-q");
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700932 args->check_self_contained_and_connected = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700933 }
Jeff King984a43b2015-09-24 17:07:54 -0400934
935 if (pass_header)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400936 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
Jeff Kingf6d89422020-07-28 16:26:31 -0400937 ntohl(header.hdr_version),
Jeff King984a43b2015-09-24 17:07:54 -0400938 ntohl(header.hdr_entries));
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800939 if (fsck_objects) {
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800940 if (args->from_promisor || index_pack_args)
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700941 /*
942 * We cannot use --strict in index-pack because it
943 * checks both broken objects and links, but we only
944 * want to check for broken objects.
945 */
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400946 strvec_push(&cmd.args, "--fsck-objects");
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700947 else
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400948 strvec_pushf(&cmd.args, "--strict%s",
Jeff Kingf6d89422020-07-28 16:26:31 -0400949 fsck_msg_types.buf);
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700950 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700951
Jonathan Tanb664e9f2021-02-22 11:20:08 -0800952 if (index_pack_args) {
953 int i;
954
955 for (i = 0; i < cmd.args.nr; i++)
956 strvec_push(index_pack_args, cmd.args.v[i]);
957 }
958
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700959 cmd.in = demux.out;
960 cmd.git_cmd = 1;
961 if (start_command(&cmd))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700962 die(_("fetch-pack: unable to fork off %s"), cmd_name);
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800963 if (do_keep && (pack_lockfiles || fsck_objects)) {
964 int is_well_formed;
965 char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed);
966
967 if (!is_well_formed)
968 die(_("fetch-pack: invalid index-pack output"));
René Scharfe6031af32020-11-30 20:27:15 +0100969 if (pack_lockfile)
970 string_list_append_nodup(pack_lockfiles, pack_lockfile);
Jonathan Tan5476e1e2021-02-22 11:20:09 -0800971 parse_gitmodules_oids(cmd.out, gitmodules_oids);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700972 close(cmd.out);
973 }
974
Jens Lindstrom37cb1dd2013-10-22 15:36:02 +0200975 if (!use_sideband)
976 /* Closed by start_command() */
977 xd[0] = -1;
978
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700979 ret = finish_command(&cmd);
980 if (!ret || (args->check_self_contained_and_connected && ret == 1))
981 args->self_contained_and_connected =
982 args->check_self_contained_and_connected &&
983 ret == 0;
984 else
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700985 die(_("%s failed"), cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700986 if (use_sideband && finish_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700987 die(_("error in sideband demultiplexer"));
Jonathan Tan5374a292019-10-14 17:12:31 -0700988
989 /*
990 * Now that index-pack has succeeded, write the promisor file using the
991 * obtained .keep filename if necessary
992 */
Jonathan Tan9da69a62020-06-10 13:57:22 -0700993 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
Christian Couder9d7fa3b2021-01-12 09:21:58 +0100994 create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
Jonathan Tan5374a292019-10-14 17:12:31 -0700995
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700996 return 0;
997}
998
Junio C Hamanof2db8542013-01-29 14:02:15 -0800999static int cmp_ref_by_name(const void *a_, const void *b_)
1000{
1001 const struct ref *a = *((const struct ref **)a_);
1002 const struct ref *b = *((const struct ref **)b_);
1003 return strcmp(a->name, b->name);
1004}
1005
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001006static struct ref *do_fetch_pack(struct fetch_pack_args *args,
1007 int fd[2],
1008 const struct ref *orig_ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -08001009 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001010 struct shallow_info *si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001011 struct string_list *pack_lockfiles)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001012{
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001013 struct repository *r = the_repository;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001014 struct ref *ref = copy_ref_list(orig_ref);
brian m. carlson1b283372017-05-01 02:28:54 +00001015 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001016 const char *agent_feature;
1017 int agent_len;
Jonathan Tan603960b2019-11-12 16:34:20 -08001018 struct fetch_negotiator negotiator_alloc;
1019 struct fetch_negotiator *negotiator;
1020
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001021 negotiator = &negotiator_alloc;
1022 fetch_negotiator_init(r, negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001023
1024 sort_ref_list(&ref, ref_compare_name);
René Scharfe9ed0d8d2016-09-29 17:27:31 +02001025 QSORT(sought, nr_sought, cmp_ref_by_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001026
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001027 if ((agent_feature = server_feature_value("agent", &agent_len))) {
1028 agent_supported = 1;
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +07001029 if (agent_len)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001030 print_verbose(args, _("Server version is %.*s"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +07001031 agent_len, agent_feature);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001032 }
Nguyễn Thái Ngọc Duy0e042972019-06-20 18:59:51 +07001033
Josh Steadmon1e905bb2020-11-11 15:29:31 -08001034 if (!server_supports("session-id"))
1035 advertise_sid = 0;
1036
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001037 if (server_supports("shallow"))
1038 print_verbose(args, _("Server supports %s"), "shallow");
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001039 else if (args->depth > 0 || is_repository_shallow(r))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001040 die(_("Server does not support shallow clients"));
1041 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1042 args->deepen = 1;
1043 if (server_supports("multi_ack_detailed")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001044 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001045 multi_ack = 2;
1046 if (server_supports("no-done")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001047 print_verbose(args, _("Server supports %s"), "no-done");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001048 if (args->stateless_rpc)
1049 no_done = 1;
1050 }
1051 }
1052 else if (server_supports("multi_ack")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001053 print_verbose(args, _("Server supports %s"), "multi_ack");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001054 multi_ack = 1;
1055 }
1056 if (server_supports("side-band-64k")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001057 print_verbose(args, _("Server supports %s"), "side-band-64k");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001058 use_sideband = 2;
1059 }
1060 else if (server_supports("side-band")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001061 print_verbose(args, _("Server supports %s"), "side-band");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001062 use_sideband = 1;
1063 }
1064 if (server_supports("allow-tip-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001065 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001066 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1067 }
1068 if (server_supports("allow-reachable-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001069 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001070 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1071 }
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001072 if (server_supports("thin-pack"))
1073 print_verbose(args, _("Server supports %s"), "thin-pack");
1074 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001075 args->use_thin_pack = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001076 if (server_supports("no-progress"))
1077 print_verbose(args, _("Server supports %s"), "no-progress");
1078 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001079 args->no_progress = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001080 if (server_supports("include-tag"))
1081 print_verbose(args, _("Server supports %s"), "include-tag");
1082 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001083 args->include_tag = 0;
1084 if (server_supports("ofs-delta"))
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001085 print_verbose(args, _("Server supports %s"), "ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001086 else
1087 prefer_ofs_delta = 0;
1088
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001089 if (server_supports("filter")) {
1090 server_supports_filtering = 1;
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001091 print_verbose(args, _("Server supports %s"), "filter");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001092 } else if (args->filter_options.choice) {
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001093 warning("filtering not recognized by server, ignoring");
1094 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001095
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001096 if (server_supports("deepen-since")) {
1097 print_verbose(args, _("Server supports %s"), "deepen-since");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001098 deepen_since_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001099 } else if (args->deepen_since)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001100 die(_("Server does not support --shallow-since"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001101 if (server_supports("deepen-not")) {
1102 print_verbose(args, _("Server supports %s"), "deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001103 deepen_not_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001104 } else if (args->deepen_not)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001105 die(_("Server does not support --shallow-exclude"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001106 if (server_supports("deepen-relative"))
1107 print_verbose(args, _("Server supports %s"), "deepen-relative");
1108 else if (args->deepen_relative)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001109 die(_("Server does not support --deepen"));
brian m. carlson48bf1412020-05-25 19:58:59 +00001110 if (!server_supports_hash(the_hash_algo->name, NULL))
1111 die(_("Server does not support this repository's object format"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001112
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001113 mark_complete_and_common_ref(negotiator, args, &ref);
1114 filter_refs(args, &ref, sought, nr_sought);
1115 if (everything_local(args, &ref)) {
1116 packet_flush(fd[1]);
1117 goto all_done;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001118 }
Jonathan Tan603960b2019-11-12 16:34:20 -08001119 if (find_common(negotiator, args, fd, &oid, ref) < 0)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001120 if (!args->keep_pack)
1121 /* When cloning, it is not unusual to have
1122 * no common commit.
1123 */
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001124 warning(_("no common commits"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001125
1126 if (args->stateless_rpc)
1127 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001128 if (args->deepen)
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +07001129 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1130 NULL);
Li Linchao4fe788b2021-04-01 10:46:59 +00001131 else if (si->nr_ours || si->nr_theirs) {
1132 if (args->reject_shallow_remote)
1133 die(_("source repository is shallow, reject to clone."));
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001134 alternate_shallow_file = setup_temporary_shallow(si->shallow);
Li Linchao4fe788b2021-04-01 10:46:59 +00001135 } else
Nguyễn Thái Ngọc Duy6da8bdc2013-08-26 09:17:26 +07001136 alternate_shallow_file = NULL;
Jonathan Tan5476e1e2021-02-22 11:20:09 -08001137 if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought,
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001138 &fsck_options.gitmodules_found))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001139 die(_("git fetch-pack: fetch failed."));
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001140 if (fsck_finish(&fsck_options))
1141 die("fsck failed");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001142
1143 all_done:
Jonathan Tan603960b2019-11-12 16:34:20 -08001144 if (negotiator)
1145 negotiator->release(negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001146 return ref;
1147}
1148
Brandon Williamsf7e20502018-03-15 10:31:29 -07001149static void add_shallow_requests(struct strbuf *req_buf,
1150 const struct fetch_pack_args *args)
1151{
Junio C Hamano00624d62018-07-18 12:20:27 -07001152 if (is_repository_shallow(the_repository))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001153 write_shallow_commits(req_buf, 1, NULL);
1154 if (args->depth > 0)
1155 packet_buf_write(req_buf, "deepen %d", args->depth);
1156 if (args->deepen_since) {
1157 timestamp_t max_age = approxidate(args->deepen_since);
1158 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1159 }
1160 if (args->deepen_not) {
1161 int i;
1162 for (i = 0; i < args->deepen_not->nr; i++) {
1163 struct string_list_item *s = args->deepen_not->items + i;
1164 packet_buf_write(req_buf, "deepen-not %s", s->string);
1165 }
1166 }
Jonathan Tan5056cf42018-12-18 13:24:35 -08001167 if (args->deepen_relative)
1168 packet_buf_write(req_buf, "deepen-relative\n");
Brandon Williamsf7e20502018-03-15 10:31:29 -07001169}
1170
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001171static void add_wants(const struct ref *wants, struct strbuf *req_buf)
Brandon Williams685fbd32018-03-15 10:31:28 -07001172{
Brandon Williams73302052018-06-27 15:30:23 -07001173 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1174
Brandon Williams685fbd32018-03-15 10:31:28 -07001175 for ( ; wants ; wants = wants->next) {
1176 const struct object_id *remote = &wants->old_oid;
Brandon Williams685fbd32018-03-15 10:31:28 -07001177 struct object *o;
1178
1179 /*
1180 * If that object is complete (i.e. it is an ancestor of a
1181 * local ref), we tell them we have it but do not have to
1182 * tell them about its ancestors, which they already know
1183 * about.
1184 *
1185 * We use lookup_object here because we are only
1186 * interested in the case we *know* the object is
1187 * reachable and we have already scanned it.
1188 */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001189 if (((o = lookup_object(the_repository, remote)) != NULL) &&
Brandon Williams685fbd32018-03-15 10:31:28 -07001190 (o->flags & COMPLETE)) {
1191 continue;
1192 }
1193
Brandon Williams73302052018-06-27 15:30:23 -07001194 if (!use_ref_in_want || wants->exact_oid)
1195 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1196 else
1197 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
Brandon Williams685fbd32018-03-15 10:31:28 -07001198 }
1199}
1200
1201static void add_common(struct strbuf *req_buf, struct oidset *common)
1202{
1203 struct oidset_iter iter;
1204 const struct object_id *oid;
1205 oidset_iter_init(common, &iter);
1206
1207 while ((oid = oidset_iter_next(&iter))) {
1208 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1209 }
1210}
1211
Jonathan Tanec062832018-06-14 15:54:28 -07001212static int add_haves(struct fetch_negotiator *negotiator,
1213 struct strbuf *req_buf,
Jonathan Tan57c34512021-04-08 18:10:00 -07001214 int *haves_to_send)
Brandon Williams685fbd32018-03-15 10:31:28 -07001215{
Brandon Williams685fbd32018-03-15 10:31:28 -07001216 int haves_added = 0;
1217 const struct object_id *oid;
1218
Jonathan Tanec062832018-06-14 15:54:28 -07001219 while ((oid = negotiator->next(negotiator))) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001220 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1221 if (++haves_added >= *haves_to_send)
1222 break;
1223 }
1224
Brandon Williams685fbd32018-03-15 10:31:28 -07001225 /* Increase haves to send on next round */
1226 *haves_to_send = next_flush(1, *haves_to_send);
1227
Jonathan Tan57c34512021-04-08 18:10:00 -07001228 return haves_added;
Brandon Williams685fbd32018-03-15 10:31:28 -07001229}
1230
Jonathan Tan6871d0c2021-04-08 18:10:01 -07001231static void write_fetch_command_and_capabilities(struct strbuf *req_buf,
1232 const struct string_list *server_options)
1233{
1234 const char *hash_name;
1235
1236 if (server_supports_v2("fetch", 1))
1237 packet_buf_write(req_buf, "command=fetch");
1238 if (server_supports_v2("agent", 0))
1239 packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized());
1240 if (advertise_sid && server_supports_v2("session-id", 0))
1241 packet_buf_write(req_buf, "session-id=%s", trace2_session_id());
1242 if (server_options && server_options->nr &&
1243 server_supports_v2("server-option", 1)) {
1244 int i;
1245 for (i = 0; i < server_options->nr; i++)
1246 packet_buf_write(req_buf, "server-option=%s",
1247 server_options->items[i].string);
1248 }
1249
1250 if (server_feature_v2("object-format", &hash_name)) {
1251 int hash_algo = hash_algo_by_name(hash_name);
1252 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1253 die(_("mismatched algorithms: client %s; server %s"),
1254 the_hash_algo->name, hash_name);
1255 packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name);
1256 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1257 die(_("the server does not support algorithm '%s'"),
1258 the_hash_algo->name);
1259 }
1260 packet_buf_delim(req_buf);
1261}
1262
Jonathan Tanec062832018-06-14 15:54:28 -07001263static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001264 struct fetch_pack_args *args,
Brandon Williams685fbd32018-03-15 10:31:28 -07001265 const struct ref *wants, struct oidset *common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001266 int *haves_to_send, int *in_vain,
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001267 int sideband_all, int seen_ack)
Brandon Williams685fbd32018-03-15 10:31:28 -07001268{
Jonathan Tan57c34512021-04-08 18:10:00 -07001269 int haves_added;
1270 int done_sent = 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001271 struct strbuf req_buf = STRBUF_INIT;
1272
Jonathan Tan6871d0c2021-04-08 18:10:01 -07001273 write_fetch_command_and_capabilities(&req_buf, args->server_options);
Brandon Williams685fbd32018-03-15 10:31:28 -07001274
Brandon Williams685fbd32018-03-15 10:31:28 -07001275 if (args->use_thin_pack)
1276 packet_buf_write(&req_buf, "thin-pack");
1277 if (args->no_progress)
1278 packet_buf_write(&req_buf, "no-progress");
1279 if (args->include_tag)
1280 packet_buf_write(&req_buf, "include-tag");
1281 if (prefer_ofs_delta)
1282 packet_buf_write(&req_buf, "ofs-delta");
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001283 if (sideband_all)
1284 packet_buf_write(&req_buf, "sideband-all");
Brandon Williams685fbd32018-03-15 10:31:28 -07001285
Brandon Williamsf7e20502018-03-15 10:31:29 -07001286 /* Add shallow-info and deepen request */
1287 if (server_supports_feature("fetch", "shallow", 0))
1288 add_shallow_requests(&req_buf, args);
Junio C Hamano00624d62018-07-18 12:20:27 -07001289 else if (is_repository_shallow(the_repository) || args->deepen)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001290 die(_("Server does not support shallow requests"));
1291
Jonathan Tanba957102018-05-03 16:46:56 -07001292 /* Add filter */
1293 if (server_supports_feature("fetch", "filter", 0) &&
1294 args->filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001295 const char *spec =
1296 expand_list_objects_filter_spec(&args->filter_options);
Jonathan Tanba957102018-05-03 16:46:56 -07001297 print_verbose(args, _("Server supports filter"));
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001298 packet_buf_write(&req_buf, "filter %s", spec);
Jonathan Tanba957102018-05-03 16:46:56 -07001299 } else if (args->filter_options.choice) {
1300 warning("filtering not recognized by server, ignoring");
1301 }
1302
Jonathan Tandd4b7322020-06-10 13:57:23 -07001303 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1304 int i;
1305 struct strbuf to_send = STRBUF_INIT;
1306
1307 for (i = 0; i < uri_protocols.nr; i++) {
1308 const char *s = uri_protocols.items[i].string;
1309
1310 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1311 if (to_send.len)
1312 strbuf_addch(&to_send, ',');
1313 strbuf_addstr(&to_send, s);
1314 }
1315 }
1316 if (to_send.len) {
1317 packet_buf_write(&req_buf, "packfile-uris %s",
1318 to_send.buf);
1319 strbuf_release(&to_send);
1320 }
1321 }
1322
Brandon Williams685fbd32018-03-15 10:31:28 -07001323 /* add wants */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001324 add_wants(wants, &req_buf);
Brandon Williams685fbd32018-03-15 10:31:28 -07001325
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001326 /* Add all of the common commits we've found in previous rounds */
1327 add_common(&req_buf, common);
Brandon Williams685fbd32018-03-15 10:31:28 -07001328
Jonathan Tan57c34512021-04-08 18:10:00 -07001329 haves_added = add_haves(negotiator, &req_buf, haves_to_send);
1330 *in_vain += haves_added;
1331 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
1332 /* Send Done */
1333 packet_buf_write(&req_buf, "done\n");
1334 done_sent = 1;
1335 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001336
1337 /* Send request */
1338 packet_buf_flush(&req_buf);
Jeff King37c80012019-03-04 23:11:39 -05001339 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1340 die_errno(_("unable to write request to remote"));
Brandon Williams685fbd32018-03-15 10:31:28 -07001341
1342 strbuf_release(&req_buf);
Jonathan Tan57c34512021-04-08 18:10:00 -07001343 return done_sent;
Brandon Williams685fbd32018-03-15 10:31:28 -07001344}
1345
1346/*
1347 * Processes a section header in a server's response and checks if it matches
1348 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1349 * not consumed); if 0, the line will be consumed and the function will die if
1350 * the section header doesn't match what was expected.
1351 */
1352static int process_section_header(struct packet_reader *reader,
1353 const char *section, int peek)
1354{
1355 int ret;
1356
1357 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001358 die(_("error reading section header '%s'"), section);
Brandon Williams685fbd32018-03-15 10:31:28 -07001359
1360 ret = !strcmp(reader->line, section);
1361
1362 if (!peek) {
1363 if (!ret)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001364 die(_("expected '%s', received '%s'"),
Brandon Williams685fbd32018-03-15 10:31:28 -07001365 section, reader->line);
1366 packet_reader_read(reader);
1367 }
1368
1369 return ret;
1370}
1371
Jonathan Tan81025702021-04-08 18:09:59 -07001372static int process_ack(struct fetch_negotiator *negotiator,
1373 struct packet_reader *reader,
1374 struct object_id *common_oid,
1375 int *received_ready)
Brandon Williams685fbd32018-03-15 10:31:28 -07001376{
Brandon Williams685fbd32018-03-15 10:31:28 -07001377 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1378 const char *arg;
1379
1380 if (!strcmp(reader->line, "NAK"))
1381 continue;
1382
1383 if (skip_prefix(reader->line, "ACK ", &arg)) {
Jonathan Tan81025702021-04-08 18:09:59 -07001384 if (!get_oid_hex(arg, common_oid)) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001385 struct commit *commit;
Jonathan Tan81025702021-04-08 18:09:59 -07001386 commit = lookup_commit(the_repository, common_oid);
Jonathan Tan603960b2019-11-12 16:34:20 -08001387 if (negotiator)
1388 negotiator->ack(negotiator, commit);
Brandon Williams685fbd32018-03-15 10:31:28 -07001389 }
Jonathan Tan81025702021-04-08 18:09:59 -07001390 return 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001391 }
1392
1393 if (!strcmp(reader->line, "ready")) {
Jonathan Tan81025702021-04-08 18:09:59 -07001394 *received_ready = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001395 continue;
1396 }
1397
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001398 die(_("unexpected acknowledgment line: '%s'"), reader->line);
Brandon Williams685fbd32018-03-15 10:31:28 -07001399 }
1400
1401 if (reader->status != PACKET_READ_FLUSH &&
1402 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001403 die(_("error processing acks: %d"), reader->status);
Brandon Williams685fbd32018-03-15 10:31:28 -07001404
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001405 /*
1406 * If an "acknowledgments" section is sent, a packfile is sent if and
1407 * only if "ready" was sent in this section. The other sections
1408 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1409 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1410 * otherwise.
1411 */
Jonathan Tan81025702021-04-08 18:09:59 -07001412 if (*received_ready && reader->status != PACKET_READ_DELIM)
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001413 die(_("expected packfile to be sent after 'ready'"));
Jonathan Tan81025702021-04-08 18:09:59 -07001414 if (!*received_ready && reader->status != PACKET_READ_FLUSH)
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001415 die(_("expected no other sections to be sent after no 'ready'"));
1416
Jonathan Tan81025702021-04-08 18:09:59 -07001417 return 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001418}
1419
Brandon Williamsf7e20502018-03-15 10:31:29 -07001420static void receive_shallow_info(struct fetch_pack_args *args,
Jonathan Tan13390782019-03-26 12:31:21 -07001421 struct packet_reader *reader,
1422 struct oid_array *shallows,
1423 struct shallow_info *si)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001424{
Jonathan Tan13390782019-03-26 12:31:21 -07001425 int unshallow_received = 0;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001426
Brandon Williamsf7e20502018-03-15 10:31:29 -07001427 process_section_header(reader, "shallow-info", 0);
1428 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1429 const char *arg;
1430 struct object_id oid;
1431
1432 if (skip_prefix(reader->line, "shallow ", &arg)) {
1433 if (get_oid_hex(arg, &oid))
1434 die(_("invalid shallow line: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001435 oid_array_append(shallows, &oid);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001436 continue;
1437 }
1438 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1439 if (get_oid_hex(arg, &oid))
1440 die(_("invalid unshallow line: %s"), reader->line);
Jeff Kingd0229ab2019-06-20 03:41:14 -04001441 if (!lookup_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001442 die(_("object not found: %s"), reader->line);
1443 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -07001444 if (!parse_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001445 die(_("error in object: %s"), reader->line);
1446 if (unregister_shallow(&oid))
1447 die(_("no shallow found: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001448 unshallow_received = 1;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001449 continue;
1450 }
1451 die(_("expected shallow/unshallow, got %s"), reader->line);
1452 }
1453
1454 if (reader->status != PACKET_READ_FLUSH &&
1455 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001456 die(_("error processing shallow info: %d"), reader->status);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001457
Jonathan Tan13390782019-03-26 12:31:21 -07001458 if (args->deepen || unshallow_received) {
1459 /*
1460 * Treat these as shallow lines caused by our depth settings.
1461 * In v0, these lines cannot cause refs to be rejected; do the
1462 * same.
1463 */
1464 int i;
1465
1466 for (i = 0; i < shallows->nr; i++)
1467 register_shallow(the_repository, &shallows->oid[i]);
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001468 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1469 NULL);
1470 args->deepen = 1;
Jonathan Tan13390782019-03-26 12:31:21 -07001471 } else if (shallows->nr) {
1472 /*
1473 * Treat these as shallow lines caused by the remote being
1474 * shallow. In v0, remote refs that reach these objects are
1475 * rejected (unless --update-shallow is set); do the same.
1476 */
1477 prepare_shallow_info(si, shallows);
Li Linchao4fe788b2021-04-01 10:46:59 +00001478 if (si->nr_ours || si->nr_theirs) {
1479 if (args->reject_shallow_remote)
1480 die(_("source repository is shallow, reject to clone."));
Jonathan Tan13390782019-03-26 12:31:21 -07001481 alternate_shallow_file =
1482 setup_temporary_shallow(si->shallow);
Li Linchao4fe788b2021-04-01 10:46:59 +00001483 } else
Jonathan Tan13390782019-03-26 12:31:21 -07001484 alternate_shallow_file = NULL;
brian m. carlson380ebab2019-02-06 23:59:37 +00001485 } else {
1486 alternate_shallow_file = NULL;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001487 }
Brandon Williamsf7e20502018-03-15 10:31:29 -07001488}
1489
Jonathan Tanb7643002019-03-27 14:11:10 -07001490static int cmp_name_ref(const void *name, const void *ref)
1491{
1492 return strcmp(name, (*(struct ref **)ref)->name);
1493}
1494
Jonathan Tane2842b32018-08-01 13:13:20 -07001495static void receive_wanted_refs(struct packet_reader *reader,
1496 struct ref **sought, int nr_sought)
Brandon Williams73302052018-06-27 15:30:23 -07001497{
1498 process_section_header(reader, "wanted-refs", 0);
1499 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1500 struct object_id oid;
1501 const char *end;
Jonathan Tanb7643002019-03-27 14:11:10 -07001502 struct ref **found;
Brandon Williams73302052018-06-27 15:30:23 -07001503
1504 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001505 die(_("expected wanted-ref, got '%s'"), reader->line);
Brandon Williams73302052018-06-27 15:30:23 -07001506
Jonathan Tanb7643002019-03-27 14:11:10 -07001507 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1508 cmp_name_ref);
1509 if (!found)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001510 die(_("unexpected wanted-ref: '%s'"), reader->line);
Jonathan Tanb7643002019-03-27 14:11:10 -07001511 oidcpy(&(*found)->old_oid, &oid);
Brandon Williams73302052018-06-27 15:30:23 -07001512 }
1513
1514 if (reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001515 die(_("error processing wanted refs: %d"), reader->status);
Brandon Williams73302052018-06-27 15:30:23 -07001516}
1517
Jonathan Tandd4b7322020-06-10 13:57:23 -07001518static void receive_packfile_uris(struct packet_reader *reader,
1519 struct string_list *uris)
1520{
1521 process_section_header(reader, "packfile-uris", 0);
1522 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1523 if (reader->pktlen < the_hash_algo->hexsz ||
1524 reader->line[the_hash_algo->hexsz] != ' ')
1525 die("expected '<hash> <uri>', got: %s\n", reader->line);
1526
1527 string_list_append(uris, reader->line);
1528 }
1529 if (reader->status != PACKET_READ_DELIM)
1530 die("expected DELIM");
1531}
1532
Brandon Williams685fbd32018-03-15 10:31:28 -07001533enum fetch_state {
1534 FETCH_CHECK_LOCAL = 0,
1535 FETCH_SEND_REQUEST,
1536 FETCH_PROCESS_ACKS,
1537 FETCH_GET_PACK,
1538 FETCH_DONE,
1539};
1540
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001541static void do_check_stateless_delimiter(int stateless_rpc,
Denton Liub0df0c12020-05-19 06:54:00 -04001542 struct packet_reader *reader)
1543{
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001544 check_stateless_delimiter(stateless_rpc, reader,
Denton Liub0df0c12020-05-19 06:54:00 -04001545 _("git fetch-pack: expected response end packet"));
1546}
1547
Brandon Williams685fbd32018-03-15 10:31:28 -07001548static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1549 int fd[2],
1550 const struct ref *orig_ref,
1551 struct ref **sought, int nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001552 struct oid_array *shallows,
1553 struct shallow_info *si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001554 struct string_list *pack_lockfiles)
Brandon Williams685fbd32018-03-15 10:31:28 -07001555{
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001556 struct repository *r = the_repository;
Brandon Williams685fbd32018-03-15 10:31:28 -07001557 struct ref *ref = copy_ref_list(orig_ref);
1558 enum fetch_state state = FETCH_CHECK_LOCAL;
1559 struct oidset common = OIDSET_INIT;
1560 struct packet_reader reader;
Josh Steadmon5fc31182019-10-02 16:49:28 -07001561 int in_vain = 0, negotiation_started = 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001562 int haves_to_send = INITIAL_FLUSH;
Jonathan Tan603960b2019-11-12 16:34:20 -08001563 struct fetch_negotiator negotiator_alloc;
1564 struct fetch_negotiator *negotiator;
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001565 int seen_ack = 0;
Jonathan Tan81025702021-04-08 18:09:59 -07001566 struct object_id common_oid;
1567 int received_ready = 0;
Jonathan Tandd4b7322020-06-10 13:57:23 -07001568 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1569 int i;
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001570 struct strvec index_pack_args = STRVEC_INIT;
Jonathan Tan603960b2019-11-12 16:34:20 -08001571
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001572 negotiator = &negotiator_alloc;
1573 fetch_negotiator_init(r, negotiator);
Jonathan Tan603960b2019-11-12 16:34:20 -08001574
Brandon Williams685fbd32018-03-15 10:31:28 -07001575 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -08001576 PACKET_READ_CHOMP_NEWLINE |
1577 PACKET_READ_DIE_ON_ERR_PACKET);
Jonathan Tan07c3c2a2019-01-16 11:28:15 -08001578 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1579 server_supports_feature("fetch", "sideband-all", 0)) {
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001580 reader.use_sideband = 1;
1581 reader.me = "fetch-pack";
1582 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001583
1584 while (state != FETCH_DONE) {
1585 switch (state) {
1586 case FETCH_CHECK_LOCAL:
1587 sort_ref_list(&ref, ref_compare_name);
1588 QSORT(sought, nr_sought, cmp_ref_by_name);
1589
1590 /* v2 supports these by default */
1591 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1592 use_sideband = 2;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001593 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1594 args->deepen = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001595
Brandon Williams685fbd32018-03-15 10:31:28 -07001596 /* Filter 'ref' by 'sought' and those that aren't local */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001597 mark_complete_and_common_ref(negotiator, args, &ref);
1598 filter_refs(args, &ref, sought, nr_sought);
1599 if (everything_local(args, &ref))
1600 state = FETCH_DONE;
1601 else
Jonathan Tan12f19a92018-10-03 16:04:52 -07001602 state = FETCH_SEND_REQUEST;
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001603
1604 mark_tips(negotiator, args->negotiation_tips);
1605 for_each_cached_alternate(negotiator,
1606 insert_one_alternate_object);
Brandon Williams685fbd32018-03-15 10:31:28 -07001607 break;
1608 case FETCH_SEND_REQUEST:
Josh Steadmon5fc31182019-10-02 16:49:28 -07001609 if (!negotiation_started) {
1610 negotiation_started = 1;
1611 trace2_region_enter("fetch-pack",
1612 "negotiation_v2",
1613 the_repository);
1614 }
Jonathan Tan603960b2019-11-12 16:34:20 -08001615 if (send_fetch_request(negotiator, fd[1], args, ref,
Jonathan Tanec062832018-06-14 15:54:28 -07001616 &common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001617 &haves_to_send, &in_vain,
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001618 reader.use_sideband,
1619 seen_ack))
Brandon Williams685fbd32018-03-15 10:31:28 -07001620 state = FETCH_GET_PACK;
1621 else
1622 state = FETCH_PROCESS_ACKS;
1623 break;
1624 case FETCH_PROCESS_ACKS:
1625 /* Process ACKs/NAKs */
Jonathan Tan81025702021-04-08 18:09:59 -07001626 process_section_header(&reader, "acknowledgments", 0);
1627 while (process_ack(negotiator, &reader, &common_oid,
1628 &received_ready)) {
1629 in_vain = 0;
1630 seen_ack = 1;
1631 oidset_insert(&common, &common_oid);
1632 }
1633 if (received_ready) {
Denton Liub0df0c12020-05-19 06:54:00 -04001634 /*
1635 * Don't check for response delimiter; get_pack() will
1636 * read the rest of this response.
1637 */
Brandon Williams685fbd32018-03-15 10:31:28 -07001638 state = FETCH_GET_PACK;
Jonathan Tan81025702021-04-08 18:09:59 -07001639 } else {
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001640 do_check_stateless_delimiter(args->stateless_rpc, &reader);
Brandon Williams685fbd32018-03-15 10:31:28 -07001641 state = FETCH_SEND_REQUEST;
Brandon Williams685fbd32018-03-15 10:31:28 -07001642 }
1643 break;
1644 case FETCH_GET_PACK:
Josh Steadmon5fc31182019-10-02 16:49:28 -07001645 trace2_region_leave("fetch-pack",
1646 "negotiation_v2",
1647 the_repository);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001648 /* Check for shallow-info section */
1649 if (process_section_header(&reader, "shallow-info", 1))
Jonathan Tan13390782019-03-26 12:31:21 -07001650 receive_shallow_info(args, &reader, shallows, si);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001651
Brandon Williams73302052018-06-27 15:30:23 -07001652 if (process_section_header(&reader, "wanted-refs", 1))
Jonathan Tane2842b32018-08-01 13:13:20 -07001653 receive_wanted_refs(&reader, sought, nr_sought);
Brandon Williams73302052018-06-27 15:30:23 -07001654
Jonathan Tandd4b7322020-06-10 13:57:23 -07001655 /* get the pack(s) */
1656 if (process_section_header(&reader, "packfile-uris", 1))
1657 receive_packfile_uris(&reader, &packfile_uris);
Brandon Williams685fbd32018-03-15 10:31:28 -07001658 process_section_header(&reader, "packfile", 0);
Jeff Kingae1a7ee2021-05-19 12:11:05 -04001659
1660 /*
1661 * this is the final request we'll make of the server;
1662 * do a half-duplex shutdown to indicate that they can
1663 * hang up as soon as the pack is sent.
1664 */
1665 close(fd[1]);
1666 fd[1] = -1;
1667
Jonathan Tandd4b7322020-06-10 13:57:23 -07001668 if (get_pack(args, fd, pack_lockfiles,
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001669 packfile_uris.nr ? &index_pack_args : NULL,
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001670 sought, nr_sought, &fsck_options.gitmodules_found))
Brandon Williams685fbd32018-03-15 10:31:28 -07001671 die(_("git fetch-pack: fetch failed."));
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001672 do_check_stateless_delimiter(args->stateless_rpc, &reader);
Brandon Williams685fbd32018-03-15 10:31:28 -07001673
1674 state = FETCH_DONE;
1675 break;
1676 case FETCH_DONE:
1677 continue;
1678 }
1679 }
1680
Jonathan Tandd4b7322020-06-10 13:57:23 -07001681 for (i = 0; i < packfile_uris.nr; i++) {
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001682 int j;
Jonathan Tandd4b7322020-06-10 13:57:23 -07001683 struct child_process cmd = CHILD_PROCESS_INIT;
1684 char packname[GIT_MAX_HEXSZ + 1];
1685 const char *uri = packfile_uris.items[i].string +
1686 the_hash_algo->hexsz + 1;
1687
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001688 strvec_push(&cmd.args, "http-fetch");
1689 strvec_pushf(&cmd.args, "--packfile=%.*s",
Jeff Kingf6d89422020-07-28 16:26:31 -04001690 (int) the_hash_algo->hexsz,
1691 packfile_uris.items[i].string);
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001692 for (j = 0; j < index_pack_args.nr; j++)
1693 strvec_pushf(&cmd.args, "--index-pack-arg=%s",
1694 index_pack_args.v[j]);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001695 strvec_push(&cmd.args, uri);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001696 cmd.git_cmd = 1;
1697 cmd.no_stdin = 1;
1698 cmd.out = -1;
1699 if (start_command(&cmd))
1700 die("fetch-pack: unable to spawn http-fetch");
1701
1702 if (read_in_full(cmd.out, packname, 5) < 0 ||
1703 memcmp(packname, "keep\t", 5))
1704 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1705
1706 if (read_in_full(cmd.out, packname,
1707 the_hash_algo->hexsz + 1) < 0 ||
1708 packname[the_hash_algo->hexsz] != '\n')
1709 die("fetch-pack: expected hash then LF at end of http-fetch output");
1710
1711 packname[the_hash_algo->hexsz] = '\0';
1712
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001713 parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found);
Jonathan Tan5476e1e2021-02-22 11:20:09 -08001714
Jonathan Tandd4b7322020-06-10 13:57:23 -07001715 close(cmd.out);
1716
1717 if (finish_command(&cmd))
1718 die("fetch-pack: unable to finish http-fetch");
1719
1720 if (memcmp(packfile_uris.items[i].string, packname,
1721 the_hash_algo->hexsz))
1722 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1723 uri, (int) the_hash_algo->hexsz,
1724 packfile_uris.items[i].string);
1725
1726 string_list_append_nodup(pack_lockfiles,
1727 xstrfmt("%s/pack/pack-%s.keep",
1728 get_object_directory(),
1729 packname));
1730 }
1731 string_list_clear(&packfile_uris, 0);
Jonathan Tanb664e9f2021-02-22 11:20:08 -08001732 strvec_clear(&index_pack_args);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001733
Ævar Arnfjörð Bjarmason3745e262021-03-28 15:15:51 +02001734 if (fsck_finish(&fsck_options))
1735 die("fsck failed");
Brandon Williams685fbd32018-03-15 10:31:28 -07001736
Jonathan Tan603960b2019-11-12 16:34:20 -08001737 if (negotiator)
1738 negotiator->release(negotiator);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001739
Brandon Williams685fbd32018-03-15 10:31:28 -07001740 oidset_clear(&common);
1741 return ref;
1742}
1743
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001744static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1745{
1746 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1747 const char *path;
1748
1749 if (git_config_pathname(&path, var, value))
1750 return 1;
1751 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1752 fsck_msg_types.len ? ',' : '=', path);
1753 free((char *)path);
1754 return 0;
1755 }
1756
1757 if (skip_prefix(var, "fetch.fsck.", &var)) {
1758 if (is_valid_msg_type(var, value))
1759 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1760 fsck_msg_types.len ? ',' : '=', var, value);
1761 else
1762 warning("Skipping unknown msg id '%s'", var);
1763 return 0;
1764 }
1765
1766 return git_default_config(var, value, cb);
1767}
1768
Tanay Abhraf44af512014-08-07 09:21:20 -07001769static void fetch_pack_config(void)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001770{
Tanay Abhraf44af512014-08-07 09:21:20 -07001771 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1772 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1773 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1774 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1775 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
Josh Steadmon1e905bb2020-11-11 15:29:31 -08001776 git_config_get_bool("transfer.advertisesid", &advertise_sid);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001777 if (!uri_protocols.nr) {
1778 char *str;
1779
1780 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1781 string_list_split(&uri_protocols, str, ',', -1);
1782 free(str);
1783 }
1784 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001785
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001786 git_config(fetch_pack_config_cb, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001787}
1788
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001789static void fetch_pack_setup(void)
1790{
1791 static int did_setup;
1792 if (did_setup)
1793 return;
Tanay Abhraf44af512014-08-07 09:21:20 -07001794 fetch_pack_config();
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001795 if (0 <= transfer_unpack_limit)
1796 unpack_limit = transfer_unpack_limit;
1797 else if (0 <= fetch_unpack_limit)
1798 unpack_limit = fetch_unpack_limit;
1799 did_setup = 1;
1800}
1801
Junio C Hamanof2db8542013-01-29 14:02:15 -08001802static int remove_duplicates_in_refs(struct ref **ref, int nr)
1803{
1804 struct string_list names = STRING_LIST_INIT_NODUP;
1805 int src, dst;
1806
1807 for (src = dst = 0; src < nr; src++) {
1808 struct string_list_item *item;
1809 item = string_list_insert(&names, ref[src]->name);
1810 if (item->util)
1811 continue; /* already have it */
1812 item->util = ref[src];
1813 if (src != dst)
1814 ref[dst] = ref[src];
1815 dst++;
1816 }
1817 for (src = dst; src < nr; src++)
1818 ref[src] = NULL;
1819 string_list_clear(&names, 0);
1820 return dst;
1821}
1822
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001823static void update_shallow(struct fetch_pack_args *args,
Jonathan Tane2842b32018-08-01 13:13:20 -07001824 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001825 struct shallow_info *si)
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001826{
brian m. carlson910650d2017-03-31 01:40:00 +00001827 struct oid_array ref = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001828 int *status;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001829 int i;
1830
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001831 if (args->deepen && alternate_shallow_file) {
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001832 if (*alternate_shallow_file == '\0') { /* --unshallow */
Stefan Beller102de882018-05-17 15:51:51 -07001833 unlink_or_warn(git_path_shallow(the_repository));
Taylor Blau37b9dca2020-04-22 18:25:45 -06001834 rollback_shallow_file(the_repository, &shallow_lock);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001835 } else
Taylor Blau37b9dca2020-04-22 18:25:45 -06001836 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001837 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001838 return;
1839 }
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001840
1841 if (!si->shallow || !si->shallow->nr)
1842 return;
1843
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001844 if (args->cloning) {
1845 /*
1846 * remote is shallow, but this is a clone, there are
1847 * no objects in repo to worry about. Accept any
1848 * shallow points that exist in the pack (iow in repo
1849 * after get_pack() and reprepare_packed_git())
1850 */
brian m. carlson910650d2017-03-31 01:40:00 +00001851 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001852 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001853 for (i = 0; i < si->shallow->nr; i++)
brian m. carlsonee3051b2017-03-26 16:01:37 +00001854 if (has_object_file(&oid[i]))
brian m. carlson910650d2017-03-31 01:40:00 +00001855 oid_array_append(&extra, &oid[i]);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001856 if (extra.nr) {
1857 setup_alternate_shallow(&shallow_lock,
1858 &alternate_shallow_file,
1859 &extra);
Taylor Blau37b9dca2020-04-22 18:25:45 -06001860 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001861 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001862 }
brian m. carlson910650d2017-03-31 01:40:00 +00001863 oid_array_clear(&extra);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001864 return;
1865 }
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001866
1867 if (!si->nr_ours && !si->nr_theirs)
1868 return;
1869
1870 remove_nonexistent_theirs_shallow(si);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001871 if (!si->nr_ours && !si->nr_theirs)
1872 return;
Jonathan Tane2842b32018-08-01 13:13:20 -07001873 for (i = 0; i < nr_sought; i++)
1874 oid_array_append(&ref, &sought[i]->old_oid);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001875 si->ref = &ref;
1876
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001877 if (args->update_shallow) {
1878 /*
1879 * remote is also shallow, .git/shallow may be updated
1880 * so all refs can be accepted. Make sure we only add
1881 * shallow roots that are actually reachable from new
1882 * refs.
1883 */
brian m. carlson910650d2017-03-31 01:40:00 +00001884 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001885 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001886 assign_shallow_commits_to_refs(si, NULL, NULL);
1887 if (!si->nr_ours && !si->nr_theirs) {
brian m. carlson910650d2017-03-31 01:40:00 +00001888 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001889 return;
1890 }
1891 for (i = 0; i < si->nr_ours; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001892 oid_array_append(&extra, &oid[si->ours[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001893 for (i = 0; i < si->nr_theirs; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001894 oid_array_append(&extra, &oid[si->theirs[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001895 setup_alternate_shallow(&shallow_lock,
1896 &alternate_shallow_file,
1897 &extra);
Taylor Blau37b9dca2020-04-22 18:25:45 -06001898 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson910650d2017-03-31 01:40:00 +00001899 oid_array_clear(&extra);
1900 oid_array_clear(&ref);
brian m. carlson23311f32019-02-04 00:06:50 +00001901 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001902 return;
1903 }
1904
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001905 /*
1906 * remote is also shallow, check what ref is safe to update
1907 * without updating .git/shallow
1908 */
René Scharfeca56dad2021-03-13 17:17:22 +01001909 CALLOC_ARRAY(status, nr_sought);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001910 assign_shallow_commits_to_refs(si, NULL, status);
1911 if (si->nr_ours || si->nr_theirs) {
Jonathan Tane2842b32018-08-01 13:13:20 -07001912 for (i = 0; i < nr_sought; i++)
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001913 if (status[i])
Jonathan Tane2842b32018-08-01 13:13:20 -07001914 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001915 }
1916 free(status);
brian m. carlson910650d2017-03-31 01:40:00 +00001917 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001918}
1919
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +02001920static const struct object_id *iterate_ref_map(void *cb_data)
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001921{
1922 struct ref **rm = cb_data;
1923 struct ref *ref = *rm;
1924
1925 if (!ref)
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +02001926 return NULL;
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001927 *rm = ref->next;
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +02001928 return &ref->old_oid;
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001929}
1930
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001931struct ref *fetch_pack(struct fetch_pack_args *args,
Jeff King0f804b02019-03-20 04:16:14 -04001932 int fd[],
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001933 const struct ref *ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -08001934 struct ref **sought, int nr_sought,
brian m. carlson910650d2017-03-31 01:40:00 +00001935 struct oid_array *shallow,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001936 struct string_list *pack_lockfiles,
Brandon Williams685fbd32018-03-15 10:31:28 -07001937 enum protocol_version version)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001938{
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001939 struct ref *ref_cpy;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001940 struct shallow_info si;
Jonathan Tan13390782019-03-26 12:31:21 -07001941 struct oid_array shallows_scratch = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001942
1943 fetch_pack_setup();
Junio C Hamanof2db8542013-01-29 14:02:15 -08001944 if (nr_sought)
1945 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001946
Jonathan Tan01775652018-09-27 12:24:05 -07001947 if (version != protocol_v2 && !ref) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001948 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001949 die(_("no matching remote head"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001950 }
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001951 if (version == protocol_v2) {
1952 if (shallow->nr)
1953 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1954 memset(&si, 0, sizeof(si));
Brandon Williams685fbd32018-03-15 10:31:28 -07001955 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001956 &shallows_scratch, &si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001957 pack_lockfiles);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001958 } else {
1959 prepare_shallow_info(&si, shallow);
Brandon Williams685fbd32018-03-15 10:31:28 -07001960 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001961 &si, pack_lockfiles);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001962 }
Stefan Bellera49d2832018-03-23 18:45:21 +01001963 reprepare_packed_git(the_repository);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001964
1965 if (!args->cloning && args->deepen) {
1966 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1967 struct ref *iterator = ref_cpy;
1968 opt.shallow_file = alternate_shallow_file;
1969 if (args->deepen)
1970 opt.is_deepening_fetch = 1;
1971 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1972 error(_("remote did not send all necessary objects"));
1973 free_refs(ref_cpy);
1974 ref_cpy = NULL;
Taylor Blau37b9dca2020-04-22 18:25:45 -06001975 rollback_shallow_file(the_repository, &shallow_lock);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001976 goto cleanup;
1977 }
1978 args->connectivity_checked = 1;
1979 }
1980
Jonathan Tane2842b32018-08-01 13:13:20 -07001981 update_shallow(args, sought, nr_sought, &si);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001982cleanup:
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001983 clear_shallow_info(&si);
Jonathan Tan13390782019-03-26 12:31:21 -07001984 oid_array_clear(&shallows_scratch);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001985 return ref_cpy;
1986}
Matt McCutchene860d962017-02-22 11:01:22 -05001987
Jonathan Tan9c1e6572021-05-04 14:16:01 -07001988static int add_to_object_array(const struct object_id *oid, void *data)
1989{
1990 struct object_array *a = data;
1991
1992 add_object_array(lookup_object(the_repository, oid), "", a);
1993 return 0;
1994}
1995
1996static void clear_common_flag(struct oidset *s)
1997{
1998 struct oidset_iter iter;
1999 const struct object_id *oid;
2000 oidset_iter_init(s, &iter);
2001
2002 while ((oid = oidset_iter_next(&iter))) {
2003 struct object *obj = lookup_object(the_repository, oid);
2004 obj->flags &= ~COMMON;
2005 }
2006}
2007
2008void negotiate_using_fetch(const struct oid_array *negotiation_tips,
2009 const struct string_list *server_options,
2010 int stateless_rpc,
2011 int fd[],
2012 struct oidset *acked_commits)
2013{
2014 struct fetch_negotiator negotiator;
2015 struct packet_reader reader;
2016 struct object_array nt_object_array = OBJECT_ARRAY_INIT;
2017 struct strbuf req_buf = STRBUF_INIT;
2018 int haves_to_send = INITIAL_FLUSH;
2019 int in_vain = 0;
2020 int seen_ack = 0;
2021 int last_iteration = 0;
2022 timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
2023
2024 fetch_negotiator_init(the_repository, &negotiator);
2025 mark_tips(&negotiator, negotiation_tips);
2026
2027 packet_reader_init(&reader, fd[0], NULL, 0,
2028 PACKET_READ_CHOMP_NEWLINE |
2029 PACKET_READ_DIE_ON_ERR_PACKET);
2030
2031 oid_array_for_each((struct oid_array *) negotiation_tips,
2032 add_to_object_array,
2033 &nt_object_array);
2034
2035 while (!last_iteration) {
2036 int haves_added;
2037 struct object_id common_oid;
2038 int received_ready = 0;
2039
2040 strbuf_reset(&req_buf);
2041 write_fetch_command_and_capabilities(&req_buf, server_options);
2042
2043 packet_buf_write(&req_buf, "wait-for-done");
2044
2045 haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
2046 in_vain += haves_added;
2047 if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
2048 last_iteration = 1;
2049
2050 /* Send request */
2051 packet_buf_flush(&req_buf);
2052 if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
2053 die_errno(_("unable to write request to remote"));
2054
2055 /* Process ACKs/NAKs */
2056 process_section_header(&reader, "acknowledgments", 0);
2057 while (process_ack(&negotiator, &reader, &common_oid,
2058 &received_ready)) {
2059 struct commit *commit = lookup_commit(the_repository,
2060 &common_oid);
2061 if (commit) {
2062 timestamp_t generation;
2063
2064 parse_commit_or_die(commit);
2065 commit->object.flags |= COMMON;
2066 generation = commit_graph_generation(commit);
2067 if (generation < min_generation)
2068 min_generation = generation;
2069 }
2070 in_vain = 0;
2071 seen_ack = 1;
2072 oidset_insert(acked_commits, &common_oid);
2073 }
2074 if (received_ready)
2075 die(_("unexpected 'ready' from remote"));
2076 else
2077 do_check_stateless_delimiter(stateless_rpc, &reader);
2078 if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
2079 REACH_SCRATCH, 0,
2080 min_generation))
2081 last_iteration = 1;
2082 }
2083 clear_common_flag(acked_commits);
2084 strbuf_release(&req_buf);
2085}
2086
Matt McCutchene860d962017-02-22 11:01:22 -05002087int report_unmatched_refs(struct ref **sought, int nr_sought)
2088{
2089 int i, ret = 0;
2090
2091 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -05002092 if (!sought[i])
Matt McCutchene860d962017-02-22 11:01:22 -05002093 continue;
Matt McCutchend56583d2017-02-22 11:05:57 -05002094 switch (sought[i]->match_status) {
2095 case REF_MATCHED:
2096 continue;
2097 case REF_NOT_MATCHED:
2098 error(_("no such remote ref %s"), sought[i]->name);
2099 break;
2100 case REF_UNADVERTISED_NOT_ALLOWED:
2101 error(_("Server does not allow request for unadvertised object %s"),
2102 sought[i]->name);
2103 break;
2104 }
Matt McCutchene860d962017-02-22 11:01:22 -05002105 ret = 1;
2106 }
2107 return ret;
2108}