4

我目前正在运行 Pygit 0.24.1(以及 libgit 0.24.1),在我有两个分支(比如proddev)的存储库上工作。

每个更改首先提交到dev分支并推送到远程存储库。为此,我有这段代码:

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)

这工作得很好,我可以看到本地提交和远程提交,并且本地仓库保持干净:

没有什么可提交的,工作目录干净

接下来,我签出到prod分支,我想将 HEAD 提交合并到dev. 为此,我使用了另一段代码(假设我总是开始签出到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')

我实际上可以看到分支在本地和远程都被合并,但是在这段代码运行之后,提交的文件总是在 branch 上保持修改状态dev

在分支开发

要提交的更改:(使用“git reset HEAD ...”取消暂存)

修改:any_file

不过,我完全确定没有人在修改该文件。实际上, agit diff什么也没显示。此问题仅发生在已提交的文件(即,之前至少提交过一次的文件)中。当文件是新的时,这可以完美地工作并使文件处于干净状态。

我确定我遗漏了一些细节,但我无法找出它是什么。为什么文件保留为已修改

编辑:澄清一下,我的目标是进行 FF(快进)合并。我知道 Pygit2 文档中有一些关于进行非 FF 合并的文档,但我更喜欢第一种方法,因为它通过分支保持提交哈希。

编辑 2:@Leon 发表评论后,我仔细检查,确实git diff没有显示输出,同时git diff --cached显示文件在提交之前的内容。这很奇怪,因为我可以看到在本地和远程存储库上成功提交了更改,但看起来之后文件再次更改为以前的内容......

一个例子:

  1. 提交内容为“12345”的文件+推送后,我将该字符串替换为“54321”
  2. 我运行上面的代码
  3. git log显示正确提交的文件,在远程仓库中我看到内容为“54321”的文件,而在本地git diff --cached显示:

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

1 回答 1

3

我将解释观察到的问题如下:

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)
于 2016-07-18T17:32:29.937 回答