Skip to content

Assertion on creating or adding files in bare repositories #59

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

Closed
clearclaw opened this issue May 9, 2012 · 12 comments
Closed

Assertion on creating or adding files in bare repositories #59

clearclaw opened this issue May 9, 2012 · 12 comments
Milestone

Comments

@clearclaw
Copy link

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

@Byron
Copy link
Member

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

@clearclaw
Copy link
Author

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.

@Byron
Copy link
Member

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.

@clearclaw
Copy link
Author

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.

@Byron
Copy link
Member

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
Copy link

dbrgn commented Jun 28, 2012

I would be interested in this too.

@clearclaw
Copy link
Author

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
Copy link

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 Byron added this to the v0.3.2 milestone Nov 19, 2014
@Byron
Copy link
Member

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 Byron closed this as completed Nov 19, 2014
@flying-sheep
Copy link

flying-sheep commented Jul 30, 2020

Would be cool to have a convenience method for this, like:

class Repo(...):
    ...
    def add_blob(self, path: PathLike, data: bytes, mode: int = 0o100644):
        istream = IStream('blob', len(data), io.BytesIO(data))
        repo.odb.store(istream)
        return git.Blob(repo, istream.binsha, mode, str(path))

Also: Is git.IndexEntry.from_blob necessary for index.add? Because index.add says it also accepts Blobs directly.

@Byron
Copy link
Member

Byron commented Aug 12, 2020

@flying-sheep I like the idea, but wonder if the suggested implementation would be what most people expect when adding something to the repository. After all, without updating the index it's not obviously there.
However, generally GitPython would benefit from some higher-level functions that just do the right thing, and if you have ideas you are welcome to contribute them or discuss them in another ticket.

@flying-sheep
Copy link

Well, adding a index: Union[IndexFile, bool] = False to that method would also be possible, and point people in the right direction.

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

No branches or pull requests

4 participants