如何使用git filter-repo作为带有Python模块接口的库? [英] How to use git filter-repo as a library with the Python module interface?

查看:111
本文介绍了如何使用git filter-repo作为带有Python模块接口的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我在命令行中将Python代码作为字符串给出,例如:

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)

项目自述文件提到它可以用作库,我设法通过以下方式安装Python软件包:

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

但是我很难找到有关如何使用Python API做世界的文档.

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

推荐答案

源代码的最后几行

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()

旨在等同于:

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

这也可以回答:如何替换文件中的文本git历史?

如何也知道blob的路径:

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

使用以下测试存储库在git-filter-repo ac039ecc095d中进行了测试: https://github.com/cirosantilli/test-git-filter-repo

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

这篇关于如何使用git filter-repo作为带有Python模块接口的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