Repository navigation
fix(commit): stop treating --- as a patch divider for trailers - #2356
Merged
Byron merged 1 commit intoNov 24, 2026
Merged
Conversation
`Commit._interpret_trailers`, which backs `Commit.trailers_list`, `Commit.trailers_dict` and the `trailers` argument of `Commit.create_from_tree()` and `IndexFile.commit()`, ran `git interpret-trailers` without `--no-divider`. In that mode interpret-trailers reads its input as a `git format-patch` e-mail: the first line starting with `---` followed by whitespace begins the patch, and only the text above it is searched for trailers or receives new ones. Git does not apply that rule to commit messages. `%(trailers)` in `pretty.c` sets `no_divider`, `git commit --trailer` runs `interpret-trailers --no-divider` (`amend_file_with_trailers()`), and `trailer_iterator_init()`, which the sign-off check in `sequencer.c` uses, sets it as well. Commit messages are chosen by whoever authored the commit, so GitPython and Git disagreed about the trailers of the same commit, on both backends: - Dependabot commits put a `---` line before their YAML metadata and sign off below it. `trailers_list` returned `[]` for them, including the Dependabot commits in this repository's history, while `git log --format=%(trailers)` reports `Signed-off-by: dependabot[bot] <support@github.com>`. - A paragraph such as `Reviewed-by: Maintainer <m@example.com>` followed by `---` and more text was reported as a trailer, although Git treats it as body text and reports no trailers for that commit. - `create_from_tree()` with the message `Subject\n\nBody\n---\nNotes\n` and a `Signed-off-by` trailer inserted the trailer above the `---` line, where `git commit --trailer` appends it after `Notes`. Pass `--no-divider` in `_interpret_trailers`, so the parse and add paths treat their input as the commit message it is. Messages without such a `---` line are unaffected. `interpret_trailers` has no Gix handler, so the change covers both backends. Validation on macOS with CPython 3.13.17, Git 2.54.0 and GixPython 0.3.0: - `test_trailers_ignore_patch_divider` and `test_create_from_tree_appends_trailers_after_patch_divider` in `test/test_commit.py` fail before the change and pass after it on both backends. - 300 randomized messages built from trailer lines, `---` variants, comments and body text were committed and `trailers_list` was compared with `git log --format=%(trailers:only,unfold)` for the same commit. Before the change 52 disagreed on the CLI backend; after it none disagreed on either backend (300 messages each). - Full suite: 2207 passed, 155 skipped, 1 xfailed on the CLI backend; 2310 passed, 52 skipped, 1 xfailed on the Gix backend. - Ruff check and format (pre-commit), mypy, `basedpyright --warnings` and `git diff --check` pass. Agent-Model: claude-opus-5-5
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the change to treat --- as message content in commit trailer handling.
No open issues 🎉
What changed in this PR
This PR aligns GitPython’s commit trailer handling with Git by treating --- as message content rather than a patch divider.
Changes:
- Passes
--no-dividerto the shared trailer parsing and insertion helper. - Adds regression tests for reading trailers below
---, rejecting body text as trailers, and appending trailers at the message end. - Documents the behavior change for commit and index APIs.
| File | Description |
|---|---|
test/test_commit.py |
Adds regression coverage for trailer parsing and insertion around --- lines. |
git/objects/commit.py |
Disables patch-divider handling in the shared trailer helper. |
doc/source/changes.rst |
Records the updated trailer behavior and affected APIs. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem: Commit._interpret_trailers, git interpret-trailers ko --no-divider ke bina chalata hai. Is wajah se trailers_list, trailers_dict aur create_from_tree(trailers=...) pehli --- line ko format-patch diff ka start samajh lete hain aur uske baad ka sab kuch ignore kar dete hain. Git commit message ke saath aisa kabhi nahi karta. %(trailers), git commit --trailer aur sign-off check ke peeche wala trailer iterator, teeno no_divider use karte hain. Isliye author-controlled messages par dono backends mein gitpython aur Git ka result alag aata hai:
Dependabot commits --- line ke neeche sign off karte hain, toh gitpython unme koi trailer report nahi karta (is repo ke apne dependabot commits bhi). Wahin git log --format=%(trailers) mein Signed-off-by: dependabot[bot] support@github.com dikhta hai.
Agar Reviewed-by: paragraph ke baad --- aur phir aur text ho, toh gitpython use trailer bata deta hai, jabki Git use body text maanta hai.
Naye trailers --- line ke upar add ho jaate hain. git commit --trailer unhe end mein lagata hai.
Fix: --no-divider pass kar diya hai. Isse CLI aur gix dono par parse aur add wale paths theek ho jaate hain. Jin messages mein aisi line nahi hai, unme koi change nahi hai.
Tests: test/test_commit.py mein 2 naye tests hain. Dono fix se pehle fail aur fix ke baad pass hote hain, dono backends par. 300 random messages par trailers_list ko Git ke %(trailers:only,unfold) ke saath compare kiya. Fix se pehle 52 jagah mismatch tha, ab ek bhi nahi hai.
Motivation: Yeh issue tab mila jab same commits par GitPython ke commit accessors ka output Git ke apne output ke saath compare kiya ja raha tha. Isse yeh dekha ja raha tha ki GitPython author-controlled repository content ko kaise read karta hai.