xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / inc / repo.inc.php (8ddc4a8653e5f9c58938fefba590a008f6ebfd26) (64KiB) (mode 100644) [raw]
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 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
<?php
require_once($INC . "/util.inc.php");
require_once($INC . "/log.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/user.inc.php");
require_once($INC . "/git.inc.php");
require_once($INC . "/rights.inc.php");
require_once($INC . "/prof.inc.php");
require_once($INC . "/mail.inc.php");
require_once($INC . "/events.inc.php");
require_once($INC . "/webhooks.inc.php");

$rg_repo_refs_rights = array(
	"F" => "Fetch",
	"P" => "Push",
	"H" => "Anonymous push",
	"S" => "Create annotated tag",
	"n" => "Delete annotated tag",
	"Y" => "Create un-annotated tag",
	"U" => "Modify un-annotated tag",
	"u" => "Delete un-annotated tag",
	"C" => "Create branch",
	"D" => "Delete branch",
	"O" => "Non fast-forwards",
	"M" => "Merge commits",
	"W" => "Bad whitespace"
);

$rg_repo_path_rights = array(
	"P" => "Push",
	"W" => "Bad whitespace"
);

$rg_repo_rights = array(
	'A' => 'Access repo',
	'a' => 'Access bug tracker',
	'B' => 'Add bugs',
	'C' => 'Close bugs',
	'D' => 'Delete repo',
	'd' => 'Delete bugs',
	'E' => 'Create/edit repo',
	'G' => 'Grant rights',
	'K' => 'Lock repo',
	'r' => 'Reopen bugs',
	'T' => 'Delete artifacts',
	't' => 'Access artifacts',
	'W' => 'Manage hooks'
);

// TODO: default rights should go into conf file?
// TODO: better move all config to database (modulo db conn info)?

rg_rights_register("repo_refs", $rg_repo_refs_rights, "FMH",
	"rg_repo_compare_refs", "rg_repo_rights_inject");
rg_rights_register("repo_path", $rg_repo_path_rights, "P",
	"rg_repo_compare_paths", "rg_repo_rights_inject");
rg_rights_register("repo", $rg_repo_rights, 'AaBt',
	FALSE, "rg_repo_rights_inject");


/*
 * Helper to obtain the next id (used for bugs and mr)
 */
function rg_repo_next_id($db, $field, $repo_id)
{
	rg_prof_start('repo_next_id');
	rg_log_enter('repo_next_id: field=' . $field . ' repo_id=' . $repo_id);

	$next_id = FALSE;
	while (1) {
		$params = array('repo_id' => $repo_id);
		$sql = 'UPDATE repos SET last_' . $field . '_id'
			. ' = last_' . $field . '_id + 1'
			. ' WHERE repo_id = @@repo_id@@'
			. ' RETURNING last_' . $field . '_id AS next_id';
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_repo_set_error('cannot get/update max (' . rg_sql_error() . ')');
			break;
		}
		$rows = rg_sql_num_rows($res);
		if ($rows > 0)
			$row = rg_sql_fetch_array($res);
		rg_sql_free_result($res);
		if ($rows == 0) {
			rg_repo_set_error('seems that repo does not exists!');
			break;
		}
		$next_id = $row['next_id'];

		$key = 'repo_by_id' . '::' . $repo_id . '::'
			. 'last_' . $field . '_id';
		rg_cache_set($key, $next_id, RG_SOCKET_NO_WAIT);
		break;
	}

	rg_log('DEBUG: next_id=' . $next_id);

	rg_log_exit();
	rg_prof_end('repo_next_id');
	return $next_id;
}

/*
 * Improve repo info
 */
function rg_repo_cosmetic($db, &$row)
{
	global $rg_ssh_port, $rg_git_port;

	// Because this field was added later and the cache may not have it
	if (!isset($row['template']))
		$row['template'] = '';

	if (!empty($row['description'])) {
		$_a = rg_xss_safe($row['description']);
		$row['HTML:description_nlbr'] = nl2br($_a);

		if (mb_strlen($row['description']) > 100)
			$row['HTML:description_short_nlbr'] = nl2br(mb_substr($_a, 0, 100)) . '...';
		else
			$row['HTML:description_short_nlbr'] = $row['HTML:description_nlbr'];
	} else {
		$row['HTML:description_nlbr'] = 'No description available';
		$row['HTML:description_short_nlbr'] = $row['HTML:description_nlbr'];
	}

	if (isset($row['itime']))
		$row['HTML:itime_nice'] = gmdate('Y-m-d', $row['itime']);

	$_ui = rg_user_info($db, $row['uid'], '', '');
	if ($_ui['exists'] == 1) {
		$row['owner'] = $_ui['username'];
		$row['url_user'] = rg_re_userpage($_ui);
		$row['url_repo'] = rg_re_repopage($_ui, $row['name']);

		$row['clone_url_ssh'] = rg_re_repo_ssh($_ui['organization'],
			$_ui['username'], $row['name']);
		$row['clone_url_git'] = rg_re_repo_git($_ui['organization'],
			$_ui['username'], $row['name']);
		$row['clone_url_http'] = rg_base_url()
			. rg_re_repo_http($_ui['organization'],
				$_ui['username'], $row['name']);
	}

	$row['master_name'] = '-';
	if ($row['master'] > 0) {
		$_mi = rg_repo_info($db, $row['master'], 0, '');
		if ($_mi['exists'] == 1)
			$row['master_name'] = $_mi['name'];
	}

	if (isset($row['disk_used_mb']))
		$row['disk_used'] = rg_1024($row['disk_used_mb'] * 1024 * 1024);
}

/*
 * Returns info about a repo
 * If you want to lookup by repo_id or uid/repo_name
 */
function rg_repo_info($db, $repo_id, $uid, $repo_name)
{
	rg_prof_start('repo_info');

	$ret['ok'] = 0;
	$ret['exists'] = 0;
	while(1) {
		$params = array("uid" => $uid,
			"repo_id" => $repo_id,
			"repo_name" => $repo_name);

		if ($repo_id > 0) {
			$c = rg_cache_get('repo_by_id' . '::' . $repo_id);
			if (($c !== FALSE) && isset($c['repo_id'])) {
				$ret = $c;
				rg_repo_cosmetic($db, $ret);
				break;
			}

			$sql = "SELECT * FROM repos WHERE repo_id = @@repo_id@@";
		} else if (!empty($repo_name)) {
			$x_repo_id = rg_cache_get("repo_by_name::$uid::$repo_name");
			if ($x_repo_id !== FALSE) {
				$ret = rg_repo_info($db, $x_repo_id, $uid, "");
				break;
			}

			$sql = "SELECT * FROM repos WHERE uid = @@uid@@"
				. " AND name = @@repo_name@@";
		} else {
			rg_repo_set_error("no repo_id or user/repo specified!");
			break;
		}

		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_repo_set_error("cannot query (" . rg_sql_error() . ")");
			break;
		}
		$rows = rg_sql_num_rows($res);
		if ($rows > 0)
			$ret = rg_sql_fetch_array($res);
		rg_sql_free_result($res);

		if (($rows == 0) && ($repo_id == 0)) {
			// Repo not found, maybe it was renamed
			$_repo_id = rg_repo_lookup_by_old_name($db, $uid, $repo_name);
			if ($_repo_id === FALSE)
				break;

			if ($_repo_id > 0) {
				$ret = rg_repo_info($db, $_repo_id, 0, "");
				break;
			}
		}

		$ret['ok'] = 1;
		if ($rows > 0) {
			$ret['exists'] = 1;
		} else {
			$ret['exists'] = 0;
		}
		//rg_log_ml("CHECK: ret=" . print_r($ret, TRUE));

		if ($ret['exists'] == 1) {
			rg_cache_set('repo_by_id' . '::' . $ret['repo_id'],
				$ret, RG_SOCKET_NO_WAIT);

			rg_cache_set("repo_by_name::$uid::" . $ret['name'],
				$ret['repo_id'], RG_SOCKET_NO_WAIT);
		}

		if ($rows > 0)
			rg_repo_cosmetic($db, $ret);
		break;
	}

	rg_prof_end("repo_info");
	return $ret;
}

/*
 * Function used to inject rights for a obj_id/type combination
 * It will be called from rg_rights_get
 * @out - the output array where we want to put rights
 */
function rg_repo_rights_inject($db, $obj_id, $type, $owner, $uid)
{
	rg_log_enter("repo_rights_inject: obj_id=$obj_id type=$type"
		. " owner=$owner uid=$uid");

	$ret = array();
	while (1) {
		$a = array();
		$a['type'] = $type;
		$a['obj_id'] = $obj_id;
		$a['uid'] = 0; // TODO: not clear here what to put!
		$a['itime'] = 0;
		$a['misc'] = "";
		$a['prio'] = 0;
		$a['who'] = $owner; // TODO: not clear if correct/good
		$a['right_id'] = 0;
		$a['ip'] = "";
		$a['can_be_deleted'] = 0;
		$a['description'] = "Autogenerated";

		if ($uid > 0) {
			$ui = rg_user_info($db, $uid, "", "");
			if ($ui['exists'] != 1)
				break;

			if ($ui['is_admin'] == 1) {
				$a['uid'] = $owner;
				$a['rights'] = rg_rights_all($type);
				$a['description'] = 'Autogenerated (you are admin)';
				$ret[] = $a;
			}
		}

		$ri = rg_repo_info($db, $obj_id, 0, "");
		if ($ri['exists'] != 1) {
			rg_log("repo $obj_id does not exists");
			break;
		}

		// Here, rights to inject, even if repo is not public
		if (strcmp($type, "repo_path") == 0) {
			$a['uid'] = 0;
			$a['prio'] = 30001;
			$a['rights'] = "PW"; // push + whitespace
			$a['description'] = 'Autogenerated (sane default)';
			$ret[] = $a;
		}

		// No rights to inject if is not public
		if ($ri['public'] != 1)
			break;

		if (strcmp($type, "repo") == 0) {
			$a['uid'] = 0;
			$a['rights'] = 'Aat';
			$a['description'] = 'Autogenerated (repo is public)';
			$ret[] = $a;
		} else if (strcmp($type, "repo_refs") == 0) {
			$a['uid'] = 0;
			$a['prio'] = 1;
			$a['rights'] = "F"; // Fetch
			$a['description'] = 'Autogenerated (repo is public)';
			$ret[] = $a;

			$a['uid'] = 0;
			$a['prio'] = 30001;
			$a['rights'] = "H"; // push + whitespace
			$a['description'] = 'Autogenerated (sane default)';
			$ret[] = $a;
		} else if (strcmp($type, "repo_path") == 0) {
		} else {
			rg_log("type $type not used. bug?");
			break;
		}

		break;
	}

	rg_log_exit();
	return $ret;
}

