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 30 matching lines...) Expand all Loading... |
41 proc = subprocess.Popen(command, | 41 proc = subprocess.Popen(command, |
42 stdout=subprocess.PIPE, | 42 stdout=subprocess.PIPE, |
43 stderr=subprocess.PIPE, | 43 stderr=subprocess.PIPE, |
44 cwd=directory, | 44 cwd=directory, |
45 shell=(sys.platform=='win32')) | 45 shell=(sys.platform=='win32')) |
46 return proc | 46 return proc |
47 except OSError: | 47 except OSError: |
48 return None | 48 return None |
49 | 49 |
50 | 50 |
51 def FetchGitRevision(directory, hash_only): | 51 def FetchGitRevision(directory): |
52 """ | 52 """ |
53 Fetch the Git hash for a given directory. | 53 Fetch the Git hash for a given directory. |
54 | 54 |
55 Errors are swallowed. | 55 Errors are swallowed. |
56 | 56 |
57 Returns: | 57 Returns: |
58 A VersionInfo object or None on error. | 58 A VersionInfo object or None on error. |
59 """ | 59 """ |
60 hsh = '' | 60 hsh = '' |
61 git_args = ['log', '-1', '--format=%H'] | 61 git_args = ['log', '-1', '--format=%H'] |
62 if hash_only: | |
63 git_args.append('--grep=^Cr-Commit-Position:') | |
64 proc = RunGitCommand(directory, git_args) | 62 proc = RunGitCommand(directory, git_args) |
65 if proc: | 63 if proc: |
66 output = proc.communicate()[0].strip() | 64 output = proc.communicate()[0].strip() |
67 if proc.returncode == 0 and output: | 65 if proc.returncode == 0 and output: |
68 hsh = output | 66 hsh = output |
69 if not hsh: | 67 if not hsh: |
70 return None | 68 return None |
71 pos = '' | 69 pos = '' |
72 proc = RunGitCommand(directory, ['cat-file', 'commit', hsh]) | 70 proc = RunGitCommand(directory, ['cat-file', 'commit', hsh]) |
73 if proc: | 71 if proc: |
74 output = proc.communicate()[0] | 72 output = proc.communicate()[0] |
75 if proc.returncode == 0 and output: | 73 if proc.returncode == 0 and output: |
76 for line in reversed(output.splitlines()): | 74 for line in reversed(output.splitlines()): |
77 if line.startswith('Cr-Commit-Position:'): | 75 if line.startswith('Cr-Commit-Position:'): |
78 pos = line.rsplit()[-1].strip() | 76 pos = line.rsplit()[-1].strip() |
79 break | 77 break |
80 if hash_only or not pos: | |
81 return VersionInfo('git', hsh) | |
82 return VersionInfo('git', '%s-%s' % (hsh, pos)) | 78 return VersionInfo('git', '%s-%s' % (hsh, pos)) |
83 | 79 |
84 | 80 |
85 def FetchVersionInfo(directory=None, | 81 def FetchVersionInfo(directory=None, |
86 directory_regex_prior_to_src_url='chrome|blink|svn', | 82 directory_regex_prior_to_src_url='chrome|svn'): |
87 hash_only=False): | |
88 """ | 83 """ |
89 Returns the last change (in the form of a branch, revision tuple), | 84 Returns the last change (in the form of a branch, revision tuple), |
90 from some appropriate revision control system. | 85 from some appropriate revision control system. |
91 """ | 86 """ |
92 svn_url_regex = re.compile( | 87 svn_url_regex = re.compile( |
93 r'.*/(' + directory_regex_prior_to_src_url + r')(/.*)') | 88 r'.*/(' + directory_regex_prior_to_src_url + r')(/.*)') |
94 | 89 |
95 version_info = FetchGitRevision(directory, hash_only) | 90 version_info = FetchGitRevision(directory) |
96 if not version_info: | 91 if not version_info: |
97 version_info = VersionInfo(None, None) | 92 version_info = VersionInfo(None, None) |
98 return version_info | 93 return version_info |
99 | 94 |
100 | 95 |
101 def GetHeaderGuard(path): | 96 def GetHeaderGuard(path): |
102 """ | 97 """ |
103 Returns the header #define guard for the given file path. | 98 Returns the header #define guard for the given file path. |
104 This treats everything after the last instance of "src/" as being a | 99 This treats everything after the last instance of "src/" as being a |
105 relevant part of the guard. If there is no "src/", then the entire path | 100 relevant part of the guard. If there is no "src/", then the entire path |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 help="Write last change to FILE. " + | 161 help="Write last change to FILE. " + |
167 "Can be combined with --header to write both files.") | 162 "Can be combined with --header to write both files.") |
168 parser.add_option("", "--header", metavar="FILE", | 163 parser.add_option("", "--header", metavar="FILE", |
169 help="Write last change to FILE as a C/C++ header. " + | 164 help="Write last change to FILE as a C/C++ header. " + |
170 "Can be combined with --output to write both files.") | 165 "Can be combined with --output to write both files.") |
171 parser.add_option("--revision-only", action='store_true', | 166 parser.add_option("--revision-only", action='store_true', |
172 help="Just print the SVN revision number. Overrides any " + | 167 help="Just print the SVN revision number. Overrides any " + |
173 "file-output-related options.") | 168 "file-output-related options.") |
174 parser.add_option("-s", "--source-dir", metavar="DIR", | 169 parser.add_option("-s", "--source-dir", metavar="DIR", |
175 help="Use repository in the given directory.") | 170 help="Use repository in the given directory.") |
176 parser.add_option("--git-hash-only", action="store_true", | |
177 help="In a Git repo with commit positions, report only " + | |
178 "the hash of the latest commit with a position.") | |
179 opts, args = parser.parse_args(argv[1:]) | 171 opts, args = parser.parse_args(argv[1:]) |
180 | 172 |
181 out_file = opts.output | 173 out_file = opts.output |
182 header = opts.header | 174 header = opts.header |
183 | 175 |
184 while len(args) and out_file is None: | 176 while len(args) and out_file is None: |
185 if out_file is None: | 177 if out_file is None: |
186 out_file = args.pop(0) | 178 out_file = args.pop(0) |
187 if args: | 179 if args: |
188 sys.stderr.write('Unexpected arguments: %r\n\n' % args) | 180 sys.stderr.write('Unexpected arguments: %r\n\n' % args) |
189 parser.print_help() | 181 parser.print_help() |
190 sys.exit(2) | 182 sys.exit(2) |
191 | 183 |
192 if opts.source_dir: | 184 if opts.source_dir: |
193 src_dir = opts.source_dir | 185 src_dir = opts.source_dir |
194 else: | 186 else: |
195 src_dir = os.path.dirname(os.path.abspath(__file__)) | 187 src_dir = os.path.dirname(os.path.abspath(__file__)) |
196 | 188 |
197 version_info = FetchVersionInfo(directory=src_dir, | 189 version_info = FetchVersionInfo(directory=src_dir) |
198 hash_only=opts.git_hash_only) | |
199 | 190 |
200 if version_info.revision == None: | 191 if version_info.revision == None: |
201 version_info.revision = '0' | 192 version_info.revision = '0' |
202 | 193 |
203 if opts.revision_only: | 194 if opts.revision_only: |
204 print version_info.revision | 195 print version_info.revision |
205 else: | 196 else: |
206 contents = "LASTCHANGE=%s\n" % version_info.revision | 197 contents = "LASTCHANGE=%s\n" % version_info.revision |
207 if not out_file and not opts.header: | 198 if not out_file and not opts.header: |
208 sys.stdout.write(contents) | 199 sys.stdout.write(contents) |
209 else: | 200 else: |
210 if out_file: | 201 if out_file: |
211 WriteIfChanged(out_file, contents) | 202 WriteIfChanged(out_file, contents) |
212 if header: | 203 if header: |
213 WriteIfChanged(header, | 204 WriteIfChanged(header, |
214 GetHeaderContents(header, opts.version_macro, | 205 GetHeaderContents(header, opts.version_macro, |
215 version_info.revision)) | 206 version_info.revision)) |
216 | 207 |
217 return 0 | 208 return 0 |
218 | 209 |
219 | 210 |
220 if __name__ == '__main__': | 211 if __name__ == '__main__': |
221 sys.exit(main()) | 212 sys.exit(main()) |
OLD | NEW |