Skip to content

Commit

Permalink
Merge pull request #295 from bpoldrack/bf-get-repo
Browse files Browse the repository at this point in the history
BF:  get_repo_instance - do not create a new repo, do not populate handle meta unless create=True
  • Loading branch information
yarikoptic committed Nov 12, 2015
2 parents 6f00dfe + dcab04f commit c741153
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 17 deletions.
4 changes: 2 additions & 2 deletions datalad/cmdline/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ def get_repo_instance(path=curdir, class_=None):
abspath_)
else:
try:
return class_(dir_)
return class_(dir_, create=False)
except (RuntimeError, InvalidGitRepositoryError) as e:
raise RuntimeError("No %s repository found in %s" %
raise RuntimeError("No %s repository found in %s." %
(type_, abspath_))
else:
dir_ = normpath(opj(dir_, ".."))
Expand Down
128 changes: 123 additions & 5 deletions datalad/cmdline/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
__docformat__ = 'restructuredtext'

from mock import patch
from os.path import join as opj, exists
from nose.tools import assert_is_instance

from os import mkdir
from os.path import join as opj, exists, realpath
from ..helpers import get_datalad_master, get_repo_instance

from ...tests.utils import ok_, eq_, assert_cwd_unchanged, ok_clean_git, \
with_tempfile, SkipTest
with_tempfile, SkipTest, with_testrepos
from ...support.collectionrepo import CollectionRepo
from ...support.handlerepo import HandleRepo
from ...support.annexrepo import AnnexRepo
from ...support.gitrepo import GitRepo
from ...consts import DATALAD_COLLECTION_NAME
from ...utils import chpwd, getpwd


@assert_cwd_unchanged
Expand All @@ -38,6 +45,117 @@ class mocked_dirs:
get_repo_instance(lcpath, CollectionRepo)


# TODO:
def test_get_repo_instance():
raise SkipTest
@assert_cwd_unchanged
@with_testrepos('^basic_git$', flavors=['clone'])
def test_get_repo_instance_git(path):

# get instance from path:
repo = get_repo_instance(path, GitRepo)
assert_is_instance(repo, GitRepo)
eq_(realpath(repo.path), realpath(path))

old_pwd = getpwd()

# get instance from current dir:
chpwd(path)
repo = get_repo_instance()
assert_is_instance(repo, GitRepo)
eq_(realpath(repo.path), realpath(path))

# get instance from current subdir:
new_subdir = opj(path, "subdir")
mkdir(new_subdir)
chpwd(new_subdir)
eq_(new_subdir, getpwd())
repo = get_repo_instance()
assert_is_instance(repo, GitRepo)
eq_(realpath(repo.path), realpath(path))

chpwd(old_pwd)


@assert_cwd_unchanged
@with_testrepos('.*annex.*', flavors=['clone'])
def test_get_repo_instance_annex(path):

# get instance from path:
repo = get_repo_instance(path, AnnexRepo)
assert_is_instance(repo, AnnexRepo)
eq_(realpath(repo.path), realpath(path))

old_pwd = getpwd()

# get instance from current dir:
chpwd(path)
repo = get_repo_instance()
assert_is_instance(repo, AnnexRepo)
eq_(realpath(repo.path), realpath(path))

# get instance from current subdir:
new_subdir = opj(path, "subdir")
mkdir(new_subdir)
chpwd(new_subdir)
eq_(new_subdir, getpwd())
repo = get_repo_instance()
assert_is_instance(repo, AnnexRepo)
eq_(realpath(repo.path), realpath(path))

chpwd(old_pwd)


@assert_cwd_unchanged
@with_testrepos('.*handle.*', flavors=['clone'])
def test_get_repo_instance_handle(path):

# get instance from path:
repo = get_repo_instance(path, HandleRepo)
assert_is_instance(repo, HandleRepo)
eq_(realpath(repo.path), realpath(path))

old_pwd = getpwd()

# get instance from current dir:
chpwd(path)
repo = get_repo_instance()
assert_is_instance(repo, HandleRepo)
eq_(realpath(repo.path), realpath(path))

# get instance from current subdir:
new_subdir = opj(path, "subdir")
mkdir(new_subdir)
chpwd(new_subdir)
eq_(new_subdir, getpwd())
repo = get_repo_instance()
assert_is_instance(repo, HandleRepo)
eq_(realpath(repo.path), realpath(path))

chpwd(old_pwd)


@assert_cwd_unchanged
@with_testrepos('.*collection.*', flavors=['clone'])
def test_get_repo_instance_collection(path):

# get instance from path:
repo = get_repo_instance(path, CollectionRepo)
assert_is_instance(repo, CollectionRepo)
eq_(realpath(repo.path), realpath(path))

old_pwd = getpwd()

# get instance from current dir:
chpwd(path)
repo = get_repo_instance()
assert_is_instance(repo, CollectionRepo)
eq_(realpath(repo.path), realpath(path))

# get instance from current subdir:
new_subdir = opj(path, "subdir")
mkdir(new_subdir)
chpwd(new_subdir)
eq_(new_subdir, getpwd())
repo = get_repo_instance()
assert_is_instance(repo, CollectionRepo)
eq_(realpath(repo.path), realpath(path))

chpwd(old_pwd)
15 changes: 11 additions & 4 deletions datalad/interface/tests/test_upgrade_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@
from ...api import upgrade_handle

from ...support.annexrepo import AnnexRepo
from ...tests.utils import assert_raises
from ...tests.utils import assert_raises, eq_
from ...tests.utils import with_testrepos
from ...tests.utils import assert_cwd_unchanged
from ...tests.utils import ok_startswith
from ...utils import rmtree


@assert_cwd_unchanged
@with_testrepos('.*annex.*', flavors=['clone'])
@with_testrepos('.*handle.*', flavors=['clone'])
def test_upgrade_handle(path):
repo = AnnexRepo(path, create=False, init=True)
# it is there and needs to be initialized after cloning
# TODO: that 'init' has to move to testrepo setup somehow

upgrade_handle(path) # shouldn't fail if nothing to upgrade

# remove remote 'origin' -- should fail
repo = AnnexRepo(path, create=False) # it is there
repo.git_remote_remove('origin')
eq_(repo.git_get_remotes(), [])

with assert_raises(RuntimeError) as ex:
upgrade_handle(path)
ok_startswith(str(ex.exception), 'No remotes were found for %s' % path)
Expand All @@ -44,4 +50,5 @@ def test_upgrade_handle(path):
# TODO: test actual upgrade:
# probably generate a clone of a clone,
# update original clone,
# verify that upgrade was carried out
# verify that upgrade was carried out

14 changes: 8 additions & 6 deletions datalad/support/collectionrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ def __init__(self, path, url=None, name=None, runner=None, create=True):

importer = CustomImporter('Collection', 'Collection', DLNS.this)
# load existing files:

if REPO_CONFIG_FILE in self.get_indexed_files():
importer.import_data(opj(self.path, REPO_CONFIG_FILE))
elif not create:
Expand Down Expand Up @@ -355,13 +356,14 @@ def __init__(self, path, url=None, name=None, runner=None, create=True):
raise CollectionBrokenError("Missing label in %s." %
opj(self.path, REPO_CONFIG_FILE))

importer.set_graphs(graphs) # necessary?
importer.store_data(self.path)
self.git_add([REPO_CONFIG_FILE, REPO_STD_META_FILE])
if create:
importer.set_graphs(graphs) # necessary?
importer.store_data(self.path)
self.git_add([REPO_CONFIG_FILE, REPO_STD_META_FILE])

if not self.repo.head.is_valid() or \
self.repo.index.diff(self.repo.head.commit):
self.git_commit("Initialized collection metadata.")
if not self.repo.head.is_valid() or \
self.repo.index.diff(self.repo.head.commit):
self.git_commit("Initialized collection metadata.")

def _get_cfg(self):
config_handler = CustomImporter('Collection', 'Collection', DLNS.this)
Expand Down

0 comments on commit c741153

Please sign in to comment.