Skip to content

Instantly share code, notes, and snippets.

@kzar
Created October 25, 2015 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kzar/69e1ed7795d3ca3eb513 to your computer and use it in GitHub Desktop.
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
#!/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