Created
June 23, 2020 16:13
-
-
Save gabyx/477d221381b577a814957cc5ff7d5ffc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# based on https://github.com/newren/git-filter-repo/blob/main/contrib/filter-repo-demos/insert-beginning | |
""" | |
This is a simple program that will insert some regular file into the root | |
commit(s) of history, e.g. adding a file named LICENSE or COPYING to the | |
first commit. It also rewrites commit hashes in commit messages to update | |
them based on these changes. | |
""" | |
import argparse | |
import sys | |
import os | |
import git_filter_repo as fr | |
from .commonFunctions import get | |
def main(): | |
parser = argparse.ArgumentParser(description='Add a file to every commit in the history') | |
parser.add_argument( | |
'-f', | |
'--file', | |
required=True, | |
type=os.fsencode, | |
help=("Relative-path to file whose contents should be added to every commit") | |
) | |
parser.add_argument( | |
'-d', '--destPath', required=True, type=os.fsencode, help=("Relative-path in the repo for the file") | |
) | |
parser.add_argument('-r', '--repoDir', required=True, help=("Path to the git repository")) | |
args = parser.parse_args() | |
os.chdir(args.repoDir) | |
fhash = get(['git', 'hash-object', '-w', args.file], raw=True).strip() | |
fmode = b'100755' if os.access(args.file, os.X_OK) else b'100644' | |
# FIXME: I've assumed the file wasn't a directory or symlink... | |
def fixup(commit, metadata): | |
# add to every commit | |
commit.file_changes.append(fr.FileChange(b'M', args.destPath, fhash, fmode)) | |
# Filter everything | |
fr_args = fr.FilteringOptions.parse_args(['--force', '--replace-refs', 'delete-no-add']) | |
filter = fr.RepoFilter(fr_args, commit_callback=fixup) | |
filter.run() | |
if __name__ == "__main__": | |
main() | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment