Git filter-repo-将文件添加到根提交失败

时间:2019-11-25 13:07:15

标签: git git-filter-branch

尝试将文件添加到分支的根目录失败,并出现以下错误:

git filter-repo --force --commit-callback "if not commit.parents: commit.file_changes.append(FileChange(b'M', 'C:\MDC\MDC.7z', $(git hash-object -w 'C:\MDC\MDC.7z'), 100644))"

Traceback (most recent call last):
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 3839, in <module>
    filter = RepoFilter(args)
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 2661, in __init__
    self._handle_arg_callbacks()
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 2763, in _handle_arg_callbacks
    handle('commit')
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 2756, in handle
    setattr(self, callback_field, make_callback(type, code_string))
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 2741, in make_callback
    exec('def callback({}, _do_not_use_this_var = None):\n'.format(argname)+
  File "<string>", line 2
    if not commit.parents: commit.file_changes.append(FileChange(b'M', 'C:\MDC\MDC.7z', 3d5fb68077a1d627a7ec3b18f335713c4262fbf0, 100644))
                                                                                         ^
SyntaxError: invalid syntax

Windows 10
Git版本2.24

https://github.com/newren/git-filter-repo

2 个答案:

答案 0 :(得分:1)

git filter-repo --force --commit-callback "if not commit.parents: commit.file_changes.append(FileChange(b'M', b'MDC.7z', bytes('$(git hash-object -w 'C:\MDC\MDC.7z')',encoding='utf-8'), b'100644'))"
  1. FileChange的第二个参数应该运行git rev-list --objects --all的路径。
  2. FileChange的每个参数都必须为字节类型,因此:
  • 100644-> b'100644'
  • $(git hash-object -w 'C:\MDC\MDC.7z')-> bytes('$(git hash-object -w 'C:\MDC\MDC.7z')',encoding='utf-8')

示例:

$ git filter-repo --force --commit-callback "if not commit.parents: commit.file_changes.append(FileChange(b'M', b'1.txt', bytes('$(git hash-object -w 'D:\tmp\git\git1\1.txt')',encoding='utf-8'), b'100644'))"
Parsed 3 commitsHEAD is now at 067c5a4 add 5.txt
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (10/10), done.
Total 10 (delta 2), reused 3 (delta 0)

New history written in 0.68 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
Completely finished after 2.18 seconds.

答案 1 :(得分:0)

从Python的角度来看,3d5fb68077a1d627a7ec3b18f335713c4262fbf0必须是字符串('3d5fb68077a1d627a7ec3b18f335713c4262fbf0'),因此请在其两边加上引号:

--commit-callback "if not commit.parents: commit.file_changes.append(FileChange(b'M', 'C:\MDC\MDC.7z', '$(git hash-object -w 'C:\MDC\MDC.7z')', 100644))"