/*
 * Returns TRUE if the login user has @rights rights
 */
function rg_repo_has_rights($db, $rg, $rights)
{
	$x = array();
	$x['obj_id'] = $rg['ri']['repo_id'];
	$x['type'] = 'repo';
	$x['owner'] = $rg['ri']['uid'];
	$x['uid'] = $rg['login_ui']['uid'];
	$x['username'] = $rg['login_ui']['username'];
	$x['needed_rights'] = $rights;
	$x['ip'] = $rg['ip'];
	$x['misc'] = '';
	return rg_rights_allow($db, $x);
}

/*
 * Bring a ref to a canonical format (refs/x/name)
 * TODO: move to git.inc?
 */
function rg_repo_ref_canon($ref)
{
	if (strncmp($ref, "refs/", 5) == 0) {
		// do nothing
	} else if (strncmp($ref, "/refs/", 6) == 0) {
		$ref = substr($ref, 1);
	} else {
		$ref = "refs/heads/" . $ref;
	}

	return $ref;
}

/*
 * Compare function for refs
 */
function rg_repo_compare_refs($misc, $ref)
{
	rg_prof_start("repo_compare_refs");

	if (empty($misc))
		return TRUE;

	$misc = rg_repo_ref_canon($misc);
	$ref = rg_repo_ref_canon($ref);

	// seems that by doing escape, nothing will match!
	//$qmisc = preg_quote($misc, '/');
	$qmisc = str_replace('`', '', $misc);

	$ret = preg_match('`^' . $qmisc . '`uD', $ref);
	rg_log("repo_compare_refs: ret=$ret misc=$misc ref=$ref [qmisc=$qmisc] => "
		. ($ret === 1 ? "match" : "no match"));

	rg_prof_end("repo_compare_refs");
	return $ret === 1;
}

/*
 * Compare function for paths
 */
function rg_repo_compare_paths($misc, $path)
{
	rg_prof_start("repo_compare_paths");

	$qmisc = preg_quote($misc, '/');

	$ret = preg_match('/' . $qmisc . '/uD', $path);
	rg_log("repo_compare_paths: misc=$misc path=$path => " . ($ret === 1 ? "T" : "F"));

	rg_prof_end("repo_compare_paths");
	return $ret === 1;
}

/*
 * Remove "refs/heads/" when showing rights on web
 * TODO: don't know where to call it. Maybe in rg_rights_load?
 * TODO: also, register this kind of function.
 */
function rg_repo_ref_nice($ref)
{
	if (strncmp($ref, "refs/heads/", 11) == 0)
		$ref = substr($ref, 11);
	return $ref;
}

// Repo history categories
define('REPO_CAT_CREATE', 1);
define('REPO_CAT_CLONED', 2);
define('REPO_CAT_PUSH', 3);
define('REPO_CAT_RENAME', 4);
define('REPO_CAT_UPDATE', 5);
define('REPO_CAT_BUG_ADDED', 10);
define('REPO_CAT_BUG_CLOSED', 11);
define('REPO_CAT_GIT_ATAG_CREATE', 20);
define('REPO_CAT_GIT_ATAG_DELETE', 21);
define('REPO_CAT_GIT_ATAG_UPDATE', 22);
define('REPO_CAT_GIT_UTAG_CREATE', 30);
define('REPO_CAT_GIT_UTAG_DELETE', 31);
define('REPO_CAT_GIT_UTAG_UPDATE', 32);
define('REPO_CAT_GIT_BRANCH_DELETE', 40);
define('REPO_CAT_GIT_BRANCH_UPDATE', 41);
define('REPO_CAT_GIT_BRANCH_CREATE', 42);
define('REPO_CAT_GIT_BRANCH_ANON_PUSH', 43);
define('REPO_CAT_LOCK', 50);
define('REPO_CAT_UNLOCK', 51);

$rg_repo_error = '';

function rg_repo_set_error($str)
{
	global $rg_repo_error;
	$rg_repo_error = $str;
	rg_log('repo_set_error: ' . $str);
}

function rg_repo_error()
{
	global $rg_repo_error;
	return $rg_repo_error;
}

/*
 * Events functions
 */
$rg_repo_functions = array(
	3000 => "rg_repo_event_new",
	3001 => "rg_repo_event_del",
	3002 => "rg_repo_event_update",
	3003 => "rg_repo_event_notify_user",
	3004 => "rg_repo_event_symlink_by_name",
	3005 => "rg_repo_event_storage_create",
	3006 => "rg_repo_history_insert",
	3007 => 'rg_repo_event_push',
	// new new style
	'repo_event_new' => 'rg_repo_event_new',
	'repo_event_del' => 'rg_repo_event_del',
	'repo_event_update' => 'rg_repo_event_update',
	'repo_event_notify_user' => 'rg_repo_event_notify_user',
	'repo_event_symlink_by_name' => 'rg_repo_event_symlink_by_name',
	'repo_event_storage_create' => 'rg_repo_event_storage_create',
	'repo_history_insert' => 'rg_repo_history_insert',
	'repo_event_push' => 'rg_repo_event_push'
);
rg_event_register_functions($rg_repo_functions);

/*
 * Event for adding a new repo
 */
function rg_repo_event_new($db, $event)
{
	$ret = array();

	$event['op'] = "new";

	$x = $event;
	$x['category'] = 'repo_event_storage_create';
	$x['prio'] = 20;
	$x['notification'] .= "-git";
	$ret[] = $x;

	$x = $event;
	$x['category'] = 'repo_event_symlink_by_name';
	$x['prio'] = 200;
	$x['notification'] .= "-symlink";
	$ret[] = $x;

	$x = $event;
	$x['category'] = 'repo_event_notify_user';
	$x['prio'] = 100;
	$x['notification'] .= "-notify";
	$ret[] = $x;

	// webhook
	$x = $event;
	$x['category'] = 'wh_send';
	$x['prio'] = 50;
	$x['wh_event'] = 'C'; // see rg_wh_events array
	$ret[] = $x;

	// add a history entry
	$x = $event;
	$x['category'] = 'repo_history_insert';
	$x['prio'] = 50;
	$ret[] = $x;

	// TODO: notify watchers of the user

	return $ret;
}

/*
 * Event for deleting repo
 */
function rg_repo_event_del($db, $event)
{
	$ret = array();

	$event['op'] = "del";
	// notify user
	$ret[] = array_merge($event,
		array('category' => 'repo_event_notify_user', 'prio' => 100));

	// TODO: notify watchers

	return $ret;
}

/*
 * Make a symlink by name (by_name/name -> ../by_id/xx/xx/xx/xx/xxxxxxxx.git)
 * TODO: why return may be an array?!
 */
function rg_repo_event_symlink_by_name($db, $e)
{
	rg_prof_start("repo_event_symlink_by_name");

	$id_path = rg_repo_path_by_id($e['ui']['uid'], $e['ri']['repo_id']);
	$id_path_rel = rg_repo_path_by_id_rel($e['ui']['uid'], $e['ri']['repo_id']);
	$new_path = rg_repo_path_by_name($e['ui']['uid'], $e['ri']['name']);

	$ret = FALSE;
	while (1) {
		// Check if we already did the rename
		if (file_exists($new_path)) {
			if (!is_link($new_path)) {
				rg_internal_error("$new_path is not a link");
				break;
			}

			$v = readlink($new_path);
			if ($v === FALSE) {
				rg_internal_error("cannot read link $new_path");
				break;
			}
			rg_log("new_path points to [$v]");

			if (strcmp($id_path_rel, $v) == 0) {
				// Link already done
				$ret = array();
				break;
			}

			// Seems that new_path points to other place
			$r = rename($new_path, $new_path . ".BOGUS." . time());
			if ($r !== TRUE) {
				rg_internal_error("cannot rename bogus");
				break;
			}
		}

		// Create links' parent
		$new_path_parent = dirname($new_path);
		if (!is_dir($new_path_parent)) {
			$r = mkdir($new_path_parent, 0700, TRUE);
			if ($r === FALSE) {
				rg_repo_set_error("cannot create links' parent");
				break;
			}
		}

		// Now, the new name is free, do the link
		$r = symlink($id_path_rel, $new_path);
		if ($r !== TRUE) {
			rg_internal_error("cannot symlink $id_path -> $new_path (" . rg_php_err() . ")!");
			break;
		}

		$ret = array();
		break;
	}

	rg_prof_end("repo_event_symlink_by_name");
	return $ret;
}

/*
 * Creates git dir storage
 */
function rg_repo_event_storage_create($db, $e)
{
	rg_prof_start("repo_event_storage_create");
	rg_log_enter("repo_event_storage_create: e=" . rg_array2string($e));

	$ret = FALSE;
	while (1) {
		$by_id_path = rg_repo_path_by_id($e['ui']['uid'], $e['ri']['repo_id']);
		if (!is_dir($by_id_path)) {
			if (mkdir($by_id_path, 0700, TRUE) === FALSE) {
				rg_repo_set_error("could not create folder $dst");
				break;
			}
		}

		if ($e['ri']['master'] == 0) {
			$r = rg_git_init($by_id_path);
			if ($r === FALSE) {
				rg_repo_set_error("cannot init master"
					. " (" . rg_git_error() . ")");
				break;
			}
		} else {
			$mi = rg_repo_info($db, $e['ri']['master'], 0, "");
			if ($mi['exists'] != 1) {
				rg_repo_set_error("cannot find master"
					. " (" . rg_repo_error() . ")");
				break;
			}

			$master_by_id_path = rg_repo_path_by_id($mi['uid'],
				$mi['repo_id']);
			$r = rg_git_clone($master_by_id_path, $by_id_path);
			if ($r === FALSE) {
				rg_repo_set_error("could not create repo"
					. " (" . rg_git_error() . ")");
				break;
			}

		}

		$r = rg_repo_event_symlink_by_name($db, $e);
		if ($r === FALSE)
			break;

		$r = rg_repo_git_done($db, $e['ri']['repo_id']);
		if ($r !== TRUE)
			break;

		$ret = array();
		break;
	}

	rg_log_exit();
	rg_prof_end("repo_event_storage_create");
	return $ret;
}

/*
 * Event for renaming a repo
 */
function rg_repo_event_update($db, $event)
{
	$ret = array();

	$event['op'] = "update";

	// make symlink by name
	$ret[] = array_merge($event,
		array('category' => 'repo_event_symlink_by_name', 'prio' => 200));

	// notify user
	$ret[] = array_merge($event,
		array('category' => 'repo_event_notify_user', 'prio' => 100));

	// TODO: notify watchers

	return $ret;
}

/*
 * User notification
 */
function rg_repo_event_notify_user($db, $event)
{
	rg_prof_start("repo_event_notify_user");

	$ret = FALSE;
	while (1) {
		$r = rg_mail_template("mail/user/repo/" . $event['op'], $event);
		if ($r === FALSE)
			break;

		$ret = array();
		break;
	}

	rg_prof_end("repo_event_notify_user");
	return $ret;
}

/*
 * Inserts an event into repo_history table
 * TODO: we do not know yet if update-ref succeded.
 */
function rg_repo_history_insert($db, $event)
{
	rg_prof_start("repo_history_insert");
	rg_log_enter("repo_history_insert: event=" . rg_array2string($event));

	if (!isset($event['ui']['uid']))
		$event['ui']['uid'] = 0;

	$ret = FALSE;
	while (1) {
		$now = time();
		$params = array("now" => $now,
			"repo_id" => $event['ri']['repo_id'],
			"uid" => $event['ui']['uid'],
			"cat" => $event['history_category'],
			"mess" => $event['history_message']);
		$sql = "INSERT INTO repo_history_" . gmdate("Y_m", $now)
			. " (itime, uid, repo_id, category, message)"
			. " VALUES (@@now@@, @@uid@@, @@repo_id@@, @@cat@@, @@mess@@)";
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE)
			break;

		rg_sql_free_result($res);
		$ret = array();
		break;
	}

	rg_log_exit();
	rg_prof_end("repo_history_insert");
	return $ret;
}

/*
 * Event for pushing into a repo
 */
function rg_repo_event_push($db, $event)
{
	$ret = array();

	// TODO: we must be sure here that the update-ref finished with success
	// TODO: load references and check them?
	// 'post-update' hook knows if it succeeded or not. How to use it?

	// notify user
	if (0) { // TODO
	$x = $event;
	$x['category'] = 'repo_event_notify_user';
	$x['prio'] = 100;
	$x['notification'] .= "-notify";
	$ret[] = $x;
	}

	// TODO: notify watchers?

	// webhook
	$x = $event;
	$x['category'] = 'wh_send';
	$x['wh_event'] = 'P'; // push
	$ret[] = $x;

	// TODO: notify watchers of the user

	return $ret;
}

/*
 * Returns last events from repo_history
 * @category: 0 for any
 * @number: number of entries
 * @max_seconds: limit the search to less than max_seconds in the past
 */
function rg_repo_history_load($db, $repo_id, $category, $number, $max_seconds)
{
	rg_prof_start("repo_history_load");
	rg_log_enter("repo_history_load: repo_id=$repo_id, category=$category"
		. ", number=$number max_seconds=$max_seconds");

	$ret = FALSE;
	while (1) {
		$now = time();

		$category_sql = "";
		if ($category > 0)
			$category_sql = " AND category = " . $category;

		$limit_sql = "";
		if ($number > 0)
			$limit_sql = " LIMIT " . $number;
		else
			$limit_sql = " LIMIT 50";

		$time_sql = "";
		if ($max_seconds > 0)
			$time_sql = " AND itime >= " . ($now - $max_seconds);

		$sql = "SELECT * FROM repo_history"
			. " WHERE repo_id = $repo_id"
			. $category_sql
			. $time_sql
			. " ORDER BY itime DESC"
			. $limit_sql;
		$res = rg_sql_query($db, $sql);
		if ($res === FALSE)
			break;

		$ret = array();
		while (($row = rg_sql_fetch_array($res))) {
			$row['username'] = 'n/a';
			if ($row['uid'] > 0) {
				$ui = rg_user_info($db, $row['uid'], '', '');
				if ($ui['exists'] == 1)
					$row['username'] = $ui['username'];
			}

			if ($row['itime'] == 0)
				$row['itime_text'] = "N/A";
			else
				$row['itime_text'] = gmdate("Y-m-d H:i", $row['itime']);
			$ret[] = $row;
		}

		rg_sql_free_result($res);
		break;
	}

	rg_log_exit();
	rg_prof_end("repo_history_load");
	return $ret;
}

/*
 * Enforce name
 */
function rg_repo_ok($repo)
{
	global $rg_repo_allow;
	global $rg_repo_min_len;
	global $rg_repo_max_len;

	if (empty($repo)) {
		rg_repo_set_error("invalid repository name (empty)");
		return FALSE;
	}

	if (strcmp(substr($repo, 0, 1), '-') == 0) {
		rg_repo_set_error('invalid repository name'
			. ' (cannot start with minus)');
		return FALSE;
	}

	if (strpos($repo, ':') !== FALSE) {
		rg_repo_set_error('invalid repository name'
			. ' (cannot contain :)');
		return FALSE;
	}

	if (rg_chars_allow($repo, $rg_repo_allow, $invalid) !== 1) {
		rg_repo_set_error("invalid repository name"
			. " (invalid chars: '$invalid')");
		return FALSE;
	}

	if (preg_match('/\.\./', $repo) !== 0) {
		rg_repo_set_error("invalid repository name (..)");
		return FALSE;
	}

	$len = mb_strlen($repo);
	if ($len < $rg_repo_min_len) {
		rg_repo_set_error("repository name is too short"
			. " (minimum $rg_repo_min_len < $len)");
		return FALSE;
	}

	if ($len > $rg_repo_max_len) {
		rg_repo_set_error("repository name is too long"
			. " (maximum $rg_repo_max_len > $len)");
		return FALSE;
	}

	return TRUE;
}

/*
 * Returns the relative path to a repository based on id
 */
function rg_repo_path_by_id_rel($uid, $repo_id)
{
	return "../by_id/" . $repo_id . ".git";
}

/*
 * Returns the path to a repository based on id
 */
function rg_repo_path_by_id($uid, $repo_id)
{
	return rg_user_path_by_id($uid) . "/repos/by_id/" . $repo_id . ".git";
}

/*
 * Returns the path to a artifacts based on id
 */
function rg_repo_artifacts_path_by_id($uid, $repo_id)
{
	return rg_user_path_by_id($uid) . '/repos/by_id/' . $repo_id . '.a';
}

/*
 * Returns the path to a repository based on name
 */
function rg_repo_path_by_name($uid, $repo_name)
{
	return rg_user_path_by_id($uid) . "/repos/by_name/" . $repo_name . ".git";
}

/*
 * Delete a repo
 */
function rg_repo_delete($db, $repo_id, $ui)
{
	rg_prof_start("repo_delete");
	rg_log_enter("repo_delete: uid=" . $ui['uid'] . ", repo_id=$repo_id");

	$ret = FALSE;
	while (1) {
		// TODO: Transaction?

		$ri = rg_repo_info($db, $repo_id, 0, "");
		if ($ri['ok'] != 1)
			break;
		if ($ri['exists'] != 1) {
			rg_repo_set_error("Repository does not exists.");
			break;
		}

		// Only mark it as such, deletion will happen in background
		$params = array('repo_id' => $repo_id, 'now' => time());
		$sql = "UPDATE repos SET deleted = @@now@@"
			. " WHERE repo_id = @@repo_id@@";
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_repo_set_error("Cannot delete (" . rg_sql_error() . ")");
			break;
		}
		rg_sql_free_result($res);

		$event = array(
			'category' => 'repo_event_del',
			'prio' => 50,
			'ui' => $ui,
			'ri' => array(
				'name' => $ri['name'],
				'repo_id' => $repo_id
			)
		);
		$r = rg_event_add($db, $event);
		if ($r !== TRUE) {
			rg_repo_set_error("cannot add event"
				. " (" . rg_event_error() . ")");
			break;
		}
		rg_event_signal_daemon("", 0);

		rg_cache_unset('repo_by_id::' . $repo_id, RG_SOCKET_NO_WAIT);
		rg_cache_unset('repo_by_name::' . $ui['uid']
			. '::' . $ri['name'], RG_SOCKET_NO_WAIT);
		rg_cache_unset('user' . '::' . $ui['uid'] . '::' . 'has_repos',
			RG_SOCKET_NO_WAIT);

		$ret = TRUE;
		break;
	}

	rg_log_exit();
	rg_prof_end("repo_delete");
	return $ret;
}

/*
 * Lookup in db the old name, so we can redirect the user to the new name
 */
function rg_repo_lookup_by_old_name($db, $uid, $old_name)
{
	rg_prof_start("repo_lookup_by_old_name");
	rg_log_enter("repo_lookup_by_old_name: uid=$uid old_name=$old_name");

	$ret = FALSE;
	while (1) {
		$c = rg_cache_get("repo_by_old_name::$uid::$old_name");
		if ($c !== FALSE) {
			$ret = $c;
			break;
		}

		$params = array("uid" => $uid, "old_name" => $old_name);
		$sql = "SELECT repo_id FROM repos_renames"
			. " WHERE uid = @@uid@@"
			. " AND old_name = @@old_name@@";
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_repo_set_error("cannot lookup old name ("
				. rg_sql_error() . ")");
			break;
		}
		$rows = rg_sql_num_rows($res);
		if ($rows > 0)
			$row = rg_sql_fetch_array($res);
		rg_sql_free_result($res);
		if ($rows == 0)
			$ret = 0;
		else
			$ret = $row['repo_id'];

		rg_cache_set("repo_by_old_name::$uid::$old_name", $ret, RG_SOCKET_NO_WAIT);
		break;
	}

	rg_log_exit();
	rg_prof_end("repo_lookup_by_old_name");
	return $ret;
}

/*
 * Add a rename to the database
 */
function rg_repo_insert_rename($db, $uid, $repo_id, $old_name)
{
	rg_prof_start("repo_insert_rename");
	rg_log_enter("repo_insert_rename: uid=$uid repo_id=$repo_id old_name=$old_name");

	$ret = FALSE;
	while (1) {
		// Check if already exists in the database
		$r = rg_repo_lookup_by_old_name($db, $uid, $old_name);
		if ($r === FALSE)
			break;

		$params = array("repo_id" => $repo_id,
			"uid" => $uid,
			"old_name" => $old_name,
			"now" => time());

		if ($r > 0) {
			$sql = "UPDATE repos_renames"
				. " SET repo_id = @@repo_id@@"
				. " WHERE uid = @@uid@@"
				. " AND old_name = @@old_name@@";
		} else {
			$sql = "INSERT INTO repos_renames (uid, old_name"
				. ", repo_id, itime)"
				. " VALUES (@@uid@@, @@old_name@@, @@repo_id@@"
				. ", @@now@@)";
		}
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_repo_set_error("cannot link with old_name in db"
				. " (" . rg_sql_error() . ")");
			break;
		}
		rg_sql_free_result($res);

		rg_cache_set("repo_by_name::$uid::$old_name", $repo_id, RG_SOCKET_NO_WAIT);

		$ret = TRUE;
		break;
	}

	rg_log_exit();
	rg_prof_end("repo_insert_rename");
	return $ret;
}

/*
 * Creates/updates a repository
 * @login_ui - info of the user doing the update.
 * TODO: Warning, it may not be the owner.
 * TODO: where do we validate if the user has enough public/private slots?
 */
function rg_repo_edit($db, $login_ui, &$new)
{
	rg_prof_start("repo_edit");
	rg_log_enter("repo_edit: login_uid=" . $login_ui['uid']
		. " new=" . rg_array2string($new));

	// TODO: test if user is allowed to add a repository
	// TODO: test if user did not cross the limit for number of repos

	$ret = FALSE;
	while (1) {
		if (rg_repo_ok($new['name']) !== TRUE)
			break;

		if ($new['repo_id'] == 0) {
			// Check if name is already taken
			$ri = rg_repo_info($db, 0, $login_ui['uid'],
				$new['name']);
			if ($ri['ok'] != 1)
				break;
			if ($ri['exists'] == 1) {
				rg_repo_set_error('name already taken;'
					. ' choose a different one');
				break;
			}
		} else {
			// Test if repo_id is valid
			// TODO: also here we must test if we have a dup name!
			$ri = rg_repo_info($db, $new['repo_id'],
				$login_ui['uid'], "");
			if ($ri['ok'] != 1)
				break;
			if ($ri['exists'] != 1) {
				rg_repo_set_error('repo ' . $new['repo_id']
					. ' does not exists.');
				break;
			}
		}

		$renamed = 0;
		if ($new['repo_id'] > 0) {
			// Check if the user renamed the repo
			if (strcmp($new['name'], $ri['name']) != 0) {
				$renamed = 1;
				$r = rg_repo_insert_rename($db, $login_ui['uid'],
					$new['repo_id'], $ri['name']);
				if ($r !== TRUE)
					break;
			}
		}

		//TODO: master may be not accessible to this user. check.

		// Small fixes
		$new['itime'] = time();
		$new['uid'] = $login_ui['uid'];
		if (!isset($new['main_branch']))
			$new['main_branch'] = '';

		if ($new['repo_id'] == 0) {
			$new['deleted'] = 0;
			$new['disk_used_mb'] = 0;
			$new['git_dir_done'] = 0;
			$new['last_bug_id'] = 0;
			$new['last_mr_id'] = 0;

			$sql = "INSERT INTO repos (uid, master, name"
				. ", itime, max_commit_size, description"
				. ", git_dir_done, public, license, template"
				. ", main_branch)"
				. " VALUES (@@uid@@, @@master@@, @@name@@"
				. ", @@itime@@, @@max_commit_size@@"
				. ", @@description@@, 0, @@public@@"
				. ", @@license@@, @@template@@, @@main_branch@@)"
				. " RETURNING repo_id";
		} else {
			$sql = "UPDATE repos SET name = @@name@@"
				. ", max_commit_size = @@max_commit_size@@"
				. ", description = @@description@@"
				. ", public = @@public@@"
				. ", license = @@license@@"
				. ", template = @@template@@"
				. ", main_branch = @@main_branch@@"
				. " WHERE repo_id = @@repo_id@@";
		}
		$res = rg_sql_query_params($db, $sql, $new);
		if ($res === FALSE) {
			rg_repo_set_error("cannot update: " . rg_sql_error());
			break;
		}
		if ($new['repo_id'] == 0) {
			$row = rg_sql_fetch_array($res);
			if ($row === FALSE) {
				rg_repo_set_error('cannot fetch row');
				break;
			}
		}
		rg_sql_free_result($res);

		if ($new['repo_id'] == 0) {
			$cat = 'repo_event_new';
			$hcat = REPO_CAT_CREATE;
			$hmess = "Repository has been created";
			$notification = "repo_create-" . $login_ui['uid']
				. "-" . $row['repo_id'];
			$old_description = "";
			$new['repo_id'] = $row['repo_id'];
		} else {
			$cat = 'repo_event_update';
			$hcat = REPO_CAT_UPDATE;
			$hmess = "Repository has been updated";
			$notification = "";
			$old_description = $ri['description'];
			// TODO: we should log what changed
		}

		$event = array(
			'category' => $cat,
			'prio' => 50,
			'notification' => $notification,
			'ui' => $login_ui,
			'history_category' => $hcat,
			'history_message' => $hmess);
		$event = rg_array_merge($event, 'ri_old', $ri);
		$new['url'] = rg_base_url()
			. rg_re_repopage($login_ui, $new['name']);
		$event = rg_array_merge($event, 'ri', $new);
		$event['ri_old']['description_md5'] = md5($old_description);
		$event['ri']['description_md5'] = md5($new['description']);
		$r = rg_event_add($db, $event);
		if ($r !== TRUE) {
			rg_repo_set_error("cannot add event"
				. " (" . rg_event_error() . ")");
			break;
		}

		$new['ok'] = 1;
		$new['exists'] = 1;
		rg_cache_merge("repo_by_id::" . $new['repo_id'], $new,
			RG_SOCKET_NO_WAIT);
		rg_cache_set("repo_by_name::" . $login_ui['uid'] . "::"
			. $new['name'], $new['repo_id'], RG_SOCKET_NO_WAIT);

		rg_event_signal_daemon('', 0);

		$ret = TRUE;
		break;
	}

	rg_log_exit();
	rg_prof_end("repo_edit");
	return $ret;
}

/*
 * Returns 1 if user has at least one repo
 * Used to go directly to 'create' page instead to 'list'
 */
function rg_repo_have($db, $uid)
{
	rg_prof_start('repo_have');

	$ret = 1;
	while (1) {
		$key = 'user' . '::' . $uid . '::' . 'has_repos';
		$c = rg_cache_get($key);
		if ($c !== FALSE) {
			$ret = $c;
			break;
		}

		$params = array('uid' => $uid);
		$sql = 'SELECT 1 FROM repos WHERE uid = @@uid@@ LIMIT 1';
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE)
			break;

		$rows = rg_sql_num_rows($res);
		rg_sql_free_result($res);

		$ret = $rows;
		rg_cache_set($key, $ret, RG_SOCKET_NO_WAIT);
		break;
	}

	rg_prof_end('repo_have');
	return $ret;
}

/*
 * List repositories
 */
function rg_repo_list_query($db, $url, $sql, $params, $login_ui)
{
	rg_prof_start("repo_list_query");
	rg_log("repo_list_query: url=$url, sql=$sql...");

	$res = rg_sql_query_params($db, $sql, $params);
	if ($res === FALSE) {
		rg_repo_set_error("cannot list by query (" . rg_sql_error() . ")");
		return FALSE;
	}

	$d = array();
	while (($row = rg_sql_fetch_array($res))) {
		$_line = array();
		foreach ($row as $k => $v)
			$_line[$k] = $v;

		$_ui = rg_user_info($db, $row['uid'], "", "");
		if ($_ui['exists'] != 1) {
			rg_log("uid " . $row['uid']
				. " associated with this repo not found");
			continue;
		}

		rg_repo_cosmetic($db, $_line);

		$d[] = $_line;
	}
	rg_sql_free_result($res);

	rg_prof_end("repo_list_query");
	return rg_template_table("repo/list", $d, $login_ui);
}

/*
 * List repos of page user 'ui'.
 * @uid - owner of the to be listed repos
 */
function rg_repo_list($db, $rg, $url, $uid, $limit)
{
	rg_log("repo_list: url=$url uid=" . $uid
		. " login_uid=" . $rg['login_ui']['uid']);

	$params = array(
		"uid" => $uid,
		"login_uid" => $rg['login_ui']['uid']);

	if ($uid > 0)
		$add = ' AND uid = @@uid@@';
	else
		$add = ' AND uid != @@login_uid@@';

	// TODO: also admin must be able to see them?
	if (($rg['login_ui']['uid'] == 0)
		|| ($rg['login_ui']['uid'] != $uid))
		$add .= " AND public = 1";

	if ($limit > 0)
		$add_limit = ' LIMIT ' . $limit;
	else
		$add_limit = '';

	$sql = 'SELECT * FROM repos'
		. ' WHERE deleted = 0'
		. $add
		. ' ORDER BY name'
		. $add_limit;

	return rg_repo_list_query($db, $url, $sql, $params, $rg['login_ui']);
}

/*
 * Search in all repositories owned by 'login_ui' or public
 * We need to exclude private repositories.
 */
function rg_repo_search($db, $login_ui, $q)
{
	rg_prof_start("repo_search");
	rg_log("repo_search: q=[$q]");

	$admin = 0;
	if (isset($login_ui['admin']) && ($login_ui['admin'] == 1))
		$admin = 1;

	$params = array("q" => "%" . $q . "%",
		"uid" => $login_ui['uid']);

	$sql = "SELECT * FROM repos"
		. " WHERE deleted = 0"
		. " AND (uid = @@uid@@ OR public = 1 OR " . $admin . " = 1)"
		. " AND (name ILIKE @@q@@ OR description ILIKE @@q@@)"
		. " ORDER BY master, name"
		. " LIMIT 50";
	$r = rg_repo_list_query($db, "", $sql, $params, $login_ui);

	rg_prof_end("repo_search");
	return $r;
}

/*
 * Computes the sizes of a repository
 */
function rg_repo_size($uid, $repo_id)
{
	$git_path = rg_repo_path_by_id($uid, $repo_id);
	$git = rg_dir_size($git_path);
	if ($git === FALSE)
		return FALSE;
	//rg_log_ml('DEBUG: XXX: git:' . print_r($git, TRUE));
	$git_size = intval($git['blocks'] / 2 / 1024);

	$artifact_path = rg_repo_artifacts_path_by_id($uid, $repo_id);
	$a = rg_dir_size($artifact_path);
	if ($a === FALSE)
		return FALSE;
	//rg_log_ml('DEBUG: XXX: a:' . print_r($a, TRUE));
	$a_size = intval($a['blocks'] / 2 / 1024);

	return array(
		'disk_used_mb' => $git_size + $a_size,
		'git_mb' => $git_size,
		'artifacts_mb' => $a_size
	);
}

