如何以编程方式模拟解决与GitPython的合并冲突? [英] How do I programmatically simulate resolving a merge conflict with GitPython?

查看:224
本文介绍了如何以编程方式模拟解决与GitPython的合并冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我最近关于使用GitPython合并分支的问题,我正在尝试单元测试解决方案。要做到这一点,我需要模拟一个用户打开他们的合并工具,解决冲突并提交结果。

As per my recent question on merging branches using GitPython, I'm trying to unit test the solution there. To do so I need to simulate a user opening their merge tool, resolving the conflicts and committing the result.

如果我使用CLI手动执行此操作, :

If I do this manually using the CLI it all seems to work:

echo 'Something to resolve the conflict' > conflicted_file.txt
git add conflicted_file.txt
git commit -m "Conflict resolved"



I've tried to simulate that same process using GitPython:

filename = 'conflicted_file.txt"
with open(filename, 'w') as f:
    f.write('Some combined content which resolves the merge issue')
# repo is a git.Repo instance
repo.index.add([filename])
repo.index.commit("Simulating user resolving a conflict"+filename)

..但这只是给我一个例外:

..but this just raises an exception for me:

Traceback (most recent call last):
  File "C:\Projects\grit\test_Gritter.py", line 240, in test_MergeConflicts
    repo.index.commit("Simulating user resolving a conflict"+filename)
  File "C:\Python34\lib\site-packages\git\index\base.py", line 934, in commit
    tree = self.write_tree()
  File "C:\Python34\lib\site-packages\git\index\base.py", line 531, in write_tree
    binsha, tree_items = write_tree_from_cache(entries, mdb, slice(0, len(entries)))
  File "C:\Python34\lib\site-packages\git\index\fun.py", line 234, in write_tree_from_cache
    raise UnmergedEntriesError(entry)
git.exc.UnmergedEntriesError: 100644 fd05580faebf11aee13944da595112eced471664 2 conflicted_file.txt

有什么我需要将其标记为已解决?

Is there something else I need to mark it as resolved?

编辑 - 关于我想要自动化的更多背景

所以为了清楚起见,我希望尽可能简化合并已更新到远程功能分支的远程主分支中的更改,解决所有合并冲突。

So just to be clear I want to make it as easy as possible to merge changes in a remote master branch which has been updated into a remote feature branch, resolving any merge conflicts.

据我所知,正确的做法是:

As far as I'm aware, the correct way to do that is to:


  1. 创建一个本地特性分支
  2. >
  3. 进行一些更改 li>
  4. 将我的更改推送到远程

  5. ...同时有人将他们的更改合并到远程主服务器,所以我现在需要将这些更改合并到我的功能中分支...

  6. 切换到master(在我的本地签出处)

  7. 从远程主服务器拉取更新我的本地副本

  8. 切换到我的本地功能分支

  9. 尝试合并

  10. 解决所有冲突
  11. 将合并推送到远程功能分支

  1. Create a local feature branch
  2. Make some changes
  3. Push my changes to the remote
  4. ...meanwhile someone else merges their changes into the remote master so I now need to merge those changes into my feature branch...
  5. Switch to master (on my local checkout)
  6. Pull from the remote master to update my local copy
  7. Switch to my local feature branch
  8. Attempt to merge
  9. Resolve any conflicts
  10. Push the merge to the remote feature branch

我现在已经在一个Python脚本中获得了大部分内容,但是这个问题存在问题所涉及的是在模拟上述步骤中的第9步。

I've got most of this in a single Python script now but the problem this question pertains to is in simulating step 9 in the steps above.

推荐答案

我放弃了尝试使用GitPython内部的理由试图从单元测试中分离出相关的命令是相当棘手的。

I gave up trying to use the internals of GitPython on the grounds that trying to disentangle the relevent commands from the unit tests was proving rather tricky.

最后,这个工作:

In the end, this worked:

g = git.Git('my/repo')
g.execute(["git","add","conflicted_file.txt"])
g.execute(["git","commit","-m", "Conflict resolved"])

这篇关于如何以编程方式模拟解决与GitPython的合并冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