Skip to content

Instantly share code, notes, and snippets.

@depp
Created April 23, 2019 19:11
Show Gist options
  • Save depp/f1838cf4559f9cde74b9d3052b8abbb0 to your computer and use it in GitHub Desktop.
Save depp/f1838cf4559f9cde74b9d3052b8abbb0 to your computer and use it in GitHub Desktop.
Find Git merge conflicts
# https://stackoverflow.com/questions/55817243/does-git-keep-a-record-of-past-merge-conflicts
import subprocess
import sys
def list_merges():
proc = subprocess.run(
['git', 'rev-list', '--parents', '--min-parents=2', '--all'],
stdout=subprocess.PIPE,
check=True,
encoding='ASCII',
)
merges = []
for line in proc.stdout.splitlines():
fields = line.split()
merges.append((fields[0], fields[1:]))
print('Found {} merges'.format(len(merges)), file=sys.stderr)
return merges
def analyze_merge(commit, parents):
subprocess.run(
['git', 'checkout', '--quiet', '--detach', parents[0]],
check=True,
)
out = subprocess.run(
['git', 'merge', '--quiet', '--no-commit', '--no-ff', *parents[1:]],
stdout=subprocess.DEVNULL,
)
if out.returncode:
# Merge conflicts
print("Merge conflict: commit={}".format(commit))
out = subprocess.run(
['git', 'status', '--porcelain'],
stdout=subprocess.PIPE,
check=True,
encoding='ASCII',
)
for line in out.stdout.splitlines():
if line.startswith("UU"):
print(" File: {}".format(line[3:]))
subprocess.run(
['git', 'merge', '--abort'],
check=True
)
def main():
for commit, parents in list_merges():
analyze_merge(commit, parents)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment