Repository navigation
Commit d491eca
committed
fix(objects): read commit and tag headers by name instead of position
<!-- agent -->
`Commit._deserialize()`, which parses `git cat-file commit` output on the
CLI backend, took the line after the parent headers as the author and the
next one as the committer without checking their names, and counted any
header starting with `parent` as a parent. Git finds these headers by
name: `parse_commit_buffer()` accepts only `parent ` lines right after
`tree`, and `find_commit_header()` and the pretty formatter look up
`author` and `committer` anywhere in the header block. Whoever authors a
commit controls its headers, so a commit with
tree ...
x-note Linus Torvalds <torvalds@linux-foundation.org> 1700000000 +0000
author Mallory <mallory@evil.example> 1112591163 +0200
committer Mallory <mallory@evil.example> 1112591163 +0200
is shown by `git log`, `git show --format=%an` and
`git for-each-ref --format=%(authorname)` as authored by Mallory, while
`Commit.author` and `Commit.authored_date` reported Linus Torvalds and his
date. The same header between `author` and `committer` replaced the
committer. A `parentx <oid>` header added a parent that Git does not see.
Without a committer line, the parse ran into the message: the subject was
lost, and a message line starting with `gpgsig ` became `Commit.gpgsig`
of an unsigned commit. `git fsck` reports these commits
(`missingAuthor`, `missingCommitter`), but with the default
`transfer.fsckObjects=false` a clone or fetch accepts them. GixPython
0.3.0 refuses to decode them, so the Gix backend falls back to the same
parser and reported the same values.
`TagObject._set_cache_()` read tags the same way on its CLI path. It took
the tagger only from the fourth line and the message from a fixed line
after it, so a header before `tagger` hid the tagger and put header lines
into `TagObject.message`, and an extra header after `tagger` leaked into
the message. `git for-each-ref --format=%(taggername)%00%(contents)`
finds the tagger by name and the message after the first empty line.
Read the author and committer by name in the existing header loop, and
require `parent ` for parents. Continuation lines, such as those of a
`mergetag`, start with a space and match no header name, so the separate
`mergetag` skip is no longer needed. For tags, take the first `tagger`
header after the `tag` line and the message after the first empty line.
When a commit repeats a header, use the first one, as
`find_commit_header()` does for `commit -C`/`--amend`, the sequencer and
`for-each-ref`. Git's pretty formatter takes the last one, so Git is not
consistent there itself. Commits and tags laid out the way Git writes
them parse as before.
Limitation: commits with an `encoding` header still take author and
committer names from Git's pretty formatter, so such a commit that also
repeats `author` pairs the last name with the first date, as before.
Validation: a `header-names` case in `tests/fixtures/commit.sh` backs
`test_deserialization_finds_headers_by_name` in `test/test_commit.py`. It
compares commit and tag fields with `git show` and `git for-each-ref` for
a header before `author`/`tagger`, a header between `author` and
`committer` (after `tagger`), a missing committer, and a `parentx`
header. All four cases fail before the change and pass after it with
`GIT_PYTHON_BACKEND=cli` and `GIT_PYTHON_BACKEND=gix`, and the tag
assertions also fail with only the commit change applied. A scratch
comparison of 14 crafted commits against `git for-each-ref`, which also
covered repeated headers, `encoding` or `gpgsig` before `author`, a
`mergetag` and a missing author, differed in 10 cases before the change
and matched in all of them after it, on both backends. The full suite
passes on both backends (2086 passed on `cli`, 2189 on `gix`; Git 2.56.0,
GixPython 0.3.0, Python 3.11), as do `pre-commit` (ruff, shellcheck),
`mypy` and `basedpyright --warnings`.
Agent-Model: claude-opus-5-51 parent 9232dc5 commit d491eca
4 files changed
Lines changed: 85 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
979 | 979 | | |
980 | 980 | | |
981 | 981 | | |
982 | | - | |
| 982 | + | |
983 | 983 | | |
984 | 984 | | |
985 | 985 | | |
| |||
988 | 988 | | |
989 | 989 | | |
990 | 990 | | |
991 | | - | |
992 | | - | |
993 | | - | |
994 | | - | |
995 | | - | |
996 | | - | |
997 | | - | |
998 | | - | |
999 | | - | |
1000 | | - | |
1001 | | - | |
1002 | | - | |
1003 | | - | |
1004 | | - | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
1005 | 994 | | |
1006 | 995 | | |
1007 | 996 | | |
1008 | 997 | | |
1009 | | - | |
| 998 | + | |
| 999 | + | |
1010 | 1000 | | |
1011 | 1001 | | |
1012 | 1002 | | |
1013 | | - | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
1014 | 1009 | | |
1015 | 1010 | | |
1016 | 1011 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
681 | 681 | | |
682 | 682 | | |
683 | 683 | | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
684 | 723 | | |
685 | 724 | | |
686 | 725 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
81 | 107 | | |
82 | 108 | | |
83 | 109 | | |
| |||
0 commit comments