Elijah Newren | 5579f44 | 2023-04-11 00:41:48 -0700 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 3 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 4 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 5 | #include "hex.h" |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 6 | #include "refs.h" |
| 7 | #include "pkt-line.h" |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 8 | #include "sideband.h" |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 9 | #include "repository.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 10 | #include "object-store-ll.h" |
Elijah Newren | 6f2d743 | 2023-04-11 03:00:42 +0000 | [diff] [blame] | 11 | #include "oid-array.h" |
Junio C Hamano | f6b42a8 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 12 | #include "tag.h" |
| 13 | #include "object.h" |
Johannes Schindelin | f0243f2 | 2005-10-28 04:48:32 +0200 | [diff] [blame] | 14 | #include "commit.h" |
Johannes Schindelin | 9b8dc26 | 2006-10-30 20:08:43 +0100 | [diff] [blame] | 15 | #include "diff.h" |
| 16 | #include "revision.h" |
| 17 | #include "list-objects.h" |
Jeff Hostetler | 10ac85c | 2017-12-08 15:58:39 +0000 | [diff] [blame] | 18 | #include "list-objects-filter.h" |
| 19 | #include "list-objects-filter-options.h" |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 20 | #include "run-command.h" |
Junio C Hamano | 47a5918 | 2013-07-08 13:56:53 -0700 | [diff] [blame] | 21 | #include "connect.h" |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 22 | #include "sigchain.h" |
Jeff King | ff5effd | 2012-08-03 12:19:16 -0400 | [diff] [blame] | 23 | #include "version.h" |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 24 | #include "string-list.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 25 | #include "strvec.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 26 | #include "trace2.h" |
Jeff King | 5411b10 | 2016-10-11 17:20:16 -0400 | [diff] [blame] | 27 | #include "prio-queue.h" |
Brandon Williams | aa9bab2 | 2017-10-16 10:55:26 -0700 | [diff] [blame] | 28 | #include "protocol.h" |
Jeff Hostetler | 10ac85c | 2017-12-08 15:58:39 +0000 | [diff] [blame] | 29 | #include "quote.h" |
Brandon Williams | a3d6b53 | 2018-03-14 11:31:41 -0700 | [diff] [blame] | 30 | #include "upload-pack.h" |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 31 | #include "serve.h" |
Derrick Stolee | 829a321 | 2018-08-20 18:24:34 +0000 | [diff] [blame] | 32 | #include "commit-graph.h" |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 33 | #include "commit-reach.h" |
Taylor Blau | 120ad2b | 2020-04-30 13:48:50 -0600 | [diff] [blame] | 34 | #include "shallow.h" |
Elijah Newren | d48be35 | 2023-03-21 06:26:07 +0000 | [diff] [blame] | 35 | #include "write-or-die.h" |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 36 | |
Nguyễn Thái Ngọc Duy | 208acbf | 2014-03-25 20:23:26 +0700 | [diff] [blame] | 37 | /* Remember to update object flag allocation in object.h */ |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 38 | #define THEY_HAVE (1u << 11) |
| 39 | #define OUR_REF (1u << 12) |
| 40 | #define WANTED (1u << 13) |
| 41 | #define COMMON_KNOWN (1u << 14) |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 42 | |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 43 | #define SHALLOW (1u << 16) |
| 44 | #define NOT_SHALLOW (1u << 17) |
| 45 | #define CLIENT_SHALLOW (1u << 18) |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 46 | #define HIDDEN_REF (1u << 19) |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 47 | |
Jonathan Tan | d1035ca | 2018-10-18 13:43:29 -0700 | [diff] [blame] | 48 | #define ALL_FLAGS (THEY_HAVE | OUR_REF | WANTED | COMMON_KNOWN | SHALLOW | \ |
| 49 | NOT_SHALLOW | CLIENT_SHALLOW | HIDDEN_REF) |
| 50 | |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 51 | /* Enum for allowed unadvertised object request (UOR) */ |
| 52 | enum allow_uor { |
| 53 | /* Allow specifying sha1 if it is a ref tip. */ |
| 54 | ALLOW_TIP_SHA1 = 0x01, |
| 55 | /* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ |
| 56 | ALLOW_REACHABLE_SHA1 = 0x02, |
| 57 | /* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */ |
| 58 | ALLOW_ANY_SHA1 = 0x07 |
| 59 | }; |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 60 | |
Christian Couder | a849728 | 2020-06-04 19:54:39 +0200 | [diff] [blame] | 61 | /* |
| 62 | * Please annotate, and if possible group together, fields used only |
| 63 | * for protocol v0 or only for protocol v2. |
| 64 | */ |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 65 | struct upload_pack_data { |
Christian Couder | a849728 | 2020-06-04 19:54:39 +0200 | [diff] [blame] | 66 | struct string_list symref; /* v0 only */ |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 67 | struct object_array want_obj; |
| 68 | struct object_array have_obj; |
Christian Couder | a849728 | 2020-06-04 19:54:39 +0200 | [diff] [blame] | 69 | struct oid_array haves; /* v2 only */ |
| 70 | struct string_list wanted_refs; /* v2 only */ |
Taylor Blau | c45841f | 2023-07-10 17:12:33 -0400 | [diff] [blame] | 71 | struct strvec hidden_refs; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 72 | |
| 73 | struct object_array shallows; |
| 74 | struct string_list deepen_not; |
Christian Couder | de0e9f7 | 2020-06-11 14:05:10 +0200 | [diff] [blame] | 75 | struct object_array extra_edge_obj; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 76 | int depth; |
| 77 | timestamp_t deepen_since; |
| 78 | int deepen_rev_list; |
| 79 | int deepen_relative; |
Christian Couder | f203a88 | 2020-06-04 19:54:46 +0200 | [diff] [blame] | 80 | int keepalive; |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 81 | int shallow_nr; |
Christian Couder | f01c791 | 2020-06-11 14:05:17 +0200 | [diff] [blame] | 82 | timestamp_t oldest_have; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 83 | |
Christian Couder | d40f04e | 2020-06-04 19:54:40 +0200 | [diff] [blame] | 84 | unsigned int timeout; /* v0 only */ |
Christian Couder | e9d882b | 2020-06-04 19:54:44 +0200 | [diff] [blame] | 85 | enum { |
| 86 | NO_MULTI_ACK = 0, |
| 87 | MULTI_ACK = 1, |
| 88 | MULTI_ACK_DETAILED = 2 |
| 89 | } multi_ack; /* v0 only */ |
Christian Couder | d40f04e | 2020-06-04 19:54:40 +0200 | [diff] [blame] | 90 | |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 91 | /* 0 for no sideband, otherwise DEFAULT_PACKET_MAX or LARGE_PACKET_MAX */ |
| 92 | int use_sideband; |
| 93 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 94 | struct string_list uri_protocols; |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 95 | enum allow_uor allow_uor; |
Christian Couder | f1514c6 | 2020-06-11 14:05:11 +0200 | [diff] [blame] | 96 | |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 97 | struct list_objects_filter_options filter_options; |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 98 | struct string_list allowed_filters; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 99 | |
| 100 | struct packet_writer writer; |
| 101 | |
Christian Couder | 339a984 | 2020-06-04 19:54:50 +0200 | [diff] [blame] | 102 | const char *pack_objects_hook; |
| 103 | |
Christian Couder | a849728 | 2020-06-04 19:54:39 +0200 | [diff] [blame] | 104 | unsigned stateless_rpc : 1; /* v0 only */ |
Christian Couder | d40f04e | 2020-06-04 19:54:40 +0200 | [diff] [blame] | 105 | unsigned no_done : 1; /* v0 only */ |
| 106 | unsigned daemon_mode : 1; /* v0 only */ |
Christian Couder | 59a9026 | 2020-06-04 19:54:42 +0200 | [diff] [blame] | 107 | unsigned filter_capability_requested : 1; /* v0 only */ |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 108 | |
| 109 | unsigned use_thin_pack : 1; |
| 110 | unsigned use_ofs_delta : 1; |
| 111 | unsigned no_progress : 1; |
| 112 | unsigned use_include_tag : 1; |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 113 | unsigned wait_for_done : 1; |
Christian Couder | 59abe19 | 2020-06-04 19:54:47 +0200 | [diff] [blame] | 114 | unsigned allow_filter : 1; |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 115 | unsigned allow_filter_fallback : 1; |
Taylor Blau | 5b01a4e | 2020-08-03 14:00:17 -0400 | [diff] [blame] | 116 | unsigned long tree_filter_max_depth; |
Christian Couder | a849728 | 2020-06-04 19:54:39 +0200 | [diff] [blame] | 117 | |
| 118 | unsigned done : 1; /* v2 only */ |
Christian Couder | d1d7a94 | 2020-06-04 19:54:48 +0200 | [diff] [blame] | 119 | unsigned allow_ref_in_want : 1; /* v2 only */ |
Christian Couder | e3835cd | 2020-06-04 19:54:49 +0200 | [diff] [blame] | 120 | unsigned allow_sideband_all : 1; /* v2 only */ |
Josh Steadmon | 791e1ad | 2020-11-11 15:29:27 -0800 | [diff] [blame] | 121 | unsigned advertise_sid : 1; |
brian m. carlson | 933e3a4 | 2023-05-17 19:24:43 +0000 | [diff] [blame] | 122 | unsigned sent_capabilities : 1; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static void upload_pack_data_init(struct upload_pack_data *data) |
| 126 | { |
Christian Couder | 438528f | 2020-05-15 12:04:49 +0200 | [diff] [blame] | 127 | struct string_list symref = STRING_LIST_INIT_DUP; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 128 | struct string_list wanted_refs = STRING_LIST_INIT_DUP; |
Taylor Blau | c45841f | 2023-07-10 17:12:33 -0400 | [diff] [blame] | 129 | struct strvec hidden_refs = STRVEC_INIT; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 130 | struct object_array want_obj = OBJECT_ARRAY_INIT; |
| 131 | struct object_array have_obj = OBJECT_ARRAY_INIT; |
| 132 | struct oid_array haves = OID_ARRAY_INIT; |
| 133 | struct object_array shallows = OBJECT_ARRAY_INIT; |
| 134 | struct string_list deepen_not = STRING_LIST_INIT_DUP; |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 135 | struct string_list uri_protocols = STRING_LIST_INIT_DUP; |
Christian Couder | de0e9f7 | 2020-06-11 14:05:10 +0200 | [diff] [blame] | 136 | struct object_array extra_edge_obj = OBJECT_ARRAY_INIT; |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 137 | struct string_list allowed_filters = STRING_LIST_INIT_DUP; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 138 | |
| 139 | memset(data, 0, sizeof(*data)); |
Christian Couder | 438528f | 2020-05-15 12:04:49 +0200 | [diff] [blame] | 140 | data->symref = symref; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 141 | data->wanted_refs = wanted_refs; |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 142 | data->hidden_refs = hidden_refs; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 143 | data->want_obj = want_obj; |
| 144 | data->have_obj = have_obj; |
| 145 | data->haves = haves; |
| 146 | data->shallows = shallows; |
| 147 | data->deepen_not = deepen_not; |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 148 | data->uri_protocols = uri_protocols; |
Christian Couder | de0e9f7 | 2020-06-11 14:05:10 +0200 | [diff] [blame] | 149 | data->extra_edge_obj = extra_edge_obj; |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 150 | data->allowed_filters = allowed_filters; |
| 151 | data->allow_filter_fallback = 1; |
Taylor Blau | 5b01a4e | 2020-08-03 14:00:17 -0400 | [diff] [blame] | 152 | data->tree_filter_max_depth = ULONG_MAX; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 153 | packet_writer_init(&data->writer, 1); |
Jeff King | 2a01bde | 2022-09-11 01:03:07 -0400 | [diff] [blame] | 154 | list_objects_filter_init(&data->filter_options); |
Christian Couder | f203a88 | 2020-06-04 19:54:46 +0200 | [diff] [blame] | 155 | |
| 156 | data->keepalive = 5; |
Josh Steadmon | 791e1ad | 2020-11-11 15:29:27 -0800 | [diff] [blame] | 157 | data->advertise_sid = 0; |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void upload_pack_data_clear(struct upload_pack_data *data) |
| 161 | { |
Christian Couder | 438528f | 2020-05-15 12:04:49 +0200 | [diff] [blame] | 162 | string_list_clear(&data->symref, 1); |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 163 | string_list_clear(&data->wanted_refs, 1); |
Taylor Blau | c45841f | 2023-07-10 17:12:33 -0400 | [diff] [blame] | 164 | strvec_clear(&data->hidden_refs); |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 165 | object_array_clear(&data->want_obj); |
| 166 | object_array_clear(&data->have_obj); |
| 167 | oid_array_clear(&data->haves); |
| 168 | object_array_clear(&data->shallows); |
| 169 | string_list_clear(&data->deepen_not, 0); |
Christian Couder | de0e9f7 | 2020-06-11 14:05:10 +0200 | [diff] [blame] | 170 | object_array_clear(&data->extra_edge_obj); |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 171 | list_objects_filter_release(&data->filter_options); |
Taylor Blau | 8d133f5 | 2020-12-03 13:55:18 -0500 | [diff] [blame] | 172 | string_list_clear(&data->allowed_filters, 0); |
Christian Couder | 339a984 | 2020-06-04 19:54:50 +0200 | [diff] [blame] | 173 | |
| 174 | free((char *)data->pack_objects_hook); |
Christian Couder | e849832 | 2020-05-15 12:04:44 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Christian Couder | d40f04e | 2020-06-04 19:54:40 +0200 | [diff] [blame] | 177 | static void reset_timeout(unsigned int timeout) |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 178 | { |
| 179 | alarm(timeout); |
| 180 | } |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 181 | |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 182 | static void send_client_data(int fd, const char *data, ssize_t sz, |
| 183 | int use_sideband) |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 184 | { |
Lukas Fleischer | 4c4b7d1 | 2016-06-14 16:49:16 +0200 | [diff] [blame] | 185 | if (use_sideband) { |
| 186 | send_sideband(1, fd, data, sz, use_sideband); |
Lukas Fleischer | fcf0fe9 | 2016-06-14 16:49:17 +0200 | [diff] [blame] | 187 | return; |
Lukas Fleischer | 4c4b7d1 | 2016-06-14 16:49:16 +0200 | [diff] [blame] | 188 | } |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 189 | if (fd == 3) |
| 190 | /* emergency quit */ |
| 191 | fd = 2; |
| 192 | if (fd == 2) { |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 193 | /* XXX: are we happy to lose stuff here? */ |
Junio C Hamano | 958c24b | 2006-09-10 03:20:24 -0700 | [diff] [blame] | 194 | xwrite(fd, data, sz); |
Lukas Fleischer | fcf0fe9 | 2016-06-14 16:49:17 +0200 | [diff] [blame] | 195 | return; |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 196 | } |
Jeff King | cdf4fb8 | 2013-02-20 15:01:56 -0500 | [diff] [blame] | 197 | write_or_die(fd, data, sz); |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Nguyễn Thái Ngọc Duy | b790e0f | 2014-03-11 19:59:46 +0700 | [diff] [blame] | 200 | static int write_one_shallow(const struct commit_graft *graft, void *cb_data) |
| 201 | { |
| 202 | FILE *fp = cb_data; |
| 203 | if (graft->nr_parent == -1) |
brian m. carlson | 7683e2e | 2015-03-13 23:39:34 +0000 | [diff] [blame] | 204 | fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid)); |
Nguyễn Thái Ngọc Duy | b790e0f | 2014-03-11 19:59:46 +0700 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
Jonathan Tan | acaaca7 | 2020-06-10 13:57:21 -0700 | [diff] [blame] | 208 | struct output_state { |
Jacob Vosmaer | 55a9651 | 2021-12-14 20:46:26 +0100 | [diff] [blame] | 209 | /* |
| 210 | * We do writes no bigger than LARGE_PACKET_DATA_MAX - 1, because with |
| 211 | * sideband-64k the band designator takes up 1 byte of space. Because |
| 212 | * relay_pack_data keeps the last byte to itself, we make the buffer 1 |
| 213 | * byte bigger than the intended maximum write size. |
| 214 | */ |
| 215 | char buffer[(LARGE_PACKET_DATA_MAX - 1) + 1]; |
Jonathan Tan | acaaca7 | 2020-06-10 13:57:21 -0700 | [diff] [blame] | 216 | int used; |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 217 | unsigned packfile_uris_started : 1; |
| 218 | unsigned packfile_started : 1; |
Jonathan Tan | acaaca7 | 2020-06-10 13:57:21 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | static int relay_pack_data(int pack_objects_out, struct output_state *os, |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 222 | int use_sideband, int write_packfile_line) |
Jonathan Tan | acaaca7 | 2020-06-10 13:57:21 -0700 | [diff] [blame] | 223 | { |
| 224 | /* |
| 225 | * We keep the last byte to ourselves |
| 226 | * in case we detect broken rev-list, so that we |
| 227 | * can leave the stream corrupted. This is |
| 228 | * unfortunate -- unpack-objects would happily |
| 229 | * accept a valid packdata with trailing garbage, |
| 230 | * so appending garbage after we pass all the |
| 231 | * pack data is not good enough to signal |
| 232 | * breakage to downstream. |
| 233 | */ |
| 234 | ssize_t readsz; |
| 235 | |
| 236 | readsz = xread(pack_objects_out, os->buffer + os->used, |
| 237 | sizeof(os->buffer) - os->used); |
| 238 | if (readsz < 0) { |
| 239 | return readsz; |
| 240 | } |
| 241 | os->used += readsz; |
| 242 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 243 | while (!os->packfile_started) { |
| 244 | char *p; |
| 245 | if (os->used >= 4 && !memcmp(os->buffer, "PACK", 4)) { |
| 246 | os->packfile_started = 1; |
| 247 | if (write_packfile_line) { |
| 248 | if (os->packfile_uris_started) |
| 249 | packet_delim(1); |
| 250 | packet_write_fmt(1, "\1packfile\n"); |
| 251 | } |
| 252 | break; |
| 253 | } |
| 254 | if ((p = memchr(os->buffer, '\n', os->used))) { |
| 255 | if (!os->packfile_uris_started) { |
| 256 | os->packfile_uris_started = 1; |
| 257 | if (!write_packfile_line) |
| 258 | BUG("packfile_uris requires sideband-all"); |
| 259 | packet_write_fmt(1, "\1packfile-uris\n"); |
| 260 | } |
| 261 | *p = '\0'; |
| 262 | packet_write_fmt(1, "\1%s\n", os->buffer); |
| 263 | |
| 264 | os->used -= p - os->buffer + 1; |
| 265 | memmove(os->buffer, p + 1, os->used); |
| 266 | } else { |
| 267 | /* |
| 268 | * Incomplete line. |
| 269 | */ |
| 270 | return readsz; |
| 271 | } |
| 272 | } |
| 273 | |
Jonathan Tan | acaaca7 | 2020-06-10 13:57:21 -0700 | [diff] [blame] | 274 | if (os->used > 1) { |
| 275 | send_client_data(1, os->buffer, os->used - 1, use_sideband); |
| 276 | os->buffer[0] = os->buffer[os->used - 1]; |
| 277 | os->used = 1; |
| 278 | } else { |
| 279 | send_client_data(1, os->buffer, os->used, use_sideband); |
| 280 | os->used = 0; |
| 281 | } |
| 282 | |
| 283 | return readsz; |
| 284 | } |
| 285 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 286 | static void create_pack_file(struct upload_pack_data *pack_data, |
| 287 | const struct string_list *uri_protocols) |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 288 | { |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 289 | struct child_process pack_objects = CHILD_PROCESS_INIT; |
Jacob Vosmaer | 55a9651 | 2021-12-14 20:46:26 +0100 | [diff] [blame] | 290 | struct output_state *output_state = xcalloc(1, sizeof(struct output_state)); |
Jonathan Tan | acaaca7 | 2020-06-10 13:57:21 -0700 | [diff] [blame] | 291 | char progress[128]; |
Junio C Hamano | 583b7ea | 2006-06-21 00:30:21 -0700 | [diff] [blame] | 292 | char abort_msg[] = "aborting due to possible repository " |
| 293 | "corruption on the remote side."; |
Junio C Hamano | 1456b04 | 2009-12-10 12:17:11 -0800 | [diff] [blame] | 294 | ssize_t sz; |
Michael Procter | 65a3629 | 2016-02-25 12:13:26 +0000 | [diff] [blame] | 295 | int i; |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 296 | FILE *pipe_fd; |
Linus Torvalds | 75bfc6c | 2005-07-04 16:35:13 -0700 | [diff] [blame] | 297 | |
Christian Couder | 339a984 | 2020-06-04 19:54:50 +0200 | [diff] [blame] | 298 | if (!pack_data->pack_objects_hook) |
Jeff King | 20b20a2 | 2016-05-18 18:45:37 -0400 | [diff] [blame] | 299 | pack_objects.git_cmd = 1; |
| 300 | else { |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 301 | strvec_push(&pack_objects.args, pack_data->pack_objects_hook); |
| 302 | strvec_push(&pack_objects.args, "git"); |
Jeff King | 20b20a2 | 2016-05-18 18:45:37 -0400 | [diff] [blame] | 303 | pack_objects.use_shell = 1; |
| 304 | } |
| 305 | |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 306 | if (pack_data->shallow_nr) { |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 307 | strvec_push(&pack_objects.args, "--shallow-file"); |
| 308 | strvec_push(&pack_objects.args, ""); |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 309 | } |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 310 | strvec_push(&pack_objects.args, "pack-objects"); |
| 311 | strvec_push(&pack_objects.args, "--revs"); |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 312 | if (pack_data->use_thin_pack) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 313 | strvec_push(&pack_objects.args, "--thin"); |
Linus Torvalds | 75bfc6c | 2005-07-04 16:35:13 -0700 | [diff] [blame] | 314 | |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 315 | strvec_push(&pack_objects.args, "--stdout"); |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 316 | if (pack_data->shallow_nr) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 317 | strvec_push(&pack_objects.args, "--shallow"); |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 318 | if (!pack_data->no_progress) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 319 | strvec_push(&pack_objects.args, "--progress"); |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 320 | if (pack_data->use_ofs_delta) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 321 | strvec_push(&pack_objects.args, "--delta-base-offset"); |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 322 | if (pack_data->use_include_tag) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 323 | strvec_push(&pack_objects.args, "--include-tag"); |
Christian Couder | c9f0325 | 2020-05-15 12:04:53 +0200 | [diff] [blame] | 324 | if (pack_data->filter_options.choice) { |
Matthew DeVore | cf9ceb5 | 2019-06-27 15:54:10 -0700 | [diff] [blame] | 325 | const char *spec = |
Christian Couder | c9f0325 | 2020-05-15 12:04:53 +0200 | [diff] [blame] | 326 | expand_list_objects_filter_spec(&pack_data->filter_options); |
Jacob Vosmaer | ad5df6b | 2021-01-28 17:04:53 +0100 | [diff] [blame] | 327 | strvec_pushf(&pack_objects.args, "--filter=%s", spec); |
Jeff Hostetler | 10ac85c | 2017-12-08 15:58:39 +0000 | [diff] [blame] | 328 | } |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 329 | if (uri_protocols) { |
| 330 | for (i = 0; i < uri_protocols->nr; i++) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 331 | strvec_pushf(&pack_objects.args, "--uri-protocol=%s", |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 332 | uri_protocols->items[i].string); |
| 333 | } |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 334 | |
Jeff King | b961219 | 2011-04-06 17:33:33 -0400 | [diff] [blame] | 335 | pack_objects.in = -1; |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 336 | pack_objects.out = -1; |
| 337 | pack_objects.err = -1; |
Jeff King | 309a402 | 2020-12-01 07:15:13 -0500 | [diff] [blame] | 338 | pack_objects.clean_on_exit = 1; |
Johannes Sixt | 21edd3f | 2007-10-19 21:48:03 +0200 | [diff] [blame] | 339 | |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 340 | if (start_command(&pack_objects)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 341 | die("git upload-pack: unable to fork git-pack-objects"); |
Johannes Schindelin | 83a5ad6 | 2007-02-20 03:01:44 +0100 | [diff] [blame] | 342 | |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 343 | pipe_fd = xfdopen(pack_objects.in, "w"); |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 344 | |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 345 | if (pack_data->shallow_nr) |
Nguyễn Thái Ngọc Duy | b790e0f | 2014-03-11 19:59:46 +0700 | [diff] [blame] | 346 | for_each_commit_graft(write_one_shallow, pipe_fd); |
| 347 | |
Christian Couder | c9f0325 | 2020-05-15 12:04:53 +0200 | [diff] [blame] | 348 | for (i = 0; i < pack_data->want_obj.nr; i++) |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 349 | fprintf(pipe_fd, "%s\n", |
Christian Couder | c9f0325 | 2020-05-15 12:04:53 +0200 | [diff] [blame] | 350 | oid_to_hex(&pack_data->want_obj.objects[i].item->oid)); |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 351 | fprintf(pipe_fd, "--not\n"); |
Christian Couder | c9f0325 | 2020-05-15 12:04:53 +0200 | [diff] [blame] | 352 | for (i = 0; i < pack_data->have_obj.nr; i++) |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 353 | fprintf(pipe_fd, "%s\n", |
Christian Couder | c9f0325 | 2020-05-15 12:04:53 +0200 | [diff] [blame] | 354 | oid_to_hex(&pack_data->have_obj.objects[i].item->oid)); |
Christian Couder | de0e9f7 | 2020-06-11 14:05:10 +0200 | [diff] [blame] | 355 | for (i = 0; i < pack_data->extra_edge_obj.nr; i++) |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 356 | fprintf(pipe_fd, "%s\n", |
Christian Couder | de0e9f7 | 2020-06-11 14:05:10 +0200 | [diff] [blame] | 357 | oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid)); |
Nguyễn Thái Ngọc Duy | cdab485 | 2013-08-16 16:52:05 +0700 | [diff] [blame] | 358 | fprintf(pipe_fd, "\n"); |
| 359 | fflush(pipe_fd); |
| 360 | fclose(pipe_fd); |
Nick Edelen | f0cea83 | 2009-06-10 01:50:18 +0200 | [diff] [blame] | 361 | |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 362 | /* We read from pack_objects.err to capture stderr output for |
| 363 | * progress bar, and pack_objects.out to capture the pack data. |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 364 | */ |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 365 | |
| 366 | while (1) { |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 367 | struct pollfd pfd[2]; |
Christian Couder | f203a88 | 2020-06-04 19:54:46 +0200 | [diff] [blame] | 368 | int pe, pu, pollsize, polltimeout; |
Jeff King | 05e9515 | 2013-09-08 05:01:31 -0400 | [diff] [blame] | 369 | int ret; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 370 | |
Christian Couder | d40f04e | 2020-06-04 19:54:40 +0200 | [diff] [blame] | 371 | reset_timeout(pack_data->timeout); |
Matthias Lederhofer | 0d516ad | 2006-07-18 19:14:51 +0200 | [diff] [blame] | 372 | |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 373 | pollsize = 0; |
Junio C Hamano | 363b781 | 2006-06-20 22:48:23 -0700 | [diff] [blame] | 374 | pe = pu = -1; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 375 | |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 376 | if (0 <= pack_objects.out) { |
| 377 | pfd[pollsize].fd = pack_objects.out; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 378 | pfd[pollsize].events = POLLIN; |
| 379 | pu = pollsize; |
| 380 | pollsize++; |
| 381 | } |
Johannes Sixt | cc41fa8 | 2007-10-19 21:47:59 +0200 | [diff] [blame] | 382 | if (0 <= pack_objects.err) { |
| 383 | pfd[pollsize].fd = pack_objects.err; |
Junio C Hamano | 363b781 | 2006-06-20 22:48:23 -0700 | [diff] [blame] | 384 | pfd[pollsize].events = POLLIN; |
| 385 | pe = pollsize; |
| 386 | pollsize++; |
| 387 | } |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 388 | |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 389 | if (!pollsize) |
| 390 | break; |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 391 | |
Christian Couder | f203a88 | 2020-06-04 19:54:46 +0200 | [diff] [blame] | 392 | polltimeout = pack_data->keepalive < 0 |
| 393 | ? -1 |
| 394 | : 1000 * pack_data->keepalive; |
| 395 | |
| 396 | ret = poll(pfd, pollsize, polltimeout); |
Edward Thomson | 6c71f8b | 2014-08-22 15:19:11 +0000 | [diff] [blame] | 397 | |
Jeff King | 05e9515 | 2013-09-08 05:01:31 -0400 | [diff] [blame] | 398 | if (ret < 0) { |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 399 | if (errno != EINTR) { |
Nguyễn Thái Ngọc Duy | d2b6afa | 2016-05-08 16:47:59 +0700 | [diff] [blame] | 400 | error_errno("poll failed, resuming"); |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 401 | sleep(1); |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 402 | } |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 403 | continue; |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 404 | } |
Nicolas Pitre | 6b59f51 | 2009-11-11 17:24:42 -0500 | [diff] [blame] | 405 | if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) { |
| 406 | /* Status ready; we ship that in the side-band |
| 407 | * or dump to the standard error. |
| 408 | */ |
| 409 | sz = xread(pack_objects.err, progress, |
| 410 | sizeof(progress)); |
| 411 | if (0 < sz) |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 412 | send_client_data(2, progress, sz, |
| 413 | pack_data->use_sideband); |
Nicolas Pitre | 6b59f51 | 2009-11-11 17:24:42 -0500 | [diff] [blame] | 414 | else if (sz == 0) { |
| 415 | close(pack_objects.err); |
| 416 | pack_objects.err = -1; |
| 417 | } |
| 418 | else |
| 419 | goto fail; |
| 420 | /* give priority to status messages */ |
| 421 | continue; |
| 422 | } |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 423 | if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { |
Jonathan Tan | acaaca7 | 2020-06-10 13:57:21 -0700 | [diff] [blame] | 424 | int result = relay_pack_data(pack_objects.out, |
Jacob Vosmaer | 55a9651 | 2021-12-14 20:46:26 +0100 | [diff] [blame] | 425 | output_state, |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 426 | pack_data->use_sideband, |
| 427 | !!uri_protocols); |
Jonathan Tan | acaaca7 | 2020-06-10 13:57:21 -0700 | [diff] [blame] | 428 | |
| 429 | if (result == 0) { |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 430 | close(pack_objects.out); |
| 431 | pack_objects.out = -1; |
Jonathan Tan | acaaca7 | 2020-06-10 13:57:21 -0700 | [diff] [blame] | 432 | } else if (result < 0) { |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 433 | goto fail; |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 434 | } |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 435 | } |
Jeff King | 05e9515 | 2013-09-08 05:01:31 -0400 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | * We hit the keepalive timeout without saying anything; send |
| 439 | * an empty message on the data sideband just to let the other |
| 440 | * side know we're still working on it, but don't have any data |
| 441 | * yet. |
| 442 | * |
| 443 | * If we don't have a sideband channel, there's no room in the |
| 444 | * protocol to say anything, so those clients are just out of |
| 445 | * luck. |
| 446 | */ |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 447 | if (!ret && pack_data->use_sideband) { |
Jeff King | 05e9515 | 2013-09-08 05:01:31 -0400 | [diff] [blame] | 448 | static const char buf[] = "0005\1"; |
| 449 | write_or_die(1, buf, 5); |
| 450 | } |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 451 | } |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 452 | |
| 453 | if (finish_command(&pack_objects)) { |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 454 | error("git upload-pack: git-pack-objects died with error."); |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 455 | goto fail; |
| 456 | } |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 457 | |
| 458 | /* flush the data */ |
Jacob Vosmaer | 55a9651 | 2021-12-14 20:46:26 +0100 | [diff] [blame] | 459 | if (output_state->used > 0) { |
| 460 | send_client_data(1, output_state->buffer, output_state->used, |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 461 | pack_data->use_sideband); |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 462 | fprintf(stderr, "flushed.\n"); |
| 463 | } |
Jacob Vosmaer | 55a9651 | 2021-12-14 20:46:26 +0100 | [diff] [blame] | 464 | free(output_state); |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 465 | if (pack_data->use_sideband) |
Johannes Sixt | 4c324c0 | 2007-11-04 20:46:48 +0100 | [diff] [blame] | 466 | packet_flush(1); |
| 467 | return; |
| 468 | |
Junio C Hamano | b1c71b7 | 2006-06-20 18:26:34 -0700 | [diff] [blame] | 469 | fail: |
Ævar Arnfjörð Bjarmason | c68d5db | 2022-07-28 01:13:42 +0200 | [diff] [blame] | 470 | free(output_state); |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 471 | send_client_data(3, abort_msg, sizeof(abort_msg), |
| 472 | pack_data->use_sideband); |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 473 | die("git upload-pack: %s", abort_msg); |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Christian Couder | ea2c6e6 | 2020-06-11 14:05:18 +0200 | [diff] [blame] | 476 | static int do_got_oid(struct upload_pack_data *data, const struct object_id *oid) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 477 | { |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 478 | int we_knew_they_have = 0; |
Christian Couder | ea2c6e6 | 2020-06-11 14:05:18 +0200 | [diff] [blame] | 479 | struct object *o = parse_object(the_repository, oid); |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 480 | |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 481 | if (!o) |
brian m. carlson | cf93982 | 2017-05-06 22:10:28 +0000 | [diff] [blame] | 482 | die("oops (%s)", oid_to_hex(oid)); |
Junio C Hamano | 182a8da | 2006-08-12 22:16:51 -0700 | [diff] [blame] | 483 | if (o->type == OBJ_COMMIT) { |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 484 | struct commit_list *parents; |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 485 | struct commit *commit = (struct commit *)o; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 486 | if (o->flags & THEY_HAVE) |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 487 | we_knew_they_have = 1; |
| 488 | else |
| 489 | o->flags |= THEY_HAVE; |
Christian Couder | f01c791 | 2020-06-11 14:05:17 +0200 | [diff] [blame] | 490 | if (!data->oldest_have || (commit->date < data->oldest_have)) |
| 491 | data->oldest_have = commit->date; |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 492 | for (parents = commit->parents; |
Junio C Hamano | b1e9fff | 2006-07-05 18:00:02 -0700 | [diff] [blame] | 493 | parents; |
| 494 | parents = parents->next) |
| 495 | parents->item->object.flags |= THEY_HAVE; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 496 | } |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 497 | if (!we_knew_they_have) { |
Christian Couder | 460ed0d | 2020-06-11 14:05:16 +0200 | [diff] [blame] | 498 | add_object_array(o, NULL, &data->have_obj); |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 499 | return 1; |
| 500 | } |
| 501 | return 0; |
| 502 | } |
| 503 | |
Christian Couder | ea2c6e6 | 2020-06-11 14:05:18 +0200 | [diff] [blame] | 504 | static int got_oid(struct upload_pack_data *data, |
| 505 | const char *hex, struct object_id *oid) |
| 506 | { |
| 507 | if (get_oid_hex(hex, oid)) |
| 508 | die("git upload-pack: expected SHA1 object, got '%s'", hex); |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 509 | if (!repo_has_object_file_with_flags(the_repository, oid, |
| 510 | OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT)) |
Christian Couder | ea2c6e6 | 2020-06-11 14:05:18 +0200 | [diff] [blame] | 511 | return -1; |
| 512 | return do_got_oid(data, oid); |
| 513 | } |
| 514 | |
Christian Couder | 0866734 | 2020-06-11 14:05:15 +0200 | [diff] [blame] | 515 | static int ok_to_give_up(struct upload_pack_data *data) |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 516 | { |
Abhishek Kumar | d7f9278 | 2021-01-16 18:11:13 +0000 | [diff] [blame] | 517 | timestamp_t min_generation = GENERATION_NUMBER_ZERO; |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 518 | |
Christian Couder | 0866734 | 2020-06-11 14:05:15 +0200 | [diff] [blame] | 519 | if (!data->have_obj.nr) |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 520 | return 0; |
| 521 | |
Christian Couder | 0866734 | 2020-06-11 14:05:15 +0200 | [diff] [blame] | 522 | return can_all_from_reach_with_flag(&data->want_obj, THEY_HAVE, |
Christian Couder | f01c791 | 2020-06-11 14:05:17 +0200 | [diff] [blame] | 523 | COMMON_KNOWN, data->oldest_have, |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 524 | min_generation); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Christian Couder | 0797769 | 2020-05-15 12:04:46 +0200 | [diff] [blame] | 527 | static int get_common_commits(struct upload_pack_data *data, |
| 528 | struct packet_reader *reader) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 529 | { |
brian m. carlson | cf93982 | 2017-05-06 22:10:28 +0000 | [diff] [blame] | 530 | struct object_id oid; |
| 531 | char last_hex[GIT_MAX_HEXSZ + 1]; |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 532 | int got_common = 0; |
| 533 | int got_other = 0; |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 534 | int sent_ready = 0; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 535 | |
Johannes Schindelin | f0243f2 | 2005-10-28 04:48:32 +0200 | [diff] [blame] | 536 | save_commit_buffer = 0; |
| 537 | |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 538 | for (;;) { |
Nguyễn Thái Ngọc Duy | 8bf3b75 | 2016-06-12 17:53:49 +0700 | [diff] [blame] | 539 | const char *arg; |
| 540 | |
Christian Couder | d40f04e | 2020-06-04 19:54:40 +0200 | [diff] [blame] | 541 | reset_timeout(data->timeout); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 542 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 543 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) { |
Christian Couder | e9d882b | 2020-06-04 19:54:44 +0200 | [diff] [blame] | 544 | if (data->multi_ack == MULTI_ACK_DETAILED |
Christian Couder | 0797769 | 2020-05-15 12:04:46 +0200 | [diff] [blame] | 545 | && got_common |
| 546 | && !got_other |
Christian Couder | 0866734 | 2020-06-11 14:05:15 +0200 | [diff] [blame] | 547 | && ok_to_give_up(data)) { |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 548 | sent_ready = 1; |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 549 | packet_write_fmt(1, "ACK %s ready\n", last_hex); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 550 | } |
Christian Couder | 53d6950 | 2020-06-04 19:54:43 +0200 | [diff] [blame] | 551 | if (data->have_obj.nr == 0 || data->multi_ack) |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 552 | packet_write_fmt(1, "NAK\n"); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 553 | |
Christian Couder | d40f04e | 2020-06-04 19:54:40 +0200 | [diff] [blame] | 554 | if (data->no_done && sent_ready) { |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 555 | packet_write_fmt(1, "ACK %s\n", last_hex); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 556 | return 0; |
| 557 | } |
Christian Couder | df654ab | 2020-05-15 12:04:52 +0200 | [diff] [blame] | 558 | if (data->stateless_rpc) |
Shawn O. Pearce | 42526b4 | 2009-10-30 17:47:33 -0700 | [diff] [blame] | 559 | exit(0); |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 560 | got_common = 0; |
| 561 | got_other = 0; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 562 | continue; |
| 563 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 564 | if (skip_prefix(reader->line, "have ", &arg)) { |
Christian Couder | 460ed0d | 2020-06-11 14:05:16 +0200 | [diff] [blame] | 565 | switch (got_oid(data, arg, &oid)) { |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 566 | case -1: /* they have what we do not */ |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 567 | got_other = 1; |
Christian Couder | 53d6950 | 2020-06-04 19:54:43 +0200 | [diff] [blame] | 568 | if (data->multi_ack |
Christian Couder | 0866734 | 2020-06-11 14:05:15 +0200 | [diff] [blame] | 569 | && ok_to_give_up(data)) { |
brian m. carlson | cf93982 | 2017-05-06 22:10:28 +0000 | [diff] [blame] | 570 | const char *hex = oid_to_hex(&oid); |
Christian Couder | e9d882b | 2020-06-04 19:54:44 +0200 | [diff] [blame] | 571 | if (data->multi_ack == MULTI_ACK_DETAILED) { |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 572 | sent_ready = 1; |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 573 | packet_write_fmt(1, "ACK %s ready\n", hex); |
Junio C Hamano | 4e10cf9 | 2011-03-29 12:29:10 -0700 | [diff] [blame] | 574 | } else |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 575 | packet_write_fmt(1, "ACK %s continue\n", hex); |
Shawn O. Pearce | 78affc4 | 2009-10-30 17:47:25 -0700 | [diff] [blame] | 576 | } |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 577 | break; |
| 578 | default: |
Shawn O. Pearce | 49bee71 | 2011-03-14 16:48:39 -0700 | [diff] [blame] | 579 | got_common = 1; |
brian m. carlson | 55dc227 | 2018-05-02 00:25:51 +0000 | [diff] [blame] | 580 | oid_to_hex_r(last_hex, &oid); |
Christian Couder | e9d882b | 2020-06-04 19:54:44 +0200 | [diff] [blame] | 581 | if (data->multi_ack == MULTI_ACK_DETAILED) |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 582 | packet_write_fmt(1, "ACK %s common\n", last_hex); |
Christian Couder | 53d6950 | 2020-06-04 19:54:43 +0200 | [diff] [blame] | 583 | else if (data->multi_ack) |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 584 | packet_write_fmt(1, "ACK %s continue\n", last_hex); |
Christian Couder | 0797769 | 2020-05-15 12:04:46 +0200 | [diff] [blame] | 585 | else if (data->have_obj.nr == 1) |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 586 | packet_write_fmt(1, "ACK %s\n", last_hex); |
Junio C Hamano | 937a515 | 2006-07-05 21:28:20 -0700 | [diff] [blame] | 587 | break; |
Junio C Hamano | af2d3aa | 2005-10-25 14:55:24 -0700 | [diff] [blame] | 588 | } |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 589 | continue; |
| 590 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 591 | if (!strcmp(reader->line, "done")) { |
Christian Couder | 0797769 | 2020-05-15 12:04:46 +0200 | [diff] [blame] | 592 | if (data->have_obj.nr > 0) { |
Christian Couder | 53d6950 | 2020-06-04 19:54:43 +0200 | [diff] [blame] | 593 | if (data->multi_ack) |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 594 | packet_write_fmt(1, "ACK %s\n", last_hex); |
Johannes Schindelin | 1bd8c8f | 2005-10-28 04:49:16 +0200 | [diff] [blame] | 595 | return 0; |
| 596 | } |
Lars Schneider | 81c634e | 2016-10-16 16:20:29 -0700 | [diff] [blame] | 597 | packet_write_fmt(1, "NAK\n"); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 598 | return -1; |
| 599 | } |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 600 | die("git upload-pack: expected SHA1 list, got '%s'", reader->line); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 601 | } |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Taylor Blau | 18b6b1b | 2023-07-10 17:12:45 -0400 | [diff] [blame] | 604 | static int allow_hidden_refs(enum allow_uor allow_uor) |
| 605 | { |
| 606 | if ((allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1) |
| 607 | return 1; |
| 608 | return !(allow_uor & (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); |
| 609 | } |
| 610 | |
| 611 | static void for_each_namespaced_ref_1(each_ref_fn fn, |
| 612 | struct upload_pack_data *data) |
| 613 | { |
| 614 | const char **excludes = NULL; |
| 615 | /* |
| 616 | * If `data->allow_uor` allows fetching hidden refs, we need to |
| 617 | * mark all references (including hidden ones), to check in |
| 618 | * `is_our_ref()` below. |
| 619 | * |
| 620 | * Otherwise, we only care about whether each reference's object |
| 621 | * has the OUR_REF bit set or not, so do not need to visit |
| 622 | * hidden references. |
| 623 | */ |
| 624 | if (allow_hidden_refs(data->allow_uor)) |
| 625 | excludes = hidden_refs_to_excludes(&data->hidden_refs); |
| 626 | |
| 627 | for_each_namespaced_ref(excludes, fn, data); |
| 628 | } |
| 629 | |
| 630 | |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 631 | static int is_our_ref(struct object *o, enum allow_uor allow_uor) |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 632 | { |
Taylor Blau | 18b6b1b | 2023-07-10 17:12:45 -0400 | [diff] [blame] | 633 | return o->flags & ((allow_hidden_refs(allow_uor) ? 0 : HIDDEN_REF) | OUR_REF); |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Nguyễn Thái Ngọc Duy | 2997178 | 2016-06-12 17:54:07 +0700 | [diff] [blame] | 636 | /* |
| 637 | * on successful case, it's up to the caller to close cmd->out |
| 638 | */ |
| 639 | static int do_reachable_revlist(struct child_process *cmd, |
Nguyễn Thái Ngọc Duy | 079aa97 | 2016-06-12 17:54:08 +0700 | [diff] [blame] | 640 | struct object_array *src, |
Christian Couder | f1514c6 | 2020-06-11 14:05:11 +0200 | [diff] [blame] | 641 | struct object_array *reachable, |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 642 | enum allow_uor allow_uor) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 643 | { |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 644 | struct object *o; |
René Scharfe | a698d67 | 2020-08-12 18:52:55 +0200 | [diff] [blame] | 645 | FILE *cmd_in = NULL; |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 646 | int i; |
| 647 | |
Ævar Arnfjörð Bjarmason | 2b70989 | 2021-11-25 23:52:20 +0100 | [diff] [blame] | 648 | strvec_pushl(&cmd->args, "rev-list", "--stdin", NULL); |
Nguyễn Thái Ngọc Duy | 2997178 | 2016-06-12 17:54:07 +0700 | [diff] [blame] | 649 | cmd->git_cmd = 1; |
| 650 | cmd->no_stderr = 1; |
| 651 | cmd->in = -1; |
| 652 | cmd->out = -1; |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 653 | |
| 654 | /* |
Nguyễn Thái Ngọc Duy | 7fcbd37 | 2016-06-12 17:53:51 +0700 | [diff] [blame] | 655 | * If the next rev-list --stdin encounters an unknown commit, |
| 656 | * it terminates, which will cause SIGPIPE in the write loop |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 657 | * below. |
| 658 | */ |
| 659 | sigchain_push(SIGPIPE, SIG_IGN); |
| 660 | |
Nguyễn Thái Ngọc Duy | 2997178 | 2016-06-12 17:54:07 +0700 | [diff] [blame] | 661 | if (start_command(cmd)) |
Nguyễn Thái Ngọc Duy | 7fcbd37 | 2016-06-12 17:53:51 +0700 | [diff] [blame] | 662 | goto error; |
| 663 | |
René Scharfe | a698d67 | 2020-08-12 18:52:55 +0200 | [diff] [blame] | 664 | cmd_in = xfdopen(cmd->in, "w"); |
| 665 | |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 666 | for (i = get_max_object_index(); 0 < i; ) { |
| 667 | o = get_indexed_object(--i); |
Brian Harring | 2a74532 | 2011-08-23 22:47:17 -0700 | [diff] [blame] | 668 | if (!o) |
| 669 | continue; |
Nguyễn Thái Ngọc Duy | 079aa97 | 2016-06-12 17:54:08 +0700 | [diff] [blame] | 670 | if (reachable && o->type == OBJ_COMMIT) |
| 671 | o->flags &= ~TMP_MARK; |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 672 | if (!is_our_ref(o, allow_uor)) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 673 | continue; |
René Scharfe | a698d67 | 2020-08-12 18:52:55 +0200 | [diff] [blame] | 674 | if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 675 | goto error; |
| 676 | } |
Nguyễn Thái Ngọc Duy | 3f0f662 | 2016-06-12 17:53:52 +0700 | [diff] [blame] | 677 | for (i = 0; i < src->nr; i++) { |
| 678 | o = src->objects[i].item; |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 679 | if (is_our_ref(o, allow_uor)) { |
Nguyễn Thái Ngọc Duy | 079aa97 | 2016-06-12 17:54:08 +0700 | [diff] [blame] | 680 | if (reachable) |
| 681 | add_object_array(o, NULL, reachable); |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 682 | continue; |
Nguyễn Thái Ngọc Duy | 079aa97 | 2016-06-12 17:54:08 +0700 | [diff] [blame] | 683 | } |
| 684 | if (reachable && o->type == OBJ_COMMIT) |
| 685 | o->flags |= TMP_MARK; |
René Scharfe | a698d67 | 2020-08-12 18:52:55 +0200 | [diff] [blame] | 686 | if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 687 | goto error; |
| 688 | } |
René Scharfe | a698d67 | 2020-08-12 18:52:55 +0200 | [diff] [blame] | 689 | if (ferror(cmd_in) || fflush(cmd_in)) |
| 690 | goto error; |
| 691 | fclose(cmd_in); |
Nguyễn Thái Ngọc Duy | 2997178 | 2016-06-12 17:54:07 +0700 | [diff] [blame] | 692 | cmd->in = -1; |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 693 | sigchain_pop(SIGPIPE); |
| 694 | |
Nguyễn Thái Ngọc Duy | 2997178 | 2016-06-12 17:54:07 +0700 | [diff] [blame] | 695 | return 0; |
| 696 | |
| 697 | error: |
| 698 | sigchain_pop(SIGPIPE); |
| 699 | |
René Scharfe | a698d67 | 2020-08-12 18:52:55 +0200 | [diff] [blame] | 700 | if (cmd_in) |
| 701 | fclose(cmd_in); |
Nguyễn Thái Ngọc Duy | 2997178 | 2016-06-12 17:54:07 +0700 | [diff] [blame] | 702 | if (cmd->out >= 0) |
| 703 | close(cmd->out); |
| 704 | return -1; |
| 705 | } |
| 706 | |
Christian Couder | f1514c6 | 2020-06-11 14:05:11 +0200 | [diff] [blame] | 707 | static int get_reachable_list(struct upload_pack_data *data, |
Nguyễn Thái Ngọc Duy | 079aa97 | 2016-06-12 17:54:08 +0700 | [diff] [blame] | 708 | struct object_array *reachable) |
| 709 | { |
| 710 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 711 | int i; |
| 712 | struct object *o; |
brian m. carlson | 55dc227 | 2018-05-02 00:25:51 +0000 | [diff] [blame] | 713 | char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */ |
| 714 | const unsigned hexsz = the_hash_algo->hexsz; |
Nguyễn Thái Ngọc Duy | 079aa97 | 2016-06-12 17:54:08 +0700 | [diff] [blame] | 715 | |
Christian Couder | f1514c6 | 2020-06-11 14:05:11 +0200 | [diff] [blame] | 716 | if (do_reachable_revlist(&cmd, &data->shallows, reachable, |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 717 | data->allow_uor) < 0) |
Nguyễn Thái Ngọc Duy | 079aa97 | 2016-06-12 17:54:08 +0700 | [diff] [blame] | 718 | return -1; |
| 719 | |
brian m. carlson | 55dc227 | 2018-05-02 00:25:51 +0000 | [diff] [blame] | 720 | while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) { |
Jeff King | 62b89d4 | 2019-06-20 03:40:54 -0400 | [diff] [blame] | 721 | struct object_id oid; |
brian m. carlson | 55dc227 | 2018-05-02 00:25:51 +0000 | [diff] [blame] | 722 | const char *p; |
Nguyễn Thái Ngọc Duy | 079aa97 | 2016-06-12 17:54:08 +0700 | [diff] [blame] | 723 | |
Jeff King | 62b89d4 | 2019-06-20 03:40:54 -0400 | [diff] [blame] | 724 | if (parse_oid_hex(namebuf, &oid, &p) || *p != '\n') |
Nguyễn Thái Ngọc Duy | 079aa97 | 2016-06-12 17:54:08 +0700 | [diff] [blame] | 725 | break; |
| 726 | |
Jeff King | d0229ab | 2019-06-20 03:41:14 -0400 | [diff] [blame] | 727 | o = lookup_object(the_repository, &oid); |
Nguyễn Thái Ngọc Duy | 079aa97 | 2016-06-12 17:54:08 +0700 | [diff] [blame] | 728 | if (o && o->type == OBJ_COMMIT) { |
| 729 | o->flags &= ~TMP_MARK; |
| 730 | } |
| 731 | } |
| 732 | for (i = get_max_object_index(); 0 < i; i--) { |
| 733 | o = get_indexed_object(i - 1); |
| 734 | if (o && o->type == OBJ_COMMIT && |
| 735 | (o->flags & TMP_MARK)) { |
| 736 | add_object_array(o, NULL, reachable); |
| 737 | o->flags &= ~TMP_MARK; |
| 738 | } |
| 739 | } |
| 740 | close(cmd.out); |
| 741 | |
| 742 | if (finish_command(&cmd)) |
| 743 | return -1; |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 748 | static int has_unreachable(struct object_array *src, enum allow_uor allow_uor) |
Nguyễn Thái Ngọc Duy | 2997178 | 2016-06-12 17:54:07 +0700 | [diff] [blame] | 749 | { |
| 750 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 751 | char buf[1]; |
| 752 | int i; |
| 753 | |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 754 | if (do_reachable_revlist(&cmd, src, NULL, allow_uor) < 0) |
Nguyễn Thái Ngọc Duy | 2997178 | 2016-06-12 17:54:07 +0700 | [diff] [blame] | 755 | return 1; |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 756 | |
| 757 | /* |
| 758 | * The commits out of the rev-list are not ancestors of |
| 759 | * our ref. |
| 760 | */ |
Nguyễn Thái Ngọc Duy | 2997178 | 2016-06-12 17:54:07 +0700 | [diff] [blame] | 761 | i = read_in_full(cmd.out, buf, 1); |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 762 | if (i) |
| 763 | goto error; |
| 764 | close(cmd.out); |
Nguyễn Thái Ngọc Duy | 7fcbd37 | 2016-06-12 17:53:51 +0700 | [diff] [blame] | 765 | cmd.out = -1; |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 766 | |
| 767 | /* |
| 768 | * rev-list may have died by encountering a bad commit |
| 769 | * in the history, in which case we do want to bail out |
| 770 | * even when it showed no commit. |
| 771 | */ |
| 772 | if (finish_command(&cmd)) |
| 773 | goto error; |
| 774 | |
| 775 | /* All the non-tip ones are ancestors of what we advertised */ |
Nguyễn Thái Ngọc Duy | 3f0f662 | 2016-06-12 17:53:52 +0700 | [diff] [blame] | 776 | return 0; |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 777 | |
| 778 | error: |
Nguyễn Thái Ngọc Duy | 7fcbd37 | 2016-06-12 17:53:51 +0700 | [diff] [blame] | 779 | if (cmd.out >= 0) |
| 780 | close(cmd.out); |
Nguyễn Thái Ngọc Duy | 3f0f662 | 2016-06-12 17:53:52 +0700 | [diff] [blame] | 781 | return 1; |
| 782 | } |
Nguyễn Thái Ngọc Duy | 7fcbd37 | 2016-06-12 17:53:51 +0700 | [diff] [blame] | 783 | |
Christian Couder | b08c974 | 2020-05-15 12:04:51 +0200 | [diff] [blame] | 784 | static void check_non_tip(struct upload_pack_data *data) |
Nguyễn Thái Ngọc Duy | 3f0f662 | 2016-06-12 17:53:52 +0700 | [diff] [blame] | 785 | { |
| 786 | int i; |
| 787 | |
| 788 | /* |
| 789 | * In the normal in-process case without |
| 790 | * uploadpack.allowReachableSHA1InWant, |
| 791 | * non-tip requests can never happen. |
| 792 | */ |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 793 | if (!data->stateless_rpc && !(data->allow_uor & ALLOW_REACHABLE_SHA1)) |
Nguyễn Thái Ngọc Duy | 3f0f662 | 2016-06-12 17:53:52 +0700 | [diff] [blame] | 794 | goto error; |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 795 | if (!has_unreachable(&data->want_obj, data->allow_uor)) |
Nguyễn Thái Ngọc Duy | 3f0f662 | 2016-06-12 17:53:52 +0700 | [diff] [blame] | 796 | /* All the non-tip ones are ancestors of what we advertised */ |
| 797 | return; |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 798 | |
| 799 | error: |
| 800 | /* Pick one of them (we know there at least is one) */ |
Christian Couder | b08c974 | 2020-05-15 12:04:51 +0200 | [diff] [blame] | 801 | for (i = 0; i < data->want_obj.nr; i++) { |
| 802 | struct object *o = data->want_obj.objects[i].item; |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 803 | if (!is_our_ref(o, data->allow_uor)) { |
Derrick Stolee | 7ba7c52 | 2023-08-10 14:40:50 +0000 | [diff] [blame] | 804 | error("git upload-pack: not our ref %s", |
| 805 | oid_to_hex(&o->oid)); |
Christian Couder | b08c974 | 2020-05-15 12:04:51 +0200 | [diff] [blame] | 806 | packet_writer_error(&data->writer, |
Jeff King | 014ade7 | 2019-04-13 01:53:34 -0400 | [diff] [blame] | 807 | "upload-pack: not our ref %s", |
| 808 | oid_to_hex(&o->oid)); |
Patrick Steinhardt | 5f33a84 | 2023-08-16 08:06:59 +0200 | [diff] [blame] | 809 | exit(128); |
Jeff King | 014ade7 | 2019-04-13 01:53:34 -0400 | [diff] [blame] | 810 | } |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 814 | static void send_shallow(struct upload_pack_data *data, |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 815 | struct commit_list *result) |
Nguyễn Thái Ngọc Duy | 5c24cde | 2016-06-12 17:53:46 +0700 | [diff] [blame] | 816 | { |
| 817 | while (result) { |
| 818 | struct object *object = &result->item->object; |
| 819 | if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) { |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 820 | packet_writer_write(&data->writer, "shallow %s", |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 821 | oid_to_hex(&object->oid)); |
Stefan Beller | 19143f1 | 2018-05-17 15:51:44 -0700 | [diff] [blame] | 822 | register_shallow(the_repository, &object->oid); |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 823 | data->shallow_nr++; |
Nguyễn Thái Ngọc Duy | 5c24cde | 2016-06-12 17:53:46 +0700 | [diff] [blame] | 824 | } |
| 825 | result = result->next; |
| 826 | } |
| 827 | } |
| 828 | |
Christian Couder | 329f996 | 2020-06-11 14:05:08 +0200 | [diff] [blame] | 829 | static void send_unshallow(struct upload_pack_data *data) |
Nguyễn Thái Ngọc Duy | e8e44de | 2016-06-12 17:53:45 +0700 | [diff] [blame] | 830 | { |
Nguyễn Thái Ngọc Duy | e8e44de | 2016-06-12 17:53:45 +0700 | [diff] [blame] | 831 | int i; |
Nguyễn Thái Ngọc Duy | 873700c | 2016-06-12 17:53:48 +0700 | [diff] [blame] | 832 | |
Christian Couder | 329f996 | 2020-06-11 14:05:08 +0200 | [diff] [blame] | 833 | for (i = 0; i < data->shallows.nr; i++) { |
| 834 | struct object *object = data->shallows.objects[i].item; |
Nguyễn Thái Ngọc Duy | e8e44de | 2016-06-12 17:53:45 +0700 | [diff] [blame] | 835 | if (object->flags & NOT_SHALLOW) { |
| 836 | struct commit_list *parents; |
Christian Couder | 329f996 | 2020-06-11 14:05:08 +0200 | [diff] [blame] | 837 | packet_writer_write(&data->writer, "unshallow %s", |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 838 | oid_to_hex(&object->oid)); |
Nguyễn Thái Ngọc Duy | e8e44de | 2016-06-12 17:53:45 +0700 | [diff] [blame] | 839 | object->flags &= ~CLIENT_SHALLOW; |
Nguyễn Thái Ngọc Duy | 873700c | 2016-06-12 17:53:48 +0700 | [diff] [blame] | 840 | /* |
| 841 | * We want to _register_ "object" as shallow, but we |
| 842 | * also need to traverse object's parents to deepen a |
| 843 | * shallow clone. Unregister it for now so we can |
| 844 | * parse and add the parents to the want list, then |
| 845 | * re-register it. |
| 846 | */ |
brian m. carlson | e92b848 | 2017-05-06 22:10:06 +0000 | [diff] [blame] | 847 | unregister_shallow(&object->oid); |
Nguyễn Thái Ngọc Duy | e8e44de | 2016-06-12 17:53:45 +0700 | [diff] [blame] | 848 | object->parsed = 0; |
| 849 | parse_commit_or_die((struct commit *)object); |
| 850 | parents = ((struct commit *)object)->parents; |
| 851 | while (parents) { |
| 852 | add_object_array(&parents->item->object, |
Christian Couder | 329f996 | 2020-06-11 14:05:08 +0200 | [diff] [blame] | 853 | NULL, &data->want_obj); |
Nguyễn Thái Ngọc Duy | e8e44de | 2016-06-12 17:53:45 +0700 | [diff] [blame] | 854 | parents = parents->next; |
| 855 | } |
Christian Couder | de0e9f7 | 2020-06-11 14:05:10 +0200 | [diff] [blame] | 856 | add_object_array(object, NULL, &data->extra_edge_obj); |
Nguyễn Thái Ngọc Duy | e8e44de | 2016-06-12 17:53:45 +0700 | [diff] [blame] | 857 | } |
| 858 | /* make sure commit traversal conforms to client */ |
Stefan Beller | 19143f1 | 2018-05-17 15:51:44 -0700 | [diff] [blame] | 859 | register_shallow(the_repository, &object->oid); |
Nguyễn Thái Ngọc Duy | e8e44de | 2016-06-12 17:53:45 +0700 | [diff] [blame] | 860 | } |
Nguyễn Thái Ngọc Duy | 873700c | 2016-06-12 17:53:48 +0700 | [diff] [blame] | 861 | } |
| 862 | |
Jonathan Tan | 5056cf4 | 2018-12-18 13:24:35 -0800 | [diff] [blame] | 863 | static int check_ref(const char *refname_full, const struct object_id *oid, |
| 864 | int flag, void *cb_data); |
Christian Couder | b1492f2 | 2020-06-11 14:05:06 +0200 | [diff] [blame] | 865 | static void deepen(struct upload_pack_data *data, int depth) |
Nguyễn Thái Ngọc Duy | 873700c | 2016-06-12 17:53:48 +0700 | [diff] [blame] | 866 | { |
Stefan Beller | c881348 | 2018-05-17 15:51:46 -0700 | [diff] [blame] | 867 | if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) { |
Nguyễn Thái Ngọc Duy | 873700c | 2016-06-12 17:53:48 +0700 | [diff] [blame] | 868 | int i; |
| 869 | |
Christian Couder | b1492f2 | 2020-06-11 14:05:06 +0200 | [diff] [blame] | 870 | for (i = 0; i < data->shallows.nr; i++) { |
| 871 | struct object *object = data->shallows.objects[i].item; |
Nguyễn Thái Ngọc Duy | 873700c | 2016-06-12 17:53:48 +0700 | [diff] [blame] | 872 | object->flags |= NOT_SHALLOW; |
| 873 | } |
Christian Couder | b1492f2 | 2020-06-11 14:05:06 +0200 | [diff] [blame] | 874 | } else if (data->deepen_relative) { |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 875 | struct object_array reachable_shallows = OBJECT_ARRAY_INIT; |
| 876 | struct commit_list *result; |
| 877 | |
Jonathan Tan | 5056cf4 | 2018-12-18 13:24:35 -0800 | [diff] [blame] | 878 | /* |
| 879 | * Checking for reachable shallows requires that our refs be |
| 880 | * marked with OUR_REF. |
| 881 | */ |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 882 | head_ref_namespaced(check_ref, data); |
Taylor Blau | 18b6b1b | 2023-07-10 17:12:45 -0400 | [diff] [blame] | 883 | for_each_namespaced_ref_1(check_ref, data); |
Jonathan Tan | 5056cf4 | 2018-12-18 13:24:35 -0800 | [diff] [blame] | 884 | |
Christian Couder | f1514c6 | 2020-06-11 14:05:11 +0200 | [diff] [blame] | 885 | get_reachable_list(data, &reachable_shallows); |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 886 | result = get_shallow_commits(&reachable_shallows, |
| 887 | depth + 1, |
| 888 | SHALLOW, NOT_SHALLOW); |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 889 | send_shallow(data, result); |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 890 | free_commit_list(result); |
| 891 | object_array_clear(&reachable_shallows); |
Nguyễn Thái Ngọc Duy | 873700c | 2016-06-12 17:53:48 +0700 | [diff] [blame] | 892 | } else { |
| 893 | struct commit_list *result; |
| 894 | |
Christian Couder | b1492f2 | 2020-06-11 14:05:06 +0200 | [diff] [blame] | 895 | result = get_shallow_commits(&data->want_obj, depth, |
Nguyễn Thái Ngọc Duy | 873700c | 2016-06-12 17:53:48 +0700 | [diff] [blame] | 896 | SHALLOW, NOT_SHALLOW); |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 897 | send_shallow(data, result); |
Nguyễn Thái Ngọc Duy | 873700c | 2016-06-12 17:53:48 +0700 | [diff] [blame] | 898 | free_commit_list(result); |
| 899 | } |
| 900 | |
Christian Couder | 329f996 | 2020-06-11 14:05:08 +0200 | [diff] [blame] | 901 | send_unshallow(data); |
Nguyễn Thái Ngọc Duy | e8e44de | 2016-06-12 17:53:45 +0700 | [diff] [blame] | 902 | } |
| 903 | |
Christian Couder | 446e42c | 2020-06-11 14:05:07 +0200 | [diff] [blame] | 904 | static void deepen_by_rev_list(struct upload_pack_data *data, |
| 905 | int ac, |
| 906 | const char **av) |
Nguyễn Thái Ngọc Duy | 569e554 | 2016-06-12 17:53:58 +0700 | [diff] [blame] | 907 | { |
| 908 | struct commit_list *result; |
| 909 | |
Jeff King | 6abada1 | 2019-09-12 10:44:45 -0400 | [diff] [blame] | 910 | disable_commit_graph(the_repository); |
Nguyễn Thái Ngọc Duy | 569e554 | 2016-06-12 17:53:58 +0700 | [diff] [blame] | 911 | result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW); |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 912 | send_shallow(data, result); |
Nguyễn Thái Ngọc Duy | 569e554 | 2016-06-12 17:53:58 +0700 | [diff] [blame] | 913 | free_commit_list(result); |
Christian Couder | 329f996 | 2020-06-11 14:05:08 +0200 | [diff] [blame] | 914 | send_unshallow(data); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /* Returns 1 if a shallow list is sent or 0 otherwise */ |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 918 | static int send_shallow_list(struct upload_pack_data *data) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 919 | { |
| 920 | int ret = 0; |
| 921 | |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 922 | if (data->depth > 0 && data->deepen_rev_list) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 923 | die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together"); |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 924 | if (data->depth > 0) { |
Christian Couder | b1492f2 | 2020-06-11 14:05:06 +0200 | [diff] [blame] | 925 | deepen(data, data->depth); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 926 | ret = 1; |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 927 | } else if (data->deepen_rev_list) { |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 928 | struct strvec av = STRVEC_INIT; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 929 | int i; |
| 930 | |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 931 | strvec_push(&av, "rev-list"); |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 932 | if (data->deepen_since) |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 933 | strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since); |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 934 | if (data->deepen_not.nr) { |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 935 | strvec_push(&av, "--not"); |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 936 | for (i = 0; i < data->deepen_not.nr; i++) { |
| 937 | struct string_list_item *s = data->deepen_not.items + i; |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 938 | strvec_push(&av, s->string); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 939 | } |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 940 | strvec_push(&av, "--not"); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 941 | } |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 942 | for (i = 0; i < data->want_obj.nr; i++) { |
| 943 | struct object *o = data->want_obj.objects[i].item; |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 944 | strvec_push(&av, oid_to_hex(&o->oid)); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 945 | } |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 946 | deepen_by_rev_list(data, av.nr, av.v); |
Jeff King | c972bf4 | 2020-07-28 16:25:12 -0400 | [diff] [blame] | 947 | strvec_clear(&av); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 948 | ret = 1; |
| 949 | } else { |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 950 | if (data->shallows.nr > 0) { |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 951 | int i; |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 952 | for (i = 0; i < data->shallows.nr; i++) |
Junio C Hamano | 00624d6 | 2018-07-18 12:20:27 -0700 | [diff] [blame] | 953 | register_shallow(the_repository, |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 954 | &data->shallows.objects[i].item->oid); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 958 | data->shallow_nr += data->shallows.nr; |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 959 | return ret; |
Nguyễn Thái Ngọc Duy | 569e554 | 2016-06-12 17:53:58 +0700 | [diff] [blame] | 960 | } |
| 961 | |
Brandon Williams | ae2948f | 2018-03-14 11:31:42 -0700 | [diff] [blame] | 962 | static int process_shallow(const char *line, struct object_array *shallows) |
| 963 | { |
| 964 | const char *arg; |
| 965 | if (skip_prefix(line, "shallow ", &arg)) { |
| 966 | struct object_id oid; |
| 967 | struct object *object; |
| 968 | if (get_oid_hex(arg, &oid)) |
| 969 | die("invalid shallow line: %s", line); |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 970 | object = parse_object(the_repository, &oid); |
Brandon Williams | ae2948f | 2018-03-14 11:31:42 -0700 | [diff] [blame] | 971 | if (!object) |
| 972 | return 1; |
| 973 | if (object->type != OBJ_COMMIT) |
| 974 | die("invalid shallow object %s", oid_to_hex(&oid)); |
| 975 | if (!(object->flags & CLIENT_SHALLOW)) { |
| 976 | object->flags |= CLIENT_SHALLOW; |
| 977 | add_object_array(object, NULL, shallows); |
| 978 | } |
| 979 | return 1; |
| 980 | } |
| 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | static int process_deepen(const char *line, int *depth) |
| 986 | { |
| 987 | const char *arg; |
| 988 | if (skip_prefix(line, "deepen ", &arg)) { |
| 989 | char *end = NULL; |
| 990 | *depth = (int)strtol(arg, &end, 0); |
| 991 | if (!end || *end || *depth <= 0) |
| 992 | die("Invalid deepen: %s", line); |
| 993 | return 1; |
| 994 | } |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list) |
| 1000 | { |
| 1001 | const char *arg; |
| 1002 | if (skip_prefix(line, "deepen-since ", &arg)) { |
| 1003 | char *end = NULL; |
| 1004 | *deepen_since = parse_timestamp(arg, &end, 0); |
| 1005 | if (!end || *end || !deepen_since || |
| 1006 | /* revisions.c's max_age -1 is special */ |
| 1007 | *deepen_since == -1) |
| 1008 | die("Invalid deepen-since: %s", line); |
| 1009 | *deepen_rev_list = 1; |
| 1010 | return 1; |
| 1011 | } |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list) |
| 1016 | { |
| 1017 | const char *arg; |
| 1018 | if (skip_prefix(line, "deepen-not ", &arg)) { |
| 1019 | char *ref = NULL; |
| 1020 | struct object_id oid; |
Nguyễn Thái Ngọc Duy | 0b1dbf5 | 2019-04-06 18:34:27 +0700 | [diff] [blame] | 1021 | if (expand_ref(the_repository, arg, strlen(arg), &oid, &ref) != 1) |
Brandon Williams | ae2948f | 2018-03-14 11:31:42 -0700 | [diff] [blame] | 1022 | die("git upload-pack: ambiguous deepen-not: %s", line); |
| 1023 | string_list_append(deepen_not, ref); |
| 1024 | free(ref); |
| 1025 | *deepen_rev_list = 1; |
| 1026 | return 1; |
| 1027 | } |
| 1028 | return 0; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 1031 | NORETURN __attribute__((format(printf,2,3))) |
| 1032 | static void send_err_and_die(struct upload_pack_data *data, |
| 1033 | const char *fmt, ...) |
| 1034 | { |
| 1035 | struct strbuf buf = STRBUF_INIT; |
| 1036 | va_list ap; |
| 1037 | |
| 1038 | va_start(ap, fmt); |
| 1039 | strbuf_vaddf(&buf, fmt, ap); |
| 1040 | va_end(ap); |
| 1041 | |
| 1042 | packet_writer_error(&data->writer, "%s", buf.buf); |
| 1043 | die("%s", buf.buf); |
| 1044 | } |
| 1045 | |
| 1046 | static void check_one_filter(struct upload_pack_data *data, |
| 1047 | struct list_objects_filter_options *opts) |
| 1048 | { |
| 1049 | const char *key = list_object_filter_config_name(opts->choice); |
| 1050 | struct string_list_item *item = string_list_lookup(&data->allowed_filters, |
| 1051 | key); |
| 1052 | int allowed; |
| 1053 | |
| 1054 | if (item) |
| 1055 | allowed = (intptr_t)item->util; |
| 1056 | else |
| 1057 | allowed = data->allow_filter_fallback; |
| 1058 | |
| 1059 | if (!allowed) |
| 1060 | send_err_and_die(data, "filter '%s' not supported", key); |
Taylor Blau | 5b01a4e | 2020-08-03 14:00:17 -0400 | [diff] [blame] | 1061 | |
| 1062 | if (opts->choice == LOFC_TREE_DEPTH && |
| 1063 | opts->tree_exclude_depth > data->tree_filter_max_depth) |
| 1064 | send_err_and_die(data, |
| 1065 | "tree filter allows max depth %lu, but got %lu", |
| 1066 | data->tree_filter_max_depth, |
| 1067 | opts->tree_exclude_depth); |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static void check_filter_recurse(struct upload_pack_data *data, |
| 1071 | struct list_objects_filter_options *opts) |
| 1072 | { |
| 1073 | size_t i; |
| 1074 | |
| 1075 | check_one_filter(data, opts); |
| 1076 | if (opts->choice != LOFC_COMBINE) |
| 1077 | return; |
| 1078 | |
| 1079 | for (i = 0; i < opts->sub_nr; i++) |
| 1080 | check_filter_recurse(data, &opts->sub[i]); |
| 1081 | } |
| 1082 | |
| 1083 | static void die_if_using_banned_filter(struct upload_pack_data *data) |
| 1084 | { |
| 1085 | check_filter_recurse(data, &data->filter_options); |
| 1086 | } |
| 1087 | |
Christian Couder | d92ae2c | 2020-05-15 12:04:47 +0200 | [diff] [blame] | 1088 | static void receive_needs(struct upload_pack_data *data, |
| 1089 | struct packet_reader *reader) |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 1090 | { |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 1091 | int has_non_tip = 0; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 1092 | |
Christian Couder | 35b43a1 | 2020-06-11 14:05:09 +0200 | [diff] [blame] | 1093 | data->shallow_nr = 0; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 1094 | for (;;) { |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 1095 | struct object *o; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1096 | const char *features; |
brian m. carlson | cf93982 | 2017-05-06 22:10:28 +0000 | [diff] [blame] | 1097 | struct object_id oid_buf; |
Nguyễn Thái Ngọc Duy | 8bf3b75 | 2016-06-12 17:53:49 +0700 | [diff] [blame] | 1098 | const char *arg; |
Jeff King | 7ce4c8f | 2023-04-14 17:25:20 -0400 | [diff] [blame] | 1099 | size_t feature_len; |
Nguyễn Thái Ngọc Duy | 8bf3b75 | 2016-06-12 17:53:49 +0700 | [diff] [blame] | 1100 | |
Christian Couder | d40f04e | 2020-06-04 19:54:40 +0200 | [diff] [blame] | 1101 | reset_timeout(data->timeout); |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 1102 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 1103 | break; |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 1104 | |
Christian Couder | 7a51676 | 2020-05-15 12:04:54 +0200 | [diff] [blame] | 1105 | if (process_shallow(reader->line, &data->shallows)) |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 1106 | continue; |
Christian Couder | 7a51676 | 2020-05-15 12:04:54 +0200 | [diff] [blame] | 1107 | if (process_deepen(reader->line, &data->depth)) |
Johannes Schindelin | 016e6cc | 2006-10-30 20:09:29 +0100 | [diff] [blame] | 1108 | continue; |
Christian Couder | 7a51676 | 2020-05-15 12:04:54 +0200 | [diff] [blame] | 1109 | if (process_deepen_since(reader->line, &data->deepen_since, &data->deepen_rev_list)) |
Nguyễn Thái Ngọc Duy | 569e554 | 2016-06-12 17:53:58 +0700 | [diff] [blame] | 1110 | continue; |
Christian Couder | 7a51676 | 2020-05-15 12:04:54 +0200 | [diff] [blame] | 1111 | if (process_deepen_not(reader->line, &data->deepen_not, &data->deepen_rev_list)) |
Nguyễn Thái Ngọc Duy | 269a7a8 | 2016-06-12 17:54:03 +0700 | [diff] [blame] | 1112 | continue; |
Brandon Williams | ae2948f | 2018-03-14 11:31:42 -0700 | [diff] [blame] | 1113 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 1114 | if (skip_prefix(reader->line, "filter ", &arg)) { |
Christian Couder | 59a9026 | 2020-06-04 19:54:42 +0200 | [diff] [blame] | 1115 | if (!data->filter_capability_requested) |
Jeff Hostetler | 10ac85c | 2017-12-08 15:58:39 +0000 | [diff] [blame] | 1116 | die("git upload-pack: filtering capability not negotiated"); |
Christian Couder | d92ae2c | 2020-05-15 12:04:47 +0200 | [diff] [blame] | 1117 | list_objects_filter_die_if_populated(&data->filter_options); |
| 1118 | parse_list_objects_filter(&data->filter_options, arg); |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 1119 | die_if_using_banned_filter(data); |
Jeff Hostetler | 10ac85c | 2017-12-08 15:58:39 +0000 | [diff] [blame] | 1120 | continue; |
| 1121 | } |
Junio C Hamano | 9bfa0f9 | 2018-05-08 15:59:15 +0900 | [diff] [blame] | 1122 | |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 1123 | if (!skip_prefix(reader->line, "want ", &arg) || |
brian m. carlson | 55dc227 | 2018-05-02 00:25:51 +0000 | [diff] [blame] | 1124 | parse_oid_hex(arg, &oid_buf, &features)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 1125 | die("git upload-pack: protocol error, " |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 1126 | "expected to get object ID, not '%s'", reader->line); |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1127 | |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 1128 | if (parse_feature_request(features, "deepen-relative")) |
Christian Couder | 7a51676 | 2020-05-15 12:04:54 +0200 | [diff] [blame] | 1129 | data->deepen_relative = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1130 | if (parse_feature_request(features, "multi_ack_detailed")) |
Christian Couder | e9d882b | 2020-06-04 19:54:44 +0200 | [diff] [blame] | 1131 | data->multi_ack = MULTI_ACK_DETAILED; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1132 | else if (parse_feature_request(features, "multi_ack")) |
Christian Couder | e9d882b | 2020-06-04 19:54:44 +0200 | [diff] [blame] | 1133 | data->multi_ack = MULTI_ACK; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1134 | if (parse_feature_request(features, "no-done")) |
Christian Couder | d40f04e | 2020-06-04 19:54:40 +0200 | [diff] [blame] | 1135 | data->no_done = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1136 | if (parse_feature_request(features, "thin-pack")) |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 1137 | data->use_thin_pack = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1138 | if (parse_feature_request(features, "ofs-delta")) |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 1139 | data->use_ofs_delta = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1140 | if (parse_feature_request(features, "side-band-64k")) |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 1141 | data->use_sideband = LARGE_PACKET_MAX; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1142 | else if (parse_feature_request(features, "side-band")) |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 1143 | data->use_sideband = DEFAULT_PACKET_MAX; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1144 | if (parse_feature_request(features, "no-progress")) |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 1145 | data->no_progress = 1; |
Junio C Hamano | f47182c | 2012-01-08 22:06:19 +0100 | [diff] [blame] | 1146 | if (parse_feature_request(features, "include-tag")) |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 1147 | data->use_include_tag = 1; |
Christian Couder | 59abe19 | 2020-06-04 19:54:47 +0200 | [diff] [blame] | 1148 | if (data->allow_filter && |
| 1149 | parse_feature_request(features, "filter")) |
Christian Couder | 59a9026 | 2020-06-04 19:54:42 +0200 | [diff] [blame] | 1150 | data->filter_capability_requested = 1; |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 1151 | |
Josh Steadmon | 8295946 | 2020-11-11 15:29:32 -0800 | [diff] [blame] | 1152 | arg = parse_feature_value(features, "session-id", &feature_len, NULL); |
| 1153 | if (arg) { |
| 1154 | char *client_sid = xstrndup(arg, feature_len); |
| 1155 | trace2_data_string("transfer", NULL, "client-sid", client_sid); |
| 1156 | free(client_sid); |
| 1157 | } |
| 1158 | |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 1159 | o = parse_object(the_repository, &oid_buf); |
Jonathan Tan | bdb31ea | 2017-02-23 10:43:03 -0800 | [diff] [blame] | 1160 | if (!o) { |
Christian Couder | 4ace028 | 2020-05-15 12:04:48 +0200 | [diff] [blame] | 1161 | packet_writer_error(&data->writer, |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1162 | "upload-pack: not our ref %s", |
| 1163 | oid_to_hex(&oid_buf)); |
Elijah Newren | 9f9aa76 | 2010-07-31 14:11:46 -0600 | [diff] [blame] | 1164 | die("git upload-pack: not our ref %s", |
brian m. carlson | cf93982 | 2017-05-06 22:10:28 +0000 | [diff] [blame] | 1165 | oid_to_hex(&oid_buf)); |
Jonathan Tan | bdb31ea | 2017-02-23 10:43:03 -0800 | [diff] [blame] | 1166 | } |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 1167 | if (!(o->flags & WANTED)) { |
| 1168 | o->flags |= WANTED; |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 1169 | if (!((data->allow_uor & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1 |
| 1170 | || is_our_ref(o, data->allow_uor))) |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 1171 | has_non_tip = 1; |
Christian Couder | d92ae2c | 2020-05-15 12:04:47 +0200 | [diff] [blame] | 1172 | add_object_array(o, NULL, &data->want_obj); |
Junio C Hamano | 565ebbf | 2005-10-24 18:59:18 -0700 | [diff] [blame] | 1173 | } |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 1174 | } |
Johannes Sixt | 9462e3f | 2009-06-16 20:41:16 +0200 | [diff] [blame] | 1175 | |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 1176 | /* |
| 1177 | * We have sent all our refs already, and the other end |
| 1178 | * should have chosen out of them. When we are operating |
| 1179 | * in the stateless RPC mode, however, their choice may |
| 1180 | * have been based on the set of older refs advertised |
| 1181 | * by another process that handled the initial request. |
| 1182 | */ |
| 1183 | if (has_non_tip) |
Christian Couder | b08c974 | 2020-05-15 12:04:51 +0200 | [diff] [blame] | 1184 | check_non_tip(data); |
Junio C Hamano | 051e400 | 2011-08-05 13:54:06 -0700 | [diff] [blame] | 1185 | |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 1186 | if (!data->use_sideband && data->daemon_mode) |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 1187 | data->no_progress = 1; |
Johannes Sixt | 9462e3f | 2009-06-16 20:41:16 +0200 | [diff] [blame] | 1188 | |
Christian Couder | 7a51676 | 2020-05-15 12:04:54 +0200 | [diff] [blame] | 1189 | if (data->depth == 0 && !data->deepen_rev_list && data->shallows.nr == 0) |
Johannes Schindelin | f53514b | 2006-10-30 20:09:53 +0100 | [diff] [blame] | 1190 | return; |
Nguyễn Thái Ngọc Duy | 569e554 | 2016-06-12 17:53:58 +0700 | [diff] [blame] | 1191 | |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 1192 | if (send_shallow_list(data)) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1193 | packet_flush(1); |
Linus Torvalds | fb9040c | 2005-07-04 15:29:17 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 1196 | /* return non-zero if the ref is hidden, otherwise 0 */ |
Lukas Fleischer | 78a766a | 2015-11-03 08:58:16 +0100 | [diff] [blame] | 1197 | static int mark_our_ref(const char *refname, const char *refname_full, |
Taylor Blau | c45841f | 2023-07-10 17:12:33 -0400 | [diff] [blame] | 1198 | const struct object_id *oid, const struct strvec *hidden_refs) |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 1199 | { |
Jeff King | 45a187c | 2021-04-13 03:16:36 -0400 | [diff] [blame] | 1200 | struct object *o = lookup_unknown_object(the_repository, oid); |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 1201 | |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 1202 | if (ref_is_hidden(refname, refname_full, hidden_refs)) { |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 1203 | o->flags |= HIDDEN_REF; |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 1204 | return 1; |
Junio C Hamano | 390eb36 | 2013-01-28 21:49:57 -0800 | [diff] [blame] | 1205 | } |
Junio C Hamano | 3f1da57 | 2013-01-28 20:45:43 -0800 | [diff] [blame] | 1206 | o->flags |= OUR_REF; |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 1207 | return 0; |
| 1208 | } |
| 1209 | |
Lukas Fleischer | 78a766a | 2015-11-03 08:58:16 +0100 | [diff] [blame] | 1210 | static int check_ref(const char *refname_full, const struct object_id *oid, |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 1211 | int flag UNUSED, void *cb_data) |
Jeff King | e172755 | 2015-03-13 00:42:12 -0400 | [diff] [blame] | 1212 | { |
Lukas Fleischer | 78a766a | 2015-11-03 08:58:16 +0100 | [diff] [blame] | 1213 | const char *refname = strip_namespace(refname_full); |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 1214 | struct upload_pack_data *data = cb_data; |
Lukas Fleischer | 78a766a | 2015-11-03 08:58:16 +0100 | [diff] [blame] | 1215 | |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 1216 | mark_our_ref(refname, refname_full, oid, &data->hidden_refs); |
Jeff King | e172755 | 2015-03-13 00:42:12 -0400 | [diff] [blame] | 1217 | return 0; |
| 1218 | } |
| 1219 | |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1220 | static void format_symref_info(struct strbuf *buf, struct string_list *symref) |
| 1221 | { |
| 1222 | struct string_list_item *item; |
| 1223 | |
| 1224 | if (!symref->nr) |
| 1225 | return; |
| 1226 | for_each_string_list_item(item, symref) |
| 1227 | strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util); |
| 1228 | } |
| 1229 | |
Josh Steadmon | 791e1ad | 2020-11-11 15:29:27 -0800 | [diff] [blame] | 1230 | static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) { |
| 1231 | if (d->advertise_sid) |
| 1232 | strbuf_addf(buf, " session-id=%s", trace2_session_id()); |
| 1233 | } |
| 1234 | |
brian m. carlson | 933e3a4 | 2023-05-17 19:24:43 +0000 | [diff] [blame] | 1235 | static void write_v0_ref(struct upload_pack_data *data, |
| 1236 | const char *refname, const char *refname_nons, |
| 1237 | const struct object_id *oid) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 1238 | { |
Johannes Schindelin | ed09aef | 2006-10-30 20:09:06 +0100 | [diff] [blame] | 1239 | static const char *capabilities = "multi_ack thin-pack side-band" |
Nguyễn Thái Ngọc Duy | cccf74e | 2016-06-12 17:54:09 +0700 | [diff] [blame] | 1240 | " side-band-64k ofs-delta shallow deepen-since deepen-not" |
| 1241 | " deepen-relative no-progress include-tag multi_ack_detailed"; |
Michael Haggerty | 21758af | 2015-05-25 18:39:13 +0000 | [diff] [blame] | 1242 | struct object_id peeled; |
Carl Worth | b5b1699 | 2006-02-17 16:14:52 -0800 | [diff] [blame] | 1243 | |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 1244 | if (mark_our_ref(refname_nons, refname, oid, &data->hidden_refs)) |
brian m. carlson | 933e3a4 | 2023-05-17 19:24:43 +0000 | [diff] [blame] | 1245 | return; |
Junio C Hamano | cbbe50d | 2013-01-18 15:48:49 -0800 | [diff] [blame] | 1246 | |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1247 | if (capabilities) { |
| 1248 | struct strbuf symref_info = STRBUF_INIT; |
Josh Steadmon | 791e1ad | 2020-11-11 15:29:27 -0800 | [diff] [blame] | 1249 | struct strbuf session_id = STRBUF_INIT; |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1250 | |
Christian Couder | 762f927 | 2020-05-15 12:04:50 +0200 | [diff] [blame] | 1251 | format_symref_info(&symref_info, &data->symref); |
Josh Steadmon | 791e1ad | 2020-11-11 15:29:27 -0800 | [diff] [blame] | 1252 | format_session_id(&session_id, data); |
Jacob Vosmaer | 70afef5 | 2021-09-01 14:54:42 +0200 | [diff] [blame] | 1253 | packet_fwrite_fmt(stdout, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n", |
Michael Haggerty | 363e98b | 2015-05-25 18:39:12 +0000 | [diff] [blame] | 1254 | oid_to_hex(oid), refname_nons, |
Junio C Hamano | cf2ad8e | 2011-03-29 10:24:59 -0700 | [diff] [blame] | 1255 | 0, capabilities, |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 1256 | (data->allow_uor & ALLOW_TIP_SHA1) ? |
Fredrik Medley | 7199c09 | 2015-05-21 22:23:38 +0200 | [diff] [blame] | 1257 | " allow-tip-sha1-in-want" : "", |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 1258 | (data->allow_uor & ALLOW_REACHABLE_SHA1) ? |
Fredrik Medley | 68ee628 | 2015-05-21 22:23:39 +0200 | [diff] [blame] | 1259 | " allow-reachable-sha1-in-want" : "", |
Ævar Arnfjörð Bjarmason | f234da8 | 2021-08-05 03:25:42 +0200 | [diff] [blame] | 1260 | data->no_done ? " no-done" : "", |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1261 | symref_info.buf, |
Christian Couder | 59abe19 | 2020-06-04 19:54:47 +0200 | [diff] [blame] | 1262 | data->allow_filter ? " filter" : "", |
Josh Steadmon | 791e1ad | 2020-11-11 15:29:27 -0800 | [diff] [blame] | 1263 | session_id.buf, |
brian m. carlson | bf30dbf | 2020-05-25 19:58:51 +0000 | [diff] [blame] | 1264 | the_hash_algo->name, |
Jeff King | ff5effd | 2012-08-03 12:19:16 -0400 | [diff] [blame] | 1265 | git_user_agent_sanitized()); |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1266 | strbuf_release(&symref_info); |
Josh Steadmon | 791e1ad | 2020-11-11 15:29:27 -0800 | [diff] [blame] | 1267 | strbuf_release(&session_id); |
brian m. carlson | 933e3a4 | 2023-05-17 19:24:43 +0000 | [diff] [blame] | 1268 | data->sent_capabilities = 1; |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1269 | } else { |
Jacob Vosmaer | 70afef5 | 2021-09-01 14:54:42 +0200 | [diff] [blame] | 1270 | packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons); |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1271 | } |
Johannes Schindelin | 1f5881b | 2005-10-28 05:56:41 +0200 | [diff] [blame] | 1272 | capabilities = NULL; |
Jeff King | 36a3179 | 2021-01-20 14:44:43 -0500 | [diff] [blame] | 1273 | if (!peel_iterated_oid(oid, &peeled)) |
Jacob Vosmaer | 70afef5 | 2021-09-01 14:54:42 +0200 | [diff] [blame] | 1274 | packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons); |
brian m. carlson | 933e3a4 | 2023-05-17 19:24:43 +0000 | [diff] [blame] | 1275 | return; |
| 1276 | } |
| 1277 | |
| 1278 | static int send_ref(const char *refname, const struct object_id *oid, |
| 1279 | int flag UNUSED, void *cb_data) |
| 1280 | { |
| 1281 | write_v0_ref(cb_data, refname, strip_namespace(refname), oid); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
| 1284 | |
Jeff King | 63e14ee | 2022-08-19 06:08:32 -0400 | [diff] [blame] | 1285 | static int find_symref(const char *refname, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 1286 | const struct object_id *oid UNUSED, |
Michael Haggerty | 7dabd05 | 2015-05-25 18:39:10 +0000 | [diff] [blame] | 1287 | int flag, void *cb_data) |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1288 | { |
| 1289 | const char *symref_target; |
| 1290 | struct string_list_item *item; |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1291 | |
| 1292 | if ((flag & REF_ISSYMREF) == 0) |
| 1293 | return 0; |
René Scharfe | 744c040 | 2017-09-23 11:45:04 +0200 | [diff] [blame] | 1294 | symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag); |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1295 | if (!symref_target || (flag & REF_ISSYMREF) == 0) |
| 1296 | die("'%s' is a symref but it is not?", refname); |
Jeff King | 533e088 | 2019-05-23 02:11:21 -0400 | [diff] [blame] | 1297 | item = string_list_append(cb_data, strip_namespace(refname)); |
| 1298 | item->util = xstrdup(strip_namespace(symref_target)); |
Junio C Hamano | 7171d8c | 2013-09-17 16:17:33 -0700 | [diff] [blame] | 1299 | return 0; |
| 1300 | } |
| 1301 | |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 1302 | static int parse_object_filter_config(const char *var, const char *value, |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1303 | const struct key_value_info *kvi, |
| 1304 | struct upload_pack_data *data) |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 1305 | { |
| 1306 | struct strbuf buf = STRBUF_INIT; |
| 1307 | const char *sub, *key; |
| 1308 | size_t sub_len; |
| 1309 | |
| 1310 | if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key)) |
| 1311 | return 0; |
| 1312 | |
| 1313 | if (!sub) { |
| 1314 | if (!strcmp(key, "allow")) |
| 1315 | data->allow_filter_fallback = git_config_bool(var, value); |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
| 1319 | strbuf_add(&buf, sub, sub_len); |
| 1320 | |
| 1321 | if (!strcmp(key, "allow")) |
| 1322 | string_list_insert(&data->allowed_filters, buf.buf)->util = |
| 1323 | (void *)(intptr_t)git_config_bool(var, value); |
Taylor Blau | 5b01a4e | 2020-08-03 14:00:17 -0400 | [diff] [blame] | 1324 | else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) { |
| 1325 | if (!value) { |
| 1326 | strbuf_release(&buf); |
| 1327 | return config_error_nonbool(var); |
| 1328 | } |
| 1329 | string_list_insert(&data->allowed_filters, buf.buf)->util = |
| 1330 | (void *)(intptr_t)1; |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1331 | data->tree_filter_max_depth = git_config_ulong(var, value, |
| 1332 | kvi); |
Taylor Blau | 5b01a4e | 2020-08-03 14:00:17 -0400 | [diff] [blame] | 1333 | } |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 1334 | |
| 1335 | strbuf_release(&buf); |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1339 | static int upload_pack_config(const char *var, const char *value, |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1340 | const struct config_context *ctx, |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1341 | void *cb_data) |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 1342 | { |
Christian Couder | f203a88 | 2020-06-04 19:54:46 +0200 | [diff] [blame] | 1343 | struct upload_pack_data *data = cb_data; |
| 1344 | |
Fredrik Medley | 7199c09 | 2015-05-21 22:23:38 +0200 | [diff] [blame] | 1345 | if (!strcmp("uploadpack.allowtipsha1inwant", var)) { |
| 1346 | if (git_config_bool(var, value)) |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 1347 | data->allow_uor |= ALLOW_TIP_SHA1; |
Fredrik Medley | 7199c09 | 2015-05-21 22:23:38 +0200 | [diff] [blame] | 1348 | else |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 1349 | data->allow_uor &= ~ALLOW_TIP_SHA1; |
Fredrik Medley | 68ee628 | 2015-05-21 22:23:39 +0200 | [diff] [blame] | 1350 | } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) { |
| 1351 | if (git_config_bool(var, value)) |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 1352 | data->allow_uor |= ALLOW_REACHABLE_SHA1; |
Fredrik Medley | 68ee628 | 2015-05-21 22:23:39 +0200 | [diff] [blame] | 1353 | else |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 1354 | data->allow_uor &= ~ALLOW_REACHABLE_SHA1; |
David Turner | f8edeaa | 2016-11-11 12:23:48 -0500 | [diff] [blame] | 1355 | } else if (!strcmp("uploadpack.allowanysha1inwant", var)) { |
| 1356 | if (git_config_bool(var, value)) |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 1357 | data->allow_uor |= ALLOW_ANY_SHA1; |
David Turner | f8edeaa | 2016-11-11 12:23:48 -0500 | [diff] [blame] | 1358 | else |
Christian Couder | 629060d | 2020-06-11 14:05:12 +0200 | [diff] [blame] | 1359 | data->allow_uor &= ~ALLOW_ANY_SHA1; |
Fredrik Medley | 7199c09 | 2015-05-21 22:23:38 +0200 | [diff] [blame] | 1360 | } else if (!strcmp("uploadpack.keepalive", var)) { |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1361 | data->keepalive = git_config_int(var, value, ctx->kvi); |
Christian Couder | f203a88 | 2020-06-04 19:54:46 +0200 | [diff] [blame] | 1362 | if (!data->keepalive) |
| 1363 | data->keepalive = -1; |
Jeff Hostetler | 10ac85c | 2017-12-08 15:58:39 +0000 | [diff] [blame] | 1364 | } else if (!strcmp("uploadpack.allowfilter", var)) { |
Christian Couder | 59abe19 | 2020-06-04 19:54:47 +0200 | [diff] [blame] | 1365 | data->allow_filter = git_config_bool(var, value); |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1366 | } else if (!strcmp("uploadpack.allowrefinwant", var)) { |
Christian Couder | d1d7a94 | 2020-06-04 19:54:48 +0200 | [diff] [blame] | 1367 | data->allow_ref_in_want = git_config_bool(var, value); |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1368 | } else if (!strcmp("uploadpack.allowsidebandall", var)) { |
Christian Couder | e3835cd | 2020-06-04 19:54:49 +0200 | [diff] [blame] | 1369 | data->allow_sideband_all = git_config_bool(var, value); |
Elijah Newren | 8e712ef | 2019-04-25 07:58:54 -0700 | [diff] [blame] | 1370 | } else if (!strcmp("core.precomposeunicode", var)) { |
| 1371 | precomposed_unicode = git_config_bool(var, value); |
Josh Steadmon | 791e1ad | 2020-11-11 15:29:27 -0800 | [diff] [blame] | 1372 | } else if (!strcmp("transfer.advertisesid", var)) { |
| 1373 | data->advertise_sid = git_config_bool(var, value); |
Jeff King | 05e9515 | 2013-09-08 05:01:31 -0400 | [diff] [blame] | 1374 | } |
Jeff King | aaaa881 | 2018-10-24 03:27:52 -0400 | [diff] [blame] | 1375 | |
Glen Choo | 8868b1e | 2023-06-28 19:26:27 +0000 | [diff] [blame] | 1376 | if (parse_object_filter_config(var, value, ctx->kvi, data) < 0) |
Jeff King | d43a21b | 2020-12-03 03:09:42 -0500 | [diff] [blame] | 1377 | return -1; |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 1378 | |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 1379 | return parse_hide_refs_config(var, value, "uploadpack", &data->hidden_refs); |
Junio C Hamano | daebaa7 | 2013-01-18 16:08:30 -0800 | [diff] [blame] | 1380 | } |
| 1381 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 1382 | static int upload_pack_protected_config(const char *var, const char *value, |
| 1383 | const struct config_context *ctx UNUSED, |
| 1384 | void *cb_data) |
Glen Choo | 5b3c650 | 2022-07-14 21:27:59 +0000 | [diff] [blame] | 1385 | { |
| 1386 | struct upload_pack_data *data = cb_data; |
| 1387 | |
| 1388 | if (!strcmp("uploadpack.packobjectshook", var)) |
| 1389 | return git_config_string(&data->pack_objects_hook, var, value); |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | static void get_upload_pack_config(struct upload_pack_data *data) |
| 1394 | { |
| 1395 | git_config(upload_pack_config, data); |
| 1396 | git_protected_config(upload_pack_protected_config, data); |
| 1397 | } |
| 1398 | |
Ævar Arnfjörð Bjarmason | f234da8 | 2021-08-05 03:25:42 +0200 | [diff] [blame] | 1399 | void upload_pack(const int advertise_refs, const int stateless_rpc, |
| 1400 | const int timeout) |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 1401 | { |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 1402 | struct packet_reader reader; |
Christian Couder | ebf8ebc | 2020-05-15 12:04:45 +0200 | [diff] [blame] | 1403 | struct upload_pack_data data; |
H. Peter Anvin | 960decc | 2005-10-19 14:27:01 -0700 | [diff] [blame] | 1404 | |
Christian Couder | ebf8ebc | 2020-05-15 12:04:45 +0200 | [diff] [blame] | 1405 | upload_pack_data_init(&data); |
Glen Choo | 5b3c650 | 2022-07-14 21:27:59 +0000 | [diff] [blame] | 1406 | get_upload_pack_config(&data); |
Christian Couder | 8a0e6f1 | 2020-06-04 19:54:45 +0200 | [diff] [blame] | 1407 | |
Ævar Arnfjörð Bjarmason | f234da8 | 2021-08-05 03:25:42 +0200 | [diff] [blame] | 1408 | data.stateless_rpc = stateless_rpc; |
| 1409 | data.timeout = timeout; |
| 1410 | if (data.timeout) |
| 1411 | data.daemon_mode = 1; |
Christian Couder | df654ab | 2020-05-15 12:04:52 +0200 | [diff] [blame] | 1412 | |
Christian Couder | 438528f | 2020-05-15 12:04:49 +0200 | [diff] [blame] | 1413 | head_ref_namespaced(find_symref, &data.symref); |
Brandon Williams | aa9bab2 | 2017-10-16 10:55:26 -0700 | [diff] [blame] | 1414 | |
Ævar Arnfjörð Bjarmason | f234da8 | 2021-08-05 03:25:42 +0200 | [diff] [blame] | 1415 | if (advertise_refs || !data.stateless_rpc) { |
Christian Couder | d40f04e | 2020-06-04 19:54:40 +0200 | [diff] [blame] | 1416 | reset_timeout(data.timeout); |
Ævar Arnfjörð Bjarmason | f234da8 | 2021-08-05 03:25:42 +0200 | [diff] [blame] | 1417 | if (advertise_refs) |
| 1418 | data.no_done = 1; |
Christian Couder | 762f927 | 2020-05-15 12:04:50 +0200 | [diff] [blame] | 1419 | head_ref_namespaced(send_ref, &data); |
Taylor Blau | 18b6b1b | 2023-07-10 17:12:45 -0400 | [diff] [blame] | 1420 | for_each_namespaced_ref_1(send_ref, &data); |
brian m. carlson | 933e3a4 | 2023-05-17 19:24:43 +0000 | [diff] [blame] | 1421 | if (!data.sent_capabilities) { |
| 1422 | const char *refname = "capabilities^{}"; |
| 1423 | write_v0_ref(&data, refname, refname, null_oid()); |
| 1424 | } |
Jacob Vosmaer | 70afef5 | 2021-09-01 14:54:42 +0200 | [diff] [blame] | 1425 | /* |
| 1426 | * fflush stdout before calling advertise_shallow_grafts because send_ref |
| 1427 | * uses stdio. |
| 1428 | */ |
| 1429 | fflush_or_die(stdout); |
Brandon Williams | a3d6b53 | 2018-03-14 11:31:41 -0700 | [diff] [blame] | 1430 | advertise_shallow_grafts(1); |
| 1431 | packet_flush(1); |
| 1432 | } else { |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 1433 | head_ref_namespaced(check_ref, &data); |
Taylor Blau | 18b6b1b | 2023-07-10 17:12:45 -0400 | [diff] [blame] | 1434 | for_each_namespaced_ref_1(check_ref, &data); |
Brandon Williams | aa9bab2 | 2017-10-16 10:55:26 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
Ævar Arnfjörð Bjarmason | f234da8 | 2021-08-05 03:25:42 +0200 | [diff] [blame] | 1437 | if (!advertise_refs) { |
Christian Couder | ebf8ebc | 2020-05-15 12:04:45 +0200 | [diff] [blame] | 1438 | packet_reader_init(&reader, 0, NULL, 0, |
| 1439 | PACKET_READ_CHOMP_NEWLINE | |
| 1440 | PACKET_READ_DIE_ON_ERR_PACKET); |
Masaya Suzuki | 01f9ec6 | 2018-12-29 13:19:14 -0800 | [diff] [blame] | 1441 | |
Christian Couder | d92ae2c | 2020-05-15 12:04:47 +0200 | [diff] [blame] | 1442 | receive_needs(&data, &reader); |
Daniel Duvall | fb3d1a0 | 2020-10-30 19:39:02 -0700 | [diff] [blame] | 1443 | |
| 1444 | /* |
| 1445 | * An EOF at this exact point in negotiation should be |
| 1446 | * acceptable from stateless clients as they will consume the |
| 1447 | * shallow list before doing subsequent rpc with haves/etc. |
| 1448 | */ |
| 1449 | if (data.stateless_rpc) |
| 1450 | reader.options |= PACKET_READ_GENTLE_ON_EOF; |
| 1451 | |
| 1452 | if (data.want_obj.nr && |
| 1453 | packet_reader_peek(&reader) != PACKET_READ_EOF) { |
| 1454 | reader.options &= ~PACKET_READ_GENTLE_ON_EOF; |
Christian Couder | 0797769 | 2020-05-15 12:04:46 +0200 | [diff] [blame] | 1455 | get_common_commits(&data, &reader); |
Ramsay Jones | cae2ee1 | 2020-06-16 00:00:20 +0100 | [diff] [blame] | 1456 | create_pack_file(&data, NULL); |
Christian Couder | ebf8ebc | 2020-05-15 12:04:45 +0200 | [diff] [blame] | 1457 | } |
Brandon Williams | a3d6b53 | 2018-03-14 11:31:41 -0700 | [diff] [blame] | 1458 | } |
Christian Couder | 08450ef | 2020-05-08 10:01:15 +0200 | [diff] [blame] | 1459 | |
Christian Couder | ebf8ebc | 2020-05-15 12:04:45 +0200 | [diff] [blame] | 1460 | upload_pack_data_clear(&data); |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 1461 | } |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1462 | |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1463 | static int parse_want(struct packet_writer *writer, const char *line, |
| 1464 | struct object_array *want_obj) |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1465 | { |
| 1466 | const char *arg; |
| 1467 | if (skip_prefix(line, "want ", &arg)) { |
| 1468 | struct object_id oid; |
| 1469 | struct object *o; |
| 1470 | |
| 1471 | if (get_oid_hex(arg, &oid)) |
| 1472 | die("git upload-pack: protocol error, " |
| 1473 | "expected to get oid, not '%s'", line); |
| 1474 | |
Jeff King | 9a8c3c4 | 2022-09-06 19:06:25 -0400 | [diff] [blame] | 1475 | o = parse_object_with_flags(the_repository, &oid, |
| 1476 | PARSE_OBJECT_SKIP_HASH_CHECK); |
Patrick Steinhardt | 4de6562 | 2022-03-01 10:33:37 +0100 | [diff] [blame] | 1477 | |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1478 | if (!o) { |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1479 | packet_writer_error(writer, |
| 1480 | "upload-pack: not our ref %s", |
| 1481 | oid_to_hex(&oid)); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1482 | die("git upload-pack: not our ref %s", |
| 1483 | oid_to_hex(&oid)); |
| 1484 | } |
| 1485 | |
| 1486 | if (!(o->flags & WANTED)) { |
| 1487 | o->flags |= WANTED; |
Jonathan Tan | 1d1243f | 2018-10-18 13:43:28 -0700 | [diff] [blame] | 1488 | add_object_array(o, NULL, want_obj); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | return 1; |
Linus Torvalds | def88e9 | 2005-07-04 13:26:53 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | return 0; |
| 1495 | } |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1496 | |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1497 | static int parse_want_ref(struct packet_writer *writer, const char *line, |
| 1498 | struct string_list *wanted_refs, |
Taylor Blau | c45841f | 2023-07-10 17:12:33 -0400 | [diff] [blame] | 1499 | struct strvec *hidden_refs, |
Jonathan Tan | 1d1243f | 2018-10-18 13:43:28 -0700 | [diff] [blame] | 1500 | struct object_array *want_obj) |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1501 | { |
Kim Altintop | 3955140 | 2021-08-13 06:23:50 +0000 | [diff] [blame] | 1502 | const char *refname_nons; |
| 1503 | if (skip_prefix(line, "want-ref ", &refname_nons)) { |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1504 | struct object_id oid; |
| 1505 | struct string_list_item *item; |
Patrick Steinhardt | 4de6562 | 2022-03-01 10:33:37 +0100 | [diff] [blame] | 1506 | struct object *o = NULL; |
Kim Altintop | 3955140 | 2021-08-13 06:23:50 +0000 | [diff] [blame] | 1507 | struct strbuf refname = STRBUF_INIT; |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1508 | |
Kim Altintop | 3955140 | 2021-08-13 06:23:50 +0000 | [diff] [blame] | 1509 | strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons); |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 1510 | if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) || |
Kim Altintop | 3955140 | 2021-08-13 06:23:50 +0000 | [diff] [blame] | 1511 | read_ref(refname.buf, &oid)) { |
| 1512 | packet_writer_error(writer, "unknown ref %s", refname_nons); |
| 1513 | die("unknown ref %s", refname_nons); |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1514 | } |
Kim Altintop | 3955140 | 2021-08-13 06:23:50 +0000 | [diff] [blame] | 1515 | strbuf_release(&refname); |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1516 | |
Kim Altintop | 3955140 | 2021-08-13 06:23:50 +0000 | [diff] [blame] | 1517 | item = string_list_append(wanted_refs, refname_nons); |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1518 | item->util = oiddup(&oid); |
| 1519 | |
Patrick Steinhardt | 4de6562 | 2022-03-01 10:33:37 +0100 | [diff] [blame] | 1520 | if (!starts_with(refname_nons, "refs/tags/")) { |
| 1521 | struct commit *commit = lookup_commit_in_graph(the_repository, &oid); |
| 1522 | if (commit) |
| 1523 | o = &commit->object; |
| 1524 | } |
| 1525 | |
| 1526 | if (!o) |
| 1527 | o = parse_object_or_die(&oid, refname_nons); |
| 1528 | |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1529 | if (!(o->flags & WANTED)) { |
| 1530 | o->flags |= WANTED; |
Jonathan Tan | 1d1243f | 2018-10-18 13:43:28 -0700 | [diff] [blame] | 1531 | add_object_array(o, NULL, want_obj); |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | return 1; |
| 1535 | } |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1540 | static int parse_have(const char *line, struct oid_array *haves) |
| 1541 | { |
| 1542 | const char *arg; |
| 1543 | if (skip_prefix(line, "have ", &arg)) { |
| 1544 | struct object_id oid; |
| 1545 | |
| 1546 | if (get_oid_hex(arg, &oid)) |
| 1547 | die("git upload-pack: expected SHA1 object, got '%s'", arg); |
| 1548 | oid_array_append(haves, &oid); |
| 1549 | return 1; |
| 1550 | } |
| 1551 | |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
| 1555 | static void process_args(struct packet_reader *request, |
Christian Couder | 389f161 | 2020-05-15 12:04:43 +0200 | [diff] [blame] | 1556 | struct upload_pack_data *data) |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1557 | { |
Jeff King | 4845b77 | 2020-03-27 04:03:38 -0400 | [diff] [blame] | 1558 | while (packet_reader_read(request) == PACKET_READ_NORMAL) { |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1559 | const char *arg = request->line; |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1560 | const char *p; |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1561 | |
| 1562 | /* process want */ |
Christian Couder | 389f161 | 2020-05-15 12:04:43 +0200 | [diff] [blame] | 1563 | if (parse_want(&data->writer, arg, &data->want_obj)) |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1564 | continue; |
Christian Couder | d1d7a94 | 2020-06-04 19:54:48 +0200 | [diff] [blame] | 1565 | if (data->allow_ref_in_want && |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1566 | parse_want_ref(&data->writer, arg, &data->wanted_refs, |
Patrick Steinhardt | 9b67eb6 | 2022-11-17 06:46:43 +0100 | [diff] [blame] | 1567 | &data->hidden_refs, &data->want_obj)) |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1568 | continue; |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1569 | /* process have line */ |
| 1570 | if (parse_have(arg, &data->haves)) |
| 1571 | continue; |
| 1572 | |
| 1573 | /* process args like thin-pack */ |
| 1574 | if (!strcmp(arg, "thin-pack")) { |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 1575 | data->use_thin_pack = 1; |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1576 | continue; |
| 1577 | } |
| 1578 | if (!strcmp(arg, "ofs-delta")) { |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 1579 | data->use_ofs_delta = 1; |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1580 | continue; |
| 1581 | } |
| 1582 | if (!strcmp(arg, "no-progress")) { |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 1583 | data->no_progress = 1; |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1584 | continue; |
| 1585 | } |
| 1586 | if (!strcmp(arg, "include-tag")) { |
Jeff King | b5a2068 | 2020-06-04 19:54:38 +0200 | [diff] [blame] | 1587 | data->use_include_tag = 1; |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1588 | continue; |
| 1589 | } |
| 1590 | if (!strcmp(arg, "done")) { |
| 1591 | data->done = 1; |
| 1592 | continue; |
| 1593 | } |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 1594 | if (!strcmp(arg, "wait-for-done")) { |
| 1595 | data->wait_for_done = 1; |
| 1596 | continue; |
| 1597 | } |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1598 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1599 | /* Shallow related arguments */ |
| 1600 | if (process_shallow(arg, &data->shallows)) |
| 1601 | continue; |
| 1602 | if (process_deepen(arg, &data->depth)) |
| 1603 | continue; |
| 1604 | if (process_deepen_since(arg, &data->deepen_since, |
| 1605 | &data->deepen_rev_list)) |
| 1606 | continue; |
| 1607 | if (process_deepen_not(arg, &data->deepen_not, |
| 1608 | &data->deepen_rev_list)) |
| 1609 | continue; |
| 1610 | if (!strcmp(arg, "deepen-relative")) { |
| 1611 | data->deepen_relative = 1; |
| 1612 | continue; |
| 1613 | } |
| 1614 | |
Christian Couder | 59abe19 | 2020-06-04 19:54:47 +0200 | [diff] [blame] | 1615 | if (data->allow_filter && skip_prefix(arg, "filter ", &p)) { |
Christian Couder | 08450ef | 2020-05-08 10:01:15 +0200 | [diff] [blame] | 1616 | list_objects_filter_die_if_populated(&data->filter_options); |
| 1617 | parse_list_objects_filter(&data->filter_options, p); |
Taylor Blau | 6dd3456 | 2020-08-03 14:00:10 -0400 | [diff] [blame] | 1618 | die_if_using_banned_filter(data); |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1619 | continue; |
| 1620 | } |
| 1621 | |
Jonathan Tan | 07c3c2a | 2019-01-16 11:28:15 -0800 | [diff] [blame] | 1622 | if ((git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) || |
Christian Couder | e3835cd | 2020-06-04 19:54:49 +0200 | [diff] [blame] | 1623 | data->allow_sideband_all) && |
Jonathan Tan | 07c3c2a | 2019-01-16 11:28:15 -0800 | [diff] [blame] | 1624 | !strcmp(arg, "sideband-all")) { |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1625 | data->writer.use_sideband = 1; |
| 1626 | continue; |
| 1627 | } |
| 1628 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1629 | if (skip_prefix(arg, "packfile-uris ", &p)) { |
| 1630 | string_list_split(&data->uri_protocols, p, ',', -1); |
| 1631 | continue; |
| 1632 | } |
| 1633 | |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1634 | /* ignore unknown lines maybe? */ |
Jonathan Tan | 7cc6ed2 | 2018-05-01 17:31:29 -0700 | [diff] [blame] | 1635 | die("unexpected line: '%s'", arg); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1636 | } |
Jeff King | 4845b77 | 2020-03-27 04:03:38 -0400 | [diff] [blame] | 1637 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1638 | if (data->uri_protocols.nr && !data->writer.use_sideband) |
| 1639 | string_list_clear(&data->uri_protocols, 0); |
| 1640 | |
Jeff King | 4845b77 | 2020-03-27 04:03:38 -0400 | [diff] [blame] | 1641 | if (request->status != PACKET_READ_FLUSH) |
| 1642 | die(_("expected flush after fetch arguments")); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
Christian Couder | 8dcf227 | 2020-06-11 14:05:13 +0200 | [diff] [blame] | 1645 | static int process_haves(struct upload_pack_data *data, struct oid_array *common) |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1646 | { |
| 1647 | int i; |
| 1648 | |
| 1649 | /* Process haves */ |
Christian Couder | 8dcf227 | 2020-06-11 14:05:13 +0200 | [diff] [blame] | 1650 | for (i = 0; i < data->haves.nr; i++) { |
| 1651 | const struct object_id *oid = &data->haves.oid[i]; |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1652 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 1653 | if (!repo_has_object_file_with_flags(the_repository, oid, |
| 1654 | OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT)) |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1655 | continue; |
| 1656 | |
| 1657 | oid_array_append(common, oid); |
| 1658 | |
Christian Couder | ea2c6e6 | 2020-06-11 14:05:18 +0200 | [diff] [blame] | 1659 | do_got_oid(data, oid); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
Christian Couder | 6fbbc43 | 2020-06-11 14:05:14 +0200 | [diff] [blame] | 1665 | static int send_acks(struct upload_pack_data *data, struct oid_array *acks) |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1666 | { |
| 1667 | int i; |
| 1668 | |
Christian Couder | 6fbbc43 | 2020-06-11 14:05:14 +0200 | [diff] [blame] | 1669 | packet_writer_write(&data->writer, "acknowledgments\n"); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1670 | |
| 1671 | /* Send Acks */ |
| 1672 | if (!acks->nr) |
Christian Couder | 6fbbc43 | 2020-06-11 14:05:14 +0200 | [diff] [blame] | 1673 | packet_writer_write(&data->writer, "NAK\n"); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1674 | |
| 1675 | for (i = 0; i < acks->nr; i++) { |
Christian Couder | 6fbbc43 | 2020-06-11 14:05:14 +0200 | [diff] [blame] | 1676 | packet_writer_write(&data->writer, "ACK %s\n", |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1677 | oid_to_hex(&acks->oid[i])); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 1680 | if (!data->wait_for_done && ok_to_give_up(data)) { |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1681 | /* Send Ready */ |
Christian Couder | 6fbbc43 | 2020-06-11 14:05:14 +0200 | [diff] [blame] | 1682 | packet_writer_write(&data->writer, "ready\n"); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1683 | return 1; |
| 1684 | } |
| 1685 | |
| 1686 | return 0; |
| 1687 | } |
| 1688 | |
Christian Couder | 389f161 | 2020-05-15 12:04:43 +0200 | [diff] [blame] | 1689 | static int process_haves_and_send_acks(struct upload_pack_data *data) |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1690 | { |
| 1691 | struct oid_array common = OID_ARRAY_INIT; |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1692 | int ret = 0; |
| 1693 | |
Christian Couder | 8dcf227 | 2020-06-11 14:05:13 +0200 | [diff] [blame] | 1694 | process_haves(data, &common); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1695 | if (data->done) { |
| 1696 | ret = 1; |
Christian Couder | 6fbbc43 | 2020-06-11 14:05:14 +0200 | [diff] [blame] | 1697 | } else if (send_acks(data, &common)) { |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1698 | packet_writer_delim(&data->writer); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1699 | ret = 1; |
| 1700 | } else { |
| 1701 | /* Add Flush */ |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1702 | packet_writer_flush(&data->writer); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1703 | ret = 0; |
| 1704 | } |
| 1705 | |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1706 | oid_array_clear(&data->haves); |
| 1707 | oid_array_clear(&common); |
| 1708 | return ret; |
| 1709 | } |
| 1710 | |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1711 | static void send_wanted_ref_info(struct upload_pack_data *data) |
| 1712 | { |
| 1713 | const struct string_list_item *item; |
| 1714 | |
| 1715 | if (!data->wanted_refs.nr) |
| 1716 | return; |
| 1717 | |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1718 | packet_writer_write(&data->writer, "wanted-refs\n"); |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1719 | |
| 1720 | for_each_string_list_item(item, &data->wanted_refs) { |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1721 | packet_writer_write(&data->writer, "%s %s\n", |
| 1722 | oid_to_hex(item->util), |
| 1723 | item->string); |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1726 | packet_writer_delim(&data->writer); |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
Christian Couder | 389f161 | 2020-05-15 12:04:43 +0200 | [diff] [blame] | 1729 | static void send_shallow_info(struct upload_pack_data *data) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1730 | { |
| 1731 | /* No shallow info needs to be sent */ |
| 1732 | if (!data->depth && !data->deepen_rev_list && !data->shallows.nr && |
Junio C Hamano | 00624d6 | 2018-07-18 12:20:27 -0700 | [diff] [blame] | 1733 | !is_repository_shallow(the_repository)) |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1734 | return; |
| 1735 | |
Jonathan Tan | bc2e795 | 2019-01-15 11:40:27 -0800 | [diff] [blame] | 1736 | packet_writer_write(&data->writer, "shallow-info\n"); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1737 | |
Christian Couder | ee703c8 | 2020-06-11 14:05:05 +0200 | [diff] [blame] | 1738 | if (!send_shallow_list(data) && |
Junio C Hamano | 00624d6 | 2018-07-18 12:20:27 -0700 | [diff] [blame] | 1739 | is_repository_shallow(the_repository)) |
Christian Couder | b1492f2 | 2020-06-11 14:05:06 +0200 | [diff] [blame] | 1740 | deepen(data, INFINITE_DEPTH); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1741 | |
| 1742 | packet_delim(1); |
| 1743 | } |
| 1744 | |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1745 | enum fetch_state { |
| 1746 | FETCH_PROCESS_ARGS = 0, |
| 1747 | FETCH_SEND_ACKS, |
| 1748 | FETCH_SEND_PACK, |
| 1749 | FETCH_DONE, |
| 1750 | }; |
| 1751 | |
Jeff King | 74595cc | 2023-02-24 01:38:23 -0500 | [diff] [blame] | 1752 | int upload_pack_v2(struct repository *r UNUSED, struct packet_reader *request) |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1753 | { |
| 1754 | enum fetch_state state = FETCH_PROCESS_ARGS; |
| 1755 | struct upload_pack_data data; |
Jonathan Tan | d1035ca | 2018-10-18 13:43:29 -0700 | [diff] [blame] | 1756 | |
| 1757 | clear_object_flags(ALL_FLAGS); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1758 | |
| 1759 | upload_pack_data_init(&data); |
Christian Couder | f8edd1c | 2020-06-04 19:54:41 +0200 | [diff] [blame] | 1760 | data.use_sideband = LARGE_PACKET_MAX; |
Glen Choo | 5b3c650 | 2022-07-14 21:27:59 +0000 | [diff] [blame] | 1761 | get_upload_pack_config(&data); |
Christian Couder | 8a0e6f1 | 2020-06-04 19:54:45 +0200 | [diff] [blame] | 1762 | |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1763 | while (state != FETCH_DONE) { |
| 1764 | switch (state) { |
| 1765 | case FETCH_PROCESS_ARGS: |
Christian Couder | 389f161 | 2020-05-15 12:04:43 +0200 | [diff] [blame] | 1766 | process_args(request, &data); |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1767 | |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 1768 | if (!data.want_obj.nr && !data.wait_for_done) { |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1769 | /* |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 1770 | * Request didn't contain any 'want' lines (and |
| 1771 | * the request does not contain |
| 1772 | * "wait-for-done", in which it is reasonable |
| 1773 | * to just send 'have's without 'want's); guess |
| 1774 | * they didn't want anything. |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1775 | */ |
| 1776 | state = FETCH_DONE; |
| 1777 | } else if (data.haves.nr) { |
| 1778 | /* |
| 1779 | * Request had 'have' lines, so lets ACK them. |
| 1780 | */ |
| 1781 | state = FETCH_SEND_ACKS; |
| 1782 | } else { |
| 1783 | /* |
| 1784 | * Request had 'want's but no 'have's so we can |
| 1785 | * immedietly go to construct and send a pack. |
| 1786 | */ |
| 1787 | state = FETCH_SEND_PACK; |
| 1788 | } |
| 1789 | break; |
| 1790 | case FETCH_SEND_ACKS: |
Christian Couder | 389f161 | 2020-05-15 12:04:43 +0200 | [diff] [blame] | 1791 | if (process_haves_and_send_acks(&data)) |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1792 | state = FETCH_SEND_PACK; |
| 1793 | else |
| 1794 | state = FETCH_DONE; |
| 1795 | break; |
| 1796 | case FETCH_SEND_PACK: |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1797 | send_wanted_ref_info(&data); |
Christian Couder | 389f161 | 2020-05-15 12:04:43 +0200 | [diff] [blame] | 1798 | send_shallow_info(&data); |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1799 | |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1800 | if (data.uri_protocols.nr) { |
| 1801 | create_pack_file(&data, &data.uri_protocols); |
| 1802 | } else { |
| 1803 | packet_writer_write(&data.writer, "packfile\n"); |
| 1804 | create_pack_file(&data, NULL); |
| 1805 | } |
Brandon Williams | 3145ea9 | 2018-03-15 10:31:27 -0700 | [diff] [blame] | 1806 | state = FETCH_DONE; |
| 1807 | break; |
| 1808 | case FETCH_DONE: |
| 1809 | continue; |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | upload_pack_data_clear(&data); |
| 1814 | return 0; |
| 1815 | } |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1816 | |
| 1817 | int upload_pack_advertise(struct repository *r, |
| 1818 | struct strbuf *value) |
| 1819 | { |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1820 | if (value) { |
| 1821 | int allow_filter_value; |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1822 | int allow_ref_in_want; |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1823 | int allow_sideband_all_value; |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1824 | char *str = NULL; |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1825 | |
Jonathan Tan | 9c1e657 | 2021-05-04 14:16:01 -0700 | [diff] [blame] | 1826 | strbuf_addstr(value, "shallow wait-for-done"); |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1827 | |
Jeff King | 4b4e75d | 2023-02-24 01:38:10 -0500 | [diff] [blame] | 1828 | if (!repo_config_get_bool(r, |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1829 | "uploadpack.allowfilter", |
| 1830 | &allow_filter_value) && |
| 1831 | allow_filter_value) |
| 1832 | strbuf_addstr(value, " filter"); |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1833 | |
Jeff King | 4b4e75d | 2023-02-24 01:38:10 -0500 | [diff] [blame] | 1834 | if (!repo_config_get_bool(r, |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1835 | "uploadpack.allowrefinwant", |
| 1836 | &allow_ref_in_want) && |
| 1837 | allow_ref_in_want) |
| 1838 | strbuf_addstr(value, " ref-in-want"); |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1839 | |
Jonathan Tan | 07c3c2a | 2019-01-16 11:28:15 -0800 | [diff] [blame] | 1840 | if (git_env_bool("GIT_TEST_SIDEBAND_ALL", 0) || |
Jeff King | 4b4e75d | 2023-02-24 01:38:10 -0500 | [diff] [blame] | 1841 | (!repo_config_get_bool(r, |
Jonathan Tan | 07c3c2a | 2019-01-16 11:28:15 -0800 | [diff] [blame] | 1842 | "uploadpack.allowsidebandall", |
| 1843 | &allow_sideband_all_value) && |
| 1844 | allow_sideband_all_value)) |
Jonathan Tan | 0bbc0bc | 2019-01-16 11:28:14 -0800 | [diff] [blame] | 1845 | strbuf_addstr(value, " sideband-all"); |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1846 | |
Jeff King | 4b4e75d | 2023-02-24 01:38:10 -0500 | [diff] [blame] | 1847 | if (!repo_config_get_string(r, |
Jonathan Tan | dd4b732 | 2020-06-10 13:57:23 -0700 | [diff] [blame] | 1848 | "uploadpack.blobpackfileuri", |
| 1849 | &str) && |
| 1850 | str) { |
| 1851 | strbuf_addstr(value, " packfile-uris"); |
| 1852 | free(str); |
| 1853 | } |
Jonathan Tan | ba95710 | 2018-05-03 16:46:56 -0700 | [diff] [blame] | 1854 | } |
Brandon Williams | 516e2b7 | 2018-06-27 15:30:17 -0700 | [diff] [blame] | 1855 | |
Brandon Williams | 685fbd3 | 2018-03-15 10:31:28 -0700 | [diff] [blame] | 1856 | return 1; |
| 1857 | } |