project: don't rerun git status after a failed snapshot

When a status snapshot fails, fall back directly to _IsDirtyLegacy()
instead of IsDirty() to avoid running git status twice.

Expose Project.GetDirtyAndHead() so sync bloat checks can inspect
worktree state without invoking private Project methods.

Bug: 565047698
Change-Id: I6432ed82da88778eec9d151fd5b70bedb1efc62b
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/634424
Reviewed-by: Brian Gan <brgan@google.com>
Commit-Queue: Gavin Mak <gavinmak@google.com>
Tested-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Gavin Mak authored and gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com committed 2026-09-23 13:26:55 -07:00
1 parent e7d4d97ffc
commit 310c99571f
4 files changed
+102 -26

No files matched your search

+20 -1
View File
@@ -943,7 +943,26 @@ class Project:
if has_status_stash:
return bool(status.stash_count)
return self.HasStash()
return self.IsDirty(consider_untracked=True) or self.HasStash()
# The snapshot already failed; don't run git status again.
return self._IsDirtyLegacy(consider_untracked=True) or self.HasStash()
def GetDirtyAndHead(self) -> Tuple[bool, Optional[str]]:
"""Return whether the worktree is dirty, and the commit at HEAD.
Untracked files count as dirty. HEAD is None when it can't be
resolved, e.g. on an unborn branch. Both come from one status
snapshot when possible.
"""
status = self._GetStatusSnapshot(untracked_files="normal", branch=True)
if status is not None:
return status.is_dirty(consider_untracked=True), status.branch_oid
# The snapshot already failed; don't run git status again.
is_dirty = self._IsDirtyLegacy(consider_untracked=True)
try:
head = self.work_git.rev_parse(HEAD)
except GitError:
head = None
return is_dirty, head
_userident_name = None
_userident_email = None