例如,在如何在Python模块接口中使用git filter-repo作为库?的问题中,我成功地使用以下方法修改了旧提交的blob以进行重构:
def blob_callback(blob, callback_metadata):
blob.data = blob.data.replace(b'd1', b'asdf')
git_filter_repo.RepoFilter(
args,
blob_callback=blob_callback
).run()
但我找不到blob的路径,这将是一个有用的信息,特别是通过文件扩展名确定文件类型并相应地调整数据修改。
如果使用 blob_callback
不可能实现这一点,我期望通过 commit_callback
可以实现,所以我尝试了以下内容:
#!/usr/bin/env python
# https://stackoverflow.com/questions/64160917/how-to-use-git-filter-repo-as-a-library-with-the-python-module-interface/64160918#64160918
import git_filter_repo
def blob_callback(blob, callback_metadata):
blob.data = blob.data.replace(b'd1', b'asdf')
def commit_callback(commit, callback_metadata):
for file_change in commit.file_changes:
print(commit)
print(file_change)
print(file_change.filename)
print(file_change.blob_id)
print(callback_metadata)
print()
# 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
commit_callback=commit_callback
).run()
这次,我成功获取到了 print(file_change.filename)
处的 blob 路径,但没有得到 blob 数据。
我有那个 blob_id
,但不知道该如何使用它。
我想我可以分两步来做,第一步使用提交回调函数从 blob ID 创建路径映射表,第二步使用 blob 回调函数来使用这些信息,但感觉有点丑陋。
有更好的方法来同时访问它们吗?例如,我错过了一些关于 commit_callback
参数的字段吗?
在问题追踪器上标记为重要:https://github.com/newren/git-filter-repo/issues/158
在 git filter-repo
版本 ac039ecc095d 中进行了测试。