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" |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 18 | #include "sha1-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" |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 25 | |
| 26 | static int transfer_unpack_limit = -1; |
| 27 | static int fetch_unpack_limit = -1; |
| 28 | static int unpack_limit = 100; |
| 29 | static int prefer_ofs_delta = 1; |
| 30 | static int no_done; |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 31 | static int deepen_since_ok; |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 32 | static int deepen_not_ok; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 33 | static int fetch_fsck_objects = -1; |
| 34 | static int transfer_fsck_objects = -1; |
| 35 | static int agent_supported; |
Jeff Hostetler | 640d8b7 | 2017-12-08 15:58:40 +0000 | [diff] [blame] | 36 | static int server_supports_filtering; |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 37 | static struct lock_file shallow_lock; |
| 38 | static const char *alternate_shallow_file; |
Jonathan Tan | 42cc748 | 2018-07-16 11:44:01 -0700 | [diff] [blame] | 39 | static char *negotiation_algorithm; |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 40 | static struct strbuf fsck_msg_types = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 41 | |
Nguyễn Thái Ngọc Duy | 208acbf | 2014-03-25 20:23:26 +0700 | [diff] [blame] | 42 | /* 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] | 43 | #define COMPLETE (1U << 0) |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 44 | #define ALTERNATE (1U << 1) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * After sending this many "have"s if we do not get any new ACK , we |
| 48 | * give up traversing our history. |
| 49 | */ |
| 50 | #define MAX_IN_VAIN 256 |
| 51 | |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 52 | static int multi_ack, use_sideband; |
Fredrik Medley | 7199c09 | 2015-05-21 22:23:38 +0200 | [diff] [blame] | 53 | /* Allow specifying sha1 if it is a ref tip. */ |
| 54 | #define ALLOW_TIP_SHA1 01 |
Fredrik Medley | 68ee628 | 2015-05-21 22:23:39 +0200 | [diff] [blame] | 55 | /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ |
| 56 | #define ALLOW_REACHABLE_SHA1 02 |
Fredrik Medley | 7199c09 | 2015-05-21 22:23:38 +0200 | [diff] [blame] | 57 | static unsigned int allow_unadvertised_object_request; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 58 | |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 59 | __attribute__((format (printf, 2, 3))) |
| 60 | static inline void print_verbose(const struct fetch_pack_args *args, |
| 61 | const char *fmt, ...) |
| 62 | { |
| 63 | va_list params; |
| 64 | |
| 65 | if (!args->verbose) |
| 66 | return; |
| 67 | |
| 68 | va_start(params, fmt); |
| 69 | vfprintf(stderr, fmt, params); |
| 70 | va_end(params); |
| 71 | fputc('\n', stderr); |
| 72 | } |
| 73 | |
Jeff King | 41a078c | 2017-02-08 15:53:03 -0500 | [diff] [blame] | 74 | struct alternate_object_cache { |
| 75 | struct object **items; |
| 76 | size_t nr, alloc; |
| 77 | }; |
| 78 | |
Jeff King | bdf4276 | 2018-10-08 11:09:23 -0700 | [diff] [blame] | 79 | static void cache_one_alternate(const struct object_id *oid, |
Jeff King | 41a078c | 2017-02-08 15:53:03 -0500 | [diff] [blame] | 80 | void *vcache) |
| 81 | { |
| 82 | struct alternate_object_cache *cache = vcache; |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 83 | struct object *obj = parse_object(the_repository, oid); |
Jeff King | 41a078c | 2017-02-08 15:53:03 -0500 | [diff] [blame] | 84 | |
| 85 | if (!obj || (obj->flags & ALTERNATE)) |
| 86 | return; |
| 87 | |
| 88 | obj->flags |= ALTERNATE; |
| 89 | ALLOC_GROW(cache->items, cache->nr + 1, cache->alloc); |
| 90 | cache->items[cache->nr++] = obj; |
| 91 | } |
| 92 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 93 | static void for_each_cached_alternate(struct fetch_negotiator *negotiator, |
| 94 | void (*cb)(struct fetch_negotiator *, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 95 | struct object *)) |
Jeff King | 41a078c | 2017-02-08 15:53:03 -0500 | [diff] [blame] | 96 | { |
| 97 | static int initialized; |
| 98 | static struct alternate_object_cache cache; |
| 99 | size_t i; |
| 100 | |
| 101 | if (!initialized) { |
| 102 | for_each_alternate_ref(cache_one_alternate, &cache); |
| 103 | initialized = 1; |
| 104 | } |
| 105 | |
| 106 | for (i = 0; i < cache.nr; i++) |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 107 | cb(negotiator, cache.items[i]); |
Jeff King | 41a078c | 2017-02-08 15:53:03 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 110 | static int rev_list_insert_ref(struct fetch_negotiator *negotiator, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 111 | const char *refname, |
| 112 | const struct object_id *oid) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 113 | { |
Stefan Beller | a74093d | 2018-06-28 18:22:05 -0700 | [diff] [blame] | 114 | struct object *o = deref_tag(the_repository, |
| 115 | parse_object(the_repository, oid), |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 116 | refname, 0); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 117 | |
| 118 | if (o && o->type == OBJ_COMMIT) |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 119 | negotiator->add_tip(negotiator, (struct commit *)o); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Michael Haggerty | b1b49c6 | 2015-05-25 18:39:18 +0000 | [diff] [blame] | 124 | static int rev_list_insert_ref_oid(const char *refname, const struct object_id *oid, |
| 125 | int flag, void *cb_data) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 126 | { |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 127 | return rev_list_insert_ref(cb_data, refname, oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | enum ack_type { |
| 131 | NAK = 0, |
| 132 | ACK, |
| 133 | ACK_continue, |
| 134 | ACK_common, |
| 135 | ACK_ready |
| 136 | }; |
| 137 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 138 | static void consume_shallow_list(struct fetch_pack_args *args, |
| 139 | struct packet_reader *reader) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 140 | { |
Nguyễn Thái Ngọc Duy | 79891cb | 2016-06-12 17:53:56 +0700 | [diff] [blame] | 141 | if (args->stateless_rpc && args->deepen) { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 142 | /* If we sent a depth we will get back "duplicate" |
| 143 | * shallow and unshallow commands every time there |
| 144 | * is a block of have lines exchanged. |
| 145 | */ |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 146 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 147 | if (starts_with(reader->line, "shallow ")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 148 | continue; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 149 | if (starts_with(reader->line, "unshallow ")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 150 | continue; |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 151 | die(_("git fetch-pack: expected shallow list")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 152 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 153 | if (reader->status != PACKET_READ_FLUSH) |
| 154 | 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] | 155 | } |
| 156 | } |
| 157 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 158 | static enum ack_type get_ack(struct packet_reader *reader, |
| 159 | struct object_id *result_oid) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 160 | { |
Jeff King | 74543a0 | 2013-02-20 15:02:57 -0500 | [diff] [blame] | 161 | int len; |
Jeff King | 82e5676 | 2014-06-18 15:56:03 -0400 | [diff] [blame] | 162 | const char *arg; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 163 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 164 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
Jeff King | bc9d4dc | 2018-02-08 13:47:49 -0500 | [diff] [blame] | 165 | die(_("git fetch-pack: expected ACK/NAK, got a flush packet")); |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 166 | len = reader->pktlen; |
| 167 | |
| 168 | if (!strcmp(reader->line, "NAK")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 169 | return NAK; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 170 | if (skip_prefix(reader->line, "ACK ", &arg)) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 171 | if (!get_oid_hex(arg, result_oid)) { |
Jeff King | 82e5676 | 2014-06-18 15:56:03 -0400 | [diff] [blame] | 172 | arg += 40; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 173 | len -= arg - reader->line; |
Jeff King | 82e5676 | 2014-06-18 15:56:03 -0400 | [diff] [blame] | 174 | if (len < 1) |
Jeff King | 030e9dd | 2013-02-20 15:00:28 -0500 | [diff] [blame] | 175 | return ACK; |
Jeff King | 82e5676 | 2014-06-18 15:56:03 -0400 | [diff] [blame] | 176 | if (strstr(arg, "continue")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 177 | return ACK_continue; |
Jeff King | 82e5676 | 2014-06-18 15:56:03 -0400 | [diff] [blame] | 178 | if (strstr(arg, "common")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 179 | return ACK_common; |
Jeff King | 82e5676 | 2014-06-18 15:56:03 -0400 | [diff] [blame] | 180 | if (strstr(arg, "ready")) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 181 | return ACK_ready; |
| 182 | return ACK; |
| 183 | } |
| 184 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 185 | 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] | 186 | } |
| 187 | |
| 188 | static void send_request(struct fetch_pack_args *args, |
| 189 | int fd, struct strbuf *buf) |
| 190 | { |
| 191 | if (args->stateless_rpc) { |
| 192 | send_sideband(fd, -1, buf->buf, buf->len, LARGE_PACKET_MAX); |
| 193 | packet_flush(fd); |
Jeff King | 37c8001 | 2019-03-04 23:11:39 -0500 | [diff] [blame] | 194 | } else { |
| 195 | if (write_in_full(fd, buf->buf, buf->len) < 0) |
| 196 | die_errno(_("unable to write to remote")); |
| 197 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 198 | } |
| 199 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 200 | static void insert_one_alternate_object(struct fetch_negotiator *negotiator, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 201 | struct object *obj) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 202 | { |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 203 | rev_list_insert_ref(negotiator, NULL, &obj->oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | #define INITIAL_FLUSH 16 |
| 207 | #define PIPESAFE_FLUSH 32 |
Jonathan Tan | da47098 | 2016-07-18 15:21:38 -0700 | [diff] [blame] | 208 | #define LARGE_FLUSH 16384 |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 209 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 210 | 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] | 211 | { |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 212 | if (stateless_rpc) { |
Jonathan Tan | da47098 | 2016-07-18 15:21:38 -0700 | [diff] [blame] | 213 | if (count < LARGE_FLUSH) |
| 214 | count <<= 1; |
| 215 | else |
| 216 | count = count * 11 / 10; |
| 217 | } else { |
| 218 | if (count < PIPESAFE_FLUSH) |
| 219 | count <<= 1; |
| 220 | else |
| 221 | count += PIPESAFE_FLUSH; |
| 222 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 223 | return count; |
| 224 | } |
| 225 | |
Jonathan Tan | 3390e42 | 2018-07-02 15:39:44 -0700 | [diff] [blame] | 226 | static void mark_tips(struct fetch_negotiator *negotiator, |
| 227 | const struct oid_array *negotiation_tips) |
| 228 | { |
| 229 | int i; |
| 230 | |
| 231 | if (!negotiation_tips) { |
| 232 | for_each_ref(rev_list_insert_ref_oid, negotiator); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | for (i = 0; i < negotiation_tips->nr; i++) |
| 237 | rev_list_insert_ref(negotiator, NULL, |
| 238 | &negotiation_tips->oid[i]); |
| 239 | return; |
| 240 | } |
| 241 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 242 | static int find_common(struct fetch_negotiator *negotiator, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 243 | struct fetch_pack_args *args, |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 244 | 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] | 245 | struct ref *refs) |
| 246 | { |
| 247 | int fetching; |
| 248 | int count = 0, flushes = 0, flush_at = INITIAL_FLUSH, retval; |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 249 | const struct object_id *oid; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 250 | unsigned in_vain = 0; |
| 251 | int got_continue = 0; |
| 252 | int got_ready = 0; |
| 253 | struct strbuf req_buf = STRBUF_INIT; |
| 254 | size_t state_len = 0; |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 255 | struct packet_reader reader; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 256 | |
| 257 | if (args->stateless_rpc && multi_ack == 1) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 258 | die(_("--stateless-rpc requires multi_ack_detailed")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 259 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 260 | packet_reader_init(&reader, fd[0], NULL, 0, |
Masaya Suzuki | 2d103c3 | 2018-12-29 13:19:15 -0800 | [diff] [blame] | 261 | PACKET_READ_CHOMP_NEWLINE | |
| 262 | PACKET_READ_DIE_ON_ERR_PACKET); |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 263 | |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 264 | if (!args->no_dependents) { |
| 265 | mark_tips(negotiator, args->negotiation_tips); |
| 266 | for_each_cached_alternate(negotiator, insert_one_alternate_object); |
| 267 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 268 | |
| 269 | fetching = 0; |
| 270 | for ( ; refs ; refs = refs->next) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 271 | struct object_id *remote = &refs->old_oid; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 272 | const char *remote_hex; |
| 273 | struct object *o; |
| 274 | |
| 275 | /* |
| 276 | * If that object is complete (i.e. it is an ancestor of a |
| 277 | * local ref), we tell them we have it but do not have to |
| 278 | * tell them about its ancestors, which they already know |
| 279 | * about. |
| 280 | * |
| 281 | * We use lookup_object here because we are only |
| 282 | * interested in the case we *know* the object is |
| 283 | * reachable and we have already scanned it. |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 284 | * |
| 285 | * Do this only if args->no_dependents is false (if it is true, |
| 286 | * we cannot trust the object flags). |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 287 | */ |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 288 | if (!args->no_dependents && |
| 289 | ((o = lookup_object(the_repository, remote->hash)) != NULL) && |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 290 | (o->flags & COMPLETE)) { |
| 291 | continue; |
| 292 | } |
| 293 | |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 294 | remote_hex = oid_to_hex(remote); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 295 | if (!fetching) { |
| 296 | struct strbuf c = STRBUF_INIT; |
| 297 | if (multi_ack == 2) strbuf_addstr(&c, " multi_ack_detailed"); |
| 298 | if (multi_ack == 1) strbuf_addstr(&c, " multi_ack"); |
| 299 | if (no_done) strbuf_addstr(&c, " no-done"); |
| 300 | if (use_sideband == 2) strbuf_addstr(&c, " side-band-64k"); |
| 301 | 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] | 302 | 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] | 303 | if (args->use_thin_pack) strbuf_addstr(&c, " thin-pack"); |
| 304 | if (args->no_progress) strbuf_addstr(&c, " no-progress"); |
| 305 | if (args->include_tag) strbuf_addstr(&c, " include-tag"); |
| 306 | 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] | 307 | 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] | 308 | 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] | 309 | if (agent_supported) strbuf_addf(&c, " agent=%s", |
| 310 | git_user_agent_sanitized()); |
Jeff Hostetler | 640d8b7 | 2017-12-08 15:58:40 +0000 | [diff] [blame] | 311 | if (args->filter_options.choice) |
| 312 | strbuf_addstr(&c, " filter"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 313 | packet_buf_write(&req_buf, "want %s%s\n", remote_hex, c.buf); |
| 314 | strbuf_release(&c); |
| 315 | } else |
| 316 | packet_buf_write(&req_buf, "want %s\n", remote_hex); |
| 317 | fetching++; |
| 318 | } |
| 319 | |
| 320 | if (!fetching) { |
| 321 | strbuf_release(&req_buf); |
| 322 | packet_flush(fd[1]); |
| 323 | return 1; |
| 324 | } |
| 325 | |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 326 | if (is_repository_shallow(the_repository)) |
Nguyễn Thái Ngọc Duy | 1a30f5a | 2013-12-05 20:02:34 +0700 | [diff] [blame] | 327 | write_shallow_commits(&req_buf, 1, NULL); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 328 | if (args->depth > 0) |
| 329 | 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] | 330 | if (args->deepen_since) { |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 331 | timestamp_t max_age = approxidate(args->deepen_since); |
Johannes Schindelin | cb71f8b | 2017-04-21 12:45:48 +0200 | [diff] [blame] | 332 | 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] | 333 | } |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 334 | if (args->deepen_not) { |
| 335 | int i; |
| 336 | for (i = 0; i < args->deepen_not->nr; i++) { |
| 337 | struct string_list_item *s = args->deepen_not->items + i; |
| 338 | packet_buf_write(&req_buf, "deepen-not %s", s->string); |
| 339 | } |
| 340 | } |
Josh Steadmon | 87c2d9d | 2019-01-07 16:17:09 -0800 | [diff] [blame] | 341 | if (server_supports_filtering && args->filter_options.choice) { |
| 342 | struct strbuf expanded_filter_spec = STRBUF_INIT; |
| 343 | expand_list_objects_filter_spec(&args->filter_options, |
| 344 | &expanded_filter_spec); |
Jeff Hostetler | 640d8b7 | 2017-12-08 15:58:40 +0000 | [diff] [blame] | 345 | packet_buf_write(&req_buf, "filter %s", |
Josh Steadmon | 87c2d9d | 2019-01-07 16:17:09 -0800 | [diff] [blame] | 346 | expanded_filter_spec.buf); |
| 347 | strbuf_release(&expanded_filter_spec); |
| 348 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 349 | packet_buf_flush(&req_buf); |
| 350 | state_len = req_buf.len; |
| 351 | |
Nguyễn Thái Ngọc Duy | 79891cb | 2016-06-12 17:53:56 +0700 | [diff] [blame] | 352 | if (args->deepen) { |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 353 | const char *arg; |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 354 | struct object_id oid; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 355 | |
| 356 | send_request(args, fd[1], &req_buf); |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 357 | while (packet_reader_read(&reader) == PACKET_READ_NORMAL) { |
| 358 | if (skip_prefix(reader.line, "shallow ", &arg)) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 359 | if (get_oid_hex(arg, &oid)) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 360 | die(_("invalid shallow line: %s"), reader.line); |
Stefan Beller | 19143f1 | 2018-05-17 15:51:44 -0700 | [diff] [blame] | 361 | register_shallow(the_repository, &oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 362 | continue; |
| 363 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 364 | if (skip_prefix(reader.line, "unshallow ", &arg)) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 365 | if (get_oid_hex(arg, &oid)) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 366 | die(_("invalid unshallow line: %s"), reader.line); |
Stefan Beller | 5abddd1 | 2018-06-28 18:21:52 -0700 | [diff] [blame] | 367 | if (!lookup_object(the_repository, oid.hash)) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 368 | die(_("object not found: %s"), reader.line); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 369 | /* make sure that it is parsed as shallow */ |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 370 | if (!parse_object(the_repository, &oid)) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 371 | die(_("error in object: %s"), reader.line); |
brian m. carlson | e92b848 | 2017-05-06 22:10:06 +0000 | [diff] [blame] | 372 | if (unregister_shallow(&oid)) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 373 | die(_("no shallow found: %s"), reader.line); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 374 | continue; |
| 375 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 376 | 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] | 377 | } |
| 378 | } else if (!args->stateless_rpc) |
| 379 | send_request(args, fd[1], &req_buf); |
| 380 | |
| 381 | if (!args->stateless_rpc) { |
| 382 | /* If we aren't using the stateless-rpc interface |
| 383 | * we don't need to retain the headers. |
| 384 | */ |
| 385 | strbuf_setlen(&req_buf, 0); |
| 386 | state_len = 0; |
| 387 | } |
| 388 | |
| 389 | flushes = 0; |
| 390 | retval = -1; |
Jonathan Tan | 88e2f9e | 2017-12-05 16:58:49 +0000 | [diff] [blame] | 391 | if (args->no_dependents) |
| 392 | goto done; |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 393 | while ((oid = negotiator->next(negotiator))) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 394 | packet_buf_write(&req_buf, "have %s\n", oid_to_hex(oid)); |
| 395 | 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] | 396 | in_vain++; |
| 397 | if (flush_at <= ++count) { |
| 398 | int ack; |
| 399 | |
| 400 | packet_buf_flush(&req_buf); |
| 401 | send_request(args, fd[1], &req_buf); |
| 402 | strbuf_setlen(&req_buf, state_len); |
| 403 | flushes++; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 404 | 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] | 405 | |
| 406 | /* |
| 407 | * We keep one window "ahead" of the other side, and |
| 408 | * will wait for an ACK only on the next one |
| 409 | */ |
| 410 | if (!args->stateless_rpc && count == INITIAL_FLUSH) |
| 411 | continue; |
| 412 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 413 | consume_shallow_list(args, &reader); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 414 | do { |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 415 | ack = get_ack(&reader, result_oid); |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 416 | if (ack) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 417 | print_verbose(args, _("got %s %d %s"), "ack", |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 418 | ack, oid_to_hex(result_oid)); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 419 | switch (ack) { |
| 420 | case ACK: |
| 421 | flushes = 0; |
| 422 | multi_ack = 0; |
| 423 | retval = 0; |
| 424 | goto done; |
| 425 | case ACK_common: |
| 426 | case ACK_ready: |
| 427 | case ACK_continue: { |
| 428 | struct commit *commit = |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 429 | lookup_commit(the_repository, |
| 430 | result_oid); |
Jonathan Tan | d093bc7 | 2018-06-14 15:54:27 -0700 | [diff] [blame] | 431 | int was_common; |
Junio C Hamano | 3a2a1dc | 2018-08-02 15:30:42 -0700 | [diff] [blame] | 432 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 433 | if (!commit) |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 434 | die(_("invalid commit %s"), oid_to_hex(result_oid)); |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 435 | was_common = negotiator->ack(negotiator, commit); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 436 | if (args->stateless_rpc |
| 437 | && ack == ACK_common |
Jonathan Tan | d093bc7 | 2018-06-14 15:54:27 -0700 | [diff] [blame] | 438 | && !was_common) { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 439 | /* We need to replay the have for this object |
| 440 | * on the next RPC request so the peer knows |
| 441 | * it is in common with us. |
| 442 | */ |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 443 | 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] | 444 | packet_buf_write(&req_buf, "have %s\n", hex); |
| 445 | state_len = req_buf.len; |
Jonathan Tan | 06b3d38 | 2016-09-23 10:41:35 -0700 | [diff] [blame] | 446 | /* |
| 447 | * Reset in_vain because an ack |
| 448 | * for this commit has not been |
| 449 | * seen. |
| 450 | */ |
| 451 | in_vain = 0; |
| 452 | } else if (!args->stateless_rpc |
| 453 | || ack != ACK_common) |
| 454 | in_vain = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 455 | retval = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 456 | got_continue = 1; |
Jonathan Tan | 21bcf6e | 2018-06-14 15:54:24 -0700 | [diff] [blame] | 457 | if (ack == ACK_ready) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 458 | got_ready = 1; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | } |
| 462 | } while (ack); |
| 463 | flushes--; |
| 464 | 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] | 465 | print_verbose(args, _("giving up")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 466 | break; /* give up */ |
| 467 | } |
Jonathan Tan | 21bcf6e | 2018-06-14 15:54:24 -0700 | [diff] [blame] | 468 | if (got_ready) |
| 469 | break; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | done: |
| 473 | if (!got_ready || !no_done) { |
| 474 | packet_buf_write(&req_buf, "done\n"); |
| 475 | send_request(args, fd[1], &req_buf); |
| 476 | } |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 477 | print_verbose(args, _("done")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 478 | if (retval != 0) { |
| 479 | multi_ack = 0; |
| 480 | flushes++; |
| 481 | } |
| 482 | strbuf_release(&req_buf); |
| 483 | |
Nguyễn Thái Ngọc Duy | ff62eca | 2014-02-06 22:10:39 +0700 | [diff] [blame] | 484 | if (!got_ready || !no_done) |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 485 | consume_shallow_list(args, &reader); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 486 | while (flushes || multi_ack) { |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 487 | int ack = get_ack(&reader, result_oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 488 | if (ack) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 489 | print_verbose(args, _("got %s (%d) %s"), "ack", |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 490 | ack, oid_to_hex(result_oid)); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 491 | if (ack == ACK) |
| 492 | return 0; |
| 493 | multi_ack = 1; |
| 494 | continue; |
| 495 | } |
| 496 | flushes--; |
| 497 | } |
| 498 | /* it is no error to fetch into a completely empty repo */ |
| 499 | return count ? retval : 0; |
| 500 | } |
| 501 | |
| 502 | static struct commit_list *complete; |
| 503 | |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 504 | 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] | 505 | { |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 506 | struct object *o = parse_object(the_repository, oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 507 | |
| 508 | while (o && o->type == OBJ_TAG) { |
| 509 | struct tag *t = (struct tag *) o; |
| 510 | if (!t->tagged) |
| 511 | break; /* broken repository */ |
| 512 | o->flags |= COMPLETE; |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 513 | o = parse_object(the_repository, &t->tagged->oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 514 | } |
| 515 | if (o && o->type == OBJ_COMMIT) { |
| 516 | struct commit *commit = (struct commit *)o; |
| 517 | if (!(commit->object.flags & COMPLETE)) { |
| 518 | commit->object.flags |= COMPLETE; |
Jeff King | 1644524 | 2013-07-02 02:16:23 -0400 | [diff] [blame] | 519 | commit_list_insert(commit, &complete); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | return 0; |
| 523 | } |
| 524 | |
Michael Haggerty | f8ee4d8 | 2015-05-25 18:39:16 +0000 | [diff] [blame] | 525 | static int mark_complete_oid(const char *refname, const struct object_id *oid, |
| 526 | int flag, void *cb_data) |
| 527 | { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 528 | return mark_complete(oid); |
Michael Haggerty | f8ee4d8 | 2015-05-25 18:39:16 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 531 | static void mark_recent_complete_commits(struct fetch_pack_args *args, |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 532 | timestamp_t cutoff) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 533 | { |
| 534 | while (complete && cutoff <= complete->item->date) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 535 | print_verbose(args, _("Marking %s as complete"), |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 536 | oid_to_hex(&complete->item->object.oid)); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 537 | pop_most_recent_commit(&complete, COMPLETE); |
| 538 | } |
| 539 | } |
| 540 | |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 541 | static void add_refs_to_oidset(struct oidset *oids, struct ref *refs) |
| 542 | { |
| 543 | for (; refs; refs = refs->next) |
| 544 | oidset_insert(oids, &refs->old_oid); |
| 545 | } |
| 546 | |
René Scharfe | bf73282 | 2018-10-04 17:09:06 +0200 | [diff] [blame] | 547 | static int is_unmatched_ref(const struct ref *ref) |
| 548 | { |
| 549 | struct object_id oid; |
| 550 | const char *p; |
| 551 | return ref->match_status == REF_NOT_MATCHED && |
| 552 | !parse_oid_hex(ref->name, &oid, &p) && |
| 553 | *p == '\0' && |
| 554 | oideq(&oid, &ref->old_oid); |
| 555 | } |
| 556 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 557 | static void filter_refs(struct fetch_pack_args *args, |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 558 | struct ref **refs, |
| 559 | struct ref **sought, int nr_sought) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 560 | { |
| 561 | struct ref *newlist = NULL; |
| 562 | struct ref **newtail = &newlist; |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 563 | struct ref *unmatched = NULL; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 564 | struct ref *ref, *next; |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 565 | struct oidset tip_oids = OIDSET_INIT; |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 566 | int i; |
René Scharfe | 22a1646 | 2018-10-04 17:09:39 +0200 | [diff] [blame] | 567 | int strict = !(allow_unadvertised_object_request & |
| 568 | (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 569 | |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 570 | i = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 571 | for (ref = *refs; ref; ref = next) { |
| 572 | int keep = 0; |
| 573 | next = ref->next; |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 574 | |
René Scharfe | 50e19a8 | 2014-06-06 19:24:48 +0200 | [diff] [blame] | 575 | if (starts_with(ref->name, "refs/") && |
Jeff King | 34066f0 | 2019-04-13 01:57:37 -0400 | [diff] [blame] | 576 | check_refname_format(ref->name, 0)) { |
| 577 | /* |
| 578 | * trash or a peeled value; do not even add it to |
| 579 | * unmatched list |
| 580 | */ |
| 581 | free_one_ref(ref); |
| 582 | continue; |
| 583 | } else { |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 584 | while (i < nr_sought) { |
| 585 | 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] | 586 | if (cmp < 0) |
| 587 | break; /* definitely do not have it */ |
| 588 | else if (cmp == 0) { |
| 589 | keep = 1; /* definitely have it */ |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 590 | sought[i]->match_status = REF_MATCHED; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 591 | } |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 592 | i++; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 593 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 594 | |
Jeff King | e9502c0 | 2018-06-11 01:53:57 -0400 | [diff] [blame] | 595 | if (!keep && args->fetch_all && |
| 596 | (!args->deepen || !starts_with(ref->name, "refs/tags/"))) |
| 597 | keep = 1; |
| 598 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 599 | |
| 600 | if (keep) { |
| 601 | *newtail = ref; |
| 602 | ref->next = NULL; |
| 603 | newtail = &ref->next; |
| 604 | } else { |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 605 | ref->next = unmatched; |
| 606 | unmatched = ref; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
René Scharfe | 22a1646 | 2018-10-04 17:09:39 +0200 | [diff] [blame] | 610 | if (strict) { |
| 611 | for (i = 0; i < nr_sought; i++) { |
| 612 | ref = sought[i]; |
| 613 | if (!is_unmatched_ref(ref)) |
| 614 | continue; |
| 615 | |
| 616 | add_refs_to_oidset(&tip_oids, unmatched); |
| 617 | add_refs_to_oidset(&tip_oids, newlist); |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | |
Junio C Hamano | 6e7b66e | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 622 | /* Append unmatched requests to the list */ |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 623 | for (i = 0; i < nr_sought; i++) { |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 624 | ref = sought[i]; |
René Scharfe | bf73282 | 2018-10-04 17:09:06 +0200 | [diff] [blame] | 625 | if (!is_unmatched_ref(ref)) |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 626 | continue; |
Junio C Hamano | 6e7b66e | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 627 | |
René Scharfe | 22a1646 | 2018-10-04 17:09:39 +0200 | [diff] [blame] | 628 | if (!strict || oidset_contains(&tip_oids, &ref->old_oid)) { |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 629 | ref->match_status = REF_MATCHED; |
Jeff King | c3c17bf | 2015-03-19 16:37:09 -0400 | [diff] [blame] | 630 | *newtail = copy_ref(ref); |
| 631 | newtail = &(*newtail)->next; |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 632 | } else { |
| 633 | ref->match_status = REF_UNADVERTISED_NOT_ALLOWED; |
Junio C Hamano | 6e7b66e | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 634 | } |
| 635 | } |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 636 | |
| 637 | oidset_clear(&tip_oids); |
Jeff King | 259eddd | 2019-04-13 01:54:09 -0400 | [diff] [blame] | 638 | free_refs(unmatched); |
Jonathan Tan | fdb69d3 | 2017-05-15 10:32:20 -0700 | [diff] [blame] | 639 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 640 | *refs = newlist; |
| 641 | } |
| 642 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 643 | static void mark_alternate_complete(struct fetch_negotiator *unused, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 644 | struct object *obj) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 645 | { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 646 | mark_complete(&obj->oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 647 | } |
| 648 | |
Takuto Ikuta | 024aa46 | 2018-03-14 15:32:42 +0900 | [diff] [blame] | 649 | struct loose_object_iter { |
| 650 | struct oidset *loose_object_set; |
| 651 | struct ref *refs; |
| 652 | }; |
| 653 | |
| 654 | /* |
Jonathan Tan | 34c2903 | 2018-06-06 13:47:07 -0700 | [diff] [blame] | 655 | * Mark recent commits available locally and reachable from a local ref as |
| 656 | * COMPLETE. If args->no_dependents is false, also mark COMPLETE remote refs as |
| 657 | * COMMON_REF (otherwise, we are not planning to participate in negotiation, and |
| 658 | * thus do not need COMMON_REF marks). |
| 659 | * |
| 660 | * The cutoff time for recency is determined by this heuristic: it is the |
| 661 | * earliest commit time of the objects in refs that are commits and that we know |
| 662 | * the commit time of. |
| 663 | */ |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 664 | static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 665 | struct fetch_pack_args *args, |
Jonathan Tan | 34c2903 | 2018-06-06 13:47:07 -0700 | [diff] [blame] | 666 | struct ref **refs) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 667 | { |
| 668 | struct ref *ref; |
Jonathan Tan | a1c6d7c | 2017-12-08 15:58:48 +0000 | [diff] [blame] | 669 | int old_save_commit_buffer = save_commit_buffer; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 670 | timestamp_t cutoff = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 671 | |
| 672 | save_commit_buffer = 0; |
| 673 | |
| 674 | for (ref = *refs; ref; ref = ref->next) { |
| 675 | struct object *o; |
| 676 | |
Jeff King | 97b2fa0 | 2018-11-12 09:55:58 -0500 | [diff] [blame] | 677 | if (!has_object_file_with_flags(&ref->old_oid, |
| 678 | OBJECT_INFO_QUICK)) |
Junio C Hamano | 012a1bb | 2013-01-26 19:42:09 -0800 | [diff] [blame] | 679 | continue; |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 680 | o = parse_object(the_repository, &ref->old_oid); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 681 | if (!o) |
| 682 | continue; |
| 683 | |
| 684 | /* We already have it -- which may mean that we were |
| 685 | * in sync with the other side at some time after |
| 686 | * that (it is OK if we guess wrong here). |
| 687 | */ |
| 688 | if (o->type == OBJ_COMMIT) { |
| 689 | struct commit *commit = (struct commit *)o; |
| 690 | if (!cutoff || cutoff < commit->date) |
| 691 | cutoff = commit->date; |
| 692 | } |
| 693 | } |
| 694 | |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 695 | if (!args->deepen) { |
| 696 | for_each_ref(mark_complete_oid, NULL); |
| 697 | for_each_cached_alternate(NULL, mark_alternate_complete); |
| 698 | commit_list_sort_by_date(&complete); |
| 699 | if (cutoff) |
| 700 | mark_recent_complete_commits(args, cutoff); |
| 701 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 702 | |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 703 | /* |
| 704 | * Mark all complete remote refs as common refs. |
| 705 | * Don't mark them common yet; the server has to be told so first. |
| 706 | */ |
| 707 | for (ref = *refs; ref; ref = ref->next) { |
| 708 | struct object *o = deref_tag(the_repository, |
| 709 | lookup_object(the_repository, |
| 710 | ref->old_oid.hash), |
| 711 | NULL, 0); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 712 | |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 713 | if (!o || o->type != OBJ_COMMIT || !(o->flags & COMPLETE)) |
| 714 | continue; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 715 | |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 716 | negotiator->known_common(negotiator, |
| 717 | (struct commit *)o); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 718 | } |
| 719 | |
Jonathan Tan | 34c2903 | 2018-06-06 13:47:07 -0700 | [diff] [blame] | 720 | save_commit_buffer = old_save_commit_buffer; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * Returns 1 if every object pointed to by the given remote refs is available |
| 725 | * locally and reachable from a local ref, and 0 otherwise. |
| 726 | */ |
| 727 | static int everything_local(struct fetch_pack_args *args, |
| 728 | struct ref **refs) |
| 729 | { |
| 730 | struct ref *ref; |
| 731 | int retval; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 732 | |
| 733 | for (retval = 1, ref = *refs; ref ; ref = ref->next) { |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 734 | 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] | 735 | struct object *o; |
| 736 | |
Stefan Beller | 5abddd1 | 2018-06-28 18:21:52 -0700 | [diff] [blame] | 737 | o = lookup_object(the_repository, remote->hash); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 738 | if (!o || !(o->flags & COMPLETE)) { |
| 739 | retval = 0; |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 740 | 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] | 741 | ref->name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 742 | continue; |
| 743 | } |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 744 | 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] | 745 | ref->name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 746 | } |
Jonathan Tan | a1c6d7c | 2017-12-08 15:58:48 +0000 | [diff] [blame] | 747 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 748 | return retval; |
| 749 | } |
| 750 | |
| 751 | static int sideband_demux(int in, int out, void *data) |
| 752 | { |
| 753 | int *xd = data; |
Jeff King | 9ff18fa | 2016-02-24 02:44:58 -0500 | [diff] [blame] | 754 | int ret; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 755 | |
Jeff King | 9ff18fa | 2016-02-24 02:44:58 -0500 | [diff] [blame] | 756 | 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] | 757 | close(out); |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | static int get_pack(struct fetch_pack_args *args, |
| 762 | int xd[2], char **pack_lockfile) |
| 763 | { |
| 764 | struct async demux; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 765 | int do_keep = args->keep_pack; |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 766 | const char *cmd_name; |
| 767 | struct pack_header header; |
| 768 | int pass_header = 0; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 769 | struct child_process cmd = CHILD_PROCESS_INIT; |
Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 08:16:17 +0700 | [diff] [blame] | 770 | int ret; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 771 | |
| 772 | memset(&demux, 0, sizeof(demux)); |
| 773 | if (use_sideband) { |
| 774 | /* xd[] is talking with upload-pack; subprocess reads from |
| 775 | * xd[0], spits out band#2 to stderr, and feeds us band#1 |
| 776 | * through demux->out. |
| 777 | */ |
| 778 | demux.proc = sideband_demux; |
| 779 | demux.data = xd; |
| 780 | demux.out = -1; |
Jeff King | df85757 | 2016-04-19 18:50:29 -0400 | [diff] [blame] | 781 | demux.isolate_sigpipe = 1; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 782 | if (start_async(&demux)) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 783 | 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] | 784 | } |
| 785 | else |
| 786 | demux.out = xd[0]; |
| 787 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 788 | if (!args->keep_pack && unpack_limit) { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 789 | |
| 790 | if (read_pack_header(demux.out, &header)) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 791 | die(_("protocol error: bad pack header")); |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 792 | pass_header = 1; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 793 | if (ntohl(header.hdr_entries) < unpack_limit) |
| 794 | do_keep = 0; |
| 795 | else |
| 796 | do_keep = 1; |
| 797 | } |
| 798 | |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 799 | if (alternate_shallow_file) { |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 800 | argv_array_push(&cmd.args, "--shallow-file"); |
| 801 | argv_array_push(&cmd.args, alternate_shallow_file); |
Nguyễn Thái Ngọc Duy | 6035d6a | 2013-05-26 08:16:15 +0700 | [diff] [blame] | 802 | } |
| 803 | |
Jonathan Tan | 88e2f9e | 2017-12-05 16:58:49 +0000 | [diff] [blame] | 804 | if (do_keep || args->from_promisor) { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 805 | if (pack_lockfile) |
| 806 | cmd.out = -1; |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 807 | cmd_name = "index-pack"; |
| 808 | argv_array_push(&cmd.args, cmd_name); |
| 809 | argv_array_push(&cmd.args, "--stdin"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 810 | if (!args->quiet && !args->no_progress) |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 811 | argv_array_push(&cmd.args, "-v"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 812 | if (args->use_thin_pack) |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 813 | argv_array_push(&cmd.args, "--fix-thin"); |
Jonathan Tan | 88e2f9e | 2017-12-05 16:58:49 +0000 | [diff] [blame] | 814 | if (do_keep && (args->lock_pack || unpack_limit)) { |
René Scharfe | da25bdb | 2017-04-18 17:57:42 -0400 | [diff] [blame] | 815 | char hostname[HOST_NAME_MAX + 1]; |
David Turner | 5781a9a | 2017-04-18 17:57:43 -0400 | [diff] [blame] | 816 | if (xgethostname(hostname, sizeof(hostname))) |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 817 | xsnprintf(hostname, sizeof(hostname), "localhost"); |
| 818 | argv_array_pushf(&cmd.args, |
| 819 | "--keep=fetch-pack %"PRIuMAX " on %s", |
| 820 | (uintmax_t)getpid(), hostname); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 821 | } |
Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 08:16:17 +0700 | [diff] [blame] | 822 | if (args->check_self_contained_and_connected) |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 823 | argv_array_push(&cmd.args, "--check-self-contained-and-connected"); |
Jonathan Tan | 88e2f9e | 2017-12-05 16:58:49 +0000 | [diff] [blame] | 824 | if (args->from_promisor) |
| 825 | argv_array_push(&cmd.args, "--promisor"); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 826 | } |
| 827 | else { |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 828 | cmd_name = "unpack-objects"; |
| 829 | argv_array_push(&cmd.args, cmd_name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 830 | if (args->quiet || args->no_progress) |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 831 | argv_array_push(&cmd.args, "-q"); |
Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 08:16:17 +0700 | [diff] [blame] | 832 | args->check_self_contained_and_connected = 0; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 833 | } |
Jeff King | 984a43b | 2015-09-24 17:07:54 -0400 | [diff] [blame] | 834 | |
| 835 | if (pass_header) |
| 836 | argv_array_pushf(&cmd.args, "--pack_header=%"PRIu32",%"PRIu32, |
| 837 | ntohl(header.hdr_version), |
| 838 | ntohl(header.hdr_entries)); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 839 | if (fetch_fsck_objects >= 0 |
| 840 | ? fetch_fsck_objects |
| 841 | : transfer_fsck_objects >= 0 |
| 842 | ? transfer_fsck_objects |
Jonathan Tan | 98a2ea4 | 2018-03-14 11:42:41 -0700 | [diff] [blame] | 843 | : 0) { |
| 844 | if (args->from_promisor) |
| 845 | /* |
| 846 | * We cannot use --strict in index-pack because it |
| 847 | * checks both broken objects and links, but we only |
| 848 | * want to check for broken objects. |
| 849 | */ |
| 850 | argv_array_push(&cmd.args, "--fsck-objects"); |
| 851 | else |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 852 | argv_array_pushf(&cmd.args, "--strict%s", |
| 853 | fsck_msg_types.buf); |
Jonathan Tan | 98a2ea4 | 2018-03-14 11:42:41 -0700 | [diff] [blame] | 854 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 855 | |
| 856 | cmd.in = demux.out; |
| 857 | cmd.git_cmd = 1; |
| 858 | if (start_command(&cmd)) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 859 | die(_("fetch-pack: unable to fork off %s"), cmd_name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 860 | if (do_keep && pack_lockfile) { |
| 861 | *pack_lockfile = index_pack_lockfile(cmd.out); |
| 862 | close(cmd.out); |
| 863 | } |
| 864 | |
Jens Lindstrom | 37cb1dd | 2013-10-22 15:36:02 +0200 | [diff] [blame] | 865 | if (!use_sideband) |
| 866 | /* Closed by start_command() */ |
| 867 | xd[0] = -1; |
| 868 | |
Nguyễn Thái Ngọc Duy | c6807a4 | 2013-05-26 08:16:17 +0700 | [diff] [blame] | 869 | ret = finish_command(&cmd); |
| 870 | if (!ret || (args->check_self_contained_and_connected && ret == 1)) |
| 871 | args->self_contained_and_connected = |
| 872 | args->check_self_contained_and_connected && |
| 873 | ret == 0; |
| 874 | else |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 875 | die(_("%s failed"), cmd_name); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 876 | if (use_sideband && finish_async(&demux)) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 877 | die(_("error in sideband demultiplexer")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 878 | return 0; |
| 879 | } |
| 880 | |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 881 | static int cmp_ref_by_name(const void *a_, const void *b_) |
| 882 | { |
| 883 | const struct ref *a = *((const struct ref **)a_); |
| 884 | const struct ref *b = *((const struct ref **)b_); |
| 885 | return strcmp(a->name, b->name); |
| 886 | } |
| 887 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 888 | static struct ref *do_fetch_pack(struct fetch_pack_args *args, |
| 889 | int fd[2], |
| 890 | const struct ref *orig_ref, |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 891 | struct ref **sought, int nr_sought, |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 892 | struct shallow_info *si, |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 893 | char **pack_lockfile) |
| 894 | { |
| 895 | struct ref *ref = copy_ref_list(orig_ref); |
brian m. carlson | 1b28337 | 2017-05-01 02:28:54 +0000 | [diff] [blame] | 896 | struct object_id oid; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 897 | const char *agent_feature; |
| 898 | int agent_len; |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 899 | struct fetch_negotiator negotiator; |
Jonathan Tan | 42cc748 | 2018-07-16 11:44:01 -0700 | [diff] [blame] | 900 | fetch_negotiator_init(&negotiator, negotiation_algorithm); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 901 | |
| 902 | sort_ref_list(&ref, ref_compare_name); |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 903 | 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] | 904 | |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 905 | if ((args->depth > 0 || is_repository_shallow(the_repository)) && !server_supports("shallow")) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 906 | die(_("Server does not support shallow clients")); |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 907 | if (args->depth > 0 || args->deepen_since || args->deepen_not) |
Nguyễn Thái Ngọc Duy | 79891cb | 2016-06-12 17:53:56 +0700 | [diff] [blame] | 908 | args->deepen = 1; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 909 | if (server_supports("multi_ack_detailed")) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 910 | print_verbose(args, _("Server supports multi_ack_detailed")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 911 | multi_ack = 2; |
| 912 | if (server_supports("no-done")) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 913 | print_verbose(args, _("Server supports no-done")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 914 | if (args->stateless_rpc) |
| 915 | no_done = 1; |
| 916 | } |
| 917 | } |
| 918 | else if (server_supports("multi_ack")) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 919 | print_verbose(args, _("Server supports multi_ack")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 920 | multi_ack = 1; |
| 921 | } |
| 922 | if (server_supports("side-band-64k")) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 923 | print_verbose(args, _("Server supports side-band-64k")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 924 | use_sideband = 2; |
| 925 | } |
| 926 | else if (server_supports("side-band")) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 927 | print_verbose(args, _("Server supports side-band")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 928 | use_sideband = 1; |
| 929 | } |
Junio C Hamano | 6e7b66e | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 930 | if (server_supports("allow-tip-sha1-in-want")) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 931 | print_verbose(args, _("Server supports allow-tip-sha1-in-want")); |
Fredrik Medley | 7199c09 | 2015-05-21 22:23:38 +0200 | [diff] [blame] | 932 | allow_unadvertised_object_request |= ALLOW_TIP_SHA1; |
Junio C Hamano | 6e7b66e | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 933 | } |
Fredrik Medley | 68ee628 | 2015-05-21 22:23:39 +0200 | [diff] [blame] | 934 | if (server_supports("allow-reachable-sha1-in-want")) { |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 935 | print_verbose(args, _("Server supports allow-reachable-sha1-in-want")); |
Fredrik Medley | 68ee628 | 2015-05-21 22:23:39 +0200 | [diff] [blame] | 936 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; |
| 937 | } |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 938 | if (!server_supports("thin-pack")) |
| 939 | args->use_thin_pack = 0; |
| 940 | if (!server_supports("no-progress")) |
| 941 | args->no_progress = 0; |
| 942 | if (!server_supports("include-tag")) |
| 943 | args->include_tag = 0; |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 944 | if (server_supports("ofs-delta")) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 945 | print_verbose(args, _("Server supports ofs-delta")); |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 946 | else |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 947 | prefer_ofs_delta = 0; |
| 948 | |
Jeff Hostetler | 640d8b7 | 2017-12-08 15:58:40 +0000 | [diff] [blame] | 949 | if (server_supports("filter")) { |
| 950 | server_supports_filtering = 1; |
| 951 | print_verbose(args, _("Server supports filter")); |
| 952 | } else if (args->filter_options.choice) { |
| 953 | warning("filtering not recognized by server, ignoring"); |
| 954 | } |
| 955 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 956 | if ((agent_feature = server_feature_value("agent", &agent_len))) { |
| 957 | agent_supported = 1; |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 958 | if (agent_len) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 959 | print_verbose(args, _("Server version is %.*s"), |
Nguyễn Thái Ngọc Duy | 0d789a5 | 2016-06-12 17:53:54 +0700 | [diff] [blame] | 960 | agent_len, agent_feature); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 961 | } |
Nguyễn Thái Ngọc Duy | 508ea88 | 2016-06-12 17:53:59 +0700 | [diff] [blame] | 962 | if (server_supports("deepen-since")) |
| 963 | deepen_since_ok = 1; |
| 964 | else if (args->deepen_since) |
| 965 | die(_("Server does not support --shallow-since")); |
Nguyễn Thái Ngọc Duy | a45a260 | 2016-06-12 17:54:04 +0700 | [diff] [blame] | 966 | if (server_supports("deepen-not")) |
| 967 | deepen_not_ok = 1; |
| 968 | else if (args->deepen_not) |
| 969 | die(_("Server does not support --shallow-exclude")); |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 970 | if (!server_supports("deepen-relative") && args->deepen_relative) |
| 971 | die(_("Server does not support --deepen")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 972 | |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 973 | if (!args->no_dependents) { |
| 974 | mark_complete_and_common_ref(&negotiator, args, &ref); |
| 975 | filter_refs(args, &ref, sought, nr_sought); |
| 976 | if (everything_local(args, &ref)) { |
| 977 | packet_flush(fd[1]); |
| 978 | goto all_done; |
| 979 | } |
| 980 | } else { |
| 981 | filter_refs(args, &ref, sought, nr_sought); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 982 | } |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 983 | 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] | 984 | if (!args->keep_pack) |
| 985 | /* When cloning, it is not unusual to have |
| 986 | * no common commit. |
| 987 | */ |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 988 | warning(_("no common commits")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 989 | |
| 990 | if (args->stateless_rpc) |
| 991 | packet_flush(fd[1]); |
Nguyễn Thái Ngọc Duy | 79891cb | 2016-06-12 17:53:56 +0700 | [diff] [blame] | 992 | if (args->deepen) |
Nguyễn Thái Ngọc Duy | 1a30f5a | 2013-12-05 20:02:34 +0700 | [diff] [blame] | 993 | setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, |
| 994 | NULL); |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 995 | else if (si->nr_ours || si->nr_theirs) |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 996 | alternate_shallow_file = setup_temporary_shallow(si->shallow); |
Nguyễn Thái Ngọc Duy | 6da8bdc | 2013-08-26 09:17:26 +0700 | [diff] [blame] | 997 | else |
| 998 | alternate_shallow_file = NULL; |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 999 | if (get_pack(args, fd, pack_lockfile)) |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 1000 | die(_("git fetch-pack: fetch failed.")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1001 | |
| 1002 | all_done: |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1003 | negotiator.release(&negotiator); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1004 | return ref; |
| 1005 | } |
| 1006 | |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1007 | static void add_shallow_requests(struct strbuf *req_buf, |
| 1008 | const struct fetch_pack_args *args) |
| 1009 | { |
Junio C Hamano | 00624d6 | 2018-07-18 12:20:27 -0700 | [diff] [blame] | 1010 | if (is_repository_shallow(the_repository)) |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1011 | write_shallow_commits(req_buf, 1, NULL); |
| 1012 | if (args->depth > 0) |
| 1013 | packet_buf_write(req_buf, "deepen %d", args->depth); |
| 1014 | if (args->deepen_since) { |
| 1015 | timestamp_t max_age = approxidate(args->deepen_since); |
| 1016 | packet_buf_write(req_buf, "deepen-since %"PRItime, max_age); |
| 1017 | } |
| 1018 | if (args->deepen_not) { |
| 1019 | int i; |
| 1020 | for (i = 0; i < args->deepen_not->nr; i++) { |
| 1021 | struct string_list_item *s = args->deepen_not->items + i; |
| 1022 | packet_buf_write(req_buf, "deepen-not %s", s->string); |
| 1023 | } |
| 1024 | } |
Jonathan Tan | 5056cf4 | 2018-12-18 13:24:35 -0800 | [diff] [blame] | 1025 | if (args->deepen_relative) |
| 1026 | packet_buf_write(req_buf, "deepen-relative\n"); |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 1029 | static void add_wants(int no_dependents, const struct ref *wants, struct strbuf *req_buf) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1030 | { |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1031 | int use_ref_in_want = server_supports_feature("fetch", "ref-in-want", 0); |
| 1032 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1033 | for ( ; wants ; wants = wants->next) { |
| 1034 | const struct object_id *remote = &wants->old_oid; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1035 | struct object *o; |
| 1036 | |
| 1037 | /* |
| 1038 | * If that object is complete (i.e. it is an ancestor of a |
| 1039 | * local ref), we tell them we have it but do not have to |
| 1040 | * tell them about its ancestors, which they already know |
| 1041 | * about. |
| 1042 | * |
| 1043 | * We use lookup_object here because we are only |
| 1044 | * interested in the case we *know* the object is |
| 1045 | * reachable and we have already scanned it. |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 1046 | * |
| 1047 | * Do this only if args->no_dependents is false (if it is true, |
| 1048 | * we cannot trust the object flags). |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1049 | */ |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 1050 | if (!no_dependents && |
| 1051 | ((o = lookup_object(the_repository, remote->hash)) != NULL) && |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1052 | (o->flags & COMPLETE)) { |
| 1053 | continue; |
| 1054 | } |
| 1055 | |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1056 | if (!use_ref_in_want || wants->exact_oid) |
| 1057 | packet_buf_write(req_buf, "want %s\n", oid_to_hex(remote)); |
| 1058 | else |
| 1059 | packet_buf_write(req_buf, "want-ref %s\n", wants->name); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | static void add_common(struct strbuf *req_buf, struct oidset *common) |
| 1064 | { |
| 1065 | struct oidset_iter iter; |
| 1066 | const struct object_id *oid; |
| 1067 | oidset_iter_init(common, &iter); |
| 1068 | |
| 1069 | while ((oid = oidset_iter_next(&iter))) { |
| 1070 | packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); |
| 1071 | } |
| 1072 | } |
| 1073 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1074 | static int add_haves(struct fetch_negotiator *negotiator, |
| 1075 | struct strbuf *req_buf, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 1076 | int *haves_to_send, int *in_vain) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1077 | { |
| 1078 | int ret = 0; |
| 1079 | int haves_added = 0; |
| 1080 | const struct object_id *oid; |
| 1081 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1082 | while ((oid = negotiator->next(negotiator))) { |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1083 | packet_buf_write(req_buf, "have %s\n", oid_to_hex(oid)); |
| 1084 | if (++haves_added >= *haves_to_send) |
| 1085 | break; |
| 1086 | } |
| 1087 | |
| 1088 | *in_vain += haves_added; |
| 1089 | if (!haves_added || *in_vain >= MAX_IN_VAIN) { |
| 1090 | /* Send Done */ |
| 1091 | packet_buf_write(req_buf, "done\n"); |
| 1092 | ret = 1; |
| 1093 | } |
| 1094 | |
| 1095 | /* Increase haves to send on next round */ |
| 1096 | *haves_to_send = next_flush(1, *haves_to_send); |
| 1097 | |
| 1098 | return ret; |
| 1099 | } |
| 1100 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1101 | static int send_fetch_request(struct fetch_negotiator *negotiator, int fd_out, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 1102 | const struct fetch_pack_args *args, |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1103 | const struct ref *wants, struct oidset *common, |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1104 | int *haves_to_send, int *in_vain, |
| 1105 | int sideband_all) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1106 | { |
| 1107 | int ret = 0; |
| 1108 | struct strbuf req_buf = STRBUF_INIT; |
| 1109 | |
| 1110 | if (server_supports_v2("fetch", 1)) |
| 1111 | packet_buf_write(&req_buf, "command=fetch"); |
| 1112 | if (server_supports_v2("agent", 0)) |
| 1113 | packet_buf_write(&req_buf, "agent=%s", git_user_agent_sanitized()); |
Brandon Williams | 5e3548e | 2018-04-23 15:46:24 -0700 | [diff] [blame] | 1114 | if (args->server_options && args->server_options->nr && |
| 1115 | server_supports_v2("server-option", 1)) { |
| 1116 | int i; |
| 1117 | for (i = 0; i < args->server_options->nr; i++) |
Jonathan Tan | 5b204b7 | 2019-05-22 13:08:22 -0700 | [diff] [blame] | 1118 | packet_buf_write(&req_buf, "server-option=%s", |
Brandon Williams | 5e3548e | 2018-04-23 15:46:24 -0700 | [diff] [blame] | 1119 | args->server_options->items[i].string); |
| 1120 | } |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1121 | |
| 1122 | packet_buf_delim(&req_buf); |
| 1123 | if (args->use_thin_pack) |
| 1124 | packet_buf_write(&req_buf, "thin-pack"); |
| 1125 | if (args->no_progress) |
| 1126 | packet_buf_write(&req_buf, "no-progress"); |
| 1127 | if (args->include_tag) |
| 1128 | packet_buf_write(&req_buf, "include-tag"); |
| 1129 | if (prefer_ofs_delta) |
| 1130 | packet_buf_write(&req_buf, "ofs-delta"); |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1131 | if (sideband_all) |
| 1132 | packet_buf_write(&req_buf, "sideband-all"); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1133 | |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1134 | /* Add shallow-info and deepen request */ |
| 1135 | if (server_supports_feature("fetch", "shallow", 0)) |
| 1136 | add_shallow_requests(&req_buf, args); |
Junio C Hamano | 00624d6 | 2018-07-18 12:20:27 -0700 | [diff] [blame] | 1137 | else if (is_repository_shallow(the_repository) || args->deepen) |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1138 | die(_("Server does not support shallow requests")); |
| 1139 | |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1140 | /* Add filter */ |
| 1141 | if (server_supports_feature("fetch", "filter", 0) && |
| 1142 | args->filter_options.choice) { |
Josh Steadmon | 87c2d9d | 2019-01-07 16:17:09 -0800 | [diff] [blame] | 1143 | struct strbuf expanded_filter_spec = STRBUF_INIT; |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1144 | print_verbose(args, _("Server supports filter")); |
Josh Steadmon | 87c2d9d | 2019-01-07 16:17:09 -0800 | [diff] [blame] | 1145 | expand_list_objects_filter_spec(&args->filter_options, |
| 1146 | &expanded_filter_spec); |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1147 | packet_buf_write(&req_buf, "filter %s", |
Josh Steadmon | 87c2d9d | 2019-01-07 16:17:09 -0800 | [diff] [blame] | 1148 | expanded_filter_spec.buf); |
| 1149 | strbuf_release(&expanded_filter_spec); |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1150 | } else if (args->filter_options.choice) { |
| 1151 | warning("filtering not recognized by server, ignoring"); |
| 1152 | } |
| 1153 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1154 | /* add wants */ |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 1155 | add_wants(args->no_dependents, wants, &req_buf); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1156 | |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1157 | if (args->no_dependents) { |
| 1158 | packet_buf_write(&req_buf, "done"); |
| 1159 | ret = 1; |
| 1160 | } else { |
| 1161 | /* Add all of the common commits we've found in previous rounds */ |
| 1162 | add_common(&req_buf, common); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1163 | |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1164 | /* Add initial haves */ |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1165 | ret = add_haves(negotiator, &req_buf, haves_to_send, in_vain); |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1166 | } |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1167 | |
| 1168 | /* Send request */ |
| 1169 | packet_buf_flush(&req_buf); |
Jeff King | 37c8001 | 2019-03-04 23:11:39 -0500 | [diff] [blame] | 1170 | if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0) |
| 1171 | die_errno(_("unable to write request to remote")); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1172 | |
| 1173 | strbuf_release(&req_buf); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Processes a section header in a server's response and checks if it matches |
| 1179 | * `section`. If the value of `peek` is 1, the header line will be peeked (and |
| 1180 | * not consumed); if 0, the line will be consumed and the function will die if |
| 1181 | * the section header doesn't match what was expected. |
| 1182 | */ |
| 1183 | static int process_section_header(struct packet_reader *reader, |
| 1184 | const char *section, int peek) |
| 1185 | { |
| 1186 | int ret; |
| 1187 | |
| 1188 | if (packet_reader_peek(reader) != PACKET_READ_NORMAL) |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1189 | die(_("error reading section header '%s'"), section); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1190 | |
| 1191 | ret = !strcmp(reader->line, section); |
| 1192 | |
| 1193 | if (!peek) { |
| 1194 | if (!ret) |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1195 | die(_("expected '%s', received '%s'"), |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1196 | section, reader->line); |
| 1197 | packet_reader_read(reader); |
| 1198 | } |
| 1199 | |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1203 | static int process_acks(struct fetch_negotiator *negotiator, |
Jonathan Tan | d30fe89 | 2018-06-14 15:54:26 -0700 | [diff] [blame] | 1204 | struct packet_reader *reader, |
| 1205 | struct oidset *common) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1206 | { |
| 1207 | /* received */ |
| 1208 | int received_ready = 0; |
| 1209 | int received_ack = 0; |
| 1210 | |
| 1211 | process_section_header(reader, "acknowledgments", 0); |
| 1212 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 1213 | const char *arg; |
| 1214 | |
| 1215 | if (!strcmp(reader->line, "NAK")) |
| 1216 | continue; |
| 1217 | |
| 1218 | if (skip_prefix(reader->line, "ACK ", &arg)) { |
| 1219 | struct object_id oid; |
| 1220 | if (!get_oid_hex(arg, &oid)) { |
| 1221 | struct commit *commit; |
| 1222 | oidset_insert(common, &oid); |
Stefan Beller | c1f5eb4 | 2018-06-28 18:21:59 -0700 | [diff] [blame] | 1223 | commit = lookup_commit(the_repository, &oid); |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1224 | negotiator->ack(negotiator, commit); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1225 | } |
| 1226 | continue; |
| 1227 | } |
| 1228 | |
| 1229 | if (!strcmp(reader->line, "ready")) { |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1230 | received_ready = 1; |
| 1231 | continue; |
| 1232 | } |
| 1233 | |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1234 | die(_("unexpected acknowledgment line: '%s'"), reader->line); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | if (reader->status != PACKET_READ_FLUSH && |
| 1238 | reader->status != PACKET_READ_DELIM) |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1239 | die(_("error processing acks: %d"), reader->status); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1240 | |
Jonathan Tan | 5400b2a | 2018-10-19 15:54:04 -0700 | [diff] [blame] | 1241 | /* |
| 1242 | * If an "acknowledgments" section is sent, a packfile is sent if and |
| 1243 | * only if "ready" was sent in this section. The other sections |
| 1244 | * ("shallow-info" and "wanted-refs") are sent only if a packfile is |
| 1245 | * sent. Therefore, a DELIM is expected if "ready" is sent, and a FLUSH |
| 1246 | * otherwise. |
| 1247 | */ |
| 1248 | if (received_ready && reader->status != PACKET_READ_DELIM) |
| 1249 | die(_("expected packfile to be sent after 'ready'")); |
| 1250 | if (!received_ready && reader->status != PACKET_READ_FLUSH) |
| 1251 | die(_("expected no other sections to be sent after no 'ready'")); |
| 1252 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1253 | /* return 0 if no common, 1 if there are common, or 2 if ready */ |
| 1254 | return received_ready ? 2 : (received_ack ? 1 : 0); |
| 1255 | } |
| 1256 | |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1257 | static void receive_shallow_info(struct fetch_pack_args *args, |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1258 | struct packet_reader *reader, |
| 1259 | struct oid_array *shallows, |
| 1260 | struct shallow_info *si) |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1261 | { |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1262 | int unshallow_received = 0; |
Jonathan Tan | bd0b42a | 2019-01-10 11:36:45 -0800 | [diff] [blame] | 1263 | |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1264 | process_section_header(reader, "shallow-info", 0); |
| 1265 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 1266 | const char *arg; |
| 1267 | struct object_id oid; |
| 1268 | |
| 1269 | if (skip_prefix(reader->line, "shallow ", &arg)) { |
| 1270 | if (get_oid_hex(arg, &oid)) |
| 1271 | die(_("invalid shallow line: %s"), reader->line); |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1272 | oid_array_append(shallows, &oid); |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1273 | continue; |
| 1274 | } |
| 1275 | if (skip_prefix(reader->line, "unshallow ", &arg)) { |
| 1276 | if (get_oid_hex(arg, &oid)) |
| 1277 | die(_("invalid unshallow line: %s"), reader->line); |
Stefan Beller | 5abddd1 | 2018-06-28 18:21:52 -0700 | [diff] [blame] | 1278 | if (!lookup_object(the_repository, oid.hash)) |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1279 | die(_("object not found: %s"), reader->line); |
| 1280 | /* make sure that it is parsed as shallow */ |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 1281 | if (!parse_object(the_repository, &oid)) |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1282 | die(_("error in object: %s"), reader->line); |
| 1283 | if (unregister_shallow(&oid)) |
| 1284 | die(_("no shallow found: %s"), reader->line); |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1285 | unshallow_received = 1; |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1286 | continue; |
| 1287 | } |
| 1288 | die(_("expected shallow/unshallow, got %s"), reader->line); |
| 1289 | } |
| 1290 | |
| 1291 | if (reader->status != PACKET_READ_FLUSH && |
| 1292 | reader->status != PACKET_READ_DELIM) |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1293 | die(_("error processing shallow info: %d"), reader->status); |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1294 | |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1295 | if (args->deepen || unshallow_received) { |
| 1296 | /* |
| 1297 | * Treat these as shallow lines caused by our depth settings. |
| 1298 | * In v0, these lines cannot cause refs to be rejected; do the |
| 1299 | * same. |
| 1300 | */ |
| 1301 | int i; |
| 1302 | |
| 1303 | for (i = 0; i < shallows->nr; i++) |
| 1304 | register_shallow(the_repository, &shallows->oid[i]); |
Jonathan Tan | bd0b42a | 2019-01-10 11:36:45 -0800 | [diff] [blame] | 1305 | setup_alternate_shallow(&shallow_lock, &alternate_shallow_file, |
| 1306 | NULL); |
| 1307 | args->deepen = 1; |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1308 | } else if (shallows->nr) { |
| 1309 | /* |
| 1310 | * Treat these as shallow lines caused by the remote being |
| 1311 | * shallow. In v0, remote refs that reach these objects are |
| 1312 | * rejected (unless --update-shallow is set); do the same. |
| 1313 | */ |
| 1314 | prepare_shallow_info(si, shallows); |
| 1315 | if (si->nr_ours || si->nr_theirs) |
| 1316 | alternate_shallow_file = |
| 1317 | setup_temporary_shallow(si->shallow); |
| 1318 | else |
| 1319 | alternate_shallow_file = NULL; |
brian m. carlson | 380ebab | 2019-02-06 23:59:37 +0000 | [diff] [blame] | 1320 | } else { |
| 1321 | alternate_shallow_file = NULL; |
Jonathan Tan | bd0b42a | 2019-01-10 11:36:45 -0800 | [diff] [blame] | 1322 | } |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Jonathan Tan | b764300 | 2019-03-27 14:11:10 -0700 | [diff] [blame] | 1325 | static int cmp_name_ref(const void *name, const void *ref) |
| 1326 | { |
| 1327 | return strcmp(name, (*(struct ref **)ref)->name); |
| 1328 | } |
| 1329 | |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1330 | static void receive_wanted_refs(struct packet_reader *reader, |
| 1331 | struct ref **sought, int nr_sought) |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1332 | { |
| 1333 | process_section_header(reader, "wanted-refs", 0); |
| 1334 | while (packet_reader_read(reader) == PACKET_READ_NORMAL) { |
| 1335 | struct object_id oid; |
| 1336 | const char *end; |
Jonathan Tan | b764300 | 2019-03-27 14:11:10 -0700 | [diff] [blame] | 1337 | struct ref **found; |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1338 | |
| 1339 | if (parse_oid_hex(reader->line, &oid, &end) || *end++ != ' ') |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1340 | die(_("expected wanted-ref, got '%s'"), reader->line); |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1341 | |
Jonathan Tan | b764300 | 2019-03-27 14:11:10 -0700 | [diff] [blame] | 1342 | found = bsearch(end, sought, nr_sought, sizeof(*sought), |
| 1343 | cmp_name_ref); |
| 1344 | if (!found) |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1345 | die(_("unexpected wanted-ref: '%s'"), reader->line); |
Jonathan Tan | b764300 | 2019-03-27 14:11:10 -0700 | [diff] [blame] | 1346 | oidcpy(&(*found)->old_oid, &oid); |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | if (reader->status != PACKET_READ_DELIM) |
Brandon Williams | bbb19a8 | 2018-07-23 10:56:35 -0700 | [diff] [blame] | 1350 | die(_("error processing wanted refs: %d"), reader->status); |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1353 | enum fetch_state { |
| 1354 | FETCH_CHECK_LOCAL = 0, |
| 1355 | FETCH_SEND_REQUEST, |
| 1356 | FETCH_PROCESS_ACKS, |
| 1357 | FETCH_GET_PACK, |
| 1358 | FETCH_DONE, |
| 1359 | }; |
| 1360 | |
| 1361 | static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args, |
| 1362 | int fd[2], |
| 1363 | const struct ref *orig_ref, |
| 1364 | struct ref **sought, int nr_sought, |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1365 | struct oid_array *shallows, |
| 1366 | struct shallow_info *si, |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1367 | char **pack_lockfile) |
| 1368 | { |
| 1369 | struct ref *ref = copy_ref_list(orig_ref); |
| 1370 | enum fetch_state state = FETCH_CHECK_LOCAL; |
| 1371 | struct oidset common = OIDSET_INIT; |
| 1372 | struct packet_reader reader; |
| 1373 | int in_vain = 0; |
| 1374 | int haves_to_send = INITIAL_FLUSH; |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1375 | struct fetch_negotiator negotiator; |
Jonathan Tan | 42cc748 | 2018-07-16 11:44:01 -0700 | [diff] [blame] | 1376 | fetch_negotiator_init(&negotiator, negotiation_algorithm); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1377 | packet_reader_init(&reader, fd[0], NULL, 0, |
Masaya Suzuki | 2d103c3 | 2018-12-29 13:19:15 -0800 | [diff] [blame] | 1378 | PACKET_READ_CHOMP_NEWLINE | |
| 1379 | PACKET_READ_DIE_ON_ERR_PACKET); |
Jonathan Tan | 07c3c2a | 2019-01-16 11:28:15 -0800 | [diff] [blame] | 1380 | if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 1) && |
| 1381 | server_supports_feature("fetch", "sideband-all", 0)) { |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1382 | reader.use_sideband = 1; |
| 1383 | reader.me = "fetch-pack"; |
| 1384 | } |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1385 | |
| 1386 | while (state != FETCH_DONE) { |
| 1387 | switch (state) { |
| 1388 | case FETCH_CHECK_LOCAL: |
| 1389 | sort_ref_list(&ref, ref_compare_name); |
| 1390 | QSORT(sought, nr_sought, cmp_ref_by_name); |
| 1391 | |
| 1392 | /* v2 supports these by default */ |
| 1393 | allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; |
| 1394 | use_sideband = 2; |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1395 | if (args->depth > 0 || args->deepen_since || args->deepen_not) |
| 1396 | args->deepen = 1; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1397 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1398 | /* Filter 'ref' by 'sought' and those that aren't local */ |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 1399 | if (!args->no_dependents) { |
| 1400 | mark_complete_and_common_ref(&negotiator, args, &ref); |
| 1401 | filter_refs(args, &ref, sought, nr_sought); |
| 1402 | if (everything_local(args, &ref)) |
| 1403 | state = FETCH_DONE; |
| 1404 | else |
| 1405 | state = FETCH_SEND_REQUEST; |
Jonathan Tan | af1c90d | 2018-06-14 15:54:25 -0700 | [diff] [blame] | 1406 | |
Jonathan Tan | 12f19a9 | 2018-10-03 16:04:52 -0700 | [diff] [blame] | 1407 | mark_tips(&negotiator, args->negotiation_tips); |
| 1408 | for_each_cached_alternate(&negotiator, |
| 1409 | insert_one_alternate_object); |
| 1410 | } else { |
| 1411 | filter_refs(args, &ref, sought, nr_sought); |
| 1412 | state = FETCH_SEND_REQUEST; |
| 1413 | } |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1414 | break; |
| 1415 | case FETCH_SEND_REQUEST: |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1416 | if (send_fetch_request(&negotiator, fd[1], args, ref, |
| 1417 | &common, |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1418 | &haves_to_send, &in_vain, |
| 1419 | reader.use_sideband)) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1420 | state = FETCH_GET_PACK; |
| 1421 | else |
| 1422 | state = FETCH_PROCESS_ACKS; |
| 1423 | break; |
| 1424 | case FETCH_PROCESS_ACKS: |
| 1425 | /* Process ACKs/NAKs */ |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1426 | switch (process_acks(&negotiator, &reader, &common)) { |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1427 | case 2: |
| 1428 | state = FETCH_GET_PACK; |
| 1429 | break; |
| 1430 | case 1: |
| 1431 | in_vain = 0; |
| 1432 | /* fallthrough */ |
| 1433 | default: |
| 1434 | state = FETCH_SEND_REQUEST; |
| 1435 | break; |
| 1436 | } |
| 1437 | break; |
| 1438 | case FETCH_GET_PACK: |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1439 | /* Check for shallow-info section */ |
| 1440 | if (process_section_header(&reader, "shallow-info", 1)) |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1441 | receive_shallow_info(args, &reader, shallows, si); |
Brandon Williams | f7e2050 | 2018-03-15 10:31:29 -0700 | [diff] [blame] | 1442 | |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1443 | if (process_section_header(&reader, "wanted-refs", 1)) |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1444 | receive_wanted_refs(&reader, sought, nr_sought); |
Brandon Williams | 7330205 | 2018-06-27 15:30:23 -0700 | [diff] [blame] | 1445 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1446 | /* get the pack */ |
| 1447 | process_section_header(&reader, "packfile", 0); |
| 1448 | if (get_pack(args, fd, pack_lockfile)) |
| 1449 | die(_("git fetch-pack: fetch failed.")); |
| 1450 | |
| 1451 | state = FETCH_DONE; |
| 1452 | break; |
| 1453 | case FETCH_DONE: |
| 1454 | continue; |
| 1455 | } |
| 1456 | } |
| 1457 | |
Jonathan Tan | ec06283 | 2018-06-14 15:54:28 -0700 | [diff] [blame] | 1458 | negotiator.release(&negotiator); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1459 | oidset_clear(&common); |
| 1460 | return ref; |
| 1461 | } |
| 1462 | |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 1463 | static int fetch_pack_config_cb(const char *var, const char *value, void *cb) |
| 1464 | { |
| 1465 | if (strcmp(var, "fetch.fsck.skiplist") == 0) { |
| 1466 | const char *path; |
| 1467 | |
| 1468 | if (git_config_pathname(&path, var, value)) |
| 1469 | return 1; |
| 1470 | strbuf_addf(&fsck_msg_types, "%cskiplist=%s", |
| 1471 | fsck_msg_types.len ? ',' : '=', path); |
| 1472 | free((char *)path); |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
| 1476 | if (skip_prefix(var, "fetch.fsck.", &var)) { |
| 1477 | if (is_valid_msg_type(var, value)) |
| 1478 | strbuf_addf(&fsck_msg_types, "%c%s=%s", |
| 1479 | fsck_msg_types.len ? ',' : '=', var, value); |
| 1480 | else |
| 1481 | warning("Skipping unknown msg id '%s'", var); |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | return git_default_config(var, value, cb); |
| 1486 | } |
| 1487 | |
Tanay Abhra | f44af51 | 2014-08-07 09:21:20 -0700 | [diff] [blame] | 1488 | static void fetch_pack_config(void) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1489 | { |
Tanay Abhra | f44af51 | 2014-08-07 09:21:20 -0700 | [diff] [blame] | 1490 | git_config_get_int("fetch.unpacklimit", &fetch_unpack_limit); |
| 1491 | git_config_get_int("transfer.unpacklimit", &transfer_unpack_limit); |
| 1492 | git_config_get_bool("repack.usedeltabaseoffset", &prefer_ofs_delta); |
| 1493 | git_config_get_bool("fetch.fsckobjects", &fetch_fsck_objects); |
| 1494 | git_config_get_bool("transfer.fsckobjects", &transfer_fsck_objects); |
Jonathan Tan | 42cc748 | 2018-07-16 11:44:01 -0700 | [diff] [blame] | 1495 | git_config_get_string("fetch.negotiationalgorithm", |
| 1496 | &negotiation_algorithm); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1497 | |
Ævar Arnfjörð Bjarmason | 1362df0 | 2018-07-27 14:37:17 +0000 | [diff] [blame] | 1498 | git_config(fetch_pack_config_cb, NULL); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1501 | static void fetch_pack_setup(void) |
| 1502 | { |
| 1503 | static int did_setup; |
| 1504 | if (did_setup) |
| 1505 | return; |
Tanay Abhra | f44af51 | 2014-08-07 09:21:20 -0700 | [diff] [blame] | 1506 | fetch_pack_config(); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1507 | if (0 <= transfer_unpack_limit) |
| 1508 | unpack_limit = transfer_unpack_limit; |
| 1509 | else if (0 <= fetch_unpack_limit) |
| 1510 | unpack_limit = fetch_unpack_limit; |
| 1511 | did_setup = 1; |
| 1512 | } |
| 1513 | |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 1514 | static int remove_duplicates_in_refs(struct ref **ref, int nr) |
| 1515 | { |
| 1516 | struct string_list names = STRING_LIST_INIT_NODUP; |
| 1517 | int src, dst; |
| 1518 | |
| 1519 | for (src = dst = 0; src < nr; src++) { |
| 1520 | struct string_list_item *item; |
| 1521 | item = string_list_insert(&names, ref[src]->name); |
| 1522 | if (item->util) |
| 1523 | continue; /* already have it */ |
| 1524 | item->util = ref[src]; |
| 1525 | if (src != dst) |
| 1526 | ref[dst] = ref[src]; |
| 1527 | dst++; |
| 1528 | } |
| 1529 | for (src = dst; src < nr; src++) |
| 1530 | ref[src] = NULL; |
| 1531 | string_list_clear(&names, 0); |
| 1532 | return dst; |
| 1533 | } |
| 1534 | |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1535 | static void update_shallow(struct fetch_pack_args *args, |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1536 | struct ref **sought, int nr_sought, |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1537 | struct shallow_info *si) |
Nguyễn Thái Ngọc Duy | a796cce | 2013-12-05 20:02:37 +0700 | [diff] [blame] | 1538 | { |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1539 | struct oid_array ref = OID_ARRAY_INIT; |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1540 | int *status; |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1541 | int i; |
| 1542 | |
Nguyễn Thái Ngọc Duy | 79891cb | 2016-06-12 17:53:56 +0700 | [diff] [blame] | 1543 | if (args->deepen && alternate_shallow_file) { |
Nguyễn Thái Ngọc Duy | a796cce | 2013-12-05 20:02:37 +0700 | [diff] [blame] | 1544 | if (*alternate_shallow_file == '\0') { /* --unshallow */ |
Stefan Beller | 102de88 | 2018-05-17 15:51:51 -0700 | [diff] [blame] | 1545 | unlink_or_warn(git_path_shallow(the_repository)); |
Nguyễn Thái Ngọc Duy | a796cce | 2013-12-05 20:02:37 +0700 | [diff] [blame] | 1546 | rollback_lock_file(&shallow_lock); |
| 1547 | } else |
| 1548 | commit_lock_file(&shallow_lock); |
brian m. carlson | 23311f3 | 2019-02-04 00:06:50 +0000 | [diff] [blame] | 1549 | alternate_shallow_file = NULL; |
Nguyễn Thái Ngọc Duy | a796cce | 2013-12-05 20:02:37 +0700 | [diff] [blame] | 1550 | return; |
| 1551 | } |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1552 | |
| 1553 | if (!si->shallow || !si->shallow->nr) |
| 1554 | return; |
| 1555 | |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1556 | if (args->cloning) { |
| 1557 | /* |
| 1558 | * remote is shallow, but this is a clone, there are |
| 1559 | * no objects in repo to worry about. Accept any |
| 1560 | * shallow points that exist in the pack (iow in repo |
| 1561 | * after get_pack() and reprepare_packed_git()) |
| 1562 | */ |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1563 | struct oid_array extra = OID_ARRAY_INIT; |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 1564 | struct object_id *oid = si->shallow->oid; |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1565 | for (i = 0; i < si->shallow->nr; i++) |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 1566 | if (has_object_file(&oid[i])) |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1567 | oid_array_append(&extra, &oid[i]); |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1568 | if (extra.nr) { |
| 1569 | setup_alternate_shallow(&shallow_lock, |
| 1570 | &alternate_shallow_file, |
| 1571 | &extra); |
| 1572 | commit_lock_file(&shallow_lock); |
brian m. carlson | 23311f3 | 2019-02-04 00:06:50 +0000 | [diff] [blame] | 1573 | alternate_shallow_file = NULL; |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1574 | } |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1575 | oid_array_clear(&extra); |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1576 | return; |
| 1577 | } |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1578 | |
| 1579 | if (!si->nr_ours && !si->nr_theirs) |
| 1580 | return; |
| 1581 | |
| 1582 | remove_nonexistent_theirs_shallow(si); |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1583 | if (!si->nr_ours && !si->nr_theirs) |
| 1584 | return; |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1585 | for (i = 0; i < nr_sought; i++) |
| 1586 | 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] | 1587 | si->ref = &ref; |
| 1588 | |
Nguyễn Thái Ngọc Duy | 48d25ca | 2013-12-05 20:02:42 +0700 | [diff] [blame] | 1589 | if (args->update_shallow) { |
| 1590 | /* |
| 1591 | * remote is also shallow, .git/shallow may be updated |
| 1592 | * so all refs can be accepted. Make sure we only add |
| 1593 | * shallow roots that are actually reachable from new |
| 1594 | * refs. |
| 1595 | */ |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1596 | struct oid_array extra = OID_ARRAY_INIT; |
brian m. carlson | ee3051b | 2017-03-26 16:01:37 +0000 | [diff] [blame] | 1597 | struct object_id *oid = si->shallow->oid; |
Nguyễn Thái Ngọc Duy | 48d25ca | 2013-12-05 20:02:42 +0700 | [diff] [blame] | 1598 | assign_shallow_commits_to_refs(si, NULL, NULL); |
| 1599 | if (!si->nr_ours && !si->nr_theirs) { |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1600 | oid_array_clear(&ref); |
Nguyễn Thái Ngọc Duy | 48d25ca | 2013-12-05 20:02:42 +0700 | [diff] [blame] | 1601 | return; |
| 1602 | } |
| 1603 | for (i = 0; i < si->nr_ours; i++) |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1604 | 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] | 1605 | for (i = 0; i < si->nr_theirs; i++) |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1606 | 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] | 1607 | setup_alternate_shallow(&shallow_lock, |
| 1608 | &alternate_shallow_file, |
| 1609 | &extra); |
| 1610 | commit_lock_file(&shallow_lock); |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1611 | oid_array_clear(&extra); |
| 1612 | oid_array_clear(&ref); |
brian m. carlson | 23311f3 | 2019-02-04 00:06:50 +0000 | [diff] [blame] | 1613 | alternate_shallow_file = NULL; |
Nguyễn Thái Ngọc Duy | 48d25ca | 2013-12-05 20:02:42 +0700 | [diff] [blame] | 1614 | return; |
| 1615 | } |
| 1616 | |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1617 | /* |
| 1618 | * remote is also shallow, check what ref is safe to update |
| 1619 | * without updating .git/shallow |
| 1620 | */ |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1621 | status = xcalloc(nr_sought, sizeof(*status)); |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1622 | assign_shallow_commits_to_refs(si, NULL, status); |
| 1623 | if (si->nr_ours || si->nr_theirs) { |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1624 | for (i = 0; i < nr_sought; i++) |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1625 | if (status[i]) |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1626 | sought[i]->status = REF_STATUS_REJECT_SHALLOW; |
Nguyễn Thái Ngọc Duy | 4820a33 | 2013-12-05 20:02:40 +0700 | [diff] [blame] | 1627 | } |
| 1628 | free(status); |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1629 | oid_array_clear(&ref); |
Nguyễn Thái Ngọc Duy | a796cce | 2013-12-05 20:02:37 +0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Jonathan Tan | cf1e7c0 | 2018-07-02 15:08:43 -0700 | [diff] [blame] | 1632 | static int iterate_ref_map(void *cb_data, struct object_id *oid) |
| 1633 | { |
| 1634 | struct ref **rm = cb_data; |
| 1635 | struct ref *ref = *rm; |
| 1636 | |
| 1637 | if (!ref) |
| 1638 | return -1; /* end of the list */ |
| 1639 | *rm = ref->next; |
| 1640 | oidcpy(oid, &ref->old_oid); |
| 1641 | return 0; |
| 1642 | } |
| 1643 | |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1644 | struct ref *fetch_pack(struct fetch_pack_args *args, |
Jeff King | 0f804b0 | 2019-03-20 04:16:14 -0400 | [diff] [blame] | 1645 | int fd[], |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1646 | const struct ref *ref, |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 1647 | struct ref **sought, int nr_sought, |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1648 | struct oid_array *shallow, |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1649 | char **pack_lockfile, |
| 1650 | enum protocol_version version) |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1651 | { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1652 | struct ref *ref_cpy; |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1653 | struct shallow_info si; |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1654 | 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] | 1655 | |
| 1656 | fetch_pack_setup(); |
Junio C Hamano | f2db854 | 2013-01-29 14:02:15 -0800 | [diff] [blame] | 1657 | if (nr_sought) |
| 1658 | 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] | 1659 | |
Jonathan Tan | 4c7f956 | 2018-10-03 16:04:53 -0700 | [diff] [blame] | 1660 | if (args->no_dependents && !args->filter_options.choice) { |
| 1661 | /* |
| 1662 | * The protocol does not support requesting that only the |
| 1663 | * wanted objects be sent, so approximate this by setting a |
| 1664 | * "blob:none" filter if no filter is already set. This works |
| 1665 | * for all object types: note that wanted blobs will still be |
| 1666 | * sent because they are directly specified as a "want". |
| 1667 | * |
| 1668 | * NEEDSWORK: Add an option in the protocol to request that |
| 1669 | * only the wanted objects be sent, and implement it. |
| 1670 | */ |
| 1671 | parse_list_objects_filter(&args->filter_options, "blob:none"); |
| 1672 | } |
| 1673 | |
Jonathan Tan | 0177565 | 2018-09-27 12:24:05 -0700 | [diff] [blame] | 1674 | if (version != protocol_v2 && !ref) { |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1675 | packet_flush(fd[1]); |
Nguyễn Thái Ngọc Duy | 1dd73e2 | 2016-06-12 17:53:55 +0700 | [diff] [blame] | 1676 | die(_("no matching remote head")); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1677 | } |
Jonathan Tan | 1e7d440 | 2019-03-26 12:31:20 -0700 | [diff] [blame] | 1678 | if (version == protocol_v2) { |
| 1679 | if (shallow->nr) |
| 1680 | BUG("Protocol V2 does not provide shallows at this point in the fetch"); |
| 1681 | memset(&si, 0, sizeof(si)); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1682 | ref_cpy = do_fetch_pack_v2(args, fd, ref, sought, nr_sought, |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1683 | &shallows_scratch, &si, |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1684 | pack_lockfile); |
Jonathan Tan | 1e7d440 | 2019-03-26 12:31:20 -0700 | [diff] [blame] | 1685 | } else { |
| 1686 | prepare_shallow_info(&si, shallow); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1687 | ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought, |
| 1688 | &si, pack_lockfile); |
Jonathan Tan | 1e7d440 | 2019-03-26 12:31:20 -0700 | [diff] [blame] | 1689 | } |
Stefan Beller | a49d283 | 2018-03-23 18:45:21 +0100 | [diff] [blame] | 1690 | reprepare_packed_git(the_repository); |
Jonathan Tan | cf1e7c0 | 2018-07-02 15:08:43 -0700 | [diff] [blame] | 1691 | |
| 1692 | if (!args->cloning && args->deepen) { |
| 1693 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
| 1694 | struct ref *iterator = ref_cpy; |
| 1695 | opt.shallow_file = alternate_shallow_file; |
| 1696 | if (args->deepen) |
| 1697 | opt.is_deepening_fetch = 1; |
| 1698 | if (check_connected(iterate_ref_map, &iterator, &opt)) { |
| 1699 | error(_("remote did not send all necessary objects")); |
| 1700 | free_refs(ref_cpy); |
| 1701 | ref_cpy = NULL; |
| 1702 | rollback_lock_file(&shallow_lock); |
| 1703 | goto cleanup; |
| 1704 | } |
| 1705 | args->connectivity_checked = 1; |
| 1706 | } |
| 1707 | |
Jonathan Tan | e2842b3 | 2018-08-01 13:13:20 -0700 | [diff] [blame] | 1708 | update_shallow(args, sought, nr_sought, &si); |
Jonathan Tan | cf1e7c0 | 2018-07-02 15:08:43 -0700 | [diff] [blame] | 1709 | cleanup: |
Nguyễn Thái Ngọc Duy | beea415 | 2013-12-05 20:02:39 +0700 | [diff] [blame] | 1710 | clear_shallow_info(&si); |
Jonathan Tan | 1339078 | 2019-03-26 12:31:21 -0700 | [diff] [blame] | 1711 | oid_array_clear(&shallows_scratch); |
Nguyễn Thái Ngọc Duy | 745f7a8 | 2012-10-26 22:53:55 +0700 | [diff] [blame] | 1712 | return ref_cpy; |
| 1713 | } |
Matt McCutchen | e860d96 | 2017-02-22 11:01:22 -0500 | [diff] [blame] | 1714 | |
| 1715 | int report_unmatched_refs(struct ref **sought, int nr_sought) |
| 1716 | { |
| 1717 | int i, ret = 0; |
| 1718 | |
| 1719 | for (i = 0; i < nr_sought; i++) { |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 1720 | if (!sought[i]) |
Matt McCutchen | e860d96 | 2017-02-22 11:01:22 -0500 | [diff] [blame] | 1721 | continue; |
Matt McCutchen | d56583d | 2017-02-22 11:05:57 -0500 | [diff] [blame] | 1722 | switch (sought[i]->match_status) { |
| 1723 | case REF_MATCHED: |
| 1724 | continue; |
| 1725 | case REF_NOT_MATCHED: |
| 1726 | error(_("no such remote ref %s"), sought[i]->name); |
| 1727 | break; |
| 1728 | case REF_UNADVERTISED_NOT_ALLOWED: |
| 1729 | error(_("Server does not allow request for unadvertised object %s"), |
| 1730 | sought[i]->name); |
| 1731 | break; |
| 1732 | } |
Matt McCutchen | e860d96 | 2017-02-22 11:01:22 -0500 | [diff] [blame] | 1733 | ret = 1; |
| 1734 | } |
| 1735 | return ret; |
| 1736 | } |