Created
October 25, 2015 22:32
-
-
Save kzar/69e1ed7795d3ca3eb513 to your computer and use it in GitHub Desktop.
Python script to help find which Git revision a diff file will successfully apply onto
This file contains hidden or 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 python | |
import os | |
import subprocess | |
import sys | |
devnull = open(os.devnull, "w") | |
def patch_applies(diff_file): | |
return not subprocess.call(["git", "apply", "--check", "--inaccurate-eof"] + | |
[diff_file], stderr=devnull) | |
def current_rev(): | |
return subprocess.check_output(["git", "rev-parse", "HEAD"]).strip() | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
sys.exit("Usage: %s path/to/your.diff" % sys.argv[0]) | |
diff_file = sys.argv[1] | |
initial_rev = current_rev() | |
if patch_applies(diff_file): | |
print("HEAD") | |
else: | |
while subprocess.call(["git", "checkout", "HEAD~"], stderr=devnull) == 0: | |
if patch_applies(diff_file): | |
print(current_rev()) | |
break | |
else: | |
print("Failed to apply diff on any ancestor of rev %s" % initial_rev) | |
subprocess.call(["git", "checkout", initial_rev]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment