Skip to content

Instantly share code, notes, and snippets.

@nicholasbishop
Created February 1, 2020 00:20
Show Gist options
  • Save nicholasbishop/07940e1ae1a69c3336591ad97c7a8ad6 to your computer and use it in GitHub Desktop.
Save nicholasbishop/07940e1ae1a69c3336591ad97c7a8ad6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from pprint import pprint
import subprocess
import whatthepatch
def main():
neverware = 'd92d86ab066bd7ab6f2fccdb16e99ac01f559cf1'
upstream = '24b3f9bd07380cd0bca9a70051d498b706f61e15'
cmd = ('git', 'diff', upstream, neverware)
output = subprocess.run(cmd, capture_output=True, check=True, text=True)
patch = output.stdout
commits = set()
for diff in whatthepatch.parse_patch(patch):
for change in diff.changes:
# Ignore lines that aren't insertions
if change.old is not None or change.new is None:
continue
cmd = ('git', 'blame', '-L{0},{0}'.format(change.new), neverware,
diff.header.new_path)
output = subprocess.run(cmd,
capture_output=True,
check=True,
text=True)
header = output.stdout.split(')')[0]
parts = header.split()
commit_hash = parts[0]
commit_date = parts[-4]
commit_time = parts[-3]
commit_zone = parts[-2]
commit_datetime = '{}T{}{}'.format(commit_date, commit_time, commit_zone)
commits.add((commit_datetime, commit_hash))
# Checkout the detached neverware revision in preparation for
# cherry picking
cmd = ('git', 'checkout', '--detach', upstream)
print(' '.join(cmd))
subprocess.run(cmd, check=True)
# Get the commit hashes from oldest to newest
sorted_commits = [commit for _, commit in sorted(commits)]
# Cherry pick all the commits from oldest to newest
cmd = ['git', 'cherry-pick'] + sorted_commits
print(' '.join(cmd))
subprocess.run(cmd, check=True)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment