Index: build/util/lastchange.py |
diff --git a/build/util/lastchange.py b/build/util/lastchange.py |
index 4d7a1fe808d4a2707c0378ddef54d3f41a267144..2b2e328aa78dc0742a2aaa9ead64838e611c0bdc 100755 |
--- a/build/util/lastchange.py |
+++ b/build/util/lastchange.py |
@@ -13,6 +13,9 @@ import os |
import subprocess |
import sys |
+_GIT_SVN_ID_REGEX = re.compile(r'.*git-svn-id:\s*([^@]*)@([0-9]+)', re.DOTALL) |
+_SVN_URL_REGEX = re.compile(r'.*/(chrome|svn|webkit)(/.*)') |
+ |
class VersionInfo(object): |
def __init__(self, url, root, revision): |
self.url = url |
@@ -27,7 +30,7 @@ def FetchSVNRevision(directory): |
Errors are swallowed. |
Returns: |
- a VersionInfo object or None on error. |
+ A VersionInfo object or None on error. |
""" |
try: |
proc = subprocess.Popen(['svn', 'info'], |
@@ -50,7 +53,11 @@ def FetchSVNRevision(directory): |
attrs[key] = val |
try: |
- url = attrs['URL'] |
+ match = _SVN_URL_REGEX.search(attrs['URL']) |
+ if match: |
+ url = match.group(2) |
+ else: |
+ url = '' |
root = attrs['Repository Root'] |
revision = attrs['Revision'] |
except KeyError: |
@@ -66,7 +73,7 @@ def RunGitCommand(directory, command): |
Errors are swallowed. |
Returns: |
- process object or None. |
+ A process object or None. |
""" |
command = ['git'] + command |
# Force shell usage under cygwin & win32. This is a workaround for |
@@ -92,7 +99,7 @@ def FetchGitRevision(directory): |
Errors are swallowed. |
Returns: |
- a VersionInfo object or None on error. |
+ A VersionInfo object or None on error. |
""" |
proc = RunGitCommand(directory, ['rev-parse', 'HEAD']) |
if proc: |
@@ -102,98 +109,79 @@ def FetchGitRevision(directory): |
return None |
-def IsGitSVN(directory): |
+def FetchGitSVNRoot(directory): |
""" |
- Checks whether git-svn has been set up. |
+ Fetch the SVN root repository through Git. |
Errors are swallowed. |
Returns: |
- whether git-svn has been set up. |
+ The SVN root repository or 'git-svn' on error. |
""" |
- # To test whether git-svn has been set up, query the config for any |
- # svn-related configuration. This command exits with an error code |
- # if there aren't any matches, so ignore its output. |
- proc = RunGitCommand(directory, ['config', '--get-regexp', '^svn']) |
+ proc = RunGitCommand(directory, |
+ ['config', '--get-regexp', '^svn-remote.svn.url$']) |
if proc: |
- return (proc.wait() == 0) |
- return False |
- |
- |
-def FetchGitSVNURL(directory): |
- """ |
- Fetch URL of SVN repository bound to git. |
- |
- Errors are swallowed. |
- |
- Returns: |
- SVN URL. |
- """ |
- if IsGitSVN(directory): |
- proc = RunGitCommand(directory, ['svn', 'info', '--url']) |
- if proc: |
- output = proc.communicate()[0].strip() |
- if proc.returncode == 0: |
- match = re.search(r'^\w+://.*$', output, re.M) |
- if match: |
- return match.group(0) |
- return '' |
+ output = proc.communicate()[0].strip() |
+ if proc.returncode == 0 and output: |
+ # Zero return code implies presence of requested configuration variable. |
+ # Its value is second (last) field of output. |
+ match = re.search(r'\S+$', output) |
+ if match: |
+ return match.group(0) |
+ return 'git-svn' |
-def FetchGitSVNRoot(directory): |
+def FetchGitSVNURLAndRevision(directory): |
""" |
- Fetch root of SVN repository bound to git. |
+ Fetch the Subversion URL and revision through Git. |
Errors are swallowed. |
Returns: |
- SVN root repository. |
- """ |
- if IsGitSVN(directory): |
- git_command = ['config', '--get-regexp', '^svn-remote.svn.url$'] |
- proc = RunGitCommand(directory, git_command) |
- if proc: |
- output = proc.communicate()[0].strip() |
- if proc.returncode == 0: |
- # Zero return code implies presence of requested configuration variable. |
- # Its value is second (last) field of output. |
- match = re.search(r'\S+$', output) |
- if match: |
- return match.group(0) |
- return '' |
- |
- |
-def LookupGitSVNRevision(directory, depth): |
+ A tuple containing the Subversion URL and revision. |
""" |
- Fetch the Git-SVN identifier for the local tree. |
- Parses first |depth| commit messages. |
- |
- Errors are swallowed. |
- """ |
- if not IsGitSVN(directory): |
- return None |
- git_re = re.compile(r'^\s*git-svn-id:\s+(\S+)@(\d+)') |
- proc = RunGitCommand(directory, ['log', '-' + str(depth)]) |
+ proc = RunGitCommand(directory, ['log', '-1', |
+ '--grep=git-svn-id', '--format=%b']) |
if proc: |
- for line in proc.stdout: |
- match = git_re.match(line) |
+ output = proc.communicate()[0].strip() |
+ if proc.returncode == 0 and output: |
+ # Extract the latest SVN revision and the SVN URL. |
+ # The target line is the last "git-svn-id: ..." line like this: |
+ # git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85528 0039d316.... |
+ match = _GIT_SVN_ID_REGEX.search(output) |
if match: |
- id = match.group(2) |
- if id: |
- proc.stdout.close() # Cut pipe for fast exit. |
- return id |
- return None |
+ revision = match.group(2) |
+ url_match = _SVN_URL_REGEX.search(match.group(1)) |
+ if url_match: |
+ url = url_match.group(2) |
+ else: |
+ url = '' |
+ return url, revision |
+ return None, None |
def IsGitSVNDirty(directory): |
""" |
- Checks whether our git-svn tree contains clean trunk or some branch. |
+ Checks whether our git-svn tree contains clean trunk or any local changes. |
Errors are swallowed. |
""" |
- # For git branches the last commit message is either |
- # some local commit or a merge. |
- return LookupGitSVNRevision(directory, 1) is None |
+ proc = RunGitCommand(directory, ['log', '-1']) |
+ if proc: |
+ output = proc.communicate()[0].strip() |
+ if proc.returncode == 0 and output: |
+ # Extract the latest SVN revision and the SVN URL. |
+ # The target line is the last "git-svn-id: ..." line like this: |
+ # git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85528 0039d316.... |
+ match = _GIT_SVN_ID_REGEX.search(output) |
+ if match: |
+ # Check if there are any local uncommitted changes. |
+ proc = RunGitCommand(directory, ['checkout']) |
+ if proc: |
+ output = proc.communicate()[0].strip() |
+ if proc.returncode == 0 and not output: |
+ return False |
+ return True |
def FetchGitSVNRevision(directory): |
@@ -202,15 +190,13 @@ def FetchGitSVNRevision(directory): |
Errors are swallowed. |
""" |
- # We assume that at least first 999 commit messages contain svn evidence. |
- revision = LookupGitSVNRevision(directory, 999) |
- if not revision: |
- return None |
- if IsGitSVNDirty(directory): |
- revision = revision + '-dirty' |
- url = FetchGitSVNURL(directory) |
- root = FetchGitSVNRoot(directory) |
- return VersionInfo(url, root, revision) |
+ url, revision = FetchGitSVNURLAndRevision(directory) |
+ if url and revision: |
+ root = FetchGitSVNRoot(directory) |
+ if IsGitSVNDirty(directory): |
+ revision = revision + '-dirty' |
+ return VersionInfo(url, root, revision) |
+ return None |
def FetchVersionInfo(default_lastchange, directory=None): |
@@ -225,7 +211,7 @@ def FetchVersionInfo(default_lastchange, directory=None): |
revision = open(default_lastchange, 'r').read().strip() |
version_info = VersionInfo(None, None, revision) |
else: |
- version_info = VersionInfo('unknown', '', '0') |
+ version_info = VersionInfo(None, None, None) |
return version_info |
@@ -270,6 +256,9 @@ def main(argv=None): |
version_info = FetchVersionInfo(opts.default_lastchange) |
+ if version_info.revision == None: |
+ version_info.revision = '0' |
+ |
if opts.revision_only: |
print version_info.revision |
else: |