根据我最近关于merging branches using GitPython的问题,我正在尝试对那里的解决方案进行单元测试。为此,我需要模拟用户打开他们的合并工具,解决冲突并提交结果。
如果我使用CLI手动执行此操作,它似乎都可以工作:
echo 'Something to resolve the conflict' > conflicted_file.txt
git add conflicted_file.txt
git commit -m "Conflict resolved"
我尝试使用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)
..但这只是为我提出了一个例外:
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
我需要将其标记为已解决的其他内容吗?
编辑 - 有关我正在尝试自动化的更多背景
所以,为了清楚起见,我希望尽可能简单地将已更新的远程主分支中的更改合并到远程功能分支中,从而解决任何合并冲突。
据我所知,正确的做法是:
我现在已经在一个Python脚本中获得了大部分内容,但这个问题所涉及的问题是模拟上述步骤中的步骤9。
答案 0 :(得分:0)
我放弃尝试使用GitPython的内部,理由是试图从单元测试中解开相关命令是相当棘手的。
最后,这有效:
g = git.Git('my/repo')
g.execute(["git","add","conflicted_file.txt"])
g.execute(["git","commit","-m", "Conflict resolved"])