Merge pull request #840 from carlosmn/remote-unify Unify the transport code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
diff --git a/examples/network/fetch.c b/examples/network/fetch.c
index 73bfbdd..52e0412 100644
--- a/examples/network/fetch.c
+++ b/examples/network/fetch.c
@@ -40,9 +40,8 @@ exit:
pthread_exit(&data->ret);
}
-int update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
+static int update_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
{
- const char *action;
char a_str[GIT_OID_HEXSZ+1], b_str[GIT_OID_HEXSZ+1];
git_oid_fmt(b_str, b);
@@ -68,6 +67,7 @@ int fetch(git_repository *repo, int argc, char **argv)
struct dl_data data;
git_remote_callbacks callbacks;
+ argc = argc;
// Figure out whether it's a named remote or a URL
printf("Fetching %s\n", argv[1]);
if (git_remote_load(&remote, repo, argv[1]) < 0) {
@@ -96,10 +96,14 @@ int fetch(git_repository *repo, int argc, char **argv)
// the download rate.
do {
usleep(10000);
- printf("\rReceived %d/%d objects in %d bytes", stats.processed, stats.total, bytes);
+ printf("\rReceived %d/%d objects in %zu bytes", stats.processed, stats.total, bytes);
} while (!data.finished);
- printf("\rReceived %d/%d objects in %d bytes\n", stats.processed, stats.total, bytes);
+ if (data.ret < 0)
+ goto on_error;
+
+ pthread_join(worker, NULL);
+ printf("\rReceived %d/%d objects in %zu bytes\n", stats.processed, stats.total, bytes);
// Disconnect the underlying connection to prevent from idling.
git_remote_disconnect(remote);
diff --git a/examples/network/git2.c b/examples/network/git2.c
index 7c02305..1bfb13c 100644
--- a/examples/network/git2.c
+++ b/examples/network/git2.c
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include "common.h"
@@ -16,7 +17,7 @@ struct {
{ NULL, NULL}
};
-int run_command(git_cb fn, int argc, char **argv)
+static int run_command(git_cb fn, int argc, char **argv)
{
int error;
git_repository *repo;
@@ -45,7 +46,7 @@ int run_command(git_cb fn, int argc, char **argv)
int main(int argc, char **argv)
{
- int i, error;
+ int i;
if (argc < 2) {
fprintf(stderr, "usage: %s <cmd> [repo]\n", argv[0]);
diff --git a/examples/network/index-pack.c b/examples/network/index-pack.c
index ef5a359..85aac4a 100644
--- a/examples/network/index-pack.c
+++ b/examples/network/index-pack.c
@@ -2,13 +2,20 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
#include "common.h"
// This could be run in the main loop whilst the application waits for
// the indexing to finish in a worker thread
-int index_cb(const git_indexer_stats *stats, void *data)
+static int index_cb(const git_indexer_stats *stats, void *data)
{
+ data = data;
printf("\rProcessing %d of %d", stats->processed, stats->total);
+
+ return 0;
}
int index_pack(git_repository *repo, int argc, char **argv)
@@ -20,6 +27,7 @@ int index_pack(git_repository *repo, int argc, char **argv)
ssize_t read_bytes;
char buf[512];
+ repo = repo;
if (argc < 2) {
fprintf(stderr, "I need a packfile\n");
return EXIT_FAILURE;
@@ -43,7 +51,7 @@ int index_pack(git_repository *repo, int argc, char **argv)
if ((error = git_indexer_stream_add(idx, buf, read_bytes, &stats)) < 0)
goto cleanup;
- printf("\rIndexing %d of %d", stats.processed, stats.total);
+ index_cb(&stats, NULL);
} while (read_bytes > 0);
if (read_bytes < 0) {
@@ -65,38 +73,3 @@ int index_pack(git_repository *repo, int argc, char **argv)
git_indexer_stream_free(idx);
return error;
}
-
-int index_pack_old(git_repository *repo, int argc, char **argv)
-{
- git_indexer *indexer;
- git_indexer_stats stats;
- int error;
- char hash[GIT_OID_HEXSZ + 1] = {0};
-
- if (argc < 2) {
- fprintf(stderr, "I need a packfile\n");
- return EXIT_FAILURE;
- }
-
- // Create a new indexer
- error = git_indexer_new(&indexer, argv[1]);
- if (error < 0)
- return error;
-
- // Index the packfile. This function can take a very long time and
- // should be run in a worker thread.
- error = git_indexer_run(indexer, &stats);
- if (error < 0)
- return error;
-
- // Write the information out to an index file
- error = git_indexer_write(indexer);
-
- // Get the packfile's hash (which should become it's filename)
- git_oid_fmt(hash, git_indexer_hash(indexer));
- puts(hash);
-
- git_indexer_free(indexer);
-
- return 0;
-}
diff --git a/examples/network/ls-remote.c b/examples/network/ls-remote.c
index 39cc647..822d6f6 100644
--- a/examples/network/ls-remote.c
+++ b/examples/network/ls-remote.c
@@ -7,12 +7,14 @@
static int show_ref__cb(git_remote_head *head, void *payload)
{
char oid[GIT_OID_HEXSZ + 1] = {0};
+
+ payload = payload;
git_oid_fmt(oid, &head->oid);
printf("%s\t%s\n", oid, head->name);
return 0;
}
-int use_unnamed(git_repository *repo, const char *url)
+static int use_unnamed(git_repository *repo, const char *url)
{
git_remote *remote = NULL;
int error;
@@ -37,7 +39,7 @@ cleanup:
return error;
}
-int use_remote(git_repository *repo, char *name)
+static int use_remote(git_repository *repo, char *name)
{
git_remote *remote = NULL;
int error;
@@ -63,8 +65,9 @@ cleanup:
int ls_remote(git_repository *repo, int argc, char **argv)
{
- int error, i;
+ int error;
+ argc = argc;
/* If there's a ':' in the name, assume it's an URL */
if (strchr(argv[1], ':') != NULL) {
error = use_unnamed(repo, argv[1]);
diff --git a/src/common.h b/src/common.h
index 1db308f..1d85428 100644
--- a/src/common.h
+++ b/src/common.h
@@ -59,4 +59,7 @@ void giterr_set_regex(const regex_t *regex, int error_code);
#include "util.h"
+typedef struct git_transport git_transport;
+typedef struct gitno_buffer gitno_buffer;
+
#endif /* INCLUDE_common_h__ */
diff --git a/src/fetch.c b/src/fetch.c
index 6032848..f8f853f 100644
--- a/src/fetch.c
+++ b/src/fetch.c
@@ -18,6 +18,7 @@
#include "pack.h"
#include "fetch.h"
#include "netops.h"
+#include "pkt.h"
struct filter_payload {
git_remote *remote;
@@ -70,7 +71,62 @@ static int filter_wants(git_remote *remote)
if (git_repository_odb__weakptr(&p.odb, remote->repo) < 0)
return -1;
- return remote->transport->ls(remote->transport, &filter_ref__cb, &p);
+ return git_remote_ls(remote, filter_ref__cb, &p);
+}
+
+/* Wait until we get an ack from the */
+static int recv_pkt(git_pkt **out, gitno_buffer *buf)
+{
+ const char *ptr = buf->data, *line_end = ptr;
+ git_pkt *pkt;
+ int pkt_type, error = 0, ret;
+
+ do {
+ if (buf->offset > 0)
+ error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset);
+ else
+ error = GIT_EBUFS;
+
+ if (error == 0)
+ break; /* return the pkt */
+
+ if (error < 0 && error != GIT_EBUFS)
+ return -1;
+
+ if ((ret = gitno_recv(buf)) < 0)
+ return -1;
+ } while (error);
+
+ gitno_consume(buf, line_end);
+ pkt_type = pkt->type;
+ if (out != NULL)
+ *out = pkt;
+ else
+ git__free(pkt);
+
+ return pkt_type;
+}
+
+static int store_common(git_transport *t)
+{
+ git_pkt *pkt = NULL;
+ gitno_buffer *buf = &t->buffer;
+
+ do {
+ if (recv_pkt(&pkt, buf) < 0)
+ return -1;
+
+ if (pkt->type == GIT_PKT_ACK) {
+ if (git_vector_insert(&t->common, pkt) < 0)
+ return -1;
+ } else {
+ git__free(pkt);
+ return 0;
+ }
+
+ } while (1);
+
+ return 0;
}
/*
@@ -81,6 +137,12 @@ static int filter_wants(git_remote *remote)
int git_fetch_negotiate(git_remote *remote)
{
git_transport *t = remote->transport;
+ gitno_buffer *buf = &t->buffer;
+ git_buf data = GIT_BUF_INIT;
+ git_revwalk *walk = NULL;
+ int error, pkt_type;
+ unsigned int i;
+ git_oid oid;
if (filter_wants(remote) < 0) {
giterr_set(GITERR_NET, "Failed to filter the reference list for wants");
@@ -92,60 +154,174 @@ int git_fetch_negotiate(git_remote *remote)
return 0;
/*
- * Now we have everything set up so we can start tell the server
- * what we want and what we have.
+ * Now we have everything set up so we can start tell the
+ * server what we want and what we have. Call the function if
+ * the transport has its own logic. This is transitional and
+ * will be removed once this function can support git and http.
*/
- return t->negotiate_fetch(t, remote->repo, &remote->refs);
+ if (t->own_logic)
+ return t->negotiate_fetch(t, remote->repo, &remote->refs);
+
+ /* No own logic, do our thing */
+ if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0)
+ return -1;
+
+ if (git_fetch_setup_walk(&walk, remote->repo) < 0)
+ goto on_error;
+ /*
+ * We don't support any kind of ACK extensions, so the negotiation
+ * boils down to sending what we have and listening for an ACK
+ * every once in a while.
+ */
+ i = 0;
+ while ((error = git_revwalk_next(&oid, walk)) == 0) {
+ git_pkt_buffer_have(&oid, &data);
+ i++;
+ if (i % 20 == 0) {
+ git_pkt_buffer_flush(&data);
+ if (git_buf_oom(&data))
+ goto on_error;
+
+ if (t->negotiation_step(t, data.ptr, data.size) < 0)
+ goto on_error;
+
+ git_buf_clear(&data);
+ if (t->caps.multi_ack) {
+ if (store_common(t) < 0)
+ goto on_error;
+ } else {
+ pkt_type = recv_pkt(NULL, buf);
+
+ if (pkt_type == GIT_PKT_ACK) {
+ break;
+ } else if (pkt_type == GIT_PKT_NAK) {
+ continue;
+ } else {
+ giterr_set(GITERR_NET, "Unexpected pkt type");
+ goto on_error;
+ }
+ }
+ }
+
+ if (t->common.length > 0)
+ break;
+
+ if (i % 20 == 0 && t->rpc) {
+ git_pkt_ack *pkt;
+ unsigned int i;
+
+ if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0)
+ goto on_error;
+
+ git_vector_foreach(&t->common, i, pkt) {
+ git_pkt_buffer_have(&pkt->oid, &data);
+ }
+
+ if (git_buf_oom(&data))
+ goto on_error;
+ }
+ }
+
+ if (error < 0 && error != GIT_REVWALKOVER)
+ goto on_error;
+
+ /* Tell the other end that we're done negotiating */
+ if (t->rpc && t->common.length > 0) {
+ git_pkt_ack *pkt;
+ unsigned int i;
+
+ if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0)
+ goto on_error;
+
+ git_vector_foreach(&t->common, i, pkt) {
+ git_pkt_buffer_have(&pkt->oid, &data);
+ }
+
+ if (git_buf_oom(&data))
+ goto on_error;
+ }
+
+ git_pkt_buffer_done(&data);
+ if (t->negotiation_step(t, data.ptr, data.size) < 0)
+ goto on_error;
+
+ git_buf_free(&data);
+ git_revwalk_free(walk);
+
+ /* Now let's eat up whatever the server gives us */
+ if (!t->caps.multi_ack) {
+ pkt_type = recv_pkt(NULL, buf);
+ if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) {
+ giterr_set(GITERR_NET, "Unexpected pkt type");
+ return -1;
+ }
+ } else {
+ git_pkt_ack *pkt;
+ do {
+ if (recv_pkt((git_pkt **)&pkt, buf) < 0)
+ return -1;
+
+ if (pkt->type == GIT_PKT_NAK ||
+ (pkt->type == GIT_PKT_ACK && pkt->status != GIT_ACK_CONTINUE)) {
+ git__free(pkt);
+ break;
+ }
+
+ git__free(pkt);
+ } while (1);
+ }
+
+ return 0;
+
+on_error:
+ git_revwalk_free(walk);
+ git_buf_free(&data);
+ return -1;
}
int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_stats *stats)
{
+ git_transport *t = remote->transport;
+
if(!remote->need_pack)
return 0;
- return remote->transport->download_pack(remote->transport, remote->repo, bytes, stats);
+ if (t->own_logic)
+ return t->download_pack(t, remote->repo, bytes, stats);
+
+ return git_fetch__download_pack(t, remote->repo, bytes, stats);
+
}
/* Receiving data from a socket and storing it is pretty much the same for git and HTTP */
int git_fetch__download_pack(
- const char *buffered,
- size_t buffered_size,
git_transport *t,
git_repository *repo,
git_off_t *bytes,
git_indexer_stats *stats)
{
int recvd;
- char buff[1024];
- gitno_buffer buf;
git_buf path = GIT_BUF_INIT;
+ gitno_buffer *buf = &t->buffer;
git_indexer_stream *idx = NULL;
- gitno_buffer_setup(t, &buf, buff, sizeof(buff));
-
- if (memcmp(buffered, "PACK", strlen("PACK"))) {
- giterr_set(GITERR_NET, "The pack doesn't start with the signature");
- return -1;
- }
-
if (git_buf_joinpath(&path, git_repository_path(repo), "objects/pack") < 0)
return -1;
if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0)
goto on_error;
+ git_buf_free(&path);
memset(stats, 0, sizeof(git_indexer_stats));
- if (git_indexer_stream_add(idx, buffered, buffered_size, stats) < 0)
- goto on_error;
-
- *bytes = buffered_size;
+ *bytes = 0;
do {
- if (git_indexer_stream_add(idx, buf.data, buf.offset, stats) < 0)
+ if (git_indexer_stream_add(idx, buf->data, buf->offset, stats) < 0)
goto on_error;
- gitno_consume_n(&buf, buf.offset);
- if ((recvd = gitno_recv(&buf)) < 0)
+ gitno_consume_n(buf, buf->offset);
+
+ if ((recvd = gitno_recv(buf)) < 0)
goto on_error;
*bytes += recvd;
diff --git a/src/fetch.h b/src/fetch.h
index a7f1265..87bb43b 100644
--- a/src/fetch.h
+++ b/src/fetch.h
@@ -12,8 +12,7 @@
int git_fetch_negotiate(git_remote *remote);
int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_stats *stats);
-int git_fetch__download_pack(const char *buffered, size_t buffered_size, git_transport *t,
- git_repository *repo, git_off_t *bytes, git_indexer_stats *stats);
+int git_fetch__download_pack(git_transport *t, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats);
int git_fetch_setup_walk(git_revwalk **out, git_repository *repo);
#endif
diff --git a/src/netops.c b/src/netops.c
index b369e51..72c1757 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -61,63 +61,67 @@ static int ssl_set_error(gitno_ssl *ssl, int error)
}
#endif
-void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len)
+int gitno_recv(gitno_buffer *buf)
{
- memset(buf, 0x0, sizeof(gitno_buffer));
- memset(data, 0x0, len);
- buf->data = data;
- buf->len = len;
- buf->offset = 0;
- buf->fd = t->socket;
-#ifdef GIT_SSL
- if (t->encrypt)
- buf->ssl = &t->ssl;
-#endif
+ return buf->recv(buf);
}
#ifdef GIT_SSL
-static int ssl_recv(gitno_ssl *ssl, void *data, size_t len)
+static int gitno__recv_ssl(gitno_buffer *buf)
{
int ret;
do {
- ret = SSL_read(ssl->ssl, data, len);
- } while (SSL_get_error(ssl->ssl, ret) == SSL_ERROR_WANT_READ);
+ ret = SSL_read(buf->ssl->ssl, buf->data + buf->offset, buf->len - buf->offset);
+ } while (SSL_get_error(buf->ssl->ssl, ret) == SSL_ERROR_WANT_READ);
- if (ret < 0)
- return ssl_set_error(ssl, ret);
+ if (ret < 0) {
+ net_set_error("Error receiving socket data");
+ return -1;
+ }
+ buf->offset += ret;
return ret;
}
#endif
-int gitno_recv(gitno_buffer *buf)
+int gitno__recv(gitno_buffer *buf)
{
int ret;
-#ifdef GIT_SSL
- if (buf->ssl != NULL) {
- if ((ret = ssl_recv(buf->ssl, buf->data + buf->offset, buf->len - buf->offset)) < 0)
- return -1;
- } else {
- ret = p_recv(buf->fd, buf->data + buf->offset, buf->len - buf->offset, 0);
- if (ret < 0) {
- net_set_error("Error receiving socket data");
- return -1;
- }
- }
-#else
ret = p_recv(buf->fd, buf->data + buf->offset, buf->len - buf->offset, 0);
if (ret < 0) {
net_set_error("Error receiving socket data");
return -1;
}
-#endif
buf->offset += ret;
return ret;
}
+void gitno_buffer_setup_callback(git_transport *t, gitno_buffer *buf, char *data, unsigned int len, int (*recv)(gitno_buffer *buf), void *cb_data)
+{
+ memset(buf, 0x0, sizeof(gitno_buffer));
+ memset(data, 0x0, len);
+ buf->data = data;
+ buf->len = len;
+ buf->offset = 0;
+ buf->fd = t->socket;
+ buf->recv = recv;
+ buf->cb_data = cb_data;
+}
+
+void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len)
+{
+#ifdef GIT_SSL
+ if (t->use_ssl) {
+ gitno_buffer_setup_callback(t, buf, data, len, gitno__recv_ssl, NULL);
+ buf->ssl = &t->ssl;
+ } else
+#endif
+ gitno_buffer_setup_callback(t, buf, data, len, gitno__recv, NULL);
+}
+
/* Consume up to ptr and move the rest of the buffer to the beginning */
void gitno_consume(gitno_buffer *buf, const char *ptr)
{
@@ -147,7 +151,7 @@ int gitno_ssl_teardown(git_transport *t)
int ret;
#endif
- if (!t->encrypt)
+ if (!t->use_ssl)
return 0;
#ifdef GIT_SSL
@@ -415,7 +419,7 @@ int gitno_connect(git_transport *t, const char *host, const char *port)
t->socket = s;
p_freeaddrinfo(info);
- if (t->encrypt && ssl_setup(t, host) < 0)
+ if (t->use_ssl && ssl_setup(t, host) < 0)
return -1;
return 0;
@@ -445,7 +449,7 @@ int gitno_send(git_transport *t, const char *msg, size_t len, int flags)
size_t off = 0;
#ifdef GIT_SSL
- if (t->encrypt)
+ if (t->use_ssl)
return send_ssl(&t->ssl, msg, len);
#endif
diff --git a/src/netops.h b/src/netops.h
index 4976f87..dded55b 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -8,10 +8,9 @@
#define INCLUDE_netops_h__
#include "posix.h"
-#include "transport.h"
#include "common.h"
-typedef struct gitno_buffer {
+struct gitno_buffer {
char *data;
size_t len;
size_t offset;
@@ -19,10 +18,14 @@ typedef struct gitno_buffer {
#ifdef GIT_SSL
struct gitno_ssl *ssl;
#endif
-} gitno_buffer;
+ int (*recv)(gitno_buffer *buffer);
+ void *cb_data;
+};
void gitno_buffer_setup(git_transport *t, gitno_buffer *buf, char *data, unsigned int len);
+void gitno_buffer_setup_callback(git_transport *t, gitno_buffer *buf, char *data, unsigned int len, int (*recv)(gitno_buffer *buf), void *cb_data);
int gitno_recv(gitno_buffer *buf);
+int gitno__recv(gitno_buffer *buf);
void gitno_consume(gitno_buffer *buf, const char *ptr);
void gitno_consume_n(gitno_buffer *buf, size_t cons);
diff --git a/src/pkt.c b/src/pkt.c
index e003b97..8c916ff 100644
--- a/src/pkt.c
+++ b/src/pkt.c
@@ -42,15 +42,29 @@ static int flush_pkt(git_pkt **out)
/* the rest of the line will be useful for multi_ack */
static int ack_pkt(git_pkt **out, const char *line, size_t len)
{
- git_pkt *pkt;
+ git_pkt_ack *pkt;
GIT_UNUSED(line);
GIT_UNUSED(len);
- pkt = git__malloc(sizeof(git_pkt));
+ pkt = git__calloc(1, sizeof(git_pkt_ack));
GITERR_CHECK_ALLOC(pkt);
pkt->type = GIT_PKT_ACK;
- *out = pkt;
+ line += 3;
+ len -= 3;
+
+ if (len >= GIT_OID_HEXSZ) {
+ git_oid_fromstr(&pkt->oid, line + 1);
+ line += GIT_OID_HEXSZ + 1;
+ len -= GIT_OID_HEXSZ + 1;
+ }
+
+ if (len >= 7) {
+ if (!git__prefixcmp(line + 1, "continue"))
+ pkt->status = GIT_ACK_CONTINUE;
+ }
+
+ *out = (git_pkt *) pkt;
return 0;
}
@@ -283,20 +297,28 @@ int git_pkt_buffer_flush(git_buf *buf)
static int buffer_want_with_caps(git_remote_head *head, git_transport_caps *caps, git_buf *buf)
{
- char capstr[20];
+ git_buf str = GIT_BUF_INIT;
char oid[GIT_OID_HEXSZ +1] = {0};
unsigned int len;
if (caps->ofs_delta)
- strncpy(capstr, GIT_CAP_OFS_DELTA, sizeof(capstr));
+ git_buf_puts(&str, GIT_CAP_OFS_DELTA " ");
+
+ if (caps->multi_ack)
+ git_buf_puts(&str, GIT_CAP_MULTI_ACK " ");
+
+ if (git_buf_oom(&str))
+ return -1;
len = (unsigned int)
(strlen("XXXXwant ") + GIT_OID_HEXSZ + 1 /* NUL */ +
- strlen(capstr) + 1 /* LF */);
+ git_buf_len(&str) + 1 /* LF */);
git_buf_grow(buf, git_buf_len(buf) + len);
-
git_oid_fmt(oid, &head->oid);
- return git_buf_printf(buf, "%04xwant %s %s\n", len, oid, capstr);
+ git_buf_printf(buf, "%04xwant %s %s\n", len, oid, git_buf_cstr(&str));
+ git_buf_free(&str);
+
+ return git_buf_oom(buf);
}
/*
diff --git a/src/protocol.c b/src/protocol.c
index 6b38617..20d6e23 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -9,38 +9,36 @@
#include "pkt.h"
#include "buffer.h"
-int git_protocol_store_refs(git_protocol *p, const char *data, size_t len)
+int git_protocol_store_refs(git_transport *t, int flushes)
{
- git_buf *buf = &p->buf;
- git_vector *refs = p->refs;
- int error;
- const char *line_end, *ptr;
-
- if (len == 0) { /* EOF */
- if (git_buf_len(buf) != 0) {
- giterr_set(GITERR_NET, "Unexpected EOF");
- return p->error = -1;
- } else {
- return 0;
- }
- }
+ gitno_buffer *buf = &t->buffer;
+ git_vector *refs = &t->refs;
+ int error, flush = 0, recvd;
+ const char *line_end;
+ git_pkt *pkt;
- git_buf_put(buf, data, len);
- ptr = buf->ptr;
- while (1) {
- git_pkt *pkt;
+ do {
+ if (buf->offset > 0)
+ error = git_pkt_parse_line(&pkt, buf->data, &line_end, buf->offset);
+ else
+ error = GIT_EBUFS;
- if (git_buf_len(buf) == 0)
- return 0;
+ if (error < 0 && error != GIT_EBUFS)
+ return -1;
- error = git_pkt_parse_line(&pkt, ptr, &line_end, git_buf_len(buf));
- if (error == GIT_EBUFS)
- return 0; /* Ask for more */
- if (error < 0)
- return p->error = -1;
+ if (error == GIT_EBUFS) {
+ if ((recvd = gitno_recv(buf)) < 0)
+ return -1;
- git_buf_consume(buf, line_end);
+ if (recvd == 0 && !flush) {
+ giterr_set(GITERR_NET, "Early EOF");
+ return -1;
+ }
+
+ continue;
+ }
+ gitno_consume(buf, line_end);
if (pkt->type == GIT_PKT_ERR) {
giterr_set(GITERR_NET, "Remote error: %s", ((git_pkt_err *)pkt)->error);
git__free(pkt);
@@ -48,10 +46,42 @@ int git_protocol_store_refs(git_protocol *p, const char *data, size_t len)
}
if (git_vector_insert(refs, pkt) < 0)
- return p->error = -1;
+ return -1;
if (pkt->type == GIT_PKT_FLUSH)
- p->flush = 1;
+ flush++;
+ } while (flush < flushes);
+
+ return flush;
+}
+
+int git_protocol_detect_caps(git_pkt_ref *pkt, git_transport_caps *caps)
+{
+ const char *ptr;
+
+ /* No refs or capabilites, odd but not a problem */
+ if (pkt == NULL || pkt->capabilities == NULL)
+ return 0;
+
+ ptr = pkt->capabilities;
+ while (ptr != NULL && *ptr != '\0') {
+ if (*ptr == ' ')
+ ptr++;
+
+ if(!git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) {
+ caps->common = caps->ofs_delta = 1;
+ ptr += strlen(GIT_CAP_OFS_DELTA);
+ continue;
+ }
+
+ if(!git__prefixcmp(ptr, GIT_CAP_MULTI_ACK)) {
+ caps->common = caps->multi_ack = 1;
+ ptr += strlen(GIT_CAP_MULTI_ACK);
+ continue;
+ }
+
+ /* We don't know this capability, so skip it */
+ ptr = strchr(ptr, ' ');
}
return 0;
diff --git a/src/protocol.h b/src/protocol.h
index a6c3e07..615be8d 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -9,15 +9,9 @@
#include "transport.h"
#include "buffer.h"
+#include "pkt.h"
-typedef struct {
- git_transport *transport;
- git_vector *refs;
- git_buf buf;
- int error;
- unsigned int flush :1;
-} git_protocol;
-
-int git_protocol_store_refs(git_protocol *p, const char *data, size_t len);
+int git_protocol_store_refs(git_transport *t, int flushes);
+int git_protocol_detect_caps(git_pkt_ref *pkt, git_transport_caps *caps);
#endif
diff --git a/src/remote.c b/src/remote.c
index c2bfb09..948da12 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -14,6 +14,7 @@
#include "remote.h"
#include "fetch.h"
#include "refs.h"
+#include "pkt.h"
#include <regex.h>
@@ -401,6 +402,10 @@ on_error:
int git_remote_ls(git_remote *remote, git_headlist_cb list_cb, void *payload)
{
+ git_vector *refs = &remote->transport->refs;
+ unsigned int i;
+ git_pkt *p = NULL;
+
assert(remote);
if (!remote->transport || !remote->transport->connected) {
@@ -408,7 +413,21 @@ int git_remote_ls(git_remote *remote, git_headlist_cb list_cb, void *payload)
return -1;
}
- return remote->transport->ls(remote->transport, list_cb, payload);
+ git_vector_foreach(refs, i, p) {
+ git_pkt_ref *pkt = NULL;
+
+ if (p->type != GIT_PKT_REF)
+ continue;
+
+ pkt = (git_pkt_ref *)p;
+
+ if (list_cb(&pkt->head, payload) < 0) {
+ giterr_set(GITERR_NET, "User callback returned error");
+ return -1;
+ }
+ }
+
+ return 0;
}
int git_remote_download(git_remote *remote, git_off_t *bytes, git_indexer_stats *stats)
diff --git a/src/transport.h b/src/transport.h
index 68b92f7..c430616 100644
--- a/src/transport.h
+++ b/src/transport.h
@@ -12,6 +12,7 @@
#include "vector.h"
#include "posix.h"
#include "common.h"
+#include "netops.h"
#ifdef GIT_SSL
# include <openssl/ssl.h>
# include <openssl/err.h>
@@ -19,10 +20,12 @@
#define GIT_CAP_OFS_DELTA "ofs-delta"
+#define GIT_CAP_MULTI_ACK "multi_ack"
typedef struct git_transport_caps {
int common:1,
- ofs_delta:1;
+ ofs_delta:1,
+ multi_ack: 1;
} git_transport_caps;
#ifdef GIT_SSL
@@ -70,19 +73,25 @@ struct git_transport {
int direction : 1, /* 0 fetch, 1 push */
connected : 1,
check_cert: 1,
- encrypt : 1;
+ use_ssl : 1,
+ own_logic: 1, /* transitional */
+ rpc: 1; /* git-speak for the HTTP transport */
#ifdef GIT_SSL
struct gitno_ssl ssl;
#endif
+ git_vector refs;
+ git_vector common;
+ gitno_buffer buffer;
GIT_SOCKET socket;
+ git_transport_caps caps;
/**
* Connect and store the remote heads
*/
int (*connect)(struct git_transport *transport, int dir);
/**
- * Give a list of references, useful for ls-remote
+ * Send our side of a negotiation
*/
- int (*ls)(struct git_transport *transport, git_headlist_cb list_cb, void *opaque);
+ int (*negotiation_step)(struct git_transport *transport, void *data, size_t len);
/**
* Push the changes over
*/
@@ -97,10 +106,6 @@ struct git_transport {
*/
int (*download_pack)(struct git_transport *transport, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats);
/**
- * Fetch the changes
- */
- int (*fetch)(struct git_transport *transport);
- /**
* Close the connection
*/
int (*close)(struct git_transport *transport);
@@ -124,7 +129,6 @@ int git_transport_dummy(struct git_transport **transport);
*/
int git_transport_valid_url(const char *url);
-typedef struct git_transport git_transport;
typedef int (*git_transport_cb)(git_transport **transport);
#endif
diff --git a/src/transports/git.c b/src/transports/git.c
index 45f571f..7a65718 100644
--- a/src/transports/git.c
+++ b/src/transports/git.c
@@ -24,12 +24,7 @@
typedef struct {
git_transport parent;
- git_protocol proto;
- git_vector refs;
- git_remote_head **heads;
- git_transport_caps caps;
char buff[1024];
- gitno_buffer buf;
#ifdef GIT_WIN32
WSADATA wsd;
#endif
@@ -127,68 +122,6 @@ on_error:
}
/*
- * Read from the socket and store the references in the vector
- */
-static int store_refs(transport_git *t)
-{
- gitno_buffer *buf = &t->buf;
- int ret = 0;
-
- while (1) {
- if ((ret = gitno_recv(buf)) < 0)
- return -1;
- if (ret == 0) /* Orderly shutdown, so exit */
- return 0;
-
- ret = git_protocol_store_refs(&t->proto, buf->data, buf->offset);
- if (ret == GIT_EBUFS) {
- gitno_consume_n(buf, buf->len);
- continue;
- }
-
- if (ret < 0)
- return ret;
-
- gitno_consume_n(buf, buf->offset);
-
- if (t->proto.flush) { /* No more refs */
- t->proto.flush = 0;
- return 0;
- }
- }
-}
-
-static int detect_caps(transport_git *t)
-{
- git_vector *refs = &t->refs;
- git_pkt_ref *pkt;
- git_transport_caps *caps = &t->caps;
- const char *ptr;
-
- pkt = git_vector_get(refs, 0);
- /* No refs or capabilites, odd but not a problem */
- if (pkt == NULL || pkt->capabilities == NULL)
- return 0;
-
- ptr = pkt->capabilities;
- while (ptr != NULL && *ptr != '\0') {
- if (*ptr == ' ')
- ptr++;
-
- if(!git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) {
- caps->common = caps->ofs_delta = 1;
- ptr += strlen(GIT_CAP_OFS_DELTA);
- continue;
- }
-
- /* We don't know this capability, so skip it */
- ptr = strchr(ptr, ' ');
- }
-
- return 0;
-}
-
-/*
* Since this is a network connection, we need to parse and store the
* pkt-lines at this stage and keep them there.
*/
@@ -202,202 +135,26 @@ static int git_connect(git_transport *transport, int direction)
}
t->parent.direction = direction;
- if (git_vector_init(&t->refs, 16, NULL) < 0)
- return -1;
/* Connect and ask for the refs */
if (do_connect(t, transport->url) < 0)
- goto cleanup;
+ return -1;
- gitno_buffer_setup(transport, &t->buf, t->buff, sizeof(t->buff));
+ gitno_buffer_setup(transport, &transport->buffer, t->buff, sizeof(t->buff));
t->parent.connected = 1;
- if (store_refs(t) < 0)
- goto cleanup;
-
- if (detect_caps(t) < 0)
- goto cleanup;
-
- return 0;
-cleanup:
- git_vector_free(&t->refs);
- return -1;
-}
-
-static int git_ls(git_transport *transport, git_headlist_cb list_cb, void *opaque)
-{
- transport_git *t = (transport_git *) transport;
- git_vector *refs = &t->refs;
- unsigned int i;
- git_pkt *p = NULL;
-
- git_vector_foreach(refs, i, p) {
- git_pkt_ref *pkt = NULL;
-
- if (p->type != GIT_PKT_REF)
- continue;
-
- pkt = (git_pkt_ref *)p;
-
- if (list_cb(&pkt->head, opaque) < 0) {
- giterr_set(GITERR_NET, "User callback returned error");
- return -1;
- }
- }
-
- return 0;
-}
-
-/* Wait until we get an ack from the */
-static int recv_pkt(gitno_buffer *buf)
-{
- const char *ptr = buf->data, *line_end;
- git_pkt *pkt;
- int pkt_type, error;
-
- do {
- /* Wait for max. 1 second */
- if ((error = gitno_select_in(buf, 1, 0)) < 0) {
- return -1;
- } else if (error == 0) {
- /*
- * Some servers don't respond immediately, so if this
- * happens, we keep sending information until it
- * answers. Pretend we received a NAK to convince higher
- * layers to do so.
- */
- return GIT_PKT_NAK;
- }
-
- if ((error = gitno_recv(buf)) < 0)
- return -1;
-
- error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset);
- if (error == GIT_EBUFS)
- continue;
- if (error < 0)
- return -1;
- } while (error);
-
- gitno_consume(buf, line_end);
- pkt_type = pkt->type;
- git__free(pkt);
-
- return pkt_type;
-}
-
-static int git_negotiate_fetch(git_transport *transport, git_repository *repo, const git_vector *wants)
-{
- transport_git *t = (transport_git *) transport;
- git_revwalk *walk;
- git_oid oid;
- int error;
- unsigned int i;
- git_buf data = GIT_BUF_INIT;
- gitno_buffer *buf = &t->buf;
-
- if (git_pkt_buffer_wants(wants, &t->caps, &data) < 0)
+ if (git_protocol_store_refs(transport, 1) < 0)
return -1;
- if (git_fetch_setup_walk(&walk, repo) < 0)
- goto on_error;
-
- if (gitno_send(transport, data.ptr, data.size, 0) < 0)
- goto on_error;
-
- git_buf_clear(&data);
- /*
- * We don't support any kind of ACK extensions, so the negotiation
- * boils down to sending what we have and listening for an ACK
- * every once in a while.
- */
- i = 0;
- while ((error = git_revwalk_next(&oid, walk)) == 0) {
- git_pkt_buffer_have(&oid, &data);
- i++;
- if (i % 20 == 0) {
- int pkt_type;
-
- git_pkt_buffer_flush(&data);
- if (git_buf_oom(&data))
- goto on_error;
-
- if (gitno_send(transport, data.ptr, data.size, 0) < 0)
- goto on_error;
-
- pkt_type = recv_pkt(buf);
-
- if (pkt_type == GIT_PKT_ACK) {
- break;
- } else if (pkt_type == GIT_PKT_NAK) {
- continue;
- } else {
- giterr_set(GITERR_NET, "Unexpected pkt type");
- goto on_error;
- }
-
- }
- }
- if (error < 0 && error != GIT_REVWALKOVER)
- goto on_error;
-
- /* Tell the other end that we're done negotiating */
- git_buf_clear(&data);
- git_pkt_buffer_flush(&data);
- git_pkt_buffer_done(&data);
- if (gitno_send(transport, data.ptr, data.size, 0) < 0)
- goto on_error;
+ if (git_protocol_detect_caps(git_vector_get(&transport->refs, 0), &transport->caps) < 0)
+ return -1;
- git_buf_free(&data);
- git_revwalk_free(walk);
return 0;
-
-on_error:
- git_buf_free(&data);
- git_revwalk_free(walk);
- return -1;
}
-static int git_download_pack(git_transport *transport, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats)
+static int git_negotiation_step(struct git_transport *transport, void *data, size_t len)
{
- transport_git *t = (transport_git *) transport;
- int error = 0, read_bytes;
- gitno_buffer *buf = &t->buf;
- git_pkt *pkt;
- const char *line_end, *ptr;
-
- /*
- * For now, we ignore everything and wait for the pack
- */
- do {
- ptr = buf->data;
- /* Whilst we're searching for the pack */
- while (1) {
- if (buf->offset == 0) {
- break;
- }
-
- error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset);
- if (error == GIT_EBUFS)
- break;
-
- if (error < 0)
- return error;
-
- if (pkt->type == GIT_PKT_PACK) {
- git__free(pkt);
- return git_fetch__download_pack(buf->data, buf->offset, transport, repo, bytes, stats);
- }
-
- /* For now we don't care about anything */
- git__free(pkt);
- gitno_consume(buf, line_end);
- }
-
- read_bytes = gitno_recv(buf);
- } while (read_bytes);
-
- return read_bytes;
+ return gitno_send(transport, data, len, 0);
}
static int git_close(git_transport *t)
@@ -408,6 +165,7 @@ static int git_close(git_transport *t)
return -1;
/* Can't do anything if there's an error, so don't bother checking */
gitno_send(t, buf.ptr, buf.size, 0);
+ git_buf_free(&buf);
if (gitno_close(t->socket) < 0) {
giterr_set(GITERR_NET, "Failed to close socket");
@@ -426,17 +184,22 @@ static int git_close(git_transport *t)
static void git_free(git_transport *transport)
{
transport_git *t = (transport_git *) transport;
- git_vector *refs = &t->refs;
+ git_vector *refs = &transport->refs;
unsigned int i;
for (i = 0; i < refs->length; ++i) {
git_pkt *p = git_vector_get(refs, i);
git_pkt_free(p);
}
+ git_vector_free(refs);
+ refs = &transport->common;
+ for (i = 0; i < refs->length; ++i) {
+ git_pkt *p = git_vector_get(refs, i);
+ git_pkt_free(p);
+ }
git_vector_free(refs);
- git__free(t->heads);
- git_buf_free(&t->proto.buf);
+
git__free(t->parent.url);
git__free(t);
}
@@ -452,15 +215,16 @@ int git_transport_git(git_transport **out)
GITERR_CHECK_ALLOC(t);
memset(t, 0x0, sizeof(transport_git));
+ if (git_vector_init(&t->parent.common, 8, NULL))
+ goto on_error;
+
+ if (git_vector_init(&t->parent.refs, 16, NULL) < 0)
+ goto on_error;
t->parent.connect = git_connect;
- t->parent.ls = git_ls;
- t->parent.negotiate_fetch = git_negotiate_fetch;
- t->parent.download_pack = git_download_pack;
+ t->parent.negotiation_step = git_negotiation_step;
t->parent.close = git_close;
t->parent.free = git_free;
- t->proto.refs = &t->refs;
- t->proto.transport = (git_transport *) t;
*out = (git_transport *) t;
@@ -474,4 +238,8 @@ int git_transport_git(git_transport **out)
#endif
return 0;
+
+on_error:
+ git__free(t);
+ return -1;
}
diff --git a/src/transports/http.c b/src/transports/http.c
index f25d639..85fec41 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -29,11 +29,8 @@ enum last_cb {
typedef struct {
git_transport parent;
- git_protocol proto;
- git_vector refs;
- git_vector common;
+ http_parser_settings settings;
git_buf buf;
- git_remote_head **heads;
int error;
int transfer_finished :1,
ct_found :1,
@@ -46,7 +43,7 @@ typedef struct {
char *host;
char *port;
char *service;
- git_transport_caps caps;
+ char buffer[4096];
#ifdef GIT_WIN32
WSADATA wsd;
#endif
@@ -183,17 +180,6 @@ static int on_headers_complete(http_parser *parser)
return 0;
}
-static int on_body_store_refs(http_parser *parser, const char *str, size_t len)
-{
- transport_http *t = (transport_http *) parser->data;
-
- if (parser->status_code == 404) {
- return git_buf_put(&t->buf, str, len);
- }
-
- return git_protocol_store_refs(&t->proto, str, len);
-}
-
static int on_message_complete(http_parser *parser)
{
transport_http *t = (transport_http *) parser->data;
@@ -208,51 +194,64 @@ static int on_message_complete(http_parser *parser)
return 0;
}
-static int store_refs(transport_http *t)
+static int on_body_fill_buffer(http_parser *parser, const char *str, size_t len)
{
- http_parser_settings settings;
- char buffer[1024];
- gitno_buffer buf;
- git_pkt *pkt;
- int ret;
+ git_transport *transport = (git_transport *) parser->data;
+ transport_http *t = (transport_http *) parser->data;
+ gitno_buffer *buf = &transport->buffer;
- http_parser_init(&t->parser, HTTP_RESPONSE);
- t->parser.data = t;
- memset(&settings, 0x0, sizeof(http_parser_settings));
- settings.on_header_field = on_header_field;
- settings.on_header_value = on_header_value;
- settings.on_headers_complete = on_headers_complete;
- settings.on_body = on_body_store_refs;
- settings.on_message_complete = on_message_complete;
+ if (buf->len - buf->offset < len) {
+ giterr_set(GITERR_NET, "Can't fit data in the buffer");
+ return t->error = -1;
+ }
- gitno_buffer_setup((git_transport *)t, &buf, buffer, sizeof(buffer));
+ memcpy(buf->data + buf->offset, str, len);
+ buf->offset += len;
- while(1) {
- size_t parsed;
+ return 0;
+}
- if ((ret = gitno_recv(&buf)) < 0)
- return -1;
+static int http_recv_cb(gitno_buffer *buf)
+{
+ git_transport *transport = (git_transport *) buf->cb_data;
+ transport_http *t = (transport_http *) transport;
+ size_t old_len;
+ gitno_buffer inner;
+ char buffer[2048];
+ int error;
- parsed = http_parser_execute(&t->parser, &settings, buf.data, buf.offset);
- /* Both should happen at the same time */
- if (parsed != buf.offset || t->error < 0)
- return t->error;
+ if (t->transfer_finished)
+ return 0;
- gitno_consume_n(&buf, parsed);
+ gitno_buffer_setup(transport, &inner, buffer, sizeof(buffer));
- if (ret == 0 || t->transfer_finished)
- return 0;
- }
+ if ((error = gitno_recv(&inner)) < 0)
+ return -1;
- pkt = git_vector_get(&t->refs, 0);
- if (pkt == NULL || pkt->type != GIT_PKT_COMMENT) {
- giterr_set(GITERR_NET, "Invalid HTTP response");
- return t->error = -1;
- } else {
- git_vector_remove(&t->refs, 0);
- }
+ old_len = buf->offset;
+ http_parser_execute(&t->parser, &t->settings, inner.data, inner.offset);
+ if (t->error < 0)
+ return t->error;
- return 0;
+ return buf->offset - old_len;
+}
+
+/* Set up the gitno_buffer so calling gitno_recv() grabs data from the HTTP response */
+static void setup_gitno_buffer(git_transport *transport)
+{
+ transport_http *t = (transport_http *) transport;
+
+ http_parser_init(&t->parser, HTTP_RESPONSE);
+ t->parser.data = t;
+ t->transfer_finished = 0;
+ memset(&t->settings, 0x0, sizeof(http_parser_settings));
+ t->settings.on_header_field = on_header_field;
+ t->settings.on_header_value = on_header_value;
+ t->settings.on_headers_complete = on_headers_complete;
+ t->settings.on_body = on_body_fill_buffer;
+ t->settings.on_message_complete = on_message_complete;
+
+ gitno_buffer_setup_callback(transport, &transport->buffer, t->buffer, sizeof(t->buffer), http_recv_cb, t);
}
static int http_connect(git_transport *transport, int direction)
@@ -263,6 +262,7 @@ static int http_connect(git_transport *transport, int direction)
const char *service = "upload-pack";
const char *url = t->parent.url, *prefix_http = "http://", *prefix_https = "https://";
const char *default_port;
+ git_pkt *pkt;
if (direction == GIT_DIR_PUSH) {
giterr_set(GITERR_NET, "Pushing over HTTP is not implemented");
@@ -270,8 +270,6 @@ static int http_connect(git_transport *transport, int direction)
}
t->parent.direction = direction;
- if (git_vector_init(&t->refs, 16, NULL) < 0)
- return -1;
if (!git__prefixcmp(url, prefix_http)) {
url = t->parent.url + strlen(prefix_http);
@@ -304,307 +302,61 @@ static int http_connect(git_transport *transport, int direction)
if (gitno_send(transport, request.ptr, request.size, 0) < 0)
goto cleanup;
- ret = store_refs(t);
-
-cleanup:
- git_buf_free(&request);
- git_buf_clear(&t->buf);
-
- return ret;
-}
-
-static int http_ls(git_transport *transport, git_headlist_cb list_cb, void *opaque)
-{
- transport_http *t = (transport_http *) transport;
- git_vector *refs = &t->refs;
- unsigned int i;
- git_pkt_ref *p;
-
- git_vector_foreach(refs, i, p) {
- if (p->type != GIT_PKT_REF)
- continue;
-
- if (list_cb(&p->head, opaque) < 0) {
- giterr_set(GITERR_NET, "The user callback returned error");
- return -1;
- }
- }
-
- return 0;
-}
-
-static int on_body_parse_response(http_parser *parser, const char *str, size_t len)
-{
- transport_http *t = (transport_http *) parser->data;
- git_buf *buf = &t->buf;
- git_vector *common = &t->common;
- int error;
- const char *line_end, *ptr;
-
- if (len == 0) { /* EOF */
- if (git_buf_len(buf) != 0) {
- giterr_set(GITERR_NET, "Unexpected EOF");
- return t->error = -1;
- } else {
- return 0;
- }
- }
-
- git_buf_put(buf, str, len);
- ptr = buf->ptr;
- while (1) {
- git_pkt *pkt;
-
- if (git_buf_len(buf) == 0)
- return 0;
-
- error = git_pkt_parse_line(&pkt, ptr, &line_end, git_buf_len(buf));
- if (error == GIT_EBUFS) {
- return 0; /* Ask for more */
- }
- if (error < 0)
- return t->error = -1;
-
- git_buf_consume(buf, line_end);
-
- if (pkt->type == GIT_PKT_PACK) {
- git__free(pkt);
- t->pack_ready = 1;
- return 0;
- }
-
- if (pkt->type == GIT_PKT_NAK) {
- git__free(pkt);
- return 0;
- }
-
- if (pkt->type != GIT_PKT_ACK) {
- git__free(pkt);
- continue;
- }
-
- if (git_vector_insert(common, pkt) < 0)
- return -1;
- }
-
- return error;
-
-}
-
-static int parse_response(transport_http *t)
-{
- int ret = 0;
- http_parser_settings settings;
- char buffer[1024];
- gitno_buffer buf;
-
- http_parser_init(&t->parser, HTTP_RESPONSE);
- t->parser.data = t;
- t->transfer_finished = 0;
- memset(&settings, 0x0, sizeof(http_parser_settings));
- settings.on_header_field = on_header_field;
- settings.on_header_value = on_header_value;
- settings.on_headers_complete = on_headers_complete;
- settings.on_body = on_body_parse_response;
- settings.on_message_complete = on_message_complete;
-
- gitno_buffer_setup((git_transport *)t, &buf, buffer, sizeof(buffer));
-
- while(1) {
- size_t parsed;
-
- if ((ret = gitno_recv(&buf)) < 0)
- return -1;
-
- parsed = http_parser_execute(&t->parser, &settings, buf.data, buf.offset);
- /* Both should happen at the same time */
- if (parsed != buf.offset || t->error < 0)
- return t->error;
-
- gitno_consume_n(&buf, parsed);
+ setup_gitno_buffer(transport);
+ if ((ret = git_protocol_store_refs(transport, 2)) < 0)
+ goto cleanup;
- if (ret == 0 || t->transfer_finished || t->pack_ready) {
- return 0;
- }
+ pkt = git_vector_get(&transport->refs, 0);
+ if (pkt == NULL || pkt->type != GIT_PKT_COMMENT) {
+ giterr_set(GITERR_NET, "Invalid HTTP response");
+ return t->error = -1;
+ } else {
+ /* Remove the comment and flush pkts */
+ git_vector_remove(&transport->refs, 0);
+ git__free(pkt);
+ pkt = git_vector_get(&transport->refs, 0);
+ git_vector_remove(&transport->refs, 0);
+ git__free(pkt);
}
- return ret;
-}
-
-static int http_negotiate_fetch(git_transport *transport, git_repository *repo, const git_vector *wants)
-{
- transport_http *t = (transport_http *) transport;
- int ret;
- unsigned int i;
- char buff[128];
- gitno_buffer buf;
- git_revwalk *walk = NULL;
- git_oid oid;
- git_pkt_ack *pkt;
- git_vector *common = &t->common;
- git_buf request = GIT_BUF_INIT, data = GIT_BUF_INIT;
-
- gitno_buffer_setup(transport, &buf, buff, sizeof(buff));
-
- if (git_vector_init(common, 16, NULL) < 0)
- return -1;
-
- if (git_fetch_setup_walk(&walk, repo) < 0)
- return -1;
-
- do {
- if ((ret = do_connect(t, t->host, t->port)) < 0)
- goto cleanup;
-
- if ((ret = git_pkt_buffer_wants(wants, &t->caps, &data)) < 0)
- goto cleanup;
-
- /* We need to send these on each connection */
- git_vector_foreach (common, i, pkt) {
- if ((ret = git_pkt_buffer_have(&pkt->oid, &data)) < 0)
- goto cleanup;
- }
-
- i = 0;
- while ((i < 20) && ((ret = git_revwalk_next(&oid, walk)) == 0)) {
- if ((ret = git_pkt_buffer_have(&oid, &data)) < 0)
- goto cleanup;
-
- i++;
- }
-
- git_pkt_buffer_done(&data);
-
- if ((ret = gen_request(&request, t->path, t->host, "POST", "upload-pack", data.size, 0)) < 0)
- goto cleanup;
-
- if ((ret = gitno_send(transport, request.ptr, request.size, 0)) < 0)
- goto cleanup;
-
- if ((ret = gitno_send(transport, data.ptr, data.size, 0)) < 0)
- goto cleanup;
-
- git_buf_clear(&request);
- git_buf_clear(&data);
-
- if (ret < 0 || i >= 256)
- break;
-
- if ((ret = parse_response(t)) < 0)
- goto cleanup;
-
- if (t->pack_ready) {
- ret = 0;
- goto cleanup;
- }
-
- } while(1);
+ if (git_protocol_detect_caps(git_vector_get(&transport->refs, 0), &transport->caps) < 0)
+ return t->error = -1;
cleanup:
git_buf_free(&request);
- git_buf_free(&data);
- git_revwalk_free(walk);
- return ret;
-}
-
-typedef struct {
- git_indexer_stream *idx;
- git_indexer_stats *stats;
- transport_http *transport;
-} download_pack_cbdata;
-
-static int on_message_complete_download_pack(http_parser *parser)
-{
- download_pack_cbdata *data = (download_pack_cbdata *) parser->data;
-
- data->transport->transfer_finished = 1;
-
- return 0;
-}
-static int on_body_download_pack(http_parser *parser, const char *str, size_t len)
-{
- download_pack_cbdata *data = (download_pack_cbdata *) parser->data;
- transport_http *t = data->transport;
- git_indexer_stream *idx = data->idx;
- git_indexer_stats *stats = data->stats;
+ git_buf_clear(&t->buf);
- return t->error = git_indexer_stream_add(idx, str, len, stats);
+ return ret;
}
-/*
- * As the server is probably using Transfer-Encoding: chunked, we have
- * to use the HTTP parser to download the pack instead of giving it to
- * the simple downloader. Furthermore, we're using keep-alive
- * connections, so the simple downloader would just hang.
- */
-static int http_download_pack(git_transport *transport, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats)
+static int http_negotiation_step(struct git_transport *transport, void *data, size_t len)
{
transport_http *t = (transport_http *) transport;
- git_buf *oldbuf = &t->buf;
- int recvd;
- http_parser_settings settings;
- char buffer[1024];
- gitno_buffer buf;
- git_buf path = GIT_BUF_INIT;
- git_indexer_stream *idx = NULL;
- download_pack_cbdata data;
-
- gitno_buffer_setup(transport, &buf, buffer, sizeof(buffer));
-
- if (memcmp(oldbuf->ptr, "PACK", strlen("PACK"))) {
- giterr_set(GITERR_NET, "The pack doesn't start with a pack signature");
- return -1;
- }
-
- if (git_buf_joinpath(&path, git_repository_path(repo), "objects/pack") < 0)
- return -1;
+ git_buf request = GIT_BUF_INIT;
+ int ret;
- if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0)
+ /* First, send the data as a HTTP POST request */
+ if ((ret = do_connect(t, t->host, t->port)) < 0)
return -1;
- /*
- * This is part of the previous response, so we don't want to
- * re-init the parser, just set these two callbacks.
- */
- memset(stats, 0, sizeof(git_indexer_stats));
- data.stats = stats;
- data.idx = idx;
- data.transport = t;
- t->parser.data = &data;
- t->transfer_finished = 0;
- memset(&settings, 0x0, sizeof(settings));
- settings.on_message_complete = on_message_complete_download_pack;
- settings.on_body = on_body_download_pack;
- *bytes = git_buf_len(oldbuf);
-
- if (git_indexer_stream_add(idx, git_buf_cstr(oldbuf), git_buf_len(oldbuf), stats) < 0)
+ if ((ret = gen_request(&request, t->path, t->host, "POST", "upload-pack", len, 0)) < 0)
goto on_error;
- gitno_buffer_setup(transport, &buf, buffer, sizeof(buffer));
-
- do {
- size_t parsed;
-
- if ((recvd = gitno_recv(&buf)) < 0)
- goto on_error;
+ if ((ret = gitno_send(transport, request.ptr, request.size, 0)) < 0)
+ goto on_error;
- parsed = http_parser_execute(&t->parser, &settings, buf.data, buf.offset);
- if (parsed != buf.offset || t->error < 0)
- goto on_error;
+ if ((ret = gitno_send(transport, data, len, 0)) < 0)
+ goto on_error;
- *bytes += recvd;
- gitno_consume_n(&buf, parsed);
- } while (recvd > 0 && !t->transfer_finished);
+ git_buf_free(&request);
- if (git_indexer_stream_finalize(idx, stats) < 0)
- goto on_error;
+ /* Then we need to set up the buffer to grab data from the HTTP response */
+ setup_gitno_buffer(transport);
- git_indexer_stream_free(idx);
return 0;
on_error:
- git_indexer_stream_free(idx);
- git_buf_free(&path);
+ git_buf_free(&request);
return -1;
}
@@ -627,8 +379,8 @@ static int http_close(git_transport *transport)
static void http_free(git_transport *transport)
{
transport_http *t = (transport_http *) transport;
- git_vector *refs = &t->refs;
- git_vector *common = &t->common;
+ git_vector *refs = &transport->refs;
+ git_vector *common = &transport->common;
unsigned int i;
git_pkt *p;
@@ -649,8 +401,6 @@ static void http_free(git_transport *transport)
}
git_vector_free(common);
git_buf_free(&t->buf);
- git_buf_free(&t->proto.buf);
- git__free(t->heads);
git__free(t->content_type);
git__free(t->host);
git__free(t->port);
@@ -669,13 +419,15 @@ int git_transport_http(git_transport **out)
memset(t, 0x0, sizeof(transport_http));
t->parent.connect = http_connect;
- t->parent.ls = http_ls;
- t->parent.negotiate_fetch = http_negotiate_fetch;
- t->parent.download_pack = http_download_pack;
+ t->parent.negotiation_step = http_negotiation_step;
t->parent.close = http_close;
t->parent.free = http_free;
- t->proto.refs = &t->refs;
- t->proto.transport = (git_transport *) t;
+ t->parent.rpc = 1;
+
+ if (git_vector_init(&t->parent.refs, 16, NULL) < 0) {
+ git__free(t);
+ return -1;
+ }
#ifdef GIT_WIN32
/* on win32, the WSA context needs to be initialized
@@ -698,7 +450,7 @@ int git_transport_https(git_transport **out)
if (git_transport_http((git_transport **)&t) < 0)
return -1;
- t->parent.encrypt = 1;
+ t->parent.use_ssl = 1;
t->parent.check_cert = 1;
*out = (git_transport *) t;
diff --git a/src/transports/local.c b/src/transports/local.c
index 0e1ae37..561c84f 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -15,11 +15,11 @@
#include "posix.h"
#include "path.h"
#include "buffer.h"
+#include "pkt.h"
typedef struct {
git_transport parent;
git_repository *repo;
- git_vector refs;
} transport_local;
static int add_ref(transport_local *t, const char *name)
@@ -27,19 +27,32 @@ static int add_ref(transport_local *t, const char *name)
const char peeled[] = "^{}";
git_remote_head *head;
git_object *obj = NULL, *target = NULL;
+ git_transport *transport = (git_transport *) t;
git_buf buf = GIT_BUF_INIT;
+ git_pkt_ref *pkt;
head = git__malloc(sizeof(git_remote_head));
GITERR_CHECK_ALLOC(head);
+ pkt = git__malloc(sizeof(git_pkt_ref));
+ GITERR_CHECK_ALLOC(pkt);
head->name = git__strdup(name);
GITERR_CHECK_ALLOC(head->name);
- if (git_reference_name_to_oid(&head->oid, t->repo, name) < 0 ||
- git_vector_insert(&t->refs, head) < 0)
- {
- git__free(head->name);
+ if (git_reference_name_to_oid(&head->oid, t->repo, name) < 0) {
git__free(head);
+ git__free(pkt->head.name);
+ git__free(pkt);
+ }
+
+ pkt->type = GIT_PKT_REF;
+ memcpy(&pkt->head, head, sizeof(git_remote_head));
+ git__free(head);
+
+ if (git_vector_insert(&transport->refs, pkt) < 0)
+ {
+ git__free(pkt->head.name);
+ git__free(pkt);
return -1;
}
@@ -47,7 +60,7 @@ static int add_ref(transport_local *t, const char *name)
if (git__prefixcmp(name, GIT_REFS_TAGS_DIR))
return 0;
- if (git_object_lookup(&obj, t->repo, &head->oid, GIT_OBJ_ANY) < 0)
+ if (git_object_lookup(&obj, t->repo, &pkt->head.oid, GIT_OBJ_ANY) < 0)
return -1;
head = NULL;
@@ -66,14 +79,20 @@ static int add_ref(transport_local *t, const char *name)
head->name = git_buf_detach(&buf);
+ pkt = git__malloc(sizeof(git_pkt_ref));
+ GITERR_CHECK_ALLOC(pkt);
+ pkt->type = GIT_PKT_REF;
+
if (git_tag_peel(&target, (git_tag *) obj) < 0)
goto on_error;
git_oid_cpy(&head->oid, git_object_id(target));
git_object_free(obj);
git_object_free(target);
+ memcpy(&pkt->head, head, sizeof(git_remote_head));
+ git__free(head);
- if (git_vector_insert(&t->refs, head) < 0)
+ if (git_vector_insert(&transport->refs, pkt) < 0)
return -1;
return 0;
@@ -88,11 +107,12 @@ static int store_refs(transport_local *t)
{
unsigned int i;
git_strarray ref_names = {0};
+ git_transport *transport = (git_transport *) t;
assert(t);
if (git_reference_list(&ref_names, t->repo, GIT_REF_LISTALL) < 0 ||
- git_vector_init(&t->refs, (unsigned int)ref_names.count, NULL) < 0)
+ git_vector_init(&transport->refs, (unsigned int)ref_names.count, NULL) < 0)
goto on_error;
/* Sort the references first */
@@ -111,28 +131,11 @@ static int store_refs(transport_local *t)
return 0;
on_error:
- git_vector_free(&t->refs);
+ git_vector_free(&transport->refs);
git_strarray_free(&ref_names);
return -1;
}
-static int local_ls(git_transport *transport, git_headlist_cb list_cb, void *payload)
-{
- transport_local *t = (transport_local *) transport;
- git_vector *refs = &t->refs;
- unsigned int i;
- git_remote_head *h;
-
- assert(transport && transport->connected);
-
- git_vector_foreach(refs, i, h) {
- if (list_cb(h, payload) < 0)
- return -1;
- }
-
- return 0;
-}
-
/*
* Try to open the url as a git directory. The direction doesn't
* matter in this case because we're calulating the heads ourselves.
@@ -201,14 +204,14 @@ static void local_free(git_transport *transport)
{
unsigned int i;
transport_local *t = (transport_local *) transport;
- git_vector *vec = &t->refs;
- git_remote_head *h;
+ git_vector *vec = &transport->refs;
+ git_pkt_ref *pkt;
assert(transport);
- git_vector_foreach (vec, i, h) {
- git__free(h->name);
- git__free(h);
+ git_vector_foreach (vec, i, pkt) {
+ git__free(pkt->head.name);
+ git__free(pkt);
}
git_vector_free(vec);
@@ -229,8 +232,8 @@ int git_transport_local(git_transport **out)
memset(t, 0x0, sizeof(transport_local));
+ t->parent.own_logic = 1;
t->parent.connect = local_connect;
- t->parent.ls = local_ls;
t->parent.negotiate_fetch = local_negotiate_fetch;
t->parent.close = local_close;
t->parent.free = local_free;