diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py index 7871a03427..2fd9a45b83 100644 --- a/presubmit_canned_checks.py +++ b/presubmit_canned_checks.py @@ -2081,8 +2081,13 @@ def CheckForCommitObjects(input_api, output_api): # If the number of affected files is small, we can avoid scanning the entire # tree. affected_files = list(input_api.AffectedFiles()) + + # We must scan the full tree if DEPS is modified to ensure that any change + # in DEPS is reflected in the gitlinks. + deps_modified = any(f.LocalPath() == 'DEPS' for f in affected_files) + cmd = ['git', 'ls-tree', '-z', '--full-tree', 'HEAD'] - if len(affected_files) < 1000: + if len(affected_files) < 1000 and not deps_modified: # We need to pass the paths relative to the repository root. repo_root = input_api.change.RepositoryRoot() files_to_check = [ diff --git a/tests/check_for_commit_objects_test.py b/tests/check_for_commit_objects_test.py index 2f3c700dc3..cae89e7ed6 100644 --- a/tests/check_for_commit_objects_test.py +++ b/tests/check_for_commit_objects_test.py @@ -14,6 +14,18 @@ import presubmit_canned_checks from testing_support.presubmit_canned_checks_test_mocks import MockInputApi, MockOutputApi, MockFile +class MockRelativeFile(MockFile): + + def __init__(self, rel_path, new_contents): + # We pass absolute path to super so AbsoluteLocalPath returns absolute path + abs_path = os.path.join('/tmp/repo', rel_path) + super(MockRelativeFile, self).__init__(abs_path, new_contents) + self._rel_path = rel_path + + def LocalPath(self): + return self._rel_path + + class CheckForCommitObjectsTest(unittest.TestCase): def setUp(self): @@ -70,6 +82,24 @@ class CheckForCommitObjectsTest(unittest.TestCase): self.assertIn('--full-tree', args) self.assertNotIn('f0.txt', args) + def testFullTreeExecutionWhenDepsModified(self): + # Small CL but DEPS is modified, should run full tree scan + self.input_api.files = [ + MockRelativeFile('DEPS', []), + MockRelativeFile('other.txt', []) + ] + + self.input_api.subprocess.check_output = mock.Mock(return_value=b'') + + presubmit_canned_checks.CheckForCommitObjects(self.input_api, + self.output_api) + + args = self.input_api.subprocess.check_output.call_args[0][0] + self.assertIn('ls-tree', args) + self.assertIn('--full-tree', args) + # Should not list specific files when running full tree + self.assertNotIn('other.txt', args) + def testBatchedFoundCommit(self): # 1 file, found a commit object (gitlink) self.input_api.files = [