depot_tools: Make gclient_scm Python 3 compatible.

Bug: 984182
Change-Id: Ib9c5d2762546f29eaca5eae89b0428431067da4a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1753029
Auto-Submit: Edward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Robbie Iannucci <iannucci@chromium.org>
Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
diff --git a/gclient_scm.py b/gclient_scm.py
index 6bd0566..d599bfe 100644
--- a/gclient_scm.py
+++ b/gclient_scm.py
@@ -448,7 +448,7 @@
             target_rev, patch_rev, base_rev))
         self.Print('Current dir is %r' % self.checkout_path)
         self.Print('git returned non-zero exit status %s:\n%s' % (
-            e.returncode, e.stderr))
+            e.returncode, e.stderr.decode('utf-8')))
         # Print the current status so that developers know what changes caused
         # the patch failure, since git cherry-pick doesn't show that
         # information.
@@ -553,7 +553,7 @@
         files = self._Capture(
             ['-c', 'core.quotePath=false', 'ls-files']).splitlines()
         file_list.extend(
-            [os.path.join(self.checkout_path, f.decode()) for f in files])
+            [os.path.join(self.checkout_path, f) for f in files])
       if mirror:
         self._Capture(
             ['remote', 'set-url', '--push', 'origin', mirror.url])
@@ -761,7 +761,8 @@
         merge_output = self._Capture(merge_args)
       except subprocess2.CalledProcessError as e:
         rebase_files = []
-        if re.match('fatal: Not possible to fast-forward, aborting.', e.stderr):
+        if re.match(b'fatal: Not possible to fast-forward, aborting.',
+                    e.stderr):
           if not printed_path:
             self.Print('_____ %s at %s' % (self.relpath, revision),
                        timestamp=False)
@@ -791,19 +792,19 @@
               return
             else:
               self.Print('Input not recognized')
-        elif re.match("error: Your local changes to '.*' would be "
-                      "overwritten by merge.  Aborting.\nPlease, commit your "
-                      "changes or stash them before you can merge.\n",
+        elif re.match(b"error: Your local changes to '.*' would be "
+                      b"overwritten by merge.  Aborting.\nPlease, commit your "
+                      b"changes or stash them before you can merge.\n",
                       e.stderr):
           if not printed_path:
             self.Print('_____ %s at %s' % (self.relpath, revision),
                        timestamp=False)
             printed_path = True
-          raise gclient_utils.Error(e.stderr)
+          raise gclient_utils.Error(e.stderr.decode('utf-8'))
         else:
           # Some other problem happened with the merge
           logging.error("Error during fast-forward merge in %s!" % self.relpath)
-          self.Print(e.stderr)
+          self.Print(e.stderr.decode('utf-8'))
           raise
       else:
         # Fast-forward merge was successful
@@ -1117,8 +1118,8 @@
     try:
       rebase_output = scm.GIT.Capture(rebase_cmd, cwd=self.checkout_path)
     except subprocess2.CalledProcessError as e:
-      if (re.match(r'cannot rebase: you have unstaged changes', e.stderr) or
-          re.match(r'cannot rebase: your index contains uncommitted changes',
+      if (re.match(br'cannot rebase: you have unstaged changes', e.stderr) or
+          re.match(br'cannot rebase: your index contains uncommitted changes',
                    e.stderr)):
         while True:
           rebase_action = self._AskForData(
@@ -1136,18 +1137,19 @@
                                       "cd %s && git " % self.checkout_path
                                       + "%s" % ' '.join(rebase_cmd))
           elif re.match(r'show|s', rebase_action, re.I):
-            self.Print('%s' % e.stderr.strip())
+            self.Print('%s' % e.stderr.decode('utf-8').strip())
             continue
           else:
             gclient_utils.Error("Input not recognized")
             continue
-      elif re.search(r'^CONFLICT', e.stdout, re.M):
+      elif re.search(br'^CONFLICT', e.stdout, re.M):
         raise gclient_utils.Error("Conflict while rebasing this branch.\n"
                                   "Fix the conflict and run gclient again.\n"
                                   "See 'man git-rebase' for details.\n")
       else:
-        self.Print(e.stdout.strip())
-        self.Print('Rebase produced error output:\n%s' % e.stderr.strip())
+        self.Print(e.stdout.decode('utf-8').strip())
+        self.Print('Rebase produced error output:\n%s' %
+        e.stderr.decode('utf-8').strip())
         raise gclient_utils.Error("Unrecognized error, please merge or rebase "
                                   "manually.\ncd %s && git " %
                                   self.checkout_path
@@ -1177,7 +1179,7 @@
     try:
       self._Capture(['rev-list', '-n', '1', 'HEAD'])
     except subprocess2.CalledProcessError as e:
-      if ('fatal: bad object HEAD' in e.stderr
+      if (b'fatal: bad object HEAD' in e.stderr
           and self.cache_dir and self.cache_dir in url):
         self.Print((
           'Likely due to DEPS change with git cache_dir, '