From 8ede00e1af29d05daa65b7e4a90c83ee39f16541 Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Tue, 12 Jan 2010 14:35:28 +0000 Subject: [PATCH] Add the capability to filter out files on try job with regexp. By default, filters out 'ChangeLog'. A pain directly coming from Webkit. TEST=none BUG=none Review URL: http://codereview.chromium.org/543012 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@36008 0039d316-1c4b-4281-b951-d872f2087c98 --- scm.py | 17 +++++++++++++++-- tests/gclient_scm_test.py | 2 +- tests/scm_unittest.py | 2 +- tests/trychange_unittest.py | 4 +++- trychange.py | 38 ++++++++++++++++++++++++++++++------- 5 files changed, 51 insertions(+), 12 deletions(-) diff --git a/scm.py b/scm.py index 7a6f66422..0fc5cbae7 100644 --- a/scm.py +++ b/scm.py @@ -230,16 +230,21 @@ class GIT(object): return upstream_branch @staticmethod - def GenerateDiff(cwd, branch=None, full_move=False): + def GenerateDiff(cwd, branch=None, branch_head='HEAD', full_move=False, + files=None): """Diffs against the upstream branch or optionally another branch. full_move means that move or copy operations should completely recreate the files, usually in the prospect to apply the patch for a try job.""" if not branch: branch = GIT.GetUpstream(cwd) - command = ['diff-tree', '-p', '--no-prefix', branch, 'HEAD'] + command = ['diff-tree', '-p', '--no-prefix', branch, branch_head] if not full_move: command.append('-C') + # TODO(maruel): --binary support. + if files: + command.append('--') + command.extend(files) diff = GIT.Capture(command, cwd).splitlines(True) for i in range(len(diff)): # In the case of added files, replace /dev/null with the path to the @@ -248,6 +253,14 @@ class GIT(object): diff[i] = '--- %s' % diff[i+1][4:] return ''.join(diff) + @staticmethod + def GetDifferentFiles(cwd, branch=None, branch_head='HEAD'): + """Returns the list of modified files between two branches.""" + if not branch: + branch = GIT.GetUpstream(cwd) + command = ['diff', '--name-only', branch, branch_head] + return GIT.Capture(command, cwd).splitlines(False) + @staticmethod def GetPatchName(cwd): """Constructs a name for this patch.""" diff --git a/tests/gclient_scm_test.py b/tests/gclient_scm_test.py index 77c14322d..829227045 100755 --- a/tests/gclient_scm_test.py +++ b/tests/gclient_scm_test.py @@ -361,7 +361,7 @@ from :3 members = [ 'COMMAND', 'Capture', 'CaptureStatus', 'FetchUpstreamTuple', 'GenerateDiff', 'GetBranch', 'GetBranchRef', 'GetCheckoutRoot', - 'GetEmail', 'GetPatchName', 'GetSVNBranch', + 'GetDifferentFiles', 'GetEmail', 'GetPatchName', 'GetSVNBranch', 'GetUpstream', 'IsGitSvn', 'RunAndFilterOutput', 'ShortBranchName', 'RunCommand', 'cleanup', 'diff', 'export', 'pack', 'relpath', 'revert', 'revinfo', 'runhooks', 'scm_name', 'status', 'update', 'url', diff --git a/tests/scm_unittest.py b/tests/scm_unittest.py index 14c1bc62e..ce7a23c26 100755 --- a/tests/scm_unittest.py +++ b/tests/scm_unittest.py @@ -118,7 +118,7 @@ from :3 members = [ 'COMMAND', 'Capture', 'CaptureStatus', 'FetchUpstreamTuple', 'GenerateDiff', 'GetBranch', 'GetBranchRef', 'GetCheckoutRoot', - 'GetEmail', 'GetPatchName', 'GetSVNBranch', + 'GetDifferentFiles', 'GetEmail', 'GetPatchName', 'GetSVNBranch', 'GetUpstream', 'IsGitSvn', 'RunAndFilterOutput', 'ShortBranchName', ] # If this test fails, you should add the relevant test. diff --git a/tests/trychange_unittest.py b/tests/trychange_unittest.py index dafeaa57c..ebd72b84a 100644 --- a/tests/trychange_unittest.py +++ b/tests/trychange_unittest.py @@ -33,6 +33,7 @@ class TryChangeTestsBase(SuperMoxTestBase): self.options.diff = None self.options.name = None self.options.email = None + self.options.exclude = [] class TryChangeUnittest(TryChangeTestsBase): @@ -43,7 +44,7 @@ class TryChangeUnittest(TryChangeTestsBase): 'HELP_STRING', 'InvalidScript', 'NoTryServerAccess', 'PrintSuccess', 'SCM', 'SVN', 'TryChange', 'USAGE', 'breakpad', 'datetime', 'errno', 'gclient_utils', 'getpass', 'logging', - 'optparse', 'os', 'posixpath', 'scm', 'shutil', 'sys', 'tempfile', + 'optparse', 'os', 'posixpath', 're', 'scm', 'shutil', 'sys', 'tempfile', 'urllib', ] # If this test fails, you should add the relevant test. @@ -90,6 +91,7 @@ class GITUnittest(TryChangeTestsBase): trychange.scm.GIT.GetCheckoutRoot(self.fake_root).AndReturn(self.fake_root) trychange.scm.GIT.GenerateDiff(self.fake_root, full_move=True, + files=['foo.txt', 'bar.txt'], branch=None).AndReturn('A diff') trychange.scm.GIT.GetPatchName(self.fake_root).AndReturn('bleh-1233') trychange.scm.GIT.GetEmail(self.fake_root).AndReturn('georges@example.com') diff --git a/trychange.py b/trychange.py index c04e32915..d9d5bdf4b 100755 --- a/trychange.py +++ b/trychange.py @@ -14,6 +14,7 @@ import logging import optparse import os import posixpath +import re import shutil import sys import tempfile @@ -174,11 +175,19 @@ class SVN(SCM): if not self.files: previous_cwd = os.getcwd() os.chdir(self.checkout_root) + excluded = ['!', '?', 'X', ' ', '~'] - self.files = [ - f[1] for f in scm.SVN.CaptureStatus(self.checkout_root) - if f[0][0] not in excluded - ] + def Excluded(f): + if f[0][0] in excluded: + return True + for r in self.options.exclude: + if re.search(r, f[1]): + logging.info('Ignoring "%s"' % f[1]) + return True + return False + + self.files = [f[1] for f in scm.SVN.CaptureStatus(self.checkout_root) + if not Excluded(f)] os.chdir(previous_cwd) return scm.SVN.GenerateDiff(self.files, self.checkout_root, full_move=True, revision=self.diff_against) @@ -206,8 +215,20 @@ class GIT(SCM): return None def GenerateDiff(self): - # For now, ignores self.files - return scm.GIT.GenerateDiff(self.checkout_root, full_move=True, + if not self.files: + self.files = scm.GIT.GetDifferentFiles(self.checkout_root, + branch=self.diff_against) + + def NotExcluded(f): + for r in self.options.exclude: + if re.search(r, f): + logging.info('Ignoring "%s"' % f) + return False + return True + + self.files = filter(NotExcluded, self.files) + return scm.GIT.GenerateDiff(self.checkout_root, files=self.files, + full_move=True, branch=self.diff_against) @@ -489,13 +510,16 @@ def TryChange(argv, try: group.add_option("--webkit", action="append_const", const="third_party/WebKit", - dest="sub_rep", + dest="PATH", help="Shorthand for -s third_party/WebKit") except optparse.OptionError: # append_const is not supported on 2.4. Too bad. pass group.add_option("--no_gclient", action="store_true", help="Disable automatic search for gclient checkout.") + group.add_option("-E", "--exclude", action="append", + default=['ChangeLog'], metavar='REGEXP', + help="Regexp patterns to exclude files. Default: %default") parser.add_option_group(group) group = optparse.OptionGroup(parser, "Access the try server by HTTP")