Improve GIT_EUSER handling This adds giterr_user_cancel to return GIT_EUSER and clear any error message that is sitting around. As a result of using that in places, we need to be more thorough with capturing errors that happen inside a callback when used internally. To help with that, this also adds giterr_capture and giterr_restore so that when we internally use a foreach-type function that clears errors and converts them to GIT_EUSER, it is easier to restore not just the return value, but the actual error message text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383
diff --git a/include/git2/errors.h b/include/git2/errors.h
index f1a8ea1..c6076f3 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -8,7 +8,6 @@
#define INCLUDE_git_errors_h__
#include "common.h"
-#include "buffer.h"
/**
* @file git2/errors.h
@@ -91,7 +90,7 @@ GIT_EXTERN(void) giterr_clear(void);
* Get the last error data and clear it.
*
* This copies the last error into the given `git_error` struct
- * and returns 0 if the copy was successful, leaving the error
+ * and returns 0 if the copy was successful, leaving the error
* cleared as if `giterr_clear` had been called.
*
* If there was no existing error in the library, -1 will be returned
diff --git a/src/attr.c b/src/attr.c
index 51895b7..2fccf21 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -193,8 +193,7 @@ int git_attr_foreach(
error = callback(assign->name, assign->value, payload);
if (error) {
- giterr_clear();
- error = GIT_EUSER;
+ error = giterr_user_cancel();
goto cleanup;
}
}
diff --git a/src/blame.c b/src/blame.c
index ea9f77a..f732338 100644
--- a/src/blame.c
+++ b/src/blame.c
@@ -108,17 +108,23 @@ git_blame* git_blame__alloc(
git_blame_options opts,
const char *path)
{
- git_blame *gbr = (git_blame*)git__calloc(1, sizeof(git_blame));
- if (!gbr) {
- giterr_set_oom();
+ git_blame *gbr = git__calloc(1, sizeof(git_blame));
+ if (!gbr)
return NULL;
- }
- git_vector_init(&gbr->hunks, 8, hunk_cmp);
- git_vector_init(&gbr->paths, 8, paths_cmp);
+
gbr->repository = repo;
gbr->options = opts;
- gbr->path = git__strdup(path);
- git_vector_insert(&gbr->paths, git__strdup(path));
+
+ if (git_vector_init(&gbr->hunks, 8, hunk_cmp) < 0 ||
+ git_vector_init(&gbr->paths, 8, paths_cmp) < 0 ||
+ (gbr->path = git__strdup(path)) == NULL ||
+ git_vector_insert(&gbr->paths, git__strdup(path)) < 0)
+ {
+ git_blame_free(gbr);
+ git__free(gbr);
+ return NULL;
+ }
+
return gbr;
}
@@ -140,7 +146,7 @@ void git_blame_free(git_blame *blame)
git_array_clear(blame->line_index);
- git__free((void*)blame->path);
+ git__free(blame->path);
git_blob_free(blame->final_blob);
git__free(blame);
}
diff --git a/src/blame.h b/src/blame.h
index 637e439..7e23de8 100644
--- a/src/blame.h
+++ b/src/blame.h
@@ -64,7 +64,7 @@ typedef struct git_blame__entry {
} git_blame__entry;
struct git_blame {
- const char *path;
+ char *path;
git_repository *repository;
git_blame_options options;
diff --git a/src/branch.c b/src/branch.c
index 95b3fd9..ef71c2c 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -90,29 +90,28 @@ int git_branch_delete(git_reference *branch)
assert(branch);
- if (!git_reference_is_branch(branch) &&
- !git_reference_is_remote(branch)) {
- giterr_set(GITERR_INVALID, "Reference '%s' is not a valid branch.", git_reference_name(branch));
- return -1;
+ if (!git_reference_is_branch(branch) && !git_reference_is_remote(branch)) {
+ giterr_set(GITERR_INVALID, "Reference '%s' is not a valid branch.",
+ git_reference_name(branch));
+ return GIT_ENOTFOUND;
}
if ((is_head = git_branch_is_head(branch)) < 0)
return is_head;
if (is_head) {
- giterr_set(GITERR_REFERENCE,
- "Cannot delete branch '%s' as it is the current HEAD of the repository.", git_reference_name(branch));
+ giterr_set(GITERR_REFERENCE, "Cannot delete branch '%s' as it is "
+ "the current HEAD of the repository.", git_reference_name(branch));
return -1;
}
- if (git_buf_printf(&config_section, "branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
+ if (git_buf_join(&config_section, '.', "branch",
+ git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
goto on_error;
if (git_config_rename_section(
- git_reference_owner(branch),
- git_buf_cstr(&config_section),
- NULL) < 0)
- goto on_error;
+ git_reference_owner(branch), git_buf_cstr(&config_section), NULL) < 0)
+ goto on_error;
if (git_reference_delete(branch) < 0)
goto on_error;
@@ -206,17 +205,21 @@ int git_branch_move(
if (error < 0)
goto done;
- git_buf_printf(&old_config_section,
- "branch.%s", git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR));
-
- git_buf_printf(&new_config_section, "branch.%s", new_branch_name);
+ /* first update ref then config so failure won't trash config */
- if ((error = git_config_rename_section(git_reference_owner(branch),
- git_buf_cstr(&old_config_section),
- git_buf_cstr(&new_config_section))) < 0)
+ error = git_reference_rename(
+ out, branch, git_buf_cstr(&new_reference_name), force);
+ if (error < 0)
goto done;
- error = git_reference_rename(out, branch, git_buf_cstr(&new_reference_name), force);
+ git_buf_join(&old_config_section, '.', "branch",
+ git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR));
+ git_buf_join(&new_config_section, '.', "branch", new_branch_name);
+
+ error = git_config_rename_section(
+ git_reference_owner(branch),
+ git_buf_cstr(&old_config_section),
+ git_buf_cstr(&new_config_section));
done:
git_buf_free(&new_reference_name);
diff --git a/src/checkout.c b/src/checkout.c
index 6d7e3cf..4305d3e 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -216,7 +216,7 @@ static int checkout_action_common(
if (notify != GIT_CHECKOUT_NOTIFY_NONE &&
checkout_notify(data, notify, delta, wd) != 0)
- return GIT_EUSER;
+ return giterr_user_cancel();
return action;
}
@@ -230,7 +230,7 @@ static int checkout_action_no_wd(
switch (delta->status) {
case GIT_DELTA_UNMODIFIED: /* case 12 */
if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL))
- return GIT_EUSER;
+ return giterr_user_cancel();
action = CHECKOUT_ACTION_IF(SAFE_CREATE, UPDATE_BLOB, NONE);
break;
case GIT_DELTA_ADDED: /* case 2 or 28 (and 5 but not really) */
@@ -302,7 +302,7 @@ static int checkout_action_wd_only(
}
if (checkout_notify(data, notify, NULL, wd))
- return GIT_EUSER;
+ return giterr_user_cancel();
if (remove) {
char *path = git_pool_strdup(&data->pool, wd->path);
@@ -342,7 +342,7 @@ static int checkout_action_with_wd(
if (checkout_is_workdir_modified(data, &delta->old_file, wd)) {
if (checkout_notify(
data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd))
- return GIT_EUSER;
+ return giterr_user_cancel();
action = CHECKOUT_ACTION_IF(FORCE, UPDATE_BLOB, NONE);
}
break;
@@ -406,7 +406,7 @@ static int checkout_action_with_wd_blocker(
case GIT_DELTA_UNMODIFIED:
/* should show delta as dirty / deleted */
if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, wd))
- return GIT_EUSER;
+ return giterr_user_cancel();
action = CHECKOUT_ACTION_IF(FORCE, REMOVE_AND_UPDATE, NONE);
break;
case GIT_DELTA_ADDED:
@@ -439,7 +439,7 @@ static int checkout_action_with_wd_dir(
if (checkout_notify(data, GIT_CHECKOUT_NOTIFY_DIRTY, delta, NULL) ||
checkout_notify(
data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd))
- return GIT_EUSER;
+ return giterr_user_cancel();
break;
case GIT_DELTA_ADDED:/* case 4 (and 7 for dir) */
case GIT_DELTA_MODIFIED: /* case 20 (or 37 but not really) */
@@ -452,7 +452,7 @@ static int checkout_action_with_wd_dir(
if (delta->old_file.mode != GIT_FILEMODE_TREE &&
checkout_notify(
data, GIT_CHECKOUT_NOTIFY_UNTRACKED, NULL, wd))
- return GIT_EUSER;
+ return giterr_user_cancel();
break;
case GIT_DELTA_TYPECHANGE: /* case 24 or 31 */
if (delta->old_file.mode == GIT_FILEMODE_TREE) {
@@ -1998,9 +1998,6 @@ int git_checkout_iterator(
assert(data.completed_steps == data.total_steps);
cleanup:
- if (error == GIT_EUSER)
- giterr_clear();
-
if (!error && data.index != NULL &&
(data.strategy & GIT_CHECKOUT_DONT_UPDATE_INDEX) == 0)
error = git_index_write(data.index);
diff --git a/src/common.h b/src/common.h
index a188878..0ad4130 100644
--- a/src/common.h
+++ b/src/common.h
@@ -78,28 +78,42 @@ int giterr_set_regex(const regex_t *regex, int error_code);
/**
* Gets the system error code for this thread.
*/
-GIT_INLINE(int) giterr_system_last(void)
-{
-#ifdef GIT_WIN32
- return GetLastError();
-#else
- return errno;
-#endif
-}
+int giterr_system_last(void);
/**
* Sets the system error code for this thread.
*/
-GIT_INLINE(void) giterr_system_set(int code)
+void giterr_system_set(int code);
+
+/**
+ * Note that a user cancelled an operation with GIT_EUSER
+ */
+GIT_INLINE(int) giterr_user_cancel(void)
{
-#ifdef GIT_WIN32
- SetLastError(code);
-#else
- errno = code;
-#endif
+ giterr_clear();
+ return GIT_EUSER;
}
/**
+ * Structure to preserve libgit2 error state
+ */
+typedef struct {
+ int error_code;
+ git_error error_msg;
+} git_error_state;
+
+/**
+ * Capture current error state to restore later, returning error code.
+ * If `error_code` is zero, this does nothing and returns zero.
+ */
+int giterr_capture(git_error_state *state, int error_code);
+
+/**
+ * Restore error state to a previous value, returning saved error code.
+ */
+int giterr_restore(git_error_state *state);
+
+/**
* Check a versioned structure for validity
*/
GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)
diff --git a/src/config.c b/src/config.c
index 227adbc..3af9d58 100644
--- a/src/config.c
+++ b/src/config.c
@@ -501,20 +501,18 @@ int git_config_backend_foreach_match(
return -1;
}
- while(!(iter->next(&entry, iter) < 0)) {
+ while (!(iter->next(&entry, iter) < 0)) {
/* skip non-matching keys if regexp was provided */
if (regexp && regexec(®ex, entry->name, 0, NULL, 0) != 0)
continue;
/* abort iterator on non-zero return value */
if (fn(entry, data)) {
- giterr_clear();
- result = GIT_EUSER;
- goto cleanup;
+ result = giterr_user_cancel();
+ break;
}
}
-cleanup:
if (regexp != NULL)
regfree(®ex);
@@ -537,9 +535,8 @@ int git_config_foreach_match(
return error;
while ((error = git_config_next(&entry, iter)) == 0) {
- if(cb(entry, payload)) {
- giterr_clear();
- error = GIT_EUSER;
+ if (cb(entry, payload)) {
+ error = giterr_user_cancel();
break;
}
}
@@ -800,9 +797,10 @@ int git_config_get_multivar_foreach(
found = 0;
while ((err = iter->next(&entry, iter)) == 0) {
found = 1;
- if(cb(entry, payload)) {
+
+ if (cb(entry, payload)) {
iter->free(iter);
- return GIT_EUSER;
+ return giterr_user_cancel();
}
}
@@ -1214,7 +1212,7 @@ struct rename_data {
git_config *config;
git_buf *name;
size_t old_len;
- int actual_error;
+ git_error_state error;
};
static int rename_config_entries_cb(
@@ -1237,9 +1235,8 @@ static int rename_config_entries_cb(
if (!error)
error = git_config_delete_entry(data->config, entry->name);
- data->actual_error = error; /* preserve actual error code */
-
- return error;
+ /* capture error message as needed, since it will become EUSER */
+ return giterr_capture(&data->error, error);
}
int git_config_rename_section(
@@ -1260,10 +1257,10 @@ int git_config_rename_section(
if ((error = git_repository_config__weakptr(&config, repo)) < 0)
goto cleanup;
+ memset(&data, 0, sizeof(data));
data.config = config;
data.name = &replace;
data.old_len = strlen(old_section_name) + 1;
- data.actual_error = 0;
if ((error = git_buf_join(&replace, '.', new_section_name, "")) < 0)
goto cleanup;
@@ -1281,7 +1278,7 @@ int git_config_rename_section(
config, git_buf_cstr(&pattern), rename_config_entries_cb, &data);
if (error == GIT_EUSER)
- error = data.actual_error;
+ error = giterr_restore(&data.error);
cleanup:
git_buf_free(&pattern);
diff --git a/src/diff.c b/src/diff.c
index 53a8f46..ad058af 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -120,7 +120,7 @@ static int diff_delta__from_one(
return -1;
}
- return notify_res < 0 ? GIT_EUSER : 0;
+ return notify_res < 0 ? giterr_user_cancel() : 0;
}
static int diff_delta__from_two(
@@ -182,7 +182,7 @@ static int diff_delta__from_two(
return -1;
}
- return notify_res < 0 ? GIT_EUSER : 0;
+ return notify_res < 0 ? giterr_user_cancel() : 0;
}
static git_diff_delta *diff_delta__last_for_item(
@@ -1343,7 +1343,7 @@ int git_diff__paired_foreach(
int (*cb)(git_diff_delta *h2i, git_diff_delta *i2w, void *payload),
void *payload)
{
- int cmp;
+ int cmp, error = 0;
git_diff_delta *h2i, *i2w;
size_t i, j, i_max, j_max;
int (*strcomp)(const char *, const char *) = git__strcmp;
@@ -1399,18 +1399,17 @@ int git_diff__paired_foreach(
strcomp(h2i->new_file.path, i2w->old_file.path);
if (cmp < 0) {
- if (cb(h2i, NULL, payload))
- return GIT_EUSER;
- i++;
+ i++; i2w = NULL;
} else if (cmp > 0) {
- if (cb(NULL, i2w, payload))
- return GIT_EUSER;
- j++;
+ j++; h2i = NULL;
} else {
- if (cb(h2i, i2w, payload))
- return GIT_EUSER;
i++; j++;
}
+
+ if (cb(h2i, i2w, payload)) {
+ error = giterr_user_cancel();
+ break;
+ }
}
/* restore case-insensitive delta sort */
@@ -1426,5 +1425,5 @@ int git_diff__paired_foreach(
git_vector_sort(&idx2wd->deltas);
}
- return 0;
+ return error;
}
diff --git a/src/diff_patch.c b/src/diff_patch.c
index cc49d68..c091055 100644
--- a/src/diff_patch.c
+++ b/src/diff_patch.c
@@ -32,6 +32,7 @@ struct git_patch {
git_array_t(git_diff_line) lines;
size_t content_size, context_size, header_size;
git_pool flattened;
+ git_error_state error;
};
enum {
@@ -193,21 +194,17 @@ cleanup:
return error;
}
-static int diff_patch_file_callback(
+static int diff_patch_invoke_file_callback(
git_patch *patch, git_diff_output *output)
{
- float progress;
-
- if (!output->file_cb)
- return 0;
-
- progress = patch->diff ?
+ float progress = patch->diff ?
((float)patch->delta_index / patch->diff->deltas.length) : 1.0f;
- if (output->file_cb(patch->delta, progress, output->payload) != 0)
- output->error = GIT_EUSER;
+ if (output->file_cb &&
+ output->file_cb(patch->delta, progress, output->payload) != 0)
+ return giterr_user_cancel();
- return output->error;
+ return 0;
}
static int diff_patch_generate(git_patch *patch, git_diff_output *output)
@@ -229,7 +226,7 @@ static int diff_patch_generate(git_patch *patch, git_diff_output *output)
return 0;
if (output->diff_cb != NULL &&
- !(error = output->diff_cb(output, patch)))
+ (error = output->diff_cb(output, patch)) < 0)
patch->flags |= GIT_DIFF_PATCH_DIFFED;
return error;
@@ -272,9 +269,10 @@ int git_diff_foreach(
size_t idx;
git_patch patch;
- if (diff_required(diff, "git_diff_foreach") < 0)
- return -1;
+ if ((error = diff_required(diff, "git_diff_foreach")) < 0)
+ return error;
+ memset(&xo, 0, sizeof(xo));
diff_output_init(
&xo.output, &diff->opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, &diff->opts);
@@ -285,22 +283,18 @@ int git_diff_foreach(
if (git_diff_delta__should_skip(&diff->opts, patch.delta))
continue;
- if (!(error = diff_patch_init_from_diff(&patch, diff, idx))) {
-
- error = diff_patch_file_callback(&patch, &xo.output);
+ if ((error = diff_patch_init_from_diff(&patch, diff, idx)) < 0)
+ break;
- if (!error)
- error = diff_patch_generate(&patch, &xo.output);
+ if (!(error = diff_patch_invoke_file_callback(&patch, &xo.output)))
+ error = diff_patch_generate(&patch, &xo.output);
- git_patch_free(&patch);
- }
+ git_patch_free(&patch);
if (error < 0)
break;
}
- if (error == GIT_EUSER)
- giterr_clear(); /* don't leave error message set invalidly */
return error;
}
@@ -332,7 +326,7 @@ static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo)
!(patch->ofile.opts_flags & GIT_DIFF_INCLUDE_UNMODIFIED))
return error;
- error = diff_patch_file_callback(patch, (git_diff_output *)xo);
+ error = diff_patch_invoke_file_callback(patch, (git_diff_output *)xo);
if (!error)
error = diff_patch_generate(patch, (git_diff_output *)xo);
@@ -424,9 +418,7 @@ int git_diff_blobs(
diff_patch_with_delta pd;
git_xdiff_output xo;
- memset(&pd, 0, sizeof(pd));
memset(&xo, 0, sizeof(xo));
-
diff_output_init(
&xo.output, opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, opts);
@@ -436,6 +428,7 @@ int git_diff_blobs(
else if (!new_path && old_path)
new_path = old_path;
+ memset(&pd, 0, sizeof(pd));
error = diff_patch_from_blobs(
&pd, &xo, old_blob, old_path, new_blob, new_path, opts);
@@ -463,13 +456,15 @@ int git_patch_from_blobs(
return -1;
memset(&xo, 0, sizeof(xo));
-
diff_output_to_patch(&xo.output, &pd->patch);
git_xdiff_init(&xo, opts);
error = diff_patch_from_blobs(
pd, &xo, old_blob, old_path, new_blob, new_path, opts);
+ if (error == GIT_EUSER)
+ error = giterr_restore(&pd->patch.error);
+
if (!error)
*out = (git_patch *)pd;
else
@@ -536,9 +531,7 @@ int git_diff_blob_to_buffer(
diff_patch_with_delta pd;
git_xdiff_output xo;
- memset(&pd, 0, sizeof(pd));
memset(&xo, 0, sizeof(xo));
-
diff_output_init(
&xo.output, opts, file_cb, hunk_cb, data_cb, payload);
git_xdiff_init(&xo, opts);
@@ -548,6 +541,7 @@ int git_diff_blob_to_buffer(
else if (!buf_path && old_path)
buf_path = old_path;
+ memset(&pd, 0, sizeof(pd));
error = diff_patch_from_blob_and_buffer(
&pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts);
@@ -576,13 +570,15 @@ int git_patch_from_blob_and_buffer(
return -1;
memset(&xo, 0, sizeof(xo));
-
diff_output_to_patch(&xo.output, &pd->patch);
git_xdiff_init(&xo, opts);
error = diff_patch_from_blob_and_buffer(
pd, &xo, old_blob, old_path, buf, buflen, buf_path, opts);
+ if (error == GIT_EUSER)
+ error = giterr_restore(&pd->patch.error);
+
if (!error)
*out = (git_patch *)pd;
else
@@ -622,14 +618,18 @@ int git_patch_from_diff(
if ((error = diff_patch_alloc_from_diff(&patch, diff, idx)) < 0)
return error;
+ memset(&xo, 0, sizeof(xo));
diff_output_to_patch(&xo.output, patch);
git_xdiff_init(&xo, &diff->opts);
- error = diff_patch_file_callback(patch, &xo.output);
+ error = diff_patch_invoke_file_callback(patch, &xo.output);
if (!error)
error = diff_patch_generate(patch, &xo.output);
+ if (error == GIT_EUSER)
+ error = giterr_restore(&patch->error);
+
if (!error) {
/* if cumulative diff size is < 0.5 total size, flatten the patch */
/* unload the file content */
@@ -879,7 +879,8 @@ static int diff_patch_hunk_cb(
GIT_UNUSED(delta);
hunk = git_array_alloc(patch->hunks);
- GITERR_CHECK_ALLOC(hunk);
+ if (!hunk)
+ return giterr_capture(&patch->error, -1);
memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk));
@@ -905,10 +906,11 @@ static int diff_patch_line_cb(
GIT_UNUSED(hunk_);
hunk = git_array_last(patch->hunks);
- GITERR_CHECK_ALLOC(hunk);
+ assert(hunk); /* programmer error if no hunk is available */
line = git_array_alloc(patch->lines);
- GITERR_CHECK_ALLOC(line);
+ if (!line)
+ return giterr_capture(&patch->error, -1);
memcpy(line, line_, sizeof(*line));
diff --git a/src/diff_print.c b/src/diff_print.c
index b04b115..7124028 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -18,6 +18,7 @@ typedef struct {
uint32_t flags;
int oid_strlen;
git_diff_line line;
+ git_error_state error;
} diff_print_info;
static int diff_print_info_init(
@@ -33,6 +34,7 @@ static int diff_print_info_init(
pi->print_cb = cb;
pi->payload = payload;
pi->buf = out;
+ memset(&pi->error, 0, sizeof(pi->error));
if (diff)
pi->flags = diff->opts.flags;
@@ -89,12 +91,6 @@ char git_diff_status_char(git_delta_t status)
return code;
}
-static int callback_error(void)
-{
- giterr_clear();
- return GIT_EUSER;
-}
-
static int diff_print_one_name_only(
const git_diff_delta *delta, float progress, void *data)
{
@@ -111,14 +107,14 @@ static int diff_print_one_name_only(
if (git_buf_puts(out, delta->new_file.path) < 0 ||
git_buf_putc(out, '\n'))
- return -1;
+ return giterr_capture(&pi->error, -1);
pi->line.origin = GIT_DIFF_LINE_FILE_HDR;
pi->line.content = git_buf_cstr(out);
pi->line.content_len = git_buf_len(out);
if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
- return callback_error();
+ return giterr_user_cancel();
return 0;
}
@@ -156,14 +152,14 @@ static int diff_print_one_name_status(
git_buf_printf(out, "%c\t%s\n", code, delta->old_file.path);
if (git_buf_oom(out))
- return -1;
+ return giterr_capture(&pi->error, -1);
pi->line.origin = GIT_DIFF_LINE_FILE_HDR;
pi->line.content = git_buf_cstr(out);
pi->line.content_len = git_buf_len(out);
if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
- return callback_error();
+ return giterr_user_cancel();
return 0;
}
@@ -202,14 +198,14 @@ static int diff_print_one_raw(
delta->old_file.path : delta->new_file.path);
if (git_buf_oom(out))
- return -1;
+ return giterr_capture(&pi->error, -1);
pi->line.origin = GIT_DIFF_LINE_FILE_HDR;
pi->line.content = git_buf_cstr(out);
pi->line.content_len = git_buf_len(out);
if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
- return callback_error();
+ return giterr_user_cancel();
return 0;
}
@@ -315,14 +311,14 @@ static int diff_print_patch_file(
if (git_diff_delta__format_file_header(
pi->buf, delta, oldpfx, newpfx, pi->oid_strlen) < 0)
- return -1;
+ return giterr_capture(&pi->error, -1);
pi->line.origin = GIT_DIFF_LINE_FILE_HDR;
pi->line.content = git_buf_cstr(pi->buf);
pi->line.content_len = git_buf_len(pi->buf);
if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
- return callback_error();
+ return giterr_user_cancel();
if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
return 0;
@@ -332,7 +328,7 @@ static int diff_print_patch_file(
if (diff_delta_format_with_paths(
pi->buf, delta, oldpfx, newpfx,
"Binary files %s%s and %s%s differ\n") < 0)
- return -1;
+ return giterr_capture(&pi->error, -1);
pi->line.origin = GIT_DIFF_LINE_BINARY;
pi->line.content = git_buf_cstr(pi->buf);
@@ -340,7 +336,7 @@ static int diff_print_patch_file(
pi->line.num_lines = 1;
if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
- return callback_error();
+ return giterr_user_cancel();
return 0;
}
@@ -360,7 +356,7 @@ static int diff_print_patch_hunk(
pi->line.content_len = h->header_len;
if (pi->print_cb(d, h, &pi->line, pi->payload))
- return callback_error();
+ return giterr_user_cancel();
return 0;
}
@@ -377,7 +373,7 @@ static int diff_print_patch_line(
return 0;
if (pi->print_cb(delta, hunk, line, pi->payload))
- return callback_error();
+ return giterr_user_cancel();
return 0;
}
@@ -421,9 +417,14 @@ int git_diff_print(
if (!(error = diff_print_info_init(
&pi, &buf, diff, format, print_cb, payload)))
+ {
error = git_diff_foreach(
diff, print_file, print_hunk, print_line, &pi);
+ if (error == GIT_EUSER && pi.error.error_code)
+ error = giterr_restore(&pi.error);
+ }
+
git_buf_free(&buf);
return error;
@@ -444,10 +445,15 @@ int git_patch_print(
if (!(error = diff_print_info_init(
&pi, &temp, git_patch__diff(patch),
GIT_DIFF_FORMAT_PATCH, print_cb, payload)))
+ {
error = git_patch__invoke_callbacks(
patch, diff_print_patch_file, diff_print_patch_hunk,
diff_print_patch_line, &pi);
+ if (error && error != GIT_EUSER)
+ error = giterr_restore(&pi.error);
+ }
+
git_buf_free(&temp);
return error;
@@ -483,8 +489,10 @@ int git_patch_to_str(
/* GIT_EUSER means git_buf_put in print_to_buffer_cb returned -1,
* meaning a memory allocation failure, so just map to -1...
*/
- if (error == GIT_EUSER)
+ if (error == GIT_EUSER) {
+ giterr_set_oom();
error = -1;
+ }
*string = git_buf_detach(&output);
diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c
index e0bc11f..c6ca488 100644
--- a/src/diff_xdiff.c
+++ b/src/diff_xdiff.c
@@ -28,25 +28,29 @@ static int git_xdiff_parse_hunk(git_diff_hunk *hunk, const char *header)
{
/* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */
if (*header != '@')
- return -1;
+ goto fail;
if (git_xdiff_scan_int(&header, &hunk->old_start) < 0)
- return -1;
+ goto fail;
if (*header == ',') {
if (git_xdiff_scan_int(&header, &hunk->old_lines) < 0)
- return -1;
+ goto fail;
} else
hunk->old_lines = 1;
if (git_xdiff_scan_int(&header, &hunk->new_start) < 0)
- return -1;
+ goto fail;
if (*header == ',') {
if (git_xdiff_scan_int(&header, &hunk->new_lines) < 0)
- return -1;
+ goto fail;
} else
hunk->new_lines = 1;
if (hunk->old_start < 0 || hunk->new_start < 0)
- return -1;
+ goto fail;
return 0;
+
+fail:
+ giterr_set(GITERR_INVALID, "Malformed hunk header from xdiff");
+ return -1;
}
typedef struct {
@@ -123,7 +127,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
if (output->hunk_cb != NULL &&
output->hunk_cb(delta, &info->hunk, output->payload))
- output->error = GIT_EUSER;
+ return (output->error = giterr_user_cancel());
info->old_lineno = info->hunk.old_start;
info->new_lineno = info->hunk.new_start;
@@ -149,7 +153,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
if (!output->error &&
output->data_cb != NULL &&
output->data_cb(delta, &info->hunk, &line, output->payload))
- output->error = GIT_EUSER;
+ output->error = giterr_user_cancel();
}
if (len == 3 && !output->error) {
@@ -171,7 +175,7 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
if (!output->error &&
output->data_cb != NULL &&
output->data_cb(delta, &info->hunk, &line, output->payload))
- output->error = GIT_EUSER;
+ output->error = giterr_user_cancel();
}
return output->error;
@@ -219,11 +223,9 @@ void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts)
xo->output.diff_cb = git_xdiff;
- memset(&xo->config, 0, sizeof(xo->config));
xo->config.ctxlen = opts ? opts->context_lines : 3;
xo->config.interhunkctxlen = opts ? opts->interhunk_lines : 0;
- memset(&xo->params, 0, sizeof(xo->params));
if (flags & GIT_DIFF_IGNORE_WHITESPACE)
xo->params.flags |= XDF_WHITESPACE_FLAGS;
if (flags & GIT_DIFF_IGNORE_WHITESPACE_CHANGE)
@@ -236,6 +238,5 @@ void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts)
if (flags & GIT_DIFF_MINIMAL)
xo->params.flags |= XDF_NEED_MINIMAL;
- memset(&xo->callback, 0, sizeof(xo->callback));
xo->callback.outf = git_xdiff_cb;
}
diff --git a/src/errors.c b/src/errors.c
index d04da4c..a0b0859 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -23,7 +23,8 @@ static void set_error(int error_class, char *string)
{
git_error *error = &GIT_GLOBAL->error_t;
- git__free(error->message);
+ if (error->message != string)
+ git__free(error->message);
error->message = string;
error->klass = error_class;
@@ -103,8 +104,10 @@ int giterr_set_regex(const regex_t *regex, int error_code)
void giterr_clear(void)
{
- set_error(0, NULL);
- GIT_GLOBAL->last_error = NULL;
+ if (GIT_GLOBAL->last_error != NULL) {
+ set_error(0, NULL);
+ GIT_GLOBAL->last_error = NULL;
+ }
errno = 0;
#ifdef GIT_WIN32
@@ -134,3 +137,39 @@ const git_error *giterr_last(void)
{
return GIT_GLOBAL->last_error;
}
+
+int giterr_capture(git_error_state *state, int error_code)
+{
+ state->error_code = error_code;
+ if (error_code)
+ giterr_detach(&state->error_msg);
+ return error_code;
+}
+
+int giterr_restore(git_error_state *state)
+{
+ if (state && state->error_code && state->error_msg.message)
+ set_error(state->error_msg.klass, state->error_msg.message);
+ else
+ giterr_clear();
+
+ return state ? state->error_code : 0;
+}
+
+int giterr_system_last(void)
+{
+#ifdef GIT_WIN32
+ return GetLastError();
+#else
+ return errno;
+#endif
+}
+
+void giterr_system_set(int code)
+{
+#ifdef GIT_WIN32
+ SetLastError(code);
+#else
+ errno = code;
+#endif
+}
diff --git a/src/fileops.c b/src/fileops.c
index 5763b37..0418e9e 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -403,8 +403,8 @@ typedef struct {
const char *base;
size_t baselen;
uint32_t flags;
- int error;
int depth;
+ git_error_state error;
} futils__rmdir_data;
#define FUTILS_MAX_DEPTH 100
@@ -447,8 +447,8 @@ static int futils__rm_first_parent(git_buf *path, const char *ceiling)
static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
{
+ int error = 0;
futils__rmdir_data *data = opaque;
- int error = data->error;
struct stat st;
if (data->depth > FUTILS_MAX_DEPTH)
@@ -474,13 +474,16 @@ static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
data->depth++;
error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data);
- if (error < 0)
- return (error == GIT_EUSER) ? data->error : error;
+ if (error == GIT_EUSER)
+ return error;
data->depth--;
+ if (error < 0)
+ goto done;
+
if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0)
- return data->error;
+ goto done;
if ((error = p_rmdir(path->ptr)) < 0) {
if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
@@ -499,35 +502,31 @@ static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
error = futils__error_cannot_rmdir(path->ptr, "still present");
- data->error = error;
- return error;
+done:
+ return giterr_capture(&data->error, error);
}
static int futils__rmdir_empty_parent(void *opaque, git_buf *path)
{
futils__rmdir_data *data = opaque;
- int error;
+ int error = 0;
if (git_buf_len(path) <= data->baselen)
- return GIT_ITEROVER;
-
- error = p_rmdir(git_buf_cstr(path));
+ return giterr_capture(&data->error, GIT_ITEROVER);
- if (error) {
+ if (p_rmdir(git_buf_cstr(path)) < 0) {
int en = errno;
if (en == ENOENT || en == ENOTDIR) {
- giterr_clear();
- error = 0;
+ /* do nothing */
} else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) {
- giterr_clear();
error = GIT_ITEROVER;
} else {
error = git_path_set_error(errno, git_buf_cstr(path), "rmdir");
}
}
- return error;
+ return giterr_capture(&data->error, error);
}
int git_futils_rmdir_r(
@@ -535,12 +534,13 @@ int git_futils_rmdir_r(
{
int error;
git_buf fullpath = GIT_BUF_INIT;
- futils__rmdir_data data = { 0 };
+ futils__rmdir_data data;
/* build path and find "root" where we should start calling mkdir */
if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
return -1;
+ memset(&data, 0, sizeof(data));
data.base = base ? base : "";
data.baselen = base ? strlen(base) : 0;
data.flags = flags;
@@ -548,13 +548,14 @@ int git_futils_rmdir_r(
error = futils__rmdir_recurs_foreach(&data, &fullpath);
/* remove now-empty parents if requested */
- if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0) {
+ if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0)
error = git_path_walk_up(
&fullpath, base, futils__rmdir_empty_parent, &data);
- if (error == GIT_ITEROVER)
- error = 0;
- }
+ if (error == GIT_EUSER)
+ error = giterr_restore(&data.error);
+ if (error == GIT_ITEROVER)
+ error = 0;
git_buf_free(&fullpath);
@@ -858,7 +859,7 @@ typedef struct {
uint32_t flags;
uint32_t mkdir_flags;
mode_t dirmode;
- int error;
+ git_error_state error;
} cp_r_info;
#define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10)
@@ -896,23 +897,21 @@ static int _cp_r_callback(void *ref, git_buf *from)
from->ptr[git_path_basename_offset(from)] == '.')
return 0;
- if (git_buf_joinpath(
- &info->to, info->to_root, from->ptr + info->from_prefix) < 0) {
- error = -1;
- goto exit;
- }
+ if ((error = git_buf_joinpath(
+ &info->to, info->to_root, from->ptr + info->from_prefix)) < 0)
+ goto done;
if (!(error = git_path_lstat(info->to.ptr, &to_st)))
exists = true;
else if (error != GIT_ENOTFOUND)
- goto exit;
+ goto done;
else {
giterr_clear();
error = 0;
}
if ((error = git_path_lstat(from->ptr, &from_st)) < 0)
- goto exit;
+ goto done;
if (S_ISDIR(from_st.st_mode)) {
mode_t oldmode = info->dirmode;
@@ -928,15 +927,14 @@ static int _cp_r_callback(void *ref, git_buf *from)
/* recurse onto target directory */
if (!error && (!exists || S_ISDIR(to_st.st_mode))) {
error = git_path_direach(from, 0, _cp_r_callback, info);
-
if (error == GIT_EUSER)
- error = info->error;
+ return error;
}
if (oldmode != 0)
info->dirmode = oldmode;
- goto exit;
+ goto done;
}
if (exists) {
@@ -947,7 +945,7 @@ static int _cp_r_callback(void *ref, git_buf *from)
giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'",
info->to.ptr);
error = -1;
- goto exit;
+ goto done;
}
}
@@ -960,7 +958,7 @@ static int _cp_r_callback(void *ref, git_buf *from)
/* Make container directory on demand if needed */
if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
(error = _cp_r_mkdir(info, from)) < 0)
- goto exit;
+ goto done;
/* make symlink or regular file */
if (S_ISLNK(from_st.st_mode))
@@ -974,9 +972,8 @@ static int _cp_r_callback(void *ref, git_buf *from)
error = git_futils_cp(from->ptr, info->to.ptr, usemode);
}
-exit:
- info->error = error;
- return error;
+done:
+ return giterr_capture(&info->error, error);
}
int git_futils_cp_r(
@@ -992,11 +989,11 @@ int git_futils_cp_r(
if (git_buf_joinpath(&path, from, "") < 0) /* ensure trailing slash */
return -1;
+ memset(&info, 0, sizeof(info));
info.to_root = to;
info.flags = flags;
info.dirmode = dirmode;
info.from_prefix = path.size;
- info.error = 0;
git_buf_init(&info.to, 0);
/* precalculate mkdir flags */
@@ -1019,7 +1016,7 @@ int git_futils_cp_r(
git_buf_free(&info.to);
if (error == GIT_EUSER)
- error = info.error;
+ error = giterr_restore(&info.error);
return error;
}
diff --git a/src/iterator.c b/src/iterator.c
index 8646399..c129222 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -991,9 +991,8 @@ static int fs_iterator__expand_dir(fs_iterator *fi)
fi->base.start, fi->base.end, &ff->entries);
if (error < 0) {
- git_error last_error = {0};
-
- giterr_detach(&last_error);
+ git_error_state last_error = { 0 };
+ giterr_capture(&last_error, error);
/* these callbacks may clear the error message */
fs_iterator__free_frame(ff);
@@ -1001,12 +1000,7 @@ static int fs_iterator__expand_dir(fs_iterator *fi)
/* next time return value we skipped to */
fi->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
- if (last_error.message) {
- giterr_set_str(last_error.klass, last_error.message);
- free(last_error.message);
- }
-
- return error;
+ return giterr_restore(&last_error);
}
if (ff->entries.length == 0) {
diff --git a/src/odb_loose.c b/src/odb_loose.c
index ced272b..ae772b4 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -696,7 +696,7 @@ struct foreach_state {
size_t dir_len;
git_odb_foreach_cb cb;
void *data;
- int cb_error;
+ git_error_state cb_error;
};
GIT_INLINE(int) filename_to_oid(git_oid *oid, const char *ptr)
@@ -735,10 +735,8 @@ static int foreach_object_dir_cb(void *_state, git_buf *path)
if (filename_to_oid(&oid, path->ptr + state->dir_len) < 0)
return 0;
- if (state->cb(&oid, state->data)) {
- state->cb_error = GIT_EUSER;
- return -1;
- }
+ if (state->cb(&oid, state->data))
+ return giterr_user_cancel();
return 0;
}
@@ -747,7 +745,9 @@ static int foreach_cb(void *_state, git_buf *path)
{
struct foreach_state *state = (struct foreach_state *) _state;
- return git_path_direach(path, 0, foreach_object_dir_cb, state);
+ return giterr_capture(
+ &state->cb_error,
+ git_path_direach(path, 0, foreach_object_dir_cb, state));
}
static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb cb, void *data)
@@ -762,7 +762,8 @@ static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb
objects_dir = backend->objects_dir;
- git_buf_sets(&buf, objects_dir);
+ if (git_buf_sets(&buf, objects_dir) < 0)
+ return -1;
git_path_to_dir(&buf);
memset(&state, 0, sizeof(state));
@@ -772,9 +773,12 @@ static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb
error = git_path_direach(&buf, 0, foreach_cb, &state);
+ if (error == GIT_EUSER)
+ error = giterr_restore(&state.cb_error);
+
git_buf_free(&buf);
- return state.cb_error ? state.cb_error : error;
+ return error;
}
static int loose_backend__stream_fwrite(git_odb_stream *_stream, const git_oid *oid)
diff --git a/src/odb_pack.c b/src/odb_pack.c
index fd2ca0f..2c0319f 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -190,31 +190,45 @@ static int packfile_sort__cb(const void *a_, const void *b_)
}
+struct packfile_load_data {
+ struct pack_backend *backend;
+ git_error_state error;
+};
static int packfile_load__cb(void *_data, git_buf *path)
{
- struct pack_backend *backend = (struct pack_backend *)_data;
+ struct packfile_load_data *data = _data;
+ struct pack_backend *backend = data->backend;
struct git_pack_file *pack;
+ const char *path_str = git_buf_cstr(path);
+ size_t i, cmp_len = git_buf_len(path);
int error;
- size_t i;
- if (git__suffixcmp(path->ptr, ".idx") != 0)
+ if (cmp_len <= strlen(".idx") || git__suffixcmp(path_str, ".idx") != 0)
return 0; /* not an index */
+ cmp_len -= strlen(".idx");
+
for (i = 0; i < backend->packs.length; ++i) {
struct git_pack_file *p = git_vector_get(&backend->packs, i);
- if (memcmp(p->pack_name, git_buf_cstr(path), git_buf_len(path) - strlen(".idx")) == 0)
+
+ if (memcmp(p->pack_name, path_str, cmp_len) == 0)
return 0;
}
error = git_packfile_alloc(&pack, path->ptr);
- if (error == GIT_ENOTFOUND)
- /* ignore missing .pack file as git does */
+
+ /* ignore missing .pack file as git does */
+ if (error == GIT_ENOTFOUND) {
+ giterr_clear();
return 0;
- else if (error < 0)
- return error;
+ }
+
+ if (!error)
+ error = git_vector_insert(&backend->packs, pack);
+
+ return giterr_capture(&data->error, error);
- return git_vector_insert(&backend->packs, pack);
}
static int pack_entry_find_inner(
@@ -314,32 +328,34 @@ static int pack_entry_find_prefix(
* Implement the git_odb_backend API calls
*
***********************************************************/
-static int pack_backend__refresh(git_odb_backend *_backend)
+static int pack_backend__refresh(git_odb_backend *backend)
{
- struct pack_backend *backend = (struct pack_backend *)_backend;
-
+ struct packfile_load_data data;
int error;
struct stat st;
git_buf path = GIT_BUF_INIT;
- if (backend->pack_folder == NULL)
+ memset(&data, 0, sizeof(data));
+ data.backend = (struct pack_backend *)backend;
+
+ if (data.backend->pack_folder == NULL)
return 0;
- if (p_stat(backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode))
+ if (p_stat(data.backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode))
return git_odb__error_notfound("failed to refresh packfiles", NULL);
- git_buf_sets(&path, backend->pack_folder);
+ git_buf_sets(&path, data.backend->pack_folder);
/* reload all packs */
- error = git_path_direach(&path, 0, packfile_load__cb, backend);
+ error = git_path_direach(&path, 0, packfile_load__cb, &data);
- git_buf_free(&path);
+ if (error == GIT_EUSER)
+ error = giterr_restore(&data.error);
- if (error < 0)
- return -1;
+ git_buf_free(&path);
+ git_vector_sort(&data.backend->packs);
- git_vector_sort(&backend->packs);
- return 0;
+ return error;
}
static int pack_backend__read_header_internal(
diff --git a/src/path.c b/src/path.c
index 750dd3e..8be41c1 100644
--- a/src/path.c
+++ b/src/path.c
@@ -434,7 +434,8 @@ int git_path_walk_up(
iter.asize = path->asize;
while (scan >= stop) {
- error = cb(data, &iter);
+ if (cb(data, &iter))
+ error = giterr_user_cancel();
iter.ptr[scan] = oldc;
if (error < 0)
break;
@@ -528,7 +529,9 @@ bool git_path_is_empty_dir(const char *path)
if (!git_path_isdir(path))
return false;
- if (!(error = git_buf_sets(&dir, path)))
+ if ((error = git_buf_sets(&dir, path)) != 0)
+ giterr_clear();
+ else
error = git_path_direach(&dir, 0, path_found_entry, NULL);
git_buf_free(&dir);
@@ -867,7 +870,7 @@ int git_path_direach(
if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0)
break;
#endif
-
+
if ((error = git_buf_put(path, de_path, de_len)) < 0)
break;
@@ -876,7 +879,7 @@ int git_path_direach(
git_buf_truncate(path, wd_len); /* restore path */
if (error) {
- error = GIT_EUSER;
+ error = giterr_user_cancel();
break;
}
}
diff --git a/src/pool.c b/src/pool.c
index d484769..4796d0a 100644
--- a/src/pool.c
+++ b/src/pool.c
@@ -217,7 +217,14 @@ char *git_pool_strdup(git_pool *pool, const char *str)
char *git_pool_strdup_safe(git_pool *pool, const char *str)
{
- return str ? git_pool_strdup(pool, str) : NULL;
+ if (!str)
+ return NULL;
+ else {
+ char *result = git_pool_strdup(pool, str);
+ if (!result)
+ giterr_clear();
+ return result;
+ }
}
char *git_pool_strcat(git_pool *pool, const char *a, const char *b)
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 62d5c10..938e02a 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -264,9 +264,14 @@ done:
return error;
}
-static int _dirent_loose_load(void *data, git_buf *full_path)
+struct packed_loadloose_data {
+ refdb_fs_backend *backend;
+ git_error_state error;
+};
+
+static int _dirent_loose_load(void *data_, git_buf *full_path)
{
- refdb_fs_backend *backend = (refdb_fs_backend *)data;
+ struct packed_loadloose_data *data = data_;
const char *file_path;
if (git__suffixcmp(full_path->ptr, ".lock") == 0)
@@ -274,11 +279,12 @@ static int _dirent_loose_load(void *data, git_buf *full_path)
if (git_path_isdir(full_path->ptr))
return git_path_direach(
- full_path, backend->direach_flags, _dirent_loose_load, backend);
+ full_path, data->backend->direach_flags, _dirent_loose_load, data);
- file_path = full_path->ptr + strlen(backend->path);
+ file_path = full_path->ptr + strlen(data->backend->path);
- return loose_lookup_to_packfile(backend, file_path);
+ return giterr_capture(
+ &data->error, loose_lookup_to_packfile(data->backend, file_path));
}
/*
@@ -291,21 +297,28 @@ static int packed_loadloose(refdb_fs_backend *backend)
{
int error;
git_buf refs_path = GIT_BUF_INIT;
+ struct packed_loadloose_data data;
if (git_buf_joinpath(&refs_path, backend->path, GIT_REFS_DIR) < 0)
return -1;
+ memset(&data, 0, sizeof(data));
+ data.backend = backend;
+
/*
* Load all the loose files from disk into the Packfile table.
* This will overwrite any old packed entries with their
* updated loose versions
*/
error = git_path_direach(
- &refs_path, backend->direach_flags, _dirent_loose_load, backend);
+ &refs_path, backend->direach_flags, _dirent_loose_load, &data);
git_buf_free(&refs_path);
- return (error == GIT_EUSER) ? -1 : error;
+ if (error == GIT_EUSER)
+ error = giterr_restore(&data.error);
+
+ return error;
}
static int refdb_fs_backend__exists(
diff --git a/src/refs.c b/src/refs.c
index 472a798..bae6215 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -471,7 +471,7 @@ int git_reference_rename(
if ((error = git_refdb_rename(out, ref->db, ref->name, new_name, force)) < 0)
return error;
- /* Update HEAD it was poiting to the reference being renamed. */
+ /* Update HEAD it was pointing to the reference being renamed */
if (should_head_be_updated &&
(error = git_repository_set_head(ref->db->repo, new_name)) < 0) {
giterr_set(GITERR_REFERENCE, "Failed to update HEAD after renaming reference");
diff --git a/src/remote.c b/src/remote.c
index 6f86a4b..e4bebe1 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -251,13 +251,14 @@ int git_remote_create_inmemory(git_remote **out, git_repository *repo, const cha
struct refspec_cb_data {
git_remote *remote;
int fetch;
+ git_error_state error;
};
static int refspec_cb(const git_config_entry *entry, void *payload)
{
- const struct refspec_cb_data *data = (struct refspec_cb_data *)payload;
-
- return add_refspec(data->remote, entry->value, data->fetch);
+ struct refspec_cb_data *data = (struct refspec_cb_data *)payload;
+ return giterr_capture(
+ &data->error, add_refspec(data->remote, entry->value, data->fetch));
}
static int get_optional_config(
@@ -283,9 +284,6 @@ static int get_optional_config(
error = 0;
}
- if (error < 0)
- error = -1;
-
return error;
}
@@ -296,7 +294,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
const char *val;
int error = 0;
git_config *config;
- struct refspec_cb_data data;
+ struct refspec_cb_data data = { NULL };
bool optional_setting_found = false, found;
assert(out && repo && name);
@@ -363,6 +361,7 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
data.remote = remote;
data.fetch = true;
+
git_buf_clear(&buf);
git_buf_printf(&buf, "remote.%s.fetch", name);
@@ -388,6 +387,9 @@ int git_remote_load(git_remote **out, git_repository *repo, const char *name)
cleanup:
git_buf_free(&buf);
+ if (error == GIT_EUSER)
+ error = giterr_restore(&data.error);
+
if (error < 0)
git_remote_free(remote);
@@ -1110,9 +1112,14 @@ void git_remote_free(git_remote *remote)
git__free(remote);
}
+struct remote_list_data {
+ git_vector list;
+ git_error_state error;
+};
+
static int remote_list_cb(const git_config_entry *entry, void *payload)
{
- git_vector *list = payload;
+ struct remote_list_data *data = payload;
const char *name = entry->name + strlen("remote.");
size_t namelen = strlen(name);
char *remote_name;
@@ -1123,47 +1130,50 @@ static int remote_list_cb(const git_config_entry *entry, void *payload)
remote_name = git__strndup(name, namelen - 4); /* strip ".url" */
else
remote_name = git__strndup(name, namelen - 8); /* strip ".pushurl" */
- GITERR_CHECK_ALLOC(remote_name);
+ if (!remote_name)
+ return giterr_capture(&data->error, -1);
- return git_vector_insert(list, remote_name);
+ return giterr_capture(
+ &data->error, git_vector_insert(&data->list, remote_name));
}
int git_remote_list(git_strarray *remotes_list, git_repository *repo)
{
- git_config *cfg;
- git_vector list;
int error;
+ git_config *cfg;
+ struct remote_list_data data;
- if (git_repository_config__weakptr(&cfg, repo) < 0)
- return -1;
+ if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
+ return error;
- if (git_vector_init(&list, 4, git__strcmp_cb) < 0)
- return -1;
+ memset(&data, 0, sizeof(data));
+ if ((error = git_vector_init(&data.list, 4, git__strcmp_cb)) < 0)
+ return error;
error = git_config_foreach_match(
- cfg, "^remote\\..*\\.(push)?url$", remote_list_cb, &list);
+ cfg, "^remote\\..*\\.(push)?url$", remote_list_cb, &data);
+
+ /* cb error is converted to GIT_EUSER by git_config_foreach */
+ if (error == GIT_EUSER)
+ error = giterr_restore(&data.error);
if (error < 0) {
size_t i;
char *elem;
- git_vector_foreach(&list, i, elem) {
+ git_vector_foreach(&data.list, i, elem) {
git__free(elem);
}
- git_vector_free(&list);
-
- /* cb error is converted to GIT_EUSER by git_config_foreach */
- if (error == GIT_EUSER)
- error = -1;
+ git_vector_free(&data.list);
return error;
}
- git_vector_uniq(&list, git__free);
+ git_vector_uniq(&data.list, git__free);
- remotes_list->strings = (char **)list.contents;
- remotes_list->count = list.length;
+ remotes_list->strings = (char **)data.list.contents;
+ remotes_list->count = data.list.length;
return 0;
}
@@ -1250,11 +1260,11 @@ cleanup:
return error;
}
-struct update_data
-{
+struct update_data {
git_config *config;
const char *old_remote_name;
const char *new_remote_name;
+ git_error_state error;
};
static int update_config_entries_cb(
@@ -1266,10 +1276,9 @@ static int update_config_entries_cb(
if (strcmp(entry->value, data->old_remote_name))
return 0;
- return git_config_set_string(
- data->config,
- entry->name,
- data->new_remote_name);
+ return giterr_capture(
+ &data->error, git_config_set_string(
+ data->config, entry->name, data->new_remote_name));
}
static int update_branch_remote_config_entry(
@@ -1277,20 +1286,22 @@ static int update_branch_remote_config_entry(
const char *old_name,
const char *new_name)
{
- git_config *config;
- struct update_data data;
+ int error;
+ struct update_data data = { NULL };
- if (git_repository_config__weakptr(&config, repo) < 0)
- return -1;
+ if ((error = git_repository_config__weakptr(&data.config, repo)) < 0)
+ return error;
- data.config = config;
data.old_remote_name = old_name;
data.new_remote_name = new_name;
- return git_config_foreach_match(
- config,
- "branch\\..+\\.remote",
- update_config_entries_cb, &data);
+ error = git_config_foreach_match(
+ data.config, "branch\\..+\\.remote", update_config_entries_cb, &data);
+
+ if (error == GIT_EUSER)
+ error = giterr_restore(&data.error);
+
+ return error;
}
static int rename_one_remote_reference(
@@ -1298,18 +1309,20 @@ static int rename_one_remote_reference(
const char *old_remote_name,
const char *new_remote_name)
{
- int error = -1;
+ int error;
git_buf new_name = GIT_BUF_INIT;
- if (git_buf_printf(
+ error = git_buf_printf(
&new_name,
GIT_REFS_REMOTES_DIR "%s%s",
new_remote_name,
- reference->name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name)) < 0)
- return -1;
+ reference->name + strlen(GIT_REFS_REMOTES_DIR) + strlen(old_remote_name));
- error = git_reference_rename(NULL, reference, git_buf_cstr(&new_name), 0);
- git_reference_free(reference);
+ if (!error) {
+ error = git_reference_rename(
+ NULL, reference, git_buf_cstr(&new_name), 0);
+ git_reference_free(reference);
+ }
git_buf_free(&new_name);
return error;
@@ -1320,12 +1333,12 @@ static int rename_remote_references(
const char *old_name,
const char *new_name)
{
- int error = -1;
+ int error;
git_reference *ref;
git_reference_iterator *iter;
- if (git_reference_iterator_new(&iter, repo) < 0)
- return -1;
+ if ((error = git_reference_iterator_new(&iter, repo)) < 0)
+ return error;
while ((error = git_reference_next(&ref, iter)) == 0) {
if (git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR)) {
@@ -1333,18 +1346,13 @@ static int rename_remote_references(
continue;
}
- if ((error = rename_one_remote_reference(ref, old_name, new_name)) < 0) {
- git_reference_iterator_free(iter);
- return error;
- }
+ if ((error = rename_one_remote_reference(ref, old_name, new_name)) < 0)
+ break;
}
git_reference_iterator_free(iter);
- if (error == GIT_ITEROVER)
- return 0;
-
- return error;
+ return (error == GIT_ITEROVER) ? 0 : error;
}
static int rename_fetch_refspecs(
diff --git a/src/status.c b/src/status.c
index 07fdcb5..fb99fb4 100644
--- a/src/status.c
+++ b/src/status.c
@@ -152,25 +152,32 @@ static git_status_t status_compute(
return st;
}
+struct status_data {
+ git_status_list *status;
+ git_error_state err;
+};
+
static int status_collect(
git_diff_delta *head2idx,
git_diff_delta *idx2wd,
void *payload)
{
- git_status_list *status = payload;
+ struct status_data *data = payload;
git_status_entry *status_entry;
- if (!status_is_included(status, head2idx, idx2wd))
+ if (!status_is_included(data->status, head2idx, idx2wd))
return 0;
status_entry = git__malloc(sizeof(git_status_entry));
- GITERR_CHECK_ALLOC(status_entry);
+ if (!status_entry)
+ return giterr_capture(&data->err, -1);
- status_entry->status = status_compute(status, head2idx, idx2wd);
+ status_entry->status = status_compute(data->status, head2idx, idx2wd);
status_entry->head_to_index = head2idx;
status_entry->index_to_workdir = idx2wd;
- return git_vector_insert(&status->paired, status_entry);
+ return giterr_capture(
+ &data->err, git_vector_insert(&data->status->paired, status_entry));
}
GIT_INLINE(int) status_entry_cmp_base(
@@ -314,9 +321,18 @@ int git_status_list_new(
goto done;
}
- if ((error = git_diff__paired_foreach(
- status->head2idx, status->idx2wd, status_collect, status)) < 0)
- goto done;
+ {
+ struct status_data data = { 0 };
+ data.status = status;
+
+ error = git_diff__paired_foreach(
+ status->head2idx, status->idx2wd, status_collect, &data);
+
+ if (error == GIT_EUSER)
+ error = giterr_restore(&data.err);
+ if (error < 0)
+ goto done;
+ }
if (flags & GIT_STATUS_OPT_SORT_CASE_SENSITIVELY)
git_vector_set_cmp(&status->paired, status_entry_cmp);
diff --git a/src/submodule.c b/src/submodule.c
index 1e3d079..15c87c0 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -71,6 +71,11 @@ __KHASH_IMPL(
str, static kh_inline, const char *, void *, 1,
str_hash_no_trailing_slash, str_equal_no_trailing_slash);
+struct submodule_callback_payload {
+ git_repository *repo;
+ git_error_state error;
+};
+
static int load_submodule_config(git_repository *repo);
static git_config_backend *open_gitmodules(git_repository *, bool, const git_oid *);
static int lookup_head_remote(git_buf *url, git_repository *repo);
@@ -169,8 +174,7 @@ int git_submodule_foreach(
}
if (callback(sm, sm->name, payload)) {
- giterr_clear();
- error = GIT_EUSER;
+ error = giterr_user_cancel();
break;
}
});
@@ -821,20 +825,21 @@ int git_submodule_reload(git_submodule *submodule)
{
int error = 0;
git_config_backend *mods;
+ struct submodule_callback_payload p;
assert(submodule);
/* refresh index data */
-
- if (submodule_update_index(submodule) < 0)
- return -1;
+ if ((error = submodule_update_index(submodule)) < 0)
+ return error;
/* refresh HEAD tree data */
-
- if (submodule_update_head(submodule) < 0)
- return -1;
+ if ((error = submodule_update_head(submodule)) < 0)
+ return error;
/* refresh config data */
+ memset(&p, 0, sizeof(p));
+ p.repo = submodule->repo;
mods = open_gitmodules(submodule->repo, false, NULL);
if (mods != NULL) {
@@ -846,23 +851,29 @@ int git_submodule_reload(git_submodule *submodule)
if (git_buf_oom(&path))
error = -1;
- else
+ else {
error = git_config_file_foreach_match(
- mods, path.ptr, submodule_load_from_config, submodule->repo);
+ mods, path.ptr, submodule_load_from_config, &p);
+
+ if (error == GIT_EUSER)
+ error = giterr_restore(&p.error);
+ }
git_buf_free(&path);
git_config_file_free(mods);
- }
- if (error < 0)
- return error;
+ if (error < 0)
+ return error;
+ }
/* refresh wd data */
submodule->flags = submodule->flags &
~(GIT_SUBMODULE_STATUS_IN_WD | GIT_SUBMODULE_STATUS__WD_OID_VALID);
- error = submodule_load_from_wd_lite(submodule, submodule->path, NULL);
+ error = submodule_load_from_wd_lite(submodule, submodule->path, &p);
+ if (error)
+ error = giterr_restore(&p.error);
return error;
}
@@ -1087,15 +1098,14 @@ int git_submodule_parse_update(git_submodule_update_t *out, const char *value)
}
static int submodule_load_from_config(
- const git_config_entry *entry, void *data)
+ const git_config_entry *entry, void *payload)
{
- git_repository *repo = data;
- git_strmap *smcfg = repo->submodules;
+ struct submodule_callback_payload *p = payload;
+ git_strmap *smcfg = p->repo->submodules;
const char *namestart, *property, *alternate = NULL;
- const char *key = entry->name, *value = entry->value;
+ const char *key = entry->name, *value = entry->value, *path;
git_buf name = GIT_BUF_INIT;
git_submodule *sm;
- bool is_path;
int error = 0;
if (git__prefixcmp(key, "submodule.") != 0)
@@ -1108,15 +1118,11 @@ static int submodule_load_from_config(
return 0;
property++;
- is_path = (strcasecmp(property, "path") == 0);
+ path = !strcasecmp(property, "path") ? value : NULL;
- if (git_buf_set(&name, namestart, property - namestart - 1) < 0)
- return -1;
-
- if (submodule_get(&sm, repo, name.ptr, is_path ? value : NULL) < 0) {
- git_buf_free(&name);
- return -1;
- }
+ if ((error = git_buf_set(&name, namestart, property - namestart - 1)) < 0 ||
+ (error = submodule_get(&sm, p->repo, name.ptr, path)) < 0)
+ goto done;
sm->flags |= GIT_SUBMODULE_STATUS_IN_CONFIG;
@@ -1130,15 +1136,20 @@ static int submodule_load_from_config(
if (strcmp(sm->name, name.ptr) != 0) {
alternate = sm->name = git_buf_detach(&name);
- } else if (is_path && value && strcmp(sm->path, value) != 0) {
+ } else if (path && strcmp(path, sm->path) != 0) {
alternate = sm->path = git__strdup(value);
- if (!sm->path)
+ if (!sm->path) {
error = -1;
+ goto done;
+ }
}
+
if (alternate) {
void *old_sm = NULL;
git_strmap_insert2(smcfg, alternate, sm, old_sm, error);
+ if (error < 0)
+ goto done;
if (error >= 0)
GIT_REFCOUNT_INC(sm); /* inserted under a new key */
@@ -1149,15 +1160,11 @@ static int submodule_load_from_config(
}
}
- git_buf_free(&name);
- if (error < 0)
- return error;
-
/* TODO: Look up path in index and if it is present but not a GITLINK
* then this should be deleted (at least to match git's behavior)
*/
- if (is_path)
+ if (path)
return 0;
/* copy other properties into submodule entry */
@@ -1165,41 +1172,47 @@ static int submodule_load_from_config(
git__free(sm->url);
sm->url = NULL;
- if (value != NULL && (sm->url = git__strdup(value)) == NULL)
- return -1;
+ if (value != NULL && (sm->url = git__strdup(value)) == NULL) {
+ error = -1;
+ goto done;
+ }
}
else if (strcasecmp(property, "update") == 0) {
- if (git_submodule_parse_update(&sm->update, value) < 0)
- return -1;
+ if ((error = git_submodule_parse_update(&sm->update, value)) < 0)
+ goto done;
sm->update_default = sm->update;
}
else if (strcasecmp(property, "fetchRecurseSubmodules") == 0) {
- if (git__parse_bool(&sm->fetch_recurse, value) < 0)
- return submodule_config_error("fetchRecurseSubmodules", value);
+ if (git__parse_bool(&sm->fetch_recurse, value) < 0) {
+ error = submodule_config_error("fetchRecurseSubmodules", value);
+ goto done;
+ }
}
else if (strcasecmp(property, "ignore") == 0) {
- if (git_submodule_parse_ignore(&sm->ignore, value) < 0)
- return -1;
+ if ((error = git_submodule_parse_ignore(&sm->ignore, value)) < 0)
+ goto done;
sm->ignore_default = sm->ignore;
}
/* ignore other unknown submodule properties */
- return 0;
+done:
+ git_buf_free(&name);
+ return giterr_capture(&p->error, error);
}
static int submodule_load_from_wd_lite(
git_submodule *sm, const char *name, void *payload)
{
+ struct submodule_callback_payload *p = payload;
git_buf path = GIT_BUF_INIT;
GIT_UNUSED(name);
- GIT_UNUSED(payload);
if (git_repository_is_bare(sm->repo))
return 0;
if (git_buf_joinpath(&path, git_repository_workdir(sm->repo), sm->path) < 0)
- return -1;
+ return giterr_capture(&p->error, -1);
if (git_path_isdir(path.ptr))
sm->flags |= GIT_SUBMODULE_STATUS__WD_SCANNED;
@@ -1208,7 +1221,6 @@ static int submodule_load_from_wd_lite(
sm->flags |= GIT_SUBMODULE_STATUS_IN_WD;
git_buf_free(&path);
-
return 0;
}
@@ -1342,13 +1354,15 @@ static int load_submodule_config(git_repository *repo)
{
int error;
git_oid gitmodules_oid;
- git_buf path = GIT_BUF_INIT;
git_config_backend *mods = NULL;
+ struct submodule_callback_payload p;
if (repo->submodules)
return 0;
memset(&gitmodules_oid, 0, sizeof(gitmodules_oid));
+ memset(&p, 0, sizeof(p));
+ p.repo = repo;
/* Submodule data is kept in a hashtable keyed by both name and path.
* These are usually the same, but that is not guaranteed.
@@ -1370,23 +1384,23 @@ static int load_submodule_config(git_repository *repo)
/* add submodule information from .gitmodules */
- if ((mods = open_gitmodules(repo, false, &gitmodules_oid)) != NULL)
- error = git_config_file_foreach(mods, submodule_load_from_config, repo);
-
- if (error != 0)
+ if ((mods = open_gitmodules(repo, false, &gitmodules_oid)) != NULL &&
+ (error = git_config_file_foreach(
+ mods, submodule_load_from_config, &p)) < 0)
goto cleanup;
/* shallow scan submodules in work tree */
if (!git_repository_is_bare(repo))
- error = git_submodule_foreach(repo, submodule_load_from_wd_lite, NULL);
+ error = git_submodule_foreach(repo, submodule_load_from_wd_lite, &p);
cleanup:
- git_buf_free(&path);
-
if (mods != NULL)
git_config_file_free(mods);
+ if (error == GIT_EUSER)
+ error = giterr_restore(&p.error);
+
if (error)
git_submodule_config_free(repo);
diff --git a/tests/config/rename.c b/tests/config/rename.c
new file mode 100644
index 0000000..29ade7b
--- /dev/null
+++ b/tests/config/rename.c
@@ -0,0 +1,82 @@
+#include "clar_libgit2.h"
+#include "config.h"
+
+static git_repository *g_repo = NULL;
+static git_config *g_config = NULL;
+
+void test_config_rename__initialize(void)
+{
+ g_repo = cl_git_sandbox_init("testrepo.git");
+ cl_git_pass(git_repository_config(&g_config, g_repo));
+}
+
+void test_config_rename__cleanup(void)
+{
+ git_config_free(g_config);
+ g_config = NULL;
+
+ cl_git_sandbox_cleanup();
+ g_repo = NULL;
+}
+
+void test_config_rename__can_rename(void)
+{
+ const git_config_entry *ce;
+
+ cl_git_pass(git_config_get_entry(
+ &ce, g_config, "branch.track-local.remote"));
+ cl_assert_equal_s(".", ce->value);
+
+ cl_git_fail(git_config_get_entry(
+ &ce, g_config, "branch.local-track.remote"));
+
+ cl_git_pass(git_config_rename_section(
+ g_repo, "branch.track-local", "branch.local-track"));
+
+ cl_git_pass(git_config_get_entry(
+ &ce, g_config, "branch.local-track.remote"));
+ cl_assert_equal_s(".", ce->value);
+
+ cl_git_fail(git_config_get_entry(
+ &ce, g_config, "branch.track-local.remote"));
+}
+
+void test_config_rename__prevent_overwrite(void)
+{
+ const git_config_entry *ce;
+ const git_error *err;
+
+ cl_git_pass(git_config_set_string(
+ g_config, "branch.local-track.remote", "yellow"));
+
+ cl_git_pass(git_config_get_entry(
+ &ce, g_config, "branch.local-track.remote"));
+ cl_assert_equal_s("yellow", ce->value);
+
+ cl_git_pass(git_config_rename_section(
+ g_repo, "branch.track-local", "branch.local-track"));
+
+ cl_git_pass(git_config_get_entry(
+ &ce, g_config, "branch.local-track.remote"));
+ cl_assert_equal_s(".", ce->value);
+
+// cl_assert((err = giterr_last()) != NULL);
+// cl_assert(err->message != NULL);
+}
+
+static void assert_invalid_config_section_name(
+ git_repository *repo, const char *name)
+{
+ cl_git_fail_with(
+ git_config_rename_section(repo, "branch.remoteless", name),
+ GIT_EINVALIDSPEC);
+}
+
+void test_config_rename__require_a_valid_new_name(void)
+{
+ assert_invalid_config_section_name(g_repo, "");
+ assert_invalid_config_section_name(g_repo, "bra\nch");
+ assert_invalid_config_section_name(g_repo, "branc#");
+ assert_invalid_config_section_name(g_repo, "bra\nch.duh");
+ assert_invalid_config_section_name(g_repo, "branc#.duh");
+}
diff --git a/tests/config/validkeyname.c b/tests/config/validkeyname.c
index 3369973..0ef4a9a 100644
--- a/tests/config/validkeyname.c
+++ b/tests/config/validkeyname.c
@@ -46,23 +46,3 @@ void test_config_validkeyname__accessing_requires_a_valid_name(void)
assert_invalid_config_key_name("dif.dir\nstat.lines");
assert_invalid_config_key_name("dif.dirstat.li\nes");
}
-
-static void assert_invalid_config_section_name(git_repository *repo, const char *name)
-{
- cl_git_fail_with(git_config_rename_section(repo, "branch.remoteless", name), GIT_EINVALIDSPEC);
-}
-
-void test_config_validkeyname__renaming_a_section_requires_a_valid_name(void)
-{
- git_repository *repo;
-
- cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
-
- assert_invalid_config_section_name(repo, "");
- assert_invalid_config_section_name(repo, "bra\nch");
- assert_invalid_config_section_name(repo, "branc#");
- assert_invalid_config_section_name(repo, "bra\nch.duh");
- assert_invalid_config_section_name(repo, "branc#.duh");
-
- git_repository_free(repo);
-}
diff --git a/tests/config/write.c b/tests/config/write.c
index 15f750d..922d755 100644
--- a/tests/config/write.c
+++ b/tests/config/write.c
@@ -303,3 +303,4 @@ void test_config_write__updating_a_locked_config_file_returns_ELOCKED(void)
git_config_free(cfg);
}
+
diff --git a/tests/refs/branches/move.c b/tests/refs/branches/move.c
index ecf14e0..9d233de 100644
--- a/tests/refs/branches/move.c
+++ b/tests/refs/branches/move.c
@@ -66,12 +66,54 @@ void test_refs_branches_move__can_move_a_local_branch_to_a_partially_colliding_n
void test_refs_branches_move__can_not_move_a_branch_if_its_destination_name_collide_with_an_existing_one(void)
{
git_reference *original_ref, *new_ref;
+ git_config *config;
+ const git_config_entry *ce;
+ char *original_remote, *original_merge;
+
+ cl_git_pass(git_repository_config(&config, repo));
+
+ cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
+ original_remote = strdup(ce->value);
+ cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
+ original_merge = strdup(ce->value);
+
cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/br2"));
- cl_assert_equal_i(GIT_EEXISTS, git_branch_move(&new_ref, original_ref, "master", 0));
+ cl_assert_equal_i(GIT_EEXISTS,
+ git_branch_move(&new_ref, original_ref, "master", 0));
+
+ cl_assert(giterr_last()->message != NULL);
+ cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
+ cl_assert_equal_s(original_remote, ce->value);
+ cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
+ cl_assert_equal_s(original_merge, ce->value);
+
+
+ cl_assert_equal_i(GIT_EEXISTS,
+ git_branch_move(&new_ref, original_ref, "cannot-fetch", 0));
+
+ cl_assert(giterr_last()->message != NULL);
+ cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
+ cl_assert_equal_s(original_remote, ce->value);
+ cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
+ cl_assert_equal_s(original_merge, ce->value);
+
+ git_reference_free(original_ref);
+ cl_git_pass(git_reference_lookup(&original_ref, repo, "refs/heads/track-local"));
+
+ cl_assert_equal_i(GIT_EEXISTS,
+ git_branch_move(&new_ref, original_ref, "master", 0));
+
+ cl_assert(giterr_last()->message != NULL);
+ cl_git_pass(git_config_get_entry(&ce, config, "branch.master.remote"));
+ cl_assert_equal_s(original_remote, ce->value);
+ cl_git_pass(git_config_get_entry(&ce, config, "branch.master.merge"));
+ cl_assert_equal_s(original_merge, ce->value);
+ free(original_remote); free(original_merge);
git_reference_free(original_ref);
+ git_config_free(config);
}
void test_refs_branches_move__moving_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)