How to use git filter-repo as a library with the Python module interface?

3.9k views Asked by At

I know I give the Python code as a string in the command line for example as:

git-filter-repo --name-callback 'return name.replace(b"Wiliam", b"William")'

but especially as I get into more complex scripts, this will get very clumsy.

Rather, is there a way to do something like:

main.py

import git_filter_repo

def name_callback(name):
    return name.replace(b"Wiliam", b"William")

git_filter_repo.name_callback(name_callback)

The project README mentions that it can be used as a library, and I managed to install the Python package with:

python3 -m pip install --user git-filter-repo

but I couldn't easily find documentation on how to do a hello world with the Python API.

1

There are 1 answers

0
Ciro Santilli OurBigBook.com On

The last few lines of the source https://github.com/newren/git-filter-repo/blob/7b3e714b94a6e5b9f478cb981c7f560ef3f36506/git-filter-repo#L3946 were a good starting point, so I can do something like this:

#!/usr/bin/env python

import git_filter_repo

def blob_callback(blob, callback_metadata):
    blob.data = blob.data.replace(b'd1', b'asdf')

# Args deduced from:
# print(git_filter_repo.FilteringOptions.parse_args(['--refs', 'HEAD', '--force'], error_on_empty=False))
args = git_filter_repo.FilteringOptions.default_options()
args.force = True
args.partial = True
args.refs = ['HEAD']
args.repack=False
args.replace_refs='update-no-add'

git_filter_repo.RepoFilter(
   args,
   blob_callback=blob_callback
).run()

which is intended to be equivalent to:

git filter-repo --refs HEAD <(echo 'd1==>asdf') --force

This also answers: How to substitute text from files in git history?

How to also know the blob's path: How to modify a blob considering both its file path and data with git filter-repo?

Tested in git-filter-repo ac039ecc095d with this test repo: https://github.com/cirosantilli/test-git-filter-repo