Git filter-repo - не удалось добавить файлы в корневую фиксацию

Попытка добавить файл в корень ветки завершается с ошибкой:

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


person galsi    schedule 25.11.2019    source источник
comment
github.com/newren/git-filter-repo/issues/   -  person phd    schedule 25.11.2019


Ответы (2)


С точки зрения 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))"
person phd    schedule 25.11.2019

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. 2-й параметр 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.
person yang    schedule 15.11.2020
comment
git hash-object кажется командой git из командной строки, а не кодом Python. Как это работает? На самом деле я хотел программно определить путь к файлу, переданному команде git hash-object. Но я не могу этого сделать. - person babueverest; 07.07.2021