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