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