Fast-Import crash using Filter-Repo - adding file to root commit

401 Views Asked by At

Trying to add file to the root of the branch fails with following error: Added Fast Import crash Report

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 3840, in <module>
    filter.run()
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 3777, in run
    self._parser.run(self._input, self._output)
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 1396, in run
    self._parse_commit()
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 1249, in _parse_commit
    self._commit_callback(commit, aux_info)
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 3334, in _tweak_commit
    self._insert_into_stream(commit)
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 3752, in _insert_into_stream
    self._parser.insert(obj)
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 1374, in insert
    obj.dump(self._output)
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 702, in dump
    change.dump(file_)
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 599, in dump
    quoted_filename = PathQuoting.enquote(self.filename)
  File "C:/Program Files/Git/mingw64/libexec/git-core\git-filter-repo", line 190, in enquote
    if unquoted_string.startswith(b'"') or b'\n' in unquoted_string:
TypeError: startswith first arg must be str or a tuple of str, not bytes
fatal: stream ends early
fast-import: dumping crash report to .git/fast_import_crash_14772

fast-import crash report: https://pastebin.com/DKrz883c

Windows 10
Git Version 2.24

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

2

There are 2 best solutions below

1
On BEST ANSWER

It seems that self.filename is unicode and enquote works with bytes.

I don't know enough about internals of git-filter-repo. May be 'C:\MDC\MDC.7z' should be bytes: b'C:\MDC\MDC.7z'. Or maybe it's a bug; I recommend to report this at their issue tracker.

In Python3 unicode and byte strings are incompatible. See:

$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> u'test'.startswith('"')
False

>>> u'test'.startswith(b'"')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: startswith first arg must be str or a tuple of str, not bytes

>>> b'test'.startswith('"')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

>>> b'test'.startswith(b'"')
False
0
On

Git filter-repo - failed on add files to root commit


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. the 2th param of FileChange should like run git rev-list --objects --all 's path
  2. each param of FileChange must type of bytes,so:
  • 100644 -> b'100644'
  • $(git hash-object -w 'C:\MDC\MDC.7z') -> bytes('$(git hash-object -w 'C:\MDC\MDC.7z')',encoding='utf-8')

example:

$ 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.