Merge pull request #570 from arrbee/uniform-iterators Uniform iterators for trees, index, and workdir
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
diff --git a/src/attr.c b/src/attr.c
index 17571f6..a7c65f9 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -218,6 +218,48 @@ int git_attr_cache__is_cached(git_repository *repo, const char *path)
return (git_hashtable_lookup(repo->attrcache.files, cache_key) == NULL);
}
+int git_attr_cache__lookup_or_create_file(
+ git_repository *repo,
+ const char *key,
+ const char *filename,
+ int (*loader)(git_repository *, const char *, git_attr_file *),
+ git_attr_file **file_ptr)
+{
+ int error;
+ git_attr_cache *cache = &repo->attrcache;
+ git_attr_file *file = NULL;
+
+ file = git_hashtable_lookup(cache->files, key);
+ if (file) {
+ *file_ptr = file;
+ return GIT_SUCCESS;
+ }
+
+ if (loader && git_path_exists(filename) != GIT_SUCCESS) {
+ *file_ptr = NULL;
+ return GIT_SUCCESS;
+ }
+
+ if ((error = git_attr_file__new(&file)) < GIT_SUCCESS)
+ return error;
+
+ if (loader)
+ error = loader(repo, filename, file);
+ else
+ error = git_attr_file__set_path(repo, key, file);
+
+ if (error == GIT_SUCCESS)
+ error = git_hashtable_insert(cache->files, file->path, file);
+
+ if (error < GIT_SUCCESS) {
+ git_attr_file__free(file);
+ file = NULL;
+ }
+
+ *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,
@@ -226,16 +268,14 @@ int git_attr_cache__push_file(
const char *filename,
int (*loader)(git_repository *, const char *, git_attr_file *))
{
- int error = GIT_SUCCESS;
- git_attr_cache *cache = &repo->attrcache;
+ int error;
git_buf path = GIT_BUF_INIT;
git_attr_file *file = NULL;
- int add_to_cache = 0;
const char *cache_key;
if (base != NULL) {
if ((error = git_buf_joinpath(&path, base, filename)) < GIT_SUCCESS)
- goto cleanup;
+ return error;
filename = path.ptr;
}
@@ -244,28 +284,12 @@ int git_attr_cache__push_file(
if (repo && git__prefixcmp(cache_key, git_repository_workdir(repo)) == 0)
cache_key += strlen(git_repository_workdir(repo));
- file = git_hashtable_lookup(cache->files, cache_key);
- if (file == NULL && git_path_exists(filename) == GIT_SUCCESS) {
- if ((error = git_attr_file__new(&file)) == GIT_SUCCESS) {
- if ((error = loader(repo, filename, file)) < GIT_SUCCESS) {
- git_attr_file__free(file);
- file = NULL;
- }
- }
- add_to_cache = (error == GIT_SUCCESS);
- }
+ error = git_attr_cache__lookup_or_create_file(
+ repo, cache_key, filename, loader, &file);
- if (error == GIT_SUCCESS && file != NULL) {
- /* add file to vector, if we found it */
+ if (error == GIT_SUCCESS && file != NULL)
error = git_vector_insert(stack, file);
- /* add file to cache, if it is new */
- /* do this after above step b/c it is not critical */
- if (error == GIT_SUCCESS && add_to_cache && file->path != NULL)
- error = git_hashtable_insert(cache->files, file->path, file);
- }
-
-cleanup:
git_buf_free(&path);
return error;
}
diff --git a/src/attr.h b/src/attr.h
index 6ae2e28..5dbbb23 100644
--- a/src/attr.h
+++ b/src/attr.h
@@ -20,6 +20,13 @@ extern int git_attr_cache__init(git_repository *repo);
extern int git_attr_cache__insert_macro(
git_repository *repo, git_attr_rule *macro);
+extern int git_attr_cache__lookup_or_create_file(
+ git_repository *repo,
+ const char *key,
+ const char *filename,
+ int (*loader)(git_repository *, const char *, git_attr_file *),
+ git_attr_file **file_ptr);
+
extern int git_attr_cache__push_file(
git_repository *repo,
git_vector *stack,
diff --git a/src/buffer.c b/src/buffer.c
index 7a186eb..183da7c 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -213,6 +213,12 @@ void git_buf_truncate(git_buf *buf, ssize_t len)
}
}
+void git_buf_rtruncate_at_char(git_buf *buf, char separator)
+{
+ int idx = git_buf_rfind_next(buf, separator);
+ git_buf_truncate(buf, idx < 0 ? 0 : idx);
+}
+
void git_buf_swap(git_buf *buf_a, git_buf *buf_b)
{
git_buf t = *buf_a;
@@ -327,7 +333,7 @@ int git_buf_join(
const char *str_b)
{
int error = GIT_SUCCESS;
- size_t strlen_a = strlen(str_a);
+ size_t strlen_a = str_a ? strlen(str_a) : 0;
size_t strlen_b = strlen(str_b);
int need_sep = 0;
ssize_t offset_a = -1;
diff --git a/src/buffer.h b/src/buffer.h
index 3a003ce..3969f46 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -84,6 +84,7 @@ int git_buf_printf(git_buf *buf, const char *format, ...) GIT_FORMAT_PRINTF(2, 3
void git_buf_clear(git_buf *buf);
void git_buf_consume(git_buf *buf, const char *end);
void git_buf_truncate(git_buf *buf, ssize_t len);
+void git_buf_rtruncate_at_char(git_buf *path, char separator);
int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...);
int git_buf_join(git_buf *buf, char separator, const char *str_a, const char *str_b);
diff --git a/src/fileops.c b/src/fileops.c
index cea954d..3241c68 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -79,6 +79,24 @@ git_off_t git_futils_filesize(git_file fd)
return sb.st_size;
}
+#define GIT_MODE_PERMS_MASK 0777
+#define GIT_CANONICAL_PERMS(MODE) (((MODE) & 0100) ? 0755 : 0644)
+#define GIT_MODE_TYPE(MODE) ((MODE) & ~GIT_MODE_PERMS_MASK)
+
+mode_t git_futils_canonical_mode(mode_t raw_mode)
+{
+ if (S_ISREG(raw_mode))
+ return S_IFREG | GIT_CANONICAL_PERMS(raw_mode);
+ else if (S_ISLNK(raw_mode))
+ return S_IFLNK;
+ else if (S_ISDIR(raw_mode))
+ return S_IFDIR;
+ else if (S_ISGITLINK(raw_mode))
+ return S_IFGITLINK;
+ else
+ return 0;
+}
+
int git_futils_readbuffer_updated(git_fbuffer *obj, const char *path, time_t *mtime, int *updated)
{
git_file fd;
diff --git a/src/fileops.h b/src/fileops.h
index c9ed05d..4c11402 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -85,13 +85,18 @@ extern int git_futils_mktmp(git_buf *path_out, const char *filename);
*/
extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode);
-
/**
* Get the filesize in bytes of a file
*/
extern git_off_t git_futils_filesize(git_file fd);
/**
+ * Convert a mode_t from the OS to a legal git mode_t value.
+ */
+extern mode_t git_futils_canonical_mode(mode_t raw_mode);
+
+
+/**
* Read-only map all or part of a file into memory.
* When possible this function should favor a virtual memory
* style mapping over some form of malloc()+read(), as the
diff --git a/src/ignore.c b/src/ignore.c
index 9690eba..30f86b8 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -69,38 +69,44 @@ static int load_ignore_file(
static int push_one_ignore(void *ref, git_buf *path)
{
git_ignores *ign = (git_ignores *)ref;
- return push_ignore(ign->repo, &ign->stack, path->ptr, GIT_IGNORE_FILE);
+ return push_ignore(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 error = GIT_SUCCESS;
- git_buf dir = GIT_BUF_INIT;
git_config *cfg;
const char *workdir = git_repository_workdir(repo);
assert(ignores);
+ ignores->repo = repo;
+ git_buf_init(&ignores->dir, 0);
+ ignores->ign_internal = NULL;
+ git_vector_init(&ignores->ign_path, 8, NULL);
+ git_vector_init(&ignores->ign_global, 2, NULL);
+
if ((error = git_attr_cache__init(repo)) < GIT_SUCCESS)
goto cleanup;
- if ((error = git_path_find_dir(&dir, path, workdir)) < GIT_SUCCESS)
+ if ((error = git_path_find_dir(&ignores->dir, path, workdir)) < GIT_SUCCESS)
goto cleanup;
- ignores->repo = repo;
- ignores->dir = NULL;
- git_vector_init(&ignores->stack, 2, NULL);
-
- /* insert internals */
- if ((error = push_ignore(repo, &ignores->stack, NULL, GIT_IGNORE_INTERNAL)) < GIT_SUCCESS)
+ /* set up internals */
+ error = git_attr_cache__lookup_or_create_file(
+ repo, GIT_IGNORE_INTERNAL, NULL, NULL, &ignores->ign_internal);
+ if (error < GIT_SUCCESS)
goto cleanup;
/* load .gitignore up the path */
- if ((error = git_path_walk_up(&dir, workdir, push_one_ignore, ignores)) < GIT_SUCCESS)
+ error = git_path_walk_up(&ignores->dir, workdir, push_one_ignore, ignores);
+ if (error < GIT_SUCCESS)
goto cleanup;
/* load .git/info/exclude */
- if ((error = push_ignore(repo, &ignores->stack, repo->path_repository, GIT_IGNORE_FILE_INREPO)) < GIT_SUCCESS)
+ error = push_ignore(repo, &ignores->ign_global,
+ repo->path_repository, GIT_IGNORE_FILE_INREPO);
+ if (error < GIT_SUCCESS)
goto cleanup;
/* load core.excludesfile */
@@ -108,7 +114,7 @@ int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *ig
const char *core_ignore;
error = git_config_get_string(cfg, GIT_IGNORE_CONFIG, &core_ignore);
if (error == GIT_SUCCESS && core_ignore != NULL)
- error = push_ignore(repo, &ignores->stack, NULL, core_ignore);
+ error = push_ignore(repo, &ignores->ign_global, NULL, core_ignore);
else {
error = GIT_SUCCESS;
git_clearerror(); /* don't care if attributesfile is not set */
@@ -117,46 +123,92 @@ int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *ig
}
cleanup:
- if (error < GIT_SUCCESS)
+ if (error < GIT_SUCCESS) {
+ git_ignore__free(ignores);
git__rethrow(error, "Could not get ignore files for '%s'", path);
- else
- ignores->dir = git_buf_detach(&dir);
+ }
- git_buf_free(&dir);
+ return error;
+}
+
+int git_ignore__push_dir(git_ignores *ign, const char *dir)
+{
+ int error = git_buf_joinpath(&ign->dir, ign->dir.ptr, dir);
+
+ if (error == GIT_SUCCESS)
+ error = push_ignore(
+ ign->repo, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE);
return error;
}
+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)
+ git_vector_pop(&ign->ign_path);
+ git_buf_rtruncate_at_char(&ign->dir, '/');
+ }
+ return GIT_SUCCESS;
+}
+
void git_ignore__free(git_ignores *ignores)
{
- git__free(ignores->dir);
- ignores->dir = NULL;
- git_vector_free(&ignores->stack);
+ /* don't need to free ignores->ign_internal since it is in cache */
+ git_vector_free(&ignores->ign_path);
+ git_vector_free(&ignores->ign_global);
+ git_buf_free(&ignores->dir);
+}
+
+static int ignore_lookup_in_rules(
+ git_vector *rules, git_attr_path *path, int *ignored)
+{
+ unsigned int j;
+ git_attr_fnmatch *match;
+
+ git_vector_rforeach(rules, j, match) {
+ if (git_attr_fnmatch__match(match, path) == GIT_SUCCESS) {
+ *ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0);
+ return GIT_SUCCESS;
+ }
+ }
+
+ return GIT_ENOTFOUND;
}
int git_ignore__lookup(git_ignores *ignores, const char *pathname, int *ignored)
{
int error;
- unsigned int i, j;
+ unsigned int i;
git_attr_file *file;
git_attr_path path;
- git_attr_fnmatch *match;
if ((error = git_attr_path__init(
&path, pathname, git_repository_workdir(ignores->repo))) < GIT_SUCCESS)
return git__rethrow(error, "Could not get attribute for '%s'", pathname);
- *ignored = 0;
+ /* first process builtins */
+ error = ignore_lookup_in_rules(
+ &ignores->ign_internal->rules, &path, ignored);
+ if (error == GIT_SUCCESS)
+ return error;
- git_vector_foreach(&ignores->stack, i, file) {
- git_vector_rforeach(&file->rules, j, match) {
- if (git_attr_fnmatch__match(match, &path) == GIT_SUCCESS) {
- *ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0);
- goto found;
- }
- }
+ /* next process files in the path */
+ git_vector_foreach(&ignores->ign_path, i, file) {
+ error = ignore_lookup_in_rules(&file->rules, &path, ignored);
+ if (error == GIT_SUCCESS)
+ return error;
}
-found:
- return error;
+ /* last process global ignores */
+ git_vector_foreach(&ignores->ign_global, i, file) {
+ error = ignore_lookup_in_rules(&file->rules, &path, ignored);
+ if (error == GIT_SUCCESS)
+ return error;
+ }
+
+ *ignored = 0;
+
+ return GIT_SUCCESS;
}
diff --git a/src/ignore.h b/src/ignore.h
index 386322f..49f72bf 100644
--- a/src/ignore.h
+++ b/src/ignore.h
@@ -10,14 +10,28 @@
#include "repository.h"
#include "vector.h"
+/* The git_ignores structure maintains three sets of ignores:
+ * - internal ignores
+ * - per directory ignores
+ * - global ignores (at lower priority than the others)
+ * As you traverse from one directory to another, you can push and pop
+ * directories onto git_ignores list efficiently.
+ */
typedef struct {
git_repository *repo;
- char *dir;
- git_vector stack;
+ git_buf dir;
+ git_attr_file *ign_internal;
+ git_vector ign_path;
+ git_vector ign_global;
} git_ignores;
-extern int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *stack);
-extern void git_ignore__free(git_ignores *stack);
-extern int git_ignore__lookup(git_ignores *stack, const char *path, int *ignored);
+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/iterator.c b/src/iterator.c
new file mode 100644
index 0000000..c2b88ab
--- /dev/null
+++ b/src/iterator.c
@@ -0,0 +1,492 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "iterator.h"
+#include "tree.h"
+#include "ignore.h"
+#include "buffer.h"
+
+typedef struct tree_iterator_frame tree_iterator_frame;
+struct tree_iterator_frame {
+ tree_iterator_frame *next;
+ git_tree *tree;
+ unsigned int index;
+};
+
+typedef struct {
+ git_iterator base;
+ git_repository *repo;
+ tree_iterator_frame *stack;
+ git_index_entry entry;
+ git_buf path;
+} tree_iterator;
+
+static const git_tree_entry *tree_iterator__tree_entry(tree_iterator *ti)
+{
+ return (ti->stack == NULL) ? NULL :
+ git_tree_entry_byindex(ti->stack->tree, ti->stack->index);
+}
+
+static int tree_iterator__current(
+ git_iterator *self, const git_index_entry **entry)
+{
+ int error;
+ tree_iterator *ti = (tree_iterator *)self;
+ const git_tree_entry *te = tree_iterator__tree_entry(ti);
+
+ *entry = NULL;
+
+ if (te == NULL)
+ return GIT_SUCCESS;
+
+ ti->entry.mode = te->attr;
+ git_oid_cpy(&ti->entry.oid, &te->oid);
+ error = git_buf_joinpath(&ti->path, ti->path.ptr, te->filename);
+ if (error < GIT_SUCCESS)
+ return error;
+ ti->entry.path = ti->path.ptr;
+
+ *entry = &ti->entry;
+
+ return GIT_SUCCESS;
+}
+
+static int tree_iterator__at_end(git_iterator *self)
+{
+ return (tree_iterator__tree_entry((tree_iterator *)self) == NULL);
+}
+
+static tree_iterator_frame *tree_iterator__alloc_frame(git_tree *tree)
+{
+ tree_iterator_frame *tf = git__calloc(1, sizeof(tree_iterator_frame));
+ tf->tree = tree;
+ return tf;
+}
+
+static int tree_iterator__expand_tree(tree_iterator *ti)
+{
+ int error;
+ git_tree *subtree;
+ const git_tree_entry *te = tree_iterator__tree_entry(ti);
+ tree_iterator_frame *tf;
+
+ while (te != NULL && entry_is_tree(te)) {
+ error = git_tree_lookup(&subtree, ti->repo, &te->oid);
+ if (error != GIT_SUCCESS)
+ return error;
+
+ if ((tf = tree_iterator__alloc_frame(subtree)) == NULL)
+ return GIT_ENOMEM;
+
+ tf->next = ti->stack;
+ ti->stack = tf;
+
+ error = git_buf_joinpath(&ti->path, ti->path.ptr, te->filename);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ te = tree_iterator__tree_entry(ti);
+ }
+
+ return GIT_SUCCESS;
+}
+
+static void tree_iterator__pop_frame(tree_iterator *ti)
+{
+ tree_iterator_frame *tf = ti->stack;
+ ti->stack = tf->next;
+ if (ti->stack != NULL) /* don't free the initial tree */
+ git_tree_free(tf->tree);
+ git__free(tf);
+}
+
+static int tree_iterator__advance(
+ git_iterator *self, const git_index_entry **entry)
+{
+ int error = GIT_SUCCESS;
+ tree_iterator *ti = (tree_iterator *)self;
+ const git_tree_entry *te;
+
+ if (entry != NULL)
+ *entry = NULL;
+
+ while (ti->stack != NULL) {
+ /* remove old entry filename */
+ git_buf_rtruncate_at_char(&ti->path, '/');
+
+ te = git_tree_entry_byindex(ti->stack->tree, ++ti->stack->index);
+ if (te != NULL)
+ break;
+
+ tree_iterator__pop_frame(ti);
+ git_buf_rtruncate_at_char(&ti->path, '/');
+ }
+
+ if (te && entry_is_tree(te))
+ error = tree_iterator__expand_tree(ti);
+
+ if (error == GIT_SUCCESS && entry != NULL)
+ error = tree_iterator__current(self, entry);
+
+ return error;
+}
+
+static void tree_iterator__free(git_iterator *self)
+{
+ tree_iterator *ti = (tree_iterator *)self;
+ while (ti->stack != NULL)
+ tree_iterator__pop_frame(ti);
+ git_buf_free(&ti->path);
+}
+
+int git_iterator_for_tree(
+ git_repository *repo, git_tree *tree, git_iterator **iter)
+{
+ int error;
+ tree_iterator *ti = git__calloc(1, sizeof(tree_iterator));
+ if (!ti)
+ return GIT_ENOMEM;
+
+ ti->base.type = GIT_ITERATOR_TREE;
+ ti->base.current = tree_iterator__current;
+ ti->base.at_end = tree_iterator__at_end;
+ ti->base.advance = tree_iterator__advance;
+ ti->base.free = tree_iterator__free;
+ ti->repo = repo;
+ ti->stack = tree_iterator__alloc_frame(tree);
+
+ if ((error = tree_iterator__expand_tree(ti)) < GIT_SUCCESS)
+ git_iterator_free((git_iterator *)ti);
+ else
+ *iter = (git_iterator *)ti;
+
+ return error;
+}
+
+
+typedef struct {
+ git_iterator base;
+ git_index *index;
+ unsigned int current;
+} index_iterator;
+
+static int index_iterator__current(
+ git_iterator *self, const git_index_entry **entry)
+{
+ index_iterator *ii = (index_iterator *)self;
+ *entry = git_index_get(ii->index, ii->current);
+ return GIT_SUCCESS;
+}
+
+static int index_iterator__at_end(git_iterator *self)
+{
+ index_iterator *ii = (index_iterator *)self;
+ return (ii->current >= git_index_entrycount(ii->index));
+}
+
+static int index_iterator__advance(
+ git_iterator *self, const git_index_entry **entry)
+{
+ index_iterator *ii = (index_iterator *)self;
+ if (ii->current < git_index_entrycount(ii->index))
+ ii->current++;
+ if (entry)
+ *entry = git_index_get(ii->index, ii->current);
+ return GIT_SUCCESS;
+}
+
+static void index_iterator__free(git_iterator *self)
+{
+ index_iterator *ii = (index_iterator *)self;
+ git_index_free(ii->index);
+ ii->index = NULL;
+}
+
+int git_iterator_for_index(git_repository *repo, git_iterator **iter)
+{
+ int error;
+ index_iterator *ii = git__calloc(1, sizeof(index_iterator));
+ if (!ii)
+ return GIT_ENOMEM;
+
+ ii->base.type = GIT_ITERATOR_INDEX;
+ ii->base.current = index_iterator__current;
+ ii->base.at_end = index_iterator__at_end;
+ ii->base.advance = index_iterator__advance;
+ ii->base.free = index_iterator__free;
+ ii->current = 0;
+
+ if ((error = git_repository_index(&ii->index, repo)) < GIT_SUCCESS)
+ git__free(ii);
+ else
+ *iter = (git_iterator *)ii;
+ return error;
+}
+
+
+typedef struct workdir_iterator_frame workdir_iterator_frame;
+struct workdir_iterator_frame {
+ workdir_iterator_frame *next;
+ git_vector entries;
+ unsigned int index;
+};
+
+typedef struct {
+ git_iterator base;
+ git_repository *repo;
+ size_t root_len;
+ workdir_iterator_frame *stack;
+ git_ignores ignores;
+ git_index_entry entry;
+ git_buf path;
+ int is_ignored;
+} workdir_iterator;
+
+static workdir_iterator_frame *workdir_iterator__alloc_frame(void)
+{
+ workdir_iterator_frame *wf = git__calloc(1, sizeof(workdir_iterator_frame));
+ if (wf == NULL)
+ return wf;
+ if (git_vector_init(&wf->entries, 0, git__strcmp_cb) != GIT_SUCCESS) {
+ git__free(wf);
+ return NULL;
+ }
+ return wf;
+}
+
+static void workdir_iterator__free_frame(workdir_iterator_frame *wf)
+{
+ unsigned int i;
+ char *path;
+
+ git_vector_foreach(&wf->entries, i, path)
+ git__free(path);
+ git_vector_free(&wf->entries);
+ git__free(wf);
+}
+
+static int workdir_iterator__update_entry(workdir_iterator *wi);
+
+static int workdir_iterator__expand_dir(workdir_iterator *wi)
+{
+ int error;
+ workdir_iterator_frame *wf = workdir_iterator__alloc_frame();
+ if (wf == NULL)
+ return GIT_ENOMEM;
+
+ /* allocate dir entries with extra byte (the "1" param) so we
+ * can suffix directory names with a "/".
+ */
+ error = git_path_dirload(wi->path.ptr, wi->root_len, 1, &wf->entries);
+ if (error < GIT_SUCCESS || wf->entries.length == 0) {
+ workdir_iterator__free_frame(wf);
+ return GIT_ENOTFOUND;
+ }
+
+ git_vector_sort(&wf->entries);
+ wf->next = wi->stack;
+ wi->stack = wf;
+
+ /* only push new ignores if this is not top level directory */
+ if (wi->stack->next != NULL) {
+ int slash_pos = git_buf_rfind_next(&wi->path, '/');
+ (void)git_ignore__push_dir(&wi->ignores, &wi->path.ptr[slash_pos + 1]);
+ }
+
+ return workdir_iterator__update_entry(wi);
+}
+
+static int workdir_iterator__current(
+ git_iterator *self, const git_index_entry **entry)
+{
+ workdir_iterator *wi = (workdir_iterator *)self;
+ *entry = (wi->entry.path == NULL) ? NULL : &wi->entry;
+ return GIT_SUCCESS;
+}
+
+static int workdir_iterator__at_end(git_iterator *self)
+{
+ return (((workdir_iterator *)self)->entry.path == NULL);
+}
+
+static int workdir_iterator__advance(
+ git_iterator *self, const git_index_entry **entry)
+{
+ int error;
+ workdir_iterator *wi = (workdir_iterator *)self;
+ workdir_iterator_frame *wf;
+ const char *next;
+
+ if (entry != NULL)
+ *entry = NULL;
+
+ if (wi->entry.path == NULL)
+ return GIT_SUCCESS;
+
+ while ((wf = wi->stack) != NULL) {
+ next = git_vector_get(&wf->entries, ++wf->index);
+ if (next != NULL) {
+ if (strcmp(next, DOT_GIT) == 0)
+ continue;
+ /* else found a good entry */
+ break;
+ }
+
+ /* pop workdir directory stack */
+ wi->stack = wf->next;
+ workdir_iterator__free_frame(wf);
+ git_ignore__pop_dir(&wi->ignores);
+
+ if (wi->stack == NULL) {
+ memset(&wi->entry, 0, sizeof(wi->entry));
+ return GIT_SUCCESS;
+ }
+ }
+
+ error = workdir_iterator__update_entry(wi);
+
+ if (error == GIT_SUCCESS && entry != NULL)
+ error = workdir_iterator__current(self, entry);
+
+ return error;
+}
+
+static void workdir_iterator__free(git_iterator *self)
+{
+ workdir_iterator *wi = (workdir_iterator *)self;
+
+ while (wi->stack != NULL) {
+ workdir_iterator_frame *wf = wi->stack;
+ wi->stack = wf->next;
+ workdir_iterator__free_frame(wf);
+ }
+
+ git_ignore__free(&wi->ignores);
+ git_buf_free(&wi->path);
+}
+
+static int workdir_iterator__update_entry(workdir_iterator *wi)
+{
+ int error;
+ struct stat st;
+ char *relpath = git_vector_get(&wi->stack->entries, wi->stack->index);
+
+ error = git_buf_joinpath(
+ &wi->path, git_repository_workdir(wi->repo), relpath);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ memset(&wi->entry, 0, sizeof(wi->entry));
+ wi->entry.path = relpath;
+
+ /* skip over .git directory */
+ if (strcmp(relpath, DOT_GIT) == 0)
+ return workdir_iterator__advance((git_iterator *)wi, NULL);
+
+ /* if there is an error processing the entry, treat as ignored */
+ wi->is_ignored = 1;
+
+ if (p_lstat(wi->path.ptr, &st) < 0)
+ return GIT_SUCCESS;
+
+ /* TODO: remove shared code for struct stat conversion with index.c */
+ wi->entry.ctime.seconds = (git_time_t)st.st_ctime;
+ wi->entry.mtime.seconds = (git_time_t)st.st_mtime;
+ wi->entry.dev = st.st_rdev;
+ wi->entry.ino = st.st_ino;
+ wi->entry.mode = git_futils_canonical_mode(st.st_mode);
+ wi->entry.uid = st.st_uid;
+ wi->entry.gid = st.st_gid;
+ wi->entry.file_size = st.st_size;
+
+ /* if this is a file type we don't handle, treat as ignored */
+ if (st.st_mode == 0)
+ return GIT_SUCCESS;
+
+ /* okay, we are far enough along to look up real ignore rule */
+ error = git_ignore__lookup(&wi->ignores, wi->entry.path, &wi->is_ignored);
+ if (error != GIT_SUCCESS)
+ return GIT_SUCCESS;
+
+ if (S_ISDIR(st.st_mode)) {
+ if (git_path_contains(&wi->path, DOT_GIT) == GIT_SUCCESS) {
+ /* create submodule entry */
+ wi->entry.mode = S_IFGITLINK;
+ } else {
+ /* create directory entry that can be advanced into as needed */
+ size_t pathlen = strlen(wi->entry.path);
+ wi->entry.path[pathlen] = '/';
+ wi->entry.path[pathlen + 1] = '\0';
+ wi->entry.mode = S_IFDIR;
+ }
+ }
+
+ return GIT_SUCCESS;
+}
+
+int git_iterator_for_workdir(git_repository *repo, git_iterator **iter)
+{
+ int error;
+ workdir_iterator *wi = git__calloc(1, sizeof(workdir_iterator));
+ if (!wi)
+ return GIT_ENOMEM;
+
+ wi->base.type = GIT_ITERATOR_WORKDIR;
+ wi->base.current = workdir_iterator__current;
+ wi->base.at_end = workdir_iterator__at_end;
+ wi->base.advance = workdir_iterator__advance;
+ wi->base.free = workdir_iterator__free;
+ wi->repo = repo;
+
+ error = git_buf_sets(&wi->path, git_repository_workdir(repo));
+ if (error == GIT_SUCCESS)
+ error = git_ignore__for_path(repo, "", &wi->ignores);
+ if (error != GIT_SUCCESS) {
+ git__free(wi);
+ return error;
+ }
+
+ wi->root_len = wi->path.size;
+
+ if ((error = workdir_iterator__expand_dir(wi)) < GIT_SUCCESS)
+ git_iterator_free((git_iterator *)wi);
+ else
+ *iter = (git_iterator *)wi;
+
+ return error;
+}
+
+
+int git_iterator_current_tree_entry(
+ git_iterator *iter, const git_tree_entry **tree_entry)
+{
+ *tree_entry = (iter->type != GIT_ITERATOR_TREE) ? NULL :
+ tree_iterator__tree_entry((tree_iterator *)iter);
+ return GIT_SUCCESS;
+}
+
+int git_iterator_current_is_ignored(git_iterator *iter)
+{
+ return (iter->type != GIT_ITERATOR_WORKDIR) ? 0 :
+ ((workdir_iterator *)iter)->is_ignored;
+}
+
+int git_iterator_advance_into_directory(
+ git_iterator *iter, const git_index_entry **entry)
+{
+ workdir_iterator *wi = (workdir_iterator *)iter;
+
+ if (iter->type == GIT_ITERATOR_WORKDIR &&
+ wi->entry.path && S_ISDIR(wi->entry.mode))
+ {
+ if (workdir_iterator__expand_dir(wi) < GIT_SUCCESS)
+ /* if error loading or if empty, skip the directory. */
+ return workdir_iterator__advance(iter, entry);
+ }
+
+ return entry ? git_iterator_current(iter, entry) : GIT_SUCCESS;
+}
diff --git a/src/iterator.h b/src/iterator.h
new file mode 100644
index 0000000..ac30b4d
--- /dev/null
+++ b/src/iterator.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2009-2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_iterator_h__
+#define INCLUDE_iterator_h__
+
+#include "common.h"
+#include "git2/index.h"
+
+typedef struct git_iterator git_iterator;
+
+typedef enum {
+ GIT_ITERATOR_TREE = 1,
+ GIT_ITERATOR_INDEX = 2,
+ GIT_ITERATOR_WORKDIR = 3
+} git_iterator_type_t;
+
+struct git_iterator {
+ git_iterator_type_t type;
+ int (*current)(git_iterator *, const git_index_entry **);
+ int (*at_end)(git_iterator *);
+ int (*advance)(git_iterator *, const git_index_entry **);
+ void (*free)(git_iterator *);
+};
+
+int git_iterator_for_tree(
+ git_repository *repo, git_tree *tree, git_iterator **iter);
+
+int git_iterator_for_index(
+ git_repository *repo, git_iterator **iter);
+
+int git_iterator_for_workdir(
+ git_repository *repo, git_iterator **iter);
+
+/* Entry is not guaranteed to be fully populated. For a tree iterator,
+ * we will only populate the mode, oid and path, for example. For a workdir
+ * iterator, we will not populate the oid.
+ *
+ * You do not need to free the entry. It is still "owned" by the iterator.
+ * Once you call `git_iterator_advance`, then content of the old entry is
+ * no longer guaranteed to be valid.
+ */
+GIT_INLINE(int) git_iterator_current(
+ git_iterator *iter, const git_index_entry **entry)
+{
+ return iter->current(iter, entry);
+}
+
+GIT_INLINE(int) git_iterator_at_end(git_iterator *iter)
+{
+ return iter->at_end(iter);
+}
+
+GIT_INLINE(int) git_iterator_advance(
+ git_iterator *iter, const git_index_entry **entry)
+{
+ return iter->advance(iter, entry);
+}
+
+GIT_INLINE(void) git_iterator_free(git_iterator *iter)
+{
+ iter->free(iter);
+ git__free(iter);
+}
+
+GIT_INLINE(git_iterator_type_t) git_iterator_type(git_iterator *iter)
+{
+ return iter->type;
+}
+
+extern int git_iterator_current_tree_entry(
+ git_iterator *iter, const git_tree_entry **tree_entry);
+
+extern int git_iterator_current_is_ignored(git_iterator *iter);
+
+/**
+ * Iterate into a workdir directory.
+ *
+ * Workdir iterators do not automatically descend into directories (so that
+ * when comparing two iterator entries you can detect a newly created
+ * directory in the workdir). As a result, you may get S_ISDIR items from
+ * a workdir iterator. If you wish to iterate over the contents of the
+ * directories you encounter, then call this function when you encounter
+ * a directory.
+ *
+ * If there are no files in the directory, this will end up acting like a
+ * regular advance and will skip past the directory, so you should be
+ * prepared for that case.
+ *
+ * On non-workdir iterators or if not pointing at a directory, this is a
+ * no-op and will not advance the iterator.
+ */
+extern int git_iterator_advance_into_directory(
+ git_iterator *iter, const git_index_entry **entry);
+
+#endif
diff --git a/src/path.c b/src/path.c
index 042332c..88ea95a 100644
--- a/src/path.c
+++ b/src/path.c
@@ -398,37 +398,38 @@ int git_path_isfile(const char *path)
static int _check_dir_contents(
git_buf *dir,
const char *sub,
- int append_on_success,
int (*predicate)(const char *))
{
int error = GIT_SUCCESS;
size_t dir_size = dir->size;
size_t sub_size = strlen(sub);
- /* leave base valid even if we could not make space for subdir */
+ /* separate allocation and join, so we can always leave git_buf valid */
if ((error = git_buf_try_grow(dir, dir_size + sub_size + 2)) < GIT_SUCCESS)
return error;
-
- /* save excursion */
git_buf_joinpath(dir, dir->ptr, sub);
error = (*predicate)(dir->ptr);
- /* restore excursion */
- if (!append_on_success || error != GIT_SUCCESS)
- git_buf_truncate(dir, dir_size);
+ /* restore path */
+ git_buf_truncate(dir, dir_size);
return error;
}
-int git_path_contains_dir(git_buf *base, const char *subdir, int append_if_exists)
+int git_path_contains(git_buf *dir, const char *item)
+{
+ return _check_dir_contents(dir, item, &git_path_exists);
+}
+
+int git_path_contains_dir(git_buf *base, const char *subdir)
{
- return _check_dir_contents(base, subdir, append_if_exists, &git_path_isdir);
+ return _check_dir_contents(base, subdir, &git_path_isdir);
}
-int git_path_contains_file(git_buf *base, const char *file, int append_if_exists)
+int git_path_contains_file(git_buf *base, const char *file)
{
- return _check_dir_contents(base, file, append_if_exists, &git_path_isfile);
+ return _check_dir_contents(base, file, &git_path_isfile);
}
int git_path_find_dir(git_buf *dir, const char *path, const char *base)
@@ -522,3 +523,63 @@ int git_path_direach(
closedir(dir);
return GIT_SUCCESS;
}
+
+int git_path_dirload(
+ const char *path,
+ size_t prefix_len,
+ size_t alloc_extra,
+ git_vector *contents)
+{
+ int error, need_slash;
+ DIR *dir;
+ struct dirent de_buf, *de;
+ size_t path_len;
+
+ assert(path != NULL && contents != NULL);
+ path_len = strlen(path);
+ assert(path_len > 0 && path_len >= prefix_len);
+
+ if ((dir = opendir(path)) == NULL)
+ return git__throw(GIT_EOSERR, "Failed to process `%s` tree structure."
+ " An error occured while opening the directory", path);
+
+ path += prefix_len;
+ path_len -= prefix_len;
+ need_slash = (path_len > 0 && path[path_len-1] != '/') ? 1 : 0;
+
+ while ((error = readdir_r(dir, &de_buf, &de)) == 0 && de != NULL) {
+ char *entry_path;
+ size_t entry_len;
+
+ if (is_dot_or_dotdot(de->d_name))
+ continue;
+
+ entry_len = strlen(de->d_name);
+
+ entry_path = git__malloc(
+ path_len + need_slash + entry_len + 1 + alloc_extra);
+ if (entry_path == NULL)
+ return GIT_ENOMEM;
+
+ if (path_len)
+ memcpy(entry_path, path, path_len);
+ if (need_slash)
+ entry_path[path_len] = '/';
+ memcpy(&entry_path[path_len + need_slash], de->d_name, entry_len);
+ entry_path[path_len + need_slash + entry_len] = '\0';
+
+ if ((error = git_vector_insert(contents, entry_path)) < GIT_SUCCESS) {
+ git__free(entry_path);
+ return error;
+ }
+ }
+
+ closedir(dir);
+
+ if (error != GIT_SUCCESS)
+ return git__throw(
+ GIT_EOSERR, "Failed to process directory entry in `%s`", path);
+
+ return GIT_SUCCESS;
+}
+
diff --git a/src/path.h b/src/path.h
index 0f7ebb7..abe6c22 100644
--- a/src/path.h
+++ b/src/path.h
@@ -9,6 +9,7 @@
#include "common.h"
#include "buffer.h"
+#include "vector.h"
/**
* Path manipulation utils
@@ -129,24 +130,31 @@ extern int git_path_isdir(const char *path);
extern int git_path_isfile(const char *path);
/**
+ * Check if the parent directory contains the item.
+ *
+ * @param dir Directory to check.
+ * @param item Item that might be in the directory.
+ * @return GIT_SUCCESS if item exists in directory, <0 otherwise.
+ */
+extern int git_path_contains(git_buf *dir, const char *item);
+
+/**
* Check if the given path contains the given subdirectory.
*
* @param parent Directory path that might contain subdir
* @param subdir Subdirectory name to look for in parent
- * @param append_if_exists If true, then subdir will be appended to the parent path if it does exist
* @return GIT_SUCCESS if subdirectory exists, < 0 otherwise.
*/
-extern int git_path_contains_dir(git_buf *parent, const char *subdir, int append_if_exists);
+extern int git_path_contains_dir(git_buf *parent, const char *subdir);
/**
* Check if the given path contains the given file.
*
* @param dir Directory path that might contain file
* @param file File name to look for in parent
- * @param append_if_exists If true, then file will be appended to the path if it does exist
* @return GIT_SUCCESS if file exists, < 0 otherwise.
*/
-extern int git_path_contains_file(git_buf *dir, const char *file, int append_if_exists);
+extern int git_path_contains_file(git_buf *dir, const char *file);
/**
* Clean up path, prepending base if it is not already rooted.
@@ -216,4 +224,26 @@ extern int git_path_walk_up(
int (*fn)(void *state, git_buf *),
void *state);
+/**
+ * Load all directory entries (except '.' and '..') into a vector.
+ *
+ * For cases where `git_path_direach()` is not appropriate, this
+ * allows you to load the filenames in a directory into a vector
+ * of strings. That vector can then be sorted, iterated, or whatever.
+ * Remember to free alloc of the allocated strings when you are done.
+ *
+ * @param path The directory to read from.
+ * @param prefix_len When inserting entries, the trailing part of path
+ * will be prefixed after this length. I.e. given path "/a/b" and
+ * prefix_len 3, the entries will look like "b/e1", "b/e2", etc.
+ * @param alloc_extra Extra bytes to add to each string allocation in
+ * case you want to append anything funny.
+ * @param contents Vector to fill with directory entry names.
+ */
+extern int git_path_dirload(
+ const char *path,
+ size_t prefix_len,
+ size_t alloc_extra,
+ git_vector *contents);
+
#endif
diff --git a/src/repository.c b/src/repository.c
index 13ad7eb..f394d06 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -81,14 +81,14 @@ void git_repository_free(git_repository *repo)
static int quickcheck_repository_dir(git_buf *repository_path)
{
/* Check OBJECTS_DIR first, since it will generate the longest path name */
- if (git_path_contains_dir(repository_path, GIT_OBJECTS_DIR, 0) < 0)
+ if (git_path_contains_dir(repository_path, GIT_OBJECTS_DIR) < 0)
return GIT_ERROR;
/* Ensure HEAD file exists */
- if (git_path_contains_file(repository_path, GIT_HEAD_FILE, 0) < 0)
+ if (git_path_contains_file(repository_path, GIT_HEAD_FILE) < 0)
return GIT_ERROR;
- if (git_path_contains_dir(repository_path, GIT_REFS_DIR, 0) < 0)
+ if (git_path_contains_dir(repository_path, GIT_REFS_DIR) < 0)
return GIT_ERROR;
return GIT_SUCCESS;
@@ -166,8 +166,8 @@ int git_repository_open(git_repository **repo_out, const char *path)
* of the working dir, by testing if it contains a `.git`
* folder inside of it.
*/
- git_path_contains_dir(&path_buf, GIT_DIR, 1); /* append on success */
- /* ignore error, since it just means `path/.git` doesn't exist */
+ if (git_path_contains_dir(&path_buf, GIT_DIR) == GIT_SUCCESS)
+ git_buf_joinpath(&path_buf, path_buf.ptr, GIT_DIR);
if (quickcheck_repository_dir(&path_buf) < GIT_SUCCESS) {
error = git__throw(GIT_ENOTAREPO,
diff --git a/src/vector.c b/src/vector.c
index ba8499d..e109704 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -25,7 +25,6 @@ static int resize_vector(git_vector *v)
return GIT_SUCCESS;
}
-
void git_vector_free(git_vector *v)
{
assert(v);
@@ -188,6 +187,12 @@ int git_vector_remove(git_vector *v, unsigned int idx)
return GIT_SUCCESS;
}
+void git_vector_pop(git_vector *v)
+{
+ if (v->length > 0)
+ v->length--;
+}
+
void git_vector_uniq(git_vector *v)
{
git_vector_cmp cmp;
diff --git a/src/vector.h b/src/vector.h
index ae38825..44635ae 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -38,6 +38,11 @@ GIT_INLINE(void *) git_vector_get(git_vector *v, unsigned int position)
return (position < v->length) ? v->contents[position] : NULL;
}
+GIT_INLINE(void *) git_vector_last(git_vector *v)
+{
+ return (v->length > 0) ? git_vector_get(v, v->length - 1) : NULL;
+}
+
#define git_vector_foreach(v, iter, elem) \
for ((iter) = 0; (iter) < (v)->length && ((elem) = (v)->contents[(iter)], 1); (iter)++ )
@@ -48,6 +53,7 @@ int git_vector_insert(git_vector *v, void *element);
int git_vector_insert_sorted(git_vector *v, void *element,
int (*on_dup)(void **old, void *new));
int git_vector_remove(git_vector *v, unsigned int idx);
+void git_vector_pop(git_vector *v);
void git_vector_uniq(git_vector *v);
#endif
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c
new file mode 100644
index 0000000..b2dbe9e
--- /dev/null
+++ b/tests-clar/diff/diff_helpers.c
@@ -0,0 +1,22 @@
+#include "clar_libgit2.h"
+#include "diff_helpers.h"
+
+git_tree *resolve_commit_oid_to_tree(
+ git_repository *repo,
+ const char *partial_oid)
+{
+ size_t len = strlen(partial_oid);
+ git_oid oid;
+ git_object *obj;
+ git_tree *tree;
+
+ if (git_oid_fromstrn(&oid, partial_oid, len) == 0)
+ git_object_lookup_prefix(&obj, repo, &oid, len, GIT_OBJ_ANY);
+ cl_assert(obj);
+ if (git_object_type(obj) == GIT_OBJ_TREE)
+ return (git_tree *)obj;
+ cl_assert(git_object_type(obj) == GIT_OBJ_COMMIT);
+ cl_git_pass(git_commit_tree(&tree, (git_commit *)obj));
+ git_object_free(obj);
+ return tree;
+}
diff --git a/tests-clar/diff/diff_helpers.h b/tests-clar/diff/diff_helpers.h
new file mode 100644
index 0000000..a75dd91
--- /dev/null
+++ b/tests-clar/diff/diff_helpers.h
@@ -0,0 +1,4 @@
+#include "fileops.h"
+
+extern git_tree *resolve_commit_oid_to_tree(
+ git_repository *repo, const char *partial_oid);
diff --git a/tests-clar/diff/iterator.c b/tests-clar/diff/iterator.c
new file mode 100644
index 0000000..46f8f59
--- /dev/null
+++ b/tests-clar/diff/iterator.c
@@ -0,0 +1,364 @@
+#include "clar_libgit2.h"
+#include "diff_helpers.h"
+#include "iterator.h"
+
+static git_repository *g_repo = NULL;
+static const char *g_sandbox = NULL;
+
+static void setup_sandbox(const char *sandbox)
+{
+ cl_fixture_sandbox(sandbox);
+ g_sandbox = sandbox;
+
+ p_chdir(sandbox);
+ cl_git_pass(p_rename(".gitted", ".git"));
+ if (p_access("gitattributes", F_OK) == 0)
+ cl_git_pass(p_rename("gitattributes", ".gitattributes"));
+ if (p_access("gitignore", F_OK) == 0)
+ cl_git_pass(p_rename("gitignore", ".gitignore"));
+ p_chdir("..");
+
+ cl_git_pass(git_repository_open(&g_repo, sandbox));
+}
+
+static void cleanup_sandbox(void)
+{
+ if (g_repo) {
+ git_repository_free(g_repo);
+ g_repo = NULL;
+ }
+ if (g_sandbox) {
+ cl_fixture_cleanup(g_sandbox);
+ g_sandbox = NULL;
+ }
+}
+
+void test_diff_iterator__initialize(void)
+{
+ /* since we are doing tests with different sandboxes, defer setup
+ * to the actual tests. cleanup will still be done in the global
+ * cleanup function so that assertion failures don't result in a
+ * missed cleanup.
+ */
+}
+
+void test_diff_iterator__cleanup(void)
+{
+ cleanup_sandbox();
+}
+
+
+/* -- TREE ITERATOR TESTS -- */
+
+static void tree_iterator_test(
+ const char *sandbox,
+ const char *treeish,
+ int expected_count,
+ const char **expected_values)
+{
+ git_tree *t;
+ git_iterator *i;
+ const git_index_entry *entry;
+ int count = 0;
+
+ setup_sandbox(sandbox);
+
+ cl_assert(t = resolve_commit_oid_to_tree(g_repo, treeish));
+ cl_git_pass(git_iterator_for_tree(g_repo, t, &i));
+ cl_git_pass(git_iterator_current(i, &entry));
+
+ while (entry != NULL) {
+ if (expected_values != NULL)
+ cl_assert_strequal(expected_values[count], entry->path);
+
+ count++;
+
+ cl_git_pass(git_iterator_advance(i, &entry));
+ }
+
+ git_iterator_free(i);
+
+ cl_assert(expected_count == count);
+
+ git_tree_free(t);
+}
+
+/* results of: git ls-tree -r --name-only 605812a */
+const char *expected_tree_0[] = {
+ ".gitattributes",
+ "attr0",
+ "attr1",
+ "attr2",
+ "attr3",
+ "binfile",
+ "macro_test",
+ "root_test1",
+ "root_test2",
+ "root_test3",
+ "root_test4.txt",
+ "subdir/.gitattributes",
+ "subdir/abc",
+ "subdir/subdir_test1",
+ "subdir/subdir_test2.txt",
+ "subdir2/subdir2_test1",
+ NULL
+};
+
+void test_diff_iterator__tree_0(void)
+{
+ tree_iterator_test("attr", "605812a", 16, expected_tree_0);
+}
+
+/* results of: git ls-tree -r --name-only 6bab5c79 */
+const char *expected_tree_1[] = {
+ ".gitattributes",
+ "attr0",
+ "attr1",
+ "attr2",
+ "attr3",
+ "root_test1",
+ "root_test2",
+ "root_test3",
+ "root_test4.txt",
+ "subdir/.gitattributes",
+ "subdir/subdir_test1",
+ "subdir/subdir_test2.txt",
+ "subdir2/subdir2_test1",
+ NULL
+};
+
+void test_diff_iterator__tree_1(void)
+{
+ tree_iterator_test("attr", "6bab5c79cd5", 13, expected_tree_1);
+}
+
+/* results of: git ls-tree -r --name-only 26a125ee1 */
+const char *expected_tree_2[] = {
+ "current_file",
+ "file_deleted",
+ "modified_file",
+ "staged_changes",
+ "staged_changes_file_deleted",
+ "staged_changes_modified_file",
+ "staged_delete_file_deleted",
+ "staged_delete_modified_file",
+ "subdir.txt",
+ "subdir/current_file",
+ "subdir/deleted_file",
+ "subdir/modified_file",
+ NULL
+};
+
+void test_diff_iterator__tree_2(void)
+{
+ tree_iterator_test("status", "26a125ee1", 12, expected_tree_2);
+}
+
+/* $ git ls-tree -r --name-only 0017bd4ab1e */
+const char *expected_tree_3[] = {
+ "current_file",
+ "file_deleted",
+ "modified_file",
+ "staged_changes",
+ "staged_changes_file_deleted",
+ "staged_changes_modified_file",
+ "staged_delete_file_deleted",
+ "staged_delete_modified_file"
+};
+
+void test_diff_iterator__tree_3(void)
+{
+ tree_iterator_test("status", "0017bd4ab1e", 8, expected_tree_3);
+}
+
+
+/* -- INDEX ITERATOR TESTS -- */
+
+static void index_iterator_test(
+ const char *sandbox,
+ int expected_count,
+ const char **expected_names,
+ const char **expected_oids)
+{
+ git_iterator *i;
+ const git_index_entry *entry;
+ int count = 0;
+
+ setup_sandbox(sandbox);
+
+ cl_git_pass(git_iterator_for_index(g_repo, &i));
+ cl_git_pass(git_iterator_current(i, &entry));
+
+ while (entry != NULL) {
+ if (expected_names != NULL)
+ cl_assert_strequal(expected_names[count], entry->path);
+
+ if (expected_oids != NULL) {
+ git_oid oid;
+ cl_git_pass(git_oid_fromstr(&oid, expected_oids[count]));
+ cl_assert(git_oid_cmp(&oid, &entry->oid) == 0);
+ }
+
+ count++;
+ cl_git_pass(git_iterator_advance(i, &entry));
+ }
+
+ git_iterator_free(i);
+
+ cl_assert(count == expected_count);
+}
+
+static const char *expected_index_0[] = {
+ "attr0",
+ "attr1",
+ "attr2",
+ "attr3",
+ "binfile",
+ "gitattributes",
+ "macro_bad",
+ "macro_test",
+ "root_test1",
+ "root_test2",
+ "root_test3",
+ "root_test4.txt",
+ "subdir/.gitattributes",
+ "subdir/abc",
+ "subdir/subdir_test1",
+ "subdir/subdir_test2.txt",
+ "subdir2/subdir2_test1",
+};
+
+static const char *expected_index_oids_0[] = {
+ "556f8c827b8e4a02ad5cab77dca2bcb3e226b0b3",
+ "3b74db7ab381105dc0d28f8295a77f6a82989292",
+ "2c66e14f77196ea763fb1e41612c1aa2bc2d8ed2",
+ "c485abe35abd4aa6fd83b076a78bbea9e2e7e06c",
+ "d800886d9c86731ae5c4a62b0b77c437015e00d2",
+ "2b40c5aca159b04ea8d20ffe36cdf8b09369b14a",
+ "5819a185d77b03325aaf87cafc771db36f6ddca7",
+ "ff69f8639ce2e6010b3f33a74160aad98b48da2b",
+ "45141a79a77842c59a63229403220a4e4be74e3d",
+ "45141a79a77842c59a63229403220a4e4be74e3d",
+ "45141a79a77842c59a63229403220a4e4be74e3d",
+ "fb5067b1aef3ac1ada4b379dbcb7d17255df7d78",
+ "99eae476896f4907224978b88e5ecaa6c5bb67a9",
+ "3e42ffc54a663f9401cc25843d6c0e71a33e4249",
+ "e563cf4758f0d646f1b14b76016aa17fa9e549a4",
+ "fb5067b1aef3ac1ada4b379dbcb7d17255df7d78",
+ "dccada462d3df8ac6de596fb8c896aba9344f941"
+};
+
+void test_diff_iterator__index_0(void)
+{
+ index_iterator_test("attr", 17, expected_index_0, expected_index_oids_0);
+}
+
+static const char *expected_index_1[] = {
+ "current_file",
+ "file_deleted",
+ "modified_file",
+ "staged_changes",
+ "staged_changes_file_deleted",
+ "staged_changes_modified_file",
+ "staged_new_file",
+ "staged_new_file_deleted_file",
+ "staged_new_file_modified_file",
+ "subdir.txt",
+ "subdir/current_file",
+ "subdir/deleted_file",
+ "subdir/modified_file",
+};
+
+static const char* expected_index_oids_1[] = {
+ "a0de7e0ac200c489c41c59dfa910154a70264e6e",
+ "5452d32f1dd538eb0405e8a83cc185f79e25e80f",
+ "452e4244b5d083ddf0460acf1ecc74db9dcfa11a",
+ "55d316c9ba708999f1918e9677d01dfcae69c6b9",
+ "a6be623522ce87a1d862128ac42672604f7b468b",
+ "906ee7711f4f4928ddcb2a5f8fbc500deba0d2a8",
+ "529a16e8e762d4acb7b9636ff540a00831f9155a",
+ "90b8c29d8ba39434d1c63e1b093daaa26e5bd972",
+ "ed062903b8f6f3dccb2fa81117ba6590944ef9bd",
+ "e8ee89e15bbe9b20137715232387b3de5b28972e",
+ "53ace0d1cc1145a5f4fe4f78a186a60263190733",
+ "1888c805345ba265b0ee9449b8877b6064592058",
+ "a6191982709b746d5650e93c2acf34ef74e11504"
+};
+
+void test_diff_iterator__index_1(void)
+{
+ index_iterator_test("status", 13, expected_index_1, expected_index_oids_1);
+}
+
+
+/* -- WORKDIR ITERATOR TESTS -- */
+
+static void workdir_iterator_test(
+ const char *sandbox,
+ int expected_count,
+ int expected_ignores,
+ const char **expected_names,
+ const char *an_ignored_name)
+{
+ git_iterator *i;
+ const git_index_entry *entry;
+ int count = 0, count_all = 0;
+
+ setup_sandbox(sandbox);
+
+ cl_git_pass(git_iterator_for_workdir(g_repo, &i));
+ cl_git_pass(git_iterator_current(i, &entry));
+
+ while (entry != NULL) {
+ int ignored = git_iterator_current_is_ignored(i);
+
+ if (!ignored && S_ISDIR(entry->mode)) {
+ cl_git_pass(git_iterator_advance_into_directory(i, &entry));
+ continue;
+ }
+
+ if (expected_names != NULL)
+ cl_assert_strequal(expected_names[count_all], entry->path);
+
+ if (an_ignored_name && strcmp(an_ignored_name,entry->path)==0)
+ cl_assert(ignored);
+
+ if (!ignored)
+ count++;
+ count_all++;
+
+ cl_git_pass(git_iterator_advance(i, &entry));
+ }
+
+ git_iterator_free(i);
+
+ cl_assert(count == expected_count);
+ cl_assert(count_all == expected_count + expected_ignores);
+}
+
+void test_diff_iterator__workdir_0(void)
+{
+ workdir_iterator_test("attr", 15, 4, NULL, "ign");
+}
+
+static const char *status_paths[] = {
+ "current_file",
+ "ignored_file",
+ "modified_file",
+ "new_file",
+ "staged_changes",
+ "staged_changes_modified_file",
+ "staged_delete_modified_file",
+ "staged_new_file",
+ "staged_new_file_modified_file",
+ "subdir/current_file",
+ "subdir/modified_file",
+ "subdir/new_file",
+ "subdir.txt",
+ NULL
+};
+
+void test_diff_iterator__workdir_1(void)
+{
+ workdir_iterator_test("status", 12, 1, status_paths, "ignored_file");
+}