2016-04-25 82 views
0

按我最近的問題上merging branches using GitPython,我試圖單元測試解決方案出現。要做到這一點,我需要模擬用戶打開他們的合併工具,解決衝突和提交的結果。如何以編程方式模擬解決與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 

有沒有別的東西,我需要將其標記爲已解決?

編輯 - 什麼我試圖自動化

一些背景,因此只是要清楚,我想讓它儘可能容易合併已經更新到遠程主分支的變化遠程功能分支,解決任何合併衝突。

據我所知,做正確的方法是:

  1. 創建本地特性分支
  2. 進行一些更改
  3. 把我的更改遠程
  4. ...同時別人合併他們的更改到遠程主機,以便我現在需要將這些更改合併到我的特性分支...
  5. 切換到主站(在我的本地結賬)
  6. 從遠程主
  7. 拉來更新我的本地副本
  8. 切換到我的本地特性分支
  9. 試圖合併
  10. 解決任何衝突
  11. 推合併到遠程特性分支

我現在已經在一個Python腳本中獲得了大部分內容,但是這個問題所涉及的問題是在上面的步驟中模擬步驟9。

+0

它也過於複雜,Git有更多的6種不同的方式來合併和解決衝突....你確定你想這樣做:-) – CodeWizard

+0

爲了澄清,我不想在腳本中處理衝突解決方案,用戶必須這樣做,但是需要從遠程主服務器中提取更改,將這些更改合併到我的功能分支中,然後暫停解決衝突,然後推送結果進入遠程分支...是我一直在做一遍又一遍,所以我一直在開發這個腳本,以減少所有的輸入:) –

+0

什麼錯:'git pull ... --rebase'?它會自動下載併合並你的代碼。它會停止,如果你有衝突 – CodeWizard

回答

0

我放棄了試圖使用GitPython的內部機制,理由是試圖從單元測試中解開相關的命令是相當棘手的。

最終,這個工作:

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