/*
 * Mark a git repo as done
 */
function rg_repo_git_done($db, $repo_id)
{
	rg_prof_start("repo_git_done");
	rg_log_enter("repo_git_done: repo_id=$repo_id...");

	$ret = FALSE;
	while (1) {
		$params = array("repo_id" => $repo_id);

		rg_cache_merge('repo_by_id::' . $repo_id,
			array('git_dir_done' => 1), RG_SOCKET_NO_WAIT);

		$sql = "UPDATE repos SET git_dir_done = 1"
			. " WHERE repo_id = @@repo_id@@";
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_repo_set_error("Cannot query (" . rg_sql_error() . ")");
			break;
		}
		rg_sql_free_result($res);

		$ret = TRUE;
		break;
	}

	rg_log_exit();
	rg_prof_end("repo_git_done");
	return $ret;
}

/*
 * Returns the status of a repo
 */
function rg_repo_lock_status($db, $repo_id)
{
	rg_prof_start('repo_lock_status');
	rg_log_enter('repo_lock_status');

	$ret = array();
	$ret['ok'] = 0;
	while (1) {
		$key = 'locks' . '::' . 'repos' . '::' . $repo_id;
		$c = rg_cache_get($key);
		if ($c !== FALSE) {
			$ret = $c;
			$ret['ok'] = 1;
			break;
		}

		$params = array('repo_id' => $repo_id);
		$sql = 'SELECT * FROM repo_locks'
			. ' WHERE repo_id = @@repo_id@@';
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_repo_set_error('cannot get lock status');
			break;
		}
		$rows = rg_sql_num_rows($res);
		if ($rows == 0) {
			$ret['status'] = 0;
		} else {
			$ret = rg_sql_fetch_array($res);
			$ret['status'] = 1;
		}
		rg_sql_free_result($res);

		$v = $ret;
		unset($v['ok']);
		rg_cache_set($key, $v, RG_SOCKET_NO_WAIT);

		$ret['ok'] = 1;
		break;
	}

	rg_log_exit();
	rg_prof_end('repo_lock_status');
	return $ret;
}

/*
 * Locks/unlocks a repo
 * @lock: 1 = lock, 0 = unlock
 */
function rg_repo_lock($db, $repo_id, $uid, $lock, $reason)
{
	rg_prof_start('repo_lock');
	rg_log_enter('repo_lock');

	$ret = FALSE;
	while (1) {
		$si = rg_repo_lock_status($db, $repo_id);
		if ($si['ok'] != 1)
			break;

		// If we already are in the same state as the passed one
		if ($si['status'] == $lock) {
			$ret = TRUE;
			break;
		}

		$params = array(
			'itime' => time(),
			'repo_id' => $repo_id,
			'uid' => $uid,
			'reason' => $reason);
		if ($lock == 1)
			$sql = 'INSERT INTO repo_locks (repo_id, uid, itime)'
				. ' VALUES (@@repo_id@@, @@uid@@, @@itime@@)';
		else
			$sql = 'DELETE FROM repo_locks'
				. ' WHERE repo_id = @@repo_id@@';
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_repo_set_error('db lock/unlock error');
			break;
		}
		rg_sql_free_result($res);

		$key = 'locks' . '::' . 'repos' . '::' . $repo_id;
		if ($lock == 1) {
			$params['status'] = 1;
			rg_cache_set($key, $params, RG_SOCKET_NO_WAIT);
		} else {
			rg_cache_unset($key, RG_SOCKET_NO_WAIT);
		}

		$h = array('ri' => array(), 'ui' => array());
		$h['ri']['repo_id'] = $repo_id;
		$h['ui']['uid'] = $uid;
		$h['history_category'] = $lock == 1 ? REPO_CAT_LOCK : REPO_CAT_UNLOCK;
		$h['history_message'] = 'Repository ' .
			($lock == 1 ? 'locked' : 'unlocked') . ': ' . $reason;
		rg_repo_history_insert($db, $h);

		$ret = TRUE;
		break;
	}

	rg_log_exit();
	rg_prof_end('repo_lock');
	return $ret;
}

/*
 * Loads data for graphs
 * @unit - interval on which a sum is made
 */
function rg_repo_data($db, $type, $start, $end, $unit, $mode)
{
	$params = array('start' => $start, 'end' => $end);
	switch ($type) {
	case 'create_repo':
		$q = 'SELECT 1 AS value, itime FROM repos'
			. ' WHERE itime >= @@start@@ AND itime <= @@end@@';
		break;
	case 'create_bug':
		$q = 'SELECT 1 AS value, itime FROM bugs'
			. ' WHERE itime >= @@start@@ AND itime <= @@end@@';
		break;
	case 'history':
		$q = 'SELECT 1 AS value, itime FROM repo_history'
			. ' WHERE itime >= @@start@@ AND itime <= @@end@@';
		break;
	default:
		rg_internal_error('invalid type');
		return FALSE;
	}

	$ret = rg_graph_query($db, $start, $end, $unit, $mode, $q, $params, '');
	if ($ret === FALSE)
		rg_repo_set_error(rg_graph_error());

	return $ret;
}

/*
 * High level function for Repo -> Admin -> Rights -> Repo/Refs/Path rights menu.
 */
function rg_repo_admin_rights($db, $rg, $type)
{
	rg_log("rg_repo_admin_rights type=$type");

	/* 'repo' is correct here, we test for granting rights on repo */
	$x = array();
	$x['obj_id'] = $rg['ri']['repo_id'];
	$x['type'] = 'repo';
	$x['owner'] = $rg['ri']['uid'];
	$x['uid'] = $rg['login_ui']['uid'];
	$x['username'] = $rg['login_ui']['username'];
	$x['needed_rights'] = 'G';
	$x['ip'] = $rg['ip'];
	$x['misc'] = "";
	if (rg_rights_allow($db, $x) !== TRUE)
		return rg_template("user/repo/rights/deny.html", $rg, TRUE /* xss */);

	$ret = "";

	$a = array();
	$a['type'] = $type;
	$a['right_id'] = rg_var_uint("right_id");
	$a['edit_id'] = rg_var_uint("edit_id");
	$a['username'] = trim(rg_var_str("username"));
	$a['rights'] = rg_rights_a2s(rg_var_str("rights"));
	$a['misc'] = trim(rg_var_str("misc"));
	$a['ip'] = trim(rg_var_str("ip"));
	$a['prio'] = rg_var_uint("prio");
	$a['description'] = trim(rg_var_str("description"));
	//rg_log_ml("CHECK: a(POST)=" . print_r($a, TRUE));

	$errmsg = array();
	$list_errmsg = array();

	$load_defaults = 1;

	$delete = rg_var_bool("delete");
	while ($delete == 1) {
		if (!rg_valid_referer()) {
			$errmsg[] = "invalid referer; try again";
			break;
		}

		if (!rg_token_valid($db, $rg, 'repo_admin_rights', FALSE)) {
			$errmsg[] = "invalid token; try again";
			break;
		}

		$list = rg_var_uint("rights_delete_ids");
		if (empty($list)) {
			$list_errmsg[] = "please select at least one item";
			break;
		}

		$my_list = array();
		foreach ($list as $k => $junk)
			$my_list[] = $k;

		$r = rg_rights_delete_list($db, $type, $rg['ri']['repo_id'], $my_list);
		if ($r !== TRUE) {
			$list_errmsg[] = "cannot delete rights: " . rg_rights_error();
			break;
		}

		$ret .= rg_template("user/repo/rights/delete_ok.html", $rg, TRUE /* xss */);
		break;
	}

	// edit
	while ($a['edit_id'] > 0) {
		$owner = $rg['ri']['uid'];
		$r = rg_rights_get($db, $rg['ri']['repo_id'], $type,
			$owner, -1, $a['edit_id']);
		if ($r['ok'] != 1) {
			$list_errmsg[] = "cannot load rights: " . rg_rights_error();
			break;
		}

		if (empty($r['list'])) {
			$list_errmsg[] = "right not found";
			break;
		}

		// Only one right is returned when we edit by right_id
		$a = $r['list'][0];

		$load_defaults = 0;
		break;
	}

	$grant = rg_var_bool("grant");
	while ($grant == 1) {
		$load_defaults = 0;

		if (!rg_valid_referer()) {
			$errmsg[] = "invalid referer; try again";
			break;
		}

		if (!rg_token_valid($db, $rg, 'repo_admin_rights', FALSE)) {
			$errmsg[] = "invalid token; try again";
			break;
		}

		if (strcmp($a['username'], '*') == 0) {
			$a['uid'] = 0;
		} else {
			$_ui = rg_user_info($db, 0, $a['username'], "");
			if ($_ui['exists'] == 1) {
				$a['uid'] = $_ui['uid'];
			} else {
				$errmsg[] = rg_user_error();
				break;
			}
		}

		$a['obj_id'] = $rg['ri']['repo_id'];
		$a['who'] = $rg['login_ui']['uid'];
		$r = rg_rights_set($db, $type, $a);
		if ($r !== TRUE) {
			$errmsg[] = rg_rights_error();
			break;
		}

		$ret .= rg_template("user/repo/rights/grant_ok.html", $rg, TRUE /* xss */);

		$load_defaults = 1;
		break;
	}

	if ($load_defaults == 1) {
		$rg['right_id'] = 0;
		$rg['username'] = "";
		$rg['rights'] = rg_rights_default($type);
		$rg['description'] = "";
		$rg['misc'] = "";
		$rg['ip'] = "";
		$rg['prio'] = 100;
	} else {
		$rg = rg_array_merge($rg, '', $a);
	}

	$rg['rg_form_token'] = rg_token_get($db, $rg, 'repo_admin_rights');
	$rg['HTML:errmsg'] = rg_template_errmsg($errmsg);
	$rg['HTML:list_errmsg'] = rg_template_errmsg($list_errmsg);
	$rg['HTML:rights_checkboxes'] = rg_rights_checkboxes($type, "rights",
		$rg['rights']);

	// list rights
	$r = rg_rights_get($db, $rg['ri']['repo_id'], $type, $rg['ri']['uid'],
		-1, 0);
	if ($r['ok'] != 1)
		$ret .= rg_warning("Cannot load rights. Try later.");
	else
		$ret .= rg_template_table("user/repo/rights/list_" . $type,
			$r['list'], $rg);

	$ret .= rg_template("user/repo/rights/form_" . $type . ".html", $rg, TRUE /* xss */);

	// hints
	$hints = array();
	$hints[]['HTML:hint'] = rg_template("hints/repo/edit_rights.html",
		$rg, TRUE /* xss */);
	$hints[]['HTML:hint'] = rg_template("hints/repo/edit_"
		. $type . "_rights.html", $rg, TRUE /* xss */);
	$ret .= rg_template_table("hints/list", $hints, $rg);

	return $ret;
}

