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

1 participant

X Tutup