scm.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """SCM-specific utility classes."""
  5. import glob
  6. import io
  7. import os
  8. import platform
  9. import re
  10. import sys
  11. import gclient_utils
  12. import subprocess2
  13. def ValidateEmail(email):
  14. return (
  15. re.match(r"^[a-zA-Z0-9._%\-+]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}$", email)
  16. is not None)
  17. def GetCasedPath(path):
  18. """Elcheapos way to get the real path case on Windows."""
  19. if sys.platform.startswith('win') and os.path.exists(path):
  20. # Reconstruct the path.
  21. path = os.path.abspath(path)
  22. paths = path.split('\\')
  23. for i in range(len(paths)):
  24. if i == 0:
  25. # Skip drive letter.
  26. continue
  27. subpath = '\\'.join(paths[:i+1])
  28. prev = len('\\'.join(paths[:i]))
  29. # glob.glob will return the cased path for the last item only. This is why
  30. # we are calling it in a loop. Extract the data we want and put it back
  31. # into the list.
  32. paths[i] = glob.glob(subpath + '*')[0][prev+1:len(subpath)]
  33. path = '\\'.join(paths)
  34. return path
  35. def GenFakeDiff(filename):
  36. """Generates a fake diff from a file."""
  37. file_content = gclient_utils.FileRead(filename, 'rb').splitlines(True)
  38. filename = filename.replace(os.sep, '/')
  39. nb_lines = len(file_content)
  40. # We need to use / since patch on unix will fail otherwise.
  41. data = io.StringIO()
  42. data.write("Index: %s\n" % filename)
  43. data.write('=' * 67 + '\n')
  44. # Note: Should we use /dev/null instead?
  45. data.write("--- %s\n" % filename)
  46. data.write("+++ %s\n" % filename)
  47. data.write("@@ -0,0 +1,%d @@\n" % nb_lines)
  48. # Prepend '+' to every lines.
  49. for line in file_content:
  50. data.write('+')
  51. data.write(line)
  52. result = data.getvalue()
  53. data.close()
  54. return result
  55. def determine_scm(root):
  56. """Similar to upload.py's version but much simpler.
  57. Returns 'git' or None.
  58. """
  59. if os.path.isdir(os.path.join(root, '.git')):
  60. return 'git'
  61. else:
  62. try:
  63. subprocess2.check_call(
  64. ['git', 'rev-parse', '--show-cdup'],
  65. stdout=subprocess2.VOID,
  66. stderr=subprocess2.VOID,
  67. cwd=root)
  68. return 'git'
  69. except (OSError, subprocess2.CalledProcessError):
  70. return None
  71. def only_int(val):
  72. if val.isdigit():
  73. return int(val)
  74. else:
  75. return 0
  76. class GIT(object):
  77. current_version = None
  78. @staticmethod
  79. def ApplyEnvVars(kwargs):
  80. env = kwargs.pop('env', None) or os.environ.copy()
  81. # Don't prompt for passwords; just fail quickly and noisily.
  82. # By default, git will use an interactive terminal prompt when a username/
  83. # password is needed. That shouldn't happen in the chromium workflow,
  84. # and if it does, then gclient may hide the prompt in the midst of a flood
  85. # of terminal spew. The only indication that something has gone wrong
  86. # will be when gclient hangs unresponsively. Instead, we disable the
  87. # password prompt and simply allow git to fail noisily. The error
  88. # message produced by git will be copied to gclient's output.
  89. env.setdefault('GIT_ASKPASS', 'true')
  90. env.setdefault('SSH_ASKPASS', 'true')
  91. # 'cat' is a magical git string that disables pagers on all platforms.
  92. env.setdefault('GIT_PAGER', 'cat')
  93. return env
  94. @staticmethod
  95. def Capture(args, cwd, strip_out=True, **kwargs):
  96. env = GIT.ApplyEnvVars(kwargs)
  97. output = subprocess2.check_output(
  98. ['git'] + args, cwd=cwd, stderr=subprocess2.PIPE, env=env,
  99. **kwargs).decode('utf-8', 'replace')
  100. return output.strip() if strip_out else output
  101. @staticmethod
  102. def CaptureStatus(files, cwd, upstream_branch):
  103. """Returns git status.
  104. @files is a list of files.
  105. Returns an array of (status, file) tuples."""
  106. if upstream_branch is None:
  107. upstream_branch = GIT.GetUpstreamBranch(cwd)
  108. if upstream_branch is None:
  109. raise gclient_utils.Error('Cannot determine upstream branch')
  110. command = ['-c', 'core.quotePath=false', 'diff',
  111. '--name-status', '--no-renames', '-r', '%s...' % upstream_branch]
  112. if files:
  113. command.extend(files)
  114. status = GIT.Capture(command, cwd)
  115. results = []
  116. if status:
  117. for statusline in status.splitlines():
  118. # 3-way merges can cause the status can be 'MMM' instead of 'M'. This
  119. # can happen when the user has 2 local branches and he diffs between
  120. # these 2 branches instead diffing to upstream.
  121. m = re.match(r'^(\w)+\t(.+)$', statusline)
  122. if not m:
  123. raise gclient_utils.Error(
  124. 'status currently unsupported: %s' % statusline)
  125. # Only grab the first letter.
  126. results.append(('%s ' % m.group(1)[0], m.group(2)))
  127. return results
  128. @staticmethod
  129. def IsWorkTreeDirty(cwd):
  130. return GIT.Capture(['status', '-s'], cwd=cwd) != ''
  131. @staticmethod
  132. def GetEmail(cwd):
  133. """Retrieves the user email address if known."""
  134. try:
  135. return GIT.Capture(['config', 'user.email'], cwd=cwd)
  136. except subprocess2.CalledProcessError:
  137. return ''
  138. @staticmethod
  139. def ShortBranchName(branch):
  140. """Converts a name like 'refs/heads/foo' to just 'foo'."""
  141. return branch.replace('refs/heads/', '')
  142. @staticmethod
  143. def GetBranchRef(cwd):
  144. """Returns the full branch reference, e.g. 'refs/heads/master'."""
  145. return GIT.Capture(['symbolic-ref', 'HEAD'], cwd=cwd)
  146. @staticmethod
  147. def GetBranch(cwd):
  148. """Returns the short branch name, e.g. 'master'."""
  149. return GIT.ShortBranchName(GIT.GetBranchRef(cwd))
  150. @staticmethod
  151. def FetchUpstreamTuple(cwd):
  152. """Returns a tuple containg remote and remote ref,
  153. e.g. 'origin', 'refs/heads/master'
  154. """
  155. remote = '.'
  156. branch = GIT.GetBranch(cwd)
  157. try:
  158. upstream_branch = GIT.Capture(
  159. ['config', '--local', 'branch.%s.merge' % branch], cwd=cwd)
  160. except subprocess2.CalledProcessError:
  161. upstream_branch = None
  162. if upstream_branch:
  163. try:
  164. remote = GIT.Capture(
  165. ['config', '--local', 'branch.%s.remote' % branch], cwd=cwd)
  166. except subprocess2.CalledProcessError:
  167. pass
  168. else:
  169. try:
  170. upstream_branch = GIT.Capture(
  171. ['config', '--local', 'rietveld.upstream-branch'], cwd=cwd)
  172. except subprocess2.CalledProcessError:
  173. upstream_branch = None
  174. if upstream_branch:
  175. try:
  176. remote = GIT.Capture(
  177. ['config', '--local', 'rietveld.upstream-remote'], cwd=cwd)
  178. except subprocess2.CalledProcessError:
  179. pass
  180. else:
  181. # Else, try to guess the origin remote.
  182. remote_branches = GIT.Capture(['branch', '-r'], cwd=cwd).split()
  183. if 'origin/master' in remote_branches:
  184. # Fall back on origin/master if it exits.
  185. remote = 'origin'
  186. upstream_branch = 'refs/heads/master'
  187. else:
  188. # Give up.
  189. remote = None
  190. upstream_branch = None
  191. return remote, upstream_branch
  192. @staticmethod
  193. def RefToRemoteRef(ref, remote):
  194. """Convert a checkout ref to the equivalent remote ref.
  195. Returns:
  196. A tuple of the remote ref's (common prefix, unique suffix), or None if it
  197. doesn't appear to refer to a remote ref (e.g. it's a commit hash).
  198. """
  199. # TODO(mmoss): This is just a brute-force mapping based of the expected git
  200. # config. It's a bit better than the even more brute-force replace('heads',
  201. # ...), but could still be smarter (like maybe actually using values gleaned
  202. # from the git config).
  203. m = re.match('^(refs/(remotes/)?)?branch-heads/', ref or '')
  204. if m:
  205. return ('refs/remotes/branch-heads/', ref.replace(m.group(0), ''))
  206. m = re.match('^((refs/)?remotes/)?%s/|(refs/)?heads/' % remote, ref or '')
  207. if m:
  208. return ('refs/remotes/%s/' % remote, ref.replace(m.group(0), ''))
  209. return None
  210. @staticmethod
  211. def RemoteRefToRef(ref, remote):
  212. assert remote, 'A remote must be given'
  213. if not ref or not ref.startswith('refs/'):
  214. return None
  215. if not ref.startswith('refs/remotes/'):
  216. return ref
  217. if ref.startswith('refs/remotes/branch-heads/'):
  218. return 'refs' + ref[len('refs/remotes'):]
  219. if ref.startswith('refs/remotes/%s/' % remote):
  220. return 'refs/heads' + ref[len('refs/remotes/%s' % remote):]
  221. return None
  222. @staticmethod
  223. def GetUpstreamBranch(cwd):
  224. """Gets the current branch's upstream branch."""
  225. remote, upstream_branch = GIT.FetchUpstreamTuple(cwd)
  226. if remote != '.' and upstream_branch:
  227. remote_ref = GIT.RefToRemoteRef(upstream_branch, remote)
  228. if remote_ref:
  229. upstream_branch = ''.join(remote_ref)
  230. return upstream_branch
  231. @staticmethod
  232. def IsAncestor(cwd, maybe_ancestor, ref):
  233. """Verifies if |maybe_ancestor| is an ancestor of |ref|."""
  234. try:
  235. GIT.Capture(['merge-base', '--is-ancestor', maybe_ancestor, ref], cwd=cwd)
  236. return True
  237. except subprocess2.CalledProcessError:
  238. return False
  239. @staticmethod
  240. def GetOldContents(cwd, filename, branch=None):
  241. if not branch:
  242. branch = GIT.GetUpstreamBranch(cwd)
  243. if platform.system() == 'Windows':
  244. # git show <sha>:<path> wants a posix path.
  245. filename = filename.replace('\\', '/')
  246. command = ['show', '%s:%s' % (branch, filename)]
  247. try:
  248. return GIT.Capture(command, cwd=cwd, strip_out=False)
  249. except subprocess2.CalledProcessError:
  250. return ''
  251. @staticmethod
  252. def GenerateDiff(cwd, branch=None, branch_head='HEAD', full_move=False,
  253. files=None):
  254. """Diffs against the upstream branch or optionally another branch.
  255. full_move means that move or copy operations should completely recreate the
  256. files, usually in the prospect to apply the patch for a try job."""
  257. if not branch:
  258. branch = GIT.GetUpstreamBranch(cwd)
  259. command = ['-c', 'core.quotePath=false', 'diff',
  260. '-p', '--no-color', '--no-prefix', '--no-ext-diff',
  261. branch + "..." + branch_head]
  262. if full_move:
  263. command.append('--no-renames')
  264. else:
  265. command.append('-C')
  266. # TODO(maruel): --binary support.
  267. if files:
  268. command.append('--')
  269. command.extend(files)
  270. diff = GIT.Capture(command, cwd=cwd, strip_out=False).splitlines(True)
  271. for i in range(len(diff)):
  272. # In the case of added files, replace /dev/null with the path to the
  273. # file being added.
  274. if diff[i].startswith('--- /dev/null'):
  275. diff[i] = '--- %s' % diff[i+1][4:]
  276. return ''.join(diff)
  277. @staticmethod
  278. def GetDifferentFiles(cwd, branch=None, branch_head='HEAD'):
  279. """Returns the list of modified files between two branches."""
  280. if not branch:
  281. branch = GIT.GetUpstreamBranch(cwd)
  282. command = ['-c', 'core.quotePath=false', 'diff',
  283. '--name-only', branch + "..." + branch_head]
  284. return GIT.Capture(command, cwd=cwd).splitlines(False)
  285. @staticmethod
  286. def GetPatchName(cwd):
  287. """Constructs a name for this patch."""
  288. short_sha = GIT.Capture(['rev-parse', '--short=4', 'HEAD'], cwd=cwd)
  289. return "%s#%s" % (GIT.GetBranch(cwd), short_sha)
  290. @staticmethod
  291. def GetCheckoutRoot(cwd):
  292. """Returns the top level directory of a git checkout as an absolute path.
  293. """
  294. root = GIT.Capture(['rev-parse', '--show-cdup'], cwd=cwd)
  295. return os.path.abspath(os.path.join(cwd, root))
  296. @staticmethod
  297. def GetGitDir(cwd):
  298. return os.path.abspath(GIT.Capture(['rev-parse', '--git-dir'], cwd=cwd))
  299. @staticmethod
  300. def IsInsideWorkTree(cwd):
  301. try:
  302. return GIT.Capture(['rev-parse', '--is-inside-work-tree'], cwd=cwd)
  303. except (OSError, subprocess2.CalledProcessError):
  304. return False
  305. @staticmethod
  306. def IsDirectoryVersioned(cwd, relative_dir):
  307. """Checks whether the given |relative_dir| is part of cwd's repo."""
  308. return bool(GIT.Capture(['ls-tree', 'HEAD', relative_dir], cwd=cwd))
  309. @staticmethod
  310. def CleanupDir(cwd, relative_dir):
  311. """Cleans up untracked file inside |relative_dir|."""
  312. return bool(GIT.Capture(['clean', '-df', relative_dir], cwd=cwd))
  313. @staticmethod
  314. def IsValidRevision(cwd, rev, sha_only=False):
  315. """Verifies the revision is a proper git revision.
  316. sha_only: Fail unless rev is a sha hash.
  317. """
  318. if sys.platform.startswith('win'):
  319. # Windows .bat scripts use ^ as escape sequence, which means we have to
  320. # escape it with itself for every .bat invocation.
  321. needle = '%s^^^^{commit}' % rev
  322. else:
  323. needle = '%s^{commit}' % rev
  324. try:
  325. sha = GIT.Capture(['rev-parse', '--verify', needle], cwd=cwd)
  326. if sha_only:
  327. return sha == rev.lower()
  328. return True
  329. except subprocess2.CalledProcessError:
  330. return False
  331. @classmethod
  332. def AssertVersion(cls, min_version):
  333. """Asserts git's version is at least min_version."""
  334. if cls.current_version is None:
  335. current_version = cls.Capture(['--version'], '.')
  336. matched = re.search(r'version ([0-9\.]+)', current_version)
  337. cls.current_version = matched.group(1)
  338. current_version_list = list(map(only_int, cls.current_version.split('.')))
  339. for min_ver in map(int, min_version.split('.')):
  340. ver = current_version_list.pop(0)
  341. if ver < min_ver:
  342. return (False, cls.current_version)
  343. elif ver > min_ver:
  344. return (True, cls.current_version)
  345. return (True, cls.current_version)