Skip to content

[v6] plumbing: format/index, Improve v4 entry name validation#1934

Merged
pjbgf merged 4 commits intogo-git:mainfrom
pjbgf:index
Mar 28, 2026
Merged

[v6] plumbing: format/index, Improve v4 entry name validation#1934
pjbgf merged 4 commits intogo-git:mainfrom
pjbgf:index

Conversation

@pjbgf
Copy link
Copy Markdown
Member

@pjbgf pjbgf commented Mar 28, 2026

Updates Git index (plumbing/format/index) encoding/decoding logic to better match on-disk format edge cases (long entry names and V4 name prefix-compression), and adds regression tests around these behaviors.

Relates to #1868.

pjbgf added 4 commits March 28, 2026 13:13
Signed-off-by: Paulo Gomes <paulo@entire.io>
Convert all IndexSuite-based tests to standalone Test functions so they
actually execute. The suite had no runner, so these tests were silently
skipped. Also fix the hash mismatch (SHA256 was used to decode SHA1
fixtures).

Additionally, simplify unknownExtensionDecoder.Decode to use io.Copy.

Signed-off-by: Paulo Gomes <paulo@entire.io>
Assisted-by: Claude Opus 4.6 <noreply@anthropic.com>
…with Git

The V2/V3 decoder truncated entry names longer than 4095 bytes because
it trusted the 12-bit length field. Git falls back to strlen (scanning
for the NUL terminator) when the field is saturated at 0xFFF; do the
same here so long paths round-trip correctly.

The V4 encoder used a path.Dir heuristic for prefix compression, which
diverges from Git's byte-level longest-common-prefix algorithm. Replace
it with a common-prefix computation so the output matches what upstream
produces.

Signed-off-by: Paulo Gomes <paulo@entire.io>
Assisted-by: Claude Opus 4.6 <noreply@anthropic.com>
The treeExtensionDecoder.readEntry early-returned on invalidated entries
(entry_count == -1) before consuming the subtree count and newline,
leaving stale bytes in the stream that corrupted subsequent entries.

Move the invalidation check after fully parsing the entry line so the
reader is always left at the correct position.

Signed-off-by: Paulo Gomes <paulo@entire.io>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Git index (plumbing/format/index) encoder/decoder to better match on-disk edge cases, especially around V4 name prefix-compression and long entry names whose lengths overflow the V2/V3 flags length field, and adds regression tests to lock in the behavior.

Changes:

  • Fix V4 entry name encoding to use true common-prefix compression (rather than directory-based heuristics).
  • Improve V2/V3 name decoding when flags name length is 0xFFF by scanning for the NUL terminator and adjusting padding consumption accordingly.
  • Add/expand regression tests for long names, V4 strip-length validation, patched flags behavior, TREE invalidated entries, and fixture coverage across index versions.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
plumbing/format/index/index_test.go Refactors tests away from testify suite into standard testing + assert/require with parallelization.
plumbing/format/index/encoder_test.go Adds an encode/decode regression test for long entry names overflowing the 12-bit flags length field.
plumbing/format/index/encoder.go Reworks V4 entry name prefix-compression logic to use longest common prefix and writes the correct strip length + suffix.
plumbing/format/index/decoder_test.go Adds comprehensive regression tests for V4 strip-length validation, 0xFFF name-length fallback behavior, fixtures coverage, and TREE invalidated entry parsing.
plumbing/format/index/decoder.go Implements long-name NUL-scan fallback for V2/V3, validates V4 strip length, fixes TREE invalidated entry parsing, and simplifies unknown extension draining.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread plumbing/format/index/decoder.go
Comment thread plumbing/format/index/decoder.go
@pjbgf pjbgf merged commit 90c7d9b into go-git:main Mar 28, 2026
27 of 28 checks passed
@pjbgf pjbgf deleted the index branch March 28, 2026 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants