Blame upload-pack.c

Packit 4511e4
#include "cache.h"
Packit 4511e4
#include "config.h"
Packit 4511e4
#include "refs.h"
Packit 4511e4
#include "pkt-line.h"
Packit 4511e4
#include "sideband.h"
Packit 4511e4
#include "tag.h"
Packit 4511e4
#include "object.h"
Packit 4511e4
#include "commit.h"
Packit 4511e4
#include "diff.h"
Packit 4511e4
#include "revision.h"
Packit 4511e4
#include "list-objects.h"
Packit 4511e4
#include "list-objects-filter.h"
Packit 4511e4
#include "list-objects-filter-options.h"
Packit 4511e4
#include "run-command.h"
Packit 4511e4
#include "connect.h"
Packit 4511e4
#include "sigchain.h"
Packit 4511e4
#include "version.h"
Packit 4511e4
#include "string-list.h"
Packit 4511e4
#include "argv-array.h"
Packit 4511e4
#include "prio-queue.h"
Packit 4511e4
#include "protocol.h"
Packit 4511e4
#include "quote.h"
Packit 4511e4
#include "upload-pack.h"
Packit 4511e4
#include "serve.h"
Packit 4511e4
Packit 4511e4
/* Remember to update object flag allocation in object.h */
Packit 4511e4
#define THEY_HAVE	(1u << 11)
Packit 4511e4
#define OUR_REF		(1u << 12)
Packit 4511e4
#define WANTED		(1u << 13)
Packit 4511e4
#define COMMON_KNOWN	(1u << 14)
Packit 4511e4
#define REACHABLE	(1u << 15)
Packit 4511e4
Packit 4511e4
#define SHALLOW		(1u << 16)
Packit 4511e4
#define NOT_SHALLOW	(1u << 17)
Packit 4511e4
#define CLIENT_SHALLOW	(1u << 18)
Packit 4511e4
#define HIDDEN_REF	(1u << 19)
Packit 4511e4
Packit 4511e4
static timestamp_t oldest_have;
Packit 4511e4
Packit 4511e4
static int deepen_relative;
Packit 4511e4
static int multi_ack;
Packit 4511e4
static int no_done;
Packit 4511e4
static int use_thin_pack, use_ofs_delta, use_include_tag;
Packit 4511e4
static int no_progress, daemon_mode;
Packit 4511e4
/* Allow specifying sha1 if it is a ref tip. */
Packit 4511e4
#define ALLOW_TIP_SHA1	01
Packit 4511e4
/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */
Packit 4511e4
#define ALLOW_REACHABLE_SHA1	02
Packit 4511e4
/* Allow request of any sha1. Implies ALLOW_TIP_SHA1 and ALLOW_REACHABLE_SHA1. */
Packit 4511e4
#define ALLOW_ANY_SHA1	07
Packit 4511e4
static unsigned int allow_unadvertised_object_request;
Packit 4511e4
static int shallow_nr;
Packit 4511e4
static struct object_array have_obj;
Packit 4511e4
static struct object_array want_obj;
Packit 4511e4
static struct object_array extra_edge_obj;
Packit 4511e4
static unsigned int timeout;
Packit 4511e4
static int keepalive = 5;
Packit 4511e4
/* 0 for no sideband,
Packit 4511e4
 * otherwise maximum packet size (up to 65520 bytes).
Packit 4511e4
 */
