Merge pull request #2116 from Krishnachaitanyakc/add-trailer-support-… · gitpython-developers/GitPython@b5b87dd · GitHub
Skip to content

Commit b5b87dd

Browse files
authored
Merge pull request #2116 from Krishnachaitanyakc/add-trailer-support-for-commit-creation
Add trailer support for commit creation
2 parents 5937d14 + af0933c commit b5b87dd

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

git/index/base.py

Lines changed: 2 additions & 0 deletions

git/objects/commit.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ def create_from_tree(
570570
committer: Union[None, Actor] = None,
571571
author_date: Union[None, str, datetime.datetime] = None,
572572
commit_date: Union[None, str, datetime.datetime] = None,
573+
trailers: Union[None, Dict[str, str], List[Tuple[str, str]]] = None,
573574
) -> "Commit":
574575
"""Commit the given tree, creating a :class:`Commit` object.
575576
@@ -609,6 +610,14 @@ def create_from_tree(
609610
:param commit_date:
610611
The timestamp for the committer field.
611612
613+
:param trailers:
614+
Optional trailer key-value pairs to append to the commit message.
615+
Can be a dictionary mapping trailer keys to values, or a list of
616+
``(key, value)`` tuples (useful when the same key appears multiple
617+
times, e.g. multiple ``Signed-off-by`` trailers). Trailers are
618+
appended using ``git interpret-trailers``.
619+
See :manpage:`git-interpret-trailers(1)`.
620+
612621
:return:
613622
:class:`Commit` object representing the new commit.
614623
@@ -678,6 +687,29 @@ def create_from_tree(
678687
tree = repo.tree(tree)
679688
# END tree conversion
680689

690+
# APPLY TRAILERS
691+
if trailers:
692+
trailer_args: List[str] = []
693+
if isinstance(trailers, dict):
694+
for key, val in trailers.items():
695+
trailer_args.append("--trailer")
696+
trailer_args.append(f"{key}: {val}")
697+
else:
698+
for key, val in trailers:
699+
trailer_args.append("--trailer")
700+
trailer_args.append(f"{key}: {val}")
701+
702+
cmd = [repo.git.GIT_PYTHON_GIT_EXECUTABLE, "interpret-trailers"] + trailer_args
703+
proc: Git.AutoInterrupt = repo.git.execute( # type: ignore[call-overload]
704+
cmd,
705+
as_process=True,
706+
istream=PIPE,
707+
)
708+
stdout_bytes, _ = proc.communicate(str(message).encode())
709+
finalize_process(proc)
710+
message = stdout_bytes.decode("utf8")
711+
# END apply trailers
712+
681713
# CREATE NEW COMMIT
682714
new_commit = cls(
683715
repo,

test/test_commit.py

Lines changed: 74 additions & 0 deletions

0 commit comments

Comments
 (0)