如何以编程方式模拟解决与 GitPython 的合并冲突?
How do I programmatically simulate resolving a merge conflict with 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
我还需要将其标记为已解决吗?
编辑 - 关于我尝试自动化的内容的更多背景知识
所以为了清楚起见,我想让它尽可能容易地合并已更新到远程功能分支的远程主分支中的更改,解决任何合并冲突。
据我所知,正确的做法是:
- 创建本地功能分支
- 进行一些更改
- 将我的更改推送到远程
- ...同时其他人将他们的更改合并到远程主机中,所以我现在需要将这些更改合并到我的功能分支中...
- 切换到 master(在我的本地结帐处)
- 从远程主机拉取更新我的本地副本
- 切换到我的本地功能分支
- 尝试合并
- 解决任何冲突
- 将合并推送到远程功能分支
我现在已经在一个 Python 脚本中得到了大部分内容,但是这个问题涉及的问题是模拟上述步骤中的第 9 步。
我放弃了尝试使用 GitPython 的内部结构,因为尝试从单元测试中分离出相关命令被证明是相当棘手的。
最后,这成功了:
g = git.Git('my/repo')
g.execute(["git","add","conflicted_file.txt"])
g.execute(["git","commit","-m", "Conflict resolved"])
根据我最近在
如果我使用 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
我还需要将其标记为已解决吗?
编辑 - 关于我尝试自动化的内容的更多背景知识
所以为了清楚起见,我想让它尽可能容易地合并已更新到远程功能分支的远程主分支中的更改,解决任何合并冲突。
据我所知,正确的做法是:
- 创建本地功能分支
- 进行一些更改
- 将我的更改推送到远程
- ...同时其他人将他们的更改合并到远程主机中,所以我现在需要将这些更改合并到我的功能分支中...
- 切换到 master(在我的本地结帐处)
- 从远程主机拉取更新我的本地副本
- 切换到我的本地功能分支
- 尝试合并
- 解决任何冲突
- 将合并推送到远程功能分支
我现在已经在一个 Python 脚本中得到了大部分内容,但是这个问题涉及的问题是模拟上述步骤中的第 9 步。
我放弃了尝试使用 GitPython 的内部结构,因为尝试从单元测试中分离出相关命令被证明是相当棘手的。
最后,这成功了:
g = git.Git('my/repo')
g.execute(["git","add","conflicted_file.txt"])
g.execute(["git","commit","-m", "Conflict resolved"])