Attribute file cache refactor This is a big refactoring of the attribute file cache to be a bit simpler which in turn makes it easier to enforce a lock around any updates to the cache so that it can be used in a threaded env. Tons of changes to the attributes and ignores code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 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
diff --git a/src/attr.c b/src/attr.c
index f52a8a9..c53a728 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -2,7 +2,7 @@
#include "repository.h"
#include "sysdir.h"
#include "config.h"
-#include "attr.h"
+#include "attr_file.h"
#include "ignore.h"
#include "git2/oid.h"
#include <ctype.h>
@@ -216,7 +216,6 @@ cleanup:
return error;
}
-
int git_attr_add_macro(
git_repository *repo,
const char *name,
@@ -251,261 +250,6 @@ int git_attr_add_macro(
return error;
}
-bool git_attr_cache__is_cached(
- git_repository *repo, git_attr_file_source source, const char *path)
-{
- git_buf cache_key = GIT_BUF_INIT;
- git_strmap *files = git_repository_attr_cache(repo)->files;
- const char *workdir = git_repository_workdir(repo);
- bool rval;
-
- if (workdir && git__prefixcmp(path, workdir) == 0)
- path += strlen(workdir);
- if (git_buf_printf(&cache_key, "%d#%s", (int)source, path) < 0)
- return false;
-
- rval = git_strmap_exists(files, git_buf_cstr(&cache_key));
-
- git_buf_free(&cache_key);
-
- return rval;
-}
-
-static int load_attr_file(
- const char **data,
- git_futils_filestamp *stamp,
- const char *filename)
-{
- int error;
- git_buf content = GIT_BUF_INIT;
-
- error = git_futils_filestamp_check(stamp, filename);
- if (error < 0)
- return error;
-
- /* if error == 0, then file is up to date. By returning GIT_ENOTFOUND,
- * we tell the caller not to reparse this file...
- */
- if (!error)
- return GIT_ENOTFOUND;
-
- error = git_futils_readbuffer(&content, filename);
- if (error < 0) {
- /* convert error into ENOTFOUND so failed permissions / invalid
- * file type don't actually stop the operation in progress.
- */
- return GIT_ENOTFOUND;
-
- /* TODO: once warnings are available, issue a warning callback */
- }
-
- *data = git_buf_detach(&content);
-
- return 0;
-}
-
-static int load_attr_blob_from_index(
- const char **content,
- git_blob **blob,
- git_repository *repo,
- const git_oid *old_oid,
- const char *relfile)
-{
- int error;
- size_t pos;
- git_index *index;
- const git_index_entry *entry;
-
- if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
- (error = git_index_find(&pos, index, relfile)) < 0)
- return error;
-
- entry = git_index_get_byindex(index, pos);
-
- if (old_oid && git_oid__cmp(old_oid, &entry->id) == 0)
- return GIT_ENOTFOUND;
-
- if ((error = git_blob_lookup(blob, repo, &entry->id)) < 0)
- return error;
-
- *content = git_blob_rawcontent(*blob);
- return 0;
-}
-
-static int load_attr_from_cache(
- git_attr_file **file,
- git_attr_cache *cache,
- git_attr_file_source source,
- const char *relative_path)
-{
- git_buf cache_key = GIT_BUF_INIT;
- khiter_t cache_pos;
-
- *file = NULL;
-
- if (!cache || !cache->files)
- return 0;
-
- if (git_buf_printf(&cache_key, "%d#%s", (int)source, relative_path) < 0)
- return -1;
-
- if (git_mutex_lock(&cache->lock) < 0) {
- giterr_set(GITERR_OS, "Could not get cache attr lock");
- git_buf_free(&cache_key);
- return -1;
- }
-
- cache_pos = git_strmap_lookup_index(cache->files, cache_key.ptr);
-
- if (git_strmap_valid_index(cache->files, cache_pos)) {
- *file = git_strmap_value_at(cache->files, cache_pos);
- GIT_REFCOUNT_INC(*file);
- }
-
- git_mutex_unlock(&cache->lock);
- git_buf_free(&cache_key);
-
- return 0;
-}
-
-int git_attr_cache__internal_file(
- git_repository *repo,
- const char *filename,
- git_attr_file **file)
-{
- int error = 0;
- git_attr_cache *cache = git_repository_attr_cache(repo);
- khiter_t cache_pos;
-
- if (git_mutex_lock(&cache->lock) < 0) {
- giterr_set(GITERR_OS, "Unable to get attr cache lock");
- return -1;
- }
-
- cache_pos = git_strmap_lookup_index(cache->files, filename);
-
- if (git_strmap_valid_index(cache->files, cache_pos)) {
- *file = git_strmap_value_at(cache->files, cache_pos);
- }
- else if (!(error = git_attr_file__new(file, 0, filename, &cache->pool))) {
-
- git_strmap_insert(cache->files, (*file)->key + 2, *file, error);
- if (error > 0)
- error = 0;
- }
-
- git_mutex_unlock(&cache->lock);
- return error;
-}
-
-int git_attr_cache__push_file(
- git_repository *repo,
- const char *base,
- const char *filename,
- git_attr_file_source source,
- git_attr_file_parser parse,
- void* parsedata,
- git_vector *stack)
-{
- int error = 0;
- git_buf path = GIT_BUF_INIT;
- const char *workdir = git_repository_workdir(repo);
- const char *relfile, *content = NULL;
- git_attr_cache *cache = git_repository_attr_cache(repo);
- git_attr_file *file = NULL;
- git_blob *blob = NULL;
- git_futils_filestamp stamp;
-
- assert(filename && stack);
-
- /* join base and path as needed */
- if (base != NULL && git_path_root(filename) < 0) {
- if (git_buf_joinpath(&path, base, filename) < 0)
- return -1;
- filename = path.ptr;
- }
-
- relfile = filename;
- if (workdir && git__prefixcmp(relfile, workdir) == 0)
- relfile += strlen(workdir);
-
- /* check cache */
- if (load_attr_from_cache(&file, cache, source, relfile) < 0)
- return -1;
-
- /* if not in cache, load data, parse, and cache */
-
- if (source == GIT_ATTR_FILE_FROM_FILE) {
- git_futils_filestamp_set(
- &stamp, file ? &file->cache_data.stamp : NULL);
-
- error = load_attr_file(&content, &stamp, filename);
- } else {
- error = load_attr_blob_from_index(&content, &blob,
- repo, file ? &file->cache_data.oid : NULL, relfile);
- }
-
- if (error) {
- /* not finding a file is not an error for this function */
- if (error == GIT_ENOTFOUND) {
- giterr_clear();
- error = 0;
- }
- goto finish;
- }
-
- /* if we got here, we have to parse and/or reparse the file */
- if (file)
- git_attr_file__clear_rules(file);
- else {
- error = git_attr_file__new(&file, source, relfile, &cache->pool);
- if (error < 0)
- goto finish;
- }
-
- if (parse && (error = parse(repo, parsedata, content, file)) < 0)
- goto finish;
-
- if (git_mutex_lock(&cache->lock) < 0) {
- giterr_set(GITERR_OS, "Unable to get attr cache lock");
- error = -1;
- } else {
- git_strmap_insert(cache->files, file->key, file, error); /* -V595 */
- if (error > 0) { /* > 0 means inserting for the first time */
- error = 0;
- GIT_REFCOUNT_INC(file);
- }
- git_mutex_unlock(&cache->lock);
- }
-
- /* remember "cache buster" file signature */
- if (blob)
- git_oid_cpy(&file->cache_data.oid, git_object_id((git_object *)blob));
- else
- git_futils_filestamp_set(&file->cache_data.stamp, &stamp);
-
-finish:
- /* push file onto vector if we found one*/
- if (!error && file != NULL)
- error = git_vector_insert(stack, file);
-
- if (error != 0)
- git_attr_file__free(file);
-
- if (blob)
- git_blob_free(blob);
- else
- git__free((void *)content);
-
- git_buf_free(&path);
-
- return error;
-}
-
-#define push_attr_file(R,S,B,F) \
- git_attr_cache__push_file \
- ((R),(B),(F),GIT_ATTR_FILE_FROM_FILE,git_attr_file__parse_buffer,NULL,(S))
-
typedef struct {
git_repository *repo;
uint32_t flags;
@@ -514,46 +258,64 @@ typedef struct {
git_vector *files;
} attr_walk_up_info;
-int git_attr_cache__decide_sources(
- uint32_t flags, bool has_wd, bool has_index, git_attr_file_source *srcs)
+static int attr_decide_sources(
+ uint32_t flags, bool has_wd, bool has_index, git_attr_cache_source *srcs)
{
int count = 0;
switch (flags & 0x03) {
case GIT_ATTR_CHECK_FILE_THEN_INDEX:
if (has_wd)
- srcs[count++] = GIT_ATTR_FILE_FROM_FILE;
+ srcs[count++] = GIT_ATTR_CACHE__FROM_FILE;
if (has_index)
- srcs[count++] = GIT_ATTR_FILE_FROM_INDEX;
+ srcs[count++] = GIT_ATTR_CACHE__FROM_INDEX;
break;
case GIT_ATTR_CHECK_INDEX_THEN_FILE:
if (has_index)
- srcs[count++] = GIT_ATTR_FILE_FROM_INDEX;
+ srcs[count++] = GIT_ATTR_CACHE__FROM_INDEX;
if (has_wd)
- srcs[count++] = GIT_ATTR_FILE_FROM_FILE;
+ srcs[count++] = GIT_ATTR_CACHE__FROM_FILE;
break;
case GIT_ATTR_CHECK_INDEX_ONLY:
if (has_index)
- srcs[count++] = GIT_ATTR_FILE_FROM_INDEX;
+ srcs[count++] = GIT_ATTR_CACHE__FROM_INDEX;
break;
}
return count;
}
+static int push_attr_file(
+ git_repository *repo,
+ git_vector *list,
+ git_attr_cache_source source,
+ const char *base,
+ const char *filename)
+{
+ int error = 0;
+ git_attr_file *file = NULL;
+
+ if ((error = git_attr_cache__get(
+ &file, repo, source, base, filename,
+ git_attr_file__parse_buffer, NULL)) < 0 ||
+ (error = git_vector_insert(list, file)) < 0)
+ git_attr_file__free(file);
+
+ return error;
+}
+
static int push_one_attr(void *ref, git_buf *path)
{
int error = 0, n_src, i;
attr_walk_up_info *info = (attr_walk_up_info *)ref;
- git_attr_file_source src[2];
+ git_attr_cache_source src[2];
- n_src = git_attr_cache__decide_sources(
+ n_src = attr_decide_sources(
info->flags, info->workdir != NULL, info->index != NULL, src);
for (i = 0; !error && i < n_src; ++i)
- error = git_attr_cache__push_file(
- info->repo, path->ptr, GIT_ATTR_FILE, src[i],
- git_attr_file__parse_buffer, NULL, info->files);
+ error = push_attr_file(
+ info->repo, info->files, src[i], path->ptr, GIT_ATTR_FILE);
return error;
}
@@ -601,7 +363,8 @@ static int collect_attr_files(
*/
error = push_attr_file(
- repo, files, git_repository_path(repo), GIT_ATTR_FILE_INREPO);
+ repo, files, GIT_ATTR_CACHE__FROM_FILE,
+ git_repository_path(repo), GIT_ATTR_FILE_INREPO);
if (error < 0)
goto cleanup;
@@ -618,7 +381,8 @@ static int collect_attr_files(
if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) {
error = push_attr_file(
- repo, files, NULL, git_repository_attr_cache(repo)->cfg_attr_file);
+ repo, files, GIT_ATTR_CACHE__FROM_FILE,
+ NULL, git_repository_attr_cache(repo)->cfg_attr_file);
if (error < 0)
goto cleanup;
}
@@ -626,7 +390,8 @@ static int collect_attr_files(
if ((flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
error = git_sysdir_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
if (!error)
- error = push_attr_file(repo, files, NULL, dir.ptr);
+ error = push_attr_file(
+ repo, files, GIT_ATTR_CACHE__FROM_FILE, NULL, dir.ptr);
else if (error == GIT_ENOTFOUND) {
giterr_clear();
error = 0;
@@ -640,172 +405,3 @@ static int collect_attr_files(
return error;
}
-
-static int attr_cache__lookup_path(
- char **out, git_config *cfg, const char *key, const char *fallback)
-{
- git_buf buf = GIT_BUF_INIT;
- int error;
- const git_config_entry *entry = NULL;
-
- *out = NULL;
-
- if ((error = git_config__lookup_entry(&entry, cfg, key, false)) < 0)
- return error;
-
- if (entry) {
- const char *cfgval = entry->value;
-
- /* expand leading ~/ as needed */
- if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' &&
- !git_sysdir_find_global_file(&buf, &cfgval[2]))
- *out = git_buf_detach(&buf);
- else if (cfgval)
- *out = git__strdup(cfgval);
-
- }
- else if (!git_sysdir_find_xdg_file(&buf, fallback))
- *out = git_buf_detach(&buf);
-
- git_buf_free(&buf);
-
- return error;
-}
-
-static void attr_cache__free(git_attr_cache *cache)
-{
- if (!cache)
- return;
-
- if (cache->files != NULL) {
- git_attr_file *file;
-
- git_strmap_foreach_value(cache->files, file, {
- git_attr_file__free(file);
- });
-
- git_strmap_free(cache->files);
- }
-
- if (cache->macros != NULL) {
- git_attr_rule *rule;
-
- git_strmap_foreach_value(cache->macros, rule, {
- git_attr_rule__free(rule);
- });
-
- git_strmap_free(cache->macros);
- }
-
- git_pool_clear(&cache->pool);
-
- git__free(cache->cfg_attr_file);
- cache->cfg_attr_file = NULL;
-
- git__free(cache->cfg_excl_file);
- cache->cfg_excl_file = NULL;
-
- git_mutex_free(&cache->lock);
-
- git__free(cache);
-}
-
-int git_attr_cache__init(git_repository *repo)
-{
- int ret = 0;
- git_attr_cache *cache = git_repository_attr_cache(repo);
- git_config *cfg;
-
- if (cache)
- return 0;
-
- if ((ret = git_repository_config__weakptr(&cfg, repo)) < 0)
- return ret;
-
- cache = git__calloc(1, sizeof(git_attr_cache));
- GITERR_CHECK_ALLOC(cache);
-
- /* set up lock */
- if (git_mutex_init(&cache->lock) < 0) {
- giterr_set(GITERR_OS, "Unable to initialize lock for attr cache");
- git__free(cache);
- return -1;
- }
-
- /* cache config settings for attributes and ignores */
- ret = attr_cache__lookup_path(
- &cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG, GIT_ATTR_FILE_XDG);
- if (ret < 0)
- goto cancel;
-
- ret = attr_cache__lookup_path(
- &cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG, GIT_IGNORE_FILE_XDG);
- if (ret < 0)
- goto cancel;
-
- /* allocate hashtable for attribute and ignore file contents,
- * hashtable for attribute macros, and string pool
- */
- if ((ret = git_strmap_alloc(&cache->files)) < 0 ||
- (ret = git_strmap_alloc(&cache->macros)) < 0 ||
- (ret = git_pool_init(&cache->pool, 1, 0)) < 0)
- goto cancel;
-
- cache = git__compare_and_swap(&repo->attrcache, NULL, cache);
- if (cache)
- goto cancel; /* raced with another thread, free this but no error */
-
- /* insert default macros */
- return git_attr_add_macro(repo, "binary", "-diff -crlf -text");
-
-cancel:
- attr_cache__free(cache);
- return ret;
-}
-
-void git_attr_cache_flush(git_repository *repo)
-{
- git_attr_cache *cache;
-
- /* this could be done less expensively, but for now, we'll just free
- * the entire attrcache and let the next use reinitialize it...
- */
- if (repo && (cache = git__swap(repo->attrcache, NULL)) != NULL)
- attr_cache__free(cache);
-}
-
-int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
-{
- git_attr_cache *cache = git_repository_attr_cache(repo);
- git_strmap *macros = cache->macros;
- int error;
-
- /* TODO: generate warning log if (macro->assigns.length == 0) */
- if (macro->assigns.length == 0)
- return 0;
-
- if (git_mutex_lock(&cache->lock) < 0) {
- giterr_set(GITERR_OS, "Unable to get attr cache lock");
- error = -1;
- } else {
- git_strmap_insert(macros, macro->match.pattern, macro, error);
- git_mutex_unlock(&cache->lock);
- }
-
- return (error < 0) ? -1 : 0;
-}
-
-git_attr_rule *git_attr_cache__lookup_macro(
- git_repository *repo, const char *name)
-{
- git_strmap *macros = git_repository_attr_cache(repo)->macros;
- khiter_t pos;
-
- pos = git_strmap_lookup_index(macros, name);
-
- if (!git_strmap_valid_index(macros, pos))
- return NULL;
-
- return (git_attr_rule *)git_strmap_value_at(macros, pos);
-}
-
diff --git a/src/attr.h b/src/attr.h
index 19c979b..f9f216d 100644
--- a/src/attr.h
+++ b/src/attr.h
@@ -8,38 +8,6 @@
#define INCLUDE_attr_h__
#include "attr_file.h"
-
-#define GIT_ATTR_CONFIG "core.attributesfile"
-#define GIT_IGNORE_CONFIG "core.excludesfile"
-
-typedef int (*git_attr_file_parser)(
- git_repository *, void *, const char *, git_attr_file *);
-
-extern int git_attr_cache__insert_macro(
- git_repository *repo, git_attr_rule *macro);
-
-extern git_attr_rule *git_attr_cache__lookup_macro(
- git_repository *repo, const char *name);
-
-extern int git_attr_cache__push_file(
- git_repository *repo,
- const char *base,
- const char *filename,
- git_attr_file_source source,
- git_attr_file_parser parse,
- void *parsedata, /* passed through to parse function */
- git_vector *stack);
-
-extern int git_attr_cache__internal_file(
- git_repository *repo,
- const char *key,
- git_attr_file **file_ptr);
-
-/* returns true if path is in cache */
-extern bool git_attr_cache__is_cached(
- git_repository *repo, git_attr_file_source source, const char *path);
-
-extern int git_attr_cache__decide_sources(
- uint32_t flags, bool has_wd, bool has_index, git_attr_file_source *srcs);
+#include "attrcache.h"
#endif
diff --git a/src/attr_file.c b/src/attr_file.c
index 695f661..86b3448 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -1,86 +1,173 @@
#include "common.h"
#include "repository.h"
#include "filebuf.h"
-#include "attr.h"
+#include "attr_file.h"
#include "git2/blob.h"
#include "git2/tree.h"
+#include "index.h"
#include <ctype.h>
-static int sort_by_hash_and_name(const void *a_raw, const void *b_raw);
-static void git_attr_rule__clear(git_attr_rule *rule);
-static bool parse_optimized_patterns(
- git_attr_fnmatch *spec,
- git_pool *pool,
- const char *pattern);
+static void attr_file_free(git_attr_file *file)
+{
+ git_attr_file__clear_rules(file);
+ git_pool_clear(&file->pool);
+ git__memzero(file, sizeof(*file));
+ git__free(file);
+}
int git_attr_file__new(
- git_attr_file **attrs_ptr,
- git_attr_file_source from,
- const char *path,
- git_pool *pool)
+ git_attr_file **out,
+ git_attr_cache_entry *ce,
+ git_attr_cache_source source)
{
- git_attr_file *attrs = NULL;
-
- attrs = git__calloc(1, sizeof(git_attr_file));
+ git_attr_file *attrs = git__calloc(1, sizeof(git_attr_file));
GITERR_CHECK_ALLOC(attrs);
- GIT_REFCOUNT_INC(attrs);
- if (pool)
- attrs->pool = pool;
- else {
- attrs->pool = git__calloc(1, sizeof(git_pool));
- if (!attrs->pool || git_pool_init(attrs->pool, 1, 0) < 0)
- goto fail;
- attrs->pool_is_allocated = true;
+ if (git_pool_init(&attrs->pool, 1, 0) < 0 ||
+ git_vector_init(&attrs->rules, 0, NULL) < 0)
+ {
+ attr_file_free(attrs);
+ return -1;
}
- if (path) {
- size_t len = strlen(path);
+ GIT_REFCOUNT_INC(attrs);
+ attrs->ce = ce;
+ attrs->source = source;
+ *out = attrs;
+ return 0;
+}
- attrs->key = git_pool_malloc(attrs->pool, (uint32_t)len + 3);
- GITERR_CHECK_ALLOC(attrs->key);
+void git_attr_file__clear_rules(git_attr_file *file)
+{
+ unsigned int i;
+ git_attr_rule *rule;
- attrs->key[0] = '0' + (char)from;
- attrs->key[1] = '#';
- memcpy(&attrs->key[2], path, len);
- attrs->key[len + 2] = '\0';
- }
+ git_vector_foreach(&file->rules, i, rule)
+ git_attr_rule__free(rule);
+ git_vector_free(&file->rules);
+}
+
+void git_attr_file__free(git_attr_file *file)
+{
+ if (!file)
+ return;
+ GIT_REFCOUNT_DEC(file, attr_file_free);
+}
+
+static int attr_file_oid_from_index(
+ git_oid *oid, git_repository *repo, const char *path)
+{
+ int error;
+ git_index *idx;
+ size_t pos;
+ const git_index_entry *entry;
- if (git_vector_init(&attrs->rules, 4, NULL) < 0)
- goto fail;
+ if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
+ (error = git_index__find_pos(&pos, idx, path, 0, 0)) < 0)
+ return error;
- *attrs_ptr = attrs;
+ if (!(entry = git_index_get_byindex(idx, pos)))
+ return GIT_ENOTFOUND;
+
+ *oid = entry->id;
return 0;
+}
+
+int git_attr_file__load(
+ git_attr_file **out,
+ git_repository *repo,
+ git_attr_cache_entry *ce,
+ git_attr_cache_source source,
+ git_attr_cache_parser parser,
+ void *payload)
+{
+ int error = 0;
+ git_blob *blob = NULL;
+ git_buf content = GIT_BUF_INIT;
+ const char *data = NULL;
+ git_attr_file *file;
-fail:
- git_attr_file__free(attrs);
- attrs_ptr = NULL;
- return -1;
+ *out = NULL;
+
+ if (source == GIT_ATTR_CACHE__FROM_INDEX) {
+ git_oid id;
+
+ if ((error = attr_file_oid_from_index(&id, repo, ce->path)) < 0 ||
+ (error = git_blob_lookup(&blob, repo, &id)) < 0)
+ return error;
+
+ data = git_blob_rawcontent(blob);
+ } else {
+ if ((error = git_futils_readbuffer(&content, ce->fullpath)) < 0)
+ /* always return ENOTFOUND so item will just be skipped */
+ /* TODO: issue a warning once warnings API is available */
+ return GIT_ENOTFOUND;
+ data = content.ptr;
+ }
+
+ if ((error = git_attr_file__new(&file, ce, source)) < 0)
+ goto cleanup;
+
+ if (parser && (error = parser(repo, file, data, payload)) < 0)
+ git_attr_file__free(file);
+ else
+ *out = file;
+
+cleanup:
+ git_blob_free(blob);
+ git_buf_free(&content);
+
+ return error;
+}
+
+int git_attr_file__out_of_date(git_repository *repo, git_attr_file *file)
+{
+ if (!file)
+ return 1;
+
+ if (file->source == GIT_ATTR_CACHE__FROM_INDEX) {
+ int error;
+ git_oid id;
+
+ if ((error = attr_file_oid_from_index(&id, repo, file->ce->path)) < 0)
+ return error;
+
+ return (git_oid__cmp(&file->cache_data.oid, &id) != 0);
+ }
+
+ return git_futils_filestamp_check(
+ &file->cache_data.stamp, file->ce->fullpath);
}
+static int sort_by_hash_and_name(const void *a_raw, const void *b_raw);
+static void git_attr_rule__clear(git_attr_rule *rule);
+static bool parse_optimized_patterns(
+ git_attr_fnmatch *spec,
+ git_pool *pool,
+ const char *pattern);
+
int git_attr_file__parse_buffer(
- git_repository *repo, void *parsedata, const char *buffer, git_attr_file *attrs)
+ git_repository *repo,
+ git_attr_file *attrs,
+ const char *data,
+ void *payload)
{
int error = 0;
- const char *scan = NULL, *context = NULL;
+ const char *scan = data, *context = NULL;
git_attr_rule *rule = NULL;
- GIT_UNUSED(parsedata);
-
- assert(buffer && attrs);
-
- scan = buffer;
+ GIT_UNUSED(payload);
/* if subdir file path, convert context for file paths */
- if (attrs->key &&
- git_path_root(attrs->key + 2) < 0 &&
- git__suffixcmp(attrs->key, "/" GIT_ATTR_FILE) == 0)
- context = attrs->key + 2;
+ if (attrs->ce &&
+ git_path_root(attrs->ce->path) < 0 &&
+ !git__suffixcmp(attrs->ce->path, "/" GIT_ATTR_FILE))
+ context = attrs->ce->path;
while (!error && *scan) {
/* allocate rule if needed */
if (!rule) {
- if (!(rule = git__calloc(1, sizeof(git_attr_rule)))) {
+ if (!(rule = git__calloc(1, sizeof(*rule)))) {
error = -1;
break;
}
@@ -90,9 +177,9 @@ int git_attr_file__parse_buffer(
/* parse the next "pattern attr attr attr" line */
if (!(error = git_attr_fnmatch__parse(
- &rule->match, attrs->pool, context, &scan)) &&
+ &rule->match, &attrs->pool, context, &scan)) &&
!(error = git_attr_assignment__parse(
- repo, attrs->pool, &rule->assigns, &scan)))
+ repo, &attrs->pool, &rule->assigns, &scan)))
{
if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO)
/* should generate error/warning if this is coming from any
@@ -118,61 +205,6 @@ int git_attr_file__parse_buffer(
return error;
}
-int git_attr_file__new_and_load(
- git_attr_file **attrs_ptr,
- const char *path)
-{
- int error;
- git_buf content = GIT_BUF_INIT;
-
- if ((error = git_attr_file__new(attrs_ptr, 0, path, NULL)) < 0)
- return error;
-
- if (!(error = git_futils_readbuffer(&content, path)))
- error = git_attr_file__parse_buffer(
- NULL, NULL, git_buf_cstr(&content), *attrs_ptr);
-
- git_buf_free(&content);
-
- if (error) {
- git_attr_file__free(*attrs_ptr);
- *attrs_ptr = NULL;
- }
-
- return error;
-}
-
-void git_attr_file__clear_rules(git_attr_file *file)
-{
- unsigned int i;
- git_attr_rule *rule;
-
- git_vector_foreach(&file->rules, i, rule)
- git_attr_rule__free(rule);
-
- git_vector_free(&file->rules);
-}
-
-static void attr_file_free(git_attr_file *file)
-{
- git_attr_file__clear_rules(file);
-
- if (file->pool_is_allocated) {
- git_pool_clear(file->pool);
- git__free(file->pool);
- }
- file->pool = NULL;
-
- git__free(file);
-}
-
-void git_attr_file__free(git_attr_file *file)
-{
- if (!file)
- return;
- GIT_REFCOUNT_DEC(file, attr_file_free);
-}
-
uint32_t git_attr_file__name_hash(const char *name)
{
uint32_t h = 5381;
@@ -183,7 +215,6 @@ uint32_t git_attr_file__name_hash(const char *name)
return h;
}
-
int git_attr_file__lookup_one(
git_attr_file *file,
const git_attr_path *path,
@@ -212,25 +243,64 @@ int git_attr_file__lookup_one(
return 0;
}
+int git_attr_file__load_standalone(
+ git_attr_file **out,
+ const char *path)
+{
+ int error;
+ git_attr_file *file;
+ git_buf content = GIT_BUF_INIT;
+
+ error = git_attr_file__new(&file, NULL, GIT_ATTR_CACHE__FROM_FILE);
+ if (error < 0)
+ return error;
+
+ error = git_attr_cache_entry__new(&file->ce, NULL, path, &file->pool);
+ if (error < 0) {
+ git_attr_file__free(file);
+ return error;
+ }
+ /* because the cache entry is allocated from the file's own pool, we
+ * don't have to free it - freeing file+pool will free cache entry, too.
+ */
+
+ if (!(error = git_futils_readbuffer(&content, path))) {
+ error = git_attr_file__parse_buffer(NULL, file, content.ptr, NULL);
+ git_buf_free(&content);
+ }
+
+ if (error < 0)
+ git_attr_file__free(file);
+ else
+ *out = file;
+
+ return error;
+}
bool git_attr_fnmatch__match(
git_attr_fnmatch *match,
const git_attr_path *path)
{
- int fnm;
- int icase_flags = (match->flags & GIT_ATTR_FNMATCH_ICASE) ? FNM_CASEFOLD : 0;
+ const char *filename;
+ int flags = 0;
- if (match->flags & GIT_ATTR_FNMATCH_DIRECTORY && !path->is_dir)
+ if ((match->flags & GIT_ATTR_FNMATCH_DIRECTORY) && !path->is_dir)
return false;
- if (match->flags & GIT_ATTR_FNMATCH_FULLPATH)
- fnm = p_fnmatch(match->pattern, path->path, FNM_PATHNAME | icase_flags);
- else if (path->is_dir)
- fnm = p_fnmatch(match->pattern, path->basename, FNM_LEADING_DIR | icase_flags);
- else
- fnm = p_fnmatch(match->pattern, path->basename, icase_flags);
+ if (match->flags & GIT_ATTR_FNMATCH_ICASE)
+ flags |= FNM_CASEFOLD;
- return (fnm == FNM_NOMATCH) ? false : true;
+ if (match->flags & GIT_ATTR_FNMATCH_FULLPATH) {
+ filename = path->path;
+ flags |= FNM_PATHNAME;
+ } else {
+ filename = path->basename;
+
+ if (path->is_dir)
+ flags |= FNM_LEADING_DIR;
+ }
+
+ return (p_fnmatch(match->pattern, filename, flags) != FNM_NOMATCH);
}
bool git_attr_rule__match(
@@ -245,7 +315,6 @@ bool git_attr_rule__match(
return matched;
}
-
git_attr_assignment *git_attr_rule__lookup_assignment(
git_attr_rule *rule, const char *name)
{
@@ -344,7 +413,7 @@ void git_attr_path__free(git_attr_path *info)
int git_attr_fnmatch__parse(
git_attr_fnmatch *spec,
git_pool *pool,
- const char *source,
+ const char *context,
const char **base)
{
const char *pattern, *scan;
@@ -412,21 +481,21 @@ int git_attr_fnmatch__parse(
}
if ((spec->flags & GIT_ATTR_FNMATCH_FULLPATH) != 0 &&
- source != NULL && git_path_root(pattern) < 0)
+ context != NULL && git_path_root(pattern) < 0)
{
- /* use context path minus the trailing filename */
- char *slash = strrchr(source, '/');
- size_t sourcelen = slash ? slash - source + 1 : 0;
+ /* use context path minus the trailing filename */
+ char *slash = strrchr(context, '/');
+ size_t contextlen = slash ? slash - context + 1 : 0;
/* given an unrooted fullpath match from a file inside a repo,
* prefix the pattern with the relative directory of the source file
*/
spec->pattern = git_pool_malloc(
- pool, (uint32_t)(sourcelen + spec->length + 1));
+ pool, (uint32_t)(contextlen + spec->length + 1));
if (spec->pattern) {
- memcpy(spec->pattern, source, sourcelen);
- memcpy(spec->pattern + sourcelen, pattern, spec->length);
- spec->length += sourcelen;
+ memcpy(spec->pattern, context, contextlen);
+ memcpy(spec->pattern + contextlen, pattern, spec->length);
+ spec->length += contextlen;
spec->pattern[spec->length] = '\0';
}
} else {
@@ -439,6 +508,7 @@ int git_attr_fnmatch__parse(
} else {
/* strip '\' that might have be used for internal whitespace */
spec->length = git__unescape(spec->pattern);
+ /* TODO: convert remaining '\' into '/' for POSIX ??? */
}
return 0;
diff --git a/src/attr_file.h b/src/attr_file.h
index dbd6696..f92ce3c 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -13,6 +13,7 @@
#include "pool.h"
#include "buffer.h"
#include "fileops.h"
+#include "attrcache.h"
#define GIT_ATTR_FILE ".gitattributes"
#define GIT_ATTR_FILE_INREPO "info/attributes"
@@ -45,10 +46,10 @@ typedef struct {
unsigned int flags;
} git_attr_fnmatch;
-typedef struct {
+struct git_attr_rule {
git_attr_fnmatch match;
git_vector assigns; /* vector of <git_attr_assignment*> */
-} git_attr_rule;
+};
typedef struct {
git_refcount unused;
@@ -63,17 +64,17 @@ typedef struct {
const char *value;
} git_attr_assignment;
-typedef struct {
+struct git_attr_file {
git_refcount rc;
- char *key; /* cache "source#path" this was loaded from */
- git_vector rules; /* vector of <rule*> or <fnmatch*> */
- git_pool *pool;
- bool pool_is_allocated;
+ git_attr_cache_entry *ce;
+ git_attr_cache_source source;
+ git_vector rules; /* vector of <rule*> or <fnmatch*> */
+ git_pool pool;
union {
git_oid oid;
git_futils_filestamp stamp;
} cache_data;
-} git_attr_file;
+};
typedef struct {
git_buf full;
@@ -82,29 +83,41 @@ typedef struct {
int is_dir;
} git_attr_path;
-typedef enum {
- GIT_ATTR_FILE_FROM_FILE = 0,
- GIT_ATTR_FILE_FROM_INDEX = 1
-} git_attr_file_source;
-
/*
* git_attr_file API
*/
-extern int git_attr_file__new(
- git_attr_file **attrs_ptr, git_attr_file_source src, const char *path, git_pool *pool);
+int git_attr_file__new(
+ git_attr_file **out,
+ git_attr_cache_entry *ce,
+ git_attr_cache_source source);
+
+void git_attr_file__free(git_attr_file *file);
+
+int git_attr_file__load(
+ git_attr_file **out,
+ git_repository *repo,
+ git_attr_cache_entry *ce,
+ git_attr_cache_source source,
+ git_attr_cache_parser parser,
+ void *payload);
-extern int git_attr_file__new_and_load(
- git_attr_file **attrs_ptr, const char *path);
+int git_attr_file__load_standalone(
+ git_attr_file **out,
+ const char *path);
-extern void git_attr_file__free(git_attr_file *file);
+int git_attr_file__out_of_date(
+ git_repository *repo, git_attr_file *file);
-extern void git_attr_file__clear_rules(git_attr_file *file);
+int git_attr_file__parse_buffer(
+ git_repository *repo,
+ git_attr_file *attrs,
+ const char *data,
+ void *payload);
-extern int git_attr_file__parse_buffer(
- git_repository *repo, void *parsedata, const char *buf, git_attr_file *file);
+void git_attr_file__clear_rules(git_attr_file *file);
-extern int git_attr_file__lookup_one(
+int git_attr_file__lookup_one(
git_attr_file *file,
const git_attr_path *path,
const char *attr,
@@ -115,7 +128,7 @@ extern int git_attr_file__lookup_one(
git_vector_rforeach(&(file)->rules, (iter), (rule)) \
if (git_attr_rule__match((rule), (path)))
-extern uint32_t git_attr_file__name_hash(const char *name);
+uint32_t git_attr_file__name_hash(const char *name);
/*
diff --git a/src/attrcache.c b/src/attrcache.c
new file mode 100644
index 0000000..6d09723
--- /dev/null
+++ b/src/attrcache.c
@@ -0,0 +1,397 @@
+#include "common.h"
+#include "repository.h"
+#include "attr_file.h"
+#include "config.h"
+#include "sysdir.h"
+#include "ignore.h"
+
+GIT__USE_STRMAP;
+
+GIT_INLINE(int) attr_cache_lock(git_attr_cache *cache)
+{
+ GIT_UNUSED(cache); /* avoid warning if threading is off */
+
+ if (git_mutex_lock(&cache->lock) < 0) {
+ giterr_set(GITERR_OS, "Unable to get attr cache lock");
+ return -1;
+ }
+ return 0;
+}
+
+GIT_INLINE(void) attr_cache_unlock(git_attr_cache *cache)
+{
+ GIT_UNUSED(cache); /* avoid warning if threading is off */
+ git_mutex_unlock(&cache->lock);
+}
+
+GIT_INLINE(git_attr_cache_entry *) attr_cache_lookup_entry(
+ git_attr_cache *cache, const char *path)
+{
+ khiter_t pos = git_strmap_lookup_index(cache->files, path);
+
+ if (git_strmap_valid_index(cache->files, pos))
+ return git_strmap_value_at(cache->files, pos);
+ else
+ return NULL;
+}
+
+int git_attr_cache_entry__new(
+ git_attr_cache_entry **out,
+ const char *base,
+ const char *path,
+ git_pool *pool)
+{
+ size_t baselen = base ? strlen(base) : 0, pathlen = strlen(path);
+ size_t cachesize = sizeof(git_attr_cache_entry) + baselen + pathlen + 1;
+ git_attr_cache_entry *ce;
+
+ ce = git_pool_mallocz(pool, cachesize);
+ GITERR_CHECK_ALLOC(ce);
+
+ if (baselen)
+ memcpy(ce->fullpath, base, baselen);
+ memcpy(&ce->fullpath[baselen], path, pathlen);
+ ce->path = &ce->fullpath[baselen];
+ *out = ce;
+
+ return 0;
+}
+
+/* call with attrcache locked */
+static int attr_cache_make_entry(
+ git_attr_cache_entry **out, git_repository *repo, const char *path)
+{
+ int error = 0;
+ git_attr_cache *cache = git_repository_attr_cache(repo);
+ git_attr_cache_entry *ce = NULL;
+
+ error = git_attr_cache_entry__new(
+ &ce, git_repository_workdir(repo), path, &cache->pool);
+
+ if (!error) {
+ git_strmap_insert(cache->files, ce->path, ce, error);
+ if (error > 0)
+ error = 0;
+ }
+
+ *out = ce;
+ return error;
+}
+
+/* insert entry or replace existing if we raced with another thread */
+static int attr_cache_upsert(git_attr_cache *cache, git_attr_file *file)
+{
+ git_attr_cache_entry *ce;
+ git_attr_file *old;
+
+ if (attr_cache_lock(cache) < 0)
+ return -1;
+
+ ce = attr_cache_lookup_entry(cache, file->ce->path);
+
+ old = ce->file[file->source];
+
+ GIT_REFCOUNT_OWN(file, ce);
+ GIT_REFCOUNT_INC(file);
+ ce->file[file->source] = file;
+
+ if (old) {
+ GIT_REFCOUNT_OWN(old, NULL);
+ git_attr_file__free(old);
+ }
+
+ attr_cache_unlock(cache);
+ return 0;
+}
+
+static int attr_cache_remove(git_attr_cache *cache, git_attr_file *file)
+{
+ int error = 0;
+ git_attr_cache_entry *ce;
+ bool found = false;
+
+ if (!file)
+ return 0;
+ if ((error = attr_cache_lock(cache)) < 0)
+ return error;
+
+ if ((ce = attr_cache_lookup_entry(cache, file->ce->path)) != NULL &&
+ ce->file[file->source] == file)
+ {
+ ce->file[file->source] = NULL;
+ found = true;
+ }
+
+ attr_cache_unlock(cache);
+
+ if (found)
+ git_attr_file__free(file);
+
+ return error;
+}
+
+int git_attr_cache__get(
+ git_attr_file **out,
+ git_repository *repo,
+ git_attr_cache_source source,
+ const char *base,
+ const char *filename,
+ git_attr_cache_parser parser,
+ void *payload)
+{
+ int error = 0;
+ git_buf path = GIT_BUF_INIT;
+ const char *wd = git_repository_workdir(repo), *relfile;
+ git_attr_cache *cache = git_repository_attr_cache(repo);
+ git_attr_cache_entry *ce = NULL;
+ git_attr_file *file = NULL;
+
+ /* join base and path as needed */
+ if (base != NULL && git_path_root(filename) < 0) {
+ if (git_buf_joinpath(&path, base, filename) < 0)
+ return -1;
+ filename = path.ptr;
+ }
+
+ relfile = filename;
+ if (wd && !git__prefixcmp(relfile, wd))
+ relfile += strlen(wd);
+
+ /* check cache for existing entry */
+ if ((error = attr_cache_lock(cache)) < 0)
+ goto cleanup;
+
+ ce = attr_cache_lookup_entry(cache, relfile);
+ if (!ce) {
+ if ((error = attr_cache_make_entry(&ce, repo, relfile)) < 0)
+ goto cleanup;
+ } else if (ce->file[source] != NULL) {
+ file = ce->file[source];
+ GIT_REFCOUNT_INC(file);
+ }
+
+ attr_cache_unlock(cache);
+
+ /* if this is not a file backed entry, just create a new empty one */
+ if (!parser) {
+ error = git_attr_file__new(&file, ce, source);
+ goto cleanup;
+ }
+
+ /* otherwise load and/or reload as needed */
+ switch (git_attr_file__out_of_date(repo, file)) {
+ case 1:
+ if (!(error = git_attr_file__load(
+ &file, repo, ce, source, parser, payload)))
+ error = attr_cache_upsert(cache, file);
+ break;
+ case 0:
+ /* just use the file */
+ break;
+ case GIT_ENOTFOUND:
+ /* did exist and now does not - remove from cache */
+ error = attr_cache_remove(cache, file);
+ file = NULL;
+ break;
+ default:
+ /* other error (e.g. out of memory, can't read index) */
+ giterr_clear();
+ break;
+ }
+
+cleanup:
+ *out = error ? NULL : file;
+ git_buf_free(&path);
+ return error;
+}
+
+bool git_attr_cache__is_cached(
+ git_repository *repo,
+ git_attr_cache_source source,
+ const char *filename)
+{
+ git_attr_cache *cache = git_repository_attr_cache(repo);
+ git_strmap *files;
+ khiter_t pos;
+ git_attr_cache_entry *ce;
+
+ if (!(cache = git_repository_attr_cache(repo)) ||
+ !(files = cache->files))
+ return false;
+
+ pos = git_strmap_lookup_index(files, filename);
+ if (!git_strmap_valid_index(files, pos))
+ return false;
+
+ ce = git_strmap_value_at(files, pos);
+
+ return ce && (ce->file[source] != NULL);
+}
+
+
+static int attr_cache__lookup_path(
+ char **out, git_config *cfg, const char *key, const char *fallback)
+{
+ git_buf buf = GIT_BUF_INIT;
+ int error;
+ const git_config_entry *entry = NULL;
+
+ *out = NULL;
+
+ if ((error = git_config__lookup_entry(&entry, cfg, key, false)) < 0)
+ return error;
+
+ if (entry) {
+ const char *cfgval = entry->value;
+
+ /* expand leading ~/ as needed */
+ if (cfgval && cfgval[0] == '~' && cfgval[1] == '/' &&
+ !git_sysdir_find_global_file(&buf, &cfgval[2]))
+ *out = git_buf_detach(&buf);
+ else if (cfgval)
+ *out = git__strdup(cfgval);
+
+ }
+ else if (!git_sysdir_find_xdg_file(&buf, fallback))
+ *out = git_buf_detach(&buf);
+
+ git_buf_free(&buf);
+
+ return error;
+}
+
+static void attr_cache__free(git_attr_cache *cache)
+{
+ if (!cache)
+ return;
+
+ if (cache->files != NULL) {
+ git_attr_file *file;
+
+ git_strmap_foreach_value(cache->files, file, {
+ git_attr_file__free(file);
+ });
+ git_strmap_free(cache->files);
+ }
+
+ if (cache->macros != NULL) {
+ git_attr_rule *rule;
+
+ git_strmap_foreach_value(cache->macros, rule, {
+ git_attr_rule__free(rule);
+ });
+ git_strmap_free(cache->macros);
+ }
+
+ git_pool_clear(&cache->pool);
+
+ git__free(cache->cfg_attr_file);
+ cache->cfg_attr_file = NULL;
+
+ git__free(cache->cfg_excl_file);
+ cache->cfg_excl_file = NULL;
+
+ git_mutex_free(&cache->lock);
+
+ git__free(cache);
+}
+
+int git_attr_cache__init(git_repository *repo)
+{
+ int ret = 0;
+ git_attr_cache *cache = git_repository_attr_cache(repo);
+ git_config *cfg;
+
+ if (cache)
+ return 0;
+
+ if ((ret = git_repository_config__weakptr(&cfg, repo)) < 0)
+ return ret;
+
+ cache = git__calloc(1, sizeof(git_attr_cache));
+ GITERR_CHECK_ALLOC(cache);
+
+ /* set up lock */
+ if (git_mutex_init(&cache->lock) < 0) {
+ giterr_set(GITERR_OS, "Unable to initialize lock for attr cache");
+ git__free(cache);
+ return -1;
+ }
+
+ /* cache config settings for attributes and ignores */
+ ret = attr_cache__lookup_path(
+ &cache->cfg_attr_file, cfg, GIT_ATTR_CONFIG, GIT_ATTR_FILE_XDG);
+ if (ret < 0)
+ goto cancel;
+
+ ret = attr_cache__lookup_path(
+ &cache->cfg_excl_file, cfg, GIT_IGNORE_CONFIG, GIT_IGNORE_FILE_XDG);
+ if (ret < 0)
+ goto cancel;
+
+ /* allocate hashtable for attribute and ignore file contents,
+ * hashtable for attribute macros, and string pool
+ */
+ if ((ret = git_strmap_alloc(&cache->files)) < 0 ||
+ (ret = git_strmap_alloc(&cache->macros)) < 0 ||
+ (ret = git_pool_init(&cache->pool, 1, 0)) < 0)
+ goto cancel;
+
+ cache = git__compare_and_swap(&repo->attrcache, NULL, cache);
+ if (cache)
+ goto cancel; /* raced with another thread, free this but no error */
+
+ /* insert default macros */
+ return git_attr_add_macro(repo, "binary", "-diff -crlf -text");
+
+cancel:
+ attr_cache__free(cache);
+ return ret;
+}
+
+void git_attr_cache_flush(git_repository *repo)
+{
+ git_attr_cache *cache;
+
+ /* this could be done less expensively, but for now, we'll just free
+ * the entire attrcache and let the next use reinitialize it...
+ */
+ if (repo && (cache = git__swap(repo->attrcache, NULL)) != NULL)
+ attr_cache__free(cache);
+}
+
+int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
+{
+ git_attr_cache *cache = git_repository_attr_cache(repo);
+ git_strmap *macros = cache->macros;
+ int error;
+
+ /* TODO: generate warning log if (macro->assigns.length == 0) */
+ if (macro->assigns.length == 0)
+ return 0;
+
+ if (git_mutex_lock(&cache->lock) < 0) {
+ giterr_set(GITERR_OS, "Unable to get attr cache lock");
+ error = -1;
+ } else {
+ git_strmap_insert(macros, macro->match.pattern, macro, error);
+ git_mutex_unlock(&cache->lock);
+ }
+
+ return (error < 0) ? -1 : 0;
+}
+
+git_attr_rule *git_attr_cache__lookup_macro(
+ git_repository *repo, const char *name)
+{
+ git_strmap *macros = git_repository_attr_cache(repo)->macros;
+ khiter_t pos;
+
+ pos = git_strmap_lookup_index(macros, name);
+
+ if (!git_strmap_valid_index(macros, pos))
+ return NULL;
+
+ return (git_attr_rule *)git_strmap_value_at(macros, pos);
+}
+
diff --git a/src/attrcache.h b/src/attrcache.h
index 4f9cff6..8e7f022 100644
--- a/src/attrcache.h
+++ b/src/attrcache.h
@@ -9,11 +9,15 @@
#include "pool.h"
#include "strmap.h"
+#include "buffer.h"
+
+#define GIT_ATTR_CONFIG "core.attributesfile"
+#define GIT_IGNORE_CONFIG "core.excludesfile"
typedef struct {
char *cfg_attr_file; /* cached value of core.attributesfile */
char *cfg_excl_file; /* cached value of core.excludesfile */
- git_strmap *files; /* hash path to git_attr_file of rules */
+ git_strmap *files; /* hash path to git_attr_cache_entry records */
git_strmap *macros; /* hash name to vector<git_attr_assignment> */
git_mutex lock;
git_pool pool;
@@ -21,4 +25,53 @@ typedef struct {
extern int git_attr_cache__init(git_repository *repo);
+typedef enum {
+ GIT_ATTR_CACHE__FROM_FILE = 0,
+ GIT_ATTR_CACHE__FROM_INDEX = 1,
+
+ GIT_ATTR_CACHE_NUM_SOURCES = 2
+} git_attr_cache_source;
+
+typedef struct git_attr_file git_attr_file;
+typedef struct git_attr_rule git_attr_rule;
+
+typedef struct {
+ git_attr_file *file[GIT_ATTR_CACHE_NUM_SOURCES];
+ const char *path; /* points into fullpath */
+ char fullpath[GIT_FLEX_ARRAY];
+} git_attr_cache_entry;
+
+typedef int (*git_attr_cache_parser)(
+ git_repository *repo,
+ git_attr_file *file,
+ const char *data,
+ void *payload);
+
+/* get file - loading and reload as needed */
+extern int git_attr_cache__get(
+ git_attr_file **file,
+ git_repository *repo,
+ git_attr_cache_source source,
+ const char *base,
+ const char *filename,
+ git_attr_cache_parser parser,
+ void *payload);
+
+extern bool git_attr_cache__is_cached(
+ git_repository *repo,
+ git_attr_cache_source source,
+ const char *path);
+
+extern int git_attr_cache__insert_macro(
+ git_repository *repo, git_attr_rule *macro);
+
+extern git_attr_rule *git_attr_cache__lookup_macro(
+ git_repository *repo, const char *name);
+
+extern int git_attr_cache_entry__new(
+ git_attr_cache_entry **out,
+ const char *base,
+ const char *path,
+ git_pool *pool);
+
#endif
diff --git a/src/fileops.c b/src/fileops.c
index 5709499..d8d8191 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -804,10 +804,8 @@ int git_futils_filestamp_check(
if (stamp == NULL)
return 1;
- if (p_stat(path, &st) < 0) {
- giterr_set(GITERR_OS, "Could not stat '%s'", path);
+ if (p_stat(path, &st) < 0)
return GIT_ENOTFOUND;
- }
if (stamp->mtime == (git_time_t)st.st_mtime &&
stamp->size == (git_off_t)st.st_size &&
diff --git a/src/fileops.h b/src/fileops.h
index 6a65235..cfc2ce7 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -292,13 +292,14 @@ typedef struct {
* Compare stat information for file with reference info.
*
* This function updates the file stamp to current data for the given path
- * and returns 0 if the file is up-to-date relative to the prior setting or
- * 1 if the file has been changed. (This also may return GIT_ENOTFOUND if
- * the file doesn't exist.)
+ * and returns 0 if the file is up-to-date relative to the prior setting,
+ * 1 if the file has been changed, or GIT_ENOTFOUND if the file doesn't
+ * exist. This will not call giterr_set, so you must set the error if you
+ * plan to return an error.
*
* @param stamp File stamp to be checked
* @param path Path to stat and check if changed
- * @return 0 if up-to-date, 1 if out-of-date, <0 on error
+ * @return 0 if up-to-date, 1 if out-of-date, GIT_ENOTFOUND if cannot stat
*/
extern int git_futils_filestamp_check(
git_futils_filestamp *stamp, const char *path);
diff --git a/src/ignore.c b/src/ignore.c
index 0fb042a..3ee7ba0 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -1,7 +1,7 @@
#include "git2/ignore.h"
#include "common.h"
#include "ignore.h"
-#include "attr.h"
+#include "attr_file.h"
#include "path.h"
#include "config.h"
@@ -10,26 +10,27 @@
#define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n"
static int parse_ignore_file(
- git_repository *repo, void *parsedata, const char *buffer, git_attr_file *ignores)
+ git_repository *repo,
+ git_attr_file *attrs,
+ const char *data,
+ void *payload)
{
int error = 0;
- git_attr_fnmatch *match = NULL;
- const char *scan = NULL, *context = NULL;
int ignore_case = false;
+ const char *scan = data, *context = NULL;
+ git_attr_fnmatch *match = NULL;
- /* Prefer to have the caller pass in a git_ignores as the parsedata
- * object. If they did not, then look up the value of ignore_case */
- if (parsedata != NULL)
- ignore_case = ((git_ignores *)parsedata)->ignore_case;
+ /* either read ignore_case from ignores structure or use repo config */
+ if (payload != NULL)
+ ignore_case = ((git_ignores *)payload)->ignore_case;
else if (git_repository__cvar(&ignore_case, repo, GIT_CVAR_IGNORECASE) < 0)
- return error;
-
- if (ignores->key &&
- git_path_root(ignores->key + 2) < 0 &&
- git__suffixcmp(ignores->key, "/" GIT_IGNORE_FILE) == 0)
- context = ignores->key + 2;
+ giterr_clear();
- scan = buffer;
+ /* if subdir file path, convert context for file paths */
+ if (attrs->ce &&
+ git_path_root(attrs->ce->path) < 0 &&
+ !git__suffixcmp(attrs->ce->path, "/" GIT_IGNORE_FILE))
+ context = attrs->ce->path;
while (!error && *scan) {
if (!match) {
@@ -40,7 +41,7 @@ static int parse_ignore_file(
match->flags = GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG;
if (!(error = git_attr_fnmatch__parse(
- match, ignores->pool, context, &scan)))
+ match, &attrs->pool, context, &scan)))
{
match->flags |= GIT_ATTR_FNMATCH_IGNORE;
@@ -48,7 +49,7 @@ static int parse_ignore_file(
match->flags |= GIT_ATTR_FNMATCH_ICASE;
scan = git__next_line(scan);
- error = git_vector_insert(&ignores->rules, match);
+ error = git_vector_insert(&attrs->rules, match);
}
if (error != 0) {
@@ -67,28 +68,46 @@ static int parse_ignore_file(
return error;
}
-#define push_ignore_file(R,IGN,S,B,F) \
- git_attr_cache__push_file((R),(B),(F),GIT_ATTR_FILE_FROM_FILE,parse_ignore_file,(IGN),(S))
+static int push_ignore_file(
+ git_ignores *ignores,
+ git_vector *which_list,
+ const char *base,
+ const char *filename)
+{
+ int error = 0;
+ git_attr_file *file = NULL;
+
+ if ((error = git_attr_cache__get(
+ &file, ignores->repo, GIT_ATTR_CACHE__FROM_FILE,
+ base, filename, parse_ignore_file, ignores)) < 0 ||
+ (error = git_vector_insert(which_list, file)) < 0)
+ git_attr_file__free(file);
+
+ return error;
+}
static int push_one_ignore(void *payload, git_buf *path)
{
git_ignores *ign = payload;
-
ign->depth++;
-
- return push_ignore_file(
- ign->repo, ign, &ign->ign_path, path->ptr, GIT_IGNORE_FILE);
+ return push_ignore_file(ign, &ign->ign_path, path->ptr, GIT_IGNORE_FILE);
}
-static int get_internal_ignores(git_attr_file **ign, git_repository *repo)
+static int get_internal_ignores(git_attr_file **out, git_repository *repo)
{
int error;
- if (!(error = git_attr_cache__init(repo)))
- error = git_attr_cache__internal_file(repo, GIT_IGNORE_INTERNAL, ign);
+ if ((error = git_attr_cache__init(repo)) < 0)
+ return error;
+
+ /* get with NULL parser, gives existing or empty git_attr_file */
+ error = git_attr_cache__get(
+ out, repo, GIT_ATTR_CACHE__FROM_FILE,
+ NULL, GIT_IGNORE_INTERNAL, NULL, NULL);
- if (!error && !(*ign)->rules.length)
- error = parse_ignore_file(repo, NULL, GIT_IGNORE_DEFAULT_RULES, *ign);
+ /* if internal rules list is empty, insert default rules */
+ if (!error && !(*out)->rules.length)
+ error = parse_ignore_file(repo, *out, GIT_IGNORE_DEFAULT_RULES, NULL);
return error;
}
@@ -127,8 +146,7 @@ int git_ignore__for_path(
goto cleanup;
/* set up internals */
- error = get_internal_ignores(&ignores->ign_internal, repo);
- if (error < 0)
+ if ((error = get_internal_ignores(&ignores->ign_internal, repo)) < 0)
goto cleanup;
/* load .gitignore up the path */
@@ -140,14 +158,16 @@ int git_ignore__for_path(
}
/* load .git/info/exclude */
- error = push_ignore_file(repo, ignores, &ignores->ign_global,
+ error = push_ignore_file(
+ ignores, &ignores->ign_global,
git_repository_path(repo), GIT_IGNORE_FILE_INREPO);
if (error < 0)
goto cleanup;
/* load core.excludesfile */
if (git_repository_attr_cache(repo)->cfg_excl_file != NULL)
- error = push_ignore_file(repo, ignores, &ignores->ign_global, NULL,
+ error = push_ignore_file(
+ ignores, &ignores->ign_global, NULL,
git_repository_attr_cache(repo)->cfg_excl_file);
cleanup:
@@ -165,35 +185,33 @@ int git_ignore__push_dir(git_ignores *ign, const char *dir)
ign->depth++;
return push_ignore_file(
- ign->repo, ign, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE);
+ ign, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE);
}
int git_ignore__pop_dir(git_ignores *ign)
{
if (ign->ign_path.length > 0) {
git_attr_file *file = git_vector_last(&ign->ign_path);
- const char *start, *end, *scan;
- size_t keylen;
+ const char *start = file->ce->path, *end;
- /* - ign->dir looks something like "a/b/" (or "a/b/c/d/")
- * - file->key looks something like "0#a/b/.gitignore
+ /* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/")
+ * - file->path looks something like "a/b/.gitignore
*
- * We are popping the last directory off ign->dir. We also want to
- * remove the file from the vector if the directory part of the key
- * matches the ign->dir path. We need to test if the "a/b" part of
+ * We are popping the last directory off ign->dir. We also want
+ * to remove the file from the vector if the popped directory
+ * matches the ignore path. We need to test if the "a/b" part of
* the file key matches the path we are about to pop.
*/
- for (start = end = scan = &file->key[2]; *scan; ++scan)
- if (*scan == '/')
- end = scan; /* point 'end' to last '/' in key */
- keylen = (end - start) + 1;
+ if ((end = strrchr(start, '/')) != NULL) {
+ size_t dirlen = (end - start) + 1;
- if (ign->dir.size >= keylen &&
- !memcmp(ign->dir.ptr + ign->dir.size - keylen, start, keylen))
- {
- git_attr_file__free(git_vector_last(&ign->ign_path));
- git_vector_pop(&ign->ign_path);
+ if (ign->dir.size >= dirlen &&
+ !memcmp(ign->dir.ptr + ign->dir.size - dirlen, start, dirlen))
+ {
+ git_vector_pop(&ign->ign_path);
+ git_attr_file__free(file);
+ }
}
}
@@ -210,7 +228,7 @@ void git_ignore__free(git_ignores *ignores)
unsigned int i;
git_attr_file *file;
- /* don't need to free ignores->ign_internal it is cached exactly once */
+ git_attr_file__free(ignores->ign_internal);
git_vector_foreach(&ignores->ign_path, i, file) {
git_attr_file__free(file);
@@ -283,10 +301,12 @@ int git_ignore_add_rule(
const char *rules)
{
int error;
- git_attr_file *ign_internal;
+ git_attr_file *ign_internal = NULL;
- if (!(error = get_internal_ignores(&ign_internal, repo)))
+ if (!(error = get_internal_ignores(&ign_internal, repo))) {
error = parse_ignore_file(repo, NULL, rules, ign_internal);
+ git_attr_file__free(ign_internal);
+ }
return error;
}
@@ -300,8 +320,10 @@ int git_ignore_clear_internal_rules(
if (!(error = get_internal_ignores(&ign_internal, repo))) {
git_attr_file__clear_rules(ign_internal);
- return parse_ignore_file(
+ error = parse_ignore_file(
repo, NULL, GIT_IGNORE_DEFAULT_RULES, ign_internal);
+
+ git_attr_file__free(ign_internal);
}
return error;
diff --git a/src/index.c b/src/index.c
index 27a557c..d7d937f 100644
--- a/src/index.c
+++ b/src/index.c
@@ -607,8 +607,15 @@ int git_index_read(git_index *index, int force)
}
updated = git_futils_filestamp_check(&stamp, index->index_file_path);
- if (updated < 0 || (!updated && !force))
+ if (updated < 0) {
+ giterr_set(
+ GITERR_INDEX,
+ "Failed to read index: '%s' no longer exists",
+ index->index_file_path);
return updated;
+ }
+ if (!updated && !force)
+ return 0;
error = git_futils_readbuffer(&buffer, index->index_file_path);
if (error < 0)
@@ -667,11 +674,11 @@ int git_index_write(git_index *index)
if ((error = git_filebuf_commit(&file)) < 0)
return error;
- error = git_futils_filestamp_check(&index->stamp, index->index_file_path);
- if (error < 0)
- return error;
+ if (git_futils_filestamp_check(&index->stamp, index->index_file_path) < 0)
+ /* index could not be read from disk! */;
+ else
+ index->on_disk = 1;
- index->on_disk = 1;
return 0;
}
diff --git a/src/sortedcache.c b/src/sortedcache.c
index 6253220..c6b2261 100644
--- a/src/sortedcache.c
+++ b/src/sortedcache.c
@@ -232,9 +232,8 @@ unlock:
void git_sortedcache_updated(git_sortedcache *sc)
{
- /* update filestamp to latest value */
- if (git_futils_filestamp_check(&sc->stamp, sc->path) < 0)
- giterr_clear();
+ /* update filestamp to latest value */
+ git_futils_filestamp_check(&sc->stamp, sc->path);
}
/* release all items in sorted cache */
diff --git a/src/submodule.c b/src/submodule.c
index 95d3d0d..5ddbfe8 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -1693,8 +1693,6 @@ static int submodule_cache_refresh(git_submodule_cache *cache, int refresh)
update_gitmod = (wd != NULL) ?
git_futils_filestamp_check(&cache->gitmodules_stamp, path.ptr) :
(cache->gitmodules_stamp.mtime != 0);
- if (update_gitmod < 0)
- giterr_clear();
}
/* clear submodule flags that are to be refreshed */
diff --git a/tests/attr/file.c b/tests/attr/file.c
index 4eb1d22..e35957b 100644
--- a/tests/attr/file.c
+++ b/tests/attr/file.c
@@ -11,9 +11,9 @@ void test_attr_file__simple_read(void)
git_attr_assignment *assign;
git_attr_rule *rule;
- cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr0")));
+ cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr0")));
- cl_assert_equal_s(cl_fixture("attr/attr0"), file->key + 2);
+ cl_assert_equal_s(cl_fixture("attr/attr0"), file->ce->path);
cl_assert(file->rules.length == 1);
rule = get_rule(0);
@@ -37,9 +37,9 @@ void test_attr_file__match_variants(void)
git_attr_rule *rule;
git_attr_assignment *assign;
- cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr1")));
+ cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr1")));
- cl_assert_equal_s(cl_fixture("attr/attr1"), file->key + 2);
+ cl_assert_equal_s(cl_fixture("attr/attr1"), file->ce->path);
cl_assert(file->rules.length == 10);
/* let's do a thorough check of this rule, then just verify
@@ -121,9 +121,9 @@ void test_attr_file__assign_variants(void)
git_attr_rule *rule;
git_attr_assignment *assign;
- cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr2")));
+ cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr2")));
- cl_assert_equal_s(cl_fixture("attr/attr2"), file->key + 2);
+ cl_assert_equal_s(cl_fixture("attr/attr2"), file->ce->path);
cl_assert(file->rules.length == 11);
check_one_assign(file, 0, 0, "pat0", "simple", EXPECT_TRUE, NULL);
@@ -187,8 +187,8 @@ void test_attr_file__check_attr_examples(void)
git_attr_rule *rule;
git_attr_assignment *assign;
- cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr3")));
- cl_assert_equal_s(cl_fixture("attr/attr3"), file->key + 2);
+ cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr3")));
+ cl_assert_equal_s(cl_fixture("attr/attr3"), file->ce->path);
cl_assert(file->rules.length == 3);
rule = get_rule(0);
diff --git a/tests/attr/lookup.c b/tests/attr/lookup.c
index 200bdd2..099597e 100644
--- a/tests/attr/lookup.c
+++ b/tests/attr/lookup.c
@@ -9,8 +9,8 @@ void test_attr_lookup__simple(void)
git_attr_path path;
const char *value = NULL;
- cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr0")));
- cl_assert_equal_s(cl_fixture("attr/attr0"), file->key + 2);
+ cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr0")));
+ cl_assert_equal_s(cl_fixture("attr/attr0"), file->ce->path);
cl_assert(file->rules.length == 1);
cl_git_pass(git_attr_path__init(&path, "test", NULL));
@@ -129,8 +129,8 @@ void test_attr_lookup__match_variants(void)
{ NULL, NULL, 0, NULL }
};
- cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr1")));
- cl_assert_equal_s(cl_fixture("attr/attr1"), file->key + 2);
+ cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr1")));
+ cl_assert_equal_s(cl_fixture("attr/attr1"), file->ce->path);
cl_assert(file->rules.length == 10);
cl_git_pass(git_attr_path__init(&path, "/testing/for/pat0", NULL));
@@ -190,7 +190,7 @@ void test_attr_lookup__assign_variants(void)
{ NULL, NULL, 0, NULL }
};
- cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr2")));
+ cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr2")));
cl_assert(file->rules.length == 11);
run_test_cases(file, cases, 0);
@@ -225,7 +225,7 @@ void test_attr_lookup__check_attr_examples(void)
{ NULL, NULL, 0, NULL }
};
- cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr3")));
+ cl_git_pass(git_attr_file__load_standalone(&file, cl_fixture("attr/attr3")));
cl_assert(file->rules.length == 3);
run_test_cases(file, cases, 0);
@@ -250,9 +250,9 @@ void test_attr_lookup__from_buffer(void)
{ NULL, NULL, 0, NULL }
};
- cl_git_pass(git_attr_file__new(&file, 0, NULL, NULL));
+ cl_git_pass(git_attr_file__new(&file, NULL, 0));
- cl_git_pass(git_attr_file__parse_buffer(NULL, NULL, "a* foo\nabc bar\n* baz", file));
+ cl_git_pass(git_attr_file__parse_buffer(NULL, file, "a* foo\nabc bar\n* baz", NULL));
cl_assert(file->rules.length == 3);
diff --git a/tests/threads/diff.c b/tests/threads/diff.c
index 5565c4b..562eec7 100644
--- a/tests/threads/diff.c
+++ b/tests/threads/diff.c
@@ -1,59 +1,22 @@
#include "clar_libgit2.h"
-#include "thread-utils.h"
+#include "thread_helpers.h"
static git_repository *_repo;
static git_tree *_a, *_b;
static git_atomic _counts[4];
static int _check_counts;
+#define THREADS 20
+
void test_threads_diff__cleanup(void)
{
cl_git_sandbox_cleanup();
}
-static void run_in_parallel(
- int repeats, int threads, void *(*func)(void *),
- void (*before_test)(void), void (*after_test)(void))
-{
- int r, t, *id = git__calloc(threads, sizeof(int));
-#ifdef GIT_THREADS
- git_thread *th = git__calloc(threads, sizeof(git_thread));
- cl_assert(th != NULL);
-#else
- void *th = NULL;
-#endif
-
- cl_assert(id != NULL);
-
- for (r = 0; r < repeats; ++r) {
- _repo = cl_git_sandbox_reopen(); /* reopen sandbox to flush caches */
-
- if (before_test) before_test();
-
- for (t = 0; t < threads; ++t) {
- id[t] = t;
-#ifdef GIT_THREADS
- cl_git_pass(git_thread_create(&th[t], NULL, func, &id[t]));
-#else
- cl_assert(func(&id[t]) == &id[t]);
-#endif
- }
-
-#ifdef GIT_THREADS
- for (t = 0; t < threads; ++t)
- cl_git_pass(git_thread_join(th[t], NULL));
- memset(th, 0, threads * sizeof(git_thread));
-#endif
-
- if (after_test) after_test();
- }
-
- git__free(id);
- git__free(th);
-}
-
static void setup_trees(void)
{
+ _repo = cl_git_sandbox_reopen(); /* reopen sandbox to flush caches */
+
cl_git_pass(git_revparse_single(
(git_object **)&_a, _repo, "0017bd4ab1^{tree}"));
cl_git_pass(git_revparse_single(
@@ -62,8 +25,6 @@ static void setup_trees(void)
memset(_counts, 0, sizeof(_counts));
}
-#define THREADS 20
-
static void free_trees(void)
{
git_tree_free(_a); _a = NULL;
diff --git a/tests/threads/iterator.c b/tests/threads/iterator.c
new file mode 100644
index 0000000..4dd251f
--- /dev/null
+++ b/tests/threads/iterator.c
@@ -0,0 +1,49 @@
+#include "clar_libgit2.h"
+#include "thread_helpers.h"
+#include "iterator.h"
+
+static git_repository *_repo;
+
+void test_threads_iterator__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+static void *run_workdir_iterator(void *arg)
+{
+ int error = 0, thread = *(int *)arg;
+ git_iterator *iter;
+ const git_index_entry *entry = NULL;
+
+ cl_git_pass(git_iterator_for_workdir(
+ &iter, _repo, GIT_ITERATOR_DONT_AUTOEXPAND, NULL, NULL));
+
+ while (!error) {
+ if (entry && entry->mode == GIT_FILEMODE_TREE) {
+ error = git_iterator_advance_into(&entry, iter);
+
+ if (error == GIT_ENOTFOUND)
+ error = git_iterator_advance(&entry, iter);
+ } else {
+ error = git_iterator_advance(&entry, iter);
+ }
+
+ if (!error)
+ (void)git_iterator_current_is_ignored(iter);
+ }
+
+ cl_assert_equal_i(GIT_ITEROVER, error);
+
+ git_iterator_free(iter);
+
+ return arg;
+}
+
+
+void test_threads_iterator__workdir(void)
+{
+ _repo = cl_git_sandbox_init("status");
+
+ run_in_parallel(
+ 1, 20, run_workdir_iterator, NULL, NULL);
+}
diff --git a/tests/threads/thread_helpers.c b/tests/threads/thread_helpers.c
new file mode 100644
index 0000000..25370dd
--- /dev/null
+++ b/tests/threads/thread_helpers.c
@@ -0,0 +1,44 @@
+#include "clar_libgit2.h"
+#include "thread_helpers.h"
+
+void run_in_parallel(
+ int repeats,
+ int threads,
+ void *(*func)(void *),
+ void (*before_test)(void),
+ void (*after_test)(void))
+{
+ int r, t, *id = git__calloc(threads, sizeof(int));
+#ifdef GIT_THREADS
+ git_thread *th = git__calloc(threads, sizeof(git_thread));
+ cl_assert(th != NULL);
+#else
+ void *th = NULL;
+#endif
+
+ cl_assert(id != NULL);
+
+ for (r = 0; r < repeats; ++r) {
+ if (before_test) before_test();
+
+ for (t = 0; t < threads; ++t) {
+ id[t] = t;
+#ifdef GIT_THREADS
+ cl_git_pass(git_thread_create(&th[t], NULL, func, &id[t]));
+#else
+ cl_assert(func(&id[t]) == &id[t]);
+#endif
+ }
+
+#ifdef GIT_THREADS
+ for (t = 0; t < threads; ++t)
+ cl_git_pass(git_thread_join(th[t], NULL));
+ memset(th, 0, threads * sizeof(git_thread));
+#endif
+
+ if (after_test) after_test();
+ }
+
+ git__free(id);
+ git__free(th);
+}
diff --git a/tests/threads/thread_helpers.h b/tests/threads/thread_helpers.h
new file mode 100644
index 0000000..3c13cfb
--- /dev/null
+++ b/tests/threads/thread_helpers.h
@@ -0,0 +1,8 @@
+#include "thread-utils.h"
+
+void run_in_parallel(
+ int repeats,
+ int threads,
+ void *(*func)(void *),
+ void (*before_test)(void),
+ void (*after_test)(void));