Browse Source

[gclient_scm] Update _CheckClean() to use `git status`

This change updates the `_CheckClean()` fn to use `git status` instead
of `git update-index` and `git diff-index`. The `_CheckClean()` fn is
run during every update and this change reduces the subprocess calls
made by it by half.

Change-Id: Ie9a23b0bce748bec4cac88df09655569e88e4841
Bug: 1501984
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5076224
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Commit-Queue: Josip Sokcevic <sokcevic@chromium.org>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Aravind Vasudevan 1 year ago
parent
commit
259774c3ac
2 changed files with 8 additions and 21 deletions
  1. 7 20
      gclient_scm.py
  2. 1 1
      tests/gclient_scm_test.py

+ 7 - 20
gclient_scm.py

@@ -1422,28 +1422,15 @@ class GitWrapper(SCMWrapper):
                 '\tIf no git executable is running, then clean up %r and try again.\n'
                 '\tIf no git executable is running, then clean up %r and try again.\n'
                 % (self.relpath, revision, lockfile))
                 % (self.relpath, revision, lockfile))
 
 
-        # Make sure the tree is clean; see git-rebase.sh for reference
-        try:
-            scm.GIT.Capture(
-                ['update-index', '--ignore-submodules', '--refresh'],
-                cwd=self.checkout_path)
-        except subprocess2.CalledProcessError:
-            raise gclient_utils.Error(
-                '\n____ %s at %s\n'
-                '\tYou have unstaged changes.\n'
-                '\tcd into %s, run git status to see changes,\n'
-                '\tand commit, stash, or reset.\n' %
-                (self.relpath, revision, self.relpath))
-        try:
-            scm.GIT.Capture([
-                'diff-index', '--cached', '--name-status', '-r',
-                '--ignore-submodules', 'HEAD', '--'
-            ],
-                            cwd=self.checkout_path)
-        except subprocess2.CalledProcessError:
+        # Ensure that the tree is clean.
+        if scm.GIT.Capture([
+                'status', '--porcelain', '--untracked-files=no',
+                '--ignore-submodules'
+        ],
+                           cwd=self.checkout_path):
             raise gclient_utils.Error(
             raise gclient_utils.Error(
                 '\n____ %s at %s\n'
                 '\n____ %s at %s\n'
-                '\tYour index contains uncommitted changes\n'
+                '\tYou have uncommitted changes.\n'
                 '\tcd into %s, run git status to see changes,\n'
                 '\tcd into %s, run git status to see changes,\n'
                 '\tand commit, stash, or reset.\n' %
                 '\tand commit, stash, or reset.\n' %
                 (self.relpath, revision, self.relpath))
                 (self.relpath, revision, self.relpath))

+ 1 - 1
tests/gclient_scm_test.py

@@ -584,7 +584,7 @@ class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
             scm.update(options, (), [])
             scm.update(options, (), [])
         self.assertEqual(
         self.assertEqual(
             e.exception.args[0], '\n____ . at refs/remotes/origin/main\n'
             e.exception.args[0], '\n____ . at refs/remotes/origin/main\n'
-            '\tYou have unstaged changes.\n'
+            '\tYou have uncommitted changes.\n'
             '\tcd into ., run git status to see changes,\n'
             '\tcd into ., run git status to see changes,\n'
             '\tand commit, stash, or reset.\n')
             '\tand commit, stash, or reset.\n')