Jürgen Gmach has proposed merging ~jugmac00/turnip:move-formatters-into-extra-module into turnip:master.
Commit message: Move formatters into separate module Requested reviews: Launchpad code reviewers (launchpad-reviewers) For more details, see: https://code.launchpad.net/~jugmac00/turnip/+git/turnip/+merge/414167 -- Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/turnip:move-formatters-into-extra-module into turnip:master.
diff --git a/turnip/api/formatter.py b/turnip/api/formatter.py new file mode 100644 index 0000000..ce353e7 --- /dev/null +++ b/turnip/api/formatter.py @@ -0,0 +1,68 @@ +import base64 + +from pygit2 import ( + GIT_OBJ_BLOB, + GIT_OBJ_COMMIT, + GIT_OBJ_TAG, + GIT_OBJ_TREE, + GitError, + ) + + +REF_TYPE_NAME = { + GIT_OBJ_COMMIT: 'commit', + GIT_OBJ_TREE: 'tree', + GIT_OBJ_BLOB: 'blob', + GIT_OBJ_TAG: 'tag' +} + + +def format_blob(blob): + """Return a formatted blob dict.""" + if blob.type != GIT_OBJ_BLOB: + raise GitError('Invalid type: object {} is not a blob.'.format( + blob.oid.hex)) + return { + 'size': blob.size, + 'data': base64.b64encode(blob.data), + } + + +def format_commit(git_object): + """Return a formatted commit object dict.""" + # XXX jugmac00 2022-01-14: this is an additional type check, which + # currently does not get executed by the test suite + # better safe than sorry + if git_object.type != GIT_OBJ_COMMIT: + raise GitError('Invalid type: object {} is not a commit.'.format( + git_object.oid.hex)) + parents = [parent.hex for parent in git_object.parent_ids] + return { + 'sha1': git_object.oid.hex, + 'message': git_object.message, + 'author': format_signature(git_object.author), + 'committer': format_signature(git_object.committer), + 'parents': parents, + 'tree': git_object.tree.hex + } + + +def format_ref(ref, git_object): + """Return a formatted object dict from a ref.""" + return { + ref: { + "object": { + 'sha1': git_object.oid.hex, + 'type': REF_TYPE_NAME[git_object.type] + } + } + } + + +def format_signature(signature): + """Return a formatted signature dict.""" + return { + 'name': signature.name, + 'email': signature.email, + 'time': signature.time + } diff --git a/turnip/api/store.py b/turnip/api/store.py index 86df2df..7a2f883 100644 --- a/turnip/api/store.py +++ b/turnip/api/store.py @@ -1,7 +1,6 @@ # Copyright 2015 Canonical Ltd. This software is licensed under the # GNU Affero General Public License version 3 (see the file LICENSE). -import base64 from collections import defaultdict import itertools import logging @@ -16,10 +15,8 @@ from contextlib2 import ( ExitStack, ) from pygit2 import ( - GIT_OBJ_BLOB, GIT_OBJ_COMMIT, GIT_OBJ_TAG, - GIT_OBJ_TREE, GIT_REF_OID, GIT_SORT_TOPOLOGICAL, GitError, @@ -31,6 +28,11 @@ from pygit2 import ( import six from twisted.web import xmlrpc +from turnip.api.formatter import ( + format_blob, + format_commit, + format_ref, + ) from turnip.config import config from turnip.helpers import TimeoutServerProxy from turnip.pack.helpers import ( @@ -46,69 +48,10 @@ from turnip.tasks import ( logger = logging.getLogger(__name__) -REF_TYPE_NAME = { - GIT_OBJ_COMMIT: 'commit', - GIT_OBJ_TREE: 'tree', - GIT_OBJ_BLOB: 'blob', - GIT_OBJ_TAG: 'tag' - } - - # Where to store repository status information inside a repository directory. REPOSITORY_CREATING_FILE_NAME = '.turnip-creating' -def format_ref(ref, git_object): - """Return a formatted object dict from a ref.""" - return { - ref: { - "object": { - 'sha1': git_object.oid.hex, - 'type': REF_TYPE_NAME[git_object.type] - } - } - } - - -def format_commit(git_object): - """Return a formatted commit object dict.""" - # XXX jugmac00 2022-01-14: this is an additional type check, which - # currently does not get executed by the test suite - # better safe than sorry - if git_object.type != GIT_OBJ_COMMIT: - raise GitError('Invalid type: object {} is not a commit.'.format( - git_object.oid.hex)) - parents = [parent.hex for parent in git_object.parent_ids] - return { - 'sha1': git_object.oid.hex, - 'message': git_object.message, - 'author': format_signature(git_object.author), - 'committer': format_signature(git_object.committer), - 'parents': parents, - 'tree': git_object.tree.hex - } - - -def format_signature(signature): - """Return a formatted signature dict.""" - return { - 'name': signature.name, - 'email': signature.email, - 'time': signature.time - } - - -def format_blob(blob): - """Return a formatted blob dict.""" - if blob.type != GIT_OBJ_BLOB: - raise GitError('Invalid type: object {} is not a blob.'.format( - blob.oid.hex)) - return { - 'size': blob.size, - 'data': base64.b64encode(blob.data), - } - - def is_bare_repo(repo_path): return not os.path.exists(os.path.join(repo_path, '.git')) diff --git a/turnip/api/views.py b/turnip/api/views.py index 27180c8..d5cf3d7 100644 --- a/turnip/api/views.py +++ b/turnip/api/views.py @@ -11,6 +11,7 @@ import pyramid.httpexceptions as exc from pyramid.response import Response from turnip.api import store +from turnip.api.formatter import format_commit from turnip.config import config @@ -351,7 +352,7 @@ class CommitAPI(BaseAPI): commit = store.get_commit(repo_store, repo_name, commit_sha1) except GitError: return exc.HTTPNotFound() - return store.format_commit(commit) + return format_commit(commit) @validate_path def collection_post(self, repo_store, repo_name): @@ -361,7 +362,7 @@ class CommitAPI(BaseAPI): commits = store.get_commits(repo_store, repo_name, commits) except GitError: return exc.HTTPNotFound() - return [store.format_commit(commit) for commit in commits] + return [format_commit(commit) for commit in commits] @resource(path='/repo/{name}/log/{sha1}')
_______________________________________________ Mailing list: https://launchpad.net/~launchpad-reviewers Post to : launchpad-reviewers@lists.launchpad.net Unsubscribe : https://launchpad.net/~launchpad-reviewers More help : https://help.launchpad.net/ListHelp