Created
May 12, 2017 18:26
-
-
Save peterwj/54c93b8bf6740c17bea0ecdf60818eaf to your computer and use it in GitHub Desktop.
git pre-commit gist for catching merge conflict markers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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