-
-
Notifications
You must be signed in to change notification settings - Fork 977
Add trailer support for commit creation #2116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -570,6 +570,7 @@ def create_from_tree( | |||||||||||||
| committer: Union[None, Actor] = None, | ||||||||||||||
| author_date: Union[None, str, datetime.datetime] = None, | ||||||||||||||
| commit_date: Union[None, str, datetime.datetime] = None, | ||||||||||||||
| trailers: Union[None, Dict[str, str], List[Tuple[str, str]]] = None, | ||||||||||||||
| ) -> "Commit": | ||||||||||||||
| """Commit the given tree, creating a :class:`Commit` object. | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -609,6 +610,14 @@ def create_from_tree( | |||||||||||||
| :param commit_date: | ||||||||||||||
| The timestamp for the committer field. | ||||||||||||||
|
|
||||||||||||||
| :param trailers: | ||||||||||||||
| Optional trailer key-value pairs to append to the commit message. | ||||||||||||||
| Can be a dictionary mapping trailer keys to values, or a list of | ||||||||||||||
| ``(key, value)`` tuples (useful when the same key appears multiple | ||||||||||||||
| times, e.g. multiple ``Signed-off-by`` trailers). Trailers are | ||||||||||||||
| appended using ``git interpret-trailers``. | ||||||||||||||
| See :manpage:`git-interpret-trailers(1)`. | ||||||||||||||
|
|
||||||||||||||
| :return: | ||||||||||||||
| :class:`Commit` object representing the new commit. | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -678,6 +687,27 @@ def create_from_tree( | |||||||||||||
| tree = repo.tree(tree) | ||||||||||||||
| # END tree conversion | ||||||||||||||
|
|
||||||||||||||
| # APPLY TRAILERS | ||||||||||||||
| if trailers: | ||||||||||||||
| trailer_args: List[str] = [] | ||||||||||||||
| if isinstance(trailers, dict): | ||||||||||||||
| for key, val in trailers.items(): | ||||||||||||||
| trailer_args.append("--trailer") | ||||||||||||||
| trailer_args.append(f"{key}: {val}") | ||||||||||||||
| else: | ||||||||||||||
| for key, val in trailers: | ||||||||||||||
| trailer_args.append("--trailer") | ||||||||||||||
| trailer_args.append(f"{key}: {val}") | ||||||||||||||
|
|
||||||||||||||
| cmd = ["git", "interpret-trailers"] + trailer_args | ||||||||||||||
| proc: Git.AutoInterrupt = repo.git.execute( # type: ignore[call-overload] | ||||||||||||||
| cmd, | ||||||||||||||
|
Comment on lines
+702
to
+704
|
||||||||||||||
| cmd = [repo.git.GIT_PYTHON_GIT_EXECUTABLE, "interpret-trailers"] + trailer_args | |
| proc: Git.AutoInterrupt = repo.git.execute( # type: ignore[call-overload] | |
| cmd, | |
| proc: Git.AutoInterrupt = repo.git._call_process( # type: ignore[attr-defined] | |
| "interpret-trailers", | |
| *trailer_args, |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -566,3 +566,77 @@ def test_commit_co_authors(self): | |
| Actor("test_user_2", "[email protected]"), | ||
| Actor("test_user_3", "[email protected]"), | ||
| ] | ||
|
|
||
| @with_rw_directory | ||
| def test_create_from_tree_with_trailers_dict(self, rw_dir): | ||
| """Test that create_from_tree supports adding trailers via a dict.""" | ||
| rw_repo = Repo.init(osp.join(rw_dir, "test_trailers_dict")) | ||
| path = osp.join(str(rw_repo.working_tree_dir), "hello.txt") | ||
| touch(path) | ||
| rw_repo.index.add([path]) | ||
| tree = rw_repo.index.write_tree() | ||
|
|
||
| trailers = {"Issue": "123", "Signed-off-by": "Test User <[email protected]>"} | ||
| commit = Commit.create_from_tree( | ||
| rw_repo, | ||
| tree, | ||
| "Test commit with trailers", | ||
| head=True, | ||
| trailers=trailers, | ||
| ) | ||
|
|
||
| assert "Issue: 123" in commit.message | ||
| assert "Signed-off-by: Test User <[email protected]>" in commit.message | ||
| assert commit.trailers_dict == { | ||
| "Issue": ["123"], | ||
| "Signed-off-by": ["Test User <[email protected]>"], | ||
| } | ||
|
|
||
| @with_rw_directory | ||
| def test_create_from_tree_with_trailers_list(self, rw_dir): | ||
| """Test that create_from_tree supports adding trailers via a list of tuples.""" | ||
| rw_repo = Repo.init(osp.join(rw_dir, "test_trailers_list")) | ||
| path = osp.join(str(rw_repo.working_tree_dir), "hello.txt") | ||
| touch(path) | ||
| rw_repo.index.add([path]) | ||
| tree = rw_repo.index.write_tree() | ||
|
|
||
| trailers = [ | ||
| ("Signed-off-by", "Alice <[email protected]>"), | ||
| ("Signed-off-by", "Bob <[email protected]>"), | ||
| ("Issue", "456"), | ||
| ] | ||
| commit = Commit.create_from_tree( | ||
| rw_repo, | ||
| tree, | ||
| "Test commit with multiple trailers", | ||
| head=True, | ||
| trailers=trailers, | ||
| ) | ||
|
|
||
| assert "Signed-off-by: Alice <[email protected]>" in commit.message | ||
| assert "Signed-off-by: Bob <[email protected]>" in commit.message | ||
| assert "Issue: 456" in commit.message | ||
| assert commit.trailers_dict == { | ||
| "Signed-off-by": ["Alice <[email protected]>", "Bob <[email protected]>"], | ||
| "Issue": ["456"], | ||
| } | ||
|
|
||
| @with_rw_directory | ||
| def test_index_commit_with_trailers(self, rw_dir): | ||
| """Test that IndexFile.commit() supports adding trailers.""" | ||
| rw_repo = Repo.init(osp.join(rw_dir, "test_index_trailers")) | ||
| path = osp.join(str(rw_repo.working_tree_dir), "hello.txt") | ||
| touch(path) | ||
| rw_repo.index.add([path]) | ||
|
|
||
| trailers = {"Reviewed-by": "Reviewer <[email protected]>"} | ||
| commit = rw_repo.index.commit( | ||
| "Test index commit with trailers", | ||
| trailers=trailers, | ||
| ) | ||
|
|
||
| assert "Reviewed-by: Reviewer <[email protected]>" in commit.message | ||
| assert commit.trailers_dict == { | ||
| "Reviewed-by": ["Reviewer <[email protected]>"], | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.