/*
 * High level function for repo deletion
 */
function rg_repo_admin_delete($db, $rg)
{
	$ret = "";

	$x = array();
	$x['obj_id'] = $rg['ri']['repo_id'];
	$x['type'] = 'repo';
	$x['owner'] = $rg['ri']['uid'];
	$x['uid'] = $rg['login_ui']['uid'];
	$x['username'] = $rg['login_ui']['username'];
	$x['needed_rights'] = 'D';
	$x['ip'] = $rg['ip'];
	$x['misc'] = "";
	if (rg_rights_allow($db, $x) !== TRUE)
		return rg_template("user/repo/delete/deny.html", $rg, TRUE /* xss */);

	$are_you_sure = rg_var_uint("are_you_sure");

	$errmsg = array();

	$show_form = 1;

	while (1) {
		if ($rg['doit'] != 1)
			break;

		if ($are_you_sure == 0) {
			$ret .= rg_template("user/repo/delete/no.html", $rg, TRUE /* xss */);
			$show_form = 0;
			break;
		}

		if (!rg_valid_referer()) {
			$errmsg[] = "invalid referer; try again";
			break;
		}

		if (!rg_token_valid($db, $rg, 'repo_admin_delete', FALSE)) {
			$errmsg[] = "invalid token; try again";
			break;
		}

		rg_log_ml("CHECK: rg: " . print_r($rg, TRUE));
		$r = rg_repo_delete($db, $rg['ri']['repo_id'], $rg['login_ui']);
		if ($r === FALSE) {
			$errmsg[] = rg_repo_error();
			break;
		}

		$ret .= rg_template("user/repo/delete/done.html", $rg, TRUE /* xss */);
		$show_form = 0;

		// TODO: shouldn't we invalidate the cache?
		break;
	}

	if ($show_form == 1) {
		$rg['HTML:errmsg'] = rg_template_errmsg($errmsg);
		$rg['rg_form_token'] = rg_token_get($db, $rg, 'repo_admin_delete');
		$ret .= rg_template("user/repo/delete/sure.html", $rg, TRUE /* xss */);
	}

	return $ret;
}

/*
 * Easy obtain repo name
 */
function rg_repo_nice($db, $repo_id)
{
	$ri = rg_repo_info($db, $repo_id, 0, '');
	if ($ri['exists'] == 1)
		return $ri['name'];

	return 'n/a';
}

/*
 * High level function creating/editing a repo
 */
function rg_repo_edit_high_level($db, &$rg)
{
	rg_log_enter("rg_repo_edit_high_level");

	$ret = "";

	$errmsg = array();
	$load_form = TRUE;
	while (1) {
		if ($rg['ri']['repo_id'] > 0)
			$edit = TRUE;
		else
			$edit = FALSE;

		// User is not logged in?
		if (!$edit && ($rg['login_ui']['uid'] == 0)) {
			$ret .= rg_template("user/repo/deny_create.html", $rg, TRUE /* xss */);
			$load_form = FALSE;
			break;
		}

		if ($edit) {
			$x = array();
			$x['obj_id'] = $rg['ri']['repo_id'];
			$x['type'] = 'repo';
			$x['owner'] = $rg['ri']['uid'];
			$x['uid'] = $rg['login_ui']['uid'];
			$x['username'] = $rg['login_ui']['username'];
			$x['needed_rights'] = 'E'; // E = create/edit
			$x['ip'] = $rg['ip'];
			$x['misc'] = "";
			if (rg_rights_allow($db, $x) !== TRUE) {
				$ret .= rg_template("user/repo/deny_edit.html", $rg, TRUE /* xss */);
				$load_form = FALSE;
				break;
			}
		}

		if ($rg['doit'] != 1) {
			if (!$edit) {
				// Defaults
				$rg['ri'] = array();
				$rg['ri']['exists'] = 0;
				$rg['ri']['repo_id'] = '0';
				$rg['ri']['master'] = '0';
				$rg['ri']['name'] = '';
				$rg['ri']['max_commit_size'] = '0';
				$rg['ri']['description'] = '';
				$rg['ri']['public'] = '1';
				$rg['ri']['license'] = '';
				$rg['ri']['template'] =
					'OS type(s) and version(s)?' . "\n\n"
					. 'Application version(s) affected?' . "\n\n"
					. 'Steps to reproduce?';
				$rg['ri']['main_branch'] = '';
			}
			break;
		}

		$rg['ri']['repo_id'] = rg_var_uint('repo_id');
		$rg['ri']['master'] = rg_var_uint('master');
		$rg['ri']['name'] = trim(rg_var_str_nocr('name')); // TODO: filter name!
		$rg['ri']['max_commit_size'] = rg_var_uint('max_commit_size');
		$rg['ri']['description'] = trim(rg_var_str('description'));
		$rg['ri']['public'] = rg_var_bool('public');
		$rg['ri']['license'] = trim(rg_var_str('license'));
		$rg['ri']['template'] = trim(rg_var_str('template'));
		$rg['ri']['main_branch'] = trim(rg_var_str_nocr('main_branch'));
		rg_repo_cosmetic($db, $rg['ri']);
		//rg_log_ml("CHECK: after repo edit: rg[ri]=" . print_r($rg['ri'], TRUE));

		if (!rg_valid_referer()) {
			$errmsg[] = "invalid referer; try again";
			break;
		}

		if (!rg_token_valid($db, $rg, 'repo_edit_hl', FALSE)) {
			// TODO: replace all of these with a template
			$errmsg[] = "invalid token; try again.";
			break;
		}

		$r = rg_repo_edit($db, $rg['login_ui'], $rg['ri']);
		if ($r === FALSE) {
			$errmsg[] = rg_repo_error();
			break;
		}

		$rg['ri']['home'] = rg_re_repopage($rg['login_ui'],
			$rg['ri']['name']);
		if ($edit) {
			$ret .= rg_template("repo/edit_ok.html", $rg, TRUE /*xss*/);
		} else {
			$ret .= rg_template("repo/create_ok.html", $rg, TRUE /*xss*/);
		}

		$load_form = FALSE;
		break;
	}

	if ($load_form) {
		if ($rg['ri']['master'] > 0) {
			$rg['ri']['master_name'] = $rg['ri']['master'];
			$_mi = rg_repo_info($db, $rg['ri']['master'], "");
			if ($_mi['exists'] == 1)
				$rg['ri']['master_name'] = $_mi['name'];
		} else {
			$rg['ri']['master_name'] = "";
		}

		$rg['HTML:errmsg'] = rg_template_errmsg($errmsg);
		$rg['rg_form_token'] = rg_token_get($db, $rg, 'repo_edit_hl');
		$hints = array();
		$hints[]['HTML:hint'] = rg_template("hints/repo/create_repo.html", $rg, TRUE /* xss */);
		$rg['HTML:repo_edit_hints'] = rg_template_table("hints/list", $hints, $rg);
		$ret .= rg_template("repo/add_edit.html", $rg, TRUE /* xss */);
	}

	rg_log_exit();
	return $ret;
}

/*
 * High level function for locking a repo
 */
function rg_repo_lock_high_level($db, &$rg)
{
	rg_log_enter("rg_repo_lock_high_level");

	$ret = "";

	$errmsg = array();
	$show_form = TRUE;
	while (1) {
		$x = array();
		$x['obj_id'] = $rg['ri']['repo_id'];
		$x['type'] = 'repo';
		$x['owner'] = $rg['ri']['uid'];
		$x['uid'] = $rg['login_ui']['uid'];
		$x['username'] = $rg['login_ui']['username'];
		$x['needed_rights'] = 'K';
		$x['ip'] = $rg['ip'];
		$x['misc'] = '';
		if (rg_rights_allow($db, $x) !== TRUE) {
			$ret .= rg_template("user/repo/deny_lock.html",
				$rg, TRUE /*xss*/);
			$show_form = FALSE;
			break;
		}

		$ls = rg_repo_lock_status($db, $rg['ri']['repo_id']);
		if ($ls['ok'] != 1)
			break;

		$rg['ri']['next_status'] = 1 - $ls['status'];
		$rg['ri']['reason'] = rg_var_str('ri::reason');
		$lock = rg_var_int('ri::lock');
		rg_log('ZZZ: status=' . $ls['status']);
		rg_log('ZZZ: lock=' . $lock);

		if ($rg['doit'] != 1)
			break;

		if (!rg_valid_referer()) {
			$errmsg[] = "invalid referer; try again";
			break;
		}

		if (!rg_token_valid($db, $rg, 'repo_lock_hl', FALSE)) {
			// TODO: replace all of these with a template
			$errmsg[] = "invalid token; try again.";
			break;
		}

		$r = rg_repo_lock($db, $rg['ri']['repo_id'],
			$rg['login_ui']['uid'], $lock, $rg['ri']['reason']);
		if ($r === FALSE) {
			$errmsg[] = rg_repo_error();
			break;
		}

		if ($lock == 1)
			$t = 'repo/lock_ok.html';
		else
			$t = 'repo/unlock_ok.html';
		$ret .= rg_template($t, $rg, TRUE /*xss*/);

		// clean form
		$rg['ri']['reason'] = '';
		$rg['ri']['next_status'] = 1 - $lock;
		break;
	}

	if ($show_form) {
		$rg['HTML:errmsg'] = rg_template_errmsg($errmsg);

		$hints = array();
		$hints[]['HTML:hint'] = rg_template("hints/repo/lock_repo.html",
			$rg, TRUE /*xss*/);
		$rg['HTML:repo_lock_hints'] = rg_template_table("hints/list",
			$hints, $rg);

		$rg['rg_form_token'] = rg_token_get($db, $rg, 'repo_lock_hl');
		$ret .= rg_template("repo/lock.html", $rg, TRUE /*xss*/);
	}

	rg_log_exit();
	return $ret;
}

/*
 * High level function for 'Repo -> Admin' menu
 */
