From 87b9bf026a99deaab178e0eb232a0379c25543e8 Mon Sep 17 00:00:00 2001 From: "sbc@chromium.org" Date: Thu, 26 Sep 2013 20:35:15 +0000 Subject: [PATCH] Add 'git cl diff' command. This command shows the differences between local branch and the last patch uploaded to rietveld. The idea being that you can see what would be uploaded before you actually upload. This can useful when using 'git cl patch' to move changes between checkouts and working from more than one machine at once. Review URL: https://chromiumcodereview.appspot.com/24276008 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@225552 0039d316-1c4b-4281-b951-d872f2087c98 --- git_cl.py | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/git_cl.py b/git_cl.py index 289640a77..1062a7a0f 100755 --- a/git_cl.py +++ b/git_cl.py @@ -1861,7 +1861,18 @@ def CMDpatch(parser, args): # TODO(maruel): Use apply_issue.py # TODO(ukai): use gerrit-cherry-pick for gerrit repository? - if issue_arg.isdigit(): + if options.newbranch: + if options.force: + RunGit(['branch', '-D', options.newbranch], + stderr=subprocess2.PIPE, error_ok=True) + RunGit(['checkout', '-b', options.newbranch, + Changelist().GetUpstreamBranch()]) + + return PatchIssue(issue_arg, options.reject, options.nocommit) + + +def PatchIssue(issue_arg, reject, nocommit): + if type(issue_arg) is int or issue_arg.isdigit(): # Input is an issue id. Figure out the URL. issue = int(issue_arg) cl = Changelist(issue=issue) @@ -1878,13 +1889,6 @@ def CMDpatch(parser, args): patchset = int(match.group(2)) patch_data = urllib2.urlopen(issue_arg).read() - if options.newbranch: - if options.force: - RunGit(['branch', '-D', options.newbranch], - stderr=subprocess2.PIPE, error_ok=True) - RunGit(['checkout', '-b', options.newbranch, - Changelist().GetUpstreamBranch()]) - # Switch up to the top-level directory, if necessary, in preparation for # applying the patch. top = RunGit(['rev-parse', '--show-cdup']).strip() @@ -1909,7 +1913,7 @@ def CMDpatch(parser, args): # pick up file adds. # The --index flag means: also insert into the index (so we catch adds). cmd = ['git', 'apply', '--index', '-p0'] - if options.reject: + if reject: cmd.append('--reject') elif IsGitVersionAtLeast('1.7.12'): cmd.append('--3way') @@ -1920,7 +1924,7 @@ def CMDpatch(parser, args): DieWithError('Failed to apply the patch') # If we had an issue, commit the current state and register the issue. - if not options.nocommit: + if not nocommit: RunGit(['commit', '-m', 'patch from issue %s' % issue]) cl = Changelist() cl.SetIssue(issue) @@ -2128,6 +2132,31 @@ def CMDset_close(parser, args): return 0 +def CMDdiff(parser, args): + """shows differences between local tree and last upload.""" + cl = Changelist() + branch = cl.GetBranch() + TMP_BRANCH = 'git-cl-diff' + base_branch = RunGit(['merge-base', cl.GetUpstreamBranch(), 'HEAD']).strip() + + # Create a new branch based on the merge-base + RunGit(['checkout', '-q', '-b', TMP_BRANCH, base_branch]) + try: + # Patch in the latest changes from rietveld. + rtn = PatchIssue(cl.GetIssue(), False, False) + if rtn != 0: + return rtn + + # Switch back to starting brand and diff against the temporary + # branch containing the latest rietveld patch. + subprocess2.check_call(['git', 'diff', TMP_BRANCH, branch]) + finally: + RunGit(['checkout', '-q', branch]) + RunGit(['branch', '-D', TMP_BRANCH]) + + return 0 + + def CMDowners(parser, args): """interactively find the owners for reviewing""" parser.add_option(