Commit a718c3eb authored by Aaron Gable's avatar Aaron Gable Committed by Commit Bot

Simplify git-cl-diff for Gerrit

Previously, git-cl-diff went through a dance where it would create
a new branch, download the uploaded patch onto that branch, and
then diff against that. This had all sorts of problems: if you
aborted the command, it might leave you on that branch; if you have
local changes, they might get clobbered or the command would refuse
to run.

Now that we're in a Gerrit-only world, and patchsets are by definition
equivalent to commits, we can simply diff against whatever local commit
was last uploaded or, in a pinch, fetch the uploaded commit and diff
against that.

Bug: 759893
Change-Id: Ia4b93dcfb9b8aba85817e62731f68d6450026e75
Reviewed-on: https://chromium-review.googlesource.com/639915Reviewed-by: 's avatarAndrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: Aaron Gable <agable@chromium.org>
parent 78ce24e3
......@@ -5724,43 +5724,27 @@ def CMDdiff(parser, args):
if args:
parser.error('Unrecognized args: %s' % ' '.join(args))
# Uncommitted (staged and unstaged) changes will be destroyed by
# "git reset --hard" if there are merging conflicts in CMDPatchIssue().
# Staged changes would be committed along with the patch from last
# upload, hence counted toward the "last upload" side in the final
# diff output, and this is not what we want.
if git_common.is_dirty_git_tree('diff'):
return 1
cl = Changelist(auth_config=auth_config)
issue = cl.GetIssue()
branch = cl.GetBranch()
if not issue:
DieWithError('No issue found for current branch (%s)' % branch)
TMP_BRANCH = 'git-cl-diff'
base_branch = cl.GetCommonAncestorWithUpstream()
# Create a new branch based on the merge-base
RunGit(['checkout', '-q', '-b', TMP_BRANCH, base_branch])
# Clear cached branch in cl object, to avoid overwriting original CL branch
# properties.
cl.ClearBranch()
try:
rtn = cl.CMDPatchIssue(issue, reject=False, nocommit=False, directory=None)
if rtn != 0:
RunGit(['reset', '--hard'])
return rtn
# Switch back to starting branch and diff against the temporary
# branch containing the latest rietveld patch.
cmd = ['git', 'diff']
if options.stat:
cmd.append('--stat')
cmd.extend([TMP_BRANCH, branch, '--'])
subprocess2.check_call(cmd)
finally:
RunGit(['checkout', '-q', branch])
RunGit(['branch', '-D', TMP_BRANCH])
base = cl._GitGetBranchConfigValue('last-upload-hash')
if not base:
base = cl._GitGetBranchConfigValue('gerritsquashhash')
if not base:
detail = cl._GetChangeDetail(['CURRENT_REVISION', 'CURRENT_COMMIT'])
revision_info = detail['revisions'][detail['current_revision']]
fetch_info = revision_info['fetch']['http']
RunGit(['fetch', fetch_info['url'], fetch_info['ref']])
base = 'FETCH_HEAD'
cmd = ['git', 'diff']
if options.stat:
cmd.append('--stat')
cmd.append(base)
subprocess2.check_call(cmd)
return 0
......
......@@ -2031,11 +2031,6 @@ class TestGitCl(TestCase):
self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
self.assertNotEqual(git_cl.main(['patch', '123456']), 0)
def test_diff_when_dirty(self):
# Do 'git cl diff' when local tree is dirty
self.mock(git_common, 'is_dirty_git_tree', lambda x: True)
self.assertNotEqual(git_cl.main(['diff']), 0)
@staticmethod
def _get_gerrit_codereview_server_calls(branch, value=None,
git_short_host='host',
......
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