Skip to content

Commit d239e12

Browse files
committed
Add new 'gerrit project commit file-content show' command and API
This patch adds new 'gerrit project commit file-content show' command and respective API that allows to gets the content of a file from a certain commit: usage: gerrit project commit file-content show [-h] --commit COMMIT --file-id FILE_ID name Gets the content of a file from a certain commit. positional arguments: name Name of the project. optional arguments: -h, --help show this help message and exit --commit COMMIT Commit ID. --file-id FILE_ID The path to the file. e.g.: gerrit project commit file-content show foo/bar \ --commit 6c94b7dfb4e32af1d99ff8f41f3f2c86bbc0e3ad \ --file-id some/path/to/file.vhd > /tmp/file.vhd
1 parent 11dbe3b commit d239e12

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CHANGES
22
=======
33

4+
* Add new 'gerrit project commit file-content show' command and API
45
* Add new 'gerrit project commit included-in' command and API
56
* Add new 'gerrit project commit show' command and API
67
* Add new 'gerrit project tag create' command and API

gerritclient/commands/project.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,35 @@ def take_action(self, parsed_args):
837837
return self.columns, data
838838

839839

840+
class ProjectCommitFileContentShow(ProjectMixIn, base.BaseCommand):
841+
"""Gets the content of a file from a certain commit."""
842+
843+
def get_parser(self, prog_name):
844+
parser = super(ProjectCommitFileContentShow, self).get_parser(
845+
prog_name)
846+
parser.add_argument(
847+
'name',
848+
help='Name of the project.'
849+
)
850+
parser.add_argument(
851+
'--commit',
852+
required=True,
853+
help='Commit ID.'
854+
)
855+
parser.add_argument(
856+
'--file-id',
857+
required=True,
858+
help='The path to the file.'
859+
)
860+
return parser
861+
862+
def take_action(self, parsed_args):
863+
response = self.client.get_file_content(parsed_args.name,
864+
parsed_args.commit,
865+
parsed_args.file_id)
866+
self.app.stdout.write(response)
867+
868+
840869
def debug(argv=None):
841870
"""Helper to debug the required command."""
842871

gerritclient/tests/unit/cli/test_project.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,25 @@ def test_project_commit_affiliation_show_fail(self, mocked_stderr):
731731
self.assertRaises(SystemExit, self.exec_command, args)
732732
self.assertIn('project commit included-in: error:',
733733
mocked_stderr.write.call_args_list[-1][0][0])
734+
735+
def test_project_commit_file_content_show(self):
736+
commit_id = "184ebe53805e102605d11f6b143486d15c23a09c"
737+
project_name = 'fake/fake-project'
738+
file_id = 'foo/bar/file.vhd'
739+
fake_file_content = 'Some fake file content'
740+
args = ('project commit file-content show {0} '
741+
'--commit {1} --file-id {2}'.format(project_name,
742+
commit_id, file_id))
743+
self.m_client.get_file_content.return_value = fake_file_content
744+
self.exec_command(args)
745+
746+
self.m_get_client.assert_called_once_with('project', mock.ANY)
747+
self.m_client.get_file_content.assert_called_once_with(
748+
project_name, commit_id, file_id)
749+
750+
@mock.patch('sys.stderr')
751+
def test_project_commit_file_content_show_fail(self, mocked_stderr):
752+
args = 'project commit file-content show'
753+
self.assertRaises(SystemExit, self.exec_command, args)
754+
self.assertIn('project commit file-content show: error:',
755+
mocked_stderr.write.call_args_list[-1][0][0])

gerritclient/v1/project.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ def get_commit_affiliation(self, name, commit):
315315
commit=commit)
316316
return self.connection.get_request(request_path)
317317

318+
def get_file_content(self, name, commit, file_id):
319+
request_path = (
320+
"{api_path}{name}/commits/{commit}/files/{file_id}/content".format(
321+
api_path=self.api_path,
322+
name=requests_utils.quote(name, safe=''),
323+
commit=commit,
324+
file_id=requests_utils.quote(file_id, safe=''))
325+
)
326+
return self.connection.get_request(request_path)
327+
318328

319329
def get_client(connection):
320330
return ProjectClient(connection)

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ gerritclient =
9696
project_branch_reflog_show=gerritclient.commands.project:ProjectBranchReflogShow
9797
project_branch_show=gerritclient.commands.project:ProjectBranchShow
9898
project_child_list=gerritclient.commands.project:ProjectChildList
99+
project_commit_file-content_show=gerritclient.commands.project:ProjectCommitFileContentShow
99100
project_commit_included-in=gerritclient.commands.project:ProjectCommitIncludedIn
100101
project_commit_show=gerritclient.commands.project:ProjectCommitShow
101102
project_configuration_download=gerritclient.commands.project:ProjectConfigDownload

0 commit comments

Comments
 (0)