project: fix rebase check with worktrees

Add a helper to our git wrapper to find the .git subdir,
and then use that to detect internal rebase state.

Change-Id: I3b3b6ed4c1f45cc8c3c98dc19c7ca3aabdc46905
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/256532
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Mike Frysinger 2020-02-23 23:22:34 -05:00
parent d38300c756
commit 4b0eb5a441

View File

@ -986,11 +986,9 @@ class Project(object):
return None return None
def IsRebaseInProgress(self): def IsRebaseInProgress(self):
w = self.worktree return (os.path.exists(self.work_git.GetDotgitPath('rebase-apply')) or
g = os.path.join(w, '.git') os.path.exists(self.work_git.GetDotgitPath('rebase-merge')) or
return os.path.exists(os.path.join(g, 'rebase-apply')) \ os.path.exists(os.path.join(self.worktree, '.dotest')))
or os.path.exists(os.path.join(g, 'rebase-merge')) \
or os.path.exists(os.path.join(w, '.dotest'))
def IsDirty(self, consider_untracked=True): def IsDirty(self, consider_untracked=True):
"""Is the working directory modified in some way? """Is the working directory modified in some way?
@ -3232,11 +3230,28 @@ class Project(object):
finally: finally:
p.Wait() p.Wait()
def GetHead(self): def GetDotgitPath(self, subpath=None):
"""Return the full path to the .git dir.
As a convenience, append |subpath| if provided.
"""
if self._bare: if self._bare:
path = os.path.join(self._project.gitdir, HEAD) dotgit = self._gitdir
else: else:
path = self._project.GetHeadPath() dotgit = os.path.join(self._project.worktree, '.git')
if os.path.isfile(dotgit):
# Git worktrees use a "gitdir:" syntax to point to the scratch space.
with open(dotgit) as fp:
setting = fp.read()
assert setting.startswith('gitdir:')
gitdir = setting.split(':', 1)[1].strip()
dotgit = os.path.normpath(os.path.join(self._project.worktree, gitdir))
return dotgit if subpath is None else os.path.join(dotgit, subpath)
def GetHead(self):
"""Return the ref that HEAD points to."""
path = self.GetDotgitPath(subpath=HEAD)
try: try:
with open(path) as fd: with open(path) as fd:
line = fd.readline() line = fd.readline()