lastchange.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """
  6. lastchange.py -- Chromium revision fetching utility.
  7. """
  8. from __future__ import print_function
  9. import argparse
  10. import collections
  11. import datetime
  12. import logging
  13. import os
  14. import subprocess
  15. import sys
  16. VersionInfo = collections.namedtuple("VersionInfo",
  17. ("revision_id", "revision", "timestamp"))
  18. class GitError(Exception):
  19. pass
  20. # This function exists for compatibility with logic outside this
  21. # repository that uses this file as a library.
  22. # TODO(eliribble) remove this function after it has been ported into
  23. # the repositories that depend on it
  24. def RunGitCommand(directory, command):
  25. """
  26. Launches git subcommand.
  27. Errors are swallowed.
  28. Returns:
  29. A process object or None.
  30. """
  31. command = ['git'] + command
  32. # Force shell usage under cygwin. This is a workaround for
  33. # mysterious loss of cwd while invoking cygwin's git.
  34. # We can't just pass shell=True to Popen, as under win32 this will
  35. # cause CMD to be used, while we explicitly want a cygwin shell.
  36. if sys.platform == 'cygwin':
  37. command = ['sh', '-c', ' '.join(command)]
  38. try:
  39. proc = subprocess.Popen(command,
  40. stdout=subprocess.PIPE,
  41. stderr=subprocess.PIPE,
  42. cwd=directory,
  43. shell=(sys.platform=='win32'))
  44. return proc
  45. except OSError as e:
  46. logging.error('Command %r failed: %s' % (' '.join(command), e))
  47. return None
  48. def _RunGitCommand(directory, command):
  49. """Launches git subcommand.
  50. Returns:
  51. The stripped stdout of the git command.
  52. Raises:
  53. GitError on failure, including a nonzero return code.
  54. """
  55. command = ['git'] + command
  56. # Force shell usage under cygwin. This is a workaround for
  57. # mysterious loss of cwd while invoking cygwin's git.
  58. # We can't just pass shell=True to Popen, as under win32 this will
  59. # cause CMD to be used, while we explicitly want a cygwin shell.
  60. if sys.platform == 'cygwin':
  61. command = ['sh', '-c', ' '.join(command)]
  62. try:
  63. logging.info("Executing '%s' in %s", ' '.join(command), directory)
  64. proc = subprocess.Popen(command,
  65. stdout=subprocess.PIPE,
  66. stderr=subprocess.PIPE,
  67. cwd=directory,
  68. shell=(sys.platform=='win32'))
  69. stdout, stderr = tuple(x.decode(encoding='utf_8')
  70. for x in proc.communicate())
  71. stdout = stdout.strip()
  72. logging.debug("returncode: %d", proc.returncode)
  73. logging.debug("stdout: %s", stdout)
  74. logging.debug("stderr: %s", stderr)
  75. if proc.returncode != 0 or not stdout:
  76. raise GitError((
  77. "Git command '{}' in {} failed: "
  78. "rc={}, stdout='{}' stderr='{}'").format(
  79. " ".join(command), directory, proc.returncode, stdout, stderr))
  80. return stdout
  81. except OSError as e:
  82. raise GitError("Git command 'git {}' in {} failed: {}".format(
  83. " ".join(command), directory, e))
  84. def GetMergeBase(directory, ref):
  85. """
  86. Return the merge-base of HEAD and ref.
  87. Args:
  88. directory: The directory containing the .git directory.
  89. ref: The ref to use to find the merge base.
  90. Returns:
  91. The git commit SHA of the merge-base as a string.
  92. """
  93. logging.debug("Calculating merge base between HEAD and %s in %s",
  94. ref, directory)
  95. command = ['merge-base', 'HEAD', ref]
  96. return _RunGitCommand(directory, command)
  97. def FetchGitRevision(directory, commit_filter, start_commit="HEAD"):
  98. """
  99. Fetch the Git hash (and Cr-Commit-Position if any) for a given directory.
  100. Args:
  101. directory: The directory containing the .git directory.
  102. commit_filter: A filter to supply to grep to filter commits
  103. start_commit: A commit identifier. The result of this function
  104. will be limited to only consider commits before the provided
  105. commit.
  106. Returns:
  107. A VersionInfo object. On error all values will be 0.
  108. """
  109. hash_ = ''
  110. git_args = ['log', '-1', '--format=%H %ct']
  111. if commit_filter is not None:
  112. git_args.append('--grep=' + commit_filter)
  113. git_args.append(start_commit)
  114. output = _RunGitCommand(directory, git_args)
  115. hash_, commit_timestamp = output.split()
  116. if not hash_:
  117. return VersionInfo('0', '0', 0)
  118. revision = hash_
  119. output = _RunGitCommand(directory, ['cat-file', 'commit', hash_])
  120. for line in reversed(output.splitlines()):
  121. if line.startswith('Cr-Commit-Position:'):
  122. pos = line.rsplit()[-1].strip()
  123. logging.debug("Found Cr-Commit-Position '%s'", pos)
  124. revision = "{}-{}".format(hash_, pos)
  125. break
  126. return VersionInfo(hash_, revision, int(commit_timestamp))
  127. def GetHeaderGuard(path):
  128. """
  129. Returns the header #define guard for the given file path.
  130. This treats everything after the last instance of "src/" as being a
  131. relevant part of the guard. If there is no "src/", then the entire path
  132. is used.
  133. """
  134. src_index = path.rfind('src/')
  135. if src_index != -1:
  136. guard = path[src_index + 4:]
  137. else:
  138. guard = path
  139. guard = guard.upper()
  140. return guard.replace('/', '_').replace('.', '_').replace('\\', '_') + '_'
  141. def GetHeaderContents(path, define, version):
  142. """
  143. Returns what the contents of the header file should be that indicate the given
  144. revision.
  145. """
  146. header_guard = GetHeaderGuard(path)
  147. header_contents = """/* Generated by lastchange.py, do not edit.*/
  148. #ifndef %(header_guard)s
  149. #define %(header_guard)s
  150. #define %(define)s "%(version)s"
  151. #endif // %(header_guard)s
  152. """
  153. header_contents = header_contents % { 'header_guard': header_guard,
  154. 'define': define,
  155. 'version': version }
  156. return header_contents
  157. def GetGitTopDirectory(source_dir):
  158. """Get the top git directory - the directory that contains the .git directory.
  159. Args:
  160. source_dir: The directory to search.
  161. Returns:
  162. The output of "git rev-parse --show-toplevel" as a string
  163. """
  164. return _RunGitCommand(source_dir, ['rev-parse', '--show-toplevel'])
  165. def WriteIfChanged(file_name, contents):
  166. """
  167. Writes the specified contents to the specified file_name
  168. iff the contents are different than the current contents.
  169. Returns if new data was written.
  170. """
  171. try:
  172. old_contents = open(file_name, 'r').read()
  173. except EnvironmentError:
  174. pass
  175. else:
  176. if contents == old_contents:
  177. return False
  178. os.unlink(file_name)
  179. open(file_name, 'w').write(contents)
  180. return True
  181. def main(argv=None):
  182. if argv is None:
  183. argv = sys.argv
  184. parser = argparse.ArgumentParser(usage="lastchange.py [options]")
  185. parser.add_argument("-m", "--version-macro",
  186. help=("Name of C #define when using --header. Defaults to "
  187. "LAST_CHANGE."))
  188. parser.add_argument("-o",
  189. "--output",
  190. metavar="FILE",
  191. help=("Write last change to FILE. "
  192. "Can be combined with other file-output-related "
  193. "options to write multiple files."))
  194. parser.add_argument("--header",
  195. metavar="FILE",
  196. help=("Write last change to FILE as a C/C++ header. "
  197. "Can be combined with other file-output-related "
  198. "options to write multiple files."))
  199. parser.add_argument("--revision",
  200. metavar="FILE",
  201. help=("Write last change to FILE as a one-line revision. "
  202. "Can be combined with other file-output-related "
  203. "options to write multiple files."))
  204. parser.add_argument("--merge-base-ref",
  205. default=None,
  206. help=("Only consider changes since the merge "
  207. "base between HEAD and the provided ref"))
  208. parser.add_argument("--revision-id-only", action='store_true',
  209. help=("Output the revision as a VCS revision ID only (in "
  210. "Git, a 40-character commit hash, excluding the "
  211. "Cr-Commit-Position)."))
  212. parser.add_argument("--revision-id-prefix",
  213. metavar="PREFIX",
  214. help=("Adds a string prefix to the VCS revision ID."))
  215. parser.add_argument("--print-only", action="store_true",
  216. help=("Just print the revision string. Overrides any "
  217. "file-output-related options."))
  218. parser.add_argument("-s", "--source-dir", metavar="DIR",
  219. help="Use repository in the given directory.")
  220. parser.add_argument("--filter", metavar="REGEX",
  221. help=("Only use log entries where the commit message "
  222. "matches the supplied filter regex. Defaults to "
  223. "'^Change-Id:' to suppress local commits."),
  224. default='^Change-Id:')
  225. args, extras = parser.parse_known_args(argv[1:])
  226. logging.basicConfig(level=logging.WARNING)
  227. out_file = args.output
  228. header = args.header
  229. revision = args.revision
  230. commit_filter=args.filter
  231. while len(extras) and out_file is None:
  232. if out_file is None:
  233. out_file = extras.pop(0)
  234. if extras:
  235. sys.stderr.write('Unexpected arguments: %r\n\n' % extras)
  236. parser.print_help()
  237. sys.exit(2)
  238. source_dir = args.source_dir or os.path.dirname(os.path.abspath(__file__))
  239. try:
  240. git_top_dir = GetGitTopDirectory(source_dir)
  241. except GitError as e:
  242. logging.error("Failed to get git top directory from '%s': %s",
  243. source_dir, e)
  244. return 2
  245. if args.merge_base_ref:
  246. try:
  247. merge_base_sha = GetMergeBase(git_top_dir, args.merge_base_ref)
  248. except GitError as e:
  249. logging.error("You requested a --merge-base-ref value of '%s' but no "
  250. "merge base could be found between it and HEAD. Git "
  251. "reports: %s", args.merge_base_ref, e)
  252. return 3
  253. else:
  254. merge_base_sha = 'HEAD'
  255. try:
  256. version_info = FetchGitRevision(git_top_dir, commit_filter, merge_base_sha)
  257. except GitError as e:
  258. logging.error("Failed to get version info: %s", e)
  259. logging.info(("Falling back to a version of 0.0.0 to allow script to "
  260. "finish. This is normal if you are bootstrapping a new environment "
  261. "or do not have a git repository for any other reason. If not, this "
  262. "could represent a serious error."))
  263. version_info = VersionInfo('0', '0', 0)
  264. revision_string = version_info.revision
  265. if args.revision_id_only:
  266. revision_string = version_info.revision_id
  267. if args.revision_id_prefix:
  268. revision_string = args.revision_id_prefix + revision_string
  269. if args.print_only:
  270. print(revision_string)
  271. else:
  272. lastchange_year = datetime.datetime.utcfromtimestamp(
  273. version_info.timestamp).year
  274. contents_lines = [
  275. "LASTCHANGE=%s" % revision_string,
  276. "LASTCHANGE_YEAR=%s" % lastchange_year,
  277. ]
  278. contents = '\n'.join(contents_lines) + '\n'
  279. if not out_file and not header and not revision:
  280. sys.stdout.write(contents)
  281. else:
  282. if out_file:
  283. committime_file = out_file + '.committime'
  284. out_changed = WriteIfChanged(out_file, contents)
  285. if out_changed or not os.path.exists(committime_file):
  286. with open(committime_file, 'w') as timefile:
  287. timefile.write(str(version_info.timestamp))
  288. if header:
  289. WriteIfChanged(header,
  290. GetHeaderContents(header, args.version_macro,
  291. revision_string))
  292. if revision:
  293. WriteIfChanged(revision, revision_string)
  294. return 0
  295. if __name__ == '__main__':
  296. sys.exit(main())