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