function rg_repo_admin($db, &$rg, $paras)
{
	rg_log("rg_repo_admin paras=" . rg_array2string($paras));

	$ret = "";

	$_op = empty($paras) ? "edit" : array_shift($paras);

	$rg['allow_edit_repo'] = 0;
	$rg['allow_lock_repo'] = 0;
	$rg['allow_grant_rights'] = 0;
	$rg['allow_delete_repo'] = 0;

	$x = array();
	$x['obj_id'] = $rg['ri']['repo_id'];
	$x['type'] = 'repo';
	$x['owner'] = $rg['ri']['uid'];
	$x['uid'] = $rg['login_ui']['uid'];
	$x['username'] = $rg['login_ui']['username'];
	$x['ip'] = $rg['ip'];
	$x['misc'] = '';

	$x['needed_rights'] = 'E';
	if (rg_rights_allow($db, $x) === TRUE)
		$rg['allow_edit_repo'] = 1;
	$x['needed_rights'] = 'K';
	if (rg_rights_allow($db, $x) === TRUE)
		$rg['allow_lock_repo'] = 1;
	$x['needed_rights'] = 'G';
	if (rg_rights_allow($db, $x) === TRUE)
		$rg['allow_grant_rights'] = 1;
	$x['needed_rights'] = 'D';
	if (rg_rights_allow($db, $x) === TRUE)
		$rg['allow_delete_repo'] = 1;

	$rg['menu']['repo'][$_op] = 1;
	$rg['HTML:menu_repo_level2'] = rg_template("user/repo/menu.html", $rg, TRUE /* xss */);

	switch ($_op) {
	case 'repo_rights':
		$ret .= rg_repo_admin_rights($db, $rg, "repo");
		break;

	case 'refs_rights':
		$ret .= rg_repo_admin_rights($db, $rg, "repo_refs");
		break;

	case 'path_rights':
		$ret .= rg_repo_admin_rights($db, $rg, "repo_path");
		break;

	case 'delete':
		$ret .= rg_repo_admin_delete($db, $rg);
		break;

	case 'lock':
		$rg['form_url'] = $rg['ri']['url_repo'] . '/admin/lock';
		$ret .= rg_repo_lock_high_level($db, $rg);
		break;

	default:
		$rg['form_url'] = $rg['ri']['url_repo'] . '/admin';
		$ret .= rg_repo_edit_high_level($db, $rg);
		break;
	}

	return $ret;
}

/*
 * HL function for repo search
 */
function rg_repo_search_high_level($db, $rg, $ui, $url)
{
	$q = trim(rg_var_str('q'));

	$ret = '';

	$errmsg = array();

	$rg['q'] = $q;
	$rg['HTML:errmsg'] = rg_template_errmsg($errmsg);
	$rg['search_url'] = $url;
	$ret .= rg_template("repo/search.html", $rg, TRUE /* xss */);

	while ($rg['doit'] == 1) {
		$_t = rg_repo_search($db, $ui, $q);
		if ($_t === FALSE) {
			$errmsg[] = rg_repo_error();
			break;
		}

		$ret .= $_t;
		break;
	}

	return $ret;
}

/*
 * Discover top menu helper
 * @ui - acts like login_ui
 */
function rg_repo_discover($db, $op, $rg, $ui)
{
	rg_log("repo_discover: op=$op");

	$ret = '';

	if (empty($op))
		$op = 'list';

	switch ($op) {
	case 'search':
		$x = rg_repo_search_high_level($db, $rg, $ui, '/op/discover');
		break;

	default:
		$x = rg_repo_list($db, $rg, '', $ui['uid'], 100);
		if ($x === FALSE)
			$x = rg_template('internal_err.html', $rg, TRUE /*xss*/);
		break;
	}

	$rg['discover_menu_' . $op] = 1;
	$ret .= rg_template('repo/discover.html', $rg, TRUE /*xss*/);
	$ret .= '<div class="generic_body">' . "\n";
	$ret .= $x;
	$ret .= '</div>' . "\n";

	return $ret;
}

/*
 * Show some stats about a repo
 */
function rg_repo_stats($rg)
{
	rg_log_enter('repo_start');

	while (1) {
		if ($rg['ri']['git_dir_done'] == 0) {
			$ret = rg_template('repo/err/no_git_dir.html', $rg, TRUE /*xss*/);
			break;
		}

		$path = rg_repo_path_by_id($rg['ri']['uid'], $rg['ri']['repo_id']);

		$r = rg_git_repo_is_empty($path);
		if ($r == -1) {
			$ret = rg_template('internal_err.html', $rg, TRUE /*xss*/);
			break;
		}
		if ($r == 1) {
			$ret = rg_template('repo/err/not_init.html', $rg, TRUE /*xss*/);
			break;
		}

		$log = rg_git_log($path, 0 /*max*/, '', '', FALSE /*also_patch*/,
			0 /*patch_limit*/);
		if ($log === FALSE) {
			$ret = rg_template('internal_err.html', $rg, TRUE /*xss*/);
			break;
		}

		$s = rg_git_stats($log);
		//rg_log_ml('DEBUG: stats: ' . print_r($s, TRUE));
		$rg['ri']['stats'] = $s['global'];

		$rg['ri']['HTML:authors'] = rg_template_table('repo/stats/authors',
			$s['authors'], $rg);

		$ret = rg_template('repo/stats/stats.html', $rg, TRUE /*xss*/);
		break;
	}

	rg_log_exit();
	return $ret;
}

/*************************** commit_labels stuff */

/*
 * Returns a list of labels attached to a commit
 */
function rg_repo_commit_labels($db, $repo_id, $head)
{
	rg_prof_start('repo_commit_labels');
	rg_log_enter('repo_commit_labels: repo_id='
		. $repo_id . ' head=' . $head);

	$ret = array();
	$ret['ok'] = 0;
	while(1) {
		$key = 'repo_commit_labels' . '::' . $repo_id . '::' . $head;
		$c = rg_cache_get($key);
		if ($c !== FALSE) {
			$ret = $c;
			//rg_repo_cosmetic($db, $ret);
			break;
		}

		$params = array(
			'repo_id' => $repo_id,
			'head' => $head
		);
		$sql = 'SELECT * FROM commit_labels'
			. ' WHERE repo_id = @@repo_id@@'
			. ' AND head = @@head@@';
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_repo_set_error('cannot query');
			break;
		}
		$rows = rg_sql_num_rows($res);
		$ret['list'] = array();
		while (($row = rg_sql_fetch_array($res))) {
			// Cosmetic
			$row['labels'] = rg_unserialize($row['labels']);
			$row['itime_nice'] = gmdate('Y-m-d H:i', $row['itime']);
			$ret['list'][] = $row;
		}
		rg_sql_free_result($res);

		$ret['ok'] = 1;

		rg_cache_set($key, $ret, RG_SOCKET_NO_WAIT);
		break;
	}

	//rg_log_ml('DEBUG: commit_labels: ' . print_r($ret, TRUE));

	rg_log_exit();
	rg_prof_end('repo_commit_labels');
	return $ret;
}

/*
 * Show commit labels as nice html
 */
function rg_repo_commit_labels_html($db, $repo_id, $commit)
{
	rg_log_enter('repo_commit_labels_html');

	$ret = '';
	while (1) {
		if (empty($commit))
			break;

		$rg = array();

		$r = rg_repo_commit_labels($db, $repo_id, $commit);
		//rg_log_ml('DEBUG: r: ' . print_r($r, TRUE));
		if ($r['ok'] != 1) {
			$ret = rg_template('repo/cl/ierror.html',
				$rg, TRUE /*xss*/);
			break;
		}

		if (empty($r['list']))
			break;

		foreach ($r['list'] as $index => &$i)
			$i['labels_nice'] = implode(' ', $i['labels']);

		$ret = rg_template_table('repo/cl/list', $r['list'], $rg);
		break;
	}

	rg_log_exit();
	return $ret;
}

/*
 * API dispatch function
 */
function rg_repo_api($db, $a, $json)
{
	rg_prof_start('repo_api');
	rg_log_enter('repo_api');

	$cmd = $json['cmd'];

	$ret = array();
	while (1) {
		if (isset($json['repo'])) {
			$ri = rg_repo_info($db, 0, $a['target_ui']['uid'],
				$json['repo']);
		} else if (isset($json['repo_id'])) {
			$repo_id = intval($json['repo_id']);
			$ri = rg_repo_info($db, $repo_id, '', '');
		} else {
			$ri = FALSE;
		}

		if (strcmp($cmd, 'repo_list') == 0) {
			$params = array('uid' => $a['target_ui']['uid']);
			$sql = 'SELECT name FROM repos'
				. ' WHERE uid = @@uid@@';
			$res = rg_sql_query_params($db, $sql, $params);
			if ($res === FALSE) {
				$ret['error'] = 'internal error';
				break;
			}
			$ret = array();
			while (($row = rg_sql_fetch_array($res))) {
				$ret[] = $row['name'];
			}
			rg_sql_free_result($res);
			break;
		}

		if ($ri === FALSE) {
			$ret['error'] = 'no repo_id=/name= specified';
			break;
		}

		if ($ri['exists'] != 1) {
			$ret['error'] = 'invalid user or no rights';
			break;
		}

		// From this point below, we have a repo
		rg_stats_conns_set('repo_id', $ri['repo_id']);

		$x = array();
		$x['obj_id'] = $ri['repo_id'];
		$x['type'] = 'repo';
		$x['owner'] = $ri['uid'];
		$x['uid'] = $a['login_ui']['uid'];
		$x['username'] = $a['login_ui']['username'];
		$x['needed_rights'] = 'A'; // A = Access repo; E = create/edit
		$x['ip'] = $a['ip'];
		$x['misc'] = '';

		if (strcmp($cmd, 'repo_info') == 0) {
			if (rg_rights_allow($db, $x) !== TRUE) {
				$ret['error'] = 'invalid user or no rights';
				break;
			}

			$ret = $ri;
			break;
		}

		if ((strcmp($cmd, 'repo_pr_list') == 0)
			|| (strcmp($cmd, 'repo_mr_list') == 0)) {
			if (rg_rights_allow($db, $x) !== TRUE) {
				$ret['error'] = 'invalid user or no rights';
				break;
			}

			$ret['repo_id'] = $ri['repo_id'];

			$params = array('repo_id' => $ri['repo_id']);
			$sql = 'SELECT * FROM merge_requests'
				. ' WHERE repo_id = @@repo_id@@';
			$res = rg_sql_query_params($db, $sql, $params);
			if ($res === FALSE) {
				$ret['error'] = 'internal error';
				break;
			}
			$ret['list'] = array();
			while (($row = rg_sql_fetch_array($res))) {
				unset($row['repo_id']);
				$ret['list'][] = $row;
			}
			rg_sql_free_result($res);
			break;
		}

		if (strcmp($cmd, 'repo_bug_list') == 0) {
			$y = $x;
			$y['needed_rights'] = 'a'; // a = Access bug repo
			if (rg_rights_allow($db, $y) !== TRUE) {
				$ret['error'] = 'invalid user or no rights';
				break;
			}

			$ret['repo_id'] = $ri['repo_id'];

			$params = array('repo_id' => $ri['repo_id']);
			$sql = 'SELECT bug_id FROM bugs'
				. ' WHERE repo_id = @@repo_id@@';
			$res = rg_sql_query_params($db, $sql, $params);
			if ($res === FALSE) {
				$ret['error'] = 'internal error';
				break;
			}
			$ret['list'] = array();
			while (($row = rg_sql_fetch_array($res))) {
				$ret['list'][] = $row['bug_id'];
			}
			rg_sql_free_result($res);
			break;
		}

		// TODO: for bugs, we do layer violation here!
		// Move this into bug.inc.php and register a function?
		if (strcmp($cmd, 'repo_bug_info') == 0) {
			if (!isset($json['bug_id'])) {
				$ret['error'] = 'bug_id= para not specified';
				break;
			}
			$bug_id = intval($json['bug_id']);

			$y = $x;
			$y['needed_rights'] = 'a'; // a = Access bug repo
			if (rg_rights_allow($db, $y) !== TRUE) {
				$ret['error'] = 'invalid user or no rights';
				break;
			}

			$bi = rg_bug_info($db, $ri['repo_id'], $bug_id);
			if ($bi['exists'] != 1) {
				$ret['error'] = 'bug does not exists';
				break;
			}

			$ret = array_merge($ret, $bi);
			break;
		}

		$ret['error'] = 'invalid command';
		break;
	}

	rg_log_exit();
	rg_prof_end('repo_api');
	return $ret;
}

