Skip to content

Writing a version 4 index corrupts it #15

Closed

Description

@dnnsjsk

Writing an index that was read as version 4 produces a file git cannot read. IndexWriter::serialize() keeps the version number from the index it read (

$header = 'DIRC'
. pack('N', $index->version())
. pack('N', count($entries));
) but always writes v2/v3 entries: full NUL-terminated paths padded to 8 bytes (
$fixed = pack('N', $entry->ctimeSec)
. pack('N', $entry->ctimeNsec)
. pack('N', $entry->mtimeSec)
. pack('N', $entry->mtimeNsec)
. pack('N', $entry->dev)
. pack('N', $entry->ino)
. pack('N', $entry->mode)
. pack('N', $entry->uid)
. pack('N', $entry->gid)
. pack('N', $entry->fileSize)
. $entry->hash->binary
. pack('n', $entry->flags);
if (($entry->flags & 0x4000) !== 0) {
$fixed .= pack('n', $entry->extendedFlags);
}
$pathBytes = $entry->path . "\0";
$entryData = $fixed . $pathBytes;
// Pad to 8-byte alignment
$padLen = (8 - (strlen($entryData) % 8)) % 8;
$entryData .= str_repeat("\0", $padLen);
return $entryData;
}
). Version 4 requires prefix-compressed paths without padding.

Repro

git init -q t && cd t && git config user.name T && git config user.email t@x
echo a > README.md && mkdir -p src/deep && echo b > src/deep/file.txt
git add . && git commit -qm i
git update-index --index-version 4
echo c >> README.md
php -r 'require "vendor/autoload.php"; Pitmaster\Pitmaster::open(getcwd())->add("README.md");'
git status
# fatal: malformed name field in the index, near path 'EADME.md'

Every operation that writes the index (add, commit state, checkout, reset, ...) is affected. Reading v4 works.

Expected

Either write v4 entries with prefix compression, or downgrade the index to version 2/3 when writing.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions