Skip to content

Commit 082a624

Browse files
authored
Merge pull request #894 from minitux/master
feat (project): add file blame api
2 parents ba2b60e + f5b4a11 commit 082a624

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

docs/gl_objects/projects.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ Delete a file::
362362

363363
f.delete(commit_message='Delete testfile')
364364

365+
Get file blame::
366+
367+
b = project.files.blame(file_path='README.rst', ref='master')
368+
365369
Project tags
366370
============
367371

gitlab/v4/objects.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3211,6 +3211,28 @@ def raw(
32113211
)
32123212
return utils.response_content(result, streamed, action, chunk_size)
32133213

3214+
@cli.register_custom_action("ProjectFileManager", ("file_path", "ref"))
3215+
@exc.on_http_error(exc.GitlabListError)
3216+
def blame(self, file_path, ref, **kwargs):
3217+
"""Return the content of a file for a commit.
3218+
3219+
Args:
3220+
file_path (str): Path of the file to retrieve
3221+
ref (str): Name of the branch, tag or commit
3222+
**kwargs: Extra options to send to the server (e.g. sudo)
3223+
3224+
Raises:
3225+
GitlabAuthenticationError: If authentication is not correct
3226+
GitlabListError: If the server failed to perform the request
3227+
3228+
Returns:
3229+
list(blame): a list of commits/lines matching the file
3230+
"""
3231+
file_path = file_path.replace("/", "%2F").replace(".", "%2E")
3232+
path = "%s/%s/blame" % (self.path, file_path)
3233+
query_data = {"ref": ref}
3234+
return self.gitlab.http_list(path, query_data, **kwargs)
3235+
32143236

32153237
class ProjectPipelineJob(RESTObject):
32163238
pass

tools/python_test_v4.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@
423423
# object method
424424
assert readme.decode().decode() == "Initial content"
425425

426+
blame = admin_project.files.blame(file_path="README.rst", ref="master")
427+
426428
data = {
427429
"branch": "master",
428430
"commit_message": "blah blah blah",

0 commit comments

Comments
 (0)