Skip to content

Commit

Permalink
RF+BF: return full .dot_git path if .git is not subpath
Browse files Browse the repository at this point in the history
In my previous refactoring, while reading the old get_git_dir implementation
that path would always be relative (since ".git" was returned etc).  But it
did resolve symlinks and read links from .git file.

This change does not necessarily makes it match 100%  old behavior, since
we would still return relative it if it points e.g. somewhere under .git somehow,
but as for the target need of discovering .git location I think it should
be functionally equivalent (I would have even stripped maybe_relative completely).
  • Loading branch information
yarikoptic committed Jun 12, 2020
1 parent 173b356 commit 9b555c5
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions datalad/support/gitrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,15 +1182,15 @@ def is_valid_repo(cls, path):
)

@staticmethod
def _get_dot_git(pathobj, *, ok_missing=False, relative=False):
def _get_dot_git(pathobj, *, ok_missing=False, maybe_relative=False):
"""Given a pathobj to a repository return path to .git/ directory
Parameters
----------
pathjob: Path
pathobj: Path
ok_missing: bool, optional
Allow for .git to be missing (useful while sensing before repo is initialized)
relative: bool, optional
maybe_relative: bool, optional
Return path relative to pathobj
Raises
Expand All @@ -1201,7 +1201,7 @@ def _get_dot_git(pathobj, *, ok_missing=False, relative=False):
Returns
-------
Path
Absolute (unless relative=True) path to resolved .git/ directory
Absolute (unless maybe_relative=True) path to resolved .git/ directory
"""
dot_git = pathobj / '.git'
if dot_git.is_file():
Expand All @@ -1216,8 +1216,12 @@ def _get_dot_git(pathobj, *, ok_missing=False, relative=False):
elif not (ok_missing or dot_git.exists()):
raise RuntimeError("Missing .git in %s." % pathobj)
# Primarily a compat kludge for get_git_dir, remove when it is deprecated
if relative:
dot_git = dot_git.relative_to(pathobj)
if maybe_relative:
try:
dot_git = dot_git.relative_to(pathobj)
except ValueError:
# is not a subpath, return as is
lgr.debug("Path %r is not subpath of %r", dot_git, pathobj)
return dot_git

@staticmethod
Expand Down Expand Up @@ -1246,7 +1250,7 @@ def get_git_dir(repo):
"""
if isinstance(repo, GitRepo):
return str(repo.dot_git)
return str(GitRepo._get_dot_git(Path(repo), ok_missing=False, relative=True))
return str(GitRepo._get_dot_git(Path(repo), ok_missing=False, maybe_relative=True))

@property
def config(self):
Expand Down

0 comments on commit 9b555c5

Please sign in to comment.