Repository: davidfokkema/git-rewrite-author Branch: master Commit: c4dbb54ddc6b Files: 4 Total size: 5.9 KB Directory structure: gitextract_zx6uwtv2/ ├── .gitignore ├── README.rst ├── git-rewrite-author └── setup.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ git_rewrite_author.egg-info/ build/ dist/ ================================================ FILE: README.rst ================================================ Rewrite author/committer history of a git repository ==================================================== Have you ever accidentally committed to a git repository with a broken user config? No? But your co-workers have? So, you're stuck with commits like this:: Author: root Hotfix on the production server. This was urgent! Nasty. Or:: Author: John Doe Fixed bug #1. Committed on my laptop. Would it be nice to rewrite history? And take care of committers, as well as of authors? Without all the hassle? Now, you can! Usage:: $ git rewrite-author -w "John Doe " "John Doe " Then, to push your changes to the default remote:: $ git push --force Not using --force may duplicate the commits on origin, not replace them, so be careful with that. You're not sure which authors/committers are hidden in your repository? What about:: $ git rewrite-author -l Tags are rewritten automagically, too! After you've checked everything is okay, you may wish to remove the original refs backed up by git --filter-branch:: $ git for-each-ref --format="%(refname)" refs/original/ | xargs -r -n 1 git update-ref -d Enjoy! Installation ------------ Clone or download this repository and run:: $ python setup.py install ================================================ FILE: git-rewrite-author ================================================ #!/usr/bin/env python """Rewrite author/committer history of a git repository Have you ever accidentally committed to a git repository with a broken user config? No? But your co-workers have? So, you're stuck with commits like this:: Author: root Hotfix on the production server. This was urgent! Nasty. Or:: Author: John Doe Fixed bug #1. Committed on my laptop. Would it be nice to rewrite history? And take care of committers, as well as of authors? Without all the hassle? Now, you can! Usage:: $ git rewrite-author -w "John Doe " "John Doe " Then, to push your changes to the default remote:: $ git push --force Not using --force may duplicate the commits on origin, not replace them, so be careful with that. You're not sure which authors/committers are hidden in your repository? What about:: $ git rewrite-author -l Tags are rewritten automagically, too! Enjoy! Installation ------------ Clone or download this repository and run:: $ python setup.py install """ import argparse import re import subprocess import textwrap description = "Rewrite author/committer in git history" epilog = """ Example: $ git-rewrite-author -w "Name " "Full Name " """ git_rewrite_command = """git filter-branch --env-filter ' if [ "$GIT_AUTHOR_NAME" = "%(old_name)s" -a "$GIT_AUTHOR_EMAIL" = "%(old_email)s" ]; then export GIT_AUTHOR_NAME="%(new_name)s"; export GIT_AUTHOR_EMAIL="%(new_email)s"; fi; ' --tag-name-filter cat -f -- --all""" git_log_command = "git log --pretty=full" def parse_args(): """Parse command-line arguments""" parser = argparse.ArgumentParser(description=description, epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('-l', '--list', action='store_true', help="List all authors and committers") parser.add_argument('-w', '--rewrite', nargs=2, metavar=('old', 'new'), type=str, help="Rewrite authors and committers") return parser.parse_args() def main(args): """Rewrite history using args""" if args.list: list_git_authors() elif args.rewrite: old_name, old_email = parse_author_arg(args.rewrite[0]) new_name, new_email = parse_author_arg(args.rewrite[1]) rewrite_git_author(old_name, old_email, new_name, new_email) rewrite_git_committer(old_name, old_email, new_name, new_email) else: print("Doing nothing. Invoke with -h for help.") def parse_author_arg(arg): """Parse name/email argument""" name, email = re.match("(.+)\s<(.*)>", arg).groups() return name, email def rewrite_git_author(old_name, old_email, new_name, new_email): """Rewrite author history in git""" command = git_rewrite_command % \ { 'old_name': old_name, 'old_email': old_email, 'new_name': new_name, 'new_email': new_email} subprocess.call(command, shell=True) def rewrite_git_committer(old_name, old_email, new_name, new_email): """Rewrite committer history in git""" command = git_rewrite_command.replace('AUTHOR', 'COMMITTER') % \ { 'old_name': old_name, 'old_email': old_email, 'new_name': new_name, 'new_email': new_email} subprocess.call(command, shell=True) def list_git_authors(): """List authors and committers""" output = subprocess.check_output(git_log_command.split(' ')) matches = re.findall(b'(Author|Commit): (.*)\n', output) names = [u[1].decode('utf-8') for u in matches] unique_names = sorted(set(names)) print("The following authors and committers have contributed:\n") for name in unique_names: print(name) if __name__ == '__main__': args = parse_args() main(args) ================================================ FILE: setup.py ================================================ #!/usr/bin/env python from setuptools import setup setup(name='git rewrite author', version='1.0', description='Rewrite author/committer history of a git repository', author='David Fokkema', author_email='davidfokkema@icloud.com', url='https://github.com/davidfokkema/git-rewrite-author', classifiers=['Intended Audience :: Developers', 'Environment :: Console', 'Programming Language :: Python :: 2.7', 'Topic :: Software Development :: Version Control', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)'], scripts=['git-rewrite-author'])