Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RF: Move get_git_dir() into GitRepo #2886

Merged
merged 1 commit into from Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions datalad/distribution/dataset.py
Expand Up @@ -10,7 +10,6 @@
"""

import logging
from os.path import abspath
from os.path import curdir
from os.path import exists
from os.path import join as opj
Expand Down Expand Up @@ -40,7 +39,6 @@
from datalad.utils import optional_args, expandpath, is_explicit_path
from datalad.utils import get_dataset_root
from datalad.utils import dlabspath
from datalad.distribution.utils import get_git_dir


lgr = logging.getLogger('datalad.dataset')
Expand Down
9 changes: 5 additions & 4 deletions datalad/distribution/tests/test_utils.py
Expand Up @@ -13,7 +13,8 @@
from os.path import join as opj

from datalad.distribution.utils import _get_flexible_source_candidates
from datalad.distribution.utils import get_git_dir

from datalad.support.gitrepo import GitRepo

from datalad.tests.utils import with_tempfile
from datalad.tests.utils import eq_
Expand Down Expand Up @@ -64,7 +65,7 @@ def test_get_flexible_source_candidates():
@with_tempfile
def test_get_git_dir(path):
# minimal, only missing coverage
assert_raises(RuntimeError, get_git_dir, path)
assert_raises(RuntimeError, GitRepo.get_git_dir, path)

srcpath = opj(path, 'src')
targetpath = opj(path, 'target')
Expand All @@ -74,9 +75,9 @@ def test_get_git_dir(path):
if not on_windows:
# with PY3 would also work with Windows 6+
os.symlink(srcpath, targetgitpath)
eq_(srcpath, get_git_dir(targetpath))
eq_(srcpath, GitRepo.get_git_dir(targetpath))
# cleanup for following test
unlink(targetgitpath)
with open(targetgitpath, 'w') as f:
f.write('gitdir: {}'.format(srcpath))
eq_(srcpath, get_git_dir(targetpath))
eq_(srcpath, GitRepo.get_git_dir(targetpath))
44 changes: 2 additions & 42 deletions datalad/distribution/utils.py
Expand Up @@ -11,18 +11,15 @@
"""

import logging
import re

from os.path import exists
from os.path import isdir
from os.path import join as opj
from os.path import islink
from os.path import isabs
from os.path import normpath
import posixpath

from six.moves.urllib.parse import unquote as urlunquote

from datalad.support.annexrepo import GitRepo
from datalad.support.annexrepo import AnnexRepo
from datalad.support.network import DataLadRI
from datalad.support.network import URL
Expand All @@ -43,50 +40,13 @@ def _fixup_submodule_dotgit_setup(ds, relativepath):
# move .git to superrepo's .git/modules, remove .git, create
# .git-file
path = opj(ds.path, relativepath)
src_dotgit = get_git_dir(path)
src_dotgit = GitRepo.get_git_dir(path)

# at this point install always yields the desired result
# just make sure
assert(src_dotgit == '.git')


def get_git_dir(path):
"""figure out a repo's gitdir

'.git' might be a directory, a symlink or a file

Parameter
---------
path: str
currently expected to be the repos base dir

Returns
-------
str
relative path to the repo's git dir; So, default would be ".git"
"""

from os.path import isfile

dot_git = opj(path, ".git")
if not exists(dot_git):
raise RuntimeError("Missing .git in %s." % path)
elif islink(dot_git):
# readlink cannot be imported on windows, but there should also
# be no symlinks
from os import readlink
git_dir = readlink(dot_git)
elif isdir(dot_git):
git_dir = ".git"
elif isfile(dot_git):
with open(dot_git) as f:
git_dir = f.readline()
if git_dir.startswith("gitdir:"):
git_dir = git_dir[7:]
git_dir = git_dir.strip()

return git_dir


def _get_git_url_from_source(source):
"""Return URL for cloning associated with a source specification
Expand Down
4 changes: 2 additions & 2 deletions datalad/interface/clean.py
Expand Up @@ -19,11 +19,11 @@
from ..consts import ANNEX_TEMP_DIR
from ..consts import SEARCH_INDEX_DOTGITDIR

from datalad.support.gitrepo import GitRepo
from datalad.support.constraints import EnsureNone
from datalad.distribution.dataset import EnsureDataset
from datalad.distribution.dataset import require_dataset
from datalad.distribution.dataset import datasetmethod
from datalad.distribution.utils import get_git_dir
from datalad.interface.annotate_paths import AnnotatePaths
from datalad.interface.common_opts import recursion_flag
from datalad.interface.common_opts import recursion_limit
Expand Down Expand Up @@ -90,7 +90,7 @@ def __call__(dataset=None, what=None, recursive=False, recursion_limit=None):
yield ap
continue
d = ap['path']
gitdir = get_git_dir(d)
gitdir = GitRepo.get_git_dir(d)
for dirpath, flag, msg, sing_pl in [
(ARCHIVES_TEMP_DIR, "cached-archives",
"temporary archive", ("directory", "directories")),
Expand Down
4 changes: 2 additions & 2 deletions datalad/metadata/search.py
Expand Up @@ -34,7 +34,7 @@
from datalad.distribution.dataset import Dataset
from datalad.distribution.dataset import datasetmethod, EnsureDataset, \
require_dataset
from datalad.distribution.utils import get_git_dir
from datalad.support.gitrepo import GitRepo
from datalad.support.param import Parameter
from datalad.support.constraints import EnsureNone
from datalad.support.constraints import EnsureInt
Expand Down Expand Up @@ -263,7 +263,7 @@ def __init__(self, ds, force_reindex=False, **kwargs):

self.idx_obj = None
# where does the bunny have the eggs?
self.index_dir = opj(self.ds.path, get_git_dir(self.ds.path), SEARCH_INDEX_DOTGITDIR)
self.index_dir = opj(self.ds.path, GitRepo.get_git_dir(ds), SEARCH_INDEX_DOTGITDIR)
self._mk_search_index(force_reindex)

def show_keys(self, mode):
Expand Down
38 changes: 38 additions & 0 deletions datalad/support/gitrepo.py
Expand Up @@ -802,6 +802,44 @@ def is_valid_repo(cls, path):
"""Returns if a given path points to a git repository"""
return exists(opj(path, '.git'))

@staticmethod
def get_git_dir(repo):
"""figure out a repo's gitdir

'.git' might be a directory, a symlink or a file

Parameter
---------
repo: path or Repo instance
currently expected to be the repos base dir

Returns
-------
str
relative path to the repo's git dir; So, default would be ".git"
"""
if hasattr(repo, 'path'):
# repo instance like given
repo = repo.path
dot_git = op.join(repo, ".git")
if not op.exists(dot_git):
raise RuntimeError("Missing .git in %s." % repo)
elif op.islink(dot_git):
# readlink cannot be imported on windows, but there should also
# be no symlinks
from os import readlink
git_dir = readlink(dot_git)
elif op.isdir(dot_git):
git_dir = ".git"
elif op.isfile(dot_git):
with open(dot_git) as f:
git_dir = f.readline()
if git_dir.startswith("gitdir:"):
git_dir = git_dir[7:]
git_dir = git_dir.strip()

return git_dir

@property
def config(self):
"""Get an instance of the parser for the persistent repository
Expand Down