blob: 0913f8ab860246f990536fd20794ac6230daf50a [file] [log] [blame]
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001#include "cache.h"
2#include "commit.h"
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08003#include "tag.h"
4#include "blob.h"
Nick Hengeveld29508e12005-11-18 11:02:58 -08005#include "http.h"
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08006#include "refs.h"
Junio C Hamanoc4e05b12006-04-10 18:14:54 -07007#include "diff.h"
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08008#include "revision.h"
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08009#include "exec_cmd.h"
Daniel Barkalow6b628162007-05-12 11:45:59 -040010#include "remote.h"
Junio C Hamanod633c882008-06-07 11:39:20 -070011#include "list-objects.h"
Jeff King4a16d072009-01-22 01:02:35 -050012#include "sigchain.h"
Jeff Kinga0355f62015-09-24 17:07:56 -040013#include "argv-array.h"
Jonathan Tand6fe0032017-08-18 15:20:34 -070014#include "packfile.h"
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080015
Matt Kraai081fd8d2013-02-11 14:03:45 -080016#ifdef EXPAT_NEEDS_XMLPARSE_H
17#include <xmlparse.h>
18#else
Paul Collinsbee8e792005-11-06 21:07:45 +000019#include <expat.h>
Matt Kraai081fd8d2013-02-11 14:03:45 -080020#endif
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080021
22static const char http_push_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020023"git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080024
Johannes Schindelin92e2eb92005-11-07 19:23:11 +010025#ifndef XML_STATUS_OK
26enum XML_Status {
27 XML_STATUS_OK = 1,
28 XML_STATUS_ERROR = 0
29};
30#define XML_STATUS_OK 1
31#define XML_STATUS_ERROR 0
32#endif
33
Nick Hengeveld197e8952006-03-10 20:18:18 -080034#define PREV_BUF_SIZE 4096
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080035
Nick Hengeveldacf59572005-11-18 11:03:18 -080036/* DAV methods */
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080037#define DAV_LOCK "LOCK"
38#define DAV_MKCOL "MKCOL"
39#define DAV_MOVE "MOVE"
40#define DAV_PROPFIND "PROPFIND"
41#define DAV_PUT "PUT"
42#define DAV_UNLOCK "UNLOCK"
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -080043#define DAV_DELETE "DELETE"
Nick Hengeveldacf59572005-11-18 11:03:18 -080044
45/* DAV lock flags */
46#define DAV_PROP_LOCKWR (1u << 0)
47#define DAV_PROP_LOCKEX (1u << 1)
48#define DAV_LOCK_OK (1u << 2)
49
50/* DAV XML properties */
51#define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
52#define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
53#define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
54#define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
55#define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
56#define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -080057#define DAV_PROPFIND_RESP ".multistatus.response"
58#define DAV_PROPFIND_NAME ".multistatus.response.href"
59#define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
Nick Hengeveldacf59572005-11-18 11:03:18 -080060
61/* DAV request body templates */
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -080062#define PROPFIND_SUPPORTEDLOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
63#define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080064#define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\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>"
65
Nick Hengeveld75187c92005-11-04 14:22:35 -080066#define LOCK_TIME 600
67#define LOCK_REFRESH 30
68
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +070069/* Remember to update object flag allocation in object.h */
Junio C Hamano1b65a5a2006-04-16 18:12:49 -070070#define LOCAL (1u<<16)
71#define REMOTE (1u<<17)
72#define FETCHING (1u<<18)
73#define PUSHING (1u<<19)
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -080074
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -080075/* We allow "recursive" symbolic refs. Only within reason, though */
76#define MAXDEPTH 5
77
David Rientjes96f1e582006-08-15 10:23:48 -070078static int pushing;
79static int aborted;
Nick Hengevelda3c57c92006-03-20 11:50:51 -080080static signed char remote_dir_exists[256];
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080081
David Rientjes96f1e582006-08-15 10:23:48 -070082static int push_verbosely;
Andy Whitcroft28b9d6e2007-11-09 23:32:10 +000083static int push_all = MATCH_REFS_NONE;
David Rientjes96f1e582006-08-15 10:23:48 -070084static int force_all;
Shawn O. Pearcefe5d1d32007-10-16 00:35:22 -040085static int dry_run;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -070086static int helper_status;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080087
David Rientjes96f1e582006-08-15 10:23:48 -070088static struct object_list *objects;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -080089
Jonathan Nieder9cba13c2011-03-16 02:08:34 -050090struct repo {
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080091 char *url;
Kirill A. Korinskiye1f33ef2008-12-23 11:31:15 +030092 char *path;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -080093 int path_len;
Nick Hengeveld197e8952006-03-10 20:18:18 -080094 int has_info_refs;
95 int can_update_info_refs;
96 int has_info_packs;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080097 struct packed_git *packs;
Nick Hengeveld512d6322006-03-10 20:18:12 -080098 struct remote_lock *locks;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -080099};
100
Amos King7b5201a2009-03-18 18:43:53 -0500101static struct repo *repo;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800102
103enum transfer_state {
Nick Hengeveld197e8952006-03-10 20:18:18 -0800104 NEED_FETCH,
105 RUN_FETCH_LOOSE,
106 RUN_FETCH_PACKED,
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800107 NEED_PUSH,
108 RUN_MKCOL,
109 RUN_PUT,
110 RUN_MOVE,
111 ABORTED,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000112 COMPLETE
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800113};
114
Jonathan Nieder9cba13c2011-03-16 02:08:34 -0500115struct transfer_request {
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800116 struct object *obj;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800117 char *url;
118 char *dest;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800119 struct remote_lock *lock;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800120 struct curl_slist *headers;
121 struct buffer buffer;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800122 enum transfer_state state;
123 CURLcode curl_result;
124 char errorstr[CURL_ERROR_SIZE];
125 long http_code;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800126 void *userData;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800127 struct active_request_slot *slot;
128 struct transfer_request *next;
129};
130
David Rientjes96f1e582006-08-15 10:23:48 -0700131static struct transfer_request *request_queue_head;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800132
Jonathan Nieder9cba13c2011-03-16 02:08:34 -0500133struct xml_ctx {
Nick Hengeveldacf59572005-11-18 11:03:18 -0800134 char *name;
135 int len;
136 char *cdata;
137 void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
138 void *userData;
139};
140
Jonathan Nieder9cba13c2011-03-16 02:08:34 -0500141struct remote_lock {
Nick Hengeveld75187c92005-11-04 14:22:35 -0800142 char *url;
Nick Hengeveld26349b22005-11-04 14:22:31 -0800143 char *owner;
Nick Hengeveld75187c92005-11-04 14:22:35 -0800144 char *token;
Tay Ray Chuandfab7c12009-02-14 17:52:14 +0800145 char tmpfile_suffix[41];
Nick Hengeveld26349b22005-11-04 14:22:31 -0800146 time_t start_time;
147 long timeout;
Nick Hengeveld75187c92005-11-04 14:22:35 -0800148 int refreshing;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800149 struct remote_lock *next;
150};
151
Nick Hengeveld3030baa2006-03-10 20:18:08 -0800152/* Flags that control remote_ls processing */
153#define PROCESS_FILES (1u << 0)
154#define PROCESS_DIRS (1u << 1)
155#define RECURSIVE (1u << 2)
156
157/* Flags that remote_ls passes to callback functions */
158#define IS_DIR (1u << 0)
159
Jonathan Nieder9cba13c2011-03-16 02:08:34 -0500160struct remote_ls_ctx {
Nick Hengeveld3030baa2006-03-10 20:18:08 -0800161 char *path;
162 void (*userFunc)(struct remote_ls_ctx *ls);
163 void *userData;
164 int flags;
165 char *dentry_name;
166 int dentry_flags;
167 struct remote_ls_ctx *parent;
Nick Hengeveld26349b22005-11-04 14:22:31 -0800168};
169
Ray Chuanb1c7d4a2009-01-24 10:00:22 +0800170/* get_dav_token_headers options */
171enum dav_header_flag {
172 DAV_HEADER_IF = (1u << 0),
173 DAV_HEADER_LOCK = (1u << 1),
174 DAV_HEADER_TIMEOUT = (1u << 2)
175};
176
Dan McGee2aab1672011-05-03 23:47:28 +0800177static char *xml_entities(const char *s)
Mike Hommey519d05b2009-04-10 00:25:37 +0200178{
179 struct strbuf buf = STRBUF_INIT;
Michael Haggerty37141f22012-11-25 12:08:35 +0100180 strbuf_addstr_xml_quoted(&buf, s);
Mike Hommey519d05b2009-04-10 00:25:37 +0200181 return strbuf_detach(&buf, NULL);
182}
183
Dan McGeeebaaf312011-05-03 23:47:29 +0800184static void curl_setup_http_get(CURL *curl, const char *url,
185 const char *custom_req)
186{
187 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
188 curl_easy_setopt(curl, CURLOPT_URL, url);
189 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
190 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
191}
192
193static void curl_setup_http(CURL *curl, const char *url,
194 const char *custom_req, struct buffer *buffer,
195 curl_write_callback write_fn)
196{
197 curl_easy_setopt(curl, CURLOPT_PUT, 1);
198 curl_easy_setopt(curl, CURLOPT_URL, url);
199 curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
200 curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
201 curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
202#ifndef NO_CURL_IOCTL
203 curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
Abbaad Haider479eaa82014-07-05 20:43:48 -0400204 curl_easy_setopt(curl, CURLOPT_IOCTLDATA, buffer);
Dan McGeeebaaf312011-05-03 23:47:29 +0800205#endif
206 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
207 curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
208 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
209 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
210}
211
Junio C Hamanod456c9f2009-01-25 00:04:15 -0800212static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
213{
Ray Chuanb1c7d4a2009-01-24 10:00:22 +0800214 struct strbuf buf = STRBUF_INIT;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200215 struct curl_slist *dav_headers = http_copy_default_headers();
Ray Chuanb1c7d4a2009-01-24 10:00:22 +0800216
Junio C Hamanod456c9f2009-01-25 00:04:15 -0800217 if (options & DAV_HEADER_IF) {
Ray Chuanb1c7d4a2009-01-24 10:00:22 +0800218 strbuf_addf(&buf, "If: (<%s>)", lock->token);
219 dav_headers = curl_slist_append(dav_headers, buf.buf);
220 strbuf_reset(&buf);
221 }
Junio C Hamanod456c9f2009-01-25 00:04:15 -0800222 if (options & DAV_HEADER_LOCK) {
Ray Chuanb1c7d4a2009-01-24 10:00:22 +0800223 strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
224 dav_headers = curl_slist_append(dav_headers, buf.buf);
225 strbuf_reset(&buf);
226 }
Junio C Hamanod456c9f2009-01-25 00:04:15 -0800227 if (options & DAV_HEADER_TIMEOUT) {
Ray Chuanb1c7d4a2009-01-24 10:00:22 +0800228 strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
229 dav_headers = curl_slist_append(dav_headers, buf.buf);
230 strbuf_reset(&buf);
231 }
232 strbuf_release(&buf);
233
234 return dav_headers;
235}
236
Nick Hengeveld29508e12005-11-18 11:02:58 -0800237static void finish_request(struct transfer_request *request);
Nick Hengeveld197e8952006-03-10 20:18:18 -0800238static void release_request(struct transfer_request *request);
Nick Hengeveld29508e12005-11-18 11:02:58 -0800239
240static void process_response(void *callback_data)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800241{
Nick Hengeveld29508e12005-11-18 11:02:58 -0800242 struct transfer_request *request =
243 (struct transfer_request *)callback_data;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800244
Nick Hengeveld29508e12005-11-18 11:02:58 -0800245 finish_request(request);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800246}
247
Junio C Hamanodd8239f2006-06-06 14:26:57 -0700248#ifdef USE_CURL_MULTI
Junio C Hamanodcdb3332009-02-02 22:24:40 -0800249
Nick Hengeveld197e8952006-03-10 20:18:18 -0800250static void start_fetch_loose(struct transfer_request *request)
251{
Nick Hengeveld197e8952006-03-10 20:18:18 -0800252 struct active_request_slot *slot;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800253 struct http_object_request *obj_req;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800254
brian m. carlsoned1c9972015-11-10 02:22:29 +0000255 obj_req = new_http_object_request(repo->url, request->obj->oid.hash);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800256 if (obj_req == NULL) {
Nick Hengeveld197e8952006-03-10 20:18:18 -0800257 request->state = ABORTED;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800258 return;
259 }
260
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800261 slot = obj_req->slot;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800262 slot->callback_func = process_response;
263 slot->callback_data = request;
264 request->slot = slot;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800265 request->userData = obj_req;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800266
267 /* Try to get the request started, abort the request on error */
268 request->state = RUN_FETCH_LOOSE;
269 if (!start_active_slot(slot)) {
270 fprintf(stderr, "Unable to start GET request\n");
Amos King7b5201a2009-03-18 18:43:53 -0500271 repo->can_update_info_refs = 0;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800272 release_http_object_request(obj_req);
Nick Hengeveld197e8952006-03-10 20:18:18 -0800273 release_request(request);
274 }
275}
276
Junio C Hamanodd8239f2006-06-06 14:26:57 -0700277static void start_mkcol(struct transfer_request *request)
278{
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000279 char *hex = oid_to_hex(&request->obj->oid);
Junio C Hamanodd8239f2006-06-06 14:26:57 -0700280 struct active_request_slot *slot;
Junio C Hamanodd8239f2006-06-06 14:26:57 -0700281
Amos King7b5201a2009-03-18 18:43:53 -0500282 request->url = get_remote_object_url(repo->url, hex, 1);
Junio C Hamanodd8239f2006-06-06 14:26:57 -0700283
284 slot = get_active_slot();
285 slot->callback_func = process_response;
286 slot->callback_data = request;
Dan McGeeebaaf312011-05-03 23:47:29 +0800287 curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
Junio C Hamanodd8239f2006-06-06 14:26:57 -0700288 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
Junio C Hamanodd8239f2006-06-06 14:26:57 -0700289
290 if (start_active_slot(slot)) {
291 request->slot = slot;
292 request->state = RUN_MKCOL;
293 } else {
294 request->state = ABORTED;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000295 FREE_AND_NULL(request->url);
Junio C Hamanodd8239f2006-06-06 14:26:57 -0700296 }
297}
298#endif
299
Nick Hengeveld197e8952006-03-10 20:18:18 -0800300static void start_fetch_packed(struct transfer_request *request)
301{
Nick Hengeveld197e8952006-03-10 20:18:18 -0800302 struct packed_git *target;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800303
304 struct transfer_request *check_request = request_queue_head;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800305 struct http_pack_request *preq;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800306
brian m. carlsoned1c9972015-11-10 02:22:29 +0000307 target = find_sha1_pack(request->obj->oid.hash, repo->packs);
Nick Hengeveld197e8952006-03-10 20:18:18 -0800308 if (!target) {
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000309 fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request->obj->oid));
Amos King7b5201a2009-03-18 18:43:53 -0500310 repo->can_update_info_refs = 0;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800311 release_request(request);
312 return;
313 }
314
315 fprintf(stderr, "Fetching pack %s\n", sha1_to_hex(target->sha1));
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000316 fprintf(stderr, " which contains %s\n", oid_to_hex(&request->obj->oid));
Nick Hengeveld197e8952006-03-10 20:18:18 -0800317
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800318 preq = new_http_pack_request(target, repo->url);
319 if (preq == NULL) {
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800320 repo->can_update_info_refs = 0;
321 return;
322 }
323 preq->lst = &repo->packs;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800324
325 /* Make sure there isn't another open request for this pack */
326 while (check_request) {
327 if (check_request->state == RUN_FETCH_PACKED &&
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800328 !strcmp(check_request->url, preq->url)) {
329 release_http_pack_request(preq);
Nick Hengeveld197e8952006-03-10 20:18:18 -0800330 release_request(request);
331 return;
332 }
333 check_request = check_request->next;
334 }
335
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800336 preq->slot->callback_func = process_response;
337 preq->slot->callback_data = request;
338 request->slot = preq->slot;
339 request->userData = preq;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800340
341 /* Try to get the request started, abort the request on error */
342 request->state = RUN_FETCH_PACKED;
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800343 if (!start_active_slot(preq->slot)) {
Nick Hengeveld197e8952006-03-10 20:18:18 -0800344 fprintf(stderr, "Unable to start GET request\n");
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800345 release_http_pack_request(preq);
Amos King7b5201a2009-03-18 18:43:53 -0500346 repo->can_update_info_refs = 0;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800347 release_request(request);
348 }
349}
350
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800351static void start_put(struct transfer_request *request)
352{
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000353 char *hex = oid_to_hex(&request->obj->oid);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800354 struct active_request_slot *slot;
Tay Ray Chuan817d14a2009-01-31 07:51:55 +0800355 struct strbuf buf = STRBUF_INIT;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500356 enum object_type type;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800357 char hdr[50];
358 void *unpacked;
359 unsigned long len;
360 int hdrlen;
361 ssize_t size;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700362 git_zstream stream;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800363
brian m. carlsoned1c9972015-11-10 02:22:29 +0000364 unpacked = read_sha1_file(request->obj->oid.hash, &type, &len);
Jeff Kingef1286d2015-09-24 17:06:42 -0400365 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", typename(type), len) + 1;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800366
367 /* Set it up */
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700368 git_deflate_init(&stream, zlib_compression_level);
Junio C Hamano225a6f12011-06-10 11:18:17 -0700369 size = git_deflate_bound(&stream, len + hdrlen);
Mike Hommey028c2972007-12-09 20:30:59 +0100370 strbuf_init(&request->buffer.buf, size);
371 request->buffer.posn = 0;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800372
373 /* Compress it */
Mike Hommey028c2972007-12-09 20:30:59 +0100374 stream.next_out = (unsigned char *)request->buffer.buf.buf;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800375 stream.avail_out = size;
376
377 /* First header.. */
378 stream.next_in = (void *)hdr;
379 stream.avail_in = hdrlen;
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700380 while (git_deflate(&stream, 0) == Z_OK)
381 ; /* nothing */
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800382
383 /* Then the data itself.. */
384 stream.next_in = unpacked;
385 stream.avail_in = len;
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700386 while (git_deflate(&stream, Z_FINISH) == Z_OK)
387 ; /* nothing */
388 git_deflate_end(&stream);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800389 free(unpacked);
390
Mike Hommey028c2972007-12-09 20:30:59 +0100391 request->buffer.buf.len = stream.total_out;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800392
Tay Ray Chuan817d14a2009-01-31 07:51:55 +0800393 strbuf_addstr(&buf, "Destination: ");
Amos King7b5201a2009-03-18 18:43:53 -0500394 append_remote_object_url(&buf, repo->url, hex, 0);
Tay Ray Chuan817d14a2009-01-31 07:51:55 +0800395 request->dest = strbuf_detach(&buf, NULL);
396
Amos King7b5201a2009-03-18 18:43:53 -0500397 append_remote_object_url(&buf, repo->url, hex, 0);
Tay Ray Chuandfab7c12009-02-14 17:52:14 +0800398 strbuf_add(&buf, request->lock->tmpfile_suffix, 41);
Tay Ray Chuan817d14a2009-01-31 07:51:55 +0800399 request->url = strbuf_detach(&buf, NULL);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800400
401 slot = get_active_slot();
Nick Hengeveld29508e12005-11-18 11:02:58 -0800402 slot->callback_func = process_response;
403 slot->callback_data = request;
Dan McGeeebaaf312011-05-03 23:47:29 +0800404 curl_setup_http(slot->curl, request->url, DAV_PUT,
405 &request->buffer, fwrite_null);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800406
407 if (start_active_slot(slot)) {
408 request->slot = slot;
409 request->state = RUN_PUT;
410 } else {
411 request->state = ABORTED;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000412 FREE_AND_NULL(request->url);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800413 }
414}
415
416static void start_move(struct transfer_request *request)
417{
418 struct active_request_slot *slot;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200419 struct curl_slist *dav_headers = http_copy_default_headers();
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800420
421 slot = get_active_slot();
Nick Hengeveld29508e12005-11-18 11:02:58 -0800422 slot->callback_func = process_response;
423 slot->callback_data = request;
Dan McGeeebaaf312011-05-03 23:47:29 +0800424 curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800425 dav_headers = curl_slist_append(dav_headers, request->dest);
426 dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
427 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800428
429 if (start_active_slot(slot)) {
430 request->slot = slot;
431 request->state = RUN_MOVE;
432 } else {
433 request->state = ABORTED;
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000434 FREE_AND_NULL(request->url);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800435 }
436}
437
Nick Hengeveld512d6322006-03-10 20:18:12 -0800438static int refresh_lock(struct remote_lock *lock)
Nick Hengeveld75187c92005-11-04 14:22:35 -0800439{
440 struct active_request_slot *slot;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800441 struct slot_results results;
Ray Chuanb1c7d4a2009-01-24 10:00:22 +0800442 struct curl_slist *dav_headers;
Nick Hengeveld512d6322006-03-10 20:18:12 -0800443 int rc = 0;
Nick Hengeveld75187c92005-11-04 14:22:35 -0800444
Nick Hengeveld512d6322006-03-10 20:18:12 -0800445 lock->refreshing = 1;
Nick Hengeveld75187c92005-11-04 14:22:35 -0800446
Ray Chuanb1c7d4a2009-01-24 10:00:22 +0800447 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800448
Nick Hengeveld512d6322006-03-10 20:18:12 -0800449 slot = get_active_slot();
450 slot->results = &results;
Dan McGeeebaaf312011-05-03 23:47:29 +0800451 curl_setup_http_get(slot->curl, lock->url, DAV_LOCK);
Nick Hengeveld512d6322006-03-10 20:18:12 -0800452 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800453
Nick Hengeveld512d6322006-03-10 20:18:12 -0800454 if (start_active_slot(slot)) {
455 run_active_slot(slot);
456 if (results.curl_result != CURLE_OK) {
457 fprintf(stderr, "LOCK HTTP error %ld\n",
458 results.http_code);
459 } else {
460 lock->start_time = time(NULL);
461 rc = 1;
Nick Hengeveld75187c92005-11-04 14:22:35 -0800462 }
463 }
464
Nick Hengeveld512d6322006-03-10 20:18:12 -0800465 lock->refreshing = 0;
466 curl_slist_free_all(dav_headers);
Nick Hengeveld512d6322006-03-10 20:18:12 -0800467
468 return rc;
469}
470
Rene Scharfeec26b4d2006-04-02 13:13:10 +0200471static void check_locks(void)
Nick Hengeveld512d6322006-03-10 20:18:12 -0800472{
Amos King7b5201a2009-03-18 18:43:53 -0500473 struct remote_lock *lock = repo->locks;
Nick Hengeveld512d6322006-03-10 20:18:12 -0800474 time_t current_time = time(NULL);
475 int time_remaining;
476
477 while (lock) {
478 time_remaining = lock->start_time + lock->timeout -
479 current_time;
480 if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
481 if (!refresh_lock(lock)) {
482 fprintf(stderr,
483 "Unable to refresh lock for %s\n",
484 lock->url);
485 aborted = 1;
486 return;
487 }
488 }
489 lock = lock->next;
490 }
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800491}
492
493static void release_request(struct transfer_request *request)
494{
495 struct transfer_request *entry = request_queue_head;
496
497 if (request == request_queue_head) {
498 request_queue_head = request->next;
499 } else {
500 while (entry->next != NULL && entry->next != request)
501 entry = entry->next;
502 if (entry->next == request)
503 entry->next = entry->next->next;
504 }
505
Jim Meyering8e0f7002008-01-31 18:26:32 +0100506 free(request->url);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800507 free(request);
508}
509
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800510static void finish_request(struct transfer_request *request)
511{
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800512 struct http_pack_request *preq;
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800513 struct http_object_request *obj_req;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800514
515 request->curl_result = request->slot->curl_result;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800516 request->http_code = request->slot->http_code;
517 request->slot = NULL;
518
519 /* Keep locks active */
Nick Hengeveld512d6322006-03-10 20:18:12 -0800520 check_locks();
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800521
522 if (request->headers != NULL)
523 curl_slist_free_all(request->headers);
524
525 /* URL is reused for MOVE after PUT */
526 if (request->state != RUN_PUT) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000527 FREE_AND_NULL(request->url);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800528 }
529
530 if (request->state == RUN_MKCOL) {
531 if (request->curl_result == CURLE_OK ||
532 request->http_code == 405) {
brian m. carlsoned1c9972015-11-10 02:22:29 +0000533 remote_dir_exists[request->obj->oid.hash[0]] = 1;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800534 start_put(request);
535 } else {
536 fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000537 oid_to_hex(&request->obj->oid),
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800538 request->curl_result, request->http_code);
539 request->state = ABORTED;
540 aborted = 1;
541 }
542 } else if (request->state == RUN_PUT) {
543 if (request->curl_result == CURLE_OK) {
544 start_move(request);
545 } else {
546 fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000547 oid_to_hex(&request->obj->oid),
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800548 request->curl_result, request->http_code);
549 request->state = ABORTED;
550 aborted = 1;
551 }
552 } else if (request->state == RUN_MOVE) {
553 if (request->curl_result == CURLE_OK) {
Nick Hengeveld1a703cb2006-03-10 20:18:33 -0800554 if (push_verbosely)
555 fprintf(stderr, " sent %s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000556 oid_to_hex(&request->obj->oid));
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800557 request->obj->flags |= REMOTE;
558 release_request(request);
559 } else {
560 fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000561 oid_to_hex(&request->obj->oid),
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800562 request->curl_result, request->http_code);
563 request->state = ABORTED;
564 aborted = 1;
565 }
Nick Hengeveld197e8952006-03-10 20:18:18 -0800566 } else if (request->state == RUN_FETCH_LOOSE) {
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800567 obj_req = (struct http_object_request *)request->userData;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800568
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800569 if (finish_http_object_request(obj_req) == 0)
570 if (obj_req->rename == 0)
571 request->obj->flags |= (LOCAL | REMOTE);
Nick Hengeveld197e8952006-03-10 20:18:18 -0800572
573 /* Try fetching packed if necessary */
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800574 if (request->obj->flags & LOCAL) {
575 release_http_object_request(obj_req);
Nick Hengeveld197e8952006-03-10 20:18:18 -0800576 release_request(request);
Tay Ray Chuan5424bc52009-06-06 16:44:02 +0800577 } else
Nick Hengeveld197e8952006-03-10 20:18:18 -0800578 start_fetch_packed(request);
579
580 } else if (request->state == RUN_FETCH_PACKED) {
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800581 int fail = 1;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800582 if (request->curl_result != CURLE_OK) {
583 fprintf(stderr, "Unable to get pack file %s\n%s",
584 request->url, curl_errorstr);
Nick Hengeveld197e8952006-03-10 20:18:18 -0800585 } else {
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800586 preq = (struct http_pack_request *)request->userData;
Shawn O. Pearce1c23d792007-01-09 20:04:52 -0500587
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800588 if (preq) {
Tay Ray Chuan609621a2009-10-30 17:47:20 -0700589 if (finish_http_pack_request(preq) == 0)
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800590 fail = 0;
591 release_http_pack_request(preq);
Nick Hengeveld197e8952006-03-10 20:18:18 -0800592 }
593 }
Tay Ray Chuan2264dfa2009-06-06 16:44:01 +0800594 if (fail)
595 repo->can_update_info_refs = 0;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800596 release_request(request);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800597 }
598}
599
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700600#ifdef USE_CURL_MULTI
Tay Ray Chuan4f662502009-06-06 16:43:27 +0800601static int is_running_queue;
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -0400602static int fill_active_slot(void *unused)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800603{
Benjamin Kramer8e24cba2009-03-15 22:01:20 +0100604 struct transfer_request *request;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800605
Tay Ray Chuan4f662502009-06-06 16:43:27 +0800606 if (aborted || !is_running_queue)
Daniel Barkalow45c17412007-09-10 23:02:28 -0400607 return 0;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800608
Daniel Barkalow45c17412007-09-10 23:02:28 -0400609 for (request = request_queue_head; request; request = request->next) {
Nick Hengeveld197e8952006-03-10 20:18:18 -0800610 if (request->state == NEED_FETCH) {
611 start_fetch_loose(request);
Daniel Barkalow45c17412007-09-10 23:02:28 -0400612 return 1;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800613 } else if (pushing && request->state == NEED_PUSH) {
brian m. carlsoned1c9972015-11-10 02:22:29 +0000614 if (remote_dir_exists[request->obj->oid.hash[0]] == 1) {
Nick Hengeveld0dd276b2005-11-07 11:39:37 -0800615 start_put(request);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800616 } else {
Nick Hengeveld0dd276b2005-11-07 11:39:37 -0800617 start_mkcol(request);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800618 }
Daniel Barkalow45c17412007-09-10 23:02:28 -0400619 return 1;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800620 }
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800621 }
Daniel Barkalow45c17412007-09-10 23:02:28 -0400622 return 0;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800623}
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700624#endif
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800625
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800626static void get_remote_object_list(unsigned char parent);
627
Nick Hengeveld197e8952006-03-10 20:18:18 -0800628static void add_fetch_request(struct object *obj)
629{
630 struct transfer_request *request;
631
632 check_locks();
633
634 /*
635 * Don't fetch the object if it's known to exist locally
636 * or is already in the request queue
637 */
brian m. carlsoned1c9972015-11-10 02:22:29 +0000638 if (remote_dir_exists[obj->oid.hash[0]] == -1)
639 get_remote_object_list(obj->oid.hash[0]);
Nick Hengeveld197e8952006-03-10 20:18:18 -0800640 if (obj->flags & (LOCAL | FETCHING))
641 return;
642
643 obj->flags |= FETCHING;
644 request = xmalloc(sizeof(*request));
645 request->obj = obj;
646 request->url = NULL;
647 request->lock = NULL;
648 request->headers = NULL;
Nick Hengeveld197e8952006-03-10 20:18:18 -0800649 request->state = NEED_FETCH;
650 request->next = request_queue_head;
651 request_queue_head = request;
652
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700653#ifdef USE_CURL_MULTI
Nick Hengeveld197e8952006-03-10 20:18:18 -0800654 fill_active_slots();
655 step_active_slots();
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700656#endif
Nick Hengeveld197e8952006-03-10 20:18:18 -0800657}
658
Nick Hengeveld1a703cb2006-03-10 20:18:33 -0800659static int add_send_request(struct object *obj, struct remote_lock *lock)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800660{
Stefan Beller3def06e2013-07-18 23:35:26 +0200661 struct transfer_request *request;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800662 struct packed_git *target;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800663
Nick Hengeveld512d6322006-03-10 20:18:12 -0800664 /* Keep locks active */
665 check_locks();
666
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800667 /*
668 * Don't push the object if it's known to exist on the remote
669 * or is already in the request queue
670 */
brian m. carlsoned1c9972015-11-10 02:22:29 +0000671 if (remote_dir_exists[obj->oid.hash[0]] == -1)
672 get_remote_object_list(obj->oid.hash[0]);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800673 if (obj->flags & (REMOTE | PUSHING))
Nick Hengeveld1a703cb2006-03-10 20:18:33 -0800674 return 0;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000675 target = find_sha1_pack(obj->oid.hash, repo->packs);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800676 if (target) {
677 obj->flags |= REMOTE;
Nick Hengeveld1a703cb2006-03-10 20:18:33 -0800678 return 0;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800679 }
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800680
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800681 obj->flags |= PUSHING;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800682 request = xmalloc(sizeof(*request));
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800683 request->obj = obj;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800684 request->url = NULL;
Nick Hengeveld26349b22005-11-04 14:22:31 -0800685 request->lock = lock;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800686 request->headers = NULL;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800687 request->state = NEED_PUSH;
Nick Hengeveldc17fb6e2005-11-04 14:22:18 -0800688 request->next = request_queue_head;
689 request_queue_head = request;
Nick Hengeveld29508e12005-11-18 11:02:58 -0800690
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700691#ifdef USE_CURL_MULTI
Nick Hengeveld29508e12005-11-18 11:02:58 -0800692 fill_active_slots();
693 step_active_slots();
Nick Hengeveldb3ca4e4e2006-06-06 09:41:32 -0700694#endif
Nick Hengeveld1a703cb2006-03-10 20:18:33 -0800695
696 return 1;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800697}
698
Peter Hagervallf4f440a2005-11-08 23:18:31 +0100699static int fetch_indices(void)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800700{
Tay Ray Chuanb8caac22009-06-06 16:43:59 +0800701 int ret;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800702
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800703 if (push_verbosely)
704 fprintf(stderr, "Getting pack list\n");
Nick Hengeveld1a703cb2006-03-10 20:18:33 -0800705
Tay Ray Chuanb8caac22009-06-06 16:43:59 +0800706 switch (http_get_info_packs(repo->url, &repo->packs)) {
707 case HTTP_OK:
708 case HTTP_MISSING_TARGET:
709 ret = 0;
710 break;
711 default:
712 ret = -1;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800713 }
714
Tay Ray Chuanb8caac22009-06-06 16:43:59 +0800715 return ret;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800716}
717
brian m. carlson1aa40df2017-05-06 22:10:25 +0000718static void one_remote_object(const struct object_id *oid)
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800719{
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800720 struct object *obj;
721
brian m. carlson1aa40df2017-05-06 22:10:25 +0000722 obj = lookup_object(oid->hash);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800723 if (!obj)
brian m. carlsonc251c832017-05-06 22:10:38 +0000724 obj = parse_object(oid);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800725
726 /* Ignore remote objects that don't exist locally */
727 if (!obj)
728 return;
729
730 obj->flags |= REMOTE;
731 if (!object_list_contains(objects, obj))
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700732 object_list_insert(obj, &objects);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800733}
734
Nick Hengeveldacf59572005-11-18 11:03:18 -0800735static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
Nick Hengeveld26349b22005-11-04 14:22:31 -0800736{
Nick Hengeveldacf59572005-11-18 11:03:18 -0800737 int *lock_flags = (int *)ctx->userData;
Nick Hengeveld26349b22005-11-04 14:22:31 -0800738
Nick Hengeveldacf59572005-11-18 11:03:18 -0800739 if (tag_closed) {
740 if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
741 if ((*lock_flags & DAV_PROP_LOCKEX) &&
742 (*lock_flags & DAV_PROP_LOCKWR)) {
743 *lock_flags |= DAV_LOCK_OK;
744 }
745 *lock_flags &= DAV_LOCK_OK;
746 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
747 *lock_flags |= DAV_PROP_LOCKWR;
748 } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
749 *lock_flags |= DAV_PROP_LOCKEX;
750 }
751 }
Nick Hengeveld26349b22005-11-04 14:22:31 -0800752}
753
Nick Hengeveldacf59572005-11-18 11:03:18 -0800754static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
Nick Hengeveld26349b22005-11-04 14:22:31 -0800755{
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800756 struct remote_lock *lock = (struct remote_lock *)ctx->userData;
Tay Ray Chuandfab7c12009-02-14 17:52:14 +0800757 git_SHA_CTX sha_ctx;
758 unsigned char lock_token_sha1[20];
Nick Hengeveld26349b22005-11-04 14:22:31 -0800759
Nick Hengeveldacf59572005-11-18 11:03:18 -0800760 if (tag_closed && ctx->cdata) {
761 if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
Jeff King95244ae2014-06-19 17:19:43 -0400762 lock->owner = xstrdup(ctx->cdata);
Nick Hengeveldacf59572005-11-18 11:03:18 -0800763 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
Jeff Kingae021d82014-06-18 15:47:50 -0400764 const char *arg;
765 if (skip_prefix(ctx->cdata, "Second-", &arg))
766 lock->timeout = strtol(arg, NULL, 10);
Nick Hengeveldacf59572005-11-18 11:03:18 -0800767 } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
Jeff King95244ae2014-06-19 17:19:43 -0400768 lock->token = xstrdup(ctx->cdata);
Tay Ray Chuandfab7c12009-02-14 17:52:14 +0800769
770 git_SHA1_Init(&sha_ctx);
771 git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
772 git_SHA1_Final(lock_token_sha1, &sha_ctx);
773
774 lock->tmpfile_suffix[0] = '_';
775 memcpy(lock->tmpfile_suffix + 1, sha1_to_hex(lock_token_sha1), 40);
Nick Hengeveldacf59572005-11-18 11:03:18 -0800776 }
Nick Hengeveld26349b22005-11-04 14:22:31 -0800777 }
778}
779
Dan McGee2aab1672011-05-03 23:47:28 +0800780static void one_remote_ref(const char *refname);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800781
Nick Hengeveld26349b22005-11-04 14:22:31 -0800782static void
Nick Hengeveldacf59572005-11-18 11:03:18 -0800783xml_start_tag(void *userData, const char *name, const char **atts)
Nick Hengeveld26349b22005-11-04 14:22:31 -0800784{
Nick Hengeveldacf59572005-11-18 11:03:18 -0800785 struct xml_ctx *ctx = (struct xml_ctx *)userData;
Dennis Stosbergef9e58c2006-04-11 18:37:58 +0200786 const char *c = strchr(name, ':');
Jeff King0cc41422015-09-24 17:06:58 -0400787 int old_namelen, new_len;
Nick Hengeveld26349b22005-11-04 14:22:31 -0800788
Nick Hengeveldacf59572005-11-18 11:03:18 -0800789 if (c == NULL)
790 c = name;
791 else
792 c++;
793
Jeff King0cc41422015-09-24 17:06:58 -0400794 old_namelen = strlen(ctx->name);
795 new_len = old_namelen + strlen(c) + 2;
Nick Hengeveldacf59572005-11-18 11:03:18 -0800796
797 if (new_len > ctx->len) {
798 ctx->name = xrealloc(ctx->name, new_len);
799 ctx->len = new_len;
800 }
Jeff King0cc41422015-09-24 17:06:58 -0400801 xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
Nick Hengeveldacf59572005-11-18 11:03:18 -0800802
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000803 FREE_AND_NULL(ctx->cdata);
Nick Hengeveld26349b22005-11-04 14:22:31 -0800804
Nick Hengeveldacf59572005-11-18 11:03:18 -0800805 ctx->userFunc(ctx, 0);
Nick Hengeveld26349b22005-11-04 14:22:31 -0800806}
807
808static void
Nick Hengeveldacf59572005-11-18 11:03:18 -0800809xml_end_tag(void *userData, const char *name)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800810{
Nick Hengeveldacf59572005-11-18 11:03:18 -0800811 struct xml_ctx *ctx = (struct xml_ctx *)userData;
Dennis Stosbergef9e58c2006-04-11 18:37:58 +0200812 const char *c = strchr(name, ':');
Nick Hengeveldacf59572005-11-18 11:03:18 -0800813 char *ep;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800814
Nick Hengeveldacf59572005-11-18 11:03:18 -0800815 ctx->userFunc(ctx, 1);
816
817 if (c == NULL)
818 c = name;
819 else
820 c++;
821
822 ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
823 *ep = 0;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800824}
825
826static void
Nick Hengeveldacf59572005-11-18 11:03:18 -0800827xml_cdata(void *userData, const XML_Char *s, int len)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800828{
Nick Hengeveldacf59572005-11-18 11:03:18 -0800829 struct xml_ctx *ctx = (struct xml_ctx *)userData;
Junio C Hamano4cac42b2006-08-27 21:19:39 -0700830 free(ctx->cdata);
Pierre Habouzit182af832007-09-16 00:32:36 +0200831 ctx->cdata = xmemdupz(s, len);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800832}
833
Timo Hirvonen554fe202006-06-28 12:04:39 +0300834static struct remote_lock *lock_remote(const char *path, long timeout)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800835{
836 struct active_request_slot *slot;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800837 struct slot_results results;
Mike Hommey028c2972007-12-09 20:30:59 +0100838 struct buffer out_buffer = { STRBUF_INIT, 0 };
839 struct strbuf in_buffer = STRBUF_INIT;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800840 char *url;
Nick Hengeveld0772b9a2005-11-04 14:22:25 -0800841 char *ep;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800842 char timeout_header[25];
Nick Hengeveld512d6322006-03-10 20:18:12 -0800843 struct remote_lock *lock = NULL;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200844 struct curl_slist *dav_headers = http_copy_default_headers();
Nick Hengeveldacf59572005-11-18 11:03:18 -0800845 struct xml_ctx ctx;
Mike Hommey519d05b2009-04-10 00:25:37 +0200846 char *escaped;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800847
Jeff King28310182014-06-19 17:24:33 -0400848 url = xstrfmt("%s%s", repo->url, path);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800849
Nick Hengeveld0772b9a2005-11-04 14:22:25 -0800850 /* Make sure leading directories exist for the remote ref */
Amos King7b5201a2009-03-18 18:43:53 -0500851 ep = strchr(url + strlen(repo->url) + 1, '/');
Nick Hengeveld0772b9a2005-11-04 14:22:25 -0800852 while (ep) {
Johannes Schindelin466ddf92009-01-17 16:11:51 +0100853 char saved_character = ep[1];
854 ep[1] = '\0';
Nick Hengeveld0772b9a2005-11-04 14:22:25 -0800855 slot = get_active_slot();
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800856 slot->results = &results;
Dan McGeeebaaf312011-05-03 23:47:29 +0800857 curl_setup_http_get(slot->curl, url, DAV_MKCOL);
Nick Hengeveld0772b9a2005-11-04 14:22:25 -0800858 if (start_active_slot(slot)) {
859 run_active_slot(slot);
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800860 if (results.curl_result != CURLE_OK &&
861 results.http_code != 405) {
Nick Hengeveld0772b9a2005-11-04 14:22:25 -0800862 fprintf(stderr,
863 "Unable to create branch path %s\n",
864 url);
865 free(url);
866 return NULL;
867 }
868 } else {
Nick Hengeveld1a703cb2006-03-10 20:18:33 -0800869 fprintf(stderr, "Unable to start MKCOL request\n");
Nick Hengeveld0772b9a2005-11-04 14:22:25 -0800870 free(url);
871 return NULL;
872 }
Johannes Schindelin466ddf92009-01-17 16:11:51 +0100873 ep[1] = saved_character;
Nick Hengeveld0772b9a2005-11-04 14:22:25 -0800874 ep = strchr(ep + 1, '/');
875 }
876
Jeff King5cb21942012-05-21 19:09:47 -0400877 escaped = xml_entities(ident_default_email());
Mike Hommey519d05b2009-04-10 00:25:37 +0200878 strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
879 free(escaped);
Nick Hengeveld26349b22005-11-04 14:22:31 -0800880
Jeff King5096d492015-09-24 17:06:08 -0400881 xsnprintf(timeout_header, sizeof(timeout_header), "Timeout: Second-%ld", timeout);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800882 dav_headers = curl_slist_append(dav_headers, timeout_header);
883 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
884
885 slot = get_active_slot();
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800886 slot->results = &results;
Dan McGeeebaaf312011-05-03 23:47:29 +0800887 curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800888 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Dan McGeeebaaf312011-05-03 23:47:29 +0800889 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800890
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800891 lock = xcalloc(1, sizeof(*lock));
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800892 lock->timeout = -1;
Nick Hengeveldacf59572005-11-18 11:03:18 -0800893
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800894 if (start_active_slot(slot)) {
895 run_active_slot(slot);
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800896 if (results.curl_result == CURLE_OK) {
Mike Hommey472b2572007-12-11 23:50:21 +0100897 XML_Parser parser = XML_ParserCreate(NULL);
898 enum XML_Status result;
Nick Hengeveldacf59572005-11-18 11:03:18 -0800899 ctx.name = xcalloc(10, 1);
900 ctx.len = 0;
901 ctx.cdata = NULL;
902 ctx.userFunc = handle_new_lock_ctx;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800903 ctx.userData = lock;
Nick Hengeveldacf59572005-11-18 11:03:18 -0800904 XML_SetUserData(parser, &ctx);
905 XML_SetElementHandler(parser, xml_start_tag,
906 xml_end_tag);
907 XML_SetCharacterDataHandler(parser, xml_cdata);
Mike Hommey028c2972007-12-09 20:30:59 +0100908 result = XML_Parse(parser, in_buffer.buf,
909 in_buffer.len, 1);
Nick Hengeveldacf59572005-11-18 11:03:18 -0800910 free(ctx.name);
911 if (result != XML_STATUS_OK) {
912 fprintf(stderr, "XML error: %s\n",
913 XML_ErrorString(
914 XML_GetErrorCode(parser)));
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800915 lock->timeout = -1;
Nick Hengeveldacf59572005-11-18 11:03:18 -0800916 }
Mike Hommey472b2572007-12-11 23:50:21 +0100917 XML_ParserFree(parser);
Patryk Obaraa2b98202018-01-24 12:11:53 +0100918 } else {
919 fprintf(stderr,
920 "error: curl result=%d, HTTP code=%ld\n",
921 results.curl_result, results.http_code);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800922 }
923 } else {
Nick Hengeveld1a703cb2006-03-10 20:18:33 -0800924 fprintf(stderr, "Unable to start LOCK request\n");
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800925 }
926
Nick Hengeveldacf59572005-11-18 11:03:18 -0800927 curl_slist_free_all(dav_headers);
Mike Hommey028c2972007-12-09 20:30:59 +0100928 strbuf_release(&out_buffer.buf);
929 strbuf_release(&in_buffer);
Nick Hengeveld26349b22005-11-04 14:22:31 -0800930
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800931 if (lock->token == NULL || lock->timeout <= 0) {
Jim Meyering8e0f7002008-01-31 18:26:32 +0100932 free(lock->token);
933 free(lock->owner);
Nick Hengeveld75187c92005-11-04 14:22:35 -0800934 free(url);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000935 FREE_AND_NULL(lock);
Nick Hengeveldacf59572005-11-18 11:03:18 -0800936 } else {
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800937 lock->url = url;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800938 lock->start_time = time(NULL);
Amos King7b5201a2009-03-18 18:43:53 -0500939 lock->next = repo->locks;
940 repo->locks = lock;
Nick Hengeveld26349b22005-11-04 14:22:31 -0800941 }
942
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800943 return lock;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800944}
945
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -0800946static int unlock_remote(struct remote_lock *lock)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800947{
948 struct active_request_slot *slot;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800949 struct slot_results results;
Amos King7b5201a2009-03-18 18:43:53 -0500950 struct remote_lock *prev = repo->locks;
Ray Chuanb1c7d4a2009-01-24 10:00:22 +0800951 struct curl_slist *dav_headers;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800952 int rc = 0;
953
Ray Chuanb1c7d4a2009-01-24 10:00:22 +0800954 dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800955
956 slot = get_active_slot();
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800957 slot->results = &results;
Dan McGeeebaaf312011-05-03 23:47:29 +0800958 curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800959 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
960
961 if (start_active_slot(slot)) {
962 run_active_slot(slot);
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800963 if (results.curl_result == CURLE_OK)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800964 rc = 1;
965 else
Nick Hengeveld512d6322006-03-10 20:18:12 -0800966 fprintf(stderr, "UNLOCK HTTP error %ld\n",
Nick Hengeveldbaa7b672006-03-10 20:18:01 -0800967 results.http_code);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800968 } else {
Nick Hengeveld512d6322006-03-10 20:18:12 -0800969 fprintf(stderr, "Unable to start UNLOCK request\n");
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800970 }
971
972 curl_slist_free_all(dav_headers);
Nick Hengeveld75187c92005-11-04 14:22:35 -0800973
Amos King7b5201a2009-03-18 18:43:53 -0500974 if (repo->locks == lock) {
975 repo->locks = lock->next;
Nick Hengeveld512d6322006-03-10 20:18:12 -0800976 } else {
977 while (prev && prev->next != lock)
978 prev = prev->next;
979 if (prev)
980 prev->next = prev->next->next;
981 }
982
Jim Meyering8e0f7002008-01-31 18:26:32 +0100983 free(lock->owner);
Nick Hengeveld512d6322006-03-10 20:18:12 -0800984 free(lock->url);
985 free(lock->token);
986 free(lock);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -0800987
988 return rc;
989}
990
Clemens Buchacher6a491a12008-05-25 20:27:44 +0200991static void remove_locks(void)
992{
Amos King7b5201a2009-03-18 18:43:53 -0500993 struct remote_lock *lock = repo->locks;
Clemens Buchacher6a491a12008-05-25 20:27:44 +0200994
995 fprintf(stderr, "Removing remote locks...\n");
996 while (lock) {
Alex Riesen6589ebf2009-05-24 15:16:49 +0200997 struct remote_lock *next = lock->next;
Clemens Buchacher6a491a12008-05-25 20:27:44 +0200998 unlock_remote(lock);
Alex Riesen6589ebf2009-05-24 15:16:49 +0200999 lock = next;
Clemens Buchacher6a491a12008-05-25 20:27:44 +02001000 }
1001}
1002
1003static void remove_locks_on_signal(int signo)
1004{
1005 remove_locks();
Jeff King4a16d072009-01-22 01:02:35 -05001006 sigchain_pop(signo);
Clemens Buchacher6a491a12008-05-25 20:27:44 +02001007 raise(signo);
1008}
1009
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001010static void remote_ls(const char *path, int flags,
1011 void (*userFunc)(struct remote_ls_ctx *ls),
1012 void *userData);
1013
René Scharfec3bdc4e2017-10-31 14:49:56 +01001014/* extract hex from sharded "xx/x{38}" filename */
brian m. carlson1aa40df2017-05-06 22:10:25 +00001015static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
Jeff King67a31f62014-06-19 17:58:10 -04001016{
brian m. carlson1aa40df2017-05-06 22:10:25 +00001017 if (strlen(path) != GIT_SHA1_HEXSZ + 1)
Jeff King67a31f62014-06-19 17:58:10 -04001018 return -1;
1019
René Scharfec3bdc4e2017-10-31 14:49:56 +01001020 if (hex_to_bytes(oid->hash, path, 1))
1021 return -1;
Jeff King67a31f62014-06-19 17:58:10 -04001022 path += 2;
1023 path++; /* skip '/' */
Jeff King67a31f62014-06-19 17:58:10 -04001024
René Scharfec3bdc4e2017-10-31 14:49:56 +01001025 return hex_to_bytes(oid->hash + 1, path, GIT_SHA1_RAWSZ - 1);
Jeff King67a31f62014-06-19 17:58:10 -04001026}
1027
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001028static void process_ls_object(struct remote_ls_ctx *ls)
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001029{
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001030 unsigned int *parent = (unsigned int *)ls->userData;
Jeff King67a31f62014-06-19 17:58:10 -04001031 const char *path = ls->dentry_name;
brian m. carlson1aa40df2017-05-06 22:10:25 +00001032 struct object_id oid;
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001033
1034 if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
1035 remote_dir_exists[*parent] = 1;
1036 return;
1037 }
1038
Jeff King67a31f62014-06-19 17:58:10 -04001039 if (!skip_prefix(path, "objects/", &path) ||
brian m. carlson1aa40df2017-05-06 22:10:25 +00001040 get_oid_hex_from_objpath(path, &oid))
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001041 return;
Jeff King67a31f62014-06-19 17:58:10 -04001042
brian m. carlson1aa40df2017-05-06 22:10:25 +00001043 one_remote_object(&oid);
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001044}
1045
1046static void process_ls_ref(struct remote_ls_ctx *ls)
1047{
1048 if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
1049 fprintf(stderr, " %s\n", ls->dentry_name);
1050 return;
1051 }
1052
1053 if (!(ls->dentry_flags & IS_DIR))
1054 one_remote_ref(ls->dentry_name);
1055}
1056
1057static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
1058{
1059 struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
1060
1061 if (tag_closed) {
1062 if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
1063 if (ls->dentry_flags & IS_DIR) {
Tay Ray Chuan0fdadc52010-11-25 16:21:07 +08001064
1065 /* ensure collection names end with slash */
1066 str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
1067
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001068 if (ls->flags & PROCESS_DIRS) {
1069 ls->userFunc(ls);
1070 }
1071 if (strcmp(ls->dentry_name, ls->path) &&
1072 ls->flags & RECURSIVE) {
1073 remote_ls(ls->dentry_name,
1074 ls->flags,
1075 ls->userFunc,
1076 ls->userData);
1077 }
1078 } else if (ls->flags & PROCESS_FILES) {
1079 ls->userFunc(ls);
1080 }
1081 } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
Kirill A. Korinskiye1f33ef2008-12-23 11:31:15 +03001082 char *path = ctx->cdata;
1083 if (*ctx->cdata == 'h') {
1084 path = strstr(path, "//");
1085 if (path) {
1086 path = strchr(path+2, '/');
1087 }
1088 }
1089 if (path) {
Tay Ray Chuandfc2dcd2010-11-25 16:21:08 +08001090 const char *url = repo->url;
1091 if (repo->path)
1092 url = repo->path;
1093 if (strncmp(path, url, repo->path_len))
Pete Wyckoff82247e92012-04-29 20:28:45 -04001094 error("Parsed path '%s' does not match url: '%s'",
Tay Ray Chuandfc2dcd2010-11-25 16:21:08 +08001095 path, url);
1096 else {
1097 path += repo->path_len;
1098 ls->dentry_name = xstrdup(path);
1099 }
Kirill A. Korinskiye1f33ef2008-12-23 11:31:15 +03001100 }
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001101 } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
1102 ls->dentry_flags |= IS_DIR;
1103 }
1104 } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001105 FREE_AND_NULL(ls->dentry_name);
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001106 ls->dentry_flags = 0;
1107 }
1108}
1109
Johannes Schindelin20642802009-01-17 16:36:26 +01001110/*
1111 * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
1112 * should _only_ heed the information from that file, instead of trying to
1113 * determine the refs from the remote file system (badly: it does not even
1114 * know about packed-refs).
1115 */
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001116static void remote_ls(const char *path, int flags,
1117 void (*userFunc)(struct remote_ls_ctx *ls),
1118 void *userData)
1119{
Jeff King28310182014-06-19 17:24:33 -04001120 char *url = xstrfmt("%s%s", repo->url, path);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001121 struct active_request_slot *slot;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001122 struct slot_results results;
Mike Hommey028c2972007-12-09 20:30:59 +01001123 struct strbuf in_buffer = STRBUF_INIT;
1124 struct buffer out_buffer = { STRBUF_INIT, 0 };
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001125 struct curl_slist *dav_headers = http_copy_default_headers();
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001126 struct xml_ctx ctx;
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001127 struct remote_ls_ctx ls;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001128
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001129 ls.flags = flags;
Shawn Pearce9befac42006-09-02 00:16:31 -04001130 ls.path = xstrdup(path);
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001131 ls.dentry_name = NULL;
1132 ls.dentry_flags = 0;
1133 ls.userData = userData;
1134 ls.userFunc = userFunc;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001135
René Scharfe02962d32016-07-30 19:36:23 +02001136 strbuf_addstr(&out_buffer.buf, PROPFIND_ALL_REQUEST);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001137
1138 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
1139 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1140
1141 slot = get_active_slot();
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001142 slot->results = &results;
Dan McGeeebaaf312011-05-03 23:47:29 +08001143 curl_setup_http(slot->curl, url, DAV_PROPFIND,
1144 &out_buffer, fwrite_buffer);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001145 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Dan McGeeebaaf312011-05-03 23:47:29 +08001146 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001147
1148 if (start_active_slot(slot)) {
1149 run_active_slot(slot);
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001150 if (results.curl_result == CURLE_OK) {
Mike Hommey472b2572007-12-11 23:50:21 +01001151 XML_Parser parser = XML_ParserCreate(NULL);
1152 enum XML_Status result;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001153 ctx.name = xcalloc(10, 1);
1154 ctx.len = 0;
1155 ctx.cdata = NULL;
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001156 ctx.userFunc = handle_remote_ls_ctx;
1157 ctx.userData = &ls;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001158 XML_SetUserData(parser, &ctx);
1159 XML_SetElementHandler(parser, xml_start_tag,
1160 xml_end_tag);
1161 XML_SetCharacterDataHandler(parser, xml_cdata);
Mike Hommey028c2972007-12-09 20:30:59 +01001162 result = XML_Parse(parser, in_buffer.buf,
1163 in_buffer.len, 1);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001164 free(ctx.name);
1165
1166 if (result != XML_STATUS_OK) {
1167 fprintf(stderr, "XML error: %s\n",
1168 XML_ErrorString(
1169 XML_GetErrorCode(parser)));
1170 }
Mike Hommey472b2572007-12-11 23:50:21 +01001171 XML_ParserFree(parser);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001172 }
1173 } else {
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001174 fprintf(stderr, "Unable to start PROPFIND request\n");
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001175 }
1176
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001177 free(ls.path);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001178 free(url);
Mike Hommey028c2972007-12-09 20:30:59 +01001179 strbuf_release(&out_buffer.buf);
1180 strbuf_release(&in_buffer);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001181 curl_slist_free_all(dav_headers);
1182}
1183
1184static void get_remote_object_list(unsigned char parent)
1185{
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001186 char path[] = "objects/XX/";
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001187 static const char hex[] = "0123456789abcdef";
1188 unsigned int val = parent;
1189
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001190 path[8] = hex[val >> 4];
1191 path[9] = hex[val & 0xf];
1192 remote_dir_exists[val] = 0;
1193 remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
1194 process_ls_object, &val);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001195}
1196
Nick Hengeveldacf59572005-11-18 11:03:18 -08001197static int locking_available(void)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001198{
1199 struct active_request_slot *slot;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001200 struct slot_results results;
Mike Hommey028c2972007-12-09 20:30:59 +01001201 struct strbuf in_buffer = STRBUF_INIT;
1202 struct buffer out_buffer = { STRBUF_INIT, 0 };
Johannes Schindelin8cb01e22016-04-27 14:20:37 +02001203 struct curl_slist *dav_headers = http_copy_default_headers();
Nick Hengeveldacf59572005-11-18 11:03:18 -08001204 struct xml_ctx ctx;
1205 int lock_flags = 0;
Mike Hommey519d05b2009-04-10 00:25:37 +02001206 char *escaped;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001207
Mike Hommey519d05b2009-04-10 00:25:37 +02001208 escaped = xml_entities(repo->url);
1209 strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
1210 free(escaped);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001211
1212 dav_headers = curl_slist_append(dav_headers, "Depth: 0");
1213 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001214
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001215 slot = get_active_slot();
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001216 slot->results = &results;
Dan McGeeebaaf312011-05-03 23:47:29 +08001217 curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
1218 &out_buffer, fwrite_buffer);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001219 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Dan McGeeebaaf312011-05-03 23:47:29 +08001220 curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001221
1222 if (start_active_slot(slot)) {
1223 run_active_slot(slot);
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001224 if (results.curl_result == CURLE_OK) {
Mike Hommey472b2572007-12-11 23:50:21 +01001225 XML_Parser parser = XML_ParserCreate(NULL);
1226 enum XML_Status result;
Nick Hengeveldacf59572005-11-18 11:03:18 -08001227 ctx.name = xcalloc(10, 1);
1228 ctx.len = 0;
1229 ctx.cdata = NULL;
1230 ctx.userFunc = handle_lockprop_ctx;
1231 ctx.userData = &lock_flags;
1232 XML_SetUserData(parser, &ctx);
1233 XML_SetElementHandler(parser, xml_start_tag,
1234 xml_end_tag);
Mike Hommey028c2972007-12-09 20:30:59 +01001235 result = XML_Parse(parser, in_buffer.buf,
1236 in_buffer.len, 1);
Nick Hengeveldacf59572005-11-18 11:03:18 -08001237 free(ctx.name);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001238
Nick Hengeveldacf59572005-11-18 11:03:18 -08001239 if (result != XML_STATUS_OK) {
1240 fprintf(stderr, "XML error: %s\n",
1241 XML_ErrorString(
1242 XML_GetErrorCode(parser)));
1243 lock_flags = 0;
1244 }
Mike Hommey472b2572007-12-11 23:50:21 +01001245 XML_ParserFree(parser);
Junio C Hamano325ce392008-01-20 15:00:54 -08001246 if (!lock_flags)
Miklos Vajnad5c87cb2009-03-24 02:09:09 +01001247 error("no DAV locking support on %s",
Amos King7b5201a2009-03-18 18:43:53 -05001248 repo->url);
Junio C Hamano325ce392008-01-20 15:00:54 -08001249
1250 } else {
1251 error("Cannot access URL %s, return code %d",
Amos King7b5201a2009-03-18 18:43:53 -05001252 repo->url, results.curl_result);
Junio C Hamano325ce392008-01-20 15:00:54 -08001253 lock_flags = 0;
Nick Hengeveldacf59572005-11-18 11:03:18 -08001254 }
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001255 } else {
Amos King7b5201a2009-03-18 18:43:53 -05001256 error("Unable to start PROPFIND request on %s", repo->url);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001257 }
1258
Mike Hommey028c2972007-12-09 20:30:59 +01001259 strbuf_release(&out_buffer.buf);
1260 strbuf_release(&in_buffer);
Nick Hengeveldacf59572005-11-18 11:03:18 -08001261 curl_slist_free_all(dav_headers);
1262
1263 return lock_flags;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001264}
1265
Pierre Habouzitb5bf7cd2006-08-23 12:39:12 +02001266static struct object_list **add_one_object(struct object *obj, struct object_list **p)
Linus Torvalds1f1e8952006-06-19 17:42:35 -07001267{
1268 struct object_list *entry = xmalloc(sizeof(struct object_list));
1269 entry->item = obj;
1270 entry->next = *p;
1271 *p = entry;
1272 return &entry->next;
1273}
1274
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001275static struct object_list **process_blob(struct blob *blob,
Jeff King41595932016-02-11 17:23:48 -05001276 struct object_list **p)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001277{
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001278 struct object *obj = &blob->object;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001279
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001280 obj->flags |= LOCAL;
1281
1282 if (obj->flags & (UNINTERESTING | SEEN))
1283 return p;
1284
1285 obj->flags |= SEEN;
Linus Torvalds1f1e8952006-06-19 17:42:35 -07001286 return add_one_object(obj, p);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001287}
1288
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001289static struct object_list **process_tree(struct tree *tree,
Jeff King41595932016-02-11 17:23:48 -05001290 struct object_list **p)
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001291{
1292 struct object *obj = &tree->object;
Linus Torvalds2d9c58c2006-05-29 12:18:33 -07001293 struct tree_desc desc;
Linus Torvalds4c068a92006-05-30 09:45:45 -07001294 struct name_entry entry;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001295
1296 obj->flags |= LOCAL;
1297
1298 if (obj->flags & (UNINTERESTING | SEEN))
1299 return p;
1300 if (parse_tree(tree) < 0)
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001301 die("bad tree object %s", oid_to_hex(&obj->oid));
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001302
1303 obj->flags |= SEEN;
Linus Torvalds1f1e8952006-06-19 17:42:35 -07001304 p = add_one_object(obj, p);
Linus Torvalds2d9c58c2006-05-29 12:18:33 -07001305
Linus Torvalds6fda5e52007-03-21 10:08:25 -07001306 init_tree_desc(&desc, tree->buffer, tree->size);
Linus Torvalds2d9c58c2006-05-29 12:18:33 -07001307
Johannes Schindelin1bbeb4c2008-02-14 23:32:32 +00001308 while (tree_entry(&desc, &entry))
1309 switch (object_type(entry.mode)) {
1310 case OBJ_TREE:
brian m. carlson740ee052017-05-06 22:10:17 +00001311 p = process_tree(lookup_tree(entry.oid), p);
Johannes Schindelin1bbeb4c2008-02-14 23:32:32 +00001312 break;
1313 case OBJ_BLOB:
brian m. carlson3aca1fc2017-05-06 22:10:14 +00001314 p = process_blob(lookup_blob(entry.oid), p);
Johannes Schindelin1bbeb4c2008-02-14 23:32:32 +00001315 break;
1316 default:
1317 /* Subproject commit - not in this repository */
1318 break;
1319 }
1320
Jeff King6e454b92013-06-05 18:37:39 -04001321 free_tree_buffer(tree);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001322 return p;
1323}
1324
Nick Hengeveld1a703cb2006-03-10 20:18:33 -08001325static int get_delta(struct rev_info *revs, struct remote_lock *lock)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001326{
Linus Torvalds1f1e8952006-06-19 17:42:35 -07001327 int i;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001328 struct commit *commit;
Linus Torvalds1f1e8952006-06-19 17:42:35 -07001329 struct object_list **p = &objects;
Nick Hengeveld1a703cb2006-03-10 20:18:33 -08001330 int count = 0;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001331
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001332 while ((commit = get_revision(revs)) != NULL) {
Jeff King41595932016-02-11 17:23:48 -05001333 p = process_tree(commit->tree, p);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001334 commit->object.flags |= LOCAL;
1335 if (!(commit->object.flags & UNINTERESTING))
Nick Hengeveld1a703cb2006-03-10 20:18:33 -08001336 count += add_send_request(&commit->object, lock);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001337 }
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001338
Linus Torvalds1f1e8952006-06-19 17:42:35 -07001339 for (i = 0; i < revs->pending.nr; i++) {
1340 struct object_array_entry *entry = revs->pending.objects + i;
1341 struct object *obj = entry->item;
1342 const char *name = entry->name;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001343
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001344 if (obj->flags & (UNINTERESTING | SEEN))
1345 continue;
Linus Torvalds19746322006-07-11 20:45:31 -07001346 if (obj->type == OBJ_TAG) {
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001347 obj->flags |= SEEN;
Linus Torvalds1f1e8952006-06-19 17:42:35 -07001348 p = add_one_object(obj, p);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001349 continue;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001350 }
Linus Torvalds19746322006-07-11 20:45:31 -07001351 if (obj->type == OBJ_TREE) {
Jeff King41595932016-02-11 17:23:48 -05001352 p = process_tree((struct tree *)obj, p);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001353 continue;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001354 }
Linus Torvalds19746322006-07-11 20:45:31 -07001355 if (obj->type == OBJ_BLOB) {
Jeff King41595932016-02-11 17:23:48 -05001356 p = process_blob((struct blob *)obj, p);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001357 continue;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001358 }
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001359 die("unknown pending object %s (%s)", oid_to_hex(&obj->oid), name);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001360 }
1361
1362 while (objects) {
1363 if (!(objects->item->flags & UNINTERESTING))
Nick Hengeveld1a703cb2006-03-10 20:18:33 -08001364 count += add_send_request(objects->item, lock);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001365 objects = objects->next;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001366 }
Nick Hengeveld1a703cb2006-03-10 20:18:33 -08001367
1368 return count;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001369}
1370
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001371static int update_remote(unsigned char *sha1, struct remote_lock *lock)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001372{
1373 struct active_request_slot *slot;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001374 struct slot_results results;
Mike Hommey028c2972007-12-09 20:30:59 +01001375 struct buffer out_buffer = { STRBUF_INIT, 0 };
Ray Chuanb1c7d4a2009-01-24 10:00:22 +08001376 struct curl_slist *dav_headers;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001377
Ray Chuanb1c7d4a2009-01-24 10:00:22 +08001378 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001379
Mike Hommey028c2972007-12-09 20:30:59 +01001380 strbuf_addf(&out_buffer.buf, "%s\n", sha1_to_hex(sha1));
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001381
1382 slot = get_active_slot();
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001383 slot->results = &results;
Dan McGeeebaaf312011-05-03 23:47:29 +08001384 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1385 &out_buffer, fwrite_null);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001386 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001387
1388 if (start_active_slot(slot)) {
1389 run_active_slot(slot);
Mike Hommey028c2972007-12-09 20:30:59 +01001390 strbuf_release(&out_buffer.buf);
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001391 if (results.curl_result != CURLE_OK) {
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001392 fprintf(stderr,
1393 "PUT error: curl result=%d, HTTP code=%ld\n",
Nick Hengeveldbaa7b672006-03-10 20:18:01 -08001394 results.curl_result, results.http_code);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001395 /* We should attempt recovery? */
1396 return 0;
1397 }
1398 } else {
Mike Hommey028c2972007-12-09 20:30:59 +01001399 strbuf_release(&out_buffer.buf);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001400 fprintf(stderr, "Unable to start PUT request\n");
1401 return 0;
1402 }
1403
1404 return 1;
1405}
1406
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001407static struct ref *remote_refs;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001408
Dan McGee2aab1672011-05-03 23:47:28 +08001409static void one_remote_ref(const char *refname)
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001410{
1411 struct ref *ref;
Nick Hengeveld197e8952006-03-10 20:18:18 -08001412 struct object *obj;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001413
René Scharfe59c69c02008-10-18 10:44:18 +02001414 ref = alloc_ref(refname);
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001415
Amos King7b5201a2009-03-18 18:43:53 -05001416 if (http_fetch_ref(repo->url, ref) != 0) {
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001417 fprintf(stderr,
1418 "Unable to fetch ref %s from %s\n",
Amos King7b5201a2009-03-18 18:43:53 -05001419 refname, repo->url);
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001420 free(ref);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001421 return;
1422 }
1423
Nick Hengeveld197e8952006-03-10 20:18:18 -08001424 /*
1425 * Fetch a copy of the object if it doesn't exist locally - it
1426 * may be required for updating server info later.
1427 */
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001428 if (repo->can_update_info_refs && !has_object_file(&ref->old_oid)) {
1429 obj = lookup_unknown_object(ref->old_oid.hash);
René Scharfee94eac42017-03-17 23:12:31 +01001430 fprintf(stderr, " fetch %s for %s\n",
1431 oid_to_hex(&ref->old_oid), refname);
1432 add_fetch_request(obj);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001433 }
1434
Clemens Buchacher6d2bf962009-05-31 16:26:48 +02001435 ref->next = remote_refs;
1436 remote_refs = ref;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001437}
1438
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001439static void get_dav_remote_heads(void)
1440{
Nick Hengeveld3030baa2006-03-10 20:18:08 -08001441 remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001442}
1443
Nick Hengeveld197e8952006-03-10 20:18:18 -08001444static void add_remote_info_ref(struct remote_ls_ctx *ls)
1445{
Mike Hommey028c2972007-12-09 20:30:59 +01001446 struct strbuf *buf = (struct strbuf *)ls->userData;
Nick Hengeveld197e8952006-03-10 20:18:18 -08001447 struct object *o;
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001448 struct ref *ref;
Nick Hengeveld197e8952006-03-10 20:18:18 -08001449
René Scharfe59c69c02008-10-18 10:44:18 +02001450 ref = alloc_ref(ls->dentry_name);
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001451
Amos King7b5201a2009-03-18 18:43:53 -05001452 if (http_fetch_ref(repo->url, ref) != 0) {
Nick Hengeveld197e8952006-03-10 20:18:18 -08001453 fprintf(stderr,
1454 "Unable to fetch ref %s from %s\n",
Amos King7b5201a2009-03-18 18:43:53 -05001455 ls->dentry_name, repo->url);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001456 aborted = 1;
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001457 free(ref);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001458 return;
1459 }
1460
brian m. carlsonc251c832017-05-06 22:10:38 +00001461 o = parse_object(&ref->old_oid);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001462 if (!o) {
1463 fprintf(stderr,
1464 "Unable to parse object %s for remote ref %s\n",
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001465 oid_to_hex(&ref->old_oid), ls->dentry_name);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001466 aborted = 1;
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001467 free(ref);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001468 return;
1469 }
1470
Jeff King7d0581a2015-09-24 17:07:29 -04001471 strbuf_addf(buf, "%s\t%s\n",
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001472 oid_to_hex(&ref->old_oid), ls->dentry_name);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001473
Linus Torvalds19746322006-07-11 20:45:31 -07001474 if (o->type == OBJ_TAG) {
Nick Hengeveld197e8952006-03-10 20:18:18 -08001475 o = deref_tag(o, ls->dentry_name, 0);
Jeff King7d0581a2015-09-24 17:07:29 -04001476 if (o)
1477 strbuf_addf(buf, "%s\t%s^{}\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001478 oid_to_hex(&o->oid), ls->dentry_name);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001479 }
Daniel Barkalowc13b2632008-04-26 15:53:09 -04001480 free(ref);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001481}
1482
1483static void update_remote_info_refs(struct remote_lock *lock)
1484{
Mike Hommey028c2972007-12-09 20:30:59 +01001485 struct buffer buffer = { STRBUF_INIT, 0 };
Nick Hengeveld197e8952006-03-10 20:18:18 -08001486 struct active_request_slot *slot;
1487 struct slot_results results;
Ray Chuanb1c7d4a2009-01-24 10:00:22 +08001488 struct curl_slist *dav_headers;
Nick Hengeveld197e8952006-03-10 20:18:18 -08001489
Nick Hengeveld197e8952006-03-10 20:18:18 -08001490 remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
Mike Hommey028c2972007-12-09 20:30:59 +01001491 add_remote_info_ref, &buffer.buf);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001492 if (!aborted) {
Ray Chuanb1c7d4a2009-01-24 10:00:22 +08001493 dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001494
1495 slot = get_active_slot();
1496 slot->results = &results;
Dan McGeeebaaf312011-05-03 23:47:29 +08001497 curl_setup_http(slot->curl, lock->url, DAV_PUT,
1498 &buffer, fwrite_null);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001499 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001500
Nick Hengeveld197e8952006-03-10 20:18:18 -08001501 if (start_active_slot(slot)) {
1502 run_active_slot(slot);
1503 if (results.curl_result != CURLE_OK) {
1504 fprintf(stderr,
1505 "PUT error: curl result=%d, HTTP code=%ld\n",
1506 results.curl_result, results.http_code);
1507 }
1508 }
Nick Hengeveld197e8952006-03-10 20:18:18 -08001509 }
Mike Hommey028c2972007-12-09 20:30:59 +01001510 strbuf_release(&buffer.buf);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001511}
1512
1513static int remote_exists(const char *path)
1514{
Jeff King28310182014-06-19 17:24:33 -04001515 char *url = xstrfmt("%s%s", repo->url, path);
Mike Hommey446b9412009-06-06 16:43:57 +08001516 int ret;
Nick Hengeveld197e8952006-03-10 20:18:18 -08001517
Nick Hengeveld197e8952006-03-10 20:18:18 -08001518
Jeff King1bbcc222013-09-28 04:31:23 -04001519 switch (http_get_strbuf(url, NULL, NULL)) {
Mike Hommey446b9412009-06-06 16:43:57 +08001520 case HTTP_OK:
1521 ret = 1;
1522 break;
1523 case HTTP_MISSING_TARGET:
1524 ret = 0;
1525 break;
1526 case HTTP_ERROR:
Jeff King4df13f62013-04-05 18:22:31 -04001527 error("unable to access '%s': %s", url, curl_errorstr);
Jeff King1cf01a32017-09-21 02:25:41 -04001528 /* fallthrough */
Mike Hommey446b9412009-06-06 16:43:57 +08001529 default:
1530 ret = -1;
Nick Hengeveld197e8952006-03-10 20:18:18 -08001531 }
Mike Hommey3a462bc2007-12-10 22:36:11 +01001532 free(url);
1533 return ret;
Nick Hengeveld197e8952006-03-10 20:18:18 -08001534}
1535
brian m. carlson8eb94602017-05-06 22:10:03 +00001536static void fetch_symref(const char *path, char **symref, struct object_id *oid)
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001537{
Jeff King28310182014-06-19 17:24:33 -04001538 char *url = xstrfmt("%s%s", repo->url, path);
Mike Hommey028c2972007-12-09 20:30:59 +01001539 struct strbuf buffer = STRBUF_INIT;
Jeff Kingae021d82014-06-18 15:47:50 -04001540 const char *name;
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001541
Jeff King1bbcc222013-09-28 04:31:23 -04001542 if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
Mike Hommey9af5abd2009-06-06 16:43:58 +08001543 die("Couldn't get %s for remote symref\n%s", url,
1544 curl_errorstr);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001545 free(url);
1546
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00001547 FREE_AND_NULL(*symref);
brian m. carlson8eb94602017-05-06 22:10:03 +00001548 oidclr(oid);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001549
Mike Hommey028c2972007-12-09 20:30:59 +01001550 if (buffer.len == 0)
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001551 return;
1552
Jeff Kingf6786c82015-01-12 21:28:58 -05001553 /* Cut off trailing newline. */
1554 strbuf_rtrim(&buffer);
1555
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001556 /* If it's a symref, set the refname; otherwise try for a sha1 */
Jeff Kingae021d82014-06-18 15:47:50 -04001557 if (skip_prefix(buffer.buf, "ref: ", &name)) {
1558 *symref = xmemdupz(name, buffer.len - (name - buffer.buf));
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001559 } else {
brian m. carlson8eb94602017-05-06 22:10:03 +00001560 get_oid_hex(buffer.buf, oid);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001561 }
1562
Mike Hommey028c2972007-12-09 20:30:59 +01001563 strbuf_release(&buffer);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001564}
1565
brian m. carlson8eb94602017-05-06 22:10:03 +00001566static int verify_merge_base(struct object_id *head_oid, struct ref *remote)
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001567{
brian m. carlsonbc832662017-05-06 22:10:10 +00001568 struct commit *head = lookup_commit_or_die(head_oid, "HEAD");
1569 struct commit *branch = lookup_commit_or_die(&remote->old_oid,
1570 remote->name);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001571
Junio C Hamano65712252012-08-27 15:21:37 -07001572 return in_merge_bases(branch, head);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001573}
1574
Dan McGee2aab1672011-05-03 23:47:28 +08001575static int delete_remote_branch(const char *pattern, int force)
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001576{
1577 struct ref *refs = remote_refs;
1578 struct ref *remote_ref = NULL;
brian m. carlson8eb94602017-05-06 22:10:03 +00001579 struct object_id head_oid;
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001580 char *symref = NULL;
1581 int match;
1582 int patlen = strlen(pattern);
1583 int i;
1584 struct active_request_slot *slot;
1585 struct slot_results results;
1586 char *url;
1587
1588 /* Find the remote branch(es) matching the specified branch name */
1589 for (match = 0; refs; refs = refs->next) {
1590 char *name = refs->name;
1591 int namelen = strlen(name);
1592 if (namelen < patlen ||
1593 memcmp(name + namelen - patlen, pattern, patlen))
1594 continue;
1595 if (namelen != patlen && name[namelen - patlen - 1] != '/')
1596 continue;
1597 match++;
1598 remote_ref = refs;
1599 }
1600 if (match == 0)
1601 return error("No remote branch matches %s", pattern);
1602 if (match != 1)
1603 return error("More than one remote branch matches %s",
1604 pattern);
1605
1606 /*
1607 * Remote HEAD must be a symref (not exactly foolproof; a remote
1608 * symlink to a symref will look like a symref)
1609 */
brian m. carlson8eb94602017-05-06 22:10:03 +00001610 fetch_symref("HEAD", &symref, &head_oid);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001611 if (!symref)
1612 return error("Remote HEAD is not a symref");
1613
1614 /* Remote branch must not be the remote HEAD */
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -07001615 for (i = 0; symref && i < MAXDEPTH; i++) {
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001616 if (!strcmp(remote_ref->name, symref))
1617 return error("Remote branch %s is the current HEAD",
1618 remote_ref->name);
brian m. carlson8eb94602017-05-06 22:10:03 +00001619 fetch_symref(symref, &symref, &head_oid);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001620 }
1621
1622 /* Run extra sanity checks if delete is not forced */
1623 if (!force) {
1624 /* Remote HEAD must resolve to a known object */
1625 if (symref)
1626 return error("Remote HEAD symrefs too deep");
brian m. carlson8eb94602017-05-06 22:10:03 +00001627 if (is_null_oid(&head_oid))
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001628 return error("Unable to resolve remote HEAD");
brian m. carlson8eb94602017-05-06 22:10:03 +00001629 if (!has_object_file(&head_oid))
1630 return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid));
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001631
1632 /* Remote branch must resolve to a known object */
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001633 if (is_null_oid(&remote_ref->old_oid))
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001634 return error("Unable to resolve remote branch %s",
1635 remote_ref->name);
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001636 if (!has_object_file(&remote_ref->old_oid))
1637 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));
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001638
1639 /* Remote branch must be an ancestor of remote HEAD */
brian m. carlson8eb94602017-05-06 22:10:03 +00001640 if (!verify_merge_base(&head_oid, remote_ref)) {
J. Bruce Fields00ae8282007-11-02 22:39:44 -04001641 return error("The branch '%s' is not an ancestor "
1642 "of your current HEAD.\n"
1643 "If you are sure you want to delete it,"
1644 " run:\n\t'git http-push -D %s %s'",
Amos King7b5201a2009-03-18 18:43:53 -05001645 remote_ref->name, repo->url, pattern);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001646 }
1647 }
1648
1649 /* Send delete request */
1650 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
Clemens Buchacher6eaf4062008-02-27 20:27:53 +01001651 if (dry_run)
1652 return 0;
Jeff King28310182014-06-19 17:24:33 -04001653 url = xstrfmt("%s%s", repo->url, remote_ref->name);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001654 slot = get_active_slot();
1655 slot->results = &results;
Dan McGeeebaaf312011-05-03 23:47:29 +08001656 curl_setup_http_get(slot->curl, url, DAV_DELETE);
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001657 if (start_active_slot(slot)) {
1658 run_active_slot(slot);
1659 free(url);
1660 if (results.curl_result != CURLE_OK)
Pete Wyckoff82247e92012-04-29 20:28:45 -04001661 return error("DELETE request failed (%d/%ld)",
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001662 results.curl_result, results.http_code);
1663 } else {
1664 free(url);
1665 return error("Unable to start DELETE request");
1666 }
1667
1668 return 0;
1669}
1670
Linus Torvalds2af202b2009-06-18 10:28:43 -07001671static void run_request_queue(void)
Tay Ray Chuan4f662502009-06-06 16:43:27 +08001672{
1673#ifdef USE_CURL_MULTI
1674 is_running_queue = 1;
1675 fill_active_slots();
1676 add_fill_function(NULL, fill_active_slot);
1677#endif
1678 do {
1679 finish_all_active_slots();
1680#ifdef USE_CURL_MULTI
1681 fill_active_slots();
1682#endif
1683 } while (request_queue_head && !aborted);
1684
1685#ifdef USE_CURL_MULTI
1686 is_running_queue = 0;
1687#endif
1688}
1689
Jeff King3f2e2292016-07-01 01:58:58 -04001690int cmd_main(int argc, const char **argv)
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001691{
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001692 struct transfer_request *request;
1693 struct transfer_request *next_request;
1694 int nr_refspec = 0;
Jeff King3f2e2292016-07-01 01:58:58 -04001695 const char **refspec = NULL;
Nick Hengeveld512d6322006-03-10 20:18:12 -08001696 struct remote_lock *ref_lock = NULL;
Nick Hengeveld197e8952006-03-10 20:18:18 -08001697 struct remote_lock *info_ref_lock = NULL;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001698 struct rev_info revs;
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001699 int delete_branch = 0;
1700 int force_delete = 0;
Nick Hengeveld1a703cb2006-03-10 20:18:33 -08001701 int objects_to_send;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001702 int rc = 0;
1703 int i;
Junio C Hamano8c9e7942006-03-21 15:49:05 -08001704 int new_refs;
Jay Soffian454e2022009-02-25 03:32:11 -05001705 struct ref *ref, *local_refs;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001706
Brian Gesiakf3d51ff2014-05-27 00:33:50 +09001707 repo = xcalloc(1, sizeof(*repo));
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001708
1709 argv++;
1710 for (i = 1; i < argc; i++, argv++) {
Jeff King3f2e2292016-07-01 01:58:58 -04001711 const char *arg = *argv;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001712
1713 if (*arg == '-') {
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001714 if (!strcmp(arg, "--all")) {
Andy Whitcroft28b9d6e2007-11-09 23:32:10 +00001715 push_all = MATCH_REFS_ALL;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001716 continue;
1717 }
1718 if (!strcmp(arg, "--force")) {
1719 force_all = 1;
1720 continue;
1721 }
Shawn O. Pearcefe5d1d32007-10-16 00:35:22 -04001722 if (!strcmp(arg, "--dry-run")) {
1723 dry_run = 1;
1724 continue;
1725 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001726 if (!strcmp(arg, "--helper-status")) {
1727 helper_status = 1;
1728 continue;
1729 }
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001730 if (!strcmp(arg, "--verbose")) {
1731 push_verbosely = 1;
Tay Ray Chuane9176742009-06-06 16:43:41 +08001732 http_is_verbose = 1;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001733 continue;
1734 }
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001735 if (!strcmp(arg, "-d")) {
1736 delete_branch = 1;
1737 continue;
1738 }
1739 if (!strcmp(arg, "-D")) {
1740 delete_branch = 1;
1741 force_delete = 1;
1742 continue;
1743 }
Jonathan Nieder548d3462009-11-09 04:47:34 -06001744 if (!strcmp(arg, "-h"))
1745 usage(http_push_usage);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001746 }
Amos King7b5201a2009-03-18 18:43:53 -05001747 if (!repo->url) {
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001748 char *path = strstr(arg, "//");
Tay Ray Chuan1462d1a2010-11-25 16:21:09 +08001749 str_end_url_with_slash(arg, &repo->url);
1750 repo->path_len = strlen(repo->url);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001751 if (path) {
Amos King7b5201a2009-03-18 18:43:53 -05001752 repo->path = strchr(path+2, '/');
1753 if (repo->path)
1754 repo->path_len = strlen(repo->path);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001755 }
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001756 continue;
1757 }
1758 refspec = argv;
1759 nr_refspec = argc - i;
1760 break;
1761 }
1762
Grégoire Barbierf8548242008-01-13 20:02:59 +01001763#ifndef USE_CURL_MULTI
1764 die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
1765#endif
1766
Amos King7b5201a2009-03-18 18:43:53 -05001767 if (!repo->url)
Nick Hengeveld3e9fabc2005-11-29 09:33:36 -08001768 usage(http_push_usage);
1769
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001770 if (delete_branch && nr_refspec != 1)
1771 die("You must specify only one branch name when deleting a remote branch");
1772
Jonathan Nieder548d3462009-11-09 04:47:34 -06001773 setup_git_directory();
1774
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001775 memset(remote_dir_exists, -1, 256);
Nick Hengeveld0dd276b2005-11-07 11:39:37 -08001776
Jeff Kinga4ddbc32011-12-13 19:11:56 -05001777 http_init(NULL, repo->url, 1);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001778
Tay Ray Chuan68862a32009-06-06 16:43:31 +08001779#ifdef USE_CURL_MULTI
Tay Ray Chuan4f662502009-06-06 16:43:27 +08001780 is_running_queue = 0;
Tay Ray Chuan68862a32009-06-06 16:43:31 +08001781#endif
Tay Ray Chuan4f662502009-06-06 16:43:27 +08001782
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001783 /* Verify DAV compliance/lock support */
Nick Hengeveldacf59572005-11-18 11:03:18 -08001784 if (!locking_available()) {
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001785 rc = 1;
1786 goto cleanup;
1787 }
1788
Jeff King57b235a2009-01-22 01:03:08 -05001789 sigchain_push_common(remove_locks_on_signal);
Clemens Buchacher6a491a12008-05-25 20:27:44 +02001790
Nick Hengeveld197e8952006-03-10 20:18:18 -08001791 /* Check whether the remote has server info files */
Amos King7b5201a2009-03-18 18:43:53 -05001792 repo->can_update_info_refs = 0;
1793 repo->has_info_refs = remote_exists("info/refs");
1794 repo->has_info_packs = remote_exists("objects/info/packs");
1795 if (repo->has_info_refs) {
Nick Hengeveld197e8952006-03-10 20:18:18 -08001796 info_ref_lock = lock_remote("info/refs", LOCK_TIME);
1797 if (info_ref_lock)
Amos King7b5201a2009-03-18 18:43:53 -05001798 repo->can_update_info_refs = 1;
Grégoire Barbier9bdbaba2008-01-19 16:22:48 +01001799 else {
Miklos Vajnad5c87cb2009-03-24 02:09:09 +01001800 error("cannot lock existing info/refs");
Grégoire Barbier9bdbaba2008-01-19 16:22:48 +01001801 rc = 1;
1802 goto cleanup;
1803 }
Nick Hengeveld197e8952006-03-10 20:18:18 -08001804 }
Amos King7b5201a2009-03-18 18:43:53 -05001805 if (repo->has_info_packs)
Nick Hengeveld197e8952006-03-10 20:18:18 -08001806 fetch_indices();
1807
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001808 /* Get a list of all local and remote heads to validate refspecs */
Jay Soffian454e2022009-02-25 03:32:11 -05001809 local_refs = get_local_heads();
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001810 fprintf(stderr, "Fetching remote heads...\n");
1811 get_dav_remote_heads();
Tay Ray Chuan4f662502009-06-06 16:43:27 +08001812 run_request_queue();
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001813
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001814 /* Remove a remote branch if -d or -D was specified */
1815 if (delete_branch) {
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001816 if (delete_remote_branch(refspec[0], force_delete) == -1) {
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001817 fprintf(stderr, "Unable to delete remote branch %s\n",
1818 refspec[0]);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001819 if (helper_status)
1820 printf("error %s cannot remove\n", refspec[0]);
1821 }
Nick Hengeveld3dfaf7b2006-03-20 10:31:06 -08001822 goto cleanup;
1823 }
1824
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001825 /* match them up */
Junio C Hamano29753cd2011-09-09 11:54:58 -07001826 if (match_push_refs(local_refs, &remote_refs,
1827 nr_refspec, (const char **) refspec, push_all)) {
Grégoire Barbier9116de52008-01-19 16:22:47 +01001828 rc = -1;
1829 goto cleanup;
1830 }
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001831 if (!remote_refs) {
1832 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001833 if (helper_status)
1834 printf("error null no match\n");
Grégoire Barbier9116de52008-01-19 16:22:47 +01001835 rc = 0;
1836 goto cleanup;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001837 }
1838
Junio C Hamano8c9e7942006-03-21 15:49:05 -08001839 new_refs = 0;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001840 for (ref = remote_refs; ref; ref = ref->next) {
Jeff Kinga0355f62015-09-24 17:07:56 -04001841 struct argv_array commit_argv = ARGV_ARRAY_INIT;
Junio C Hamano8c9e7942006-03-21 15:49:05 -08001842
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001843 if (!ref->peer_ref)
1844 continue;
Clemens Buchacher6eaf4062008-02-27 20:27:53 +01001845
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001846 if (is_null_oid(&ref->peer_ref->new_oid)) {
Clemens Buchacher6eaf4062008-02-27 20:27:53 +01001847 if (delete_remote_branch(ref->name, 1) == -1) {
1848 error("Could not remove %s", ref->name);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001849 if (helper_status)
1850 printf("error %s cannot remove\n", ref->name);
Clemens Buchacher6eaf4062008-02-27 20:27:53 +01001851 rc = -4;
1852 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001853 else if (helper_status)
1854 printf("ok %s\n", ref->name);
Clemens Buchacher6eaf4062008-02-27 20:27:53 +01001855 new_refs++;
1856 continue;
1857 }
1858
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001859 if (!oidcmp(&ref->old_oid, &ref->peer_ref->new_oid)) {
Tay Ray Chuanb5e59982010-03-02 18:49:27 +08001860 if (push_verbosely)
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001861 fprintf(stderr, "'%s': up-to-date\n", ref->name);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001862 if (helper_status)
1863 printf("ok %s up to date\n", ref->name);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001864 continue;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001865 }
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001866
1867 if (!force_all &&
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001868 !is_null_oid(&ref->old_oid) &&
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001869 !ref->force) {
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001870 if (!has_object_file(&ref->old_oid) ||
brian m. carlson6f3d57b2015-11-10 02:22:25 +00001871 !ref_newer(&ref->peer_ref->new_oid,
1872 &ref->old_oid)) {
J. Bruce Fields00ae8282007-11-02 22:39:44 -04001873 /*
1874 * We do not have the remote ref, or
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001875 * we know that the remote ref is not
1876 * an ancestor of what we are trying to
1877 * push. Either way this can be losing
1878 * commits at the remote end and likely
1879 * we were not up to date to begin with.
1880 */
J. Bruce Fields00ae8282007-11-02 22:39:44 -04001881 error("remote '%s' is not an ancestor of\n"
1882 "local '%s'.\n"
1883 "Maybe you are not up-to-date and "
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001884 "need to pull first?",
1885 ref->name,
1886 ref->peer_ref->name);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001887 if (helper_status)
1888 printf("error %s non-fast forward\n", ref->name);
Nick Hengeveld1a703cb2006-03-10 20:18:33 -08001889 rc = -2;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001890 continue;
1891 }
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001892 }
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001893 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001894 new_refs++;
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001895
1896 fprintf(stderr, "updating '%s'", ref->name);
1897 if (strcmp(ref->name, ref->peer_ref->name))
1898 fprintf(stderr, " using '%s'", ref->peer_ref->name);
Jeff King2b87d3a2015-09-24 17:08:05 -04001899 fprintf(stderr, "\n from %s\n to %s\n",
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001900 oid_to_hex(&ref->old_oid), oid_to_hex(&ref->new_oid));
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001901 if (dry_run) {
1902 if (helper_status)
1903 printf("ok %s\n", ref->name);
Shawn O. Pearcefe5d1d32007-10-16 00:35:22 -04001904 continue;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001905 }
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001906
1907 /* Lock remote branch ref */
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001908 ref_lock = lock_remote(ref->name, LOCK_TIME);
1909 if (ref_lock == NULL) {
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001910 fprintf(stderr, "Unable to lock remote branch %s\n",
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001911 ref->name);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001912 if (helper_status)
1913 printf("error %s lock error\n", ref->name);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001914 rc = 1;
1915 continue;
1916 }
1917
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001918 /* Set up revision info for this refspec */
Jeff Kinga0355f62015-09-24 17:07:56 -04001919 argv_array_push(&commit_argv, ""); /* ignored */
1920 argv_array_push(&commit_argv, "--objects");
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001921 argv_array_push(&commit_argv, oid_to_hex(&ref->new_oid));
1922 if (!push_all && !is_null_oid(&ref->old_oid))
Jeff Kinga0355f62015-09-24 17:07:56 -04001923 argv_array_pushf(&commit_argv, "^%s",
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001924 oid_to_hex(&ref->old_oid));
Linus Torvaldsdb6296a2006-07-28 21:21:48 -07001925 init_revisions(&revs, setup_git_directory());
Jeff Kinga0355f62015-09-24 17:07:56 -04001926 setup_revisions(commit_argv.argc, commit_argv.argv, &revs, NULL);
Junio C Hamanod633c882008-06-07 11:39:20 -07001927 revs.edge_hint = 0; /* just in case */
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001928
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001929 /* Generate a list of objects that need to be pushed */
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001930 pushing = 0;
Martin Koegler3d51e1b2008-02-18 08:31:56 +01001931 if (prepare_revision_walk(&revs))
1932 die("revision walk setup failed");
Nguyễn Thái Ngọc Duye76a5fb2013-08-16 16:52:06 +07001933 mark_edges_uninteresting(&revs, NULL);
Nick Hengeveld1a703cb2006-03-10 20:18:33 -08001934 objects_to_send = get_delta(&revs, ref_lock);
Nick Hengeveld29508e12005-11-18 11:02:58 -08001935 finish_all_active_slots();
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001936
1937 /* Push missing objects to remote, this would be a
1938 convenient time to pack them first if appropriate. */
1939 pushing = 1;
Nick Hengeveld1a703cb2006-03-10 20:18:33 -08001940 if (objects_to_send)
1941 fprintf(stderr, " sending %d objects\n",
1942 objects_to_send);
Tay Ray Chuan4f662502009-06-06 16:43:27 +08001943
1944 run_request_queue();
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001945
1946 /* Update the remote branch if all went well */
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001947 if (aborted || !update_remote(ref->new_oid.hash, ref_lock))
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001948 rc = 1;
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001949
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001950 if (!rc)
1951 fprintf(stderr, " done\n");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001952 if (helper_status)
1953 printf("%s %s\n", !rc ? "ok" : "error", ref->name);
Nick Hengeveldaa1dbc92006-03-07 16:13:20 -08001954 unlock_remote(ref_lock);
Nick Hengeveld512d6322006-03-10 20:18:12 -08001955 check_locks();
Jeff Kinga0355f62015-09-24 17:07:56 -04001956 argv_array_clear(&commit_argv);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001957 }
1958
Nick Hengeveld197e8952006-03-10 20:18:18 -08001959 /* Update remote server info if appropriate */
Amos King7b5201a2009-03-18 18:43:53 -05001960 if (repo->has_info_refs && new_refs) {
1961 if (info_ref_lock && repo->can_update_info_refs) {
Nick Hengeveld197e8952006-03-10 20:18:18 -08001962 fprintf(stderr, "Updating remote server info\n");
Shawn O. Pearcefe5d1d32007-10-16 00:35:22 -04001963 if (!dry_run)
1964 update_remote_info_refs(info_ref_lock);
Nick Hengeveld197e8952006-03-10 20:18:18 -08001965 } else {
1966 fprintf(stderr, "Unable to update server info\n");
1967 }
1968 }
Nick Hengeveld197e8952006-03-10 20:18:18 -08001969
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001970 cleanup:
Grégoire Barbier9116de52008-01-19 16:22:47 +01001971 if (info_ref_lock)
1972 unlock_remote(info_ref_lock);
Amos King7b5201a2009-03-18 18:43:53 -05001973 free(repo);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001974
Nick Hengeveld29508e12005-11-18 11:02:58 -08001975 http_cleanup();
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001976
1977 request = request_queue_head;
1978 while (request != NULL) {
1979 next_request = request->next;
1980 release_request(request);
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001981 request = next_request;
1982 }
1983
Nick Hengeveld58e60dd2005-11-02 11:19:24 -08001984 return rc;
1985}