我正在尝试使用 pygit2 实现一些“瓷器”命令。似乎我在实现拉动时遇到了一些障碍。特别是最简单的拉动情况,快进。

设置:

我有两个 git 仓库。一个“远程”和一个“本地”。我在远程仓库上进行一次提交,然后使用 pygit2 的 clone_repository() 创建本地仓库。我在 Remote 上进行了后续提交,然后尝试运行下面概述的 pull() 函数。

我的实现:

def pull(repo, remote_name='origin'):
    for remote in repo.remotes:
        if remote.name == remote_name:
            remote.fetch()
            remote_master_id = repo.lookup_reference('refs/remotes/origin/master').target
            merge_result, _ = repo.merge_analysis(remote_master_id)
            # Up to date, do nothing
            if merge_result & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE:
                return
            # We can just fastforward
            elif merge_result & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD:
                print repo.head.target
                print repo.status()
                master_ref = repo.lookup_reference('refs/heads/master')
                master_ref.set_target(remote_master_id)
                repo.head.set_target(master_ref)
                repo.checkout_head()
                print repo.status()
            elif merge_result & pygit2.GIT_MERGE_ANALYSIS_NORMAL:
                repo.merge(remote_master_id)
                print repo.index.conflicts

                assert repo.index.conflicts is None, 'Conflicts, ahhhh!'
                user = repo.default_signature
                tree = repo.index.write_tree()
                commit = repo.create_commit('HEAD',
                                            user,
                                            user,
                                            'Merge!',
                                            tree,
                                            [repo.head.target, remote_master_id])
                repo.state_cleanup()
            else:
                raise AssertionError('Unknown merge analysis result')

Full Source

在我的代码的快进位执行后,我的索引不再干净,我不知道为什么。查看git-log,看起来成功了。我的头和主分支现在指向远程仓库上的最新提交。但是,为什么在运行 remote_repo_test.txt 的过程中修改了 set_target()

输出:

拉打印语句:
abfe58ce5098e106a14263df725247bc1f4b22d2
{}
{'remote_repo_test.txt': 2}

Git日志:
* commit b1842f03efe959e93ebad197f36d50ee658e71a4
| Author: Michael Boselowitz <xxx>
| Date:   Fri Jan 2 17:21:45 2015 -0500
|
|     Version 2 of test.txt on remote_repo
|
| diff --git a/remote_repo_test.txt b/remote_repo_test.txt
| index a1665f0..13f7f3f 100644
| --- a/remote_repo_test.txt
| +++ b/remote_repo_test.txt
| @@ -1,2 +1,4 @@
|  Version 1.
|
| +Version 2.
| +
|
* commit abfe58ce5098e106a14263df725247bc1f4b22d2
  Author: Michael Boselowitz <xxx>
  Date:   Fri Jan 2 17:21:45 2015 -0500

      Version 1 of test.txt on remote_repo

  diff --git a/remote_repo_test.txt b/remote_repo_test.txt
  new file mode 100644
  index 0000000..a1665f0
  --- /dev/null
  +++ b/remote_repo_test.txt
  @@ -0,0 +1,2 @@
  +Version 1.
  +

git状态:
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   remote_repo_test.txt

remote_repo_test.txt 内容:
Version 1.

相关问题没有答案:
  • pulling and integrating remote changes with pygit2
  • Implementing 'git pull' with libgit2?
  • Performing a “fast-forward” merge with Rugged

  • 想法?

    最佳答案

    第一个解决方案(不推荐):

    你应该 而不是 这样做。这样可能会丢失工作。但是,它可以作为一种hacky 解决方案。

    master_ref = repo.lookup_reference('refs/heads/master')
    master_ref.set_target(remote_master_id)
    # Terrible hack to fix set_target() screwing with the index
    repo.reset(master_ref.target, pygit2.GIT_RESET_HARD)
    

    Full source

    第二个解决方案:

    这个似乎很有希望。经过多次反复试验,我可能已经找到了解决方案。如果在 之前 checkout 要定位 的树对象,则更新其工作的引用。索引很干净,就像您在运行 git pullgit merge 时所期望的那样
    repo.checkout_tree(repo.get(remote_master_id))
    master_ref = repo.lookup_reference('refs/heads/master')
    master_ref.set_target(remote_master_id)
    repo.head.set_target(remote_master_id)
    

    Full source

    关于python - 使用 Pygit2 实现拉取,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27749418/

    10-10 18:19