libgit2 / pygit2

Python bindings for libgit2
https://www.pygit2.org/
Other
1.62k stars 387 forks source link

'Failed to create commit: current tip is not the first parent' for basic commit #956

Open Zylatis opened 4 years ago

Zylatis commented 4 years ago

I am trying to do something relatively simple which is just add and commit file changes to an existing repository as i would through the command line. The one complicating caveat is i am calling the code to do this from inside a submodule, however everything seems to be okay until the commit part.

In the folder above the code/submodule folder i have another which is the main git repo and which also has a folder of data i wish to monitor and commit changes of.

The code is

data_repo = pygit2.Repository('../')

index = data_repo.index
index.add_all(pathspecs=["tables/"]) # Because of the data repo path we are already in that repo local path so all paths relative to that
index.write()
tree = index.write_tree()
modified_tables = [ x for x in data_repo.status().keys() if 'tables' in x]

author = pygit2.Signature('Alice Author', 'alice@authors.tld')
committer = pygit2.Signature('GitRateWatcher', 'me@internet.com')
msg  = f"GitRateWatcher commit on {datetime.datetime.today().date()} modified tables: {', '.join(modified_tables)}"
oid = data_repo.create_commit('refs/heads/master', author, committer, msg, tree, [] )
data_repo.head.set_target(oid)

However when I try this i get

_pygit2.GitError: failed to create commit: current tip is not the first parent

if i remove the 'refs/heads/master' part it works but it basically does a rebase and wipes all previous stuff which is no good either. What am I missing?

Zylatis commented 4 years ago

Okay I might have answered my own question by changing the 2nd last line to

oid = data_repo.create_commit('HEAD', author, committer, msg, tree, [data_repo.head.target] )

but this was just based on a total hail mary looking at another part of the docs. It would be great if someone could provide an explanation of what's going on here (and i fully appreciate it might just be me not understanding git properly!)