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
|
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Gclient-specific SCM-specific operations."""
import collections
import contextlib
import errno
import glob
import json
import logging
import os
import platform
import posixpath
import re
import shutil
import sys
import tempfile
import threading
import traceback
import gclient_utils
import gerrit_util
import git_auth
import git_cache
import git_common
import scm
import subprocess2
# TODO: Should fix these warnings.
# pylint: disable=line-too-long
class NoUsableRevError(gclient_utils.Error):
"""Raised if requested revision isn't found in checkout."""
class DiffFiltererWrapper(object):
"""Simple base class which tracks which file is being diffed and
replaces instances of its file name in the original and
working copy lines of the git diff output."""
index_string = None
original_prefix = "--- "
working_prefix = "+++ "
def __init__(self, relpath, print_func):
# Note that we always use '/' as the path separator to be
# consistent with cygwin-style output on Windows
self._relpath = relpath.replace("\\", "/")
self._current_file = None
self._print_func = print_func
def SetCurrentFile(self, current_file):
self._current_file = current_file
@property
def _replacement_file(self):
return posixpath.join(self._relpath, self._current_file)
def _Replace(self, line):
return line.replace(self._current_file, self._replacement_file)
def Filter(self, line):
if (line.startswith(self.index_string)):
self.SetCurrentFile(line[len(self.index_string):])
line = self._Replace(line)
else:
if (line.startswith(self.original_prefix)
or line.startswith(self.working_prefix)):
line = self._Replace(line)
self._print_func(line)
class GitDiffFilterer(DiffFiltererWrapper):
index_string = "diff --git "
def SetCurrentFile(self, current_file):
# Get filename by parsing "a/<filename> b/<filename>"
self._current_file = current_file[:(len(current_file) / 2)][2:]
def _Replace(self, line):
return re.sub("[a|b]/" + self._current_file, self._replacement_file,
line)
# SCMWrapper base class
class SCMWrapper(object):
"""Add necessary glue between all the supported SCM.
This is the abstraction layer to bind to different SCM.
"""
def __init__(self,
url=None,
root_dir=None,
relpath=None,
out_fh=None,
out_cb=None,
print_outbuf=False):
self.url = url
self._root_dir = root_dir
if self._root_dir:
self._root_dir = self._root_dir.replace('/', os.sep)
self.relpath = relpath
if self.relpath:
self.relpath = self.relpath.replace('/', os.sep)
if self.relpath and self._root_dir:
self.checkout_path = os.path.join(self._root_dir, self.relpath)
if out_fh is None:
out_fh = sys.stdout
self.out_fh = out_fh
self.out_cb = out_cb
self.print_outbuf = print_outbuf
def Print(self, *args, **kwargs):
kwargs.setdefault('file', self.out_fh)
if kwargs.pop('timestamp', True):
self.out_fh.write('[%s] ' % gclient_utils.Elapsed())
print(*args, **kwargs)
def RunCommand(self, command, options, args, file_list=None):
commands = [
'update', 'updatesingle', 'revert', 'revinfo', 'status', 'diff',
'pack', 'runhooks'
]
if not command in commands:
raise gclient_utils.Error('Unknown command %s' % command)
if not command in dir(self):
raise gclient_utils.Error(
'Command %s not implemented in %s wrapper' %
(command, self.__class__.__name__))
return getattr(self, command)(options, args, file_list)
@staticmethod
def _get_first_remote_url(checkout_path):
log = scm.GIT.YieldConfigRegexp(checkout_path, r'remote.*.url')
return next(log)[1]
def GetCacheMirror(self):
if getattr(self, 'cache_dir', None):
url, _ = gclient_utils.SplitUrlRevision(self.url)
return git_cache.Mirror(url)
return None
def GetActualRemoteURL(self, options):
"""Attempt to determine the remote URL for this SCMWrapper."""
# Git
if os.path.exists(os.path.join(self.checkout_path, '.git')):
actual_remote_url = self._get_first_remote_url(self.checkout_path)
mirror = self.GetCacheMirror()
# If the cache is used, obtain the actual remote URL from there.
if (mirror and mirror.exists() and mirror.mirror_path.replace(
'\\', '/') == actual_remote_url.replace('\\', '/')):
actual_remote_url = self._get_first_remote_url(
mirror.mirror_path)
return actual_remote_url
return None
def DoesRemoteURLMatch(self, options):
"""Determine whether the remote URL of this checkout is the expected URL."""
if not os.path.exists(self.checkout_path):
# A checkout which doesn't exist can't be broken.
return True
actual_remote_url = self.GetActualRemoteURL(options)
if actual_remote_url:
return (gclient_utils.SplitUrlRevision(actual_remote_url)[0].rstrip(
'/') == gclient_utils.SplitUrlRevision(self.url)[0].rstrip('/'))
# This may occur if the self.checkout_path exists but does not contain a
# valid git checkout.
return False
def _DeleteOrMove(self, force):
"""Delete the checkout directory or move it out of the way.
Args:
force: bool; if True, delete the directory. Otherwise, just move it.
"""
if force and os.environ.get('CHROME_HEADLESS') == '1':
self.Print('_____ Conflicting directory found in %s. Removing.' %
self.checkout_path)
gclient_utils.AddWarning('Conflicting directory %s deleted.' %
self.checkout_path)
gclient_utils.rmtree(self.checkout_path)
else:
bad_scm_dir = os.path.join(self._root_dir, '_bad_scm',
os.path.dirname(self.relpath))
try:
os.makedirs(bad_scm_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
dest_path = tempfile.mkdtemp(prefix=os.path.basename(self.relpath),
dir=bad_scm_dir)
self.Print(
'_____ Conflicting directory found in %s. Moving to %s.' %
(self.checkout_path, dest_path))
gclient_utils.AddWarning('Conflicting directory %s moved to %s.' %
(self.checkout_path, dest_path))
shutil.move(self.checkout_path, dest_path)
class GitWrapper(SCMWrapper):
"""Wrapper for Git"""
name = 'git'
remote = 'origin'
@property
def cache_dir(self):
try:
return git_cache.Mirror.GetCachePath()
except RuntimeError:
return None
def __init__(self, url=None, *args, **kwargs):
"""Removes 'git+' fake prefix from git URL."""
if url and (url.startswith('git+http://')
or url.startswith('git+https://')):
url = url[4:]
SCMWrapper.__init__(self, url, *args, **kwargs)
filter_kwargs = {'time_throttle': 1, 'out_fh': self.out_fh}
if self.out_cb:
filter_kwargs['predicate'] = self.out_cb
self.filter = gclient_utils.GitFilter(**filter_kwargs)
self._running_under_rosetta = None
self.current_revision = None
def GetCheckoutRoot(self):
return scm.GIT.GetCheckoutRoot(self.checkout_path)
def GetRevisionDate(self, _revision):
"""Returns the given revision's date in ISO-8601 format (which contains the
time zone)."""
# TODO(floitsch): get the time-stamp of the given revision and not just
# the time-stamp of the currently checked out revision.
return self._Capture(['log', '-n', '1', '--format=%ai'])
def _GetDiffFilenames(self, base):
"""Returns the names of files modified since base."""
return self._Capture(
# Filter to remove base if it is None.
list(
filter(
bool,
['-c', 'core.quotePath=false', 'diff', '--name-only', base])
)).split()
def GetSubmoduleStateFromIndex(self):
"""Returns a map where keys are submodule names and values are commit
hashes. It reads data from the Git index, so only committed values are
present."""
out = self._Capture(['ls-files', '-s'])
result = {}
for l in out.split('\n'):
if not l.startswith('160000'):
# Not a submodule
continue
(_, commit, _, filepath) = l.split(maxsplit=3)
result[filepath] = commit
return result
def GetSubmoduleDiff(self):
"""Returns a map where keys are submodule names and values are tuples of
(old_commit_hash, new_commit_hash). old_commit_hash matches the Git
index, whereas new_commit_hash matches currently checked out commit
hash."""
out = self._Capture([
'diff',
'--no-prefix',
'--no-ext-diff',
'--no-color',
'--ignore-submodules=dirty',
'--submodule=short',
'-G',
'Subproject commit',
])
NO_COMMIT = 40 * '0'
committed_submodule = None
checked_submodule = None
filepath = None
state = 0
diff = {}
# Parsing git diff uses simple state machine. States:
# 0 - start state
# 1 - diff file/line detected, ready to process content
# 2 - gitlink detected, ready to process gitlink past and current
# content.
# 3 - past gitlink content detected. It contains a commit hash that's in
# git index.
# 4 - new gitlink content detected. It contains currently checked
# commit. At this point, we have all information needed, and we can
# reset state to 0.
for l in out.split('\n'):
if l.startswith('diff --git'):
# New file detected, reset state.
state = 1
elif state == 1 and l.startswith('index') and l.endswith('160000'):
# We detected gitlink
state = 2
elif state == 2 and l.startswith('+++ '):
# This line contains filename
filepath = l[4:]
state = 3
elif state == 3 and l.startswith('-Subproject commit '):
# This line contains what commit hash Git index expects
# (ls-files).
committed_submodule = l.split(' ')[-1]
state = 4
elif state == 4 and l.startswith('+Subproject commit '):
# This line contains currently checked out commit for this submodule.
checked_submodule = l.split(' ')[-1]
if NO_COMMIT not in (committed_submodule, checked_submodule):
diff[filepath] = (committed_submodule, checked_submodule)
state = 0
return diff
def diff(self, options, _args, _file_list):
_, revision = gclient_utils.SplitUrlRevision(self.url)
if not revision:
revision = 'refs/remotes/%s/main' % self.remote
self._Run(['-c', 'core.quotePath=false', 'diff', revision], options)
def pack(self, _options, _args, _file_list):
"""Generates a patch file which can be applied to the root of the
repository.
The patch file is generated from a diff of the merge base of HEAD and
its upstream branch.
"""
try:
merge_base = [self._Capture(['merge-base', 'HEAD', self.remote])]
except subprocess2.CalledProcessError:
merge_base = []
gclient_utils.CheckCallAndFilter(['git', 'diff'] + merge_base,
cwd=self.checkout_path,
filter_fn=GitDiffFilterer(
self.relpath,
print_func=self.Print).Filter)
def _Scrub(self, target, options):
"""Scrubs out all changes in the local repo, back to the state of target."""
quiet = []
if not options.verbose:
quiet = ['--quiet']
self._Run(['reset', '--hard', target] + quiet, options)
if options.force and options.delete_unversioned_trees:
# where `target` is a commit that contains both upper and lower case
# versions of the same file on a case insensitive filesystem, we are
# actually in a broken state here. The index will have both 'a' and
# 'A', but only one of them will exist on the disk. To progress, we
# delete everything that status thinks is modified.
output = self._Capture(
['-c', 'core.quotePath=false', 'status', '--porcelain'],
strip=False)
for line in output.splitlines():
# --porcelain (v1) looks like:
# XY filename
try:
filename = line[3:]
self.Print('_____ Deleting residual after reset: %r.' %
filename)
gclient_utils.rm_file_or_tree(
os.path.join(self.checkout_path, filename))
except OSError:
pass
def _FetchAndReset(self, revision, file_list, options):
"""Equivalent to git fetch; git reset."""
self._SetFetchConfig(options)
self._Fetch(options, prune=True, quiet=options.verbose)
revision = self._AutoFetchRef(options, revision)
self._Scrub(revision, options)
if file_list is not None:
files = self._Capture(['-c', 'core.quotePath=false',
'ls-files']).splitlines()
file_list.extend(
[os.path.join(self.checkout_path, f) for f in files])
def _DisableHooks(self):
hook_dir = os.path.join(self.checkout_path, '.git', 'hooks')
if not os.path.isdir(hook_dir):
return
for f in os.listdir(hook_dir):
if not f.endswith('.sample') and not f.endswith('.disabled'):
disabled_hook_path = os.path.join(hook_dir, f + '.disabled')
if os.path.exists(disabled_hook_path):
os.remove(disabled_hook_path)
os.rename(os.path.join(hook_dir, f), disabled_hook_path)
def _maybe_break_locks(self, options):
"""This removes all .lock files from this repo's .git directory, if the
user passed the --break_repo_locks command line flag.
In particular, this will cleanup index.lock files, as well as ref lock
files.
"""
if options.break_repo_locks:
git_dir = os.path.join(self.checkout_path, '.git')
for path, _, filenames in os.walk(git_dir):
for filename in filenames:
if filename.endswith('.lock'):
to_break = os.path.join(path, filename)
self.Print('breaking lock: %s' % (to_break, ))
try:
os.remove(to_break)
except OSError as ex:
self.Print('FAILED to break lock: %s: %s' %
(to_break, ex))
raise
def _download_topics(self, patch_rev, googlesource_url):
"""This method returns new patch_revs to process that have the same topic.
It does the following:
1. Finds the topic of the Gerrit change specified in the patch_rev.
2. Find all changes with that topic.
3. Append patch_rev of the changes with the same topic to the patch_revs
to process.
4. Returns the new patch_revs to process.
"""
patch_revs_to_process = []
# Parse the patch_rev to extract the CL and patchset.
patch_rev_tokens = patch_rev.split('/')
change = patch_rev_tokens[-2]
# Parse the googlesource_url.
tokens = re.search(r'//(.+).googlesource.com/(.+?)(?:\.git)?$',
googlesource_url)
if not tokens or len(tokens.groups()) != 2:
# googlesource_url is not in the expected format.
return patch_revs_to_process
# parse the gerrit host and repo out of googlesource_url.
host, repo = tokens.groups()[:2]
gerrit_host_url = '%s-review.googlesource.com' % host
# 1. Find the topic of the Gerrit change specified in the patch_rev.
change_object = gerrit_util.GetChange(gerrit_host_url, change)
topic = change_object.get('topic')
if not topic:
# This change has no topic set.
return patch_revs_to_process
# 2. Find all changes with that topic.
changes_with_same_topic = gerrit_util.QueryChanges(
gerrit_host_url, [('topic', topic), ('status', 'open'),
('repo', repo)],
o_params=['ALL_REVISIONS'])
for c in changes_with_same_topic:
if str(c['_number']) == change:
# This change is already in the patch_rev.
continue
self.Print('Found CL %d with the topic name %s' %
(c['_number'], topic))
# 3. Append patch_rev of the changes with the same topic to the
# patch_revs to process.
curr_rev = c['current_revision']
new_patch_rev = c['revisions'][curr_rev]['ref']
patch_revs_to_process.append(new_patch_rev)
# 4. Return the new patch_revs to process.
return patch_revs_to_process
def _ref_to_remote_ref(self, target_rev):
"""Helper function for scm.GIT.RefToRemoteRef with error checking.
Joins the results of scm.GIT.RefToRemoteRef into a string, but raises a
comprehensible error if RefToRemoteRef fails.
Args:
target_rev: a ref somewhere under refs/.
"""
tmp_ref = scm.GIT.RefToRemoteRef(target_rev, self.remote)
if not tmp_ref:
raise gclient_utils.Error(
'Failed to turn target revision %r in repo %r into remote ref' %
(target_rev, self.checkout_path))
return ''.join(tmp_ref)
def apply_patch_ref(self, patch_repo, patch_rev, target_rev, options,
file_list):
# type: (str, str, str, optparse.Values, Collection[str]) -> str
"""Apply a patch on top of the revision we're synced at.
The patch ref is given by |patch_repo|@|patch_rev|.
|target_rev| is usually the branch that the |patch_rev| was uploaded against
(e.g. 'refs/heads/main'), but this is not required.
We cherry-pick all commits reachable from |patch_rev| on top of the curret
HEAD, excluding those reachable from |target_rev|
(i.e. git cherry-pick target_rev..patch_rev).
Graphically, it looks like this:
... -> o -> [possibly already landed commits] -> target_rev
\
-> [possibly not yet landed dependent CLs] -> patch_rev
The final checkout state is then:
... -> HEAD -> [possibly not yet landed dependent CLs] -> patch_rev
After application, if |options.reset_patch_ref| is specified, we soft reset
the cherry-picked changes, keeping them in git index only.
Args:
patch_repo: The patch origin.
e.g. 'https://foo.googlesource.com/bar'
patch_rev: The revision to patch.
e.g. 'refs/changes/1234/34/1'.
target_rev: The revision to use when finding the merge base.
Typically, the branch that the patch was uploaded against.
e.g. 'refs/heads/main' or 'refs/heads/infra/config'.
options: The options passed to gclient.
file_list: A list where modified files will be appended.
"""
# Abort any cherry-picks in progress.
try:
self._Capture(['cherry-pick', '--abort'])
except subprocess2.CalledProcessError:
pass
base_rev = self.revinfo(None, None, None)
if not target_rev:
raise gclient_utils.Error(
'A target revision for the patch must be given')
if target_rev.startswith(('refs/heads/', 'refs/branch-heads')):
# If |target_rev| is in refs/heads/** or refs/branch-heads/**, try
# first to find the corresponding remote ref for it, since
# |target_rev| might point to a local ref which is not up to date
# with the corresponding remote ref.
remote_ref = self._ref_to_remote_ref(target_rev)
self.Print('Trying the corresponding remote ref for %r: %r\n' %
(target_rev, remote_ref))
if scm.GIT.IsValidRevision(self.checkout_path, remote_ref):
# refs/remotes may need to be updated to cleanly cherry-pick
# changes. See https://crbug.com/1255178.
url, _ = gclient_utils.SplitUrlRevision(self.url)
mirror = self._GetMirror(url, options, target_rev, remote_ref)
if mirror:
self._UpdateMirrorIfNotContains(mirror, options, 'branch',
target_rev)
self._Capture(['fetch', '--no-tags', self.remote, target_rev])
target_rev = remote_ref
elif not scm.GIT.IsValidRevision(self.checkout_path, target_rev):
# Fetch |target_rev| if it's not already available.
url, _ = gclient_utils.SplitUrlRevision(self.url)
mirror = self._GetMirror(url, options, target_rev, target_rev)
if mirror:
rev_type = 'branch' if target_rev.startswith(
'refs/') else 'hash'
self._UpdateMirrorIfNotContains(mirror, options, rev_type,
target_rev)
self._Fetch(options, refspec=target_rev)
patch_revs_to_process = [patch_rev]
if hasattr(options, 'download_topics') and options.download_topics:
patch_revs_to_process_from_topics = self._download_topics(
patch_rev, self.url)
patch_revs_to_process.extend(patch_revs_to_process_from_topics)
self._Capture(['reset', '--hard'])
for pr in patch_revs_to_process:
self.Print('===Applying patch===')
self.Print('Revision to patch is %r @ %r.' % (patch_repo, pr))
self.Print('Current dir is %r' % self.checkout_path)
self._Capture(['fetch', '--no-tags', patch_repo, pr])
pr = self._Capture(['rev-parse', 'FETCH_HEAD'])
if not options.rebase_patch_ref:
self._Capture(['checkout', pr])
# Adjust base_rev to be the first parent of our checked out
# patch ref; This will allow us to correctly extend `file_list`,
# and will show the correct file-list to programs which do `git
# diff --cached` expecting to see the patch diff.
base_rev = self._Capture(['rev-parse', pr + '~'])
else:
target_rev_hash = self._Capture(['rev-parse', target_rev])
commit_list = self._Capture(
['log', '--oneline', target_rev + '..' + pr])
self.Print('Will cherrypick %r (%r) .. %r on top of %r:' %
(target_rev_hash, target_rev, pr, base_rev))
self.Print(commit_list)
try:
if scm.GIT.IsAncestor(pr,
target_rev,
cwd=self.checkout_path):
if len(patch_revs_to_process) > 1:
# If there are multiple patch_revs_to_process then
# we do not want want to invalidate a previous patch
# so throw an error.
raise gclient_utils.Error(
'patch_rev %s is an ancestor of target_rev %s. This '
'situation is unsupported when we need to apply multiple '
'patch_revs: %s' %
(pr, target_rev, patch_revs_to_process))
# If |patch_rev| is an ancestor of |target_rev|, check
# it out.
self._Capture(['checkout', pr])
else:
# If a change was uploaded on top of another change,
# which has already landed, one of the commits in the
# cherry-pick range will be redundant, since it has
# already landed and its changes incorporated in the
# tree. We pass '--keep-redundant-commits' to ignore
# those changes.
self._Capture([
'cherry-pick', target_rev + '..' + pr,
'--keep-redundant-commits'
])
except subprocess2.CalledProcessError as e:
self.Print('Failed to apply patch.')
self.Print('Revision to patch was %r @ %r.' %
(patch_repo, pr))
self.Print('Tried to cherrypick %r .. %r on top of %r.' %
(target_rev, pr, base_rev))
self.Print('Current dir is %r' % self.checkout_path)
self.Print('git returned non-zero exit status %s:\n%s' %
(e.returncode, e.stderr.decode('utf-8')))
# Print the current status so that developers know what
# changes caused the patch failure, since git cherry-pick
# doesn't show that information.
self.Print(self._Capture(['status']))
try:
self._Capture(['cherry-pick', '--abort'])
except subprocess2.CalledProcessError:
pass
raise
if file_list is not None:
file_list.extend(self._GetDiffFilenames(base_rev))
latest_commit = self.revinfo(None, None, None)
if options.reset_patch_ref:
self._Capture(['reset', '--soft', base_rev])
return latest_commit
def check_diff(self, previous_commit, files=None):
# type: (str, Optional[List[str]]) -> bool
"""Check if a diff exists between the current commit and `previous_commit`.
Returns True if there were diffs or if an error was encountered.
"""
cmd = ['diff', previous_commit, '--quiet']
if files:
cmd += ['--'] + files
try:
self._Capture(cmd)
return False
except subprocess2.CalledProcessError as e:
# git diff --quiet exits with 1 if there were diffs.
if e.returncode != 1:
self.Print('git returned non-zero exit status %s:\n%s' %
(e.returncode, e.stderr.decode('utf-8')))
return True
def set_config(f):
def wrapper(*args):
return_val = f(*args)
checkout_path = args[0].checkout_path
if os.path.exists(os.path.join(checkout_path, '.git')):
# The config updates to the project are stored in this list
# and updated consecutively after the reads. The updates
# are done this way because `scm.GIT.GetConfig` caches
# the config file and `scm.GIT.SetConfig` evicts the cache.
# This ensures we don't interleave reads and writes causing
# the cache to set and unset consecutively.
config_updates = []
blame_ignore_revs_cfg = scm.GIT.GetConfig(
checkout_path, 'blame.ignorerevsfile')
blame_ignore_revs_cfg_set = \
blame_ignore_revs_cfg == \
git_common.GIT_BLAME_IGNORE_REV_FILE
blame_ignore_revs_exists = os.path.isfile(
os.path.join(checkout_path,
git_common.GIT_BLAME_IGNORE_REV_FILE))
if not blame_ignore_revs_cfg_set and blame_ignore_revs_exists:
config_updates.append(
('blame.ignoreRevsFile',
git_common.GIT_BLAME_IGNORE_REV_FILE))
elif blame_ignore_revs_cfg_set and not blame_ignore_revs_exists:
# Some repos may have incorrect config set, unset this
# value. Moreover, some repositories may decide to remove
# git_common.GIT_BLAME_IGNORE_REV_FILE, which would break
# blame without this check.
# See https://crbug.com/368562244 for more details.
config_updates.append(('blame.ignoreRevsFile', None))
ignore_submodules = scm.GIT.GetConfig(checkout_path,
'diff.ignoresubmodules',
None, 'local')
if not ignore_submodules:
config_updates.append(('diff.ignoreSubmodules', 'dirty'))
elif ignore_submodules != 'dirty':
warning_message = (
"diff.ignoreSubmodules is not set to 'dirty' "
"for this repository.\n"
"This may cause unexpected behavior with submodules; "
"see //docs/git_submodules.md\n"
"Consider setting the config:\n"
"\tgit config diff.ignoreSubmodules dirty\n"
"or disable this warning by setting the "
"GCLIENT_SUPPRESS_SUBMODULE_WARNING\n"
"environment variable to 1.")
if os.environ.get(
'GCLIENT_SUPPRESS_SUBMODULE_WARNING') != '1':
gclient_utils.AddWarning(warning_message)
if scm.GIT.GetConfig(checkout_path,
'fetch.recursesubmodules') != 'off':
config_updates.append(('fetch.recurseSubmodules', 'off'))
if scm.GIT.GetConfig(checkout_path,
'push.recursesubmodules') != 'off':
# The default is off, but if user sets submodules.recurse to
# on, this becomes on too. We never want to push submodules
# for gclient managed repositories. Push, even if a no-op,
# will increase `git cl upload` latency.
config_updates.append(('push.recurseSubmodules', 'off'))
for update in config_updates:
scm.GIT.SetConfig(checkout_path,
update[0],
update[1],
modify_all=True)
return return_val
return wrapper
@set_config
def update(self, options, args, file_list):
"""Runs git to update or transparently checkout the working copy.
All updated files will be appended to file_list.
Raises:
Error: if can't get URL for relative path.
"""
if args:
raise gclient_utils.Error("Unsupported argument(s): %s" %
",".join(args))
url, deps_revision = gclient_utils.SplitUrlRevision(self.url)
revision = deps_revision
managed = True
if options.revision:
# Override the revision number.
revision = str(options.revision)
if revision == 'unmanaged':
# Check again for a revision in case an initial ref was specified
# in the url, for example bla.git@refs/heads/custombranch
revision = deps_revision
managed = False
if not revision:
# If a dependency is not pinned, track the default remote branch.
revision = scm.GIT.GetRemoteHeadRef(self.checkout_path, self.url,
self.remote)
if revision.startswith('origin/'):
revision = 'refs/remotes/' + revision
if managed and platform.system() == 'Windows':
self._DisableHooks()
printed_path = False
verbose = []
if options.verbose:
self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False)
verbose = ['--verbose']
printed_path = True
revision_ref = revision
if ':' in revision:
revision_ref, _, revision = revision.partition(':')
if revision_ref.startswith('refs/branch-heads'):
options.with_branch_heads = True
mirror = self._GetMirror(url, options, revision, revision_ref)
if mirror:
url = mirror.mirror_path
remote_ref = scm.GIT.RefToRemoteRef(revision, self.remote)
if remote_ref:
# Rewrite remote refs to their local equivalents.
revision = ''.join(remote_ref)
rev_type = "branch"
elif revision.startswith('refs/heads/'):
# Local branch? We probably don't want to support, since DEPS should
# always specify branches as they are in the upstream repo.
rev_type = "branch"
else:
# hash is also a tag, only make a distinction at checkout.
# Any ref (e.g. /refs/changes/*) not a branch has no difference from
# a hash.
rev_type = "hash"
# If we are going to introduce a new project, there is a possibility
# that we are syncing back to a state where the project was originally a
# sub-project rolled by DEPS (realistic case: crossing the Blink merge
# point syncing backwards, when Blink was a DEPS entry and not part of
# src.git). In such case, we might have a backup of the former .git
# folder, which can be used to avoid re-fetching the entire repo again
# (useful for bisects).
backup_dir = self.GetGitBackupDirPath()
target_dir = os.path.join(self.checkout_path, '.git')
if os.path.exists(backup_dir) and not os.path.exists(target_dir):
gclient_utils.safe_makedirs(self.checkout_path)
os.rename(backup_dir, target_dir)
# Reset to a clean state
self._Scrub('HEAD', options)
if (not os.path.exists(self.checkout_path) or
(os.path.isdir(self.checkout_path)
and not os.path.exists(os.path.join(self.checkout_path, '.git')))):
if mirror:
self._UpdateMirrorIfNotContains(mirror, options, rev_type,
revision)
try:
self.current_revision = self._Clone(revision, url, options)
except subprocess2.CalledProcessError as e:
logging.warning('Clone failed due to: %s', e)
self._DeleteOrMove(options.force)
self.current_revision = self._Clone(revision, url, options)
if file_list is not None:
files = self._Capture(
['-c', 'core.quotePath=false', 'ls-files']).splitlines()
file_list.extend(
[os.path.join(self.checkout_path, f) for f in files])
if mirror:
self._Capture(
['remote', 'set-url', '--push', 'origin', mirror.url])
if not verbose:
# Make the output a little prettier. It's nice to have some
# whitespace between projects when cloning.
self.Print('')
return self._Capture(['rev-parse', '--verify', 'HEAD'])
if mirror:
self._Capture(['remote', 'set-url', '--push', 'origin', mirror.url])
if not managed:
self._SetFetchConfig(options)
self.Print('________ unmanaged solution; skipping %s' %
self.relpath)
return self._Capture(['rev-parse', '--verify', 'HEAD'])
# Special case for rev_type = hash. If we use submodules, we can check
# information already.
if rev_type == 'hash':
if self.current_revision == revision:
if verbose:
self.Print('Using submodule information to skip check')
if options.reset or options.force:
self._Scrub('HEAD', options)
return revision
self._maybe_break_locks(options)
if mirror:
self._UpdateMirrorIfNotContains(mirror, options, rev_type, revision)
# See if the url has changed (the unittests use git://foo for the url,
# let that through).
current_url = scm.GIT.GetConfig(self.checkout_path,
f'remote.{self.remote}.url',
default='')
return_early = False
# TODO(maruel): Delete url != 'git://foo' since it's just to make the
# unit test pass. (and update the comment above)
strp_url = url[:-4] if url.endswith('.git') else url
strp_current_url = current_url[:-4] if current_url.endswith(
'.git') else current_url
if (strp_current_url.rstrip('/') != strp_url.rstrip('/')
and url != 'git://foo'):
self.Print('_____ switching %s from %s to new upstream %s' %
(self.relpath, current_url, url))
if not (options.force or options.reset):
# Make sure it's clean
self._CheckClean(revision)
# Switch over to the new upstream
self._Run(['remote', 'set-url', self.remote, url], options)
if mirror:
if git_cache.Mirror.CacheDirToUrl(current_url.rstrip(
'/')) == git_cache.Mirror.CacheDirToUrl(
url.rstrip('/')):
# Reset alternates when the cache dir is updated.
with open(
os.path.join(self.checkout_path, '.git', 'objects',
'info', 'alternates'), 'w') as fh:
fh.write(os.path.join(url, 'objects'))
else:
# Because we use Git alternatives, our existing repository
# is not self-contained. It's possible that new git
# alternative doesn't have all necessary objects that the
# current repository needs. Instead of blindly hoping that
# new alternative contains all necessary objects, keep the
# old alternative and just append a new one on top of it.
with open(
os.path.join(self.checkout_path, '.git', 'objects',
'info', 'alternates'), 'a') as fh:
fh.write("\n" + os.path.join(url, 'objects'))
current_revision = self._EnsureValidHeadObjectOrCheckout(
revision, options, url)
self._FetchAndReset(revision, file_list, options)
return_early = True
else:
current_revision = self._EnsureValidHeadObjectOrCheckout(
revision, options, url)
if return_early:
return current_revision or self._Capture(
['rev-parse', '--verify', 'HEAD'])
cur_branch = self._GetCurrentBranch()
# Cases:
# 0) HEAD is detached. Probably from our initial clone.
# - make sure HEAD is contained by a named ref, then update.
# Cases 1-4. HEAD is a branch.
# 1) current branch is not tracking a remote branch
# - try to rebase onto the new hash or branch
# 2) current branch is tracking a remote branch with local committed
# changes, but the DEPS file switched to point to a hash
# - rebase those changes on top of the hash
# 3) current branch is tracking a remote branch w/or w/out changes, and
# no DEPS switch
# - see if we can FF, if not, prompt the user for rebase, merge, or stop
# 4) current branch is tracking a remote branch, but DEPS switches to a
# different remote branch, and a) current branch has no local changes,
# and --force: - checkout new branch b) current branch has local
# changes, and --force and --reset: - checkout new branch c) otherwise
# exit
# GetUpstreamBranch returns something like 'refs/remotes/origin/main'
# for a tracking branch or 'main' if not a tracking branch (it's based
# on a specific rev/hash) or it returns None if it couldn't find an
# upstream
if cur_branch is None:
upstream_branch = None
current_type = "detached"
logging.debug("Detached HEAD")
else:
upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
if not upstream_branch or not upstream_branch.startswith(
'refs/remotes'):
current_type = "hash"
logging.debug(
"Current branch is not tracking an upstream (remote)"
" branch.")
elif upstream_branch.startswith('refs/remotes'):
current_type = "branch"
else:
raise gclient_utils.Error('Invalid Upstream: %s' %
upstream_branch)
self._SetFetchConfig(options)
# Fetch upstream if we don't already have |revision|.
if not scm.GIT.IsValidRevision(
self.checkout_path, revision, sha_only=True):
self._Fetch(options, prune=options.force)
if not scm.GIT.IsValidRevision(
self.checkout_path, revision, sha_only=True):
# Update the remotes first so we have all the refs.
remote_output = scm.GIT.Capture(['remote'] + verbose +
['update'],
cwd=self.checkout_path)
if verbose:
self.Print(remote_output)
revision = self._AutoFetchRef(options, revision)
# This is a big hammer, debatable if it should even be here...
if options.force or options.reset:
target = 'HEAD'
if options.upstream and upstream_branch:
target = upstream_branch
self._Scrub(target, options)
if current_type == 'detached':
# case 0
# We just did a Scrub, this is as clean as it's going to get. In
# particular if HEAD is a commit that contains two versions of the
# same file on a case-insensitive filesystem (e.g. 'a' and 'A'),
# there's no way to actually "Clean" the checkout; that commit is
# uncheckoutable on this system. The best we can do is carry forward
# to the checkout step.
if not (options.force or options.reset):
self._CheckClean(revision)
self._CheckDetachedHead(revision, options)
if not current_revision:
current_revision = self._Capture(
['rev-list', '-n', '1', 'HEAD'])
if current_revision == revision:
self.Print('Up-to-date; skipping checkout.')
else:
# 'git checkout' may need to overwrite existing untracked files.
# Allow it only when nuclear options are enabled.
self._Checkout(
options,
revision,
force=(options.force and options.delete_unversioned_trees),
quiet=True,
)
if not printed_path:
self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False)
elif current_type == 'hash':
# case 1
# Can't find a merge-base since we don't know our upstream. That
# makes this command VERY likely to produce a rebase failure. For
# now we assume origin is our upstream since that's what the old
# behavior was.
upstream_branch = self.remote
if options.revision or deps_revision:
upstream_branch = revision
self._AttemptRebase(upstream_branch,
file_list,
options,
printed_path=printed_path,
merge=options.merge)
printed_path = True
elif rev_type == 'hash':
# case 2
self._AttemptRebase(upstream_branch,
file_list,
options,
newbase=revision,
printed_path=printed_path,
merge=options.merge)
printed_path = True
elif remote_ref and ''.join(remote_ref) != upstream_branch:
# case 4
new_base = ''.join(remote_ref)
if not printed_path:
self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False)
switch_error = (
"Could not switch upstream branch from %s to %s\n" %
(upstream_branch, new_base) +
"Please use --force or merge or rebase manually:\n" +
"cd %s; git rebase %s\n" % (self.checkout_path, new_base) +
"OR git checkout -b <some new branch> %s" % new_base)
force_switch = False
if options.force:
try:
self._CheckClean(revision)
# case 4a
force_switch = True
except gclient_utils.Error as e:
if options.reset:
# case 4b
force_switch = True
else:
switch_error = '%s\n%s' % (str(e), switch_error)
if force_switch:
self.Print("Switching upstream branch from %s to %s" %
(upstream_branch, new_base))
switch_branch = 'gclient_' + remote_ref[1]
self._Capture(['branch', '-f', switch_branch, new_base])
self._Checkout(options, switch_branch, force=True, quiet=True)
else:
# case 4c
raise gclient_utils.Error(switch_error)
else:
# case 3 - the default case
rebase_files = self._GetDiffFilenames(upstream_branch)
if verbose:
self.Print('Trying fast-forward merge to branch : %s' %
upstream_branch)
try:
merge_args = ['merge']
if options.merge:
merge_args.append('--ff')
else:
merge_args.append('--ff-only')
merge_args.append(upstream_branch)
merge_output = self._Capture(merge_args)
except subprocess2.CalledProcessError as e:
rebase_files = []
if re.search(b'fatal: Not possible to fast-forward, aborting.',
e.stderr):
if not printed_path:
self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False)
printed_path = True
while True:
if not options.auto_rebase:
try:
action = self._AskForData(
'Cannot %s, attempt to rebase? '
'(y)es / (q)uit / (s)kip : ' %
('merge' if options.merge else
'fast-forward merge'), options)
except ValueError:
raise gclient_utils.Error('Invalid Character')
if options.auto_rebase or re.match(
r'yes|y', action, re.I):
self._AttemptRebase(upstream_branch,
rebase_files,
options,
printed_path=printed_path,
merge=False)
printed_path = True
break
if re.match(r'quit|q', action, re.I):
raise gclient_utils.Error(
"Can't fast-forward, please merge or "
"rebase manually.\n"
"cd %s && git " % self.checkout_path +
"rebase %s" % upstream_branch)
if re.match(r'skip|s', action, re.I):
self.Print('Skipping %s' % self.relpath)
return
self.Print('Input not recognized')
elif re.match(
b"error: Your local changes to '.*' would be "
b"overwritten by merge. Aborting.\nPlease, commit your "
b"changes or stash them before you can merge.\n",
e.stderr):
if not printed_path:
self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False)
printed_path = True
raise gclient_utils.Error(e.stderr.decode('utf-8'))
else:
# Some other problem happened with the merge
logging.error("Error during fast-forward merge in %s!" %
self.relpath)
self.Print(e.stderr.decode('utf-8'))
raise
else:
# Fast-forward merge was successful
if not re.match('Already up-to-date.', merge_output) or verbose:
if not printed_path:
self.Print('_____ %s at %s' % (self.relpath, revision),
timestamp=False)
printed_path = True
self.Print(merge_output.strip())
if not verbose:
# Make the output a little prettier. It's nice to have
# some whitespace between projects when syncing.
self.Print('')
if file_list is not None:
file_list.extend(
[os.path.join(self.checkout_path, f) for f in rebase_files])
# If the rebase generated a conflict, abort and ask user to fix
if self._IsRebasing():
raise gclient_utils.Error(
'\n____ %s at %s\n'
'\nConflict while rebasing this branch.\n'
'Fix the conflict and run gclient again.\n'
'See man git-rebase for details.\n' % (self.relpath, revision))
# If --reset and --delete_unversioned_trees are specified, remove any
# untracked directories.
if options.reset and options.delete_unversioned_trees:
# GIT.CaptureStatus() uses 'dit diff' to compare to a specific SHA1
# (the merge-base by default), so doesn't include untracked files.
# So we use 'git ls-files --directory --others --exclude-standard'
# here directly.
paths = scm.GIT.Capture([
'-c', 'core.quotePath=false', 'ls-files', '--directory',
'--others', '--exclude-standard'
], self.checkout_path)
for path in (p for p in paths.splitlines() if p.endswith('/')):
full_path = os.path.join(self.checkout_path, path)
if not os.path.islink(full_path):
self.Print('_____ removing unversioned directory %s' % path)
gclient_utils.rmtree(full_path)
if not current_revision:
current_revision = self._Capture(['rev-parse', '--verify', 'HEAD'])
if verbose:
self.Print(f'Checked out revision {current_revision}',
timestamp=False)
return current_revision
def revert(self, options, _args, file_list):
"""Reverts local modifications.
All reverted files will be appended to file_list.
"""
if not os.path.isdir(self.checkout_path):
# revert won't work if the directory doesn't exist. It needs to
# checkout instead.
self.Print('_____ %s is missing, syncing instead' % self.relpath)
# Don't reuse the args.
return self.update(options, [], file_list)
default_rev = "refs/heads/main"
if options.upstream:
if self._GetCurrentBranch():
upstream_branch = scm.GIT.GetUpstreamBranch(self.checkout_path)
default_rev = upstream_branch or default_rev
_, deps_revision = gclient_utils.SplitUrlRevision(self.url)
if not deps_revision:
deps_revision = default_rev
if deps_revision.startswith('refs/heads/'):
deps_revision = deps_revision.replace('refs/heads/',
self.remote + '/')
try:
deps_revision = self.GetUsableRev(deps_revision, options)
except NoUsableRevError as e:
# If the DEPS entry's url and hash changed, try to update the
# origin. See also http://crbug.com/520067.
logging.warning(
"Couldn't find usable revision, will retrying to update instead: %s",
str(e))
return self.update(options, [], file_list)
if file_list is not None:
files = self._GetDiffFilenames(deps_revision)
self._Scrub(deps_revision, options)
self._Run(['clean', '-f', '-d'], options)
if file_list is not None:
file_list.extend(
[os.path.join(self.checkout_path, f) for f in files])
def revinfo(self, _options, _args, _file_list):
"""Returns revision"""
return self._Capture(['rev-parse', 'HEAD'])
def runhooks(self, options, args, file_list):
self.status(options, args, file_list)
def status(self, options, _args, file_list):
"""Display status information."""
if not os.path.isdir(self.checkout_path):
self.Print('________ couldn\'t run status in %s:\n'
'The directory does not exist.' % self.checkout_path)
else:
merge_base = []
if self.url:
_, base_rev = gclient_utils.SplitUrlRevision(self.url)
if base_rev:
if base_rev.startswith('refs/'):
base_rev = self._ref_to_remote_ref(base_rev)
merge_base = [base_rev]
self._Run(['-c', 'core.quotePath=false', 'diff', '--name-status'] +
merge_base,
options,
always_show_header=options.verbose)
if file_list is not None:
files = self._GetDiffFilenames(
merge_base[0] if merge_base else None)
file_list.extend(
[os.path.join(self.checkout_path, f) for f in files])
def GetUsableRev(self, rev, options):
"""Finds a useful revision for this repository."""
sha1 = None
if not os.path.isdir(self.checkout_path):
raise NoUsableRevError(
'This is not a git repo, so we cannot get a usable rev.')
if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
sha1 = rev
else:
# May exist in origin, but we don't have it yet, so fetch and look
# again.
self._Fetch(options)
if scm.GIT.IsValidRevision(cwd=self.checkout_path, rev=rev):
sha1 = rev
if not sha1:
raise NoUsableRevError(
'Hash %s does not appear to be a valid hash in this repo.' %
rev)
return sha1
def GetGitBackupDirPath(self):
"""Returns the path where the .git folder for the current project can be
staged/restored. Use case: subproject moved from DEPS <-> outer project."""
return os.path.join(self._root_dir,
'old_' + self.relpath.replace(os.sep, '_')) + '.git'
def _GetMirror(self, url, options, revision=None, revision_ref=None):
"""Get a git_cache.Mirror object for the argument url."""
if not self.cache_dir:
return None
mirror_kwargs = {
'print_func': self.filter,
'refs': [],
'commits': [],
}
if hasattr(options, 'with_branch_heads') and options.with_branch_heads:
mirror_kwargs['refs'].append('refs/branch-heads/*')
elif revision_ref and revision_ref.startswith('refs/branch-heads/'):
mirror_kwargs['refs'].append(revision_ref)
if hasattr(options, 'with_tags') and options.with_tags:
mirror_kwargs['refs'].append('refs/tags/*')
elif revision_ref and revision_ref.startswith('refs/tags/'):
mirror_kwargs['refs'].append(revision_ref)
if revision and not revision.startswith('refs/'):
mirror_kwargs['commits'].append(revision)
return git_cache.Mirror(url, **mirror_kwargs)
def _UpdateMirrorIfNotContains(self, mirror, options, rev_type, revision):
"""Update a git mirror unless it already contains a hash revision.
This raises an error if a SHA-1 revision isn't present even after
fetching from the remote.
"""
# 'hash' is overloaded and can refer to a SHA-1 hash or refs/changes/*.
is_sha = gclient_utils.IsFullGitSha(revision)
if rev_type == 'hash' and is_sha and mirror.contains_revision(revision):
if options.verbose:
self.Print('skipping mirror update, it has rev=%s already' %
revision,
timestamp=False)
return
if getattr(options, 'shallow', False):
depth = 10000
else:
depth = None
mirror.populate(verbose=False,
bootstrap=not getattr(options, 'no_bootstrap', False),
depth=depth,
lock_timeout=getattr(options, 'lock_timeout', 0))
# Make sure we've actually fetched the revision we want, but only if it
# was specified as an explicit commit hash.
if rev_type == 'hash' and is_sha and not mirror.contains_revision(
revision):
raise gclient_utils.Error(f'Failed to fetch {revision}.')
def _Clone(self, revision, url, options):
"""Clone a git repository from the given URL.
Once we've cloned the repo, we checkout a working branch if the
specified revision is a branch head. If it is a tag or a specific
commit, then we leave HEAD detached as it makes future updates simpler
-- in this case the user should first create a new branch or switch to
an existing branch before making changes in the repo."""
if self.print_outbuf:
print_stdout = True
filter_fn = None
else:
print_stdout = False
filter_fn = self.filter
if not options.verbose:
# git clone doesn't seem to insert a newline properly before
# printing to stdout
self.Print('')
# If the parent directory does not exist, Git clone on Windows will not
# create it, so we need to do it manually.
parent_dir = os.path.dirname(self.checkout_path)
gclient_utils.safe_makedirs(parent_dir)
if hasattr(options, 'no_history') and options.no_history:
self._Run(['init', self.checkout_path], options, cwd=self._root_dir)
self._Run(['remote', 'add', 'origin', url], options)
revision = self._AutoFetchRef(options, revision, depth=1)
remote_ref = scm.GIT.RefToRemoteRef(revision, self.remote)
self._Checkout(options, ''.join(remote_ref or revision), quiet=True)
else:
cfg = gclient_utils.DefaultIndexPackConfig(url)
clone_cmd = cfg + ['clone', '--no-checkout', '--progress']
if self.cache_dir:
clone_cmd.append('--shared')
if options.verbose:
clone_cmd.append('--verbose')
clone_cmd.append(url)
tmp_dir = tempfile.mkdtemp(prefix='_gclient_%s_' %
os.path.basename(self.checkout_path),
dir=parent_dir)
clone_cmd.append(tmp_dir)
try:
self._Run(clone_cmd,
options,
cwd=self._root_dir,
retry=True,
print_stdout=print_stdout,
filter_fn=filter_fn)
logging.debug(
'Cloned into temporary dir, moving to checkout_path')
gclient_utils.safe_makedirs(self.checkout_path)
gclient_utils.safe_rename(
os.path.join(tmp_dir, '.git'),
os.path.join(self.checkout_path, '.git'))
except:
traceback.print_exc(file=self.out_fh)
raise
finally:
if os.listdir(tmp_dir):
self.Print('_____ removing non-empty tmp dir %s' % tmp_dir)
gclient_utils.rmtree(tmp_dir)
self._SetFetchConfig(options)
self._Fetch(options, prune=options.force)
revision = self._AutoFetchRef(options, revision)
remote_ref = scm.GIT.RefToRemoteRef(revision, self.remote)
self._Checkout(options, ''.join(remote_ref or revision), quiet=True)
if self._GetCurrentBranch() is None:
# Squelch git's very verbose detached HEAD warning and use our own
self.Print((
'Checked out %s to a detached HEAD. Before making any commits\n'
'in this repo, you should use \'git checkout <branch>\' to switch \n'
'to an existing branch or use \'git checkout %s -b <branch>\' to\n'
'create a new branch for your work.') % (revision, self.remote))
return revision
def _AskForData(self, prompt, options):
if options.jobs > 1:
self.Print(prompt)
raise gclient_utils.Error("Background task requires input. Rerun "
"gclient with --jobs=1 so that\n"
"interaction is possible.")
return gclient_utils.AskForData(prompt)
def _AttemptRebase(self,
upstream,
files,
options,
newbase=None,
branch=None,
printed_path=False,
merge=False):
"""Attempt to rebase onto either upstream or, if specified, newbase."""
if files is not None:
files.extend(self._GetDiffFilenames(upstream))
revision = upstream
if newbase:
revision = newbase
action = 'merge' if merge else 'rebase'
if not printed_path:
self.Print('_____ %s : Attempting %s onto %s...' %
(self.relpath, action, revision))
printed_path = True
else:
self.Print('Attempting %s onto %s...' % (action, revision))
if merge:
merge_output = self._Capture(['merge', revision])
if options.verbose:
self.Print(merge_output)
return
# Build the rebase command here using the args
# git rebase [options] [--onto <newbase>] <upstream> [<branch>]
rebase_cmd = ['rebase']
if options.verbose:
rebase_cmd.append('--verbose')
if newbase:
rebase_cmd.extend(['--onto', newbase])
rebase_cmd.append(upstream)
if branch:
rebase_cmd.append(branch)
try:
rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
except subprocess2.CalledProcessError as e:
if (re.match(
br'cannot rebase: you have unstaged changes', e.stderr
) or re.match(
br'cannot rebase: your index contains uncommitted changes',
e.stderr)):
while True:
rebase_action = self._AskForData(
'Cannot rebase because of unstaged changes.\n'
'\'git reset --hard HEAD\' ?\n'
'WARNING: destroys any uncommitted work in your current branch!'
' (y)es / (q)uit / (s)how : ', options)
if re.match(r'yes|y', rebase_action, re.I):
self._Scrub('HEAD', options)
# Should this be recursive?
rebase_output = scm.GIT.Capture(rebase_cmd,
cwd=self.checkout_path)
break
if re.match(r'quit|q', rebase_action, re.I):
raise gclient_utils.Error(
"Please merge or rebase manually\n"
"cd %s && git " % self.checkout_path +
"%s" % ' '.join(rebase_cmd))
if re.match(r'show|s', rebase_action, re.I):
self.Print('%s' % e.stderr.decode('utf-8').strip())
continue
gclient_utils.Error("Input not recognized")
continue
elif re.search(br'^CONFLICT', e.stdout, re.M):
raise gclient_utils.Error(
"Conflict while rebasing this branch.\n"
"Fix the conflict and run gclient again.\n"
"See 'man git-rebase' for details.\n")
else:
self.Print(e.stdout.decode('utf-8').strip())
self.Print('Rebase produced error output:\n%s' %
e.stderr.decode('utf-8').strip())
raise gclient_utils.Error(
"Unrecognized error, please merge or rebase "
"manually.\ncd %s && git " % self.checkout_path +
"%s" % ' '.join(rebase_cmd))
self.Print(rebase_output.strip())
if not options.verbose:
# Make the output a little prettier. It's nice to have some
# whitespace between projects when syncing.
self.Print('')
def _EnsureValidHeadObjectOrCheckout(self, revision, options, url):
# Special case handling if all 3 conditions are met:
# * the mirros have recently changed, but deps destination remains same,
# * the git histories of mirrors are conflicting. * git cache is used
# This manifests itself in current checkout having invalid HEAD commit
# on most git operations. Since git cache is used, just deleted the .git
# folder, and re-create it by cloning.
try:
return self._Capture(['rev-list', '-n', '1', 'HEAD'])
except subprocess2.CalledProcessError as e:
if (b'fatal: bad object HEAD' in e.stderr and self.cache_dir
and self.cache_dir in url):
self.Print(
('Likely due to DEPS change with git cache_dir, '
'the current commit points to no longer existing object.\n'
'%s' % e))
self._DeleteOrMove(options.force)
return self._Clone(revision, url, options)
raise
def _IsRebasing(self):
# Check for any of REBASE-i/REBASE-m/REBASE/AM. Unfortunately git
# doesn't have a plumbing command to determine whether a rebase is in
# progress, so for now emualate (more-or-less) git-rebase.sh /
# git-completion.bash
g = os.path.join(self.checkout_path, '.git')
return (os.path.isdir(os.path.join(g, "rebase-merge"))
or os.path.isdir(os.path.join(g, "rebase-apply")))
def _CheckClean(self, revision):
lockfile = os.path.join(self.checkout_path, ".git", "index.lock")
if os.path.exists(lockfile):
raise gclient_utils.Error(
'\n____ %s at %s\n'
'\tYour repo is locked, possibly due to a concurrent git process.\n'
'\tIf no git executable is running, then clean up %r and try again.\n'
% (self.relpath, revision, lockfile))
# Ensure that the tree is clean.
if scm.GIT.Capture([
'status', '--porcelain', '--untracked-files=no',
'--ignore-submodules'
],
cwd=self.checkout_path):
raise gclient_utils.Error(
'\n____ %s at %s\n'
'\tYou have uncommitted changes.\n'
'\tcd into %s, run git status to see changes,\n'
'\tand commit, stash, or reset.\n' %
(self.relpath, revision, self.relpath))
def _CheckDetachedHead(self, revision, _options):
# HEAD is detached. Make sure it is safe to move away from (i.e., it is
# reference by a commit). If not, error out -- most likely a rebase is
# in progress, try to detect so we can give a better error.
try:
scm.GIT.Capture(['name-rev', '--no-undefined', 'HEAD'],
cwd=self.checkout_path)
except subprocess2.CalledProcessError:
# Commit is not contained by any rev. See if the user is rebasing:
if self._IsRebasing():
# Punt to the user
raise gclient_utils.Error(
'\n____ %s at %s\n'
'\tAlready in a conflict, i.e. (no branch).\n'
'\tFix the conflict and run gclient again.\n'
'\tOr to abort run:\n\t\tgit-rebase --abort\n'
'\tSee man git-rebase for details.\n' %
(self.relpath, revision))
# Let's just save off the commit so we can proceed.
name = ('saved-by-gclient-' +
self._Capture(['rev-parse', '--short', 'HEAD']))
self._Capture(['branch', '-f', name])
self.Print(
'_____ found an unreferenced commit and saved it as \'%s\'' %
name)
def _GetCurrentBranch(self):
# Returns name of current branch or None for detached HEAD
branch = self._Capture(['rev-parse', '--abbrev-ref=strict', 'HEAD'])
if branch == 'HEAD':
return None
return branch
def _Capture(self, args, **kwargs):
set_git_dir = 'cwd' not in kwargs
kwargs.setdefault('cwd', self.checkout_path)
kwargs.setdefault('stderr', subprocess2.PIPE)
strip = kwargs.pop('strip', True)
env = scm.GIT.ApplyEnvVars(kwargs)
# If an explicit cwd isn't set, then default to the .git/ subdir so we
# get stricter behavior. This can be useful in cases of slight
# corruption -- we don't accidentally go corrupting parent git checks
# too. See https://crbug.com/1000825 for an example.
if set_git_dir:
env.setdefault(
'GIT_DIR',
os.path.abspath(os.path.join(self.checkout_path, '.git')))
kwargs.setdefault('env', env)
ret = git_common.run(*args, **kwargs)
if strip:
ret = ret.strip()
self.Print('Finished running: %s %s' % ('git', ' '.join(args)))
return ret
def _Checkout(self, options, ref, force=False, quiet=None):
"""Performs a 'git-checkout' operation.
Args:
options: The configured option set
ref: (str) The branch/commit to checkout
quiet: (bool/None) Whether or not the checkout should pass '--quiet'; if
'None', the behavior is inferred from 'options.verbose'.
Returns: (str) The output of the checkout operation
"""
if quiet is None:
quiet = (not options.verbose)
checkout_args = ['checkout']
if force:
checkout_args.append('--force')
if quiet:
checkout_args.append('--quiet')
checkout_args.append(ref)
return self._Capture(checkout_args)
def _Fetch(self,
options,
remote=None,
prune=False,
quiet=False,
refspec=None,
depth=None):
cfg = gclient_utils.DefaultIndexPackConfig(self.url)
# When updating, the ref is modified to be a remote ref .
# (e.g. refs/heads/NAME becomes refs/remotes/REMOTE/NAME).
# Try to reverse that mapping.
original_ref = scm.GIT.RemoteRefToRef(refspec, self.remote)
if original_ref:
refspec = original_ref + ':' + refspec
# When a mirror is configured, it only fetches
# refs/{heads,branch-heads,tags}/*.
# If asked to fetch other refs, we must fetch those directly from
# the repository, and not from the mirror.
if not original_ref.startswith(
('refs/heads/', 'refs/branch-heads/', 'refs/tags/')):
remote, _ = gclient_utils.SplitUrlRevision(self.url)
fetch_cmd = cfg + [
'fetch',
remote or self.remote,
]
if refspec:
fetch_cmd.append(refspec)
if prune:
fetch_cmd.append('--prune')
if options.verbose:
fetch_cmd.append('--verbose')
if not hasattr(options, 'with_tags') or not options.with_tags:
fetch_cmd.append('--no-tags')
elif quiet:
fetch_cmd.append('--quiet')
if depth:
fetch_cmd.append('--depth=' + str(depth))
self._Run(fetch_cmd, options, show_header=options.verbose, retry=True)
def _SetFetchConfig(self, options):
"""Adds, and optionally fetches, "branch-heads" and "tags" refspecs
if requested."""
if options.force or options.reset:
try:
scm.GIT.SetConfig(self.checkout_path,
f'remote.{self.remote}.fetch',
modify_all=True)
scm.GIT.SetConfig(
self.checkout_path, f'remote.{self.remote}.fetch',
f'+refs/heads/*:refs/remotes/{self.remote}/*')
except subprocess2.CalledProcessError as e:
# If exit code was 5, it means we attempted to unset a config
# that didn't exist. Ignore it.
if e.returncode != 5:
raise
if hasattr(options, 'with_branch_heads') and options.with_branch_heads:
scm.GIT.SetConfig(
self.checkout_path,
f'remote.{self.remote}.fetch',
'+refs/branch-heads/*:refs/remotes/branch-heads/*',
value_pattern='^\\+refs/branch-heads/\\*:.*$',
modify_all=True)
if hasattr(options, 'with_tags') and options.with_tags:
scm.GIT.SetConfig(self.checkout_path,
f'remote.{self.remote}.fetch',
'+refs/tags/*:refs/tags/*',
value_pattern='^\\+refs/tags/\\*:.*$',
modify_all=True)
def _AutoFetchRef(self, options, revision, depth=None):
"""Attempts to fetch |revision| if not available in local repo.
Returns possibly updated revision."""
if not scm.GIT.IsValidRevision(self.checkout_path, revision):
self._Fetch(options, refspec=revision, depth=depth)
revision = self._Capture(['rev-parse', 'FETCH_HEAD'])
return revision
def _Run(self, args, options, **kwargs):
# Disable 'unused options' warning | pylint: disable=unused-argument
kwargs.setdefault('cwd', self.checkout_path)
kwargs.setdefault('filter_fn', self.filter)
kwargs.setdefault('show_header', True)
env = scm.GIT.ApplyEnvVars(kwargs)
cmd = ['git'] + args
gclient_utils.CheckCallAndFilter(cmd, env=env, **kwargs)
class CipdPackage(object):
"""A representation of a single CIPD package."""
def __init__(self, name, version, authority_for_subdir):
self._authority_for_subdir = authority_for_subdir
self._name = name
self._version = version
@property
def authority_for_subdir(self):
"""Whether this package has authority to act on behalf of its subdir.
Some operations should only be performed once per subdirectory. A package
that has authority for its subdirectory is the only package that should
perform such operations.
Returns:
bool; whether this package has subdir authority.
"""
return self._authority_for_subdir
@property
def name(self):
return self._name
@property
def version(self):
return self._version
class CipdRoot(object):
"""A representation of a single CIPD root."""
def __init__(self, root_dir, service_url, log_level=None):
self._all_packages = set()
self._mutator_lock = threading.Lock()
self._packages_by_subdir = collections.defaultdict(list)
self._root_dir = root_dir
self._service_url = service_url
self._resolved_packages = None
self._log_level = log_level or 'error'
def add_package(self, subdir, package, version):
"""Adds a package to this CIPD root.
As far as clients are concerned, this grants both root and subdir authority
to packages arbitrarily. (The implementation grants root authority to the
first package added and subdir authority to the first package added for that
subdir, but clients should not depend on or expect that behavior.)
Args:
subdir: str; relative path to where the package should be installed from
the cipd root directory.
package: str; the cipd package name.
version: str; the cipd package version.
Returns:
CipdPackage; the package that was created and added to this root.
"""
with self._mutator_lock:
cipd_package = CipdPackage(package, version,
not self._packages_by_subdir[subdir])
self._all_packages.add(cipd_package)
self._packages_by_subdir[subdir].append(cipd_package)
return cipd_package
def packages(self, subdir):
"""Get the list of configured packages for the given subdir."""
return list(self._packages_by_subdir[subdir])
def resolved_packages(self):
if not self._resolved_packages:
self._resolved_packages = self.ensure_file_resolve()
return self._resolved_packages
def clobber(self):
"""Remove the .cipd directory.
This is useful for forcing ensure to redownload and reinitialize all
packages.
"""
with self._mutator_lock:
cipd_cache_dir = os.path.join(self.root_dir, '.cipd')
try:
gclient_utils.rmtree(os.path.join(cipd_cache_dir))
except OSError:
if os.path.exists(cipd_cache_dir):
raise
def expand_package_name(self, package_name_string, **kwargs):
"""Run `cipd expand-package-name`.
CIPD package names can be declared with placeholder variables
such as '${platform}', this cmd will return the package name
with the variables resolved. The resolution is based on the host
the command is executing on.
"""
kwargs.setdefault('stderr', subprocess2.PIPE)
cmd = ['cipd', 'expand-package-name', package_name_string]
ret = subprocess2.check_output(cmd, **kwargs).decode('utf-8')
return ret.strip()
@contextlib.contextmanager
def _create_ensure_file(self):
try:
contents = '$ParanoidMode CheckPresence\n'
# TODO(crbug/1329641): Remove once cipd packages have been updated
# to always be created in copy mode.
contents += '$OverrideInstallMode copy\n\n'
for subdir, packages in sorted(self._packages_by_subdir.items()):
contents += '@Subdir %s\n' % subdir
for package in sorted(packages, key=lambda p: p.name):
contents += '%s %s\n' % (package.name, package.version)
contents += '\n'
ensure_file = None
with tempfile.NamedTemporaryFile(suffix='.ensure',
delete=False,
mode='wb') as ensure_file:
ensure_file.write(contents.encode('utf-8', 'replace'))
yield ensure_file.name
finally:
if ensure_file is not None and os.path.exists(ensure_file.name):
os.remove(ensure_file.name)
def ensure(self):
"""Run `cipd ensure`."""
with self._mutator_lock:
with self._create_ensure_file() as ensure_file:
cmd = [
'cipd',
'ensure',
'-log-level',
self._log_level,
'-root',
self.root_dir,
'-ensure-file',
ensure_file,
]
gclient_utils.CheckCallAndFilter(cmd,
print_stdout=True,
show_header=True)
@contextlib.contextmanager
def _create_ensure_file_for_resolve(self):
try:
contents = '$ResolvedVersions %s\n' % os.devnull
for subdir, packages in sorted(self._packages_by_subdir.items()):
contents += '@Subdir %s\n' % subdir
for package in sorted(packages, key=lambda p: p.name):
contents += '%s %s\n' % (package.name, package.version)
contents += '\n'
ensure_file = None
with tempfile.NamedTemporaryFile(suffix='.ensure',
delete=False,
mode='wb') as ensure_file:
ensure_file.write(contents.encode('utf-8', 'replace'))
yield ensure_file.name
finally:
if ensure_file is not None and os.path.exists(ensure_file.name):
os.remove(ensure_file.name)
def _create_resolved_file(self):
return tempfile.NamedTemporaryFile(suffix='.resolved',
delete=False,
mode='wb')
def ensure_file_resolve(self):
"""Run `cipd ensure-file-resolve`."""
with self._mutator_lock:
with self._create_resolved_file() as output_file:
with self._create_ensure_file_for_resolve() as ensure_file:
cmd = [
'cipd',
'ensure-file-resolve',
'-log-level',
self._log_level,
'-ensure-file',
ensure_file,
'-json-output',
output_file.name,
]
gclient_utils.CheckCallAndFilter(cmd,
print_stdout=False,
show_header=False)
with open(output_file.name) as f:
output_json = json.load(f)
return output_json.get('result', {})
def run(self, command):
if command == 'update':
self.ensure()
elif command == 'revert':
self.clobber()
self.ensure()
def created_package(self, package):
"""Checks whether this root created the given package.
Args:
package: CipdPackage; the package to check.
Returns:
bool; whether this root created the given package.
"""
return package in self._all_packages
@property
def root_dir(self):
return self._root_dir
@property
def service_url(self):
return self._service_url
class CipdWrapper(SCMWrapper):
"""Wrapper for CIPD.
Currently only supports chrome-infra-packages.appspot.com.
"""
name = 'cipd'
def __init__(self,
url=None,
root_dir=None,
relpath=None,
out_fh=None,
out_cb=None,
root=None,
package=None):
super(CipdWrapper, self).__init__(url=url,
root_dir=root_dir,
relpath=relpath,
out_fh=out_fh,
out_cb=out_cb)
assert root.created_package(package)
self._package = package
self._root = root
#override
def GetCacheMirror(self):
return None
#override
def GetActualRemoteURL(self, options):
return self._root.service_url
#override
def DoesRemoteURLMatch(self, options):
del options
return True
def revert(self, options, args, file_list):
"""Does nothing.
CIPD packages should be reverted at the root by running
`CipdRoot.run('revert')`.
"""
def diff(self, options, args, file_list):
"""CIPD has no notion of diffing."""
def pack(self, options, args, file_list):
"""CIPD has no notion of diffing."""
def revinfo(self, options, args, file_list):
"""Grab the instance ID."""
try:
tmpdir = tempfile.mkdtemp()
# Attempt to get instance_id from the root resolved cache.
# Resolved cache will not match on any CIPD packages with
# variables such as ${platform}, they will fall back to
# the slower method below.
resolved = self._root.resolved_packages()
if resolved:
# CIPD uses POSIX separators across all platforms, so
# replace any Windows separators.
path_split = self.relpath.replace(os.sep, "/").split(":")
if len(path_split) > 1:
src_path, package = path_split
if src_path in resolved:
for resolved_package in resolved[src_path]:
if package == resolved_package.get(
'pin', {}).get('package'):
return resolved_package.get(
'pin', {}).get('instance_id')
describe_json_path = os.path.join(tmpdir, 'describe.json')
cmd = [
'cipd', 'describe', self._package.name, '-log-level', 'error',
'-version', self._package.version, '-json-output',
describe_json_path
]
gclient_utils.CheckCallAndFilter(cmd)
with open(describe_json_path) as f:
describe_json = json.load(f)
return describe_json.get('result', {}).get('pin',
{}).get('instance_id')
finally:
gclient_utils.rmtree(tmpdir)
def status(self, options, args, file_list):
pass
def update(self, options, args, file_list):
"""Does nothing.
CIPD packages should be updated at the root by running
`CipdRoot.run('update')`.
"""
class GcsRoot(object):
"""Root to keep track of all GCS objects, per checkout"""
def __init__(self, root_dir):
self._mutator_lock = threading.Lock()
self._root_dir = root_dir
# Populated when the DEPS file is parsed
# The objects here have not yet been downloaded and written into
# the .gcs_entries file
self._parsed_objects = {}
# .gcs_entries keeps track of which GCS deps have already been installed
# Maps checkout_name -> {GCS dep path -> [object_name]}
# This file is in the same directory as .gclient
self._gcs_entries_file = os.path.join(self._root_dir, '.gcs_entries')
# Contents of the .gcs_entries file
self._gcs_entries = self.read_gcs_entries()
@property
def root_dir(self):
return self._root_dir
def add_object(self, checkout_name, dep_path, object_name):
"""Records the object in the _parsed_objects variable
This does not actually download the object"""
with self._mutator_lock:
if checkout_name not in self._parsed_objects:
self._parsed_objects[checkout_name] = {}
if dep_path not in self._parsed_objects[checkout_name]:
self._parsed_objects[checkout_name][dep_path] = [object_name]
else:
self._parsed_objects[checkout_name][dep_path].append(
object_name)
def read_gcs_entries(self):
"""Reads .gcs_entries file and loads the content into _gcs_entries"""
if not os.path.exists(self._gcs_entries_file):
return {}
with open(self._gcs_entries_file, 'r') as f:
content = f.read().rstrip()
if content:
return json.loads(content)
return {}
def resolve_objects(self, checkout_name):
"""Updates .gcs_entries with objects in _parsed_objects
This should only be called after the objects have been downloaded
and extracted."""
with self._mutator_lock:
object_dict = self._parsed_objects.get(checkout_name)
if not object_dict:
return
self._gcs_entries[checkout_name] = object_dict
with open(self._gcs_entries_file, 'w') as f:
f.write(json.dumps(self._gcs_entries, indent=2))
self._parsed_objects[checkout_name] = {}
def clobber_deps_with_updated_objects(self, checkout_name):
"""Clobber the path if an object or GCS dependency is removed/added
This must be called before the GCS dependencies are
downloaded and extracted."""
with self._mutator_lock:
parsed_object_dict = self._parsed_objects.get(checkout_name, {})
parsed_paths = set(parsed_object_dict.keys())
resolved_object_dict = self._gcs_entries.get(checkout_name, {})
resolved_paths = set(resolved_object_dict.keys())
# If any GCS deps are added or removed entirely, clobber that path
intersected_paths = parsed_paths.intersection(resolved_paths)
# If any objects within a GCS dep are added/removed, clobber its
# extracted contents and relevant gcs dotfiles
for path in intersected_paths:
resolved_objects = resolved_object_dict[path]
parsed_objects = parsed_object_dict[path]
full_path = os.path.join(self.root_dir, path)
if (len(resolved_objects) != len(parsed_objects)
and os.path.exists(full_path)):
self.clobber_tar_content_names(full_path)
self.clobber_hash_files(full_path)
self.clobber_migration_files(full_path)
def clobber_tar_content_names(self, entry_directory):
"""Delete paths written in .*_content_names files"""
content_names_files = glob.glob(
os.path.join(entry_directory, '.*_content_names'))
for file in content_names_files:
with open(file, 'r') as f:
names = json.loads(f.read().strip())
for name in names:
name_path = os.path.join(entry_directory, name)
if os.path.isdir(
name_path) or not os.path.exists(name_path):
continue
os.remove(os.path.join(entry_directory, name))
os.remove(file)
def clobber_hash_files(self, entry_directory):
files = glob.glob(os.path.join(entry_directory, '.*_hash'))
for f in files:
os.remove(f)
def clobber_migration_files(self, entry_directory):
files = glob.glob(os.path.join(entry_directory,
'.*_is_first_class_gcs'))
for f in files:
os.remove(f)
def clobber(self):
"""Remove all dep path gcs items and clear .gcs_entries"""
for _, objects_dict in self._gcs_entries.items():
for dep_path, _ in objects_dict.items():
full_path = os.path.join(self.root_dir, dep_path)
self.clobber_tar_content_names(full_path)
self.clobber_hash_files(full_path)
self.clobber_migration_files(full_path)
if os.path.exists(self._gcs_entries_file):
os.remove(self._gcs_entries_file)
with self._mutator_lock:
self._gcs_entries = {}
class GcsWrapper(SCMWrapper):
"""Wrapper for GCS.
Currently only supports content from Google Cloud Storage.
"""
name = 'gcs'
def __init__(self,
url=None,
root_dir=None,
relpath=None,
out_fh=None,
out_cb=None):
super(GcsWrapper, self).__init__(url=url,
root_dir=root_dir,
relpath=relpath,
out_fh=out_fh,
out_cb=out_cb)
#override
def GetCacheMirror(self):
return None
#override
def GetActualRemoteURL(self, options):
return None
#override
def DoesRemoteURLMatch(self, options):
del options
return True
def revert(self, options, args, file_list):
"""Does nothing."""
def diff(self, options, args, file_list):
"""GCS has no notion of diffing."""
def pack(self, options, args, file_list):
"""GCS has no notion of diffing."""
def revinfo(self, options, args, file_list):
"""Does nothing"""
def status(self, options, args, file_list):
pass
def update(self, options, args, file_list):
"""Does nothing."""
class CogWrapper(SCMWrapper):
"""Wrapper for Cog, all no-op."""
name = 'cog'
def __init__(self):
super(CogWrapper, self).__init__()
#override
def GetCacheMirror(self):
return None
#override
def GetActualRemoteURL(self, options):
return None
#override
def GetSubmoduleDiff(self):
return None
#override
def GetSubmoduleStateFromIndex(self):
return None
#override
def DoesRemoteURLMatch(self, options):
del options
return True
def revert(self, options, args, file_list):
pass
def diff(self, options, args, file_list):
pass
def pack(self, options, args, file_list):
pass
def revinfo(self, options, args, file_list):
pass
def status(self, options, args, file_list):
pass
def update(self, options, args, file_list):
pass
|