Skip to content

Instantly share code, notes, and snippets.

@peterwj
Created May 12, 2017 18:26
Show Gist options
  • Save peterwj/54c93b8bf6740c17bea0ecdf60818eaf to your computer and use it in GitHub Desktop.
Save peterwj/54c93b8bf6740c17bea0ecdf60818eaf to your computer and use it in GitHub Desktop.
git pre-commit gist for catching merge conflict markers
#!/usr/bin/env python3
import subprocess
import sys
def get_diff():
base_commit = subprocess.run(['git', 'rev-parse', '--verify', 'HEAD'], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
return subprocess.run(['git', 'diff', '--cached', base_commit], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
def check_for_merge_markers(diff):
if (
diff.find("<<<<<<<") > -1
or diff.find(">>>>>>>") > -1
or diff.find("=======") > -1
):
print("Attempting to commit merge markers")
return False
else:
return True
# add more functions of diff above for more checks, then add them to
# this list here. these functions should return True if the diff
# passes.
CHECKS = [
check_for_merge_markers,
]
def main():
diff = get_diff()
passed = all([check(diff) for check in CHECKS])
if passed:
sys.exit(0)
else:
sys.exit(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment