From ad2802af6840811ceae740febffe66512d285197 Mon Sep 17 00:00:00 2001 From: Nick Brown Date: Tue, 20 Jan 2026 10:40:31 +0000 Subject: [PATCH] fix: file save start_branch as a body attribute Passing `start_branch` as kwargs results in it being passed as query argument to the API: ``` send: b'PUT /api/v4/projects/12345678/repository/files/readme.txt?start_branch=main send: b'{"file_path": "readme.txt", "branch": "new_branch", "content": "Modified contents", "commit_message": "File was modified for this new branch"}' ``` which results in error being returned: ``` {"message":"You can only create or edit files when you are on a branch"} ``` It should instead be sent a body attribute, which succeeds in creating the branch during the save. To be sent as body attribute it must be specified as concrete function argument and class attribute instead of just using kwargs Closes: #3318 --- gitlab/v4/objects/files.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/gitlab/v4/objects/files.py b/gitlab/v4/objects/files.py index 757d16eeb..3bcf931a2 100644 --- a/gitlab/v4/objects/files.py +++ b/gitlab/v4/objects/files.py @@ -29,6 +29,7 @@ class ProjectFile(SaveMixin, ObjectDeleteMixin, RESTObject): file_path: str manager: ProjectFileManager content: str # since the `decode()` method uses `self.content` + start_branch: str | None = None def decode(self) -> bytes: """Returns the decoded content of the file. @@ -41,7 +42,11 @@ def decode(self) -> bytes: # NOTE(jlvillal): Signature doesn't match SaveMixin.save() so ignore # type error def save( # type: ignore[override] - self, branch: str, commit_message: str, **kwargs: Any + self, + branch: str, + commit_message: str, + start_branch: str | None = None, + **kwargs: Any, ) -> None: """Save the changes made to the file to the server. @@ -50,6 +55,7 @@ def save( # type: ignore[override] Args: branch: Branch in which the file will be updated commit_message: Message to send with the commit + start_branch: Name of the branch to start the new branch from **kwargs: Extra options to send to the server (e.g. sudo) Raises: @@ -58,6 +64,7 @@ def save( # type: ignore[override] """ self.branch = branch self.commit_message = commit_message + self.start_branch = start_branch self.file_path = utils.EncodedId(self.file_path) super().save(**kwargs)