blob: 88a078e9befd281cf5f03e9e64615b14ca768a35 [file] [log] [blame]
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001#include "cache.h"
Stefan Bellera49d2832018-03-23 18:45:21 +01002#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02004#include "lockfile.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07005#include "refs.h"
6#include "pkt-line.h"
7#include "commit.h"
8#include "tag.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07009#include "exec-cmd.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070010#include "pack.h"
11#include "sideband.h"
12#include "fetch-pack.h"
13#include "remote.h"
14#include "run-command.h"
Junio C Hamano47a59182013-07-08 13:56:53 -070015#include "connect.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070016#include "transport.h"
17#include "version.h"
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +070018#include "sha1-array.h"
Jonathan Tanfdb69d32017-05-15 10:32:20 -070019#include "oidset.h"
Jonathan Tan0abe14f2017-08-18 15:20:26 -070020#include "packfile.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070021#include "object-store.h"
Jonathan Tancf1e7c02018-07-02 15:08:43 -070022#include "connected.h"
Jonathan Tanec062832018-06-14 15:54:28 -070023#include "fetch-negotiator.h"
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +000024#include "fsck.h"
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070025
26static int transfer_unpack_limit = -1;
27static int fetch_unpack_limit = -1;
28static int unpack_limit = 100;
29static int prefer_ofs_delta = 1;
30static int no_done;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070031static int deepen_since_ok;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070032static int deepen_not_ok;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070033static int fetch_fsck_objects = -1;
34static int transfer_fsck_objects = -1;
35static int agent_supported;
Jeff Hostetler640d8b72017-12-08 15:58:40 +000036static int server_supports_filtering;
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +070037static struct lock_file shallow_lock;
38static const char *alternate_shallow_file;
Jonathan Tan42cc7482018-07-16 11:44:01 -070039static char *negotiation_algorithm;
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +000040static struct strbuf fsck_msg_types = STRBUF_INIT;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070041
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070042/* Remember to update object flag allocation in object.h */
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070043#define COMPLETE (1U << 0)
Jonathan Tanec062832018-06-14 15:54:28 -070044#define ALTERNATE (1U << 1)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070045
46/*
47 * After sending this many "have"s if we do not get any new ACK , we
48 * give up traversing our history.
49 */
50#define MAX_IN_VAIN 256
51
Jonathan Tand30fe892018-06-14 15:54:26 -070052static int multi_ack, use_sideband;
Fredrik Medley7199c092015-05-21 22:23:38 +020053/* Allow specifying sha1 if it is a ref tip. */
54#define ALLOW_TIP_SHA1 01
Fredrik Medley68ee6282015-05-21 22:23:39 +020055/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
56#define ALLOW_REACHABLE_SHA1 02
Fredrik Medley7199c092015-05-21 22:23:38 +020057static unsigned int allow_unadvertised_object_request;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +070058
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +070059__attribute__((format (printf, 2, 3)))
60static inline void print_verbose(const struct fetch_pack_args *args,
61 const char *fmt, ...)
62{
63 va_list params;
64
65 if (!args->verbose)
66 return;
67
68 va_start(params, fmt);
69 vfprintf(stderr, fmt, params);
70 va_end(params);
71 fputc('\n', stderr);
72}
73
Jeff King41a078c2017-02-08 15:53:03 -050074struct alternate_object_cache {
75 struct object **items;
76 size_t nr, alloc;
77};
78
79static void cache_one_alternate(const char *refname,
80 const struct object_id *oid,
81 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 Tanec062832018-06-14 15:54:28 -0700111static int rev_list_insert_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700112 const char *refname,
113 const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700114{
Stefan Bellera74093d2018-06-28 18:22:05 -0700115 struct object *o = deref_tag(the_repository,
116 parse_object(the_repository, oid),
Stefan Beller109cd762018-06-28 18:21:51 -0700117 refname, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700118
119 if (o && o->type == OBJ_COMMIT)
Jonathan Tanec062832018-06-14 15:54:28 -0700120 negotiator->add_tip(negotiator, (struct commit *)o);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700121
122 return 0;
123}
124
Michael Haggertyb1b49c62015-05-25 18:39:18 +0000125static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid,
126 int flag, void *cb_data)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700127{
Jonathan Tand30fe892018-06-14 15:54:26 -0700128 return rev_list_insert_ref(cb_data, refname, oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700129}
130
131enum ack_type {
132 NAK = 0,
133 ACK,
134 ACK_continue,
135 ACK_common,
136 ACK_ready
137};
138
139static void consume_shallow_list(struct fetch_pack_args *args, int fd)
140{
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700141 if (args->stateless_rpc && args->deepen) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700142 /* If we sent a depth we will get back "duplicate"
143 * shallow and unshallow commands every time there
144 * is a block of have lines exchanged.
145 */
Jeff King74543a02013-02-20 15:02:57 -0500146 char *line;
147 while ((line = packet_read_line(fd, NULL))) {
Christian Couder59556542013-11-30 21:55:40 +0100148 if (starts_with(line, "shallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700149 continue;
Christian Couder59556542013-11-30 21:55:40 +0100150 if (starts_with(line, "unshallow "))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700151 continue;
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700152 die(_("git fetch-pack: expected shallow list"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700153 }
154 }
155}
156
brian m. carlson1b283372017-05-01 02:28:54 +0000157static enum ack_type get_ack(int fd, struct object_id *result_oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700158{
Jeff King74543a02013-02-20 15:02:57 -0500159 int len;
160 char *line = packet_read_line(fd, &len);
Jeff King82e56762014-06-18 15:56:03 -0400161 const char *arg;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700162
Jeff Kingbc9d4dc2018-02-08 13:47:49 -0500163 if (!line)
164 die(_("git fetch-pack: expected ACK/NAK, got a flush packet"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700165 if (!strcmp(line, "NAK"))
166 return NAK;
Jeff King82e56762014-06-18 15:56:03 -0400167 if (skip_prefix(line, "ACK ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000168 if (!get_oid_hex(arg, result_oid)) {
Jeff King82e56762014-06-18 15:56:03 -0400169 arg += 40;
170 len -= arg - line;
171 if (len < 1)
Jeff King030e9dd2013-02-20 15:00:28 -0500172 return ACK;
Jeff King82e56762014-06-18 15:56:03 -0400173 if (strstr(arg, "continue"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700174 return ACK_continue;
Jeff King82e56762014-06-18 15:56:03 -0400175 if (strstr(arg, "common"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700176 return ACK_common;
Jeff King82e56762014-06-18 15:56:03 -0400177 if (strstr(arg, "ready"))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700178 return ACK_ready;
179 return ACK;
180 }
181 }
Jonathan Tan8e2c7be2017-04-12 11:06:02 -0700182 if (skip_prefix(line, "ERR ", &arg))
183 die(_("remote error: %s"), arg);
Ralf Thielowdfbfb9f2016-11-11 18:21:00 +0100184 die(_("git fetch-pack: expected ACK/NAK, got '%s'"), line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700185}
186
187static void send_request(struct fetch_pack_args *args,
188 int fd, struct strbuf *buf)
189{
190 if (args->stateless_rpc) {
191 send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX);
192 packet_flush(fd);
193 } else
Jeff Kingcdf4fb82013-02-20 15:01:56 -0500194 write_or_die(fd, buf->buf, buf->len);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700195}
196
Jonathan Tanec062832018-06-14 15:54:28 -0700197static void insert_one_alternate_object(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700198 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700199{
Jonathan Tanec062832018-06-14 15:54:28 -0700200 rev_list_insert_ref(negotiator, NULL, &obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700201}
202
203#define INITIAL_FLUSH 16
204#define PIPESAFE_FLUSH 32
Jonathan Tanda470982016-07-18 15:21:38 -0700205#define LARGE_FLUSH 16384
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700206
Brandon Williams685fbd32018-03-15 10:31:28 -0700207static int next_flush(int stateless_rpc, int count)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700208{
Brandon Williams685fbd32018-03-15 10:31:28 -0700209 if (stateless_rpc) {
Jonathan Tanda470982016-07-18 15:21:38 -0700210 if (count < LARGE_FLUSH)
211 count <<= 1;
212 else
213 count = count * 11 / 10;
214 } else {
215 if (count < PIPESAFE_FLUSH)
216 count <<= 1;
217 else
218 count += PIPESAFE_FLUSH;
219 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700220 return count;
221}
222
Jonathan Tan3390e422018-07-02 15:39:44 -0700223static void mark_tips(struct fetch_negotiator *negotiator,
224 const struct oid_array *negotiation_tips)
225{
226 int i;
227
228 if (!negotiation_tips) {
229 for_each_ref(rev_list_insert_ref_oid, negotiator);
230 return;
231 }
232
233 for (i = 0; i < negotiation_tips->nr; i++)
234 rev_list_insert_ref(negotiator, NULL,
235 &negotiation_tips->oid[i]);
236 return;
237}
238
Jonathan Tanec062832018-06-14 15:54:28 -0700239static int find_common(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700240 struct fetch_pack_args *args,
brian m. carlson1b283372017-05-01 02:28:54 +0000241 int fd[2], struct object_id *result_oid,
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700242 struct ref *refs)
243{
244 int fetching;
245 int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval;
brian m. carlson1b283372017-05-01 02:28:54 +0000246 const struct object_id *oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700247 unsigned in_vain = 0;
248 int got_continue = 0;
249 int got_ready = 0;
250 struct strbuf req_buf = STRBUF_INIT;
251 size_t state_len = 0;
252
253 if (args->stateless_rpc && multi_ack == 1)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700254 die(_("--stateless-rpc requires multi_ack_detailed"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700255
Jonathan Tan3390e422018-07-02 15:39:44 -0700256 mark_tips(negotiator, args->negotiation_tips);
Jonathan Tanec062832018-06-14 15:54:28 -0700257 for_each_cached_alternate(negotiator, insert_one_alternate_object);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700258
259 fetching = 0;
260 for ( ; refs ; refs = refs->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000261 struct object_id *remote = &refs->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700262 const char *remote_hex;
263 struct object *o;
264
265 /*
266 * If that object is complete (i.e. it is an ancestor of a
267 * local ref), we tell them we have it but do not have to
268 * tell them about its ancestors, which they already know
269 * about.
270 *
271 * We use lookup_object here because we are only
272 * interested in the case we *know* the object is
273 * reachable and we have already scanned it.
274 */
Stefan Beller5abddd12018-06-28 18:21:52 -0700275 if (((o = lookup_object(the_repository, remote->hash)) != NULL) &&
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700276 (o->flags & COMPLETE)) {
277 continue;
278 }
279
brian m. carlson1b283372017-05-01 02:28:54 +0000280 remote_hex = oid_to_hex(remote);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700281 if (!fetching) {
282 struct strbuf c = STRBUF_INIT;
283 if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed");
284 if (multi_ack == 1) strbuf_addstr(&c, " multi_ack");
285 if (no_done) strbuf_addstr(&c, " no-done");
286 if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k");
287 if (use_sideband == 1) strbuf_addstr(&c, " side-band");
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700288 if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700289 if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack");
290 if (args->no_progress) strbuf_addstr(&c, " no-progress");
291 if (args->include_tag) strbuf_addstr(&c, " include-tag");
292 if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta");
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700293 if (deepen_since_ok) strbuf_addstr(&c, " deepen-since");
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700294 if (deepen_not_ok) strbuf_addstr(&c, " deepen-not");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700295 if (agent_supported) strbuf_addf(&c, " agent=%s",
296 git_user_agent_sanitized());
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000297 if (args->filter_options.choice)
298 strbuf_addstr(&c, " filter");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700299 packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf);
300 strbuf_release(&c);
301 } else
302 packet_buf_write(&req_buf, "want %s\n", remote_hex);
303 fetching++;
304 }
305
306 if (!fetching) {
307 strbuf_release(&req_buf);
308 packet_flush(fd[1]);
309 return 1;
310 }
311
Stefan Bellerc8813482018-05-17 15:51:46 -0700312 if (is_repository_shallow(the_repository))
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +0700313 write_shallow_commits(&req_buf, 1, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700314 if (args->depth > 0)
315 packet_buf_write(&req_buf, "deepen %d", args->depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700316 if (args->deepen_since) {
Johannes Schindelindddbad72017-04-26 21:29:31 +0200317 timestamp_t max_age = approxidate(args->deepen_since);
Johannes Schindelincb71f8b2017-04-21 12:45:48 +0200318 packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700319 }
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700320 if (args->deepen_not) {
321 int i;
322 for (i = 0; i < args->deepen_not->nr; i++) {
323 struct string_list_item *s = args->deepen_not->items + i;
324 packet_buf_write(&req_buf, "deepen-not %s", s->string);
325 }
326 }
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000327 if (server_supports_filtering && args->filter_options.choice)
328 packet_buf_write(&req_buf, "filter %s",
329 args->filter_options.filter_spec);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700330 packet_buf_flush(&req_buf);
331 state_len = req_buf.len;
332
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700333 if (args->deepen) {
Jeff King74543a02013-02-20 15:02:57 -0500334 char *line;
Jeff Kingae021d82014-06-18 15:47:50 -0400335 const char *arg;
brian m. carlson1b283372017-05-01 02:28:54 +0000336 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700337
338 send_request(args, fd[1], &req_buf);
Jeff King74543a02013-02-20 15:02:57 -0500339 while ((line = packet_read_line(fd[0], NULL))) {
Jeff Kingae021d82014-06-18 15:47:50 -0400340 if (skip_prefix(line, "shallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000341 if (get_oid_hex(arg, &oid))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700342 die(_("invalid shallow line: %s"), line);
Stefan Beller19143f12018-05-17 15:51:44 -0700343 register_shallow(the_repository, &oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700344 continue;
345 }
Jeff Kingae021d82014-06-18 15:47:50 -0400346 if (skip_prefix(line, "unshallow ", &arg)) {
brian m. carlson1b283372017-05-01 02:28:54 +0000347 if (get_oid_hex(arg, &oid))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700348 die(_("invalid unshallow line: %s"), line);
Stefan Beller5abddd12018-06-28 18:21:52 -0700349 if (!lookup_object(the_repository, oid.hash))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700350 die(_("object not found: %s"), line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700351 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -0700352 if (!parse_object(the_repository, &oid))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700353 die(_("error in object: %s"), line);
brian m. carlsone92b8482017-05-06 22:10:06 +0000354 if (unregister_shallow(&oid))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700355 die(_("no shallow found: %s"), line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700356 continue;
357 }
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700358 die(_("expected shallow/unshallow, got %s"), line);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700359 }
360 } else if (!args->stateless_rpc)
361 send_request(args, fd[1], &req_buf);
362
363 if (!args->stateless_rpc) {
364 /* If we aren't using the stateless-rpc interface
365 * we don't need to retain the headers.
366 */
367 strbuf_setlen(&req_buf, 0);
368 state_len = 0;
369 }
370
371 flushes = 0;
372 retval = -1;
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000373 if (args->no_dependents)
374 goto done;
Jonathan Tanec062832018-06-14 15:54:28 -0700375 while ((oid = negotiator->next(negotiator))) {
brian m. carlson1b283372017-05-01 02:28:54 +0000376 packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid));
377 print_verbose(args, "have %s", oid_to_hex(oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700378 in_vain++;
379 if (flush_at <= ++count) {
380 int ack;
381
382 packet_buf_flush(&req_buf);
383 send_request(args, fd[1], &req_buf);
384 strbuf_setlen(&req_buf, state_len);
385 flushes++;
Brandon Williams685fbd32018-03-15 10:31:28 -0700386 flush_at = next_flush(args->stateless_rpc, count);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700387
388 /*
389 * We keep one window "ahead" of the other side, and
390 * will wait for an ACK only on the next one
391 */
392 if (!args->stateless_rpc && count == INITIAL_FLUSH)
393 continue;
394
395 consume_shallow_list(args, fd[0]);
396 do {
brian m. carlson1b283372017-05-01 02:28:54 +0000397 ack = get_ack(fd[0], result_oid);
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700398 if (ack)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700399 print_verbose(args, _("got %s %d %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000400 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700401 switch (ack) {
402 case ACK:
403 flushes = 0;
404 multi_ack = 0;
405 retval = 0;
406 goto done;
407 case ACK_common:
408 case ACK_ready:
409 case ACK_continue: {
410 struct commit *commit =
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700411 lookup_commit(the_repository,
412 result_oid);
Jonathan Tand093bc72018-06-14 15:54:27 -0700413 int was_common;
Junio C Hamano3a2a1dc2018-08-02 15:30:42 -0700414
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700415 if (!commit)
brian m. carlson1b283372017-05-01 02:28:54 +0000416 die(_("invalid commit %s"), oid_to_hex(result_oid));
Jonathan Tanec062832018-06-14 15:54:28 -0700417 was_common = negotiator->ack(negotiator, commit);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700418 if (args->stateless_rpc
419 && ack == ACK_common
Jonathan Tand093bc72018-06-14 15:54:27 -0700420 && !was_common) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700421 /* We need to replay the have for this object
422 * on the next RPC request so the peer knows
423 * it is in common with us.
424 */
brian m. carlson1b283372017-05-01 02:28:54 +0000425 const char *hex = oid_to_hex(result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700426 packet_buf_write(&req_buf, "have %s\n", hex);
427 state_len = req_buf.len;
Jonathan Tan06b3d382016-09-23 10:41:35 -0700428 /*
429 * Reset in_vain because an ack
430 * for this commit has not been
431 * seen.
432 */
433 in_vain = 0;
434 } else if (!args->stateless_rpc
435 || ack != ACK_common)
436 in_vain = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700437 retval = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700438 got_continue = 1;
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700439 if (ack == ACK_ready)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700440 got_ready = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700441 break;
442 }
443 }
444 } while (ack);
445 flushes--;
446 if (got_continue && MAX_IN_VAIN < in_vain) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700447 print_verbose(args, _("giving up"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700448 break; /* give up */
449 }
Jonathan Tan21bcf6e2018-06-14 15:54:24 -0700450 if (got_ready)
451 break;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700452 }
453 }
454done:
455 if (!got_ready || !no_done) {
456 packet_buf_write(&req_buf, "done\n");
457 send_request(args, fd[1], &req_buf);
458 }
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700459 print_verbose(args, _("done"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700460 if (retval != 0) {
461 multi_ack = 0;
462 flushes++;
463 }
464 strbuf_release(&req_buf);
465
Nguyễn Thái Ngọc Duyff62eca2014-02-06 22:10:39 +0700466 if (!got_ready || !no_done)
467 consume_shallow_list(args, fd[0]);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700468 while (flushes || multi_ack) {
brian m. carlson1b283372017-05-01 02:28:54 +0000469 int ack = get_ack(fd[0], result_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700470 if (ack) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700471 print_verbose(args, _("got %s (%d) %s"), "ack",
brian m. carlson1b283372017-05-01 02:28:54 +0000472 ack, oid_to_hex(result_oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700473 if (ack == ACK)
474 return 0;
475 multi_ack = 1;
476 continue;
477 }
478 flushes--;
479 }
480 /* it is no error to fetch into a completely empty repo */
481 return count ? retval : 0;
482}
483
484static struct commit_list *complete;
485
brian m. carlson1b283372017-05-01 02:28:54 +0000486static int mark_complete(const struct object_id *oid)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700487{
Stefan Beller109cd762018-06-28 18:21:51 -0700488 struct object *o = parse_object(the_repository, oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700489
490 while (o && o->type == OBJ_TAG) {
491 struct tag *t = (struct tag *) o;
492 if (!t->tagged)
493 break; /* broken repository */
494 o->flags |= COMPLETE;
Stefan Beller109cd762018-06-28 18:21:51 -0700495 o = parse_object(the_repository, &t->tagged->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700496 }
497 if (o && o->type == OBJ_COMMIT) {
498 struct commit *commit = (struct commit *)o;
499 if (!(commit->object.flags & COMPLETE)) {
500 commit->object.flags |= COMPLETE;
Jeff King16445242013-07-02 02:16:23 -0400501 commit_list_insert(commit, &complete);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700502 }
503 }
504 return 0;
505}
506
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000507static int mark_complete_oid(const char *refname, const struct object_id *oid,
508 int flag, void *cb_data)
509{
brian m. carlson1b283372017-05-01 02:28:54 +0000510 return mark_complete(oid);
Michael Haggertyf8ee4d82015-05-25 18:39:16 +0000511}
512
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700513static void mark_recent_complete_commits(struct fetch_pack_args *args,
Johannes Schindelindddbad72017-04-26 21:29:31 +0200514 timestamp_t cutoff)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700515{
516 while (complete && cutoff <= complete->item->date) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700517 print_verbose(args, _("Marking %s as complete"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700518 oid_to_hex(&complete->item->object.oid));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700519 pop_most_recent_commit(&complete, COMPLETE);
520 }
521}
522
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700523static void add_refs_to_oidset(struct oidset *oids, struct ref *refs)
524{
525 for (; refs; refs = refs->next)
526 oidset_insert(oids, &refs->old_oid);
527}
528
529static int tip_oids_contain(struct oidset *tip_oids,
530 struct ref *unmatched, struct ref *newlist,
531 const struct object_id *id)
532{
533 /*
534 * Note that this only looks at the ref lists the first time it's
535 * called. This works out in filter_refs() because even though it may
536 * add to "newlist" between calls, the additions will always be for
537 * oids that are already in the set.
538 */
Jonathan Tan9e6fabde2017-09-29 15:54:22 -0700539 if (!tip_oids->map.map.tablesize) {
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700540 add_refs_to_oidset(tip_oids, unmatched);
541 add_refs_to_oidset(tip_oids, newlist);
542 }
543 return oidset_contains(tip_oids, id);
544}
545
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700546static void filter_refs(struct fetch_pack_args *args,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800547 struct ref **refs,
548 struct ref **sought, int nr_sought)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700549{
550 struct ref *newlist = NULL;
551 struct ref **newtail = &newlist;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700552 struct ref *unmatched = NULL;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700553 struct ref *ref, *next;
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700554 struct oidset tip_oids = OIDSET_INIT;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800555 int i;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700556
Junio C Hamanof2db8542013-01-29 14:02:15 -0800557 i = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700558 for (ref = *refs; ref; ref = next) {
559 int keep = 0;
560 next = ref->next;
Junio C Hamanof2db8542013-01-29 14:02:15 -0800561
René Scharfe50e19a82014-06-06 19:24:48 +0200562 if (starts_with(ref->name, "refs/") &&
Jeff King4c224082014-01-15 05:46:13 -0500563 check_refname_format(ref->name, 0))
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700564 ; /* trash */
565 else {
Junio C Hamanof2db8542013-01-29 14:02:15 -0800566 while (i < nr_sought) {
567 int cmp = strcmp(ref->name, sought[i]->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700568 if (cmp < 0)
569 break; /* definitely do not have it */
570 else if (cmp == 0) {
571 keep = 1; /* definitely have it */
Matt McCutchend56583d2017-02-22 11:05:57 -0500572 sought[i]->match_status = REF_MATCHED;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700573 }
Junio C Hamanof2db8542013-01-29 14:02:15 -0800574 i++;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700575 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700576
Jeff Kinge9502c02018-06-11 01:53:57 -0400577 if (!keep && args->fetch_all &&
578 (!args->deepen || !starts_with(ref->name, "refs/tags/")))
579 keep = 1;
580 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700581
582 if (keep) {
583 *newtail = ref;
584 ref->next = NULL;
585 newtail = &ref->next;
586 } else {
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700587 ref->next = unmatched;
588 unmatched = ref;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700589 }
590 }
591
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800592 /* Append unmatched requests to the list */
Matt McCutchend56583d2017-02-22 11:05:57 -0500593 for (i = 0; i < nr_sought; i++) {
brian m. carlson1b283372017-05-01 02:28:54 +0000594 struct object_id oid;
595 const char *p;
Jeff Kingb7916422015-03-19 16:34:51 -0400596
Matt McCutchend56583d2017-02-22 11:05:57 -0500597 ref = sought[i];
598 if (ref->match_status != REF_NOT_MATCHED)
599 continue;
brian m. carlson1b283372017-05-01 02:28:54 +0000600 if (parse_oid_hex(ref->name, &oid, &p) ||
601 *p != '\0' ||
602 oidcmp(&oid, &ref->old_oid))
Matt McCutchend56583d2017-02-22 11:05:57 -0500603 continue;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800604
Matt McCutchend56583d2017-02-22 11:05:57 -0500605 if ((allow_unadvertised_object_request &
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700606 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)) ||
607 tip_oids_contain(&tip_oids, unmatched, newlist,
608 &ref->old_oid)) {
Matt McCutchend56583d2017-02-22 11:05:57 -0500609 ref->match_status = REF_MATCHED;
Jeff Kingc3c17bf2015-03-19 16:37:09 -0400610 *newtail = copy_ref(ref);
611 newtail = &(*newtail)->next;
Matt McCutchend56583d2017-02-22 11:05:57 -0500612 } else {
613 ref->match_status = REF_UNADVERTISED_NOT_ALLOWED;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800614 }
615 }
Jonathan Tanfdb69d32017-05-15 10:32:20 -0700616
617 oidset_clear(&tip_oids);
618 for (ref = unmatched; ref; ref = next) {
619 next = ref->next;
620 free(ref);
621 }
622
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700623 *refs = newlist;
624}
625
Jonathan Tanec062832018-06-14 15:54:28 -0700626static void mark_alternate_complete(struct fetch_negotiator *unused,
Jonathan Tand30fe892018-06-14 15:54:26 -0700627 struct object *obj)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700628{
brian m. carlson1b283372017-05-01 02:28:54 +0000629 mark_complete(&obj->oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700630}
631
Takuto Ikuta024aa462018-03-14 15:32:42 +0900632struct loose_object_iter {
633 struct oidset *loose_object_set;
634 struct ref *refs;
635};
636
637/*
638 * If the number of refs is not larger than the number of loose objects,
639 * this function stops inserting.
640 */
641static int add_loose_objects_to_set(const struct object_id *oid,
642 const char *path,
643 void *data)
644{
645 struct loose_object_iter *iter = data;
646 oidset_insert(iter->loose_object_set, oid);
647 if (iter->refs == NULL)
648 return 1;
649
650 iter->refs = iter->refs->next;
651 return 0;
652}
653
Jonathan Tan34c29032018-06-06 13:47:07 -0700654/*
655 * Mark recent commits available locally and reachable from a local ref as
656 * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as
657 * COMMON_REF (otherwise, we are not planning to participate in negotiation, and
658 * thus do not need COMMON_REF marks).
659 *
660 * The cutoff time for recency is determined by this heuristic: it is the
661 * earliest commit time of the objects in refs that are commits and that we know
662 * the commit time of.
663 */
Jonathan Tanec062832018-06-14 15:54:28 -0700664static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -0700665 struct fetch_pack_args *args,
Jonathan Tan34c29032018-06-06 13:47:07 -0700666 struct ref **refs)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700667{
668 struct ref *ref;
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000669 int old_save_commit_buffer = save_commit_buffer;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200670 timestamp_t cutoff = 0;
Takuto Ikuta024aa462018-03-14 15:32:42 +0900671 struct oidset loose_oid_set = OIDSET_INIT;
672 int use_oidset = 0;
673 struct loose_object_iter iter = {&loose_oid_set, *refs};
674
675 /* Enumerate all loose objects or know refs are not so many. */
676 use_oidset = !for_each_loose_object(add_loose_objects_to_set,
677 &iter, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700678
679 save_commit_buffer = 0;
680
681 for (ref = *refs; ref; ref = ref->next) {
682 struct object *o;
Takuto Ikuta024aa462018-03-14 15:32:42 +0900683 unsigned int flags = OBJECT_INFO_QUICK;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700684
Takuto Ikuta024aa462018-03-14 15:32:42 +0900685 if (use_oidset &&
686 !oidset_contains(&loose_oid_set, &ref->old_oid)) {
687 /*
688 * I know this does not exist in the loose form,
689 * so check if it exists in a non-loose form.
690 */
691 flags |= OBJECT_INFO_IGNORE_LOOSE;
692 }
693
694 if (!has_object_file_with_flags(&ref->old_oid, flags))
Junio C Hamano012a1bb2013-01-26 19:42:09 -0800695 continue;
Stefan Beller109cd762018-06-28 18:21:51 -0700696 o = parse_object(the_repository, &ref->old_oid);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700697 if (!o)
698 continue;
699
700 /* We already have it -- which may mean that we were
701 * in sync with the other side at some time after
702 * that (it is OK if we guess wrong here).
703 */
704 if (o->type == OBJ_COMMIT) {
705 struct commit *commit = (struct commit *)o;
706 if (!cutoff || cutoff < commit->date)
707 cutoff = commit->date;
708 }
709 }
710
Takuto Ikuta024aa462018-03-14 15:32:42 +0900711 oidset_clear(&loose_oid_set);
712
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000713 if (!args->no_dependents) {
714 if (!args->deepen) {
715 for_each_ref(mark_complete_oid, NULL);
Jonathan Tand30fe892018-06-14 15:54:26 -0700716 for_each_cached_alternate(NULL, mark_alternate_complete);
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000717 commit_list_sort_by_date(&complete);
718 if (cutoff)
719 mark_recent_complete_commits(args, cutoff);
720 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700721
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000722 /*
723 * Mark all complete remote refs as common refs.
724 * Don't mark them common yet; the server has to be told so first.
725 */
726 for (ref = *refs; ref; ref = ref->next) {
Stefan Bellera74093d2018-06-28 18:22:05 -0700727 struct object *o = deref_tag(the_repository,
728 lookup_object(the_repository,
Stefan Beller5abddd12018-06-28 18:21:52 -0700729 ref->old_oid.hash),
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000730 NULL, 0);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700731
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000732 if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE))
733 continue;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700734
Jonathan Tanec062832018-06-14 15:54:28 -0700735 negotiator->known_common(negotiator,
736 (struct commit *)o);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700737 }
738 }
739
Jonathan Tan34c29032018-06-06 13:47:07 -0700740 save_commit_buffer = old_save_commit_buffer;
741}
742
743/*
744 * Returns 1 if every object pointed to by the given remote refs is available
745 * locally and reachable from a local ref, and 0 otherwise.
746 */
747static int everything_local(struct fetch_pack_args *args,
748 struct ref **refs)
749{
750 struct ref *ref;
751 int retval;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700752
753 for (retval = 1, ref = *refs; ref ; ref = ref->next) {
brian m. carlson1b283372017-05-01 02:28:54 +0000754 const struct object_id *remote = &ref->old_oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700755 struct object *o;
756
Stefan Beller5abddd12018-06-28 18:21:52 -0700757 o = lookup_object(the_repository, remote->hash);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700758 if (!o || !(o->flags & COMPLETE)) {
759 retval = 0;
brian m. carlson1b283372017-05-01 02:28:54 +0000760 print_verbose(args, "want %s (%s)", oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700761 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700762 continue;
763 }
brian m. carlson1b283372017-05-01 02:28:54 +0000764 print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700765 ref->name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700766 }
Jonathan Tana1c6d7c2017-12-08 15:58:48 +0000767
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700768 return retval;
769}
770
771static int sideband_demux(int in, int out, void *data)
772{
773 int *xd = data;
Jeff King9ff18fa2016-02-24 02:44:58 -0500774 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700775
Jeff King9ff18fa2016-02-24 02:44:58 -0500776 ret = recv_sideband("fetch-pack", xd[0], out);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700777 close(out);
778 return ret;
779}
780
781static int get_pack(struct fetch_pack_args *args,
782 int xd[2], char **pack_lockfile)
783{
784 struct async demux;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700785 int do_keep = args->keep_pack;
Jeff King984a43b2015-09-24 17:07:54 -0400786 const char *cmd_name;
787 struct pack_header header;
788 int pass_header = 0;
René Scharfed3180272014-08-19 21:09:35 +0200789 struct child_process cmd = CHILD_PROCESS_INIT;
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700790 int ret;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700791
792 memset(&demux, 0, sizeof(demux));
793 if (use_sideband) {
794 /* xd[] is talking with upload-pack; subprocess reads from
795 * xd[0], spits out band#2 to stderr, and feeds us band#1
796 * through demux->out.
797 */
798 demux.proc = sideband_demux;
799 demux.data = xd;
800 demux.out = -1;
Jeff Kingdf857572016-04-19 18:50:29 -0400801 demux.isolate_sigpipe = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700802 if (start_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700803 die(_("fetch-pack: unable to fork off sideband demultiplexer"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700804 }
805 else
806 demux.out = xd[0];
807
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700808 if (!args->keep_pack && unpack_limit) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700809
810 if (read_pack_header(demux.out, &header))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700811 die(_("protocol error: bad pack header"));
Jeff King984a43b2015-09-24 17:07:54 -0400812 pass_header = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700813 if (ntohl(header.hdr_entries) < unpack_limit)
814 do_keep = 0;
815 else
816 do_keep = 1;
817 }
818
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700819 if (alternate_shallow_file) {
Jeff King984a43b2015-09-24 17:07:54 -0400820 argv_array_push(&cmd.args, "--shallow-file");
821 argv_array_push(&cmd.args, alternate_shallow_file);
Nguyễn Thái Ngọc Duy6035d6a2013-05-26 08:16:15 +0700822 }
823
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000824 if (do_keep || args->from_promisor) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700825 if (pack_lockfile)
826 cmd.out = -1;
Jeff King984a43b2015-09-24 17:07:54 -0400827 cmd_name = "index-pack";
828 argv_array_push(&cmd.args, cmd_name);
829 argv_array_push(&cmd.args, "--stdin");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700830 if (!args->quiet && !args->no_progress)
Jeff King984a43b2015-09-24 17:07:54 -0400831 argv_array_push(&cmd.args, "-v");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700832 if (args->use_thin_pack)
Jeff King984a43b2015-09-24 17:07:54 -0400833 argv_array_push(&cmd.args, "--fix-thin");
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000834 if (do_keep && (args->lock_pack || unpack_limit)) {
René Scharfeda25bdb2017-04-18 17:57:42 -0400835 char hostname[HOST_NAME_MAX + 1];
David Turner5781a9a2017-04-18 17:57:43 -0400836 if (xgethostname(hostname, sizeof(hostname)))
Jeff King984a43b2015-09-24 17:07:54 -0400837 xsnprintf(hostname, sizeof(hostname), "localhost");
838 argv_array_pushf(&cmd.args,
839 "--keep=fetch-pack %"PRIuMAX " on %s",
840 (uintmax_t)getpid(), hostname);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700841 }
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700842 if (args->check_self_contained_and_connected)
Jeff King984a43b2015-09-24 17:07:54 -0400843 argv_array_push(&cmd.args, "--check-self-contained-and-connected");
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000844 if (args->from_promisor)
845 argv_array_push(&cmd.args, "--promisor");
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700846 }
847 else {
Jeff King984a43b2015-09-24 17:07:54 -0400848 cmd_name = "unpack-objects";
849 argv_array_push(&cmd.args, cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700850 if (args->quiet || args->no_progress)
Jeff King984a43b2015-09-24 17:07:54 -0400851 argv_array_push(&cmd.args, "-q");
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700852 args->check_self_contained_and_connected = 0;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700853 }
Jeff King984a43b2015-09-24 17:07:54 -0400854
855 if (pass_header)
856 argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32,
857 ntohl(header.hdr_version),
858 ntohl(header.hdr_entries));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700859 if (fetch_fsck_objects >= 0
860 ? fetch_fsck_objects
861 : transfer_fsck_objects >= 0
862 ? transfer_fsck_objects
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700863 : 0) {
864 if (args->from_promisor)
865 /*
866 * We cannot use --strict in index-pack because it
867 * checks both broken objects and links, but we only
868 * want to check for broken objects.
869 */
870 argv_array_push(&cmd.args, "--fsck-objects");
871 else
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +0000872 argv_array_pushf(&cmd.args, "--strict%s",
873 fsck_msg_types.buf);
Jonathan Tan98a2ea42018-03-14 11:42:41 -0700874 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700875
876 cmd.in = demux.out;
877 cmd.git_cmd = 1;
878 if (start_command(&cmd))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700879 die(_("fetch-pack: unable to fork off %s"), cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700880 if (do_keep && pack_lockfile) {
881 *pack_lockfile = index_pack_lockfile(cmd.out);
882 close(cmd.out);
883 }
884
Jens Lindstrom37cb1dd2013-10-22 15:36:02 +0200885 if (!use_sideband)
886 /* Closed by start_command() */
887 xd[0] = -1;
888
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700889 ret = finish_command(&cmd);
890 if (!ret || (args->check_self_contained_and_connected && ret == 1))
891 args->self_contained_and_connected =
892 args->check_self_contained_and_connected &&
893 ret == 0;
894 else
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700895 die(_("%s failed"), cmd_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700896 if (use_sideband && finish_async(&demux))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700897 die(_("error in sideband demultiplexer"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700898 return 0;
899}
900
Junio C Hamanof2db8542013-01-29 14:02:15 -0800901static int cmp_ref_by_name(const void *a_, const void *b_)
902{
903 const struct ref *a = *((const struct ref **)a_);
904 const struct ref *b = *((const struct ref **)b_);
905 return strcmp(a->name, b->name);
906}
907
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700908static struct ref *do_fetch_pack(struct fetch_pack_args *args,
909 int fd[2],
910 const struct ref *orig_ref,
Junio C Hamanof2db8542013-01-29 14:02:15 -0800911 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +0700912 struct shallow_info *si,
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700913 char **pack_lockfile)
914{
915 struct ref *ref = copy_ref_list(orig_ref);
brian m. carlson1b283372017-05-01 02:28:54 +0000916 struct object_id oid;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700917 const char *agent_feature;
918 int agent_len;
Jonathan Tanec062832018-06-14 15:54:28 -0700919 struct fetch_negotiator negotiator;
Jonathan Tan42cc7482018-07-16 11:44:01 -0700920 fetch_negotiator_init(&negotiator, negotiation_algorithm);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700921
922 sort_ref_list(&ref, ref_compare_name);
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200923 QSORT(sought, nr_sought, cmp_ref_by_name);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700924
Stefan Bellerc8813482018-05-17 15:51:46 -0700925 if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow"))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700926 die(_("Server does not support shallow clients"));
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700927 if (args->depth > 0 || args->deepen_since || args->deepen_not)
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +0700928 args->deepen = 1;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700929 if (server_supports("multi_ack_detailed")) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700930 print_verbose(args, _("Server supports multi_ack_detailed"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700931 multi_ack = 2;
932 if (server_supports("no-done")) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700933 print_verbose(args, _("Server supports no-done"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700934 if (args->stateless_rpc)
935 no_done = 1;
936 }
937 }
938 else if (server_supports("multi_ack")) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700939 print_verbose(args, _("Server supports multi_ack"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700940 multi_ack = 1;
941 }
942 if (server_supports("side-band-64k")) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700943 print_verbose(args, _("Server supports side-band-64k"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700944 use_sideband = 2;
945 }
946 else if (server_supports("side-band")) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700947 print_verbose(args, _("Server supports side-band"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700948 use_sideband = 1;
949 }
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800950 if (server_supports("allow-tip-sha1-in-want")) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700951 print_verbose(args, _("Server supports allow-tip-sha1-in-want"));
Fredrik Medley7199c092015-05-21 22:23:38 +0200952 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
Junio C Hamano6e7b66e2013-01-29 14:02:15 -0800953 }
Fredrik Medley68ee6282015-05-21 22:23:39 +0200954 if (server_supports("allow-reachable-sha1-in-want")) {
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700955 print_verbose(args, _("Server supports allow-reachable-sha1-in-want"));
Fredrik Medley68ee6282015-05-21 22:23:39 +0200956 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
957 }
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700958 if (!server_supports("thin-pack"))
959 args->use_thin_pack = 0;
960 if (!server_supports("no-progress"))
961 args->no_progress = 0;
962 if (!server_supports("include-tag"))
963 args->include_tag = 0;
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700964 if (server_supports("ofs-delta"))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700965 print_verbose(args, _("Server supports ofs-delta"));
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700966 else
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700967 prefer_ofs_delta = 0;
968
Jeff Hostetler640d8b72017-12-08 15:58:40 +0000969 if (server_supports("filter")) {
970 server_supports_filtering = 1;
971 print_verbose(args, _("Server supports filter"));
972 } else if (args->filter_options.choice) {
973 warning("filtering not recognized by server, ignoring");
974 }
975
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700976 if ((agent_feature = server_feature_value("agent", &agent_len))) {
977 agent_supported = 1;
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700978 if (agent_len)
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +0700979 print_verbose(args, _("Server version is %.*s"),
Nguyễn Thái Ngọc Duy0d789a52016-06-12 17:53:54 +0700980 agent_len, agent_feature);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700981 }
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +0700982 if (server_supports("deepen-since"))
983 deepen_since_ok = 1;
984 else if (args->deepen_since)
985 die(_("Server does not support --shallow-since"));
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +0700986 if (server_supports("deepen-not"))
987 deepen_not_ok = 1;
988 else if (args->deepen_not)
989 die(_("Server does not support --shallow-exclude"));
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700990 if (!server_supports("deepen-relative") && args->deepen_relative)
991 die(_("Server does not support --deepen"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700992
Jonathan Tanec062832018-06-14 15:54:28 -0700993 mark_complete_and_common_ref(&negotiator, args, &ref);
Jonathan Tan34c29032018-06-06 13:47:07 -0700994 filter_refs(args, &ref, sought, nr_sought);
995 if (everything_local(args, &ref)) {
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +0700996 packet_flush(fd[1]);
997 goto all_done;
998 }
Jonathan Tanec062832018-06-14 15:54:28 -0700999 if (find_common(&negotiator, args, fd, &oid, ref) < 0)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001000 if (!args->keep_pack)
1001 /* When cloning, it is not unusual to have
1002 * no common commit.
1003 */
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001004 warning(_("no common commits"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001005
1006 if (args->stateless_rpc)
1007 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001008 if (args->deepen)
Nguyễn Thái Ngọc Duy1a30f5a2013-12-05 20:02:34 +07001009 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file,
1010 NULL);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001011 else if (si->nr_ours || si->nr_theirs)
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001012 alternate_shallow_file = setup_temporary_shallow(si->shallow);
Nguyễn Thái Ngọc Duy6da8bdc2013-08-26 09:17:26 +07001013 else
1014 alternate_shallow_file = NULL;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001015 if (get_pack(args, fd, pack_lockfile))
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001016 die(_("git fetch-pack: fetch failed."));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001017
1018 all_done:
Jonathan Tanec062832018-06-14 15:54:28 -07001019 negotiator.release(&negotiator);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001020 return ref;
1021}
1022
Brandon Williamsf7e20502018-03-15 10:31:29 -07001023static void add_shallow_requests(struct strbuf *req_buf,
1024 const struct fetch_pack_args *args)
1025{
Junio C Hamano00624d62018-07-18 12:20:27 -07001026 if (is_repository_shallow(the_repository))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001027 write_shallow_commits(req_buf, 1, NULL);
1028 if (args->depth > 0)
1029 packet_buf_write(req_buf, "deepen %d", args->depth);
1030 if (args->deepen_since) {
1031 timestamp_t max_age = approxidate(args->deepen_since);
1032 packet_buf_write(req_buf, "deepen-since %"PRItime, max_age);
1033 }
1034 if (args->deepen_not) {
1035 int i;
1036 for (i = 0; i < args->deepen_not->nr; i++) {
1037 struct string_list_item *s = args->deepen_not->items + i;
1038 packet_buf_write(req_buf, "deepen-not %s", s->string);
1039 }
1040 }
1041}
1042
Brandon Williams685fbd32018-03-15 10:31:28 -07001043static void add_wants(const struct ref *wants, struct strbuf *req_buf)
1044{
Brandon Williams73302052018-06-27 15:30:23 -07001045 int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0);
1046
Brandon Williams685fbd32018-03-15 10:31:28 -07001047 for ( ; wants ; wants = wants->next) {
1048 const struct object_id *remote = &wants->old_oid;
Brandon Williams685fbd32018-03-15 10:31:28 -07001049 struct object *o;
1050
1051 /*
1052 * If that object is complete (i.e. it is an ancestor of a
1053 * local ref), we tell them we have it but do not have to
1054 * tell them about its ancestors, which they already know
1055 * about.
1056 *
1057 * We use lookup_object here because we are only
1058 * interested in the case we *know* the object is
1059 * reachable and we have already scanned it.
1060 */
Stefan Beller5abddd12018-06-28 18:21:52 -07001061 if (((o = lookup_object(the_repository, remote->hash)) != NULL) &&
Brandon Williams685fbd32018-03-15 10:31:28 -07001062 (o->flags & COMPLETE)) {
1063 continue;
1064 }
1065
Brandon Williams73302052018-06-27 15:30:23 -07001066 if (!use_ref_in_want || wants->exact_oid)
1067 packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote));
1068 else
1069 packet_buf_write(req_buf, "want-ref %s\n", wants->name);
Brandon Williams685fbd32018-03-15 10:31:28 -07001070 }
1071}
1072
1073static void add_common(struct strbuf *req_buf, struct oidset *common)
1074{
1075 struct oidset_iter iter;
1076 const struct object_id *oid;
1077 oidset_iter_init(common, &iter);
1078
1079 while ((oid = oidset_iter_next(&iter))) {
1080 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1081 }
1082}
1083
Jonathan Tanec062832018-06-14 15:54:28 -07001084static int add_haves(struct fetch_negotiator *negotiator,
1085 struct strbuf *req_buf,
Jonathan Tand30fe892018-06-14 15:54:26 -07001086 int *haves_to_send, int *in_vain)
Brandon Williams685fbd32018-03-15 10:31:28 -07001087{
1088 int ret = 0;
1089 int haves_added = 0;
1090 const struct object_id *oid;
1091
Jonathan Tanec062832018-06-14 15:54:28 -07001092 while ((oid = negotiator->next(negotiator))) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001093 packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid));
1094 if (++haves_added >= *haves_to_send)
1095 break;
1096 }
1097
1098 *in_vain += haves_added;
1099 if (!haves_added || *in_vain >= MAX_IN_VAIN) {
1100 /* Send Done */
1101 packet_buf_write(req_buf, "done\n");
1102 ret = 1;
1103 }
1104
1105 /* Increase haves to send on next round */
1106 *haves_to_send = next_flush(1, *haves_to_send);
1107
1108 return ret;
1109}
1110
Jonathan Tanec062832018-06-14 15:54:28 -07001111static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out,
Jonathan Tand30fe892018-06-14 15:54:26 -07001112 const struct fetch_pack_args *args,
Brandon Williams685fbd32018-03-15 10:31:28 -07001113 const struct ref *wants, struct oidset *common,
1114 int *haves_to_send, int *in_vain)
1115{
1116 int ret = 0;
1117 struct strbuf req_buf = STRBUF_INIT;
1118
1119 if (server_supports_v2("fetch", 1))
1120 packet_buf_write(&req_buf, "command=fetch");
1121 if (server_supports_v2("agent", 0))
1122 packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized());
Brandon Williams5e3548e2018-04-23 15:46:24 -07001123 if (args->server_options && args->server_options->nr &&
1124 server_supports_v2("server-option", 1)) {
1125 int i;
1126 for (i = 0; i < args->server_options->nr; i++)
1127 packet_write_fmt(fd_out, "server-option=%s",
1128 args->server_options->items[i].string);
1129 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001130
1131 packet_buf_delim(&req_buf);
1132 if (args->use_thin_pack)
1133 packet_buf_write(&req_buf, "thin-pack");
1134 if (args->no_progress)
1135 packet_buf_write(&req_buf, "no-progress");
1136 if (args->include_tag)
1137 packet_buf_write(&req_buf, "include-tag");
1138 if (prefer_ofs_delta)
1139 packet_buf_write(&req_buf, "ofs-delta");
1140
Brandon Williamsf7e20502018-03-15 10:31:29 -07001141 /* Add shallow-info and deepen request */
1142 if (server_supports_feature("fetch", "shallow", 0))
1143 add_shallow_requests(&req_buf, args);
Junio C Hamano00624d62018-07-18 12:20:27 -07001144 else if (is_repository_shallow(the_repository) || args->deepen)
Brandon Williamsf7e20502018-03-15 10:31:29 -07001145 die(_("Server does not support shallow requests"));
1146
Jonathan Tanba957102018-05-03 16:46:56 -07001147 /* Add filter */
1148 if (server_supports_feature("fetch", "filter", 0) &&
1149 args->filter_options.choice) {
1150 print_verbose(args, _("Server supports filter"));
1151 packet_buf_write(&req_buf, "filter %s",
1152 args->filter_options.filter_spec);
1153 } else if (args->filter_options.choice) {
1154 warning("filtering not recognized by server, ignoring");
1155 }
1156
Brandon Williams685fbd32018-03-15 10:31:28 -07001157 /* add wants */
1158 add_wants(wants, &req_buf);
1159
Jonathan Tanba957102018-05-03 16:46:56 -07001160 if (args->no_dependents) {
1161 packet_buf_write(&req_buf, "done");
1162 ret = 1;
1163 } else {
1164 /* Add all of the common commits we've found in previous rounds */
1165 add_common(&req_buf, common);
Brandon Williams685fbd32018-03-15 10:31:28 -07001166
Jonathan Tanba957102018-05-03 16:46:56 -07001167 /* Add initial haves */
Jonathan Tanec062832018-06-14 15:54:28 -07001168 ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain);
Jonathan Tanba957102018-05-03 16:46:56 -07001169 }
Brandon Williams685fbd32018-03-15 10:31:28 -07001170
1171 /* Send request */
1172 packet_buf_flush(&req_buf);
1173 write_or_die(fd_out, req_buf.buf, req_buf.len);
1174
1175 strbuf_release(&req_buf);
1176 return ret;
1177}
1178
1179/*
1180 * Processes a section header in a server's response and checks if it matches
1181 * `section`. If the value of `peek` is 1, the header line will be peeked (and
1182 * not consumed); if 0, the line will be consumed and the function will die if
1183 * the section header doesn't match what was expected.
1184 */
1185static int process_section_header(struct packet_reader *reader,
1186 const char *section, int peek)
1187{
1188 int ret;
1189
1190 if (packet_reader_peek(reader) != PACKET_READ_NORMAL)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001191 die(_("error reading section header '%s'"), section);
Brandon Williams685fbd32018-03-15 10:31:28 -07001192
1193 ret = !strcmp(reader->line, section);
1194
1195 if (!peek) {
1196 if (!ret)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001197 die(_("expected '%s', received '%s'"),
Brandon Williams685fbd32018-03-15 10:31:28 -07001198 section, reader->line);
1199 packet_reader_read(reader);
1200 }
1201
1202 return ret;
1203}
1204
Jonathan Tanec062832018-06-14 15:54:28 -07001205static int process_acks(struct fetch_negotiator *negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -07001206 struct packet_reader *reader,
1207 struct oidset *common)
Brandon Williams685fbd32018-03-15 10:31:28 -07001208{
1209 /* received */
1210 int received_ready = 0;
1211 int received_ack = 0;
1212
1213 process_section_header(reader, "acknowledgments", 0);
1214 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1215 const char *arg;
1216
1217 if (!strcmp(reader->line, "NAK"))
1218 continue;
1219
1220 if (skip_prefix(reader->line, "ACK ", &arg)) {
1221 struct object_id oid;
1222 if (!get_oid_hex(arg, &oid)) {
1223 struct commit *commit;
1224 oidset_insert(common, &oid);
Stefan Bellerc1f5eb42018-06-28 18:21:59 -07001225 commit = lookup_commit(the_repository, &oid);
Jonathan Tanec062832018-06-14 15:54:28 -07001226 negotiator->ack(negotiator, commit);
Brandon Williams685fbd32018-03-15 10:31:28 -07001227 }
1228 continue;
1229 }
1230
1231 if (!strcmp(reader->line, "ready")) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001232 received_ready = 1;
1233 continue;
1234 }
1235
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001236 die(_("unexpected acknowledgment line: '%s'"), reader->line);
Brandon Williams685fbd32018-03-15 10:31:28 -07001237 }
1238
1239 if (reader->status != PACKET_READ_FLUSH &&
1240 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001241 die(_("error processing acks: %d"), reader->status);
Brandon Williams685fbd32018-03-15 10:31:28 -07001242
1243 /* return 0 if no common, 1 if there are common, or 2 if ready */
1244 return received_ready ? 2 : (received_ack ? 1 : 0);
1245}
1246
Brandon Williamsf7e20502018-03-15 10:31:29 -07001247static void receive_shallow_info(struct fetch_pack_args *args,
1248 struct packet_reader *reader)
1249{
1250 process_section_header(reader, "shallow-info", 0);
1251 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1252 const char *arg;
1253 struct object_id oid;
1254
1255 if (skip_prefix(reader->line, "shallow ", &arg)) {
1256 if (get_oid_hex(arg, &oid))
1257 die(_("invalid shallow line: %s"), reader->line);
Junio C Hamano00624d62018-07-18 12:20:27 -07001258 register_shallow(the_repository, &oid);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001259 continue;
1260 }
1261 if (skip_prefix(reader->line, "unshallow ", &arg)) {
1262 if (get_oid_hex(arg, &oid))
1263 die(_("invalid unshallow line: %s"), reader->line);
Stefan Beller5abddd12018-06-28 18:21:52 -07001264 if (!lookup_object(the_repository, oid.hash))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001265 die(_("object not found: %s"), reader->line);
1266 /* make sure that it is parsed as shallow */
Stefan Beller109cd762018-06-28 18:21:51 -07001267 if (!parse_object(the_repository, &oid))
Brandon Williamsf7e20502018-03-15 10:31:29 -07001268 die(_("error in object: %s"), reader->line);
1269 if (unregister_shallow(&oid))
1270 die(_("no shallow found: %s"), reader->line);
1271 continue;
1272 }
1273 die(_("expected shallow/unshallow, got %s"), reader->line);
1274 }
1275
1276 if (reader->status != PACKET_READ_FLUSH &&
1277 reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001278 die(_("error processing shallow info: %d"), reader->status);
Brandon Williamsf7e20502018-03-15 10:31:29 -07001279
1280 setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, NULL);
1281 args->deepen = 1;
1282}
1283
Jonathan Tane2842b32018-08-01 13:13:20 -07001284static void receive_wanted_refs(struct packet_reader *reader,
1285 struct ref **sought, int nr_sought)
Brandon Williams73302052018-06-27 15:30:23 -07001286{
1287 process_section_header(reader, "wanted-refs", 0);
1288 while (packet_reader_read(reader) == PACKET_READ_NORMAL) {
1289 struct object_id oid;
1290 const char *end;
Jonathan Tane2842b32018-08-01 13:13:20 -07001291 int i;
Brandon Williams73302052018-06-27 15:30:23 -07001292
1293 if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ')
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001294 die(_("expected wanted-ref, got '%s'"), reader->line);
Brandon Williams73302052018-06-27 15:30:23 -07001295
Jonathan Tane2842b32018-08-01 13:13:20 -07001296 for (i = 0; i < nr_sought; i++) {
1297 if (!strcmp(end, sought[i]->name)) {
1298 oidcpy(&sought[i]->old_oid, &oid);
Brandon Williams73302052018-06-27 15:30:23 -07001299 break;
1300 }
1301 }
1302
Jonathan Tane2842b32018-08-01 13:13:20 -07001303 if (i == nr_sought)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001304 die(_("unexpected wanted-ref: '%s'"), reader->line);
Brandon Williams73302052018-06-27 15:30:23 -07001305 }
1306
1307 if (reader->status != PACKET_READ_DELIM)
Brandon Williamsbbb19a82018-07-23 10:56:35 -07001308 die(_("error processing wanted refs: %d"), reader->status);
Brandon Williams73302052018-06-27 15:30:23 -07001309}
1310
Brandon Williams685fbd32018-03-15 10:31:28 -07001311enum fetch_state {
1312 FETCH_CHECK_LOCAL = 0,
1313 FETCH_SEND_REQUEST,
1314 FETCH_PROCESS_ACKS,
1315 FETCH_GET_PACK,
1316 FETCH_DONE,
1317};
1318
1319static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
1320 int fd[2],
1321 const struct ref *orig_ref,
1322 struct ref **sought, int nr_sought,
1323 char **pack_lockfile)
1324{
1325 struct ref *ref = copy_ref_list(orig_ref);
1326 enum fetch_state state = FETCH_CHECK_LOCAL;
1327 struct oidset common = OIDSET_INIT;
1328 struct packet_reader reader;
1329 int in_vain = 0;
1330 int haves_to_send = INITIAL_FLUSH;
Jonathan Tanec062832018-06-14 15:54:28 -07001331 struct fetch_negotiator negotiator;
Jonathan Tan42cc7482018-07-16 11:44:01 -07001332 fetch_negotiator_init(&negotiator, negotiation_algorithm);
Brandon Williams685fbd32018-03-15 10:31:28 -07001333 packet_reader_init(&reader, fd[0], NULL, 0,
1334 PACKET_READ_CHOMP_NEWLINE);
1335
1336 while (state != FETCH_DONE) {
1337 switch (state) {
1338 case FETCH_CHECK_LOCAL:
1339 sort_ref_list(&ref, ref_compare_name);
1340 QSORT(sought, nr_sought, cmp_ref_by_name);
1341
1342 /* v2 supports these by default */
1343 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1344 use_sideband = 2;
Brandon Williamsf7e20502018-03-15 10:31:29 -07001345 if (args->depth > 0 || args->deepen_since || args->deepen_not)
1346 args->deepen = 1;
Brandon Williams685fbd32018-03-15 10:31:28 -07001347
Brandon Williams685fbd32018-03-15 10:31:28 -07001348 /* Filter 'ref' by 'sought' and those that aren't local */
Jonathan Tanec062832018-06-14 15:54:28 -07001349 mark_complete_and_common_ref(&negotiator, args, &ref);
Jonathan Tan34c29032018-06-06 13:47:07 -07001350 filter_refs(args, &ref, sought, nr_sought);
1351 if (everything_local(args, &ref))
Brandon Williams685fbd32018-03-15 10:31:28 -07001352 state = FETCH_DONE;
1353 else
1354 state = FETCH_SEND_REQUEST;
Jonathan Tanaf1c90d2018-06-14 15:54:25 -07001355
Jonathan Tan3390e422018-07-02 15:39:44 -07001356 mark_tips(&negotiator, args->negotiation_tips);
Jonathan Tanec062832018-06-14 15:54:28 -07001357 for_each_cached_alternate(&negotiator,
Jonathan Tand30fe892018-06-14 15:54:26 -07001358 insert_one_alternate_object);
Brandon Williams685fbd32018-03-15 10:31:28 -07001359 break;
1360 case FETCH_SEND_REQUEST:
Jonathan Tanec062832018-06-14 15:54:28 -07001361 if (send_fetch_request(&negotiator, fd[1], args, ref,
1362 &common,
Brandon Williams685fbd32018-03-15 10:31:28 -07001363 &haves_to_send, &in_vain))
1364 state = FETCH_GET_PACK;
1365 else
1366 state = FETCH_PROCESS_ACKS;
1367 break;
1368 case FETCH_PROCESS_ACKS:
1369 /* Process ACKs/NAKs */
Jonathan Tanec062832018-06-14 15:54:28 -07001370 switch (process_acks(&negotiator, &reader, &common)) {
Brandon Williams685fbd32018-03-15 10:31:28 -07001371 case 2:
1372 state = FETCH_GET_PACK;
1373 break;
1374 case 1:
1375 in_vain = 0;
1376 /* fallthrough */
1377 default:
1378 state = FETCH_SEND_REQUEST;
1379 break;
1380 }
1381 break;
1382 case FETCH_GET_PACK:
Brandon Williamsf7e20502018-03-15 10:31:29 -07001383 /* Check for shallow-info section */
1384 if (process_section_header(&reader, "shallow-info", 1))
1385 receive_shallow_info(args, &reader);
1386
Brandon Williams73302052018-06-27 15:30:23 -07001387 if (process_section_header(&reader, "wanted-refs", 1))
Jonathan Tane2842b32018-08-01 13:13:20 -07001388 receive_wanted_refs(&reader, sought, nr_sought);
Brandon Williams73302052018-06-27 15:30:23 -07001389
Brandon Williams685fbd32018-03-15 10:31:28 -07001390 /* get the pack */
1391 process_section_header(&reader, "packfile", 0);
1392 if (get_pack(args, fd, pack_lockfile))
1393 die(_("git fetch-pack: fetch failed."));
1394
1395 state = FETCH_DONE;
1396 break;
1397 case FETCH_DONE:
1398 continue;
1399 }
1400 }
1401
Jonathan Tanec062832018-06-14 15:54:28 -07001402 negotiator.release(&negotiator);
Brandon Williams685fbd32018-03-15 10:31:28 -07001403 oidset_clear(&common);
1404 return ref;
1405}
1406
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001407static int fetch_pack_config_cb(const char *var, const char *value, void *cb)
1408{
1409 if (strcmp(var, "fetch.fsck.skiplist") == 0) {
1410 const char *path;
1411
1412 if (git_config_pathname(&path, var, value))
1413 return 1;
1414 strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
1415 fsck_msg_types.len ? ',' : '=', path);
1416 free((char *)path);
1417 return 0;
1418 }
1419
1420 if (skip_prefix(var, "fetch.fsck.", &var)) {
1421 if (is_valid_msg_type(var, value))
1422 strbuf_addf(&fsck_msg_types, "%c%s=%s",
1423 fsck_msg_types.len ? ',' : '=', var, value);
1424 else
1425 warning("Skipping unknown msg id '%s'", var);
1426 return 0;
1427 }
1428
1429 return git_default_config(var, value, cb);
1430}
1431
Tanay Abhraf44af512014-08-07 09:21:20 -07001432static void fetch_pack_config(void)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001433{
Tanay Abhraf44af512014-08-07 09:21:20 -07001434 git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit);
1435 git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit);
1436 git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta);
1437 git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects);
1438 git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects);
Jonathan Tan42cc7482018-07-16 11:44:01 -07001439 git_config_get_string("fetch.negotiationalgorithm",
1440 &negotiation_algorithm);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001441
Ævar Arnfjörð Bjarmason1362df02018-07-27 14:37:17 +00001442 git_config(fetch_pack_config_cb, NULL);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001443}
1444
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001445static void fetch_pack_setup(void)
1446{
1447 static int did_setup;
1448 if (did_setup)
1449 return;
Tanay Abhraf44af512014-08-07 09:21:20 -07001450 fetch_pack_config();
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001451 if (0 <= transfer_unpack_limit)
1452 unpack_limit = transfer_unpack_limit;
1453 else if (0 <= fetch_unpack_limit)
1454 unpack_limit = fetch_unpack_limit;
1455 did_setup = 1;
1456}
1457
Junio C Hamanof2db8542013-01-29 14:02:15 -08001458static int remove_duplicates_in_refs(struct ref **ref, int nr)
1459{
1460 struct string_list names = STRING_LIST_INIT_NODUP;
1461 int src, dst;
1462
1463 for (src = dst = 0; src < nr; src++) {
1464 struct string_list_item *item;
1465 item = string_list_insert(&names, ref[src]->name);
1466 if (item->util)
1467 continue; /* already have it */
1468 item->util = ref[src];
1469 if (src != dst)
1470 ref[dst] = ref[src];
1471 dst++;
1472 }
1473 for (src = dst; src < nr; src++)
1474 ref[src] = NULL;
1475 string_list_clear(&names, 0);
1476 return dst;
1477}
1478
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001479static void update_shallow(struct fetch_pack_args *args,
Jonathan Tane2842b32018-08-01 13:13:20 -07001480 struct ref **sought, int nr_sought,
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001481 struct shallow_info *si)
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001482{
brian m. carlson910650d2017-03-31 01:40:00 +00001483 struct oid_array ref = OID_ARRAY_INIT;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001484 int *status;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001485 int i;
1486
Nguyễn Thái Ngọc Duy79891cb2016-06-12 17:53:56 +07001487 if (args->deepen && alternate_shallow_file) {
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001488 if (*alternate_shallow_file == '\0') { /* --unshallow */
Stefan Beller102de882018-05-17 15:51:51 -07001489 unlink_or_warn(git_path_shallow(the_repository));
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001490 rollback_lock_file(&shallow_lock);
1491 } else
1492 commit_lock_file(&shallow_lock);
1493 return;
1494 }
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001495
1496 if (!si->shallow || !si->shallow->nr)
1497 return;
1498
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001499 if (args->cloning) {
1500 /*
1501 * remote is shallow, but this is a clone, there are
1502 * no objects in repo to worry about. Accept any
1503 * shallow points that exist in the pack (iow in repo
1504 * after get_pack() and reprepare_packed_git())
1505 */
brian m. carlson910650d2017-03-31 01:40:00 +00001506 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001507 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001508 for (i = 0; i < si->shallow->nr; i++)
brian m. carlsonee3051b2017-03-26 16:01:37 +00001509 if (has_object_file(&oid[i]))
brian m. carlson910650d2017-03-31 01:40:00 +00001510 oid_array_append(&extra, &oid[i]);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001511 if (extra.nr) {
1512 setup_alternate_shallow(&shallow_lock,
1513 &alternate_shallow_file,
1514 &extra);
1515 commit_lock_file(&shallow_lock);
1516 }
brian m. carlson910650d2017-03-31 01:40:00 +00001517 oid_array_clear(&extra);
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001518 return;
1519 }
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001520
1521 if (!si->nr_ours && !si->nr_theirs)
1522 return;
1523
1524 remove_nonexistent_theirs_shallow(si);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001525 if (!si->nr_ours && !si->nr_theirs)
1526 return;
Jonathan Tane2842b32018-08-01 13:13:20 -07001527 for (i = 0; i < nr_sought; i++)
1528 oid_array_append(&ref, &sought[i]->old_oid);
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001529 si->ref = &ref;
1530
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001531 if (args->update_shallow) {
1532 /*
1533 * remote is also shallow, .git/shallow may be updated
1534 * so all refs can be accepted. Make sure we only add
1535 * shallow roots that are actually reachable from new
1536 * refs.
1537 */
brian m. carlson910650d2017-03-31 01:40:00 +00001538 struct oid_array extra = OID_ARRAY_INIT;
brian m. carlsonee3051b2017-03-26 16:01:37 +00001539 struct object_id *oid = si->shallow->oid;
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001540 assign_shallow_commits_to_refs(si, NULL, NULL);
1541 if (!si->nr_ours && !si->nr_theirs) {
brian m. carlson910650d2017-03-31 01:40:00 +00001542 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001543 return;
1544 }
1545 for (i = 0; i < si->nr_ours; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001546 oid_array_append(&extra, &oid[si->ours[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001547 for (i = 0; i < si->nr_theirs; i++)
brian m. carlson910650d2017-03-31 01:40:00 +00001548 oid_array_append(&extra, &oid[si->theirs[i]]);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001549 setup_alternate_shallow(&shallow_lock,
1550 &alternate_shallow_file,
1551 &extra);
1552 commit_lock_file(&shallow_lock);
brian m. carlson910650d2017-03-31 01:40:00 +00001553 oid_array_clear(&extra);
1554 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duy48d25ca2013-12-05 20:02:42 +07001555 return;
1556 }
1557
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001558 /*
1559 * remote is also shallow, check what ref is safe to update
1560 * without updating .git/shallow
1561 */
Jonathan Tane2842b32018-08-01 13:13:20 -07001562 status = xcalloc(nr_sought, sizeof(*status));
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001563 assign_shallow_commits_to_refs(si, NULL, status);
1564 if (si->nr_ours || si->nr_theirs) {
Jonathan Tane2842b32018-08-01 13:13:20 -07001565 for (i = 0; i < nr_sought; i++)
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001566 if (status[i])
Jonathan Tane2842b32018-08-01 13:13:20 -07001567 sought[i]->status = REF_STATUS_REJECT_SHALLOW;
Nguyễn Thái Ngọc Duy4820a332013-12-05 20:02:40 +07001568 }
1569 free(status);
brian m. carlson910650d2017-03-31 01:40:00 +00001570 oid_array_clear(&ref);
Nguyễn Thái Ngọc Duya796cce2013-12-05 20:02:37 +07001571}
1572
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001573static int iterate_ref_map(void *cb_data, struct object_id *oid)
1574{
1575 struct ref **rm = cb_data;
1576 struct ref *ref = *rm;
1577
1578 if (!ref)
1579 return -1; /* end of the list */
1580 *rm = ref->next;
1581 oidcpy(oid, &ref->old_oid);
1582 return 0;
1583}
1584
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001585struct ref *fetch_pack(struct fetch_pack_args *args,
1586 int fd[], struct child_process *conn,
1587 const struct ref *ref,
1588 const char *dest,
Junio C Hamanof2db8542013-01-29 14:02:15 -08001589 struct ref **sought, int nr_sought,
brian m. carlson910650d2017-03-31 01:40:00 +00001590 struct oid_array *shallow,
Brandon Williams685fbd32018-03-15 10:31:28 -07001591 char **pack_lockfile,
1592 enum protocol_version version)
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001593{
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001594 struct ref *ref_cpy;
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001595 struct shallow_info si;
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001596
1597 fetch_pack_setup();
Junio C Hamanof2db8542013-01-29 14:02:15 -08001598 if (nr_sought)
1599 nr_sought = remove_duplicates_in_refs(sought, nr_sought);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001600
1601 if (!ref) {
1602 packet_flush(fd[1]);
Nguyễn Thái Ngọc Duy1dd73e22016-06-12 17:53:55 +07001603 die(_("no matching remote head"));
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001604 }
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001605 prepare_shallow_info(&si, shallow);
Brandon Williams685fbd32018-03-15 10:31:28 -07001606 if (version == protocol_v2)
1607 ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought,
1608 pack_lockfile);
1609 else
1610 ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
1611 &si, pack_lockfile);
Stefan Bellera49d2832018-03-23 18:45:21 +01001612 reprepare_packed_git(the_repository);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001613
1614 if (!args->cloning && args->deepen) {
1615 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1616 struct ref *iterator = ref_cpy;
1617 opt.shallow_file = alternate_shallow_file;
1618 if (args->deepen)
1619 opt.is_deepening_fetch = 1;
1620 if (check_connected(iterate_ref_map, &iterator, &opt)) {
1621 error(_("remote did not send all necessary objects"));
1622 free_refs(ref_cpy);
1623 ref_cpy = NULL;
1624 rollback_lock_file(&shallow_lock);
1625 goto cleanup;
1626 }
1627 args->connectivity_checked = 1;
1628 }
1629
Jonathan Tane2842b32018-08-01 13:13:20 -07001630 update_shallow(args, sought, nr_sought, &si);
Jonathan Tancf1e7c02018-07-02 15:08:43 -07001631cleanup:
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001632 clear_shallow_info(&si);
Nguyễn Thái Ngọc Duy745f7a82012-10-26 22:53:55 +07001633 return ref_cpy;
1634}
Matt McCutchene860d962017-02-22 11:01:22 -05001635
1636int report_unmatched_refs(struct ref **sought, int nr_sought)
1637{
1638 int i, ret = 0;
1639
1640 for (i = 0; i < nr_sought; i++) {
Matt McCutchend56583d2017-02-22 11:05:57 -05001641 if (!sought[i])
Matt McCutchene860d962017-02-22 11:01:22 -05001642 continue;
Matt McCutchend56583d2017-02-22 11:05:57 -05001643 switch (sought[i]->match_status) {
1644 case REF_MATCHED:
1645 continue;
1646 case REF_NOT_MATCHED:
1647 error(_("no such remote ref %s"), sought[i]->name);
1648 break;
1649 case REF_UNADVERTISED_NOT_ALLOWED:
1650 error(_("Server does not allow request for unadvertised object %s"),
1651 sought[i]->name);
1652 break;
1653 }
Matt McCutchene860d962017-02-22 11:01:22 -05001654 ret = 1;
1655 }
1656 return ret;
1657}