Skip to content

Repository.cherrypick() remains in cherry-picking state #516

Closed
@rmoehn

Description

@rmoehn

The following somewhat lengthy code (I was too lazy to reduce boilerplate for this example) creates two branches and then performs a cherry-pick using Repository.cherrypick(). Since there are no conflicts, I would expect the repo to end up in a clean state afterwards. However, when I run git status, it shows that I'm still in cherry-picking mode and I have to run git cherry-pick --abort to change back to normal. This leaves me in the state where I want to be, i. e. with the cherry picked.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
import pygit2
import os
import codecs

ident = pygit2.Signature('Euclid', '[email protected]')

def add_and_write(repo, fpath):
  repo.index.add(fpath)
  repo.index.write()
  return repo.index.write_tree()

os.mkdir("test-repo")
os.chdir("test-repo")
repo = pygit2.init_repository(".")

# Create initial commit
with codecs.open("bla.txt", 'w', 'utf-8') as f:
    f.write("A line from first commit on master in bla.txt.\n")
c1_oid = repo.create_commit("HEAD", ident, ident, "Add a first line to bla.txt.",
                            add_and_write(repo, "bla.txt"), [])
c1 = repo.get(c1_oid)

# Create necessary branches
repo.create_branch("cherry", c1)
repo.create_branch("basket", c1)

# Create cherry commit
repo.checkout("refs/heads/cherry")
with codecs.open("bla.txt", 'a', 'utf-8') as f:
    f.write("A second line from commit on cherry in bla.txt.\n")
cherry_oid = repo.create_commit("HEAD", ident, ident,
                                "Add a second line to bla.txt",
                                add_and_write(repo, "bla.txt"), [c1_oid])
cherry = repo.get(cherry_oid)

# Create basket commit
repo.checkout("refs/heads/basket")
with codecs.open("blu.txt", 'w', 'utf-8') as f:
    f.write("A line from commit on basket in blu.txt.\n")
basket_oid = repo.create_commit("HEAD", ident, ident,
                                "Add a line to blu.txt",
                                add_and_write(repo, "blu.txt"), [c1_oid])

# Get the current branches
basket_b = repo.lookup_branch("basket")
cherry_b = repo.lookup_branch("cherry")

# Pick cherry onto basket
repo.cherrypick(cherry_b.target)

if repo.index.conflicts is None:
    repo.index.write()
    tree_id = repo.index.write_tree()

    committer = pygit2.Signature('Archimedes', '[email protected]')

    repo.create_commit(basket_b.name, cherry.author, committer,
                       cherry.message, tree_id, [basket_b.target])
    del basket_b # outdated, prevent from accidentally using it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions