From 8baaea7b8327b060dde2e34301ee98cdacbeeba2 Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Thu, 8 Sep 2011 12:55:29 +0000 Subject: [PATCH] Add automatic is_new=True on git copy or rename. R=dpranke@chromium.org BUG= TEST= Review URL: http://codereview.chromium.org/7847005 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@100140 0039d316-1c4b-4281-b951-d872f2087c98 --- patch.py | 17 ++++++++++------- tests/patch_test.py | 11 ++++++----- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/patch.py b/patch.py index 888e10095..8b0407fab 100644 --- a/patch.py +++ b/patch.py @@ -221,6 +221,7 @@ class FilePatchDiff(FilePatchBase): if old not in (self.filename, 'dev/null'): # Copy or rename. self.source_filename = old + self.is_new = True last_line = '' @@ -275,9 +276,10 @@ class FilePatchDiff(FilePatchBase): if match: if last_line[:3] in ('---', '+++'): self._fail('--- and +++ are reversed') - self.is_new = match.group(1) == '/dev/null' - # TODO(maruel): Use self.source_file. - if self.mangle(match.group(1)) not in (old, 'dev/null'): + if match.group(1) == '/dev/null': + self.is_new = True + elif self.mangle(match.group(1)) != old: + # git patches are always well formatted, do not allow random filenames. self._fail('Unexpected git diff: %s != %s.' % (old, match.group(1))) if not lines or not lines[0].startswith('+++'): self._fail('Missing git diff output name.') @@ -287,7 +289,6 @@ class FilePatchDiff(FilePatchBase): if match: if not last_line.startswith('---'): self._fail('Unexpected git diff: --- not following +++.') - # TODO(maruel): new == self.filename. if '/dev/null' == match.group(1): self.is_delete = True elif self.filename != self.mangle(match.group(1)): @@ -327,10 +328,12 @@ class FilePatchDiff(FilePatchBase): if match: if last_line[:3] in ('---', '+++'): self._fail('--- and +++ are reversed') - self.is_new = match.group(1) == '/dev/null' - if (self.mangle(match.group(1)) != self.filename and - match.group(1) != '/dev/null'): + if match.group(1) == '/dev/null': + self.is_new = True + elif self.mangle(match.group(1)) != self.filename: + # guess the source filename. self.source_filename = match.group(1) + self.is_new = True if not lines or not lines[0].startswith('+++'): self._fail('Nothing after header.') return diff --git a/tests/patch_test.py b/tests/patch_test.py index 5f6562a00..db621ea51 100755 --- a/tests/patch_test.py +++ b/tests/patch_test.py @@ -459,7 +459,7 @@ class PatchTest(unittest.TestCase): p = patch.FilePatchDiff('tools/run_local_server.sh', GIT_RENAME, []) self._check_patch(p, 'tools/run_local_server.sh', GIT_RENAME, is_git_diff=True, patchlevel=1, - source_filename='tools/run_local_server.PY') + source_filename='tools/run_local_server.PY', is_new=True) def testGitRenamePartial(self): p = patch.FilePatchDiff( @@ -467,12 +467,12 @@ class PatchTest(unittest.TestCase): self._check_patch( p, 'chromeos/views/webui_menu_widget.h', GIT_RENAME_PARTIAL, source_filename='chromeos/views/DOMui_menu_widget.h', is_git_diff=True, - patchlevel=1) + patchlevel=1, is_new=True) def testGitCopy(self): p = patch.FilePatchDiff('pp', GIT_COPY, []) self._check_patch(p, 'pp', GIT_COPY, is_git_diff=True, patchlevel=1, - source_filename='PRESUBMIT.py') + source_filename='PRESUBMIT.py', is_new=True) def testOnlyHeader(self): diff = '--- file_a\n+++ file_a\n' @@ -503,7 +503,7 @@ class PatchTest(unittest.TestCase): diff = '--- file_a\n+++ file_b\n' p = patch.FilePatchDiff('file_b', diff, []) # Should it be marked as new? - self._check_patch(p, 'file_b', diff, source_filename='file_a') + self._check_patch(p, 'file_b', diff, source_filename='file_a', is_new=True) def testGitCopyPartial(self): diff = ( @@ -523,7 +523,8 @@ class PatchTest(unittest.TestCase): p = patch.FilePatchDiff('wtf2', diff, []) # Should it be marked as new? self._check_patch( - p, 'wtf2', diff, source_filename='wtf', is_git_diff=True, patchlevel=1) + p, 'wtf2', diff, source_filename='wtf', is_git_diff=True, patchlevel=1, + is_new=True) def testGitExe(self): diff = (