Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 a VersionInfo object or None on error. | 95 a VersionInfo object or None on error. |
| 96 """ | 96 """ |
| 97 proc = RunGitCommand(directory, ['rev-parse', 'HEAD']) | 97 proc = RunGitCommand(directory, ['rev-parse', 'HEAD']) |
| 98 if proc: | 98 if proc: |
| 99 output = proc.communicate()[0].strip() | 99 output = proc.communicate()[0].strip() |
| 100 if proc.returncode == 0 and output: | 100 if proc.returncode == 0 and output: |
| 101 return VersionInfo('git', 'git', output[:7]) | 101 return VersionInfo('git', 'git', output[:7]) |
| 102 return None | 102 return None |
| 103 | 103 |
| 104 | 104 |
| 105 def IsGitSVN(directory): | |
| 106 """ | |
| 107 Checks whether git-svn has been set up. | |
| 108 | |
| 109 Errors are swallowed. | |
| 110 | |
| 111 Returns: | |
| 112 whether git-svn has been set up. | |
| 113 """ | |
| 114 # To test whether git-svn has been set up, query the config for any | |
| 115 # svn-related configuration. This command exits with an error code | |
| 116 # if there aren't any matches, so ignore its output. | |
| 117 proc = RunGitCommand(directory, ['config', '--get-regexp', '^svn']) | |
| 118 if proc: | |
| 119 return (proc.wait() == 0) | |
| 120 return False | |
| 121 | |
| 122 | |
| 123 def FetchGitSVNURL(directory): | |
| 124 """ | |
| 125 Fetch URL of SVN repository bound to git. | |
| 126 | |
| 127 Errors are swallowed. | |
| 128 | |
| 129 Returns: | |
| 130 SVN URL. | |
| 131 """ | |
| 132 if IsGitSVN(directory): | |
| 133 proc = RunGitCommand(directory, ['svn', 'info', '--url']) | |
| 134 if proc: | |
| 135 output = proc.communicate()[0].strip() | |
| 136 if proc.returncode == 0: | |
| 137 match = re.search(r'^\w+://.*$', output, re.M) | |
| 138 if match: | |
| 139 return match.group(0) | |
| 140 return '' | |
| 141 | |
| 142 | |
| 143 def FetchGitSVNRoot(directory): | |
| 144 """ | |
| 145 Fetch root of SVN repository bound to git. | |
| 146 | |
| 147 Errors are swallowed. | |
| 148 | |
| 149 Returns: | |
| 150 SVN root repository. | |
| 151 """ | |
| 152 if IsGitSVN(directory): | |
| 153 git_command = ['config', '--get-regexp', '^svn-remote.svn.url$'] | |
| 154 proc = RunGitCommand(directory, git_command) | |
| 155 if proc: | |
| 156 output = proc.communicate()[0].strip() | |
| 157 if proc.returncode == 0: | |
| 158 # Zero return code implies presence of requested configuration variable. | |
| 159 # Its value is second (last) field of output. | |
| 160 match = re.search(r'\S+$', output) | |
| 161 if match: | |
| 162 return match.group(0) | |
| 163 return '' | |
| 164 | |
| 165 | |
| 166 def LookupGitSVNRevision(directory, depth): | |
| 167 """ | |
| 168 Fetch the Git-SVN identifier for the local tree. | |
| 169 Parses first |depth| commit messages. | |
| 170 | |
| 171 Errors are swallowed. | |
| 172 """ | |
| 173 if not IsGitSVN(directory): | |
| 174 return None | |
| 175 git_re = re.compile(r'^\s*git-svn-id:\s+(\S+)@(\d+)') | |
| 176 proc = RunGitCommand(directory, ['log', '-' + str(depth)]) | |
| 177 if proc: | |
| 178 for line in proc.stdout: | |
| 179 match = git_re.match(line) | |
| 180 if match: | |
| 181 id = match.group(2) | |
| 182 if id: | |
| 183 proc.stdout.close() # Cut pipe for fast exit. | |
| 184 return id | |
| 185 return None | |
| 186 | |
| 187 | |
| 188 def IsGitSVNDirty(directory): | |
| 189 """ | |
| 190 Checks whether our git-svn tree contains clean trunk or some branch. | |
| 191 | |
| 192 Errors are swallowed. | |
| 193 """ | |
| 194 # For git branches the last commit message is either | |
| 195 # some local commit or a merge. | |
| 196 return LookupGitSVNRevision(directory, 1) is None | |
| 197 | |
| 198 | |
| 199 def FetchGitSVNRevision(directory): | 105 def FetchGitSVNRevision(directory): |
| 200 """ | 106 """ |
| 201 Fetch the Git-SVN identifier for the local tree. | 107 Fetch the SVN revision for a given directory through Git. |
| 202 | 108 |
| 203 Errors are swallowed. | 109 Errors are swallowed. |
| 204 """ | 110 """ |
| 205 # We assume that at least first 999 commit messages contain svn evidence. | 111 proc = RunGitCommand(directory, ['log', '-1', |
| 206 revision = LookupGitSVNRevision(directory, 999) | 112 '--grep=git-svn-id', '--format=%b']) |
| 207 if not revision: | 113 if proc: |
| 208 return None | 114 output = proc.communicate()[0].strip() |
| 209 if IsGitSVNDirty(directory): | 115 if proc.returncode == 0 and output: |
| 210 revision = revision + '-dirty' | 116 # Extracts both the last changed SVN revision and the SVN URL. |
| 211 url = FetchGitSVNURL(directory) | 117 # git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85528 0039d316... |
| 212 root = FetchGitSVNRoot(directory) | 118 match = re.search(r'git-svn-id:\s*([^@]*)@([0-9]+)', output) |
|
Denis Lagno
2011/06/15 12:25:34
sometimes it will give incorrect revision:
for exa
haraken1
2011/06/15 16:22:53
I modified so that it chooses the "git-svn-id: ...
Denis Lagno
2011/06/15 17:14:55
maybe just pick last match instead of first?
haraken1
2011/06/16 00:38:01
Done.
| |
| 213 return VersionInfo(url, root, revision) | 119 if match: |
| 120 url = match.group(1) | |
| 121 revision = match.group(2) | |
| 122 | |
| 123 proc = RunGitCommand(directory, ['log', '-1']) | |
| 124 if proc: | |
| 125 output = proc.communicate()[0].strip() | |
| 126 if proc.returncode == 0 and output: | |
| 127 # Checks if there are any local commits. | |
|
Denis Lagno
2011/06/15 12:25:34
as long as we are here I'd prefer to do this in mo
haraken1
2011/06/15 16:22:53
Done.
| |
| 128 match = re.search(r'git-svn-id:', output) | |
| 129 if not match: | |
| 130 revision = revision + "-dirty" | |
| 131 return VersionInfo(url, 'git-svn', revision) | |
| 132 return None | |
| 214 | 133 |
| 215 | 134 |
| 216 def FetchVersionInfo(default_lastchange, directory=None): | 135 def FetchVersionInfo(default_lastchange, directory=None): |
| 217 """ | 136 """ |
| 218 Returns the last change (in the form of a branch, revision tuple), | 137 Returns the last change (in the form of a branch, revision tuple), |
| 219 from some appropriate revision control system. | 138 from some appropriate revision control system. |
| 220 """ | 139 """ |
| 221 version_info = (FetchSVNRevision(directory) or | 140 version_info = (FetchSVNRevision(directory) or |
| 222 FetchGitSVNRevision(directory) or FetchGitRevision(directory)) | 141 FetchGitSVNRevision(directory) or FetchGitRevision(directory)) |
| 223 if not version_info: | 142 if not version_info: |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 if out_file: | 196 if out_file: |
| 278 WriteIfChanged(out_file, contents) | 197 WriteIfChanged(out_file, contents) |
| 279 else: | 198 else: |
| 280 sys.stdout.write(contents) | 199 sys.stdout.write(contents) |
| 281 | 200 |
| 282 return 0 | 201 return 0 |
| 283 | 202 |
| 284 | 203 |
| 285 if __name__ == '__main__': | 204 if __name__ == '__main__': |
| 286 sys.exit(main()) | 205 sys.exit(main()) |
| OLD | NEW |