Disallow applying patches when git tree is dirty

This allows proper clean-up after failing to apply a patch. Therefore,
if "git cl diff" ends up having conflict, it could be cleaned up.

BUG=438362

Review URL: https://codereview.chromium.org/896453002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@294679 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/git_cl.py b/git_cl.py
index 95bf695..6470575 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -2531,6 +2531,10 @@
     return 1
   issue_arg = args[0]
 
+  # We don't want uncommitted changes mixed up with the patch.
+  if is_dirty_git_tree('patch'):
+    return 1
+
   # TODO(maruel): Use apply_issue.py
   # TODO(ukai): use gerrit-cherry-pick for gerrit repository?
 
@@ -2546,6 +2550,10 @@
 
 
 def PatchIssue(issue_arg, reject, nocommit, directory):
+  # There's a "reset --hard" when failing to apply the patch. In order
+  # not to destroy users' data, make sure the tree is not dirty here.
+  assert(not is_dirty_git_tree('apply'))
+
   if type(issue_arg) is int or issue_arg.isdigit():
     # Input is an issue id.  Figure out the URL.
     issue = int(issue_arg)
@@ -2594,6 +2602,7 @@
     subprocess2.check_call(cmd, env=GetNoGitPagerEnv(),
                            stdin=patch_data, stdout=subprocess2.VOID)
   except subprocess2.CalledProcessError:
+    RunGit(['reset', '--hard'])
     DieWithError('Failed to apply the patch')
 
   # If we had an issue, commit the current state and register the issue.
@@ -2900,6 +2909,15 @@
 def CMDdiff(parser, args):
   """Shows differences between local tree and last upload."""
   parser.parse_args(args)
+
+  # Uncommitted (staged and unstaged) changes will be destroyed by
+  # "git reset --hard" if there are merging conflicts in PatchIssue().
+  # 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 is_dirty_git_tree('diff'):
+    return 1
+
   cl = Changelist()
   issue = cl.GetIssue()
   branch = cl.GetBranch()