Support reading attributes from index Depending on the operation, we need to consider gitattributes in both the work dir and the index. This adds a parameter to all of the gitattributes related functions that allows user control of attribute reading behavior (i.e. prefer workdir, prefer index, only use index). This fix also covers allowing us to check attributes (and hence do diff and status) on bare repositories. This was a somewhat larger change that I hoped because it had to change the cache key used for gitattributes files.
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
diff --git a/AUTHORS b/AUTHORS
index 954f259..03904ff 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -43,6 +43,7 @@ Ramsay Jones
Robert G. Jakabosky
Romain Geissler
Romain Muller
+Russell Belfer
Sakari Jokinen
Sam
Sarath Lakshman
diff --git a/include/git2/attr.h b/include/git2/attr.h
index 81d1e51..dfa1d27 100644
--- a/include/git2/attr.h
+++ b/include/git2/attr.h
@@ -19,42 +19,186 @@
*/
GIT_BEGIN_DECL
+/**
+ * GIT_ATTR_TRUE checks if an attribute is set on. In core git
+ * parlance, this the value for "Set" attributes.
+ *
+ * For example, if the attribute file contains:
+ *
+ * *.c foo
+ *
+ * Then for file `xyz.c` looking up attribute "foo" gives a value for
+ * which `GIT_ATTR_TRUE(value)` is true.
+ */
#define GIT_ATTR_TRUE(attr) ((attr) == git_attr__true)
+
+/**
+ * GIT_ATTR_FALSE checks if an attribute is set off. In core git
+ * parlance, this is the value for attributes that are "Unset" (not to
+ * be confused with values that a "Unspecified").
+ *
+ * For example, if the attribute file contains:
+ *
+ * *.h -foo
+ *
+ * Then for file `zyx.h` looking up attribute "foo" gives a value for
+ * which `GIT_ATTR_FALSE(value)` is true.
+ */
#define GIT_ATTR_FALSE(attr) ((attr) == git_attr__false)
-#define GIT_ATTR_UNSPECIFIED(attr) ((attr) == NULL)
+
+/**
+ * GIT_ATTR_UNSPECIFIED checks if an attribute is unspecified. This
+ * may be due to the attribute not being mentioned at all or because
+ * the attribute was explicitly set unspecified via the `!` operator.
+ *
+ * For example, if the attribute file contains:
+ *
+ * *.c foo
+ * *.h -foo
+ * onefile.c !foo
+ *
+ * Then for `onefile.c` looking up attribute "foo" yields a value with
+ * `GIT_ATTR_UNSPECIFIED(value)` of true. Also, looking up "foo" on
+ * file `onefile.rb` or looking up "bar" on any file will all give
+ * `GIT_ATTR_UNSPECIFIED(value)` of true.
+ */
+#define GIT_ATTR_UNSPECIFIED(attr) (!(attr) || (attr) == git_attr__unset)
+
+/**
+ * GIT_ATTR_SET_TO_VALUE checks if an attribute is set to a value (as
+ * opposied to TRUE, FALSE or UNSPECIFIED). This would be the case if
+ * for a file with something like:
+ *
+ * *.txt eol=lf
+ *
+ * Given this, looking up "eol" for `onefile.txt` will give back the
+ * string "lf" and `GIT_ATTR_SET_TO_VALUE(attr)` will return true.
+ */
+#define GIT_ATTR_SET_TO_VALUE(attr) \
+ ((attr) && (attr) != git_attr__unset && \
+ (attr) != git_attr__true && (attr) != git_attr__false)
GIT_EXTERN(const char *) git_attr__true;
GIT_EXTERN(const char *) git_attr__false;
+GIT_EXTERN(const char *) git_attr__unset;
+/**
+ * Check attribute flags: Reading values from index and working directory.
+ *
+ * When checking attributes, it is possible to check attribute files
+ * in both the working directory (if there is one) and the index (if
+ * there is one). You can explicitly choose where to check and in
+ * which order using the following flags.
+ *
+ * Core git usually checks the working directory then the index,
+ * except during a checkout when it checks the index first. It will
+ * use index only for creating archives or for a bare repo (if an
+ * index has been specified for the bare repo).
+ */
+#define GIT_ATTR_CHECK_FILE_THEN_INDEX 0
+#define GIT_ATTR_CHECK_INDEX_THEN_FILE 1
+#define GIT_ATTR_CHECK_INDEX_ONLY 2
/**
- * Lookup attribute for path returning string caller must free
+ * Check attribute flags: Using the system attributes file.
+ *
+ * Normally, attribute checks include looking in the /etc (or system
+ * equivalent) directory for a `gitattributes` file. Passing this
+ * flag will cause attribute checks to ignore that file.
+ */
+#define GIT_ATTR_CHECK_NO_SYSTEM (1 << 2)
+
+/**
+ * Look up the value of one git attribute for path.
+ *
+ * @param repo The repository containing the path.
+ * @param flags A combination of GIT_ATTR_CHECK... flags.
+ * @param path The path to check for attributes. Relative paths are
+ * interpreted relative to the repo root. The file does
+ * not have to exist, but if it does not, then it will be
+ * treated as a plain file (not a directory).
+ * @param name The name of the attribute to look up.
+ * @param value Output of the value of the attribute. Use the GIT_ATTR_...
+ * macros to test for TRUE, FALSE, UNSPECIFIED, etc. or just
+ * use the string value for attributes set to a value. You
+ * should NOT modify or free this value.
*/
GIT_EXTERN(int) git_attr_get(
- git_repository *repo, const char *path, const char *name,
+ git_repository *repo,
+ uint32_t flags,
+ const char *path,
+ const char *name,
const char **value);
/**
- * Lookup list of attributes for path, populating array of strings
+ * Look up a list of git attributes for path.
+ *
+ * Use this if you have a known list of attributes that you want to
+ * look up in a single call. This is somewhat more efficient than
+ * calling `git_attr_get()` multiple times.
+ *
+ * For example, you might write:
+ *
+ * const char *attrs[] = { "crlf", "diff", "foo" };
+ * const char **values[3];
+ * git_attr_get_many(repo, 0, "my/fun/file.c", 3, attrs, values);
+ *
+ * Then you could loop through the 3 values to get the settings for
+ * the three attributes you asked about.
+ *
+ * @param repo The repository containing the path.
+ * @param flags A combination of GIT_ATTR_CHECK... flags.
+ * @param path The path inside the repo to check attributes. This
+ * does not have to exist, but if it does not, then
+ * it will be treated as a plain file (i.e. not a directory).
+ * @param num_attr The number of attributes being looked up
+ * @param names An array of num_attr strings containing attribute names.
+ * @param values An array of num_attr entries that will have string
+ * pointers written into it for the values of the attributes.
+ * You should not modify or free the values that are written
+ * into this array (although of course, you should free the
+ * array itself if you allocated it).
*/
GIT_EXTERN(int) git_attr_get_many(
- git_repository *repo, const char *path,
- size_t num_attr, const char **names,
+ git_repository *repo,
+ uint32_t flags,
+ const char *path,
+ size_t num_attr,
+ const char **names,
const char **values);
/**
- * Perform an operation on each attribute of a path.
+ * Loop over all the git attributes for a path.
+ *
+ * @param repo The repository containing the path.
+ * @param flags A combination of GIT_ATTR_CHECK... flags.
+ * @param path The path inside the repo to check attributes. This
+ * does not have to exist, but if it does not, then
+ * it will be treated as a plain file (i.e. not a directory).
+ * @param callback The function that will be invoked on each attribute
+ * and attribute value. The name parameter will be the name
+ * of the attribute and the value will be the value it is
+ * set to, including possibly NULL if the attribute is
+ * explicitly set to UNSPECIFIED using the ! sign. This
+ * will be invoked only once per attribute name, even if
+ * there are multiple rules for a given file. The highest
+ * priority rule will be used.
+ * @param payload Passed on as extra parameter to callback function.
*/
GIT_EXTERN(int) git_attr_foreach(
- git_repository *repo, const char *path,
+ git_repository *repo,
+ uint32_t flags,
+ const char *path,
int (*callback)(const char *name, const char *value, void *payload),
void *payload);
/**
* Flush the gitattributes cache.
*
- * Call this if you have reason to believe that the attributes files
- * on disk no longer match the cached contents of memory.
+ * Call this if you have reason to believe that the attributes files on
+ * disk no longer match the cached contents of memory. This will cause
+ * the attributes files to be reloaded the next time that an attribute
+ * access function is called.
*/
GIT_EXTERN(void) git_attr_cache_flush(
git_repository *repo);
@@ -62,7 +206,7 @@ GIT_EXTERN(void) git_attr_cache_flush(
/**
* Add a macro definition.
*
- * Macros will automatically be loaded from the top level .gitattributes
+ * Macros will automatically be loaded from the top level `.gitattributes`
* file of the repository (plus the build-in "binary" macro). This
* function allows you to add others. For example, to add the default
* macro, you would call:
diff --git a/src/attr.c b/src/attr.c
index 120d127..56d04d3 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -6,12 +6,18 @@
GIT__USE_STRMAP;
static int collect_attr_files(
- git_repository *repo, const char *path, git_vector *files);
+ git_repository *repo,
+ uint32_t flags,
+ const char *path,
+ git_vector *files);
int git_attr_get(
- git_repository *repo, const char *pathname,
- const char *name, const char **value)
+ git_repository *repo,
+ uint32_t flags,
+ const char *pathname,
+ const char *name,
+ const char **value)
{
int error;
git_attr_path path;
@@ -26,7 +32,7 @@ int git_attr_get(
if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
return -1;
- if ((error = collect_attr_files(repo, pathname, &files)) < 0)
+ if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0)
goto cleanup;
attr.name = name;
@@ -58,8 +64,12 @@ typedef struct {
} attr_get_many_info;
int git_attr_get_many(
- git_repository *repo, const char *pathname,
- size_t num_attr, const char **names, const char **values)
+ git_repository *repo,
+ uint32_t flags,
+ const char *pathname,
+ size_t num_attr,
+ const char **names,
+ const char **values)
{
int error;
git_attr_path path;
@@ -75,7 +85,7 @@ int git_attr_get_many(
if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
return -1;
- if ((error = collect_attr_files(repo, pathname, &files)) < 0)
+ if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0)
goto cleanup;
info = git__calloc(num_attr, sizeof(attr_get_many_info));
@@ -119,7 +129,9 @@ cleanup:
int git_attr_foreach(
- git_repository *repo, const char *pathname,
+ git_repository *repo,
+ uint32_t flags,
+ const char *pathname,
int (*callback)(const char *name, const char *value, void *payload),
void *payload)
{
@@ -135,7 +147,7 @@ int git_attr_foreach(
if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
return -1;
- if ((error = collect_attr_files(repo, pathname, &files)) < 0)
+ if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0)
goto cleanup;
seen = git_strmap_alloc();
@@ -203,113 +215,230 @@ int git_attr_add_macro(
return error;
}
-bool git_attr_cache__is_cached(git_repository *repo, const char *path)
+bool git_attr_cache__is_cached(
+ git_repository *repo, git_attr_file_source source, const char *path)
{
- const char *cache_key = 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 *filename, const char **data)
+{
+ int error;
+ git_buf content = GIT_BUF_INIT;
+
+ error = git_futils_readbuffer(&content, filename);
+ *data = error ? NULL : git_buf_detach(&content);
+
+ return error;
+}
- if (repo && git__prefixcmp(cache_key, git_repository_workdir(repo)) == 0)
- cache_key += strlen(git_repository_workdir(repo));
+static int load_attr_blob_from_index(
+ git_repository *repo, const char *filename, git_blob **blob)
+{
+ int error;
+ git_index *index;
+ git_index_entry *entry;
+
+ if ((error = git_repository_index__weakptr(&index, repo)) < 0 ||
+ (error = git_index_find(index, filename)) < 0)
+ return error;
- return git_strmap_exists(files, cache_key);
+ entry = git_index_get(index, error);
+
+ return git_blob_lookup(blob, repo, &entry->oid);
}
-int git_attr_cache__lookup_or_create_file(
+int git_attr_cache__internal_file(
git_repository *repo,
- const char *key,
const char *filename,
- int (*loader)(git_repository *, const char *, git_attr_file *),
- git_attr_file **file_ptr)
+ git_attr_file **file)
{
- int error;
+ int error = 0;
git_attr_cache *cache = git_repository_attr_cache(repo);
- git_attr_file *file = NULL;
- khiter_t pos;
-
- pos = git_strmap_lookup_index(cache->files, key);
- if (git_strmap_valid_index(cache->files, pos)) {
- *file_ptr = git_strmap_value_at(cache->files, pos);
- return 0;
- }
+ khiter_t cache_pos = git_strmap_lookup_index(cache->files, filename);
- if (loader && git_path_exists(filename) == false) {
- *file_ptr = NULL;
+ if (git_strmap_valid_index(cache->files, cache_pos)) {
+ *file = git_strmap_value_at(cache->files, cache_pos);
return 0;
}
- if (git_attr_file__new(&file, &cache->pool) < 0)
+ if (git_attr_file__new(file, 0, filename, &cache->pool) < 0)
return -1;
- if (loader)
- error = loader(repo, filename, file);
- else
- error = git_attr_file__set_path(repo, key, file);
-
- if (!error) {
- git_strmap_insert(cache->files, file->path, file, error);
- if (error > 0)
- error = 0;
- }
-
- if (error < 0) {
- git_attr_file__free(file);
- file = NULL;
- }
+ git_strmap_insert(cache->files, (*file)->key + 2, *file, error);
+ if (error > 0)
+ error = 0;
- *file_ptr = file;
return error;
}
-/* add git_attr_file to vector of files, loading if needed */
int git_attr_cache__push_file(
git_repository *repo,
- git_vector *stack,
- const char *base,
- const char *filename,
- int (*loader)(git_repository *, const char *, git_attr_file *))
+ const char *base,
+ const char *filename,
+ git_attr_file_source source,
+ git_attr_file_parser parse,
+ git_vector *stack)
{
- int error;
+ 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;
- const char *cache_key;
+ git_blob *blob = NULL;
- if (base != NULL) {
+ 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;
}
- /* either get attr_file from cache or read from disk */
- cache_key = filename;
- if (repo && git__prefixcmp(cache_key, git_repository_workdir(repo)) == 0)
- cache_key += strlen(git_repository_workdir(repo));
+ relfile = filename;
+ if (workdir && git__prefixcmp(relfile, workdir) == 0)
+ relfile += strlen(workdir);
+
+ /* check cache */
+ if (cache && cache->files) {
+ git_buf cache_key = GIT_BUF_INIT;
+ khiter_t cache_pos;
+
+ if (git_buf_printf(&cache_key, "%d#%s", (int)source, relfile) < 0)
+ return -1;
+
+ cache_pos = git_strmap_lookup_index(cache->files, cache_key.ptr);
+
+ git_buf_free(&cache_key);
+
+ if (git_strmap_valid_index(cache->files, cache_pos)) {
+ file = git_strmap_value_at(cache->files, cache_pos);
+ goto finish;
+ }
+ }
+
+ /* if not in cache, load data, parse, and cache */
+ if (git_attr_file__new(&file, source, relfile, &cache->pool) < 0)
+ return -1;
+
+ if (source == GIT_ATTR_FILE_FROM_FILE)
+ error = load_attr_file(filename, &content);
+ else
+ error = load_attr_blob_from_index(repo, relfile, &blob);
+
+ if (error) {
+ /* not finding a file is not an error for this function */
+ if (error == GIT_ENOTFOUND) {
+ giterr_clear();
+ error = 0;
+ }
+ goto finish;
+ }
+
+ if (blob)
+ content = git_blob_rawcontent(blob);
+
+ if (parse && (error = parse(repo, content, file)) < 0)
+ goto finish;
- error = git_attr_cache__lookup_or_create_file(
- repo, cache_key, filename, loader, &file);
+ git_strmap_insert(cache->files, file->key, file, error);
+ if (error > 0)
+ error = 0;
+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_attrs(R,S,B,F) \
- git_attr_cache__push_file((R),(S),(B),(F),git_attr_file__from_file)
+#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,(S))
typedef struct {
git_repository *repo;
+ uint32_t flags;
+ const char *workdir;
+ git_index *index;
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)
+{
+ int count = 0;
+
+ switch (flags & 0x03) {
+ case GIT_ATTR_CHECK_FILE_THEN_INDEX:
+ if (has_wd)
+ srcs[count++] = GIT_ATTR_FILE_FROM_FILE;
+ if (has_index)
+ srcs[count++] = GIT_ATTR_FILE_FROM_INDEX;
+ break;
+ case GIT_ATTR_CHECK_INDEX_THEN_FILE:
+ if (has_index)
+ srcs[count++] = GIT_ATTR_FILE_FROM_INDEX;
+ if (has_wd)
+ srcs[count++] = GIT_ATTR_FILE_FROM_FILE;
+ break;
+ case GIT_ATTR_CHECK_INDEX_ONLY:
+ if (has_index)
+ srcs[count++] = GIT_ATTR_FILE_FROM_INDEX;
+ break;
+ }
+
+ return count;
+}
+
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;
- return push_attrs(info->repo, info->files, path->ptr, GIT_ATTR_FILE);
+ git_attr_file_source src[2];
+
+ n_src = git_attr_cache__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, info->files);
+
+ return error;
}
static int collect_attr_files(
- git_repository *repo, const char *path, git_vector *files)
+ git_repository *repo,
+ uint32_t flags,
+ const char *path,
+ git_vector *files)
{
int error;
git_buf dir = GIT_BUF_INIT;
@@ -320,7 +449,11 @@ static int collect_attr_files(
git_vector_init(files, 4, NULL) < 0)
return -1;
- error = git_path_find_dir(&dir, path, workdir);
+ /* given a unrooted path in a non-bare repo, resolve it */
+ if (workdir && git_path_root(path) < 0)
+ error = git_path_find_dir(&dir, path, workdir);
+ else
+ error = git_buf_sets(&dir, path);
if (error < 0)
goto cleanup;
@@ -331,29 +464,36 @@ static int collect_attr_files(
* - $GIT_PREFIX/etc/gitattributes
*/
- error = push_attrs(
+ error = push_attr_file(
repo, files, git_repository_path(repo), GIT_ATTR_FILE_INREPO);
if (error < 0)
goto cleanup;
- info.repo = repo;
+ info.repo = repo;
+ info.flags = flags;
+ info.workdir = workdir;
+ if (git_repository_index__weakptr(&info.index, repo) < 0)
+ giterr_clear(); /* no error even if there is no index */
info.files = files;
+
error = git_path_walk_up(&dir, workdir, push_one_attr, &info);
if (error < 0)
goto cleanup;
if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) {
- error = push_attrs(
+ error = push_attr_file(
repo, files, NULL, git_repository_attr_cache(repo)->cfg_attr_file);
if (error < 0)
goto cleanup;
}
- error = git_futils_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
- if (!error)
- error = push_attrs(repo, files, NULL, dir.ptr);
- else if (error == GIT_ENOTFOUND)
- error = 0;
+ if ((flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
+ error = git_futils_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
+ if (!error)
+ error = push_attr_file(repo, files, NULL, dir.ptr);
+ else if (error == GIT_ENOTFOUND)
+ error = 0;
+ }
cleanup:
if (error < 0)
diff --git a/src/attr.h b/src/attr.h
index 43caf1b..a35b116 100644
--- a/src/attr.h
+++ b/src/attr.h
@@ -22,6 +22,9 @@ typedef struct {
const char *cfg_excl_file; /* cached value of core.excludesfile */
} git_attr_cache;
+typedef int (*git_attr_file_parser)(
+ git_repository *, const char *, git_attr_file *);
+
extern int git_attr_cache__init(git_repository *repo);
extern int git_attr_cache__insert_macro(
@@ -30,21 +33,24 @@ extern int git_attr_cache__insert_macro(
extern git_attr_rule *git_attr_cache__lookup_macro(
git_repository *repo, const char *name);
-extern int git_attr_cache__lookup_or_create_file(
+extern int git_attr_cache__push_file(
git_repository *repo,
- const char *key,
+ const char *base,
const char *filename,
- int (*loader)(git_repository *, const char *, git_attr_file *),
- git_attr_file **file_ptr);
+ git_attr_file_source source,
+ git_attr_file_parser parse,
+ git_vector *stack);
-extern int git_attr_cache__push_file(
+extern int git_attr_cache__internal_file(
git_repository *repo,
- git_vector *stack,
- const char *base,
- const char *filename,
- int (*loader)(git_repository *, const char *, git_attr_file *));
+ 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, const char *path);
+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);
#endif
diff --git a/src/attr_file.c b/src/attr_file.c
index 25c21b1..ab320a6 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -1,15 +1,22 @@
#include "common.h"
#include "repository.h"
#include "filebuf.h"
+#include "git2/blob.h"
+#include "git2/tree.h"
#include <ctype.h>
const char *git_attr__true = "[internal]__TRUE__";
const char *git_attr__false = "[internal]__FALSE__";
+const char *git_attr__unset = "[internal]__UNSET__";
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);
-int git_attr_file__new(git_attr_file **attrs_ptr, git_pool *pool)
+int git_attr_file__new(
+ git_attr_file **attrs_ptr,
+ git_attr_file_source from,
+ const char *path,
+ git_pool *pool)
{
git_attr_file *attrs = NULL;
@@ -25,6 +32,18 @@ int git_attr_file__new(git_attr_file **attrs_ptr, git_pool *pool)
attrs->pool_is_allocated = true;
}
+ if (path) {
+ size_t len = strlen(path);
+
+ attrs->key = git_pool_malloc(attrs->pool, len + 3);
+ GITERR_CHECK_ALLOC(attrs->key);
+
+ attrs->key[0] = '0' + from;
+ attrs->key[1] = '#';
+ memcpy(&attrs->key[2], path, len);
+ attrs->key[len + 2] = '\0';
+ }
+
if (git_vector_init(&attrs->rules, 4, NULL) < 0)
goto fail;
@@ -37,31 +56,7 @@ fail:
return -1;
}
-int git_attr_file__set_path(
- git_repository *repo, const char *path, git_attr_file *file)
-{
- if (file->path != NULL) {
- git__free(file->path);
- file->path = NULL;
- }
-
- if (repo == NULL)
- file->path = git__strdup(path);
- else {
- const char *workdir = git_repository_workdir(repo);
-
- if (workdir && git__prefixcmp(path, workdir) == 0)
- file->path = git__strdup(path + strlen(workdir));
- else
- file->path = git__strdup(path);
- }
-
- GITERR_CHECK_ALLOC(file->path);
-
- return 0;
-}
-
-int git_attr_file__from_buffer(
+int git_attr_file__parse_buffer(
git_repository *repo, const char *buffer, git_attr_file *attrs)
{
int error = 0;
@@ -73,10 +68,10 @@ int git_attr_file__from_buffer(
scan = buffer;
- if (attrs->path && git__suffixcmp(attrs->path, GIT_ATTR_FILE) == 0) {
- context = git__strndup(attrs->path,
- strlen(attrs->path) - strlen(GIT_ATTR_FILE));
- GITERR_CHECK_ALLOC(context);
+ /* if subdir file path, convert context for file paths */
+ if (attrs->key && git__suffixcmp(attrs->key, "/" GIT_ATTR_FILE) == 0) {
+ context = attrs->key + 2;
+ context[strlen(context) - strlen(GIT_ATTR_FILE)] = '\0';
}
while (!error && *scan) {
@@ -112,28 +107,34 @@ int git_attr_file__from_buffer(
}
git_attr_rule__free(rule);
- git__free(context);
+
+ /* restore file path used for context */
+ if (context)
+ context[strlen(context)] = '.'; /* first char of GIT_ATTR_FILE */
return error;
}
-int git_attr_file__from_file(
- git_repository *repo, const char *path, git_attr_file *file)
+int git_attr_file__new_and_load(
+ git_attr_file **attrs_ptr,
+ const char *path)
{
int error;
- git_buf fbuf = GIT_BUF_INIT;
+ git_buf content = GIT_BUF_INIT;
- assert(path && file);
+ if ((error = git_attr_file__new(attrs_ptr, 0, path, NULL)) < 0)
+ return error;
- if (file->path == NULL && git_attr_file__set_path(repo, path, file) < 0)
- return -1;
-
- if (git_futils_readbuffer(&fbuf, path) < 0)
- return -1;
+ if (!(error = git_futils_readbuffer(&content, path)))
+ error = git_attr_file__parse_buffer(
+ NULL, git_buf_cstr(&content), *attrs_ptr);
- error = git_attr_file__from_buffer(repo, fbuf.ptr, file);
+ git_buf_free(&content);
- git_buf_free(&fbuf);
+ if (error) {
+ git_attr_file__free(*attrs_ptr);
+ *attrs_ptr = NULL;
+ }
return error;
}
@@ -151,9 +152,6 @@ void git_attr_file__free(git_attr_file *file)
git_vector_free(&file->rules);
- git__free(file->path);
- file->path = NULL;
-
if (file->pool_is_allocated) {
git_pool_clear(file->pool);
git__free(file->pool);
@@ -504,7 +502,7 @@ int git_attr_assignment__parse(
assign->value = git_attr__false;
scan++;
} else if (*scan == '!') {
- assign->value = NULL; /* explicit unspecified state */
+ assign->value = git_attr__unset; /* explicit unspecified state */
scan++;
} else if (*scan == '#') /* comment rest of line */
break;
diff --git a/src/attr_file.h b/src/attr_file.h
index 10851bc..ec488c4 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -48,7 +48,7 @@ typedef struct {
} git_attr_assignment;
typedef struct {
- char *path; /* cache the path this was loaded from */
+ char *key; /* cache "source#path" this was loaded from */
git_vector rules; /* vector of <rule*> or <fnmatch*> */
git_pool *pool;
bool pool_is_allocated;
@@ -61,20 +61,25 @@ 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_pool *pool);
+extern int git_attr_file__new(
+ git_attr_file **attrs_ptr, git_attr_file_source src, const char *path, git_pool *pool);
+
+extern int git_attr_file__new_and_load(
+ git_attr_file **attrs_ptr, const char *path);
+
extern void git_attr_file__free(git_attr_file *file);
-extern int git_attr_file__from_buffer(
+extern int git_attr_file__parse_buffer(
git_repository *repo, const char *buf, git_attr_file *file);
-extern int git_attr_file__from_file(
- git_repository *repo, const char *path, git_attr_file *file);
-
-extern int git_attr_file__set_path(
- git_repository *repo, const char *path, git_attr_file *file);
extern int git_attr_file__lookup_one(
git_attr_file *file,
diff --git a/src/crlf.c b/src/crlf.c
index b495d2d..5d09a1f 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -82,7 +82,8 @@ static int crlf_load_attributes(struct crlf_attrs *ca, git_repository *repo, con
const char *attr_vals[NUM_CONV_ATTRS];
int error;
- error = git_attr_get_many(repo, path, NUM_CONV_ATTRS, attr_names, attr_vals);
+ error = git_attr_get_many(
+ repo, 0, path, NUM_CONV_ATTRS, attr_names, attr_vals);
if (error == GIT_ENOTFOUND) {
ca->crlf_action = GIT_CRLF_GUESS;
diff --git a/src/diff_output.c b/src/diff_output.c
index ca28fd0..c380db9 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -103,7 +103,7 @@ static int diff_output_cb(void *priv, mmbuffer_t *bufs, int len)
static int update_file_is_binary_by_attr(git_repository *repo, git_diff_file *file)
{
const char *value;
- if (git_attr_get(repo, file->path, "diff", &value) < 0)
+ if (git_attr_get(repo, 0, file->path, "diff", &value) < 0)
return -1;
if (GIT_ATTR_FALSE(value))
diff --git a/src/ignore.c b/src/ignore.c
index 20b96c6..6f70b97 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -5,29 +5,22 @@
#define GIT_IGNORE_FILE_INREPO "info/exclude"
#define GIT_IGNORE_FILE ".gitignore"
-static int load_ignore_file(
- git_repository *repo, const char *path, git_attr_file *ignores)
+static int parse_ignore_file(
+ git_repository *repo, const char *buffer, git_attr_file *ignores)
{
int error;
- git_buf fbuf = GIT_BUF_INIT;
git_attr_fnmatch *match = NULL;
const char *scan = NULL;
char *context = NULL;
- if (ignores->path == NULL) {
- if (git_attr_file__set_path(repo, path, ignores) < 0)
- return -1;
- }
+ GIT_UNUSED(repo);
- if (git__suffixcmp(ignores->path, GIT_IGNORE_FILE) == 0) {
- context = git__strndup(ignores->path,
- strlen(ignores->path) - strlen(GIT_IGNORE_FILE));
- GITERR_CHECK_ALLOC(context);
+ if (ignores->key && git__suffixcmp(ignores->key, "/" GIT_IGNORE_FILE) == 0) {
+ context = ignores->key + 2;
+ context[strlen(context) - strlen(GIT_IGNORE_FILE)] = '\0';
}
- error = git_futils_readbuffer(&fbuf, path);
-
- scan = fbuf.ptr;
+ scan = buffer;
while (!error && *scan) {
if (!match) {
@@ -54,23 +47,27 @@ static int load_ignore_file(
}
}
- git_buf_free(&fbuf);
git__free(match);
- git__free(context);
+ /* restore file path used for context */
+ if (context)
+ context[strlen(context)] = '.'; /* first char of GIT_IGNORE_FILE */
return error;
}
-#define push_ignore(R,S,B,F) \
- git_attr_cache__push_file((R),(S),(B),(F),load_ignore_file)
+#define push_ignore_file(R,S,B,F) \
+ git_attr_cache__push_file((R),(B),(F),GIT_ATTR_FILE_FROM_FILE,parse_ignore_file,(S))
static int push_one_ignore(void *ref, git_buf *path)
{
git_ignores *ign = (git_ignores *)ref;
- return push_ignore(ign->repo, &ign->ign_path, path->ptr, GIT_IGNORE_FILE);
+ return push_ignore_file(ign->repo, &ign->ign_path, path->ptr, GIT_IGNORE_FILE);
}
-int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *ignores)
+int git_ignore__for_path(
+ git_repository *repo,
+ const char *path,
+ git_ignores *ignores)
{
int error = 0;
const char *workdir = git_repository_workdir(repo);
@@ -86,30 +83,37 @@ int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *ig
(error = git_attr_cache__init(repo)) < 0)
goto cleanup;
- /* translate path into directory within workdir */
- if ((error = git_path_find_dir(&ignores->dir, path, workdir)) < 0)
+ /* given a unrooted path in a non-bare repo, resolve it */
+ if (workdir && git_path_root(path) < 0)
+ error = git_path_find_dir(&ignores->dir, path, workdir);
+ else
+ error = git_buf_sets(&ignores->dir, path);
+ if (error < 0)
goto cleanup;
/* set up internals */
- error = git_attr_cache__lookup_or_create_file(
- repo, GIT_IGNORE_INTERNAL, NULL, NULL, &ignores->ign_internal);
+ error = git_attr_cache__internal_file(
+ repo, GIT_IGNORE_INTERNAL, &ignores->ign_internal);
if (error < 0)
goto cleanup;
/* load .gitignore up the path */
- error = git_path_walk_up(&ignores->dir, workdir, push_one_ignore, ignores);
- if (error < 0)
- goto cleanup;
+ if (workdir != NULL) {
+ error = git_path_walk_up(
+ &ignores->dir, workdir, push_one_ignore, ignores);
+ if (error < 0)
+ goto cleanup;
+ }
/* load .git/info/exclude */
- error = push_ignore(repo, &ignores->ign_global,
+ error = push_ignore_file(repo, &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(repo, &ignores->ign_global, NULL,
+ error = push_ignore_file(repo, &ignores->ign_global, NULL,
git_repository_attr_cache(repo)->cfg_excl_file);
cleanup:
@@ -124,7 +128,7 @@ int git_ignore__push_dir(git_ignores *ign, const char *dir)
if (git_buf_joinpath(&ign->dir, ign->dir.ptr, dir) < 0)
return -1;
else
- return push_ignore(
+ return push_ignore_file(
ign->repo, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE);
}
@@ -132,7 +136,7 @@ int git_ignore__pop_dir(git_ignores *ign)
{
if (ign->ign_path.length > 0) {
git_attr_file *file = git_vector_last(&ign->ign_path);
- if (git__suffixcmp(ign->dir.ptr, file->path) == 0)
+ if (git__suffixcmp(ign->dir.ptr, file->key + 2) == 0)
git_vector_pop(&ign->ign_path);
git_buf_rtruncate_at_char(&ign->dir, '/');
}
@@ -163,7 +167,8 @@ static bool ignore_lookup_in_rules(
return false;
}
-int git_ignore__lookup(git_ignores *ignores, const char *pathname, int *ignored)
+int git_ignore__lookup(
+ git_ignores *ignores, const char *pathname, int *ignored)
{
unsigned int i;
git_attr_file *file;
diff --git a/src/ignore.h b/src/ignore.h
index 49f72bf..809d2ed 100644
--- a/src/ignore.h
+++ b/src/ignore.h
@@ -25,13 +25,14 @@ typedef struct {
git_vector ign_global;
} git_ignores;
-extern int git_ignore__for_path(
- git_repository *repo, const char *path, git_ignores *ign);
+extern int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *ign);
extern int git_ignore__push_dir(git_ignores *ign, const char *dir);
+
extern int git_ignore__pop_dir(git_ignores *ign);
extern void git_ignore__free(git_ignores *ign);
+
extern int git_ignore__lookup(git_ignores *ign, const char *path, int *ignored);
#endif
diff --git a/src/object.c b/src/object.c
index 8e8eac4..7189d60 100644
--- a/src/object.c
+++ b/src/object.c
@@ -292,3 +292,42 @@ size_t git_object__size(git_otype type)
return git_objects_table[type].size;
}
+int git_object__resolve_to_type(git_object **obj, git_otype type)
+{
+ int error = 0;
+ git_object *scan, *next;
+
+ if (type == GIT_OBJ_ANY)
+ return 0;
+
+ scan = *obj;
+
+ while (!error && scan && git_object_type(scan) != type) {
+
+ switch (git_object_type(scan)) {
+ case GIT_OBJ_COMMIT:
+ {
+ git_tree *tree = NULL;
+ error = git_commit_tree(&tree, (git_commit *)scan);
+ next = (git_object *)tree;
+ break;
+ }
+
+ case GIT_OBJ_TAG:
+ error = git_tag_target(&next, (git_tag *)scan);
+ break;
+
+ default:
+ giterr_set(GITERR_REFERENCE, "Object does not resolve to type");
+ error = -1;
+ next = NULL;
+ break;
+ }
+
+ git_object_free(scan);
+ scan = next;
+ }
+
+ *obj = scan;
+ return error;
+}
diff --git a/src/repository.c b/src/repository.c
index cfabee4..d4de381 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -862,3 +862,23 @@ int git_repository_is_bare(git_repository *repo)
assert(repo);
return repo->is_bare;
}
+
+int git_repository_head_tree(git_tree **tree, git_repository *repo)
+{
+ git_oid head_oid;
+ git_object *obj = NULL;
+
+ if (git_reference_name_to_oid(&head_oid, repo, GIT_HEAD_FILE) < 0) {
+ /* cannot resolve HEAD - probably brand new repo */
+ giterr_clear();
+ *tree = NULL;
+ return 0;
+ }
+
+ if (git_object_lookup(&obj, repo, &head_oid, GIT_OBJ_ANY) < 0 ||
+ git_object__resolve_to_type(&obj, GIT_OBJ_TREE) < 0)
+ return -1;
+
+ *tree = (git_tree *)obj;
+ return 0;
+}
diff --git a/src/repository.h b/src/repository.h
index 1ffac58..91c69a6 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -98,6 +98,8 @@ struct git_repository {
* export */
void git_object__free(void *object);
+int git_object__resolve_to_type(git_object **obj, git_otype type);
+
int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header);
void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid);
@@ -106,6 +108,8 @@ GIT_INLINE(git_attr_cache *) git_repository_attr_cache(git_repository *repo)
return &repo->attrcache;
}
+int git_repository_head_tree(git_tree **tree, git_repository *repo);
+
/*
* Weak pointers to repository internals.
*
diff --git a/src/status.c b/src/status.c
index 356cbeb..ff8535c 100644
--- a/src/status.c
+++ b/src/status.c
@@ -18,41 +18,6 @@
#include "git2/diff.h"
#include "diff.h"
-static int resolve_head_to_tree(git_tree **tree, git_repository *repo)
-{
- git_oid head_oid;
- git_object *obj = NULL;
-
- if (git_reference_name_to_oid(&head_oid, repo, GIT_HEAD_FILE) < 0) {
- /* cannot resolve HEAD - probably brand new repo */
- giterr_clear();
- *tree = NULL;
- return 0;
- }
-
- if (git_object_lookup(&obj, repo, &head_oid, GIT_OBJ_ANY) < 0)
- goto fail;
-
- switch (git_object_type(obj)) {
- case GIT_OBJ_TREE:
- *tree = (git_tree *)obj;
- break;
- case GIT_OBJ_COMMIT:
- if (git_commit_tree(tree, (git_commit *)obj) < 0)
- goto fail;
- git_object_free(obj);
- break;
- default:
- goto fail;
- }
-
- return 0;
-
-fail:
- git_object_free(obj);
- return -1;
-}
-
static unsigned int index_delta2status(git_delta_t index_status)
{
unsigned int st = GIT_STATUS_CURRENT;
@@ -120,11 +85,8 @@ int git_status_foreach_ext(
assert(show <= GIT_STATUS_SHOW_INDEX_THEN_WORKDIR);
- switch (resolve_head_to_tree(&head, repo)) {
- case 0: break;
- case GIT_ENOTFOUND: return 0;
- default: return -1;
- }
+ if ((err = git_repository_head_tree(&head, repo)) < 0)
+ return err;
memset(&diffopt, 0, sizeof(diffopt));
memcpy(&diffopt.pathspec, &opts->pathspec, sizeof(diffopt.pathspec));
@@ -405,7 +367,7 @@ int git_status_file(
status_entry_update_from_index(e, index);
/* Try to find file in HEAD */
- if ((error = resolve_head_to_tree(&tree, repo)) < 0)
+ if ((error = git_repository_head_tree(&tree, repo)) < 0)
goto cleanup;
if (tree != NULL) {
diff --git a/tests-clar/attr/file.c b/tests-clar/attr/file.c
index 4e10102..d197088 100644
--- a/tests-clar/attr/file.c
+++ b/tests-clar/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(&file, NULL));
- cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr0"), file));
- cl_assert_equal_s(cl_fixture("attr/attr0"), file->path);
+ 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_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(&file, NULL));
- cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr1"), file));
- cl_assert_equal_s(cl_fixture("attr/attr1"), file->path);
+ 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_assert(file->rules.length == 10);
/* let's do a thorough check of this rule, then just verify
@@ -123,9 +123,9 @@ void test_attr_file__assign_variants(void)
git_attr_rule *rule;
git_attr_assignment *assign;
- cl_git_pass(git_attr_file__new(&file, NULL));
- cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr2"), file));
- cl_assert_equal_s(cl_fixture("attr/attr2"), file->path);
+ cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr2")));
+
+ cl_assert_equal_s(cl_fixture("attr/attr2"), file->key + 2);
cl_assert(file->rules.length == 11);
check_one_assign(file, 0, 0, "pat0", "simple", EXPECT_TRUE, NULL);
@@ -189,9 +189,8 @@ void test_attr_file__check_attr_examples(void)
git_attr_rule *rule;
git_attr_assignment *assign;
- cl_git_pass(git_attr_file__new(&file, NULL));
- cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr3"), file));
- cl_assert_equal_s(cl_fixture("attr/attr3"), file->path);
+ 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_assert(file->rules.length == 3);
rule = get_rule(0);
@@ -214,7 +213,7 @@ void test_attr_file__check_attr_examples(void)
cl_assert(rule->assigns.length == 1);
assign = get_assign(rule, 0);
cl_assert_equal_s("myAttr", assign->name);
- cl_assert(assign->value == NULL);
+ cl_assert(GIT_ATTR_UNSPECIFIED(assign->value));
rule = get_rule(2);
cl_assert_equal_s("README", rule->match.pattern);
diff --git a/tests-clar/attr/flags.c b/tests-clar/attr/flags.c
new file mode 100644
index 0000000..5081de8
--- /dev/null
+++ b/tests-clar/attr/flags.c
@@ -0,0 +1,108 @@
+#include "clar_libgit2.h"
+#include "git2/attr.h"
+
+void test_attr_flags__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+void test_attr_flags__bare(void)
+{
+ git_repository *repo = cl_git_sandbox_init("testrepo.git");
+ const char *value;
+
+ cl_assert(git_repository_is_bare(repo));
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM, "README.md", "diff", &value));
+ cl_assert(GIT_ATTR_UNSPECIFIED(value));
+}
+
+void test_attr_flags__index_vs_workdir(void)
+{
+ git_repository *repo = cl_git_sandbox_init("attr_index");
+ const char *value;
+
+ cl_assert(!git_repository_is_bare(repo));
+
+ /* wd then index */
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
+ "README.md", "bar", &value));
+ cl_assert(GIT_ATTR_FALSE(value));
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
+ "README.md", "blargh", &value));
+ cl_assert_equal_s(value, "goop");
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
+ "README.txt", "foo", &value));
+ cl_assert(GIT_ATTR_FALSE(value));
+
+ /* index then wd */
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
+ "README.md", "bar", &value));
+ cl_assert(GIT_ATTR_TRUE(value));
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
+ "README.md", "blargh", &value));
+ cl_assert_equal_s(value, "garble");
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
+ "README.txt", "foo", &value));
+ cl_assert(GIT_ATTR_TRUE(value));
+}
+
+void test_attr_flags__subdir(void)
+{
+ git_repository *repo = cl_git_sandbox_init("attr_index");
+ const char *value;
+
+ /* wd then index */
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
+ "sub/sub/README.md", "bar", &value));
+ cl_assert_equal_s(value, "1234");
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
+ "sub/sub/README.txt", "another", &value));
+ cl_assert_equal_s(value, "one");
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
+ "sub/sub/README.txt", "again", &value));
+ cl_assert(GIT_ATTR_TRUE(value));
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_FILE_THEN_INDEX,
+ "sub/sub/README.txt", "beep", &value));
+ cl_assert_equal_s(value, "10");
+
+ /* index then wd */
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
+ "sub/sub/README.md", "bar", &value));
+ cl_assert_equal_s(value, "1337");
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
+ "sub/sub/README.txt", "another", &value));
+ cl_assert_equal_s(value, "one");
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
+ "sub/sub/README.txt", "again", &value));
+ cl_assert(GIT_ATTR_TRUE(value));
+
+ cl_git_pass(git_attr_get(
+ repo, GIT_ATTR_CHECK_NO_SYSTEM | GIT_ATTR_CHECK_INDEX_THEN_FILE,
+ "sub/sub/README.txt", "beep", &value));
+ cl_assert_equal_s(value, "5");
+}
+
diff --git a/tests-clar/attr/lookup.c b/tests-clar/attr/lookup.c
index 81a4a55..b2a6aac 100644
--- a/tests-clar/attr/lookup.c
+++ b/tests-clar/attr/lookup.c
@@ -9,9 +9,8 @@ void test_attr_lookup__simple(void)
git_attr_path path;
const char *value = NULL;
- cl_git_pass(git_attr_file__new(&file, NULL));
- cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr0"), file));
- cl_assert_equal_s(cl_fixture("attr/attr0"), file->path);
+ 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_assert(file->rules.length == 1);
cl_git_pass(git_attr_path__init(&path, "test", NULL));
@@ -130,9 +129,8 @@ void test_attr_lookup__match_variants(void)
{ NULL, NULL, 0, NULL }
};
- cl_git_pass(git_attr_file__new(&file, NULL));
- cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr1"), file));
- cl_assert_equal_s(cl_fixture("attr/attr1"), file->path);
+ 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_assert(file->rules.length == 10);
cl_git_pass(git_attr_path__init(&path, "/testing/for/pat0", NULL));
@@ -192,8 +190,7 @@ void test_attr_lookup__assign_variants(void)
{ NULL, NULL, 0, NULL }
};
- cl_git_pass(git_attr_file__new(&file, NULL));
- cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr2"), file));
+ cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr2")));
cl_assert(file->rules.length == 11);
run_test_cases(file, cases, 0);
@@ -228,8 +225,7 @@ void test_attr_lookup__check_attr_examples(void)
{ NULL, NULL, 0, NULL }
};
- cl_git_pass(git_attr_file__new(&file, NULL));
- cl_git_pass(git_attr_file__from_file(NULL, cl_fixture("attr/attr3"), file));
+ cl_git_pass(git_attr_file__new_and_load(&file, cl_fixture("attr/attr3")));
cl_assert(file->rules.length == 3);
run_test_cases(file, cases, 0);
@@ -254,8 +250,10 @@ void test_attr_lookup__from_buffer(void)
{ NULL, NULL, 0, NULL }
};
- cl_git_pass(git_attr_file__new(&file, NULL));
- cl_git_pass(git_attr_file__from_buffer(NULL, "a* foo\nabc bar\n* baz", file));
+ cl_git_pass(git_attr_file__new(&file, 0, NULL, NULL));
+
+ cl_git_pass(git_attr_file__parse_buffer(NULL, "a* foo\nabc bar\n* baz", file));
+
cl_assert(file->rules.length == 3);
run_test_cases(file, cases, 0);
diff --git a/tests-clar/attr/repo.c b/tests-clar/attr/repo.c
index 7423c30..006a490 100644
--- a/tests-clar/attr/repo.c
+++ b/tests-clar/attr/repo.c
@@ -64,13 +64,13 @@ void test_attr_repo__get_one(void)
for (scan = test_cases; scan->path != NULL; scan++) {
const char *value;
- cl_git_pass(git_attr_get(g_repo, scan->path, scan->attr, &value));
+ cl_git_pass(git_attr_get(g_repo, 0, scan->path, scan->attr, &value));
attr_check_expected(scan->expected, scan->expected_str, value);
}
- cl_assert(git_attr_cache__is_cached(g_repo, ".git/info/attributes"));
- cl_assert(git_attr_cache__is_cached(g_repo, ".gitattributes"));
- cl_assert(git_attr_cache__is_cached(g_repo, "sub/.gitattributes"));
+ cl_assert(git_attr_cache__is_cached(g_repo, 0, ".git/info/attributes"));
+ cl_assert(git_attr_cache__is_cached(g_repo, 0, ".gitattributes"));
+ cl_assert(git_attr_cache__is_cached(g_repo, 0, "sub/.gitattributes"));
}
void test_attr_repo__get_many(void)
@@ -78,21 +78,21 @@ void test_attr_repo__get_many(void)
const char *names[4] = { "repoattr", "rootattr", "missingattr", "subattr" };
const char *values[4];
- cl_git_pass(git_attr_get_many(g_repo, "root_test1", 4, names, values));
+ cl_git_pass(git_attr_get_many(g_repo, 0, "root_test1", 4, names, values));
cl_assert(GIT_ATTR_TRUE(values[0]));
cl_assert(GIT_ATTR_TRUE(values[1]));
cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
- cl_git_pass(git_attr_get_many(g_repo, "root_test2", 4, names, values));
+ cl_git_pass(git_attr_get_many(g_repo, 0, "root_test2", 4, names, values));
cl_assert(GIT_ATTR_TRUE(values[0]));
cl_assert(GIT_ATTR_FALSE(values[1]));
cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
- cl_git_pass(git_attr_get_many(g_repo, "sub/subdir_test1", 4, names, values));
+ cl_git_pass(git_attr_get_many(g_repo, 0, "sub/subdir_test1", 4, names, values));
cl_assert(GIT_ATTR_TRUE(values[0]));
cl_assert(GIT_ATTR_TRUE(values[1]));
@@ -118,16 +118,17 @@ void test_attr_repo__foreach(void)
int count;
count = 0;
- cl_git_pass(git_attr_foreach(g_repo, "root_test1", &count_attrs, &count));
+ cl_git_pass(git_attr_foreach(
+ g_repo, 0, "root_test1", &count_attrs, &count));
cl_assert(count == 2);
count = 0;
- cl_git_pass(git_attr_foreach(g_repo, "sub/subdir_test1",
+ cl_git_pass(git_attr_foreach(g_repo, 0, "sub/subdir_test1",
&count_attrs, &count));
cl_assert(count == 4); /* repoattr, rootattr, subattr, negattr */
count = 0;
- cl_git_pass(git_attr_foreach(g_repo, "sub/subdir_test2.txt",
+ cl_git_pass(git_attr_foreach(g_repo, 0, "sub/subdir_test2.txt",
&count_attrs, &count));
cl_assert(count == 6); /* repoattr, rootattr, subattr, reposub, negattr, another */
}
@@ -136,19 +137,19 @@ void test_attr_repo__manpage_example(void)
{
const char *value;
- cl_git_pass(git_attr_get(g_repo, "sub/abc", "foo", &value));
+ cl_git_pass(git_attr_get(g_repo, 0, "sub/abc", "foo", &value));
cl_assert(GIT_ATTR_TRUE(value));
- cl_git_pass(git_attr_get(g_repo, "sub/abc", "bar", &value));
+ cl_git_pass(git_attr_get(g_repo, 0, "sub/abc", "bar", &value));
cl_assert(GIT_ATTR_UNSPECIFIED(value));
- cl_git_pass(git_attr_get(g_repo, "sub/abc", "baz", &value));
+ cl_git_pass(git_attr_get(g_repo, 0, "sub/abc", "baz", &value));
cl_assert(GIT_ATTR_FALSE(value));
- cl_git_pass(git_attr_get(g_repo, "sub/abc", "merge", &value));
+ cl_git_pass(git_attr_get(g_repo, 0, "sub/abc", "merge", &value));
cl_assert_equal_s("filfre", value);
- cl_git_pass(git_attr_get(g_repo, "sub/abc", "frotz", &value));
+ cl_git_pass(git_attr_get(g_repo, 0, "sub/abc", "frotz", &value));
cl_assert(GIT_ATTR_UNSPECIFIED(value));
}
@@ -159,7 +160,7 @@ void test_attr_repo__macros(void)
const char *names3[3] = { "macro2", "multi2", "multi3" };
const char *values[5];
- cl_git_pass(git_attr_get_many(g_repo, "binfile", 5, names, values));
+ cl_git_pass(git_attr_get_many(g_repo, 0, "binfile", 5, names, values));
cl_assert(GIT_ATTR_TRUE(values[0]));
cl_assert(GIT_ATTR_TRUE(values[1]));
@@ -167,7 +168,7 @@ void test_attr_repo__macros(void)
cl_assert(GIT_ATTR_FALSE(values[3]));
cl_assert(GIT_ATTR_UNSPECIFIED(values[4]));
- cl_git_pass(git_attr_get_many(g_repo, "macro_test", 5, names2, values));
+ cl_git_pass(git_attr_get_many(g_repo, 0, "macro_test", 5, names2, values));
cl_assert(GIT_ATTR_TRUE(values[0]));
cl_assert(GIT_ATTR_TRUE(values[1]));
@@ -175,7 +176,7 @@ void test_attr_repo__macros(void)
cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
cl_assert_equal_s("77", values[4]);
- cl_git_pass(git_attr_get_many(g_repo, "macro_test", 3, names3, values));
+ cl_git_pass(git_attr_get_many(g_repo, 0, "macro_test", 3, names3, values));
cl_assert(GIT_ATTR_TRUE(values[0]));
cl_assert(GIT_ATTR_FALSE(values[1]));
@@ -188,7 +189,7 @@ void test_attr_repo__bad_macros(void)
"firstmacro", "secondmacro", "thirdmacro" };
const char *values[6];
- cl_git_pass(git_attr_get_many(g_repo, "macro_bad", 6, names, values));
+ cl_git_pass(git_attr_get_many(g_repo, 0, "macro_bad", 6, names, values));
/* these three just confirm that the "mymacro" rule ran */
cl_assert(GIT_ATTR_UNSPECIFIED(values[0]));
diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c
index 1e269ae..3fb4877 100644
--- a/tests-clar/diff/tree.c
+++ b/tests-clar/diff/tree.c
@@ -5,7 +5,6 @@ static git_repository *g_repo = NULL;
void test_diff_tree__initialize(void)
{
- g_repo = cl_git_sandbox_init("attr");
}
void test_diff_tree__cleanup(void)
@@ -19,15 +18,16 @@ void test_diff_tree__0(void)
const char *a_commit = "605812a";
const char *b_commit = "370fe9ec22";
const char *c_commit = "f5b0af1fb4f5c";
- git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
- git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit);
- git_tree *c = resolve_commit_oid_to_tree(g_repo, c_commit);
+ git_tree *a, *b, *c;
git_diff_options opts = {0};
git_diff_list *diff = NULL;
diff_expects exp;
- cl_assert(a);
- cl_assert(b);
+ g_repo = cl_git_sandbox_init("attr");
+
+ cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
+ cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
+ cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL);
opts.context_lines = 1;
opts.interhunk_lines = 1;
@@ -87,12 +87,7 @@ void test_diff_tree__options(void)
const char *b_commit = "605812ab7fe421fdd";
const char *c_commit = "f5b0af1fb4f5";
const char *d_commit = "a97cc019851";
-
- git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
- git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit);
- git_tree *c = resolve_commit_oid_to_tree(g_repo, c_commit);
- git_tree *d = resolve_commit_oid_to_tree(g_repo, d_commit);
-
+ git_tree *a, *b, *c, *d;
git_diff_options opts = {0};
git_diff_list *diff = NULL;
diff_expects actual;
@@ -133,8 +128,12 @@ void test_diff_tree__options(void)
diff_expects *expected;
int i;
- cl_assert(a);
- cl_assert(b);
+ g_repo = cl_git_sandbox_init("attr");
+
+ cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
+ cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
+ cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL);
+ cl_assert((d = resolve_commit_oid_to_tree(g_repo, d_commit)) != NULL);
for (i = 0; test_expects[i].files > 0; i++) {
memset(&actual, 0, sizeof(actual)); /* clear accumulator */
@@ -168,3 +167,42 @@ void test_diff_tree__options(void)
git_tree_free(c);
git_tree_free(d);
}
+
+void test_diff_tree__bare(void)
+{
+ const char *a_commit = "8496071c1b46c85";
+ const char *b_commit = "be3563ae3f79";
+ git_tree *a, *b;
+ git_diff_options opts = {0};
+ git_diff_list *diff = NULL;
+ diff_expects exp;
+
+ g_repo = cl_git_sandbox_init("testrepo.git");
+
+ cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
+ cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
+
+ opts.context_lines = 1;
+ opts.interhunk_lines = 1;
+
+ memset(&exp, 0, sizeof(exp));
+
+ cl_git_pass(git_diff_tree_to_tree(g_repo, &opts, a, b, &diff));
+
+ cl_git_pass(git_diff_foreach(
+ diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn));
+
+ cl_assert(exp.files == 3);
+ cl_assert(exp.file_adds == 2);
+ cl_assert(exp.file_dels == 0);
+ cl_assert(exp.file_mods == 1);
+
+ cl_assert(exp.hunks == 3);
+
+ cl_assert(exp.lines == 4);
+ cl_assert(exp.line_ctxt == 0);
+ cl_assert(exp.line_adds == 3);
+ cl_assert(exp.line_dels == 1);
+
+ git_diff_list_free(diff);
+}
diff --git a/tests-clar/resources/attr_index/.gitted/HEAD b/tests-clar/resources/attr_index/.gitted/HEAD
new file mode 100644
index 0000000..cb089cd
--- /dev/null
+++ b/tests-clar/resources/attr_index/.gitted/HEAD
@@ -0,0 +1 @@
+ref: refs/heads/master
diff --git a/tests-clar/resources/attr_index/.gitted/config b/tests-clar/resources/attr_index/.gitted/config
new file mode 100644
index 0000000..af10792
--- /dev/null
+++ b/tests-clar/resources/attr_index/.gitted/config
@@ -0,0 +1,6 @@
+[core]
+ repositoryformatversion = 0
+ filemode = true
+ bare = false
+ logallrefupdates = true
+ ignorecase = true
diff --git a/tests-clar/resources/attr_index/.gitted/description b/tests-clar/resources/attr_index/.gitted/description
new file mode 100644
index 0000000..498b267
--- /dev/null
+++ b/tests-clar/resources/attr_index/.gitted/description
@@ -0,0 +1 @@
+Unnamed repository; edit this file 'description' to name the repository.
diff --git a/tests-clar/resources/attr_index/.gitted/index b/tests-clar/resources/attr_index/.gitted/index
new file mode 100644
index 0000000..4e6343b
Binary files /dev/null and b/tests-clar/resources/attr_index/.gitted/index differ
diff --git a/tests-clar/resources/attr_index/.gitted/info/exclude b/tests-clar/resources/attr_index/.gitted/info/exclude
new file mode 100644
index 0000000..a5196d1
--- /dev/null
+++ b/tests-clar/resources/attr_index/.gitted/info/exclude
@@ -0,0 +1,6 @@
+# git ls-files --others --exclude-from=.git/info/exclude
+# Lines that start with '#' are comments.
+# For a project mostly in C, the following would be a good set of
+# exclude patterns (uncomment them if you want to use them):
+# *.[oa]
+# *~
diff --git a/tests-clar/resources/attr_index/.gitted/info/refs b/tests-clar/resources/attr_index/.gitted/info/refs
new file mode 100644
index 0000000..60feca2
--- /dev/null
+++ b/tests-clar/resources/attr_index/.gitted/info/refs
@@ -0,0 +1 @@
+58f7cf825b553ef7c26e5b9f8a23599c1a9ca296 refs/heads/master
diff --git a/tests-clar/resources/attr_index/.gitted/logs/HEAD b/tests-clar/resources/attr_index/.gitted/logs/HEAD
new file mode 100644
index 0000000..ffd298c
--- /dev/null
+++ b/tests-clar/resources/attr_index/.gitted/logs/HEAD
@@ -0,0 +1,4 @@
+0000000000000000000000000000000000000000 67c1640e91ccbaf0793591be09bf572cf40c9a53 Russell Belfer <rb@github.com> 1335817070 -0700 commit (initial): Initial commit
+67c1640e91ccbaf0793591be09bf572cf40c9a53 d441d7d88f52c28c2b23940ce4c33756748425f9 Russell Belfer <rb@github.com> 1335817296 -0700 commit: Adding some files in subtrees
+d441d7d88f52c28c2b23940ce4c33756748425f9 67c1640e91ccbaf0793591be09bf572cf40c9a53 Russell Belfer <rb@github.com> 1335817353 -0700 HEAD^: updating HEAD
+67c1640e91ccbaf0793591be09bf572cf40c9a53 58f7cf825b553ef7c26e5b9f8a23599c1a9ca296 Russell Belfer <rb@github.com> 1335817372 -0700 commit: Adding subtree data
diff --git a/tests-clar/resources/attr_index/.gitted/logs/refs/heads/master b/tests-clar/resources/attr_index/.gitted/logs/refs/heads/master
new file mode 100644
index 0000000..ffd298c
--- /dev/null
+++ b/tests-clar/resources/attr_index/.gitted/logs/refs/heads/master
@@ -0,0 +1,4 @@
+0000000000000000000000000000000000000000 67c1640e91ccbaf0793591be09bf572cf40c9a53 Russell Belfer <rb@github.com> 1335817070 -0700 commit (initial): Initial commit
+67c1640e91ccbaf0793591be09bf572cf40c9a53 d441d7d88f52c28c2b23940ce4c33756748425f9 Russell Belfer <rb@github.com> 1335817296 -0700 commit: Adding some files in subtrees
+d441d7d88f52c28c2b23940ce4c33756748425f9 67c1640e91ccbaf0793591be09bf572cf40c9a53 Russell Belfer <rb@github.com> 1335817353 -0700 HEAD^: updating HEAD
+67c1640e91ccbaf0793591be09bf572cf40c9a53 58f7cf825b553ef7c26e5b9f8a23599c1a9ca296 Russell Belfer <rb@github.com> 1335817372 -0700 commit: Adding subtree data
diff --git a/tests-clar/resources/attr_index/.gitted/objects/cd/f17ea3fe625ef812f4dce7f423f4f299287505 b/tests-clar/resources/attr_index/.gitted/objects/cd/f17ea3fe625ef812f4dce7f423f4f299287505
new file mode 100644
index 0000000..2a41005
Binary files /dev/null and b/tests-clar/resources/attr_index/.gitted/objects/cd/f17ea3fe625ef812f4dce7f423f4f299287505 differ
diff --git a/tests-clar/resources/attr_index/.gitted/objects/info/packs b/tests-clar/resources/attr_index/.gitted/objects/info/packs
new file mode 100644
index 0000000..559dc74
--- /dev/null
+++ b/tests-clar/resources/attr_index/.gitted/objects/info/packs
@@ -0,0 +1,2 @@
+P pack-4e6438607204ce78827e3885594b2c0bb4f13895.pack
+
diff --git a/tests-clar/resources/attr_index/.gitted/objects/pack/pack-4e6438607204ce78827e3885594b2c0bb4f13895.idx b/tests-clar/resources/attr_index/.gitted/objects/pack/pack-4e6438607204ce78827e3885594b2c0bb4f13895.idx
new file mode 100644
index 0000000..fbef4aa
Binary files /dev/null and b/tests-clar/resources/attr_index/.gitted/objects/pack/pack-4e6438607204ce78827e3885594b2c0bb4f13895.idx differ
diff --git a/tests-clar/resources/attr_index/.gitted/objects/pack/pack-4e6438607204ce78827e3885594b2c0bb4f13895.pack b/tests-clar/resources/attr_index/.gitted/objects/pack/pack-4e6438607204ce78827e3885594b2c0bb4f13895.pack
new file mode 100644
index 0000000..09c9e06
Binary files /dev/null and b/tests-clar/resources/attr_index/.gitted/objects/pack/pack-4e6438607204ce78827e3885594b2c0bb4f13895.pack differ
diff --git a/tests-clar/resources/attr_index/.gitted/packed-refs b/tests-clar/resources/attr_index/.gitted/packed-refs
new file mode 100644
index 0000000..6b3e4de
--- /dev/null
+++ b/tests-clar/resources/attr_index/.gitted/packed-refs
@@ -0,0 +1,2 @@
+# pack-refs with: peeled
+58f7cf825b553ef7c26e5b9f8a23599c1a9ca296 refs/heads/master
diff --git a/tests-clar/resources/attr_index/README.md b/tests-clar/resources/attr_index/README.md
new file mode 100644
index 0000000..0397f41
--- /dev/null
+++ b/tests-clar/resources/attr_index/README.md
@@ -0,0 +1 @@
+This is contains tests for when the index and workdir differ
diff --git a/tests-clar/resources/attr_index/README.txt b/tests-clar/resources/attr_index/README.txt
new file mode 100644
index 0000000..874c12b
--- /dev/null
+++ b/tests-clar/resources/attr_index/README.txt
@@ -0,0 +1 @@
+This contains files for testing when the index and the workdir differ
diff --git a/tests-clar/resources/attr_index/gitattributes b/tests-clar/resources/attr_index/gitattributes
new file mode 100644
index 0000000..cdf17ea
--- /dev/null
+++ b/tests-clar/resources/attr_index/gitattributes
@@ -0,0 +1,4 @@
+* bar
+*.txt -foo beep=10
+*.md blargh=goop -bar
+
diff --git a/tests-clar/resources/attr_index/sub/sub/.gitattributes b/tests-clar/resources/attr_index/sub/sub/.gitattributes
new file mode 100644
index 0000000..060c9a2
--- /dev/null
+++ b/tests-clar/resources/attr_index/sub/sub/.gitattributes
@@ -0,0 +1,3 @@
+*.txt another=one again
+*.md bar=1234
+
diff --git a/tests-clar/resources/attr_index/sub/sub/README.md b/tests-clar/resources/attr_index/sub/sub/README.md
new file mode 100644
index 0000000..59652e3
--- /dev/null
+++ b/tests-clar/resources/attr_index/sub/sub/README.md
@@ -0,0 +1 @@
+More testing
diff --git a/tests-clar/resources/attr_index/sub/sub/README.txt b/tests-clar/resources/attr_index/sub/sub/README.txt
new file mode 100644
index 0000000..59652e3
--- /dev/null
+++ b/tests-clar/resources/attr_index/sub/sub/README.txt
@@ -0,0 +1 @@
+More testing
diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c
index 94f8c3d..e92d6a5 100644
--- a/tests-clar/status/ignore.c
+++ b/tests-clar/status/ignore.c
@@ -47,8 +47,8 @@ void test_status_ignore__0(void)
}
/* confirm that ignore files were cached */
- cl_assert(git_attr_cache__is_cached(g_repo, ".git/info/exclude"));
- cl_assert(git_attr_cache__is_cached(g_repo, ".gitignore"));
+ cl_assert(git_attr_cache__is_cached(g_repo, 0, ".git/info/exclude"));
+ cl_assert(git_attr_cache__is_cached(g_repo, 0, ".gitignore"));
}