/*
 * Helper for SSH/GIT/HTTP(S) fetch/push
 * @host - the host name accessed - may be different than the good one
 * @ip - from where the connections come
 * @cmd: 'git-upload-pack' or 'git-receive-pack'
 * @flags: B = bypass rights check (for example, for a global worker)
 * TODO: move it to user.inc.php?!
 */
function rg_repo_fetch_push_helper($db, $host, $ip, $login_ui, $prefix, $user,
	$repo, $cmd, $flags)
{
	global $rg_log_sid;

	rg_prof_start('repo_fetch_push_helper');
	rg_log_enter('repo_fetch_push_helper: host=' . $host
		. ' ip=' . $ip . ' prefix=' . $prefix . ' user=[' . $user . ']'
		. ' repo=[' . $repo . '] cmd=[' . $cmd . '] flags=' . $flags);

	$ret = array('ok' => 0, 'allow' => 0, 'push_allowed' => 0);
	while (1) {
		// Extracts command and computes permissions
		if (strncasecmp($cmd, 'git-upload-pack', 15) == 0) {
			$cmd = 'git-upload-pack';
			$needed_rights = 'F';
			$ret['push'] = 0;
		} else if (strncasecmp($cmd, 'git-receive-pack', 16) == 0) {
			$cmd = 'git-receive-pack';
			// We need push or anonymous push
			$needed_rights = 'P|H';
			$ret['push'] = 1;
		} else {
			$ret['errmsg'] = 'unknown command';
			break;
		}

		// Validity/security checks
		// Load info about the owner
		if (rg_user_ok($user) !== TRUE) {
			$ret['errmsg'] = 'user is invalid (' . rg_user_error() . ')';
			break;
		}
		$ret['owner_ui'] = rg_user_info($db, 0, $user, '');
		if ($ret['owner_ui']['ok'] != 1) {
			$ret['errmsg'] = 'internal problems; try again later, please';
			break;
		}
		if ($ret['owner_ui']['exists'] != 1) {
			$ret['errmsg'] = 'user does not exists';
			break;
		}

		// TODO: What if the user is deleted?

		// Loading info about the repository
		if (rg_repo_ok($repo) !== TRUE) {
			$ret['errmsg'] = rg_repo_error();
			break;
		}
		$ret['ri'] = rg_repo_info($db, 0, $ret['owner_ui']['uid'], $repo);
		if ($ret['ri']['ok'] != 1) {
			$ret['errmsg'] = 'internal problems; try again later, please';
			break;
		}
		if ($ret['ri']['exists'] != 1) {
			$ret['errmsg'] = 'repository does not exists';
			break;
		}
		if ($ret['ri']['deleted'] > 0) {
			$ret['errmsg'] = 'repository has been deleted';
			break;
		}

		$ls = rg_repo_lock_status($db, $ret['ri']['repo_id']);
		if ($ls['ok'] != 1) {
			$ret['errmsg'] = 'could not get lock status: ' . rg_repo_error();
			break;
		}
		if (($ls['status'] == 1) && ($login_ui['uid'] != $ls['uid'])) {
			$_u = rg_user_info($db, $ls['uid'], '', '');
			if ($_u['exists'] == 1)
				$_user = $_u['username'];
			else
				$_user = '?';
			$ret['errmsg'] = 'repository has been locked user ' . $_user
				. ' at ' . gmdate('Y-m-d H:i', $ls['itime']) . ' UTC;'
				. ' reason: ' . $ls['reason'];
			break;
		}

		$repo_path = rg_repo_path_by_id($ret['owner_ui']['uid'],
			$ret['ri']['repo_id']);
		$ret['repo_path'] = $repo_path;

		// TODO: signal user that the repo moved and provide a hint how to follow

		$ret['ok'] = 1;

		if (!strstr($flags, 'B')) {
			$x = array();
			$x['obj_id'] = $ret['ri']['repo_id'];
			$x['type'] = 'repo_refs';
			$x['owner'] = $ret['ri']['uid'];
			$x['uid'] = $login_ui['uid'];
			$x['username'] = $login_ui['uid'] == 0 ? '' : $login_ui['username'];
			$x['needed_rights'] = $needed_rights;
			$x['ip'] = $ip;
			$x['misc'] = '';
			$r = rg_rights_allow($db, $x);
			// TODO: what if an error occured? How do we signal this?! $r must have ok key
			if ($r !== TRUE) {
				$ret['errmsg'] = 'non existing repo or you are not allowed to push';
				break;
			}

			// We need to know if Push (and not anon) is allowed, so we can
			// give the user a chance to authenticate.
			// TODO: change rg_rights_allow to return what rights are
			// allowed and use it.
			if ($ret['push'] == 1) {
				$x['needed_rights'] = 'P';
				$r = rg_rights_allow($db, $x);
				if ($r === TRUE)
					$ret['push_allowed'] = 1;
			}

			// If we are enrolled, ask for login token
			// For push we always ask for it, for fetch only if repo is
			// NOT public. And we can ask only if we have a ssh connection.
			// For git protocol we cannot because we do not have the
			// username/uid of the connecting user.
			if (isset($_SERVER['SSH_CONNECTION'])) {
				if (($ret['ri']['public'] == 0) || ($ret['push'] == 1)) {
					$r = rg_totp_verify_ip($db, $login_ui['uid'],
						$ip);
					if (($r['ok'] !== 1)
						|| ($r['enrolled'] && empty($r['ip_list']))) {
						$ret['errmsg'] = rg_totp_error();
						$ret['ok'] = 0;
						break;
					}
				}
			}
		}

		// TODO: limit per connection
		// TODO: limit time and/or cpu
		// TODO: limit cpuset
		// TODO: limit io
		// TODO: put process in a cgroup?

		if (($ret['push'] == 1)
			&& rg_user_over_limit($db, $ret['owner_ui'], $max)) {
			// TODO: be aware that a push may mean a delete =>
			// free space?!
			// We should mark the repo over limit when we compute
			// the disk space - same problem
			$ret['errmsg'] = 'cannot push: user is over limit'
				. ' (' . $ret['owner_ui']['disk_used_mb'] . 'MiB >= '
				. $max . 'MiB)';
			break;
		}

		// We are allowed at this point
		$ret['allow'] = 1;

		// Put in environment all we need
		putenv('ROCKETGIT_LOGIN_UID=' . $login_ui['uid']);
		putenv('ROCKETGIT_LOGIN_URL=' . rg_re_userpage($login_ui));
		putenv('ROCKETGIT_REPO_ID=' . $ret['ri']['repo_id']);
		putenv('ROCKETGIT_REPO_PATH=' . $repo_path);
		putenv('ROCKETGIT_REPO_NAME=' . $ret['ri']['name']);
		putenv('ROCKETGIT_REPO_UID=' . $ret['ri']['uid']);
		putenv('ROCKETGIT_REPO_CLONE_URL=' . $ret['ri']['clone_url_http']);
		putenv('ROCKETGIT_IP=' . $ip);
		putenv('ROCKETGIT_ITIME=' . microtime(TRUE));
		putenv('ROCKETGIT_HOST=' . $host);
		putenv('ROCKETGIT_LOG_SID=' . $rg_log_sid);
		if (($ret['push'] == 1) && ($ret['push_allowed'] == 0)) {
			rg_log('DEBUG: We need to clone the namespace...');
			$namespace = 'rg_' . rg_id(8);
			rg_log('namespace is ' . $namespace);
			putenv('GIT_NAMESPACE=' . $namespace);

			// Prepare refs to avoid the following message:
			// 'No refs in common and none specified; doing nothing.
			// Perhaps you should specify a branch such as 'master'.'
			// TODO: cannot we copy only the pushed branch?!
			$dst = $repo_path . '/refs/namespaces/' . $namespace . '/refs';
			$r = rg_copy_tree($repo_path . '/refs/heads', $dst . '/heads/', 0700);
			if ($r !== TRUE) {
				$ret['errmsg'] = 'internal error (cannot copy heads refs)';
				break;
			}
			$r = rg_copy_tree($repo_path . '/refs/tags',
				$dst . '/tags/', 0700);
			if ($r !== TRUE) {
				$ret['errmsg'] =
					'internal error (cannot copy tags refs)';
				break;
			}
		}

		// TODO: we may want to call here the git-shell cmd and remove
		// it from remote.php?

		$ret['ok'] = 1;
		break;
	}

	rg_log_exit();
	rg_prof_end('repo_fetch_push_helper');
	return $ret;
}

Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master