Elijah Newren | 5579f44 | 2023-04-11 00:41:48 -0700 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Stefan Beller | a49d283 | 2018-03-23 18:45:21 +0100 | [diff] [blame] | 2 | #include "repository.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Elijah Newren | d4a4f92 | 2023-04-22 20:17:26 +0000 | [diff] [blame] | 4 | #include "date.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 5 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 6 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 7 | #include "hex.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 8 | #include "lockfile.h" |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 9 | #include "refs.h" |
| 10 | #include "pkt-line.h" |
| 11 | #include "commit.h" |
| 12 | #include "tag.h" |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 13 | #include "pack.h" |
| 14 | #include "sideband.h" |
| 15 | #include "fetch-pack.h" |
| 16 | #include "remote.h" |
| 17 | #include "run-command.h" |
Junio C Hamano | 47a5918 | 2013-07-08 13:56:53 -0700 | [diff] [blame] | 18 | #include "connect.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 19 | #include "trace2.h" |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 20 | #include "version.h" |
Jeff King | fe299ec | 2020-03-30 10:03:46 -0400 | [diff] [blame] | 21 | #include "oid-array.h" |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 22 | #include "oidset.h" |
Jonathan Tan | 0abe14f | 2017-08-18 15:20:26 -0700 | [diff] [blame] | 23 | #include "packfile.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 24 | #include "object-store-ll.h" |
Elijah Newren | c339932 | 2023-05-16 06:33:59 +0000 | [diff] [blame] | 25 | #include "path.h" |
Jonathan Tan | cf1e7c0 | 2018-07-02 15:08:43 -0700 | [diff] [blame] | 26 | #include "connected.h" |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 27 | #include "fetch-negotiator.h" |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 28 | #include "fsck.h" |
Taylor Blau | 120ad2b | 2020-04-30 13:48:50 -0600 | [diff] [blame] | 29 | #include "shallow.h" |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 30 | #include "commit-reach.h" |
| 31 | #include "commit-graph.h" |
Jeff King | 2a4aed4 | 2021-11-19 15:58:55 -0500 | [diff] [blame] | 32 | #include "sigchain.h" |
René Scharfe | 6fc9fec | 2022-07-16 18:59:59 +0200 | [diff] [blame] | 33 | #include "mergesort.h" |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 34 | |
| 35 | static int transfer_unpack_limit = -1; |
| 36 | static int fetch_unpack_limit = -1; |
| 37 | static int unpack_limit = 100; |
| 38 | static int prefer_ofs_delta = 1; |
| 39 | static int no_done; |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 40 | static int deepen_since_ok; |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 41 | static int deepen_not_ok; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 42 | static int fetch_fsck_objects = -1; |
| 43 | static int transfer_fsck_objects = -1; |
| 44 | static int agent_supported; |
Jeff Hostetler | 640d8b7 | 2017-12-08 15:58:40 +0000 | [diff] [blame] | 45 | static int server_supports_filtering; |
Josh Steadmon | 1e905bb | 2020-11-11 15:29:31 -0800 | [diff] [blame] | 46 | static int advertise_sid; |
Taylor Blau | cac4b8e | 2020-04-30 13:48:57 -0600 | [diff] [blame] | 47 | static struct shallow_lock shallow_lock; |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 48 | static const char *alternate_shallow_file; |
Ævar Arnfjörð Bjarmason | 3745e26 | 2021-03-28 15:15:51 +0200 | [diff] [blame] | 49 | static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES; |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 50 | static struct strbuf fsck_msg_types = STRBUF_INIT; |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 51 | static struct string_list uri_protocols = STRING_LIST_INIT_DUP; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 52 | |
Nguyễn Thái Ngọc Duy | 208acbf | 2014-03-25 20:23:26 +0700 | [diff] [blame] | 53 | /* Remember to update object flag allocation in object.h */ |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 54 | #define COMPLETE (1U << 0) |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 55 | #define ALTERNATE (1U << 1) |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 56 | #define COMMON (1U << 6) |
| 57 | #define REACH_SCRATCH (1U << 7) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * After sending this many "have"s if we do not get any new ACK , we |
| 61 | * give up traversing our history. |
| 62 | */ |
| 63 | #define MAX_IN_VAIN 256 |
| 64 | |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 65 | static int multi_ack, use_sideband; |
Fredrik Medley | 7199c09 | 2015-05-21 22:23:38 +0200 | [diff] [blame] | 66 | /* Allow specifying sha1 if it is a ref tip. */ |
| 67 | #define ALLOW_TIP_SHA1 01 |
Fredrik Medley | 68ee628 | 2015-05-21 22:23:39 +0200 | [diff] [blame] | 68 | /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ |
| 69 | #define ALLOW_REACHABLE_SHA1 02 |
Fredrik Medley | 7199c09 | 2015-05-21 22:23:38 +0200 | [diff] [blame] | 70 | static unsigned int allow_unadvertised_object_request; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 71 | |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 72 | __attribute__((format (printf, 2, 3))) |
| 73 | static inline void print_verbose(const struct fetch_pack_args *args, |
| 74 | const char *fmt, ...) |
| 75 | { |
| 76 | va_list params; |
| 77 | |
| 78 | if (!args->verbose) |
| 79 | return; |
| 80 | |
| 81 | va_start(params, fmt); |
| 82 | vfprintf(stderr, fmt, params); |
| 83 | va_end(params); |
| 84 | fputc('\n', stderr); |
| 85 | } |
| 86 | |
Jeff King | 41a078c | 2017-02-08 15:53:03 -0500 | [diff] [blame] | 87 | struct alternate_object_cache { |
| 88 | struct object **items; |
| 89 | size_t nr, alloc; |
| 90 | }; |
| 91 | |
Jeff King | bdf4276 | 2018-10-08 11:09:23 -0700 | [diff] [blame] | 92 | static void cache_one_alternate(const struct object_id *oid, |
Jeff King | 41a078c | 2017-02-08 15:53:03 -0500 | [diff] [blame] | 93 | void *vcache) |
| 94 | { |
| 95 | struct alternate_object_cache *cache = vcache; |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 96 | struct object *obj = parse_object(the_repository, oid); |
Jeff King | 41a078c | 2017-02-08 15:53:03 -0500 | [diff] [blame] | 97 | |
| 98 | if (!obj || (obj->flags & ALTERNATE)) |
| 99 | return; |
| 100 | |
| 101 | obj->flags |= ALTERNATE; |
| 102 | ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc); |
| 103 | cache->items[cache->nr++] = obj; |
| 104 | } |
| 105 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 106 | static void for_each_cached_alternate(struct fetch_negotiator *negotiator, |
| 107 | void (*cb)(struct fetch_negotiator *, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 108 | struct object *)) |
Jeff King | 41a078c | 2017-02-08 15:53:03 -0500 | [diff] [blame] | 109 | { |
| 110 | static int initialized; |
| 111 | static struct alternate_object_cache cache; |
| 112 | size_t i; |
| 113 | |
| 114 | if (!initialized) { |
| 115 | for_each_alternate_ref(cache_one_alternate, &cache); |
| 116 | initialized = 1; |
| 117 | } |
| 118 | |
| 119 | for (i = 0; i < cache.nr; i++) |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 120 | cb(negotiator, cache.items[i]); |
Jeff King | 41a078c | 2017-02-08 15:53:03 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Ævar Arnfjörð Bjarmason | a6e65fb | 2022-05-16 20:11:00 +0000 | [diff] [blame] | 123 | static struct commit *deref_without_lazy_fetch_extended(const struct object_id *oid, |
| 124 | int mark_tags_complete, |
| 125 | enum object_type *type, |
| 126 | unsigned int oi_flags) |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 127 | { |
Ævar Arnfjörð Bjarmason | a6e65fb | 2022-05-16 20:11:00 +0000 | [diff] [blame] | 128 | struct object_info info = { .typep = type }; |
Patrick Steinhardt | 62b5a35 | 2021-09-01 15:09:54 +0200 | [diff] [blame] | 129 | struct commit *commit; |
| 130 | |
| 131 | commit = lookup_commit_in_graph(the_repository, oid); |
| 132 | if (commit) |
| 133 | return commit; |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 134 | |
| 135 | while (1) { |
| 136 | if (oid_object_info_extended(the_repository, oid, &info, |
Ævar Arnfjörð Bjarmason | a6e65fb | 2022-05-16 20:11:00 +0000 | [diff] [blame] | 137 | oi_flags)) |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 138 | return NULL; |
Ævar Arnfjörð Bjarmason | a6e65fb | 2022-05-16 20:11:00 +0000 | [diff] [blame] | 139 | if (*type == OBJ_TAG) { |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 140 | struct tag *tag = (struct tag *) |
| 141 | parse_object(the_repository, oid); |
| 142 | |
| 143 | if (!tag->tagged) |
| 144 | return NULL; |
| 145 | if (mark_tags_complete) |
| 146 | tag->object.flags |= COMPLETE; |
| 147 | oid = &tag->tagged->oid; |
| 148 | } else { |
| 149 | break; |
| 150 | } |
| 151 | } |
Patrick Steinhardt | 3e5e6c6 | 2021-08-04 15:56:11 +0200 | [diff] [blame] | 152 | |
Ævar Arnfjörð Bjarmason | a6e65fb | 2022-05-16 20:11:00 +0000 | [diff] [blame] | 153 | if (*type == OBJ_COMMIT) { |
Patrick Steinhardt | 3e5e6c6 | 2021-08-04 15:56:11 +0200 | [diff] [blame] | 154 | struct commit *commit = lookup_commit(the_repository, oid); |
| 155 | if (!commit || repo_parse_commit(the_repository, commit)) |
| 156 | return NULL; |
| 157 | return commit; |
| 158 | } |
| 159 | |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 160 | return NULL; |
| 161 | } |
| 162 | |
Ævar Arnfjörð Bjarmason | a6e65fb | 2022-05-16 20:11:00 +0000 | [diff] [blame] | 163 | |
| 164 | static struct commit *deref_without_lazy_fetch(const struct object_id *oid, |
| 165 | int mark_tags_complete) |
| 166 | { |
| 167 | enum object_type type; |
| 168 | unsigned flags = OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK; |
| 169 | return deref_without_lazy_fetch_extended(oid, mark_tags_complete, |
| 170 | &type, flags); |
| 171 | } |
| 172 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 173 | static int rev_list_insert_ref(struct fetch_negotiator *negotiator, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 174 | const struct object_id *oid) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 175 | { |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 176 | struct commit *c = deref_without_lazy_fetch(oid, 0); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 177 | |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 178 | if (c) |
| 179 | negotiator->add_tip(negotiator, c); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 183 | static int rev_list_insert_ref_oid(const char *refname UNUSED, |
Jeff King | 63e14ee | 2022-08-19 06:08:32 -0400 | [diff] [blame] | 184 | const struct object_id *oid, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 185 | int flag UNUSED, |
Jeff King | 63e14ee | 2022-08-19 06:08:32 -0400 | [diff] [blame] | 186 | void *cb_data) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 187 | { |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 188 | return rev_list_insert_ref(cb_data, oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | enum ack_type { |
| 192 | NAK = 0, |
| 193 | ACK, |
| 194 | ACK_continue, |
| 195 | ACK_common, |
| 196 | ACK_ready |
| 197 | }; |
| 198 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 199 | static void consume_shallow_list(struct fetch_pack_args *args, |
| 200 | struct packet_reader *reader) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 201 | { |
Nguyễn Thái Ngọc Duy | 79891cb | 2016-06-12 17:53:56 +0700 | [diff] [blame] | 202 | if (args->stateless_rpc && args->deepen) { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 203 | /* If we sent a depth we will get back "duplicate" |
| 204 | * shallow and unshallow commands every time there |
| 205 | * is a block of have lines exchanged. |
| 206 | */ |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 207 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 208 | if (starts_with(reader->line, "shallow ")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 209 | continue; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 210 | if (starts_with(reader->line, "unshallow ")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 211 | continue; |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 212 | die(_("git fetch-pack: expected shallow list")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 213 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 214 | if (reader->status != PACKET_READ_FLUSH) |
| 215 | die(_("git fetch-pack: expected a flush packet after shallow list")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 219 | static enum ack_type get_ack(struct packet_reader *reader, |
| 220 | struct object_id *result_oid) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 221 | { |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 222 | int len; |
Jeff King | 82e5676 | 2014-06-18 15:56:03 -0400 | [diff] [blame] | 223 | const char *arg; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 224 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 225 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
Jeff King | bc9d4dc | 2018-02-08 13:47:49 -0500 | [diff] [blame] | 226 | die(_("git fetch-pack: expected ACK/NAK, got a flush packet")); |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 227 | len = reader->pktlen; |
| 228 | |
| 229 | if (!strcmp(reader->line, "NAK")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 230 | return NAK; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 231 | if (skip_prefix(reader->line, "ACK ", &arg)) { |
brian m. carlson | f6af19a | 2019-08-18 20:04:04 +0000 | [diff] [blame] | 232 | const char *p; |
| 233 | if (!parse_oid_hex(arg, result_oid, &p)) { |
| 234 | len -= p - reader->line; |
Jeff King | 82e5676 | 2014-06-18 15:56:03 -0400 | [diff] [blame] | 235 | if (len < 1) |
Jeff King | 030e9dd | 2013-02-20 15:00:28 -0500 | [diff] [blame] | 236 | return ACK; |
brian m. carlson | f6af19a | 2019-08-18 20:04:04 +0000 | [diff] [blame] | 237 | if (strstr(p, "continue")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 238 | return ACK_continue; |
brian m. carlson | f6af19a | 2019-08-18 20:04:04 +0000 | [diff] [blame] | 239 | if (strstr(p, "common")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 240 | return ACK_common; |
brian m. carlson | f6af19a | 2019-08-18 20:04:04 +0000 | [diff] [blame] | 241 | if (strstr(p, "ready")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 242 | return ACK_ready; |
| 243 | return ACK; |
| 244 | } |
| 245 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 246 | die(_("git fetch-pack: expected ACK/NAK, got '%s'"), reader->line); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void send_request(struct fetch_pack_args *args, |
| 250 | int fd, struct strbuf *buf) |
| 251 | { |
| 252 | if (args->stateless_rpc) { |
| 253 | send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX); |
| 254 | packet_flush(fd); |
Jeff King | 37c8001 | 2019-03-04 23:11:39 -0500 | [diff] [blame] | 255 | } else { |
| 256 | if (write_in_full(fd, buf->buf, buf->len) < 0) |
| 257 | die_errno(_("unable to write to remote")); |
| 258 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 259 | } |
| 260 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 261 | static void insert_one_alternate_object(struct fetch_negotiator *negotiator, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 262 | struct object *obj) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 263 | { |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 264 | rev_list_insert_ref(negotiator, &obj->oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | #define INITIAL_FLUSH 16 |
| 268 | #define PIPESAFE_FLUSH 32 |
Jonathan Tan | da47098 | 2016-07-18 15:21:38 -0700 | [diff] [blame] | 269 | #define LARGE_FLUSH 16384 |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 270 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 271 | static int next_flush(int stateless_rpc, int count) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 272 | { |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 273 | if (stateless_rpc) { |
Jonathan Tan | da47098 | 2016-07-18 15:21:38 -0700 | [diff] [blame] | 274 | if (count < LARGE_FLUSH) |
| 275 | count <<= 1; |
| 276 | else |
| 277 | count = count * 11 / 10; |
| 278 | } else { |
| 279 | if (count < PIPESAFE_FLUSH) |
| 280 | count <<= 1; |
| 281 | else |
| 282 | count += PIPESAFE_FLUSH; |
| 283 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 284 | return count; |
| 285 | } |
| 286 | |
Jonathan Tan | 3390e42 | 2018-07-02 15:39:44 -0700 | [diff] [blame] | 287 | static void mark_tips(struct fetch_negotiator *negotiator, |
| 288 | const struct oid_array *negotiation_tips) |
| 289 | { |
| 290 | int i; |
| 291 | |
| 292 | if (!negotiation_tips) { |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 293 | for_each_rawref(rev_list_insert_ref_oid, negotiator); |
Jonathan Tan | 3390e42 | 2018-07-02 15:39:44 -0700 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
| 297 | for (i = 0; i < negotiation_tips->nr; i++) |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 298 | rev_list_insert_ref(negotiator, &negotiation_tips->oid[i]); |
Jonathan Tan | 3390e42 | 2018-07-02 15:39:44 -0700 | [diff] [blame] | 299 | return; |
| 300 | } |
| 301 | |
Jonathan Tan | 1007557 | 2022-07-26 09:27:11 -0700 | [diff] [blame] | 302 | static void send_filter(struct fetch_pack_args *args, |
| 303 | struct strbuf *req_buf, |
| 304 | int server_supports_filter) |
| 305 | { |
| 306 | if (args->filter_options.choice) { |
| 307 | const char *spec = |
| 308 | expand_list_objects_filter_spec(&args->filter_options); |
| 309 | if (server_supports_filter) { |
| 310 | print_verbose(args, _("Server supports filter")); |
| 311 | packet_buf_write(req_buf, "filter %s", spec); |
| 312 | trace2_data_string("fetch", the_repository, |
| 313 | "filter/effective", spec); |
| 314 | } else { |
| 315 | warning("filtering not recognized by server, ignoring"); |
| 316 | trace2_data_string("fetch", the_repository, |
| 317 | "filter/unsupported", spec); |
| 318 | } |
| 319 | } else { |
| 320 | trace2_data_string("fetch", the_repository, |
| 321 | "filter/none", ""); |
| 322 | } |
| 323 | } |
| 324 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 325 | static int find_common(struct fetch_negotiator *negotiator, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 326 | struct fetch_pack_args *args, |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 327 | int fd[2], struct object_id *result_oid, |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 328 | struct ref *refs) |
| 329 | { |
| 330 | int fetching; |
| 331 | int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval; |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 332 | int negotiation_round = 0, haves = 0; |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 333 | const struct object_id *oid; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 334 | unsigned in_vain = 0; |
| 335 | int got_continue = 0; |
| 336 | int got_ready = 0; |
| 337 | struct strbuf req_buf = STRBUF_INIT; |
| 338 | size_t state_len = 0; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 339 | struct packet_reader reader; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 340 | |
| 341 | if (args->stateless_rpc && multi_ack == 1) |
Jean-Noël Avila | 6fa00ee | 2022-01-05 20:02:19 +0000 | [diff] [blame] | 342 | die(_("the option '%s' requires '%s'"), "--stateless-rpc", "multi_ack_detailed"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 343 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 344 | packet_reader_init(&reader, fd[0], NULL, 0, |
Masaya Suzuki | 2d103c3 | 2018-12-29 13:19:15 -0800 | [diff] [blame] | 345 | PACKET_READ_CHOMP_NEWLINE | |
| 346 | PACKET_READ_DIE_ON_ERR_PACKET); |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 347 | |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 348 | mark_tips(negotiator, args->negotiation_tips); |
| 349 | for_each_cached_alternate(negotiator, insert_one_alternate_object); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 350 | |
| 351 | fetching = 0; |
| 352 | for ( ; refs ; refs = refs->next) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 353 | struct object_id *remote = &refs->old_oid; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 354 | const char *remote_hex; |
| 355 | struct object *o; |
| 356 | |
Robert Coup | 4dfd092 | 2022-03-28 14:02:06 +0000 | [diff] [blame] | 357 | if (!args->refetch) { |
| 358 | /* |
| 359 | * If that object is complete (i.e. it is an ancestor of a |
| 360 | * local ref), we tell them we have it but do not have to |
| 361 | * tell them about its ancestors, which they already know |
| 362 | * about. |
| 363 | * |
| 364 | * We use lookup_object here because we are only |
| 365 | * interested in the case we *know* the object is |
| 366 | * reachable and we have already scanned it. |
| 367 | */ |
| 368 | if (((o = lookup_object(the_repository, remote)) != NULL) && |
| 369 | (o->flags & COMPLETE)) { |
| 370 | continue; |
| 371 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 372 | } |
| 373 | |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 374 | remote_hex = oid_to_hex(remote); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 375 | if (!fetching) { |
| 376 | struct strbuf c = STRBUF_INIT; |
| 377 | if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed"); |
| 378 | if (multi_ack == 1) strbuf_addstr(&c, " multi_ack"); |
| 379 | if (no_done) strbuf_addstr(&c, " no-done"); |
| 380 | if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k"); |
| 381 | if (use_sideband == 1) strbuf_addstr(&c, " side-band"); |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 382 | if (args->deepen_relative) strbuf_addstr(&c, " deepen-relative"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 383 | if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack"); |
| 384 | if (args->no_progress) strbuf_addstr(&c, " no-progress"); |
| 385 | if (args->include_tag) strbuf_addstr(&c, " include-tag"); |
| 386 | if (prefer_ofs_delta) strbuf_addstr(&c, " ofs-delta"); |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 387 | if (deepen_since_ok) strbuf_addstr(&c, " deepen-since"); |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 388 | if (deepen_not_ok) strbuf_addstr(&c, " deepen-not"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 389 | if (agent_supported) strbuf_addf(&c, " agent=%s", |
| 390 | git_user_agent_sanitized()); |
Josh Steadmon | 1e905bb | 2020-11-11 15:29:31 -0800 | [diff] [blame] | 391 | if (advertise_sid) |
| 392 | strbuf_addf(&c, " session-id=%s", trace2_session_id()); |
Jeff Hostetler | 640d8b7 | 2017-12-08 15:58:40 +0000 | [diff] [blame] | 393 | if (args->filter_options.choice) |
| 394 | strbuf_addstr(&c, " filter"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 395 | packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf); |
| 396 | strbuf_release(&c); |
| 397 | } else |
| 398 | packet_buf_write(&req_buf, "want %s\n", remote_hex); |
| 399 | fetching++; |
| 400 | } |
| 401 | |
| 402 | if (!fetching) { |
| 403 | strbuf_release(&req_buf); |
| 404 | packet_flush(fd[1]); |
| 405 | return 1; |
| 406 | } |
| 407 | |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 408 | if (is_repository_shallow(the_repository)) |
Nguyễn Thái Ngọc Duy | 1a30f5a | 2013-12-05 20:02:34 +0700 | [diff] [blame] | 409 | write_shallow_commits(&req_buf, 1, NULL); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 410 | if (args->depth > 0) |
| 411 | packet_buf_write(&req_buf, "deepen %d", args->depth); |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 412 | if (args->deepen_since) { |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 413 | timestamp_t max_age = approxidate(args->deepen_since); |
Johannes Schindelin | cb71f8b | 2017-04-21 12:45:48 +0200 | [diff] [blame] | 414 | packet_buf_write(&req_buf, "deepen-since %"PRItime, max_age); |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 415 | } |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 416 | if (args->deepen_not) { |
| 417 | int i; |
| 418 | for (i = 0; i < args->deepen_not->nr; i++) { |
| 419 | struct string_list_item *s = args->deepen_not->items + i; |
| 420 | packet_buf_write(&req_buf, "deepen-not %s", s->string); |
| 421 | } |
| 422 | } |
Jonathan Tan | 1007557 | 2022-07-26 09:27:11 -0700 | [diff] [blame] | 423 | send_filter(args, &req_buf, server_supports_filtering); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 424 | packet_buf_flush(&req_buf); |
| 425 | state_len = req_buf.len; |
| 426 | |
Nguyễn Thái Ngọc Duy | 79891cb | 2016-06-12 17:53:56 +0700 | [diff] [blame] | 427 | if (args->deepen) { |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 428 | const char *arg; |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 429 | struct object_id oid; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 430 | |
| 431 | send_request(args, fd[1], &req_buf); |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 432 | while (packet_reader_read(&reader) == PACKET_READ_NORMAL) { |
| 433 | if (skip_prefix(reader.line, "shallow ", &arg)) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 434 | if (get_oid_hex(arg, &oid)) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 435 | die(_("invalid shallow line: %s"), reader.line); |
Stefan Beller | 19143f1 | 2018-05-17 15:51:44 -0700 | [diff] [blame] | 436 | register_shallow(the_repository, &oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 437 | continue; |
| 438 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 439 | if (skip_prefix(reader.line, "unshallow ", &arg)) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 440 | if (get_oid_hex(arg, &oid)) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 441 | die(_("invalid unshallow line: %s"), reader.line); |
Jeff King | d0229ab | 2019-06-20 03:41:14 -0400 | [diff] [blame] | 442 | if (!lookup_object(the_repository, &oid)) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 443 | die(_("object not found: %s"), reader.line); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 444 | /* make sure that it is parsed as shallow */ |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 445 | if (!parse_object(the_repository, &oid)) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 446 | die(_("error in object: %s"), reader.line); |
brian m. carlson | e92b848 | 2017-05-06 22:10:06 +0000 | [diff] [blame] | 447 | if (unregister_shallow(&oid)) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 448 | die(_("no shallow found: %s"), reader.line); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 449 | continue; |
| 450 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 451 | die(_("expected shallow/unshallow, got %s"), reader.line); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 452 | } |
| 453 | } else if (!args->stateless_rpc) |
| 454 | send_request(args, fd[1], &req_buf); |
| 455 | |
| 456 | if (!args->stateless_rpc) { |
| 457 | /* If we aren't using the stateless-rpc interface |
| 458 | * we don't need to retain the headers. |
| 459 | */ |
| 460 | strbuf_setlen(&req_buf, 0); |
| 461 | state_len = 0; |
| 462 | } |
| 463 | |
Josh Steadmon | 5fc3118 | 2019-10-02 16:49:28 -0700 | [diff] [blame] | 464 | trace2_region_enter("fetch-pack", "negotiation_v0_v1", the_repository); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 465 | flushes = 0; |
| 466 | retval = -1; |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 467 | while ((oid = negotiator->next(negotiator))) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 468 | packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid)); |
| 469 | print_verbose(args, "have %s", oid_to_hex(oid)); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 470 | in_vain++; |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 471 | haves++; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 472 | if (flush_at <= ++count) { |
| 473 | int ack; |
| 474 | |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 475 | negotiation_round++; |
| 476 | trace2_region_enter_printf("negotiation_v0_v1", "round", |
| 477 | the_repository, "%d", |
| 478 | negotiation_round); |
| 479 | trace2_data_intmax("negotiation_v0_v1", the_repository, |
| 480 | "haves_added", haves); |
| 481 | trace2_data_intmax("negotiation_v0_v1", the_repository, |
| 482 | "in_vain", in_vain); |
| 483 | haves = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 484 | packet_buf_flush(&req_buf); |
| 485 | send_request(args, fd[1], &req_buf); |
| 486 | strbuf_setlen(&req_buf, state_len); |
| 487 | flushes++; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 488 | flush_at = next_flush(args->stateless_rpc, count); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * We keep one window "ahead" of the other side, and |
| 492 | * will wait for an ACK only on the next one |
| 493 | */ |
| 494 | if (!args->stateless_rpc && count == INITIAL_FLUSH) |
| 495 | continue; |
| 496 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 497 | consume_shallow_list(args, &reader); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 498 | do { |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 499 | ack = get_ack(&reader, result_oid); |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 500 | if (ack) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 501 | print_verbose(args, _("got %s %d %s"), "ack", |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 502 | ack, oid_to_hex(result_oid)); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 503 | switch (ack) { |
| 504 | case ACK: |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 505 | trace2_region_leave_printf("negotiation_v0_v1", "round", |
| 506 | the_repository, "%d", |
| 507 | negotiation_round); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 508 | flushes = 0; |
| 509 | multi_ack = 0; |
| 510 | retval = 0; |
| 511 | goto done; |
| 512 | case ACK_common: |
| 513 | case ACK_ready: |
| 514 | case ACK_continue: { |
| 515 | struct commit *commit = |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 516 | lookup_commit(the_repository, |
| 517 | result_oid); |
Jonathan Tan | d093bc7 | 2018-06-14 15:54:27 -0700 | [diff] [blame] | 518 | int was_common; |
Junio C Hamano | 3a2a1dc | 2018-08-02 15:30:42 -0700 | [diff] [blame] | 519 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 520 | if (!commit) |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 521 | die(_("invalid commit %s"), oid_to_hex(result_oid)); |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 522 | was_common = negotiator->ack(negotiator, commit); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 523 | if (args->stateless_rpc |
| 524 | && ack == ACK_common |
Jonathan Tan | d093bc7 | 2018-06-14 15:54:27 -0700 | [diff] [blame] | 525 | && !was_common) { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 526 | /* We need to replay the have for this object |
| 527 | * on the next RPC request so the peer knows |
| 528 | * it is in common with us. |
| 529 | */ |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 530 | const char *hex = oid_to_hex(result_oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 531 | packet_buf_write(&req_buf, "have %s\n", hex); |
| 532 | state_len = req_buf.len; |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 533 | haves++; |
Jonathan Tan | 06b3d38 | 2016-09-23 10:41:35 -0700 | [diff] [blame] | 534 | /* |
| 535 | * Reset in_vain because an ack |
| 536 | * for this commit has not been |
| 537 | * seen. |
| 538 | */ |
| 539 | in_vain = 0; |
| 540 | } else if (!args->stateless_rpc |
| 541 | || ack != ACK_common) |
| 542 | in_vain = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 543 | retval = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 544 | got_continue = 1; |
Jonathan Tan | 21bcf6e | 2018-06-14 15:54:24 -0700 | [diff] [blame] | 545 | if (ack == ACK_ready) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 546 | got_ready = 1; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | } |
| 550 | } while (ack); |
| 551 | flushes--; |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 552 | trace2_region_leave_printf("negotiation_v0_v1", "round", |
| 553 | the_repository, "%d", |
| 554 | negotiation_round); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 555 | if (got_continue && MAX_IN_VAIN < in_vain) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 556 | print_verbose(args, _("giving up")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 557 | break; /* give up */ |
| 558 | } |
Jonathan Tan | 21bcf6e | 2018-06-14 15:54:24 -0700 | [diff] [blame] | 559 | if (got_ready) |
| 560 | break; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | done: |
Josh Steadmon | 5fc3118 | 2019-10-02 16:49:28 -0700 | [diff] [blame] | 564 | trace2_region_leave("fetch-pack", "negotiation_v0_v1", the_repository); |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 565 | trace2_data_intmax("negotiation_v0_v1", the_repository, "total_rounds", |
| 566 | negotiation_round); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 567 | if (!got_ready || !no_done) { |
| 568 | packet_buf_write(&req_buf, "done\n"); |
| 569 | send_request(args, fd[1], &req_buf); |
| 570 | } |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 571 | print_verbose(args, _("done")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 572 | if (retval != 0) { |
| 573 | multi_ack = 0; |
| 574 | flushes++; |
| 575 | } |
| 576 | strbuf_release(&req_buf); |
| 577 | |
Nguyễn Thái Ngọc Duy | ff62eca | 2014-02-06 22:10:39 +0700 | [diff] [blame] | 578 | if (!got_ready || !no_done) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 579 | consume_shallow_list(args, &reader); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 580 | while (flushes || multi_ack) { |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 581 | int ack = get_ack(&reader, result_oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 582 | if (ack) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 583 | print_verbose(args, _("got %s (%d) %s"), "ack", |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 584 | ack, oid_to_hex(result_oid)); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 585 | if (ack == ACK) |
| 586 | return 0; |
| 587 | multi_ack = 1; |
| 588 | continue; |
| 589 | } |
| 590 | flushes--; |
| 591 | } |
| 592 | /* it is no error to fetch into a completely empty repo */ |
| 593 | return count ? retval : 0; |
| 594 | } |
| 595 | |
| 596 | static struct commit_list *complete; |
| 597 | |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 598 | static int mark_complete(const struct object_id *oid) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 599 | { |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 600 | struct commit *commit = deref_without_lazy_fetch(oid, 1); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 601 | |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 602 | if (commit && !(commit->object.flags & COMPLETE)) { |
| 603 | commit->object.flags |= COMPLETE; |
| 604 | commit_list_insert(commit, &complete); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 605 | } |
| 606 | return 0; |
| 607 | } |
| 608 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 609 | static int mark_complete_oid(const char *refname UNUSED, |
Jeff King | 63e14ee | 2022-08-19 06:08:32 -0400 | [diff] [blame] | 610 | const struct object_id *oid, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 611 | int flag UNUSED, |
| 612 | void *cb_data UNUSED) |
Michael Haggerty | f8ee4d8 | 2015-05-25 18:39:16 +0000 | [diff] [blame] | 613 | { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 614 | return mark_complete(oid); |
Michael Haggerty | f8ee4d8 | 2015-05-25 18:39:16 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 617 | static void mark_recent_complete_commits(struct fetch_pack_args *args, |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 618 | timestamp_t cutoff) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 619 | { |
| 620 | while (complete && cutoff <= complete->item->date) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 621 | print_verbose(args, _("Marking %s as complete"), |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 622 | oid_to_hex(&complete->item->object.oid)); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 623 | pop_most_recent_commit(&complete, COMPLETE); |
| 624 | } |
| 625 | } |
| 626 | |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 627 | static void add_refs_to_oidset(struct oidset *oids, struct ref *refs) |
| 628 | { |
| 629 | for (; refs; refs = refs->next) |
| 630 | oidset_insert(oids, &refs->old_oid); |
| 631 | } |
| 632 | |
René Scharfe | bf73282 | 2018-10-04 17:09:06 +0200 | [diff] [blame] | 633 | static int is_unmatched_ref(const struct ref *ref) |
| 634 | { |
| 635 | struct object_id oid; |
| 636 | const char *p; |
| 637 | return ref->match_status == REF_NOT_MATCHED && |
| 638 | !parse_oid_hex(ref->name, &oid, &p) && |
| 639 | *p == '\0' && |
| 640 | oideq(&oid, &ref->old_oid); |
| 641 | } |
| 642 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 643 | static void filter_refs(struct fetch_pack_args *args, |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 644 | struct ref **refs, |
| 645 | struct ref **sought, int nr_sought) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 646 | { |
| 647 | struct ref *newlist = NULL; |
| 648 | struct ref **newtail = &newlist; |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 649 | struct ref *unmatched = NULL; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 650 | struct ref *ref, *next; |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 651 | struct oidset tip_oids = OIDSET_INIT; |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 652 | int i; |
René Scharfe | 22a1646 | 2018-10-04 17:09:39 +0200 | [diff] [blame] | 653 | int strict = !(allow_unadvertised_object_request & |
| 654 | (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 655 | |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 656 | i = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 657 | for (ref = *refs; ref; ref = next) { |
| 658 | int keep = 0; |
| 659 | next = ref->next; |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 660 | |
René Scharfe | 50e19a8 | 2014-06-06 19:24:48 +0200 | [diff] [blame] | 661 | if (starts_with(ref->name, "refs/") && |
Jeff King | 34066f0 | 2019-04-13 01:57:37 -0400 | [diff] [blame] | 662 | check_refname_format(ref->name, 0)) { |
| 663 | /* |
| 664 | * trash or a peeled value; do not even add it to |
| 665 | * unmatched list |
| 666 | */ |
| 667 | free_one_ref(ref); |
| 668 | continue; |
| 669 | } else { |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 670 | while (i < nr_sought) { |
| 671 | int cmp = strcmp(ref->name, sought[i]->name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 672 | if (cmp < 0) |
| 673 | break; /* definitely do not have it */ |
| 674 | else if (cmp == 0) { |
| 675 | keep = 1; /* definitely have it */ |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 676 | sought[i]->match_status = REF_MATCHED; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 677 | } |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 678 | i++; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 679 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 680 | |
Jeff King | e9502c0 | 2018-06-11 01:53:57 -0400 | [diff] [blame] | 681 | if (!keep && args->fetch_all && |
| 682 | (!args->deepen || !starts_with(ref->name, "refs/tags/"))) |
| 683 | keep = 1; |
| 684 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 685 | |
| 686 | if (keep) { |
| 687 | *newtail = ref; |
| 688 | ref->next = NULL; |
| 689 | newtail = &ref->next; |
| 690 | } else { |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 691 | ref->next = unmatched; |
| 692 | unmatched = ref; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
René Scharfe | 22a1646 | 2018-10-04 17:09:39 +0200 | [diff] [blame] | 696 | if (strict) { |
| 697 | for (i = 0; i < nr_sought; i++) { |
| 698 | ref = sought[i]; |
| 699 | if (!is_unmatched_ref(ref)) |
| 700 | continue; |
| 701 | |
| 702 | add_refs_to_oidset(&tip_oids, unmatched); |
| 703 | add_refs_to_oidset(&tip_oids, newlist); |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | |
Junio C Hamano | 6e7b66e | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 708 | /* Append unmatched requests to the list */ |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 709 | for (i = 0; i < nr_sought; i++) { |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 710 | ref = sought[i]; |
René Scharfe | bf73282 | 2018-10-04 17:09:06 +0200 | [diff] [blame] | 711 | if (!is_unmatched_ref(ref)) |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 712 | continue; |
Junio C Hamano | 6e7b66e | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 713 | |
René Scharfe | 22a1646 | 2018-10-04 17:09:39 +0200 | [diff] [blame] | 714 | if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) { |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 715 | ref->match_status = REF_MATCHED; |
Jeff King | c3c17bf | 2015-03-19 16:37:09 -0400 | [diff] [blame] | 716 | *newtail = copy_ref(ref); |
| 717 | newtail = &(*newtail)->next; |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 718 | } else { |
| 719 | ref->match_status = REF_UNADVERTISED_NOT_ALLOWED; |
Junio C Hamano | 6e7b66e | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 720 | } |
| 721 | } |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 722 | |
| 723 | oidset_clear(&tip_oids); |
Jeff King | 259eddd | 2019-04-13 01:54:09 -0400 | [diff] [blame] | 724 | free_refs(unmatched); |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 725 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 726 | *refs = newlist; |
| 727 | } |
| 728 | |
Jeff King | 65daa9b | 2023-02-24 01:39:35 -0500 | [diff] [blame] | 729 | static void mark_alternate_complete(struct fetch_negotiator *negotiator UNUSED, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 730 | struct object *obj) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 731 | { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 732 | mark_complete(&obj->oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 733 | } |
| 734 | |
Takuto Ikuta | 024aa46 | 2018-03-14 15:32:42 +0900 | [diff] [blame] | 735 | /* |
Jonathan Tan | 34c2903 | 2018-06-06 13:47:07 -0700 | [diff] [blame] | 736 | * Mark recent commits available locally and reachable from a local ref as |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 737 | * COMPLETE. |
Jonathan Tan | 34c2903 | 2018-06-06 13:47:07 -0700 | [diff] [blame] | 738 | * |
| 739 | * The cutoff time for recency is determined by this heuristic: it is the |
| 740 | * earliest commit time of the objects in refs that are commits and that we know |
| 741 | * the commit time of. |
| 742 | */ |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 743 | static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 744 | struct fetch_pack_args *args, |
Jonathan Tan | 34c2903 | 2018-06-06 13:47:07 -0700 | [diff] [blame] | 745 | struct ref **refs) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 746 | { |
| 747 | struct ref *ref; |
Jonathan Tan | a1c6d7c | 2017-12-08 15:58:48 +0000 | [diff] [blame] | 748 | int old_save_commit_buffer = save_commit_buffer; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 749 | timestamp_t cutoff = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 750 | |
Robert Coup | 4dfd092 | 2022-03-28 14:02:06 +0000 | [diff] [blame] | 751 | if (args->refetch) |
| 752 | return; |
| 753 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 754 | save_commit_buffer = 0; |
| 755 | |
Erik Chen | 9e5afdf | 2019-11-19 23:02:09 +0000 | [diff] [blame] | 756 | trace2_region_enter("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 757 | for (ref = *refs; ref; ref = ref->next) { |
Patrick Steinhardt | 6fd1cc8 | 2022-02-10 13:28:09 +0100 | [diff] [blame] | 758 | struct commit *commit; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 759 | |
Patrick Steinhardt | 6fd1cc8 | 2022-02-10 13:28:09 +0100 | [diff] [blame] | 760 | commit = lookup_commit_in_graph(the_repository, &ref->old_oid); |
| 761 | if (!commit) { |
| 762 | struct object *o; |
| 763 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 764 | if (!repo_has_object_file_with_flags(the_repository, &ref->old_oid, |
| 765 | OBJECT_INFO_QUICK | |
| 766 | OBJECT_INFO_SKIP_FETCH_OBJECT)) |
Patrick Steinhardt | 6fd1cc8 | 2022-02-10 13:28:09 +0100 | [diff] [blame] | 767 | continue; |
| 768 | o = parse_object(the_repository, &ref->old_oid); |
| 769 | if (!o || o->type != OBJ_COMMIT) |
| 770 | continue; |
| 771 | |
| 772 | commit = (struct commit *)o; |
| 773 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 774 | |
Erik Chen | 9e5afdf | 2019-11-19 23:02:09 +0000 | [diff] [blame] | 775 | /* |
| 776 | * We already have it -- which may mean that we were |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 777 | * in sync with the other side at some time after |
| 778 | * that (it is OK if we guess wrong here). |
| 779 | */ |
Patrick Steinhardt | 6fd1cc8 | 2022-02-10 13:28:09 +0100 | [diff] [blame] | 780 | if (!cutoff || cutoff < commit->date) |
| 781 | cutoff = commit->date; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 782 | } |
Erik Chen | 9e5afdf | 2019-11-19 23:02:09 +0000 | [diff] [blame] | 783 | trace2_region_leave("fetch-pack", "parse_remote_refs_and_find_cutoff", NULL); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 784 | |
Erik Chen | 9e5afdf | 2019-11-19 23:02:09 +0000 | [diff] [blame] | 785 | /* |
| 786 | * This block marks all local refs as COMPLETE, and then recursively marks all |
| 787 | * parents of those refs as COMPLETE. |
| 788 | */ |
| 789 | trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL); |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 790 | if (!args->deepen) { |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 791 | for_each_rawref(mark_complete_oid, NULL); |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 792 | for_each_cached_alternate(NULL, mark_alternate_complete); |
| 793 | commit_list_sort_by_date(&complete); |
| 794 | if (cutoff) |
| 795 | mark_recent_complete_commits(args, cutoff); |
| 796 | } |
Erik Chen | 9e5afdf | 2019-11-19 23:02:09 +0000 | [diff] [blame] | 797 | trace2_region_leave("fetch-pack", "mark_complete_local_refs", NULL); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 798 | |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 799 | /* |
| 800 | * Mark all complete remote refs as common refs. |
| 801 | * Don't mark them common yet; the server has to be told so first. |
| 802 | */ |
Erik Chen | 9e5afdf | 2019-11-19 23:02:09 +0000 | [diff] [blame] | 803 | trace2_region_enter("fetch-pack", "mark_common_remote_refs", NULL); |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 804 | for (ref = *refs; ref; ref = ref->next) { |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 805 | struct commit *c = deref_without_lazy_fetch(&ref->old_oid, 0); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 806 | |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 807 | if (!c || !(c->object.flags & COMPLETE)) |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 808 | continue; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 809 | |
Jonathan Tan | 5c3b801 | 2020-08-17 21:01:35 -0700 | [diff] [blame] | 810 | negotiator->known_common(negotiator, c); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 811 | } |
Erik Chen | 9e5afdf | 2019-11-19 23:02:09 +0000 | [diff] [blame] | 812 | trace2_region_leave("fetch-pack", "mark_common_remote_refs", NULL); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 813 | |
Jonathan Tan | 34c2903 | 2018-06-06 13:47:07 -0700 | [diff] [blame] | 814 | save_commit_buffer = old_save_commit_buffer; |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * Returns 1 if every object pointed to by the given remote refs is available |
| 819 | * locally and reachable from a local ref, and 0 otherwise. |
| 820 | */ |
| 821 | static int everything_local(struct fetch_pack_args *args, |
| 822 | struct ref **refs) |
| 823 | { |
| 824 | struct ref *ref; |
| 825 | int retval; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 826 | |
| 827 | for (retval = 1, ref = *refs; ref ; ref = ref->next) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 828 | const struct object_id *remote = &ref->old_oid; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 829 | struct object *o; |
| 830 | |
Jeff King | d0229ab | 2019-06-20 03:41:14 -0400 | [diff] [blame] | 831 | o = lookup_object(the_repository, remote); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 832 | if (!o || !(o->flags & COMPLETE)) { |
| 833 | retval = 0; |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 834 | print_verbose(args, "want %s (%s)", oid_to_hex(remote), |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 835 | ref->name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 836 | continue; |
| 837 | } |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 838 | print_verbose(args, _("already have %s (%s)"), oid_to_hex(remote), |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 839 | ref->name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 840 | } |
Jonathan Tan | a1c6d7c | 2017-12-08 15:58:48 +0000 | [diff] [blame] | 841 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 842 | return retval; |
| 843 | } |
| 844 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 845 | static int sideband_demux(int in UNUSED, int out, void *data) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 846 | { |
| 847 | int *xd = data; |
Jeff King | 9ff18fa | 2016-02-24 02:44:58 -0500 | [diff] [blame] | 848 | int ret; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 849 | |
Jeff King | 9ff18fa | 2016-02-24 02:44:58 -0500 | [diff] [blame] | 850 | ret = recv_sideband("fetch-pack", xd[0], out); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 851 | close(out); |
| 852 | return ret; |
| 853 | } |
| 854 | |
Christian Couder | 9d7fa3b | 2021-01-12 09:21:58 +0100 | [diff] [blame] | 855 | static void create_promisor_file(const char *keep_name, |
| 856 | struct ref **sought, int nr_sought) |
Jonathan Tan | 5374a29 | 2019-10-14 17:12:31 -0700 | [diff] [blame] | 857 | { |
| 858 | struct strbuf promisor_name = STRBUF_INIT; |
| 859 | int suffix_stripped; |
Jonathan Tan | 5374a29 | 2019-10-14 17:12:31 -0700 | [diff] [blame] | 860 | |
| 861 | strbuf_addstr(&promisor_name, keep_name); |
| 862 | suffix_stripped = strbuf_strip_suffix(&promisor_name, ".keep"); |
| 863 | if (!suffix_stripped) |
| 864 | BUG("name of pack lockfile should end with .keep (was '%s')", |
| 865 | keep_name); |
| 866 | strbuf_addstr(&promisor_name, ".promisor"); |
| 867 | |
Christian Couder | 33add2a | 2021-01-12 09:21:59 +0100 | [diff] [blame] | 868 | write_promisor_file(promisor_name.buf, sought, nr_sought); |
Jonathan Tan | 5374a29 | 2019-10-14 17:12:31 -0700 | [diff] [blame] | 869 | |
| 870 | strbuf_release(&promisor_name); |
| 871 | } |
| 872 | |
Jonathan Tan | 5476e1e | 2021-02-22 11:20:09 -0800 | [diff] [blame] | 873 | static void parse_gitmodules_oids(int fd, struct oidset *gitmodules_oids) |
| 874 | { |
| 875 | int len = the_hash_algo->hexsz + 1; /* hash + NL */ |
| 876 | |
| 877 | do { |
| 878 | char hex_hash[GIT_MAX_HEXSZ + 1]; |
| 879 | int read_len = read_in_full(fd, hex_hash, len); |
| 880 | struct object_id oid; |
| 881 | const char *end; |
| 882 | |
| 883 | if (!read_len) |
| 884 | return; |
| 885 | if (read_len != len) |
| 886 | die("invalid length read %d", read_len); |
| 887 | if (parse_oid_hex(hex_hash, &oid, &end) || *end != '\n') |
| 888 | die("invalid hash"); |
| 889 | oidset_insert(gitmodules_oids, &oid); |
| 890 | } while (1); |
| 891 | } |
| 892 | |
Ævar Arnfjörð Bjarmason | 1f6cf45 | 2022-05-16 20:11:01 +0000 | [diff] [blame] | 893 | static void add_index_pack_keep_option(struct strvec *args) |
| 894 | { |
| 895 | char hostname[HOST_NAME_MAX + 1]; |
| 896 | |
| 897 | if (xgethostname(hostname, sizeof(hostname))) |
| 898 | xsnprintf(hostname, sizeof(hostname), "localhost"); |
| 899 | strvec_pushf(args, "--keep=fetch-pack %"PRIuMAX " on %s", |
| 900 | (uintmax_t)getpid(), hostname); |
| 901 | } |
| 902 | |
Jonathan Tan | ece9aea | 2020-08-17 12:48:19 -0700 | [diff] [blame] | 903 | /* |
Jonathan Tan | b664e9f | 2021-02-22 11:20:08 -0800 | [diff] [blame] | 904 | * If packfile URIs were provided, pass a non-NULL pointer to index_pack_args. |
| 905 | * The strings to pass as the --index-pack-arg arguments to http-fetch will be |
| 906 | * stored there. (It must be freed by the caller.) |
Jonathan Tan | ece9aea | 2020-08-17 12:48:19 -0700 | [diff] [blame] | 907 | */ |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 908 | static int get_pack(struct fetch_pack_args *args, |
Jonathan Tan | 9da69a6 | 2020-06-10 13:57:22 -0700 | [diff] [blame] | 909 | int xd[2], struct string_list *pack_lockfiles, |
Jonathan Tan | b664e9f | 2021-02-22 11:20:08 -0800 | [diff] [blame] | 910 | struct strvec *index_pack_args, |
Jonathan Tan | 5476e1e | 2021-02-22 11:20:09 -0800 | [diff] [blame] | 911 | struct ref **sought, int nr_sought, |
| 912 | struct oidset *gitmodules_oids) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 913 | { |
| 914 | struct async demux; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 915 | int do_keep = args->keep_pack; |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 916 | const char *cmd_name; |
| 917 | struct pack_header header; |
| 918 | int pass_header = 0; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 919 | struct child_process cmd = CHILD_PROCESS_INIT; |
Jonathan Tan | 5476e1e | 2021-02-22 11:20:09 -0800 | [diff] [blame] | 920 | int fsck_objects = 0; |
Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 08:16:17 +0700 | [diff] [blame] | 921 | int ret; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 922 | |
| 923 | memset(&demux, 0, sizeof(demux)); |
| 924 | if (use_sideband) { |
| 925 | /* xd[] is talking with upload-pack; subprocess reads from |
| 926 | * xd[0], spits out band#2 to stderr, and feeds us band#1 |
| 927 | * through demux->out. |
| 928 | */ |
| 929 | demux.proc = sideband_demux; |
| 930 | demux.data = xd; |
| 931 | demux.out = -1; |
Jeff King | df85757 | 2016-04-19 18:50:29 -0400 | [diff] [blame] | 932 | demux.isolate_sigpipe = 1; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 933 | if (start_async(&demux)) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 934 | die(_("fetch-pack: unable to fork off sideband demultiplexer")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 935 | } |
| 936 | else |
| 937 | demux.out = xd[0]; |
| 938 | |
Jonathan Tan | 2aec3bc | 2021-03-04 17:16:20 -0800 | [diff] [blame] | 939 | if (!args->keep_pack && unpack_limit && !index_pack_args) { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 940 | |
| 941 | if (read_pack_header(demux.out, &header)) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 942 | die(_("protocol error: bad pack header")); |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 943 | pass_header = 1; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 944 | if (ntohl(header.hdr_entries) < unpack_limit) |
| 945 | do_keep = 0; |
| 946 | else |
| 947 | do_keep = 1; |
| 948 | } |
| 949 | |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 950 | if (alternate_shallow_file) { |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 951 | strvec_push(&cmd.args, "--shallow-file"); |
| 952 | strvec_push(&cmd.args, alternate_shallow_file); |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 953 | } |
| 954 | |
Jonathan Tan | 5476e1e | 2021-02-22 11:20:09 -0800 | [diff] [blame] | 955 | if (fetch_fsck_objects >= 0 |
| 956 | ? fetch_fsck_objects |
| 957 | : transfer_fsck_objects >= 0 |
| 958 | ? transfer_fsck_objects |
| 959 | : 0) |
| 960 | fsck_objects = 1; |
| 961 | |
| 962 | if (do_keep || args->from_promisor || index_pack_args || fsck_objects) { |
| 963 | if (pack_lockfiles || fsck_objects) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 964 | cmd.out = -1; |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 965 | cmd_name = "index-pack"; |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 966 | strvec_push(&cmd.args, cmd_name); |
| 967 | strvec_push(&cmd.args, "--stdin"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 968 | if (!args->quiet && !args->no_progress) |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 969 | strvec_push(&cmd.args, "-v"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 970 | if (args->use_thin_pack) |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 971 | strvec_push(&cmd.args, "--fix-thin"); |
Ævar Arnfjörð Bjarmason | 1f6cf45 | 2022-05-16 20:11:01 +0000 | [diff] [blame] | 972 | if ((do_keep || index_pack_args) && (args->lock_pack || unpack_limit)) |
| 973 | add_index_pack_keep_option(&cmd.args); |
Jonathan Tan | b664e9f | 2021-02-22 11:20:08 -0800 | [diff] [blame] | 974 | if (!index_pack_args && args->check_self_contained_and_connected) |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 975 | strvec_push(&cmd.args, "--check-self-contained-and-connected"); |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 976 | else |
| 977 | /* |
| 978 | * We cannot perform any connectivity checks because |
| 979 | * not all packs have been downloaded; let the caller |
| 980 | * have this responsibility. |
| 981 | */ |
| 982 | args->check_self_contained_and_connected = 0; |
Jonathan Tan | 1b03df5 | 2020-08-20 10:51:16 -0700 | [diff] [blame] | 983 | |
| 984 | if (args->from_promisor) |
| 985 | /* |
Christian Couder | 9d7fa3b | 2021-01-12 09:21:58 +0100 | [diff] [blame] | 986 | * create_promisor_file() may be called afterwards but |
Jonathan Tan | 1b03df5 | 2020-08-20 10:51:16 -0700 | [diff] [blame] | 987 | * we still need index-pack to know that this is a |
| 988 | * promisor pack. For example, if transfer.fsckobjects |
| 989 | * is true, index-pack needs to know that .gitmodules |
| 990 | * is a promisor object (so that it won't complain if |
| 991 | * it is missing). |
| 992 | */ |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 993 | strvec_push(&cmd.args, "--promisor"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 994 | } |
| 995 | else { |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 996 | cmd_name = "unpack-objects"; |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 997 | strvec_push(&cmd.args, cmd_name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 998 | if (args->quiet || args->no_progress) |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 999 | strvec_push(&cmd.args, "-q"); |
Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 08:16:17 +0700 | [diff] [blame] | 1000 | args->check_self_contained_and_connected = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1001 | } |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 1002 | |
| 1003 | if (pass_header) |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1004 | strvec_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32, |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 1005 | ntohl(header.hdr_version), |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 1006 | ntohl(header.hdr_entries)); |
Jonathan Tan | 5476e1e | 2021-02-22 11:20:09 -0800 | [diff] [blame] | 1007 | if (fsck_objects) { |
Jonathan Tan | b664e9f | 2021-02-22 11:20:08 -0800 | [diff] [blame] | 1008 | if (args->from_promisor || index_pack_args) |
Jonathan Tan | 98a2ea4 | 2018-03-14 11:42:41 -0700 | [diff] [blame] | 1009 | /* |
| 1010 | * We cannot use --strict in index-pack because it |
| 1011 | * checks both broken objects and links, but we only |
| 1012 | * want to check for broken objects. |
| 1013 | */ |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1014 | strvec_push(&cmd.args, "--fsck-objects"); |
Jonathan Tan | 98a2ea4 | 2018-03-14 11:42:41 -0700 | [diff] [blame] | 1015 | else |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1016 | strvec_pushf(&cmd.args, "--strict%s", |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 1017 | fsck_msg_types.buf); |
Jonathan Tan | 98a2ea4 | 2018-03-14 11:42:41 -0700 | [diff] [blame] | 1018 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1019 | |
Jonathan Tan | b664e9f | 2021-02-22 11:20:08 -0800 | [diff] [blame] | 1020 | if (index_pack_args) { |
| 1021 | int i; |
| 1022 | |
| 1023 | for (i = 0; i < cmd.args.nr; i++) |
| 1024 | strvec_push(index_pack_args, cmd.args.v[i]); |
| 1025 | } |
| 1026 | |
Jeff King | 2a4aed4 | 2021-11-19 15:58:55 -0500 | [diff] [blame] | 1027 | sigchain_push(SIGPIPE, SIG_IGN); |
| 1028 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1029 | cmd.in = demux.out; |
| 1030 | cmd.git_cmd = 1; |
| 1031 | if (start_command(&cmd)) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 1032 | die(_("fetch-pack: unable to fork off %s"), cmd_name); |
Jonathan Tan | 5476e1e | 2021-02-22 11:20:09 -0800 | [diff] [blame] | 1033 | if (do_keep && (pack_lockfiles || fsck_objects)) { |
| 1034 | int is_well_formed; |
| 1035 | char *pack_lockfile = index_pack_lockfile(cmd.out, &is_well_formed); |
| 1036 | |
| 1037 | if (!is_well_formed) |
| 1038 | die(_("fetch-pack: invalid index-pack output")); |
René Scharfe | 6031af3 | 2020-11-30 20:27:15 +0100 | [diff] [blame] | 1039 | if (pack_lockfile) |
| 1040 | string_list_append_nodup(pack_lockfiles, pack_lockfile); |
Jonathan Tan | 5476e1e | 2021-02-22 11:20:09 -0800 | [diff] [blame] | 1041 | parse_gitmodules_oids(cmd.out, gitmodules_oids); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1042 | close(cmd.out); |
| 1043 | } |
| 1044 | |
Jens Lindstrom | 37cb1dd | 2013-10-22 15:36:02 +0200 | [diff] [blame] | 1045 | if (!use_sideband) |
| 1046 | /* Closed by start_command() */ |
| 1047 | xd[0] = -1; |
| 1048 | |
Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 08:16:17 +0700 | [diff] [blame] | 1049 | ret = finish_command(&cmd); |
| 1050 | if (!ret || (args->check_self_contained_and_connected && ret == 1)) |
| 1051 | args->self_contained_and_connected = |
| 1052 | args->check_self_contained_and_connected && |
| 1053 | ret == 0; |
| 1054 | else |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 1055 | die(_("%s failed"), cmd_name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1056 | if (use_sideband && finish_async(&demux)) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 1057 | die(_("error in sideband demultiplexer")); |
Jonathan Tan | 5374a29 | 2019-10-14 17:12:31 -0700 | [diff] [blame] | 1058 | |
Jeff King | 2a4aed4 | 2021-11-19 15:58:55 -0500 | [diff] [blame] | 1059 | sigchain_pop(SIGPIPE); |
| 1060 | |
Jonathan Tan | 5374a29 | 2019-10-14 17:12:31 -0700 | [diff] [blame] | 1061 | /* |
| 1062 | * Now that index-pack has succeeded, write the promisor file using the |
| 1063 | * obtained .keep filename if necessary |
| 1064 | */ |
Jonathan Tan | 9da69a6 | 2020-06-10 13:57:22 -0700 | [diff] [blame] | 1065 | if (do_keep && pack_lockfiles && pack_lockfiles->nr && args->from_promisor) |
Christian Couder | 9d7fa3b | 2021-01-12 09:21:58 +0100 | [diff] [blame] | 1066 | create_promisor_file(pack_lockfiles->items[0].string, sought, nr_sought); |
Jonathan Tan | 5374a29 | 2019-10-14 17:12:31 -0700 | [diff] [blame] | 1067 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1068 | return 0; |
| 1069 | } |
| 1070 | |
René Scharfe | 6fc9fec | 2022-07-16 18:59:59 +0200 | [diff] [blame] | 1071 | static int ref_compare_name(const struct ref *a, const struct ref *b) |
| 1072 | { |
| 1073 | return strcmp(a->name, b->name); |
| 1074 | } |
| 1075 | |
| 1076 | DEFINE_LIST_SORT(static, sort_ref_list, struct ref, next); |
| 1077 | |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 1078 | static int cmp_ref_by_name(const void *a_, const void *b_) |
| 1079 | { |
| 1080 | const struct ref *a = *((const struct ref **)a_); |
| 1081 | const struct ref *b = *((const struct ref **)b_); |
| 1082 | return strcmp(a->name, b->name); |
| 1083 | } |
| 1084 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1085 | static struct ref *do_fetch_pack(struct fetch_pack_args *args, |
| 1086 | int fd[2], |
| 1087 | const struct ref *orig_ref, |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 1088 | struct ref **sought, int nr_sought, |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1089 | struct shallow_info *si, |
Jonathan Tan | 9da69a6 | 2020-06-10 13:57:22 -0700 | [diff] [blame] | 1090 | struct string_list *pack_lockfiles) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1091 | { |
Derrick Stolee | aaf633c | 2019-08-13 11:37:48 -0700 | [diff] [blame] | 1092 | struct repository *r = the_repository; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1093 | struct ref *ref = copy_ref_list(orig_ref); |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 1094 | struct object_id oid; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1095 | const char *agent_feature; |
Jeff King | 7ce4c8f | 2023-04-14 17:25:20 -0400 | [diff] [blame] | 1096 | size_t agent_len; |
Jonathan Tan | 603960b | 2019-11-12 16:34:20 -0800 | [diff] [blame] | 1097 | struct fetch_negotiator negotiator_alloc; |
| 1098 | struct fetch_negotiator *negotiator; |
| 1099 | |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1100 | negotiator = &negotiator_alloc; |
Robert Coup | 4dfd092 | 2022-03-28 14:02:06 +0000 | [diff] [blame] | 1101 | if (args->refetch) { |
| 1102 | fetch_negotiator_init_noop(negotiator); |
| 1103 | } else { |
| 1104 | fetch_negotiator_init(r, negotiator); |
| 1105 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1106 | |
| 1107 | sort_ref_list(&ref, ref_compare_name); |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 1108 | QSORT(sought, nr_sought, cmp_ref_by_name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1109 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1110 | if ((agent_feature = server_feature_value("agent", &agent_len))) { |
| 1111 | agent_supported = 1; |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 1112 | if (agent_len) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 1113 | print_verbose(args, _("Server version is %.*s"), |
Jeff King | 7ce4c8f | 2023-04-14 17:25:20 -0400 | [diff] [blame] | 1114 | (int)agent_len, agent_feature); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1115 | } |
Nguyễn Thái Ngọc Duy | 0e04297 | 2019-06-20 18:59:51 +0700 | [diff] [blame] | 1116 | |
Josh Steadmon | 1e905bb | 2020-11-11 15:29:31 -0800 | [diff] [blame] | 1117 | if (!server_supports("session-id")) |
| 1118 | advertise_sid = 0; |
| 1119 | |
Nguyễn Thái Ngọc Duy | 5a88583 | 2019-06-20 18:59:50 +0700 | [diff] [blame] | 1120 | if (server_supports("shallow")) |
| 1121 | print_verbose(args, _("Server supports %s"), "shallow"); |
Derrick Stolee | aaf633c | 2019-08-13 11:37:48 -0700 | [diff] [blame] | 1122 | else if (args->depth > 0 || is_repository_shallow(r)) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1123 | die(_("Server does not support shallow clients")); |
| 1124 | if (args->depth > 0 || args->deepen_since || args->deepen_not) |
| 1125 | args->deepen = 1; |
| 1126 | if (server_supports("multi_ack_detailed")) { |
Nguyễn Thái Ngọc Duy | 0778b29 | 2019-06-20 18:59:49 +0700 | [diff] [blame] | 1127 | print_verbose(args, _("Server supports %s"), "multi_ack_detailed"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1128 | multi_ack = 2; |
| 1129 | if (server_supports("no-done")) { |
Nguyễn Thái Ngọc Duy | 0778b29 | 2019-06-20 18:59:49 +0700 | [diff] [blame] | 1130 | print_verbose(args, _("Server supports %s"), "no-done"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1131 | if (args->stateless_rpc) |
| 1132 | no_done = 1; |
| 1133 | } |
| 1134 | } |
| 1135 | else if (server_supports("multi_ack")) { |
Nguyễn Thái Ngọc Duy | 0778b29 | 2019-06-20 18:59:49 +0700 | [diff] [blame] | 1136 | print_verbose(args, _("Server supports %s"), "multi_ack"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1137 | multi_ack = 1; |
| 1138 | } |
| 1139 | if (server_supports("side-band-64k")) { |
Nguyễn Thái Ngọc Duy | 0778b29 | 2019-06-20 18:59:49 +0700 | [diff] [blame] | 1140 | print_verbose(args, _("Server supports %s"), "side-band-64k"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1141 | use_sideband = 2; |
| 1142 | } |
| 1143 | else if (server_supports("side-band")) { |
Nguyễn Thái Ngọc Duy | 0778b29 | 2019-06-20 18:59:49 +0700 | [diff] [blame] | 1144 | print_verbose(args, _("Server supports %s"), "side-band"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1145 | use_sideband = 1; |
| 1146 | } |
| 1147 | if (server_supports("allow-tip-sha1-in-want")) { |
Nguyễn Thái Ngọc Duy | 0778b29 | 2019-06-20 18:59:49 +0700 | [diff] [blame] | 1148 | print_verbose(args, _("Server supports %s"), "allow-tip-sha1-in-want"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1149 | allow_unadvertised_object_request |= ALLOW_TIP_SHA1; |
| 1150 | } |
| 1151 | if (server_supports("allow-reachable-sha1-in-want")) { |
Nguyễn Thái Ngọc Duy | 0778b29 | 2019-06-20 18:59:49 +0700 | [diff] [blame] | 1152 | print_verbose(args, _("Server supports %s"), "allow-reachable-sha1-in-want"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1153 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; |
| 1154 | } |
Nguyễn Thái Ngọc Duy | 5a88583 | 2019-06-20 18:59:50 +0700 | [diff] [blame] | 1155 | if (server_supports("thin-pack")) |
| 1156 | print_verbose(args, _("Server supports %s"), "thin-pack"); |
| 1157 | else |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1158 | args->use_thin_pack = 0; |
Nguyễn Thái Ngọc Duy | 5a88583 | 2019-06-20 18:59:50 +0700 | [diff] [blame] | 1159 | if (server_supports("no-progress")) |
| 1160 | print_verbose(args, _("Server supports %s"), "no-progress"); |
| 1161 | else |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1162 | args->no_progress = 0; |
Nguyễn Thái Ngọc Duy | 5a88583 | 2019-06-20 18:59:50 +0700 | [diff] [blame] | 1163 | if (server_supports("include-tag")) |
| 1164 | print_verbose(args, _("Server supports %s"), "include-tag"); |
| 1165 | else |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1166 | args->include_tag = 0; |
| 1167 | if (server_supports("ofs-delta")) |
Nguyễn Thái Ngọc Duy | 0778b29 | 2019-06-20 18:59:49 +0700 | [diff] [blame] | 1168 | print_verbose(args, _("Server supports %s"), "ofs-delta"); |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 1169 | else |
| 1170 | prefer_ofs_delta = 0; |
| 1171 | |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 1172 | if (server_supports("filter")) { |
| 1173 | server_supports_filtering = 1; |
Nguyễn Thái Ngọc Duy | 0778b29 | 2019-06-20 18:59:49 +0700 | [diff] [blame] | 1174 | print_verbose(args, _("Server supports %s"), "filter"); |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 1175 | } else if (args->filter_options.choice) { |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 1176 | warning("filtering not recognized by server, ignoring"); |
| 1177 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1178 | |
Nguyễn Thái Ngọc Duy | 5a88583 | 2019-06-20 18:59:50 +0700 | [diff] [blame] | 1179 | if (server_supports("deepen-since")) { |
| 1180 | print_verbose(args, _("Server supports %s"), "deepen-since"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1181 | deepen_since_ok = 1; |
Nguyễn Thái Ngọc Duy | 5a88583 | 2019-06-20 18:59:50 +0700 | [diff] [blame] | 1182 | } else if (args->deepen_since) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1183 | die(_("Server does not support --shallow-since")); |
Nguyễn Thái Ngọc Duy | 5a88583 | 2019-06-20 18:59:50 +0700 | [diff] [blame] | 1184 | if (server_supports("deepen-not")) { |
| 1185 | print_verbose(args, _("Server supports %s"), "deepen-not"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1186 | deepen_not_ok = 1; |
Nguyễn Thái Ngọc Duy | 5a88583 | 2019-06-20 18:59:50 +0700 | [diff] [blame] | 1187 | } else if (args->deepen_not) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1188 | die(_("Server does not support --shallow-exclude")); |
Nguyễn Thái Ngọc Duy | 5a88583 | 2019-06-20 18:59:50 +0700 | [diff] [blame] | 1189 | if (server_supports("deepen-relative")) |
| 1190 | print_verbose(args, _("Server supports %s"), "deepen-relative"); |
| 1191 | else if (args->deepen_relative) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1192 | die(_("Server does not support --deepen")); |
brian m. carlson | 48bf141 | 2020-05-25 19:58:59 +0000 | [diff] [blame] | 1193 | if (!server_supports_hash(the_hash_algo->name, NULL)) |
| 1194 | die(_("Server does not support this repository's object format")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1195 | |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1196 | mark_complete_and_common_ref(negotiator, args, &ref); |
| 1197 | filter_refs(args, &ref, sought, nr_sought); |
Robert Coup | 4dfd092 | 2022-03-28 14:02:06 +0000 | [diff] [blame] | 1198 | if (!args->refetch && everything_local(args, &ref)) { |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1199 | packet_flush(fd[1]); |
| 1200 | goto all_done; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1201 | } |
Jonathan Tan | 603960b | 2019-11-12 16:34:20 -0800 | [diff] [blame] | 1202 | if (find_common(negotiator, args, fd, &oid, ref) < 0) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1203 | if (!args->keep_pack) |
| 1204 | /* When cloning, it is not unusual to have |
| 1205 | * no common commit. |
| 1206 | */ |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 1207 | warning(_("no common commits")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1208 | |
| 1209 | if (args->stateless_rpc) |
| 1210 | packet_flush(fd[1]); |
Nguyễn Thái Ngọc Duy | 79891cb | 2016-06-12 17:53:56 +0700 | [diff] [blame] | 1211 | if (args->deepen) |
Nguyễn Thái Ngọc Duy | 1a30f5a | 2013-12-05 20:02:34 +0700 | [diff] [blame] | 1212 | setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, |
| 1213 | NULL); |
Li Linchao | 4fe788b | 2021-04-01 10:46:59 +0000 | [diff] [blame] | 1214 | else if (si->nr_ours || si->nr_theirs) { |
| 1215 | if (args->reject_shallow_remote) |
| 1216 | die(_("source repository is shallow, reject to clone.")); |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1217 | alternate_shallow_file = setup_temporary_shallow(si->shallow); |
Li Linchao | 4fe788b | 2021-04-01 10:46:59 +0000 | [diff] [blame] | 1218 | } else |
Nguyễn Thái Ngọc Duy | 6da8bdc | 2013-08-26 09:17:26 +0700 | [diff] [blame] | 1219 | alternate_shallow_file = NULL; |
Jonathan Tan | 5476e1e | 2021-02-22 11:20:09 -0800 | [diff] [blame] | 1220 | if (get_pack(args, fd, pack_lockfiles, NULL, sought, nr_sought, |
Ævar Arnfjörð Bjarmason | 3745e26 | 2021-03-28 15:15:51 +0200 | [diff] [blame] | 1221 | &fsck_options.gitmodules_found)) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 1222 | die(_("git fetch-pack: fetch failed.")); |
Ævar Arnfjörð Bjarmason | 3745e26 | 2021-03-28 15:15:51 +0200 | [diff] [blame] | 1223 | if (fsck_finish(&fsck_options)) |
| 1224 | die("fsck failed"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1225 | |
| 1226 | all_done: |
Jonathan Tan | 603960b | 2019-11-12 16:34:20 -0800 | [diff] [blame] | 1227 | if (negotiator) |
| 1228 | negotiator->release(negotiator); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1229 | return ref; |
| 1230 | } |
| 1231 | |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1232 | static void add_shallow_requests(struct strbuf *req_buf, |
| 1233 | const struct fetch_pack_args *args) |
| 1234 | { |
Junio C Hamano | 00624d6 | 2018-07-18 12:20:27 -0700 | [diff] [blame] | 1235 | if (is_repository_shallow(the_repository)) |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1236 | write_shallow_commits(req_buf, 1, NULL); |
| 1237 | if (args->depth > 0) |
| 1238 | packet_buf_write(req_buf, "deepen %d", args->depth); |
| 1239 | if (args->deepen_since) { |
| 1240 | timestamp_t max_age = approxidate(args->deepen_since); |
| 1241 | packet_buf_write(req_buf, "deepen-since %"PRItime, max_age); |
| 1242 | } |
| 1243 | if (args->deepen_not) { |
| 1244 | int i; |
| 1245 | for (i = 0; i < args->deepen_not->nr; i++) { |
| 1246 | struct string_list_item *s = args->deepen_not->items + i; |
| 1247 | packet_buf_write(req_buf, "deepen-not %s", s->string); |
| 1248 | } |
| 1249 | } |
Jonathan Tan | 5056cf4 | 2018-12-18 13:24:35 -0800 | [diff] [blame] | 1250 | if (args->deepen_relative) |
| 1251 | packet_buf_write(req_buf, "deepen-relative\n"); |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1254 | static void add_wants(const struct ref *wants, struct strbuf *req_buf) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1255 | { |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1256 | int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0); |
| 1257 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1258 | for ( ; wants ; wants = wants->next) { |
| 1259 | const struct object_id *remote = &wants->old_oid; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1260 | struct object *o; |
| 1261 | |
| 1262 | /* |
| 1263 | * If that object is complete (i.e. it is an ancestor of a |
| 1264 | * local ref), we tell them we have it but do not have to |
| 1265 | * tell them about its ancestors, which they already know |
| 1266 | * about. |
| 1267 | * |
| 1268 | * We use lookup_object here because we are only |
| 1269 | * interested in the case we *know* the object is |
| 1270 | * reachable and we have already scanned it. |
| 1271 | */ |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1272 | if (((o = lookup_object(the_repository, remote)) != NULL) && |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1273 | (o->flags & COMPLETE)) { |
| 1274 | continue; |
| 1275 | } |
| 1276 | |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1277 | if (!use_ref_in_want || wants->exact_oid) |
| 1278 | packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote)); |
| 1279 | else |
| 1280 | packet_buf_write(req_buf, "want-ref %s\n", wants->name); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | static void add_common(struct strbuf *req_buf, struct oidset *common) |
| 1285 | { |
| 1286 | struct oidset_iter iter; |
| 1287 | const struct object_id *oid; |
| 1288 | oidset_iter_init(common, &iter); |
| 1289 | |
| 1290 | while ((oid = oidset_iter_next(&iter))) { |
| 1291 | packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); |
| 1292 | } |
| 1293 | } |
| 1294 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1295 | static int add_haves(struct fetch_negotiator *negotiator, |
| 1296 | struct strbuf *req_buf, |
Jonathan Tan | 57c3451 | 2021-04-08 18:10:00 -0700 | [diff] [blame] | 1297 | int *haves_to_send) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1298 | { |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1299 | int haves_added = 0; |
| 1300 | const struct object_id *oid; |
| 1301 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1302 | while ((oid = negotiator->next(negotiator))) { |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1303 | packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); |
| 1304 | if (++haves_added >= *haves_to_send) |
| 1305 | break; |
| 1306 | } |
| 1307 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1308 | /* Increase haves to send on next round */ |
| 1309 | *haves_to_send = next_flush(1, *haves_to_send); |
| 1310 | |
Jonathan Tan | 57c3451 | 2021-04-08 18:10:00 -0700 | [diff] [blame] | 1311 | return haves_added; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Jonathan Tan | 6871d0c | 2021-04-08 18:10:01 -0700 | [diff] [blame] | 1314 | static void write_fetch_command_and_capabilities(struct strbuf *req_buf, |
| 1315 | const struct string_list *server_options) |
| 1316 | { |
| 1317 | const char *hash_name; |
| 1318 | |
Jeff King | a31cfe3 | 2022-12-13 05:52:58 -0500 | [diff] [blame] | 1319 | ensure_server_supports_v2("fetch"); |
| 1320 | packet_buf_write(req_buf, "command=fetch"); |
| 1321 | if (server_supports_v2("agent")) |
Jonathan Tan | 6871d0c | 2021-04-08 18:10:01 -0700 | [diff] [blame] | 1322 | packet_buf_write(req_buf, "agent=%s", git_user_agent_sanitized()); |
Jeff King | a31cfe3 | 2022-12-13 05:52:58 -0500 | [diff] [blame] | 1323 | if (advertise_sid && server_supports_v2("session-id")) |
Jonathan Tan | 6871d0c | 2021-04-08 18:10:01 -0700 | [diff] [blame] | 1324 | packet_buf_write(req_buf, "session-id=%s", trace2_session_id()); |
Jeff King | a31cfe3 | 2022-12-13 05:52:58 -0500 | [diff] [blame] | 1325 | if (server_options && server_options->nr) { |
Jonathan Tan | 6871d0c | 2021-04-08 18:10:01 -0700 | [diff] [blame] | 1326 | int i; |
Jeff King | a31cfe3 | 2022-12-13 05:52:58 -0500 | [diff] [blame] | 1327 | ensure_server_supports_v2("server-option"); |
Jonathan Tan | 6871d0c | 2021-04-08 18:10:01 -0700 | [diff] [blame] | 1328 | for (i = 0; i < server_options->nr; i++) |
| 1329 | packet_buf_write(req_buf, "server-option=%s", |
| 1330 | server_options->items[i].string); |
| 1331 | } |
| 1332 | |
| 1333 | if (server_feature_v2("object-format", &hash_name)) { |
| 1334 | int hash_algo = hash_algo_by_name(hash_name); |
| 1335 | if (hash_algo_by_ptr(the_hash_algo) != hash_algo) |
| 1336 | die(_("mismatched algorithms: client %s; server %s"), |
| 1337 | the_hash_algo->name, hash_name); |
| 1338 | packet_buf_write(req_buf, "object-format=%s", the_hash_algo->name); |
| 1339 | } else if (hash_algo_by_ptr(the_hash_algo) != GIT_HASH_SHA1) { |
| 1340 | die(_("the server does not support algorithm '%s'"), |
| 1341 | the_hash_algo->name); |
| 1342 | } |
| 1343 | packet_buf_delim(req_buf); |
| 1344 | } |
| 1345 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1346 | static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, |
Matthew DeVore | cf9ceb5 | 2019-06-27 15:54:10 -0700 | [diff] [blame] | 1347 | struct fetch_pack_args *args, |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1348 | const struct ref *wants, struct oidset *common, |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1349 | int *haves_to_send, int *in_vain, |
Jonathan Tan | 4fa3f00 | 2020-04-27 17:01:09 -0700 | [diff] [blame] | 1350 | int sideband_all, int seen_ack) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1351 | { |
Jonathan Tan | 57c3451 | 2021-04-08 18:10:00 -0700 | [diff] [blame] | 1352 | int haves_added; |
| 1353 | int done_sent = 0; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1354 | struct strbuf req_buf = STRBUF_INIT; |
| 1355 | |
Jonathan Tan | 6871d0c | 2021-04-08 18:10:01 -0700 | [diff] [blame] | 1356 | write_fetch_command_and_capabilities(&req_buf, args->server_options); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1357 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1358 | if (args->use_thin_pack) |
| 1359 | packet_buf_write(&req_buf, "thin-pack"); |
| 1360 | if (args->no_progress) |
| 1361 | packet_buf_write(&req_buf, "no-progress"); |
| 1362 | if (args->include_tag) |
| 1363 | packet_buf_write(&req_buf, "include-tag"); |
| 1364 | if (prefer_ofs_delta) |
| 1365 | packet_buf_write(&req_buf, "ofs-delta"); |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1366 | if (sideband_all) |
| 1367 | packet_buf_write(&req_buf, "sideband-all"); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1368 | |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1369 | /* Add shallow-info and deepen request */ |
| 1370 | if (server_supports_feature("fetch", "shallow", 0)) |
| 1371 | add_shallow_requests(&req_buf, args); |
Junio C Hamano | 00624d6 | 2018-07-18 12:20:27 -0700 | [diff] [blame] | 1372 | else if (is_repository_shallow(the_repository) || args->deepen) |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1373 | die(_("Server does not support shallow requests")); |
| 1374 | |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1375 | /* Add filter */ |
Jonathan Tan | 1007557 | 2022-07-26 09:27:11 -0700 | [diff] [blame] | 1376 | send_filter(args, &req_buf, |
| 1377 | server_supports_feature("fetch", "filter", 0)); |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1378 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1379 | if (server_supports_feature("fetch", "packfile-uris", 0)) { |
| 1380 | int i; |
| 1381 | struct strbuf to_send = STRBUF_INIT; |
| 1382 | |
| 1383 | for (i = 0; i < uri_protocols.nr; i++) { |
| 1384 | const char *s = uri_protocols.items[i].string; |
| 1385 | |
| 1386 | if (!strcmp(s, "https") || !strcmp(s, "http")) { |
| 1387 | if (to_send.len) |
| 1388 | strbuf_addch(&to_send, ','); |
| 1389 | strbuf_addstr(&to_send, s); |
| 1390 | } |
| 1391 | } |
| 1392 | if (to_send.len) { |
| 1393 | packet_buf_write(&req_buf, "packfile-uris %s", |
| 1394 | to_send.buf); |
| 1395 | strbuf_release(&to_send); |
| 1396 | } |
| 1397 | } |
| 1398 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1399 | /* add wants */ |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1400 | add_wants(wants, &req_buf); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1401 | |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1402 | /* Add all of the common commits we've found in previous rounds */ |
| 1403 | add_common(&req_buf, common); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1404 | |
Jonathan Tan | 57c3451 | 2021-04-08 18:10:00 -0700 | [diff] [blame] | 1405 | haves_added = add_haves(negotiator, &req_buf, haves_to_send); |
| 1406 | *in_vain += haves_added; |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 1407 | trace2_data_intmax("negotiation_v2", the_repository, "haves_added", haves_added); |
| 1408 | trace2_data_intmax("negotiation_v2", the_repository, "in_vain", *in_vain); |
Jonathan Tan | 57c3451 | 2021-04-08 18:10:00 -0700 | [diff] [blame] | 1409 | if (!haves_added || (seen_ack && *in_vain >= MAX_IN_VAIN)) { |
| 1410 | /* Send Done */ |
| 1411 | packet_buf_write(&req_buf, "done\n"); |
| 1412 | done_sent = 1; |
| 1413 | } |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1414 | |
| 1415 | /* Send request */ |
| 1416 | packet_buf_flush(&req_buf); |
Jeff King | 37c8001 | 2019-03-04 23:11:39 -0500 | [diff] [blame] | 1417 | if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0) |
| 1418 | die_errno(_("unable to write request to remote")); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1419 | |
| 1420 | strbuf_release(&req_buf); |
Jonathan Tan | 57c3451 | 2021-04-08 18:10:00 -0700 | [diff] [blame] | 1421 | return done_sent; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * Processes a section header in a server's response and checks if it matches |
| 1426 | * `section`. If the value of `peek` is 1, the header line will be peeked (and |
| 1427 | * not consumed); if 0, the line will be consumed and the function will die if |
| 1428 | * the section header doesn't match what was expected. |
| 1429 | */ |
| 1430 | static int process_section_header(struct packet_reader *reader, |
| 1431 | const char *section, int peek) |
| 1432 | { |
Jonathan Tan | 7709acf | 2022-05-16 04:02:20 -0700 | [diff] [blame] | 1433 | int ret = 0; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1434 | |
Jonathan Tan | 7709acf | 2022-05-16 04:02:20 -0700 | [diff] [blame] | 1435 | if (packet_reader_peek(reader) == PACKET_READ_NORMAL && |
| 1436 | !strcmp(reader->line, section)) |
| 1437 | ret = 1; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1438 | |
| 1439 | if (!peek) { |
Jonathan Tan | 7709acf | 2022-05-16 04:02:20 -0700 | [diff] [blame] | 1440 | if (!ret) { |
| 1441 | if (reader->line) |
| 1442 | die(_("expected '%s', received '%s'"), |
| 1443 | section, reader->line); |
| 1444 | else |
| 1445 | die(_("expected '%s'"), section); |
| 1446 | } |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1447 | packet_reader_read(reader); |
| 1448 | } |
| 1449 | |
| 1450 | return ret; |
| 1451 | } |
| 1452 | |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1453 | static int process_ack(struct fetch_negotiator *negotiator, |
| 1454 | struct packet_reader *reader, |
| 1455 | struct object_id *common_oid, |
| 1456 | int *received_ready) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1457 | { |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1458 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 1459 | const char *arg; |
| 1460 | |
| 1461 | if (!strcmp(reader->line, "NAK")) |
| 1462 | continue; |
| 1463 | |
| 1464 | if (skip_prefix(reader->line, "ACK ", &arg)) { |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1465 | if (!get_oid_hex(arg, common_oid)) { |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1466 | struct commit *commit; |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1467 | commit = lookup_commit(the_repository, common_oid); |
Jonathan Tan | 603960b | 2019-11-12 16:34:20 -0800 | [diff] [blame] | 1468 | if (negotiator) |
| 1469 | negotiator->ack(negotiator, commit); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1470 | } |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1471 | return 1; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | if (!strcmp(reader->line, "ready")) { |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1475 | *received_ready = 1; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1476 | continue; |
| 1477 | } |
| 1478 | |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1479 | die(_("unexpected acknowledgment line: '%s'"), reader->line); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | if (reader->status != PACKET_READ_FLUSH && |
| 1483 | reader->status != PACKET_READ_DELIM) |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1484 | die(_("error processing acks: %d"), reader->status); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1485 | |
Jonathan Tan | 5400b2a | 2018-10-19 15:54:04 -0700 | [diff] [blame] | 1486 | /* |
| 1487 | * If an "acknowledgments" section is sent, a packfile is sent if and |
| 1488 | * only if "ready" was sent in this section. The other sections |
| 1489 | * ("shallow-info" and "wanted-refs") are sent only if a packfile is |
| 1490 | * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH |
| 1491 | * otherwise. |
| 1492 | */ |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1493 | if (*received_ready && reader->status != PACKET_READ_DELIM) |
Bagas Sanjaya | 3d3c23b | 2021-12-22 14:58:06 +0700 | [diff] [blame] | 1494 | /* |
| 1495 | * TRANSLATORS: The parameter will be 'ready', a protocol |
| 1496 | * keyword. |
| 1497 | */ |
| 1498 | die(_("expected packfile to be sent after '%s'"), "ready"); |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1499 | if (!*received_ready && reader->status != PACKET_READ_FLUSH) |
Bagas Sanjaya | 3d3c23b | 2021-12-22 14:58:06 +0700 | [diff] [blame] | 1500 | /* |
| 1501 | * TRANSLATORS: The parameter will be 'ready', a protocol |
| 1502 | * keyword. |
| 1503 | */ |
| 1504 | die(_("expected no other sections to be sent after no '%s'"), "ready"); |
Jonathan Tan | 5400b2a | 2018-10-19 15:54:04 -0700 | [diff] [blame] | 1505 | |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1506 | return 0; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1509 | static void receive_shallow_info(struct fetch_pack_args *args, |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1510 | struct packet_reader *reader, |
| 1511 | struct oid_array *shallows, |
| 1512 | struct shallow_info *si) |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1513 | { |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1514 | int unshallow_received = 0; |
Jonathan Tan | bd0b42a | 2019-01-10 11:36:45 -0800 | [diff] [blame] | 1515 | |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1516 | process_section_header(reader, "shallow-info", 0); |
| 1517 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 1518 | const char *arg; |
| 1519 | struct object_id oid; |
| 1520 | |
| 1521 | if (skip_prefix(reader->line, "shallow ", &arg)) { |
| 1522 | if (get_oid_hex(arg, &oid)) |
| 1523 | die(_("invalid shallow line: %s"), reader->line); |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1524 | oid_array_append(shallows, &oid); |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1525 | continue; |
| 1526 | } |
| 1527 | if (skip_prefix(reader->line, "unshallow ", &arg)) { |
| 1528 | if (get_oid_hex(arg, &oid)) |
| 1529 | die(_("invalid unshallow line: %s"), reader->line); |
Jeff King | d0229ab | 2019-06-20 03:41:14 -0400 | [diff] [blame] | 1530 | if (!lookup_object(the_repository, &oid)) |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1531 | die(_("object not found: %s"), reader->line); |
| 1532 | /* make sure that it is parsed as shallow */ |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 1533 | if (!parse_object(the_repository, &oid)) |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1534 | die(_("error in object: %s"), reader->line); |
| 1535 | if (unregister_shallow(&oid)) |
| 1536 | die(_("no shallow found: %s"), reader->line); |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1537 | unshallow_received = 1; |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1538 | continue; |
| 1539 | } |
| 1540 | die(_("expected shallow/unshallow, got %s"), reader->line); |
| 1541 | } |
| 1542 | |
| 1543 | if (reader->status != PACKET_READ_FLUSH && |
| 1544 | reader->status != PACKET_READ_DELIM) |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1545 | die(_("error processing shallow info: %d"), reader->status); |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1546 | |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1547 | if (args->deepen || unshallow_received) { |
| 1548 | /* |
| 1549 | * Treat these as shallow lines caused by our depth settings. |
| 1550 | * In v0, these lines cannot cause refs to be rejected; do the |
| 1551 | * same. |
| 1552 | */ |
| 1553 | int i; |
| 1554 | |
| 1555 | for (i = 0; i < shallows->nr; i++) |
| 1556 | register_shallow(the_repository, &shallows->oid[i]); |
Jonathan Tan | bd0b42a | 2019-01-10 11:36:45 -0800 | [diff] [blame] | 1557 | setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, |
| 1558 | NULL); |
| 1559 | args->deepen = 1; |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1560 | } else if (shallows->nr) { |
| 1561 | /* |
| 1562 | * Treat these as shallow lines caused by the remote being |
| 1563 | * shallow. In v0, remote refs that reach these objects are |
| 1564 | * rejected (unless --update-shallow is set); do the same. |
| 1565 | */ |
| 1566 | prepare_shallow_info(si, shallows); |
Li Linchao | 4fe788b | 2021-04-01 10:46:59 +0000 | [diff] [blame] | 1567 | if (si->nr_ours || si->nr_theirs) { |
| 1568 | if (args->reject_shallow_remote) |
| 1569 | die(_("source repository is shallow, reject to clone.")); |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1570 | alternate_shallow_file = |
| 1571 | setup_temporary_shallow(si->shallow); |
Li Linchao | 4fe788b | 2021-04-01 10:46:59 +0000 | [diff] [blame] | 1572 | } else |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1573 | alternate_shallow_file = NULL; |
brian m. carlson | 380ebab | 2019-02-06 23:59:37 +0000 | [diff] [blame] | 1574 | } else { |
| 1575 | alternate_shallow_file = NULL; |
Jonathan Tan | bd0b42a | 2019-01-10 11:36:45 -0800 | [diff] [blame] | 1576 | } |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
Jonathan Tan | b764300 | 2019-03-27 14:11:10 -0700 | [diff] [blame] | 1579 | static int cmp_name_ref(const void *name, const void *ref) |
| 1580 | { |
| 1581 | return strcmp(name, (*(struct ref **)ref)->name); |
| 1582 | } |
| 1583 | |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1584 | static void receive_wanted_refs(struct packet_reader *reader, |
| 1585 | struct ref **sought, int nr_sought) |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1586 | { |
| 1587 | process_section_header(reader, "wanted-refs", 0); |
| 1588 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 1589 | struct object_id oid; |
| 1590 | const char *end; |
Jonathan Tan | b764300 | 2019-03-27 14:11:10 -0700 | [diff] [blame] | 1591 | struct ref **found; |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1592 | |
| 1593 | if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ') |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1594 | die(_("expected wanted-ref, got '%s'"), reader->line); |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1595 | |
Jonathan Tan | b764300 | 2019-03-27 14:11:10 -0700 | [diff] [blame] | 1596 | found = bsearch(end, sought, nr_sought, sizeof(*sought), |
| 1597 | cmp_name_ref); |
| 1598 | if (!found) |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1599 | die(_("unexpected wanted-ref: '%s'"), reader->line); |
Jonathan Tan | b764300 | 2019-03-27 14:11:10 -0700 | [diff] [blame] | 1600 | oidcpy(&(*found)->old_oid, &oid); |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | if (reader->status != PACKET_READ_DELIM) |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1604 | die(_("error processing wanted refs: %d"), reader->status); |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1607 | static void receive_packfile_uris(struct packet_reader *reader, |
| 1608 | struct string_list *uris) |
| 1609 | { |
| 1610 | process_section_header(reader, "packfile-uris", 0); |
| 1611 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 1612 | if (reader->pktlen < the_hash_algo->hexsz || |
| 1613 | reader->line[the_hash_algo->hexsz] != ' ') |
| 1614 | die("expected '<hash> <uri>', got: %s\n", reader->line); |
| 1615 | |
| 1616 | string_list_append(uris, reader->line); |
| 1617 | } |
| 1618 | if (reader->status != PACKET_READ_DELIM) |
| 1619 | die("expected DELIM"); |
| 1620 | } |
| 1621 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1622 | enum fetch_state { |
| 1623 | FETCH_CHECK_LOCAL = 0, |
| 1624 | FETCH_SEND_REQUEST, |
| 1625 | FETCH_PROCESS_ACKS, |
| 1626 | FETCH_GET_PACK, |
| 1627 | FETCH_DONE, |
| 1628 | }; |
| 1629 | |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 1630 | static void do_check_stateless_delimiter(int stateless_rpc, |
Denton Liu | b0df0c1 | 2020-05-19 06:54:00 -0400 | [diff] [blame] | 1631 | struct packet_reader *reader) |
| 1632 | { |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 1633 | check_stateless_delimiter(stateless_rpc, reader, |
Denton Liu | b0df0c1 | 2020-05-19 06:54:00 -0400 | [diff] [blame] | 1634 | _("git fetch-pack: expected response end packet")); |
| 1635 | } |
| 1636 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1637 | static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, |
| 1638 | int fd[2], |
| 1639 | const struct ref *orig_ref, |
| 1640 | struct ref **sought, int nr_sought, |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1641 | struct oid_array *shallows, |
| 1642 | struct shallow_info *si, |
Jonathan Tan | 9da69a6 | 2020-06-10 13:57:22 -0700 | [diff] [blame] | 1643 | struct string_list *pack_lockfiles) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1644 | { |
Derrick Stolee | aaf633c | 2019-08-13 11:37:48 -0700 | [diff] [blame] | 1645 | struct repository *r = the_repository; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1646 | struct ref *ref = copy_ref_list(orig_ref); |
| 1647 | enum fetch_state state = FETCH_CHECK_LOCAL; |
| 1648 | struct oidset common = OIDSET_INIT; |
| 1649 | struct packet_reader reader; |
Josh Steadmon | 5fc3118 | 2019-10-02 16:49:28 -0700 | [diff] [blame] | 1650 | int in_vain = 0, negotiation_started = 0; |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 1651 | int negotiation_round = 0; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1652 | int haves_to_send = INITIAL_FLUSH; |
Jonathan Tan | 603960b | 2019-11-12 16:34:20 -0800 | [diff] [blame] | 1653 | struct fetch_negotiator negotiator_alloc; |
| 1654 | struct fetch_negotiator *negotiator; |
Jonathan Tan | 4fa3f00 | 2020-04-27 17:01:09 -0700 | [diff] [blame] | 1655 | int seen_ack = 0; |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1656 | struct object_id common_oid; |
| 1657 | int received_ready = 0; |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1658 | struct string_list packfile_uris = STRING_LIST_INIT_DUP; |
| 1659 | int i; |
Jonathan Tan | b664e9f | 2021-02-22 11:20:08 -0800 | [diff] [blame] | 1660 | struct strvec index_pack_args = STRVEC_INIT; |
Jonathan Tan | 603960b | 2019-11-12 16:34:20 -0800 | [diff] [blame] | 1661 | |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1662 | negotiator = &negotiator_alloc; |
Robert Coup | 4dfd092 | 2022-03-28 14:02:06 +0000 | [diff] [blame] | 1663 | if (args->refetch) |
| 1664 | fetch_negotiator_init_noop(negotiator); |
| 1665 | else |
| 1666 | fetch_negotiator_init(r, negotiator); |
Jonathan Tan | 603960b | 2019-11-12 16:34:20 -0800 | [diff] [blame] | 1667 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1668 | packet_reader_init(&reader, fd[0], NULL, 0, |
Masaya Suzuki | 2d103c3 | 2018-12-29 13:19:15 -0800 | [diff] [blame] | 1669 | PACKET_READ_CHOMP_NEWLINE | |
| 1670 | PACKET_READ_DIE_ON_ERR_PACKET); |
Jonathan Tan | 07c3c2a | 2019-01-16 11:28:15 -0800 | [diff] [blame] | 1671 | if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) && |
| 1672 | server_supports_feature("fetch", "sideband-all", 0)) { |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1673 | reader.use_sideband = 1; |
| 1674 | reader.me = "fetch-pack"; |
| 1675 | } |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1676 | |
| 1677 | while (state != FETCH_DONE) { |
| 1678 | switch (state) { |
| 1679 | case FETCH_CHECK_LOCAL: |
| 1680 | sort_ref_list(&ref, ref_compare_name); |
| 1681 | QSORT(sought, nr_sought, cmp_ref_by_name); |
| 1682 | |
| 1683 | /* v2 supports these by default */ |
| 1684 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; |
| 1685 | use_sideband = 2; |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1686 | if (args->depth > 0 || args->deepen_since || args->deepen_not) |
| 1687 | args->deepen = 1; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1688 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1689 | /* Filter 'ref' by 'sought' and those that aren't local */ |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1690 | mark_complete_and_common_ref(negotiator, args, &ref); |
| 1691 | filter_refs(args, &ref, sought, nr_sought); |
Robert Coup | 4dfd092 | 2022-03-28 14:02:06 +0000 | [diff] [blame] | 1692 | if (!args->refetch && everything_local(args, &ref)) |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1693 | state = FETCH_DONE; |
| 1694 | else |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 1695 | state = FETCH_SEND_REQUEST; |
Jonathan Tan | 9dfa8db | 2020-08-17 21:01:37 -0700 | [diff] [blame] | 1696 | |
| 1697 | mark_tips(negotiator, args->negotiation_tips); |
| 1698 | for_each_cached_alternate(negotiator, |
| 1699 | insert_one_alternate_object); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1700 | break; |
| 1701 | case FETCH_SEND_REQUEST: |
Josh Steadmon | 5fc3118 | 2019-10-02 16:49:28 -0700 | [diff] [blame] | 1702 | if (!negotiation_started) { |
| 1703 | negotiation_started = 1; |
| 1704 | trace2_region_enter("fetch-pack", |
| 1705 | "negotiation_v2", |
| 1706 | the_repository); |
| 1707 | } |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 1708 | negotiation_round++; |
| 1709 | trace2_region_enter_printf("negotiation_v2", "round", |
| 1710 | the_repository, "%d", |
| 1711 | negotiation_round); |
Jonathan Tan | 603960b | 2019-11-12 16:34:20 -0800 | [diff] [blame] | 1712 | if (send_fetch_request(negotiator, fd[1], args, ref, |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1713 | &common, |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1714 | &haves_to_send, &in_vain, |
Jonathan Tan | 4fa3f00 | 2020-04-27 17:01:09 -0700 | [diff] [blame] | 1715 | reader.use_sideband, |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 1716 | seen_ack)) { |
| 1717 | trace2_region_leave_printf("negotiation_v2", "round", |
| 1718 | the_repository, "%d", |
| 1719 | negotiation_round); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1720 | state = FETCH_GET_PACK; |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 1721 | } |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1722 | else |
| 1723 | state = FETCH_PROCESS_ACKS; |
| 1724 | break; |
| 1725 | case FETCH_PROCESS_ACKS: |
| 1726 | /* Process ACKs/NAKs */ |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1727 | process_section_header(&reader, "acknowledgments", 0); |
| 1728 | while (process_ack(negotiator, &reader, &common_oid, |
| 1729 | &received_ready)) { |
| 1730 | in_vain = 0; |
| 1731 | seen_ack = 1; |
| 1732 | oidset_insert(&common, &common_oid); |
| 1733 | } |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 1734 | trace2_region_leave_printf("negotiation_v2", "round", |
| 1735 | the_repository, "%d", |
| 1736 | negotiation_round); |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1737 | if (received_ready) { |
Denton Liu | b0df0c1 | 2020-05-19 06:54:00 -0400 | [diff] [blame] | 1738 | /* |
| 1739 | * Don't check for response delimiter; get_pack() will |
| 1740 | * read the rest of this response. |
| 1741 | */ |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1742 | state = FETCH_GET_PACK; |
Jonathan Tan | 8102570 | 2021-04-08 18:09:59 -0700 | [diff] [blame] | 1743 | } else { |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 1744 | do_check_stateless_delimiter(args->stateless_rpc, &reader); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1745 | state = FETCH_SEND_REQUEST; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1746 | } |
| 1747 | break; |
| 1748 | case FETCH_GET_PACK: |
Josh Steadmon | 5fc3118 | 2019-10-02 16:49:28 -0700 | [diff] [blame] | 1749 | trace2_region_leave("fetch-pack", |
| 1750 | "negotiation_v2", |
| 1751 | the_repository); |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 1752 | trace2_data_intmax("negotiation_v2", the_repository, |
| 1753 | "total_rounds", negotiation_round); |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1754 | /* Check for shallow-info section */ |
| 1755 | if (process_section_header(&reader, "shallow-info", 1)) |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1756 | receive_shallow_info(args, &reader, shallows, si); |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1757 | |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1758 | if (process_section_header(&reader, "wanted-refs", 1)) |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1759 | receive_wanted_refs(&reader, sought, nr_sought); |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1760 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1761 | /* get the pack(s) */ |
Ivan Frade | 88e9b1e | 2021-11-10 23:51:28 +0000 | [diff] [blame] | 1762 | if (git_env_bool("GIT_TRACE_REDACT", 1)) |
| 1763 | reader.options |= PACKET_READ_REDACT_URI_PATH; |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1764 | if (process_section_header(&reader, "packfile-uris", 1)) |
| 1765 | receive_packfile_uris(&reader, &packfile_uris); |
Ivan Frade | 88e9b1e | 2021-11-10 23:51:28 +0000 | [diff] [blame] | 1766 | /* We don't expect more URIs. Reset to avoid expensive URI check. */ |
| 1767 | reader.options &= ~PACKET_READ_REDACT_URI_PATH; |
| 1768 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1769 | process_section_header(&reader, "packfile", 0); |
Jeff King | ae1a7ee | 2021-05-19 12:11:05 -0400 | [diff] [blame] | 1770 | |
| 1771 | /* |
| 1772 | * this is the final request we'll make of the server; |
| 1773 | * do a half-duplex shutdown to indicate that they can |
| 1774 | * hang up as soon as the pack is sent. |
| 1775 | */ |
| 1776 | close(fd[1]); |
| 1777 | fd[1] = -1; |
| 1778 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1779 | if (get_pack(args, fd, pack_lockfiles, |
Jonathan Tan | b664e9f | 2021-02-22 11:20:08 -0800 | [diff] [blame] | 1780 | packfile_uris.nr ? &index_pack_args : NULL, |
Ævar Arnfjörð Bjarmason | 3745e26 | 2021-03-28 15:15:51 +0200 | [diff] [blame] | 1781 | sought, nr_sought, &fsck_options.gitmodules_found)) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1782 | die(_("git fetch-pack: fetch failed.")); |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 1783 | do_check_stateless_delimiter(args->stateless_rpc, &reader); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1784 | |
| 1785 | state = FETCH_DONE; |
| 1786 | break; |
| 1787 | case FETCH_DONE: |
| 1788 | continue; |
| 1789 | } |
| 1790 | } |
| 1791 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1792 | for (i = 0; i < packfile_uris.nr; i++) { |
Jonathan Tan | b664e9f | 2021-02-22 11:20:08 -0800 | [diff] [blame] | 1793 | int j; |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1794 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 1795 | char packname[GIT_MAX_HEXSZ + 1]; |
| 1796 | const char *uri = packfile_uris.items[i].string + |
| 1797 | the_hash_algo->hexsz + 1; |
| 1798 | |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1799 | strvec_push(&cmd.args, "http-fetch"); |
| 1800 | strvec_pushf(&cmd.args, "--packfile=%.*s", |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 1801 | (int) the_hash_algo->hexsz, |
| 1802 | packfile_uris.items[i].string); |
Jonathan Tan | b664e9f | 2021-02-22 11:20:08 -0800 | [diff] [blame] | 1803 | for (j = 0; j < index_pack_args.nr; j++) |
| 1804 | strvec_pushf(&cmd.args, "--index-pack-arg=%s", |
| 1805 | index_pack_args.v[j]); |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 1806 | strvec_push(&cmd.args, uri); |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1807 | cmd.git_cmd = 1; |
| 1808 | cmd.no_stdin = 1; |
| 1809 | cmd.out = -1; |
| 1810 | if (start_command(&cmd)) |
| 1811 | die("fetch-pack: unable to spawn http-fetch"); |
| 1812 | |
| 1813 | if (read_in_full(cmd.out, packname, 5) < 0 || |
| 1814 | memcmp(packname, "keep\t", 5)) |
| 1815 | die("fetch-pack: expected keep then TAB at start of http-fetch output"); |
| 1816 | |
| 1817 | if (read_in_full(cmd.out, packname, |
| 1818 | the_hash_algo->hexsz + 1) < 0 || |
| 1819 | packname[the_hash_algo->hexsz] != '\n') |
| 1820 | die("fetch-pack: expected hash then LF at end of http-fetch output"); |
| 1821 | |
| 1822 | packname[the_hash_algo->hexsz] = '\0'; |
| 1823 | |
Ævar Arnfjörð Bjarmason | 3745e26 | 2021-03-28 15:15:51 +0200 | [diff] [blame] | 1824 | parse_gitmodules_oids(cmd.out, &fsck_options.gitmodules_found); |
Jonathan Tan | 5476e1e | 2021-02-22 11:20:09 -0800 | [diff] [blame] | 1825 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1826 | close(cmd.out); |
| 1827 | |
| 1828 | if (finish_command(&cmd)) |
| 1829 | die("fetch-pack: unable to finish http-fetch"); |
| 1830 | |
| 1831 | if (memcmp(packfile_uris.items[i].string, packname, |
| 1832 | the_hash_algo->hexsz)) |
| 1833 | die("fetch-pack: pack downloaded from %s does not match expected hash %.*s", |
| 1834 | uri, (int) the_hash_algo->hexsz, |
| 1835 | packfile_uris.items[i].string); |
| 1836 | |
| 1837 | string_list_append_nodup(pack_lockfiles, |
| 1838 | xstrfmt("%s/pack/pack-%s.keep", |
| 1839 | get_object_directory(), |
| 1840 | packname)); |
| 1841 | } |
| 1842 | string_list_clear(&packfile_uris, 0); |
Jonathan Tan | b664e9f | 2021-02-22 11:20:08 -0800 | [diff] [blame] | 1843 | strvec_clear(&index_pack_args); |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1844 | |
Ævar Arnfjörð Bjarmason | 3745e26 | 2021-03-28 15:15:51 +0200 | [diff] [blame] | 1845 | if (fsck_finish(&fsck_options)) |
| 1846 | die("fsck failed"); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1847 | |
Jonathan Tan | 603960b | 2019-11-12 16:34:20 -0800 | [diff] [blame] | 1848 | if (negotiator) |
| 1849 | negotiator->release(negotiator); |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1850 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1851 | oidset_clear(&common); |
| 1852 | return ref; |
| 1853 | } |
| 1854 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1855 | static int fetch_pack_config_cb(const char *var, const char *value, |
| 1856 | const struct config_context *ctx, void *cb) |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 1857 | { |
Jeff King | d49cb16 | 2023-12-07 02:11:35 -0500 | [diff] [blame] | 1858 | const char *msg_id; |
| 1859 | |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 1860 | if (strcmp(var, "fetch.fsck.skiplist") == 0) { |
| 1861 | const char *path; |
| 1862 | |
| 1863 | if (git_config_pathname(&path, var, value)) |
| 1864 | return 1; |
| 1865 | strbuf_addf(&fsck_msg_types, "%cskiplist=%s", |
| 1866 | fsck_msg_types.len ? ',' : '=', path); |
| 1867 | free((char *)path); |
| 1868 | return 0; |
| 1869 | } |
| 1870 | |
Jeff King | d49cb16 | 2023-12-07 02:11:35 -0500 | [diff] [blame] | 1871 | if (skip_prefix(var, "fetch.fsck.", &msg_id)) { |
| 1872 | if (!value) |
| 1873 | return config_error_nonbool(var); |
| 1874 | if (is_valid_msg_type(msg_id, value)) |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 1875 | strbuf_addf(&fsck_msg_types, "%c%s=%s", |
Jeff King | d49cb16 | 2023-12-07 02:11:35 -0500 | [diff] [blame] | 1876 | fsck_msg_types.len ? ',' : '=', msg_id, value); |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 1877 | else |
Jeff King | d49cb16 | 2023-12-07 02:11:35 -0500 | [diff] [blame] | 1878 | warning("Skipping unknown msg id '%s'", msg_id); |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 1879 | return 0; |
| 1880 | } |
| 1881 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1882 | return git_default_config(var, value, ctx, cb); |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Tanay Abhra | f44af51 | 2014-08-07 09:21:20 -0700 | [diff] [blame] | 1885 | static void fetch_pack_config(void) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1886 | { |
Tanay Abhra | f44af51 | 2014-08-07 09:21:20 -0700 | [diff] [blame] | 1887 | git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit); |
| 1888 | git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit); |
| 1889 | git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta); |
| 1890 | git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects); |
| 1891 | git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects); |
Josh Steadmon | 1e905bb | 2020-11-11 15:29:31 -0800 | [diff] [blame] | 1892 | git_config_get_bool("transfer.advertisesid", &advertise_sid); |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1893 | if (!uri_protocols.nr) { |
| 1894 | char *str; |
| 1895 | |
| 1896 | if (!git_config_get_string("fetch.uriprotocols", &str) && str) { |
| 1897 | string_list_split(&uri_protocols, str, ',', -1); |
| 1898 | free(str); |
| 1899 | } |
| 1900 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1901 | |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 1902 | git_config(fetch_pack_config_cb, NULL); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1903 | } |
| 1904 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1905 | static void fetch_pack_setup(void) |
| 1906 | { |
| 1907 | static int did_setup; |
| 1908 | if (did_setup) |
| 1909 | return; |
Tanay Abhra | f44af51 | 2014-08-07 09:21:20 -0700 | [diff] [blame] | 1910 | fetch_pack_config(); |
Junio C Hamano | f3d33f8 | 2023-08-22 18:30:21 -0700 | [diff] [blame] | 1911 | if (0 <= fetch_unpack_limit) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1912 | unpack_limit = fetch_unpack_limit; |
Junio C Hamano | f3d33f8 | 2023-08-22 18:30:21 -0700 | [diff] [blame] | 1913 | else if (0 <= transfer_unpack_limit) |
| 1914 | unpack_limit = transfer_unpack_limit; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1915 | did_setup = 1; |
| 1916 | } |
| 1917 | |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 1918 | static int remove_duplicates_in_refs(struct ref **ref, int nr) |
| 1919 | { |
| 1920 | struct string_list names = STRING_LIST_INIT_NODUP; |
| 1921 | int src, dst; |
| 1922 | |
| 1923 | for (src = dst = 0; src < nr; src++) { |
| 1924 | struct string_list_item *item; |
| 1925 | item = string_list_insert(&names, ref[src]->name); |
| 1926 | if (item->util) |
| 1927 | continue; /* already have it */ |
| 1928 | item->util = ref[src]; |
| 1929 | if (src != dst) |
| 1930 | ref[dst] = ref[src]; |
| 1931 | dst++; |
| 1932 | } |
| 1933 | for (src = dst; src < nr; src++) |
| 1934 | ref[src] = NULL; |
| 1935 | string_list_clear(&names, 0); |
| 1936 | return dst; |
| 1937 | } |
| 1938 | |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1939 | static void update_shallow(struct fetch_pack_args *args, |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1940 | struct ref **sought, int nr_sought, |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1941 | struct shallow_info *si) |
Nguyễn Thái Ngọc Duy | a796cce | 2013-12-05 20:02:37 +0700 | [diff] [blame] | 1942 | { |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1943 | struct oid_array ref = OID_ARRAY_INIT; |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1944 | int *status; |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1945 | int i; |
| 1946 | |
Nguyễn Thái Ngọc Duy | 79891cb | 2016-06-12 17:53:56 +0700 | [diff] [blame] | 1947 | if (args->deepen && alternate_shallow_file) { |
Nguyễn Thái Ngọc Duy | a796cce | 2013-12-05 20:02:37 +0700 | [diff] [blame] | 1948 | if (*alternate_shallow_file == '\0') { /* --unshallow */ |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 1949 | unlink_or_warn(git_path_shallow(the_repository)); |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 1950 | rollback_shallow_file(the_repository, &shallow_lock); |
Nguyễn Thái Ngọc Duy | a796cce | 2013-12-05 20:02:37 +0700 | [diff] [blame] | 1951 | } else |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 1952 | commit_shallow_file(the_repository, &shallow_lock); |
brian m. carlson | 23311f3 | 2019-02-04 00:06:50 +0000 | [diff] [blame] | 1953 | alternate_shallow_file = NULL; |
Nguyễn Thái Ngọc Duy | a796cce | 2013-12-05 20:02:37 +0700 | [diff] [blame] | 1954 | return; |
| 1955 | } |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1956 | |
| 1957 | if (!si->shallow || !si->shallow->nr) |
| 1958 | return; |
| 1959 | |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1960 | if (args->cloning) { |
| 1961 | /* |
| 1962 | * remote is shallow, but this is a clone, there are |
| 1963 | * no objects in repo to worry about. Accept any |
| 1964 | * shallow points that exist in the pack (iow in repo |
| 1965 | * after get_pack() and reprepare_packed_git()) |
| 1966 | */ |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1967 | struct oid_array extra = OID_ARRAY_INIT; |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 1968 | struct object_id *oid = si->shallow->oid; |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1969 | for (i = 0; i < si->shallow->nr; i++) |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 1970 | if (repo_has_object_file(the_repository, &oid[i])) |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1971 | oid_array_append(&extra, &oid[i]); |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1972 | if (extra.nr) { |
| 1973 | setup_alternate_shallow(&shallow_lock, |
| 1974 | &alternate_shallow_file, |
| 1975 | &extra); |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 1976 | commit_shallow_file(the_repository, &shallow_lock); |
brian m. carlson | 23311f3 | 2019-02-04 00:06:50 +0000 | [diff] [blame] | 1977 | alternate_shallow_file = NULL; |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1978 | } |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1979 | oid_array_clear(&extra); |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1980 | return; |
| 1981 | } |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1982 | |
| 1983 | if (!si->nr_ours && !si->nr_theirs) |
| 1984 | return; |
| 1985 | |
| 1986 | remove_nonexistent_theirs_shallow(si); |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1987 | if (!si->nr_ours && !si->nr_theirs) |
| 1988 | return; |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1989 | for (i = 0; i < nr_sought; i++) |
| 1990 | oid_array_append(&ref, &sought[i]->old_oid); |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1991 | si->ref = &ref; |
| 1992 | |
Nguyễn Thái Ngọc Duy | 48d25ca | 2013-12-05 20:02:42 +0700 | [diff] [blame] | 1993 | if (args->update_shallow) { |
| 1994 | /* |
| 1995 | * remote is also shallow, .git/shallow may be updated |
| 1996 | * so all refs can be accepted. Make sure we only add |
| 1997 | * shallow roots that are actually reachable from new |
| 1998 | * refs. |
| 1999 | */ |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 2000 | struct oid_array extra = OID_ARRAY_INIT; |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 2001 | struct object_id *oid = si->shallow->oid; |
Nguyễn Thái Ngọc Duy | 48d25ca | 2013-12-05 20:02:42 +0700 | [diff] [blame] | 2002 | assign_shallow_commits_to_refs(si, NULL, NULL); |
| 2003 | if (!si->nr_ours && !si->nr_theirs) { |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 2004 | oid_array_clear(&ref); |
Nguyễn Thái Ngọc Duy | 48d25ca | 2013-12-05 20:02:42 +0700 | [diff] [blame] | 2005 | return; |
| 2006 | } |
| 2007 | for (i = 0; i < si->nr_ours; i++) |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 2008 | oid_array_append(&extra, &oid[si->ours[i]]); |
Nguyễn Thái Ngọc Duy | 48d25ca | 2013-12-05 20:02:42 +0700 | [diff] [blame] | 2009 | for (i = 0; i < si->nr_theirs; i++) |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 2010 | oid_array_append(&extra, &oid[si->theirs[i]]); |
Nguyễn Thái Ngọc Duy | 48d25ca | 2013-12-05 20:02:42 +0700 | [diff] [blame] | 2011 | setup_alternate_shallow(&shallow_lock, |
| 2012 | &alternate_shallow_file, |
| 2013 | &extra); |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 2014 | commit_shallow_file(the_repository, &shallow_lock); |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 2015 | oid_array_clear(&extra); |
| 2016 | oid_array_clear(&ref); |
brian m. carlson | 23311f3 | 2019-02-04 00:06:50 +0000 | [diff] [blame] | 2017 | alternate_shallow_file = NULL; |
Nguyễn Thái Ngọc Duy | 48d25ca | 2013-12-05 20:02:42 +0700 | [diff] [blame] | 2018 | return; |
| 2019 | } |
| 2020 | |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 2021 | /* |
| 2022 | * remote is also shallow, check what ref is safe to update |
| 2023 | * without updating .git/shallow |
| 2024 | */ |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 2025 | CALLOC_ARRAY(status, nr_sought); |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 2026 | assign_shallow_commits_to_refs(si, NULL, status); |
| 2027 | if (si->nr_ours || si->nr_theirs) { |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 2028 | for (i = 0; i < nr_sought; i++) |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 2029 | if (status[i]) |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 2030 | sought[i]->status = REF_STATUS_REJECT_SHALLOW; |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 2031 | } |
| 2032 | free(status); |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 2033 | oid_array_clear(&ref); |
Nguyễn Thái Ngọc Duy | a796cce | 2013-12-05 20:02:37 +0700 | [diff] [blame] | 2034 | } |
| 2035 | |
Patrick Steinhardt | 9fec7b2 | 2021-09-01 15:09:50 +0200 | [diff] [blame] | 2036 | static const struct object_id *iterate_ref_map(void *cb_data) |
Jonathan Tan | cf1e7c0 | 2018-07-02 15:08:43 -0700 | [diff] [blame] | 2037 | { |
| 2038 | struct ref **rm = cb_data; |
| 2039 | struct ref *ref = *rm; |
| 2040 | |
| 2041 | if (!ref) |
Patrick Steinhardt | 9fec7b2 | 2021-09-01 15:09:50 +0200 | [diff] [blame] | 2042 | return NULL; |
Jonathan Tan | cf1e7c0 | 2018-07-02 15:08:43 -0700 | [diff] [blame] | 2043 | *rm = ref->next; |
Patrick Steinhardt | 9fec7b2 | 2021-09-01 15:09:50 +0200 | [diff] [blame] | 2044 | return &ref->old_oid; |
Jonathan Tan | cf1e7c0 | 2018-07-02 15:08:43 -0700 | [diff] [blame] | 2045 | } |
| 2046 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 2047 | struct ref *fetch_pack(struct fetch_pack_args *args, |
Jeff King | 0f804b0 | 2019-03-20 04:16:14 -0400 | [diff] [blame] | 2048 | int fd[], |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 2049 | const struct ref *ref, |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 2050 | struct ref **sought, int nr_sought, |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 2051 | struct oid_array *shallow, |
Jonathan Tan | 9da69a6 | 2020-06-10 13:57:22 -0700 | [diff] [blame] | 2052 | struct string_list *pack_lockfiles, |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 2053 | enum protocol_version version) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 2054 | { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 2055 | struct ref *ref_cpy; |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 2056 | struct shallow_info si; |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 2057 | struct oid_array shallows_scratch = OID_ARRAY_INIT; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 2058 | |
| 2059 | fetch_pack_setup(); |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 2060 | if (nr_sought) |
| 2061 | nr_sought = remove_duplicates_in_refs(sought, nr_sought); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 2062 | |
Jonathan Tan | 0177565 | 2018-09-27 12:24:05 -0700 | [diff] [blame] | 2063 | if (version != protocol_v2 && !ref) { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 2064 | packet_flush(fd[1]); |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 2065 | die(_("no matching remote head")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 2066 | } |
Jonathan Tan | 1e7d440 | 2019-03-26 12:31:20 -0700 | [diff] [blame] | 2067 | if (version == protocol_v2) { |
| 2068 | if (shallow->nr) |
| 2069 | BUG("Protocol V2 does not provide shallows at this point in the fetch"); |
| 2070 | memset(&si, 0, sizeof(si)); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 2071 | ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought, |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 2072 | &shallows_scratch, &si, |
Jonathan Tan | 9da69a6 | 2020-06-10 13:57:22 -0700 | [diff] [blame] | 2073 | pack_lockfiles); |
Jonathan Tan | 1e7d440 | 2019-03-26 12:31:20 -0700 | [diff] [blame] | 2074 | } else { |
| 2075 | prepare_shallow_info(&si, shallow); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 2076 | ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought, |
Jonathan Tan | 9da69a6 | 2020-06-10 13:57:22 -0700 | [diff] [blame] | 2077 | &si, pack_lockfiles); |
Jonathan Tan | 1e7d440 | 2019-03-26 12:31:20 -0700 | [diff] [blame] | 2078 | } |
Stefan Beller | a49d283 | 2018-03-23 18:45:21 +0100 | [diff] [blame] | 2079 | reprepare_packed_git(the_repository); |
Jonathan Tan | cf1e7c0 | 2018-07-02 15:08:43 -0700 | [diff] [blame] | 2080 | |
| 2081 | if (!args->cloning && args->deepen) { |
| 2082 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
| 2083 | struct ref *iterator = ref_cpy; |
| 2084 | opt.shallow_file = alternate_shallow_file; |
| 2085 | if (args->deepen) |
| 2086 | opt.is_deepening_fetch = 1; |
| 2087 | if (check_connected(iterate_ref_map, &iterator, &opt)) { |
| 2088 | error(_("remote did not send all necessary objects")); |
| 2089 | free_refs(ref_cpy); |
| 2090 | ref_cpy = NULL; |
Taylor Blau | 37b9dca | 2020-04-22 18:25:45 -0600 | [diff] [blame] | 2091 | rollback_shallow_file(the_repository, &shallow_lock); |
Jonathan Tan | cf1e7c0 | 2018-07-02 15:08:43 -0700 | [diff] [blame] | 2092 | goto cleanup; |
| 2093 | } |
| 2094 | args->connectivity_checked = 1; |
| 2095 | } |
| 2096 | |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 2097 | update_shallow(args, sought, nr_sought, &si); |
Jonathan Tan | cf1e7c0 | 2018-07-02 15:08:43 -0700 | [diff] [blame] | 2098 | cleanup: |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 2099 | clear_shallow_info(&si); |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 2100 | oid_array_clear(&shallows_scratch); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 2101 | return ref_cpy; |
| 2102 | } |
Matt McCutchen | e860d96 | 2017-02-22 11:01:22 -0500 | [diff] [blame] | 2103 | |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 2104 | static int add_to_object_array(const struct object_id *oid, void *data) |
| 2105 | { |
| 2106 | struct object_array *a = data; |
| 2107 | |
| 2108 | add_object_array(lookup_object(the_repository, oid), "", a); |
| 2109 | return 0; |
| 2110 | } |
| 2111 | |
| 2112 | static void clear_common_flag(struct oidset *s) |
| 2113 | { |
| 2114 | struct oidset_iter iter; |
| 2115 | const struct object_id *oid; |
| 2116 | oidset_iter_init(s, &iter); |
| 2117 | |
| 2118 | while ((oid = oidset_iter_next(&iter))) { |
| 2119 | struct object *obj = lookup_object(the_repository, oid); |
| 2120 | obj->flags &= ~COMMON; |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | void negotiate_using_fetch(const struct oid_array *negotiation_tips, |
| 2125 | const struct string_list *server_options, |
| 2126 | int stateless_rpc, |
| 2127 | int fd[], |
| 2128 | struct oidset *acked_commits) |
| 2129 | { |
| 2130 | struct fetch_negotiator negotiator; |
| 2131 | struct packet_reader reader; |
| 2132 | struct object_array nt_object_array = OBJECT_ARRAY_INIT; |
| 2133 | struct strbuf req_buf = STRBUF_INIT; |
| 2134 | int haves_to_send = INITIAL_FLUSH; |
| 2135 | int in_vain = 0; |
| 2136 | int seen_ack = 0; |
| 2137 | int last_iteration = 0; |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 2138 | int negotiation_round = 0; |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 2139 | timestamp_t min_generation = GENERATION_NUMBER_INFINITY; |
| 2140 | |
| 2141 | fetch_negotiator_init(the_repository, &negotiator); |
| 2142 | mark_tips(&negotiator, negotiation_tips); |
| 2143 | |
| 2144 | packet_reader_init(&reader, fd[0], NULL, 0, |
| 2145 | PACKET_READ_CHOMP_NEWLINE | |
| 2146 | PACKET_READ_DIE_ON_ERR_PACKET); |
| 2147 | |
| 2148 | oid_array_for_each((struct oid_array *) negotiation_tips, |
| 2149 | add_to_object_array, |
| 2150 | &nt_object_array); |
| 2151 | |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 2152 | trace2_region_enter("fetch-pack", "negotiate_using_fetch", the_repository); |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 2153 | while (!last_iteration) { |
| 2154 | int haves_added; |
| 2155 | struct object_id common_oid; |
| 2156 | int received_ready = 0; |
| 2157 | |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 2158 | negotiation_round++; |
| 2159 | |
| 2160 | trace2_region_enter_printf("negotiate_using_fetch", "round", |
| 2161 | the_repository, "%d", |
| 2162 | negotiation_round); |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 2163 | strbuf_reset(&req_buf); |
| 2164 | write_fetch_command_and_capabilities(&req_buf, server_options); |
| 2165 | |
| 2166 | packet_buf_write(&req_buf, "wait-for-done"); |
| 2167 | |
| 2168 | haves_added = add_haves(&negotiator, &req_buf, &haves_to_send); |
| 2169 | in_vain += haves_added; |
| 2170 | if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN)) |
| 2171 | last_iteration = 1; |
| 2172 | |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 2173 | trace2_data_intmax("negotiate_using_fetch", the_repository, |
| 2174 | "haves_added", haves_added); |
| 2175 | trace2_data_intmax("negotiate_using_fetch", the_repository, |
| 2176 | "in_vain", in_vain); |
| 2177 | |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 2178 | /* Send request */ |
| 2179 | packet_buf_flush(&req_buf); |
| 2180 | if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0) |
| 2181 | die_errno(_("unable to write request to remote")); |
| 2182 | |
| 2183 | /* Process ACKs/NAKs */ |
| 2184 | process_section_header(&reader, "acknowledgments", 0); |
| 2185 | while (process_ack(&negotiator, &reader, &common_oid, |
| 2186 | &received_ready)) { |
| 2187 | struct commit *commit = lookup_commit(the_repository, |
| 2188 | &common_oid); |
| 2189 | if (commit) { |
| 2190 | timestamp_t generation; |
| 2191 | |
| 2192 | parse_commit_or_die(commit); |
| 2193 | commit->object.flags |= COMMON; |
| 2194 | generation = commit_graph_generation(commit); |
| 2195 | if (generation < min_generation) |
| 2196 | min_generation = generation; |
| 2197 | } |
| 2198 | in_vain = 0; |
| 2199 | seen_ack = 1; |
| 2200 | oidset_insert(acked_commits, &common_oid); |
| 2201 | } |
| 2202 | if (received_ready) |
| 2203 | die(_("unexpected 'ready' from remote")); |
| 2204 | else |
| 2205 | do_check_stateless_delimiter(stateless_rpc, &reader); |
| 2206 | if (can_all_from_reach_with_flag(&nt_object_array, COMMON, |
| 2207 | REACH_SCRATCH, 0, |
| 2208 | min_generation)) |
| 2209 | last_iteration = 1; |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 2210 | trace2_region_leave_printf("negotiation", "round", |
| 2211 | the_repository, "%d", |
| 2212 | negotiation_round); |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 2213 | } |
Sam Delmerico | ee9895b | 2024-01-03 14:40:54 -0800 | [diff] [blame] | 2214 | trace2_region_leave("fetch-pack", "negotiate_using_fetch", the_repository); |
Josh Steadmon | a29263c | 2022-08-02 15:04:05 -0700 | [diff] [blame] | 2215 | trace2_data_intmax("negotiate_using_fetch", the_repository, |
| 2216 | "total_rounds", negotiation_round); |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 2217 | clear_common_flag(acked_commits); |
| 2218 | strbuf_release(&req_buf); |
| 2219 | } |
| 2220 | |
Matt McCutchen | e860d96 | 2017-02-22 11:01:22 -0500 | [diff] [blame] | 2221 | int report_unmatched_refs(struct ref **sought, int nr_sought) |
| 2222 | { |
| 2223 | int i, ret = 0; |
| 2224 | |
| 2225 | for (i = 0; i < nr_sought; i++) { |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 2226 | if (!sought[i]) |
Matt McCutchen | e860d96 | 2017-02-22 11:01:22 -0500 | [diff] [blame] | 2227 | continue; |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 2228 | switch (sought[i]->match_status) { |
| 2229 | case REF_MATCHED: |
| 2230 | continue; |
| 2231 | case REF_NOT_MATCHED: |
| 2232 | error(_("no such remote ref %s"), sought[i]->name); |
| 2233 | break; |
| 2234 | case REF_UNADVERTISED_NOT_ALLOWED: |
| 2235 | error(_("Server does not allow request for unadvertised object %s"), |
| 2236 | sought[i]->name); |
| 2237 | break; |
| 2238 | } |
Matt McCutchen | e860d96 | 2017-02-22 11:01:22 -0500 | [diff] [blame] | 2239 | ret = 1; |
| 2240 | } |
| 2241 | return ret; |
| 2242 | } |