Check merge for conflicts using GitPython

不羁的心 提交于 2019-12-05 02:23:11

问题


I am performing a merge using GitPython:

repo.merge_tree(branch1, branch2)

After the merge, I would like to see whether or not there were any merge conflicts. How do I do so?


回答1:


Nota buena: I wasn't quite able to get this answer to work when I tried it in my own project. I'm not sure if it's because the information I present in this answer is incorrect, or if it's because there was another issue in my code.

At any rate, the information in this answer is difficult to find, and I believe it is either correct or very close to correct, so it is still useful. Just be aware that there be dragons when you use this advice.


After the merge, GitPython stores the state of the working directory in repo.index. repo.index includes a method, index.unmerged_blobs, that lets you check the status of every blob (file) that has been modified but not staged for commit. You can iterate over these blobs to see if any have merge conflicts.

Each blob is associated with a status from 0 to 3 (inclusive). Blobs with status 0 were merged successfully. Blobs with status 1, 2, or 3 have conflicts after your merge.


To be precise, the index.unmerged_blobs function returns a dictionary of filepaths to a list of tuples. Each tuple contains a stage from 0 to 3 and a blob. Here's how that breaks down:

  • Each key in the dictionary is a path to one of the files in your project.
  • Each value is effectively a list of blobs that modifies the file referenced by the key. This is because, in the general case, it's possible to have multiple changes staged that affect the same file.
    • Each entry in the list stored by the value is a tuple.
      • The first entry in the tuple is the stage of the blob. Immediately after a merge, a stage of 0 means that the blob has no merge conflicts. A stage of 1 to 3 means that there were conflicts.
      • The second entry in the tuple is the blob itself. You can, if you choose, analyze it to see changes at the blob's original contents. For the purpose stated in the question, you can ignore the blobs.

Here's some code that ties it all together:

# 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


来源:https://stackoverflow.com/questions/34030400/check-merge-for-conflicts-using-gitpython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!