[no-sync] Add method to check for diffs between the current checkout and another commit.

Bug: 1339472

Change-Id: If97d2fad7e5c61ec907e15a00db225b653a62bef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3738365
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Joanna Wang <jojwang@chromium.org>
diff --git a/gclient_scm.py b/gclient_scm.py
index 8810e2c..f58c07a 100644
--- a/gclient_scm.py
+++ b/gclient_scm.py
@@ -536,6 +536,25 @@
     if options.reset_patch_ref:
       self._Capture(['reset', '--soft', base_rev])
 
+  def check_diff(self, previous_commit, files=None):
+    # type: (str, Optional[List[str]]) -> bool
+    """Check if a diff exists between the current commit and `previous_commit`.
+
+      Returns True if there were diffs or if an error was encountered.
+    """
+    cmd = ['diff', previous_commit, '--quiet']
+    if files:
+      cmd += ['--'] + files
+    try:
+      self._Capture(cmd)
+      return False
+    except subprocess2.CalledProcessError as e:
+      # git diff --quiet exits with 1 if there were diffs.
+      if e.returncode != 1:
+        self.Print('git returned non-zero exit status %s:\n%s' %
+                   (e.returncode, e.stderr.decode('utf-8')))
+      return True
+
   def update(self, options, args, file_list):
     """Runs git to update or transparently checkout the working copy.