Skip to content

Commit

Permalink
Merge pull request #31 from universalcore/feature/issue-31-create-sto…
Browse files Browse the repository at this point in the history
…rage-does-not-init-correctly

`StorageManager.create_storage()` doesnt initialize the repo correctly if its brand new
  • Loading branch information
miltontony committed Oct 27, 2014
2 parents f24340f + ee7fa0b commit cfe6884
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
11 changes: 6 additions & 5 deletions .travis.yml
@@ -1,15 +1,16 @@
language: python
python:
- "2.7"
- "pypy"
# - "pypy"
env:
- BUILD_FROM_SOURCE=False SMMAP=smmap GITDB=gitdb GITPYTHON="GitPython==0.3.2.RC1"
- BUILD_FROM_SOURCE=True SMMAP=git+https://github.com/Byron/smmap.git#egg=smmap GITDB=git+git://github.com/gitpython-developers/gitdb.git#egg=gitdb GITPYTHON=git+git://github.com/gitpython-developers/GitPython.git#egg=GitPython
matrix:
- SMMAP=smmap GITDB=gitdb GITPYTHON="GitPython==0.3.2.RC1"
- SMMAP=git+https://github.com/Byron/smmap.git#egg=smmap GITDB=git+git://github.com/gitpython-developers/gitdb.git#egg=gitdb GITPYTHON=git+git://github.com/gitpython-developers/GitPython.git#egg=GitPython
matrix:
fast_finish: true
allow_failures:
- python: pypy
- env: BUILD_FROM_SOURCE=True
- env: SMMAP=git+https://github.com/Byron/smmap.git#egg=smmap GITDB=git+git://github.com/gitpython-developers/gitdb.git#egg=gitdb GITPYTHON=git+git://github.com/gitpython-developers/GitPython.git#egg=GitPython
services:
- elasticsearch
install:
Expand All @@ -21,7 +22,7 @@ install:
- pip install -e . --use-wheel
- pip install coveralls --use-wheel
script:
- py.test elasticgit
- py.test elasticgit -s
- cd docs && make doctest && cd -
after_success:
- coveralls
Expand Down
18 changes: 14 additions & 4 deletions elasticgit/manager.py
Expand Up @@ -754,19 +754,29 @@ def workspace(cls, workdir, es={}, index_prefix='elastic-git'):
else cls.init_repo(workdir))
return Workspace(repo, get_es(**es), index_prefix)

@classmethod
def dot_git_path(cls, workdir):
return os.path.join(workdir, '.git')

@classmethod
def is_repo(cls, workdir):
return os.path.isdir(os.path.join(workdir, '.git'))
return cls.is_dir(cls.dot_git_path(workdir))

@classmethod
def is_dir(cls, workdir):
return os.path.isdir(workdir)

@classmethod
def read_repo(cls, workdir):
return Repo(workdir)

@classmethod
def init_repo(cls, workdir, bare=False):
if not cls.is_repo(workdir):
os.makedirs(workdir)
return Repo.init(workdir, bare)
return Repo.init(workdir, bare=bare)

@classmethod
def clone_repo(cls, repo_url, workdir):
return Repo.clone_from(repo_url, workdir)

Q
F
4 changes: 3 additions & 1 deletion elasticgit/tests/base.py
Expand Up @@ -38,6 +38,7 @@ class TestFallbackPerson(Model):
class ModelBaseTest(TestCase):

destroy = 'KEEP_REPO' not in os.environ
WORKING_DIR = '.test_repos/'

def mk_model(self, fields):
return type('TempModel', (Model,), fields)
Expand All @@ -48,11 +49,12 @@ def mk_index_prefix(self):
index_prefix = '%s-%s' % (class_name, test_name)
return index_prefix.lower()

def mk_workspace(self, working_dir='.test_repos/',
def mk_workspace(self, working_dir=None,
name=None,
url='http://localhost',
index_prefix=None,
auto_destroy=None):
working_dir = working_dir or self.WORKING_DIR
name = name or self.id()
index_prefix = index_prefix or self.mk_index_prefix()
auto_destroy = auto_destroy or self.destroy
Expand Down
58 changes: 57 additions & 1 deletion elasticgit/tests/test_storage.py
@@ -1,5 +1,8 @@
import os
import shutil

from elasticgit.tests.base import ModelBaseTest, TestPerson
from elasticgit.manager import StorageException
from elasticgit.manager import StorageException, EG, StorageManager

from git import Repo, GitCommandError

Expand Down Expand Up @@ -124,3 +127,56 @@ def test_load(self):
self.sm.git_path(
person.__class__, '%s.json' % (person.uuid,)))
self.assertEqual(person, reloaded_person)

def test_clone_from(self):
workspace = self.workspace
person = TestPerson({
'age': 1,
'name': 'Test Kees 1'
})
workspace.save(person, 'Saving a person')

clone_source = workspace.working_dir
clone_dest = '%s_clone' % (workspace.working_dir,)
cloned_repo = EG.clone_repo(clone_source, clone_dest)
self.addCleanup(EG.workspace(cloned_repo.working_dir).destroy)

sm = StorageManager(cloned_repo)
[cloned_person] = sm.iterate(TestPerson)
self.assertEqual(person, cloned_person)

def test_clone_from_bare_repository(self):
bare_repo_name = '%s_bare' % (self.id(),)
bare_repo_path = os.path.join(self.WORKING_DIR, bare_repo_name)
bare_repo = EG.init_repo(bare_repo_path, bare=True)
self.assertEqual(bare_repo.bare, True)

if self.destroy:
self.addCleanup(lambda: shutil.rmtree(bare_repo_path))

cloned_repo_path = '%s_clone' % (bare_repo_path,)
EG.clone_repo(bare_repo_path, cloned_repo_path)
new_workspace = EG.workspace(cloned_repo_path)
if self.destroy:
self.addCleanup(new_workspace.destroy)

# create an initial commit
initial_commit = new_workspace.sm.store_data(
'README.md', '# Hello World', 'Initial commit')

repo = new_workspace.repo
# NOTE: this is a bare remote repo and so it doesn't have a working
# copy checked out, there's nothing on the remote.
[origin] = repo.remotes
origin.push('refs/heads/master:refs/heads/master')

# Now pull in the changes in a remote repo to ensure we've
# succesfully are able to push & pull things around
second_cloned_repo_path = '%s_second_clone' % (bare_repo_path,)
EG.clone_repo(bare_repo_path, second_cloned_repo_path)
second_workspace = EG.workspace(second_cloned_repo_path)
second_workspace.fast_forward()
self.addCleanup(second_workspace.destroy)

[found_commit] = second_workspace.repo.iter_commits()
self.assertEqual(found_commit, initial_commit)

0 comments on commit cfe6884

Please sign in to comment.