I broke your bindings Hey. Apologies in advance -- I broke your bindings. This is a major commit that includes a long-overdue redesign of the whole object-database structure. This is expected to be the last major external API redesign of the library until the first non-alpha release. Please get your bindings up to date with these changes. They will be included in the next minor release. Sorry again! Major features include: - Real caching and refcounting on parsed objects - Real caching and refcounting on objects read from the ODB - Streaming writes & reads from the ODB - Single-method writes for all object types - The external API is now partially thread-safe The speed increases are significant in all aspects, specially when reading an object several times from the ODB (revwalking) and when writing big objects to the ODB. Here's a full changelog for the external API: blob.h ------ - Remove `git_blob_new` - Remove `git_blob_set_rawcontent` - Remove `git_blob_set_rawcontent_fromfile` - Rename `git_blob_writefile` -> `git_blob_create_fromfile` - Change `git_blob_create_fromfile`: The `path` argument is now relative to the repository's working dir - Add `git_blob_create_frombuffer` commit.h -------- - Remove `git_commit_new` - Remove `git_commit_add_parent` - Remove `git_commit_set_message` - Remove `git_commit_set_committer` - Remove `git_commit_set_author` - Remove `git_commit_set_tree` - Add `git_commit_create` - Add `git_commit_create_v` - Add `git_commit_create_o` - Add `git_commit_create_ov` tag.h ----- - Remove `git_tag_new` - Remove `git_tag_set_target` - Remove `git_tag_set_name` - Remove `git_tag_set_tagger` - Remove `git_tag_set_message` - Add `git_tag_create` - Add `git_tag_create_o` tree.h ------ - Change `git_tree_entry_2object`: New signature is `(git_object **object_out, git_repository *repo, git_tree_entry *entry)` - Remove `git_tree_new` - Remove `git_tree_add_entry` - Remove `git_tree_remove_entry_byindex` - Remove `git_tree_remove_entry_byname` - Remove `git_tree_clearentries` - Remove `git_tree_entry_set_id` - Remove `git_tree_entry_set_name` - Remove `git_tree_entry_set_attributes` object.h ------------ - Remove `git_object_new - Remove `git_object_write` - Change `git_object_close`: This method is now *mandatory*. Not closing an object causes a memory leak. odb.h ----- - Remove type `git_rawobj` - Remove `git_rawobj_close` - Rename `git_rawobj_hash` -> `git_odb_hash` - Change `git_odb_hash`: New signature is `(git_oid *id, const void *data, size_t len, git_otype type)` - Add type `git_odb_object` - Add `git_odb_object_close` - Change `git_odb_read`: New signature is `(git_odb_object **out, git_odb *db, const git_oid *id)` - Change `git_odb_read_header`: New signature is `(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id)` - Remove `git_odb_write` - Add `git_odb_open_wstream` - Add `git_odb_open_rstream` odb_backend.h ------------- - Change type `git_odb_backend`: New internal signatures are as follows int (* read)(void **, size_t *, git_otype *, struct git_odb_backend *, const git_oid *) int (* read_header)(size_t *, git_otype *, struct git_odb_backend *, const git_oid *) int (* writestream)(struct git_odb_stream **, struct git_odb_backend *, size_t, git_otype) int (* readstream)( struct git_odb_stream **, struct git_odb_backend *, const git_oid *) - Add type `git_odb_stream` - Add enum `git_odb_streammode` Signed-off-by: Vicent Marti <tanoku@gmail.com>
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 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194
diff --git a/include/git2/blob.h b/include/git2/blob.h
index 2b7154f..3cd1467 100644
--- a/include/git2/blob.h
+++ b/include/git2/blob.h
@@ -41,8 +41,6 @@ GIT_BEGIN_DECL
/**
* Lookup a blob object from a repository.
- * The generated blob object is owned by the revision
- * repo and shall not be freed by the user.
*
* @param blob pointer to the looked up blob
* @param repo the repo to use when locating the blob.
@@ -55,49 +53,12 @@ GIT_INLINE(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git
}
/**
- * Create a new in-memory git_blob.
- *
- * The blob object must be manually filled using
- * the 'set_rawcontent' methods before it can
- * be written back to disk.
- *
- * @param blob pointer to the new blob
- * @param repo The repository where the object will reside
- * @return 0 on success; error code otherwise
- */
-GIT_INLINE(int) git_blob_new(git_blob **blob, git_repository *repo)
-{
- return git_object_new((git_object **)blob, repo, GIT_OBJ_BLOB);
-}
-
-/**
- * Fill a blob with the contents inside
- * the pointed file.
- *
- * @param blob pointer to the new blob
- * @param filename name of the file to read
- * @return 0 on success; error code otherwise
- */
-GIT_EXTERN(int) git_blob_set_rawcontent_fromfile(git_blob *blob, const char *filename);
-
-/**
- * Fill a blob with the contents inside
- * the pointed buffer
- *
- * @param blob pointer to the blob
- * @param buffer buffer with the contents for the blob
- * @param len size of the buffer
- * @return 0 on success; error code otherwise
- */
-GIT_EXTERN(int) git_blob_set_rawcontent(git_blob *blob, const void *buffer, size_t len);
-
-/**
* Get a read-only buffer with the raw content of a blob.
*
* A pointer to the raw content of a blob is returned;
* this pointer is owned internally by the object and shall
* not be free'd. The pointer may be invalidated at a later
- * time (e.g. when changing the contents of the blob).
+ * time.
*
* @param blob pointer to the blob
* @return the pointer; NULL if the blob has no contents
@@ -114,14 +75,28 @@ GIT_EXTERN(int) git_blob_rawsize(git_blob *blob);
/**
* Read a file from the working folder of a repository
- * and write it to the Object Database as a loose blob,
- * if such doesn't exist yet.
+ * and write it to the Object Database as a loose blob
*
- * @param written_id return the id of the written blob
- * @param repo repository where the blob will be written
- * @param path file from which the blob will be created
+ * @param oid return the id of the written blob
+ * @param repo repository where the blob will be written.
+ * this repository cannot be bare
+ * @param path file from which the blob will be created,
+ * relative to the repository's working dir
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *path);
+
+
+/**
+ * Write an in-memory buffer to the ODB as a blob
+ *
+ * @param oid return the oid of the written blob
+ * @param repo repository where to blob will be written
+ * @param buffer data to be written into the blob
+ * @param len length of the data
+ * @return 0 on success; error code otherwise
*/
-GIT_EXTERN(int) git_blob_writefile(git_oid *written_id, git_repository *repo, const char *path);
+GIT_EXTERN(int) git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *buffer, size_t len);
/** @} */
GIT_END_DECL
diff --git a/include/git2/commit.h b/include/git2/commit.h
index 1556e52..ba18a5b 100644
--- a/include/git2/commit.h
+++ b/include/git2/commit.h
@@ -41,8 +41,6 @@ GIT_BEGIN_DECL
/**
* Lookup a commit object from a repository.
- * The generated commit object is owned by the revision
- * repo and shall not be freed by the user.
*
* @param commit pointer to the looked up commit
* @param repo the repo to use when locating the commit.
@@ -56,23 +54,8 @@ GIT_INLINE(int) git_commit_lookup(git_commit **commit, git_repository *repo, con
}
/**
- * Create a new in-memory git_commit.
- *
- * The commit object must be manually filled using
- * setter methods before it can be written to its
- * repository.
- *
- * @param commit pointer to the new commit
- * @param repo The repository where the object will reside
- * @return 0 on success; error code otherwise
- */
-GIT_INLINE(int) git_commit_new(git_commit **commit, git_repository *repo)
-{
- return git_object_new((git_object **)commit, repo, GIT_OBJ_COMMIT);
-}
-
-/**
* Get the id of a commit.
+ *
* @param commit a previously loaded commit.
* @return object identity for the commit.
*/
@@ -80,6 +63,7 @@ GIT_EXTERN(const git_oid *) git_commit_id(git_commit *commit);
/**
* Get the short (one line) message of a commit.
+ *
* @param commit a previously loaded commit.
* @return the short message of a commit
*/
@@ -87,6 +71,7 @@ GIT_EXTERN(const char *) git_commit_message_short(git_commit *commit);
/**
* Get the full message of a commit.
+ *
* @param commit a previously loaded commit.
* @return the message of a commit
*/
@@ -94,6 +79,7 @@ GIT_EXTERN(const char *) git_commit_message(git_commit *commit);
/**
* Get the commit time (i.e. committer time) of a commit.
+ *
* @param commit a previously loaded commit.
* @return the time of a commit
*/
@@ -101,6 +87,7 @@ GIT_EXTERN(time_t) git_commit_time(git_commit *commit);
/**
* Get the commit timezone offset (i.e. committer's preferred timezone) of a commit.
+ *
* @param commit a previously loaded commit.
* @return positive or negative timezone offset, in minutes from UTC
*/
@@ -108,6 +95,7 @@ GIT_EXTERN(int) git_commit_time_offset(git_commit *commit);
/**
* Get the committer of a commit.
+ *
* @param commit a previously loaded commit.
* @return the committer of a commit
*/
@@ -115,6 +103,7 @@ GIT_EXTERN(const git_signature *) git_commit_committer(git_commit *commit);
/**
* Get the author of a commit.
+ *
* @param commit a previously loaded commit.
* @return the author of a commit
*/
@@ -122,6 +111,7 @@ GIT_EXTERN(const git_signature *) git_commit_author(git_commit *commit);
/**
* Get the tree pointed to by a commit.
+ *
* @param tree_out pointer where to store the tree object
* @param commit a previously loaded commit.
* @return 0 on success; error code otherwise
@@ -146,42 +136,129 @@ GIT_EXTERN(unsigned int) git_commit_parentcount(git_commit *commit);
*/
GIT_EXTERN(int) git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n);
+
/**
- * Add a new parent commit to an existing commit
- * @param commit the commit object
- * @param new_parent the new commit which will be a parent
+ * Create a new commit in the repository
+ *
+ *
+ * @param oid Pointer where to store the OID of the
+ * newly created commit
+ *
+ * @param repo Repository where to store the commit
+ *
+ * @param update_ref If not NULL, name of the reference that
+ * will be updated to point to this commit. If the reference
+ * is not direct, it will be resolved to a direct reference.
+ * Use "HEAD" to update the HEAD of the current branch and
+ * make it point to this commit
+ *
+ * @param author Signature representing the author and the authory
+ * time of this commit
+ *
+ * @param committer Signature representing the committer and the
+ * commit time of this commit
+ *
+ * @param message Full message for this commit
+ *
+ * @param tree_oid Object ID of the tree for this commit. Note that
+ * no validation is performed on this OID. Use the _o variants of
+ * this method to assure a proper tree is passed to the commit.
+ *
+ * @param parent_count Number of parents for this commit
+ *
+ * @param parents Array of pointers to parent OIDs for this commit.
+ * Note that no validation is performed on these OIDs. Use the _o
+ * variants of this method to assure that are parents for the commit
+ * are proper objects.
+ *
* @return 0 on success; error code otherwise
+ * The created commit will be written to the Object Database and
+ * the given reference will be updated to point to it
*/
-GIT_EXTERN(int) git_commit_add_parent(git_commit *commit, git_commit *new_parent);
+GIT_EXTERN(int) git_commit_create(
+ git_oid *oid,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message,
+ const git_oid *tree_oid,
+ int parent_count,
+ const git_oid *parent_oids[]);
/**
- * Set the message of a commit
- * @param commit the commit object
- * @param message the new message
+ * Create a new commit in the repository using `git_object`
+ * instances as parameters.
+ *
+ * The `tree_oid` and `parent_oids` paremeters now take a instance
+ * of `git_tree` and `git_commit`, respectively.
+ *
+ * All other parameters remain the same
+ *
+ * @see git_commit_create
*/
-GIT_EXTERN(void) git_commit_set_message(git_commit *commit, const char *message);
+GIT_EXTERN(int) git_commit_create_o(
+ git_oid *oid,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message,
+ const git_tree *tree,
+ int parent_count,
+ const git_commit *parents[]);
/**
- * Set the committer of a commit
- * @param commit the commit object
- * @param author_sig signature of the committer
+ * Create a new commit in the repository using `git_object`
+ * instances and a variable argument list.
+ *
+ * The `tree_oid` paremeter now takes a instance
+ * of `const git_tree *`.
+ *
+ * The parents for the commit are specified as a variable
+ * list of pointers to `const git_commit *`. Note that this
+ * is a convenience method which may not be safe to export
+ * for certain languages or compilers
+ *
+ * All other parameters remain the same
+ *
+ * @see git_commit_create
*/
-GIT_EXTERN(void) git_commit_set_committer(git_commit *commit, const git_signature *committer_sig);
+GIT_EXTERN(int) git_commit_create_ov(
+ git_oid *oid,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message,
+ const git_tree *tree,
+ int parent_count,
+ ...);
-/**
- * Set the author of a commit
- * @param commit the commit object
- * @param author_sig signature of the author
- */
-GIT_EXTERN(void) git_commit_set_author(git_commit *commit, const git_signature *author_sig);
/**
- * Set the tree which is pointed to by a commit
- * @param commit the commit object
- * @param tree the new tree
- * @param 0 on success; error code otherwise
+ * Create a new commit in the repository using
+ * a variable argument list.
+ *
+ * The parents for the commit are specified as a variable
+ * list of pointers to `const git_oid *`. Note that this
+ * is a convenience method which may not be safe to export
+ * for certain languages or compilers
+ *
+ * All other parameters remain the same
+ *
+ * @see git_commit_create
*/
-GIT_EXTERN(int) git_commit_set_tree(git_commit *commit, git_tree *tree);
+GIT_EXTERN(int) git_commit_create_v(
+ git_oid *oid,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message,
+ const git_oid *tree_oid,
+ int parent_count,
+ ...);
/** @} */
GIT_END_DECL
diff --git a/include/git2/common.h b/include/git2/common.h
index f904521..7cfb898 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -158,6 +158,9 @@
/** The state of the reference is not valid */
#define GIT_EINVALIDREFSTATE (GIT_ERROR - 21)
+/** This feature has not been implemented yet */
+#define GIT_ENOTIMPLEMENTED (GIT_ERROR - 22)
+
GIT_BEGIN_DECL
typedef struct {
diff --git a/include/git2/object.h b/include/git2/object.h
index 748386f..16dde8e 100644
--- a/include/git2/object.h
+++ b/include/git2/object.h
@@ -42,7 +42,8 @@ GIT_BEGIN_DECL
* Lookup a reference to one of the objects in a repostory.
*
* The generated reference is owned by the repository and
- * should not be freed by the user.
+ * should be closed with the `git_object_close` method
+ * instead of free'd manually.
*
* The 'type' parameter must match the type of the object
* in the odb; the method will fail otherwise.
@@ -58,54 +59,8 @@ GIT_BEGIN_DECL
GIT_EXTERN(int) git_object_lookup(git_object **object, git_repository *repo, const git_oid *id, git_otype type);
/**
- * Create a new in-memory repository object with
- * the given type.
- *
- * The object's attributes can be filled in using the
- * corresponding setter methods.
- *
- * The object will be written back to given git_repository
- * when the git_object_write() function is called; objects
- * cannot be written to disk until all their main
- * attributes have been properly filled.
- *
- * Objects are instantiated with no SHA1 id; their id
- * will be automatically generated when writing to the
- * repository.
- *
- * @param object pointer to the new object
- * @parem repo Repository where the object belongs
- * @param type Type of the object to be created
- * @return the new object
- */
-GIT_EXTERN(int) git_object_new(git_object **object, git_repository *repo, git_otype type);
-
-
-/**
- * Write back an object to disk.
- *
- * The object will be written to its corresponding
- * repository.
- *
- * If the object has no changes since it was first
- * read from the repository, no actions will take place.
- *
- * If the object has been modified since it was read from
- * the repository, or it has been created from scratch
- * in memory, it will be written to the repository and
- * its SHA1 ID will be updated accordingly.
- *
- * @param object Git object to write back
- * @return 0 on success; otherwise an error code
- */
-GIT_EXTERN(int) git_object_write(git_object *object);
-
-/**
* Get the id (SHA1) of a repository object
*
- * In-memory objects created by git_object_new() do not
- * have a SHA1 ID until they are written on a repository.
- *
* @param obj the repository object
* @return the SHA1 id
*/
@@ -137,14 +92,8 @@ GIT_EXTERN(git_repository *) git_object_owner(const git_object *obj);
* by the repository.
*
* IMPORTANT:
- * It is *not* necessary to call this method when you stop using
- * an object, since all object memory is automatically reclaimed
- * by the repository when it is freed.
- *
- * Forgetting to call `git_object_close` does not cause memory
- * leaks, but it's is recommended to close as soon as possible
- * the biggest objects (e.g. blobs) to prevent wasting memory
- * space.
+ * It *is* necessary to call this method when you stop using
+ * an object. Failure to do so will cause a memory leak.
*
* @param object the object to close
*/
diff --git a/include/git2/odb.h b/include/git2/odb.h
index 0d28589..8926b44 100644
--- a/include/git2/odb.h
+++ b/include/git2/odb.h
@@ -28,6 +28,7 @@
#include "common.h"
#include "types.h"
#include "oid.h"
+#include "odb_backend.h"
/**
* @file git2/odb.h
@@ -100,61 +101,49 @@ GIT_EXTERN(int) git_odb_add_alternate(git_odb *odb, git_odb_backend *backend, in
/**
* Close an open object database.
+ *
* @param db database pointer to close. If NULL no action is taken.
*/
GIT_EXTERN(void) git_odb_close(git_odb *db);
-/** An object read from the database. */
-typedef struct {
- void *data; /**< Raw, decompressed object data. */
- size_t len; /**< Total number of bytes in data. */
- git_otype type; /**< Type of this object. */
-} git_rawobj;
-
/**
* Read an object from the database.
*
- * If GIT_ENOTFOUND then out->data is set to NULL.
+ * This method queries all avaiable ODB backends
+ * trying to read the given OID.
+ *
+ * The returned object is reference counted and
+ * internally cached, so it should be closed
+ * by the user once it's no longer in use.
*
- * @param out object descriptor to populate upon reading.
+ * @param out pointer where to store the read object
* @param db database to search for the object in.
* @param id identity of the object to read.
* @return
* - GIT_SUCCESS if the object was read;
* - GIT_ENOTFOUND if the object is not in the database.
*/
-GIT_EXTERN(int) git_odb_read(git_rawobj *out, git_odb *db, const git_oid *id);
+GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id);
/**
* Read the header of an object from the database, without
* reading its full contents.
*
- * Only the 'type' and 'len' fields of the git_rawobj structure
- * are filled. The 'data' pointer will always be NULL.
+ * The header includes the length and the type of an object.
*
- * The raw object pointed by 'out' doesn't need to be manually
- * closed with git_rawobj_close().
+ * Note that most backends do not support reading only the header
+ * of an object, so the whole object will be read and then the
+ * header will be returned.
*
- * @param out object descriptor to populate upon reading.
+ * @param len_p pointer where to store the length
+ * @param type_p pointer where to store the type
* @param db database to search for the object in.
* @param id identity of the object to read.
* @return
* - GIT_SUCCESS if the object was read;
* - GIT_ENOTFOUND if the object is not in the database.
*/
-GIT_EXTERN(int) git_odb_read_header(git_rawobj *out, git_odb *db, const git_oid *id);
-
-/**
- * Write an object to the database.
- *
- * @param id identity of the object written.
- * @param db database to which the object should be written.
- * @param obj object descriptor for the object to write.
- * @return
- * - GIT_SUCCESS if the object was written;
- * - GIT_ERROR otherwise.
- */
-GIT_EXTERN(int) git_odb_write(git_oid *id, git_odb *db, git_rawobj *obj);
+GIT_EXTERN(int) git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id);
/**
* Determine if the given object can be found in the object database.
@@ -162,39 +151,89 @@ GIT_EXTERN(int) git_odb_write(git_oid *id, git_odb *db, git_rawobj *obj);
* @param db database to be searched for the given object.
* @param id the object to search for.
* @return
- * - true, if the object was found
- * - false, otherwise
+ * - 1, if the object was found
+ * - 0, otherwise
*/
GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
+/**
+ * Open a stream to write an object into the ODB
+ *
+ * The type and final length of the object must be specified
+ * when opening the stream.
+ *
+ * The returned stream will be of type `GIT_STREAM_WRONLY` and
+ * will have the following methods:
+ *
+ * - stream->write: write `n` bytes into the stream
+ * - stream->finalize_write: close the stream and store the object in
+ * the odb
+ * - stream->free: free the stream
+ *
+ * The streaming write won't be effective until `stream->finalize_write`
+ * is called and returns without an error
+ *
+ * The stream must always be free'd or will leak memory.
+ *
+ * @see git_odb_stream
+ *
+ * @param stream pointer where to store the stream
+ * @param db object database where the stream will write
+ * @param size final size of the object that will be written
+ * @para type type of the object that will be written
+ * @return 0 if the stream was created; error code otherwise
+ */
+GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **stream, git_odb *db, size_t size, git_otype type);
-
-
+/**
+ * Open a stream to read an object from the ODB
+ *
+ * Note that most backends do *not* support streaming reads
+ * because they store their objects as compressed/delta'ed blobs.
+ *
+ * It's recommended to use `git_odb_read` instead, which is
+ * assured to work on all backends.
+ *
+ * The returned stream will be of type `GIT_STREAM_RDONLY` and
+ * will have the following methods:
+ *
+ * - stream->read: read `n` bytes from the stream
+ * - stream->free: free the stream
+ *
+ * The stream must always be free'd or will leak memory.
+ *
+ * @see git_odb_stream
+ *
+ * @param stream pointer where to store the stream
+ * @param db object database where the stream will read from
+ * @param oid oid of the object the stream will read from
+ * @return 0 if the stream was created; error code otherwise
+ */
+GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid);
/**
- * Determine the object-ID (sha1 hash) of the given git_rawobj.
+ * Determine the object-ID (sha1 hash) of a data buffer
*
- * The input obj must be a valid loose object type and the data
- * pointer must not be NULL, unless the len field is also zero.
+ * The resulting SHA-1 OID will the itentifier for the data
+ * buffer as if the data buffer it were to written to the ODB.
*
* @param id the resulting object-ID.
- * @param obj the object whose hash is to be determined.
- * @return
- * - GIT_SUCCESS if the object-ID was correctly determined.
- * - GIT_ERROR if the given object is malformed.
+ * @param data data to hash
+ * @param len size of the data
+ * @param type of the data to hash
+ * @return 0 on success; error code otherwise
*/
-GIT_EXTERN(int) git_rawobj_hash(git_oid *id, git_rawobj *obj);
+GIT_EXTERN(int) git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type);
/**
- * Release all memory used by the obj structure.
- *
- * As a result of this call, obj->data will be set to NULL.
+ * Close an ODB object
*
- * If obj->data is already NULL, nothing happens.
+ * This method must always be called once a `git_odb_object` is no
+ * longer needed, otherwise memory will leak.
*
- * @param obj object descriptor to free.
+ * @param object object to close
*/
-GIT_EXTERN(void) git_rawobj_close(git_rawobj *obj);
+GIT_EXTERN(void) git_odb_object_close(git_odb_object *object);
/** @} */
GIT_END_DECL
diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h
index 0e817eb..3875ec7 100644
--- a/include/git2/odb_backend.h
+++ b/include/git2/odb_backend.h
@@ -39,24 +39,32 @@
*/
GIT_BEGIN_DECL
+struct git_odb_stream;
+
/** An instance for a custom backend */
struct git_odb_backend {
git_odb *odb;
int (* read)(
- git_rawobj *,
+ void **, size_t *, git_otype *,
struct git_odb_backend *,
const git_oid *);
int (* read_header)(
- git_rawobj *,
+ size_t *, git_otype *,
struct git_odb_backend *,
const git_oid *);
- int (* write)(
- git_oid *id,
+ int (* writestream)(
+ struct git_odb_stream **,
+ struct git_odb_backend *,
+ size_t,
+ git_otype);
+
+ int (* readstream)(
+ struct git_odb_stream **,
struct git_odb_backend *,
- git_rawobj *obj);
+ const git_oid *);
int (* exists)(
struct git_odb_backend *,
@@ -65,12 +73,28 @@ struct git_odb_backend {
void (* free)(struct git_odb_backend *);
};
+/** A stream to read/write from a backend */
+struct git_odb_stream {
+ struct git_odb_backend *backend;
+ int mode;
+
+ int (*read)(struct git_odb_stream *stream, char *buffer, size_t len);
+ int (*write)(struct git_odb_stream *stream, const char *buffer, size_t len);
+ int (*finalize_write)(git_oid *oid_p, struct git_odb_stream *stream);
+ void (*free)(struct git_odb_stream *stream);
+};
+
+/** Streaming mode */
+typedef enum {
+ GIT_STREAM_RDONLY = (1 << 1),
+ GIT_STREAM_WRONLY = (1 << 2),
+ GIT_STREAM_RW = (GIT_STREAM_RDONLY | GIT_STREAM_WRONLY),
+} git_odb_streammode;
+
+
GIT_EXTERN(int) git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir);
GIT_EXTERN(int) git_odb_backend_loose(git_odb_backend **backend_out, const char *objects_dir);
-
-#ifdef GIT2_SQLITE_BACKEND
GIT_EXTERN(int) git_odb_backend_sqlite(git_odb_backend **backend_out, const char *sqlite_db);
-#endif
GIT_END_DECL
diff --git a/include/git2/tag.h b/include/git2/tag.h
index f1669eb..c343f6b 100644
--- a/include/git2/tag.h
+++ b/include/git2/tag.h
@@ -41,8 +41,6 @@ GIT_BEGIN_DECL
/**
* Lookup a tag object from the repository.
- * The generated tag object is owned by the revision
- * repo and shall not be freed by the user.
*
* @param tag pointer to the looked up tag
* @param repo the repo to use when locating the tag.
@@ -55,23 +53,8 @@ GIT_INLINE(int) git_tag_lookup(git_tag **tag, git_repository *repo, const git_oi
}
/**
- * Create a new in-memory git_tag.
- *
- * The tag object must be manually filled using
- * setter methods before it can be written to its
- * repository.
- *
- * @param tag pointer to the new tag
- * @param repo The repository where the object will reside
- * @return 0 on success; error code otherwise
- */
-GIT_INLINE(int) git_tag_new(git_tag **tag, git_repository *repo)
-{
- return git_object_new((git_object **)tag, repo, (git_otype)GIT_OBJ_TAG);
-}
-
-/**
* Get the id of a tag.
+ *
* @param tag a previously loaded tag.
* @return object identity for the tag.
*/
@@ -79,6 +62,10 @@ GIT_EXTERN(const git_oid *) git_tag_id(git_tag *tag);
/**
* Get the tagged object of a tag
+ *
+ * This method performs a repository lookup for the
+ * given object and returns it
+ *
* @param target pointer where to store the target
* @param tag a previously loaded tag.
* @return 0 on success; error code otherwise
@@ -87,6 +74,7 @@ GIT_EXTERN(int) git_tag_target(git_object **target, git_tag *t);
/**
* Get the OID of the tagged object of a tag
+ *
* @param tag a previously loaded tag.
* @return pointer to the OID
*/
@@ -94,6 +82,7 @@ GIT_EXTERN(const git_oid *) git_tag_target_oid(git_tag *t);
/**
* Get the type of a tag's tagged object
+ *
* @param tag a previously loaded tag.
* @return type of the tagged object
*/
@@ -101,6 +90,7 @@ GIT_EXTERN(git_otype) git_tag_type(git_tag *t);
/**
* Get the name of a tag
+ *
* @param tag a previously loaded tag.
* @return name of the tag
*/
@@ -108,6 +98,7 @@ GIT_EXTERN(const char *) git_tag_name(git_tag *t);
/**
* Get the tagger (author) of a tag
+ *
* @param tag a previously loaded tag.
* @return reference to the tag's author
*/
@@ -115,39 +106,69 @@ GIT_EXTERN(const git_signature *) git_tag_tagger(git_tag *t);
/**
* Get the message of a tag
+ *
* @param tag a previously loaded tag.
* @return message of the tag
*/
GIT_EXTERN(const char *) git_tag_message(git_tag *t);
-/**
- * Set the target of a tag (i.e. the object that the tag points to)
- * @param tag The tag to modify
- * @param target the new tagged target
- */
-GIT_EXTERN(int) git_tag_set_target(git_tag *tag, git_object *target);
/**
- * Set the name of a tag
- * @param tag The tag to modify
- * @param name the new name for the tag
+ * Create a new tag in the repository from an OID
+ *
+ * @param oid Pointer where to store the OID of the
+ * newly created tag
+ *
+ * @param repo Repository where to store the tag
+ *
+ * @param tag_name Name for the tag; this name is validated
+ * for consistency
+ *
+ * @param target OID to which this tag points; note that no
+ * validation is done on this OID. Use the _o version of this
+ * method to assure a proper object is being tagged
+ *
+ * @param target_type Type of the tagged OID; note that no
+ * validation is performed here either
+ *
+ * @param tagger Signature of the tagger for this tag, and
+ * of the tagging time
+ *
+ * @param message Full message for this tag
+ *
+ * @return 0 on success; error code otherwise.
+ * A tag object is written to the ODB, and a proper reference
+ * is written in the /refs/tags folder, pointing to it
*/
-GIT_EXTERN(void) git_tag_set_name(git_tag *tag, const char *name);
+GIT_EXTERN(int) git_tag_create(
+ git_oid *oid,
+ git_repository *repo,
+ const char *tag_name,
+ const git_oid *target,
+ git_otype target_type,
+ const git_signature *tagger,
+ const char *message);
-/**
- * Set the tagger of a tag
- * @param tag The tag to modify
- * @param tagger_sig signature of the tagging action
- * @return 0 on success; error code otherwise
- */
-GIT_EXTERN(void) git_tag_set_tagger(git_tag *tag, const git_signature *tagger_sig);
/**
- * Set the message of a tag
- * @param tag The tag to modify
- * @param message the new tagger for the tag
+ * Create a new tag in the repository from an existing
+ * `git_object` instance
+ *
+ * This method replaces the `target` and `target_type`
+ * paremeters of `git_tag_create` by a single instance
+ * of a `const git_object *`, which is assured to be
+ * a proper object in the ODB and hence will create
+ * a valid tag
+ *
+ * @see git_tag_create
*/
-GIT_EXTERN(void) git_tag_set_message(git_tag *tag, const char *message);
+GIT_EXTERN(int) git_tag_create_o(
+ git_oid *oid,
+ git_repository *repo,
+ const char *tag_name,
+ const git_object *target,
+ const git_signature *tagger,
+ const char *message);
/** @} */
GIT_END_DECL
diff --git a/include/git2/tree.h b/include/git2/tree.h
index 3085b3f..ec2b516 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -41,8 +41,6 @@ GIT_BEGIN_DECL
/**
* Lookup a tree object from the repository.
- * The generated tree object is owned by the revision
- * repo and shall not be freed by the user.
*
* @param tree pointer to the looked up tree
* @param repo the repo to use when locating the tree.
@@ -55,31 +53,16 @@ GIT_INLINE(int) git_tree_lookup(git_tree **tree, git_repository *repo, const git
}
/**
- * Create a new in-memory git_tree.
- *
- * The tree object must be manually filled using
- * setter methods before it can be written to its
- * repository.
- *
- * @param tree pointer to the new tree
- * @param repo The repository where the object will reside
- * @return 0 on success; error code otherwise
- */
-GIT_INLINE(int) git_tree_new(git_tree **tree, git_repository *repo)
-{
- return git_object_new((git_object **)tree, repo, GIT_OBJ_TREE);
-}
-
-/**
* Get the id of a tree.
+ *
* @param tree a previously loaded tree.
* @return object identity for the tree.
*/
GIT_EXTERN(const git_oid *) git_tree_id(git_tree *tree);
-
/**
* Get the number of entries listed in a tree
+ *
* @param tree a previously loaded tree.
* @return the number of entries in the tree
*/
@@ -87,6 +70,7 @@ GIT_EXTERN(size_t) git_tree_entrycount(git_tree *tree);
/**
* Lookup a tree entry by its filename
+ *
* @param tree a previously loaded tree.
* @param filename the filename of the desired entry
* @return the tree entry; NULL if not found
@@ -95,6 +79,7 @@ GIT_EXTERN(git_tree_entry *) git_tree_entry_byname(git_tree *tree, const char *f
/**
* Lookup a tree entry by its position in the tree
+ *
* @param tree a previously loaded tree.
* @param idx the position in the entry list
* @return the tree entry; NULL if not found
@@ -103,6 +88,7 @@ GIT_EXTERN(git_tree_entry *) git_tree_entry_byindex(git_tree *tree, int idx);
/**
* Get the UNIX file attributes of a tree entry
+ *
* @param entry a tree entry
* @return attributes as an integer
*/
@@ -110,6 +96,7 @@ GIT_EXTERN(unsigned int) git_tree_entry_attributes(git_tree_entry *entry);
/**
* Get the filename of a tree entry
+ *
* @param entry a tree entry
* @return the name of the file
*/
@@ -117,6 +104,7 @@ GIT_EXTERN(const char *) git_tree_entry_name(git_tree_entry *entry);
/**
* Get the id of the object pointed by the entry
+ *
* @param entry a tree entry
* @return the oid of the object
*/
@@ -126,97 +114,11 @@ GIT_EXTERN(const git_oid *) git_tree_entry_id(git_tree_entry *entry);
* Convert a tree entry to the git_object it points too.
*
* @param object pointer to the converted object
+ * @param repo repository where to lookup the pointed object
* @param entry a tree entry
* @return a reference to the pointed object in the repository
*/
-GIT_EXTERN(int) git_tree_entry_2object(git_object **object, git_tree_entry *entry);
-
-/**
- * Add a new entry to a tree and return the new entry.
- *
- * This will mark the tree as modified; the new entry will
- * be written back to disk on the next git_object_write()
- *
- * @param entry_out Pointer to the entry that just got
- * created. May be NULL if you are not interested on
- * getting the new entry
- * @param tree Tree object to store the entry
- * @iparam id OID for the tree entry
- * @param filename Filename for the tree entry
- * @param attributes UNIX file attributes for the entry
- * @return 0 on success; otherwise error code
- */
-GIT_EXTERN(int) git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes);
-
-/**
- * Remove an entry by its index.
- *
- * Index must be >= 0 and < than git_tree_entrycount().
- *
- * This will mark the tree as modified; the modified entry will
- * be written back to disk on the next git_object_write()
- *
- * @param tree Tree where to remove the entry
- * @param idx index of the entry
- * @return 0 on successful removal; GIT_ENOTFOUND if the entry wasn't found
- */
-GIT_EXTERN(int) git_tree_remove_entry_byindex(git_tree *tree, int idx);
-
-/**
- * Remove an entry by its filename.
- *
- * This will mark the tree as modified; the modified entry will
- * be written back to disk on the next git_object_write()
- *
- * @param tree Tree where to remove the entry
- * @param filename File name of the entry
- * @return 0 on successful removal; GIT_ENOTFOUND if the entry wasn't found
- */
-GIT_EXTERN(int) git_tree_remove_entry_byname(git_tree *tree, const char *filename);
-
-/**
- * Clear all the entries in a tree.
- *
- * This will mark the tree as modified; the modified entry will
- * be written back to disk on the next git_object_write().
- *
- * @param tree Tree object whose entries are to be sorted
- */
-GIT_EXTERN(void) git_tree_clear_entries(git_tree *tree);
-
-/**
- * Change the SHA1 id of a tree entry.
- *
- * This will mark the tree that contains the entry as modified;
- * the modified entry will be written back to disk on the next git_object_write()
- *
- * @param entry Entry object which will be modified
- * @param oid new SHA1 oid for the entry
- */
-GIT_EXTERN(void) git_tree_entry_set_id(git_tree_entry *entry, const git_oid *oid);
-
-/**
- * Change the filename of a tree entry.
- *
- * This will mark the tree that contains the entry as modified;
- * the modified entry will be written back to disk on the next git_object_write()
- *
- * @param entry Entry object which will be modified
- * @param oid new filename for the entry
- */
-GIT_EXTERN(void) git_tree_entry_set_name(git_tree_entry *entry, const char *name);
-
-/**
- * Change the attributes of a tree entry.
- *
- * This will mark the tree that contains the entry as modified;
- * the modified entry will be written back to disk on the next git_object_write()
- *
- * @param entry Entry object which will be modified
- * @param oid new attributes for the entry
- * @return 0 if the attributes were properly set; error code otherwise
- */
-GIT_EXTERN(int) git_tree_entry_set_attributes(git_tree_entry *entry, unsigned int attr);
+GIT_EXTERN(int) git_tree_entry_2object(git_object **object_out, git_repository *repo, git_tree_entry *entry);
/** @} */
GIT_END_DECL
diff --git a/include/git2/types.h b/include/git2/types.h
index b5a8d7b..64f7fc7 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -71,7 +71,6 @@ typedef time_t git_time_t;
#endif
-
/** Basic type (loose or packed) of any Git object. */
typedef enum {
GIT_OBJ_ANY = -2, /**< Object can be any of the following */
@@ -92,6 +91,12 @@ typedef struct git_odb git_odb;
/** A custom backend in an ODB */
typedef struct git_odb_backend git_odb_backend;
+/** An object read from the ODB */
+typedef struct git_odb_object git_odb_object;
+
+/** A stream to read/write from the ODB */
+typedef struct git_odb_stream git_odb_stream;
+
/**
* Representation of an existing git repository,
* including all its object contents
diff --git a/src/backends/sqlite.c b/src/backends/sqlite.c
index b4c941a..72d7b4d 100644
--- a/src/backends/sqlite.c
+++ b/src/backends/sqlite.c
@@ -272,4 +272,13 @@ cleanup:
return GIT_ERROR;
}
+#else
+
+int git_odb_backend_sqlite(git_odb_backend **GIT_UNUSED(backend_out), const char *GIT_UNUSED(sqlite_db))
+{
+ GIT_UNUSED_ARG(backend_out);
+ GIT_UNUSED_ARG(sqlite_db);
+ return GIT_ENOTIMPLEMENTED;
+}
+
#endif /* HAVE_SQLITE3 */
diff --git a/src/blob.c b/src/blob.c
index 1e03b6b..bc0a08a 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -33,104 +33,89 @@
const void *git_blob_rawcontent(git_blob *blob)
{
assert(blob);
-
- if (blob->content.data != NULL)
- return blob->content.data;
-
- if (blob->object.in_memory)
- return NULL;
-
- if (!blob->object.source.open && git_object__source_open((git_object *)blob) < GIT_SUCCESS)
- return NULL;
-
- return blob->object.source.raw.data;
+ return blob->odb_object->raw.data;
}
int git_blob_rawsize(git_blob *blob)
{
assert(blob);
-
- if (blob->content.data != NULL)
- return blob->content.len;
-
- return blob->object.source.raw.len;
+ return blob->odb_object->raw.len;
}
void git_blob__free(git_blob *blob)
{
- gitfo_free_buf(&blob->content);
+ git_odb_object_close(blob->odb_object);
free(blob);
}
-int git_blob__parse(git_blob *blob)
+int git_blob__parse(git_blob *blob, git_odb_object *odb_obj)
{
assert(blob);
+ git_cached_obj_incref((git_cached_obj *)odb_obj);
+ blob->odb_object = odb_obj;
return GIT_SUCCESS;
}
-int git_blob__writeback(git_blob *blob, git_odb_source *src)
-{
- assert(blob->object.modified);
-
- if (blob->content.data == NULL)
- return GIT_EMISSINGOBJDATA;
-
- return git__source_write(src, blob->content.data, blob->content.len);
-}
-
-int git_blob_set_rawcontent(git_blob *blob, const void *buffer, size_t len)
+int git_blob_create_frombuffer(git_oid *oid, git_repository *repo, const void *buffer, size_t len)
{
- assert(blob && buffer);
-
- blob->object.modified = 1;
-
- git_object__source_close((git_object *)blob);
-
- if (blob->content.data != NULL)
- gitfo_free_buf(&blob->content);
+ int error;
+ git_odb_stream *stream;
- blob->content.data = git__malloc(len);
- blob->content.len = len;
+ if ((error = git_odb_open_wstream(&stream, repo->db, len, GIT_OBJ_BLOB)) < GIT_SUCCESS)
+ return error;
- if (blob->content.data == NULL)
- return GIT_ENOMEM;
+ stream->write(stream, buffer, len);
- memcpy(blob->content.data, buffer, len);
+ error = stream->finalize_write(oid, stream);
+ stream->free(stream);
- return GIT_SUCCESS;
+ return error;
}
-int git_blob_set_rawcontent_fromfile(git_blob *blob, const char *filename)
+int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *path)
{
- assert(blob && filename);
- blob->object.modified = 1;
+ int error, fd;
+ char full_path[GIT_PATH_MAX];
+ char buffer[2048];
+ git_off_t size;
+ git_odb_stream *stream;
- if (blob->content.data != NULL)
- gitfo_free_buf(&blob->content);
-
- return gitfo_read_file(&blob->content, filename);
-}
+ if (repo->path_workdir == NULL)
+ return GIT_ENOTFOUND;
-int git_blob_writefile(git_oid *written_id, git_repository *repo, const char *path)
-{
- int error;
- git_blob *blob;
+ git__joinpath(full_path, repo->path_workdir, path);
- if (gitfo_exists(path) < 0)
+ if ((fd = gitfo_open(full_path, O_RDONLY)) < 0)
return GIT_ENOTFOUND;
- if ((error = git_blob_new(&blob, repo)) < GIT_SUCCESS)
- return error;
+ if ((size = gitfo_size(fd)) < 0 || !git__is_sizet(size)) {
+ gitfo_close(fd);
+ return GIT_EOSERR;
+ }
- if ((error = git_blob_set_rawcontent_fromfile(blob, path)) < GIT_SUCCESS)
+ if ((error = git_odb_open_wstream(&stream, repo->db, (size_t)size, GIT_OBJ_BLOB)) < GIT_SUCCESS) {
+ gitfo_close(fd);
return error;
+ }
- if ((error = git_object_write((git_object *)blob)) < GIT_SUCCESS)
- return error;
+ while (size > 0) {
+ ssize_t read_len;
- git_oid_cpy(written_id, git_object_id((git_object *)blob));
+ read_len = read(fd, buffer, sizeof(buffer));
- git_object_close((git_object*)blob);
- return GIT_SUCCESS;
+ if (read_len < 0) {
+ gitfo_close(fd);
+ stream->free(stream);
+ return GIT_EOSERR;
+ }
+
+ stream->write(stream, buffer, read_len);
+ size -= read_len;
+ }
+
+ error = stream->finalize_write(oid, stream);
+ stream->free(stream);
+
+ return error;
}
diff --git a/src/blob.h b/src/blob.h
index febc296..4300d7e 100644
--- a/src/blob.h
+++ b/src/blob.h
@@ -3,15 +3,15 @@
#include "git2/blob.h"
#include "repository.h"
+#include "odb.h"
#include "fileops.h"
struct git_blob {
git_object object;
- gitfo_buf content;
+ git_odb_object *odb_object;
};
void git_blob__free(git_blob *blob);
-int git_blob__parse(git_blob *blob);
-int git_blob__writeback(git_blob *blob, git_odb_source *src);
+int git_blob__parse(git_blob *blob, git_odb_object *obj);
#endif
diff --git a/src/cache.c b/src/cache.c
index 5a9b841..fdf5c1b 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -32,23 +32,6 @@
#define GIT_CACHE_OPENADR 3
-GIT_INLINE(int) cached_obj_compare(git_cached_obj *obj, const git_oid *oid)
-{
- return git_oid_cmp(&obj->oid, oid);
-}
-
-GIT_INLINE(void) cached_obj_incref(git_cached_obj *obj)
-{
- git_atomic_inc(&obj->refcount);
-}
-
-GIT_INLINE(void) cached_obj_decref(git_cached_obj *obj, git_cached_obj_freeptr free_obj)
-{
- if (git_atomic_dec(&obj->refcount) == 0)
- free_obj(obj);
-}
-
-
void git_cache_init(git_cache *cache, size_t size, git_cached_obj_freeptr free_ptr)
{
size_t i;
@@ -82,7 +65,9 @@ void git_cache_free(git_cache *cache)
size_t i;
for (i = 0; i < (cache->size_mask + 1); ++i) {
- cached_obj_decref(cache->nodes[i].ptr, cache->free_obj);
+ if (cache->nodes[i].ptr)
+ git_cached_obj_decref(cache->nodes[i].ptr, cache->free_obj);
+
git_mutex_free(&cache->nodes[i].lock);
}
@@ -103,8 +88,8 @@ void *git_cache_get(git_cache *cache, const git_oid *oid)
git_mutex_lock(&node->lock);
{
- if (cached_obj_compare(node->ptr, oid) == 0) {
- cached_obj_incref(node->ptr);
+ if (node->ptr && git_cached_obj_compare(node->ptr, oid) == 0) {
+ git_cached_obj_incref(node->ptr);
node->lru = ++cache->lru_count;
found = 1;
}
@@ -121,14 +106,14 @@ void *git_cache_try_store(git_cache *cache, void *entry)
cache_node *nodes[GIT_CACHE_OPENADR], *lru_node;
const uint32_t *hash;
const git_oid *oid;
- size_t i, stored = 0;
+ size_t i;
oid = &((git_cached_obj*)entry)->oid;
hash = (const uint32_t *)oid->id;
/* increase the refcount on this object, because
* the cache now owns it */
- cached_obj_incref(entry);
+ git_cached_obj_incref(entry);
for (i = 0; i < GIT_CACHE_OPENADR; ++i) {
size_t pos = hash[i] & cache->size_mask;
@@ -139,34 +124,35 @@ void *git_cache_try_store(git_cache *cache, void *entry)
lru_node = nodes[0];
- for (i = 0; !stored && entry && i < GIT_CACHE_OPENADR; ++i) {
+ for (i = 0; i < GIT_CACHE_OPENADR; ++i) {
if (nodes[i]->ptr == NULL) {
nodes[i]->ptr = entry;
nodes[i]->lru = ++cache->lru_count;
- stored = 1;
- } else if (cached_obj_compare(nodes[i]->ptr, oid) == 0) {
- cached_obj_decref(entry, cache->free_obj);
+ break;
+ } else if (git_cached_obj_compare(nodes[i]->ptr, oid) == 0) {
+ git_cached_obj_decref(entry, cache->free_obj);
entry = nodes[i]->ptr;
- stored = 1;
+ nodes[i]->lru = ++cache->lru_count;
+ break;
}
if (nodes[i]->lru < lru_node->lru)
lru_node = nodes[i];
}
- if (!stored) {
+ if (i == GIT_CACHE_OPENADR) {
void *old_entry = lru_node->ptr;
assert(old_entry);
- cached_obj_decref(old_entry, cache->free_obj);
+ git_cached_obj_decref(old_entry, cache->free_obj);
lru_node->ptr = entry;
lru_node->lru = ++cache->lru_count;
}
/* increase the refcount again, because we are
* returning it to the user */
- cached_obj_incref(entry);
+ git_cached_obj_incref(entry);
for (i = 0; i < GIT_CACHE_OPENADR; ++i)
git_mutex_unlock(&nodes[i]->lock);
diff --git a/src/cache.h b/src/cache.h
index 9f525e6..975aaff 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -7,6 +7,8 @@
#include "thread-utils.h"
+#define GIT_DEFAULT_CACHE_SIZE 128
+
typedef void (*git_cached_obj_freeptr)(void *);
typedef struct {
@@ -35,4 +37,23 @@ void git_cache_free(git_cache *cache);
void *git_cache_try_store(git_cache *cache, void *entry);
void *git_cache_get(git_cache *cache, const git_oid *oid);
+
+GIT_INLINE(int) git_cached_obj_compare(git_cached_obj *obj, const git_oid *oid)
+{
+ return git_oid_cmp(&obj->oid, oid);
+}
+
+GIT_INLINE(void) git_cached_obj_incref(git_cached_obj *obj)
+{
+ git_atomic_inc(&obj->refcount);
+}
+
+GIT_INLINE(void) git_cached_obj_decref(git_cached_obj *obj, git_cached_obj_freeptr free_obj)
+{
+ if (git_atomic_dec(&obj->refcount) == 0)
+ free_obj(obj);
+}
+
+
+
#endif
diff --git a/src/commit.c b/src/commit.c
index 8da170c..2670c2e 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -29,9 +29,12 @@
#include "git2/signature.h"
#include "common.h"
+#include "odb.h"
#include "commit.h"
#include "signature.h"
+#include <stdarg.h>
+
#define COMMIT_BASIC_PARSE 0x0
#define COMMIT_FULL_PARSE 0x1
@@ -71,35 +74,165 @@ const git_oid *git_commit_id(git_commit *c)
return git_object_id((git_object *)c);
}
-int git_commit__writeback(git_commit *commit, git_odb_source *src)
+
+int git_commit_create_v(
+ git_oid *oid,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message,
+ const git_oid *tree_oid,
+ int parent_count,
+ ...)
{
- unsigned int i;
+ va_list ap;
+ int i, error;
+ const git_oid **oids;
- git__write_oid(src, "tree", &commit->tree_oid);
+ oids = git__malloc(parent_count * sizeof(git_oid *));
- for (i = 0; i < commit->parent_oids.length; ++i) {
- git_oid *parent_oid;
+ va_start(ap, parent_count);
+ for (i = 0; i < parent_count; ++i)
+ oids[i] = va_arg(ap, const git_oid *);
+ va_end(ap);
- parent_oid = git_vector_get(&commit->parent_oids, i);
- git__write_oid(src, "parent", parent_oid);
- }
+ error = git_commit_create(
+ oid, repo, update_ref, author, committer, message,
+ tree_oid, parent_count, oids);
+
+ free(oids);
+ return error;
+}
+
+int git_commit_create_ov(
+ git_oid *oid,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message,
+ const git_tree *tree,
+ int parent_count,
+ ...)
+{
+ va_list ap;
+ int i, error;
+ const git_oid **oids;
+
+ oids = git__malloc(parent_count * sizeof(git_oid *));
+
+ va_start(ap, parent_count);
+ for (i = 0; i < parent_count; ++i)
+ oids[i] = git_object_id(va_arg(ap, const git_object *));
+ va_end(ap);
+
+ error = git_commit_create(
+ oid, repo, update_ref, author, committer, message,
+ git_object_id((git_object *)tree),
+ parent_count, oids);
+
+ free(oids);
+ return error;
+}
+
+int git_commit_create_o(
+ git_oid *oid,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message,
+ const git_tree *tree,
+ int parent_count,
+ const git_commit *parents[])
+{
+ int i, error;
+ const git_oid **oids;
+
+ oids = git__malloc(parent_count * sizeof(git_oid *));
+
+ for (i = 0; i < parent_count; ++i)
+ oids[i] = git_object_id((git_object *)parents[i]);
+
+ error = git_commit_create(
+ oid, repo, update_ref, author, committer, message,
+ git_object_id((git_object *)tree),
+ parent_count, oids);
+
+ free(oids);
+ return error;
+}
- if (commit->author == NULL)
- return GIT_EMISSINGOBJDATA;
+int git_commit_create(
+ git_oid *oid,
+ git_repository *repo,
+ const char *update_ref,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message,
+ const git_oid *tree_oid,
+ int parent_count,
+ const git_oid *parents[])
+{
+ size_t final_size = 0;
+ int message_length, author_length, committer_length;
+
+ char *author_str, *committer_str;
+
+ int error, i;
+ git_odb_stream *stream;
+
+ message_length = strlen(message);
+ author_length = git_signature__write(&author_str, "author", author);
+ committer_length = git_signature__write(&committer_str, "committer", committer);
+
+ if (author_length < 0 || committer_length < 0)
+ return GIT_ENOMEM;
+
+ final_size += GIT_OID_LINE_LENGTH("tree");
+ final_size += GIT_OID_LINE_LENGTH("parent") * parent_count;
+ final_size += author_length;
+ final_size += committer_length;
+ final_size += 1 + message_length;
+
+ if ((error = git_odb_open_wstream(&stream, repo->db, final_size, GIT_OBJ_COMMIT)) < GIT_SUCCESS)
+ return error;
+
+ git__write_oid(stream, "tree", tree_oid);
- git_signature__write(src, "author", commit->author);
+ for (i = 0; i < parent_count; ++i)
+ git__write_oid(stream, "parent", parents[i]);
- if (commit->committer == NULL)
- return GIT_EMISSINGOBJDATA;
+ stream->write(stream, author_str, author_length);
+ free(author_str);
- git_signature__write(src, "committer", commit->committer);
+ stream->write(stream, committer_str, committer_length);
+ free(committer_str);
- if (commit->message != NULL) {
- git__source_write(src, "\n", 1);
- git__source_write(src, commit->message, strlen(commit->message));
+
+ stream->write(stream, "\n", 1);
+ stream->write(stream, message, message_length);
+
+ error = stream->finalize_write(oid, stream);
+ stream->free(stream);
+
+ if (error == GIT_SUCCESS && update_ref != NULL) {
+ git_reference *head;
+
+ error = git_reference_lookup(&head, repo, update_ref);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ if (git_reference_type(head) == GIT_REF_SYMBOLIC) {
+ if ((error = git_reference_resolve(&head, head)) < GIT_SUCCESS)
+ return error;
+ }
+
+ error = git_reference_set_oid(head, oid);
}
- return GIT_SUCCESS;
+ return error;
}
int commit_parse_buffer(git_commit *commit, void *data, size_t len)
@@ -110,12 +243,7 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len)
git_oid parent_oid;
int error;
- /* first parse; the vector hasn't been initialized yet */
- if (commit->parent_oids.contents == NULL) {
- git_vector_init(&commit->parent_oids, 4, NULL);
- }
-
- clear_parents(commit);
+ git_vector_init(&commit->parent_oids, 4, NULL);
if ((error = git__parse_oid(&commit->tree_oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS)
return error;
@@ -134,17 +262,11 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len)
return GIT_ENOMEM;
}
- if (commit->author)
- git_signature_free(commit->author);
-
commit->author = git__malloc(sizeof(git_signature));
if ((error = git_signature__parse(commit->author, &buffer, buffer_end, "author ")) < GIT_SUCCESS)
return error;
/* Always parse the committer; we need the commit time */
- if (commit->committer)
- git_signature_free(commit->committer);
-
commit->committer = git__malloc(sizeof(git_signature));
if ((error = git_signature__parse(commit->committer, &buffer, buffer_end, "committer ")) < GIT_SUCCESS)
return error;
@@ -176,11 +298,10 @@ int commit_parse_buffer(git_commit *commit, void *data, size_t len)
return GIT_SUCCESS;
}
-int git_commit__parse(git_commit *commit)
+int git_commit__parse(git_commit *commit, git_odb_object *obj)
{
- assert(commit && commit->object.source.open);
- return commit_parse_buffer(commit,
- commit->object.source.raw.data, commit->object.source.raw.len);
+ assert(commit);
+ return commit_parse_buffer(commit, obj->raw.data, obj->raw.len);
}
#define GIT_COMMIT_GETTER(_rvalue, _name, _return) \
@@ -218,82 +339,3 @@ int git_commit_parent(git_commit **parent, git_commit *commit, unsigned int n)
}
-
-int git_commit_set_tree(git_commit *commit, git_tree *tree)
-{
- const git_oid *oid;
-
- assert(commit && tree);
-
- if ((oid = git_object_id((git_object *)tree)) == NULL)
- return GIT_EMISSINGOBJDATA;
-
- commit->object.modified = 1;
- git_oid_cpy(&commit->tree_oid, oid);
- return GIT_SUCCESS;
-}
-
-int git_commit_add_parent(git_commit *commit, git_commit *new_parent)
-{
- const git_oid *parent_oid;
- git_oid *new_oid;
- assert(commit && new_parent);
-
- if ((parent_oid = git_object_id((git_object *)new_parent)) == NULL)
- return GIT_EMISSINGOBJDATA;
-
- new_oid = git__malloc(sizeof(git_oid));
- if (new_oid == NULL)
- return GIT_ENOMEM;
-
- commit->object.modified = 1;
- git_oid_cpy(new_oid, parent_oid);
- return git_vector_insert(&commit->parent_oids, new_oid);
-}
-
-void git_commit_set_author(git_commit *commit, const git_signature *author_sig)
-{
- assert(commit && author_sig);
- commit->object.modified = 1;
-
- git_signature_free(commit->author);
- commit->author = git_signature_dup(author_sig);
-}
-
-void git_commit_set_committer(git_commit *commit, const git_signature *committer_sig)
-{
- assert(commit && committer_sig);
- commit->object.modified = 1;
-
- git_signature_free(commit->committer);
- commit->committer = git_signature_dup(committer_sig);
-}
-
-void git_commit_set_message(git_commit *commit, const char *message)
-{
- const char *line_end;
- size_t message_len;
-
- commit->object.modified = 1;
-
- if (commit->message)
- free(commit->message);
-
- if (commit->message_short)
- free(commit->message_short);
-
- commit->message = git__strdup(message);
-
- /* Short message */
- if((line_end = strchr(message, '\n')) == NULL) {
- commit->message_short = git__strdup(message);
- return;
- }
-
- message_len = line_end - message;
-
- commit->message_short = git__malloc(message_len + 1);
- memcpy(commit->message_short, message, message_len);
- commit->message_short[message_len] = 0;
-}
-
diff --git a/src/commit.h b/src/commit.h
index aaf349c..3d15c50 100644
--- a/src/commit.h
+++ b/src/commit.h
@@ -22,8 +22,6 @@ struct git_commit {
};
void git_commit__free(git_commit *c);
-int git_commit__parse(git_commit *commit);
-
-int git_commit__writeback(git_commit *commit, git_odb_source *src);
+int git_commit__parse(git_commit *commit, git_odb_object *obj);
#endif
diff --git a/src/delta-apply.h b/src/delta-apply.h
index 642442d..36c5cc6 100644
--- a/src/delta-apply.h
+++ b/src/delta-apply.h
@@ -1,6 +1,8 @@
#ifndef INCLUDE_delta_apply_h__
#define INCLUDE_delta_apply_h__
+#include "odb.h"
+
/**
* Apply a git binary delta to recover the original content.
*
diff --git a/src/errors.c b/src/errors.c
index 880163f..f6b9648 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -27,7 +27,8 @@ static struct {
{GIT_EPACKEDREFSCORRUPTED, "The pack-refs file is either corrupted of its format is not currently supported"},
{GIT_EINVALIDPATH, "The path is invalid" },
{GIT_EREVWALKOVER, "The revision walker is empty; there are no more commits left to iterate"},
- {GIT_EINVALIDREFSTATE, "The state of the reference is not valid"}
+ {GIT_EINVALIDREFSTATE, "The state of the reference is not valid"},
+ {GIT_ENOTIMPLEMENTED, "This feature has not been implemented yet"}
};
const char *git_strerror(int num)
diff --git a/src/filebuf.c b/src/filebuf.c
index 4fc4f14..607ad61 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -77,43 +77,81 @@ void git_filebuf_cleanup(git_filebuf *file)
if (file->fd >= 0)
gitfo_close(file->fd);
- if (gitfo_exists(file->path_lock) == GIT_SUCCESS)
+ if (file->path_lock && gitfo_exists(file->path_lock) == GIT_SUCCESS)
gitfo_unlink(file->path_lock);
if (file->digest)
git_hash_free_ctx(file->digest);
free(file->buffer);
+ free(file->z_buf);
-#ifdef GIT_FILEBUF_THREADS
- free(file->buffer_back);
-#endif
+ deflateEnd(&file->zs);
free(file->path_original);
free(file->path_lock);
}
-static int flush_buffer(git_filebuf *file)
+GIT_INLINE(int) flush_buffer(git_filebuf *file)
{
- int result = GIT_SUCCESS;
+ int result = file->write(file, file->buffer, file->buf_pos);
+ file->buf_pos = 0;
+ return result;
+}
- if (file->buf_pos > 0) {
- result = gitfo_write(file->fd, file->buffer, file->buf_pos);
- if (file->digest)
- git_hash_update(file->digest, file->buffer, file->buf_pos);
+static int write_normal(git_filebuf *file, const void *source, size_t len)
+{
+ int result = 0;
- file->buf_pos = 0;
+ if (len > 0) {
+ result = gitfo_write(file->fd, (void *)source, len);
+ if (file->digest)
+ git_hash_update(file->digest, source, len);
}
return result;
}
+static int write_deflate(git_filebuf *file, const void *source, size_t len)
+{
+ int result = Z_OK;
+ z_stream *zs = &file->zs;
+
+ if (len > 0 || file->flush_mode == Z_FINISH) {
+ zs->next_in = (void *)source;
+ zs->avail_in = len;
+
+ do {
+ int have;
+
+ zs->next_out = file->z_buf;
+ zs->avail_out = file->buf_size;
+
+ result = deflate(zs, file->flush_mode);
+ assert(result != Z_STREAM_ERROR);
+
+ have = file->buf_size - zs->avail_out;
+
+ if (gitfo_write(file->fd, file->z_buf, have) < GIT_SUCCESS)
+ return GIT_EOSERR;
+
+ } while (zs->avail_out == 0);
+
+ assert(zs->avail_in == 0);
+
+ if (file->digest)
+ git_hash_update(file->digest, source, len);
+ }
+
+ return GIT_SUCCESS;
+}
+
int git_filebuf_open(git_filebuf *file, const char *path, int flags)
{
int error;
size_t path_len;
- if (file == NULL || path == NULL)
+ if (file == NULL)
return GIT_ERROR;
memset(file, 0x0, sizeof(git_filebuf));
@@ -122,46 +160,93 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags)
file->buf_pos = 0;
file->fd = -1;
- path_len = strlen(path);
-
- file->path_original = git__strdup(path);
- if (file->path_original == NULL) {
+ /* Allocate the main cache buffer */
+ file->buffer = git__malloc(file->buf_size);
+ if (file->buffer == NULL){
error = GIT_ENOMEM;
goto cleanup;
}
- file->path_lock = git__malloc(path_len + GIT_FILELOCK_EXTLENGTH);
- if (file->path_lock == NULL) {
- error = GIT_ENOMEM;
- goto cleanup;
+ /* If we are hashing on-write, allocate a new hash context */
+ if (flags & GIT_FILEBUF_HASH_CONTENTS) {
+ if ((file->digest = git_hash_new_ctx()) == NULL) {
+ error = GIT_ENOMEM;
+ goto cleanup;
+ }
}
- memcpy(file->path_lock, file->path_original, path_len);
- memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH);
+ /* If we are deflating on-write, */
+ if (flags & GIT_FILEBUF_DEFLATE_CONTENTS) {
- file->buffer = git__malloc(file->buf_size);
- if (file->buffer == NULL){
- error = GIT_ENOMEM;
- goto cleanup;
- }
+ /* Initialize the ZLib stream */
+ if (deflateInit(&file->zs, Z_DEFAULT_COMPRESSION) != Z_OK) {
+ error = GIT_EZLIB;
+ goto cleanup;
+ }
-#ifdef GIT_FILEBUF_THREADS
- file->buffer_back = git__malloc(file->buf_size);
- if (file->buffer_back == NULL){
- error = GIT_ENOMEM;
- goto cleanup;
+ /* Allocate the Zlib cache buffer */
+ file->z_buf = git__malloc(file->buf_size);
+ if (file->z_buf == NULL){
+ error = GIT_ENOMEM;
+ goto cleanup;
+ }
+
+ /* Never flush */
+ file->flush_mode = Z_NO_FLUSH;
+ file->write = &write_deflate;
+ } else {
+ file->write = &write_normal;
}
-#endif
- if (flags & GIT_FILEBUF_HASH_CONTENTS) {
- if ((file->digest = git_hash_new_ctx()) == NULL) {
+ /* If we are writing to a temp file */
+ if (flags & GIT_FILEBUF_TEMPORARY) {
+ char tmp_path[GIT_PATH_MAX];
+
+ /* Open the file as temporary for locking */
+ file->fd = gitfo_creat_tmp(tmp_path, "_filebuf_");
+ if (file->fd < 0) {
+ error = GIT_EOSERR;
+ goto cleanup;
+ }
+
+ /* No original path */
+ file->path_original = NULL;
+ file->path_lock = git__strdup(tmp_path);
+
+ if (file->path_lock == NULL) {
error = GIT_ENOMEM;
goto cleanup;
}
- }
+ } else {
+ /* If the file is not temporary, make sure we have a path */
+ if (path == NULL) {
+ error = GIT_ERROR;
+ goto cleanup;
+ }
- if ((error = lock_file(file, flags)) < GIT_SUCCESS)
- goto cleanup;
+ path_len = strlen(path);
+
+ /* Save the original path of the file */
+ file->path_original = git__strdup(path);
+ if (file->path_original == NULL) {
+ error = GIT_ENOMEM;
+ goto cleanup;
+ }
+
+ /* create the locking path by appending ".lock" to the original */
+ file->path_lock = git__malloc(path_len + GIT_FILELOCK_EXTLENGTH);
+ if (file->path_lock == NULL) {
+ error = GIT_ENOMEM;
+ goto cleanup;
+ }
+
+ memcpy(file->path_lock, file->path_original, path_len);
+ memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH);
+
+ /* open the file for locking */
+ if ((error = lock_file(file, flags)) < GIT_SUCCESS)
+ goto cleanup;
+ }
return GIT_SUCCESS;
@@ -187,10 +272,25 @@ int git_filebuf_hash(git_oid *oid, git_filebuf *file)
return GIT_SUCCESS;
}
+int git_filebuf_commit_at(git_filebuf *file, const char *path)
+{
+ free(file->path_original);
+ file->path_original = git__strdup(path);
+ if (file->path_original == NULL)
+ return GIT_ENOMEM;
+
+ return git_filebuf_commit(file);
+}
+
int git_filebuf_commit(git_filebuf *file)
{
int error;
+ /* tmp file cannot be commited */
+ if (file->path_original == NULL)
+ return GIT_EOSERR;
+
+ file->flush_mode = Z_FINISH;
if ((error = flush_buffer(file)) < GIT_SUCCESS)
goto cleanup;
@@ -204,16 +304,16 @@ cleanup:
return error;
}
-GIT_INLINE(void) add_to_cache(git_filebuf *file, void *buf, size_t len)
+GIT_INLINE(void) add_to_cache(git_filebuf *file, const void *buf, size_t len)
{
memcpy(file->buffer + file->buf_pos, buf, len);
file->buf_pos += len;
}
-int git_filebuf_write(git_filebuf *file, void *buff, size_t len)
+int git_filebuf_write(git_filebuf *file, const void *buff, size_t len)
{
int error;
- unsigned char *buf = buff;
+ const unsigned char *buf = buff;
for (;;) {
size_t space_left = file->buf_size - file->buf_pos;
@@ -237,9 +337,9 @@ int git_filebuf_write(git_filebuf *file, void *buff, size_t len)
/* write too-large chunks immediately */
if (len > file->buf_size) {
- error = gitfo_write(file->fd, buf, len);
- if (file->digest)
- git_hash_update(file->digest, buf, len);
+ error = file->write(file, buf, len);
+ if (error < GIT_SUCCESS)
+ return error;
}
}
}
diff --git a/src/filebuf.h b/src/filebuf.h
index 9db615f..37cb367 100644
--- a/src/filebuf.h
+++ b/src/filebuf.h
@@ -3,14 +3,17 @@
#include "fileops.h"
#include "hash.h"
+#include "git2/zlib.h"
#ifdef GIT_THREADS
# define GIT_FILEBUF_THREADS
#endif
-#define GIT_FILEBUF_HASH_CONTENTS 0x1
-#define GIT_FILEBUF_APPEND 0x2
-#define GIT_FILEBUF_FORCE 0x4
+#define GIT_FILEBUF_HASH_CONTENTS (1 << 0)
+#define GIT_FILEBUF_APPEND (1 << 2)
+#define GIT_FILEBUF_FORCE (1 << 3)
+#define GIT_FILEBUF_TEMPORARY (1 << 4)
+#define GIT_FILEBUF_DEFLATE_CONTENTS (1 << 5)
#define GIT_FILELOCK_EXTENSION ".lock\0"
#define GIT_FILELOCK_EXTLENGTH 6
@@ -19,12 +22,16 @@ struct git_filebuf {
char *path_original;
char *path_lock;
+ int (*write)(struct git_filebuf *file,
+ const void *source, size_t len);
+
git_hash_ctx *digest;
unsigned char *buffer;
-#ifdef GIT_FILEBUF_THREADS
- unsigned char *buffer_back;
-#endif
+ unsigned char *z_buf;
+
+ z_stream zs;
+ int flush_mode;
size_t buf_size, buf_pos;
git_file fd;
@@ -32,12 +39,13 @@ struct git_filebuf {
typedef struct git_filebuf git_filebuf;
-int git_filebuf_write(git_filebuf *lock, void *buff, size_t len);
+int git_filebuf_write(git_filebuf *lock, const void *buff, size_t len);
int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len);
int git_filebuf_printf(git_filebuf *file, const char *format, ...);
int git_filebuf_open(git_filebuf *lock, const char *path, int flags);
int git_filebuf_commit(git_filebuf *lock);
+int git_filebuf_commit_at(git_filebuf *lock, const char *path);
void git_filebuf_cleanup(git_filebuf *lock);
int git_filebuf_hash(git_oid *oid, git_filebuf *file);
diff --git a/src/fileops.c b/src/fileops.c
index 76e689e..237a16a 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -2,13 +2,13 @@
#include "fileops.h"
#include <ctype.h>
-static int force_path(const char *to)
+int gitfo_mkdir_2file(const char *file_path)
{
const int mode = 0755; /* or 0777 ? */
int error = GIT_SUCCESS;
char target_folder_path[GIT_PATH_MAX];
- error = git__dirname_r(target_folder_path, sizeof(target_folder_path), to);
+ error = git__dirname_r(target_folder_path, sizeof(target_folder_path), file_path);
if (error < GIT_SUCCESS)
return error;
@@ -25,6 +25,87 @@ static int force_path(const char *to)
return GIT_SUCCESS;
}
+static int creat_tempfile(char *path_out, const char *tmp_dir, const char *filename)
+{
+ int fd;
+
+ git__joinpath(path_out, tmp_dir, filename);
+ strcat(path_out, "_git2_XXXXXX");
+
+#ifdef GIT_WIN32
+ /* FIXME: there may be race conditions when multi-threading
+ * with the library */
+ if (_mktemp_s(path_out, GIT_PATH_MAX) != 0)
+ return GIT_EOSERR;
+
+ fd = gitfo_creat(path_out, 0744);
+#else
+ fd = mkstemp(path_out);
+#endif
+
+ return fd >= 0 ? fd : GIT_EOSERR;
+}
+
+static const char *find_tmpdir(void)
+{
+ static int tmpdir_not_found = 0;
+ static char temp_dir[GIT_PATH_MAX];
+ static const char *env_vars[] = {
+ "TEMP", "TMP", "TMPDIR"
+ };
+
+ unsigned int i, j;
+ char test_file[GIT_PATH_MAX];
+
+ if (tmpdir_not_found)
+ return NULL;
+
+ if (temp_dir[0] != '\0')
+ return temp_dir;
+
+ for (i = 0; i < ARRAY_SIZE(env_vars); ++i) {
+ char *env_path;
+
+ env_path = getenv(env_vars[i]);
+ if (env_path == NULL)
+ continue;
+
+ strcpy(temp_dir, env_path);
+
+ /* Fix backslashes because Windows environment vars
+ * are probably fucked up */
+ for (j = 0; j < strlen(temp_dir); ++j)
+ if (temp_dir[j] == '\\')
+ temp_dir[j] = '/';
+
+ if (creat_tempfile(test_file, temp_dir, "writetest") >= 0) {
+ gitfo_unlink(test_file);
+ return temp_dir;
+ }
+ }
+
+ /* last resort: current folder. */
+ strcpy(temp_dir, "./");
+ if (creat_tempfile(test_file, temp_dir, "writetest") >= 0) {
+ gitfo_unlink(test_file);
+ return temp_dir;
+ }
+
+ tmpdir_not_found = 1;
+ return NULL;
+}
+
+int gitfo_creat_tmp(char *path_out, const char *filename)
+{
+ const char *tmp_dir;
+
+ tmp_dir = find_tmpdir();
+ if (tmp_dir == NULL)
+ return GIT_EOSERR;
+
+ return creat_tempfile(path_out, tmp_dir, filename);
+}
+
int gitfo_open(const char *path, int flags)
{
int fd = open(path, flags | O_BINARY);
@@ -39,7 +120,7 @@ int gitfo_creat(const char *path, int mode)
int gitfo_creat_force(const char *path, int mode)
{
- if (force_path(path) < GIT_SUCCESS)
+ if (gitfo_mkdir_2file(path) < GIT_SUCCESS)
return GIT_EOSERR;
return gitfo_creat(path, mode);
@@ -117,6 +198,7 @@ int gitfo_isdir(const char *path)
int gitfo_exists(const char *path)
{
+ assert(path);
return access(path, F_OK);
}
@@ -198,7 +280,7 @@ int gitfo_mv(const char *from, const char *to)
int gitfo_mv_force(const char *from, const char *to)
{
- if (force_path(to) < GIT_SUCCESS)
+ if (gitfo_mkdir_2file(to) < GIT_SUCCESS)
return GIT_EOSERR;
return gitfo_mv(from, to);
diff --git a/src/fileops.h b/src/fileops.h
index 5aa302b..bc636fc 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -58,8 +58,10 @@ extern int gitfo_exists(const char *path);
extern int gitfo_open(const char *path, int flags);
extern int gitfo_creat(const char *path, int mode);
extern int gitfo_creat_force(const char *path, int mode);
+extern int gitfo_creat_tmp(char *path_out, const char *filename);
extern int gitfo_isdir(const char *path);
extern int gitfo_mkdir_recurs(const char *path, int mode);
+extern int gitfo_mkdir_2file(const char *path);
#define gitfo_close(fd) close(fd)
extern int gitfo_read(git_file fd, void *buf, size_t cnt);
diff --git a/src/index.c b/src/index.c
index 95e56b7..6a355e1 100644
--- a/src/index.c
+++ b/src/index.c
@@ -324,7 +324,7 @@ int git_index_add(git_index *index, const char *rel_path, int stage)
entry.file_size = st.st_size;
/* write the blob to disk and get the oid */
- if ((error = git_blob_writefile(&entry.oid, index->repository, full_path)) < GIT_SUCCESS)
+ if ((error = git_blob_create_fromfile(&entry.oid, index->repository, rel_path)) < GIT_SUCCESS)
return error;
entry.flags |= (stage << GIT_IDXENTRY_STAGESHIFT);
diff --git a/src/object.c b/src/object.c
index c432c6d..0572663 100644
--- a/src/object.c
+++ b/src/object.c
@@ -66,153 +66,6 @@ static struct {
{ "REF_DELTA", 0, 0 }
};
-/*
- * Object source methods
- *
- * Abstract buffer methods that allow the writeback system
- * to prepare the contents of any git file in-memory before
- * writing them to disk.
- */
-static int source_resize(git_odb_source *src)
-{
- size_t write_offset, new_size;
- void *new_data;
-
- write_offset = (size_t)((char *)src->write_ptr - (char *)src->raw.data);
-
- new_size = src->raw.len * 2;
- if ((new_data = git__malloc(new_size)) == NULL)
- return GIT_ENOMEM;
-
- memcpy(new_data, src->raw.data, src->written_bytes);
- free(src->raw.data);
-
- src->raw.data = new_data;
- src->raw.len = new_size;
- src->write_ptr = (char *)new_data + write_offset;
-
- return GIT_SUCCESS;
-}
-
-int git__source_printf(git_odb_source *source, const char *format, ...)
-{
- va_list arglist;
- int len;
-
- assert(source->open && source->write_ptr);
-
- va_start(arglist, format);
-
- len = vsnprintf(source->write_ptr, source->raw.len - source->written_bytes, format, arglist);
-
- while (source->written_bytes + len >= source->raw.len) {
- if (source_resize(source) < GIT_SUCCESS)
- return GIT_ENOMEM;
-
- len = vsnprintf(source->write_ptr, source->raw.len - source->written_bytes, format, arglist);
- }
-
- source->write_ptr = (char *)source->write_ptr + len;
- source->written_bytes += len;
-
- return GIT_SUCCESS;
-}
-
-int git__source_write(git_odb_source *source, const void *bytes, size_t len)
-{
- assert(source);
-
- assert(source->open && source->write_ptr);
-
- while (source->written_bytes + len >= source->raw.len) {
- if (source_resize(source) < GIT_SUCCESS)
- return GIT_ENOMEM;
- }
-
- memcpy(source->write_ptr, bytes, len);
- source->write_ptr = (char *)source->write_ptr + len;
- source->written_bytes += len;
-
- return GIT_SUCCESS;
-}
-
-static void prepare_write(git_object *object)
-{
- if (object->source.write_ptr != NULL || object->source.open)
- git_object__source_close(object);
-
- /* TODO: proper size calculation */
- object->source.raw.data = git__malloc(OBJECT_BASE_SIZE);
- object->source.raw.len = OBJECT_BASE_SIZE;
-
- object->source.write_ptr = object->source.raw.data;
- object->source.written_bytes = 0;
-
- object->source.open = 1;
-}
-
-static int write_back(git_object *object)
-{
- int error;
- git_oid new_id;
-
- assert(object);
-
- assert(object->source.open);
- assert(object->modified);
-
- object->source.raw.len = object->source.written_bytes;
-
- if ((error = git_odb_write(&new_id, object->repo->db, &object->source.raw)) < GIT_SUCCESS)
- return error;
-
- if (object->in_memory) {
- int idx = git_vector_search(&object->repo->memory_objects, object);
- git_vector_remove(&object->repo->memory_objects, idx);
- } else {
- git_hashtable_remove(object->repo->objects, &object->id);
- }
-
- git_oid_cpy(&object->id, &new_id);
- git_hashtable_insert(object->repo->objects, &object->id, object);
-
- object->source.write_ptr = NULL;
- object->source.written_bytes = 0;
-
- object->modified = 0;
- object->in_memory = 0;
-
- git_object__source_close(object);
- return GIT_SUCCESS;
-}
-
-int git_object__source_open(git_object *object)
-{
- int error;
-
- assert(object && !object->in_memory);
-
- if (object->source.open)
- git_object__source_close(object);
-
- error = git_odb_read(&object->source.raw, object->repo->db, &object->id);
- if (error < GIT_SUCCESS)
- return error;
-
- object->source.open = 1;
- return GIT_SUCCESS;
-}
-
-void git_object__source_close(git_object *object)
-{
- assert(object);
-
- if (object->source.open) {
- git_rawobj_close(&object->source.raw);
- object->source.open = 0;
- }
-}
-
static int create_object(git_object **object_out, git_otype type)
{
git_object *object = NULL;
@@ -225,43 +78,19 @@ static int create_object(git_object **object_out, git_otype type)
case GIT_OBJ_COMMIT:
case GIT_OBJ_TAG:
case GIT_OBJ_BLOB:
+ case GIT_OBJ_TREE:
object = git__malloc(git_object__size(type));
if (object == NULL)
return GIT_ENOMEM;
memset(object, 0x0, git_object__size(type));
break;
-
- case GIT_OBJ_TREE:
- object = (git_object *)git_tree__new();
- if (object == NULL)
- return GIT_ENOMEM;
- break;
default:
return GIT_EINVALIDTYPE;
}
- *object_out = object;
- return GIT_SUCCESS;
-}
-
-int git_object_new(git_object **object_out, git_repository *repo, git_otype type)
-{
- git_object *object = NULL;
- int error;
-
- assert(object_out && repo);
-
- if ((error = create_object(&object, type)) < GIT_SUCCESS)
- return error;
-
- object->repo = repo;
- object->in_memory = 1;
- object->modified = 1;
-
- object->source.raw.type = type;
+ object->type = type;
- object->lru = ++repo->lru_counter;
*object_out = object;
return GIT_SUCCESS;
}
@@ -269,126 +98,77 @@ int git_object_new(git_object **object_out, git_repository *repo, git_otype type
int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_otype type)
{
git_object *object = NULL;
- git_rawobj obj_file;
+ git_odb_object *odb_obj;
int error = GIT_SUCCESS;
assert(repo && object_out && id);
- object = git_hashtable_lookup(repo->objects, id);
+ object = git_cache_get(&repo->objects, id);
if (object != NULL) {
- if (type != GIT_OBJ_ANY && type != object->source.raw.type)
+ if (type != GIT_OBJ_ANY && type != object->type)
return GIT_EINVALIDTYPE;
*object_out = object;
- object->lru = ++repo->lru_counter;
- object->can_free = 0;
return GIT_SUCCESS;
}
- error = git_odb_read(&obj_file, repo->db, id);
+ error = git_odb_read(&odb_obj, repo->db, id);
if (error < GIT_SUCCESS)
return error;
- if (type != GIT_OBJ_ANY && type != obj_file.type) {
- git_rawobj_close(&obj_file);
+ if (type != GIT_OBJ_ANY && type != odb_obj->raw.type) {
+ git_odb_object_close(odb_obj);
return GIT_EINVALIDTYPE;
}
- type = obj_file.type;
+ type = odb_obj->raw.type;
if ((error = create_object(&object, type)) < GIT_SUCCESS)
return error;
/* Initialize parent object */
- git_oid_cpy(&object->id, id);
+ git_oid_cpy(&object->cached.oid, id);
object->repo = repo;
- memcpy(&object->source.raw, &obj_file, sizeof(git_rawobj));
- object->source.open = 1;
switch (type) {
case GIT_OBJ_COMMIT:
- error = git_commit__parse((git_commit *)object);
+ error = git_commit__parse((git_commit *)object, odb_obj);
break;
case GIT_OBJ_TREE:
- error = git_tree__parse((git_tree *)object);
+ error = git_tree__parse((git_tree *)object, odb_obj);
break;
case GIT_OBJ_TAG:
- error = git_tag__parse((git_tag *)object);
+ error = git_tag__parse((git_tag *)object, odb_obj);
break;
case GIT_OBJ_BLOB:
- error = git_blob__parse((git_blob *)object);
+ error = git_blob__parse((git_blob *)object, odb_obj);
break;
default:
break;
}
+ git_odb_object_close(odb_obj);
+
if (error < GIT_SUCCESS) {
git_object__free(object);
return error;
}
- git_object__source_close(object);
- git_hashtable_insert(repo->objects, &object->id, object);
-
- object->lru = ++repo->lru_counter;
- *object_out = object;
+ *object_out = git_cache_try_store(&repo->objects, object);
return GIT_SUCCESS;
}
-int git_object_write(git_object *object)
+void git_object__free(void *_obj)
{
- int error;
- git_odb_source *source;
+ git_object *object = (git_object *)_obj;
assert(object);
- if (object->modified == 0)
- return GIT_SUCCESS;
-
- prepare_write(object);
- source = &object->source;
-
- switch (source->raw.type) {
- case GIT_OBJ_COMMIT:
- error = git_commit__writeback((git_commit *)object, source);
- break;
-
- case GIT_OBJ_TREE:
- error = git_tree__writeback((git_tree *)object, source);
- break;
-
- case GIT_OBJ_TAG:
- error = git_tag__writeback((git_tag *)object, source);
- break;
-
- case GIT_OBJ_BLOB:
- error = git_blob__writeback((git_blob *)object, source);
- break;
-
- default:
- error = GIT_ERROR;
- break;
- }
-
- if (error < GIT_SUCCESS) {
- git_object__source_close(object);
- return error;
- }
-
- return write_back(object);
-}
-
-void git_object__free(git_object *object)
-{
- assert(object);
-
- git_object__source_close(object);
-
- switch (object->source.raw.type) {
+ switch (object->type) {
case GIT_OBJ_COMMIT:
git_commit__free((git_commit *)object);
break;
@@ -416,29 +196,19 @@ void git_object_close(git_object *object)
if (object == NULL)
return;
- if (object->in_memory) {
- int idx = git_vector_search(&object->repo->memory_objects, object);
- git_vector_remove(&object->repo->memory_objects, idx);
- git_object__free(object);
- } else {
- object->can_free = 1;
- }
+ git_cached_obj_decref((git_cached_obj *)object, git_object__free);
}
const git_oid *git_object_id(const git_object *obj)
{
assert(obj);
-
- if (obj->in_memory)
- return NULL;
-
- return &obj->id;
+ return &obj->cached.oid;
}
git_otype git_object_type(const git_object *obj)
{
assert(obj);
- return obj->source.raw.type;
+ return obj->type;
}
git_repository *git_object_owner(const git_object *obj)
diff --git a/src/odb.c b/src/odb.c
index 2013ac2..9aeaa8a 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -87,56 +87,49 @@ int git_odb__hash_obj(git_oid *id, char *hdr, size_t n, int *len, git_rawobj *ob
return GIT_SUCCESS;
}
-void git_rawobj_close(git_rawobj *obj)
-{
- free(obj->data);
- obj->data = NULL;
-}
-int git_rawobj_hash(git_oid *id, git_rawobj *obj)
+static git_odb_object *new_odb_object(const git_oid *oid, git_rawobj *source)
{
- char hdr[64];
- int hdrlen;
+ git_odb_object *object = git__malloc(sizeof(git_odb_object));
+ memset(object, 0x0, sizeof(git_odb_object));
- assert(id && obj);
+ git_oid_cpy(&object->cached.oid, oid);
+ memcpy(&object->raw, source, sizeof(git_rawobj));
- return git_odb__hash_obj(id, hdr, sizeof(hdr), &hdrlen, obj);
+ return object;
}
-int git_odb__inflate_buffer(void *in, size_t inlen, void *out, size_t outlen)
+static void free_odb_object(void *o)
{
- z_stream zs;
- int status = Z_OK;
-
- memset(&zs, 0x0, sizeof(zs));
-
- zs.next_out = out;
- zs.avail_out = outlen;
-
- zs.next_in = in;
- zs.avail_in = inlen;
+ git_odb_object *object = (git_odb_object *)o;
- if (inflateInit(&zs) < Z_OK)
- return GIT_ERROR;
+ if (object != NULL) {
+ free(object->raw.data);
+ free(object);
+ }
+}
- while (status == Z_OK)
- status = inflate(&zs, Z_FINISH);
+void git_odb_object_close(git_odb_object *object)
+{
+ git_cached_obj_decref((git_cached_obj *)object, &free_odb_object);
+}
- inflateEnd(&zs);
+int git_odb_hash(git_oid *id, const void *data, size_t len, git_otype type)
+{
+ char hdr[64];
+ int hdrlen;
+ git_rawobj raw;
- if ((status != Z_STREAM_END) /*|| (zs.avail_in != 0) */)
- return GIT_ERROR;
+ assert(id);
- if (zs.total_out != outlen)
- return GIT_ERROR;
+ raw.data = (void *)data;
+ raw.len = len;
+ raw.type = type;
- return GIT_SUCCESS;
+ return git_odb__hash_obj(id, hdr, sizeof(hdr), &hdrlen, &raw);
}
-
-
-
/***********************************************************
*
* OBJECT DATABASE PUBLIC API
@@ -162,6 +155,8 @@ int git_odb_new(git_odb **out)
if (!db)
return GIT_ENOMEM;
+ git_cache_init(&db->cache, GIT_DEFAULT_CACHE_SIZE, &free_odb_object);
+
if (git_vector_init(&db->backends, 4, backend_sort_cmp) < 0) {
free(db);
return GIT_ENOMEM;
@@ -306,16 +301,23 @@ void git_odb_close(git_odb *db)
}
git_vector_free(&db->backends);
+ git_cache_free(&db->cache);
free(db);
}
int git_odb_exists(git_odb *db, const git_oid *id)
{
+ git_odb_object *object;
unsigned int i;
int found = 0;
assert(db && id);
+ if ((object = git_cache_get(&db->cache, id)) != NULL) {
+ git_odb_object_close(object);
+ return 1;
+ }
+
for (i = 0; i < db->backends.length && !found; ++i) {
backend_internal *internal = git_vector_get(&db->backends, i);
git_odb_backend *b = internal->backend;
@@ -327,19 +329,27 @@ int git_odb_exists(git_odb *db, const git_oid *id)
return found;
}
-int git_odb_read_header(git_rawobj *out, git_odb *db, const git_oid *id)
+int git_odb_read_header(size_t *len_p, git_otype *type_p, git_odb *db, const git_oid *id)
{
unsigned int i;
int error = GIT_ENOTFOUND;
+ git_odb_object *object;
- assert(out && db && id);
+ assert(db && id);
+
+ if ((object = git_cache_get(&db->cache, id)) != NULL) {
+ *len_p = object->raw.len;
+ *type_p = object->raw.type;
+ git_odb_object_close(object);
+ return GIT_SUCCESS;
+ }
for (i = 0; i < db->backends.length && error < 0; ++i) {
backend_internal *internal = git_vector_get(&db->backends, i);
git_odb_backend *b = internal->backend;
if (b->read_header != NULL)
- error = b->read_header(out, b, id);
+ error = b->read_header(len_p, type_p, b, id);
}
/*
@@ -347,37 +357,50 @@ int git_odb_read_header(git_rawobj *out, git_odb *db, const git_oid *id)
* try reading the whole object and freeing the contents
*/
if (error < 0) {
- error = git_odb_read(out, db, id);
- git_rawobj_close(out);
+ if ((error = git_odb_read(&object, db, id)) < GIT_SUCCESS)
+ return error;
+
+ *len_p = object->raw.len;
+ *type_p = object->raw.len;
+ git_odb_object_close(object);
}
- return error;
+ return GIT_SUCCESS;
}
-int git_odb_read(git_rawobj *out, git_odb *db, const git_oid *id)
+int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
{
unsigned int i;
int error = GIT_ENOTFOUND;
+ git_rawobj raw;
assert(out && db && id);
+ *out = git_cache_get(&db->cache, id);
+ if (*out != NULL)
+ return GIT_SUCCESS;
+
for (i = 0; i < db->backends.length && error < 0; ++i) {
backend_internal *internal = git_vector_get(&db->backends, i);
git_odb_backend *b = internal->backend;
if (b->read != NULL)
- error = b->read(out, b, id);
+ error = b->read(&raw.data, &raw.len, &raw.type, b, id);
+ }
+
+ if (error == GIT_SUCCESS) {
+ *out = git_cache_try_store(&db->cache, new_odb_object(id, &raw));
}
return error;
}
-int git_odb_write(git_oid *id, git_odb *db, git_rawobj *obj)
+int git_odb_open_wstream(git_odb_stream **stream, git_odb *db, size_t size, git_otype type)
{
unsigned int i;
int error = GIT_ERROR;
- assert(obj && db && id);
+ assert(stream && db);
for (i = 0; i < db->backends.length && error < 0; ++i) {
backend_internal *internal = git_vector_get(&db->backends, i);
@@ -387,8 +410,26 @@ int git_odb_write(git_oid *id, git_odb *db, git_rawobj *obj)
if (internal->is_alternate)
continue;
- if (b->write != NULL)
- error = b->write(id, b, obj);
+ if (b->writestream != NULL)
+ error = b->writestream(stream, b, size, type);
+ }
+
+ return error;
+}
+
+int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid)
+{
+ unsigned int i;
+ int error = GIT_ERROR;
+
+ assert(stream && db);
+
+ for (i = 0; i < db->backends.length && error < 0; ++i) {
+ backend_internal *internal = git_vector_get(&db->backends, i);
+ git_odb_backend *b = internal->backend;
+
+ if (b->readstream != NULL)
+ error = b->readstream(stream, b, oid);
}
return error;
diff --git a/src/odb.h b/src/odb.h
index c3d0a17..f368583 100644
--- a/src/odb.h
+++ b/src/odb.h
@@ -3,15 +3,31 @@
#include "git2/odb.h"
#include "git2/oid.h"
+#include "git2/types.h"
#include "vector.h"
+#include "cache.h"
+/* DO NOT EXPORT */
+typedef struct {
+ void *data; /**< Raw, decompressed object data. */
+ size_t len; /**< Total number of bytes in data. */
+ git_otype type; /**< Type of this object. */
+} git_rawobj;
+
+/* EXPORT */
+struct git_odb_object {
+ git_cached_obj cached;
+ git_rawobj raw;
+};
+
+/* EXPORT */
struct git_odb {
void *_internal;
git_vector backends;
+ git_cache cache;
};
int git_odb__hash_obj(git_oid *id, char *hdr, size_t n, int *len, git_rawobj *obj);
-int git_odb__inflate_buffer(void *in, size_t inlen, void *out, size_t outlen);
#endif
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 4e2d9a6..4ab1128 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -30,14 +30,22 @@
#include "hash.h"
#include "odb.h"
#include "delta-apply.h"
+#include "filebuf.h"
#include "git2/odb_backend.h"
+#include "git2/types.h"
typedef struct { /* object header data */
git_otype type; /* object type */
size_t size; /* object size */
} obj_hdr;
+typedef struct {
+ git_odb_stream stream;
+ git_filebuf fbuf;
+ int finished;
+} loose_writestream;
+
typedef struct loose_backend {
git_odb_backend parent;
@@ -53,38 +61,6 @@ typedef struct loose_backend {
*
***********************************************************/
-static int make_temp_file(git_file *fd, char *tmp, size_t n, char *file)
-{
- char *template = "/tmp_obj_XXXXXX";
- size_t tmplen = strlen(template);
- int dirlen;
-
- if ((dirlen = git__dirname_r(tmp, n, file)) < 0)
- return GIT_ERROR;
-
- if ((dirlen + tmplen) >= n)
- return GIT_ERROR;
-
- strcpy(tmp + dirlen, (dirlen) ? template : template + 1);
-
- *fd = gitfo_mkstemp(tmp);
- if (*fd < 0 && dirlen) {
- /* create directory if it doesn't exist */
- tmp[dirlen] = '\0';
- if ((gitfo_exists(tmp) < 0) && gitfo_mkdir(tmp, 0755))
- return GIT_ERROR;
- /* try again */
- strcpy(tmp + dirlen, template);
- *fd = gitfo_mkstemp(tmp);
- }
- if (*fd < 0)
- return GIT_ERROR;
-
- return GIT_SUCCESS;
-}
-
-
-
static size_t object_file_name(char *name, size_t n, char *dir, const git_oid *id)
{
size_t len = strlen(dir);
@@ -236,72 +212,44 @@ static int finish_inflate(z_stream *s)
return GIT_SUCCESS;
}
-static int deflate_buf(z_stream *s, void *in, size_t len, int flush)
+static int is_zlib_compressed_data(unsigned char *data)
{
- int status = Z_OK;
+ unsigned int w;
- set_stream_input(s, in, len);
- while (status == Z_OK) {
- status = deflate(s, flush);
- if (s->avail_in == 0)
- break;
- }
- return status;
+ w = ((unsigned int)(data[0]) << 8) + data[1];
+ return data[0] == 0x78 && !(w % 31);
}
-static int deflate_obj(gitfo_buf *buf, char *hdr, int hdrlen, git_rawobj *obj, int level)
+static int inflate_buffer(void *in, size_t inlen, void *out, size_t outlen)
{
z_stream zs;
- int status;
- size_t size;
-
- assert(buf && !buf->data && hdr && obj);
- assert(level == Z_DEFAULT_COMPRESSION || (level >= 0 && level <= 9));
+ int status = Z_OK;
- buf->data = NULL;
- buf->len = 0;
- init_stream(&zs, NULL, 0);
+ memset(&zs, 0x0, sizeof(zs));
- if (deflateInit(&zs, level) < Z_OK)
- return GIT_ERROR;
+ zs.next_out = out;
+ zs.avail_out = outlen;
- size = deflateBound(&zs, hdrlen + obj->len);
+ zs.next_in = in;
+ zs.avail_in = inlen;
- if ((buf->data = git__malloc(size)) == NULL) {
- deflateEnd(&zs);
+ if (inflateInit(&zs) < Z_OK)
return GIT_ERROR;
- }
- set_stream_output(&zs, buf->data, size);
-
- /* compress the header */
- status = deflate_buf(&zs, hdr, hdrlen, Z_NO_FLUSH);
+ while (status == Z_OK)
+ status = inflate(&zs, Z_FINISH);
- /* if header compressed OK, compress the object */
- if (status == Z_OK)
- status = deflate_buf(&zs, obj->data, obj->len, Z_FINISH);
+ inflateEnd(&zs);
- if (status != Z_STREAM_END) {
- deflateEnd(&zs);
- free(buf->data);
- buf->data = NULL;
+ if ((status != Z_STREAM_END) /*|| (zs.avail_in != 0) */)
return GIT_ERROR;
- }
- buf->len = zs.total_out;
- deflateEnd(&zs);
+ if (zs.total_out != outlen)
+ return GIT_ERROR;
return GIT_SUCCESS;
}
-static int is_zlib_compressed_data(unsigned char *data)
-{
- unsigned int w;
-
- w = ((unsigned int)(data[0]) << 8) + data[1];
- return data[0] == 0x78 && !(w % 31);
-}
-
static void *inflate_tail(z_stream *s, void *hb, size_t used, obj_hdr *hdr)
{
unsigned char *buf, *head = hb;
@@ -371,7 +319,7 @@ static int inflate_packlike_loose_disk_obj(git_rawobj *out, gitfo_buf *obj)
in = ((unsigned char *)obj->data) + used;
len = obj->len - used;
- if (git_odb__inflate_buffer(in, len, buf, hdr.size)) {
+ if (inflate_buffer(in, len, buf, hdr.size)) {
free(buf);
return GIT_ERROR;
}
@@ -505,37 +453,6 @@ cleanup:
return error;
}
-static int write_obj(gitfo_buf *buf, git_oid *id, loose_backend *backend)
-{
- char file[GIT_PATH_MAX];
- char temp[GIT_PATH_MAX];
- git_file fd;
-
- if (object_file_name(file, sizeof(file), backend->objects_dir, id))
- return GIT_EOSERR;
-
- if (make_temp_file(&fd, temp, sizeof(temp), file) < 0)
- return GIT_EOSERR;
-
- if (gitfo_write(fd, buf->data, buf->len) < 0) {
- gitfo_close(fd);
- gitfo_unlink(temp);
- return GIT_EOSERR;
- }
-
- if (backend->fsync_object_files)
- gitfo_fsync(fd);
- gitfo_close(fd);
- gitfo_chmod(temp, 0444);
-
- if (gitfo_mv(temp, file) < 0) {
- gitfo_unlink(temp);
- return GIT_EOSERR;
- }
-
- return GIT_SUCCESS;
-}
-
static int locate_object(char *object_location, loose_backend *backend, const git_oid *oid)
{
object_file_name(object_location, GIT_PATH_MAX, backend->objects_dir, oid);
@@ -558,29 +475,44 @@ static int locate_object(char *object_location, loose_backend *backend, const gi
*
***********************************************************/
-int loose_backend__read_header(git_rawobj *obj, git_odb_backend *backend, const git_oid *oid)
+int loose_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid)
{
char object_path[GIT_PATH_MAX];
+ git_rawobj raw;
+ int error;
- assert(obj && backend && oid);
+ assert(backend && oid);
if (locate_object(object_path, (loose_backend *)backend, oid) < 0)
return GIT_ENOTFOUND;
- return read_header_loose(obj, object_path);
-}
+ if ((error = read_header_loose(&raw, object_path)) < GIT_SUCCESS)
+ return error;
+ *len_p = raw.len;
+ *type_p = raw.type;
+ return GIT_SUCCESS;
+}
-int loose_backend__read(git_rawobj *obj, git_odb_backend *backend, const git_oid *oid)
+int loose_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid)
{
char object_path[GIT_PATH_MAX];
+ git_rawobj raw;
+ int error;
- assert(obj && backend && oid);
+ assert(backend && oid);
if (locate_object(object_path, (loose_backend *)backend, oid) < 0)
return GIT_ENOTFOUND;
- return read_loose(obj, object_path);
+ if ((error = read_loose(&raw, object_path)) < GIT_SUCCESS)
+ return error;
+
+ *buffer_p = raw.data;
+ *len_p = raw.len;
+ *type_p = raw.type;
+
+ return GIT_SUCCESS;
}
int loose_backend__exists(git_odb_backend *backend, const git_oid *oid)
@@ -592,32 +524,104 @@ int loose_backend__exists(git_odb_backend *backend, const git_oid *oid)
return locate_object(object_path, (loose_backend *)backend, oid) == GIT_SUCCESS;
}
+int loose_backend__stream_fwrite(git_oid *oid, git_odb_stream *_stream)
+{
+ loose_writestream *stream = (loose_writestream *)_stream;
+ loose_backend *backend = (loose_backend *)_stream->backend;
+
+ int error;
+ char final_path[GIT_PATH_MAX];
+
+ if ((error = git_filebuf_hash(oid, &stream->fbuf)) < GIT_SUCCESS)
+ return error;
+
+ if (object_file_name(final_path, sizeof(final_path), backend->objects_dir, oid))
+ return GIT_ENOMEM;
-int loose_backend__write(git_oid *id, git_odb_backend *_backend, git_rawobj *obj)
+ if ((error = gitfo_mkdir_2file(final_path)) < GIT_SUCCESS)
+ return error;
+
+ stream->finished = 1;
+ return git_filebuf_commit_at(&stream->fbuf, final_path);
+}
+
+int loose_backend__stream_write(git_odb_stream *_stream, const char *data, size_t len)
{
+ loose_writestream *stream = (loose_writestream *)_stream;
+ return git_filebuf_write(&stream->fbuf, data, len);
+}
+
+void loose_backend__stream_free(git_odb_stream *_stream)
+{
+ loose_writestream *stream = (loose_writestream *)_stream;
+
+ if (!stream->finished)
+ git_filebuf_cleanup(&stream->fbuf);
+
+ free(stream);
+}
+
+static int format_object_header(char *hdr, size_t n, size_t obj_len, git_otype obj_type)
+{
+ const char *type_str = git_object_type2string(obj_type);
+ int len = snprintf(hdr, n, "%s %"PRIuZ, type_str, obj_len);
+
+ assert(len > 0); /* otherwise snprintf() is broken */
+ assert(((size_t) len) < n); /* otherwise the caller is broken! */
+
+ if (len < 0 || ((size_t) len) >= n)
+ return GIT_ERROR;
+ return len+1;
+}
+
+int loose_backend__stream(git_odb_stream **stream_out, git_odb_backend *_backend, size_t length, git_otype type)
+{
+ loose_backend *backend;
+ loose_writestream *stream;
+
char hdr[64];
int hdrlen;
- gitfo_buf buf = GITFO_BUF_INIT;
int error;
- loose_backend *backend;
- assert(id && _backend && obj);
+ assert(_backend);
backend = (loose_backend *)_backend;
+ *stream_out = NULL;
- if ((error = git_odb__hash_obj(id, hdr, sizeof(hdr), &hdrlen, obj)) < 0)
- return error;
+ hdrlen = format_object_header(hdr, sizeof(hdr), length, type);
+ if (hdrlen < GIT_SUCCESS)
+ return GIT_EOBJCORRUPTED;
+
+ stream = git__calloc(1, sizeof(loose_writestream));
+ if (stream == NULL)
+ return GIT_ENOMEM;
- if (git_odb_exists(_backend->odb, id))
- return GIT_SUCCESS;
+ stream->stream.backend = _backend;
+ stream->stream.read = NULL; /* read only */
+ stream->stream.write = &loose_backend__stream_write;
+ stream->stream.finalize_write = &loose_backend__stream_fwrite;
+ stream->stream.free = &loose_backend__stream_free;
+ stream->stream.mode = GIT_STREAM_WRONLY;
- if ((error = deflate_obj(&buf, hdr, hdrlen, obj, backend->object_zlib_level)) < 0)
+ error = git_filebuf_open(&stream->fbuf, NULL,
+ GIT_FILEBUF_HASH_CONTENTS |
+ GIT_FILEBUF_DEFLATE_CONTENTS |
+ GIT_FILEBUF_TEMPORARY);
+
+ if (error < GIT_SUCCESS) {
+ free(stream);
return error;
+ }
- error = write_obj(&buf, id, backend);
+ error = stream->stream.write((git_odb_stream *)stream, hdr, hdrlen);
+ if (error < GIT_SUCCESS) {
+ git_filebuf_cleanup(&stream->fbuf);
+ free(stream);
+ return error;
+ }
- gitfo_free_buf(&buf);
- return error;
+ *stream_out = (git_odb_stream *)stream;
+ return GIT_SUCCESS;
}
void loose_backend__free(git_odb_backend *_backend)
@@ -649,7 +653,7 @@ int git_odb_backend_loose(git_odb_backend **backend_out, const char *objects_dir
backend->parent.read = &loose_backend__read;
backend->parent.read_header = &loose_backend__read_header;
- backend->parent.write = &loose_backend__write;
+ backend->parent.writestream = &loose_backend__stream;
backend->parent.exists = &loose_backend__exists;
backend->parent.free = &loose_backend__free;
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 3067179..65210f0 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -1243,7 +1243,7 @@ static int packfile_unpack_delta(
error = packfile_unpack_compressed(&delta, backend, p, w_curs, curpos, delta_size, delta_type);
if (error < GIT_SUCCESS) {
- git_rawobj_close(&base);
+ free(base.data);
return error;
}
@@ -1252,8 +1252,8 @@ static int packfile_unpack_delta(
base.data, base.len,
delta.data, delta.len);
- git_rawobj_close(&base);
- git_rawobj_close(&delta);
+ free(base.data);
+ free(delta.data);
/* TODO: we might want to cache this shit. eventually */
//add_delta_base_cache(p, base_offset, base, base_size, *type);
@@ -1337,15 +1337,23 @@ int pack_backend__read_header(git_rawobj *obj, git_odb_backend *backend, const g
}
*/
-int pack_backend__read(git_rawobj *obj, git_odb_backend *backend, const git_oid *oid)
+int pack_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid)
{
struct pack_entry e;
+ git_rawobj raw;
int error;
if ((error = pack_entry_find(&e, (struct pack_backend *)backend, oid)) < GIT_SUCCESS)
return error;
- return packfile_unpack(obj, (struct pack_backend *)backend, e.p, e.offset);
+ if ((error = packfile_unpack(&raw, (struct pack_backend *)backend, e.p, e.offset)) < GIT_SUCCESS)
+ return error;
+
+ *buffer_p = raw.data;
+ *len_p = raw.len;
+ *type_p = raw.type;
+
+ return GIT_SUCCESS;
}
int pack_backend__exists(git_odb_backend *backend, const git_oid *oid)
@@ -1397,7 +1405,6 @@ int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
backend->parent.read = &pack_backend__read;
backend->parent.read_header = NULL;
- backend->parent.write = NULL;
backend->parent.exists = &pack_backend__exists;
backend->parent.free = &pack_backend__free;
diff --git a/src/oid.c b/src/oid.c
index 81b7d60..eb167a6 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -143,14 +143,18 @@ int git__parse_oid(git_oid *oid, char **buffer_out,
return GIT_SUCCESS;
}
-int git__write_oid(git_odb_source *src, const char *header, const git_oid *oid)
+int git__write_oid(git_odb_stream *stream, const char *header, const git_oid *oid)
{
- char hex_oid[41];
+ char hex_oid[42];
- git_oid_fmt(hex_oid, oid);
- hex_oid[40] = 0;
+ git_oid_fmt(hex_oid + 1, oid);
- return git__source_printf(src, "%s %s\n", header, hex_oid);
+ hex_oid[0] = ' ';
+ hex_oid[41] = '\n';
+
+ stream->write(stream, header, strlen(header));
+ stream->write(stream, hex_oid, 42);
+ return GIT_SUCCESS;
}
void git_oid_mkraw(git_oid *out, const unsigned char *raw)
diff --git a/src/repository.c b/src/repository.c
index 37aa447..1329694 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -40,30 +40,12 @@
#define GIT_BRANCH_MASTER "master"
-static const int OBJECT_TABLE_SIZE = 32;
-
typedef struct {
char *path_repository;
unsigned is_bare:1, has_been_reinit:1;
} repo_init;
/*
- * Hash table methods
- *
- * Callbacks for the ODB cache, implemented
- * as a hash table
- */
-static uint32_t object_table_hash(const void *key, int hash_id)
-{
- uint32_t r;
- git_oid *id;
-
- id = (git_oid *)key;
- memcpy(&r, id->id + (hash_id * sizeof(uint32_t)), sizeof(r));
- return r;
-}
-
-/*
* Git repository open methods
*
* Open a repository object from its path
@@ -186,25 +168,9 @@ static git_repository *repository_alloc()
memset(repo, 0x0, sizeof(git_repository));
- repo->objects = git_hashtable_alloc(
- OBJECT_TABLE_SIZE,
- object_table_hash,
- (git_hash_keyeq_ptr)git_oid_cmp);
-
- if (repo->objects == NULL) {
- free(repo);
- return NULL;
- }
+ git_cache_init(&repo->objects, GIT_DEFAULT_CACHE_SIZE, &git_object__free);
if (git_repository__refcache_init(&repo->references) < GIT_SUCCESS) {
- git_hashtable_free(repo->objects);
- free(repo);
- return NULL;
- }
-
- if (git_vector_init(&repo->memory_objects, 16, NULL) < GIT_SUCCESS) {
- git_hashtable_free(repo->objects);
- git_repository__refcache_free(&repo->references);
free(repo);
return NULL;
}
@@ -330,51 +296,19 @@ cleanup:
return error;
}
-int git_repository_gc(git_repository *repo)
-{
- int collected = 0;
- git_object *object;
- const void *_unused;
-
- GIT_HASHTABLE_FOREACH(repo->objects, _unused, object,
- if (object->can_free) {
- git_object__free(object);
- collected++;
- }
- );
-
- return collected;
-}
-
void git_repository_free(git_repository *repo)
{
- git_object *object;
- const void *_unused;
- unsigned int i;
-
if (repo == NULL)
return;
- /* force free all the objects */
- GIT_HASHTABLE_FOREACH(repo->objects, _unused, object,
- git_object__free(object);
- );
-
- for (i = 0; i < repo->memory_objects.length; ++i) {
- object = git_vector_get(&repo->memory_objects, i);
- git_object__free(object);
- }
+ git_cache_free(&repo->objects);
+ git_repository__refcache_free(&repo->references);
free(repo->path_workdir);
free(repo->path_index);
free(repo->path_repository);
free(repo->path_odb);
- git_hashtable_free(repo->objects);
- git_vector_free(&repo->memory_objects);
-
- git_repository__refcache_free(&repo->references);
-
if (repo->db != NULL)
git_odb_close(repo->db);
diff --git a/src/repository.h b/src/repository.h
index 48b8dae..fef1c7d 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -9,6 +9,7 @@
#include "hashtable.h"
#include "index.h"
+#include "cache.h"
#include "refs.h"
#define DOT_GIT ".git"
@@ -16,28 +17,17 @@
#define GIT_OBJECTS_DIR "objects/"
#define GIT_INDEX_FILE "index"
-typedef struct {
- git_rawobj raw;
- void *write_ptr;
- size_t written_bytes;
- int open:1;
-} git_odb_source;
-
struct git_object {
- git_oid id;
+ git_cached_obj cached;
git_repository *repo;
- git_odb_source source;
- unsigned int lru;
- unsigned char in_memory, modified, can_free, _pad;
+ git_otype type;
};
struct git_repository {
git_odb *db;
git_index *index;
- git_hashtable *objects;
- git_vector memory_objects;
-
+ git_cache objects;
git_refcache references;
char *path_repository;
@@ -49,17 +39,11 @@ struct git_repository {
unsigned int lru_counter;
};
-int git_object__source_open(git_object *object);
-void git_object__source_close(git_object *object);
-
/* fully free the object; internal method, do not
* export */
-void git_object__free(git_object *object);
-
-int git__source_printf(git_odb_source *source, const char *format, ...);
-int git__source_write(git_odb_source *source, const void *bytes, size_t len);
+void git_object__free(void *object);
int git__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_end, const char *header);
-int git__write_oid(git_odb_source *src, const char *header, const git_oid *oid);
+int git__write_oid(git_odb_stream *src, const char *header, const git_oid *oid);
#endif
diff --git a/src/revwalk.c b/src/revwalk.c
index 3b7ad34..a9d4f87 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -25,6 +25,7 @@
#include "common.h"
#include "commit.h"
+#include "odb.h"
#include "hashtable.h"
#include "pqueue.h"
@@ -236,22 +237,22 @@ static int commit_quick_parse(git_revwalk *walk, commit_object *commit, git_rawo
static int commit_parse(git_revwalk *walk, commit_object *commit)
{
- git_rawobj data;
+ git_odb_object *obj;
int error;
if (commit->parsed)
return GIT_SUCCESS;
- if ((error = git_odb_read(&data, walk->repo->db, &commit->oid)) < GIT_SUCCESS)
+ if ((error = git_odb_read(&obj, walk->repo->db, &commit->oid)) < GIT_SUCCESS)
return error;
- if (data.type != GIT_OBJ_COMMIT) {
- git_rawobj_close(&data);
+ if (obj->raw.type != GIT_OBJ_COMMIT) {
+ git_odb_object_close(obj);
return GIT_EOBJTYPE;
}
- error = commit_quick_parse(walk, commit, &data);
- git_rawobj_close(&data);
+ error = commit_quick_parse(walk, commit, &obj->raw);
+ git_odb_object_close(obj);
return error;
}
diff --git a/src/signature.c b/src/signature.c
index 5c9f159..13816c3 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -46,13 +46,7 @@ git_signature *git_signature_new(const char *name, const char *email, time_t tim
goto cleanup;
p->name = git__strdup(name);
- if (p->name == NULL)
- goto cleanup;
-
p->email = git__strdup(email);
- if (p->email == NULL)
- goto cleanup;
-
p->when.time = time;
p->when.offset = offset;
@@ -179,10 +173,12 @@ int git_signature__parse(git_signature *sig, char **buffer_out,
return GIT_SUCCESS;
}
-int git_signature__write(git_odb_source *src, const char *header, const git_signature *sig)
+int git_signature__write(char **signature, const char *header, const git_signature *sig)
{
- char sign;
int offset, hours, mins;
+ char sig_buffer[2048];
+ int sig_buffer_len;
+ char sign;
offset = sig->when.offset;
sign = (sig->when.offset < 0) ? '-' : '+';
@@ -193,7 +189,16 @@ int git_signature__write(git_odb_source *src, const char *header, const git_sign
hours = offset / 60;
mins = offset % 60;
- return git__source_printf(src, "%s %s <%s> %u %c%02d%02d\n", header, sig->name, sig->email, (unsigned)sig->when.time, sign, hours, mins);
+ sig_buffer_len = snprintf(sig_buffer, sizeof(sig_buffer),
+ "%s %s <%s> %u %c%02d%02d\n",
+ header, sig->name, sig->email,
+ (unsigned)sig->when.time, sign, hours, mins);
+
+ if (sig_buffer_len < 0 || (size_t)sig_buffer_len > sizeof(sig_buffer))
+ return GIT_ENOMEM;
+
+ *signature = git__strdup(sig_buffer);
+ return sig_buffer_len;
}
diff --git a/src/signature.h b/src/signature.h
index ee212c2..3534cb2 100644
--- a/src/signature.h
+++ b/src/signature.h
@@ -7,6 +7,6 @@
#include <time.h>
int git_signature__parse(git_signature *sig, char **buffer_out, const char *buffer_end, const char *header);
-int git_signature__write(git_odb_source *src, const char *header, const git_signature *sig);
+int git_signature__write(char **signature, const char *header, const git_signature *sig);
#endif
diff --git a/src/tag.c b/src/tag.c
index 0489e7e..7baabab 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -56,21 +56,6 @@ const git_oid *git_tag_target_oid(git_tag *t)
return &t->target;
}
-int git_tag_set_target(git_tag *tag, git_object *target)
-{
- const git_oid *oid;
-
- assert(tag && target);
-
- if ((oid = git_object_id(target)) == NULL)
- return GIT_EMISSINGOBJDATA;
-
- tag->object.modified = 1;
- git_oid_cpy(&tag->target, oid);
- tag->type = git_object_type(target);
- return GIT_SUCCESS;
-}
-
git_otype git_tag_type(git_tag *t)
{
assert(t);
@@ -83,50 +68,17 @@ const char *git_tag_name(git_tag *t)
return t->tag_name;
}
-void git_tag_set_name(git_tag *tag, const char *name)
-{
- assert(tag && name);
-
- tag->object.modified = 1;
-
- if (tag->tag_name)
- free(tag->tag_name);
-
- tag->tag_name = git__strdup(name);
-}
-
const git_signature *git_tag_tagger(git_tag *t)
{
return t->tagger;
}
-void git_tag_set_tagger(git_tag *tag, const git_signature *tagger_sig)
-{
- assert(tag && tagger_sig);
- tag->object.modified = 1;
-
- git_signature_free(tag->tagger);
- tag->tagger = git_signature_dup(tagger_sig);
-}
-
const char *git_tag_message(git_tag *t)
{
assert(t);
return t->message;
}
-void git_tag_set_message(git_tag *tag, const char *message)
-{
- assert(tag && message);
-
- tag->object.modified = 1;
-
- if (tag->message)
- free(tag->message);
-
- tag->message = git__strdup(message);
-}
-
static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
{
static const char *tag_types[] = {
@@ -187,9 +139,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
buffer = search + 1;
- if (tag->tagger != NULL)
- git_signature_free(tag->tagger);
-
tag->tagger = git__malloc(sizeof(git_signature));
if ((error = git_signature__parse(tag->tagger, &buffer, buffer_end, "tagger ")) != 0)
@@ -197,9 +146,6 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
text_len = buffer_end - ++buffer;
- if (tag->message != NULL)
- free(tag->message);
-
tag->message = git__malloc(text_len + 1);
memcpy(tag->message, buffer, text_len);
tag->message[text_len] = '\0';
@@ -207,26 +153,90 @@ static int parse_tag_buffer(git_tag *tag, char *buffer, const char *buffer_end)
return GIT_SUCCESS;
}
-int git_tag__writeback(git_tag *tag, git_odb_source *src)
+int git_tag_create_o(
+ git_oid *oid,
+ git_repository *repo,
+ const char *tag_name,
+ const git_object *target,
+ const git_signature *tagger,
+ const char *message)
{
- if (tag->tag_name == NULL || tag->tagger == NULL)
- return GIT_EMISSINGOBJDATA;
+ return git_tag_create(
+ oid, repo, tag_name,
+ git_object_id(target),
+ git_object_type(target),
+ tagger, message);
+}
- git__write_oid(src, "object", &tag->target);
- git__source_printf(src, "type %s\n", git_object_type2string(tag->type));
- git__source_printf(src, "tag %s\n", tag->tag_name);
- git_signature__write(src, "tagger", tag->tagger);
+int git_tag_create(
+ git_oid *oid,
+ git_repository *repo,
+ const char *tag_name,
+ const git_oid *target,
+ git_otype target_type,
+ const git_signature *tagger,
+ const char *message)
+{
+ size_t final_size = 0;
+ git_odb_stream *stream;
- if (tag->message != NULL)
- git__source_printf(src, "\n%s", tag->message);
+ const char *type_str;
+ char *tagger_str;
- return GIT_SUCCESS;
+ int type_str_len, tag_name_len, tagger_str_len, message_len;
+ int error;
+
+
+ type_str = git_object_type2string(target_type);
+
+ tagger_str_len = git_signature__write(&tagger_str, "tagger", tagger);
+
+ type_str_len = strlen(type_str);
+ tag_name_len = strlen(tag_name);
+ message_len = strlen(message);
+
+ final_size += GIT_OID_LINE_LENGTH("object");
+ final_size += STRLEN("type ") + type_str_len + 1;
+ final_size += STRLEN("tag ") + tag_name_len + 1;
+ final_size += tagger_str_len;
+ final_size += 1 + message_len;
+
+ if ((error = git_odb_open_wstream(&stream, repo->db, final_size, GIT_OBJ_TAG)) < GIT_SUCCESS)
+ return error;
+
+ git__write_oid(stream, "object", target);
+
+ stream->write(stream, "type ", STRLEN("type "));
+ stream->write(stream, type_str, type_str_len);
+
+ stream->write(stream, "\ntag ", STRLEN("\ntag "));
+ stream->write(stream, tag_name, tag_name_len);
+ stream->write(stream, "\n", 1);
+
+ stream->write(stream, tagger_str, tagger_str_len);
+ free(tagger_str);
+
+ stream->write(stream, "\n", 1);
+ stream->write(stream, message, message_len);
+
+
+ error = stream->finalize_write(oid, stream);
+ stream->free(stream);
+
+ if (error == GIT_SUCCESS) {
+ char ref_name[512];
+ git_reference *new_ref;
+ git__joinpath(ref_name, GIT_REFS_TAGS_DIR, tag_name);
+ error = git_reference_create_oid(&new_ref, repo, ref_name, oid);
+ }
+
+ return error;
}
-int git_tag__parse(git_tag *tag)
+int git_tag__parse(git_tag *tag, git_odb_object *obj)
{
- assert(tag && tag->object.source.open);
- return parse_tag_buffer(tag, tag->object.source.raw.data, (char *)tag->object.source.raw.data + tag->object.source.raw.len);
+ assert(tag);
+ return parse_tag_buffer(tag, obj->raw.data, (char *)obj->raw.data + obj->raw.len);
}
diff --git a/src/tag.h b/src/tag.h
index a1782d0..eddf8fa 100644
--- a/src/tag.h
+++ b/src/tag.h
@@ -3,6 +3,7 @@
#include "git2/tag.h"
#include "repository.h"
+#include "odb.h"
struct git_tag {
git_object object;
@@ -16,7 +17,6 @@ struct git_tag {
};
void git_tag__free(git_tag *tag);
-int git_tag__parse(git_tag *tag);
-int git_tag__writeback(git_tag *tag, git_odb_source *src);
+int git_tag__parse(git_tag *tag, git_odb_object *obj);
#endif
diff --git a/src/thread-utils.h b/src/thread-utils.h
index 1cf0e34..e542639 100644
--- a/src/thread-utils.h
+++ b/src/thread-utils.h
@@ -8,7 +8,7 @@ typedef struct {
volatile int val;
} git_atomic;
-static inline void git_atomic_set(git_atomic *a, int val)
+GIT_INLINE(void) git_atomic_set(git_atomic *a, int val)
{
a->val = val;
}
@@ -36,7 +36,7 @@ static inline void git_atomic_set(git_atomic *a, int val)
#define git_cond_signal(c) (void)0 //pthread_cond_signal(c)
#define git_cond_broadcast(c) (void)0 //pthread_cond_broadcast(c)
-static inline int git_atomic_inc(git_atomic *a)
+GIT_INLINE(int) git_atomic_inc(git_atomic *a)
{
#ifdef __GNUC__
return __sync_add_and_fetch(&a->val, 1);
@@ -47,7 +47,7 @@ static inline int git_atomic_inc(git_atomic *a)
#endif
}
-static inline int git_atomic_dec(git_atomic *a)
+GIT_INLINE(int) git_atomic_dec(git_atomic *a)
{
#ifdef __GNUC__
return __sync_sub_and_fetch(&a->val, 1);
@@ -81,12 +81,12 @@ static inline int git_atomic_dec(git_atomic *a)
#define git_cond_signal(c) (void)0
#define git_cond_broadcast(c) (void)0
-static inline int git_atomic_inc(git_atomic *a)
+GIT_INLINE(int) git_atomic_inc(git_atomic *a)
{
return ++a->val;
}
-static inline int git_atomic_dec(git_atomic *a)
+GIT_INLINE(int) git_atomic_dec(git_atomic *a)
{
return --a->val;
}
diff --git a/src/tree.c b/src/tree.c
index 307d41b..31b286e 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -41,9 +41,11 @@ int entry_search_cmp(const void *key, const void *array_member)
return strcmp(filename, entry->filename);
}
+#if 0
static int valid_attributes(const int attributes) {
return attributes >= 0 && attributes <= MAX_FILEMODE;
}
+#endif
int entry_sort_cmp(const void *a, const void *b)
{
@@ -56,13 +58,10 @@ int entry_sort_cmp(const void *a, const void *b)
entry_b->attr & 040000);
}
-void git_tree_clear_entries(git_tree *tree)
+void git_tree__free(git_tree *tree)
{
unsigned int i;
- if (tree == NULL)
- return;
-
for (i = 0; i < tree->entries.length; ++i) {
git_tree_entry *e;
e = git_vector_get(&tree->entries, i);
@@ -71,32 +70,6 @@ void git_tree_clear_entries(git_tree *tree)
free(e);
}
- git_vector_clear(&tree->entries);
- tree->object.modified = 1;
-}
-
-
-git_tree *git_tree__new(void)
-{
- git_tree *tree;
-
- tree = git__malloc(sizeof(struct git_tree));
- if (tree == NULL)
- return NULL;
-
- memset(tree, 0x0, sizeof(struct git_tree));
-
- if (git_vector_init(&tree->entries, DEFAULT_TREE_SIZE, entry_sort_cmp) < GIT_SUCCESS) {
- free(tree);
- return NULL;
- }
-
- return tree;
-}
-
-void git_tree__free(git_tree *tree)
-{
- git_tree_clear_entries(tree);
git_vector_free(&tree->entries);
free(tree);
}
@@ -106,37 +79,6 @@ const git_oid *git_tree_id(git_tree *c)
return git_object_id((git_object *)c);
}
-int git_tree_entry_set_attributes(git_tree_entry *entry, unsigned int attr)
-{
- assert(entry && entry->owner);
-
- if (!valid_attributes(attr)) {
- return GIT_ERROR;
- }
-
- entry->attr = attr;
- entry->owner->object.modified = 1;
- return GIT_SUCCESS;
-}
-
-void git_tree_entry_set_name(git_tree_entry *entry, const char *name)
-{
- assert(entry && entry->owner);
-
- free(entry->filename);
- entry->filename = git__strdup(name);
- git_vector_sort(&entry->owner->entries);
- entry->owner->object.modified = 1;
-}
-
-void git_tree_entry_set_id(git_tree_entry *entry, const git_oid *oid)
-{
- assert(entry && entry->owner);
-
- git_oid_cpy(&entry->oid, oid);
- entry->owner->object.modified = 1;
-}
-
unsigned int git_tree_entry_attributes(git_tree_entry *entry)
{
return entry->attr;
@@ -154,15 +96,10 @@ const git_oid *git_tree_entry_id(git_tree_entry *entry)
return &entry->oid;
}
-int git_tree_entry_2object(git_object **object_out, git_tree_entry *entry)
+int git_tree_entry_2object(git_object **object_out, git_repository *repo, git_tree_entry *entry)
{
assert(entry && object_out);
- return git_object_lookup(object_out, entry->owner->object.repo, &entry->oid, GIT_OBJ_ANY);
-}
-
-static void sort_entries(git_tree *tree)
-{
- git_vector_sort(&tree->entries);
+ return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJ_ANY);
}
git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
@@ -171,8 +108,6 @@ git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
assert(tree && filename);
- sort_entries(tree);
-
idx = git_vector_bsearch2(&tree->entries, entry_search_cmp, filename);
if (idx == GIT_ENOTFOUND)
return NULL;
@@ -183,9 +118,6 @@ git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx)
{
assert(tree);
-
- sort_entries(tree);
-
return git_vector_get(&tree->entries, (unsigned int)idx);
}
@@ -195,107 +127,12 @@ size_t git_tree_entrycount(git_tree *tree)
return tree->entries.length;
}
-int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes)
-{
- git_tree_entry *entry;
-
- assert(tree && id && filename);
- if (!valid_attributes(attributes)) {
- return GIT_ERROR;
- }
-
- if ((entry = git__malloc(sizeof(git_tree_entry))) == NULL)
- return GIT_ENOMEM;
-
- memset(entry, 0x0, sizeof(git_tree_entry));
-
- entry->filename = git__strdup(filename);
- git_oid_cpy(&entry->oid, id);
- entry->attr = attributes;
- entry->owner = tree;
-
- if (git_vector_insert(&tree->entries, entry) < 0)
- return GIT_ENOMEM;
-
- if (entry_out != NULL)
- *entry_out = entry;
-
- tree->object.modified = 1;
- return GIT_SUCCESS;
-}
-
-int git_tree_remove_entry_byindex(git_tree *tree, int idx)
-{
- git_tree_entry *remove_ptr;
-
- assert(tree);
-
- sort_entries(tree);
-
- remove_ptr = git_vector_get(&tree->entries, (unsigned int)idx);
- if (remove_ptr == NULL)
- return GIT_ENOTFOUND;
-
- free(remove_ptr->filename);
- free(remove_ptr);
-
- tree->object.modified = 1;
-
- return git_vector_remove(&tree->entries, (unsigned int)idx);
-}
-
-int git_tree_remove_entry_byname(git_tree *tree, const char *filename)
-{
- int idx;
-
- assert(tree && filename);
-
- sort_entries(tree);
-
- idx = git_vector_bsearch2(&tree->entries, entry_search_cmp, filename);
- if (idx == GIT_ENOTFOUND)
- return GIT_ENOTFOUND;
-
- return git_tree_remove_entry_byindex(tree, idx);
-}
-
-int git_tree__writeback(git_tree *tree, git_odb_source *src)
-{
- size_t i;
- char filemode[MAX_FILEMODE_BYTES + 1 + 1];
-
- assert(tree && src);
-
- if (tree->entries.length == 0)
- return GIT_EMISSINGOBJDATA;
-
- sort_entries(tree);
-
- for (i = 0; i < tree->entries.length; ++i) {
- git_tree_entry *entry;
-
- entry = git_vector_get(&tree->entries, i);
-
- snprintf(filemode, sizeof(filemode), "%o ", entry->attr);
-
- git__source_write(src, filemode, strlen(filemode));
- git__source_write(src, entry->filename, strlen(entry->filename) + 1);
- git__source_write(src, entry->oid.id, GIT_OID_RAWSZ);
- }
-
- return GIT_SUCCESS;
-}
-
-
static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
{
- static const size_t avg_entry_size = 40;
- unsigned int expected_size;
int error = GIT_SUCCESS;
- expected_size = (tree->object.source.raw.len / avg_entry_size) + 1;
-
- git_tree_clear_entries(tree);
+ if (git_vector_init(&tree->entries, DEFAULT_TREE_SIZE, entry_sort_cmp) < GIT_SUCCESS)
+ return GIT_ENOMEM;
while (buffer < buffer_end) {
git_tree_entry *entry;
@@ -309,7 +146,6 @@ static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
if (git_vector_insert(&tree->entries, entry) < GIT_SUCCESS)
return GIT_ENOMEM;
- entry->owner = tree;
entry->attr = strtol(buffer, &buffer, 8);
if (*buffer++ != ' ') {
@@ -336,16 +172,9 @@ static int tree_parse_buffer(git_tree *tree, char *buffer, char *buffer_end)
return error;
}
-int git_tree__parse(git_tree *tree)
+int git_tree__parse(git_tree *tree, git_odb_object *obj)
{
- char *buffer, *buffer_end;
-
- assert(tree && tree->object.source.open);
- assert(!tree->object.in_memory);
-
- buffer = tree->object.source.raw.data;
- buffer_end = buffer + tree->object.source.raw.len;
-
- return tree_parse_buffer(tree, buffer, buffer_end);
+ assert(tree);
+ return tree_parse_buffer(tree, (char *)obj->raw.data, (char *)obj->raw.data + obj->raw.len);
}
diff --git a/src/tree.h b/src/tree.h
index 78500c4..b4e910a 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -3,14 +3,13 @@
#include "git2/tree.h"
#include "repository.h"
+#include "odb.h"
#include "vector.h"
struct git_tree_entry {
unsigned int attr;
char *filename;
git_oid oid;
-
- git_tree *owner;
};
struct git_tree {
@@ -19,8 +18,6 @@ struct git_tree {
};
void git_tree__free(git_tree *tree);
-git_tree *git_tree__new(void);
-int git_tree__parse(git_tree *tree);
-int git_tree__writeback(git_tree *tree, git_odb_source *src);
+int git_tree__parse(git_tree *tree, git_odb_object *obj);
#endif
diff --git a/src/util.h b/src/util.h
index e0dfd7b..653b34d 100644
--- a/src/util.h
+++ b/src/util.h
@@ -97,6 +97,8 @@ extern char *git__strtok_keep(char *output, char *src, char *delimit);
#define STRLEN(str) (sizeof(str) - 1)
+#define GIT_OID_LINE_LENGTH(header) (STRLEN(header) + 1 + GIT_OID_HEXSZ + 1)
+
/*
* Realloc the buffer pointed at by variable 'x' so that it can hold
* at least 'nr' entries; the number of entries currently allocated
diff --git a/tests/t01-rawobj.c b/tests/t01-rawobj.c
index 3dfa3c9..5db9a79 100644
--- a/tests/t01-rawobj.c
+++ b/tests/t01-rawobj.c
@@ -23,10 +23,17 @@
* Boston, MA 02110-1301, USA.
*/
#include "test_lib.h"
-#include "t01-data.h"
+#include "odb.h"
#include "hash.h"
+#include "t01-data.h"
+
+static int hash_object(git_oid *oid, git_rawobj *obj)
+{
+ return git_odb_hash(oid, obj->data, obj->len, obj->type);
+}
+
BEGIN_TEST(oid0, "validate size of oid objects")
git_oid out;
must_be_true(20 == GIT_OID_RAWSZ);
@@ -497,28 +504,28 @@ BEGIN_TEST(objhash0, "hash junk data")
/* invalid types: */
junk_obj.data = some_data;
- must_fail(git_rawobj_hash(&id, &junk_obj));
+ must_fail(hash_object(&id, &junk_obj));
junk_obj.type = GIT_OBJ__EXT1;
- must_fail(git_rawobj_hash(&id, &junk_obj));
+ must_fail(hash_object(&id, &junk_obj));
junk_obj.type = GIT_OBJ__EXT2;
- must_fail(git_rawobj_hash(&id, &junk_obj));
+ must_fail(hash_object(&id, &junk_obj));
junk_obj.type = GIT_OBJ_OFS_DELTA;
- must_fail(git_rawobj_hash(&id, &junk_obj));
+ must_fail(hash_object(&id, &junk_obj));
junk_obj.type = GIT_OBJ_REF_DELTA;
- must_fail(git_rawobj_hash(&id, &junk_obj));
+ must_fail(hash_object(&id, &junk_obj));
/* data can be NULL only if len is zero: */
junk_obj.type = GIT_OBJ_BLOB;
junk_obj.data = NULL;
- must_pass(git_rawobj_hash(&id, &junk_obj));
+ must_pass(hash_object(&id, &junk_obj));
must_be_true(git_oid_cmp(&id, &id_zero) == 0);
junk_obj.len = 1;
- must_fail(git_rawobj_hash(&id, &junk_obj));
+ must_fail(hash_object(&id, &junk_obj));
END_TEST
BEGIN_TEST(objhash1, "hash a commit object")
@@ -526,7 +533,7 @@ BEGIN_TEST(objhash1, "hash a commit object")
must_pass(git_oid_mkstr(&id1, commit_id));
- must_pass(git_rawobj_hash(&id2, &commit_obj));
+ must_pass(hash_object(&id2, &commit_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST
@@ -536,7 +543,7 @@ BEGIN_TEST(objhash2, "hash a tree object")
must_pass(git_oid_mkstr(&id1, tree_id));
- must_pass(git_rawobj_hash(&id2, &tree_obj));
+ must_pass(hash_object(&id2, &tree_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST
@@ -546,7 +553,7 @@ BEGIN_TEST(objhash3, "hash a tag object")
must_pass(git_oid_mkstr(&id1, tag_id));
- must_pass(git_rawobj_hash(&id2, &tag_obj));
+ must_pass(hash_object(&id2, &tag_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST
@@ -556,7 +563,7 @@ BEGIN_TEST(objhash4, "hash a zero-length object")
must_pass(git_oid_mkstr(&id1, zero_id));
- must_pass(git_rawobj_hash(&id2, &zero_obj));
+ must_pass(hash_object(&id2, &zero_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST
@@ -566,7 +573,7 @@ BEGIN_TEST(objhash5, "hash an one-byte long object")
must_pass(git_oid_mkstr(&id1, one_id));
- must_pass(git_rawobj_hash(&id2, &one_obj));
+ must_pass(hash_object(&id2, &one_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST
@@ -576,7 +583,7 @@ BEGIN_TEST(objhash6, "hash a two-byte long object")
must_pass(git_oid_mkstr(&id1, two_id));
- must_pass(git_rawobj_hash(&id2, &two_obj));
+ must_pass(hash_object(&id2, &two_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST
@@ -586,7 +593,7 @@ BEGIN_TEST(objhash7, "hash an object several bytes long")
must_pass(git_oid_mkstr(&id1, some_id));
- must_pass(git_rawobj_hash(&id2, &some_obj));
+ must_pass(hash_object(&id2, &some_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
END_TEST
diff --git a/tests/t02-objread.c b/tests/t02-objread.c
index 2a9d130..85b03b0 100644
--- a/tests/t02-objread.c
+++ b/tests/t02-objread.c
@@ -24,6 +24,7 @@
*/
#include "test_lib.h"
#include "test_helpers.h"
+#include "odb.h"
#include "t02-data.h"
#include "t02-oids.h"
@@ -50,16 +51,16 @@ END_TEST
BEGIN_TEST(readloose0, "read a loose commit")
git_odb *db;
git_oid id;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(write_object_files(odb_dir, &commit));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, commit.id));
must_pass(git_odb_read(&obj, db, &id));
- must_pass(cmp_objects(&obj, &commit));
+ must_pass(cmp_objects((git_rawobj *)&obj->raw, &commit));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(odb_dir, &commit));
END_TEST
@@ -67,16 +68,16 @@ END_TEST
BEGIN_TEST(readloose1, "read a loose tree")
git_odb *db;
git_oid id;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(write_object_files(odb_dir, &tree));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, tree.id));
must_pass(git_odb_read(&obj, db, &id));
- must_pass(cmp_objects(&obj, &tree));
+ must_pass(cmp_objects((git_rawobj *)&obj->raw, &tree));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(odb_dir, &tree));
END_TEST
@@ -84,16 +85,16 @@ END_TEST
BEGIN_TEST(readloose2, "read a loose tag")
git_odb *db;
git_oid id;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(write_object_files(odb_dir, &tag));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, tag.id));
must_pass(git_odb_read(&obj, db, &id));
- must_pass(cmp_objects(&obj, &tag));
+ must_pass(cmp_objects((git_rawobj *)&obj->raw, &tag));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(odb_dir, &tag));
END_TEST
@@ -101,16 +102,16 @@ END_TEST
BEGIN_TEST(readloose3, "read a loose zero-bytes object")
git_odb *db;
git_oid id;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(write_object_files(odb_dir, &zero));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, zero.id));
must_pass(git_odb_read(&obj, db, &id));
- must_pass(cmp_objects(&obj, &zero));
+ must_pass(cmp_objects((git_rawobj *)&obj->raw, &zero));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(odb_dir, &zero));
END_TEST
@@ -118,16 +119,16 @@ END_TEST
BEGIN_TEST(readloose4, "read a one-byte long loose object")
git_odb *db;
git_oid id;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(write_object_files(odb_dir, &one));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, one.id));
must_pass(git_odb_read(&obj, db, &id));
- must_pass(cmp_objects(&obj, &one));
+ must_pass(cmp_objects(&obj->raw, &one));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(odb_dir, &one));
END_TEST
@@ -135,16 +136,16 @@ END_TEST
BEGIN_TEST(readloose5, "read a two-bytes long loose object")
git_odb *db;
git_oid id;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(write_object_files(odb_dir, &two));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, two.id));
must_pass(git_odb_read(&obj, db, &id));
- must_pass(cmp_objects(&obj, &two));
+ must_pass(cmp_objects(&obj->raw, &two));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(odb_dir, &two));
END_TEST
@@ -152,16 +153,16 @@ END_TEST
BEGIN_TEST(readloose6, "read a loose object which is several bytes long")
git_odb *db;
git_oid id;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(write_object_files(odb_dir, &some));
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id, some.id));
must_pass(git_odb_read(&obj, db, &id));
- must_pass(cmp_objects(&obj, &some));
+ must_pass(cmp_objects(&obj->raw, &some));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(odb_dir, &some));
END_TEST
@@ -174,13 +175,13 @@ BEGIN_TEST(readpack0, "read several packed objects")
for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) {
git_oid id;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(git_oid_mkstr(&id, packed_objects[i]));
must_be_true(git_odb_exists(db, &id) == 1);
must_pass(git_odb_read(&obj, db, &id));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
}
git_odb_close(db);
@@ -194,17 +195,19 @@ BEGIN_TEST(readheader0, "read only the header of several packed objects")
for (i = 0; i < ARRAY_SIZE(packed_objects); ++i) {
git_oid id;
- git_rawobj obj, header;
+ git_odb_object *obj;
+ size_t len;
+ git_otype type;
must_pass(git_oid_mkstr(&id, packed_objects[i]));
must_pass(git_odb_read(&obj, db, &id));
- must_pass(git_odb_read_header(&header, db, &id));
+ must_pass(git_odb_read_header(&len, &type, db, &id));
- must_be_true(obj.len == header.len);
- must_be_true(obj.type == header.type);
+ must_be_true(obj->raw.len == len);
+ must_be_true(obj->raw.type == type);
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
}
git_odb_close(db);
@@ -218,19 +221,21 @@ BEGIN_TEST(readheader1, "read only the header of several loose objects")
for (i = 0; i < ARRAY_SIZE(loose_objects); ++i) {
git_oid id;
- git_rawobj obj, header;
+ git_odb_object *obj;
+ size_t len;
+ git_otype type;
must_pass(git_oid_mkstr(&id, loose_objects[i]));
must_be_true(git_odb_exists(db, &id) == 1);
must_pass(git_odb_read(&obj, db, &id));
- must_pass(git_odb_read_header(&header, db, &id));
+ must_pass(git_odb_read_header(&len, &type, db, &id));
- must_be_true(obj.len == header.len);
- must_be_true(obj.type == header.type);
+ must_be_true(obj->raw.len == len);
+ must_be_true(obj->raw.type == type);
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
}
git_odb_close(db);
diff --git a/tests/t03-objwrite.c b/tests/t03-objwrite.c
index 10c6c7f..7738873 100644
--- a/tests/t03-objwrite.c
+++ b/tests/t03-objwrite.c
@@ -24,6 +24,7 @@
*/
#include "test_lib.h"
#include "fileops.h"
+#include "odb.h"
static char *odb_dir = "test-objects";
#include "t03-data.h"
@@ -80,23 +81,39 @@ static int remove_object_files(object_data *d)
return 0;
}
+static int streaming_write(git_oid *oid, git_odb *odb, git_rawobj *raw)
+{
+ git_odb_stream *stream;
+ int error;
+
+ if ((error = git_odb_open_wstream(&stream, odb, raw->len, raw->type)) < GIT_SUCCESS)
+ return error;
+
+ stream->write(stream, raw->data, raw->len);
+
+ error = stream->finalize_write(oid, stream);
+ stream->free(stream);
+
+ return error;
+}
+
BEGIN_TEST(write0, "write loose commit object")
git_odb *db;
git_oid id1, id2;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(make_odb_dir());
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id1, commit.id));
- must_pass(git_odb_write(&id2, db, &commit_obj));
+ must_pass(streaming_write(&id2, db, &commit_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
must_pass(check_object_files(&commit));
must_pass(git_odb_read(&obj, db, &id1));
- must_pass(cmp_objects(&obj, &commit_obj));
+ must_pass(cmp_objects(&obj->raw, &commit_obj));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(&commit));
END_TEST
@@ -104,20 +121,20 @@ END_TEST
BEGIN_TEST(write1, "write loose tree object")
git_odb *db;
git_oid id1, id2;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(make_odb_dir());
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id1, tree.id));
- must_pass(git_odb_write(&id2, db, &tree_obj));
+ must_pass(streaming_write(&id2, db, &tree_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
must_pass(check_object_files(&tree));
must_pass(git_odb_read(&obj, db, &id1));
- must_pass(cmp_objects(&obj, &tree_obj));
+ must_pass(cmp_objects(&obj->raw, &tree_obj));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(&tree));
END_TEST
@@ -125,20 +142,20 @@ END_TEST
BEGIN_TEST(write2, "write loose tag object")
git_odb *db;
git_oid id1, id2;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(make_odb_dir());
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id1, tag.id));
- must_pass(git_odb_write(&id2, db, &tag_obj));
+ must_pass(streaming_write(&id2, db, &tag_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
must_pass(check_object_files(&tag));
must_pass(git_odb_read(&obj, db, &id1));
- must_pass(cmp_objects(&obj, &tag_obj));
+ must_pass(cmp_objects(&obj->raw, &tag_obj));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(&tag));
END_TEST
@@ -146,20 +163,20 @@ END_TEST
BEGIN_TEST(write3, "write zero-length object")
git_odb *db;
git_oid id1, id2;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(make_odb_dir());
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id1, zero.id));
- must_pass(git_odb_write(&id2, db, &zero_obj));
+ must_pass(streaming_write(&id2, db, &zero_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
must_pass(check_object_files(&zero));
must_pass(git_odb_read(&obj, db, &id1));
- must_pass(cmp_objects(&obj, &zero_obj));
+ must_pass(cmp_objects(&obj->raw, &zero_obj));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(&zero));
END_TEST
@@ -167,20 +184,20 @@ END_TEST
BEGIN_TEST(write4, "write one-byte long object")
git_odb *db;
git_oid id1, id2;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(make_odb_dir());
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id1, one.id));
- must_pass(git_odb_write(&id2, db, &one_obj));
+ must_pass(streaming_write(&id2, db, &one_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
must_pass(check_object_files(&one));
must_pass(git_odb_read(&obj, db, &id1));
- must_pass(cmp_objects(&obj, &one_obj));
+ must_pass(cmp_objects(&obj->raw, &one_obj));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(&one));
END_TEST
@@ -188,20 +205,20 @@ END_TEST
BEGIN_TEST(write5, "write two-byte long object")
git_odb *db;
git_oid id1, id2;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(make_odb_dir());
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id1, two.id));
- must_pass(git_odb_write(&id2, db, &two_obj));
+ must_pass(streaming_write(&id2, db, &two_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
must_pass(check_object_files(&two));
must_pass(git_odb_read(&obj, db, &id1));
- must_pass(cmp_objects(&obj, &two_obj));
+ must_pass(cmp_objects(&obj->raw, &two_obj));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(&two));
END_TEST
@@ -209,20 +226,20 @@ END_TEST
BEGIN_TEST(write6, "write an object which is several bytes long")
git_odb *db;
git_oid id1, id2;
- git_rawobj obj;
+ git_odb_object *obj;
must_pass(make_odb_dir());
must_pass(git_odb_open(&db, odb_dir));
must_pass(git_oid_mkstr(&id1, some.id));
- must_pass(git_odb_write(&id2, db, &some_obj));
+ must_pass(streaming_write(&id2, db, &some_obj));
must_be_true(git_oid_cmp(&id1, &id2) == 0);
must_pass(check_object_files(&some));
must_pass(git_odb_read(&obj, db, &id1));
- must_pass(cmp_objects(&obj, &some_obj));
+ must_pass(cmp_objects(&obj->raw, &some_obj));
- git_rawobj_close(&obj);
+ git_odb_object_close(obj);
git_odb_close(db);
must_pass(remove_object_files(&some));
END_TEST
diff --git a/tests/t04-commit.c b/tests/t04-commit.c
index 855cf98..1140d33 100644
--- a/tests/t04-commit.c
+++ b/tests/t04-commit.c
@@ -407,39 +407,42 @@ This is a commit created in memory and it will be written back to disk\n"
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
+
BEGIN_TEST(write0, "write a new commit object from memory to disk")
git_repository *repo;
- git_commit *commit, *parent;
- git_tree *tree;
- git_oid id;
+ git_commit *commit;
+ git_oid tree_id, parent_id, commit_id;
const git_signature *author, *committer;
/* char hex_oid[41]; */
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
- /* Create commit in memory */
- must_pass(git_commit_new(&commit, repo));
-
- /* Add new parent */
- git_oid_mkstr(&id, commit_ids[4]);
- must_pass(git_commit_lookup(&parent, repo, &id));
- git_commit_add_parent(commit, parent);
+ git_oid_mkstr(&tree_id, tree_oid);
+ git_oid_mkstr(&parent_id, commit_ids[4]);
- /* Set other attributes */
+ /* create signatures */
committer = git_signature_new(COMMITTER_NAME, COMMITTER_EMAIL, 123456789, 60);
must_be_true(committer != NULL);
author = git_signature_new(COMMITTER_NAME, COMMITTER_EMAIL, 987654321, 90);
must_be_true(author != NULL);
- git_commit_set_committer(commit, committer);
- git_commit_set_author(commit, author);
- git_commit_set_message(commit, COMMIT_MESSAGE);
+ must_pass(git_commit_create_v(
+ &commit_id, /* out id */
+ repo,
+ NULL, /* do not update the HEAD */
+ author,
+ committer,
+ COMMIT_MESSAGE,
+ &tree_id,
+ 1, &parent_id));
git_signature_free((git_signature *)committer);
git_signature_free((git_signature *)author);
+ must_pass(git_commit_lookup(&commit, repo, &commit_id));
+
/* Check attributes were set correctly */
author = git_commit_author(commit);
must_be_true(author != NULL);
@@ -457,47 +460,6 @@ BEGIN_TEST(write0, "write a new commit object from memory to disk")
must_be_true(strcmp(git_commit_message(commit), COMMIT_MESSAGE) == 0);
- /* add new tree */
- git_oid_mkstr(&id, tree_oid);
- must_pass(git_tree_lookup(&tree, repo, &id));
-
- git_commit_set_tree(commit, tree);
-
- /* Test it has no OID */
- must_be_true(git_commit_id(commit) == NULL);
-
- /* Write to disk */
- must_pass(git_object_write((git_object *)commit));
-
- must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)commit));
-
- git_repository_free(repo);
-END_TEST
-
-BEGIN_TEST(write1, "load a commit object, modify it and write it back")
- git_repository *repo;
- git_oid id;
- git_commit *commit, *parent;
- const char *message;
- /* char hex_oid[41]; */
-
- must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
-
- git_oid_mkstr(&id, commit_ids[0]);
-
- must_pass(git_commit_lookup(&commit, repo, &id));
-
- message = git_commit_message(commit);
-
- git_commit_set_message(commit, "This is a new test message. Cool!\n");
-
- git_oid_mkstr(&id, commit_ids[4]);
- must_pass(git_commit_lookup(&parent, repo, &id));
-
- git_commit_add_parent(commit, parent);
-
- must_pass(git_object_write((git_object *)commit));
-
must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)commit));
git_repository_free(repo);
@@ -509,6 +471,7 @@ BEGIN_SUITE(commit)
ADD_TEST(parse1);
ADD_TEST(parse2);
ADD_TEST(details0);
+
ADD_TEST(write0);
- ADD_TEST(write1);
+ //ADD_TEST(write1);
END_SUITE
diff --git a/tests/t08-tag.c b/tests/t08-tag.c
index c678926..70eeb28 100644
--- a/tests/t08-tag.c
+++ b/tests/t08-tag.c
@@ -61,27 +61,62 @@ BEGIN_TEST(read0, "read and parse a tag from the repository")
git_repository_free(repo);
END_TEST
-BEGIN_TEST(write0, "write back a tag to the repository")
- git_oid id;
+
+#define TAGGER_NAME "Vicent Marti"
+#define TAGGER_EMAIL "vicent@github.com"
+#define TAGGER_MESSAGE "This is my tag.\n\nThere are many tags, but this one is mine\n"
+
+BEGIN_TEST(write0, "write a tag to the repository and read it again")
git_repository *repo;
git_tag *tag;
+ git_oid target_id, tag_id;
+ const git_signature *tagger;
+ git_reference *ref_tag;
+ /* char hex_oid[41]; */
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
- git_oid_mkstr(&id, tag1_id);
+ git_oid_mkstr(&target_id, tagged_commit);
+
+ /* create signatures */
+ tagger = git_signature_new(TAGGER_NAME, TAGGER_EMAIL, 123456789, 60);
+ must_be_true(tagger != NULL);
+
+ must_pass(git_tag_create(
+ &tag_id, /* out id */
+ repo,
+ "the-tag", /* do not update the HEAD */
+ &target_id,
+ GIT_OBJ_COMMIT,
+ tagger,
+ TAGGER_MESSAGE));
- must_pass(git_tag_lookup(&tag, repo, &id));
+ git_signature_free((git_signature *)tagger);
- git_tag_set_name(tag, "This is a different tag LOL");
+ must_pass(git_tag_lookup(&tag, repo, &tag_id));
+
+ /* Check attributes were set correctly */
+ tagger = git_tag_tagger(tag);
+ must_be_true(tagger != NULL);
+ must_be_true(strcmp(tagger->name, TAGGER_NAME) == 0);
+ must_be_true(strcmp(tagger->email, TAGGER_EMAIL) == 0);
+ must_be_true(tagger->when.time == 123456789);
+ must_be_true(tagger->when.offset == 60);
+
+ must_be_true(strcmp(git_tag_message(tag), TAGGER_MESSAGE) == 0);
+
+ must_pass(git_reference_lookup(&ref_tag, repo, "refs/tags/the-tag"));
+ must_be_true(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0);
+ must_pass(git_reference_delete(ref_tag));
- must_pass(git_object_write((git_object *)tag));
must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tag));
git_repository_free(repo);
+
END_TEST
BEGIN_SUITE(tag)
ADD_TEST(read0);
- ADD_TEST(write0);
+ ADD_TEST(write0);
END_SUITE
diff --git a/tests/t09-tree.c b/tests/t09-tree.c
index dfd266d..6c1b2e6 100644
--- a/tests/t09-tree.c
+++ b/tests/t09-tree.c
@@ -75,93 +75,15 @@ BEGIN_TEST(read1, "read a tree from the repository")
must_be_true(strcmp(git_tree_entry_name(entry), "README") == 0);
- must_pass(git_tree_entry_2object(&obj, entry));
+ must_pass(git_tree_entry_2object(&obj, repo, entry));
git_repository_free(repo);
END_TEST
-BEGIN_TEST(write0, "add a new entry to a tree and write it back to disk")
- const unsigned int entry_count = 128;
-
- git_repository *repo;
- git_tree *tree;
- unsigned int i;
- git_oid entry_id;
-
- must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
- must_pass(git_tree_new(&tree, repo));
-
- git_oid_mkstr(&entry_id, tree_oid);
- for (i = 0; i < entry_count; ++i) {
- char filename[32];
- git_tree_entry *ent = NULL;
-
- sprintf(filename, "file%d.txt", i);
- must_pass(git_tree_add_entry(&ent, tree, &entry_id, filename, 040000));
- must_be_true(ent != NULL);
- }
-
- must_be_true(git_tree_entrycount(tree) == entry_count);
- must_pass(git_object_write((git_object *)tree));
- must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tree));
-
- git_repository_free(repo);
-END_TEST
-
-BEGIN_TEST(write1, "add several entries in-memory and validate that they exist; write back to disk")
- git_oid id;
- git_repository *repo;
- git_tree *tree;
- git_tree_entry *entry;
- unsigned int i;
- /* char hex_oid[41]; */
-
- must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
-
- git_oid_mkstr(&id, tree_oid);
-
- must_pass(git_tree_lookup(&tree, repo, &id));
-
- must_be_true(git_tree_entrycount(tree) == 3);
-
- /* check there is NP if we don't want the
- * created entry back */
- git_tree_add_entry(NULL, tree, &id, "zzz_test_entry.dat", 0);
- git_tree_add_entry(NULL, tree, &id, "01_test_entry.txt", 0);
-
- must_be_true(git_tree_entrycount(tree) == 5);
-
- entry = git_tree_entry_byindex(tree, 0);
- must_be_true(strcmp(git_tree_entry_name(entry), "01_test_entry.txt") == 0);
-
- entry = git_tree_entry_byindex(tree, 4);
- must_be_true(strcmp(git_tree_entry_name(entry), "zzz_test_entry.dat") == 0);
-
- must_pass(git_tree_remove_entry_byname(tree, "README"));
- must_be_true(git_tree_entrycount(tree) == 4);
-
- for (i = 0; i < git_tree_entrycount(tree); ++i) {
- entry = git_tree_entry_byindex(tree, i);
- must_be_true(strcmp(git_tree_entry_name(entry), "README") != 0);
- }
-
- must_pass(git_object_write((git_object *)tree));
-
-/*
- git_oid_fmt(hex_oid, git_tree_id(tree));
- hex_oid[40] = 0;
- printf("TREE New SHA1: %s\n", hex_oid);
-*/
-
- must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tree));
- git_repository_free(repo);
-END_TEST
-
-
BEGIN_SUITE(tree)
ADD_TEST(read0);
ADD_TEST(read1);
- ADD_TEST(write0);
- ADD_TEST(write1);
+// ADD_TEST(write0); /* TODO THREADSAFE */
+// ADD_TEST(write1);
END_SUITE
diff --git a/tests/t11-sqlite.c b/tests/t11-sqlite.c
index ba9065e..fecf788 100644
--- a/tests/t11-sqlite.c
+++ b/tests/t11-sqlite.c
@@ -31,7 +31,7 @@
#include "git2/odb_backend.h"
-static int cmp_objects(git_rawobj *o1, git_rawobj *o2)
+static int cmp_objects(raw_object *o1, raw_object *o2)
{
if (o1->type != o2->type)
return -1;
@@ -62,7 +62,7 @@ static git_odb *open_sqlite_odb(void)
#define TEST_WRITE(PTR) {\
git_odb *db; \
git_oid id1, id2; \
- git_rawobj obj; \
+ raw_object obj; \
db = open_sqlite_odb(); \
must_be_true(db != NULL); \
must_pass(git_oid_mkstr(&id1, PTR.id)); \
diff --git a/tests/test_helpers.h b/tests/test_helpers.h
index 97b81ab..9a24ebc 100644
--- a/tests/test_helpers.h
+++ b/tests/test_helpers.h
@@ -29,6 +29,8 @@
#include "test_lib.h"
#include <git2.h>
+#include "odb.h"
+
#define TEST_REPOSITORY_NAME "testrepo.git"
#define REPOSITORY_FOLDER TEST_RESOURCES "/" TEST_REPOSITORY_NAME "/"
#define ODB_FOLDER (REPOSITORY_FOLDER "objects/")