Skip to content

fix(commit): stop treating --- as a patch divider for trailers - #2356

Merged
Byron merged 1 commit into
gitpython-developers:mainfrom
Keerthana-64:trailers-no-divider
Nov 24, 2026
Merged

Byron merged 1 commit into
gitpython-developers:mainfrom
Keerthana-64:trailers-no-divider

Conversation

@Keerthana-64

Copy link
Copy Markdown
Contributor

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.

`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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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-divider to 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

@Byron
Byron merged commit f21d0ba into gitpython-developers:main Nov 24, 2026
64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants