Chromium Code Reviews| 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 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): | 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 |
| 130 Returns: | 130 Returns: |
| 131 A tuple containing the Subversion URL and revision. | 131 A tuple containing the Subversion URL and revision. |
| 132 """ | 132 """ |
| 133 proc = RunGitCommand(directory, ['log', '-1', '--format=%b']) | 133 git_args = ['log', '-1', '--format=%b'] |
| 134 if go_deeper: | |
| 135 git_args.append('--grep=git-svn-id') | |
| 136 proc = RunGitCommand(directory, git_args) | |
| 134 if proc: | 137 if proc: |
| 135 output = proc.communicate()[0].strip() | 138 output = proc.communicate()[0].strip() |
| 136 if proc.returncode == 0 and output: | 139 if proc.returncode == 0 and output: |
| 137 # Extract the latest SVN revision and the SVN URL. | 140 # Extract the latest SVN revision and the SVN URL. |
| 138 # The target line is the last "git-svn-id: ..." line like this: | 141 # The target line is the last "git-svn-id: ..." line like this: |
| 139 # git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85528 0039d316.... | 142 # git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85528 0039d316.... |
| 140 match = _GIT_SVN_ID_REGEX.search(output) | 143 match = _GIT_SVN_ID_REGEX.search(output) |
| 141 if match: | 144 if match: |
| 142 revision = match.group(2) | 145 revision = match.group(2) |
| 143 url_match = svn_url_regex.search(match.group(1)) | 146 url_match = svn_url_regex.search(match.group(1)) |
| 144 if url_match: | 147 if url_match: |
| 145 url = url_match.group(2) | 148 url = url_match.group(2) |
| 146 else: | 149 else: |
| 147 url = '' | 150 url = '' |
| 148 return url, revision | 151 return url, revision |
| 149 return None, None | 152 return None, None |
| 150 | 153 |
| 151 | 154 |
| 152 def FetchGitSVNRevision(directory, svn_url_regex): | 155 def FetchGitSVNRevision(directory, svn_url_regex, go_deeper): |
| 153 """ | 156 """ |
| 154 Fetch the Git-SVN identifier for the local tree. | 157 Fetch the Git-SVN identifier for the local tree. |
| 155 | 158 |
| 156 Errors are swallowed. | 159 Errors are swallowed. |
| 157 """ | 160 """ |
| 158 url, revision = FetchGitSVNURLAndRevision(directory, svn_url_regex) | 161 url, revision = FetchGitSVNURLAndRevision(directory, svn_url_regex, go_deeper) |
| 159 if url and revision: | 162 if url and revision: |
| 160 return VersionInfo(url, revision) | 163 return VersionInfo(url, revision) |
| 161 return None | 164 return None |
| 162 | 165 |
| 163 | 166 |
| 164 def FetchVersionInfo(default_lastchange, directory=None, | 167 def FetchVersionInfo(default_lastchange, directory=None, |
| 165 directory_regex_prior_to_src_url='chrome|blink|svn'): | 168 directory_regex_prior_to_src_url='chrome|blink|svn', |
| 169 go_deeper=False): | |
| 166 """ | 170 """ |
| 167 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), |
| 168 from some appropriate revision control system. | 172 from some appropriate revision control system. |
| 169 """ | 173 """ |
| 170 svn_url_regex = re.compile( | 174 svn_url_regex = re.compile( |
| 171 r'.*/(' + directory_regex_prior_to_src_url + r')(/.*)') | 175 r'.*/(' + directory_regex_prior_to_src_url + r')(/.*)') |
| 172 | 176 |
| 173 version_info = (FetchSVNRevision(directory, svn_url_regex) or | 177 version_info = (FetchSVNRevision(directory, svn_url_regex) or |
| 174 FetchGitSVNRevision(directory, svn_url_regex) or | 178 FetchGitSVNRevision(directory, svn_url_regex, go_deeper) or |
| 175 FetchGitRevision(directory)) | 179 FetchGitRevision(directory)) |
| 176 if not version_info: | 180 if not version_info: |
| 177 if default_lastchange and os.path.exists(default_lastchange): | 181 if default_lastchange and os.path.exists(default_lastchange): |
| 178 revision = open(default_lastchange, 'r').read().strip() | 182 revision = open(default_lastchange, 'r').read().strip() |
| 179 version_info = VersionInfo(None, revision) | 183 version_info = VersionInfo(None, revision) |
| 180 else: | 184 else: |
| 181 version_info = VersionInfo(None, None) | 185 version_info = VersionInfo(None, None) |
| 182 return version_info | 186 return version_info |
| 183 | 187 |
| 184 def GetHeaderGuard(path): | 188 def GetHeaderGuard(path): |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 help="Write last change to FILE. " + | 253 help="Write last change to FILE. " + |
| 250 "Can be combined with --header to write both files.") | 254 "Can be combined with --header to write both files.") |
| 251 parser.add_option("", "--header", metavar="FILE", | 255 parser.add_option("", "--header", metavar="FILE", |
| 252 help="Write last change to FILE as a C/C++ header. " + | 256 help="Write last change to FILE as a C/C++ header. " + |
| 253 "Can be combined with --output to write both files.") | 257 "Can be combined with --output to write both files.") |
| 254 parser.add_option("--revision-only", action='store_true', | 258 parser.add_option("--revision-only", action='store_true', |
| 255 help="Just print the SVN revision number. Overrides any " + | 259 help="Just print the SVN revision number. Overrides any " + |
| 256 "file-output-related options.") | 260 "file-output-related options.") |
| 257 parser.add_option("-s", "--source-dir", metavar="DIR", | 261 parser.add_option("-s", "--source-dir", metavar="DIR", |
| 258 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', | |
| 264 help="In a Git-SVN repo, dig down to the last committed " + | |
| 265 "SVN change (historic behaviour).") | |
| 259 opts, args = parser.parse_args(argv[1:]) | 266 opts, args = parser.parse_args(argv[1:]) |
| 260 | 267 |
| 261 out_file = opts.output | 268 out_file = opts.output |
| 262 header = opts.header | 269 header = opts.header |
| 263 | 270 |
| 264 while len(args) and out_file is None: | 271 while len(args) and out_file is None: |
| 265 if out_file is None: | 272 if out_file is None: |
| 266 out_file = args.pop(0) | 273 out_file = args.pop(0) |
| 267 if args: | 274 if args: |
| 268 sys.stderr.write('Unexpected arguments: %r\n\n' % args) | 275 sys.stderr.write('Unexpected arguments: %r\n\n' % args) |
| 269 parser.print_help() | 276 parser.print_help() |
| 270 sys.exit(2) | 277 sys.exit(2) |
| 271 | 278 |
| 272 if opts.source_dir: | 279 if opts.source_dir: |
| 273 src_dir = opts.source_dir | 280 src_dir = opts.source_dir |
| 274 else: | 281 else: |
| 275 src_dir = os.path.dirname(os.path.abspath(__file__)) | 282 src_dir = os.path.dirname(os.path.abspath(__file__)) |
| 276 | 283 |
| 277 version_info = FetchVersionInfo(opts.default_lastchange, src_dir) | 284 version_info = FetchVersionInfo( |
| 285 opts.default_lastchange, | |
|
scottmg
2015/06/18 22:56:34
this looks oddly indented, if it's not from an aut
mnaganov (inactive)
2015/06/18 22:59:50
Done.
| |
| 286 directory=src_dir, | |
| 287 go_deeper=opts.git_svn_go_deeper) | |
| 278 | 288 |
| 279 if version_info.revision == None: | 289 if version_info.revision == None: |
| 280 version_info.revision = '0' | 290 version_info.revision = '0' |
| 281 | 291 |
| 282 if opts.revision_only: | 292 if opts.revision_only: |
| 283 print version_info.revision | 293 print version_info.revision |
| 284 else: | 294 else: |
| 285 contents = "LASTCHANGE=%s\n" % version_info.revision | 295 contents = "LASTCHANGE=%s\n" % version_info.revision |
| 286 if not out_file and not opts.header: | 296 if not out_file and not opts.header: |
| 287 sys.stdout.write(contents) | 297 sys.stdout.write(contents) |
| 288 else: | 298 else: |
| 289 if out_file: | 299 if out_file: |
| 290 WriteIfChanged(out_file, contents) | 300 WriteIfChanged(out_file, contents) |
| 291 if header: | 301 if header: |
| 292 WriteIfChanged(header, | 302 WriteIfChanged(header, |
| 293 GetHeaderContents(header, opts.version_macro, | 303 GetHeaderContents(header, opts.version_macro, |
| 294 version_info.revision)) | 304 version_info.revision)) |
| 295 | 305 |
| 296 return 0 | 306 return 0 |
| 297 | 307 |
| 298 | 308 |
| 299 if __name__ == '__main__': | 309 if __name__ == '__main__': |
| 300 sys.exit(main()) | 310 sys.exit(main()) |
| OLD | NEW |