2015-12-01 32 views
2

冲突,我使用执行一个GitPython合并检查合并:使用GitPython

repo.merge_tree(branch1, branch2) 

合并后,我想看看是否有任何合并冲突。我该怎么做?

+0

我已经找到了这个问题的答案,但我有一个很难找到自己的答案。 [所以我发布的问题和答案,我会找到帮助](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/ )。 – Kevin

回答

3

Nota buena:当我在自己的项目中尝试过时,我不太能够得到这个答案。我不确定是否因为我在此答案中提供的信息不正确,或者是因为我的代码中存在其他问题。

无论如何,这个答案中的信息很难找到,我相信它是正确的或非常接近正确的,所以它仍然有用。请注意,当你使用这个建议时会有龙。


合并后,GitPython将工作目录的状态存储在repo.index中。 repo.index包含一个方法index.unmerged_blobs,它可以让你检查每个blob(文件)的状态,这些blob(文件)已经被修改但没有被提交进行提交。 您可以迭代这些blob以查看是否有合并冲突。

每个blob都与从0到3(包括)的状态相关联。状态为0的Blob成功合并。合并后,状态为1,2或3的Blob发生冲突。


准确地说,所述index.unmerged_blobs函数返回文件路径的字典来一个元组列表。每个元组包含一个从0到3的阶段和一个blob。这是如何打破:

  • 字典中的每个键是项目中的其中一个文件的路径。
  • 每个值实际上是修改密钥引用文件的blob列表。这是因为,在一般情况下,可能有多个更改会影响相同的文件。
    • 由值存储的列表中的每个条目都是元组。
      • 元组中的第一个条目是blob的阶段。合并后立即进入0阶段意味着该blob没有合并冲突。 1到3的阶段意味着有冲突。
      • 元组中的第二项是blob本身。如果您选择,您可以对其进行分析以查看blob原始内容的更改。为了在问题中陈述的目的,您可以忽略斑点。

下面是一些代码,以上设备结合起来:

# We'll use this as a flag to determine whether we found any files with conflicts 
found_a_conflict = False 

# This gets the dictionary discussed above 
unmerged_blobs = repo.index.unmerged_blobs() 

# We're really interested in the stage each blob is associated with. 
# So we'll iterate through all of the paths and the entries in each value 
# list, but we won't do anything with most of the values. 
for path in unmerged_blobs: 
    list_of_blobs = unmerged_blobs[path] 
    for (stage, blob) in list_of_blobs: 
    # Now we can check each stage to see whether there were any conflicts 
    if stage != 0: 
     found_a_conflict = true 
+0

我从这个缓存的Google网上论坛讨论中获得了大部分答案:http://webcache.googleusercontent.com/search?q=cache:WNqVZWPhBvAJ:https://groups.google。com/d/topic/git-python/SefBpTUNXVY +&cd = 8&hl = en&ct = clnk&gl = us – Kevin

+0

以及GitPython的API参考:https://gitpython.readthedocs.org/en/stable/reference.html#git.index .base.IndexFile.unmerged_blobs – Kevin