Skip to content

create_submodule for a submodule repo without initial commit leaves main repo object in a bad state #616

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

Open
idij opened this issue Mar 28, 2017 · 1 comment

Comments

@idij
Copy link

idij commented Mar 28, 2017

The following script raises an exception iterating the submodules of the repo after a failed creation of a submodule. I'd expect after catching the exception on the create_submodule for sub2, that I could still iterate the successfully added submodule, sub1.

This is a toy example obviously, but it illustrates the problem that a fail in the create_submodule causes.

import git

def list_submodules (repo):
    print "List of submodules"
    for submodule in repo.submodules:
        print "Submodule: " + str(submodule)

main = git.Repo.init ('main')
sub1 = git.Repo.init ('sub1')
sub2 = git.Repo.init ('sub2')

main.index.commit ('Initial Commit to main')
sub1.index.commit ('Initial Commit to sub1')

list_submodules (main)

for m in ['sub1', 'sub2']:
    try:
        main.create_submodule (name='modules/' + m, path='modules/' + m, url=m)
        main.index.add (['.gitmodules'])
        print "Added submodule: " + m
    except:
        print "Fail for: " + m

list_submodules (main)

This is the output:

○ → rm -rf main sub1 sub2 && python sm.py
List of submodules
Added submodule: sub1
Fail for: sub2
List of submodules
Traceback (most recent call last):
  File "sm.py", line 24, in <module>
    list_submodules (main)
  File "sm.py", line 5, in list_submodules
    for submodule in repo.submodules:
  File "C:\Python27\lib\site-packages\git\repo\base.py", line 290, in submodules
    return Submodule.list_items(self)
  File "C:\Python27\lib\site-packages\git\util.py", line 932, in list_items
    out_list.extend(cls.iter_items(repo, *args, **kwargs))
  File "C:\Python27\lib\site-packages\git\objects\submodule\base.py", line 1189, in iter_items
    "Gitmodule path %r did not exist in revision of parent commit %s" % (p, parent_commit))
git.exc.InvalidGitRepositoryError: Gitmodule path u'modules/sub2' did not exist in revision of parent commit HEAD

Is there a GitPython equivalent to a git checkout . , which would discard the non-index changes, and might function as a workaround?

Ideally the create_submodule would clean up after itself though.

@Byron
Copy link
Member

Byron commented Apr 9, 2017

Thanks for providing the details to help me reproducing the issue. Implementing atomicity for all GitPython commands would be great, even though it appears out of scope given that GitPython is in maintenance mode.

In an attempt to answer your question: GitPython itself allows to checkout files from the index itself which behaves more like plumbing. Some high-level commands are most easily accessed by using git itself, for example like repo.git.checkout('.').

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

No branches or pull requests

2 participants