Assertion on creating or adding files in bare repositories #59

Closed
clearclaw opened this Issue May 9, 2012 · 9 comments

Comments

Projects
None yet
3 participants

I can create new files from in-memory bytestreams in non-bare
repositories easily enough:

        repo = git.Repo (repopath)
        istream = IStream ("blob", len (filedata), StringIO (filedata))
        repo.odb.store (istream)
        blob = git.Blob (repo, istream.binsha, 0100644, filename)
        repo.index.add ([git.IndexEntry.from_blob (blob),])
        repo.index.commit (comment)

However this fails with bare repositories with an assertion error
around repo.working_tree_dir.

/home/allura/scm/git/p/jcltest/code.git/ in ()
----> 1 repo.index.add ([git.IndexEntry.from_blob (blob),])

/home/allura/anvil/local/lib/python2.7/site-packages/GitPython-0.3.0_beta2-py2.7.egg/git/index/util.pyc in
set_git_working_dir(self, _args, *_kwargs)
73 def set_git_working_dir(self, _args, *_kwargs):
74 cur_wd = os.getcwd()
---> 75 os.chdir(self.repo.working_tree_dir)
76 try:
77 return func(self, _args, *_kwargs)

/home/allura/anvil/local/lib/python2.7/site-packages/GitPython-0.3.0_beta2-py2.7.egg/git/repo/base.pyc in working_tree_dir(self)
174 :raise AssertionError: If we are a bare repository"""
175 if self._working_tree_dir is None:
--> 176 raise AssertionError( "Repository at %r is bare and does not have a working tree directory" % self.git_dir )
177 return self._working_tree_dir/home/allura/scm/git/p/jcltest/code.git/ in ()
----> 1 repo.index.add ([git.IndexEntry.from_blob (blob),])

/home/allura/anvil/local/lib/python2.7/site-packages/GitPython-0.3.0_beta2-py2.7.egg/git/index/util.pyc in set_git_working_dir(self, _args, *_kwargs)
73 def set_git_working_dir(self, _args, *_kwargs):
74 cur_wd = os.getcwd()
---> 75 os.chdir(self.repo.working_tree_dir)
76 try:
77 return func(self, _args, *_kwargs)

/home/allura/anvil/local/lib/python2.7/site-packages/GitPython-0.3.0_beta2-py2.7.egg/git/repo/base.pyc in working_tree_dir(self)
174 :raise AssertionError: If we are a bare repository"""
175 if self._working_tree_dir is None:
--> 176 raise AssertionError( "Repository at %r is bare and does not have a working tree directory" % self.git_dir )
177 return self._working_tree_dir

Owner

Byron commented May 9, 2012

Would you try the latest version please ? Its can be downloaded here:
http://pypi.python.org/pypi/GitPython/0.3.2.RC1

Still fails:

/home/allura/scm/git/p/jcltest/<ipython-input-11-1e177f9166a1> in <module>()
----> 1 repo.index.add ([IndexEntry.from_blob (blob),])
/home/allura/anvil/local/lib/python2.7/site-packages/GitPython-0.3.2.RC1-py2.7.egg/git/index/util.pyc in set_git_working_dir(self, *args, **kwargs)
    73         def set_git_working_dir(self, *args, **kwargs):
    74                 cur_wd = os.getcwd()
 ---> 75                 os.chdir(self.repo.working_tree_dir)
    76                 try:
    77                         return func(self, *args, **kwargs)
/home/allura/anvil/local/lib/python2.7/site-packages/GitPython-0.3.2.RC1-py2.7.egg/git/repo/base.pyc in working_tree_dir(self)
    179                 :raise AssertionError: If we are a bare repository"""
    180                 if self._working_tree_dir is None:
--> 181                         raise AssertionError( "Repository at %r is bare and does not have a working tree directory" % self.git_dir )
    182                 return self._working_tree_dir
    183
AssertionError: Repository at '/home/allura/scm/git/p/jcltest/code.git' is bare and does not have a working tree directory

Also repo.iter_commits(...) similarly does not work on bare repositories:

/home/allura/scm/git/p/jcltest/code.git/<ipython-input-5-589cbf8a19e3> in latest_commit(repo, tree)
     1 def latest_commit(repo, tree):
----> 2     for commit in repo.iter_commits(paths=tree.abspath):
     3         return commit
     4
/home/allura/anvil/local/lib/python2.7/site-packages/GitPython-0.3.2.RC1-py2.7.egg/git/objects/base.pyc in abspath(self)
   173
   174                         The returned path will be native to the system and contains '\' on windows. """
--> 175                 return join_path_native(self.repo.working_tree_dir, self.path)
   176
/home/allura/anvil/local/lib/python2.7/site-packages/GitPython-0.3.2.RC1-py2.7.egg/git/repo/base.pyc in working_tree_dir(self)
   179                 :raise AssertionError: If we are a bare repository"""
   180                 if self._working_tree_dir is None:
--> 181                         raise AssertionError( "Repository at %r is bare and does not have a working tree directory" % self.git_dir )
   182                 return self._working_tree_dir
   183

AssertionError: Repository at '/home/allura/scm/git/p/jcltest/code.git' is bare and does not have a working tree directory

Note: The "rev-list" sub-command in git does not care if it is run in a bare repository.

Owner

Byron commented May 14, 2012

Thanks for verifying this issue.
This appears to be a bug in the Repo implementation, which should be easily corrected though.

For some reason I was pretty sure this actually worked, but maybe I always used non-bare repositories in the test-suite as well, which let this issue slip.

Not to pressure, but any sense of an ETA?

I'd be happy to help, and have time to do so, but would need likely an intro to the model/assumption in the current code.

Owner

Byron commented May 17, 2012

Currently, I have no ETAs for git-python, first I would have to free up some time by getting rid of other projects of mine, and I would say, I am working on that at least.

The best introduction you could get is by strolling through the actual code and have a look at the accompanying unit-tests. Generally, every line of code must be run by a unit-test, and ideally it is approached from a few vectors as well.

In this particular case, the cause of the issue is likely to be found in the Repo implementation, the respective test is in test_repo.py I assume.
Good luck !

dbrgn commented Jun 28, 2012

I would be interested in this too.

On 28 Jun 2012, at 15:17, Danilo Bargen wrote:

I would be interested in this too.

FWLIW my company's priorities have changed (yay, startup!) and it is not likely I'll get back to this for a long while yet.

-- JCL

dbrgn commented Jun 29, 2012

@clearclaw OK :) i currently don't have time to get started with GitPython code either, but i've played around with libgit2/pygit2 which is more focused on lowlevel and works with bare repositories too (but is poorly documented).

Byron added this to the v0.3.2 milestone Nov 19, 2014

Owner

Byron commented Nov 19, 2014

The code of IndexFile.add() was altered to only require a working directory when it actually needs one. This should resolve this issue.
Please have a look at the latest version on pypi as well.

Byron closed this Nov 19, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment