| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ | 6 """ |
| 7 lastchange.py -- Chromium revision fetching utility. | 7 lastchange.py -- Chromium revision fetching utility. |
| 8 """ | 8 """ |
| 9 | 9 |
| 10 import re | 10 import re |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 proc = subprocess.Popen(command, | 83 proc = subprocess.Popen(command, |
| 84 stdout=subprocess.PIPE, | 84 stdout=subprocess.PIPE, |
| 85 stderr=subprocess.PIPE, | 85 stderr=subprocess.PIPE, |
| 86 cwd=directory, | 86 cwd=directory, |
| 87 shell=(sys.platform=='win32')) | 87 shell=(sys.platform=='win32')) |
| 88 return proc | 88 return proc |
| 89 except OSError: | 89 except OSError: |
| 90 return None | 90 return None |
| 91 | 91 |
| 92 | 92 |
| 93 def FetchGitRevision(directory): | 93 def FetchGitRevision(directory, hash_only): |
| 94 """ | 94 """ |
| 95 Fetch the Git hash for a given directory. | 95 Fetch the Git hash for a given directory. |
| 96 | 96 |
| 97 Errors are swallowed. | 97 Errors are swallowed. |
| 98 | 98 |
| 99 Returns: | 99 Returns: |
| 100 A VersionInfo object or None on error. | 100 A VersionInfo object or None on error. |
| 101 """ | 101 """ |
| 102 hsh = '' | 102 hsh = '' |
| 103 proc = RunGitCommand(directory, ['rev-parse', 'HEAD']) | 103 proc = RunGitCommand(directory, ['rev-parse', 'HEAD']) |
| 104 if proc: | 104 if proc: |
| 105 output = proc.communicate()[0].strip() | 105 output = proc.communicate()[0].strip() |
| 106 if proc.returncode == 0 and output: | 106 if proc.returncode == 0 and output: |
| 107 hsh = output | 107 hsh = output |
| 108 if not hsh: | 108 if not hsh: |
| 109 return None | 109 return None |
| 110 pos = '' | 110 pos = '' |
| 111 proc = RunGitCommand(directory, ['cat-file', 'commit', 'HEAD']) | 111 proc = RunGitCommand(directory, ['cat-file', 'commit', 'HEAD']) |
| 112 if proc: | 112 if proc: |
| 113 output = proc.communicate()[0] | 113 output = proc.communicate()[0] |
| 114 if proc.returncode == 0 and output: | 114 if proc.returncode == 0 and output: |
| 115 for line in reversed(output.splitlines()): | 115 for line in reversed(output.splitlines()): |
| 116 if line.startswith('Cr-Commit-Position:'): | 116 if line.startswith('Cr-Commit-Position:'): |
| 117 pos = line.rsplit()[-1].strip() | 117 pos = line.rsplit()[-1].strip() |
| 118 break | 118 break |
| 119 if not pos: | 119 if hash_only or not pos: |
| 120 return VersionInfo('git', hsh) | 120 return VersionInfo('git', hsh) |
| 121 return VersionInfo('git', '%s-%s' % (hsh, pos)) | 121 return VersionInfo('git', '%s-%s' % (hsh, pos)) |
| 122 | 122 |
| 123 | 123 |
| 124 def FetchGitSVNURLAndRevision(directory, svn_url_regex, go_deeper): | 124 def FetchGitSVNURLAndRevision(directory, svn_url_regex, go_deeper): |
| 125 """ | 125 """ |
| 126 Fetch the Subversion URL and revision through Git. | 126 Fetch the Subversion URL and revision through Git. |
| 127 | 127 |
| 128 Errors are swallowed. | 128 Errors are swallowed. |
| 129 | 129 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 159 Errors are swallowed. | 159 Errors are swallowed. |
| 160 """ | 160 """ |
| 161 url, revision = FetchGitSVNURLAndRevision(directory, svn_url_regex, go_deeper) | 161 url, revision = FetchGitSVNURLAndRevision(directory, svn_url_regex, go_deeper) |
| 162 if url and revision: | 162 if url and revision: |
| 163 return VersionInfo(url, revision) | 163 return VersionInfo(url, revision) |
| 164 return None | 164 return None |
| 165 | 165 |
| 166 | 166 |
| 167 def FetchVersionInfo(default_lastchange, directory=None, | 167 def FetchVersionInfo(default_lastchange, directory=None, |
| 168 directory_regex_prior_to_src_url='chrome|blink|svn', | 168 directory_regex_prior_to_src_url='chrome|blink|svn', |
| 169 go_deeper=False): | 169 go_deeper=False, hash_only=False): |
| 170 """ | 170 """ |
| 171 Returns the last change (in the form of a branch, revision tuple), | 171 Returns the last change (in the form of a branch, revision tuple), |
| 172 from some appropriate revision control system. | 172 from some appropriate revision control system. |
| 173 """ | 173 """ |
| 174 svn_url_regex = re.compile( | 174 svn_url_regex = re.compile( |
| 175 r'.*/(' + directory_regex_prior_to_src_url + r')(/.*)') | 175 r'.*/(' + directory_regex_prior_to_src_url + r')(/.*)') |
| 176 | 176 |
| 177 version_info = (FetchSVNRevision(directory, svn_url_regex) or | 177 version_info = (FetchSVNRevision(directory, svn_url_regex) or |
| 178 FetchGitSVNRevision(directory, svn_url_regex, go_deeper) or | 178 FetchGitSVNRevision(directory, svn_url_regex, go_deeper) or |
| 179 FetchGitRevision(directory)) | 179 FetchGitRevision(directory, hash_only)) |
| 180 if not version_info: | 180 if not version_info: |
| 181 if default_lastchange and os.path.exists(default_lastchange): | 181 if default_lastchange and os.path.exists(default_lastchange): |
| 182 revision = open(default_lastchange, 'r').read().strip() | 182 revision = open(default_lastchange, 'r').read().strip() |
| 183 version_info = VersionInfo(None, revision) | 183 version_info = VersionInfo(None, revision) |
| 184 else: | 184 else: |
| 185 version_info = VersionInfo(None, None) | 185 version_info = VersionInfo(None, None) |
| 186 return version_info | 186 return version_info |
| 187 | 187 |
| 188 def GetHeaderGuard(path): | 188 def GetHeaderGuard(path): |
| 189 """ | 189 """ |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 help="Write last change to FILE as a C/C++ header. " + | 256 help="Write last change to FILE as a C/C++ header. " + |
| 257 "Can be combined with --output to write both files.") | 257 "Can be combined with --output to write both files.") |
| 258 parser.add_option("--revision-only", action='store_true', | 258 parser.add_option("--revision-only", action='store_true', |
| 259 help="Just print the SVN revision number. Overrides any " + | 259 help="Just print the SVN revision number. Overrides any " + |
| 260 "file-output-related options.") | 260 "file-output-related options.") |
| 261 parser.add_option("-s", "--source-dir", metavar="DIR", | 261 parser.add_option("-s", "--source-dir", metavar="DIR", |
| 262 help="Use repository in the given directory.") | 262 help="Use repository in the given directory.") |
| 263 parser.add_option("--git-svn-go-deeper", action='store_true', | 263 parser.add_option("--git-svn-go-deeper", action='store_true', |
| 264 help="In a Git-SVN repo, dig down to the last committed " + | 264 help="In a Git-SVN repo, dig down to the last committed " + |
| 265 "SVN change (historic behaviour).") | 265 "SVN change (historic behaviour).") |
| 266 parser.add_option("--git-hash-only", action="store_true", |
| 267 help="In a Git repo with commit positions, only report " + |
| 268 "the hash.") |
| 266 opts, args = parser.parse_args(argv[1:]) | 269 opts, args = parser.parse_args(argv[1:]) |
| 267 | 270 |
| 268 out_file = opts.output | 271 out_file = opts.output |
| 269 header = opts.header | 272 header = opts.header |
| 270 | 273 |
| 271 while len(args) and out_file is None: | 274 while len(args) and out_file is None: |
| 272 if out_file is None: | 275 if out_file is None: |
| 273 out_file = args.pop(0) | 276 out_file = args.pop(0) |
| 274 if args: | 277 if args: |
| 275 sys.stderr.write('Unexpected arguments: %r\n\n' % args) | 278 sys.stderr.write('Unexpected arguments: %r\n\n' % args) |
| 276 parser.print_help() | 279 parser.print_help() |
| 277 sys.exit(2) | 280 sys.exit(2) |
| 278 | 281 |
| 279 if opts.source_dir: | 282 if opts.source_dir: |
| 280 src_dir = opts.source_dir | 283 src_dir = opts.source_dir |
| 281 else: | 284 else: |
| 282 src_dir = os.path.dirname(os.path.abspath(__file__)) | 285 src_dir = os.path.dirname(os.path.abspath(__file__)) |
| 283 | 286 |
| 284 version_info = FetchVersionInfo(opts.default_lastchange, | 287 version_info = FetchVersionInfo(opts.default_lastchange, |
| 285 directory=src_dir, | 288 directory=src_dir, |
| 286 go_deeper=opts.git_svn_go_deeper) | 289 go_deeper=opts.git_svn_go_deeper, |
| 290 hash_only=opts.git_hash_only) |
| 287 | 291 |
| 288 if version_info.revision == None: | 292 if version_info.revision == None: |
| 289 version_info.revision = '0' | 293 version_info.revision = '0' |
| 290 | 294 |
| 291 if opts.revision_only: | 295 if opts.revision_only: |
| 292 print version_info.revision | 296 print version_info.revision |
| 293 else: | 297 else: |
| 294 contents = "LASTCHANGE=%s\n" % version_info.revision | 298 contents = "LASTCHANGE=%s\n" % version_info.revision |
| 295 if not out_file and not opts.header: | 299 if not out_file and not opts.header: |
| 296 sys.stdout.write(contents) | 300 sys.stdout.write(contents) |
| 297 else: | 301 else: |
| 298 if out_file: | 302 if out_file: |
| 299 WriteIfChanged(out_file, contents) | 303 WriteIfChanged(out_file, contents) |
| 300 if header: | 304 if header: |
| 301 WriteIfChanged(header, | 305 WriteIfChanged(header, |
| 302 GetHeaderContents(header, opts.version_macro, | 306 GetHeaderContents(header, opts.version_macro, |
| 303 version_info.revision)) | 307 version_info.revision)) |
| 304 | 308 |
| 305 return 0 | 309 return 0 |
| 306 | 310 |
| 307 | 311 |
| 308 if __name__ == '__main__': | 312 if __name__ == '__main__': |
| 309 sys.exit(main()) | 313 sys.exit(main()) |
| OLD | NEW |