From c70a220f724fe7744a99814466cc16fb0ecdf650 Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Wed, 17 Jun 2009 12:55:10 +0000 Subject: [PATCH] Add partial presubmit support to git checkout. Also fix argument processing. It doesn't works at the moment but doesn't throw an exception either. :) TEST=none BUG=none Review URL: http://codereview.chromium.org/125095 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@18611 0039d316-1c4b-4281-b951-d872f2087c98 --- presubmit_support.py | 103 ++++++++++++++++++++++++++++++------ tests/presubmit_unittest.py | 21 +++++++- 2 files changed, 106 insertions(+), 18 deletions(-) diff --git a/presubmit_support.py b/presubmit_support.py index 8936ec5b1..88a535fde 100755 --- a/presubmit_support.py +++ b/presubmit_support.py @@ -489,6 +489,50 @@ class SvnAffectedFile(AffectedFile): return self._is_text_file +class GitAffectedFile(AffectedFile): + """Representation of a file in a change out of a git checkout.""" + + def __init__(self, *args, **kwargs): + AffectedFile.__init__(self, *args, **kwargs) + self._server_path = None + self._is_text_file = None + self.scm = 'git' + + def ServerPath(self): + if self._server_path is None: + raise NotImplementedException() # TODO(maruel) Implement. + return self._server_path + + def IsDirectory(self): + if self._is_directory is None: + path = self.AbsoluteLocalPath() + if os.path.exists(path): + # Retrieve directly from the file system; it is much faster than + # querying subversion, especially on Windows. + self._is_directory = os.path.isdir(path) + else: + # raise NotImplementedException() # TODO(maruel) Implement. + self._is_directory = False + return self._is_directory + + def Property(self, property_name): + if not property_name in self._properties: + raise NotImplementedException() # TODO(maruel) Implement. + return self._properties[property_name] + + def IsTextFile(self): + if self._is_text_file is None: + if self.Action() == 'D': + # A deleted file is not a text file. + self._is_text_file = False + elif self.IsDirectory(): + self._is_text_file = False + else: + # raise NotImplementedException() # TODO(maruel) Implement. + self._is_text_file = os.path.isfile(self.AbsoluteLocalPath()) + return self._is_text_file + + class Change(object): """Describe a change. @@ -629,6 +673,10 @@ class SvnChange(Change): _AFFECTED_FILES = SvnAffectedFile +class GitChange(Change): + _AFFECTED_FILES = GitAffectedFile + + def ListRelevantPresubmitFiles(files, root): """Finds all presubmit files that apply to a given set of source files. @@ -783,12 +831,14 @@ def DoPresubmitChecks(change, def ScanSubDirs(mask, recursive): if not recursive: - return [x for x in glob.glob(mask) if '.svn' not in x] + return [x for x in glob.glob(mask) if '.svn' not in x and '.git' not in x] else: results = [] for root, dirs, files in os.walk('.'): if '.svn' in dirs: dirs.remove('.svn') + if '.git' in dirs: + dirs.remove('.git') for name in files: if fnmatch.fnmatch(name, mask): results.append(os.path.join(root, name)) @@ -805,33 +855,52 @@ def ParseFiles(args, recursive): def Main(argv): parser = optparse.OptionParser(usage="%prog [options]", version="%prog " + str(__version__)) - parser.add_option("-c", "--commit", action="store_true", + parser.add_option("-c", "--commit", action="store_true", default=False, help="Use commit instead of upload checks") + parser.add_option("-u", "--upload", action="store_false", dest='commit', + help="Use upload instead of commit checks") parser.add_option("-r", "--recursive", action="store_true", help="Act recursively") - parser.add_option("-v", "--verbose", action="store_true", + parser.add_option("-v", "--verbose", action="store_true", default=False, help="Verbose output") - parser.add_option("--files", default='') + parser.add_option("--files") parser.add_option("--name", default='no name') parser.add_option("--description", default='') parser.add_option("--issue", type='int', default=0) parser.add_option("--patchset", type='int', default=0) - parser.add_options("--root", default='') - parser.add_options("--default_presubmit", default='') - parser.add_options("--may_prompt", action='store_true') + parser.add_option("--root", default='') + parser.add_option("--default_presubmit") + parser.add_option("--may_prompt", action='store_true', default=False) options, args = parser.parse_args(argv[1:]) - if not options.files: - options.files = ParseFiles(args, options.recursive) if not options.root: - options.root = gcl.GetRepositoryRoot() + options.root = os.getcwd() + if os.path.isdir(os.path.join(options.root, '.git')): + change_class = GitChange + if not options.files: + if args: + options.files = ParseFiles(args, options.recursive) + else: + # Grab modified files. + raise NotImplementedException() # TODO(maruel) Implement. + elif os.path.isdir(os.path.join(options.root, '.svn')): + change_class = SvnChange + if not options.files: + if args: + options.files = ParseFiles(args, options.recursive) + else: + # Grab modified files. + files = gclient.CaptureSVNStatus([options.root]) + else: + # Doesn't seem under source control. + change_class = Change if options.verbose: - print "Found %d files." % len(files) - return not DoPresubmitChecks(SvnChange(options.name, - options.description, - options.root, - options.files, - options.issue, - options.patchset), + print "Found %d files." % len(options.files) + return not DoPresubmitChecks(change_class(options.name, + options.description, + options.root, + options.files, + options.issue, + options.patchset), options.commit, options.verbose, sys.stdout, diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index 0de435a9c..004469d41 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -63,7 +63,8 @@ class PresubmitUnittest(PresubmitTestsBase): def testMembersChanged(self): self.mox.ReplayAll() members = [ - 'AffectedFile', 'Change', 'DoPresubmitChecks', 'InputApi', + 'AffectedFile', 'Change', 'DoPresubmitChecks', 'GitChange', + 'GitAffectedFile', 'InputApi', 'ListRelevantPresubmitFiles', 'Main', 'NotImplementedException', 'OutputApi', 'ParseFiles', 'PresubmitExecuter', 'ScanSubDirs', 'SvnAffectedFile', 'SvnChange', @@ -462,6 +463,24 @@ def CheckChangeOnCommit(input_api, output_api): '** Presubmit Messages **\n' 'http://tracker.com/42\n\n')) + def testMain(self): + self.mox.StubOutWithMock(presubmit, 'DoPresubmitChecks') + self.mox.StubOutWithMock(presubmit, 'ParseFiles') + presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.git') + ).AndReturn(False) + presubmit.os.path.isdir(presubmit.os.path.join(self.fake_root_dir, '.svn') + ).AndReturn(False) + #presubmit.ParseFiles([], None).AndReturn([]) + presubmit.DoPresubmitChecks(mox.IgnoreArg(), False, False, + mox.IgnoreArg(), + mox.IgnoreArg(), + None, False).AndReturn(False) + self.mox.ReplayAll() + + self.assertEquals(True, + presubmit.Main(['presubmit', '--root', + self.fake_root_dir])) + class InputApiUnittest(PresubmitTestsBase): """Tests presubmit.InputApi."""