X Tutup
Skip to content

Add trailer support for commit creation#2116

Open
Krishnachaitanyakc wants to merge 1 commit intogitpython-developers:mainfrom
Krishnachaitanyakc:add-trailer-support-for-commit-creation
Open

Add trailer support for commit creation#2116
Krishnachaitanyakc wants to merge 1 commit intogitpython-developers:mainfrom
Krishnachaitanyakc:add-trailer-support-for-commit-creation

Conversation

@Krishnachaitanyakc
Copy link

Summary

  • Adds a trailers parameter to Commit.create_from_tree() and IndexFile.commit() enabling trailer creation (e.g. Signed-off-by, Issue) when committing programmatically
  • Accepts either a dict or a list of (key, value) tuples (for duplicate keys like multiple Signed-off-by)
  • Uses git interpret-trailers for formatting, consistent with existing trailer parsing in Commit.trailers_list

Closes #1998

Motivation

GitPython already supports parsing trailers from existing commits, but did not support adding them when creating new commits. The git commit CLI supports this via --trailer, e.g.:

git commit --message "Did a thing" --trailer "Issue: abc"

This change brings equivalent functionality to GitPython's Python API.

Usage

# Using a dict
repo.index.commit("Fix bug", trailers={"Issue": "123"})

# Using a list of tuples (for duplicate keys)
repo.index.commit("Fix bug", trailers=[
    ("Signed-off-by", "Alice <alice@example.com>"),
    ("Signed-off-by", "Bob <bob@example.com>"),
])

# Via Commit.create_from_tree directly
Commit.create_from_tree(repo, tree, "Fix bug", trailers={"Reviewed-by": "Eve"})

Test plan

  • test_create_from_tree_with_trailers_dict - verifies dict-based trailer creation and round-trip parsing
  • test_create_from_tree_with_trailers_list - verifies list-of-tuples trailer creation with duplicate keys
  • test_index_commit_with_trailers - verifies IndexFile.commit() passes trailers through correctly
  • All 3 new tests pass locally

Add a `trailers` parameter to `Commit.create_from_tree()` and
`IndexFile.commit()` that allows appending trailer key-value pairs
(e.g. Signed-off-by, Issue) to the commit message at creation time.

Trailers can be passed as either a dict or a list of (key, value) tuples,
the latter being useful when duplicate keys are needed. The implementation
uses `git interpret-trailers` for proper formatting, consistent with
the existing trailer parsing in `Commit.trailers_list`.

Closes gitpython-developers#1998
@Byron Byron requested a review from Copilot March 25, 2026 12:01
@Byron
Copy link
Member

Byron commented Mar 25, 2026

@codex review

Copy link
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

Adds support for creating commit trailers when programmatically generating commits, aligning commit creation with GitPython’s existing trailer parsing capabilities.

Changes:

  • Add a trailers parameter to Commit.create_from_tree() to append trailers via git interpret-trailers.
  • Thread trailers through IndexFile.commit() so index commits can include trailers.
  • Add tests covering dict-based trailers, list-of-tuples trailers (duplicate keys), and the index commit plumbing.

Reviewed changes

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

File Description
git/objects/commit.py Adds trailers support in Commit.create_from_tree() by invoking git interpret-trailers.
git/index/base.py Exposes trailers in IndexFile.commit() and forwards it to Commit.create_from_tree().
test/test_commit.py Adds round-trip tests validating trailer creation and parsing for both APIs.

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

as_process=True,
istream=PIPE,
)
message = proc.communicate(str(message).encode())[0].decode("utf8")
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

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

When running git interpret-trailers via repo.git.execute(..., as_process=True), non-zero exit codes are not surfaced unless the process is explicitly waited/finalized. After proc.communicate(...), call finalize_process(proc) (or proc.wait()) so failures (e.g., unsupported git version, config errors) raise GitCommandError instead of silently producing an incomplete/empty message.

Suggested change
message = proc.communicate(str(message).encode())[0].decode("utf8")
stdout_bytes, _ = proc.communicate(str(message).encode())
finalize_process(proc)
message = stdout_bytes.decode("utf8")

Copilot uses AI. Check for mistakes.
trailer_args.append("--trailer")
trailer_args.append(f"{key}: {val}")

cmd = ["git", "interpret-trailers"] + trailer_args
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

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

cmd hard-codes the git executable as the literal string "git". This bypasses GitPython’s configured executable (repo.git.GIT_PYTHON_GIT_EXECUTABLE) and can break environments that override it (custom path/wrapper). Consider building the command using the configured executable (and ideally aligning with the existing trailers_list implementation too).

Suggested change
cmd = ["git", "interpret-trailers"] + trailer_args
cmd = [repo.git.GIT_PYTHON_GIT_EXECUTABLE, "interpret-trailers"] + trailer_args

Copilot uses AI. Check for mistakes.
Copy link
Member

@Byron Byron left a comment

Choose a reason for hiding this comment

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

Thanks a lot, this looks good to me.

The Copilot comments should be addressed and then it can be merged.

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.

Ability to add trailers when creating a commit

3 participants

X Tutup