4

I'm stuck trying to implement a git rebase with pygit2.

Assuming this repo history, how to rebase topic on master using pygit2 ? (ie, the equivalent of git rebase master topic):

      A---B---C topic
     /
D---E---F---G master

According to pygit2 documentation, merge_trees can be used to this end. So far, I got:

import pygit2 as git

repo = git.Repository(".")

master = repo.lookup_branch("master")
topic = repo.lookup_branch("topic")

base = repo.merge_base(master.target, topic.target)

master_tree = repo.get(master.target).tree
topic_tree = repo.get(topic.target).tree
base_tree = repo.get(base).tree

index = repo.merge_trees(base_tree,topic,master)
tree_id = index.write_tree(repo)

sig = git.Signature('FooBar', 'foo@bar.org')

# this is not going to work -> several commits need to be re-created
repo.create_commit(topic.name,sig,sig,"msg?",tree_id,[master.target])

This fails with the error GitError: failed to create commit: current tip is not the first parent.

Other SO questions (here and here) discuss fast-forward merges but not proper rebase.

Community
  • 1
  • 1
skadge
  • 121
  • 7

0 Answers0