如何使用 git filter-repo 修改 Blob 的文件路径和数据?

3
例如,在如何在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 中进行了测试。

1个回答

3
Elijah,filter-repo项目的领导回复道:https://github.com/newren/git-filter-repo/issues/158#issuecomment-702962073并解释称,这是不可能的,除非通过“黑科技”实现。
他指引我看了这个内置的例子:https://github.com/newren/git-filter-repo/blob/7b3e714b94a6e5b9f478cb981c7f560ef3f36506/contrib/filter-repo-demos/lint-history#L152它使用一个提交过滤器 + 调用git cat-file 来实现。
潜在的问题是,一个blob可能在较早的时候已经被传送到了git fast-export流中,并且只有在稍后添加了相同的 blob 的第二个提交中才会被引用。而且将所有内容保存在内存中通常会在大型仓库上消耗大量内存。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接