Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Created August 26, 2021 07:21
Show Gist options
  • Save dmikurube/c779d31983dad9bb280a0d3fb39b5990 to your computer and use it in GitHub Desktop.
Save dmikurube/c779d31983dad9bb280a0d3fb39b5990 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
import sys
def git_author_date(commit):
return subprocess.check_output(['git', 'show', '--format=%aD', '--no-patch', commit]).decode("utf-8").strip()
def git_author_email(commit):
return subprocess.check_output(['git', 'show', '--format=%ae', '--no-patch', commit]).decode("utf-8").strip()
def git_author_name(commit):
return subprocess.check_output(['git', 'show', '--format=%an', '--no-patch', commit]).decode("utf-8").strip()
def git_committer_date(commit):
return subprocess.check_output(['git', 'show', '--format=%cD', '--no-patch', commit]).decode("utf-8").strip()
def git_committer_email(commit):
return subprocess.check_output(['git', 'show', '--format=%ce', '--no-patch', commit]).decode("utf-8").strip()
def git_committer_name(commit):
return subprocess.check_output(['git', 'show', '--format=%cn', '--no-patch', commit]).decode("utf-8").strip()
def git_committer_name(commit):
return subprocess.check_output(['git', 'show', '--format=%cn', '--no-patch', commit]).decode("utf-8").strip()
def git_cherry_pick(commit, author_date, author_email, author_name, committer_date, committer_email, committer_name):
subprocess.check_call(['git', 'cherry-pick', commit], env={
"GIT_AUTHOR_DATE": author_date,
"GIT_AUTHOR_EMAIL": author_email,
"GIT_AUTHOR_NAME": author_name,
"GIT_COMMITTER_DATE": committer_date,
"GIT_COMMITTER_EMAIL": committer_email,
"GIT_COMMITTER_NAME": committer_name,
})
def main(argv):
if len(argv) < 2:
return 1
for commit in argv[1:]:
author_date = git_author_date(commit)
author_email = git_author_email(commit)
author_name = git_author_name(commit)
committer_date = git_committer_date(commit)
committer_email = git_committer_email(commit)
committer_name = git_committer_name(commit)
print("commit {}".format(commit))
print("Author: {} <{}> {}".format(author_name, author_email, author_date))
print("Committer: {} <{}> {}".format(committer_name, committer_email, committer_date))
print("")
git_cherry_pick(commit, author_date, author_email, author_name, committer_date, committer_email, committer_name)
if __name__ == '__main__':
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment