首页 > 解决方案 > 如何使用 git filter-repo 同时考虑其文件路径和数据来修改 blob?

问题描述

例如在如何使用 git filter-repo 作为具有 Python 模块接口的库?为了重构目的,我设法修改了旧提交的 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,我希望肯定 acommit_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()

这一次,我确实设法获得了 blob 路径print(file_change.filename),但没有获得 blob 数据。

我有那个blob_id,但我不知道如何使用它。

我想我可以分两次完成,一次提交回调创建从 blob ID 到路径的映射,第二个 blob 回调使用该信息,但感觉有点难看。

有没有更好的方法来访问两者,例如commit_callback我错过的一些参数字段?

Ping 问题跟踪器:https ://github.com/newren/git-filter-repo/issues/158

git filter-repoac039ecc095d 中测试。

标签: gitgit-filter-repo

解决方案


filter-repo 项目负责人 Elijah 回答: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 的第二次提交中仅由 ID 引用。并且将所有内容保存在内存中通常会破坏大型存储库的内存。


推荐阅读