blob: b10c4323155b77f2ef271564cef862147a880596 [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"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070026
27static int transfer_unpack_limit = -1;
28static int fetch_unpack_limit = -1;
29static int unpack_limit = 100;
30static int prefer_ofs_delta = 1;
31static int no_done;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070032static int deepen_since_ok;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070033static int deepen_not_ok;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070034static int fetch_fsck_objects = -1;
35static int transfer_fsck_objects = -1;
36static int agent_supported;
Jeff Hostetler640d8b72017-12-08 15:58:40 +000037static int server_supports_filtering;
Taylor Blaucac4b8e2020-04-30 13:48:57 -060038static struct shallow_lock shallow_lock;
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +070039static const char *alternate_shallow_file;
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +000040static struct strbuf fsck_msg_types = STRBUF_INIT;
Jonathan Tandd4b7322020-06-10 13:57:23 -070041static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070042
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070043/* Remember to update object flag allocation in object.h */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070044#define COMPLETE (1U << 0)
Jonathan Tanec062832018-06-14 15:54:28 -070045#define ALTERNATE (1U << 1)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070046
47/*
48 * After sending this many "have"s if we do not get any new ACK , we
49 * give up traversing our history.
50 */
51#define MAX_IN_VAIN 256
52
Jonathan Tand30fe892018-06-14 15:54:26 -070053static int multi_ack, use_sideband;
Fredrik Medley7199c092015-05-21 22:23:38 +020054/* Allow specifying sha1 if it is a ref tip. */
55#define ALLOW_TIP_SHA1 01
Fredrik Medley68ee6282015-05-21 22:23:39 +020056/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
57#define ALLOW_REACHABLE_SHA1 02
Fredrik Medley7199c092015-05-21 22:23:38 +020058static unsigned int allow_unadvertised_object_request;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070059
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +070060__attribute__((format (printf, 2, 3)))
61static inline void print_verbose(const struct fetch_pack_args *args,
62 const char *fmt, ...)
63{
64 va_list params;
65
66 if (!args->verbose)
67 return;
68
69 va_start(params, fmt);
70 vfprintf(stderr, fmt, params);
71 va_end(params);
72 fputc('\n', stderr);
73}
74
Jeff King41a078c2017-02-08 15:53:03 -050075struct alternate_object_cache {
76 struct object **items;
77 size_t nr, alloc;
78};
79
Jeff Kingbdf42762018-10-08 11:09:23 -070080static void cache_one_alternate(const struct object_id *oid,
Jeff King41a078c2017-02-08 15:53:03 -050081 void *vcache)
82{
83 struct alternate_object_cache *cache = vcache;
Stefan Beller109cd762018-06-28 18:21:51 -070084 struct object *obj = parse_object(the_repository, oid);
Jeff King41a078c2017-02-08 15:53:03 -050085
86 if (!obj || (obj->flags & ALTERNATE))
87 return;
88
89 obj->flags |= ALTERNATE;
90 ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc);
91 cache->items[cache->nr++] = obj;
92}
93
Jonathan Tanec062832018-06-14 15:54:28 -070094static void for_each_cached_alternate(struct fetch_negotiator *negotiator,
95 void (*cb)(struct fetch_negotiator *,
Jonathan Tand30fe892018-06-14 15:54:26 -070096 struct object *))
Jeff King41a078c2017-02-08 15:53:03 -050097{
98 static int initialized;
99 static struct alternate_object_cache cache;
100 size_t i;
101
102 if (!initialized) {
103 for_each_alternate_ref(cache_one_alternate, &cache);
104 initialized = 1;
105 }
106
107 for (i = 0; i < cache.nr; i++)
Jonathan Tanec062832018-06-14 15:54:28 -0700108 cb(negotiator, cache.items[i]);
Jeff King41a078c2017-02-08 15:53:03 -0500109}
110
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700111static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
112 int mark_tags_complete)
113{
114 enum object_type type;
115 struct object_info info = { .typep = &type };
116
117 while (1) {
118 if (oid_object_info_extended(the_repository, oid, &info,
119 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK))
120 return NULL;
121 if (type == OBJ_TAG) {
122 struct tag *tag = (struct tag *)
123 parse_object(the_repository, oid);
124
125 if (!tag->tagged)
126 return NULL;
127 if (mark_tags_complete)
128 tag->object.flags |= COMPLETE;
129 oid = &tag->tagged->oid;
130 } else {
131 break;
132 }
133 }
134 if (type == OBJ_COMMIT)
135 return (struct commit *) parse_object(the_repository, oid);
136 return NULL;
137}
138
Jonathan Tanec062832018-06-14 15:54:28 -0700139static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700140 const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700141{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700142 struct commit *c = deref_without_lazy_fetch(oid, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700143
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700144 if (c)
145 negotiator->add_tip(negotiator, c);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700146 return 0;
147}
148
Michael Haggertyb1b49c62015-05-25 18:39:18 +0000149static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
150 int flag, void *cb_data)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700151{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700152 return rev_list_insert_ref(cb_data, oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700153}
154
155enum ack_type {
156 NAK = 0,
157 ACK,
158 ACK_continue,
159 ACK_common,
160 ACK_ready
161};
162
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800163static void consume_shallow_list(struct fetch_pack_args *args,
164 struct packet_reader *reader)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700165{
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700166 if (args->stateless_rpc && args->deepen) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700167 /* If we sent a depth we will get back "duplicate"
168 * shallow and unshallow commands every time there
169 * is a block of have lines exchanged.
170 */
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800171 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
172 if (starts_with(reader->line, "shallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700173 continue;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800174 if (starts_with(reader->line, "unshallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700175 continue;
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700176 die(_("git fetch-pack: expected shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700177 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800178 if (reader->status != PACKET_READ_FLUSH)
179 die(_("git fetch-pack: expected a flush packet after shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700180 }
181}
182
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800183static enum ack_type get_ack(struct packet_reader *reader,
184 struct object_id *result_oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700185{
Jeff King74543a02013-02-20 15:02:57 -0500186 int len;
Jeff King82e56762014-06-18 15:56:03 -0400187 const char *arg;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700188
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800189 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
Jeff Kingbc9d4dc2018-02-08 13:47:49 -0500190 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800191 len = reader->pktlen;
192
193 if (!strcmp(reader->line, "NAK"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700194 return NAK;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800195 if (skip_prefix(reader->line, "ACK ", &arg)) {
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000196 const char *p;
197 if (!parse_oid_hex(arg, result_oid, &p)) {
198 len -= p - reader->line;
Jeff King82e56762014-06-18 15:56:03 -0400199 if (len < 1)
Jeff King030e9dd2013-02-20 15:00:28 -0500200 return ACK;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000201 if (strstr(p, "continue"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700202 return ACK_continue;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000203 if (strstr(p, "common"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700204 return ACK_common;
brian m. carlsonf6af19a2019-08-18 20:04:04 +0000205 if (strstr(p, "ready"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700206 return ACK_ready;
207 return ACK;
208 }
209 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800210 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700211}
212
213static void send_request(struct fetch_pack_args *args,
214 int fd, struct strbuf *buf)
215{
216 if (args->stateless_rpc) {
217 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
218 packet_flush(fd);
Jeff King37c80012019-03-04 23:11:39 -0500219 } else {
220 if (write_in_full(fd, buf->buf, buf->len) < 0)
221 die_errno(_("unable to write to remote"));
222 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700223}
224
Jonathan Tanec062832018-06-14 15:54:28 -0700225static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700226 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700227{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700228 rev_list_insert_ref(negotiator, &obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700229}
230
231#define INITIAL_FLUSH 16
232#define PIPESAFE_FLUSH 32
Jonathan Tanda470982016-07-18 15:21:38 -0700233#define LARGE_FLUSH 16384
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700234
Brandon Williams685fbd32018-03-15 10:31:28 -0700235static int next_flush(int stateless_rpc, int count)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700236{
Brandon Williams685fbd32018-03-15 10:31:28 -0700237 if (stateless_rpc) {
Jonathan Tanda470982016-07-18 15:21:38 -0700238 if (count < LARGE_FLUSH)
239 count <<= 1;
240 else
241 count = count * 11 / 10;
242 } else {
243 if (count < PIPESAFE_FLUSH)
244 count <<= 1;
245 else
246 count += PIPESAFE_FLUSH;
247 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700248 return count;
249}
250
Jonathan Tan3390e422018-07-02 15:39:44 -0700251static void mark_tips(struct fetch_negotiator *negotiator,
252 const struct oid_array *negotiation_tips)
253{
254 int i;
255
256 if (!negotiation_tips) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700257 for_each_rawref(rev_list_insert_ref_oid, negotiator);
Jonathan Tan3390e422018-07-02 15:39:44 -0700258 return;
259 }
260
261 for (i = 0; i < negotiation_tips->nr; i++)
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700262 rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]);
Jonathan Tan3390e422018-07-02 15:39:44 -0700263 return;
264}
265
Jonathan Tanec062832018-06-14 15:54:28 -0700266static int find_common(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700267 struct fetch_pack_args *args,
brian m. carlson1b283372017-05-01 02:28:54 +0000268 int fd[2], struct object_id *result_oid,
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700269 struct ref *refs)
270{
271 int fetching;
272 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
brian m. carlson1b283372017-05-01 02:28:54 +0000273 const struct object_id *oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700274 unsigned in_vain = 0;
275 int got_continue = 0;
276 int got_ready = 0;
277 struct strbuf req_buf = STRBUF_INIT;
278 size_t state_len = 0;
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800279 struct packet_reader reader;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700280
281 if (args->stateless_rpc && multi_ack == 1)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700282 die(_("--stateless-rpc requires multi_ack_detailed"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700283
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800284 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -0800285 PACKET_READ_CHOMP_NEWLINE |
286 PACKET_READ_DIE_ON_ERR_PACKET);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800287
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700288 mark_tips(negotiator, args->negotiation_tips);
289 for_each_cached_alternate(negotiator, insert_one_alternate_object);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700290
291 fetching = 0;
292 for ( ; refs ; refs = refs->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000293 struct object_id *remote = &refs->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700294 const char *remote_hex;
295 struct object *o;
296
297 /*
298 * If that object is complete (i.e. it is an ancestor of a
299 * local ref), we tell them we have it but do not have to
300 * tell them about its ancestors, which they already know
301 * about.
302 *
303 * We use lookup_object here because we are only
304 * interested in the case we *know* the object is
305 * reachable and we have already scanned it.
306 */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700307 if (((o = lookup_object(the_repository, remote)) != NULL) &&
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700308 (o->flags & COMPLETE)) {
309 continue;
310 }
311
brian m. carlson1b283372017-05-01 02:28:54 +0000312 remote_hex = oid_to_hex(remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700313 if (!fetching) {
314 struct strbuf c = STRBUF_INIT;
315 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
316 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
317 if (no_done) strbuf_addstr(&c, " no-done");
318 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
319 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700320 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700321 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
322 if (args->no_progress) strbuf_addstr(&c, " no-progress");
323 if (args->include_tag) strbuf_addstr(&c, " include-tag");
324 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700325 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700326 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700327 if (agent_supported) strbuf_addf(&c, " agent=%s",
328 git_user_agent_sanitized());
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000329 if (args->filter_options.choice)
330 strbuf_addstr(&c, " filter");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700331 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
332 strbuf_release(&c);
333 } else
334 packet_buf_write(&req_buf, "want %s\n", remote_hex);
335 fetching++;
336 }
337
338 if (!fetching) {
339 strbuf_release(&req_buf);
340 packet_flush(fd[1]);
341 return 1;
342 }
343
Stefan Bellerc8813482018-05-17 15:51:46 -0700344 if (is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700345 write_shallow_commits(&req_buf, 1, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700346 if (args->depth > 0)
347 packet_buf_write(&req_buf, "deepen %d", args->depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700348 if (args->deepen_since) {
Johannes Schindelindddbad72017-04-26 21:29:31 +0200349 timestamp_t max_age = approxidate(args->deepen_since);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200350 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700351 }
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700352 if (args->deepen_not) {
353 int i;
354 for (i = 0; i < args->deepen_not->nr; i++) {
355 struct string_list_item *s = args->deepen_not->items + i;
356 packet_buf_write(&req_buf, "deepen-not %s", s->string);
357 }
358 }
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800359 if (server_supports_filtering && args->filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700360 const char *spec =
361 expand_list_objects_filter_spec(&args->filter_options);
362 packet_buf_write(&req_buf, "filter %s", spec);
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800363 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700364 packet_buf_flush(&req_buf);
365 state_len = req_buf.len;
366
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700367 if (args->deepen) {
Jeff Kingae021d82014-06-18 15:47:50 -0400368 const char *arg;
brian m. carlson1b283372017-05-01 02:28:54 +0000369 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700370
371 send_request(args, fd[1], &req_buf);
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800372 while (packet_reader_read(&reader) == PACKET_READ_NORMAL) {
373 if (skip_prefix(reader.line, "shallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000374 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800375 die(_("invalid shallow line: %s"), reader.line);
Stefan Beller19143f12018-05-17 15:51:44 -0700376 register_shallow(the_repository, &oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700377 continue;
378 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800379 if (skip_prefix(reader.line, "unshallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000380 if (get_oid_hex(arg, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800381 die(_("invalid unshallow line: %s"), reader.line);
Jeff Kingd0229ab2019-06-20 03:41:14 -0400382 if (!lookup_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800383 die(_("object not found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700384 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -0700385 if (!parse_object(the_repository, &oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800386 die(_("error in object: %s"), reader.line);
brian m. carlsone92b8482017-05-06 22:10:06 +0000387 if (unregister_shallow(&oid))
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800388 die(_("no shallow found: %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700389 continue;
390 }
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800391 die(_("expected shallow/unshallow, got %s"), reader.line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700392 }
393 } else if (!args->stateless_rpc)
394 send_request(args, fd[1], &req_buf);
395
396 if (!args->stateless_rpc) {
397 /* If we aren't using the stateless-rpc interface
398 * we don't need to retain the headers.
399 */
400 strbuf_setlen(&req_buf, 0);
401 state_len = 0;
402 }
403
Josh Steadmon5fc31182019-10-02 16:49:28 -0700404 trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700405 flushes = 0;
406 retval = -1;
Jonathan Tanec062832018-06-14 15:54:28 -0700407 while ((oid = negotiator->next(negotiator))) {
brian m. carlson1b283372017-05-01 02:28:54 +0000408 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
409 print_verbose(args, "have %s", oid_to_hex(oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700410 in_vain++;
411 if (flush_at <= ++count) {
412 int ack;
413
414 packet_buf_flush(&req_buf);
415 send_request(args, fd[1], &req_buf);
416 strbuf_setlen(&req_buf, state_len);
417 flushes++;
Brandon Williams685fbd32018-03-15 10:31:28 -0700418 flush_at = next_flush(args->stateless_rpc, count);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700419
420 /*
421 * We keep one window "ahead" of the other side, and
422 * will wait for an ACK only on the next one
423 */
424 if (!args->stateless_rpc && count == INITIAL_FLUSH)
425 continue;
426
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800427 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700428 do {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800429 ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700430 if (ack)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700431 print_verbose(args, _("got %s %d %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000432 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700433 switch (ack) {
434 case ACK:
435 flushes = 0;
436 multi_ack = 0;
437 retval = 0;
438 goto done;
439 case ACK_common:
440 case ACK_ready:
441 case ACK_continue: {
442 struct commit *commit =
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700443 lookup_commit(the_repository,
444 result_oid);
Jonathan Tand093bc72018-06-14 15:54:27 -0700445 int was_common;
Junio C Hamano3a2a1dc2018-08-02 15:30:42 -0700446
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700447 if (!commit)
brian m. carlson1b283372017-05-01 02:28:54 +0000448 die(_("invalid commit %s"), oid_to_hex(result_oid));
Jonathan Tanec062832018-06-14 15:54:28 -0700449 was_common = negotiator->ack(negotiator, commit);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700450 if (args->stateless_rpc
451 && ack == ACK_common
Jonathan Tand093bc72018-06-14 15:54:27 -0700452 && !was_common) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700453 /* We need to replay the have for this object
454 * on the next RPC request so the peer knows
455 * it is in common with us.
456 */
brian m. carlson1b283372017-05-01 02:28:54 +0000457 const char *hex = oid_to_hex(result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700458 packet_buf_write(&req_buf, "have %s\n", hex);
459 state_len = req_buf.len;
Jonathan Tan06b3d382016-09-23 10:41:35 -0700460 /*
461 * Reset in_vain because an ack
462 * for this commit has not been
463 * seen.
464 */
465 in_vain = 0;
466 } else if (!args->stateless_rpc
467 || ack != ACK_common)
468 in_vain = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700469 retval = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700470 got_continue = 1;
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700471 if (ack == ACK_ready)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700472 got_ready = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700473 break;
474 }
475 }
476 } while (ack);
477 flushes--;
478 if (got_continue && MAX_IN_VAIN < in_vain) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700479 print_verbose(args, _("giving up"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700480 break; /* give up */
481 }
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700482 if (got_ready)
483 break;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700484 }
485 }
486done:
Josh Steadmon5fc31182019-10-02 16:49:28 -0700487 trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700488 if (!got_ready || !no_done) {
489 packet_buf_write(&req_buf, "done\n");
490 send_request(args, fd[1], &req_buf);
491 }
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700492 print_verbose(args, _("done"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700493 if (retval != 0) {
494 multi_ack = 0;
495 flushes++;
496 }
497 strbuf_release(&req_buf);
498
Nguyễn Thái Ngọc Duyff62eca2014-02-06 22:10:39 +0700499 if (!got_ready || !no_done)
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800500 consume_shallow_list(args, &reader);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700501 while (flushes || multi_ack) {
Masaya Suzuki01f9ec62018-12-29 13:19:14 -0800502 int ack = get_ack(&reader, result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700503 if (ack) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700504 print_verbose(args, _("got %s (%d) %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000505 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700506 if (ack == ACK)
507 return 0;
508 multi_ack = 1;
509 continue;
510 }
511 flushes--;
512 }
513 /* it is no error to fetch into a completely empty repo */
514 return count ? retval : 0;
515}
516
517static struct commit_list *complete;
518
brian m. carlson1b283372017-05-01 02:28:54 +0000519static int mark_complete(const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700520{
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700521 struct commit *commit = deref_without_lazy_fetch(oid, 1);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700522
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700523 if (commit && !(commit->object.flags & COMPLETE)) {
524 commit->object.flags |= COMPLETE;
525 commit_list_insert(commit, &complete);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700526 }
527 return 0;
528}
529
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000530static int mark_complete_oid(const char *refname, const struct object_id *oid,
531 int flag, void *cb_data)
532{
brian m. carlson1b283372017-05-01 02:28:54 +0000533 return mark_complete(oid);
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000534}
535
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700536static void mark_recent_complete_commits(struct fetch_pack_args *args,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200537 timestamp_t cutoff)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700538{
539 while (complete && cutoff <= complete->item->date) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700540 print_verbose(args, _("Marking %s as complete"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700541 oid_to_hex(&complete->item->object.oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700542 pop_most_recent_commit(&complete, COMPLETE);
543 }
544}
545
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700546static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
547{
548 for (; refs; refs = refs->next)
549 oidset_insert(oids, &refs->old_oid);
550}
551
René Scharfebf732822018-10-04 17:09:06 +0200552static int is_unmatched_ref(const struct ref *ref)
553{
554 struct object_id oid;
555 const char *p;
556 return ref->match_status == REF_NOT_MATCHED &&
557 !parse_oid_hex(ref->name, &oid, &p) &&
558 *p == '\0' &&
559 oideq(&oid, &ref->old_oid);
560}
561
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700562static void filter_refs(struct fetch_pack_args *args,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800563 struct ref **refs,
564 struct ref **sought, int nr_sought)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700565{
566 struct ref *newlist = NULL;
567 struct ref **newtail = &newlist;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700568 struct ref *unmatched = NULL;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700569 struct ref *ref, *next;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700570 struct oidset tip_oids = OIDSET_INIT;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800571 int i;
René Scharfe22a16462018-10-04 17:09:39 +0200572 int strict = !(allow_unadvertised_object_request &
573 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700574
Junio C Hamanof2db8542013-01-29 14:02:15 -0800575 i = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700576 for (ref = *refs; ref; ref = next) {
577 int keep = 0;
578 next = ref->next;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800579
René Scharfe50e19a82014-06-06 19:24:48 +0200580 if (starts_with(ref->name, "refs/") &&
Jeff King34066f02019-04-13 01:57:37 -0400581 check_refname_format(ref->name, 0)) {
582 /*
583 * trash or a peeled value; do not even add it to
584 * unmatched list
585 */
586 free_one_ref(ref);
587 continue;
588 } else {
Junio C Hamanof2db8542013-01-29 14:02:15 -0800589 while (i < nr_sought) {
590 int cmp = strcmp(ref->name, sought[i]->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700591 if (cmp < 0)
592 break; /* definitely do not have it */
593 else if (cmp == 0) {
594 keep = 1; /* definitely have it */
Matt McCutchend56583d2017-02-22 11:05:57 -0500595 sought[i]->match_status = REF_MATCHED;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700596 }
Junio C Hamanof2db8542013-01-29 14:02:15 -0800597 i++;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700598 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700599
Jeff Kinge9502c02018-06-11 01:53:57 -0400600 if (!keep && args->fetch_all &&
601 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
602 keep = 1;
603 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700604
605 if (keep) {
606 *newtail = ref;
607 ref->next = NULL;
608 newtail = &ref->next;
609 } else {
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700610 ref->next = unmatched;
611 unmatched = ref;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700612 }
613 }
614
René Scharfe22a16462018-10-04 17:09:39 +0200615 if (strict) {
616 for (i = 0; i < nr_sought; i++) {
617 ref = sought[i];
618 if (!is_unmatched_ref(ref))
619 continue;
620
621 add_refs_to_oidset(&tip_oids, unmatched);
622 add_refs_to_oidset(&tip_oids, newlist);
623 break;
624 }
625 }
626
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800627 /* Append unmatched requests to the list */
Matt McCutchend56583d2017-02-22 11:05:57 -0500628 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500629 ref = sought[i];
René Scharfebf732822018-10-04 17:09:06 +0200630 if (!is_unmatched_ref(ref))
Matt McCutchend56583d2017-02-22 11:05:57 -0500631 continue;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800632
René Scharfe22a16462018-10-04 17:09:39 +0200633 if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500634 ref->match_status = REF_MATCHED;
Jeff Kingc3c17bf2015-03-19 16:37:09 -0400635 *newtail = copy_ref(ref);
636 newtail = &(*newtail)->next;
Matt McCutchend56583d2017-02-22 11:05:57 -0500637 } else {
638 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800639 }
640 }
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700641
642 oidset_clear(&tip_oids);
Jeff King259eddd2019-04-13 01:54:09 -0400643 free_refs(unmatched);
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700644
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700645 *refs = newlist;
646}
647
Jonathan Tanec062832018-06-14 15:54:28 -0700648static void mark_alternate_complete(struct fetch_negotiator *unused,
Jonathan Tand30fe892018-06-14 15:54:26 -0700649 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700650{
brian m. carlson1b283372017-05-01 02:28:54 +0000651 mark_complete(&obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700652}
653
Takuto Ikuta024aa462018-03-14 15:32:42 +0900654struct loose_object_iter {
655 struct oidset *loose_object_set;
656 struct ref *refs;
657};
658
659/*
Jonathan Tan34c29032018-06-06 13:47:07 -0700660 * Mark recent commits available locally and reachable from a local ref as
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700661 * COMPLETE.
Jonathan Tan34c29032018-06-06 13:47:07 -0700662 *
663 * The cutoff time for recency is determined by this heuristic: it is the
664 * earliest commit time of the objects in refs that are commits and that we know
665 * the commit time of.
666 */
Jonathan Tanec062832018-06-14 15:54:28 -0700667static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700668 struct fetch_pack_args *args,
Jonathan Tan34c29032018-06-06 13:47:07 -0700669 struct ref **refs)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700670{
671 struct ref *ref;
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000672 int old_save_commit_buffer = save_commit_buffer;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200673 timestamp_t cutoff = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700674
675 save_commit_buffer = 0;
676
Erik Chen9e5afdf2019-11-19 23:02:09 +0000677 trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700678 for (ref = *refs; ref; ref = ref->next) {
679 struct object *o;
680
Jeff King97b2fa02018-11-12 09:55:58 -0500681 if (!has_object_file_with_flags(&ref->old_oid,
Jonathan Tan6462d5e2019-11-05 10:56:19 -0800682 OBJECT_INFO_QUICK |
683 OBJECT_INFO_SKIP_FETCH_OBJECT))
Junio C Hamano012a1bb2013-01-26 19:42:09 -0800684 continue;
Stefan Beller109cd762018-06-28 18:21:51 -0700685 o = parse_object(the_repository, &ref->old_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700686 if (!o)
687 continue;
688
Erik Chen9e5afdf2019-11-19 23:02:09 +0000689 /*
690 * We already have it -- which may mean that we were
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700691 * in sync with the other side at some time after
692 * that (it is OK if we guess wrong here).
693 */
694 if (o->type == OBJ_COMMIT) {
695 struct commit *commit = (struct commit *)o;
696 if (!cutoff || cutoff < commit->date)
697 cutoff = commit->date;
698 }
699 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000700 trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700701
Erik Chen9e5afdf2019-11-19 23:02:09 +0000702 /*
703 * This block marks all local refs as COMPLETE, and then recursively marks all
704 * parents of those refs as COMPLETE.
705 */
706 trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700707 if (!args->deepen) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700708 for_each_rawref(mark_complete_oid, NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700709 for_each_cached_alternate(NULL, mark_alternate_complete);
710 commit_list_sort_by_date(&complete);
711 if (cutoff)
712 mark_recent_complete_commits(args, cutoff);
713 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000714 trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700715
Jonathan Tan12f19a92018-10-03 16:04:52 -0700716 /*
717 * Mark all complete remote refs as common refs.
718 * Don't mark them common yet; the server has to be told so first.
719 */
Erik Chen9e5afdf2019-11-19 23:02:09 +0000720 trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL);
Jonathan Tan12f19a92018-10-03 16:04:52 -0700721 for (ref = *refs; ref; ref = ref->next) {
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700722 struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700723
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700724 if (!c || !(c->object.flags & COMPLETE))
Jonathan Tan12f19a92018-10-03 16:04:52 -0700725 continue;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700726
Jonathan Tan5c3b8012020-08-17 21:01:35 -0700727 negotiator->known_common(negotiator, c);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700728 }
Erik Chen9e5afdf2019-11-19 23:02:09 +0000729 trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700730
Jonathan Tan34c29032018-06-06 13:47:07 -0700731 save_commit_buffer = old_save_commit_buffer;
732}
733
734/*
735 * Returns 1 if every object pointed to by the given remote refs is available
736 * locally and reachable from a local ref, and 0 otherwise.
737 */
738static int everything_local(struct fetch_pack_args *args,
739 struct ref **refs)
740{
741 struct ref *ref;
742 int retval;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700743
744 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000745 const struct object_id *remote = &ref->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700746 struct object *o;
747
Jeff Kingd0229ab2019-06-20 03:41:14 -0400748 o = lookup_object(the_repository, remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700749 if (!o || !(o->flags & COMPLETE)) {
750 retval = 0;
brian m. carlson1b283372017-05-01 02:28:54 +0000751 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700752 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700753 continue;
754 }
brian m. carlson1b283372017-05-01 02:28:54 +0000755 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700756 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700757 }
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000758
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700759 return retval;
760}
761
762static int sideband_demux(int in, int out, void *data)
763{
764 int *xd = data;
Jeff King9ff18fa2016-02-24 02:44:58 -0500765 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700766
Jeff King9ff18fa2016-02-24 02:44:58 -0500767 ret = recv_sideband("fetch-pack", xd[0], out);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700768 close(out);
769 return ret;
770}
771
Jonathan Tan5374a292019-10-14 17:12:31 -0700772static void write_promisor_file(const char *keep_name,
773 struct ref **sought, int nr_sought)
774{
775 struct strbuf promisor_name = STRBUF_INIT;
776 int suffix_stripped;
777 FILE *output;
778 int i;
779
780 strbuf_addstr(&promisor_name, keep_name);
781 suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep");
782 if (!suffix_stripped)
783 BUG("name of pack lockfile should end with .keep (was '%s')",
784 keep_name);
785 strbuf_addstr(&promisor_name, ".promisor");
786
787 output = xfopen(promisor_name.buf, "w");
788 for (i = 0; i < nr_sought; i++)
789 fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid),
790 sought[i]->name);
791 fclose(output);
792
793 strbuf_release(&promisor_name);
794}
795
Jonathan Tanece9aea2020-08-17 12:48:19 -0700796/*
797 * Pass 1 as "only_packfile" if the pack received is the only pack in this
798 * fetch request (that is, if there were no packfile URIs provided).
799 */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700800static int get_pack(struct fetch_pack_args *args,
Jonathan Tan9da69a62020-06-10 13:57:22 -0700801 int xd[2], struct string_list *pack_lockfiles,
Jonathan Tandd4b7322020-06-10 13:57:23 -0700802 int only_packfile,
Jonathan Tan5374a292019-10-14 17:12:31 -0700803 struct ref **sought, int nr_sought)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700804{
805 struct async demux;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700806 int do_keep = args->keep_pack;
Jeff King984a43b2015-09-24 17:07:54 -0400807 const char *cmd_name;
808 struct pack_header header;
809 int pass_header = 0;
René Scharfed3180272014-08-19 21:09:35 +0200810 struct child_process cmd = CHILD_PROCESS_INIT;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700811 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700812
813 memset(&demux, 0, sizeof(demux));
814 if (use_sideband) {
815 /* xd[] is talking with upload-pack; subprocess reads from
816 * xd[0], spits out band#2 to stderr, and feeds us band#1
817 * through demux->out.
818 */
819 demux.proc = sideband_demux;
820 demux.data = xd;
821 demux.out = -1;
Jeff Kingdf857572016-04-19 18:50:29 -0400822 demux.isolate_sigpipe = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700823 if (start_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700824 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700825 }
826 else
827 demux.out = xd[0];
828
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700829 if (!args->keep_pack && unpack_limit) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700830
831 if (read_pack_header(demux.out, &header))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700832 die(_("protocol error: bad pack header"));
Jeff King984a43b2015-09-24 17:07:54 -0400833 pass_header = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700834 if (ntohl(header.hdr_entries) < unpack_limit)
835 do_keep = 0;
836 else
837 do_keep = 1;
838 }
839
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700840 if (alternate_shallow_file) {
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400841 strvec_push(&cmd.args, "--shallow-file");
842 strvec_push(&cmd.args, alternate_shallow_file);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700843 }
844
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000845 if (do_keep || args->from_promisor) {
Jonathan Tan9da69a62020-06-10 13:57:22 -0700846 if (pack_lockfiles)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700847 cmd.out = -1;
Jeff King984a43b2015-09-24 17:07:54 -0400848 cmd_name = "index-pack";
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400849 strvec_push(&cmd.args, cmd_name);
850 strvec_push(&cmd.args, "--stdin");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700851 if (!args->quiet && !args->no_progress)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400852 strvec_push(&cmd.args, "-v");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700853 if (args->use_thin_pack)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400854 strvec_push(&cmd.args, "--fix-thin");
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000855 if (do_keep && (args->lock_pack || unpack_limit)) {
René Scharfeda25bdb2017-04-18 17:57:42 -0400856 char hostname[HOST_NAME_MAX + 1];
David Turner5781a9a2017-04-18 17:57:43 -0400857 if (xgethostname(hostname, sizeof(hostname)))
Jeff King984a43b2015-09-24 17:07:54 -0400858 xsnprintf(hostname, sizeof(hostname), "localhost");
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400859 strvec_pushf(&cmd.args,
Jeff Kingf6d89422020-07-28 16:26:31 -0400860 "--keep=fetch-pack %"PRIuMAX " on %s",
861 (uintmax_t)getpid(), hostname);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700862 }
Jonathan Tandd4b7322020-06-10 13:57:23 -0700863 if (only_packfile && args->check_self_contained_and_connected)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400864 strvec_push(&cmd.args, "--check-self-contained-and-connected");
Jonathan Tandd4b7322020-06-10 13:57:23 -0700865 else
866 /*
867 * We cannot perform any connectivity checks because
868 * not all packs have been downloaded; let the caller
869 * have this responsibility.
870 */
871 args->check_self_contained_and_connected = 0;
Jonathan Tan1b03df52020-08-20 10:51:16 -0700872
873 if (args->from_promisor)
874 /*
875 * write_promisor_file() may be called afterwards but
876 * we still need index-pack to know that this is a
877 * promisor pack. For example, if transfer.fsckobjects
878 * is true, index-pack needs to know that .gitmodules
879 * is a promisor object (so that it won't complain if
880 * it is missing).
881 */
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400882 strvec_push(&cmd.args, "--promisor");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700883 }
884 else {
Jeff King984a43b2015-09-24 17:07:54 -0400885 cmd_name = "unpack-objects";
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400886 strvec_push(&cmd.args, cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700887 if (args->quiet || args->no_progress)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400888 strvec_push(&cmd.args, "-q");
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700889 args->check_self_contained_and_connected = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700890 }
Jeff King984a43b2015-09-24 17:07:54 -0400891
892 if (pass_header)
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400893 strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
Jeff Kingf6d89422020-07-28 16:26:31 -0400894 ntohl(header.hdr_version),
Jeff King984a43b2015-09-24 17:07:54 -0400895 ntohl(header.hdr_entries));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700896 if (fetch_fsck_objects >= 0
897 ? fetch_fsck_objects
898 : transfer_fsck_objects >= 0
899 ? transfer_fsck_objects
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700900 : 0) {
Jonathan Tan0bd96be2020-08-17 12:48:20 -0700901 if (args->from_promisor || !only_packfile)
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700902 /*
903 * We cannot use --strict in index-pack because it
904 * checks both broken objects and links, but we only
905 * want to check for broken objects.
906 */
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400907 strvec_push(&cmd.args, "--fsck-objects");
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700908 else
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400909 strvec_pushf(&cmd.args, "--strict%s",
Jeff Kingf6d89422020-07-28 16:26:31 -0400910 fsck_msg_types.buf);
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700911 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700912
913 cmd.in = demux.out;
914 cmd.git_cmd = 1;
915 if (start_command(&cmd))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700916 die(_("fetch-pack: unable to fork off %s"), cmd_name);
Jonathan Tan9da69a62020-06-10 13:57:22 -0700917 if (do_keep && pack_lockfiles) {
918 string_list_append_nodup(pack_lockfiles,
919 index_pack_lockfile(cmd.out));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700920 close(cmd.out);
921 }
922
Jens Lindstrom37cb1dd2013-10-22 15:36:02 +0200923 if (!use_sideband)
924 /* Closed by start_command() */
925 xd[0] = -1;
926
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700927 ret = finish_command(&cmd);
928 if (!ret || (args->check_self_contained_and_connected && ret == 1))
929 args->self_contained_and_connected =
930 args->check_self_contained_and_connected &&
931 ret == 0;
932 else
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700933 die(_("%s failed"), cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700934 if (use_sideband && finish_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700935 die(_("error in sideband demultiplexer"));
Jonathan Tan5374a292019-10-14 17:12:31 -0700936
937 /*
938 * Now that index-pack has succeeded, write the promisor file using the
939 * obtained .keep filename if necessary
940 */
Jonathan Tan9da69a62020-06-10 13:57:22 -0700941 if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor)
942 write_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought);
Jonathan Tan5374a292019-10-14 17:12:31 -0700943
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700944 return 0;
945}
946
Junio C Hamanof2db8542013-01-29 14:02:15 -0800947static int cmp_ref_by_name(const void *a_, const void *b_)
948{
949 const struct ref *a = *((const struct ref **)a_);
950 const struct ref *b = *((const struct ref **)b_);
951 return strcmp(a->name, b->name);
952}
953
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700954static struct ref *do_fetch_pack(struct fetch_pack_args *args,
955 int fd[2],
956 const struct ref *orig_ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800957 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +0700958 struct shallow_info *si,
Jonathan Tan9da69a62020-06-10 13:57:22 -0700959 struct string_list *pack_lockfiles)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700960{
Derrick Stoleeaaf633c2019-08-13 11:37:48 -0700961 struct repository *r = the_repository;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700962 struct ref *ref = copy_ref_list(orig_ref);
brian m. carlson1b283372017-05-01 02:28:54 +0000963 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700964 const char *agent_feature;
965 int agent_len;
Jonathan Tan603960b2019-11-12 16:34:20 -0800966 struct fetch_negotiator negotiator_alloc;
967 struct fetch_negotiator *negotiator;
968
Jonathan Tan9dfa8db2020-08-17 21:01:37 -0700969 negotiator = &negotiator_alloc;
970 fetch_negotiator_init(r, negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700971
972 sort_ref_list(&ref, ref_compare_name);
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200973 QSORT(sought, nr_sought, cmp_ref_by_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700974
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700975 if ((agent_feature = server_feature_value("agent", &agent_len))) {
976 agent_supported = 1;
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700977 if (agent_len)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700978 print_verbose(args, _("Server version is %.*s"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700979 agent_len, agent_feature);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700980 }
Nguyễn Thái Ngọc Duy0e042972019-06-20 18:59:51 +0700981
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +0700982 if (server_supports("shallow"))
983 print_verbose(args, _("Server supports %s"), "shallow");
Derrick Stoleeaaf633c2019-08-13 11:37:48 -0700984 else if (args->depth > 0 || is_repository_shallow(r))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700985 die(_("Server does not support shallow clients"));
986 if (args->depth > 0 || args->deepen_since || args->deepen_not)
987 args->deepen = 1;
988 if (server_supports("multi_ack_detailed")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700989 print_verbose(args, _("Server supports %s"), "multi_ack_detailed");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700990 multi_ack = 2;
991 if (server_supports("no-done")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700992 print_verbose(args, _("Server supports %s"), "no-done");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700993 if (args->stateless_rpc)
994 no_done = 1;
995 }
996 }
997 else if (server_supports("multi_ack")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +0700998 print_verbose(args, _("Server supports %s"), "multi_ack");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700999 multi_ack = 1;
1000 }
1001 if (server_supports("side-band-64k")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001002 print_verbose(args, _("Server supports %s"), "side-band-64k");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001003 use_sideband = 2;
1004 }
1005 else if (server_supports("side-band")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001006 print_verbose(args, _("Server supports %s"), "side-band");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001007 use_sideband = 1;
1008 }
1009 if (server_supports("allow-tip-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001010 print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001011 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1012 }
1013 if (server_supports("allow-reachable-sha1-in-want")) {
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001014 print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001015 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1016 }
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001017 if (server_supports("thin-pack"))
1018 print_verbose(args, _("Server supports %s"), "thin-pack");
1019 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001020 args->use_thin_pack = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001021 if (server_supports("no-progress"))
1022 print_verbose(args, _("Server supports %s"), "no-progress");
1023 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001024 args->no_progress = 0;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001025 if (server_supports("include-tag"))
1026 print_verbose(args, _("Server supports %s"), "include-tag");
1027 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001028 args->include_tag = 0;
1029 if (server_supports("ofs-delta"))
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001030 print_verbose(args, _("Server supports %s"), "ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001031 else
1032 prefer_ofs_delta = 0;
1033
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001034 if (server_supports("filter")) {
1035 server_supports_filtering = 1;
Nguyễn Thái Ngọc Duy0778b292019-06-20 18:59:49 +07001036 print_verbose(args, _("Server supports %s"), "filter");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001037 } else if (args->filter_options.choice) {
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001038 warning("filtering not recognized by server, ignoring");
1039 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001040
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001041 if (server_supports("deepen-since")) {
1042 print_verbose(args, _("Server supports %s"), "deepen-since");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001043 deepen_since_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001044 } else if (args->deepen_since)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001045 die(_("Server does not support --shallow-since"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001046 if (server_supports("deepen-not")) {
1047 print_verbose(args, _("Server supports %s"), "deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001048 deepen_not_ok = 1;
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001049 } else if (args->deepen_not)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001050 die(_("Server does not support --shallow-exclude"));
Nguyễn Thái Ngọc Duy5a885832019-06-20 18:59:50 +07001051 if (server_supports("deepen-relative"))
1052 print_verbose(args, _("Server supports %s"), "deepen-relative");
1053 else if (args->deepen_relative)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001054 die(_("Server does not support --deepen"));
brian m. carlson48bf1412020-05-25 19:58:59 +00001055 if (!server_supports_hash(the_hash_algo->name, NULL))
1056 die(_("Server does not support this repository's object format"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001057
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001058 mark_complete_and_common_ref(negotiator, args, &ref);
1059 filter_refs(args, &ref, sought, nr_sought);
1060 if (everything_local(args, &ref)) {
1061 packet_flush(fd[1]);
1062 goto all_done;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001063 }
Jonathan Tan603960b2019-11-12 16:34:20 -08001064 if (find_common(negotiator, args, fd, &oid, ref) < 0)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001065 if (!args->keep_pack)
1066 /* When cloning, it is not unusual to have
1067 * no common commit.
1068 */
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001069 warning(_("no common commits"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001070
1071 if (args->stateless_rpc)
1072 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001073 if (args->deepen)
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +07001074 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1075 NULL);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001076 else if (si->nr_ours || si->nr_theirs)
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001077 alternate_shallow_file = setup_temporary_shallow(si->shallow);
Nguyễn Thái Ngọc Duy6da8bdc2013-08-26 09:17:26 +07001078 else
1079 alternate_shallow_file = NULL;
Jonathan Tandd4b7322020-06-10 13:57:23 -07001080 if (get_pack(args, fd, pack_lockfiles, 1, sought, nr_sought))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001081 die(_("git fetch-pack: fetch failed."));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001082
1083 all_done:
Jonathan Tan603960b2019-11-12 16:34:20 -08001084 if (negotiator)
1085 negotiator->release(negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001086 return ref;
1087}
1088
Brandon Williamsf7e20502018-03-15 10:31:29 -07001089static void add_shallow_requests(struct strbuf *req_buf,
1090 const struct fetch_pack_args *args)
1091{
Junio C Hamano00624d62018-07-18 12:20:27 -07001092 if (is_repository_shallow(the_repository))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001093 write_shallow_commits(req_buf, 1, NULL);
1094 if (args->depth > 0)
1095 packet_buf_write(req_buf, "deepen %d", args->depth);
1096 if (args->deepen_since) {
1097 timestamp_t max_age = approxidate(args->deepen_since);
1098 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1099 }
1100 if (args->deepen_not) {
1101 int i;
1102 for (i = 0; i < args->deepen_not->nr; i++) {
1103 struct string_list_item *s = args->deepen_not->items + i;
1104 packet_buf_write(req_buf, "deepen-not %s", s->string);
1105 }
1106 }
Jonathan Tan5056cf42018-12-18 13:24:35 -08001107 if (args->deepen_relative)
1108 packet_buf_write(req_buf, "deepen-relative\n");
Brandon Williamsf7e20502018-03-15 10:31:29 -07001109}
1110
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001111static void add_wants(const struct ref *wants, struct strbuf *req_buf)
Brandon Williams685fbd32018-03-15 10:31:28 -07001112{
Brandon Williams73302052018-06-27 15:30:23 -07001113 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1114
Brandon Williams685fbd32018-03-15 10:31:28 -07001115 for ( ; wants ; wants = wants->next) {
1116 const struct object_id *remote = &wants->old_oid;
Brandon Williams685fbd32018-03-15 10:31:28 -07001117 struct object *o;
1118
1119 /*
1120 * If that object is complete (i.e. it is an ancestor of a
1121 * local ref), we tell them we have it but do not have to
1122 * tell them about its ancestors, which they already know
1123 * about.
1124 *
1125 * We use lookup_object here because we are only
1126 * interested in the case we *know* the object is
1127 * reachable and we have already scanned it.
1128 */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001129 if (((o = lookup_object(the_repository, remote)) != NULL) &&
Brandon Williams685fbd32018-03-15 10:31:28 -07001130 (o->flags & COMPLETE)) {
1131 continue;
1132 }
1133
Brandon Williams73302052018-06-27 15:30:23 -07001134 if (!use_ref_in_want || wants->exact_oid)
1135 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1136 else
1137 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
Brandon Williams685fbd32018-03-15 10:31:28 -07001138 }
1139}
1140
1141static void add_common(struct strbuf *req_buf, struct oidset *common)
1142{
1143 struct oidset_iter iter;
1144 const struct object_id *oid;
1145 oidset_iter_init(common, &iter);
1146
1147 while ((oid = oidset_iter_next(&iter))) {
1148 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1149 }
1150}
1151
Jonathan Tanec062832018-06-14 15:54:28 -07001152static int add_haves(struct fetch_negotiator *negotiator,
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001153 int seen_ack,
Jonathan Tanec062832018-06-14 15:54:28 -07001154 struct strbuf *req_buf,
Jonathan Tand30fe892018-06-14 15:54:26 -07001155 int *haves_to_send, int *in_vain)
Brandon Williams685fbd32018-03-15 10:31:28 -07001156{
1157 int ret = 0;
1158 int haves_added = 0;
1159 const struct object_id *oid;
1160
Jonathan Tanec062832018-06-14 15:54:28 -07001161 while ((oid = negotiator->next(negotiator))) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001162 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1163 if (++haves_added >= *haves_to_send)
1164 break;
1165 }
1166
1167 *in_vain += haves_added;
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001168 if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001169 /* Send Done */
1170 packet_buf_write(req_buf, "done\n");
1171 ret = 1;
1172 }
1173
1174 /* Increase haves to send on next round */
1175 *haves_to_send = next_flush(1, *haves_to_send);
1176
1177 return ret;
1178}
1179
Jonathan Tanec062832018-06-14 15:54:28 -07001180static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001181 struct fetch_pack_args *args,
Brandon Williams685fbd32018-03-15 10:31:28 -07001182 const struct ref *wants, struct oidset *common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001183 int *haves_to_send, int *in_vain,
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001184 int sideband_all, int seen_ack)
Brandon Williams685fbd32018-03-15 10:31:28 -07001185{
1186 int ret = 0;
brian m. carlson4b831202020-05-25 19:59:07 +00001187 const char *hash_name;
Brandon Williams685fbd32018-03-15 10:31:28 -07001188 struct strbuf req_buf = STRBUF_INIT;
1189
1190 if (server_supports_v2("fetch", 1))
1191 packet_buf_write(&req_buf, "command=fetch");
1192 if (server_supports_v2("agent", 0))
1193 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
Brandon Williams5e3548e2018-04-23 15:46:24 -07001194 if (args->server_options && args->server_options->nr &&
1195 server_supports_v2("server-option", 1)) {
1196 int i;
1197 for (i = 0; i < args->server_options->nr; i++)
Jonathan Tan5b204b72019-05-22 13:08:22 -07001198 packet_buf_write(&req_buf, "server-option=%s",
Brandon Williams5e3548e2018-04-23 15:46:24 -07001199 args->server_options->items[i].string);
1200 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001201
brian m. carlson4b831202020-05-25 19:59:07 +00001202 if (server_feature_v2("object-format", &hash_name)) {
1203 int hash_algo = hash_algo_by_name(hash_name);
1204 if (hash_algo_by_ptr(the_hash_algo) != hash_algo)
1205 die(_("mismatched algorithms: client %s; server %s"),
1206 the_hash_algo->name, hash_name);
1207 packet_write_fmt(fd_out, "object-format=%s", the_hash_algo->name);
1208 } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) {
1209 die(_("the server does not support algorithm '%s'"),
1210 the_hash_algo->name);
1211 }
1212
Brandon Williams685fbd32018-03-15 10:31:28 -07001213 packet_buf_delim(&req_buf);
1214 if (args->use_thin_pack)
1215 packet_buf_write(&req_buf, "thin-pack");
1216 if (args->no_progress)
1217 packet_buf_write(&req_buf, "no-progress");
1218 if (args->include_tag)
1219 packet_buf_write(&req_buf, "include-tag");
1220 if (prefer_ofs_delta)
1221 packet_buf_write(&req_buf, "ofs-delta");
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001222 if (sideband_all)
1223 packet_buf_write(&req_buf, "sideband-all");
Brandon Williams685fbd32018-03-15 10:31:28 -07001224
Brandon Williamsf7e20502018-03-15 10:31:29 -07001225 /* Add shallow-info and deepen request */
1226 if (server_supports_feature("fetch", "shallow", 0))
1227 add_shallow_requests(&req_buf, args);
Junio C Hamano00624d62018-07-18 12:20:27 -07001228 else if (is_repository_shallow(the_repository) || args->deepen)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001229 die(_("Server does not support shallow requests"));
1230
Jonathan Tanba957102018-05-03 16:46:56 -07001231 /* Add filter */
1232 if (server_supports_feature("fetch", "filter", 0) &&
1233 args->filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001234 const char *spec =
1235 expand_list_objects_filter_spec(&args->filter_options);
Jonathan Tanba957102018-05-03 16:46:56 -07001236 print_verbose(args, _("Server supports filter"));
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001237 packet_buf_write(&req_buf, "filter %s", spec);
Jonathan Tanba957102018-05-03 16:46:56 -07001238 } else if (args->filter_options.choice) {
1239 warning("filtering not recognized by server, ignoring");
1240 }
1241
Jonathan Tandd4b7322020-06-10 13:57:23 -07001242 if (server_supports_feature("fetch", "packfile-uris", 0)) {
1243 int i;
1244 struct strbuf to_send = STRBUF_INIT;
1245
1246 for (i = 0; i < uri_protocols.nr; i++) {
1247 const char *s = uri_protocols.items[i].string;
1248
1249 if (!strcmp(s, "https") || !strcmp(s, "http")) {
1250 if (to_send.len)
1251 strbuf_addch(&to_send, ',');
1252 strbuf_addstr(&to_send, s);
1253 }
1254 }
1255 if (to_send.len) {
1256 packet_buf_write(&req_buf, "packfile-uris %s",
1257 to_send.buf);
1258 strbuf_release(&to_send);
1259 }
1260 }
1261
Brandon Williams685fbd32018-03-15 10:31:28 -07001262 /* add wants */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001263 add_wants(wants, &req_buf);
Brandon Williams685fbd32018-03-15 10:31:28 -07001264
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001265 /* Add all of the common commits we've found in previous rounds */
1266 add_common(&req_buf, common);
Brandon Williams685fbd32018-03-15 10:31:28 -07001267
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001268 /* Add initial haves */
1269 ret = add_haves(negotiator, seen_ack, &req_buf,
1270 haves_to_send, in_vain);
Brandon Williams685fbd32018-03-15 10:31:28 -07001271
1272 /* Send request */
1273 packet_buf_flush(&req_buf);
Jeff King37c80012019-03-04 23:11:39 -05001274 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
1275 die_errno(_("unable to write request to remote"));
Brandon Williams685fbd32018-03-15 10:31:28 -07001276
1277 strbuf_release(&req_buf);
1278 return ret;
1279}
1280
1281/*
1282 * Processes a section header in a server's response and checks if it matches
1283 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1284 * not consumed); if 0, the line will be consumed and the function will die if
1285 * the section header doesn't match what was expected.
1286 */
1287static int process_section_header(struct packet_reader *reader,
1288 const char *section, int peek)
1289{
1290 int ret;
1291
1292 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001293 die(_("error reading section header '%s'"), section);
Brandon Williams685fbd32018-03-15 10:31:28 -07001294
1295 ret = !strcmp(reader->line, section);
1296
1297 if (!peek) {
1298 if (!ret)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001299 die(_("expected '%s', received '%s'"),
Brandon Williams685fbd32018-03-15 10:31:28 -07001300 section, reader->line);
1301 packet_reader_read(reader);
1302 }
1303
1304 return ret;
1305}
1306
Jonathan Tand1185aa2020-04-27 17:01:08 -07001307enum common_found {
1308 /*
1309 * No commit was found to be possessed by both the client and the
1310 * server, and "ready" was not received.
1311 */
1312 NO_COMMON_FOUND,
1313
1314 /*
1315 * At least one commit was found to be possessed by both the client and
1316 * the server, and "ready" was not received.
1317 */
1318 COMMON_FOUND,
1319
1320 /*
1321 * "ready" was received, indicating that the server is ready to send
1322 * the packfile without any further negotiation.
1323 */
1324 READY
1325};
1326
1327static enum common_found process_acks(struct fetch_negotiator *negotiator,
1328 struct packet_reader *reader,
1329 struct oidset *common)
Brandon Williams685fbd32018-03-15 10:31:28 -07001330{
1331 /* received */
1332 int received_ready = 0;
1333 int received_ack = 0;
1334
1335 process_section_header(reader, "acknowledgments", 0);
1336 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1337 const char *arg;
1338
1339 if (!strcmp(reader->line, "NAK"))
1340 continue;
1341
1342 if (skip_prefix(reader->line, "ACK ", &arg)) {
1343 struct object_id oid;
Jonathan Tan2f0a0932020-04-27 17:01:10 -07001344 received_ack = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001345 if (!get_oid_hex(arg, &oid)) {
1346 struct commit *commit;
1347 oidset_insert(common, &oid);
Stefan Bellerc1f5eb42018-06-28 18:21:59 -07001348 commit = lookup_commit(the_repository, &oid);
Jonathan Tan603960b2019-11-12 16:34:20 -08001349 if (negotiator)
1350 negotiator->ack(negotiator, commit);
Brandon Williams685fbd32018-03-15 10:31:28 -07001351 }
1352 continue;
1353 }
1354
1355 if (!strcmp(reader->line, "ready")) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001356 received_ready = 1;
1357 continue;
1358 }
1359
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001360 die(_("unexpected acknowledgment line: '%s'"), reader->line);
Brandon Williams685fbd32018-03-15 10:31:28 -07001361 }
1362
1363 if (reader->status != PACKET_READ_FLUSH &&
1364 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001365 die(_("error processing acks: %d"), reader->status);
Brandon Williams685fbd32018-03-15 10:31:28 -07001366
Jonathan Tan5400b2a2018-10-19 15:54:04 -07001367 /*
1368 * If an "acknowledgments" section is sent, a packfile is sent if and
1369 * only if "ready" was sent in this section. The other sections
1370 * ("shallow-info" and "wanted-refs") are sent only if a packfile is
1371 * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH
1372 * otherwise.
1373 */
1374 if (received_ready && reader->status != PACKET_READ_DELIM)
1375 die(_("expected packfile to be sent after 'ready'"));
1376 if (!received_ready && reader->status != PACKET_READ_FLUSH)
1377 die(_("expected no other sections to be sent after no 'ready'"));
1378
Jonathan Tand1185aa2020-04-27 17:01:08 -07001379 return received_ready ? READY :
1380 (received_ack ? COMMON_FOUND : NO_COMMON_FOUND);
Brandon Williams685fbd32018-03-15 10:31:28 -07001381}
1382
Brandon Williamsf7e20502018-03-15 10:31:29 -07001383static void receive_shallow_info(struct fetch_pack_args *args,
Jonathan Tan13390782019-03-26 12:31:21 -07001384 struct packet_reader *reader,
1385 struct oid_array *shallows,
1386 struct shallow_info *si)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001387{
Jonathan Tan13390782019-03-26 12:31:21 -07001388 int unshallow_received = 0;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001389
Brandon Williamsf7e20502018-03-15 10:31:29 -07001390 process_section_header(reader, "shallow-info", 0);
1391 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1392 const char *arg;
1393 struct object_id oid;
1394
1395 if (skip_prefix(reader->line, "shallow ", &arg)) {
1396 if (get_oid_hex(arg, &oid))
1397 die(_("invalid shallow line: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001398 oid_array_append(shallows, &oid);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001399 continue;
1400 }
1401 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1402 if (get_oid_hex(arg, &oid))
1403 die(_("invalid unshallow line: %s"), reader->line);
Jeff Kingd0229ab2019-06-20 03:41:14 -04001404 if (!lookup_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001405 die(_("object not found: %s"), reader->line);
1406 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -07001407 if (!parse_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001408 die(_("error in object: %s"), reader->line);
1409 if (unregister_shallow(&oid))
1410 die(_("no shallow found: %s"), reader->line);
Jonathan Tan13390782019-03-26 12:31:21 -07001411 unshallow_received = 1;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001412 continue;
1413 }
1414 die(_("expected shallow/unshallow, got %s"), reader->line);
1415 }
1416
1417 if (reader->status != PACKET_READ_FLUSH &&
1418 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001419 die(_("error processing shallow info: %d"), reader->status);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001420
Jonathan Tan13390782019-03-26 12:31:21 -07001421 if (args->deepen || unshallow_received) {
1422 /*
1423 * Treat these as shallow lines caused by our depth settings.
1424 * In v0, these lines cannot cause refs to be rejected; do the
1425 * same.
1426 */
1427 int i;
1428
1429 for (i = 0; i < shallows->nr; i++)
1430 register_shallow(the_repository, &shallows->oid[i]);
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001431 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1432 NULL);
1433 args->deepen = 1;
Jonathan Tan13390782019-03-26 12:31:21 -07001434 } else if (shallows->nr) {
1435 /*
1436 * Treat these as shallow lines caused by the remote being
1437 * shallow. In v0, remote refs that reach these objects are
1438 * rejected (unless --update-shallow is set); do the same.
1439 */
1440 prepare_shallow_info(si, shallows);
1441 if (si->nr_ours || si->nr_theirs)
1442 alternate_shallow_file =
1443 setup_temporary_shallow(si->shallow);
1444 else
1445 alternate_shallow_file = NULL;
brian m. carlson380ebab2019-02-06 23:59:37 +00001446 } else {
1447 alternate_shallow_file = NULL;
Jonathan Tanbd0b42a2019-01-10 11:36:45 -08001448 }
Brandon Williamsf7e20502018-03-15 10:31:29 -07001449}
1450
Jonathan Tanb7643002019-03-27 14:11:10 -07001451static int cmp_name_ref(const void *name, const void *ref)
1452{
1453 return strcmp(name, (*(struct ref **)ref)->name);
1454}
1455
Jonathan Tane2842b32018-08-01 13:13:20 -07001456static void receive_wanted_refs(struct packet_reader *reader,
1457 struct ref **sought, int nr_sought)
Brandon Williams73302052018-06-27 15:30:23 -07001458{
1459 process_section_header(reader, "wanted-refs", 0);
1460 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1461 struct object_id oid;
1462 const char *end;
Jonathan Tanb7643002019-03-27 14:11:10 -07001463 struct ref **found;
Brandon Williams73302052018-06-27 15:30:23 -07001464
1465 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001466 die(_("expected wanted-ref, got '%s'"), reader->line);
Brandon Williams73302052018-06-27 15:30:23 -07001467
Jonathan Tanb7643002019-03-27 14:11:10 -07001468 found = bsearch(end, sought, nr_sought, sizeof(*sought),
1469 cmp_name_ref);
1470 if (!found)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001471 die(_("unexpected wanted-ref: '%s'"), reader->line);
Jonathan Tanb7643002019-03-27 14:11:10 -07001472 oidcpy(&(*found)->old_oid, &oid);
Brandon Williams73302052018-06-27 15:30:23 -07001473 }
1474
1475 if (reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001476 die(_("error processing wanted refs: %d"), reader->status);
Brandon Williams73302052018-06-27 15:30:23 -07001477}
1478
Jonathan Tandd4b7322020-06-10 13:57:23 -07001479static void receive_packfile_uris(struct packet_reader *reader,
1480 struct string_list *uris)
1481{
1482 process_section_header(reader, "packfile-uris", 0);
1483 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1484 if (reader->pktlen < the_hash_algo->hexsz ||
1485 reader->line[the_hash_algo->hexsz] != ' ')
1486 die("expected '<hash> <uri>', got: %s\n", reader->line);
1487
1488 string_list_append(uris, reader->line);
1489 }
1490 if (reader->status != PACKET_READ_DELIM)
1491 die("expected DELIM");
1492}
1493
Brandon Williams685fbd32018-03-15 10:31:28 -07001494enum fetch_state {
1495 FETCH_CHECK_LOCAL = 0,
1496 FETCH_SEND_REQUEST,
1497 FETCH_PROCESS_ACKS,
1498 FETCH_GET_PACK,
1499 FETCH_DONE,
1500};
1501
Denton Liub0df0c12020-05-19 06:54:00 -04001502static void do_check_stateless_delimiter(const struct fetch_pack_args *args,
1503 struct packet_reader *reader)
1504{
1505 check_stateless_delimiter(args->stateless_rpc, reader,
1506 _("git fetch-pack: expected response end packet"));
1507}
1508
Brandon Williams685fbd32018-03-15 10:31:28 -07001509static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1510 int fd[2],
1511 const struct ref *orig_ref,
1512 struct ref **sought, int nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001513 struct oid_array *shallows,
1514 struct shallow_info *si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001515 struct string_list *pack_lockfiles)
Brandon Williams685fbd32018-03-15 10:31:28 -07001516{
Derrick Stoleeaaf633c2019-08-13 11:37:48 -07001517 struct repository *r = the_repository;
Brandon Williams685fbd32018-03-15 10:31:28 -07001518 struct ref *ref = copy_ref_list(orig_ref);
1519 enum fetch_state state = FETCH_CHECK_LOCAL;
1520 struct oidset common = OIDSET_INIT;
1521 struct packet_reader reader;
Josh Steadmon5fc31182019-10-02 16:49:28 -07001522 int in_vain = 0, negotiation_started = 0;
Brandon Williams685fbd32018-03-15 10:31:28 -07001523 int haves_to_send = INITIAL_FLUSH;
Jonathan Tan603960b2019-11-12 16:34:20 -08001524 struct fetch_negotiator negotiator_alloc;
1525 struct fetch_negotiator *negotiator;
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001526 int seen_ack = 0;
Jonathan Tandd4b7322020-06-10 13:57:23 -07001527 struct string_list packfile_uris = STRING_LIST_INIT_DUP;
1528 int i;
Jonathan Tan603960b2019-11-12 16:34:20 -08001529
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001530 negotiator = &negotiator_alloc;
1531 fetch_negotiator_init(r, negotiator);
Jonathan Tan603960b2019-11-12 16:34:20 -08001532
Brandon Williams685fbd32018-03-15 10:31:28 -07001533 packet_reader_init(&reader, fd[0], NULL, 0,
Masaya Suzuki2d103c32018-12-29 13:19:15 -08001534 PACKET_READ_CHOMP_NEWLINE |
1535 PACKET_READ_DIE_ON_ERR_PACKET);
Jonathan Tan07c3c2a2019-01-16 11:28:15 -08001536 if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) &&
1537 server_supports_feature("fetch", "sideband-all", 0)) {
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001538 reader.use_sideband = 1;
1539 reader.me = "fetch-pack";
1540 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001541
1542 while (state != FETCH_DONE) {
1543 switch (state) {
1544 case FETCH_CHECK_LOCAL:
1545 sort_ref_list(&ref, ref_compare_name);
1546 QSORT(sought, nr_sought, cmp_ref_by_name);
1547
1548 /* v2 supports these by default */
1549 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1550 use_sideband = 2;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001551 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1552 args->deepen = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001553
Brandon Williams685fbd32018-03-15 10:31:28 -07001554 /* Filter 'ref' by 'sought' and those that aren't local */
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001555 mark_complete_and_common_ref(negotiator, args, &ref);
1556 filter_refs(args, &ref, sought, nr_sought);
1557 if (everything_local(args, &ref))
1558 state = FETCH_DONE;
1559 else
Jonathan Tan12f19a92018-10-03 16:04:52 -07001560 state = FETCH_SEND_REQUEST;
Jonathan Tan9dfa8db2020-08-17 21:01:37 -07001561
1562 mark_tips(negotiator, args->negotiation_tips);
1563 for_each_cached_alternate(negotiator,
1564 insert_one_alternate_object);
Brandon Williams685fbd32018-03-15 10:31:28 -07001565 break;
1566 case FETCH_SEND_REQUEST:
Josh Steadmon5fc31182019-10-02 16:49:28 -07001567 if (!negotiation_started) {
1568 negotiation_started = 1;
1569 trace2_region_enter("fetch-pack",
1570 "negotiation_v2",
1571 the_repository);
1572 }
Jonathan Tan603960b2019-11-12 16:34:20 -08001573 if (send_fetch_request(negotiator, fd[1], args, ref,
Jonathan Tanec062832018-06-14 15:54:28 -07001574 &common,
Jonathan Tan0bbc0bc2019-01-16 11:28:14 -08001575 &haves_to_send, &in_vain,
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001576 reader.use_sideband,
1577 seen_ack))
Brandon Williams685fbd32018-03-15 10:31:28 -07001578 state = FETCH_GET_PACK;
1579 else
1580 state = FETCH_PROCESS_ACKS;
1581 break;
1582 case FETCH_PROCESS_ACKS:
1583 /* Process ACKs/NAKs */
Jonathan Tan603960b2019-11-12 16:34:20 -08001584 switch (process_acks(negotiator, &reader, &common)) {
Jonathan Tand1185aa2020-04-27 17:01:08 -07001585 case READY:
Denton Liub0df0c12020-05-19 06:54:00 -04001586 /*
1587 * Don't check for response delimiter; get_pack() will
1588 * read the rest of this response.
1589 */
Brandon Williams685fbd32018-03-15 10:31:28 -07001590 state = FETCH_GET_PACK;
1591 break;
Jonathan Tand1185aa2020-04-27 17:01:08 -07001592 case COMMON_FOUND:
Brandon Williams685fbd32018-03-15 10:31:28 -07001593 in_vain = 0;
Jonathan Tan4fa3f002020-04-27 17:01:09 -07001594 seen_ack = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001595 /* fallthrough */
Jonathan Tand1185aa2020-04-27 17:01:08 -07001596 case NO_COMMON_FOUND:
Denton Liub0df0c12020-05-19 06:54:00 -04001597 do_check_stateless_delimiter(args, &reader);
Brandon Williams685fbd32018-03-15 10:31:28 -07001598 state = FETCH_SEND_REQUEST;
1599 break;
1600 }
1601 break;
1602 case FETCH_GET_PACK:
Josh Steadmon5fc31182019-10-02 16:49:28 -07001603 trace2_region_leave("fetch-pack",
1604 "negotiation_v2",
1605 the_repository);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001606 /* Check for shallow-info section */
1607 if (process_section_header(&reader, "shallow-info", 1))
Jonathan Tan13390782019-03-26 12:31:21 -07001608 receive_shallow_info(args, &reader, shallows, si);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001609
Brandon Williams73302052018-06-27 15:30:23 -07001610 if (process_section_header(&reader, "wanted-refs", 1))
Jonathan Tane2842b32018-08-01 13:13:20 -07001611 receive_wanted_refs(&reader, sought, nr_sought);
Brandon Williams73302052018-06-27 15:30:23 -07001612
Jonathan Tandd4b7322020-06-10 13:57:23 -07001613 /* get the pack(s) */
1614 if (process_section_header(&reader, "packfile-uris", 1))
1615 receive_packfile_uris(&reader, &packfile_uris);
Brandon Williams685fbd32018-03-15 10:31:28 -07001616 process_section_header(&reader, "packfile", 0);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001617 if (get_pack(args, fd, pack_lockfiles,
1618 !packfile_uris.nr, sought, nr_sought))
Brandon Williams685fbd32018-03-15 10:31:28 -07001619 die(_("git fetch-pack: fetch failed."));
Denton Liub0df0c12020-05-19 06:54:00 -04001620 do_check_stateless_delimiter(args, &reader);
Brandon Williams685fbd32018-03-15 10:31:28 -07001621
1622 state = FETCH_DONE;
1623 break;
1624 case FETCH_DONE:
1625 continue;
1626 }
1627 }
1628
Jonathan Tandd4b7322020-06-10 13:57:23 -07001629 for (i = 0; i < packfile_uris.nr; i++) {
1630 struct child_process cmd = CHILD_PROCESS_INIT;
1631 char packname[GIT_MAX_HEXSZ + 1];
1632 const char *uri = packfile_uris.items[i].string +
1633 the_hash_algo->hexsz + 1;
1634
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001635 strvec_push(&cmd.args, "http-fetch");
1636 strvec_pushf(&cmd.args, "--packfile=%.*s",
Jeff Kingf6d89422020-07-28 16:26:31 -04001637 (int) the_hash_algo->hexsz,
1638 packfile_uris.items[i].string);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001639 strvec_push(&cmd.args, uri);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001640 cmd.git_cmd = 1;
1641 cmd.no_stdin = 1;
1642 cmd.out = -1;
1643 if (start_command(&cmd))
1644 die("fetch-pack: unable to spawn http-fetch");
1645
1646 if (read_in_full(cmd.out, packname, 5) < 0 ||
1647 memcmp(packname, "keep\t", 5))
1648 die("fetch-pack: expected keep then TAB at start of http-fetch output");
1649
1650 if (read_in_full(cmd.out, packname,
1651 the_hash_algo->hexsz + 1) < 0 ||
1652 packname[the_hash_algo->hexsz] != '\n')
1653 die("fetch-pack: expected hash then LF at end of http-fetch output");
1654
1655 packname[the_hash_algo->hexsz] = '\0';
1656
1657 close(cmd.out);
1658
1659 if (finish_command(&cmd))
1660 die("fetch-pack: unable to finish http-fetch");
1661
1662 if (memcmp(packfile_uris.items[i].string, packname,
1663 the_hash_algo->hexsz))
1664 die("fetch-pack: pack downloaded from %s does not match expected hash %.*s",
1665 uri, (int) the_hash_algo->hexsz,
1666 packfile_uris.items[i].string);
1667
1668 string_list_append_nodup(pack_lockfiles,
1669 xstrfmt("%s/pack/pack-%s.keep",
1670 get_object_directory(),
1671 packname));
1672 }
1673 string_list_clear(&packfile_uris, 0);
1674
Jonathan Tan603960b2019-11-12 16:34:20 -08001675 if (negotiator)
1676 negotiator->release(negotiator);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001677
Brandon Williams685fbd32018-03-15 10:31:28 -07001678 oidset_clear(&common);
1679 return ref;
1680}
1681
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001682static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1683{
1684 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1685 const char *path;
1686
1687 if (git_config_pathname(&path, var, value))
1688 return 1;
1689 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1690 fsck_msg_types.len ? ',' : '=', path);
1691 free((char *)path);
1692 return 0;
1693 }
1694
1695 if (skip_prefix(var, "fetch.fsck.", &var)) {
1696 if (is_valid_msg_type(var, value))
1697 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1698 fsck_msg_types.len ? ',' : '=', var, value);
1699 else
1700 warning("Skipping unknown msg id '%s'", var);
1701 return 0;
1702 }
1703
1704 return git_default_config(var, value, cb);
1705}
1706
Tanay Abhraf44af512014-08-07 09:21:20 -07001707static void fetch_pack_config(void)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001708{
Tanay Abhraf44af512014-08-07 09:21:20 -07001709 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1710 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1711 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1712 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1713 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
Jonathan Tandd4b7322020-06-10 13:57:23 -07001714 if (!uri_protocols.nr) {
1715 char *str;
1716
1717 if (!git_config_get_string("fetch.uriprotocols", &str) && str) {
1718 string_list_split(&uri_protocols, str, ',', -1);
1719 free(str);
1720 }
1721 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001722
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001723 git_config(fetch_pack_config_cb, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001724}
1725
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001726static void fetch_pack_setup(void)
1727{
1728 static int did_setup;
1729 if (did_setup)
1730 return;
Tanay Abhraf44af512014-08-07 09:21:20 -07001731 fetch_pack_config();
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001732 if (0 <= transfer_unpack_limit)
1733 unpack_limit = transfer_unpack_limit;
1734 else if (0 <= fetch_unpack_limit)
1735 unpack_limit = fetch_unpack_limit;
1736 did_setup = 1;
1737}
1738
Junio C Hamanof2db8542013-01-29 14:02:15 -08001739static int remove_duplicates_in_refs(struct ref **ref, int nr)
1740{
1741 struct string_list names = STRING_LIST_INIT_NODUP;
1742 int src, dst;
1743
1744 for (src = dst = 0; src < nr; src++) {
1745 struct string_list_item *item;
1746 item = string_list_insert(&names, ref[src]->name);
1747 if (item->util)
1748 continue; /* already have it */
1749 item->util = ref[src];
1750 if (src != dst)
1751 ref[dst] = ref[src];
1752 dst++;
1753 }
1754 for (src = dst; src < nr; src++)
1755 ref[src] = NULL;
1756 string_list_clear(&names, 0);
1757 return dst;
1758}
1759
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001760static void update_shallow(struct fetch_pack_args *args,
Jonathan Tane2842b32018-08-01 13:13:20 -07001761 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001762 struct shallow_info *si)
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001763{
brian m. carlson910650d2017-03-31 01:40:00 +00001764 struct oid_array ref = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001765 int *status;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001766 int i;
1767
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001768 if (args->deepen && alternate_shallow_file) {
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001769 if (*alternate_shallow_file == '\0') { /* --unshallow */
Stefan Beller102de882018-05-17 15:51:51 -07001770 unlink_or_warn(git_path_shallow(the_repository));
Taylor Blau37b9dca2020-04-22 18:25:45 -06001771 rollback_shallow_file(the_repository, &shallow_lock);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001772 } else
Taylor Blau37b9dca2020-04-22 18:25:45 -06001773 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001774 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001775 return;
1776 }
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001777
1778 if (!si->shallow || !si->shallow->nr)
1779 return;
1780
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001781 if (args->cloning) {
1782 /*
1783 * remote is shallow, but this is a clone, there are
1784 * no objects in repo to worry about. Accept any
1785 * shallow points that exist in the pack (iow in repo
1786 * after get_pack() and reprepare_packed_git())
1787 */
brian m. carlson910650d2017-03-31 01:40:00 +00001788 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001789 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001790 for (i = 0; i < si->shallow->nr; i++)
brian m. carlsonee3051b2017-03-26 16:01:37 +00001791 if (has_object_file(&oid[i]))
brian m. carlson910650d2017-03-31 01:40:00 +00001792 oid_array_append(&extra, &oid[i]);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001793 if (extra.nr) {
1794 setup_alternate_shallow(&shallow_lock,
1795 &alternate_shallow_file,
1796 &extra);
Taylor Blau37b9dca2020-04-22 18:25:45 -06001797 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson23311f32019-02-04 00:06:50 +00001798 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001799 }
brian m. carlson910650d2017-03-31 01:40:00 +00001800 oid_array_clear(&extra);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001801 return;
1802 }
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001803
1804 if (!si->nr_ours && !si->nr_theirs)
1805 return;
1806
1807 remove_nonexistent_theirs_shallow(si);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001808 if (!si->nr_ours && !si->nr_theirs)
1809 return;
Jonathan Tane2842b32018-08-01 13:13:20 -07001810 for (i = 0; i < nr_sought; i++)
1811 oid_array_append(&ref, &sought[i]->old_oid);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001812 si->ref = &ref;
1813
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001814 if (args->update_shallow) {
1815 /*
1816 * remote is also shallow, .git/shallow may be updated
1817 * so all refs can be accepted. Make sure we only add
1818 * shallow roots that are actually reachable from new
1819 * refs.
1820 */
brian m. carlson910650d2017-03-31 01:40:00 +00001821 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001822 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001823 assign_shallow_commits_to_refs(si, NULL, NULL);
1824 if (!si->nr_ours && !si->nr_theirs) {
brian m. carlson910650d2017-03-31 01:40:00 +00001825 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001826 return;
1827 }
1828 for (i = 0; i < si->nr_ours; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001829 oid_array_append(&extra, &oid[si->ours[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001830 for (i = 0; i < si->nr_theirs; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001831 oid_array_append(&extra, &oid[si->theirs[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001832 setup_alternate_shallow(&shallow_lock,
1833 &alternate_shallow_file,
1834 &extra);
Taylor Blau37b9dca2020-04-22 18:25:45 -06001835 commit_shallow_file(the_repository, &shallow_lock);
brian m. carlson910650d2017-03-31 01:40:00 +00001836 oid_array_clear(&extra);
1837 oid_array_clear(&ref);
brian m. carlson23311f32019-02-04 00:06:50 +00001838 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001839 return;
1840 }
1841
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001842 /*
1843 * remote is also shallow, check what ref is safe to update
1844 * without updating .git/shallow
1845 */
Jonathan Tane2842b32018-08-01 13:13:20 -07001846 status = xcalloc(nr_sought, sizeof(*status));
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001847 assign_shallow_commits_to_refs(si, NULL, status);
1848 if (si->nr_ours || si->nr_theirs) {
Jonathan Tane2842b32018-08-01 13:13:20 -07001849 for (i = 0; i < nr_sought; i++)
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001850 if (status[i])
Jonathan Tane2842b32018-08-01 13:13:20 -07001851 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001852 }
1853 free(status);
brian m. carlson910650d2017-03-31 01:40:00 +00001854 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001855}
1856
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001857static int iterate_ref_map(void *cb_data, struct object_id *oid)
1858{
1859 struct ref **rm = cb_data;
1860 struct ref *ref = *rm;
1861
1862 if (!ref)
1863 return -1; /* end of the list */
1864 *rm = ref->next;
1865 oidcpy(oid, &ref->old_oid);
1866 return 0;
1867}
1868
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001869struct ref *fetch_pack(struct fetch_pack_args *args,
Jeff King0f804b02019-03-20 04:16:14 -04001870 int fd[],
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001871 const struct ref *ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -08001872 struct ref **sought, int nr_sought,
brian m. carlson910650d2017-03-31 01:40:00 +00001873 struct oid_array *shallow,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001874 struct string_list *pack_lockfiles,
Brandon Williams685fbd32018-03-15 10:31:28 -07001875 enum protocol_version version)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001876{
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001877 struct ref *ref_cpy;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001878 struct shallow_info si;
Jonathan Tan13390782019-03-26 12:31:21 -07001879 struct oid_array shallows_scratch = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001880
1881 fetch_pack_setup();
Junio C Hamanof2db8542013-01-29 14:02:15 -08001882 if (nr_sought)
1883 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001884
Jonathan Tan01775652018-09-27 12:24:05 -07001885 if (version != protocol_v2 && !ref) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001886 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001887 die(_("no matching remote head"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001888 }
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001889 if (version == protocol_v2) {
1890 if (shallow->nr)
1891 BUG("Protocol V2 does not provide shallows at this point in the fetch");
1892 memset(&si, 0, sizeof(si));
Brandon Williams685fbd32018-03-15 10:31:28 -07001893 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
Jonathan Tan13390782019-03-26 12:31:21 -07001894 &shallows_scratch, &si,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001895 pack_lockfiles);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001896 } else {
1897 prepare_shallow_info(&si, shallow);
Brandon Williams685fbd32018-03-15 10:31:28 -07001898 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
Jonathan Tan9da69a62020-06-10 13:57:22 -07001899 &si, pack_lockfiles);
Jonathan Tan1e7d4402019-03-26 12:31:20 -07001900 }
Stefan Bellera49d2832018-03-23 18:45:21 +01001901 reprepare_packed_git(the_repository);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001902
1903 if (!args->cloning && args->deepen) {
1904 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1905 struct ref *iterator = ref_cpy;
1906 opt.shallow_file = alternate_shallow_file;
1907 if (args->deepen)
1908 opt.is_deepening_fetch = 1;
1909 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1910 error(_("remote did not send all necessary objects"));
1911 free_refs(ref_cpy);
1912 ref_cpy = NULL;
Taylor Blau37b9dca2020-04-22 18:25:45 -06001913 rollback_shallow_file(the_repository, &shallow_lock);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001914 goto cleanup;
1915 }
1916 args->connectivity_checked = 1;
1917 }
1918
Jonathan Tane2842b32018-08-01 13:13:20 -07001919 update_shallow(args, sought, nr_sought, &si);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001920cleanup:
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001921 clear_shallow_info(&si);
Jonathan Tan13390782019-03-26 12:31:21 -07001922 oid_array_clear(&shallows_scratch);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001923 return ref_cpy;
1924}
Matt McCutchene860d962017-02-22 11:01:22 -05001925
1926int report_unmatched_refs(struct ref **sought, int nr_sought)
1927{
1928 int i, ret = 0;
1929
1930 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -05001931 if (!sought[i])
Matt McCutchene860d962017-02-22 11:01:22 -05001932 continue;
Matt McCutchend56583d2017-02-22 11:05:57 -05001933 switch (sought[i]->match_status) {
1934 case REF_MATCHED:
1935 continue;
1936 case REF_NOT_MATCHED:
1937 error(_("no such remote ref %s"), sought[i]->name);
1938 break;
1939 case REF_UNADVERTISED_NOT_ALLOWED:
1940 error(_("Server does not allow request for unadvertised object %s"),
1941 sought[i]->name);
1942 break;
1943 }
Matt McCutchene860d962017-02-22 11:01:22 -05001944 ret = 1;
1945 }
1946 return ret;
1947}