Skip to content

Instantly share code, notes, and snippets.

/fwd_lines.py Secret

Created April 20, 2017 06:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d400a67f21f10880294ea0950e2ced15 to your computer and use it in GitHub Desktop.
Save anonymous/d400a67f21f10880294ea0950e2ced15 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import difflib
import subprocess
commit = sys.argv[1]
file = sys.argv[2]
line = int(sys.argv[3]) - 1
prev = subprocess.check_output(["git", "show", "%s:%s" % (commit, file)]).decode('utf-8').split("\n")
cur = open(file).read().split("\n")
sm = difflib.SequenceMatcher(None, prev, cur)
for tag, i1, i2, j1, j2 in sm.get_opcodes():
if i1 <= line < i2:
if tag != "equal":
print("line was deleted")
sys.exit(1)
corr_line = line - i1 + j1
break
prev[line] = ">>>" + prev[line] + "<<<"
cur[corr_line] = ">>>" + cur[corr_line] + "<<<"
for i in range(len(prev)):
prev[i] = str(i+1) + ": " + prev[i]
for i in range(len(cur)):
cur[i] = str(i+1) + ": " + cur[i]
print("PREV")
print("==================================")
print("\n".join(prev))
print("")
print("CUR, found in", corr_line + 1)
print("==================================")
print("\n".join(cur))
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment