xref: /aosp_15_r20/external/cronet/build/util/lastchange.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env python3
2# Copyright 2012 The Chromium Authors
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"""
9
10import argparse
11import collections
12import datetime
13import logging
14import os
15import subprocess
16import sys
17
18VersionInfo = collections.namedtuple("VersionInfo",
19                                     ("revision_id", "revision", "timestamp"))
20
21class GitError(Exception):
22  pass
23
24# This function exists for compatibility with logic outside this
25# repository that uses this file as a library.
26# TODO(eliribble) remove this function after it has been ported into
27# the repositories that depend on it
28def RunGitCommand(directory, command):
29  """
30  Launches git subcommand.
31
32  Errors are swallowed.
33
34  Returns:
35    A process object or None.
36  """
37  command = ['git'] + command
38  # Force shell usage under cygwin. This is a workaround for
39  # mysterious loss of cwd while invoking cygwin's git.
40  # We can't just pass shell=True to Popen, as under win32 this will
41  # cause CMD to be used, while we explicitly want a cygwin shell.
42  if sys.platform == 'cygwin':
43    command = ['sh', '-c', ' '.join(command)]
44  try:
45    proc = subprocess.Popen(command,
46                            stdout=subprocess.PIPE,
47                            stderr=subprocess.PIPE,
48                            cwd=directory,
49                            shell=(sys.platform=='win32'))
50    return proc
51  except OSError as e:
52    logging.error('Command %r failed: %s' % (' '.join(command), e))
53    return None
54
55
56def _RunGitCommand(directory, command):
57  """Launches git subcommand.
58
59  Returns:
60    The stripped stdout of the git command.
61  Raises:
62    GitError on failure, including a nonzero return code.
63  """
64  command = ['git'] + command
65  # Force shell usage under cygwin. This is a workaround for
66  # mysterious loss of cwd while invoking cygwin's git.
67  # We can't just pass shell=True to Popen, as under win32 this will
68  # cause CMD to be used, while we explicitly want a cygwin shell.
69  if sys.platform == 'cygwin':
70    command = ['sh', '-c', ' '.join(command)]
71  try:
72    logging.info("Executing '%s' in %s", ' '.join(command), directory)
73    proc = subprocess.Popen(command,
74                            stdout=subprocess.PIPE,
75                            stderr=subprocess.PIPE,
76                            cwd=directory,
77                            shell=(sys.platform=='win32'))
78    stdout, stderr = tuple(x.decode(encoding='utf_8')
79                           for x in proc.communicate())
80    stdout = stdout.strip()
81    stderr = stderr.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",
225                      "--output",
226                      metavar="FILE",
227                      help=("Write last change to FILE. "
228                            "Can be combined with other file-output-related "
229                            "options to write multiple files."))
230  parser.add_argument("--header",
231                      metavar="FILE",
232                      help=("Write last change to FILE as a C/C++ header. "
233                            "Can be combined with other file-output-related "
234                            "options to write multiple files."))
235  parser.add_argument("--revision",
236                      metavar="FILE",
237                      help=("Write last change to FILE as a one-line revision. "
238                            "Can be combined with other file-output-related "
239                            "options to write multiple files."))
240  parser.add_argument("--merge-base-ref",
241                    default=None,
242                    help=("Only consider changes since the merge "
243                          "base between HEAD and the provided ref"))
244  parser.add_argument("--revision-id-only", action='store_true',
245                    help=("Output the revision as a VCS revision ID only (in "
246                          "Git, a 40-character commit hash, excluding the "
247                          "Cr-Commit-Position)."))
248  parser.add_argument("--revision-id-prefix",
249                      metavar="PREFIX",
250                      help=("Adds a string prefix to the VCS revision ID."))
251  parser.add_argument("--print-only", action="store_true",
252                    help=("Just print the revision string. Overrides any "
253                          "file-output-related options."))
254  parser.add_argument("-s", "--source-dir", metavar="DIR",
255                    help="Use repository in the given directory.")
256  parser.add_argument("--filter", metavar="REGEX",
257                    help=("Only use log entries where the commit message "
258                          "matches the supplied filter regex. Defaults to "
259                          "'^Change-Id:' to suppress local commits."),
260                    default='^Change-Id:')
261
262  args, extras = parser.parse_known_args(argv[1:])
263
264  logging.basicConfig(level=logging.WARNING)
265
266  out_file = args.output
267  header = args.header
268  revision = args.revision
269  commit_filter=args.filter
270
271  while len(extras) and out_file is None:
272    if out_file is None:
273      out_file = extras.pop(0)
274  if extras:
275    sys.stderr.write('Unexpected arguments: %r\n\n' % extras)
276    parser.print_help()
277    sys.exit(2)
278
279  source_dir = args.source_dir or os.path.dirname(os.path.abspath(__file__))
280
281  git_top_dir = None
282  try:
283    git_top_dir = GetGitTopDirectory(source_dir)
284  except GitError as e:
285    logging.warning("Failed to get git top directory from '%s': %s", source_dir,
286                    e)
287
288  merge_base_sha = 'HEAD'
289  if git_top_dir and args.merge_base_ref:
290    try:
291      merge_base_sha = GetMergeBase(git_top_dir, args.merge_base_ref)
292    except GitError as e:
293      logging.error("You requested a --merge-base-ref value of '%s' but no "
294                    "merge base could be found between it and HEAD. Git "
295                    "reports: %s", args.merge_base_ref, e)
296      return 3
297
298  version_info = None
299  if git_top_dir:
300    try:
301      version_info = FetchGitRevision(git_top_dir, commit_filter,
302                                      merge_base_sha)
303    except GitError as e:
304      logging.error("Failed to get version info: %s", e)
305
306  if not version_info:
307    logging.warning(
308        "Falling back to a version of 0.0.0 to allow script to "
309        "finish. This is normal if you are bootstrapping a new environment "
310        "or do not have a git repository for any other reason. If not, this "
311        "could represent a serious error.")
312    # Use a dummy revision that has the same length as a Git commit hash,
313    # same as what we use in build/util/LASTCHANGE.dummy.
314    version_info = VersionInfo('0' * 40, '0' * 40, 0)
315
316  revision_string = version_info.revision
317  if args.revision_id_only:
318    revision_string = version_info.revision_id
319
320  if args.revision_id_prefix:
321    revision_string = args.revision_id_prefix + revision_string
322
323  if args.print_only:
324    print(revision_string)
325  else:
326    lastchange_year = datetime.datetime.fromtimestamp(
327        version_info.timestamp, datetime.timezone.utc).year
328    contents_lines = [
329        "LASTCHANGE=%s" % revision_string,
330        "LASTCHANGE_YEAR=%s" % lastchange_year,
331    ]
332    contents = '\n'.join(contents_lines) + '\n'
333    if not out_file and not header and not revision:
334      sys.stdout.write(contents)
335    else:
336      if out_file:
337        committime_file = out_file + '.committime'
338        out_changed = WriteIfChanged(out_file, contents)
339        if out_changed or not os.path.exists(committime_file):
340          with open(committime_file, 'w') as timefile:
341            timefile.write(str(version_info.timestamp))
342      if header:
343        WriteIfChanged(header,
344                       GetHeaderContents(header, args.version_macro,
345                                         revision_string))
346      if revision:
347        WriteIfChanged(revision, revision_string)
348
349  return 0
350
351
352if __name__ == '__main__':
353  sys.exit(main())
354