libgit2 / libgit2/pygit2

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

Open
#956 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.7k
Forks
408
Avg merge
2d 57m
Merged PRs (30d)
7

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the shown data_repo.create_commit call and the refs/heads/master target, then inspect how its parent list relates to the repository's current tip. Reproduce the submodule and parent-repository layout from the issue and verify that a new commit preserves the existing history rather than rebasing it. Done means the commit succeeds while retaining the prior branch history.

Written by the indexing model from the issue text.

Assessment

Tech stack
git, python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.