THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1990 lines
50KB

  1. #include "cache.h"
  2. #include "repository.h"
  3. #include "commit.h"
  4. #include "tag.h"
  5. #include "blob.h"
  6. #include "http.h"
  7. #include "refs.h"
  8. #include "diff.h"
  9. #include "revision.h"
  10. #include "exec-cmd.h"
  11. #include "remote.h"
  12. #include "list-objects.h"
  13. #include "sigchain.h"
  14. #include "argv-array.h"
  15. #include "packfile.h"
  16. #include "object-store.h"
  17. #include "commit-reach.h"
  18. #ifdef EXPAT_NEEDS_XMLPARSE_H
  19. #include <xmlparse.h>
  20. #else
  21. #include <expat.h>
  22. #endif
  23. static const char http_push_usage[] =
  24. "git http-push [--all] [--dry-run] [--force] [--verbose] <remote> [<head>...]\n";
  25. #ifndef XML_STATUS_OK
  26. enum 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. #define PREV_BUF_SIZE 4096
  34. /* DAV methods */
  35. #define DAV_LOCK "LOCK"
  36. #define DAV_MKCOL "MKCOL"
  37. #define DAV_MOVE "MOVE"
  38. #define DAV_PROPFIND "PROPFIND"
  39. #define DAV_PUT "PUT"
  40. #define DAV_UNLOCK "UNLOCK"
  41. #define DAV_DELETE "DELETE"
  42. /* DAV lock flags */
  43. #define DAV_PROP_LOCKWR (1u << 0)
  44. #define DAV_PROP_LOCKEX (1u << 1)
  45. #define DAV_LOCK_OK (1u << 2)
  46. /* DAV XML properties */
  47. #define DAV_CTX_LOCKENTRY ".multistatus.response.propstat.prop.supportedlock.lockentry"
  48. #define DAV_CTX_LOCKTYPE_WRITE ".multistatus.response.propstat.prop.supportedlock.lockentry.locktype.write"
  49. #define DAV_CTX_LOCKTYPE_EXCLUSIVE ".multistatus.response.propstat.prop.supportedlock.lockentry.lockscope.exclusive"
  50. #define DAV_ACTIVELOCK_OWNER ".prop.lockdiscovery.activelock.owner.href"
  51. #define DAV_ACTIVELOCK_TIMEOUT ".prop.lockdiscovery.activelock.timeout"
  52. #define DAV_ACTIVELOCK_TOKEN ".prop.lockdiscovery.activelock.locktoken.href"
  53. #define DAV_PROPFIND_RESP ".multistatus.response"
  54. #define DAV_PROPFIND_NAME ".multistatus.response.href"
  55. #define DAV_PROPFIND_COLLECTION ".multistatus.response.propstat.prop.resourcetype.collection"
  56. /* DAV request body templates */
  57. #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>"
  58. #define PROPFIND_ALL_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/>\n</D:propfind>"
  59. #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>"
  60. #define LOCK_TIME 600
  61. #define LOCK_REFRESH 30
  62. /* Remember to update object flag allocation in object.h */
  63. #define LOCAL (1u<<16)
  64. #define REMOTE (1u<<17)
  65. #define FETCHING (1u<<18)
  66. #define PUSHING (1u<<19)
  67. /* We allow "recursive" symbolic refs. Only within reason, though */
  68. #define MAXDEPTH 5
  69. static int pushing;
  70. static int aborted;
  71. static signed char remote_dir_exists[256];
  72. static int push_verbosely;
  73. static int push_all = MATCH_REFS_NONE;
  74. static int force_all;
  75. static int dry_run;
  76. static int helper_status;
  77. static struct object_list *objects;
  78. struct repo {
  79. char *url;
  80. char *path;
  81. int path_len;
  82. int has_info_refs;
  83. int can_update_info_refs;
  84. int has_info_packs;
  85. struct packed_git *packs;
  86. struct remote_lock *locks;
  87. };
  88. static struct repo *repo;
  89. enum transfer_state {
  90. NEED_FETCH,
  91. RUN_FETCH_LOOSE,
  92. RUN_FETCH_PACKED,
  93. NEED_PUSH,
  94. RUN_MKCOL,
  95. RUN_PUT,
  96. RUN_MOVE,
  97. ABORTED,
  98. COMPLETE
  99. };
  100. struct transfer_request {
  101. struct object *obj;
  102. char *url;
  103. char *dest;
  104. struct remote_lock *lock;
  105. struct curl_slist *headers;
  106. struct buffer buffer;
  107. enum transfer_state state;
  108. CURLcode curl_result;
  109. char errorstr[CURL_ERROR_SIZE];
  110. long http_code;
  111. void *userData;
  112. struct active_request_slot *slot;
  113. struct transfer_request *next;
  114. };
  115. static struct transfer_request *request_queue_head;
  116. struct xml_ctx {
  117. char *name;
  118. int len;
  119. char *cdata;
  120. void (*userFunc)(struct xml_ctx *ctx, int tag_closed);
  121. void *userData;
  122. };
  123. struct remote_lock {
  124. char *url;
  125. char *owner;
  126. char *token;
  127. char tmpfile_suffix[GIT_MAX_HEXSZ + 1];
  128. time_t start_time;
  129. long timeout;
  130. int refreshing;
  131. struct remote_lock *next;
  132. };
  133. /* Flags that control remote_ls processing */
  134. #define PROCESS_FILES (1u << 0)
  135. #define PROCESS_DIRS (1u << 1)
  136. #define RECURSIVE (1u << 2)
  137. /* Flags that remote_ls passes to callback functions */
  138. #define IS_DIR (1u << 0)
  139. struct remote_ls_ctx {
  140. char *path;
  141. void (*userFunc)(struct remote_ls_ctx *ls);
  142. void *userData;
  143. int flags;
  144. char *dentry_name;
  145. int dentry_flags;
  146. struct remote_ls_ctx *parent;
  147. };
  148. /* get_dav_token_headers options */
  149. enum dav_header_flag {
  150. DAV_HEADER_IF = (1u << 0),
  151. DAV_HEADER_LOCK = (1u << 1),
  152. DAV_HEADER_TIMEOUT = (1u << 2)
  153. };
  154. static char *xml_entities(const char *s)
  155. {
  156. struct strbuf buf = STRBUF_INIT;
  157. strbuf_addstr_xml_quoted(&buf, s);
  158. return strbuf_detach(&buf, NULL);
  159. }
  160. static void curl_setup_http_get(CURL *curl, const char *url,
  161. const char *custom_req)
  162. {
  163. curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
  164. curl_easy_setopt(curl, CURLOPT_URL, url);
  165. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
  166. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_null);
  167. }
  168. static void curl_setup_http(CURL *curl, const char *url,
  169. const char *custom_req, struct buffer *buffer,
  170. curl_write_callback write_fn)
  171. {
  172. curl_easy_setopt(curl, CURLOPT_PUT, 1);
  173. curl_easy_setopt(curl, CURLOPT_URL, url);
  174. curl_easy_setopt(curl, CURLOPT_INFILE, buffer);
  175. curl_easy_setopt(curl, CURLOPT_INFILESIZE, buffer->buf.len);
  176. curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread_buffer);
  177. #ifndef NO_CURL_IOCTL
  178. curl_easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctl_buffer);
  179. curl_easy_setopt(curl, CURLOPT_IOCTLDATA, buffer);
  180. #endif
  181. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fn);
  182. curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
  183. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, custom_req);
  184. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  185. }
  186. static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
  187. {
  188. struct strbuf buf = STRBUF_INIT;
  189. struct curl_slist *dav_headers = http_copy_default_headers();
  190. if (options & DAV_HEADER_IF) {
  191. strbuf_addf(&buf, "If: (<%s>)", lock->token);
  192. dav_headers = curl_slist_append(dav_headers, buf.buf);
  193. strbuf_reset(&buf);
  194. }
  195. if (options & DAV_HEADER_LOCK) {
  196. strbuf_addf(&buf, "Lock-Token: <%s>", lock->token);
  197. dav_headers = curl_slist_append(dav_headers, buf.buf);
  198. strbuf_reset(&buf);
  199. }
  200. if (options & DAV_HEADER_TIMEOUT) {
  201. strbuf_addf(&buf, "Timeout: Second-%ld", lock->timeout);
  202. dav_headers = curl_slist_append(dav_headers, buf.buf);
  203. strbuf_reset(&buf);
  204. }
  205. strbuf_release(&buf);
  206. return dav_headers;
  207. }
  208. static void finish_request(struct transfer_request *request);
  209. static void release_request(struct transfer_request *request);
  210. static void process_response(void *callback_data)
  211. {
  212. struct transfer_request *request =
  213. (struct transfer_request *)callback_data;
  214. finish_request(request);
  215. }
  216. #ifdef USE_CURL_MULTI
  217. static void start_fetch_loose(struct transfer_request *request)
  218. {
  219. struct active_request_slot *slot;
  220. struct http_object_request *obj_req;
  221. obj_req = new_http_object_request(repo->url, &request->obj->oid);
  222. if (obj_req == NULL) {
  223. request->state = ABORTED;
  224. return;
  225. }
  226. slot = obj_req->slot;
  227. slot->callback_func = process_response;
  228. slot->callback_data = request;
  229. request->slot = slot;
  230. request->userData = obj_req;
  231. /* Try to get the request started, abort the request on error */
  232. request->state = RUN_FETCH_LOOSE;
  233. if (!start_active_slot(slot)) {
  234. fprintf(stderr, "Unable to start GET request\n");
  235. repo->can_update_info_refs = 0;
  236. release_http_object_request(obj_req);
  237. release_request(request);
  238. }
  239. }
  240. static void start_mkcol(struct transfer_request *request)
  241. {
  242. char *hex = oid_to_hex(&request->obj->oid);
  243. struct active_request_slot *slot;
  244. request->url = get_remote_object_url(repo->url, hex, 1);
  245. slot = get_active_slot();
  246. slot->callback_func = process_response;
  247. slot->callback_data = request;
  248. curl_setup_http_get(slot->curl, request->url, DAV_MKCOL);
  249. curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
  250. if (start_active_slot(slot)) {
  251. request->slot = slot;
  252. request->state = RUN_MKCOL;
  253. } else {
  254. request->state = ABORTED;
  255. FREE_AND_NULL(request->url);
  256. }
  257. }
  258. #endif
  259. static void start_fetch_packed(struct transfer_request *request)
  260. {
  261. struct packed_git *target;
  262. struct transfer_request *check_request = request_queue_head;
  263. struct http_pack_request *preq;
  264. target = find_sha1_pack(request->obj->oid.hash, repo->packs);
  265. if (!target) {
  266. fprintf(stderr, "Unable to fetch %s, will not be able to update server info refs\n", oid_to_hex(&request->obj->oid));
  267. repo->can_update_info_refs = 0;
  268. release_request(request);
  269. return;
  270. }
  271. fprintf(stderr, "Fetching pack %s\n",
  272. hash_to_hex(target->hash));
  273. fprintf(stderr, " which contains %s\n", oid_to_hex(&request->obj->oid));
  274. preq = new_http_pack_request(target, repo->url);
  275. if (preq == NULL) {
  276. repo->can_update_info_refs = 0;
  277. return;
  278. }
  279. preq->lst = &repo->packs;
  280. /* Make sure there isn't another open request for this pack */
  281. while (check_request) {
  282. if (check_request->state == RUN_FETCH_PACKED &&
  283. !strcmp(check_request->url, preq->url)) {
  284. release_http_pack_request(preq);
  285. release_request(request);
  286. return;
  287. }
  288. check_request = check_request->next;
  289. }
  290. preq->slot->callback_func = process_response;
  291. preq->slot->callback_data = request;
  292. request->slot = preq->slot;
  293. request->userData = preq;
  294. /* Try to get the request started, abort the request on error */
  295. request->state = RUN_FETCH_PACKED;
  296. if (!start_active_slot(preq->slot)) {
  297. fprintf(stderr, "Unable to start GET request\n");
  298. release_http_pack_request(preq);
  299. repo->can_update_info_refs = 0;
  300. release_request(request);
  301. }
  302. }
  303. static void start_put(struct transfer_request *request)
  304. {
  305. char *hex = oid_to_hex(&request->obj->oid);
  306. struct active_request_slot *slot;
  307. struct strbuf buf = STRBUF_INIT;
  308. enum object_type type;
  309. char hdr[50];
  310. void *unpacked;
  311. unsigned long len;
  312. int hdrlen;
  313. ssize_t size;
  314. git_zstream stream;
  315. unpacked = read_object_file(&request->obj->oid, &type, &len);
  316. hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1;
  317. /* Set it up */
  318. git_deflate_init(&stream, zlib_compression_level);
  319. size = git_deflate_bound(&stream, len + hdrlen);
  320. strbuf_init(&request->buffer.buf, size);
  321. request->buffer.posn = 0;
  322. /* Compress it */
  323. stream.next_out = (unsigned char *)request->buffer.buf.buf;
  324. stream.avail_out = size;
  325. /* First header.. */
  326. stream.next_in = (void *)hdr;
  327. stream.avail_in = hdrlen;
  328. while (git_deflate(&stream, 0) == Z_OK)
  329. ; /* nothing */
  330. /* Then the data itself.. */
  331. stream.next_in = unpacked;
  332. stream.avail_in = len;
  333. while (git_deflate(&stream, Z_FINISH) == Z_OK)
  334. ; /* nothing */
  335. git_deflate_end(&stream);
  336. free(unpacked);
  337. request->buffer.buf.len = stream.total_out;
  338. strbuf_addstr(&buf, "Destination: ");
  339. append_remote_object_url(&buf, repo->url, hex, 0);
  340. request->dest = strbuf_detach(&buf, NULL);
  341. append_remote_object_url(&buf, repo->url, hex, 0);
  342. strbuf_add(&buf, request->lock->tmpfile_suffix, the_hash_algo->hexsz + 1);
  343. request->url = strbuf_detach(&buf, NULL);
  344. slot = get_active_slot();
  345. slot->callback_func = process_response;
  346. slot->callback_data = request;
  347. curl_setup_http(slot->curl, request->url, DAV_PUT,
  348. &request->buffer, fwrite_null);
  349. if (start_active_slot(slot)) {
  350. request->slot = slot;
  351. request->state = RUN_PUT;
  352. } else {
  353. request->state = ABORTED;
  354. FREE_AND_NULL(request->url);
  355. }
  356. }
  357. static void start_move(struct transfer_request *request)
  358. {
  359. struct active_request_slot *slot;
  360. struct curl_slist *dav_headers = http_copy_default_headers();
  361. slot = get_active_slot();
  362. slot->callback_func = process_response;
  363. slot->callback_data = request;
  364. curl_setup_http_get(slot->curl, request->url, DAV_MOVE);
  365. dav_headers = curl_slist_append(dav_headers, request->dest);
  366. dav_headers = curl_slist_append(dav_headers, "Overwrite: T");
  367. curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
  368. if (start_active_slot(slot)) {
  369. request->slot = slot;
  370. request->state = RUN_MOVE;
  371. } else {
  372. request->state = ABORTED;
  373. FREE_AND_NULL(request->url);
  374. }
  375. }
  376. static int refresh_lock(struct remote_lock *lock)
  377. {
  378. struct active_request_slot *slot;
  379. struct slot_results results;
  380. struct curl_slist *dav_headers;
  381. int rc = 0;
  382. lock->refreshing = 1;
  383. dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF | DAV_HEADER_TIMEOUT);
  384. slot = get_active_slot();
  385. slot->results = &results;
  386. curl_setup_http_get(slot->curl, lock->url, DAV_LOCK);
  387. curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
  388. if (start_active_slot(slot)) {
  389. run_active_slot(slot);
  390. if (results.curl_result != CURLE_OK) {
  391. fprintf(stderr, "LOCK HTTP error %ld\n",
  392. results.http_code);
  393. } else {
  394. lock->start_time = time(NULL);
  395. rc = 1;
  396. }
  397. }
  398. lock->refreshing = 0;
  399. curl_slist_free_all(dav_headers);
  400. return rc;
  401. }
  402. static void check_locks(void)
  403. {
  404. struct remote_lock *lock = repo->locks;
  405. time_t current_time = time(NULL);
  406. int time_remaining;
  407. while (lock) {
  408. time_remaining = lock->start_time + lock->timeout -
  409. current_time;
  410. if (!lock->refreshing && time_remaining < LOCK_REFRESH) {
  411. if (!refresh_lock(lock)) {
  412. fprintf(stderr,
  413. "Unable to refresh lock for %s\n",
  414. lock->url);
  415. aborted = 1;
  416. return;
  417. }
  418. }
  419. lock = lock->next;
  420. }
  421. }
  422. static void release_request(struct transfer_request *request)
  423. {
  424. struct transfer_request *entry = request_queue_head;
  425. if (request == request_queue_head) {
  426. request_queue_head = request->next;
  427. } else {
  428. while (entry && entry->next != request)
  429. entry = entry->next;
  430. if (entry)
  431. entry->next = request->next;
  432. }
  433. free(request->url);
  434. free(request);
  435. }
  436. static void finish_request(struct transfer_request *request)
  437. {
  438. struct http_pack_request *preq;
  439. struct http_object_request *obj_req;
  440. request->curl_result = request->slot->curl_result;
  441. request->http_code = request->slot->http_code;
  442. request->slot = NULL;
  443. /* Keep locks active */
  444. check_locks();
  445. if (request->headers != NULL)
  446. curl_slist_free_all(request->headers);
  447. /* URL is reused for MOVE after PUT and used during FETCH */
  448. if (request->state != RUN_PUT && request->state != RUN_FETCH_PACKED) {
  449. FREE_AND_NULL(request->url);
  450. }
  451. if (request->state == RUN_MKCOL) {
  452. if (request->curl_result == CURLE_OK ||
  453. request->http_code == 405) {
  454. remote_dir_exists[request->obj->oid.hash[0]] = 1;
  455. start_put(request);
  456. } else {
  457. fprintf(stderr, "MKCOL %s failed, aborting (%d/%ld)\n",
  458. oid_to_hex(&request->obj->oid),
  459. request->curl_result, request->http_code);
  460. request->state = ABORTED;
  461. aborted = 1;
  462. }
  463. } else if (request->state == RUN_PUT) {
  464. if (request->curl_result == CURLE_OK) {
  465. start_move(request);
  466. } else {
  467. fprintf(stderr, "PUT %s failed, aborting (%d/%ld)\n",
  468. oid_to_hex(&request->obj->oid),
  469. request->curl_result, request->http_code);
  470. request->state = ABORTED;
  471. aborted = 1;
  472. }
  473. } else if (request->state == RUN_MOVE) {
  474. if (request->curl_result == CURLE_OK) {
  475. if (push_verbosely)
  476. fprintf(stderr, " sent %s\n",
  477. oid_to_hex(&request->obj->oid));
  478. request->obj->flags |= REMOTE;
  479. release_request(request);
  480. } else {
  481. fprintf(stderr, "MOVE %s failed, aborting (%d/%ld)\n",
  482. oid_to_hex(&request->obj->oid),
  483. request->curl_result, request->http_code);
  484. request->state = ABORTED;
  485. aborted = 1;
  486. }
  487. } else if (request->state == RUN_FETCH_LOOSE) {
  488. obj_req = (struct http_object_request *)request->userData;
  489. if (finish_http_object_request(obj_req) == 0)
  490. if (obj_req->rename == 0)
  491. request->obj->flags |= (LOCAL | REMOTE);
  492. /* Try fetching packed if necessary */
  493. if (request->obj->flags & LOCAL) {
  494. release_http_object_request(obj_req);
  495. release_request(request);
  496. } else
  497. start_fetch_packed(request);
  498. } else if (request->state == RUN_FETCH_PACKED) {
  499. int fail = 1;
  500. if (request->curl_result != CURLE_OK) {
  501. fprintf(stderr, "Unable to get pack file %s\n%s",
  502. request->url, curl_errorstr);
  503. } else {
  504. preq = (struct http_pack_request *)request->userData;
  505. if (preq) {
  506. if (finish_http_pack_request(preq) == 0)
  507. fail = 0;
  508. release_http_pack_request(preq);
  509. }
  510. }
  511. if (fail)
  512. repo->can_update_info_refs = 0;
  513. release_request(request);
  514. }
  515. }
  516. #ifdef USE_CURL_MULTI
  517. static int is_running_queue;
  518. static int fill_active_slot(void *unused)
  519. {
  520. struct transfer_request *request;
  521. if (aborted || !is_running_queue)
  522. return 0;
  523. for (request = request_queue_head; request; request = request->next) {
  524. if (request->state == NEED_FETCH) {
  525. start_fetch_loose(request);
  526. return 1;
  527. } else if (pushing && request->state == NEED_PUSH) {
  528. if (remote_dir_exists[request->obj->oid.hash[0]] == 1) {
  529. start_put(request);
  530. } else {
  531. start_mkcol(request);
  532. }
  533. return 1;
  534. }
  535. }
  536. return 0;
  537. }
  538. #endif
  539. static void get_remote_object_list(unsigned char parent);
  540. static void add_fetch_request(struct object *obj)
  541. {
  542. struct transfer_request *request;
  543. check_locks();
  544. /*
  545. * Don't fetch the object if it's known to exist locally
  546. * or is already in the request queue
  547. */
  548. if (remote_dir_exists[obj->oid.hash[0]] == -1)
  549. get_remote_object_list(obj->oid.hash[0]);
  550. if (obj->flags & (LOCAL | FETCHING))
  551. return;
  552. obj->flags |= FETCHING;
  553. request = xmalloc(sizeof(*request));
  554. request->obj = obj;
  555. request->url = NULL;
  556. request->lock = NULL;
  557. request->headers = NULL;
  558. request->state = NEED_FETCH;
  559. request->next = request_queue_head;
  560. request_queue_head = request;
  561. #ifdef USE_CURL_MULTI
  562. fill_active_slots();
  563. step_active_slots();
  564. #endif
  565. }
  566. static int add_send_request(struct object *obj, struct remote_lock *lock)
  567. {
  568. struct transfer_request *request;
  569. struct packed_git *target;
  570. /* Keep locks active */
  571. check_locks();
  572. /*
  573. * Don't push the object if it's known to exist on the remote
  574. * or is already in the request queue
  575. */
  576. if (remote_dir_exists[obj->oid.hash[0]] == -1)
  577. get_remote_object_list(obj->oid.hash[0]);
  578. if (obj->flags & (REMOTE | PUSHING))
  579. return 0;
  580. target = find_sha1_pack(obj->oid.hash, repo->packs);
  581. if (target) {
  582. obj->flags |= REMOTE;
  583. return 0;
  584. }
  585. obj->flags |= PUSHING;
  586. request = xmalloc(sizeof(*request));
  587. request->obj = obj;
  588. request->url = NULL;
  589. request->lock = lock;
  590. request->headers = NULL;
  591. request->state = NEED_PUSH;
  592. request->next = request_queue_head;
  593. request_queue_head = request;
  594. #ifdef USE_CURL_MULTI
  595. fill_active_slots();
  596. step_active_slots();
  597. #endif
  598. return 1;
  599. }
  600. static int fetch_indices(void)
  601. {
  602. int ret;
  603. if (push_verbosely)
  604. fprintf(stderr, "Getting pack list\n");
  605. switch (http_get_info_packs(repo->url, &repo->packs)) {
  606. case HTTP_OK:
  607. case HTTP_MISSING_TARGET:
  608. ret = 0;
  609. break;
  610. default:
  611. ret = -1;
  612. }
  613. return ret;
  614. }
  615. static void one_remote_object(const struct object_id *oid)
  616. {
  617. struct object *obj;
  618. obj = lookup_object(the_repository, oid);
  619. if (!obj)
  620. obj = parse_object(the_repository, oid);
  621. /* Ignore remote objects that don't exist locally */
  622. if (!obj)
  623. return;
  624. obj->flags |= REMOTE;
  625. if (!object_list_contains(objects, obj))
  626. object_list_insert(obj, &objects);
  627. }
  628. static void handle_lockprop_ctx(struct xml_ctx *ctx, int tag_closed)
  629. {
  630. int *lock_flags = (int *)ctx->userData;
  631. if (tag_closed) {
  632. if (!strcmp(ctx->name, DAV_CTX_LOCKENTRY)) {
  633. if ((*lock_flags & DAV_PROP_LOCKEX) &&
  634. (*lock_flags & DAV_PROP_LOCKWR)) {
  635. *lock_flags |= DAV_LOCK_OK;
  636. }
  637. *lock_flags &= DAV_LOCK_OK;
  638. } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_WRITE)) {
  639. *lock_flags |= DAV_PROP_LOCKWR;
  640. } else if (!strcmp(ctx->name, DAV_CTX_LOCKTYPE_EXCLUSIVE)) {
  641. *lock_flags |= DAV_PROP_LOCKEX;
  642. }
  643. }
  644. }
  645. static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
  646. {
  647. struct remote_lock *lock = (struct remote_lock *)ctx->userData;
  648. git_hash_ctx hash_ctx;
  649. unsigned char lock_token_hash[GIT_MAX_RAWSZ];
  650. if (tag_closed && ctx->cdata) {
  651. if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
  652. lock->owner = xstrdup(ctx->cdata);
  653. } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
  654. const char *arg;
  655. if (skip_prefix(ctx->cdata, "Second-", &arg))
  656. lock->timeout = strtol(arg, NULL, 10);
  657. } else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
  658. lock->token = xstrdup(ctx->cdata);
  659. the_hash_algo->init_fn(&hash_ctx);
  660. the_hash_algo->update_fn(&hash_ctx, lock->token, strlen(lock->token));
  661. the_hash_algo->final_fn(lock_token_hash, &hash_ctx);
  662. lock->tmpfile_suffix[0] = '_';
  663. memcpy(lock->tmpfile_suffix + 1, hash_to_hex(lock_token_hash), the_hash_algo->hexsz);
  664. }
  665. }
  666. }
  667. static void one_remote_ref(const char *refname);
  668. static void
  669. xml_start_tag(void *userData, const char *name, const char **atts)
  670. {
  671. struct xml_ctx *ctx = (struct xml_ctx *)userData;
  672. const char *c = strchr(name, ':');
  673. int old_namelen, new_len;
  674. if (c == NULL)
  675. c = name;
  676. else
  677. c++;
  678. old_namelen = strlen(ctx->name);
  679. new_len = old_namelen + strlen(c) + 2;
  680. if (new_len > ctx->len) {
  681. ctx->name = xrealloc(ctx->name, new_len);
  682. ctx->len = new_len;
  683. }
  684. xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
  685. FREE_AND_NULL(ctx->cdata);
  686. ctx->userFunc(ctx, 0);
  687. }
  688. static void
  689. xml_end_tag(void *userData, const char *name)
  690. {
  691. struct xml_ctx *ctx = (struct xml_ctx *)userData;
  692. const char *c = strchr(name, ':');
  693. char *ep;
  694. ctx->userFunc(ctx, 1);
  695. if (c == NULL)
  696. c = name;
  697. else
  698. c++;
  699. ep = ctx->name + strlen(ctx->name) - strlen(c) - 1;
  700. *ep = 0;
  701. }
  702. static void
  703. xml_cdata(void *userData, const XML_Char *s, int len)
  704. {
  705. struct xml_ctx *ctx = (struct xml_ctx *)userData;
  706. free(ctx->cdata);
  707. ctx->cdata = xmemdupz(s, len);
  708. }
  709. static struct remote_lock *lock_remote(const char *path, long timeout)
  710. {
  711. struct active_request_slot *slot;
  712. struct slot_results results;
  713. struct buffer out_buffer = { STRBUF_INIT, 0 };
  714. struct strbuf in_buffer = STRBUF_INIT;
  715. char *url;
  716. char *ep;
  717. char timeout_header[25];
  718. struct remote_lock *lock = NULL;
  719. struct curl_slist *dav_headers = http_copy_default_headers();
  720. struct xml_ctx ctx;
  721. char *escaped;
  722. url = xstrfmt("%s%s", repo->url, path);
  723. /* Make sure leading directories exist for the remote ref */
  724. ep = strchr(url + strlen(repo->url) + 1, '/');
  725. while (ep) {
  726. char saved_character = ep[1];
  727. ep[1] = '\0';
  728. slot = get_active_slot();
  729. slot->results = &results;
  730. curl_setup_http_get(slot->curl, url, DAV_MKCOL);
  731. if (start_active_slot(slot)) {
  732. run_active_slot(slot);
  733. if (results.curl_result != CURLE_OK &&
  734. results.http_code != 405) {
  735. fprintf(stderr,
  736. "Unable to create branch path %s\n",
  737. url);
  738. free(url);
  739. return NULL;
  740. }
  741. } else {
  742. fprintf(stderr, "Unable to start MKCOL request\n");
  743. free(url);
  744. return NULL;
  745. }
  746. ep[1] = saved_character;
  747. ep = strchr(ep + 1, '/');
  748. }
  749. escaped = xml_entities(ident_default_email());
  750. strbuf_addf(&out_buffer.buf, LOCK_REQUEST, escaped);
  751. free(escaped);
  752. xsnprintf(timeout_header, sizeof(timeout_header), "Timeout: Second-%ld", timeout);
  753. dav_headers = curl_slist_append(dav_headers, timeout_header);
  754. dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
  755. slot = get_active_slot();
  756. slot->results = &results;
  757. curl_setup_http(slot->curl, url, DAV_LOCK, &out_buffer, fwrite_buffer);
  758. curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
  759. curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
  760. lock = xcalloc(1, sizeof(*lock));
  761. lock->timeout = -1;
  762. if (start_active_slot(slot)) {
  763. run_active_slot(slot);
  764. if (results.curl_result == CURLE_OK) {
  765. XML_Parser parser = XML_ParserCreate(NULL);
  766. enum XML_Status result;
  767. ctx.name = xcalloc(10, 1);
  768. ctx.len = 0;
  769. ctx.cdata = NULL;
  770. ctx.userFunc = handle_new_lock_ctx;
  771. ctx.userData = lock;
  772. XML_SetUserData(parser, &ctx);
  773. XML_SetElementHandler(parser, xml_start_tag,
  774. xml_end_tag);
  775. XML_SetCharacterDataHandler(parser, xml_cdata);
  776. result = XML_Parse(parser, in_buffer.buf,
  777. in_buffer.len, 1);
  778. free(ctx.name);
  779. if (result != XML_STATUS_OK) {
  780. fprintf(stderr, "XML error: %s\n",
  781. XML_ErrorString(
  782. XML_GetErrorCode(parser)));
  783. lock->timeout = -1;
  784. }
  785. XML_ParserFree(parser);
  786. } else {
  787. fprintf(stderr,
  788. "error: curl result=%d, HTTP code=%ld\n",
  789. results.curl_result, results.http_code);
  790. }
  791. } else {
  792. fprintf(stderr, "Unable to start LOCK request\n");
  793. }
  794. curl_slist_free_all(dav_headers);
  795. strbuf_release(&out_buffer.buf);
  796. strbuf_release(&in_buffer);
  797. if (lock->token == NULL || lock->timeout <= 0) {
  798. free(lock->token);
  799. free(lock->owner);
  800. free(url);
  801. FREE_AND_NULL(lock);
  802. } else {
  803. lock->url = url;
  804. lock->start_time = time(NULL);
  805. lock->next = repo->locks;
  806. repo->locks = lock;
  807. }
  808. return lock;
  809. }
  810. static int unlock_remote(struct remote_lock *lock)
  811. {
  812. struct active_request_slot *slot;
  813. struct slot_results results;
  814. struct remote_lock *prev = repo->locks;
  815. struct curl_slist *dav_headers;
  816. int rc = 0;
  817. dav_headers = get_dav_token_headers(lock, DAV_HEADER_LOCK);
  818. slot = get_active_slot();
  819. slot->results = &results;
  820. curl_setup_http_get(slot->curl, lock->url, DAV_UNLOCK);
  821. curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
  822. if (start_active_slot(slot)) {
  823. run_active_slot(slot);
  824. if (results.curl_result == CURLE_OK)
  825. rc = 1;
  826. else
  827. fprintf(stderr, "UNLOCK HTTP error %ld\n",
  828. results.http_code);
  829. } else {
  830. fprintf(stderr, "Unable to start UNLOCK request\n");
  831. }
  832. curl_slist_free_all(dav_headers);
  833. if (repo->locks == lock) {
  834. repo->locks = lock->next;
  835. } else {
  836. while (prev && prev->next != lock)
  837. prev = prev->next;
  838. if (prev)
  839. prev->next = lock->next;
  840. }
  841. free(lock->owner);
  842. free(lock->url);
  843. free(lock->token);
  844. free(lock);
  845. return rc;
  846. }
  847. static void remove_locks(void)
  848. {
  849. struct remote_lock *lock = repo->locks;
  850. fprintf(stderr, "Removing remote locks...\n");
  851. while (lock) {
  852. struct remote_lock *next = lock->next;
  853. unlock_remote(lock);
  854. lock = next;
  855. }
  856. }
  857. static void remove_locks_on_signal(int signo)
  858. {
  859. remove_locks();
  860. sigchain_pop(signo);
  861. raise(signo);
  862. }
  863. static void remote_ls(const char *path, int flags,
  864. void (*userFunc)(struct remote_ls_ctx *ls),
  865. void *userData);
  866. /* extract hex from sharded "xx/x{38}" filename */
  867. static int get_oid_hex_from_objpath(const char *path, struct object_id *oid)
  868. {
  869. if (strlen(path) != the_hash_algo->hexsz + 1)
  870. return -1;
  871. if (hex_to_bytes(oid->hash, path, 1))
  872. return -1;
  873. path += 2;
  874. path++; /* skip '/' */
  875. return hex_to_bytes(oid->hash + 1, path, the_hash_algo->rawsz - 1);
  876. }
  877. static void process_ls_object(struct remote_ls_ctx *ls)
  878. {
  879. unsigned int *parent = (unsigned int *)ls->userData;
  880. const char *path = ls->dentry_name;
  881. struct object_id oid;
  882. if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
  883. remote_dir_exists[*parent] = 1;
  884. return;
  885. }
  886. if (!skip_prefix(path, "objects/", &path) ||
  887. get_oid_hex_from_objpath(path, &oid))
  888. return;
  889. one_remote_object(&oid);
  890. }
  891. static void process_ls_ref(struct remote_ls_ctx *ls)
  892. {
  893. if (!strcmp(ls->path, ls->dentry_name) && (ls->dentry_flags & IS_DIR)) {
  894. fprintf(stderr, " %s\n", ls->dentry_name);
  895. return;
  896. }
  897. if (!(ls->dentry_flags & IS_DIR))
  898. one_remote_ref(ls->dentry_name);
  899. }
  900. static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
  901. {
  902. struct remote_ls_ctx *ls = (struct remote_ls_ctx *)ctx->userData;
  903. if (tag_closed) {
  904. if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
  905. if (ls->dentry_flags & IS_DIR) {
  906. /* ensure collection names end with slash */
  907. str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
  908. if (ls->flags & PROCESS_DIRS) {
  909. ls->userFunc(ls);
  910. }
  911. if (strcmp(ls->dentry_name, ls->path) &&
  912. ls->flags & RECURSIVE) {
  913. remote_ls(ls->dentry_name,
  914. ls->flags,
  915. ls->userFunc,
  916. ls->userData);
  917. }
  918. } else if (ls->flags & PROCESS_FILES) {
  919. ls->userFunc(ls);
  920. }
  921. } else if (!strcmp(ctx->name, DAV_PROPFIND_NAME) && ctx->cdata) {
  922. char *path = ctx->cdata;
  923. if (*ctx->cdata == 'h') {
  924. path = strstr(path, "//");
  925. if (path) {
  926. path = strchr(path+2, '/');
  927. }
  928. }
  929. if (path) {
  930. const char *url = repo->url;
  931. if (repo->path)
  932. url = repo->path;
  933. if (strncmp(path, url, repo->path_len))
  934. error("Parsed path '%s' does not match url: '%s'",
  935. path, url);
  936. else {
  937. path += repo->path_len;
  938. ls->dentry_name = xstrdup(path);
  939. }
  940. }
  941. } else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
  942. ls->dentry_flags |= IS_DIR;
  943. }
  944. } else if (!strcmp(ctx->name, DAV_PROPFIND_RESP)) {
  945. FREE_AND_NULL(ls->dentry_name);
  946. ls->dentry_flags = 0;
  947. }
  948. }
  949. /*
  950. * NEEDSWORK: remote_ls() ignores info/refs on the remote side. But it
  951. * should _only_ heed the information from that file, instead of trying to
  952. * determine the refs from the remote file system (badly: it does not even
  953. * know about packed-refs).
  954. */
  955. static void remote_ls(const char *path, int flags,
  956. void (*userFunc)(struct remote_ls_ctx *ls),
  957. void *userData)
  958. {
  959. char *url = xstrfmt("%s%s", repo->url, path);
  960. struct active_request_slot *slot;
  961. struct slot_results results;
  962. struct strbuf in_buffer = STRBUF_INIT;
  963. struct buffer out_buffer = { STRBUF_INIT, 0 };
  964. struct curl_slist *dav_headers = http_copy_default_headers();
  965. struct xml_ctx ctx;
  966. struct remote_ls_ctx ls;
  967. ls.flags = flags;
  968. ls.path = xstrdup(path);
  969. ls.dentry_name = NULL;
  970. ls.dentry_flags = 0;
  971. ls.userData = userData;
  972. ls.userFunc = userFunc;
  973. strbuf_addstr(&out_buffer.buf, PROPFIND_ALL_REQUEST);
  974. dav_headers = curl_slist_append(dav_headers, "Depth: 1");
  975. dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
  976. slot = get_active_slot();
  977. slot->results = &results;
  978. curl_setup_http(slot->curl, url, DAV_PROPFIND,
  979. &out_buffer, fwrite_buffer);
  980. curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
  981. curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
  982. if (start_active_slot(slot)) {
  983. run_active_slot(slot);
  984. if (results.curl_result == CURLE_OK) {
  985. XML_Parser parser = XML_ParserCreate(NULL);
  986. enum XML_Status result;
  987. ctx.name = xcalloc(10, 1);
  988. ctx.len = 0;
  989. ctx.cdata = NULL;
  990. ctx.userFunc = handle_remote_ls_ctx;
  991. ctx.userData = &ls;
  992. XML_SetUserData(parser, &ctx);
  993. XML_SetElementHandler(parser, xml_start_tag,
  994. xml_end_tag);
  995. XML_SetCharacterDataHandler(parser, xml_cdata);
  996. result = XML_Parse(parser, in_buffer.buf,
  997. in_buffer.len, 1);
  998. free(ctx.name);
  999. if (result != XML_STATUS_OK) {
  1000. fprintf(stderr, "XML error: %s\n",
  1001. XML_ErrorString(
  1002. XML_GetErrorCode(parser)));
  1003. }
  1004. XML_ParserFree(parser);
  1005. }
  1006. } else {
  1007. fprintf(stderr, "Unable to start PROPFIND request\n");
  1008. }
  1009. free(ls.path);
  1010. free(url);
  1011. strbuf_release(&out_buffer.buf);
  1012. strbuf_release(&in_buffer);
  1013. curl_slist_free_all(dav_headers);
  1014. }
  1015. static void get_remote_object_list(unsigned char parent)
  1016. {
  1017. char path[] = "objects/XX/";
  1018. static const char hex[] = "0123456789abcdef";
  1019. unsigned int val = parent;
  1020. path[8] = hex[val >> 4];
  1021. path[9] = hex[val & 0xf];
  1022. remote_dir_exists[val] = 0;
  1023. remote_ls(path, (PROCESS_FILES | PROCESS_DIRS),
  1024. process_ls_object, &val);
  1025. }
  1026. static int locking_available(void)
  1027. {
  1028. struct active_request_slot *slot;
  1029. struct slot_results results;
  1030. struct strbuf in_buffer = STRBUF_INIT;
  1031. struct buffer out_buffer = { STRBUF_INIT, 0 };
  1032. struct curl_slist *dav_headers = http_copy_default_headers();
  1033. struct xml_ctx ctx;
  1034. int lock_flags = 0;
  1035. char *escaped;
  1036. escaped = xml_entities(repo->url);
  1037. strbuf_addf(&out_buffer.buf, PROPFIND_SUPPORTEDLOCK_REQUEST, escaped);
  1038. free(escaped);
  1039. dav_headers = curl_slist_append(dav_headers, "Depth: 0");
  1040. dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
  1041. slot = get_active_slot();
  1042. slot->results = &results;
  1043. curl_setup_http(slot->curl, repo->url, DAV_PROPFIND,
  1044. &out_buffer, fwrite_buffer);
  1045. curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
  1046. curl_easy_setopt(slot->curl, CURLOPT_FILE, &in_buffer);
  1047. if (start_active_slot(slot)) {
  1048. run_active_slot(slot);
  1049. if (results.curl_result == CURLE_OK) {
  1050. XML_Parser parser = XML_ParserCreate(NULL);
  1051. enum XML_Status result;
  1052. ctx.name = xcalloc(10, 1);
  1053. ctx.len = 0;
  1054. ctx.cdata = NULL;
  1055. ctx.userFunc = handle_lockprop_ctx;
  1056. ctx.userData = &lock_flags;
  1057. XML_SetUserData(parser, &ctx);
  1058. XML_SetElementHandler(parser, xml_start_tag,
  1059. xml_end_tag);
  1060. result = XML_Parse(parser, in_buffer.buf,
  1061. in_buffer.len, 1);
  1062. free(ctx.name);
  1063. if (result != XML_STATUS_OK) {
  1064. fprintf(stderr, "XML error: %s\n",
  1065. XML_ErrorString(
  1066. XML_GetErrorCode(parser)));
  1067. lock_flags = 0;
  1068. }
  1069. XML_ParserFree(parser);
  1070. if (!lock_flags)
  1071. error("no DAV locking support on %s",
  1072. repo->url);
  1073. } else {
  1074. error("Cannot access URL %s, return code %d",
  1075. repo->url, results.curl_result);
  1076. lock_flags = 0;
  1077. }
  1078. } else {
  1079. error("Unable to start PROPFIND request on %s", repo->url);
  1080. }
  1081. strbuf_release(&out_buffer.buf);
  1082. strbuf_release(&in_buffer);
  1083. curl_slist_free_all(dav_headers);
  1084. return lock_flags;
  1085. }
  1086. static struct object_list **add_one_object(struct object *obj, struct object_list **p)
  1087. {
  1088. struct object_list *entry = xmalloc(sizeof(struct object_list));
  1089. entry->item = obj;
  1090. entry->next = *p;
  1091. *p = entry;
  1092. return &entry->next;
  1093. }
  1094. static struct object_list **process_blob(struct blob *blob,
  1095. struct object_list **p)
  1096. {
  1097. struct object *obj = &blob->object;
  1098. obj->flags |= LOCAL;
  1099. if (obj->flags & (UNINTERESTING | SEEN))
  1100. return p;
  1101. obj->flags |= SEEN;
  1102. return add_one_object(obj, p);
  1103. }
  1104. static struct object_list **process_tree(struct tree *tree,
  1105. struct object_list **p)
  1106. {
  1107. struct object *obj = &tree->object;
  1108. struct tree_desc desc;
  1109. struct name_entry entry;
  1110. obj->flags |= LOCAL;
  1111. if (obj->flags & (UNINTERESTING | SEEN))
  1112. return p;
  1113. if (parse_tree(tree) < 0)
  1114. die("bad tree object %s", oid_to_hex(&obj->oid));
  1115. obj->flags |= SEEN;
  1116. p = add_one_object(obj, p);
  1117. init_tree_desc(&desc, tree->buffer, tree->size);
  1118. while (tree_entry(&desc, &entry))
  1119. switch (object_type(entry.mode)) {
  1120. case OBJ_TREE:
  1121. p = process_tree(lookup_tree(the_repository, &entry.oid),
  1122. p);
  1123. break;
  1124. case OBJ_BLOB:
  1125. p = process_blob(lookup_blob(the_repository, &entry.oid),
  1126. p);
  1127. break;
  1128. default:
  1129. /* Subproject commit - not in this repository */
  1130. break;
  1131. }
  1132. free_tree_buffer(tree);
  1133. return p;
  1134. }
  1135. static int get_delta(struct rev_info *revs, struct remote_lock *lock)
  1136. {
  1137. int i;
  1138. struct commit *commit;
  1139. struct object_list **p = &objects;
  1140. int count = 0;
  1141. while ((commit = get_revision(revs)) != NULL) {
  1142. p = process_tree(get_commit_tree(commit), p);
  1143. commit->object.flags |= LOCAL;
  1144. if (!(commit->object.flags & UNINTERESTING))
  1145. count += add_send_request(&commit->object, lock);
  1146. }
  1147. for (i = 0; i < revs->pending.nr; i++) {
  1148. struct object_array_entry *entry = revs->pending.objects + i;
  1149. struct object *obj = entry->item;
  1150. const char *name = entry->name;
  1151. if (obj->flags & (UNINTERESTING | SEEN))
  1152. continue;
  1153. if (obj->type == OBJ_TAG) {
  1154. obj->flags |= SEEN;
  1155. p = add_one_object(obj, p);
  1156. continue;
  1157. }
  1158. if (obj->type == OBJ_TREE) {
  1159. p = process_tree((struct tree *)obj, p);
  1160. continue;
  1161. }
  1162. if (obj->type == OBJ_BLOB) {
  1163. p = process_blob((struct blob *)obj, p);
  1164. continue;
  1165. }
  1166. die("unknown pending object %s (%s)", oid_to_hex(&obj->oid), name);
  1167. }
  1168. while (objects) {
  1169. if (!(objects->item->flags & UNINTERESTING))
  1170. count += add_send_request(objects->item, lock);
  1171. objects = objects->next;
  1172. }
  1173. return count;
  1174. }
  1175. static int update_remote(const struct object_id *oid, struct remote_lock *lock)
  1176. {
  1177. struct active_request_slot *slot;
  1178. struct slot_results results;
  1179. struct buffer out_buffer = { STRBUF_INIT, 0 };
  1180. struct curl_slist *dav_headers;
  1181. dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
  1182. strbuf_addf(&out_buffer.buf, "%s\n", oid_to_hex(oid));
  1183. slot = get_active_slot();
  1184. slot->results = &results;
  1185. curl_setup_http(slot->curl, lock->url, DAV_PUT,
  1186. &out_buffer, fwrite_null);
  1187. curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
  1188. if (start_active_slot(slot)) {
  1189. run_active_slot(slot);
  1190. strbuf_release(&out_buffer.buf);
  1191. if (results.curl_result != CURLE_OK) {
  1192. fprintf(stderr,
  1193. "PUT error: curl result=%d, HTTP code=%ld\n",
  1194. results.curl_result, results.http_code);
  1195. /* We should attempt recovery? */
  1196. return 0;
  1197. }
  1198. } else {
  1199. strbuf_release(&out_buffer.buf);
  1200. fprintf(stderr, "Unable to start PUT request\n");
  1201. return 0;
  1202. }
  1203. return 1;
  1204. }
  1205. static struct ref *remote_refs;
  1206. static void one_remote_ref(const char *refname)
  1207. {
  1208. struct ref *ref;
  1209. struct object *obj;
  1210. ref = alloc_ref(refname);
  1211. if (http_fetch_ref(repo->url, ref) != 0) {
  1212. fprintf(stderr,
  1213. "Unable to fetch ref %s from %s\n",
  1214. refname, repo->url);
  1215. free(ref);
  1216. return;
  1217. }
  1218. /*
  1219. * Fetch a copy of the object if it doesn't exist locally - it
  1220. * may be required for updating server info later.
  1221. */
  1222. if (repo->can_update_info_refs && !has_object_file(&ref->old_oid)) {
  1223. obj = lookup_unknown_object(&ref->old_oid);
  1224. fprintf(stderr, " fetch %s for %s\n",
  1225. oid_to_hex(&ref->old_oid), refname);
  1226. add_fetch_request(obj);
  1227. }
  1228. ref->next = remote_refs;
  1229. remote_refs = ref;
  1230. }
  1231. static void get_dav_remote_heads(void)
  1232. {
  1233. remote_ls("refs/", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), process_ls_ref, NULL);
  1234. }
  1235. static void add_remote_info_ref(struct remote_ls_ctx *ls)
  1236. {
  1237. struct strbuf *buf = (struct strbuf *)ls->userData;
  1238. struct object *o;
  1239. struct ref *ref;
  1240. ref = alloc_ref(ls->dentry_name);
  1241. if (http_fetch_ref(repo->url, ref) != 0) {
  1242. fprintf(stderr,
  1243. "Unable to fetch ref %s from %s\n",
  1244. ls->dentry_name, repo->url);
  1245. aborted = 1;
  1246. free(ref);
  1247. return;
  1248. }
  1249. o = parse_object(the_repository, &ref->old_oid);
  1250. if (!o) {
  1251. fprintf(stderr,
  1252. "Unable to parse object %s for remote ref %s\n",
  1253. oid_to_hex(&ref->old_oid), ls->dentry_name);
  1254. aborted = 1;
  1255. free(ref);
  1256. return;
  1257. }
  1258. strbuf_addf(buf, "%s\t%s\n",
  1259. oid_to_hex(&ref->old_oid), ls->dentry_name);
  1260. if (o->type == OBJ_TAG) {
  1261. o = deref_tag(the_repository, o, ls->dentry_name, 0);
  1262. if (o)
  1263. strbuf_addf(buf, "%s\t%s^{}\n",
  1264. oid_to_hex(&o->oid), ls->dentry_name);
  1265. }
  1266. free(ref);
  1267. }
  1268. static void update_remote_info_refs(struct remote_lock *lock)
  1269. {
  1270. struct buffer buffer = { STRBUF_INIT, 0 };
  1271. struct active_request_slot *slot;
  1272. struct slot_results results;
  1273. struct curl_slist *dav_headers;
  1274. remote_ls("refs/", (PROCESS_FILES | RECURSIVE),
  1275. add_remote_info_ref, &buffer.buf);
  1276. if (!aborted) {
  1277. dav_headers = get_dav_token_headers(lock, DAV_HEADER_IF);
  1278. slot = get_active_slot();
  1279. slot->results = &results;
  1280. curl_setup_http(slot->curl, lock->url, DAV_PUT,
  1281. &buffer, fwrite_null);
  1282. curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
  1283. if (start_active_slot(slot)) {
  1284. run_active_slot(slot);
  1285. if (results.curl_result != CURLE_OK) {
  1286. fprintf(stderr,
  1287. "PUT error: curl result=%d, HTTP code=%ld\n",
  1288. results.curl_result, results.http_code);
  1289. }
  1290. }
  1291. }
  1292. strbuf_release(&buffer.buf);
  1293. }
  1294. static int remote_exists(const char *path)
  1295. {
  1296. char *url = xstrfmt("%s%s", repo->url, path);
  1297. int ret;
  1298. switch (http_get_strbuf(url, NULL, NULL)) {
  1299. case HTTP_OK:
  1300. ret = 1;
  1301. break;
  1302. case HTTP_MISSING_TARGET:
  1303. ret = 0;
  1304. break;
  1305. case HTTP_ERROR:
  1306. error("unable to access '%s': %s", url, curl_errorstr);
  1307. /* fallthrough */
  1308. default:
  1309. ret = -1;
  1310. }
  1311. free(url);
  1312. return ret;
  1313. }
  1314. static void fetch_symref(const char *path, char **symref, struct object_id *oid)
  1315. {
  1316. char *url = xstrfmt("%s%s", repo->url, path);
  1317. struct strbuf buffer = STRBUF_INIT;
  1318. const char *name;
  1319. if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
  1320. die("Couldn't get %s for remote symref\n%s", url,
  1321. curl_errorstr);
  1322. free(url);
  1323. FREE_AND_NULL(*symref);
  1324. oidclr(oid);
  1325. if (buffer.len == 0)
  1326. return;
  1327. /* Cut off trailing newline. */
  1328. strbuf_rtrim(&buffer);
  1329. /* If it's a symref, set the refname; otherwise try for a sha1 */
  1330. if (skip_prefix(buffer.buf, "ref: ", &name)) {
  1331. *symref = xmemdupz(name, buffer.len - (name - buffer.buf));
  1332. } else {
  1333. get_oid_hex(buffer.buf, oid);
  1334. }
  1335. strbuf_release(&buffer);
  1336. }
  1337. static int verify_merge_base(struct object_id *head_oid, struct ref *remote)
  1338. {
  1339. struct commit *head = lookup_commit_or_die(head_oid, "HEAD");
  1340. struct commit *branch = lookup_commit_or_die(&remote->old_oid,
  1341. remote->name);
  1342. return in_merge_bases(branch, head);
  1343. }
  1344. static int delete_remote_branch(const char *pattern, int force)
  1345. {
  1346. struct ref *refs = remote_refs;
  1347. struct ref *remote_ref = NULL;
  1348. struct object_id head_oid;
  1349. char *symref = NULL;
  1350. int match;
  1351. int patlen = strlen(pattern);
  1352. int i;
  1353. struct active_request_slot *slot;
  1354. struct slot_results results;
  1355. char *url;
  1356. /* Find the remote branch(es) matching the specified branch name */
  1357. for (match = 0; refs; refs = refs->next) {
  1358. char *name = refs->name;
  1359. int namelen = strlen(name);
  1360. if (namelen < patlen ||
  1361. memcmp(name + namelen - patlen, pattern, patlen))
  1362. continue;
  1363. if (namelen != patlen && name[namelen - patlen - 1] != '/')
  1364. continue;
  1365. match++;
  1366. remote_ref = refs;
  1367. }
  1368. if (match == 0)
  1369. return error("No remote branch matches %s", pattern);
  1370. if (match != 1)
  1371. return error("More than one remote branch matches %s",
  1372. pattern);
  1373. /*
  1374. * Remote HEAD must be a symref (not exactly foolproof; a remote
  1375. * symlink to a symref will look like a symref)
  1376. */
  1377. fetch_symref("HEAD", &symref, &head_oid);
  1378. if (!symref)
  1379. return error("Remote HEAD is not a symref");
  1380. /* Remote branch must not be the remote HEAD */
  1381. for (i = 0; symref && i < MAXDEPTH; i++) {
  1382. if (!strcmp(remote_ref->name, symref))
  1383. return error("Remote branch %s is the current HEAD",
  1384. remote_ref->name);
  1385. fetch_symref(symref, &symref, &head_oid);
  1386. }
  1387. /* Run extra sanity checks if delete is not forced */
  1388. if (!force) {
  1389. /* Remote HEAD must resolve to a known object */
  1390. if (symref)
  1391. return error("Remote HEAD symrefs too deep");
  1392. if (is_null_oid(&head_oid))
  1393. return error("Unable to resolve remote HEAD");
  1394. if (!has_object_file(&head_oid))
  1395. return error("Remote HEAD resolves to object %s\nwhich does not exist locally, perhaps you need to fetch?", oid_to_hex(&head_oid));
  1396. /* Remote branch must resolve to a known object */
  1397. if (is_null_oid(&remote_ref->old_oid))
  1398. return error("Unable to resolve remote branch %s",
  1399. remote_ref->name);
  1400. if (!has_object_file(&remote_ref->old_oid))
  1401. 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));
  1402. /* Remote branch must be an ancestor of remote HEAD */
  1403. if (!verify_merge_base(&head_oid, remote_ref)) {
  1404. return error("The branch '%s' is not an ancestor "
  1405. "of your current HEAD.\n"
  1406. "If you are sure you want to delete it,"
  1407. " run:\n\t'git http-push -D %s %s'",
  1408. remote_ref->name, repo->url, pattern);
  1409. }
  1410. }
  1411. /* Send delete request */
  1412. fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
  1413. if (dry_run)
  1414. return 0;
  1415. url = xstrfmt("%s%s", repo->url, remote_ref->name);
  1416. slot = get_active_slot();
  1417. slot->results = &results;
  1418. curl_setup_http_get(slot->curl, url, DAV_DELETE);
  1419. if (start_active_slot(slot)) {
  1420. run_active_slot(slot);
  1421. free(url);
  1422. if (results.curl_result != CURLE_OK)
  1423. return error("DELETE request failed (%d/%ld)",
  1424. results.curl_result, results.http_code);
  1425. } else {
  1426. free(url);
  1427. return error("Unable to start DELETE request");
  1428. }
  1429. return 0;
  1430. }
  1431. static void run_request_queue(void)
  1432. {
  1433. #ifdef USE_CURL_MULTI
  1434. is_running_queue = 1;
  1435. fill_active_slots();
  1436. add_fill_function(NULL, fill_active_slot);
  1437. #endif
  1438. do {
  1439. finish_all_active_slots();
  1440. #ifdef USE_CURL_MULTI
  1441. fill_active_slots();
  1442. #endif
  1443. } while (request_queue_head && !aborted);
  1444. #ifdef USE_CURL_MULTI
  1445. is_running_queue = 0;
  1446. #endif
  1447. }
  1448. int cmd_main(int argc, const char **argv)
  1449. {
  1450. struct transfer_request *request;
  1451. struct transfer_request *next_request;
  1452. struct refspec rs = REFSPEC_INIT_PUSH;
  1453. struct remote_lock *ref_lock = NULL;
  1454. struct remote_lock *info_ref_lock = NULL;
  1455. struct rev_info revs;
  1456. int delete_branch = 0;
  1457. int force_delete = 0;
  1458. int objects_to_send;
  1459. int rc = 0;
  1460. int i;
  1461. int new_refs;
  1462. struct ref *ref, *local_refs;
  1463. repo = xcalloc(1, sizeof(*repo));
  1464. argv++;
  1465. for (i = 1; i < argc; i++, argv++) {
  1466. const char *arg = *argv;
  1467. if (*arg == '-') {
  1468. if (!strcmp(arg, "--all")) {
  1469. push_all = MATCH_REFS_ALL;
  1470. continue;
  1471. }
  1472. if (!strcmp(arg, "--force")) {
  1473. force_all = 1;
  1474. continue;
  1475. }
  1476. if (!strcmp(arg, "--dry-run")) {
  1477. dry_run = 1;
  1478. continue;
  1479. }
  1480. if (!strcmp(arg, "--helper-status")) {
  1481. helper_status = 1;
  1482. continue;
  1483. }
  1484. if (!strcmp(arg, "--verbose")) {
  1485. push_verbosely = 1;
  1486. http_is_verbose = 1;
  1487. continue;
  1488. }
  1489. if (!strcmp(arg, "-d")) {
  1490. delete_branch = 1;
  1491. continue;
  1492. }
  1493. if (!strcmp(arg, "-D")) {
  1494. delete_branch = 1;
  1495. force_delete = 1;
  1496. continue;
  1497. }
  1498. if (!strcmp(arg, "-h"))
  1499. usage(http_push_usage);
  1500. }
  1501. if (!repo->url) {
  1502. char *path = strstr(arg, "//");
  1503. str_end_url_with_slash(arg, &repo->url);
  1504. repo->path_len = strlen(repo->url);
  1505. if (path) {
  1506. repo->path = strchr(path+2, '/');
  1507. if (repo->path)
  1508. repo->path_len = strlen(repo->path);
  1509. }
  1510. continue;
  1511. }
  1512. refspec_appendn(&rs, argv, argc - i);
  1513. break;
  1514. }
  1515. #ifndef USE_CURL_MULTI
  1516. die("git-push is not available for http/https repository when not compiled with USE_CURL_MULTI");
  1517. #endif
  1518. if (!repo->url)
  1519. usage(http_push_usage);
  1520. if (delete_branch && rs.nr != 1)
  1521. die("You must specify only one branch name when deleting a remote branch");
  1522. setup_git_directory();
  1523. memset(remote_dir_exists, -1, 256);
  1524. http_init(NULL, repo->url, 1);
  1525. #ifdef USE_CURL_MULTI
  1526. is_running_queue = 0;
  1527. #endif
  1528. /* Verify DAV compliance/lock support */
  1529. if (!locking_available()) {
  1530. rc = 1;
  1531. goto cleanup;
  1532. }
  1533. sigchain_push_common(remove_locks_on_signal);
  1534. /* Check whether the remote has server info files */
  1535. repo->can_update_info_refs = 0;
  1536. repo->has_info_refs = remote_exists("info/refs");
  1537. repo->has_info_packs = remote_exists("objects/info/packs");
  1538. if (repo->has_info_refs) {
  1539. info_ref_lock = lock_remote("info/refs", LOCK_TIME);
  1540. if (info_ref_lock)
  1541. repo->can_update_info_refs = 1;
  1542. else {
  1543. error("cannot lock existing info/refs");
  1544. rc = 1;
  1545. goto cleanup;
  1546. }
  1547. }
  1548. if (repo->has_info_packs)
  1549. fetch_indices();
  1550. /* Get a list of all local and remote heads to validate refspecs */
  1551. local_refs = get_local_heads();
  1552. fprintf(stderr, "Fetching remote heads...\n");
  1553. get_dav_remote_heads();
  1554. run_request_queue();
  1555. /* Remove a remote branch if -d or -D was specified */
  1556. if (delete_branch) {
  1557. const char *branch = rs.items[i].src;
  1558. if (delete_remote_branch(branch, force_delete) == -1) {
  1559. fprintf(stderr, "Unable to delete remote branch %s\n",
  1560. branch);
  1561. if (helper_status)
  1562. printf("error %s cannot remove\n", branch);
  1563. }
  1564. goto cleanup;
  1565. }
  1566. /* match them up */
  1567. if (match_push_refs(local_refs, &remote_refs, &rs, push_all)) {
  1568. rc = -1;
  1569. goto cleanup;
  1570. }
  1571. if (!remote_refs) {
  1572. fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
  1573. if (helper_status)
  1574. printf("error null no match\n");
  1575. rc = 0;
  1576. goto cleanup;
  1577. }
  1578. new_refs = 0;
  1579. for (ref = remote_refs; ref; ref = ref->next) {
  1580. struct argv_array commit_argv = ARGV_ARRAY_INIT;
  1581. if (!ref->peer_ref)
  1582. continue;
  1583. if (is_null_oid(&ref->peer_ref->new_oid)) {
  1584. if (delete_remote_branch(ref->name, 1) == -1) {
  1585. error("Could not remove %s", ref->name);
  1586. if (helper_status)
  1587. printf("error %s cannot remove\n", ref->name);
  1588. rc = -4;
  1589. }
  1590. else if (helper_status)
  1591. printf("ok %s\n", ref->name);
  1592. new_refs++;
  1593. continue;
  1594. }
  1595. if (oideq(&ref->old_oid, &ref->peer_ref->new_oid)) {
  1596. if (push_verbosely)
  1597. fprintf(stderr, "'%s': up-to-date\n", ref->name);
  1598. if (helper_status)
  1599. printf("ok %s up to date\n", ref->name);
  1600. continue;
  1601. }
  1602. if (!force_all &&
  1603. !is_null_oid(&ref->old_oid) &&
  1604. !ref->force) {
  1605. if (!has_object_file(&ref->old_oid) ||
  1606. !ref_newer(&ref->peer_ref->new_oid,
  1607. &ref->old_oid)) {
  1608. /*
  1609. * We do not have the remote ref, or
  1610. * we know that the remote ref is not
  1611. * an ancestor of what we are trying to
  1612. * push. Either way this can be losing
  1613. * commits at the remote end and likely
  1614. * we were not up to date to begin with.
  1615. */
  1616. error("remote '%s' is not an ancestor of\n"
  1617. "local '%s'.\n"
  1618. "Maybe you are not up-to-date and "
  1619. "need to pull first?",
  1620. ref->name,
  1621. ref->peer_ref->name);
  1622. if (helper_status)
  1623. printf("error %s non-fast forward\n", ref->name);
  1624. rc = -2;
  1625. continue;
  1626. }
  1627. }
  1628. oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
  1629. new_refs++;
  1630. fprintf(stderr, "updating '%s'", ref->name);
  1631. if (strcmp(ref->name, ref->peer_ref->name))
  1632. fprintf(stderr, " using '%s'", ref->peer_ref->name);
  1633. fprintf(stderr, "\n from %s\n to %s\n",
  1634. oid_to_hex(&ref->old_oid), oid_to_hex(&ref->new_oid));
  1635. if (dry_run) {
  1636. if (helper_status)
  1637. printf("ok %s\n", ref->name);
  1638. continue;
  1639. }
  1640. /* Lock remote branch ref */
  1641. ref_lock = lock_remote(ref->name, LOCK_TIME);
  1642. if (ref_lock == NULL) {
  1643. fprintf(stderr, "Unable to lock remote branch %s\n",
  1644. ref->name);
  1645. if (helper_status)
  1646. printf("error %s lock error\n", ref->name);
  1647. rc = 1;
  1648. continue;
  1649. }
  1650. /* Set up revision info for this refspec */
  1651. argv_array_push(&commit_argv, ""); /* ignored */
  1652. argv_array_push(&commit_argv, "--objects");
  1653. argv_array_push(&commit_argv, oid_to_hex(&ref->new_oid));
  1654. if (!push_all && !is_null_oid(&ref->old_oid))
  1655. argv_array_pushf(&commit_argv, "^%s",
  1656. oid_to_hex(&ref->old_oid));
  1657. repo_init_revisions(the_repository, &revs, setup_git_directory());
  1658. setup_revisions(commit_argv.argc, commit_argv.argv, &revs, NULL);
  1659. revs.edge_hint = 0; /* just in case */
  1660. /* Generate a list of objects that need to be pushed */
  1661. pushing = 0;
  1662. if (prepare_revision_walk(&revs))
  1663. die("revision walk setup failed");
  1664. mark_edges_uninteresting(&revs, NULL, 0);
  1665. objects_to_send = get_delta(&revs, ref_lock);
  1666. finish_all_active_slots();
  1667. /* Push missing objects to remote, this would be a
  1668. convenient time to pack them first if appropriate. */
  1669. pushing = 1;
  1670. if (objects_to_send)
  1671. fprintf(stderr, " sending %d objects\n",
  1672. objects_to_send);
  1673. run_request_queue();
  1674. /* Update the remote branch if all went well */
  1675. if (aborted || !update_remote(&ref->new_oid, ref_lock))
  1676. rc = 1;
  1677. if (!rc)
  1678. fprintf(stderr, " done\n");
  1679. if (helper_status)
  1680. printf("%s %s\n", !rc ? "ok" : "error", ref->name);
  1681. unlock_remote(ref_lock);
  1682. check_locks();
  1683. argv_array_clear(&commit_argv);
  1684. }
  1685. /* Update remote server info if appropriate */
  1686. if (repo->has_info_refs && new_refs) {
  1687. if (info_ref_lock && repo->can_update_info_refs) {
  1688. fprintf(stderr, "Updating remote server info\n");
  1689. if (!dry_run)
  1690. update_remote_info_refs(info_ref_lock);
  1691. } else {
  1692. fprintf(stderr, "Unable to update server info\n");
  1693. }
  1694. }
  1695. cleanup:
  1696. if (info_ref_lock)
  1697. unlock_remote(info_ref_lock);
  1698. free(repo);
  1699. http_cleanup();
  1700. request = request_queue_head;
  1701. while (request != NULL) {
  1702. next_request = request->next;
  1703. release_request(request);
  1704. request = next_request;
  1705. }
  1706. return rc;
  1707. }