Blame http-push.c

Packit 4511e4
#include "cache.h"
Packit 4511e4
#include "commit.h"
Packit 4511e4
#include "tag.h"
Packit 4511e4
#include "blob.h"
Packit 4511e4
#include "http.h"
Packit 4511e4
#include "refs.h"
Packit 4511e4
#include "diff.h"
Packit 4511e4
#include "revision.h"
Packit 4511e4
#include "exec-cmd.h"
Packit 4511e4
#include "remote.h"
Packit 4511e4
#include "list-objects.h"
Packit 4511e4
#include "sigchain.h"
Packit 4511e4
#include "argv-array.h"
Packit 4511e4
#include "packfile.h"
Packit 4511e4
#include "object-store.h"
Packit 4511e4
Packit 4511e4
#ifdef EXPAT_NEEDS_XMLPARSE_H
Packit 4511e4
#include <xmlparse.h>
Packit 4511e4
#else
Packit 4511e4
#include <expat.h>
Packit 4511e4
#endif
Packit 4511e4
Packit 4511e4
static const char http_push_usage[] =
Packit 4511e4
"git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
Packit 4511e4
Packit 4511e4
#ifndef XML_STATUS_OK
Packit 4511e4
enum XML_Status {
Packit 4511e4
  XML_STATUS_OK = 1,
Packit 4511e4
  XML_STATUS_ERROR = 0
Packit 4511e4
};
Packit 4511e4
#define XML_STATUS_OK    1
Packit 4511e4
#define XML_STATUS_ERROR 0
Packit 4511e4
#endif
Packit 4511e4
Packit 4511e4
#define PREV_BUF_SIZE 4096
Packit 4511e4
Packit 4511e4
/* DAV methods */
Packit 4511e4
#define DAV_LOCK "LOCK"
Packit 4511e4
#define DAV_MKCOL "MKCOL"
Packit 4511e4
#define DAV_MOVE "MOVE"
Packit 4511e4
#define DAV_PROPFIND "PROPFIND"
Packit 4511e4
#define DAV_PUT "PUT"
Packit 4511e4
#define DAV_UNLOCK "UNLOCK"
Packit 4511e4
#define DAV_DELETE "DELETE"
Packit 4511e4
Packit 4511e4
/* DAV lock flags */
Packit 4511e4
#define DAV_PROP_LOCKWR (1u << 0)
Packit 4511e4
#define DAV_PROP_LOCKEX (1u << 1)
Packit 4511e4
#define DAV_LOCK_OK (1u << 2)
Packit 4511e4
Packit 4511e4
/* DAV XML properties */
Packit 4511e4
#define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
Packit 4511e4
#define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
Packit 4511e4
#define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
Packit 4511e4
#define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
Packit 4511e4
#define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
Packit 4511e4
#define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
Packit 4511e4
#define DAV_PROPFIND_RESP ".multistatus.response"
Packit 4511e4
#define DAV_PROPFIND_NAME ".multistatus.response.href"
Packit 4511e4
#define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
Packit 4511e4
Packit 4511e4
/* DAV request body templates */
Packit 4511e4
#define PROPFIND_SUPPORTEDLOCK_REQUEST "\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
Packit 4511e4
#define PROPFIND_ALL_REQUEST "\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
Packit 4511e4
#define LOCK_REQUEST "\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
Packit 4511e4
Packit 4511e4
#define LOCK_TIME 600
Packit 4511e4
#define LOCK_REFRESH 30
Packit 4511e4
Packit 4511e4
/* Remember to update object flag allocation in object.h */
Packit 4511e4
#define LOCAL    (1u<<16)
Packit 4511e4
#define REMOTE   (1u<<17)
Packit 4511e4
#define FETCHING (1u<<18)
Packit 4511e4
#define PUSHING  (1u<<19)
Packit 4511e4
Packit 4511e4
/* We allow "recursive" symbolic refs. Only within reason, though */
Packit 4511e4
#define MAXDEPTH 5
Packit 4511e4
Packit 4511e4
static int pushing;
Packit 4511e4
static int aborted;
Packit 4511e4
static signed char remote_dir_exists[256];
Packit 4511e4
Packit 4511e4
static int push_verbosely;
Packit 4511e4
static int push_all = MATCH_REFS_NONE;
Packit 4511e4
static int force_all;
Packit 4511e4
static int dry_run;
Packit 4511e4
static int helper_status;
Packit 4511e4
Packit 4511e4
static struct object_list *objects;
Packit 4511e4
Packit 4511e4
struct repo {
Packit 4511e4
	char *url;
Packit 4511e4
	char *path;
Packit 4511e4
	int path_len;
Packit 4511e4
	int has_info_refs;
Packit 4511e4
	int can_update_info_refs;
Packit 4511e4
	int has_info_packs;
Packit 4511e4
	struct packed_git *packs;
Packit 4511e4
	struct remote_lock *locks;
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
static struct repo *repo;
Packit 4511e4
Packit 4511e4
enum transfer_state {
Packit 4511e4
	NEED_FETCH,
Packit 4511e4
	RUN_FETCH_LOOSE,
Packit 4511e4
	RUN_FETCH_PACKED,
Packit 4511e4
	NEED_PUSH,
Packit 4511e4
	RUN_MKCOL,
Packit 4511e4
	RUN_PUT,
Packit 4511e4
	RUN_MOVE,
Packit 4511e4
	ABORTED,
Packit 4511e4
	COMPLETE
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
struct transfer_request {
Packit 4511e4
	struct object *obj;
Packit 4511e4
	char *url;
Packit 4511e4
	char *dest;
Packit 4511e4
	struct remote_lock *lock;
Packit 4511e4
	struct curl_slist *headers;
Packit 4511e4
	struct buffer buffer;
Packit 4511e4
	enum transfer_state state;
Packit 4511e4
	CURLcode curl_result;
Packit 4511e4
	char errorstr[CURL_ERROR_SIZE];
Packit 4511e4
	long http_code;
Packit 4511e4
	void *userData;
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct transfer_request *next;
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
static struct transfer_request *request_queue_head;
Packit 4511e4
Packit 4511e4
struct xml_ctx {
Packit 4511e4
	char *name;
Packit 4511e4
	int len;
Packit 4511e4
	char *cdata;
Packit 4511e4
	void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
Packit 4511e4
	void *userData;
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
struct remote_lock {
Packit 4511e4
	char *url;
Packit 4511e4
	char *owner;
Packit 4511e4
	char *token;
Packit 4511e4
	char tmpfile_suffix[41];
Packit 4511e4
	time_t start_time;
Packit 4511e4
	long timeout;
Packit 4511e4
	int refreshing;
Packit 4511e4
	struct remote_lock *next;
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
/* Flags that control remote_ls processing */
Packit 4511e4
#define PROCESS_FILES (1u << 0)
Packit 4511e4
#define PROCESS_DIRS  (1u << 1)
Packit 4511e4
#define RECURSIVE     (1u << 2)
Packit 4511e4
Packit 4511e4
/* Flags that remote_ls passes to callback functions */
Packit 4511e4
#define IS_DIR (1u << 0)
Packit 4511e4
Packit 4511e4
struct remote_ls_ctx {
Packit 4511e4
	char *path;
Packit 4511e4
	void (*userFunc)(struct remote_ls_ctx *ls);
Packit 4511e4
	void *userData;
Packit 4511e4
	int flags;
Packit 4511e4
	char *dentry_name;
Packit 4511e4
	int dentry_flags;
Packit 4511e4
	struct remote_ls_ctx *parent;
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
/* get_dav_token_headers options */
Packit 4511e4
enum dav_header_flag {
Packit 4511e4
	DAV_HEADER_IF = (1u << 0),
Packit 4511e4
	DAV_HEADER_LOCK = (1u << 1),
Packit 4511e4
	DAV_HEADER_TIMEOUT = (1u << 2)
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
static char *xml_entities(const char *s)
Packit 4511e4
{
Packit 4511e4
	struct strbuf buf = STRBUF_INIT;
Packit 4511e4
	strbuf_addstr_xml_quoted(&buf, s);
Packit 4511e4
	return strbuf_detach(&buf, NULL);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void curl_setup_http_get(CURL *curl, const char *url,
Packit 4511e4
		const char *custom_req)
Packit 4511e4
{
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_URL, url);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void curl_setup_http(CURL *curl, const char *url,
Packit 4511e4
		const char *custom_req, struct buffer *buffer,
Packit 4511e4
		curl_write_callback write_fn)
Packit 4511e4
{
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_PUT, 1);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_URL, url);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
Packit 4511e4
#ifndef NO_CURL_IOCTL
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_IOCTLDATA, buffer);
Packit 4511e4
#endif
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
Packit 4511e4
	curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
Packit 4511e4
{
Packit 4511e4
	struct strbuf buf = STRBUF_INIT;
Packit 4511e4
	struct curl_slist *dav_headers = http_copy_default_headers();
Packit 4511e4
Packit 4511e4
	if (options & DAV_HEADER_IF) {
Packit 4511e4
		strbuf_addf(&buf, "If: (<%s>)", lock->token);
Packit 4511e4
		dav_headers = curl_slist_append(dav_headers, buf.buf);
Packit 4511e4
		strbuf_reset(&buf;;
Packit 4511e4
	}
Packit 4511e4
	if (options & DAV_HEADER_LOCK) {
Packit 4511e4
		strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
Packit 4511e4
		dav_headers = curl_slist_append(dav_headers, buf.buf);
Packit 4511e4
		strbuf_reset(&buf;;
Packit 4511e4
	}
Packit 4511e4
	if (options & DAV_HEADER_TIMEOUT) {
Packit 4511e4
		strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
Packit 4511e4
		dav_headers = curl_slist_append(dav_headers, buf.buf);
Packit 4511e4
		strbuf_reset(&buf;;
Packit 4511e4
	}
Packit 4511e4
	strbuf_release(&buf;;
Packit 4511e4
Packit 4511e4
	return dav_headers;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void finish_request(struct transfer_request *request);
Packit 4511e4
static void release_request(struct transfer_request *request);
Packit 4511e4
Packit 4511e4
static void process_response(void *callback_data)
Packit 4511e4
{
Packit 4511e4
	struct transfer_request *request =
Packit 4511e4
		(struct transfer_request *)callback_data;
Packit 4511e4
Packit 4511e4
	finish_request(request);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
#ifdef USE_CURL_MULTI
Packit 4511e4
Packit 4511e4
static void start_fetch_loose(struct transfer_request *request)
Packit 4511e4
{
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct http_object_request *obj_req;
Packit 4511e4
Packit 4511e4
	obj_req = new_http_object_request(repo->url, request->obj->oid.hash);
Packit 4511e4
	if (obj_req == NULL) {
Packit 4511e4
		request->state = ABORTED;
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	slot = obj_req->slot;
Packit 4511e4
	slot->callback_func = process_response;
Packit 4511e4
	slot->callback_data = request;
Packit 4511e4
	request->slot = slot;
Packit 4511e4
	request->userData = obj_req;
Packit 4511e4
Packit 4511e4
	/* Try to get the request started, abort the request on error */
Packit 4511e4
	request->state = RUN_FETCH_LOOSE;
Packit 4511e4
	if (!start_active_slot(slot)) {
Packit 4511e4
		fprintf(stderr, "Unable to start GET request\n");
Packit 4511e4
		repo->can_update_info_refs = 0;
Packit 4511e4
		release_http_object_request(obj_req);
Packit 4511e4
		release_request(request);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void start_mkcol(struct transfer_request *request)
Packit 4511e4
{
Packit 4511e4
	char *hex = oid_to_hex(&request->obj->oid);
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
Packit 4511e4
	request->url = get_remote_object_url(repo->url, hex, 1);
Packit 4511e4
Packit 4511e4
	slot = get_active_slot();
Packit 4511e4
	slot->callback_func = process_response;
Packit 4511e4
	slot->callback_data = request;
Packit 4511e4
	curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
Packit 4511e4
Packit 4511e4
	if (start_active_slot(slot)) {
Packit 4511e4
		request->slot = slot;
Packit 4511e4
		request->state = RUN_MKCOL;
Packit 4511e4
	} else {
Packit 4511e4
		request->state = ABORTED;
Packit 4511e4
		FREE_AND_NULL(request->url);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
#endif
Packit 4511e4
Packit 4511e4
static void start_fetch_packed(struct transfer_request *request)
Packit 4511e4
{
Packit 4511e4
	struct packed_git *target;
Packit 4511e4
Packit 4511e4
	struct transfer_request *check_request = request_queue_head;
Packit 4511e4
	struct http_pack_request *preq;
Packit 4511e4
Packit 4511e4
	target = find_sha1_pack(request->obj->oid.hash, repo->packs);
Packit 4511e4
	if (!target) {
Packit 4511e4
		fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request->obj->oid));
Packit 4511e4
		repo->can_update_info_refs = 0;
Packit 4511e4
		release_request(request);
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	fprintf(stderr,	"Fetching pack %s\n", sha1_to_hex(target->sha1));
Packit 4511e4
	fprintf(stderr, " which contains %s\n", oid_to_hex(&request->obj->oid));
Packit 4511e4
Packit 4511e4
	preq = new_http_pack_request(target, repo->url);
Packit 4511e4
	if (preq == NULL) {
Packit 4511e4
		repo->can_update_info_refs = 0;
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
	preq->lst = &repo->packs;
Packit 4511e4
Packit 4511e4
	/* Make sure there isn't another open request for this pack */
Packit 4511e4
	while (check_request) {
Packit 4511e4
		if (check_request->state == RUN_FETCH_PACKED &&
Packit 4511e4
		    !strcmp(check_request->url, preq->url)) {
Packit 4511e4
			release_http_pack_request(preq);
Packit 4511e4
			release_request(request);
Packit 4511e4
			return;
Packit 4511e4
		}
Packit 4511e4
		check_request = check_request->next;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	preq->slot->callback_func = process_response;
Packit 4511e4
	preq->slot->callback_data = request;
Packit 4511e4
	request->slot = preq->slot;
Packit 4511e4
	request->userData = preq;
Packit 4511e4
Packit 4511e4
	/* Try to get the request started, abort the request on error */
Packit 4511e4
	request->state = RUN_FETCH_PACKED;
Packit 4511e4
	if (!start_active_slot(preq->slot)) {
Packit 4511e4
		fprintf(stderr, "Unable to start GET request\n");
Packit 4511e4
		release_http_pack_request(preq);
Packit 4511e4
		repo->can_update_info_refs = 0;
Packit 4511e4
		release_request(request);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void start_put(struct transfer_request *request)
Packit 4511e4
{
Packit 4511e4
	char *hex = oid_to_hex(&request->obj->oid);
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct strbuf buf = STRBUF_INIT;
Packit 4511e4
	enum object_type type;
Packit 4511e4
	char hdr[50];
Packit 4511e4
	void *unpacked;
Packit 4511e4
	unsigned long len;
Packit 4511e4
	int hdrlen;
Packit 4511e4
	ssize_t size;
Packit 4511e4
	git_zstream stream;
Packit 4511e4
Packit 4511e4
	unpacked = read_object_file(&request->obj->oid, &type, &len;;
Packit 4511e4
	hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(type), len) + 1;
Packit 4511e4
Packit 4511e4
	/* Set it up */
Packit 4511e4
	git_deflate_init(&stream, zlib_compression_level);
Packit 4511e4
	size = git_deflate_bound(&stream, len + hdrlen);
Packit 4511e4
	strbuf_init(&request->buffer.buf, size);
Packit 4511e4
	request->buffer.posn = 0;
Packit 4511e4
Packit 4511e4
	/* Compress it */
Packit 4511e4
	stream.next_out = (unsigned char *)request->buffer.buf.buf;
Packit 4511e4
	stream.avail_out = size;
Packit 4511e4
Packit 4511e4
	/* First header.. */
Packit 4511e4
	stream.next_in = (void *)hdr;
Packit 4511e4
	stream.avail_in = hdrlen;
Packit 4511e4
	while (git_deflate(&stream, 0) == Z_OK)
Packit 4511e4
		; /* nothing */
Packit 4511e4
Packit 4511e4
	/* Then the data itself.. */
Packit 4511e4
	stream.next_in = unpacked;
Packit 4511e4
	stream.avail_in = len;
Packit 4511e4
	while (git_deflate(&stream, Z_FINISH) == Z_OK)
Packit 4511e4
		; /* nothing */
Packit 4511e4
	git_deflate_end(&stream);
Packit 4511e4
	free(unpacked);
Packit 4511e4
Packit 4511e4
	request->buffer.buf.len = stream.total_out;
Packit 4511e4
Packit 4511e4
	strbuf_addstr(&buf, "Destination: ");
Packit 4511e4
	append_remote_object_url(&buf, repo->url, hex, 0);
Packit 4511e4
	request->dest = strbuf_detach(&buf, NULL);
Packit 4511e4
Packit 4511e4
	append_remote_object_url(&buf, repo->url, hex, 0);
Packit 4511e4
	strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
Packit 4511e4
	request->url = strbuf_detach(&buf, NULL);
Packit 4511e4
Packit 4511e4
	slot = get_active_slot();
Packit 4511e4
	slot->callback_func = process_response;
Packit 4511e4
	slot->callback_data = request;
Packit 4511e4
	curl_setup_http(slot->curl, request->url, DAV_PUT,
Packit 4511e4
			&request->buffer, fwrite_null);
Packit 4511e4
Packit 4511e4
	if (start_active_slot(slot)) {
Packit 4511e4
		request->slot = slot;
Packit 4511e4
		request->state = RUN_PUT;
Packit 4511e4
	} else {
Packit 4511e4
		request->state = ABORTED;
Packit 4511e4
		FREE_AND_NULL(request->url);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void start_move(struct transfer_request *request)
Packit 4511e4
{
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct curl_slist *dav_headers = http_copy_default_headers();
Packit 4511e4
Packit 4511e4
	slot = get_active_slot();
Packit 4511e4
	slot->callback_func = process_response;
Packit 4511e4
	slot->callback_data = request;
Packit 4511e4
	curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
Packit 4511e4
	dav_headers = curl_slist_append(dav_headers, request->dest);
Packit 4511e4
	dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Packit 4511e4
Packit 4511e4
	if (start_active_slot(slot)) {
Packit 4511e4
		request->slot = slot;
Packit 4511e4
		request->state = RUN_MOVE;
Packit 4511e4
	} else {
Packit 4511e4
		request->state = ABORTED;
Packit 4511e4
		FREE_AND_NULL(request->url);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int refresh_lock(struct remote_lock *lock)
Packit 4511e4
{
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct slot_results results;
Packit 4511e4
	struct curl_slist *dav_headers;
Packit 4511e4
	int rc = 0;
Packit 4511e4
Packit 4511e4
	lock->refreshing = 1;
Packit 4511e4
Packit 4511e4
	dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
Packit 4511e4
Packit 4511e4
	slot = get_active_slot();
Packit 4511e4
	slot->results = &results;
Packit 4511e4
	curl_setup_http_get(slot->curl, lock->url, DAV_LOCK);
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Packit 4511e4
Packit 4511e4
	if (start_active_slot(slot)) {
Packit 4511e4
		run_active_slot(slot);
Packit 4511e4
		if (results.curl_result != CURLE_OK) {
Packit 4511e4
			fprintf(stderr, "LOCK HTTP error %ld\n",
Packit 4511e4
				results.http_code);
Packit 4511e4
		} else {
Packit 4511e4
			lock->start_time = time(NULL);
Packit 4511e4
			rc = 1;
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	lock->refreshing = 0;
Packit 4511e4
	curl_slist_free_all(dav_headers);
Packit 4511e4
Packit 4511e4
	return rc;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void check_locks(void)
Packit 4511e4
{
Packit 4511e4
	struct remote_lock *lock = repo->locks;
Packit 4511e4
	time_t current_time = time(NULL);
Packit 4511e4
	int time_remaining;
Packit 4511e4
Packit 4511e4
	while (lock) {
Packit 4511e4
		time_remaining = lock->start_time + lock->timeout -
Packit 4511e4
			current_time;
Packit 4511e4
		if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
Packit 4511e4
			if (!refresh_lock(lock)) {
Packit 4511e4
				fprintf(stderr,
Packit 4511e4
					"Unable to refresh lock for %s\n",
Packit 4511e4
					lock->url);
Packit 4511e4
				aborted = 1;
Packit 4511e4
				return;
Packit 4511e4
			}
Packit 4511e4
		}
Packit 4511e4
		lock = lock->next;
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void release_request(struct transfer_request *request)
Packit 4511e4
{
Packit 4511e4
	struct transfer_request *entry = request_queue_head;
Packit 4511e4
Packit 4511e4
	if (request == request_queue_head) {
Packit 4511e4
		request_queue_head = request->next;
Packit 4511e4
	} else {
Packit 4511e4
		while (entry->next != NULL && entry->next != request)
Packit 4511e4
			entry = entry->next;
Packit 4511e4
		if (entry->next == request)
Packit 4511e4
			entry->next = entry->next->next;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	free(request->url);
Packit 4511e4
	free(request);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void finish_request(struct transfer_request *request)
Packit 4511e4
{
Packit 4511e4
	struct http_pack_request *preq;
Packit 4511e4
	struct http_object_request *obj_req;
Packit 4511e4
Packit 4511e4
	request->curl_result = request->slot->curl_result;
Packit 4511e4
	request->http_code = request->slot->http_code;
Packit 4511e4
	request->slot = NULL;
Packit 4511e4
Packit 4511e4
	/* Keep locks active */
Packit 4511e4
	check_locks();
Packit 4511e4
Packit 4511e4
	if (request->headers != NULL)
Packit 4511e4
		curl_slist_free_all(request->headers);
Packit 4511e4
Packit 4511e4
	/* URL is reused for MOVE after PUT */
Packit 4511e4
	if (request->state != RUN_PUT) {
Packit 4511e4
		FREE_AND_NULL(request->url);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	if (request->state == RUN_MKCOL) {
Packit 4511e4
		if (request->curl_result == CURLE_OK ||
Packit 4511e4
		    request->http_code == 405) {
Packit 4511e4
			remote_dir_exists[request->obj->oid.hash[0]] = 1;
Packit 4511e4
			start_put(request);
Packit 4511e4
		} else {
Packit 4511e4
			fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
Packit 4511e4
				oid_to_hex(&request->obj->oid),
Packit 4511e4
				request->curl_result, request->http_code);
Packit 4511e4
			request->state = ABORTED;
Packit 4511e4
			aborted = 1;
Packit 4511e4
		}
Packit 4511e4
	} else if (request->state == RUN_PUT) {
Packit 4511e4
		if (request->curl_result == CURLE_OK) {
Packit 4511e4
			start_move(request);
Packit 4511e4
		} else {
Packit 4511e4
			fprintf(stderr,	"PUT %s failed, aborting (%d/%ld)\n",
Packit 4511e4
				oid_to_hex(&request->obj->oid),
Packit 4511e4
				request->curl_result, request->http_code);
Packit 4511e4
			request->state = ABORTED;
Packit 4511e4
			aborted = 1;
Packit 4511e4
		}
Packit 4511e4
	} else if (request->state == RUN_MOVE) {
Packit 4511e4
		if (request->curl_result == CURLE_OK) {
Packit 4511e4
			if (push_verbosely)
Packit 4511e4
				fprintf(stderr, "    sent %s\n",
Packit 4511e4
					oid_to_hex(&request->obj->oid));
Packit 4511e4
			request->obj->flags |= REMOTE;
Packit 4511e4
			release_request(request);
Packit 4511e4
		} else {
Packit 4511e4
			fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
Packit 4511e4
				oid_to_hex(&request->obj->oid),
Packit 4511e4
				request->curl_result, request->http_code);
Packit 4511e4
			request->state = ABORTED;
Packit 4511e4
			aborted = 1;
Packit 4511e4
		}
Packit 4511e4
	} else if (request->state == RUN_FETCH_LOOSE) {
Packit 4511e4
		obj_req = (struct http_object_request *)request->userData;
Packit 4511e4
Packit 4511e4
		if (finish_http_object_request(obj_req) == 0)
Packit 4511e4
			if (obj_req->rename == 0)
Packit 4511e4
				request->obj->flags |= (LOCAL | REMOTE);
Packit 4511e4
Packit 4511e4
		/* Try fetching packed if necessary */
Packit 4511e4
		if (request->obj->flags & LOCAL) {
Packit 4511e4
			release_http_object_request(obj_req);
Packit 4511e4
			release_request(request);
Packit 4511e4
		} else
Packit 4511e4
			start_fetch_packed(request);
Packit 4511e4
Packit 4511e4
	} else if (request->state == RUN_FETCH_PACKED) {
Packit 4511e4
		int fail = 1;
Packit 4511e4
		if (request->curl_result != CURLE_OK) {
Packit 4511e4
			fprintf(stderr, "Unable to get pack file %s\n%s",
Packit 4511e4
				request->url, curl_errorstr);
Packit 4511e4
		} else {
Packit 4511e4
			preq = (struct http_pack_request *)request->userData;
Packit 4511e4
Packit 4511e4
			if (preq) {
Packit 4511e4
				if (finish_http_pack_request(preq) == 0)
Packit 4511e4
					fail = 0;
Packit 4511e4
				release_http_pack_request(preq);
Packit 4511e4
			}
Packit 4511e4
		}
Packit 4511e4
		if (fail)
Packit 4511e4
			repo->can_update_info_refs = 0;
Packit 4511e4
		release_request(request);
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
#ifdef USE_CURL_MULTI
Packit 4511e4
static int is_running_queue;
Packit 4511e4
static int fill_active_slot(void *unused)
Packit 4511e4
{
Packit 4511e4
	struct transfer_request *request;
Packit 4511e4
Packit 4511e4
	if (aborted || !is_running_queue)
Packit 4511e4
		return 0;
Packit 4511e4
Packit 4511e4
	for (request = request_queue_head; request; request = request->next) {
Packit 4511e4
		if (request->state == NEED_FETCH) {
Packit 4511e4
			start_fetch_loose(request);
Packit 4511e4
			return 1;
Packit 4511e4
		} else if (pushing && request->state == NEED_PUSH) {
Packit 4511e4
			if (remote_dir_exists[request->obj->oid.hash[0]] == 1) {
Packit 4511e4
				start_put(request);
Packit 4511e4
			} else {
Packit 4511e4
				start_mkcol(request);
Packit 4511e4
			}
Packit 4511e4
			return 1;
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
#endif
Packit 4511e4
Packit 4511e4
static void get_remote_object_list(unsigned char parent);
Packit 4511e4
Packit 4511e4
static void add_fetch_request(struct object *obj)
Packit 4511e4
{
Packit 4511e4
	struct transfer_request *request;
Packit 4511e4
Packit 4511e4
	check_locks();
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * Don't fetch the object if it's known to exist locally
Packit 4511e4
	 * or is already in the request queue
Packit 4511e4
	 */
Packit 4511e4
	if (remote_dir_exists[obj->oid.hash[0]] == -1)
Packit 4511e4
		get_remote_object_list(obj->oid.hash[0]);
Packit 4511e4
	if (obj->flags & (LOCAL | FETCHING))
Packit 4511e4
		return;
Packit 4511e4
Packit 4511e4
	obj->flags |= FETCHING;
Packit 4511e4
	request = xmalloc(sizeof(*request));
Packit 4511e4
	request->obj = obj;
Packit 4511e4
	request->url = NULL;
Packit 4511e4
	request->lock = NULL;
Packit 4511e4
	request->headers = NULL;
Packit 4511e4
	request->state = NEED_FETCH;
Packit 4511e4
	request->next = request_queue_head;
Packit 4511e4
	request_queue_head = request;
Packit 4511e4
Packit 4511e4
#ifdef USE_CURL_MULTI
Packit 4511e4
	fill_active_slots();
Packit 4511e4
	step_active_slots();
Packit 4511e4
#endif
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int add_send_request(struct object *obj, struct remote_lock *lock)
Packit 4511e4
{
Packit 4511e4
	struct transfer_request *request;
Packit 4511e4
	struct packed_git *target;
Packit 4511e4
Packit 4511e4
	/* Keep locks active */
Packit 4511e4
	check_locks();
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * Don't push the object if it's known to exist on the remote
Packit 4511e4
	 * or is already in the request queue
Packit 4511e4
	 */
Packit 4511e4
	if (remote_dir_exists[obj->oid.hash[0]] == -1)
Packit 4511e4
		get_remote_object_list(obj->oid.hash[0]);
Packit 4511e4
	if (obj->flags & (REMOTE | PUSHING))
Packit 4511e4
		return 0;
Packit 4511e4
	target = find_sha1_pack(obj->oid.hash, repo->packs);
Packit 4511e4
	if (target) {
Packit 4511e4
		obj->flags |= REMOTE;
Packit 4511e4
		return 0;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	obj->flags |= PUSHING;
Packit 4511e4
	request = xmalloc(sizeof(*request));
Packit 4511e4
	request->obj = obj;
Packit 4511e4
	request->url = NULL;
Packit 4511e4
	request->lock = lock;
Packit 4511e4
	request->headers = NULL;
Packit 4511e4
	request->state = NEED_PUSH;
Packit 4511e4
	request->next = request_queue_head;
Packit 4511e4
	request_queue_head = request;
Packit 4511e4
Packit 4511e4
#ifdef USE_CURL_MULTI
Packit 4511e4
	fill_active_slots();
Packit 4511e4
	step_active_slots();
Packit 4511e4
#endif
Packit 4511e4
Packit 4511e4
	return 1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int fetch_indices(void)
Packit 4511e4
{
Packit 4511e4
	int ret;
Packit 4511e4
Packit 4511e4
	if (push_verbosely)
Packit 4511e4
		fprintf(stderr, "Getting pack list\n");
Packit 4511e4
Packit 4511e4
	switch (http_get_info_packs(repo->url, &repo->packs)) {
Packit 4511e4
	case HTTP_OK:
Packit 4511e4
	case HTTP_MISSING_TARGET:
Packit 4511e4
		ret = 0;
Packit 4511e4
		break;
Packit 4511e4
	default:
Packit 4511e4
		ret = -1;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return ret;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void one_remote_object(const struct object_id *oid)
Packit 4511e4
{
Packit 4511e4
	struct object *obj;
Packit 4511e4
Packit 4511e4
	obj = lookup_object(oid->hash);
Packit 4511e4
	if (!obj)
Packit 4511e4
		obj = parse_object(oid);
Packit 4511e4
Packit 4511e4
	/* Ignore remote objects that don't exist locally */
Packit 4511e4
	if (!obj)
Packit 4511e4
		return;
Packit 4511e4
Packit 4511e4
	obj->flags |= REMOTE;
Packit 4511e4
	if (!object_list_contains(objects, obj))
Packit 4511e4
		object_list_insert(obj, &objects);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
Packit 4511e4
{
Packit 4511e4
	int *lock_flags = (int *)ctx->userData;
Packit 4511e4
Packit 4511e4
	if (tag_closed) {
Packit 4511e4
		if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
Packit 4511e4
			if ((*lock_flags & DAV_PROP_LOCKEX) &&
Packit 4511e4
			    (*lock_flags & DAV_PROP_LOCKWR)) {
Packit 4511e4
				*lock_flags |= DAV_LOCK_OK;
Packit 4511e4
			}
Packit 4511e4
			*lock_flags &= DAV_LOCK_OK;
Packit 4511e4
		} else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
Packit 4511e4
			*lock_flags |= DAV_PROP_LOCKWR;
Packit 4511e4
		} else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
Packit 4511e4
			*lock_flags |= DAV_PROP_LOCKEX;
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
Packit 4511e4
{
Packit 4511e4
	struct remote_lock *lock = (struct remote_lock *)ctx->userData;
Packit 4511e4
	git_SHA_CTX sha_ctx;
Packit 4511e4
	unsigned char lock_token_sha1[20];
Packit 4511e4
Packit 4511e4
	if (tag_closed && ctx->cdata) {
Packit 4511e4
		if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
Packit 4511e4
			lock->owner = xstrdup(ctx->cdata);
Packit 4511e4
		} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
Packit 4511e4
			const char *arg;
Packit 4511e4
			if (skip_prefix(ctx->cdata, "Second-", &arg))
Packit 4511e4
				lock->timeout = strtol(arg, NULL, 10);
Packit 4511e4
		} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
Packit 4511e4
			lock->token = xstrdup(ctx->cdata);
Packit 4511e4
Packit 4511e4
			git_SHA1_Init(&sha_ctx);
Packit 4511e4
			git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
Packit 4511e4
			git_SHA1_Final(lock_token_sha1, &sha_ctx);
Packit 4511e4
Packit 4511e4
			lock->tmpfile_suffix[0] = '_';
Packit 4511e4
			memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void one_remote_ref(const char *refname);
Packit 4511e4
Packit 4511e4
static void
Packit 4511e4
xml_start_tag(void *userData, const char *name, const char **atts)
Packit 4511e4
{
Packit 4511e4
	struct xml_ctx *ctx = (struct xml_ctx *)userData;
Packit 4511e4
	const char *c = strchr(name, ':');
Packit 4511e4
	int old_namelen, new_len;
Packit 4511e4
Packit 4511e4
	if (c == NULL)
Packit 4511e4
		c = name;
Packit 4511e4
	else
Packit 4511e4
		c++;
Packit 4511e4
Packit 4511e4
	old_namelen = strlen(ctx->name);
Packit 4511e4
	new_len = old_namelen + strlen(c) + 2;
Packit 4511e4
Packit 4511e4
	if (new_len > ctx->len) {
Packit 4511e4
		ctx->name = xrealloc(ctx->name, new_len);
Packit 4511e4
		ctx->len = new_len;
Packit 4511e4
	}
Packit 4511e4
	xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
Packit 4511e4
Packit 4511e4
	FREE_AND_NULL(ctx->cdata);
Packit 4511e4
Packit 4511e4
	ctx->userFunc(ctx, 0);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void
Packit 4511e4
xml_end_tag(void *userData, const char *name)
Packit 4511e4
{
Packit 4511e4
	struct xml_ctx *ctx = (struct xml_ctx *)userData;
Packit 4511e4
	const char *c = strchr(name, ':');
Packit 4511e4
	char *ep;
Packit 4511e4
Packit 4511e4
	ctx->userFunc(ctx, 1);
Packit 4511e4
Packit 4511e4
	if (c == NULL)
Packit 4511e4
		c = name;
Packit 4511e4
	else
Packit 4511e4
		c++;
Packit 4511e4
Packit 4511e4
	ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
Packit 4511e4
	*ep = 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void
Packit 4511e4
xml_cdata(void *userData, const XML_Char *s, int len)
Packit 4511e4
{
Packit 4511e4
	struct xml_ctx *ctx = (struct xml_ctx *)userData;
Packit 4511e4
	free(ctx->cdata);
Packit 4511e4
	ctx->cdata = xmemdupz(s, len);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static struct remote_lock *lock_remote(const char *path, long timeout)
Packit 4511e4
{
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct slot_results results;
Packit 4511e4
	struct buffer out_buffer = { STRBUF_INIT, 0 };
Packit 4511e4
	struct strbuf in_buffer = STRBUF_INIT;
Packit 4511e4
	char *url;
Packit 4511e4
	char *ep;
Packit 4511e4
	char timeout_header[25];
Packit 4511e4
	struct remote_lock *lock = NULL;
Packit 4511e4
	struct curl_slist *dav_headers = http_copy_default_headers();
Packit 4511e4
	struct xml_ctx ctx;
Packit 4511e4
	char *escaped;
Packit 4511e4
Packit 4511e4
	url = xstrfmt("%s%s", repo->url, path);
Packit 4511e4
Packit 4511e4
	/* Make sure leading directories exist for the remote ref */
Packit 4511e4
	ep = strchr(url + strlen(repo->url) + 1, '/');
Packit 4511e4
	while (ep) {
Packit 4511e4
		char saved_character = ep[1];
Packit 4511e4
		ep[1] = '\0';
Packit 4511e4
		slot = get_active_slot();
Packit 4511e4
		slot->results = &results;
Packit 4511e4
		curl_setup_http_get(slot->curl, url, DAV_MKCOL);
Packit 4511e4
		if (start_active_slot(slot)) {
Packit 4511e4
			run_active_slot(slot);
Packit 4511e4
			if (results.curl_result != CURLE_OK &&
Packit 4511e4
			    results.http_code != 405) {
Packit 4511e4
				fprintf(stderr,
Packit 4511e4
					"Unable to create branch path %s\n",
Packit 4511e4
					url);
Packit 4511e4
				free(url);
Packit 4511e4
				return NULL;
Packit 4511e4
			}
Packit 4511e4
		} else {
Packit 4511e4
			fprintf(stderr, "Unable to start MKCOL request\n");
Packit 4511e4
			free(url);
Packit 4511e4
			return NULL;
Packit 4511e4
		}
Packit 4511e4
		ep[1] = saved_character;
Packit 4511e4
		ep = strchr(ep + 1, '/');
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	escaped = xml_entities(ident_default_email());
Packit 4511e4
	strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
Packit 4511e4
	free(escaped);
Packit 4511e4
Packit 4511e4
	xsnprintf(timeout_header, sizeof(timeout_header), "Timeout: Second-%ld", timeout);
Packit 4511e4
	dav_headers = curl_slist_append(dav_headers, timeout_header);
Packit 4511e4
	dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
Packit 4511e4
Packit 4511e4
	slot = get_active_slot();
Packit 4511e4
	slot->results = &results;
Packit 4511e4
	curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
Packit 4511e4
Packit 4511e4
	lock = xcalloc(1, sizeof(*lock));
Packit 4511e4
	lock->timeout = -1;
Packit 4511e4
Packit 4511e4
	if (start_active_slot(slot)) {
Packit 4511e4
		run_active_slot(slot);
Packit 4511e4
		if (results.curl_result == CURLE_OK) {
Packit 4511e4
			XML_Parser parser = XML_ParserCreate(NULL);
Packit 4511e4
			enum XML_Status result;
Packit 4511e4
			ctx.name = xcalloc(10, 1);
Packit 4511e4
			ctx.len = 0;
Packit 4511e4
			ctx.cdata = NULL;
Packit 4511e4
			ctx.userFunc = handle_new_lock_ctx;
Packit 4511e4
			ctx.userData = lock;
Packit 4511e4
			XML_SetUserData(parser, &ctx;;
Packit 4511e4
			XML_SetElementHandler(parser, xml_start_tag,
Packit 4511e4
					      xml_end_tag);
Packit 4511e4
			XML_SetCharacterDataHandler(parser, xml_cdata);
Packit 4511e4
			result = XML_Parse(parser, in_buffer.buf,
Packit 4511e4
					   in_buffer.len, 1);
Packit 4511e4
			free(ctx.name);
Packit 4511e4
			if (result != XML_STATUS_OK) {
Packit 4511e4
				fprintf(stderr, "XML error: %s\n",
Packit 4511e4
					XML_ErrorString(
Packit 4511e4
						XML_GetErrorCode(parser)));
Packit 4511e4
				lock->timeout = -1;
Packit 4511e4
			}
Packit 4511e4
			XML_ParserFree(parser);
Packit 4511e4
		} else {
Packit 4511e4
			fprintf(stderr,
Packit 4511e4
				"error: curl result=%d, HTTP code=%ld\n",
Packit 4511e4
				results.curl_result, results.http_code);
Packit 4511e4
		}
Packit 4511e4
	} else {
Packit 4511e4
		fprintf(stderr, "Unable to start LOCK request\n");
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	curl_slist_free_all(dav_headers);
Packit 4511e4
	strbuf_release(&out_buffer.buf);
Packit 4511e4
	strbuf_release(&in_buffer);
Packit 4511e4
Packit 4511e4
	if (lock->token == NULL || lock->timeout <= 0) {
Packit 4511e4
		free(lock->token);
Packit 4511e4
		free(lock->owner);
Packit 4511e4
		free(url);
Packit 4511e4
		FREE_AND_NULL(lock);
Packit 4511e4
	} else {
Packit 4511e4
		lock->url = url;
Packit 4511e4
		lock->start_time = time(NULL);
Packit 4511e4
		lock->next = repo->locks;
Packit 4511e4
		repo->locks = lock;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return lock;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int unlock_remote(struct remote_lock *lock)
Packit 4511e4
{
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct slot_results results;
Packit 4511e4
	struct remote_lock *prev = repo->locks;
Packit 4511e4
	struct curl_slist *dav_headers;
Packit 4511e4
	int rc = 0;
Packit 4511e4
Packit 4511e4
	dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
Packit 4511e4
Packit 4511e4
	slot = get_active_slot();
Packit 4511e4
	slot->results = &results;
Packit 4511e4
	curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Packit 4511e4
Packit 4511e4
	if (start_active_slot(slot)) {
Packit 4511e4
		run_active_slot(slot);
Packit 4511e4
		if (results.curl_result == CURLE_OK)
Packit 4511e4
			rc = 1;
Packit 4511e4
		else
Packit 4511e4
			fprintf(stderr, "UNLOCK HTTP error %ld\n",
Packit 4511e4
				results.http_code);
Packit 4511e4
	} else {
Packit 4511e4
		fprintf(stderr, "Unable to start UNLOCK request\n");
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	curl_slist_free_all(dav_headers);
Packit 4511e4
Packit 4511e4
	if (repo->locks == lock) {
Packit 4511e4
		repo->locks = lock->next;
Packit 4511e4
	} else {
Packit 4511e4
		while (prev && prev->next != lock)
Packit 4511e4
			prev = prev->next;
Packit 4511e4
		if (prev)
Packit 4511e4
			prev->next = prev->next->next;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	free(lock->owner);
Packit 4511e4
	free(lock->url);
Packit 4511e4
	free(lock->token);
Packit 4511e4
	free(lock);
Packit 4511e4
Packit 4511e4
	return rc;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void remove_locks(void)
Packit 4511e4
{
Packit 4511e4
	struct remote_lock *lock = repo->locks;
Packit 4511e4
Packit 4511e4
	fprintf(stderr, "Removing remote locks...\n");
Packit 4511e4
	while (lock) {
Packit 4511e4
		struct remote_lock *next = lock->next;
Packit 4511e4
		unlock_remote(lock);
Packit 4511e4
		lock = next;
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void remove_locks_on_signal(int signo)
Packit 4511e4
{
Packit 4511e4
	remove_locks();
Packit 4511e4
	sigchain_pop(signo);
Packit 4511e4
	raise(signo);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void remote_ls(const char *path, int flags,
Packit 4511e4
		      void (*userFunc)(struct remote_ls_ctx *ls),
Packit 4511e4
		      void *userData);
Packit 4511e4
Packit 4511e4
/* extract hex from sharded "xx/x{38}" filename */
Packit 4511e4
static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
Packit 4511e4
{
Packit 4511e4
	if (strlen(path) != GIT_SHA1_HEXSZ + 1)
Packit 4511e4
		return -1;
Packit 4511e4
Packit 4511e4
	if (hex_to_bytes(oid->hash, path, 1))
Packit 4511e4
		return -1;
Packit 4511e4
	path += 2;
Packit 4511e4
	path++; /* skip '/' */
Packit 4511e4
Packit 4511e4
	return hex_to_bytes(oid->hash + 1, path, GIT_SHA1_RAWSZ - 1);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void process_ls_object(struct remote_ls_ctx *ls)
Packit 4511e4
{
Packit 4511e4
	unsigned int *parent = (unsigned int *)ls->userData;
Packit 4511e4
	const char *path = ls->dentry_name;
Packit 4511e4
	struct object_id oid;
Packit 4511e4
Packit 4511e4
	if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
Packit 4511e4
		remote_dir_exists[*parent] = 1;
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	if (!skip_prefix(path, "objects/", &path) ||
Packit 4511e4
	    get_oid_hex_from_objpath(path, &oid))
Packit 4511e4
		return;
Packit 4511e4
Packit 4511e4
	one_remote_object(&oid;;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void process_ls_ref(struct remote_ls_ctx *ls)
Packit 4511e4
{
Packit 4511e4
	if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
Packit 4511e4
		fprintf(stderr, "  %s\n", ls->dentry_name);
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	if (!(ls->dentry_flags & IS_DIR))
Packit 4511e4
		one_remote_ref(ls->dentry_name);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
Packit 4511e4
{
Packit 4511e4
	struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
Packit 4511e4
Packit 4511e4
	if (tag_closed) {
Packit 4511e4
		if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
Packit 4511e4
			if (ls->dentry_flags & IS_DIR) {
Packit 4511e4
Packit 4511e4
				/* ensure collection names end with slash */
Packit 4511e4
				str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
Packit 4511e4
Packit 4511e4
				if (ls->flags & PROCESS_DIRS) {
Packit 4511e4
					ls->userFunc(ls);
Packit 4511e4
				}
Packit 4511e4
				if (strcmp(ls->dentry_name, ls->path) &&
Packit 4511e4
				    ls->flags & RECURSIVE) {
Packit 4511e4
					remote_ls(ls->dentry_name,
Packit 4511e4
						  ls->flags,
Packit 4511e4
						  ls->userFunc,
Packit 4511e4
						  ls->userData);
Packit 4511e4
				}
Packit 4511e4
			} else if (ls->flags & PROCESS_FILES) {
Packit 4511e4
				ls->userFunc(ls);
Packit 4511e4
			}
Packit 4511e4
		} else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
Packit 4511e4
			char *path = ctx->cdata;
Packit 4511e4
			if (*ctx->cdata == 'h') {
Packit 4511e4
				path = strstr(path, "//");
Packit 4511e4
				if (path) {
Packit 4511e4
					path = strchr(path+2, '/');
Packit 4511e4
				}
Packit 4511e4
			}
Packit 4511e4
			if (path) {
Packit 4511e4
				const char *url = repo->url;
Packit 4511e4
				if (repo->path)
Packit 4511e4
					url = repo->path;
Packit 4511e4
				if (strncmp(path, url, repo->path_len))
Packit 4511e4
					error("Parsed path '%s' does not match url: '%s'",
Packit 4511e4
					      path, url);
Packit 4511e4
				else {
Packit 4511e4
					path += repo->path_len;
Packit 4511e4
					ls->dentry_name = xstrdup(path);
Packit 4511e4
				}
Packit 4511e4
			}
Packit 4511e4
		} else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
Packit 4511e4
			ls->dentry_flags |= IS_DIR;
Packit 4511e4
		}
Packit 4511e4
	} else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
Packit 4511e4
		FREE_AND_NULL(ls->dentry_name);
Packit 4511e4
		ls->dentry_flags = 0;
Packit 4511e4
	}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * NEEDSWORK: remote_ls() ignores info/refs on the remote side.  But it
Packit 4511e4
 * should _only_ heed the information from that file, instead of trying to
Packit 4511e4
 * determine the refs from the remote file system (badly: it does not even
Packit 4511e4
 * know about packed-refs).
Packit 4511e4
 */
Packit 4511e4
static void remote_ls(const char *path, int flags,
Packit 4511e4
		      void (*userFunc)(struct remote_ls_ctx *ls),
Packit 4511e4
		      void *userData)
Packit 4511e4
{
Packit 4511e4
	char *url = xstrfmt("%s%s", repo->url, path);
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct slot_results results;
Packit 4511e4
	struct strbuf in_buffer = STRBUF_INIT;
Packit 4511e4
	struct buffer out_buffer = { STRBUF_INIT, 0 };
Packit 4511e4
	struct curl_slist *dav_headers = http_copy_default_headers();
Packit 4511e4
	struct xml_ctx ctx;
Packit 4511e4
	struct remote_ls_ctx ls;
Packit 4511e4
Packit 4511e4
	ls.flags = flags;
Packit 4511e4
	ls.path = xstrdup(path);
Packit 4511e4
	ls.dentry_name = NULL;
Packit 4511e4
	ls.dentry_flags = 0;
Packit 4511e4
	ls.userData = userData;
Packit 4511e4
	ls.userFunc = userFunc;
Packit 4511e4
Packit 4511e4
	strbuf_addstr(&out_buffer.buf, PROPFIND_ALL_REQUEST);
Packit 4511e4
Packit 4511e4
	dav_headers = curl_slist_append(dav_headers, "Depth: 1");
Packit 4511e4
	dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
Packit 4511e4
Packit 4511e4
	slot = get_active_slot();
Packit 4511e4
	slot->results = &results;
Packit 4511e4
	curl_setup_http(slot->curl, url, DAV_PROPFIND,
Packit 4511e4
			&out_buffer, fwrite_buffer);
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
Packit 4511e4
Packit 4511e4
	if (start_active_slot(slot)) {
Packit 4511e4
		run_active_slot(slot);
Packit 4511e4
		if (results.curl_result == CURLE_OK) {
Packit 4511e4
			XML_Parser parser = XML_ParserCreate(NULL);
Packit 4511e4
			enum XML_Status result;
Packit 4511e4
			ctx.name = xcalloc(10, 1);
Packit 4511e4
			ctx.len = 0;
Packit 4511e4
			ctx.cdata = NULL;
Packit 4511e4
			ctx.userFunc = handle_remote_ls_ctx;
Packit 4511e4
			ctx.userData = &ls;
Packit 4511e4
			XML_SetUserData(parser, &ctx;;
Packit 4511e4
			XML_SetElementHandler(parser, xml_start_tag,
Packit 4511e4
					      xml_end_tag);
Packit 4511e4
			XML_SetCharacterDataHandler(parser, xml_cdata);
Packit 4511e4
			result = XML_Parse(parser, in_buffer.buf,
Packit 4511e4
					   in_buffer.len, 1);
Packit 4511e4
			free(ctx.name);
Packit 4511e4
Packit 4511e4
			if (result != XML_STATUS_OK) {
Packit 4511e4
				fprintf(stderr, "XML error: %s\n",
Packit 4511e4
					XML_ErrorString(
Packit 4511e4
						XML_GetErrorCode(parser)));
Packit 4511e4
			}
Packit 4511e4
			XML_ParserFree(parser);
Packit 4511e4
		}
Packit 4511e4
	} else {
Packit 4511e4
		fprintf(stderr, "Unable to start PROPFIND request\n");
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	free(ls.path);
Packit 4511e4
	free(url);
Packit 4511e4
	strbuf_release(&out_buffer.buf);
Packit 4511e4
	strbuf_release(&in_buffer);
Packit 4511e4
	curl_slist_free_all(dav_headers);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void get_remote_object_list(unsigned char parent)
Packit 4511e4
{
Packit 4511e4
	char path[] = "objects/XX/";
Packit 4511e4
	static const char hex[] = "0123456789abcdef";
Packit 4511e4
	unsigned int val = parent;
Packit 4511e4
Packit 4511e4
	path[8] = hex[val >> 4];
Packit 4511e4
	path[9] = hex[val & 0xf];
Packit 4511e4
	remote_dir_exists[val] = 0;
Packit 4511e4
	remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
Packit 4511e4
		  process_ls_object, &val;;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int locking_available(void)
Packit 4511e4
{
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct slot_results results;
Packit 4511e4
	struct strbuf in_buffer = STRBUF_INIT;
Packit 4511e4
	struct buffer out_buffer = { STRBUF_INIT, 0 };
Packit 4511e4
	struct curl_slist *dav_headers = http_copy_default_headers();
Packit 4511e4
	struct xml_ctx ctx;
Packit 4511e4
	int lock_flags = 0;
Packit 4511e4
	char *escaped;
Packit 4511e4
Packit 4511e4
	escaped = xml_entities(repo->url);
Packit 4511e4
	strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
Packit 4511e4
	free(escaped);
Packit 4511e4
Packit 4511e4
	dav_headers = curl_slist_append(dav_headers, "Depth: 0");
Packit 4511e4
	dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
Packit 4511e4
Packit 4511e4
	slot = get_active_slot();
Packit 4511e4
	slot->results = &results;
Packit 4511e4
	curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
Packit 4511e4
			&out_buffer, fwrite_buffer);
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
Packit 4511e4
Packit 4511e4
	if (start_active_slot(slot)) {
Packit 4511e4
		run_active_slot(slot);
Packit 4511e4
		if (results.curl_result == CURLE_OK) {
Packit 4511e4
			XML_Parser parser = XML_ParserCreate(NULL);
Packit 4511e4
			enum XML_Status result;
Packit 4511e4
			ctx.name = xcalloc(10, 1);
Packit 4511e4
			ctx.len = 0;
Packit 4511e4
			ctx.cdata = NULL;
Packit 4511e4
			ctx.userFunc = handle_lockprop_ctx;
Packit 4511e4
			ctx.userData = &lock_flags;
Packit 4511e4
			XML_SetUserData(parser, &ctx;;
Packit 4511e4
			XML_SetElementHandler(parser, xml_start_tag,
Packit 4511e4
					      xml_end_tag);
Packit 4511e4
			result = XML_Parse(parser, in_buffer.buf,
Packit 4511e4
					   in_buffer.len, 1);
Packit 4511e4
			free(ctx.name);
Packit 4511e4
Packit 4511e4
			if (result != XML_STATUS_OK) {
Packit 4511e4
				fprintf(stderr, "XML error: %s\n",
Packit 4511e4
					XML_ErrorString(
Packit 4511e4
						XML_GetErrorCode(parser)));
Packit 4511e4
				lock_flags = 0;
Packit 4511e4
			}
Packit 4511e4
			XML_ParserFree(parser);
Packit 4511e4
			if (!lock_flags)
Packit 4511e4
				error("no DAV locking support on %s",
Packit 4511e4
				      repo->url);
Packit 4511e4
Packit 4511e4
		} else {
Packit 4511e4
			error("Cannot access URL %s, return code %d",
Packit 4511e4
			      repo->url, results.curl_result);
Packit 4511e4
			lock_flags = 0;
Packit 4511e4
		}
Packit 4511e4
	} else {
Packit 4511e4
		error("Unable to start PROPFIND request on %s", repo->url);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	strbuf_release(&out_buffer.buf);
Packit 4511e4
	strbuf_release(&in_buffer);
Packit 4511e4
	curl_slist_free_all(dav_headers);
Packit 4511e4
Packit 4511e4
	return lock_flags;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static struct object_list **add_one_object(struct object *obj, struct object_list **p)
Packit 4511e4
{
Packit 4511e4
	struct object_list *entry = xmalloc(sizeof(struct object_list));
Packit 4511e4
	entry->item = obj;
Packit 4511e4
	entry->next = *p;
Packit 4511e4
	*p = entry;
Packit 4511e4
	return &entry->next;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static struct object_list **process_blob(struct blob *blob,
Packit 4511e4
					 struct object_list **p)
Packit 4511e4
{
Packit 4511e4
	struct object *obj = &blob->object;
Packit 4511e4
Packit 4511e4
	obj->flags |= LOCAL;
Packit 4511e4
Packit 4511e4
	if (obj->flags & (UNINTERESTING | SEEN))
Packit 4511e4
		return p;
Packit 4511e4
Packit 4511e4
	obj->flags |= SEEN;
Packit 4511e4
	return add_one_object(obj, p);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static struct object_list **process_tree(struct tree *tree,
Packit 4511e4
					 struct object_list **p)
Packit 4511e4
{
Packit 4511e4
	struct object *obj = &tree->object;
Packit 4511e4
	struct tree_desc desc;
Packit 4511e4
	struct name_entry entry;
Packit 4511e4
Packit 4511e4
	obj->flags |= LOCAL;
Packit 4511e4
Packit 4511e4
	if (obj->flags & (UNINTERESTING | SEEN))
Packit 4511e4
		return p;
Packit 4511e4
	if (parse_tree(tree) < 0)
Packit 4511e4
		die("bad tree object %s", oid_to_hex(&obj->oid));
Packit 4511e4
Packit 4511e4
	obj->flags |= SEEN;
Packit 4511e4
	p = add_one_object(obj, p);
Packit 4511e4
Packit 4511e4
	init_tree_desc(&desc, tree->buffer, tree->size);
Packit 4511e4
Packit 4511e4
	while (tree_entry(&desc, &entry))
Packit 4511e4
		switch (object_type(entry.mode)) {
Packit 4511e4
		case OBJ_TREE:
Packit 4511e4
			p = process_tree(lookup_tree(entry.oid), p);
Packit 4511e4
			break;
Packit 4511e4
		case OBJ_BLOB:
Packit 4511e4
			p = process_blob(lookup_blob(entry.oid), p);
Packit 4511e4
			break;
Packit 4511e4
		default:
Packit 4511e4
			/* Subproject commit - not in this repository */
Packit 4511e4
			break;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
	free_tree_buffer(tree);
Packit 4511e4
	return p;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int get_delta(struct rev_info *revs, struct remote_lock *lock)
Packit 4511e4
{
Packit 4511e4
	int i;
Packit 4511e4
	struct commit *commit;
Packit 4511e4
	struct object_list **p = &objects;
Packit 4511e4
	int count = 0;
Packit 4511e4
Packit 4511e4
	while ((commit = get_revision(revs)) != NULL) {
Packit 4511e4
		p = process_tree(get_commit_tree(commit), p);
Packit 4511e4
		commit->object.flags |= LOCAL;
Packit 4511e4
		if (!(commit->object.flags & UNINTERESTING))
Packit 4511e4
			count += add_send_request(&commit->object, lock);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	for (i = 0; i < revs->pending.nr; i++) {
Packit 4511e4
		struct object_array_entry *entry = revs->pending.objects + i;
Packit 4511e4
		struct object *obj = entry->item;
Packit 4511e4
		const char *name = entry->name;
Packit 4511e4
Packit 4511e4
		if (obj->flags & (UNINTERESTING | SEEN))
Packit 4511e4
			continue;
Packit 4511e4
		if (obj->type == OBJ_TAG) {
Packit 4511e4
			obj->flags |= SEEN;
Packit 4511e4
			p = add_one_object(obj, p);
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (obj->type == OBJ_TREE) {
Packit 4511e4
			p = process_tree((struct tree *)obj, p);
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		if (obj->type == OBJ_BLOB) {
Packit 4511e4
			p = process_blob((struct blob *)obj, p);
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		die("unknown pending object %s (%s)", oid_to_hex(&obj->oid), name);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	while (objects) {
Packit 4511e4
		if (!(objects->item->flags & UNINTERESTING))
Packit 4511e4
			count += add_send_request(objects->item, lock);
Packit 4511e4
		objects = objects->next;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return count;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int update_remote(unsigned char *sha1, struct remote_lock *lock)
Packit 4511e4
{
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct slot_results results;
Packit 4511e4
	struct buffer out_buffer = { STRBUF_INIT, 0 };
Packit 4511e4
	struct curl_slist *dav_headers;
Packit 4511e4
Packit 4511e4
	dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
Packit 4511e4
Packit 4511e4
	strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
Packit 4511e4
Packit 4511e4
	slot = get_active_slot();
Packit 4511e4
	slot->results = &results;
Packit 4511e4
	curl_setup_http(slot->curl, lock->url, DAV_PUT,
Packit 4511e4
			&out_buffer, fwrite_null);
Packit 4511e4
	curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Packit 4511e4
Packit 4511e4
	if (start_active_slot(slot)) {
Packit 4511e4
		run_active_slot(slot);
Packit 4511e4
		strbuf_release(&out_buffer.buf);
Packit 4511e4
		if (results.curl_result != CURLE_OK) {
Packit 4511e4
			fprintf(stderr,
Packit 4511e4
				"PUT error: curl result=%d, HTTP code=%ld\n",
Packit 4511e4
				results.curl_result, results.http_code);
Packit 4511e4
			/* We should attempt recovery? */
Packit 4511e4
			return 0;
Packit 4511e4
		}
Packit 4511e4
	} else {
Packit 4511e4
		strbuf_release(&out_buffer.buf);
Packit 4511e4
		fprintf(stderr, "Unable to start PUT request\n");
Packit 4511e4
		return 0;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return 1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static struct ref *remote_refs;
Packit 4511e4
Packit 4511e4
static void one_remote_ref(const char *refname)
Packit 4511e4
{
Packit 4511e4
	struct ref *ref;
Packit 4511e4
	struct object *obj;
Packit 4511e4
Packit 4511e4
	ref = alloc_ref(refname);
Packit 4511e4
Packit 4511e4
	if (http_fetch_ref(repo->url, ref) != 0) {
Packit 4511e4
		fprintf(stderr,
Packit 4511e4
			"Unable to fetch ref %s from %s\n",
Packit 4511e4
			refname, repo->url);
Packit 4511e4
		free(ref);
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * Fetch a copy of the object if it doesn't exist locally - it
Packit 4511e4
	 * may be required for updating server info later.
Packit 4511e4
	 */
Packit 4511e4
	if (repo->can_update_info_refs && !has_object_file(&ref->old_oid)) {
Packit 4511e4
		obj = lookup_unknown_object(ref->old_oid.hash);
Packit 4511e4
		fprintf(stderr,	"  fetch %s for %s\n",
Packit 4511e4
			oid_to_hex(&ref->old_oid), refname);
Packit 4511e4
		add_fetch_request(obj);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	ref->next = remote_refs;
Packit 4511e4
	remote_refs = ref;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void get_dav_remote_heads(void)
Packit 4511e4
{
Packit 4511e4
	remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void add_remote_info_ref(struct remote_ls_ctx *ls)
Packit 4511e4
{
Packit 4511e4
	struct strbuf *buf = (struct strbuf *)ls->userData;
Packit 4511e4
	struct object *o;
Packit 4511e4
	struct ref *ref;
Packit 4511e4
Packit 4511e4
	ref = alloc_ref(ls->dentry_name);
Packit 4511e4
Packit 4511e4
	if (http_fetch_ref(repo->url, ref) != 0) {
Packit 4511e4
		fprintf(stderr,
Packit 4511e4
			"Unable to fetch ref %s from %s\n",
Packit 4511e4
			ls->dentry_name, repo->url);
Packit 4511e4
		aborted = 1;
Packit 4511e4
		free(ref);
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	o = parse_object(&ref->old_oid);
Packit 4511e4
	if (!o) {
Packit 4511e4
		fprintf(stderr,
Packit 4511e4
			"Unable to parse object %s for remote ref %s\n",
Packit 4511e4
			oid_to_hex(&ref->old_oid), ls->dentry_name);
Packit 4511e4
		aborted = 1;
Packit 4511e4
		free(ref);
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	strbuf_addf(buf, "%s\t%s\n",
Packit 4511e4
		    oid_to_hex(&ref->old_oid), ls->dentry_name);
Packit 4511e4
Packit 4511e4
	if (o->type == OBJ_TAG) {
Packit 4511e4
		o = deref_tag(o, ls->dentry_name, 0);
Packit 4511e4
		if (o)
Packit 4511e4
			strbuf_addf(buf, "%s\t%s^{}\n",
Packit 4511e4
				    oid_to_hex(&o->oid), ls->dentry_name);
Packit 4511e4
	}
Packit 4511e4
	free(ref);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void update_remote_info_refs(struct remote_lock *lock)
Packit 4511e4
{
Packit 4511e4
	struct buffer buffer = { STRBUF_INIT, 0 };
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct slot_results results;
Packit 4511e4
	struct curl_slist *dav_headers;
Packit 4511e4
Packit 4511e4
	remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
Packit 4511e4
		  add_remote_info_ref, &buffer.buf);
Packit 4511e4
	if (!aborted) {
Packit 4511e4
		dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
Packit 4511e4
Packit 4511e4
		slot = get_active_slot();
Packit 4511e4
		slot->results = &results;
Packit 4511e4
		curl_setup_http(slot->curl, lock->url, DAV_PUT,
Packit 4511e4
				&buffer, fwrite_null);
Packit 4511e4
		curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Packit 4511e4
Packit 4511e4
		if (start_active_slot(slot)) {
Packit 4511e4
			run_active_slot(slot);
Packit 4511e4
			if (results.curl_result != CURLE_OK) {
Packit 4511e4
				fprintf(stderr,
Packit 4511e4
					"PUT error: curl result=%d, HTTP code=%ld\n",
Packit 4511e4
					results.curl_result, results.http_code);
Packit 4511e4
			}
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
	strbuf_release(&buffer.buf);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int remote_exists(const char *path)
Packit 4511e4
{
Packit 4511e4
	char *url = xstrfmt("%s%s", repo->url, path);
Packit 4511e4
	int ret;
Packit 4511e4
Packit 4511e4
Packit 4511e4
	switch (http_get_strbuf(url, NULL, NULL)) {
Packit 4511e4
	case HTTP_OK:
Packit 4511e4
		ret = 1;
Packit 4511e4
		break;
Packit 4511e4
	case HTTP_MISSING_TARGET:
Packit 4511e4
		ret = 0;
Packit 4511e4
		break;
Packit 4511e4
	case HTTP_ERROR:
Packit 4511e4
		error("unable to access '%s': %s", url, curl_errorstr);
Packit 4511e4
		/* fallthrough */
Packit 4511e4
	default:
Packit 4511e4
		ret = -1;
Packit 4511e4
	}
Packit 4511e4
	free(url);
Packit 4511e4
	return ret;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void fetch_symref(const char *path, char **symref, struct object_id *oid)
Packit 4511e4
{
Packit 4511e4
	char *url = xstrfmt("%s%s", repo->url, path);
Packit 4511e4
	struct strbuf buffer = STRBUF_INIT;
Packit 4511e4
	const char *name;
Packit 4511e4
Packit 4511e4
	if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
Packit 4511e4
		die("Couldn't get %s for remote symref\n%s", url,
Packit 4511e4
		    curl_errorstr);
Packit 4511e4
	free(url);
Packit 4511e4
Packit 4511e4
	FREE_AND_NULL(*symref);
Packit 4511e4
	oidclr(oid);
Packit 4511e4
Packit 4511e4
	if (buffer.len == 0)
Packit 4511e4
		return;
Packit 4511e4
Packit 4511e4
	/* Cut off trailing newline. */
Packit 4511e4
	strbuf_rtrim(&buffer);
Packit 4511e4
Packit 4511e4
	/* If it's a symref, set the refname; otherwise try for a sha1 */
Packit 4511e4
	if (skip_prefix(buffer.buf, "ref: ", &name)) {
Packit 4511e4
		*symref = xmemdupz(name, buffer.len - (name - buffer.buf));
Packit 4511e4
	} else {
Packit 4511e4
		get_oid_hex(buffer.buf, oid);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	strbuf_release(&buffer);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int verify_merge_base(struct object_id *head_oid, struct ref *remote)
Packit 4511e4
{
Packit 4511e4
	struct commit *head = lookup_commit_or_die(head_oid, "HEAD");
Packit 4511e4
	struct commit *branch = lookup_commit_or_die(&remote->old_oid,
Packit 4511e4
						     remote->name);
Packit 4511e4
Packit 4511e4
	return in_merge_bases(branch, head);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int delete_remote_branch(const char *pattern, int force)
Packit 4511e4
{
Packit 4511e4
	struct ref *refs = remote_refs;
Packit 4511e4
	struct ref *remote_ref = NULL;
Packit 4511e4
	struct object_id head_oid;
Packit 4511e4
	char *symref = NULL;
Packit 4511e4
	int match;
Packit 4511e4
	int patlen = strlen(pattern);
Packit 4511e4
	int i;
Packit 4511e4
	struct active_request_slot *slot;
Packit 4511e4
	struct slot_results results;
Packit 4511e4
	char *url;
Packit 4511e4
Packit 4511e4
	/* Find the remote branch(es) matching the specified branch name */
Packit 4511e4
	for (match = 0; refs; refs = refs->next) {
Packit 4511e4
		char *name = refs->name;
Packit 4511e4
		int namelen = strlen(name);
Packit 4511e4
		if (namelen < patlen ||
Packit 4511e4
		    memcmp(name + namelen - patlen, pattern, patlen))
Packit 4511e4
			continue;
Packit 4511e4
		if (namelen != patlen && name[namelen - patlen - 1] != '/')
Packit 4511e4
			continue;
Packit 4511e4
		match++;
Packit 4511e4
		remote_ref = refs;
Packit 4511e4
	}
Packit 4511e4
	if (match == 0)
Packit 4511e4
		return error("No remote branch matches %s", pattern);
Packit 4511e4
	if (match != 1)
Packit 4511e4
		return error("More than one remote branch matches %s",
Packit 4511e4
			     pattern);
Packit 4511e4
Packit 4511e4
	/*
Packit 4511e4
	 * Remote HEAD must be a symref (not exactly foolproof; a remote
Packit 4511e4
	 * symlink to a symref will look like a symref)
Packit 4511e4
	 */
Packit 4511e4
	fetch_symref("HEAD", &symref, &head_oid);
Packit 4511e4
	if (!symref)
Packit 4511e4
		return error("Remote HEAD is not a symref");
Packit 4511e4
Packit 4511e4
	/* Remote branch must not be the remote HEAD */
Packit 4511e4
	for (i = 0; symref && i < MAXDEPTH; i++) {
Packit 4511e4
		if (!strcmp(remote_ref->name, symref))
Packit 4511e4
			return error("Remote branch %s is the current HEAD",
Packit 4511e4
				     remote_ref->name);
Packit 4511e4
		fetch_symref(symref, &symref, &head_oid);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	/* Run extra sanity checks if delete is not forced */
Packit 4511e4
	if (!force) {
Packit 4511e4
		/* Remote HEAD must resolve to a known object */
Packit 4511e4
		if (symref)
Packit 4511e4
			return error("Remote HEAD symrefs too deep");
Packit 4511e4
		if (is_null_oid(&head_oid))
Packit 4511e4
			return error("Unable to resolve remote HEAD");
Packit 4511e4
		if (!has_object_file(&head_oid))
Packit 4511e4
			return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid));
Packit 4511e4
Packit 4511e4
		/* Remote branch must resolve to a known object */
Packit 4511e4
		if (is_null_oid(&remote_ref->old_oid))
Packit 4511e4
			return error("Unable to resolve remote branch %s",
Packit 4511e4
				     remote_ref->name);
Packit 4511e4
		if (!has_object_file(&remote_ref->old_oid))
Packit 4511e4
			return error("Remote branch %s resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", remote_ref->name, oid_to_hex(&remote_ref->old_oid));
Packit 4511e4
Packit 4511e4
		/* Remote branch must be an ancestor of remote HEAD */
Packit 4511e4
		if (!verify_merge_base(&head_oid, remote_ref)) {
Packit 4511e4
			return error("The branch '%s' is not an ancestor "
Packit 4511e4
				     "of your current HEAD.\n"
Packit 4511e4
				     "If you are sure you want to delete it,"
Packit 4511e4
				     " run:\n\t'git http-push -D %s %s'",
Packit 4511e4
				     remote_ref->name, repo->url, pattern);
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	/* Send delete request */
Packit 4511e4
	fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
Packit 4511e4
	if (dry_run)
Packit 4511e4
		return 0;
Packit 4511e4
	url = xstrfmt("%s%s", repo->url, remote_ref->name);
Packit 4511e4
	slot = get_active_slot();
Packit 4511e4
	slot->results = &results;
Packit 4511e4
	curl_setup_http_get(slot->curl, url, DAV_DELETE);
Packit 4511e4
	if (start_active_slot(slot)) {
Packit 4511e4
		run_active_slot(slot);
Packit 4511e4
		free(url);
Packit 4511e4
		if (results.curl_result != CURLE_OK)
Packit 4511e4
			return error("DELETE request failed (%d/%ld)",
Packit 4511e4
				     results.curl_result, results.http_code);
Packit 4511e4
	} else {
Packit 4511e4
		free(url);
Packit 4511e4
		return error("Unable to start DELETE request");
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void run_request_queue(void)
Packit 4511e4
{
Packit 4511e4
#ifdef USE_CURL_MULTI
Packit 4511e4
	is_running_queue = 1;
Packit 4511e4
	fill_active_slots();
Packit 4511e4
	add_fill_function(NULL, fill_active_slot);
Packit 4511e4
#endif
Packit 4511e4
	do {
Packit 4511e4
		finish_all_active_slots();
Packit 4511e4
#ifdef USE_CURL_MULTI
Packit 4511e4
		fill_active_slots();
Packit 4511e4
#endif
Packit 4511e4
	} while (request_queue_head && !aborted);
Packit 4511e4
Packit 4511e4
#ifdef USE_CURL_MULTI
Packit 4511e4
	is_running_queue = 0;
Packit 4511e4
#endif
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int cmd_main(int argc, const char **argv)
Packit 4511e4
{
Packit 4511e4
	struct transfer_request *request;
Packit 4511e4
	struct transfer_request *next_request;
Packit 4511e4
	struct refspec rs = REFSPEC_INIT_PUSH;
Packit 4511e4
	struct remote_lock *ref_lock = NULL;
Packit 4511e4
	struct remote_lock *info_ref_lock = NULL;
Packit 4511e4
	struct rev_info revs;
Packit 4511e4
	int delete_branch = 0;
Packit 4511e4
	int force_delete = 0;
Packit 4511e4
	int objects_to_send;
Packit 4511e4
	int rc = 0;
Packit 4511e4
	int i;
Packit 4511e4
	int new_refs;
Packit 4511e4
	struct ref *ref, *local_refs;
Packit 4511e4
Packit 4511e4
	repo = xcalloc(1, sizeof(*repo));
Packit 4511e4
Packit 4511e4
	argv++;
Packit 4511e4
	for (i = 1; i < argc; i++, argv++) {
Packit 4511e4
		const char *arg = *argv;
Packit 4511e4
Packit 4511e4
		if (*arg == '-') {
Packit 4511e4
			if (!strcmp(arg, "--all")) {
Packit 4511e4
				push_all = MATCH_REFS_ALL;
Packit 4511e4
				continue;
Packit 4511e4
			}
Packit 4511e4
			if (!strcmp(arg, "--force")) {
Packit 4511e4
				force_all = 1;
Packit 4511e4
				continue;
Packit 4511e4
			}
Packit 4511e4
			if (!strcmp(arg, "--dry-run")) {
Packit 4511e4
				dry_run = 1;
Packit 4511e4
				continue;
Packit 4511e4
			}
Packit 4511e4
			if (!strcmp(arg, "--helper-status")) {
Packit 4511e4
				helper_status = 1;
Packit 4511e4
				continue;
Packit 4511e4
			}
Packit 4511e4
			if (!strcmp(arg, "--verbose")) {
Packit 4511e4
				push_verbosely = 1;
Packit 4511e4
				http_is_verbose = 1;
Packit 4511e4
				continue;
Packit 4511e4
			}
Packit 4511e4
			if (!strcmp(arg, "-d")) {
Packit 4511e4
				delete_branch = 1;
Packit 4511e4
				continue;
Packit 4511e4
			}
Packit 4511e4
			if (!strcmp(arg, "-D")) {
Packit 4511e4
				delete_branch = 1;
Packit 4511e4
				force_delete = 1;
Packit 4511e4
				continue;
Packit 4511e4
			}
Packit 4511e4
			if (!strcmp(arg, "-h"))
Packit 4511e4
				usage(http_push_usage);
Packit 4511e4
		}
Packit 4511e4
		if (!repo->url) {
Packit 4511e4
			char *path = strstr(arg, "//");
Packit 4511e4
			str_end_url_with_slash(arg, &repo->url);
Packit 4511e4
			repo->path_len = strlen(repo->url);
Packit 4511e4
			if (path) {
Packit 4511e4
				repo->path = strchr(path+2, '/');
Packit 4511e4
				if (repo->path)
Packit 4511e4
					repo->path_len = strlen(repo->path);
Packit 4511e4
			}
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
		refspec_appendn(&rs, argv, argc - i);
Packit 4511e4
		break;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
#ifndef USE_CURL_MULTI
Packit 4511e4
	die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
Packit 4511e4
#endif
Packit 4511e4
Packit 4511e4
	if (!repo->url)
Packit 4511e4
		usage(http_push_usage);
Packit 4511e4
Packit 4511e4
	if (delete_branch && rs.nr != 1)
Packit 4511e4
		die("You must specify only one branch name when deleting a remote branch");
Packit 4511e4
Packit 4511e4
	setup_git_directory();
Packit 4511e4
Packit 4511e4
	memset(remote_dir_exists, -1, 256);
Packit 4511e4
Packit 4511e4
	http_init(NULL, repo->url, 1);
Packit 4511e4
Packit 4511e4
#ifdef USE_CURL_MULTI
Packit 4511e4
	is_running_queue = 0;
Packit 4511e4
#endif
Packit 4511e4
Packit 4511e4
	/* Verify DAV compliance/lock support */
Packit 4511e4
	if (!locking_available()) {
Packit 4511e4
		rc = 1;
Packit 4511e4
		goto cleanup;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	sigchain_push_common(remove_locks_on_signal);
Packit 4511e4
Packit 4511e4
	/* Check whether the remote has server info files */
Packit 4511e4
	repo->can_update_info_refs = 0;
Packit 4511e4
	repo->has_info_refs = remote_exists("info/refs");
Packit 4511e4
	repo->has_info_packs = remote_exists("objects/info/packs");
Packit 4511e4
	if (repo->has_info_refs) {
Packit 4511e4
		info_ref_lock = lock_remote("info/refs", LOCK_TIME);
Packit 4511e4
		if (info_ref_lock)
Packit 4511e4
			repo->can_update_info_refs = 1;
Packit 4511e4
		else {
Packit 4511e4
			error("cannot lock existing info/refs");
Packit 4511e4
			rc = 1;
Packit 4511e4
			goto cleanup;
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
	if (repo->has_info_packs)
Packit 4511e4
		fetch_indices();
Packit 4511e4
Packit 4511e4
	/* Get a list of all local and remote heads to validate refspecs */
Packit 4511e4
	local_refs = get_local_heads();
Packit 4511e4
	fprintf(stderr, "Fetching remote heads...\n");
Packit 4511e4
	get_dav_remote_heads();
Packit 4511e4
	run_request_queue();
Packit 4511e4
Packit 4511e4
	/* Remove a remote branch if -d or -D was specified */
Packit 4511e4
	if (delete_branch) {
Packit 4511e4
		const char *branch = rs.items[i].src;
Packit 4511e4
		if (delete_remote_branch(branch, force_delete) == -1) {
Packit 4511e4
			fprintf(stderr, "Unable to delete remote branch %s\n",
Packit 4511e4
				branch);
Packit 4511e4
			if (helper_status)
Packit 4511e4
				printf("error %s cannot remove\n", branch);
Packit 4511e4
		}
Packit 4511e4
		goto cleanup;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	/* match them up */
Packit 4511e4
	if (match_push_refs(local_refs, &remote_refs, &rs, push_all)) {
Packit 4511e4
		rc = -1;
Packit 4511e4
		goto cleanup;
Packit 4511e4
	}
Packit 4511e4
	if (!remote_refs) {
Packit 4511e4
		fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
Packit 4511e4
		if (helper_status)
Packit 4511e4
			printf("error null no match\n");
Packit 4511e4
		rc = 0;
Packit 4511e4
		goto cleanup;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	new_refs = 0;
Packit 4511e4
	for (ref = remote_refs; ref; ref = ref->next) {
Packit 4511e4
		struct argv_array commit_argv = ARGV_ARRAY_INIT;
Packit 4511e4
Packit 4511e4
		if (!ref->peer_ref)
Packit 4511e4
			continue;
Packit 4511e4
Packit 4511e4
		if (is_null_oid(&ref->peer_ref->new_oid)) {
Packit 4511e4
			if (delete_remote_branch(ref->name, 1) == -1) {
Packit 4511e4
				error("Could not remove %s", ref->name);
Packit 4511e4
				if (helper_status)
Packit 4511e4
					printf("error %s cannot remove\n", ref->name);
Packit 4511e4
				rc = -4;
Packit 4511e4
			}
Packit 4511e4
			else if (helper_status)
Packit 4511e4
				printf("ok %s\n", ref->name);
Packit 4511e4
			new_refs++;
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		if (!oidcmp(&ref->old_oid, &ref->peer_ref->new_oid)) {
Packit 4511e4
			if (push_verbosely)
Packit 4511e4
				fprintf(stderr, "'%s': up-to-date\n", ref->name);
Packit 4511e4
			if (helper_status)
Packit 4511e4
				printf("ok %s up to date\n", ref->name);
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		if (!force_all &&
Packit 4511e4
		    !is_null_oid(&ref->old_oid) &&
Packit 4511e4
		    !ref->force) {
Packit 4511e4
			if (!has_object_file(&ref->old_oid) ||
Packit 4511e4
			    !ref_newer(&ref->peer_ref->new_oid,
Packit 4511e4
				       &ref->old_oid)) {
Packit 4511e4
				/*
Packit 4511e4
				 * We do not have the remote ref, or
Packit 4511e4
				 * we know that the remote ref is not
Packit 4511e4
				 * an ancestor of what we are trying to
Packit 4511e4
				 * push.  Either way this can be losing
Packit 4511e4
				 * commits at the remote end and likely
Packit 4511e4
				 * we were not up to date to begin with.
Packit 4511e4
				 */
Packit 4511e4
				error("remote '%s' is not an ancestor of\n"
Packit 4511e4
				      "local '%s'.\n"
Packit 4511e4
				      "Maybe you are not up-to-date and "
Packit 4511e4
				      "need to pull first?",
Packit 4511e4
				      ref->name,
Packit 4511e4
				      ref->peer_ref->name);
Packit 4511e4
				if (helper_status)
Packit 4511e4
					printf("error %s non-fast forward\n", ref->name);
Packit 4511e4
				rc = -2;
Packit 4511e4
				continue;
Packit 4511e4
			}
Packit 4511e4
		}
Packit 4511e4
		oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
Packit 4511e4
		new_refs++;
Packit 4511e4
Packit 4511e4
		fprintf(stderr, "updating '%s'", ref->name);
Packit 4511e4
		if (strcmp(ref->name, ref->peer_ref->name))
Packit 4511e4
			fprintf(stderr, " using '%s'", ref->peer_ref->name);
Packit 4511e4
		fprintf(stderr, "\n  from %s\n  to   %s\n",
Packit 4511e4
			oid_to_hex(&ref->old_oid), oid_to_hex(&ref->new_oid));
Packit 4511e4
		if (dry_run) {
Packit 4511e4
			if (helper_status)
Packit 4511e4
				printf("ok %s\n", ref->name);
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		/* Lock remote branch ref */
Packit 4511e4
		ref_lock = lock_remote(ref->name, LOCK_TIME);
Packit 4511e4
		if (ref_lock == NULL) {
Packit 4511e4
			fprintf(stderr, "Unable to lock remote branch %s\n",
Packit 4511e4
				ref->name);
Packit 4511e4
			if (helper_status)
Packit 4511e4
				printf("error %s lock error\n", ref->name);
Packit 4511e4
			rc = 1;
Packit 4511e4
			continue;
Packit 4511e4
		}
Packit 4511e4
Packit 4511e4
		/* Set up revision info for this refspec */
Packit 4511e4
		argv_array_push(&commit_argv, ""); /* ignored */
Packit 4511e4
		argv_array_push(&commit_argv, "--objects");
Packit 4511e4
		argv_array_push(&commit_argv, oid_to_hex(&ref->new_oid));
Packit 4511e4
		if (!push_all && !is_null_oid(&ref->old_oid))
Packit 4511e4
			argv_array_pushf(&commit_argv, "^%s",
Packit 4511e4
					 oid_to_hex(&ref->old_oid));
Packit 4511e4
		init_revisions(&revs, setup_git_directory());
Packit 4511e4
		setup_revisions(commit_argv.argc, commit_argv.argv, &revs, NULL);
Packit 4511e4
		revs.edge_hint = 0; /* just in case */
Packit 4511e4
Packit 4511e4
		/* Generate a list of objects that need to be pushed */
Packit 4511e4
		pushing = 0;
Packit 4511e4
		if (prepare_revision_walk(&revs))
Packit 4511e4
			die("revision walk setup failed");
Packit 4511e4
		mark_edges_uninteresting(&revs, NULL);
Packit 4511e4
		objects_to_send = get_delta(&revs, ref_lock);
Packit 4511e4
		finish_all_active_slots();
Packit 4511e4
Packit 4511e4
		/* Push missing objects to remote, this would be a
Packit 4511e4
		   convenient time to pack them first if appropriate. */
Packit 4511e4
		pushing = 1;
Packit 4511e4
		if (objects_to_send)
Packit 4511e4
			fprintf(stderr, "    sending %d objects\n",
Packit 4511e4
				objects_to_send);
Packit 4511e4
Packit 4511e4
		run_request_queue();
Packit 4511e4
Packit 4511e4
		/* Update the remote branch if all went well */
Packit 4511e4
		if (aborted || !update_remote(ref->new_oid.hash, ref_lock))
Packit 4511e4
			rc = 1;
Packit 4511e4
Packit 4511e4
		if (!rc)
Packit 4511e4
			fprintf(stderr, "    done\n");
Packit 4511e4
		if (helper_status)
Packit 4511e4
			printf("%s %s\n", !rc ? "ok" : "error", ref->name);
Packit 4511e4
		unlock_remote(ref_lock);
Packit 4511e4
		check_locks();
Packit 4511e4
		argv_array_clear(&commit_argv);
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	/* Update remote server info if appropriate */
Packit 4511e4
	if (repo->has_info_refs && new_refs) {
Packit 4511e4
		if (info_ref_lock && repo->can_update_info_refs) {
Packit 4511e4
			fprintf(stderr, "Updating remote server info\n");
Packit 4511e4
			if (!dry_run)
Packit 4511e4
				update_remote_info_refs(info_ref_lock);
Packit 4511e4
		} else {
Packit 4511e4
			fprintf(stderr, "Unable to update server info\n");
Packit 4511e4
		}
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
 cleanup:
Packit 4511e4
	if (info_ref_lock)
Packit 4511e4
		unlock_remote(info_ref_lock);
Packit 4511e4
	free(repo);
Packit 4511e4
Packit 4511e4
	http_cleanup();
Packit 4511e4
Packit 4511e4
	request = request_queue_head;
Packit 4511e4
	while (request != NULL) {
Packit 4511e4
		next_request = request->next;
Packit 4511e4
		release_request(request);
Packit 4511e4
		request = next_request;
Packit 4511e4
	}
Packit 4511e4
Packit 4511e4
	return rc;
Packit 4511e4
}