如何以编程方式模拟解决与GitPython的合并冲突? [英] How do I programmatically simulate resolving a merge conflict with 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:
- 创建一个本地特性分支 >
- 进行一些更改 li>
- 将我的更改推送到远程
- ...同时有人将他们的更改合并到远程主服务器,所以我现在需要将这些更改合并到我的功能中分支...
- 切换到master(在我的本地签出处)
- 从远程主服务器拉取更新我的本地副本
- 切换到我的本地功能分支
- 尝试合并
- 解决所有冲突
- 将合并推送到远程功能分支
- Create a local feature branch
- Make some changes
- Push my changes to the remote
- ...meanwhile someone else merges their changes into the remote master so I now need to merge those changes into my feature branch...
- Switch to master (on my local checkout)
- Pull from the remote master to update my local copy
- Switch to my local feature branch
- Attempt to merge
- Resolve any conflicts
- 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屋!