blob: 82b393ec31917c0c2bcd904668a60b5aaef00633 [file] [log] [blame]
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Linus Torvaldsdef88e92005-07-04 13:26:53 -07003#include "refs.h"
4#include "pkt-line.h"
Junio C Hamano958c24b2006-09-10 03:20:24 -07005#include "sideband.h"
Stefan Beller109cd762018-06-28 18:21:51 -07006#include "repository.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07007#include "object-store.h"
Junio C Hamanof6b42a82005-10-13 18:57:40 -07008#include "tag.h"
9#include "object.h"
Johannes Schindelinf0243f22005-10-28 04:48:32 +020010#include "commit.h"
Johannes Schindelin9b8dc262006-10-30 20:08:43 +010011#include "diff.h"
12#include "revision.h"
13#include "list-objects.h"
Jeff Hostetler10ac85c2017-12-08 15:58:39 +000014#include "list-objects-filter.h"
15#include "list-objects-filter-options.h"
Johannes Sixtcc41fa82007-10-19 21:47:59 +020016#include "run-command.h"
Junio C Hamano47a59182013-07-08 13:56:53 -070017#include "connect.h"
Junio C Hamano051e4002011-08-05 13:54:06 -070018#include "sigchain.h"
Jeff Kingff5effd2012-08-03 12:19:16 -040019#include "version.h"
Junio C Hamanodaebaa72013-01-18 16:08:30 -080020#include "string-list.h"
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +070021#include "argv-array.h"
Jeff King5411b102016-10-11 17:20:16 -040022#include "prio-queue.h"
Brandon Williamsaa9bab22017-10-16 10:55:26 -070023#include "protocol.h"
Jeff Hostetler10ac85c2017-12-08 15:58:39 +000024#include "quote.h"
Brandon Williamsa3d6b532018-03-14 11:31:41 -070025#include "upload-pack.h"
Brandon Williams3145ea92018-03-15 10:31:27 -070026#include "serve.h"
Linus Torvaldsdef88e92005-07-04 13:26:53 -070027
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070028/* Remember to update object flag allocation in object.h */
Junio C Hamano937a5152006-07-05 21:28:20 -070029#define THEY_HAVE (1u << 11)
30#define OUR_REF (1u << 12)
31#define WANTED (1u << 13)
32#define COMMON_KNOWN (1u << 14)
33#define REACHABLE (1u << 15)
34
Johannes Schindelinf53514b2006-10-30 20:09:53 +010035#define SHALLOW (1u << 16)
36#define NOT_SHALLOW (1u << 17)
37#define CLIENT_SHALLOW (1u << 18)
Junio C Hamano390eb362013-01-28 21:49:57 -080038#define HIDDEN_REF (1u << 19)
Johannes Schindelinf53514b2006-10-30 20:09:53 +010039
Johannes Schindelindddbad72017-04-26 21:29:31 +020040static timestamp_t oldest_have;
Junio C Hamano937a5152006-07-05 21:28:20 -070041
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +070042static int deepen_relative;
Junio C Hamano3f1da572013-01-28 20:45:43 -080043static int multi_ack;
Junio C Hamano4e10cf92011-03-29 12:29:10 -070044static int no_done;
Shawn O. Pearce348e3902008-03-03 22:27:33 -050045static int use_thin_pack, use_ofs_delta, use_include_tag;
Johannes Sixt9462e3f2009-06-16 20:41:16 +020046static int no_progress, daemon_mode;
Fredrik Medley7199c092015-05-21 22:23:38 +020047/* Allow specifying sha1 if it is a ref tip. */
48#define ALLOW_TIP_SHA1 01
Fredrik Medley68ee6282015-05-21 22:23:39 +020049/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
50#define ALLOW_REACHABLE_SHA1 02
David Turnerf8edeaa2016-11-11 12:23:48 -050051/* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
52#define ALLOW_ANY_SHA1 07
Fredrik Medley7199c092015-05-21 22:23:38 +020053static unsigned int allow_unadvertised_object_request;
Nick Edelenf0cea832009-06-10 01:50:18 +020054static int shallow_nr;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -070055static struct object_array have_obj;
56static struct object_array want_obj;
Nicolas Pitre65230782009-09-03 19:08:33 -040057static struct object_array extra_edge_obj;
David Rientjes96f1e582006-08-15 10:23:48 -070058static unsigned int timeout;
Jeff King115dedd2013-09-08 05:02:06 -040059static int keepalive = 5;
Junio C Hamanod47f3db2006-09-10 16:27:08 -070060/* 0 for no sideband,
61 * otherwise maximum packet size (up to 65520 bytes).
62 */
David Rientjes96f1e582006-08-15 10:23:48 -070063static int use_sideband;
Shawn O. Pearce42526b42009-10-30 17:47:33 -070064static int stateless_rpc;
Jeff King20b20a22016-05-18 18:45:37 -040065static const char *pack_objects_hook;
H. Peter Anvin960decc2005-10-19 14:27:01 -070066
Jeff Hostetler10ac85c2017-12-08 15:58:39 +000067static int filter_capability_requested;
Jonathan Niederc7620bd2018-03-28 13:33:03 -070068static int allow_filter;
Brandon Williams516e2b72018-06-27 15:30:17 -070069static int allow_ref_in_want;
Jeff Hostetler10ac85c2017-12-08 15:58:39 +000070static struct list_objects_filter_options filter_options;
71
H. Peter Anvin960decc2005-10-19 14:27:01 -070072static void reset_timeout(void)
73{
74 alarm(timeout);
75}
Linus Torvaldsfb9040c2005-07-04 15:29:17 -070076
Lukas Fleischerfcf0fe92016-06-14 16:49:17 +020077static void send_client_data(int fd, const char *data, ssize_t sz)
Junio C Hamano583b7ea2006-06-21 00:30:21 -070078{
Lukas Fleischer4c4b7d12016-06-14 16:49:16 +020079 if (use_sideband) {
80 send_sideband(1, fd, data, sz, use_sideband);
Lukas Fleischerfcf0fe92016-06-14 16:49:17 +020081 return;
Lukas Fleischer4c4b7d12016-06-14 16:49:16 +020082 }
Junio C Hamano958c24b2006-09-10 03:20:24 -070083 if (fd == 3)
84 /* emergency quit */
85 fd = 2;
86 if (fd == 2) {
Andy Whitcroft93822c22007-01-08 15:58:23 +000087 /* XXX: are we happy to lose stuff here? */
Junio C Hamano958c24b2006-09-10 03:20:24 -070088 xwrite(fd, data, sz);
Lukas Fleischerfcf0fe92016-06-14 16:49:17 +020089 return;
Junio C Hamano583b7ea2006-06-21 00:30:21 -070090 }
Jeff Kingcdf4fb82013-02-20 15:01:56 -050091 write_or_die(fd, data, sz);
Junio C Hamano583b7ea2006-06-21 00:30:21 -070092}
93
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +070094static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
95{
96 FILE *fp = cb_data;
97 if (graft->nr_parent == -1)
brian m. carlson7683e2e2015-03-13 23:39:34 +000098 fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +070099 return 0;
100}
101
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700102static void create_pack_file(void)
103{
René Scharfed3180272014-08-19 21:09:35 +0200104 struct child_process pack_objects = CHILD_PROCESS_INIT;
Junio C Hamano363b7812006-06-20 22:48:23 -0700105 char data[8193], progress[128];
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700106 char abort_msg[] = "aborting due to possible repository "
107 "corruption on the remote side.";
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700108 int buffered = -1;
Junio C Hamano1456b042009-12-10 12:17:11 -0800109 ssize_t sz;
Michael Procter65a36292016-02-25 12:13:26 +0000110 int i;
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700111 FILE *pipe_fd;
Linus Torvalds75bfc6c2005-07-04 16:35:13 -0700112
Jeff King20b20a22016-05-18 18:45:37 -0400113 if (!pack_objects_hook)
114 pack_objects.git_cmd = 1;
115 else {
116 argv_array_push(&pack_objects.args, pack_objects_hook);
117 argv_array_push(&pack_objects.args, "git");
118 pack_objects.use_shell = 1;
119 }
120
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700121 if (shallow_nr) {
Michael Procter65a36292016-02-25 12:13:26 +0000122 argv_array_push(&pack_objects.args, "--shallow-file");
123 argv_array_push(&pack_objects.args, "");
Nick Edelenf0cea832009-06-10 01:50:18 +0200124 }
Michael Procter65a36292016-02-25 12:13:26 +0000125 argv_array_push(&pack_objects.args, "pack-objects");
126 argv_array_push(&pack_objects.args, "--revs");
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700127 if (use_thin_pack)
Michael Procter65a36292016-02-25 12:13:26 +0000128 argv_array_push(&pack_objects.args, "--thin");
Linus Torvalds75bfc6c2005-07-04 16:35:13 -0700129
Michael Procter65a36292016-02-25 12:13:26 +0000130 argv_array_push(&pack_objects.args, "--stdout");
brian m. carlson2dacf262014-12-24 23:05:40 +0000131 if (shallow_nr)
Michael Procter65a36292016-02-25 12:13:26 +0000132 argv_array_push(&pack_objects.args, "--shallow");
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200133 if (!no_progress)
Michael Procter65a36292016-02-25 12:13:26 +0000134 argv_array_push(&pack_objects.args, "--progress");
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200135 if (use_ofs_delta)
Michael Procter65a36292016-02-25 12:13:26 +0000136 argv_array_push(&pack_objects.args, "--delta-base-offset");
Shawn O. Pearce348e3902008-03-03 22:27:33 -0500137 if (use_include_tag)
Michael Procter65a36292016-02-25 12:13:26 +0000138 argv_array_push(&pack_objects.args, "--include-tag");
Jeff Hostetler10ac85c2017-12-08 15:58:39 +0000139 if (filter_options.filter_spec) {
Jonathan Tan0b6069f2017-12-08 15:58:42 +0000140 if (pack_objects.use_shell) {
141 struct strbuf buf = STRBUF_INIT;
142 sq_quote_buf(&buf, filter_options.filter_spec);
143 argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
144 strbuf_release(&buf);
145 } else {
146 argv_array_pushf(&pack_objects.args, "--filter=%s",
147 filter_options.filter_spec);
148 }
Jeff Hostetler10ac85c2017-12-08 15:58:39 +0000149 }
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200150
Jeff Kingb9612192011-04-06 17:33:33 -0400151 pack_objects.in = -1;
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200152 pack_objects.out = -1;
153 pack_objects.err = -1;
Johannes Sixt21edd3f2007-10-19 21:48:03 +0200154
Johannes Sixt4c324c02007-11-04 20:46:48 +0100155 if (start_command(&pack_objects))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700156 die("git upload-pack: unable to fork git-pack-objects");
Johannes Schindelin83a5ad62007-02-20 03:01:44 +0100157
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700158 pipe_fd = xfdopen(pack_objects.in, "w");
Nick Edelenf0cea832009-06-10 01:50:18 +0200159
Nguyễn Thái Ngọc Duyb790e0f2014-03-11 19:59:46 +0700160 if (shallow_nr)
161 for_each_commit_graft(write_one_shallow, pipe_fd);
162
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700163 for (i = 0; i < want_obj.nr; i++)
164 fprintf(pipe_fd, "%s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000165 oid_to_hex(&want_obj.objects[i].item->oid));
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700166 fprintf(pipe_fd, "--not\n");
167 for (i = 0; i < have_obj.nr; i++)
168 fprintf(pipe_fd, "%s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000169 oid_to_hex(&have_obj.objects[i].item->oid));
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700170 for (i = 0; i < extra_edge_obj.nr; i++)
171 fprintf(pipe_fd, "%s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000172 oid_to_hex(&extra_edge_obj.objects[i].item->oid));
Nguyễn Thái Ngọc Duycdab4852013-08-16 16:52:05 +0700173 fprintf(pipe_fd, "\n");
174 fflush(pipe_fd);
175 fclose(pipe_fd);
Nick Edelenf0cea832009-06-10 01:50:18 +0200176
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200177 /* We read from pack_objects.err to capture stderr output for
178 * progress bar, and pack_objects.out to capture the pack data.
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700179 */
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700180
181 while (1) {
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700182 struct pollfd pfd[2];
Junio C Hamano363b7812006-06-20 22:48:23 -0700183 int pe, pu, pollsize;
Jeff King05e95152013-09-08 05:01:31 -0400184 int ret;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700185
Matthias Lederhofer0d516ad2006-07-18 19:14:51 +0200186 reset_timeout();
187
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700188 pollsize = 0;
Junio C Hamano363b7812006-06-20 22:48:23 -0700189 pe = pu = -1;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700190
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200191 if (0 <= pack_objects.out) {
192 pfd[pollsize].fd = pack_objects.out;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700193 pfd[pollsize].events = POLLIN;
194 pu = pollsize;
195 pollsize++;
196 }
Johannes Sixtcc41fa82007-10-19 21:47:59 +0200197 if (0 <= pack_objects.err) {
198 pfd[pollsize].fd = pack_objects.err;
Junio C Hamano363b7812006-06-20 22:48:23 -0700199 pfd[pollsize].events = POLLIN;
200 pe = pollsize;
201 pollsize++;
202 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700203
Johannes Sixt4c324c02007-11-04 20:46:48 +0100204 if (!pollsize)
205 break;
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700206
Edward Thomson6c71f8b2014-08-22 15:19:11 +0000207 ret = poll(pfd, pollsize,
208 keepalive < 0 ? -1 : 1000 * keepalive);
209
Jeff King05e95152013-09-08 05:01:31 -0400210 if (ret < 0) {
Johannes Sixt4c324c02007-11-04 20:46:48 +0100211 if (errno != EINTR) {
Nguyễn Thái Ngọc Duyd2b6afa2016-05-08 16:47:59 +0700212 error_errno("poll failed, resuming");
Johannes Sixt4c324c02007-11-04 20:46:48 +0100213 sleep(1);
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700214 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700215 continue;
Johannes Sixt4c324c02007-11-04 20:46:48 +0100216 }
Nicolas Pitre6b59f512009-11-11 17:24:42 -0500217 if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
218 /* Status ready; we ship that in the side-band
219 * or dump to the standard error.
220 */
221 sz = xread(pack_objects.err, progress,
222 sizeof(progress));
223 if (0 < sz)
224 send_client_data(2, progress, sz);
225 else if (sz == 0) {
226 close(pack_objects.err);
227 pack_objects.err = -1;
228 }
229 else
230 goto fail;
231 /* give priority to status messages */
232 continue;
233 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100234 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
235 /* Data ready; we keep the last byte to ourselves
236 * in case we detect broken rev-list, so that we
237 * can leave the stream corrupted. This is
238 * unfortunate -- unpack-objects would happily
239 * accept a valid packdata with trailing garbage,
240 * so appending garbage after we pass all the
241 * pack data is not good enough to signal
242 * breakage to downstream.
243 */
244 char *cp = data;
245 ssize_t outsz = 0;
246 if (0 <= buffered) {
247 *cp++ = buffered;
248 outsz++;
249 }
250 sz = xread(pack_objects.out, cp,
251 sizeof(data) - outsz);
252 if (0 < sz)
Junio C Hamano1456b042009-12-10 12:17:11 -0800253 ;
Johannes Sixt4c324c02007-11-04 20:46:48 +0100254 else if (sz == 0) {
255 close(pack_objects.out);
256 pack_objects.out = -1;
257 }
258 else
259 goto fail;
260 sz += outsz;
261 if (1 < sz) {
262 buffered = data[sz-1] & 0xFF;
263 sz--;
264 }
265 else
266 buffered = -1;
Lukas Fleischerfcf0fe92016-06-14 16:49:17 +0200267 send_client_data(1, data, sz);
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700268 }
Jeff King05e95152013-09-08 05:01:31 -0400269
270 /*
271 * We hit the keepalive timeout without saying anything; send
272 * an empty message on the data sideband just to let the other
273 * side know we're still working on it, but don't have any data
274 * yet.
275 *
276 * If we don't have a sideband channel, there's no room in the
277 * protocol to say anything, so those clients are just out of
278 * luck.
279 */
280 if (!ret && use_sideband) {
281 static const char buf[] = "0005\1";
282 write_or_die(1, buf, 5);
283 }
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700284 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100285
286 if (finish_command(&pack_objects)) {
Junio C Hamano7e44c932008-08-31 09:39:19 -0700287 error("git upload-pack: git-pack-objects died with error.");
Johannes Sixt4c324c02007-11-04 20:46:48 +0100288 goto fail;
289 }
Johannes Sixt4c324c02007-11-04 20:46:48 +0100290
291 /* flush the data */
292 if (0 <= buffered) {
293 data[0] = buffered;
Lukas Fleischerfcf0fe92016-06-14 16:49:17 +0200294 send_client_data(1, data, 1);
Johannes Sixt4c324c02007-11-04 20:46:48 +0100295 fprintf(stderr, "flushed.\n");
296 }
297 if (use_sideband)
298 packet_flush(1);
299 return;
300
Junio C Hamanob1c71b72006-06-20 18:26:34 -0700301 fail:
Junio C Hamano583b7ea2006-06-21 00:30:21 -0700302 send_client_data(3, abort_msg, sizeof(abort_msg));
Junio C Hamano7e44c932008-08-31 09:39:19 -0700303 die("git upload-pack: %s", abort_msg);
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700304}
305
brian m. carlsoncf939822017-05-06 22:10:28 +0000306static int got_oid(const char *hex, struct object_id *oid)
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700307{
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700308 struct object *o;
Junio C Hamano937a5152006-07-05 21:28:20 -0700309 int we_knew_they_have = 0;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700310
brian m. carlsoncf939822017-05-06 22:10:28 +0000311 if (get_oid_hex(hex, oid))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700312 die("git upload-pack: expected SHA1 object, got '%s'", hex);
brian m. carlsoncf939822017-05-06 22:10:28 +0000313 if (!has_object_file(oid))
Junio C Hamano937a5152006-07-05 21:28:20 -0700314 return -1;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700315
Stefan Beller109cd762018-06-28 18:21:51 -0700316 o = parse_object(the_repository, oid);
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700317 if (!o)
brian m. carlsoncf939822017-05-06 22:10:28 +0000318 die("oops (%s)", oid_to_hex(oid));
Junio C Hamano182a8da2006-08-12 22:16:51 -0700319 if (o->type == OBJ_COMMIT) {
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700320 struct commit_list *parents;
Junio C Hamano937a5152006-07-05 21:28:20 -0700321 struct commit *commit = (struct commit *)o;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700322 if (o->flags & THEY_HAVE)
Junio C Hamano937a5152006-07-05 21:28:20 -0700323 we_knew_they_have = 1;
324 else
325 o->flags |= THEY_HAVE;
326 if (!oldest_have || (commit->date < oldest_have))
327 oldest_have = commit->date;
328 for (parents = commit->parents;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700329 parents;
330 parents = parents->next)
331 parents->item->object.flags |= THEY_HAVE;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700332 }
Junio C Hamano937a5152006-07-05 21:28:20 -0700333 if (!we_knew_they_have) {
334 add_object_array(o, NULL, &have_obj);
335 return 1;
336 }
337 return 0;
338}
339
340static int reachable(struct commit *want)
341{
Jeff King5411b102016-10-11 17:20:16 -0400342 struct prio_queue work = { compare_commits_by_commit_date };
Junio C Hamano937a5152006-07-05 21:28:20 -0700343
Jeff King5411b102016-10-11 17:20:16 -0400344 prio_queue_put(&work, want);
345 while (work.nr) {
René Scharfee510ab82015-10-24 18:21:31 +0200346 struct commit_list *list;
Jeff King5411b102016-10-11 17:20:16 -0400347 struct commit *commit = prio_queue_get(&work);
Junio C Hamano937a5152006-07-05 21:28:20 -0700348
349 if (commit->object.flags & THEY_HAVE) {
350 want->object.flags |= COMMON_KNOWN;
351 break;
352 }
353 if (!commit->object.parsed)
Stefan Beller109cd762018-06-28 18:21:51 -0700354 parse_object(the_repository, &commit->object.oid);
Junio C Hamano937a5152006-07-05 21:28:20 -0700355 if (commit->object.flags & REACHABLE)
356 continue;
357 commit->object.flags |= REACHABLE;
358 if (commit->date < oldest_have)
359 continue;
360 for (list = commit->parents; list; list = list->next) {
361 struct commit *parent = list->item;
362 if (!(parent->object.flags & REACHABLE))
Jeff King5411b102016-10-11 17:20:16 -0400363 prio_queue_put(&work, parent);
Junio C Hamano937a5152006-07-05 21:28:20 -0700364 }
365 }
366 want->object.flags |= REACHABLE;
367 clear_commit_marks(want, REACHABLE);
Jeff King5411b102016-10-11 17:20:16 -0400368 clear_prio_queue(&work);
Junio C Hamano937a5152006-07-05 21:28:20 -0700369 return (want->object.flags & COMMON_KNOWN);
370}
371
372static int ok_to_give_up(void)
373{
374 int i;
375
376 if (!have_obj.nr)
377 return 0;
378
379 for (i = 0; i < want_obj.nr; i++) {
380 struct object *want = want_obj.objects[i].item;
381
382 if (want->flags & COMMON_KNOWN)
383 continue;
Stefan Bellera74093d2018-06-28 18:22:05 -0700384 want = deref_tag(the_repository, want, "a want line", 0);
Junio C Hamano937a5152006-07-05 21:28:20 -0700385 if (!want || want->type != OBJ_COMMIT) {
386 /* no way to tell if this is reachable by
387 * looking at the ancestry chain alone, so
388 * leave a note to ourselves not to worry about
389 * this object anymore.
390 */
391 want_obj.objects[i].item->flags |= COMMON_KNOWN;
392 continue;
393 }
394 if (!reachable((struct commit *)want))
395 return 0;
396 }
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700397 return 1;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700398}
399
400static int get_common_commits(void)
401{
brian m. carlsoncf939822017-05-06 22:10:28 +0000402 struct object_id oid;
403 char last_hex[GIT_MAX_HEXSZ + 1];
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700404 int got_common = 0;
405 int got_other = 0;
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700406 int sent_ready = 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700407
Johannes Schindelinf0243f22005-10-28 04:48:32 +0200408 save_commit_buffer = 0;
409
Brian Gianforcaroeeefa7c2009-09-01 01:35:10 -0400410 for (;;) {
Jeff King74543a02013-02-20 15:02:57 -0500411 char *line = packet_read_line(0, NULL);
Nguyễn Thái Ngọc Duy8bf3b752016-06-12 17:53:49 +0700412 const char *arg;
413
H. Peter Anvin960decc2005-10-19 14:27:01 -0700414 reset_timeout();
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700415
Jeff King74543a02013-02-20 15:02:57 -0500416 if (!line) {
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700417 if (multi_ack == 2 && got_common
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700418 && !got_other && ok_to_give_up()) {
419 sent_ready = 1;
Lars Schneider81c634e2016-10-16 16:20:29 -0700420 packet_write_fmt(1, "ACK %s ready\n", last_hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700421 }
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700422 if (have_obj.nr == 0 || multi_ack)
Lars Schneider81c634e2016-10-16 16:20:29 -0700423 packet_write_fmt(1, "NAK\n");
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700424
425 if (no_done && sent_ready) {
Lars Schneider81c634e2016-10-16 16:20:29 -0700426 packet_write_fmt(1, "ACK %s\n", last_hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700427 return 0;
428 }
Shawn O. Pearce42526b42009-10-30 17:47:33 -0700429 if (stateless_rpc)
430 exit(0);
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700431 got_common = 0;
432 got_other = 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700433 continue;
434 }
Nguyễn Thái Ngọc Duy8bf3b752016-06-12 17:53:49 +0700435 if (skip_prefix(line, "have ", &arg)) {
brian m. carlsoncf939822017-05-06 22:10:28 +0000436 switch (got_oid(arg, &oid)) {
Junio C Hamano937a5152006-07-05 21:28:20 -0700437 case -1: /* they have what we do not */
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700438 got_other = 1;
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700439 if (multi_ack && ok_to_give_up()) {
brian m. carlsoncf939822017-05-06 22:10:28 +0000440 const char *hex = oid_to_hex(&oid);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700441 if (multi_ack == 2) {
442 sent_ready = 1;
Lars Schneider81c634e2016-10-16 16:20:29 -0700443 packet_write_fmt(1, "ACK %s ready\n", hex);
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700444 } else
Lars Schneider81c634e2016-10-16 16:20:29 -0700445 packet_write_fmt(1, "ACK %s continue\n", hex);
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700446 }
Junio C Hamano937a5152006-07-05 21:28:20 -0700447 break;
448 default:
Shawn O. Pearce49bee712011-03-14 16:48:39 -0700449 got_common = 1;
brian m. carlson55dc2272018-05-02 00:25:51 +0000450 oid_to_hex_r(last_hex, &oid);
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700451 if (multi_ack == 2)
Lars Schneider81c634e2016-10-16 16:20:29 -0700452 packet_write_fmt(1, "ACK %s common\n", last_hex);
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700453 else if (multi_ack)
Lars Schneider81c634e2016-10-16 16:20:29 -0700454 packet_write_fmt(1, "ACK %s continue\n", last_hex);
Junio C Hamanoc04c4e52006-07-05 18:12:12 -0700455 else if (have_obj.nr == 1)
Lars Schneider81c634e2016-10-16 16:20:29 -0700456 packet_write_fmt(1, "ACK %s\n", last_hex);
Junio C Hamano937a5152006-07-05 21:28:20 -0700457 break;
Junio C Hamanoaf2d3aa2005-10-25 14:55:24 -0700458 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700459 continue;
460 }
461 if (!strcmp(line, "done")) {
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700462 if (have_obj.nr > 0) {
Johannes Schindelin1bd8c8f2005-10-28 04:49:16 +0200463 if (multi_ack)
Lars Schneider81c634e2016-10-16 16:20:29 -0700464 packet_write_fmt(1, "ACK %s\n", last_hex);
Johannes Schindelin1bd8c8f2005-10-28 04:49:16 +0200465 return 0;
466 }
Lars Schneider81c634e2016-10-16 16:20:29 -0700467 packet_write_fmt(1, "NAK\n");
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700468 return -1;
469 }
Junio C Hamano7e44c932008-08-31 09:39:19 -0700470 die("git upload-pack: expected SHA1 list, got '%s'", line);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700471 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700472}
473
Junio C Hamano390eb362013-01-28 21:49:57 -0800474static int is_our_ref(struct object *o)
475{
Fredrik Medley68ee6282015-05-21 22:23:39 +0200476 int allow_hidden_ref = (allow_unadvertised_object_request &
477 (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
Fredrik Medley7199c092015-05-21 22:23:38 +0200478 return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
Junio C Hamano390eb362013-01-28 21:49:57 -0800479}
480
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700481/*
482 * on successful case, it's up to the caller to close cmd->out
483 */
484static int do_reachable_revlist(struct child_process *cmd,
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700485 struct object_array *src,
486 struct object_array *reachable)
Junio C Hamano051e4002011-08-05 13:54:06 -0700487{
488 static const char *argv[] = {
489 "rev-list", "--stdin", NULL,
490 };
Junio C Hamano051e4002011-08-05 13:54:06 -0700491 struct object *o;
brian m. carlson55dc2272018-05-02 00:25:51 +0000492 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
Junio C Hamano051e4002011-08-05 13:54:06 -0700493 int i;
494
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700495 cmd->argv = argv;
496 cmd->git_cmd = 1;
497 cmd->no_stderr = 1;
498 cmd->in = -1;
499 cmd->out = -1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700500
501 /*
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700502 * If the next rev-list --stdin encounters an unknown commit,
503 * it terminates, which will cause SIGPIPE in the write loop
Junio C Hamano051e4002011-08-05 13:54:06 -0700504 * below.
505 */
506 sigchain_push(SIGPIPE, SIG_IGN);
507
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700508 if (start_command(cmd))
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700509 goto error;
510
Junio C Hamano051e4002011-08-05 13:54:06 -0700511 namebuf[0] = '^';
brian m. carlsoncf939822017-05-06 22:10:28 +0000512 namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
Junio C Hamano051e4002011-08-05 13:54:06 -0700513 for (i = get_max_object_index(); 0 < i; ) {
514 o = get_indexed_object(--i);
Brian Harring2a745322011-08-23 22:47:17 -0700515 if (!o)
516 continue;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700517 if (reachable && o->type == OBJ_COMMIT)
518 o->flags &= ~TMP_MARK;
Junio C Hamano390eb362013-01-28 21:49:57 -0800519 if (!is_our_ref(o))
Junio C Hamano051e4002011-08-05 13:54:06 -0700520 continue;
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000521 memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
brian m. carlsoncf939822017-05-06 22:10:28 +0000522 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
Junio C Hamano051e4002011-08-05 13:54:06 -0700523 goto error;
524 }
brian m. carlsoncf939822017-05-06 22:10:28 +0000525 namebuf[GIT_SHA1_HEXSZ] = '\n';
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700526 for (i = 0; i < src->nr; i++) {
527 o = src->objects[i].item;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700528 if (is_our_ref(o)) {
529 if (reachable)
530 add_object_array(o, NULL, reachable);
Junio C Hamano051e4002011-08-05 13:54:06 -0700531 continue;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700532 }
533 if (reachable && o->type == OBJ_COMMIT)
534 o->flags |= TMP_MARK;
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000535 memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
brian m. carlsoncf939822017-05-06 22:10:28 +0000536 if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
Junio C Hamano051e4002011-08-05 13:54:06 -0700537 goto error;
538 }
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700539 close(cmd->in);
540 cmd->in = -1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700541 sigchain_pop(SIGPIPE);
542
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700543 return 0;
544
545error:
546 sigchain_pop(SIGPIPE);
547
548 if (cmd->in >= 0)
549 close(cmd->in);
550 if (cmd->out >= 0)
551 close(cmd->out);
552 return -1;
553}
554
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700555static int get_reachable_list(struct object_array *src,
556 struct object_array *reachable)
557{
558 struct child_process cmd = CHILD_PROCESS_INIT;
559 int i;
560 struct object *o;
brian m. carlson55dc2272018-05-02 00:25:51 +0000561 char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
562 const unsigned hexsz = the_hash_algo->hexsz;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700563
564 if (do_reachable_revlist(&cmd, src, reachable) < 0)
565 return -1;
566
brian m. carlson55dc2272018-05-02 00:25:51 +0000567 while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700568 struct object_id sha1;
brian m. carlson55dc2272018-05-02 00:25:51 +0000569 const char *p;
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700570
brian m. carlson55dc2272018-05-02 00:25:51 +0000571 if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700572 break;
573
Stefan Beller5abddd12018-06-28 18:21:52 -0700574 o = lookup_object(the_repository, sha1.hash);
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700575 if (o && o->type == OBJ_COMMIT) {
576 o->flags &= ~TMP_MARK;
577 }
578 }
579 for (i = get_max_object_index(); 0 < i; i--) {
580 o = get_indexed_object(i - 1);
581 if (o && o->type == OBJ_COMMIT &&
582 (o->flags & TMP_MARK)) {
583 add_object_array(o, NULL, reachable);
584 o->flags &= ~TMP_MARK;
585 }
586 }
587 close(cmd.out);
588
589 if (finish_command(&cmd))
590 return -1;
591
592 return 0;
593}
594
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700595static int has_unreachable(struct object_array *src)
596{
597 struct child_process cmd = CHILD_PROCESS_INIT;
598 char buf[1];
599 int i;
600
Nguyễn Thái Ngọc Duy079aa972016-06-12 17:54:08 +0700601 if (do_reachable_revlist(&cmd, src, NULL) < 0)
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700602 return 1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700603
604 /*
605 * The commits out of the rev-list are not ancestors of
606 * our ref.
607 */
Nguyễn Thái Ngọc Duy29971782016-06-12 17:54:07 +0700608 i = read_in_full(cmd.out, buf, 1);
Junio C Hamano051e4002011-08-05 13:54:06 -0700609 if (i)
610 goto error;
611 close(cmd.out);
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700612 cmd.out = -1;
Junio C Hamano051e4002011-08-05 13:54:06 -0700613
614 /*
615 * rev-list may have died by encountering a bad commit
616 * in the history, in which case we do want to bail out
617 * even when it showed no commit.
618 */
619 if (finish_command(&cmd))
620 goto error;
621
622 /* All the non-tip ones are ancestors of what we advertised */
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700623 return 0;
Junio C Hamano051e4002011-08-05 13:54:06 -0700624
625error:
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700626 sigchain_pop(SIGPIPE);
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700627 if (cmd.out >= 0)
628 close(cmd.out);
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700629 return 1;
630}
Nguyễn Thái Ngọc Duy7fcbd372016-06-12 17:53:51 +0700631
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700632static void check_non_tip(void)
633{
634 int i;
635
636 /*
637 * In the normal in-process case without
638 * uploadpack.allowReachableSHA1InWant,
639 * non-tip requests can never happen.
640 */
641 if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
642 goto error;
643 if (!has_unreachable(&want_obj))
644 /* All the non-tip ones are ancestors of what we advertised */
645 return;
Junio C Hamano051e4002011-08-05 13:54:06 -0700646
647error:
648 /* Pick one of them (we know there at least is one) */
649 for (i = 0; i < want_obj.nr; i++) {
Nguyễn Thái Ngọc Duy3f0f6622016-06-12 17:53:52 +0700650 struct object *o = want_obj.objects[i].item;
Junio C Hamano390eb362013-01-28 21:49:57 -0800651 if (!is_our_ref(o))
Junio C Hamano051e4002011-08-05 13:54:06 -0700652 die("git upload-pack: not our ref %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000653 oid_to_hex(&o->oid));
Junio C Hamano051e4002011-08-05 13:54:06 -0700654 }
655}
656
Nguyễn Thái Ngọc Duy5c24cde2016-06-12 17:53:46 +0700657static void send_shallow(struct commit_list *result)
658{
659 while (result) {
660 struct object *object = &result->item->object;
661 if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
Junio C Hamanodbaa6bd2016-10-31 13:15:21 -0700662 packet_write_fmt(1, "shallow %s",
663 oid_to_hex(&object->oid));
Stefan Beller19143f12018-05-17 15:51:44 -0700664 register_shallow(the_repository, &object->oid);
Nguyễn Thái Ngọc Duy5c24cde2016-06-12 17:53:46 +0700665 shallow_nr++;
666 }
667 result = result->next;
668 }
669}
670
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700671static void send_unshallow(const struct object_array *shallows)
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700672{
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700673 int i;
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700674
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700675 for (i = 0; i < shallows->nr; i++) {
676 struct object *object = shallows->objects[i].item;
677 if (object->flags & NOT_SHALLOW) {
678 struct commit_list *parents;
Junio C Hamanodbaa6bd2016-10-31 13:15:21 -0700679 packet_write_fmt(1, "unshallow %s",
680 oid_to_hex(&object->oid));
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700681 object->flags &= ~CLIENT_SHALLOW;
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700682 /*
683 * We want to _register_ "object" as shallow, but we
684 * also need to traverse object's parents to deepen a
685 * shallow clone. Unregister it for now so we can
686 * parse and add the parents to the want list, then
687 * re-register it.
688 */
brian m. carlsone92b8482017-05-06 22:10:06 +0000689 unregister_shallow(&object->oid);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700690 object->parsed = 0;
691 parse_commit_or_die((struct commit *)object);
692 parents = ((struct commit *)object)->parents;
693 while (parents) {
694 add_object_array(&parents->item->object,
695 NULL, &want_obj);
696 parents = parents->next;
697 }
698 add_object_array(object, NULL, &extra_edge_obj);
699 }
700 /* make sure commit traversal conforms to client */
Stefan Beller19143f12018-05-17 15:51:44 -0700701 register_shallow(the_repository, &object->oid);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700702 }
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700703}
704
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700705static void deepen(int depth, int deepen_relative,
706 struct object_array *shallows)
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700707{
Stefan Bellerc8813482018-05-17 15:51:46 -0700708 if (depth == INFINITE_DEPTH && !is_repository_shallow(the_repository)) {
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700709 int i;
710
711 for (i = 0; i < shallows->nr; i++) {
712 struct object *object = shallows->objects[i].item;
713 object->flags |= NOT_SHALLOW;
714 }
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700715 } else if (deepen_relative) {
716 struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
717 struct commit_list *result;
718
719 get_reachable_list(shallows, &reachable_shallows);
720 result = get_shallow_commits(&reachable_shallows,
721 depth + 1,
722 SHALLOW, NOT_SHALLOW);
723 send_shallow(result);
724 free_commit_list(result);
725 object_array_clear(&reachable_shallows);
Nguyễn Thái Ngọc Duy873700c2016-06-12 17:53:48 +0700726 } else {
727 struct commit_list *result;
728
729 result = get_shallow_commits(&want_obj, depth,
730 SHALLOW, NOT_SHALLOW);
731 send_shallow(result);
732 free_commit_list(result);
733 }
734
735 send_unshallow(shallows);
Nguyễn Thái Ngọc Duye8e44de2016-06-12 17:53:45 +0700736}
737
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700738static void deepen_by_rev_list(int ac, const char **av,
739 struct object_array *shallows)
740{
741 struct commit_list *result;
742
743 result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
744 send_shallow(result);
745 free_commit_list(result);
746 send_unshallow(shallows);
Brandon Williams685fbd32018-03-15 10:31:28 -0700747}
748
749/* Returns 1 if a shallow list is sent or 0 otherwise */
750static int send_shallow_list(int depth, int deepen_rev_list,
751 timestamp_t deepen_since,
752 struct string_list *deepen_not,
753 struct object_array *shallows)
754{
755 int ret = 0;
756
757 if (depth > 0 && deepen_rev_list)
758 die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
759 if (depth > 0) {
760 deepen(depth, deepen_relative, shallows);
761 ret = 1;
762 } else if (deepen_rev_list) {
763 struct argv_array av = ARGV_ARRAY_INIT;
764 int i;
765
766 argv_array_push(&av, "rev-list");
767 if (deepen_since)
768 argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
769 if (deepen_not->nr) {
770 argv_array_push(&av, "--not");
771 for (i = 0; i < deepen_not->nr; i++) {
772 struct string_list_item *s = deepen_not->items + i;
773 argv_array_push(&av, s->string);
774 }
775 argv_array_push(&av, "--not");
776 }
777 for (i = 0; i < want_obj.nr; i++) {
778 struct object *o = want_obj.objects[i].item;
779 argv_array_push(&av, oid_to_hex(&o->oid));
780 }
781 deepen_by_rev_list(av.argc, av.argv, shallows);
782 argv_array_clear(&av);
783 ret = 1;
784 } else {
785 if (shallows->nr > 0) {
786 int i;
787 for (i = 0; i < shallows->nr; i++)
Junio C Hamano00624d62018-07-18 12:20:27 -0700788 register_shallow(the_repository,
789 &shallows->objects[i].item->oid);
Brandon Williams685fbd32018-03-15 10:31:28 -0700790 }
791 }
792
793 shallow_nr += shallows->nr;
794 return ret;
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700795}
796
Brandon Williamsae2948f2018-03-14 11:31:42 -0700797static int process_shallow(const char *line, struct object_array *shallows)
798{
799 const char *arg;
800 if (skip_prefix(line, "shallow ", &arg)) {
801 struct object_id oid;
802 struct object *object;
803 if (get_oid_hex(arg, &oid))
804 die("invalid shallow line: %s", line);
Stefan Beller109cd762018-06-28 18:21:51 -0700805 object = parse_object(the_repository, &oid);
Brandon Williamsae2948f2018-03-14 11:31:42 -0700806 if (!object)
807 return 1;
808 if (object->type != OBJ_COMMIT)
809 die("invalid shallow object %s", oid_to_hex(&oid));
810 if (!(object->flags & CLIENT_SHALLOW)) {
811 object->flags |= CLIENT_SHALLOW;
812 add_object_array(object, NULL, shallows);
813 }
814 return 1;
815 }
816
817 return 0;
818}
819
820static int process_deepen(const char *line, int *depth)
821{
822 const char *arg;
823 if (skip_prefix(line, "deepen ", &arg)) {
824 char *end = NULL;
825 *depth = (int)strtol(arg, &end, 0);
826 if (!end || *end || *depth <= 0)
827 die("Invalid deepen: %s", line);
828 return 1;
829 }
830
831 return 0;
832}
833
834static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
835{
836 const char *arg;
837 if (skip_prefix(line, "deepen-since ", &arg)) {
838 char *end = NULL;
839 *deepen_since = parse_timestamp(arg, &end, 0);
840 if (!end || *end || !deepen_since ||
841 /* revisions.c's max_age -1 is special */
842 *deepen_since == -1)
843 die("Invalid deepen-since: %s", line);
844 *deepen_rev_list = 1;
845 return 1;
846 }
847 return 0;
848}
849
850static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
851{
852 const char *arg;
853 if (skip_prefix(line, "deepen-not ", &arg)) {
854 char *ref = NULL;
855 struct object_id oid;
856 if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
857 die("git upload-pack: ambiguous deepen-not: %s", line);
858 string_list_append(deepen_not, ref);
859 free(ref);
860 *deepen_rev_list = 1;
861 return 1;
862 }
863 return 0;
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700864}
865
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700866static void receive_needs(void)
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700867{
Thiago Farina3cd47452010-08-28 23:04:17 -0300868 struct object_array shallows = OBJECT_ARRAY_INIT;
Nguyễn Thái Ngọc Duy269a7a82016-06-12 17:54:03 +0700869 struct string_list deepen_not = STRING_LIST_INIT_DUP;
Jeff King74543a02013-02-20 15:02:57 -0500870 int depth = 0;
Junio C Hamano051e4002011-08-05 13:54:06 -0700871 int has_non_tip = 0;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200872 timestamp_t deepen_since = 0;
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700873 int deepen_rev_list = 0;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700874
Nick Edelenf0cea832009-06-10 01:50:18 +0200875 shallow_nr = 0;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700876 for (;;) {
Junio C Hamano565ebbf2005-10-24 18:59:18 -0700877 struct object *o;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100878 const char *features;
brian m. carlsoncf939822017-05-06 22:10:28 +0000879 struct object_id oid_buf;
Jeff King74543a02013-02-20 15:02:57 -0500880 char *line = packet_read_line(0, NULL);
Nguyễn Thái Ngọc Duy8bf3b752016-06-12 17:53:49 +0700881 const char *arg;
882
H. Peter Anvin960decc2005-10-19 14:27:01 -0700883 reset_timeout();
Jeff King74543a02013-02-20 15:02:57 -0500884 if (!line)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100885 break;
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700886
Brandon Williamsae2948f2018-03-14 11:31:42 -0700887 if (process_shallow(line, &shallows))
Johannes Schindelined09aef2006-10-30 20:09:06 +0100888 continue;
Brandon Williamsae2948f2018-03-14 11:31:42 -0700889 if (process_deepen(line, &depth))
Johannes Schindelin016e6cc2006-10-30 20:09:29 +0100890 continue;
Brandon Williamsae2948f2018-03-14 11:31:42 -0700891 if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700892 continue;
Brandon Williamsae2948f2018-03-14 11:31:42 -0700893 if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
Nguyễn Thái Ngọc Duy269a7a82016-06-12 17:54:03 +0700894 continue;
Brandon Williamsae2948f2018-03-14 11:31:42 -0700895
Jeff Hostetler10ac85c2017-12-08 15:58:39 +0000896 if (skip_prefix(line, "filter ", &arg)) {
897 if (!filter_capability_requested)
898 die("git upload-pack: filtering capability not negotiated");
899 parse_list_objects_filter(&filter_options, arg);
900 continue;
901 }
Junio C Hamano9bfa0f92018-05-08 15:59:15 +0900902
Nguyễn Thái Ngọc Duy8bf3b752016-06-12 17:53:49 +0700903 if (!skip_prefix(line, "want ", &arg) ||
brian m. carlson55dc2272018-05-02 00:25:51 +0000904 parse_oid_hex(arg, &oid_buf, &features))
Junio C Hamano7e44c932008-08-31 09:39:19 -0700905 die("git upload-pack: protocol error, "
brian m. carlson55dc2272018-05-02 00:25:51 +0000906 "expected to get object ID, not '%s'", line);
Junio C Hamanof47182c2012-01-08 22:06:19 +0100907
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +0700908 if (parse_feature_request(features, "deepen-relative"))
909 deepen_relative = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100910 if (parse_feature_request(features, "multi_ack_detailed"))
Shawn O. Pearce78affc42009-10-30 17:47:25 -0700911 multi_ack = 2;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100912 else if (parse_feature_request(features, "multi_ack"))
Johannes Schindelin1bd8c8f2005-10-28 04:49:16 +0200913 multi_ack = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100914 if (parse_feature_request(features, "no-done"))
Junio C Hamano4e10cf92011-03-29 12:29:10 -0700915 no_done = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100916 if (parse_feature_request(features, "thin-pack"))
Junio C Hamanob19696c2006-02-20 00:38:39 -0800917 use_thin_pack = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100918 if (parse_feature_request(features, "ofs-delta"))
Nicolas Pitree4fe4b82006-09-26 11:27:39 -0400919 use_ofs_delta = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100920 if (parse_feature_request(features, "side-band-64k"))
Junio C Hamanod47f3db2006-09-10 16:27:08 -0700921 use_sideband = LARGE_PACKET_MAX;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100922 else if (parse_feature_request(features, "side-band"))
Junio C Hamanod47f3db2006-09-10 16:27:08 -0700923 use_sideband = DEFAULT_PACKET_MAX;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100924 if (parse_feature_request(features, "no-progress"))
Johannes Schindelinb0e90892007-02-23 20:03:10 +0100925 no_progress = 1;
Junio C Hamanof47182c2012-01-08 22:06:19 +0100926 if (parse_feature_request(features, "include-tag"))
Shawn O. Pearce348e3902008-03-03 22:27:33 -0500927 use_include_tag = 1;
Jonathan Niederc7620bd2018-03-28 13:33:03 -0700928 if (allow_filter && parse_feature_request(features, "filter"))
Jeff Hostetler10ac85c2017-12-08 15:58:39 +0000929 filter_capability_requested = 1;
Junio C Hamano565ebbf2005-10-24 18:59:18 -0700930
Stefan Beller109cd762018-06-28 18:21:51 -0700931 o = parse_object(the_repository, &oid_buf);
Jonathan Tanbdb31ea2017-02-23 10:43:03 -0800932 if (!o) {
933 packet_write_fmt(1,
934 "ERR upload-pack: not our ref %s",
brian m. carlsoncf939822017-05-06 22:10:28 +0000935 oid_to_hex(&oid_buf));
Elijah Newren9f9aa762010-07-31 14:11:46 -0600936 die("git upload-pack: not our ref %s",
brian m. carlsoncf939822017-05-06 22:10:28 +0000937 oid_to_hex(&oid_buf));
Jonathan Tanbdb31ea2017-02-23 10:43:03 -0800938 }
Junio C Hamano565ebbf2005-10-24 18:59:18 -0700939 if (!(o->flags & WANTED)) {
940 o->flags |= WANTED;
David Turnerf8edeaa2016-11-11 12:23:48 -0500941 if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
942 || is_our_ref(o)))
Junio C Hamano051e4002011-08-05 13:54:06 -0700943 has_non_tip = 1;
Junio C Hamanob1e9fff2006-07-05 18:00:02 -0700944 add_object_array(o, NULL, &want_obj);
Junio C Hamano565ebbf2005-10-24 18:59:18 -0700945 }
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700946 }
Johannes Sixt9462e3f2009-06-16 20:41:16 +0200947
Junio C Hamano051e4002011-08-05 13:54:06 -0700948 /*
949 * We have sent all our refs already, and the other end
950 * should have chosen out of them. When we are operating
951 * in the stateless RPC mode, however, their choice may
952 * have been based on the set of older refs advertised
953 * by another process that handled the initial request.
954 */
955 if (has_non_tip)
956 check_non_tip();
957
Johannes Sixt9462e3f2009-06-16 20:41:16 +0200958 if (!use_sideband && daemon_mode)
959 no_progress = 1;
960
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700961 if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100962 return;
Nguyễn Thái Ngọc Duy569e5542016-06-12 17:53:58 +0700963
Brandon Williams685fbd32018-03-15 10:31:28 -0700964 if (send_shallow_list(depth, deepen_rev_list, deepen_since,
965 &deepen_not, &shallows))
966 packet_flush(1);
Martin Ågrendcb572a2017-09-23 01:34:52 +0200967 object_array_clear(&shallows);
Linus Torvaldsfb9040c2005-07-04 15:29:17 -0700968}
969
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800970/* return non-zero if the ref is hidden, otherwise 0 */
Lukas Fleischer78a766a2015-11-03 08:58:16 +0100971static int mark_our_ref(const char *refname, const char *refname_full,
972 const struct object_id *oid)
Junio C Hamanocbbe50d2013-01-18 15:48:49 -0800973{
Michael Haggerty363e98b2015-05-25 18:39:12 +0000974 struct object *o = lookup_unknown_object(oid->hash);
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800975
Lukas Fleischer78a766a2015-11-03 08:58:16 +0100976 if (ref_is_hidden(refname, refname_full)) {
Junio C Hamano390eb362013-01-28 21:49:57 -0800977 o->flags |= HIDDEN_REF;
Junio C Hamanodaebaa72013-01-18 16:08:30 -0800978 return 1;
Junio C Hamano390eb362013-01-28 21:49:57 -0800979 }
Junio C Hamano3f1da572013-01-28 20:45:43 -0800980 o->flags |= OUR_REF;
Junio C Hamanocbbe50d2013-01-18 15:48:49 -0800981 return 0;
982}
983
Lukas Fleischer78a766a2015-11-03 08:58:16 +0100984static int check_ref(const char *refname_full, const struct object_id *oid,
Michael Haggerty363e98b2015-05-25 18:39:12 +0000985 int flag, void *cb_data)
Jeff Kinge1727552015-03-13 00:42:12 -0400986{
Lukas Fleischer78a766a2015-11-03 08:58:16 +0100987 const char *refname = strip_namespace(refname_full);
988
989 mark_our_ref(refname, refname_full, oid);
Jeff Kinge1727552015-03-13 00:42:12 -0400990 return 0;
991}
992
Junio C Hamano7171d8c2013-09-17 16:17:33 -0700993static void format_symref_info(struct strbuf *buf, struct string_list *symref)
994{
995 struct string_list_item *item;
996
997 if (!symref->nr)
998 return;
999 for_each_string_list_item(item, symref)
1000 strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
1001}
1002
Michael Haggerty363e98b2015-05-25 18:39:12 +00001003static int send_ref(const char *refname, const struct object_id *oid,
1004 int flag, void *cb_data)
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001005{
Johannes Schindelined09aef2006-10-30 20:09:06 +01001006 static const char *capabilities = "multi_ack thin-pack side-band"
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001007 " side-band-64k ofs-delta shallow deepen-since deepen-not"
1008 " deepen-relative no-progress include-tag multi_ack_detailed";
Josh Triplett6b01ecf2011-07-08 16:13:32 -07001009 const char *refname_nons = strip_namespace(refname);
Michael Haggerty21758af2015-05-25 18:39:13 +00001010 struct object_id peeled;
Carl Worthb5b16992006-02-17 16:14:52 -08001011
Lukas Fleischer78a766a2015-11-03 08:58:16 +01001012 if (mark_our_ref(refname_nons, refname, oid))
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001013 return 0;
Junio C Hamanocbbe50d2013-01-18 15:48:49 -08001014
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001015 if (capabilities) {
1016 struct strbuf symref_info = STRBUF_INIT;
1017
1018 format_symref_info(&symref_info, cb_data);
Jeff Hostetler10ac85c2017-12-08 15:58:39 +00001019 packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
Michael Haggerty363e98b2015-05-25 18:39:12 +00001020 oid_to_hex(oid), refname_nons,
Junio C Hamanocf2ad8e2011-03-29 10:24:59 -07001021 0, capabilities,
Fredrik Medley7199c092015-05-21 22:23:38 +02001022 (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
1023 " allow-tip-sha1-in-want" : "",
Fredrik Medley68ee6282015-05-21 22:23:39 +02001024 (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
1025 " allow-reachable-sha1-in-want" : "",
Jeff Kingff5effd2012-08-03 12:19:16 -04001026 stateless_rpc ? " no-done" : "",
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001027 symref_info.buf,
Jonathan Niederc7620bd2018-03-28 13:33:03 -07001028 allow_filter ? " filter" : "",
Jeff Kingff5effd2012-08-03 12:19:16 -04001029 git_user_agent_sanitized());
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001030 strbuf_release(&symref_info);
1031 } else {
Lars Schneider81c634e2016-10-16 16:20:29 -07001032 packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001033 }
Johannes Schindelin1f5881b2005-10-28 05:56:41 +02001034 capabilities = NULL;
brian m. carlsonb420d902017-10-15 22:07:02 +00001035 if (!peel_ref(refname, &peeled))
Lars Schneider81c634e2016-10-16 16:20:29 -07001036 packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001037 return 0;
1038}
1039
Michael Haggerty7dabd052015-05-25 18:39:10 +00001040static int find_symref(const char *refname, const struct object_id *oid,
1041 int flag, void *cb_data)
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001042{
1043 const char *symref_target;
1044 struct string_list_item *item;
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001045
1046 if ((flag & REF_ISSYMREF) == 0)
1047 return 0;
René Scharfe744c0402017-09-23 11:45:04 +02001048 symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag);
Junio C Hamano7171d8c2013-09-17 16:17:33 -07001049 if (!symref_target || (flag & REF_ISSYMREF) == 0)
1050 die("'%s' is a symref but it is not?", refname);
1051 item = string_list_append(cb_data, refname);
1052 item->util = xstrdup(symref_target);
1053 return 0;
1054}
1055
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001056static int upload_pack_config(const char *var, const char *value, void *unused)
1057{
Fredrik Medley7199c092015-05-21 22:23:38 +02001058 if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
1059 if (git_config_bool(var, value))
1060 allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
1061 else
1062 allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
Fredrik Medley68ee6282015-05-21 22:23:39 +02001063 } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
1064 if (git_config_bool(var, value))
1065 allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
1066 else
1067 allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
David Turnerf8edeaa2016-11-11 12:23:48 -05001068 } else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
1069 if (git_config_bool(var, value))
1070 allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
1071 else
1072 allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
Fredrik Medley7199c092015-05-21 22:23:38 +02001073 } else if (!strcmp("uploadpack.keepalive", var)) {
Jeff King05e95152013-09-08 05:01:31 -04001074 keepalive = git_config_int(var, value);
1075 if (!keepalive)
1076 keepalive = -1;
Jeff King20b20a22016-05-18 18:45:37 -04001077 } else if (current_config_scope() != CONFIG_SCOPE_REPO) {
1078 if (!strcmp("uploadpack.packobjectshook", var))
1079 return git_config_string(&pack_objects_hook, var, value);
Jeff Hostetler10ac85c2017-12-08 15:58:39 +00001080 } else if (!strcmp("uploadpack.allowfilter", var)) {
Jonathan Niederc7620bd2018-03-28 13:33:03 -07001081 allow_filter = git_config_bool(var, value);
Brandon Williams516e2b72018-06-27 15:30:17 -07001082 } else if (!strcmp("uploadpack.allowrefinwant", var)) {
1083 allow_ref_in_want = git_config_bool(var, value);
Jeff King05e95152013-09-08 05:01:31 -04001084 }
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001085 return parse_hide_refs_config(var, value, "uploadpack");
1086}
1087
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001088void upload_pack(struct upload_pack_options *options)
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001089{
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001090 struct string_list symref = STRING_LIST_INIT_DUP;
H. Peter Anvin960decc2005-10-19 14:27:01 -07001091
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001092 stateless_rpc = options->stateless_rpc;
1093 timeout = options->timeout;
1094 daemon_mode = options->daemon_mode;
Nguyễn Thái Ngọc Duyad491362013-12-05 20:02:32 +07001095
Junio C Hamanodaebaa72013-01-18 16:08:30 -08001096 git_config(upload_pack_config, NULL);
Brandon Williamsaa9bab22017-10-16 10:55:26 -07001097
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001098 head_ref_namespaced(find_symref, &symref);
Brandon Williamsaa9bab22017-10-16 10:55:26 -07001099
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001100 if (options->advertise_refs || !stateless_rpc) {
1101 reset_timeout();
1102 head_ref_namespaced(send_ref, &symref);
1103 for_each_namespaced_ref(send_ref, &symref);
1104 advertise_shallow_grafts(1);
1105 packet_flush(1);
1106 } else {
1107 head_ref_namespaced(check_ref, NULL);
1108 for_each_namespaced_ref(check_ref, NULL);
Brandon Williamsaa9bab22017-10-16 10:55:26 -07001109 }
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001110 string_list_clear(&symref, 1);
1111 if (options->advertise_refs)
1112 return;
Brandon Williamsaa9bab22017-10-16 10:55:26 -07001113
Brandon Williamsa3d6b532018-03-14 11:31:41 -07001114 receive_needs();
1115 if (want_obj.nr) {
1116 get_common_commits();
1117 create_pack_file();
1118 }
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001119}
Brandon Williams3145ea92018-03-15 10:31:27 -07001120
1121struct upload_pack_data {
1122 struct object_array wants;
Brandon Williams516e2b72018-06-27 15:30:17 -07001123 struct string_list wanted_refs;
Brandon Williams3145ea92018-03-15 10:31:27 -07001124 struct oid_array haves;
1125
Brandon Williams685fbd32018-03-15 10:31:28 -07001126 struct object_array shallows;
1127 struct string_list deepen_not;
1128 int depth;
1129 timestamp_t deepen_since;
1130 int deepen_rev_list;
1131 int deepen_relative;
1132
Brandon Williams3145ea92018-03-15 10:31:27 -07001133 unsigned stateless_rpc : 1;
1134
1135 unsigned use_thin_pack : 1;
1136 unsigned use_ofs_delta : 1;
1137 unsigned no_progress : 1;
1138 unsigned use_include_tag : 1;
1139 unsigned done : 1;
1140};
1141
1142static void upload_pack_data_init(struct upload_pack_data *data)
1143{
1144 struct object_array wants = OBJECT_ARRAY_INIT;
Brandon Williams516e2b72018-06-27 15:30:17 -07001145 struct string_list wanted_refs = STRING_LIST_INIT_DUP;
Brandon Williams3145ea92018-03-15 10:31:27 -07001146 struct oid_array haves = OID_ARRAY_INIT;
Brandon Williams685fbd32018-03-15 10:31:28 -07001147 struct object_array shallows = OBJECT_ARRAY_INIT;
1148 struct string_list deepen_not = STRING_LIST_INIT_DUP;
Brandon Williams3145ea92018-03-15 10:31:27 -07001149
1150 memset(data, 0, sizeof(*data));
1151 data->wants = wants;
Brandon Williams516e2b72018-06-27 15:30:17 -07001152 data->wanted_refs = wanted_refs;
Brandon Williams3145ea92018-03-15 10:31:27 -07001153 data->haves = haves;
Brandon Williams685fbd32018-03-15 10:31:28 -07001154 data->shallows = shallows;
1155 data->deepen_not = deepen_not;
Brandon Williams3145ea92018-03-15 10:31:27 -07001156}
1157
1158static void upload_pack_data_clear(struct upload_pack_data *data)
1159{
1160 object_array_clear(&data->wants);
Brandon Williams516e2b72018-06-27 15:30:17 -07001161 string_list_clear(&data->wanted_refs, 1);
Brandon Williams3145ea92018-03-15 10:31:27 -07001162 oid_array_clear(&data->haves);
Brandon Williams685fbd32018-03-15 10:31:28 -07001163 object_array_clear(&data->shallows);
1164 string_list_clear(&data->deepen_not, 0);
Brandon Williams3145ea92018-03-15 10:31:27 -07001165}
1166
1167static int parse_want(const char *line)
1168{
1169 const char *arg;
1170 if (skip_prefix(line, "want ", &arg)) {
1171 struct object_id oid;
1172 struct object *o;
1173
1174 if (get_oid_hex(arg, &oid))
1175 die("git upload-pack: protocol error, "
1176 "expected to get oid, not '%s'", line);
1177
Stefan Beller109cd762018-06-28 18:21:51 -07001178 o = parse_object(the_repository, &oid);
Brandon Williams3145ea92018-03-15 10:31:27 -07001179 if (!o) {
1180 packet_write_fmt(1,
1181 "ERR upload-pack: not our ref %s",
1182 oid_to_hex(&oid));
1183 die("git upload-pack: not our ref %s",
1184 oid_to_hex(&oid));
1185 }
1186
1187 if (!(o->flags & WANTED)) {
1188 o->flags |= WANTED;
1189 add_object_array(o, NULL, &want_obj);
1190 }
1191
1192 return 1;
Linus Torvaldsdef88e92005-07-04 13:26:53 -07001193 }
1194
1195 return 0;
1196}
Brandon Williams3145ea92018-03-15 10:31:27 -07001197
Brandon Williams516e2b72018-06-27 15:30:17 -07001198static int parse_want_ref(const char *line, struct string_list *wanted_refs)
1199{
1200 const char *arg;
1201 if (skip_prefix(line, "want-ref ", &arg)) {
1202 struct object_id oid;
1203 struct string_list_item *item;
1204 struct object *o;
1205
1206 if (read_ref(arg, &oid)) {
1207 packet_write_fmt(1, "ERR unknown ref %s", arg);
1208 die("unknown ref %s", arg);
1209 }
1210
1211 item = string_list_append(wanted_refs, arg);
1212 item->util = oiddup(&oid);
1213
1214 o = parse_object_or_die(&oid, arg);
1215 if (!(o->flags & WANTED)) {
1216 o->flags |= WANTED;
1217 add_object_array(o, NULL, &want_obj);
1218 }
1219
1220 return 1;
1221 }
1222
1223 return 0;
1224}
1225
Brandon Williams3145ea92018-03-15 10:31:27 -07001226static int parse_have(const char *line, struct oid_array *haves)
1227{
1228 const char *arg;
1229 if (skip_prefix(line, "have ", &arg)) {
1230 struct object_id oid;
1231
1232 if (get_oid_hex(arg, &oid))
1233 die("git upload-pack: expected SHA1 object, got '%s'", arg);
1234 oid_array_append(haves, &oid);
1235 return 1;
1236 }
1237
1238 return 0;
1239}
1240
1241static void process_args(struct packet_reader *request,
1242 struct upload_pack_data *data)
1243{
1244 while (packet_reader_read(request) != PACKET_READ_FLUSH) {
1245 const char *arg = request->line;
Jonathan Tanba957102018-05-03 16:46:56 -07001246 const char *p;
Brandon Williams3145ea92018-03-15 10:31:27 -07001247
1248 /* process want */
1249 if (parse_want(arg))
1250 continue;
Brandon Williams516e2b72018-06-27 15:30:17 -07001251 if (allow_ref_in_want && parse_want_ref(arg, &data->wanted_refs))
1252 continue;
Brandon Williams3145ea92018-03-15 10:31:27 -07001253 /* process have line */
1254 if (parse_have(arg, &data->haves))
1255 continue;
1256
1257 /* process args like thin-pack */
1258 if (!strcmp(arg, "thin-pack")) {
1259 use_thin_pack = 1;
1260 continue;
1261 }
1262 if (!strcmp(arg, "ofs-delta")) {
1263 use_ofs_delta = 1;
1264 continue;
1265 }
1266 if (!strcmp(arg, "no-progress")) {
1267 no_progress = 1;
1268 continue;
1269 }
1270 if (!strcmp(arg, "include-tag")) {
1271 use_include_tag = 1;
1272 continue;
1273 }
1274 if (!strcmp(arg, "done")) {
1275 data->done = 1;
1276 continue;
1277 }
1278
Brandon Williams685fbd32018-03-15 10:31:28 -07001279 /* Shallow related arguments */
1280 if (process_shallow(arg, &data->shallows))
1281 continue;
1282 if (process_deepen(arg, &data->depth))
1283 continue;
1284 if (process_deepen_since(arg, &data->deepen_since,
1285 &data->deepen_rev_list))
1286 continue;
1287 if (process_deepen_not(arg, &data->deepen_not,
1288 &data->deepen_rev_list))
1289 continue;
1290 if (!strcmp(arg, "deepen-relative")) {
1291 data->deepen_relative = 1;
1292 continue;
1293 }
1294
Jonathan Tanba957102018-05-03 16:46:56 -07001295 if (allow_filter && skip_prefix(arg, "filter ", &p)) {
1296 parse_list_objects_filter(&filter_options, p);
1297 continue;
1298 }
1299
Brandon Williams3145ea92018-03-15 10:31:27 -07001300 /* ignore unknown lines maybe? */
Jonathan Tan7cc6ed22018-05-01 17:31:29 -07001301 die("unexpected line: '%s'", arg);
Brandon Williams3145ea92018-03-15 10:31:27 -07001302 }
1303}
1304
1305static int process_haves(struct oid_array *haves, struct oid_array *common)
1306{
1307 int i;
1308
1309 /* Process haves */
1310 for (i = 0; i < haves->nr; i++) {
1311 const struct object_id *oid = &haves->oid[i];
1312 struct object *o;
1313 int we_knew_they_have = 0;
1314
1315 if (!has_object_file(oid))
1316 continue;
1317
1318 oid_array_append(common, oid);
1319
Stefan Beller109cd762018-06-28 18:21:51 -07001320 o = parse_object(the_repository, oid);
Brandon Williams3145ea92018-03-15 10:31:27 -07001321 if (!o)
1322 die("oops (%s)", oid_to_hex(oid));
1323 if (o->type == OBJ_COMMIT) {
1324 struct commit_list *parents;
1325 struct commit *commit = (struct commit *)o;
1326 if (o->flags & THEY_HAVE)
1327 we_knew_they_have = 1;
1328 else
1329 o->flags |= THEY_HAVE;
1330 if (!oldest_have || (commit->date < oldest_have))
1331 oldest_have = commit->date;
1332 for (parents = commit->parents;
1333 parents;
1334 parents = parents->next)
1335 parents->item->object.flags |= THEY_HAVE;
1336 }
1337 if (!we_knew_they_have)
1338 add_object_array(o, NULL, &have_obj);
1339 }
1340
1341 return 0;
1342}
1343
1344static int send_acks(struct oid_array *acks, struct strbuf *response)
1345{
1346 int i;
1347
1348 packet_buf_write(response, "acknowledgments\n");
1349
1350 /* Send Acks */
1351 if (!acks->nr)
1352 packet_buf_write(response, "NAK\n");
1353
1354 for (i = 0; i < acks->nr; i++) {
1355 packet_buf_write(response, "ACK %s\n",
1356 oid_to_hex(&acks->oid[i]));
1357 }
1358
1359 if (ok_to_give_up()) {
1360 /* Send Ready */
1361 packet_buf_write(response, "ready\n");
1362 return 1;
1363 }
1364
1365 return 0;
1366}
1367
1368static int process_haves_and_send_acks(struct upload_pack_data *data)
1369{
1370 struct oid_array common = OID_ARRAY_INIT;
1371 struct strbuf response = STRBUF_INIT;
1372 int ret = 0;
1373
1374 process_haves(&data->haves, &common);
1375 if (data->done) {
1376 ret = 1;
1377 } else if (send_acks(&common, &response)) {
1378 packet_buf_delim(&response);
1379 ret = 1;
1380 } else {
1381 /* Add Flush */
1382 packet_buf_flush(&response);
1383 ret = 0;
1384 }
1385
1386 /* Send response */
1387 write_or_die(1, response.buf, response.len);
1388 strbuf_release(&response);
1389
1390 oid_array_clear(&data->haves);
1391 oid_array_clear(&common);
1392 return ret;
1393}
1394
Brandon Williams516e2b72018-06-27 15:30:17 -07001395static void send_wanted_ref_info(struct upload_pack_data *data)
1396{
1397 const struct string_list_item *item;
1398
1399 if (!data->wanted_refs.nr)
1400 return;
1401
1402 packet_write_fmt(1, "wanted-refs\n");
1403
1404 for_each_string_list_item(item, &data->wanted_refs) {
1405 packet_write_fmt(1, "%s %s\n",
1406 oid_to_hex(item->util),
1407 item->string);
1408 }
1409
1410 packet_delim(1);
1411}
1412
Brandon Williams685fbd32018-03-15 10:31:28 -07001413static void send_shallow_info(struct upload_pack_data *data)
1414{
1415 /* No shallow info needs to be sent */
1416 if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
Junio C Hamano00624d62018-07-18 12:20:27 -07001417 !is_repository_shallow(the_repository))
Brandon Williams685fbd32018-03-15 10:31:28 -07001418 return;
1419
1420 packet_write_fmt(1, "shallow-info\n");
1421
1422 if (!send_shallow_list(data->depth, data->deepen_rev_list,
1423 data->deepen_since, &data->deepen_not,
Junio C Hamano00624d62018-07-18 12:20:27 -07001424 &data->shallows) &&
1425 is_repository_shallow(the_repository))
Brandon Williams685fbd32018-03-15 10:31:28 -07001426 deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
1427
1428 packet_delim(1);
1429}
1430
Brandon Williams3145ea92018-03-15 10:31:27 -07001431enum fetch_state {
1432 FETCH_PROCESS_ARGS = 0,
1433 FETCH_SEND_ACKS,
1434 FETCH_SEND_PACK,
1435 FETCH_DONE,
1436};
1437
1438int upload_pack_v2(struct repository *r, struct argv_array *keys,
1439 struct packet_reader *request)
1440{
1441 enum fetch_state state = FETCH_PROCESS_ARGS;
1442 struct upload_pack_data data;
1443
Jonathan Tan54592682018-05-03 16:46:55 -07001444 git_config(upload_pack_config, NULL);
1445
Brandon Williams3145ea92018-03-15 10:31:27 -07001446 upload_pack_data_init(&data);
1447 use_sideband = LARGE_PACKET_MAX;
1448
1449 while (state != FETCH_DONE) {
1450 switch (state) {
1451 case FETCH_PROCESS_ARGS:
1452 process_args(request, &data);
1453
1454 if (!want_obj.nr) {
1455 /*
1456 * Request didn't contain any 'want' lines,
1457 * guess they didn't want anything.
1458 */
1459 state = FETCH_DONE;
1460 } else if (data.haves.nr) {
1461 /*
1462 * Request had 'have' lines, so lets ACK them.
1463 */
1464 state = FETCH_SEND_ACKS;
1465 } else {
1466 /*
1467 * Request had 'want's but no 'have's so we can
1468 * immedietly go to construct and send a pack.
1469 */
1470 state = FETCH_SEND_PACK;
1471 }
1472 break;
1473 case FETCH_SEND_ACKS:
1474 if (process_haves_and_send_acks(&data))
1475 state = FETCH_SEND_PACK;
1476 else
1477 state = FETCH_DONE;
1478 break;
1479 case FETCH_SEND_PACK:
Brandon Williams516e2b72018-06-27 15:30:17 -07001480 send_wanted_ref_info(&data);
Brandon Williams685fbd32018-03-15 10:31:28 -07001481 send_shallow_info(&data);
1482
Brandon Williams3145ea92018-03-15 10:31:27 -07001483 packet_write_fmt(1, "packfile\n");
1484 create_pack_file();
1485 state = FETCH_DONE;
1486 break;
1487 case FETCH_DONE:
1488 continue;
1489 }
1490 }
1491
1492 upload_pack_data_clear(&data);
1493 return 0;
1494}
Brandon Williams685fbd32018-03-15 10:31:28 -07001495
1496int upload_pack_advertise(struct repository *r,
1497 struct strbuf *value)
1498{
Jonathan Tanba957102018-05-03 16:46:56 -07001499 if (value) {
1500 int allow_filter_value;
Brandon Williams516e2b72018-06-27 15:30:17 -07001501 int allow_ref_in_want;
1502
Brandon Williams685fbd32018-03-15 10:31:28 -07001503 strbuf_addstr(value, "shallow");
Brandon Williams516e2b72018-06-27 15:30:17 -07001504
Jonathan Tanba957102018-05-03 16:46:56 -07001505 if (!repo_config_get_bool(the_repository,
1506 "uploadpack.allowfilter",
1507 &allow_filter_value) &&
1508 allow_filter_value)
1509 strbuf_addstr(value, " filter");
Brandon Williams516e2b72018-06-27 15:30:17 -07001510
1511 if (!repo_config_get_bool(the_repository,
1512 "uploadpack.allowrefinwant",
1513 &allow_ref_in_want) &&
1514 allow_ref_in_want)
1515 strbuf_addstr(value, " ref-in-want");
Jonathan Tanba957102018-05-03 16:46:56 -07001516 }
Brandon Williams516e2b72018-06-27 15:30:17 -07001517
Brandon Williams685fbd32018-03-15 10:31:28 -07001518 return 1;
1519}