Commit 7a1f04d3 authored by Daniel Cheng's avatar Daniel Cheng Committed by Commit Bot

Implement presubmit API to get the old contents of an affected file.

BUG=702851

Change-Id: I6f005d19524cbb8b361d3fcb0b91912885c46e00
Reviewed-on: https://chromium-review.googlesource.com/456755
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: 's avatarDirk Pranke <dpranke@chromium.org>
parent 14db1dfd
...@@ -584,6 +584,10 @@ class _DiffCache(object): ...@@ -584,6 +584,10 @@ class _DiffCache(object):
"""Get the diff for a particular path.""" """Get the diff for a particular path."""
raise NotImplementedError() raise NotImplementedError()
def GetOldContents(self, path, local_root):
"""Get the old version for a particular path."""
raise NotImplementedError()
class _GitDiffCache(_DiffCache): class _GitDiffCache(_DiffCache):
"""DiffCache implementation for git; gets all file diffs at once.""" """DiffCache implementation for git; gets all file diffs at once."""
...@@ -626,6 +630,9 @@ class _GitDiffCache(_DiffCache): ...@@ -626,6 +630,9 @@ class _GitDiffCache(_DiffCache):
return self._diffs_by_file[path] return self._diffs_by_file[path]
def GetOldContents(self, path, local_root):
return scm.GIT.GetOldContents(local_root, path, branch=self._upstream)
class AffectedFile(object): class AffectedFile(object):
"""Representation of a file in a change.""" """Representation of a file in a change."""
...@@ -670,6 +677,18 @@ class AffectedFile(object): ...@@ -670,6 +677,18 @@ class AffectedFile(object):
"""An alias to IsTestableFile for backwards compatibility.""" """An alias to IsTestableFile for backwards compatibility."""
return self.IsTestableFile() return self.IsTestableFile()
def OldContents(self):
"""Returns an iterator over the lines in the old version of file.
The new version is the file in the user's workspace, i.e. the "right hand
side".
Contents will be empty if the file is a directory or does not exist.
Note: The carriage returns (LF or CR) are stripped off.
"""
return self._diff_cache.GetOldContents(self.LocalPath(),
self._local_root).splitlines()
def NewContents(self): def NewContents(self):
"""Returns an iterator over the lines in the new version of file. """Returns an iterator over the lines in the new version of file.
......
...@@ -254,6 +254,13 @@ class GIT(object): ...@@ -254,6 +254,13 @@ class GIT(object):
upstream_branch = ''.join(remote_ref) upstream_branch = ''.join(remote_ref)
return upstream_branch return upstream_branch
@staticmethod
def GetOldContents(cwd, filename, branch=None):
if not branch:
branch = GIT.GetUpstreamBranch(cwd)
command = ['show', '%s:%s' % (branch, filename)]
return GIT.Capture(command, cwd=cwd, strip_out=False)
@staticmethod @staticmethod
def GenerateDiff(cwd, branch=None, branch_head='HEAD', full_move=False, def GenerateDiff(cwd, branch=None, branch_head='HEAD', full_move=False,
files=None): files=None):
......
...@@ -1482,7 +1482,7 @@ class AffectedFileUnittest(PresubmitTestsBase): ...@@ -1482,7 +1482,7 @@ class AffectedFileUnittest(PresubmitTestsBase):
members = [ members = [
'AbsoluteLocalPath', 'Action', 'ChangedContents', 'DIFF_CACHE', 'AbsoluteLocalPath', 'Action', 'ChangedContents', 'DIFF_CACHE',
'GenerateScmDiff', 'IsTestableFile', 'IsTextFile', 'LocalPath', 'GenerateScmDiff', 'IsTestableFile', 'IsTextFile', 'LocalPath',
'NewContents', 'NewContents', 'OldContents',
] ]
# If this test fails, you should add the relevant test. # If this test fails, you should add the relevant test.
self.compareMembers( self.compareMembers(
......
...@@ -86,6 +86,7 @@ class GitWrapperTestCase(BaseSCMTestCase): ...@@ -86,6 +86,7 @@ class GitWrapperTestCase(BaseSCMTestCase):
'GetDifferentFiles', 'GetDifferentFiles',
'GetEmail', 'GetEmail',
'GetGitDir', 'GetGitDir',
'GetOldContents',
'GetPatchName', 'GetPatchName',
'GetUpstreamBranch', 'GetUpstreamBranch',
'IsDirectoryVersioned', 'IsDirectoryVersioned',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment