Pygit2: Why does a merge leave a branch unclean?

I am currently running Pygit 0.24.1 (along with libgit 0.24.1), working on a repository where I have two branches (say prodand dev).

Each change is first passed to the branch devand put into the remote repository. For this, I have this piece of code:

repo = Repository('/foo/bar')
repo.checkout('refs/heads/dev')

index = repo.index
index.add('any_file')
index.write()

tree = index.write_tree()
author = Signature('foo', 'foo@bar')
committer = Signature('foo', 'foo@bar')
repo.create_commit('refs/heads/dev', author, committer, 'Just another commit', tree, [repo.head.get_object().hex])

up = UserPass('foo', '***')
rc = RemoteCallbacks(credentials=up)
repo.remotes['origin'].push(['refs/heads/dev'], rc)

This works very well, I see local commit as well as remote commit, and the local repo stays clean:

nothing to commit, working directory inactive

Then I go out into the branch prodand I want to combine the HEAD commit on dev. For this, I use this other piece of code (assuming that I always start checking on a branch dev):

head_commit = repo.head
repo.checkout('refs/heads/prod')
prod_branch_tip = repo.lookup_reference('HEAD').resolve()
prod_branch_tip.set_target(head_commit.target)

rc = RemoteCallbacks(credentials=up)
repo.remotes['origin'].push(['refs/heads/prod'], rc)

repo.checkout('refs/heads/dev')

, , , , . dev.

dev

, : ( "git reset HEAD..." )

: any_file

, . , a git diff . (.. , ). , .

, , , . ?

EDIT. , - FF (Fast-Forward). , FF Pygit2, , .

EDIT 2: @Leon, , git diff , git diff --cached , . , , , , ...

:

  • , "12345" + , "54321"
  • git log , , '54321', git diff --cached :

    @@ -1 +1 @@
    -54321
    +12345
    
+4
1

:

head_commit = repo.head

# This resets the index and the working tree to the old state
# and records that we are in a state corresponding to the commit
# pointed to by refs/heads/prod
repo.checkout('refs/heads/prod')

prod_branch_tip = repo.lookup_reference('HEAD').resolve()

# This changes where refs/heads/prod points. The index and
# the working tree are not updated, but (probably due to a bug in pygit2)
# they are not marked as gone-out-of-sync with refs/heads/prod
prod_branch_tip.set_target(head_commit.target)

rc = RemoteCallbacks(credentials=up)
repo.remotes['origin'].push(['refs/heads/prod'], rc)

# Now we must switch to a state corresponding to refs/heads/dev. It turns
# out that refs/heads/dev points to the same commit as refs/heads/prod.
# But we are already in the (clean) state corresponding to refs/heads/prod!
# Therefore there is no need to update the index and/or the working tree.
# So this simply changes HEAD to refs/heads/prod
repo.checkout('refs/heads/dev')

. :

head_commit = repo.head
prod_branch_tip = repo.lookup_branch('prod')
prod_branch_tip.set_target(head_commit.target)

rc = RemoteCallbacks(credentials=up)
repo.remotes['origin'].push(['refs/heads/prod'], rc)
+3

Source: https://habr.com/ru/post/1648051/


All Articles