add got.conf(5) configuration file ok tracey
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
diff --git a/got/got.1 b/got/got.1
index 19122e3..fc2c196 100644
--- a/got/got.1
+++ b/got/got.1
@@ -88,7 +88,9 @@ The
command requires the
.Ev GOT_AUTHOR
environment variable to be set,
-unless Git's
+unless an author has been configured in
+.Xr got.conf 5
+or Git's
.Dv user.name
and
.Dv user.email
@@ -186,8 +188,10 @@ More details about the pack file format are documented in
.Pp
.Cm got clone
creates a remote repository entry in the
+.Xr got.conf 5
+and
.Pa config
-file of the cloned repository to store the
+files of the cloned repository to store the
.Ar repository-url
for future use by
.Cm got fetch
@@ -234,8 +238,10 @@ This is useful if the cloned repository will not be used to store
locally created commits.
.Pp
The repository's
+.Xr got.conf 5
+and
.Pa config
-file will be set up with the
+files will be set up with the
.Dq mirror
option enabled, such that
.Cm got fetch
@@ -310,6 +316,8 @@ is specified,
.Dq origin
will be used.
The remote repository's URL is obtained from the corresponding entry in the
+.Xr got.conf 5
+or
.Pa config
file of the local repository, as created by
.Cm got clone .
@@ -1228,7 +1236,9 @@ The
command requires the
.Ev GOT_AUTHOR
environment variable to be set,
-unless Git's
+unless an author has been configured in
+.Xr got.conf 5
+or Git's
.Dv user.name
and
.Dv user.email
@@ -1920,25 +1930,29 @@ attempts to reject
.Ev GOT_AUTHOR
environment variables with a missing email address.
.Pp
-If present, Git's
+If present,
+configuration settings in
+.Xr got.conf 5 ,
+or Git's
.Dv user.name
and
.Dv user.email
configuration settings in the repository's
.Pa .git/config
-file will override the value of
+file,
+will override the value of
.Ev GOT_AUTHOR .
-However, the
+The
.Dv user.name
and
.Dv user.email
configuration settings contained in Git's global
.Pa ~/.gitconfig
-configuration file will be used only if the
+configuration file will only be used if neither
+.Xr got.conf 5
+nor the
.Ev GOT_AUTHOR
-environment variable is
-.Em not
-set.
+environment variable provide author information.
.It Ev VISUAL , EDITOR
The editor spawned by
.Cm got commit ,
@@ -1951,6 +1965,16 @@ The default limit on the number of commits traversed by
If set to zero, the limit is unbounded.
This variable will be silently ignored if it is set to a non-numeric value.
.El
+.Sh FILES
+.Bl -tag -width packed-refs -compact
+.It Pa got.conf
+Repository-wide configuration settings for
+.Nm .
+If present, this configuration file is located in the root directory
+of a Git repository and supersedes any relevant settings in Git's
+.Pa config
+file.
+.El
.Sh EXIT STATUS
.Ex -std got
.Sh EXAMPLES
@@ -2257,7 +2281,8 @@ repository with
.Sh SEE ALSO
.Xr tog 1 ,
.Xr git-repository 5 ,
-.Xr got-worktree 5
+.Xr got-worktree 5 ,
+.Xr got.conf 5
.Sh AUTHORS
.An Stefan Sperling Aq Mt stsp@openbsd.org
.An Martin Pieuchot Aq Mt mpi@openbsd.org
diff --git a/got/got.c b/got/got.c
index 2b4d757..45468e1 100644
--- a/got/got.c
+++ b/got/got.c
@@ -523,25 +523,30 @@ get_author(char **author, struct got_repository *repo)
*author = NULL;
- name = got_repo_get_gitconfig_author_name(repo);
- email = got_repo_get_gitconfig_author_email(repo);
- if (name && email) {
- if (asprintf(author, "%s <%s>", name, email) == -1)
- return got_error_from_errno("asprintf");
- return NULL;
- }
-
- got_author = getenv("GOT_AUTHOR");
+ got_author = got_repo_get_gotconfig_author(repo);
if (got_author == NULL) {
- name = got_repo_get_global_gitconfig_author_name(repo);
- email = got_repo_get_global_gitconfig_author_email(repo);
+ name = got_repo_get_gitconfig_author_name(repo);
+ email = got_repo_get_gitconfig_author_email(repo);
if (name && email) {
if (asprintf(author, "%s <%s>", name, email) == -1)
return got_error_from_errno("asprintf");
return NULL;
}
- /* TODO: Look up user in password database? */
- return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
+
+ got_author = getenv("GOT_AUTHOR");
+ if (got_author == NULL) {
+ name = got_repo_get_global_gitconfig_author_name(repo);
+ email = got_repo_get_global_gitconfig_author_email(
+ repo);
+ if (name && email) {
+ if (asprintf(author, "%s <%s>", name, email)
+ == -1)
+ return got_error_from_errno("asprintf");
+ return NULL;
+ }
+ /* TODO: Look up user in password database? */
+ return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
+ }
}
*author = strdup(got_author);
@@ -1037,9 +1042,9 @@ cmd_clone(int argc, char *argv[])
pid_t fetchpid = -1;
struct got_fetch_progress_arg fpa;
char *git_url = NULL;
- char *gitconfig_path = NULL;
- char *gitconfig = NULL;
- FILE *gitconfig_file = NULL;
+ char *gitconfig_path = NULL, *gotconfig_path = NULL;
+ char *gitconfig = NULL, *gotconfig = NULL;
+ FILE *gitconfig_file = NULL, *gotconfig_file = NULL;
ssize_t n;
int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
int list_refs_only = 0;
@@ -1340,7 +1345,40 @@ cmd_clone(int argc, char *argv[])
}
}
- /* Create a config file git-fetch(1) can understand. */
+ /* Create got.conf(5). */
+ gotconfig_path = got_repo_get_path_gotconfig(repo);
+ if (gotconfig_path == NULL) {
+ error = got_error_from_errno("got_repo_get_path_gotconfig");
+ goto done;
+ }
+ gotconfig_file = fopen(gotconfig_path, "a");
+ if (gotconfig_file == NULL) {
+ error = got_error_from_errno2("fopen", gotconfig_path);
+ goto done;
+ }
+ got_path_strip_trailing_slashes(server_path);
+ if (asprintf(&gotconfig,
+ "remote \"%s\" {\n"
+ "\tserver %s\n"
+ "\tprotocol %s\n"
+ "%s%s%s"
+ "\trepository \"%s\"\n"
+ "%s"
+ "}\n",
+ GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
+ port ? "\tport " : "", port ? port : "", port ? "\n" : "",
+ server_path,
+ mirror_references ? "\tmirror-references yes\n" : "") == -1) {
+ error = got_error_from_errno("asprintf");
+ goto done;
+ }
+ n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
+ if (n != strlen(gotconfig)) {
+ error = got_ferror(gotconfig_file, GOT_ERR_IO);
+ goto done;
+ }
+
+ /* Create a config file Git can understand. */
gitconfig_path = got_repo_get_path_gitconfig(repo);
if (gitconfig_path == NULL) {
error = got_error_from_errno("got_repo_get_path_gitconfig");
@@ -1413,6 +1451,8 @@ done:
}
if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
error = got_error_from_errno("close");
+ if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
+ error = got_error_from_errno("fclose");
if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
error = got_error_from_errno("fclose");
if (repo)
@@ -1438,6 +1478,9 @@ done:
free(server_path);
free(repo_name);
free(default_destdir);
+ free(gotconfig);
+ free(gitconfig);
+ free(gotconfig_path);
free(gitconfig_path);
free(git_url);
return error;
@@ -1858,15 +1901,23 @@ cmd_fetch(int argc, char *argv[])
if (error)
goto done;
- got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
+ got_repo_get_gotconfig_remotes(&nremotes, &remotes, repo);
for (i = 0; i < nremotes; i++) {
remote = &remotes[i];
if (strcmp(remote->name, remote_name) == 0)
break;
}
if (i == nremotes) {
- error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
- goto done;
+ got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
+ for (i = 0; i < nremotes; i++) {
+ remote = &remotes[i];
+ if (strcmp(remote->name, remote_name) == 0)
+ break;
+ }
+ if (i == nremotes) {
+ error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
+ goto done;
+ }
}
error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
diff --git a/got/got.conf.5 b/got/got.conf.5
new file mode 100644
index 0000000..d6d11e4
--- /dev/null
+++ b/got/got.conf.5
@@ -0,0 +1,156 @@
+.\"
+.\" Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd $Mdocdate$
+.Dt GOT.CONF 5
+.Os
+.Sh NAME
+.Nm got.conf
+.Nd Game of Trees configuration file
+.Sh DESCRIPTION
+.Nm
+is the run-time configuration file for
+.Xr got 1 .
+.Pp
+The file format is line-based, with one configuration directive per line.
+Any lines beginning with a
+.Sq #
+are treated as comments and ignored.
+.Pp
+The available configuration directives are as follows:
+.Bl -tag -width Ds
+.It Ic author Dq Real Name <email address>
+Configure the author's name and email address for
+.Cm got commit
+and
+.Cm got import
+when operating on this repository.
+Author information specified here overrides the
+.Ev GOT_AUTHOR
+environment variable.
+.Pp
+Because
+.Xr git 1
+may fail to parse commits without an email address in author data,
+.Xr got 1
+attempts to reject author information with a missing email address.
+.It Ic remote Ar name Brq ...
+Define a remote repository.
+The specified
+.Ar name
+can be used to refer to the remote repository on the command line of
+.Cm got fetch .
+.Pp
+Information about this repository is declared in a block of options
+enclosed in curly brackets:
+.Bl -tag -width Ds
+.It Ic server Ar hostname
+Defines the hostname to use for contacting the remote repository's server.
+.It Ic repository Ar path
+Defines the path to the repository on the remote repository's server.
+.It Ic protocol Ar scheme
+Defines the protocol to use for communicating with the remote repository's
+server.
+.Pp
+The following protocol schemes are supported:
+.Bl -tag -width git+ssh
+.It git
+The Git protocol as implemented by the
+.Xr git-daemon 1
+server.
+Use of this protocol is discouraged since it supports neither authentication
+nor encryption.
+.It git+ssh
+The Git protocol wrapped in an authenticated and encrypted
+.Xr ssh 1
+tunnel.
+With this protocol the hostname may contain an embedded username for
+.Xr ssh 1
+to use:
+.Mt user@hostname
+.It ssh
+Short alias for git+ssh.
+.El
+.It Ic port Ar port
+Defines the port to use for connecting to the remote repository's server.
+The
+.Ar port
+can be specified by number or name.
+The port name to number mappings are found in the file
+.Pa /etc/services ;
+see
+.Xr services 5
+for details.
+If not specified, the default port of the specified
+.Cm protocol
+will be used.
+.It Ic mirror-references Ar yes | no
+This option controls the behaviour of
+.Cm got fetch
+when updating references.
+.Sy Enabling this option can lead to the loss of local commits.
+Maintaining custom changes in a mirror repository is therefore discouraged.
+.Pp
+If this option is not specified or set to
+.Ar no ,
+.Cm got fetch
+will map references of the remote repository into the local repository's
+.Dq refs/remotes/
+namespace.
+.Pp
+If this option is set to
+.Ar yes ,
+all branches in the
+.Dq refs/heads/
+namespace will be updated directly to match the corresponding branches in
+the remote repository.
+.El
+.Sh EXAMPLES
+Configure author information:
+.Bd -literal -offset indent
+author "Flan Hacker <flan_hacker@openbsd.org>"
+.Ed
+.Pp
+Remote repository specification for the Game of Trees repository:
+.Bd -literal -offset indent
+remote "origin" {
+ server git.gameoftrees.org
+ protocol git
+ repository got
+}
+.Ed
+.Pp
+Mirror the OpenBSD src repository from Github:
+.Bd -literal -offset indent
+remote "origin" {
+ repository "openbsd/src"
+ server git@github.com
+ protocol git+ssh
+ mirror-references yes
+}
+.Ed
+.Sh FILES
+.Bl -tag -width Ds -compact
+.It Pa got.conf
+If present, the
+.Nm
+configuration file is located in the root directory of a Git repository
+and supersedes any relevant settings in Git's
+.Pa config
+file.
+.El
+.Sh SEE ALSO
+.Xr got 1 ,
+.Xr git-repository 5
diff --git a/gotweb/parse.y b/gotweb/parse.y
index 93dc3d7..93c6e03 100644
--- a/gotweb/parse.y
+++ b/gotweb/parse.y
@@ -234,7 +234,7 @@ yyerror(const char *fmt, ...)
gerror = got_error_from_errno("asprintf");
return(0);
}
- gerror = got_error_msg(GOT_ERR_PARSE_Y_YY, strdup(err));
+ gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
free(msg);
free(err);
return(0);
diff --git a/include/got_error.h b/include/got_error.h
index b3805d3..0f3e07a 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -141,7 +141,7 @@
#define GOT_ERR_FETCH_NO_BRANCH 124
#define GOT_ERR_FETCH_BAD_REF 125
#define GOT_ERR_TREE_ENTRY_TYPE 126
-#define GOT_ERR_PARSE_Y_YY 127
+#define GOT_ERR_PARSE_CONFIG 127
#define GOT_ERR_NO_CONFIG_FILE 128
#define GOT_ERR_BAD_SYMLINK 129
@@ -291,7 +291,7 @@ static const struct got_error {
{ GOT_ERR_FETCH_NO_BRANCH, "could not find any branches to fetch" },
{ GOT_ERR_FETCH_BAD_REF, "reference cannot be fetched" },
{ GOT_ERR_TREE_ENTRY_TYPE, "unexpected tree entry type" },
- { GOT_ERR_PARSE_Y_YY, "yyerror error" },
+ { GOT_ERR_PARSE_CONFIG, "configuration file syntax error" },
{ GOT_ERR_NO_CONFIG_FILE, "configuration file doesn't exit" },
{ GOT_ERR_BAD_SYMLINK, "symbolic link points outside of paths under "
"version control" },
diff --git a/include/got_repository.h b/include/got_repository.h
index 1e0ca3b..bc9bddc 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -59,10 +59,17 @@ struct got_remote_repo {
int mirror_references;
};
+/* Obtain the commit author if parsed from got.conf, else NULL. */
+const char *got_repo_get_gotconfig_author(struct got_repository *);
+
/* Obtain the list of remote repositories parsed from gitconfig. */
void got_repo_get_gitconfig_remotes(int *, struct got_remote_repo **,
struct got_repository *);
+/* Obtain the list of remote repositories parsed from got.conf. */
+void got_repo_get_gotconfig_remotes(int *, struct got_remote_repo **,
+ struct got_repository *);
+
/*
* Obtain paths to various directories within a repository.
* The caller must dispose of a path with free(3).
@@ -72,6 +79,7 @@ char *got_repo_get_path_objects_pack(struct got_repository *);
char *got_repo_get_path_refs(struct got_repository *);
char *got_repo_get_path_packed_refs(struct got_repository *);
char *got_repo_get_path_gitconfig(struct got_repository *);
+char *got_repo_get_path_gotconfig(struct got_repository *);
struct got_reference;
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 950723f..51acfb2 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -43,6 +43,7 @@
#define GOT_PROG_READ_TAG got-read-tag
#define GOT_PROG_READ_PACK got-read-pack
#define GOT_PROG_READ_GITCONFIG got-read-gitconfig
+#define GOT_PROG_READ_GOTCONFIG got-read-gotconfig
#define GOT_PROG_FETCH_PACK got-fetch-pack
#define GOT_PROG_INDEX_PACK got-index-pack
#define GOT_PROG_SEND_PACK got-send-pack
@@ -65,6 +66,8 @@
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_PACK)
#define GOT_PATH_PROG_READ_GITCONFIG \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_GITCONFIG)
+#define GOT_PATH_PROG_READ_GOTCONFIG \
+ GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_GOTCONFIG)
#define GOT_PATH_PROG_FETCH_PACK \
GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_FETCH_PACK)
#define GOT_PATH_PROG_SEND_PACK \
@@ -147,6 +150,15 @@ enum got_imsg_type {
GOT_IMSG_GITCONFIG_REMOTE,
GOT_IMSG_GITCONFIG_OWNER_REQUEST,
GOT_IMSG_GITCONFIG_OWNER,
+
+ /* Messages related to gotconfig files. */
+ GOT_IMSG_GOTCONFIG_PARSE_REQUEST,
+ GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST,
+ GOT_IMSG_GOTCONFIG_REMOTES_REQUEST,
+ GOT_IMSG_GOTCONFIG_INT_VAL,
+ GOT_IMSG_GOTCONFIG_STR_VAL,
+ GOT_IMSG_GOTCONFIG_REMOTES,
+ GOT_IMSG_GOTCONFIG_REMOTE,
};
/* Structure for GOT_IMSG_ERROR. */
@@ -459,6 +471,16 @@ const struct got_error *got_privsep_recv_gitconfig_int(int *, struct imsgbuf *);
const struct got_error *got_privsep_recv_gitconfig_remotes(
struct got_remote_repo **, int *, struct imsgbuf *);
+const struct got_error *got_privsep_send_gotconfig_parse_req(struct imsgbuf *,
+ int);
+const struct got_error *got_privsep_send_gotconfig_author_req(struct imsgbuf *);
+const struct got_error *got_privsep_send_gotconfig_remotes_req(
+ struct imsgbuf *);
+const struct got_error *got_privsep_recv_gotconfig_str(char **,
+ struct imsgbuf *);
+const struct got_error *got_privsep_recv_gotconfig_remotes(
+ struct got_remote_repo **, int *, struct imsgbuf *);
+
const struct got_error *got_privsep_send_commit_traversal_request(
struct imsgbuf *, struct got_object_id *, int, const char *);
const struct got_error *got_privsep_recv_traversed_commits(
diff --git a/lib/got_lib_repository.h b/lib/got_lib_repository.h
index df440da..53390ef 100644
--- a/lib/got_lib_repository.h
+++ b/lib/got_lib_repository.h
@@ -21,6 +21,7 @@
#define GOT_REFS_DIR "refs"
#define GOT_HEAD_FILE "HEAD"
#define GOT_GITCONFIG "config"
+#define GOT_GOTCONFIG "got.conf"
/* Other files and directories inside the git directory. */
#define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
@@ -64,6 +65,11 @@ struct got_repository {
int ngitconfig_remotes;
struct got_remote_repo *gitconfig_remotes;
char *gitconfig_owner;
+
+ /* Settings read from got.conf. */
+ char *gotconfig_author;
+ int ngotconfig_remotes;
+ struct got_remote_repo *gotconfig_remotes;
};
const struct got_error*got_repo_cache_object(struct got_repository *,
diff --git a/lib/privsep.c b/lib/privsep.c
index 5c21a19..63a8b0d 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -1888,6 +1888,208 @@ got_privsep_recv_gitconfig_remotes(struct got_remote_repo **remotes,
}
const struct got_error *
+got_privsep_send_gotconfig_parse_req(struct imsgbuf *ibuf, int fd)
+{
+ const struct got_error *err = NULL;
+
+ if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_PARSE_REQUEST, 0, 0, fd,
+ NULL, 0) == -1) {
+ err = got_error_from_errno("imsg_compose "
+ "GOTCONFIG_PARSE_REQUEST");
+ close(fd);
+ return err;
+ }
+
+ return flush_imsg(ibuf);
+}
+
+const struct got_error *
+got_privsep_send_gotconfig_author_req(struct imsgbuf *ibuf)
+{
+ if (imsg_compose(ibuf,
+ GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST, 0, 0, -1, NULL, 0) == -1)
+ return got_error_from_errno("imsg_compose "
+ "GOTCONFIG_AUTHOR_REQUEST");
+
+ return flush_imsg(ibuf);
+}
+
+const struct got_error *
+got_privsep_send_gotconfig_remotes_req(struct imsgbuf *ibuf)
+{
+ if (imsg_compose(ibuf,
+ GOT_IMSG_GOTCONFIG_REMOTES_REQUEST, 0, 0, -1, NULL, 0) == -1)
+ return got_error_from_errno("imsg_compose "
+ "GOTCONFIG_REMOTE_REQUEST");
+
+ return flush_imsg(ibuf);
+}
+
+const struct got_error *
+got_privsep_recv_gotconfig_str(char **str, struct imsgbuf *ibuf)
+{
+ const struct got_error *err = NULL;
+ struct imsg imsg;
+ size_t datalen;
+ const size_t min_datalen = 0;
+
+ *str = NULL;
+
+ err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
+ if (err)
+ return err;
+ datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+
+ switch (imsg.hdr.type) {
+ case GOT_IMSG_ERROR:
+ if (datalen < sizeof(struct got_imsg_error)) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ err = recv_imsg_error(&imsg, datalen);
+ break;
+ case GOT_IMSG_GOTCONFIG_STR_VAL:
+ if (datalen == 0)
+ break;
+ *str = malloc(datalen);
+ if (*str == NULL) {
+ err = got_error_from_errno("malloc");
+ break;
+ }
+ if (strlcpy(*str, imsg.data, datalen) >= datalen)
+ err = got_error(GOT_ERR_NO_SPACE);
+ break;
+ default:
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ break;
+ }
+
+ imsg_free(&imsg);
+ return err;
+}
+
+const struct got_error *
+got_privsep_recv_gotconfig_remotes(struct got_remote_repo **remotes,
+ int *nremotes, struct imsgbuf *ibuf)
+{
+ const struct got_error *err = NULL;
+ struct imsg imsg;
+ size_t datalen;
+ struct got_imsg_remotes iremotes;
+ struct got_imsg_remote iremote;
+ const size_t min_datalen =
+ MIN(sizeof(struct got_imsg_error), sizeof(iremotes));
+
+ *remotes = NULL;
+ *nremotes = 0;
+ iremotes.nremotes = 0;
+
+ err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
+ if (err)
+ return err;
+ datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+
+ switch (imsg.hdr.type) {
+ case GOT_IMSG_ERROR:
+ if (datalen < sizeof(struct got_imsg_error)) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ err = recv_imsg_error(&imsg, datalen);
+ break;
+ case GOT_IMSG_GOTCONFIG_REMOTES:
+ if (datalen != sizeof(iremotes)) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ memcpy(&iremotes, imsg.data, sizeof(iremotes));
+ if (iremotes.nremotes == 0) {
+ imsg_free(&imsg);
+ return NULL;
+ }
+ break;
+ default:
+ imsg_free(&imsg);
+ return got_error(GOT_ERR_PRIVSEP_MSG);
+ }
+
+ imsg_free(&imsg);
+
+ *remotes = recallocarray(NULL, 0, iremotes.nremotes, sizeof(**remotes));
+ if (*remotes == NULL)
+ return got_error_from_errno("recallocarray");
+
+ while (*nremotes < iremotes.nremotes) {
+ struct got_remote_repo *remote;
+ const size_t min_datalen =
+ MIN(sizeof(struct got_imsg_error), sizeof(iremote));
+
+ err = got_privsep_recv_imsg(&imsg, ibuf, min_datalen);
+ if (err)
+ break;
+ datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+
+ switch (imsg.hdr.type) {
+ case GOT_IMSG_ERROR:
+ if (datalen < sizeof(struct got_imsg_error)) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ err = recv_imsg_error(&imsg, datalen);
+ break;
+ case GOT_IMSG_GOTCONFIG_REMOTE:
+ remote = &(*remotes)[*nremotes];
+ if (datalen < sizeof(iremote)) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ memcpy(&iremote, imsg.data, sizeof(iremote));
+ if (iremote.name_len == 0 || iremote.url_len == 0 ||
+ (sizeof(iremote) + iremote.name_len +
+ iremote.url_len) > datalen) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ remote->name = strndup(imsg.data + sizeof(iremote),
+ iremote.name_len);
+ if (remote->name == NULL) {
+ err = got_error_from_errno("strndup");
+ break;
+ }
+ remote->url = strndup(imsg.data + sizeof(iremote) +
+ iremote.name_len, iremote.url_len);
+ if (remote->url == NULL) {
+ err = got_error_from_errno("strndup");
+ free(remote->name);
+ break;
+ }
+ remote->mirror_references = iremote.mirror_references;
+ (*nremotes)++;
+ break;
+ default:
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ break;
+ }
+
+ imsg_free(&imsg);
+ if (err)
+ break;
+ }
+
+ if (err) {
+ int i;
+ for (i = 0; i < *nremotes; i++) {
+ free((*remotes)[i].name);
+ free((*remotes)[i].url);
+ }
+ free(*remotes);
+ *remotes = NULL;
+ *nremotes = 0;
+ }
+ return err;
+}
+
+const struct got_error *
got_privsep_send_commit_traversal_request(struct imsgbuf *ibuf,
struct got_object_id *id, int idx, const char *path)
{
@@ -2009,6 +2211,7 @@ got_privsep_unveil_exec_helpers(void)
GOT_PATH_PROG_READ_BLOB,
GOT_PATH_PROG_READ_TAG,
GOT_PATH_PROG_READ_GITCONFIG,
+ GOT_PATH_PROG_READ_GOTCONFIG,
GOT_PATH_PROG_FETCH_PACK,
GOT_PATH_PROG_INDEX_PACK,
};
diff --git a/lib/repository.c b/lib/repository.c
index c2ee66c..b120196 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -161,6 +161,12 @@ got_repo_get_path_gitconfig(struct got_repository *repo)
return get_path_git_child(repo, GOT_GITCONFIG);
}
+char *
+got_repo_get_path_gotconfig(struct got_repository *repo)
+{
+ return get_path_git_child(repo, GOT_GOTCONFIG);
+}
+
void
got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
struct got_repository *repo)
@@ -169,6 +175,20 @@ got_repo_get_gitconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
*remotes = repo->gitconfig_remotes;
}
+const char *
+got_repo_get_gotconfig_author(struct got_repository *repo)
+{
+ return repo->gotconfig_author;
+}
+
+void
+got_repo_get_gotconfig_remotes(int *nremotes, struct got_remote_repo **remotes,
+ struct got_repository *repo)
+{
+ *nremotes = repo->ngotconfig_remotes;
+ *remotes = repo->gotconfig_remotes;
+}
+
static int
is_git_repo(struct got_repository *repo)
{
@@ -518,6 +538,124 @@ done:
return err;
}
+static const struct got_error *
+parse_gotconfig_file(char **author,
+ struct got_remote_repo **remotes, int *nremotes,
+ const char *gotconfig_path)
+{
+ const struct got_error *err = NULL, *child_err = NULL;
+ int fd = -1;
+ int imsg_fds[2] = { -1, -1 };
+ pid_t pid;
+ struct imsgbuf *ibuf;
+
+ if (author)
+ *author = NULL;
+ if (remotes)
+ *remotes = NULL;
+ if (nremotes)
+ *nremotes = 0;
+
+ fd = open(gotconfig_path, O_RDONLY);
+ if (fd == -1) {
+ if (errno == ENOENT)
+ return NULL;
+ return got_error_from_errno2("open", gotconfig_path);
+ }
+
+ ibuf = calloc(1, sizeof(*ibuf));
+ if (ibuf == NULL) {
+ err = got_error_from_errno("calloc");
+ goto done;
+ }
+
+ if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
+ err = got_error_from_errno("socketpair");
+ goto done;
+ }
+
+ pid = fork();
+ if (pid == -1) {
+ err = got_error_from_errno("fork");
+ goto done;
+ } else if (pid == 0) {
+ got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
+ gotconfig_path);
+ /* not reached */
+ }
+
+ if (close(imsg_fds[1]) == -1) {
+ err = got_error_from_errno("close");
+ goto done;
+ }
+ imsg_fds[1] = -1;
+ imsg_init(ibuf, imsg_fds[0]);
+
+ err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
+ if (err)
+ goto done;
+ fd = -1;
+
+ if (author) {
+ err = got_privsep_send_gotconfig_author_req(ibuf);
+ if (err)
+ goto done;
+
+ err = got_privsep_recv_gotconfig_str(author, ibuf);
+ if (err)
+ goto done;
+ }
+
+ if (remotes && nremotes) {
+ err = got_privsep_send_gotconfig_remotes_req(ibuf);
+ if (err)
+ goto done;
+
+ err = got_privsep_recv_gotconfig_remotes(remotes,
+ nremotes, ibuf);
+ if (err)
+ goto done;
+ }
+
+ imsg_clear(ibuf);
+ err = got_privsep_send_stop(imsg_fds[0]);
+ child_err = got_privsep_wait_for_child(pid);
+ if (child_err && err == NULL)
+ err = child_err;
+done:
+ if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
+ err = got_error_from_errno("close");
+ if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
+ err = got_error_from_errno("close");
+ if (fd != -1 && close(fd) == -1 && err == NULL)
+ err = got_error_from_errno2("close", gotconfig_path);
+ if (err) {
+ if (author) {
+ free(*author);
+ *author = NULL;
+ }
+ }
+ free(ibuf);
+ return err;
+}
+
+static const struct got_error *
+read_gotconfig(struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ char *gotconfig_path;
+
+ gotconfig_path = got_repo_get_path_gotconfig(repo);
+ if (gotconfig_path == NULL)
+ return got_error_from_errno("got_repo_get_path_gotconfig");
+
+ err = parse_gotconfig_file(&repo->gotconfig_author,
+ &repo->gotconfig_remotes, &repo->ngotconfig_remotes,
+ gotconfig_path);
+ free(gotconfig_path);
+ return err;
+}
+
const struct got_error *
got_repo_open(struct got_repository **repop, const char *path,
const char *global_gitconfig_path)
@@ -591,6 +729,10 @@ got_repo_open(struct got_repository **repop, const char *path,
}
} while (path);
+ err = read_gotconfig(repo);
+ if (err)
+ goto done;
+
err = read_gitconfig(repo, global_gitconfig_path);
if (err)
goto done;
@@ -646,6 +788,12 @@ got_repo_close(struct got_repository *repo)
err = got_error_from_errno("close");
}
+ free(repo->gotconfig_author);
+ for (i = 0; i < repo->ngotconfig_remotes; i++) {
+ free(repo->gotconfig_remotes[i].name);
+ free(repo->gotconfig_remotes[i].url);
+ }
+ free(repo->gotconfig_remotes);
free(repo->gitconfig_author_name);
free(repo->gitconfig_author_email);
for (i = 0; i < repo->ngitconfig_remotes; i++) {
diff --git a/libexec/Makefile b/libexec/Makefile
index 41c5a86..1e55c98 100644
--- a/libexec/Makefile
+++ b/libexec/Makefile
@@ -1,5 +1,5 @@
SUBDIR = got-read-blob got-read-commit got-read-object got-read-tree \
got-read-tag got-fetch-pack got-index-pack got-read-pack \
- got-read-gitconfig
+ got-read-gitconfig got-read-gotconfig
.include <bsd.subdir.mk>
diff --git a/libexec/got-read-gotconfig/Makefile b/libexec/got-read-gotconfig/Makefile
new file mode 100644
index 0000000..a683cf2
--- /dev/null
+++ b/libexec/got-read-gotconfig/Makefile
@@ -0,0 +1,13 @@
+.PATH:${.CURDIR}/../../lib
+
+.include "../../got-version.mk"
+
+PROG= got-read-gotconfig
+SRCS= got-read-gotconfig.c error.c inflate.c object_parse.c \
+ path.c privsep.c sha1.c parse.y
+
+CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib -I${.CURDIR}
+LDADD = -lutil -lz
+DPADD = ${LIBZ} ${LIBUTIL}
+
+.include <bsd.prog.mk>
diff --git a/libexec/got-read-gotconfig/got-read-gotconfig.c b/libexec/got-read-gotconfig/got-read-gotconfig.c
new file mode 100644
index 0000000..e3bcc20
--- /dev/null
+++ b/libexec/got-read-gotconfig/got-read-gotconfig.c
@@ -0,0 +1,358 @@
+/*
+ * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/uio.h>
+#include <sys/time.h>
+#include <sys/syslimits.h>
+
+#include <stdint.h>
+#include <imsg.h>
+#include <limits.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sha1.h>
+#include <zlib.h>
+
+#include "got_error.h"
+#include "got_object.h"
+#include "got_repository.h"
+
+#include "got_lib_delta.h"
+#include "got_lib_object.h"
+#include "got_lib_privsep.h"
+
+#include "gotconfig.h"
+
+/* parse.y */
+static volatile sig_atomic_t sigint_received;
+
+static void
+catch_sigint(int signo)
+{
+ sigint_received = 1;
+}
+
+static const struct got_error *
+make_repo_url(char **url, struct gotconfig_remote_repo *repo)
+{
+ const struct got_error *err = NULL;
+ char *s = NULL, *p = NULL;
+
+ *url = NULL;
+
+ if (asprintf(&s, "%s://", repo->protocol) == -1)
+ return got_error_from_errno("asprintf");
+
+ if (repo->server) {
+ p = s;
+ s = NULL;
+ if (asprintf(&s, "%s%s", p, repo->server) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+ free(p);
+ p = NULL;
+ }
+
+ if (repo->port) {
+ p = s;
+ s = NULL;
+ if (asprintf(&s, "%s:%d", p, repo->port) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+ free(p);
+ p = NULL;
+ }
+
+ if (repo->repository) {
+ p = s;
+ s = NULL;
+ if (asprintf(&s, "%s/%s", p, repo->repository) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+ free(p);
+ p = NULL;
+ }
+done:
+ if (err) {
+ free(s);
+ free(p);
+ } else
+ *url = s;
+ return err;
+}
+
+static const struct got_error *
+send_gotconfig_str(struct imsgbuf *ibuf, const char *value)
+{
+ size_t len = value ? strlen(value) + 1 : 0;
+
+ if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_STR_VAL, 0, 0, -1,
+ value, len) == -1)
+ return got_error_from_errno("imsg_compose GOTCONFIG_STR_VAL");
+
+ return got_privsep_flush_imsg(ibuf);
+}
+
+static const struct got_error *
+send_gotconfig_remotes(struct imsgbuf *ibuf,
+ struct gotconfig_remote_repo_list *remotes, int nremotes)
+{
+ const struct got_error *err = NULL;
+ struct got_imsg_remotes iremotes;
+ struct gotconfig_remote_repo *repo;
+ char *url = NULL;
+
+ iremotes.nremotes = nremotes;
+ if (imsg_compose(ibuf, GOT_IMSG_GOTCONFIG_REMOTES, 0, 0, -1,
+ &iremotes, sizeof(iremotes)) == -1)
+ return got_error_from_errno("imsg_compose GOTCONFIG_REMOTES");
+
+ err = got_privsep_flush_imsg(ibuf);
+ imsg_clear(ibuf);
+ if (err)
+ return err;
+
+ TAILQ_FOREACH(repo, remotes, entry) {
+ struct got_imsg_remote iremote;
+ size_t len = sizeof(iremote);
+ struct ibuf *wbuf;
+
+ iremote.mirror_references = repo->mirror_references;
+
+ iremote.name_len = strlen(repo->name);
+ len += iremote.name_len;
+
+ err = make_repo_url(&url, repo);
+ if (err)
+ break;
+ iremote.url_len = strlen(url);
+ len += iremote.url_len;
+
+ wbuf = imsg_create(ibuf, GOT_IMSG_GOTCONFIG_REMOTE, 0, 0, len);
+ if (wbuf == NULL) {
+ err = got_error_from_errno(
+ "imsg_create GOTCONFIG_REMOTE");
+ break;
+ }
+
+ if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
+ err = got_error_from_errno(
+ "imsg_add GOTCONFIG_REMOTE");
+ ibuf_free(wbuf);
+ break;
+ }
+
+ if (imsg_add(wbuf, repo->name, iremote.name_len) == -1) {
+ err = got_error_from_errno(
+ "imsg_add GOTCONFIG_REMOTE");
+ ibuf_free(wbuf);
+ break;
+ }
+ if (imsg_add(wbuf, url, iremote.url_len) == -1) {
+ err = got_error_from_errno(
+ "imsg_add GOTCONFIG_REMOTE");
+ ibuf_free(wbuf);
+ break;
+ }
+
+ wbuf->fd = -1;
+ imsg_close(ibuf, wbuf);
+ err = got_privsep_flush_imsg(ibuf);
+ if (err)
+ break;
+
+ free(url);
+ url = NULL;
+ }
+
+ free(url);
+ return err;
+}
+
+static const struct got_error *
+validate_config(struct gotconfig *gotconfig)
+{
+ struct gotconfig_remote_repo *repo, *repo2;
+ static char msg[512];
+
+ TAILQ_FOREACH(repo, &gotconfig->remotes, entry) {
+ if (repo->name == NULL) {
+ return got_error_msg(GOT_ERR_PARSE_CONFIG,
+ "name required for remote repository");
+ }
+
+ TAILQ_FOREACH(repo2, &gotconfig->remotes, entry) {
+ if (repo == repo2 ||
+ strcmp(repo->name, repo2->name) != 0)
+ continue;
+ snprintf(msg, sizeof(msg),
+ "duplicate remote repository name '%s'",
+ repo->name);
+ return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
+ }
+
+ if (repo->server == NULL) {
+ snprintf(msg, sizeof(msg),
+ "server required for remote repository \"%s\"",
+ repo->name);
+ return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
+ }
+
+ if (repo->protocol == NULL) {
+ snprintf(msg, sizeof(msg),
+ "protocol required for remote repository \"%s\"",
+ repo->name);
+ return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
+ }
+ if (strcmp(repo->protocol, "ssh") != 0 &&
+ strcmp(repo->protocol, "git+ssh") != 0 &&
+ strcmp(repo->protocol, "git") != 0) {
+ snprintf(msg, sizeof(msg),"unknown protocol \"%s\" "
+ "for remote repository \"%s\"", repo->protocol,
+ repo->name);
+ return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
+ }
+
+ if (repo->repository == NULL) {
+ snprintf(msg, sizeof(msg),
+ "repository path required for remote "
+ "repository \"%s\"", repo->name);
+ return got_error_msg(GOT_ERR_PARSE_CONFIG, msg);
+ }
+ }
+
+ return NULL;
+}
+
+int
+main(int argc, char *argv[])
+{
+ const struct got_error *err = NULL;
+ struct imsgbuf ibuf;
+ struct gotconfig *gotconfig;
+ size_t datalen;
+ const char *filename = "got.conf";
+#if 0
+ static int attached;
+
+ while (!attached)
+ sleep(1);
+#endif
+ signal(SIGINT, catch_sigint);
+
+ imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
+
+#ifndef PROFILE
+ /* revoke access to most system calls */
+ if (pledge("stdio recvfd", NULL) == -1) {
+ err = got_error_from_errno("pledge");
+ got_privsep_send_error(&ibuf, err);
+ return 1;
+ }
+#endif
+
+ if (argc > 1)
+ filename = argv[1];
+
+ for (;;) {
+ struct imsg imsg;
+
+ memset(&imsg, 0, sizeof(imsg));
+ imsg.fd = -1;
+
+ if (sigint_received) {
+ err = got_error(GOT_ERR_CANCELLED);
+ break;
+ }
+
+ err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
+ if (err) {
+ if (err->code == GOT_ERR_PRIVSEP_PIPE)
+ err = NULL;
+ break;
+ }
+
+ if (imsg.hdr.type == GOT_IMSG_STOP)
+ break;
+
+ switch (imsg.hdr.type) {
+ case GOT_IMSG_GOTCONFIG_PARSE_REQUEST:
+ datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
+ if (datalen != 0) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+ if (imsg.fd == -1){
+ err = got_error(GOT_ERR_PRIVSEP_NO_FD);
+ break;
+ }
+
+ if (gotconfig)
+ gotconfig_free(gotconfig);
+ err = gotconfig_parse(&gotconfig, filename, &imsg.fd);
+ if (err)
+ break;
+ err = validate_config(gotconfig);
+ break;
+ case GOT_IMSG_GOTCONFIG_AUTHOR_REQUEST:
+ if (gotconfig == NULL) {
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ break;
+ }
+ err = send_gotconfig_str(&ibuf,
+ gotconfig->author ? gotconfig->author : "");
+ break;
+ case GOT_IMSG_GOTCONFIG_REMOTES_REQUEST:
+ if (gotconfig == NULL) {
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ break;
+ }
+ err = send_gotconfig_remotes(&ibuf,
+ &gotconfig->remotes, gotconfig->nremotes);
+ break;
+ default:
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ break;
+ }
+
+ if (imsg.fd != -1) {
+ if (close(imsg.fd) == -1 && err == NULL)
+ err = got_error_from_errno("close");
+ }
+
+ imsg_free(&imsg);
+ if (err)
+ break;
+ }
+
+ imsg_clear(&ibuf);
+ if (err) {
+ if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
+ fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
+ got_privsep_send_error(&ibuf, err);
+ }
+ }
+ if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
+ err = got_error_from_errno("close");
+ return err ? 1 : 0;
+}
diff --git a/libexec/got-read-gotconfig/gotconfig.h b/libexec/got-read-gotconfig/gotconfig.h
new file mode 100644
index 0000000..ab55bd3
--- /dev/null
+++ b/libexec/got-read-gotconfig/gotconfig.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2020 Tracey Emery <tracey@openbsd.org>
+ * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+struct gotconfig_remote_repo {
+ TAILQ_ENTRY(gotconfig_remote_repo) entry;
+ char *name;
+ char *repository;
+ char *server;
+ char *protocol;
+ int port;
+ int mirror_references;
+};
+TAILQ_HEAD(gotconfig_remote_repo_list, gotconfig_remote_repo);
+
+struct gotconfig {
+ char *author;
+ struct gotconfig_remote_repo_list remotes;
+ int nremotes;
+};
+
+/*
+ * Parse individual gotconfig repository files
+ */
+const struct got_error *gotconfig_parse(struct gotconfig **, const char *,
+ int *);
+void gotconfig_free(struct gotconfig *);
diff --git a/libexec/got-read-gotconfig/parse.y b/libexec/got-read-gotconfig/parse.y
new file mode 100644
index 0000000..4bba644
--- /dev/null
+++ b/libexec/got-read-gotconfig/parse.y
@@ -0,0 +1,762 @@
+/*
+ * Copyright (c) 2020 Tracey Emery <tracey@openbsd.org>
+ * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
+ * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
+ * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
+ * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
+ * Copyright (c) 2001 Markus Friedl. All rights reserved.
+ * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
+ * Copyright (c) 2001 Theo de Raadt. All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+%{
+#include <sys/types.h>
+#include <sys/queue.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+
+#include <netinet/in.h>
+
+#include <arpa/inet.h>
+
+#include <netdb.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <event.h>
+#include <ifaddrs.h>
+#include <imsg.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <syslog.h>
+#include <unistd.h>
+
+#include "got_error.h"
+#include "gotconfig.h"
+
+static struct file {
+ FILE *stream;
+ const char *name;
+ size_t ungetpos;
+ size_t ungetsize;
+ u_char *ungetbuf;
+ int eof_reached;
+ int lineno;
+} *file;
+static const struct got_error* newfile(struct file**, const char *, int *);
+static void closefile(struct file *);
+int yyparse(void);
+int yylex(void);
+int yyerror(const char *, ...)
+ __attribute__((__format__ (printf, 1, 2)))
+ __attribute__((__nonnull__ (1)));
+int kw_cmp(const void *, const void *);
+int lookup(char *);
+int igetc(void);
+int lgetc(int);
+void lungetc(int);
+int findeol(void);
+static int parseport(char *, long long *);
+
+TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
+struct sym {
+ TAILQ_ENTRY(sym) entry;
+ int used;
+ int persist;
+ char *nam;
+ char *val;
+};
+
+int symset(const char *, const char *, int);
+char *symget(const char *);
+
+static int atoul(char *, u_long *);
+
+static const struct got_error* gerror;
+static struct gotconfig_remote_repo *remote;
+static struct gotconfig gotconfig;
+static const struct got_error* new_remote(struct gotconfig_remote_repo **);
+
+typedef struct {
+ union {
+ int64_t number;
+ char *string;
+ } v;
+ int lineno;
+} YYSTYPE;
+
+%}
+
+%token ERROR
+%token REMOTE REPOSITORY SERVER PORT PROTOCOL MIRROR_REFERENCES AUTHOR
+%token <v.string> STRING
+%token <v.number> NUMBER
+%type <v.number> boolean portplain
+%type <v.string> numberstring
+
+%%
+
+grammar : /* empty */
+ | grammar '\n'
+ | grammar author '\n'
+ | grammar remote '\n'
+ ;
+boolean : STRING {
+ if (strcasecmp($1, "true") == 0 ||
+ strcasecmp($1, "yes") == 0)
+ $$ = 1;
+ else if (strcasecmp($1, "false") == 0 ||
+ strcasecmp($1, "no") == 0)
+ $$ = 0;
+ else {
+ yyerror("invalid boolean value '%s'", $1);
+ free($1);
+ YYERROR;
+ }
+ free($1);
+ }
+ ;
+numberstring : NUMBER {
+ char *s;
+ if (asprintf(&s, "%lld", $1) == -1) {
+ yyerror("string: asprintf");
+ YYERROR;
+ }
+ $$ = s;
+ }
+ | STRING
+ ;
+portplain : numberstring {
+ if (parseport($1, &$$) == -1) {
+ free($1);
+ YYERROR;
+ }
+ free($1);
+ }
+ ;
+remoteopts2 : remoteopts2 remoteopts1 nl
+ | remoteopts1 optnl
+ ;
+remoteopts1 : REPOSITORY STRING {
+ remote->repository = strdup($2);
+ if (remote->repository == NULL) {
+ free($2);
+ yyerror("strdup");
+ YYERROR;
+ }
+ free($2);
+ }
+ | SERVER STRING {
+ remote->server = strdup($2);
+ if (remote->server == NULL) {
+ free($2);
+ yyerror("strdup");
+ YYERROR;
+ }
+ free($2);
+ }
+ | PROTOCOL STRING {
+ remote->protocol = strdup($2);
+ if (remote->protocol == NULL) {
+ free($2);
+ yyerror("strdup");
+ YYERROR;
+ }
+ free($2);
+ }
+ | MIRROR_REFERENCES boolean {
+ remote->mirror_references = $2;
+ }
+ | PORT portplain {
+ remote->port = $2;
+ }
+ ;
+remote : REMOTE STRING {
+ static const struct got_error* error;
+
+ error = new_remote(&remote);
+ if (error) {
+ free($2);
+ yyerror("%s", error->msg);
+ YYERROR;
+ }
+ remote->name = strdup($2);
+ if (remote->name == NULL) {
+ free($2);
+ yyerror("strdup");
+ YYERROR;
+ }
+ free($2);
+ } '{' optnl remoteopts2 '}' {
+ TAILQ_INSERT_TAIL(&gotconfig.remotes, remote, entry);
+ gotconfig.nremotes++;
+ }
+ ;
+author : AUTHOR STRING {
+ gotconfig.author = strdup($2);
+ if (gotconfig.author == NULL) {
+ free($2);
+ yyerror("strdup");
+ YYERROR;
+ }
+ free($2);
+ }
+ ;
+optnl : '\n' optnl
+ | /* empty */
+ ;
+nl : '\n' optnl
+ ;
+%%
+
+struct keywords {
+ const char *k_name;
+ int k_val;
+};
+
+int
+yyerror(const char *fmt, ...)
+{
+ va_list ap;
+ char *msg;
+ char *err = NULL;
+
+ va_start(ap, fmt);
+ if (vasprintf(&msg, fmt, ap) == -1) {
+ gerror = got_error_from_errno("vasprintf");
+ return 0;
+ }
+ va_end(ap);
+ if (asprintf(&err, "%s: line %d: %s", file->name, yylval.lineno,
+ msg) == -1) {
+ gerror = got_error_from_errno("asprintf");
+ return(0);
+ }
+ gerror = got_error_msg(GOT_ERR_PARSE_CONFIG, strdup(err));
+ free(msg);
+ free(err);
+ return(0);
+}
+int
+kw_cmp(const void *k, const void *e)
+{
+ return (strcmp(k, ((const struct keywords *)e)->k_name));
+}
+
+int
+lookup(char *s)
+{
+ /* This has to be sorted always. */
+ static const struct keywords keywords[] = {
+ {"author", AUTHOR},
+ {"mirror-references", MIRROR_REFERENCES},
+ {"port", PORT},
+ {"protocol", PROTOCOL},
+ {"remote", REMOTE},
+ {"repository", REPOSITORY},
+ {"server", SERVER},
+ };
+ const struct keywords *p;
+
+ p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
+ sizeof(keywords[0]), kw_cmp);
+
+ if (p)
+ return (p->k_val);
+ else
+ return (STRING);
+}
+
+#define START_EXPAND 1
+#define DONE_EXPAND 2
+
+static int expanding;
+
+int
+igetc(void)
+{
+ int c;
+
+ while (1) {
+ if (file->ungetpos > 0)
+ c = file->ungetbuf[--file->ungetpos];
+ else
+ c = getc(file->stream);
+
+ if (c == START_EXPAND)
+ expanding = 1;
+ else if (c == DONE_EXPAND)
+ expanding = 0;
+ else
+ break;
+ }
+ return (c);
+}
+
+int
+lgetc(int quotec)
+{
+ int c, next;
+
+ if (quotec) {
+ c = igetc();
+ if (c == EOF) {
+ yyerror("reached end of file while parsing "
+ "quoted string");
+ }
+ return (c);
+ }
+
+ c = igetc();
+ while (c == '\\') {
+ next = igetc();
+ if (next != '\n') {
+ c = next;
+ break;
+ }
+ yylval.lineno = file->lineno;
+ file->lineno++;
+ }
+
+ return (c);
+}
+
+void
+lungetc(int c)
+{
+ if (c == EOF)
+ return;
+
+ if (file->ungetpos >= file->ungetsize) {
+ void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
+ if (p == NULL)
+ err(1, "%s", __func__);
+ file->ungetbuf = p;
+ file->ungetsize *= 2;
+ }
+ file->ungetbuf[file->ungetpos++] = c;
+}
+
+int
+findeol(void)
+{
+ int c;
+
+ /* Skip to either EOF or the first real EOL. */
+ while (1) {
+ c = lgetc(0);
+ if (c == '\n') {
+ file->lineno++;
+ break;
+ }
+ if (c == EOF)
+ break;
+ }
+ return (ERROR);
+}
+
+static long long
+getservice(char *n)
+{
+ struct servent *s;
+ u_long ulval;
+
+ if (atoul(n, &ulval) == 0) {
+ if (ulval > 65535) {
+ yyerror("illegal port value %lu", ulval);
+ return (-1);
+ }
+ return ulval;
+ } else {
+ s = getservbyname(n, "tcp");
+ if (s == NULL)
+ s = getservbyname(n, "udp");
+ if (s == NULL) {
+ yyerror("unknown port %s", n);
+ return (-1);
+ }
+ return (s->s_port);
+ }
+}
+
+static int
+parseport(char *port, long long *pn)
+{
+ if ((*pn = getservice(port)) == -1) {
+ *pn = 0LL;
+ return (-1);
+ }
+ return (0);
+}
+
+
+int
+yylex(void)
+{
+ unsigned char buf[8096];
+ unsigned char *p, *val;
+ int quotec, next, c;
+ int token;
+
+top:
+ p = buf;
+ c = lgetc(0);
+ while (c == ' ' || c == '\t')
+ c = lgetc(0); /* nothing */
+
+ yylval.lineno = file->lineno;
+ if (c == '#') {
+ c = lgetc(0);
+ while (c != '\n' && c != EOF)
+ c = lgetc(0); /* nothing */
+ }
+ if (c == '$' && !expanding) {
+ while (1) {
+ c = lgetc(0);
+ if (c == EOF)
+ return (0);
+
+ if (p + 1 >= buf + sizeof(buf) - 1) {
+ yyerror("string too long");
+ return (findeol());
+ }
+ if (isalnum(c) || c == '_') {
+ *p++ = c;
+ continue;
+ }
+ *p = '\0';
+ lungetc(c);
+ break;
+ }
+ val = symget(buf);
+ if (val == NULL) {
+ yyerror("macro '%s' not defined", buf);
+ return (findeol());
+ }
+ p = val + strlen(val) - 1;
+ lungetc(DONE_EXPAND);
+ while (p >= val) {
+ lungetc(*p);
+ p--;
+ }
+ lungetc(START_EXPAND);
+ goto top;
+ }
+
+ switch (c) {
+ case '\'':
+ case '"':
+ quotec = c;
+ while (1) {
+ c = lgetc(quotec);
+ if (c == EOF)
+ return (0);
+ if (c == '\n') {
+ file->lineno++;
+ continue;
+ } else if (c == '\\') {
+ next = lgetc(quotec);
+ if (next == EOF)
+ return (0);
+ if (next == quotec || c == ' ' || c == '\t')
+ c = next;
+ else if (next == '\n') {
+ file->lineno++;
+ continue;
+ } else
+ lungetc(next);
+ } else if (c == quotec) {
+ *p = '\0';
+ break;
+ } else if (c == '\0') {
+ yyerror("syntax error");
+ return (findeol());
+ }
+ if (p + 1 >= buf + sizeof(buf) - 1) {
+ yyerror("string too long");
+ return (findeol());
+ }
+ *p++ = c;
+ }
+ yylval.v.string = strdup(buf);
+ if (yylval.v.string == NULL)
+ err(1, "%s", __func__);
+ return (STRING);
+ }
+
+#define allowed_to_end_number(x) \
+ (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
+
+ if (c == '-' || isdigit(c)) {
+ do {
+ *p++ = c;
+ if ((unsigned)(p-buf) >= sizeof(buf)) {
+ yyerror("string too long");
+ return (findeol());
+ }
+ c = lgetc(0);
+ } while (c != EOF && isdigit(c));
+ lungetc(c);
+ if (p == buf + 1 && buf[0] == '-')
+ goto nodigits;
+ if (c == EOF || allowed_to_end_number(c)) {
+ const char *errstr = NULL;
+
+ *p = '\0';
+ yylval.v.number = strtonum(buf, LLONG_MIN,
+ LLONG_MAX, &errstr);
+ if (errstr) {
+ yyerror("\"%s\" invalid number: %s",
+ buf, errstr);
+ return (findeol());
+ }
+ return (NUMBER);
+ } else {
+nodigits:
+ while (p > buf + 1)
+ lungetc(*--p);
+ c = *--p;
+ if (c == '-')
+ return (c);
+ }
+ }
+
+#define allowed_in_string(x) \
+ (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
+ x != '{' && x != '}' && \
+ x != '!' && x != '=' && x != '#' && \
+ x != ','))
+
+ if (isalnum(c) || c == ':' || c == '_') {
+ do {
+ *p++ = c;
+ if ((unsigned)(p-buf) >= sizeof(buf)) {
+ yyerror("string too long");
+ return (findeol());
+ }
+ c = lgetc(0);
+ } while (c != EOF && (allowed_in_string(c)));
+ lungetc(c);
+ *p = '\0';
+ token = lookup(buf);
+ if (token == STRING) {
+ yylval.v.string = strdup(buf);
+ if (yylval.v.string == NULL)
+ err(1, "%s", __func__);
+ }
+ return (token);
+ }
+ if (c == '\n') {
+ yylval.lineno = file->lineno;
+ file->lineno++;
+ }
+ if (c == EOF)
+ return (0);
+ return (c);
+}
+
+static const struct got_error*
+newfile(struct file **nfile, const char *filename, int *fd)
+{
+ const struct got_error* error = NULL;
+
+ (*nfile) = calloc(1, sizeof(struct file));
+ if ((*nfile) == NULL)
+ return got_error_from_errno("calloc");
+ (*nfile)->stream = fdopen(*fd, "r");
+ if ((*nfile)->stream == NULL) {
+ error = got_error_from_errno("fdopen");
+ free((*nfile));
+ return error;
+ }
+ *fd = -1; /* Stream owns the file descriptor now. */
+ (*nfile)->name = filename;
+ (*nfile)->lineno = 1;
+ (*nfile)->ungetsize = 16;
+ (*nfile)->ungetbuf = malloc((*nfile)->ungetsize);
+ if ((*nfile)->ungetbuf == NULL) {
+ error = got_error_from_errno("malloc");
+ fclose((*nfile)->stream);
+ free((*nfile));
+ return error;
+ }
+ return NULL;
+}
+
+static const struct got_error*
+new_remote(struct gotconfig_remote_repo **remote)
+{
+ const struct got_error *error = NULL;
+
+ *remote = calloc(1, sizeof(**remote));
+ if (*remote == NULL)
+ error = got_error_from_errno("calloc");
+ return error;
+}
+
+static void
+closefile(struct file *file)
+{
+ fclose(file->stream);
+ free(file->ungetbuf);
+ free(file);
+}
+
+const struct got_error *
+gotconfig_parse(struct gotconfig **conf, const char *filename, int *fd)
+{
+ const struct got_error *err = NULL;
+ struct sym *sym, *next;
+
+ *conf = NULL;
+
+ err = newfile(&file, filename, fd);
+ if (err)
+ return err;
+
+ TAILQ_INIT(&gotconfig.remotes);
+
+ yyparse();
+ closefile(file);
+
+ /* Free macros and check which have not been used. */
+ TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
+ if (!sym->persist) {
+ free(sym->nam);
+ free(sym->val);
+ TAILQ_REMOVE(&symhead, sym, entry);
+ free(sym);
+ }
+ }
+
+ if (gerror == NULL)
+ *conf = &gotconfig;
+ return gerror;
+}
+
+void
+gotconfig_free(struct gotconfig *conf)
+{
+ struct gotconfig_remote_repo *remote;
+
+ free(conf->author);
+ while (!TAILQ_EMPTY(&conf->remotes)) {
+ remote = TAILQ_FIRST(&conf->remotes);
+ TAILQ_REMOVE(&conf->remotes, remote, entry);
+ free(remote->name);
+ free(remote->repository);
+ free(remote->server);
+ free(remote->protocol);
+ free(remote);
+ }
+}
+
+int
+symset(const char *nam, const char *val, int persist)
+{
+ struct sym *sym;
+
+ TAILQ_FOREACH(sym, &symhead, entry) {
+ if (strcmp(nam, sym->nam) == 0)
+ break;
+ }
+
+ if (sym != NULL) {
+ if (sym->persist == 1)
+ return (0);
+ else {
+ free(sym->nam);
+ free(sym->val);
+ TAILQ_REMOVE(&symhead, sym, entry);
+ free(sym);
+ }
+ }
+ sym = calloc(1, sizeof(*sym));
+ if (sym == NULL)
+ return (-1);
+
+ sym->nam = strdup(nam);
+ if (sym->nam == NULL) {
+ free(sym);
+ return (-1);
+ }
+ sym->val = strdup(val);
+ if (sym->val == NULL) {
+ free(sym->nam);
+ free(sym);
+ return (-1);
+ }
+ sym->used = 0;
+ sym->persist = persist;
+ TAILQ_INSERT_TAIL(&symhead, sym, entry);
+ return (0);
+}
+
+int
+cmdline_symset(char *s)
+{
+ char *sym, *val;
+ int ret;
+ size_t len;
+
+ val = strrchr(s, '=');
+ if (val == NULL)
+ return (-1);
+
+ len = strlen(s) - strlen(val) + 1;
+ sym = malloc(len);
+ if (sym == NULL)
+ errx(1, "cmdline_symset: malloc");
+
+ strlcpy(sym, s, len);
+
+ ret = symset(sym, val + 1, 1);
+ free(sym);
+
+ return (ret);
+}
+
+char *
+symget(const char *nam)
+{
+ struct sym *sym;
+
+ TAILQ_FOREACH(sym, &symhead, entry) {
+ if (strcmp(nam, sym->nam) == 0) {
+ sym->used = 1;
+ return (sym->val);
+ }
+ }
+ return (NULL);
+}
+
+static int
+atoul(char *s, u_long *ulvalp)
+{
+ u_long ulval;
+ char *ep;
+
+ errno = 0;
+ ulval = strtoul(s, &ep, 0);
+ if (s[0] == '\0' || *ep != '\0')
+ return (-1);
+ if (errno == ERANGE && ulval == ULONG_MAX)
+ return (-1);
+ *ulvalp = ulval;
+ return (0);
+}
diff --git a/regress/cmdline/commit.sh b/regress/cmdline/commit.sh
index 34abaa9..13a4e58 100755
--- a/regress/cmdline/commit.sh
+++ b/regress/cmdline/commit.sh
@@ -699,6 +699,43 @@ function test_commit_tree_entry_sorting {
test_done "$testroot" "$ret"
}
+function test_commit_gotconfig_author {
+ local testroot=`test_init commit_gotconfig_author`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+ echo 'author "Flan Luck <flan_luck@openbsd.org>"' \
+ > $testroot/repo/.git/got.conf
+
+ echo "modified alpha" > $testroot/wt/alpha
+ (cd $testroot/wt && got commit -m 'test gotconfig author' > /dev/null)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/repo && got log -l1 | grep ^from: > $testroot/stdout)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "from: Flan Luck <flan_luck@openbsd.org>" \
+ > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
function test_commit_gitconfig_author {
local testroot=`test_init commit_gitconfig_author`
@@ -1297,6 +1334,7 @@ run_test test_commit_selected_paths
run_test test_commit_outside_refs_heads
run_test test_commit_no_email
run_test test_commit_tree_entry_sorting
+run_test test_commit_gotconfig_author
run_test test_commit_gitconfig_author
run_test test_commit_xbit_change
run_test test_commit_normalizes_filemodes