← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/turnip:refactor-format-commit into turnip:master

 

Jürgen Gmach has proposed merging ~jugmac00/turnip:refactor-format-commit into turnip:master.

Commit message:
Refactor commit retrieving and formatting

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/turnip/+git/turnip/+merge/414151
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/turnip:refactor-format-commit into turnip:master.
diff --git a/turnip/api/store.py b/turnip/api/store.py
index 6fe3095..c97d32e 100644
--- a/turnip/api/store.py
+++ b/turnip/api/store.py
@@ -72,9 +72,6 @@ def format_ref(ref, git_object):
 
 def format_commit(git_object):
     """Return a formatted commit object dict."""
-    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,
@@ -672,8 +669,10 @@ def get_merge_diff(repo_store, repo_name, sha1_base,
         if patch is None:
             patch = u''
         shas = [sha1_base, sha1_head]
-        commits = [get_commit(repo_store, repo_name, sha, repo)
-                   for sha in shas]
+        commits = [
+            format_commit(get_commit(repo_store, repo_name, sha, repo))
+            for sha in shas
+        ]
         return {'commits': commits, 'patch': patch,
                 'conflicts': sorted(conflicts)}
 
@@ -687,8 +686,10 @@ def get_diff(repo_store, repo_name, sha1_from, sha1_to, context_lines=3):
     """
     with open_repo(repo_store, repo_name) as repo:
         shas = [sha1_from, sha1_to]
-        commits = [get_commit(repo_store, repo_name, sha, repo)
-                   for sha in shas]
+        commits = [
+            format_commit(get_commit(repo_store, repo_name, sha, repo))
+            for sha in shas
+        ]
         diff = repo.diff(
             commits[0]['sha1'], commits[1]['sha1'], False, 0,
             context_lines)
@@ -735,7 +736,10 @@ def get_commit(repo_store, repo_name, revision, repo=None):
         except KeyError:
             raise GitError('Object {} does not exist in repository {}.'.format(
                 revision, repo_name))
-        return format_commit(git_object)
+        if git_object.type != GIT_OBJ_COMMIT:
+            raise GitError('Invalid type: object {} is not a commit.'.format(
+                git_object.oid.hex))
+        return git_object
 
 
 def get_commits(repo_store, repo_name, commit_oids):
diff --git a/turnip/api/views.py b/turnip/api/views.py
index d6dcddf..dc68644 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.store 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 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 commits
+        return [format_commit(commit) for commit in commits]
 
 
 @resource(path='/repo/{name}/log/{sha1}')