Packit 4511e4
static int use_sideband;
Packit 4511e4
static int stateless_rpc;
Packit 4511e4
static const char *pack_objects_hook;
Packit 4511e4
Packit 4511e4
static int filter_capability_requested;
Packit 4511e4
static int allow_filter;
Packit 4511e4
static struct list_objects_filter_options filter_options;
Packit 4511e4
Packit 4511e4
static void reset_timeout(void)
Packit 4511e4
{
Packit 4511e4
	alarm(timeout);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void send_client_data(int fd, const char *data, ssize_t sz)
Packit 4511e4
{
Packit 4511e4
	if (use_sideband) {
Packit 4511e4
		send_sideband(1, fd, data, sz, use_sideband);
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
	if (fd == 3)
Packit 4511e4
		/* emergency quit */
Packit 4511e4
		fd = 2;
Packit 4511e4
	if (fd == 2) {
Packit 4511e4
		/* XXX: are we happy to lose stuff here? */
Packit 4511e4
		xwrite(fd, data, sz);
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
	write_or_die(fd, data, sz);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
Packit 4511e4
{
Packit 4511e4
	FILE *fp = cb_data;
Packit 4511e4
	if (graft->nr_parent == -1)
Packit 4511e4
		fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void create_pack_file(void)
Packit 4511e4
{
Packit 4511e4
	struct child_process pack_objects = CHILD_PROCESS_INIT;
Packit 4511e4
	char data[8193], progress[128];
Packit 4511e4
	char abort_msg[] = "aborting due to possible repository "
Packit 4511e4
		"corruption on the remote side.";
Packit 4511e4
	int buffered = -1;
Packit 4511e4
	ssize_t sz;
Packit 4511e4
	int i;
Packit 4511e4
	FILE *pipe_fd;
Packit 4511e4
Packit 4511e4
	if (!pack_objects_hook)
Packit 4511e4
		pack_objects.git_cmd = 1;
Packit 4511e4
	else {
Packit 4511e4
		argv_array_push(&pack_objects.args, pack_objects_hook);
Packit 4511e4
		argv_array_push(&pack_objects.args, "git");
Packit 4511e4
		pack_objects.use_shell = 1;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	if (shallow_nr) {
Packit 4511e4
		argv_array_push(&pack_objects.args, "--shallow-file");
Packit 4511e4
		argv_array_push(&pack_objects.args, "");
Packit 4511e4
	}
Packit 4511e4
	argv_array_push(&pack_objects.args, "pack-objects");
Packit 4511e4
	argv_array_push(&pack_objects.args, "--revs");
Packit 4511e4
	if (use_thin_pack)
Packit 4511e4
		argv_array_push(&pack_objects.args, "--thin");
Packit 4511e4
Packit 4511e4
	argv_array_push(&pack_objects.args, "--stdout");
Packit 4511e4
	if (shallow_nr)
Packit 4511e4
		argv_array_push(&pack_objects.args, "--shallow");
Packit 4511e4
	if (!no_progress)
Packit 4511e4
		argv_array_push(&pack_objects.args, "--progress");
Packit 4511e4
	if (use_ofs_delta)
Packit 4511e4
		argv_array_push(&pack_objects.args, "--delta-base-offset");
Packit 4511e4
	if (use_include_tag)
Packit 4511e4
		argv_array_push(&pack_objects.args, "--include-tag");
Packit 4511e4
	if (filter_options.filter_spec) {
Packit 4511e4
		if (pack_objects.use_shell) {
Packit 4511e4
			struct strbuf buf = STRBUF_INIT;
Packit 4511e4
			sq_quote_buf(&buf, filter_options.filter_spec);
Packit 4511e4
			argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
Packit 4511e4
			strbuf_release(&buf;;
Packit 4511e4
		} else {
Packit 4511e4
			argv_array_pushf(&pack_objects.args, "--filter=%s",
Packit 4511e4
					 filter_options.filter_spec);
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	pack_objects.in = -1;
Packit 4511e4
	pack_objects.out = -1;
Packit 4511e4
	pack_objects.err = -1;
Packit 4511e4
Packit 4511e4
	if (start_command(&pack_objects))
Packit 4511e4
		die("git upload-pack: unable to fork git-pack-objects");
Packit 4511e4
Packit 4511e4
	pipe_fd = xfdopen(pack_objects.in, "w");
Packit 4511e4
Packit 4511e4
	if (shallow_nr)
Packit 4511e4
		for_each_commit_graft(write_one_shallow, pipe_fd);
Packit 4511e4
Packit 4511e4
	for (i = 0; i < want_obj.nr; i++)
Packit 4511e4
		fprintf(pipe_fd, "%s\n",
Packit 4511e4
			oid_to_hex(&want_obj.objects[i].item->oid));
Packit 4511e4
	fprintf(pipe_fd, "--not\n");
Packit 4511e4
	for (i = 0; i < have_obj.nr; i++)
Packit 4511e4
		fprintf(pipe_fd, "%s\n",
Packit 4511e4
			oid_to_hex(&have_obj.objects[i].item->oid));
Packit 4511e4
	for (i = 0; i < extra_edge_obj.nr; i++)
Packit 4511e4
		fprintf(pipe_fd, "%s\n",
Packit 4511e4
			oid_to_hex(&extra_edge_obj.objects[i].item->oid));
Packit 4511e4
	fprintf(pipe_fd, "\n");
Packit 4511e4
	fflush(pipe_fd);
Packit 4511e4
	fclose(pipe_fd);
Packit 4511e4
Packit 4511e4
	/* We read from pack_objects.err to capture stderr output for
Packit 4511e4
	 * progress bar, and pack_objects.out to capture the pack data.
Packit 4511e4
	 */
Packit 4511e4
Packit 4511e4
	while (1) {
Packit 4511e4
		struct pollfd pfd[2];
Packit 4511e4
		int pe, pu, pollsize;
Packit 4511e4
		int ret;
Packit 4511e4
Packit 4511e4
		reset_timeout();
Packit 4511e4
Packit 4511e4
		pollsize = 0;
Packit 4511e4
		pe = pu = -1;
Packit 4511e4
Packit 4511e4
		if (0 <= pack_objects.out) {
Packit 4511e4
			pfd[pollsize].fd = pack_objects.out;
Packit 4511e4
			pfd[pollsize].events = POLLIN;
Packit 4511e4
			pu = pollsize;
Packit 4511e4
			pollsize++;
Packit 4511e4
		}
Packit 4511e4
		if (0 <= pack_objects.err) {
Packit 4511e4
			pfd[pollsize].fd = pack_objects.err;
Packit 4511e4
			pfd[pollsize].events = POLLIN;
Packit 4511e4
			pe = pollsize;
Packit 4511e4
			pollsize++;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		if (!pollsize)
Packit 4511e4
			break;
Packit 4511e4
Packit 4511e4
		ret = poll(pfd, pollsize,
Packit 4511e4
			keepalive < 0 ? -1 : 1000 * keepalive);
Packit 4511e4
Packit 4511e4
		if (ret < 0) {
Packit 4511e4
			if (errno != EINTR) {
Packit 4511e4
				error_errno("poll failed, resuming");
Packit 4511e4
				sleep(1);
Packit 4511e4
			}
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (0 <= pe && (pfd[pe].revents & (POLLIN|POLLHUP))) {
Packit 4511e4
			/* Status ready; we ship that in the side-band
Packit 4511e4
			 * or dump to the standard error.
Packit 4511e4
			 */
Packit 4511e4
			sz = xread(pack_objects.err, progress,
Packit 4511e4
				  sizeof(progress));
Packit 4511e4
			if (0 < sz)
Packit 4511e4
				send_client_data(2, progress, sz);
Packit 4511e4
			else if (sz == 0) {
Packit 4511e4
				close(pack_objects.err);
Packit 4511e4
				pack_objects.err = -1;
Packit 4511e4
			}
Packit 4511e4
			else
Packit 4511e4
				goto fail;
Packit 4511e4
			/* give priority to status messages */
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
Packit 4511e4
			/* Data ready; we keep the last byte to ourselves
Packit 4511e4
			 * in case we detect broken rev-list, so that we
Packit 4511e4
			 * can leave the stream corrupted.  This is
Packit 4511e4
			 * unfortunate -- unpack-objects would happily
Packit 4511e4
			 * accept a valid packdata with trailing garbage,
Packit 4511e4
			 * so appending garbage after we pass all the
Packit 4511e4
			 * pack data is not good enough to signal
Packit 4511e4
			 * breakage to downstream.
Packit 4511e4
			 */
Packit 4511e4
			char *cp = data;
Packit 4511e4
			ssize_t outsz = 0;
Packit 4511e4
			if (0 <= buffered) {
Packit 4511e4
				*cp++ = buffered;
Packit 4511e4
				outsz++;
Packit 4511e4
			}
Packit 4511e4
			sz = xread(pack_objects.out, cp,
Packit 4511e4
				  sizeof(data) - outsz);
Packit 4511e4
			if (0 < sz)
Packit 4511e4
				;
Packit 4511e4
			else if (sz == 0) {
Packit 4511e4
				close(pack_objects.out);
Packit 4511e4
				pack_objects.out = -1;
Packit 4511e4
			}
Packit 4511e4
			else
Packit 4511e4
				goto fail;
Packit 4511e4
			sz += outsz;
Packit 4511e4
			if (1 < sz) {
Packit 4511e4
				buffered = data[sz-1] & 0xFF;
Packit 4511e4
				sz--;
Packit 4511e4
			}
Packit 4511e4
			else
Packit 4511e4
				buffered = -1;
Packit 4511e4
			send_client_data(1, data, sz);
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		/*
Packit 4511e4
		 * We hit the keepalive timeout without saying anything; send
Packit 4511e4
		 * an empty message on the data sideband just to let the other
Packit 4511e4
		 * side know we're still working on it, but don't have any data
Packit 4511e4
		 * yet.
Packit 4511e4
		 *
Packit 4511e4
		 * If we don't have a sideband channel, there's no room in the
Packit 4511e4
		 * protocol to say anything, so those clients are just out of
Packit 4511e4
		 * luck.
Packit 4511e4
		 */
Packit 4511e4
		if (!ret && use_sideband) {
Packit 4511e4
			static const char buf[] = "0005\1";
Packit 4511e4
			write_or_die(1, buf, 5);
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	if (finish_command(&pack_objects)) {
Packit 4511e4
		error("git upload-pack: git-pack-objects died with error.");
Packit 4511e4
		goto fail;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	/* flush the data */
Packit 4511e4
	if (0 <= buffered) {
Packit 4511e4
		data[0] = buffered;
Packit 4511e4
		send_client_data(1, data, 1);
Packit 4511e4
		fprintf(stderr, "flushed.\n");
Packit 4511e4
	}
Packit 4511e4
	if (use_sideband)
Packit 4511e4
		packet_flush(1);
Packit 4511e4
	return;
Packit 4511e4
Packit 4511e4
 fail:
Packit 4511e4
	send_client_data(3, abort_msg, sizeof(abort_msg));
Packit 4511e4
	die("git upload-pack: %s", abort_msg);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int got_oid(const char *hex, struct object_id *oid)
Packit 4511e4
{
Packit 4511e4
	struct object *o;
Packit 4511e4
	int we_knew_they_have = 0;
Packit 4511e4
Packit 4511e4
	if (get_oid_hex(hex, oid))
Packit 4511e4
		die("git upload-pack: expected SHA1 object, got '%s'", hex);
Packit 4511e4
	if (!has_object_file(oid))
Packit 4511e4
		return -1;
Packit 4511e4
Packit 4511e4
	o = parse_object(oid);
Packit 4511e4
	if (!o)
Packit 4511e4
		die("oops (%s)", oid_to_hex(oid));
Packit 4511e4
	if (o->type == OBJ_COMMIT) {
Packit 4511e4
		struct commit_list *parents;
Packit 4511e4
		struct commit *commit = (struct commit *)o;
Packit 4511e4
		if (o->flags & THEY_HAVE)
Packit 4511e4
			we_knew_they_have = 1;
Packit 4511e4
		else
Packit 4511e4
			o->flags |= THEY_HAVE;
Packit 4511e4
		if (!oldest_have || (commit->date < oldest_have))
Packit 4511e4
			oldest_have = commit->date;
Packit 4511e4
		for (parents = commit->parents;
Packit 4511e4
		     parents;
Packit 4511e4
		     parents = parents->next)
Packit 4511e4
			parents->item->object.flags |= THEY_HAVE;
Packit 4511e4
	}
Packit 4511e4
	if (!we_knew_they_have) {
Packit 4511e4
		add_object_array(o, NULL, &have_obj);
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int reachable(struct commit *want)
Packit 4511e4
{
Packit 4511e4
	struct prio_queue work = { compare_commits_by_commit_date };
Packit 4511e4
Packit 4511e4
	prio_queue_put(&work, want);
Packit 4511e4
	while (work.nr) {
Packit 4511e4
		struct commit_list *list;
Packit 4511e4
		struct commit *commit = prio_queue_get(&work);
Packit 4511e4
Packit 4511e4
		if (commit->object.flags & THEY_HAVE) {
Packit 4511e4
			want->object.flags |= COMMON_KNOWN;
Packit 4511e4
			break;
Packit 4511e4
		}
Packit 4511e4
		if (!commit->object.parsed)
Packit 4511e4
			parse_object(&commit->object.oid);
Packit 4511e4
		if (commit->object.flags & REACHABLE)
Packit 4511e4
			continue;
Packit 4511e4
		commit->object.flags |= REACHABLE;
Packit 4511e4
		if (commit->date < oldest_have)
Packit 4511e4
			continue;
Packit 4511e4
		for (list = commit->parents; list; list = list->next) {
Packit 4511e4
			struct commit *parent = list->item;
Packit 4511e4
			if (!(parent->object.flags & REACHABLE))
Packit 4511e4
				prio_queue_put(&work, parent);
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
	want->object.flags |= REACHABLE;
Packit 4511e4
	clear_commit_marks(want, REACHABLE);
Packit 4511e4
	clear_prio_queue(&work);
Packit 4511e4
	return (want->object.flags & COMMON_KNOWN);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int ok_to_give_up(void)
Packit 4511e4
{
Packit 4511e4
	int i;
Packit 4511e4
Packit 4511e4
	if (!have_obj.nr)
Packit 4511e4
		return 0;
Packit 4511e4
Packit 4511e4
	for (i = 0; i < want_obj.nr; i++) {
Packit 4511e4
		struct object *want = want_obj.objects[i].item;
Packit 4511e4
Packit 4511e4
		if (want->flags & COMMON_KNOWN)
Packit 4511e4
			continue;
Packit 4511e4
		want = deref_tag(want, "a want line", 0);
Packit 4511e4
		if (!want || want->type != OBJ_COMMIT) {
Packit 4511e4
			/* no way to tell if this is reachable by
Packit 4511e4
			 * looking at the ancestry chain alone, so
Packit 4511e4
			 * leave a note to ourselves not to worry about
Packit 4511e4
			 * this object anymore.
Packit 4511e4
			 */
Packit 4511e4
			want_obj.objects[i].item->flags |= COMMON_KNOWN;
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (!reachable((struct commit *)want))
Packit 4511e4
			return 0;
Packit 4511e4
	}
Packit 4511e4
	return 1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int get_common_commits(void)
Packit 4511e4
{
Packit 4511e4
	struct object_id oid;
Packit 4511e4
	char last_hex[GIT_MAX_HEXSZ + 1];
Packit 4511e4
	int got_common = 0;
Packit 4511e4
	int got_other = 0;
Packit 4511e4
	int sent_ready = 0;
Packit 4511e4
Packit 4511e4
	save_commit_buffer = 0;
Packit 4511e4
Packit 4511e4
	for (;;) {
Packit 4511e4
		char *line = packet_read_line(0, NULL);
Packit 4511e4
		const char *arg;
Packit 4511e4
Packit 4511e4
		reset_timeout();
Packit 4511e4
Packit 4511e4
		if (!line) {
Packit 4511e4
			if (multi_ack == 2 && got_common
Packit 4511e4
			    && !got_other && ok_to_give_up()) {
Packit 4511e4
				sent_ready = 1;
Packit 4511e4
				packet_write_fmt(1, "ACK %s ready\n", last_hex);
Packit 4511e4
			}
Packit 4511e4
			if (have_obj.nr == 0 || multi_ack)
Packit 4511e4
				packet_write_fmt(1, "NAK\n");
Packit 4511e4
Packit 4511e4
			if (no_done && sent_ready) {
Packit 4511e4
				packet_write_fmt(1, "ACK %s\n", last_hex);
Packit 4511e4
				return 0;
Packit 4511e4
			}
Packit 4511e4
			if (stateless_rpc)
Packit 4511e4
				exit(0);
Packit 4511e4
			got_common = 0;
Packit 4511e4
			got_other = 0;
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (skip_prefix(line, "have ", &arg)) {
Packit 4511e4
			switch (got_oid(arg, &oid)) {
Packit 4511e4
			case -1: /* they have what we do not */
Packit 4511e4
				got_other = 1;
Packit 4511e4
				if (multi_ack && ok_to_give_up()) {
Packit 4511e4
					const char *hex = oid_to_hex(&oid;;
Packit 4511e4
					if (multi_ack == 2) {
Packit 4511e4
						sent_ready = 1;
Packit 4511e4
						packet_write_fmt(1, "ACK %s ready\n", hex);
Packit 4511e4
					} else
Packit 4511e4
						packet_write_fmt(1, "ACK %s continue\n", hex);
Packit 4511e4
				}
Packit 4511e4
				break;
Packit 4511e4
			default:
Packit 4511e4
				got_common = 1;
Packit 4511e4
				oid_to_hex_r(last_hex, &oid;;
Packit 4511e4
				if (multi_ack == 2)
Packit 4511e4
					packet_write_fmt(1, "ACK %s common\n", last_hex);
Packit 4511e4
				else if (multi_ack)
Packit 4511e4
					packet_write_fmt(1, "ACK %s continue\n", last_hex);
Packit 4511e4
				else if (have_obj.nr == 1)
Packit 4511e4
					packet_write_fmt(1, "ACK %s\n", last_hex);
Packit 4511e4
				break;
Packit 4511e4
			}
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (!strcmp(line, "done")) {
Packit 4511e4
			if (have_obj.nr > 0) {
Packit 4511e4
				if (multi_ack)
Packit 4511e4
					packet_write_fmt(1, "ACK %s\n", last_hex);
Packit 4511e4
				return 0;
Packit 4511e4
			}
Packit 4511e4
			packet_write_fmt(1, "NAK\n");
Packit 4511e4
			return -1;
Packit 4511e4
		}
Packit 4511e4
		die("git upload-pack: expected SHA1 list, got '%s'", line);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int is_our_ref(struct object *o)
Packit 4511e4
{
Packit 4511e4
	int allow_hidden_ref = (allow_unadvertised_object_request &
Packit 4511e4
			(ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1));
Packit 4511e4
	return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * on successful case, it's up to the caller to close cmd->out
Packit 4511e4
 */
Packit 4511e4
static int do_reachable_revlist(struct child_process *cmd,
Packit 4511e4
				struct object_array *src,
Packit 4511e4
				struct object_array *reachable)
Packit 4511e4
{
Packit 4511e4
	static const char *argv[] = {
Packit 4511e4
		"rev-list", "--stdin", NULL,
Packit 4511e4
	};
Packit 4511e4
	struct object *o;
Packit 4511e4
	char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
Packit 4511e4
	int i;
Packit 4511e4
Packit 4511e4
	cmd->argv = argv;
Packit 4511e4
	cmd->git_cmd = 1;
Packit 4511e4
	cmd->no_stderr = 1;
Packit 4511e4
	cmd->in = -1;
Packit 4511e4
	cmd->out = -1;
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * If the next rev-list --stdin encounters an unknown commit,
Packit 4511e4
	 * it terminates, which will cause SIGPIPE in the write loop
Packit 4511e4
	 * below.
Packit 4511e4
	 */
Packit 4511e4
	sigchain_push(SIGPIPE, SIG_IGN);
Packit 4511e4
Packit 4511e4
	if (start_command(cmd))
Packit 4511e4
		goto error;
Packit 4511e4
Packit 4511e4
	namebuf[0] = '^';
Packit 4511e4
	namebuf[GIT_SHA1_HEXSZ + 1] = '\n';
Packit 4511e4
	for (i = get_max_object_index(); 0 < i; ) {
Packit 4511e4
		o = get_indexed_object(--i);
Packit 4511e4
		if (!o)
Packit 4511e4
			continue;
Packit 4511e4
		if (reachable && o->type == OBJ_COMMIT)
Packit 4511e4
			o->flags &= ~TMP_MARK;
Packit 4511e4
		if (!is_our_ref(o))
Packit 4511e4
			continue;
Packit 4511e4
		memcpy(namebuf + 1, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
Packit 4511e4
		if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 2) < 0)
Packit 4511e4
			goto error;
Packit 4511e4
	}
Packit 4511e4
	namebuf[GIT_SHA1_HEXSZ] = '\n';
Packit 4511e4
	for (i = 0; i < src->nr; i++) {
Packit 4511e4
		o = src->objects[i].item;
Packit 4511e4
		if (is_our_ref(o)) {
Packit 4511e4
			if (reachable)
Packit 4511e4
				add_object_array(o, NULL, reachable);
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (reachable && o->type == OBJ_COMMIT)
Packit 4511e4
			o->flags |= TMP_MARK;
Packit 4511e4
		memcpy(namebuf, oid_to_hex(&o->oid), GIT_SHA1_HEXSZ);
Packit 4511e4
		if (write_in_full(cmd->in, namebuf, GIT_SHA1_HEXSZ + 1) < 0)
Packit 4511e4
			goto error;
Packit 4511e4
	}
Packit 4511e4
	close(cmd->in);
Packit 4511e4
	cmd->in = -1;
Packit 4511e4
	sigchain_pop(SIGPIPE);
Packit 4511e4
Packit 4511e4
	return 0;
Packit 4511e4
Packit 4511e4
error:
Packit 4511e4
	sigchain_pop(SIGPIPE);
Packit 4511e4
Packit 4511e4
	if (cmd->in >= 0)
Packit 4511e4
		close(cmd->in);
Packit 4511e4
	if (cmd->out >= 0)
Packit 4511e4
		close(cmd->out);
Packit 4511e4
	return -1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int get_reachable_list(struct object_array *src,
Packit 4511e4
			      struct object_array *reachable)
Packit 4511e4
{
Packit 4511e4
	struct child_process cmd = CHILD_PROCESS_INIT;
Packit 4511e4
	int i;
Packit 4511e4
	struct object *o;
Packit 4511e4
	char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
Packit 4511e4
	const unsigned hexsz = the_hash_algo->hexsz;
Packit 4511e4
Packit 4511e4
	if (do_reachable_revlist(&cmd, src, reachable) < 0)
Packit 4511e4
		return -1;
Packit 4511e4
Packit 4511e4
	while ((i = read_in_full(cmd.out, namebuf, hexsz + 1)) == hexsz + 1) {
Packit 4511e4
		struct object_id sha1;
Packit 4511e4
		const char *p;
Packit 4511e4
Packit 4511e4
		if (parse_oid_hex(namebuf, &sha1, &p) || *p != '\n')
Packit 4511e4
			break;
Packit 4511e4
Packit 4511e4
		o = lookup_object(sha1.hash);
Packit 4511e4
		if (o && o->type == OBJ_COMMIT) {
Packit 4511e4
			o->flags &= ~TMP_MARK;
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
	for (i = get_max_object_index(); 0 < i; i--) {
Packit 4511e4
		o = get_indexed_object(i - 1);
Packit 4511e4
		if (o && o->type == OBJ_COMMIT &&
Packit 4511e4
		    (o->flags & TMP_MARK)) {
Packit 4511e4
			add_object_array(o, NULL, reachable);
Packit 4511e4
				o->flags &= ~TMP_MARK;
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
	close(cmd.out);
Packit 4511e4
Packit 4511e4
	if (finish_command(&cmd))
Packit 4511e4
		return -1;
Packit 4511e4
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int has_unreachable(struct object_array *src)
Packit 4511e4
{
Packit 4511e4
	struct child_process cmd = CHILD_PROCESS_INIT;
Packit 4511e4
	char buf[1];
Packit 4511e4
	int i;
Packit 4511e4
Packit 4511e4
	if (do_reachable_revlist(&cmd, src, NULL) < 0)
Packit 4511e4
		return 1;
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * The commits out of the rev-list are not ancestors of
Packit 4511e4
	 * our ref.
Packit 4511e4
	 */
Packit 4511e4
	i = read_in_full(cmd.out, buf, 1);
Packit 4511e4
	if (i)
Packit 4511e4
		goto error;
Packit 4511e4
	close(cmd.out);
Packit 4511e4
	cmd.out = -1;
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * rev-list may have died by encountering a bad commit
Packit 4511e4
	 * in the history, in which case we do want to bail out
Packit 4511e4
	 * even when it showed no commit.
Packit 4511e4
	 */
Packit 4511e4
	if (finish_command(&cmd))
Packit 4511e4
		goto error;
Packit 4511e4
Packit 4511e4
	/* All the non-tip ones are ancestors of what we advertised */
Packit 4511e4
	return 0;
Packit 4511e4
Packit 4511e4
error:
Packit 4511e4
	sigchain_pop(SIGPIPE);
Packit 4511e4
	if (cmd.out >= 0)
Packit 4511e4
		close(cmd.out);
Packit 4511e4
	return 1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void check_non_tip(void)
Packit 4511e4
{
Packit 4511e4
	int i;
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * In the normal in-process case without
Packit 4511e4
	 * uploadpack.allowReachableSHA1InWant,
Packit 4511e4
	 * non-tip requests can never happen.
Packit 4511e4
	 */
Packit 4511e4
	if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1))
Packit 4511e4
		goto error;
Packit 4511e4
	if (!has_unreachable(&want_obj))
Packit 4511e4
		/* All the non-tip ones are ancestors of what we advertised */
Packit 4511e4
		return;
Packit 4511e4
Packit 4511e4
error:
Packit 4511e4
	/* Pick one of them (we know there at least is one) */
Packit 4511e4
	for (i = 0; i < want_obj.nr; i++) {
Packit 4511e4
		struct object *o = want_obj.objects[i].item;
Packit 4511e4
		if (!is_our_ref(o))
Packit 4511e4
			die("git upload-pack: not our ref %s",
Packit 4511e4
			    oid_to_hex(&o->oid));
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void send_shallow(struct commit_list *result)
Packit 4511e4
{
Packit 4511e4
	while (result) {
Packit 4511e4
		struct object *object = &result->item->object;
Packit 4511e4
		if (!(object->flags & (CLIENT_SHALLOW|NOT_SHALLOW))) {
Packit 4511e4
			packet_write_fmt(1, "shallow %s",
Packit 4511e4
					 oid_to_hex(&object->oid));
Packit 4511e4
			register_shallow(&object->oid);
Packit 4511e4
			shallow_nr++;
Packit 4511e4
		}
Packit 4511e4
		result = result->next;
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void send_unshallow(const struct object_array *shallows)
Packit 4511e4
{
Packit 4511e4
	int i;
Packit 4511e4
Packit 4511e4
	for (i = 0; i < shallows->nr; i++) {
Packit 4511e4
		struct object *object = shallows->objects[i].item;
Packit 4511e4
		if (object->flags & NOT_SHALLOW) {
Packit 4511e4
			struct commit_list *parents;
Packit 4511e4
			packet_write_fmt(1, "unshallow %s",
Packit 4511e4
					 oid_to_hex(&object->oid));
Packit 4511e4
			object->flags &= ~CLIENT_SHALLOW;
Packit 4511e4
			/*
Packit 4511e4
			 * We want to _register_ "object" as shallow, but we
Packit 4511e4
			 * also need to traverse object's parents to deepen a
Packit 4511e4
			 * shallow clone. Unregister it for now so we can
Packit 4511e4
			 * parse and add the parents to the want list, then
Packit 4511e4
			 * re-register it.
Packit 4511e4
			 */
Packit 4511e4
			unregister_shallow(&object->oid);
Packit 4511e4
			object->parsed = 0;
Packit 4511e4
			parse_commit_or_die((struct commit *)object);
Packit 4511e4
			parents = ((struct commit *)object)->parents;
Packit 4511e4
			while (parents) {
Packit 4511e4
				add_object_array(&parents->item->object,
Packit 4511e4
						 NULL, &want_obj);
Packit 4511e4
				parents = parents->next;
Packit 4511e4
			}
Packit 4511e4
			add_object_array(object, NULL, &extra_edge_obj);
Packit 4511e4
		}
Packit 4511e4
		/* make sure commit traversal conforms to client */
Packit 4511e4
		register_shallow(&object->oid);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void deepen(int depth, int deepen_relative,
Packit 4511e4
		   struct object_array *shallows)
Packit 4511e4
{
Packit 4511e4
	if (depth == INFINITE_DEPTH && !is_repository_shallow()) {
Packit 4511e4
		int i;
Packit 4511e4
Packit 4511e4
		for (i = 0; i < shallows->nr; i++) {
Packit 4511e4
			struct object *object = shallows->objects[i].item;
Packit 4511e4
			object->flags |= NOT_SHALLOW;
Packit 4511e4
		}
Packit 4511e4
	} else if (deepen_relative) {
Packit 4511e4
		struct object_array reachable_shallows = OBJECT_ARRAY_INIT;
Packit 4511e4
		struct commit_list *result;
Packit 4511e4
Packit 4511e4
		get_reachable_list(shallows, &reachable_shallows);
Packit 4511e4
		result = get_shallow_commits(&reachable_shallows,
Packit 4511e4
					     depth + 1,
Packit 4511e4
					     SHALLOW, NOT_SHALLOW);
Packit 4511e4
		send_shallow(result);
Packit 4511e4
		free_commit_list(result);
Packit 4511e4
		object_array_clear(&reachable_shallows);
Packit 4511e4
	} else {
Packit 4511e4
		struct commit_list *result;
Packit 4511e4
Packit 4511e4
		result = get_shallow_commits(&want_obj, depth,
Packit 4511e4
					     SHALLOW, NOT_SHALLOW);
Packit 4511e4
		send_shallow(result);
Packit 4511e4
		free_commit_list(result);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	send_unshallow(shallows);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void deepen_by_rev_list(int ac, const char **av,
Packit 4511e4
			       struct object_array *shallows)
Packit 4511e4
{
Packit 4511e4
	struct commit_list *result;
Packit 4511e4
Packit 4511e4
	result = get_shallow_commits_by_rev_list(ac, av, SHALLOW, NOT_SHALLOW);
Packit 4511e4
	send_shallow(result);
Packit 4511e4
	free_commit_list(result);
Packit 4511e4
	send_unshallow(shallows);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/* Returns 1 if a shallow list is sent or 0 otherwise */
Packit 4511e4
static int send_shallow_list(int depth, int deepen_rev_list,
Packit 4511e4
			     timestamp_t deepen_since,
Packit 4511e4
			     struct string_list *deepen_not,
Packit 4511e4
			     struct object_array *shallows)
Packit 4511e4
{
Packit 4511e4
	int ret = 0;
Packit 4511e4
Packit 4511e4
	if (depth > 0 && deepen_rev_list)
Packit 4511e4
		die("git upload-pack: deepen and deepen-since (or deepen-not) cannot be used together");
Packit 4511e4
	if (depth > 0) {
Packit 4511e4
		deepen(depth, deepen_relative, shallows);
Packit 4511e4
		ret = 1;
Packit 4511e4
	} else if (deepen_rev_list) {
Packit 4511e4
		struct argv_array av = ARGV_ARRAY_INIT;
Packit 4511e4
		int i;
Packit 4511e4
Packit 4511e4
		argv_array_push(&av, "rev-list");
Packit 4511e4
		if (deepen_since)
Packit 4511e4
			argv_array_pushf(&av, "--max-age=%"PRItime, deepen_since);
Packit 4511e4
		if (deepen_not->nr) {
Packit 4511e4
			argv_array_push(&av, "--not");
Packit 4511e4
			for (i = 0; i < deepen_not->nr; i++) {
Packit 4511e4
				struct string_list_item *s = deepen_not->items + i;
Packit 4511e4
				argv_array_push(&av, s->string);
Packit 4511e4
			}
Packit 4511e4
			argv_array_push(&av, "--not");
Packit 4511e4
		}
Packit 4511e4
		for (i = 0; i < want_obj.nr; i++) {
Packit 4511e4
			struct object *o = want_obj.objects[i].item;
Packit 4511e4
			argv_array_push(&av, oid_to_hex(&o->oid));
Packit 4511e4
		}
Packit 4511e4
		deepen_by_rev_list(av.argc, av.argv, shallows);
Packit 4511e4
		argv_array_clear(&av;;
Packit 4511e4
		ret = 1;
Packit 4511e4
	} else {
Packit 4511e4
		if (shallows->nr > 0) {
Packit 4511e4
			int i;
Packit 4511e4
			for (i = 0; i < shallows->nr; i++)
Packit 4511e4
				register_shallow(&shallows->objects[i].item->oid);
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	shallow_nr += shallows->nr;
Packit 4511e4
	return ret;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int process_shallow(const char *line, struct object_array *shallows)
Packit 4511e4
{
Packit 4511e4
	const char *arg;
Packit 4511e4
	if (skip_prefix(line, "shallow ", &arg)) {
Packit 4511e4
		struct object_id oid;
Packit 4511e4
		struct object *object;
Packit 4511e4
		if (get_oid_hex(arg, &oid))
Packit 4511e4
			die("invalid shallow line: %s", line);
Packit 4511e4
		object = parse_object(&oid;;
Packit 4511e4
		if (!object)
Packit 4511e4
			return 1;
Packit 4511e4
		if (object->type != OBJ_COMMIT)
Packit 4511e4
			die("invalid shallow object %s", oid_to_hex(&oid));
Packit 4511e4
		if (!(object->flags & CLIENT_SHALLOW)) {
Packit 4511e4
			object->flags |= CLIENT_SHALLOW;
Packit 4511e4
			add_object_array(object, NULL, shallows);
Packit 4511e4
		}
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int process_deepen(const char *line, int *depth)
Packit 4511e4
{
Packit 4511e4
	const char *arg;
Packit 4511e4
	if (skip_prefix(line, "deepen ", &arg)) {
Packit 4511e4
		char *end = NULL;
Packit 4511e4
		*depth = (int)strtol(arg, &end, 0);
Packit 4511e4
		if (!end || *end || *depth <= 0)
Packit 4511e4
			die("Invalid deepen: %s", line);
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int process_deepen_since(const char *line, timestamp_t *deepen_since, int *deepen_rev_list)
Packit 4511e4
{
Packit 4511e4
	const char *arg;
Packit 4511e4
	if (skip_prefix(line, "deepen-since ", &arg)) {
Packit 4511e4
		char *end = NULL;
Packit 4511e4
		*deepen_since = parse_timestamp(arg, &end, 0);
Packit 4511e4
		if (!end || *end || !deepen_since ||
Packit 4511e4
		    /* revisions.c's max_age -1 is special */
Packit 4511e4
		    *deepen_since == -1)
Packit 4511e4
			die("Invalid deepen-since: %s", line);
Packit 4511e4
		*deepen_rev_list = 1;
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int process_deepen_not(const char *line, struct string_list *deepen_not, int *deepen_rev_list)
Packit 4511e4
{
Packit 4511e4
	const char *arg;
Packit 4511e4
	if (skip_prefix(line, "deepen-not ", &arg)) {
Packit 4511e4
		char *ref = NULL;
Packit 4511e4
		struct object_id oid;
Packit 4511e4
		if (expand_ref(arg, strlen(arg), &oid, &ref) != 1)
Packit 4511e4
			die("git upload-pack: ambiguous deepen-not: %s", line);
Packit 4511e4
		string_list_append(deepen_not, ref);
Packit 4511e4
		free(ref);
Packit 4511e4
		*deepen_rev_list = 1;
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void receive_needs(void)
Packit 4511e4
{
Packit 4511e4
	struct object_array shallows = OBJECT_ARRAY_INIT;
Packit 4511e4
	struct string_list deepen_not = STRING_LIST_INIT_DUP;
Packit 4511e4
	int depth = 0;
Packit 4511e4
	int has_non_tip = 0;
Packit 4511e4
	timestamp_t deepen_since = 0;
Packit 4511e4
	int deepen_rev_list = 0;
Packit 4511e4
Packit 4511e4
	shallow_nr = 0;
Packit 4511e4
	for (;;) {
Packit 4511e4
		struct object *o;
Packit 4511e4
		const char *features;
Packit 4511e4
		struct object_id oid_buf;
Packit 4511e4
		char *line = packet_read_line(0, NULL);
Packit 4511e4
		const char *arg;
Packit 4511e4
Packit 4511e4
		reset_timeout();
Packit 4511e4
		if (!line)
Packit 4511e4
			break;
Packit 4511e4
Packit 4511e4
		if (process_shallow(line, &shallows))
Packit 4511e4
			continue;
Packit 4511e4
		if (process_deepen(line, &depth))
Packit 4511e4
			continue;
Packit 4511e4
		if (process_deepen_since(line, &deepen_since, &deepen_rev_list))
Packit 4511e4
			continue;
Packit 4511e4
		if (process_deepen_not(line, &deepen_not, &deepen_rev_list))
Packit 4511e4
			continue;
Packit 4511e4
Packit 4511e4
		if (skip_prefix(line, "filter ", &arg)) {
Packit 4511e4
			if (!filter_capability_requested)
Packit 4511e4
				die("git upload-pack: filtering capability not negotiated");
Packit 4511e4
			parse_list_objects_filter(&filter_options, arg);
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		if (!skip_prefix(line, "want ", &arg) ||
Packit 4511e4
		    parse_oid_hex(arg, &oid_buf, &features))
Packit 4511e4
			die("git upload-pack: protocol error, "
Packit 4511e4
			    "expected to get object ID, not '%s'", line);
Packit 4511e4
Packit 4511e4
		if (parse_feature_request(features, "deepen-relative"))
Packit 4511e4
			deepen_relative = 1;
Packit 4511e4
		if (parse_feature_request(features, "multi_ack_detailed"))
Packit 4511e4
			multi_ack = 2;
Packit 4511e4
		else if (parse_feature_request(features, "multi_ack"))
Packit 4511e4
			multi_ack = 1;
Packit 4511e4
		if (parse_feature_request(features, "no-done"))
Packit 4511e4
			no_done = 1;
Packit 4511e4
		if (parse_feature_request(features, "thin-pack"))
Packit 4511e4
			use_thin_pack = 1;
Packit 4511e4
		if (parse_feature_request(features, "ofs-delta"))
Packit 4511e4
			use_ofs_delta = 1;
Packit 4511e4
		if (parse_feature_request(features, "side-band-64k"))
Packit 4511e4
			use_sideband = LARGE_PACKET_MAX;
Packit 4511e4
		else if (parse_feature_request(features, "side-band"))
Packit 4511e4
			use_sideband = DEFAULT_PACKET_MAX;
Packit 4511e4
		if (parse_feature_request(features, "no-progress"))
Packit 4511e4
			no_progress = 1;
Packit 4511e4
		if (parse_feature_request(features, "include-tag"))
Packit 4511e4
			use_include_tag = 1;
Packit 4511e4
		if (allow_filter && parse_feature_request(features, "filter"))
Packit 4511e4
			filter_capability_requested = 1;
Packit 4511e4
Packit 4511e4
		o = parse_object(&oid_buf);
Packit 4511e4
		if (!o) {
Packit 4511e4
			packet_write_fmt(1,
Packit 4511e4
					 "ERR upload-pack: not our ref %s",
Packit 4511e4
					 oid_to_hex(&oid_buf));
Packit 4511e4
			die("git upload-pack: not our ref %s",
Packit 4511e4
			    oid_to_hex(&oid_buf));
Packit 4511e4
		}
Packit 4511e4
		if (!(o->flags & WANTED)) {
Packit 4511e4
			o->flags |= WANTED;
Packit 4511e4
			if (!((allow_unadvertised_object_request & ALLOW_ANY_SHA1) == ALLOW_ANY_SHA1
Packit 4511e4
			      || is_our_ref(o)))
Packit 4511e4
				has_non_tip = 1;
Packit 4511e4
			add_object_array(o, NULL, &want_obj);
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * We have sent all our refs already, and the other end
Packit 4511e4
	 * should have chosen out of them. When we are operating
Packit 4511e4
	 * in the stateless RPC mode, however, their choice may
Packit 4511e4
	 * have been based on the set of older refs advertised
Packit 4511e4
	 * by another process that handled the initial request.
Packit 4511e4
	 */
Packit 4511e4
	if (has_non_tip)
Packit 4511e4
		check_non_tip();
Packit 4511e4
Packit 4511e4
	if (!use_sideband && daemon_mode)
Packit 4511e4
		no_progress = 1;
Packit 4511e4
Packit 4511e4
	if (depth == 0 && !deepen_rev_list && shallows.nr == 0)
Packit 4511e4
		return;
Packit 4511e4
Packit 4511e4
	if (send_shallow_list(depth, deepen_rev_list, deepen_since,
Packit 4511e4
			      &deepen_not, &shallows))
Packit 4511e4
		packet_flush(1);
Packit 4511e4
	object_array_clear(&shallows);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/* return non-zero if the ref is hidden, otherwise 0 */
Packit 4511e4
static int mark_our_ref(const char *refname, const char *refname_full,
Packit 4511e4
			const struct object_id *oid)
Packit 4511e4
{
Packit 4511e4
	struct object *o = lookup_unknown_object(oid->hash);
Packit 4511e4
Packit 4511e4
	if (ref_is_hidden(refname, refname_full)) {
Packit 4511e4
		o->flags |= HIDDEN_REF;
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
	o->flags |= OUR_REF;
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int check_ref(const char *refname_full, const struct object_id *oid,
Packit 4511e4
		     int flag, void *cb_data)
Packit 4511e4
{
Packit 4511e4
	const char *refname = strip_namespace(refname_full);
Packit 4511e4
Packit 4511e4
	mark_our_ref(refname, refname_full, oid);
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void format_symref_info(struct strbuf *buf, struct string_list *symref)
Packit 4511e4
{
Packit 4511e4
	struct string_list_item *item;
Packit 4511e4
Packit 4511e4
	if (!symref->nr)
Packit 4511e4
		return;
Packit 4511e4
	for_each_string_list_item(item, symref)
Packit 4511e4
		strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int send_ref(const char *refname, const struct object_id *oid,
Packit 4511e4
		    int flag, void *cb_data)
Packit 4511e4
{
Packit 4511e4
	static const char *capabilities = "multi_ack thin-pack side-band"
Packit 4511e4
		" side-band-64k ofs-delta shallow deepen-since deepen-not"
Packit 4511e4
		" deepen-relative no-progress include-tag multi_ack_detailed";
Packit 4511e4
	const char *refname_nons = strip_namespace(refname);
Packit 4511e4
	struct object_id peeled;
Packit 4511e4
Packit 4511e4
	if (mark_our_ref(refname_nons, refname, oid))
Packit 4511e4
		return 0;
Packit 4511e4
Packit 4511e4
	if (capabilities) {
Packit 4511e4
		struct strbuf symref_info = STRBUF_INIT;
Packit 4511e4
Packit 4511e4
		format_symref_info(&symref_info, cb_data);
Packit 4511e4
		packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s agent=%s\n",
Packit 4511e4
			     oid_to_hex(oid), refname_nons,
Packit 4511e4
			     0, capabilities,
Packit 4511e4
			     (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ?
Packit 4511e4
				     " allow-tip-sha1-in-want" : "",
Packit 4511e4
			     (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ?
Packit 4511e4
				     " allow-reachable-sha1-in-want" : "",
Packit 4511e4
			     stateless_rpc ? " no-done" : "",
Packit 4511e4
			     symref_info.buf,
Packit 4511e4
			     allow_filter ? " filter" : "",
Packit 4511e4
			     git_user_agent_sanitized());
Packit 4511e4
		strbuf_release(&symref_info);
Packit 4511e4
	} else {
Packit 4511e4
		packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
Packit 4511e4
	}
Packit 4511e4
	capabilities = NULL;
Packit 4511e4
	if (!peel_ref(refname, &peeled))
Packit 4511e4
		packet_write_fmt(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons);
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int find_symref(const char *refname, const struct object_id *oid,
Packit 4511e4
		       int flag, void *cb_data)
Packit 4511e4
{
Packit 4511e4
	const char *symref_target;
Packit 4511e4
	struct string_list_item *item;
Packit 4511e4
Packit 4511e4
	if ((flag & REF_ISSYMREF) == 0)
Packit 4511e4
		return 0;
Packit 4511e4
	symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag;;
Packit 4511e4
	if (!symref_target || (flag & REF_ISSYMREF) == 0)
Packit 4511e4
		die("'%s' is a symref but it is not?", refname);
Packit 4511e4
	item = string_list_append(cb_data, refname);
Packit 4511e4
	item->util = xstrdup(symref_target);
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int upload_pack_config(const char *var, const char *value, void *unused)
Packit 4511e4
{
Packit 4511e4
	if (!strcmp("uploadpack.allowtipsha1inwant", var)) {
Packit 4511e4
		if (git_config_bool(var, value))
Packit 4511e4
			allow_unadvertised_object_request |= ALLOW_TIP_SHA1;
Packit 4511e4
		else
Packit 4511e4
			allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1;
Packit 4511e4
	} else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) {
Packit 4511e4
		if (git_config_bool(var, value))
Packit 4511e4
			allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1;
Packit 4511e4
		else
Packit 4511e4
			allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1;
Packit 4511e4
	} else if (!strcmp("uploadpack.allowanysha1inwant", var)) {
Packit 4511e4
		if (git_config_bool(var, value))
Packit 4511e4
			allow_unadvertised_object_request |= ALLOW_ANY_SHA1;
Packit 4511e4
		else
Packit 4511e4
			allow_unadvertised_object_request &= ~ALLOW_ANY_SHA1;
Packit 4511e4
	} else if (!strcmp("uploadpack.keepalive", var)) {
Packit 4511e4
		keepalive = git_config_int(var, value);
Packit 4511e4
		if (!keepalive)
Packit 4511e4
			keepalive = -1;
Packit 4511e4
	} else if (current_config_scope() != CONFIG_SCOPE_REPO) {
Packit 4511e4
		if (!strcmp("uploadpack.packobjectshook", var))
Packit 4511e4
			return git_config_string(&pack_objects_hook, var, value);
Packit 4511e4
	} else if (!strcmp("uploadpack.allowfilter", var)) {
Packit 4511e4
		allow_filter = git_config_bool(var, value);
Packit 4511e4
	}
Packit 4511e4
	return parse_hide_refs_config(var, value, "uploadpack");
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void upload_pack(struct upload_pack_options *options)
Packit 4511e4
{
Packit 4511e4
	struct string_list symref = STRING_LIST_INIT_DUP;
Packit 4511e4
Packit 4511e4
	stateless_rpc = options->stateless_rpc;
Packit 4511e4
	timeout = options->timeout;
Packit 4511e4
	daemon_mode = options->daemon_mode;
Packit 4511e4
Packit 4511e4
	git_config(upload_pack_config, NULL);
Packit 4511e4
Packit 4511e4
	head_ref_namespaced(find_symref, &symref);
Packit 4511e4
Packit 4511e4
	if (options->advertise_refs || !stateless_rpc) {
Packit 4511e4
		reset_timeout();
Packit 4511e4
		head_ref_namespaced(send_ref, &symref);
Packit 4511e4
		for_each_namespaced_ref(send_ref, &symref);
Packit 4511e4
		advertise_shallow_grafts(1);
Packit 4511e4
		packet_flush(1);
Packit 4511e4
	} else {
Packit 4511e4
		head_ref_namespaced(check_ref, NULL);
Packit 4511e4
		for_each_namespaced_ref(check_ref, NULL);
Packit 4511e4
	}
Packit 4511e4
	string_list_clear(&symref, 1);
Packit 4511e4
	if (options->advertise_refs)
Packit 4511e4
		return;
Packit 4511e4
Packit 4511e4
	receive_needs();
Packit 4511e4
	if (want_obj.nr) {
Packit 4511e4
		get_common_commits();
Packit 4511e4
		create_pack_file();
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
struct upload_pack_data {
Packit 4511e4
	struct object_array wants;
Packit 4511e4
	struct oid_array haves;
Packit 4511e4
Packit 4511e4
	struct object_array shallows;
Packit 4511e4
	struct string_list deepen_not;
Packit 4511e4
	int depth;
Packit 4511e4
	timestamp_t deepen_since;
Packit 4511e4
	int deepen_rev_list;
Packit 4511e4
	int deepen_relative;
Packit 4511e4
Packit 4511e4
	unsigned stateless_rpc : 1;
Packit 4511e4
Packit 4511e4
	unsigned use_thin_pack : 1;
Packit 4511e4
	unsigned use_ofs_delta : 1;
Packit 4511e4
	unsigned no_progress : 1;
Packit 4511e4
	unsigned use_include_tag : 1;
Packit 4511e4
	unsigned done : 1;
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
static void upload_pack_data_init(struct upload_pack_data *data)
Packit 4511e4
{
Packit 4511e4
	struct object_array wants = OBJECT_ARRAY_INIT;
Packit 4511e4
	struct oid_array haves = OID_ARRAY_INIT;
Packit 4511e4
	struct object_array shallows = OBJECT_ARRAY_INIT;
Packit 4511e4
	struct string_list deepen_not = STRING_LIST_INIT_DUP;
Packit 4511e4
Packit 4511e4
	memset(data, 0, sizeof(*data));
Packit 4511e4
	data->wants = wants;
Packit 4511e4
	data->haves = haves;
Packit 4511e4
	data->shallows = shallows;
Packit 4511e4
	data->deepen_not = deepen_not;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void upload_pack_data_clear(struct upload_pack_data *data)
Packit 4511e4
{
Packit 4511e4
	object_array_clear(&data->wants);
Packit 4511e4
	oid_array_clear(&data->haves);
Packit 4511e4
	object_array_clear(&data->shallows);
Packit 4511e4
	string_list_clear(&data->deepen_not, 0);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int parse_want(const char *line)
Packit 4511e4
{
Packit 4511e4
	const char *arg;
Packit 4511e4
	if (skip_prefix(line, "want ", &arg)) {
Packit 4511e4
		struct object_id oid;
Packit 4511e4
		struct object *o;
Packit 4511e4
Packit 4511e4
		if (get_oid_hex(arg, &oid))
Packit 4511e4
			die("git upload-pack: protocol error, "
Packit 4511e4
			    "expected to get oid, not '%s'", line);
Packit 4511e4
Packit 4511e4
		o = parse_object(&oid;;
Packit 4511e4
		if (!o) {
Packit 4511e4
			packet_write_fmt(1,
Packit 4511e4
					 "ERR upload-pack: not our ref %s",
Packit 4511e4
					 oid_to_hex(&oid));
Packit 4511e4
			die("git upload-pack: not our ref %s",
Packit 4511e4
			    oid_to_hex(&oid));
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		if (!(o->flags & WANTED)) {
Packit 4511e4
			o->flags |= WANTED;
Packit 4511e4
			add_object_array(o, NULL, &want_obj);
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int parse_have(const char *line, struct oid_array *haves)
Packit 4511e4
{
Packit 4511e4
	const char *arg;
Packit 4511e4
	if (skip_prefix(line, "have ", &arg)) {
Packit 4511e4
		struct object_id oid;
Packit 4511e4
Packit 4511e4
		if (get_oid_hex(arg, &oid))
Packit 4511e4
			die("git upload-pack: expected SHA1 object, got '%s'", arg);
Packit 4511e4
		oid_array_append(haves, &oid;;
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void process_args(struct packet_reader *request,
Packit 4511e4
			 struct upload_pack_data *data)
Packit 4511e4
{
Packit 4511e4
	while (packet_reader_read(request) != PACKET_READ_FLUSH) {
Packit 4511e4
		const char *arg = request->line;
Packit 4511e4
		const char *p;
Packit 4511e4
Packit 4511e4
		/* process want */
Packit 4511e4
		if (parse_want(arg))
Packit 4511e4
			continue;
Packit 4511e4
		/* process have line */
Packit 4511e4
		if (parse_have(arg, &data->haves))
Packit 4511e4
			continue;
Packit 4511e4
Packit 4511e4
		/* process args like thin-pack */
Packit 4511e4
		if (!strcmp(arg, "thin-pack")) {
Packit 4511e4
			use_thin_pack = 1;
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (!strcmp(arg, "ofs-delta")) {
Packit 4511e4
			use_ofs_delta = 1;
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (!strcmp(arg, "no-progress")) {
Packit 4511e4
			no_progress = 1;
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (!strcmp(arg, "include-tag")) {
Packit 4511e4
			use_include_tag = 1;
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (!strcmp(arg, "done")) {
Packit 4511e4
			data->done = 1;
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		/* Shallow related arguments */
Packit 4511e4
		if (process_shallow(arg, &data->shallows))
Packit 4511e4
			continue;
Packit 4511e4
		if (process_deepen(arg, &data->depth))
Packit 4511e4
			continue;
Packit 4511e4
		if (process_deepen_since(arg, &data->deepen_since,
Packit 4511e4
					 &data->deepen_rev_list))
Packit 4511e4
			continue;
Packit 4511e4
		if (process_deepen_not(arg, &data->deepen_not,
Packit 4511e4
				       &data->deepen_rev_list))
Packit 4511e4
			continue;
Packit 4511e4
		if (!strcmp(arg, "deepen-relative")) {
Packit 4511e4
			data->deepen_relative = 1;
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		if (allow_filter && skip_prefix(arg, "filter ", &p)) {
Packit 4511e4
			parse_list_objects_filter(&filter_options, p);
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		/* ignore unknown lines maybe? */
Packit 4511e4
		die("unexpected line: '%s'", arg);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int process_haves(struct oid_array *haves, struct oid_array *common)
Packit 4511e4
{
Packit 4511e4
	int i;
Packit 4511e4
Packit 4511e4
	/* Process haves */
Packit 4511e4
	for (i = 0; i < haves->nr; i++) {
Packit 4511e4
		const struct object_id *oid = &haves->oid[i];
Packit 4511e4
		struct object *o;
Packit 4511e4
		int we_knew_they_have = 0;
Packit 4511e4
Packit 4511e4
		if (!has_object_file(oid))
Packit 4511e4
			continue;
Packit 4511e4
Packit 4511e4
		oid_array_append(common, oid);
Packit 4511e4
Packit 4511e4
		o = parse_object(oid);
Packit 4511e4
		if (!o)
Packit 4511e4
			die("oops (%s)", oid_to_hex(oid));
Packit 4511e4
		if (o->type == OBJ_COMMIT) {
Packit 4511e4
			struct commit_list *parents;
Packit 4511e4
			struct commit *commit = (struct commit *)o;
Packit 4511e4
			if (o->flags & THEY_HAVE)
Packit 4511e4
				we_knew_they_have = 1;
Packit 4511e4
			else
Packit 4511e4
				o->flags |= THEY_HAVE;
Packit 4511e4
			if (!oldest_have || (commit->date < oldest_have))
Packit 4511e4
				oldest_have = commit->date;
Packit 4511e4
			for (parents = commit->parents;
Packit 4511e4
			     parents;
Packit 4511e4
			     parents = parents->next)
Packit 4511e4
				parents->item->object.flags |= THEY_HAVE;
Packit 4511e4
		}
Packit 4511e4
		if (!we_knew_they_have)
Packit 4511e4
			add_object_array(o, NULL, &have_obj);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int send_acks(struct oid_array *acks, struct strbuf *response)
Packit 4511e4
{
Packit 4511e4
	int i;
Packit 4511e4
Packit 4511e4
	packet_buf_write(response, "acknowledgments\n");
Packit 4511e4
Packit 4511e4
	/* Send Acks */
Packit 4511e4
	if (!acks->nr)
Packit 4511e4
		packet_buf_write(response, "NAK\n");
Packit 4511e4
Packit 4511e4
	for (i = 0; i < acks->nr; i++) {
Packit 4511e4
		packet_buf_write(response, "ACK %s\n",
Packit 4511e4
				 oid_to_hex(&acks->oid[i]));
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	if (ok_to_give_up()) {
Packit 4511e4
		/* Send Ready */
Packit 4511e4
		packet_buf_write(response, "ready\n");
Packit 4511e4
		return 1;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int process_haves_and_send_acks(struct upload_pack_data *data)
Packit 4511e4
{
Packit 4511e4
	struct oid_array common = OID_ARRAY_INIT;
Packit 4511e4
	struct strbuf response = STRBUF_INIT;
Packit 4511e4
	int ret = 0;
Packit 4511e4
Packit 4511e4
	process_haves(&data->haves, &common);
Packit 4511e4
	if (data->done) {
Packit 4511e4
		ret = 1;
Packit 4511e4
	} else if (send_acks(&common, &response)) {
Packit 4511e4
		packet_buf_delim(&response);
Packit 4511e4
		ret = 1;
Packit 4511e4
	} else {
Packit 4511e4
		/* Add Flush */
Packit 4511e4
		packet_buf_flush(&response);
Packit 4511e4
		ret = 0;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	/* Send response */
Packit 4511e4
	write_or_die(1, response.buf, response.len);
Packit 4511e4
	strbuf_release(&response);
Packit 4511e4
Packit 4511e4
	oid_array_clear(&data->haves);
Packit 4511e4
	oid_array_clear(&common);
Packit 4511e4
	return ret;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void send_shallow_info(struct upload_pack_data *data)
Packit 4511e4
{
Packit 4511e4
	/* No shallow info needs to be sent */
Packit 4511e4
	if (!data->depth && !data->deepen_rev_list && !data->shallows.nr &&
Packit 4511e4
	    !is_repository_shallow())
Packit 4511e4
		return;
Packit 4511e4
Packit 4511e4
	packet_write_fmt(1, "shallow-info\n");
Packit 4511e4
Packit 4511e4
	if (!send_shallow_list(data->depth, data->deepen_rev_list,
Packit 4511e4
			       data->deepen_since, &data->deepen_not,
Packit 4511e4
			       &data->shallows) && is_repository_shallow())
Packit 4511e4
		deepen(INFINITE_DEPTH, data->deepen_relative, &data->shallows);
Packit 4511e4
Packit 4511e4
	packet_delim(1);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
enum fetch_state {
Packit 4511e4
	FETCH_PROCESS_ARGS = 0,
Packit 4511e4
	FETCH_SEND_ACKS,
Packit 4511e4
	FETCH_SEND_PACK,
Packit 4511e4
	FETCH_DONE,
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
int upload_pack_v2(struct repository *r, struct argv_array *keys,
Packit 4511e4
		   struct packet_reader *request)
Packit 4511e4
{
Packit 4511e4
	enum fetch_state state = FETCH_PROCESS_ARGS;
Packit 4511e4
	struct upload_pack_data data;
Packit 4511e4
Packit 4511e4
	git_config(upload_pack_config, NULL);
Packit 4511e4
Packit 4511e4
	upload_pack_data_init(&data);
Packit 4511e4
	use_sideband = LARGE_PACKET_MAX;
Packit 4511e4
Packit 4511e4
	while (state != FETCH_DONE) {
Packit 4511e4
		switch (state) {
Packit 4511e4
		case FETCH_PROCESS_ARGS:
Packit 4511e4
			process_args(request, &data);
Packit 4511e4
Packit 4511e4
			if (!want_obj.nr) {
Packit 4511e4
				/*
Packit 4511e4
				 * Request didn't contain any 'want' lines,
Packit 4511e4
				 * guess they didn't want anything.
Packit 4511e4
				 */
Packit 4511e4
				state = FETCH_DONE;
Packit 4511e4
			} else if (data.haves.nr) {
Packit 4511e4
				/*
Packit 4511e4
				 * Request had 'have' lines, so lets ACK them.
Packit 4511e4
				 */
Packit 4511e4
				state = FETCH_SEND_ACKS;
Packit 4511e4
			} else {
Packit 4511e4
				/*
Packit 4511e4
				 * Request had 'want's but no 'have's so we can
Packit 4511e4
				 * immedietly go to construct and send a pack.
Packit 4511e4
				 */
Packit 4511e4
				state = FETCH_SEND_PACK;
Packit 4511e4
			}
Packit 4511e4
			break;
Packit 4511e4
		case FETCH_SEND_ACKS:
Packit 4511e4
			if (process_haves_and_send_acks(&data))
Packit 4511e4
				state = FETCH_SEND_PACK;
Packit 4511e4
			else
Packit 4511e4
				state = FETCH_DONE;
Packit 4511e4
			break;
Packit 4511e4
		case FETCH_SEND_PACK:
Packit 4511e4
			send_shallow_info(&data);
Packit 4511e4
Packit 4511e4
			packet_write_fmt(1, "packfile\n");
Packit 4511e4
			create_pack_file();
Packit 4511e4
			state = FETCH_DONE;
Packit 4511e4
			break;
Packit 4511e4
		case FETCH_DONE:
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	upload_pack_data_clear(&data);
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int upload_pack_advertise(struct repository *r,
Packit 4511e4
			  struct strbuf *value)
Packit 4511e4
{
Packit 4511e4
	if (value) {
Packit 4511e4
		int allow_filter_value;
Packit 4511e4
		strbuf_addstr(value, "shallow");
Packit 4511e4
		if (!repo_config_get_bool(the_repository,
Packit 4511e4
					 "uploadpack.allowfilter",
Packit 4511e4
					 &allow_filter_value) &&
Packit 4511e4
		    allow_filter_value)
Packit 4511e4
			strbuf_addstr(value, " filter");
Packit 4511e4
	}
Packit 4511e4
	return 1;
Packit 4511e4
}