]> source.dussan.org Git - gitblit.git/commitdiff
Updated the SyndicationServlet to provide an additional option to return details...
authorGerard Smyth <gerard.smyth@gmail.com>
Tue, 1 Apr 2014 11:29:01 +0000 (12:29 +0100)
committerJames Moger <james.moger@gitblit.com>
Thu, 8 May 2014 17:09:30 +0000 (13:09 -0400)
This uses a new 'ot' request parameter to indicate the object type of the content to return, which can be ither TAG or COMMIT.
If this is not provided, then COMMIT is assumed to maintain backwards compatability.
If tags are returned, then the paging parameters, 'l' and 'pg' are still supported, but searching options are currently ignored.

src/main/java/com/gitblit/Constants.java
src/main/java/com/gitblit/servlet/SyndicationServlet.java
src/main/java/com/gitblit/utils/JGitUtils.java

index c8ce83c1b67729d0f7e8db91fe3981c862b735c7..2007c0a865ca4caa5157c6a45d74521c9055e965 100644 (file)
@@ -402,6 +402,27 @@ public class Constants {
                }\r
        }\r
 \r
+       /**\r
+        * Enumeration of the feed content object types.\r
+        */\r
+       public static enum FeedContentObjectType {\r
+               COMMIT, TAG;\r
+\r
+               public static FeedContentObjectType forName(String name) {\r
+                       for (FeedContentObjectType type : values()) {\r
+                               if (type.name().equalsIgnoreCase(name)) {\r
+                                       return type;\r
+                               }\r
+                       }\r
+                       return COMMIT;\r
+               }\r
+\r
+               @Override\r
+               public String toString() {\r
+                       return name().toLowerCase();\r
+               }\r
+       }\r
+\r
        /**\r
         * The types of objects that can be indexed and queried.\r
         */\r
index 24def99525db1e1716b3735ddd0c84a1eb8f59b1..9650ee3a7da16a7a78af6a9870feea637257b7e4 100644 (file)
@@ -163,6 +163,15 @@ public class SyndicationServlet extends DaggerServlet {
                                searchType = type;\r
                        }\r
                }\r
+\r
+               Constants.FeedContentObjectType objectType = Constants.FeedContentObjectType.COMMIT;\r
+               if (!StringUtils.isEmpty(request.getParameter("ot"))) {\r
+                       Constants.FeedContentObjectType type = Constants.FeedContentObjectType.forName(request.getParameter("ot"));\r
+                       if (type != null) {\r
+                               objectType = type;\r
+                       }\r
+               }\r
+\r
                int length = settings.getInteger(Keys.web.syndicationEntries, 25);\r
                if (StringUtils.isEmpty(objectId)) {\r
                        objectId = org.eclipse.jgit.lib.Constants.HEAD;\r
@@ -214,14 +223,7 @@ public class SyndicationServlet extends DaggerServlet {
 \r
 \r
                boolean mountParameters = settings.getBoolean(Keys.web.mountParameters, true);\r
-               String urlPattern;\r
-               if (mountParameters) {\r
-                       // mounted parameters\r
-                       urlPattern = "{0}/commit/{1}/{2}";\r
-               } else {\r
-                       // parameterized parameters\r
-                       urlPattern = "{0}/commit/?r={1}&h={2}";\r
-               }\r
+\r
                String gitblitUrl = settings.getString(Keys.web.canonicalUrl, null);\r
                if (StringUtils.isEmpty(gitblitUrl)) {\r
                        gitblitUrl = HttpUtils.getGitblitURL(request);\r
@@ -247,47 +249,92 @@ public class SyndicationServlet extends DaggerServlet {
                                feedDescription = model.description;\r
                        }\r
 \r
-                       List<RevCommit> commits;\r
-                       if (StringUtils.isEmpty(searchString)) {\r
-                               // standard log/history lookup\r
-                               commits = JGitUtils.getRevLog(repository, objectId, offset, length);\r
+                       if (objectType == Constants.FeedContentObjectType.TAG) {\r
+\r
+                               String urlPattern;\r
+                               if (mountParameters) {\r
+                                       // mounted parameters\r
+                                       urlPattern = "{0}/tag/{1}/{2}";\r
+                               } else {\r
+                                       // parameterized parameters\r
+                                       urlPattern = "{0}/tag/?r={1}&h={2}";\r
+                               }\r
+\r
+                               List<RefModel> tags = JGitUtils.getTags(repository, false, length, offset);\r
+\r
+                               for (RefModel tag : tags) {\r
+                                       FeedEntryModel entry = new FeedEntryModel();\r
+                                       entry.title = tag.getName();\r
+                                       entry.author = tag.getAuthorIdent().getName();\r
+                                       entry.link = MessageFormat.format(urlPattern, gitblitUrl,\r
+                                                       StringUtils.encodeURL(model.name.replace('/', fsc)), tag.getObjectId().getName());\r
+                                       entry.published = tag.getDate();\r
+                                       entry.contentType = "text/html";\r
+                                       entry.content = tag.getFullMessage();\r
+                                       entry.repository = model.name;\r
+                                       entry.branch = objectId;\r
+\r
+                                       entry.tags = new ArrayList<String>();\r
+\r
+                                       // add tag id and referenced commit id\r
+                                       entry.tags.add("tag:" + tag.getObjectId().getName());\r
+                                       entry.tags.add("commit:" + tag.getReferencedObjectId().getName());\r
+\r
+                                       entries.add(entry);\r
+                               }\r
                        } else {\r
-                               // repository search\r
-                               commits = JGitUtils.searchRevlogs(repository, objectId, searchString, searchType,\r
-                                               offset, length);\r
-                       }\r
-                       Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository, model.showRemoteBranches);\r
-                       BugtraqProcessor processor = new BugtraqProcessor(settings);\r
-\r
-                       // convert RevCommit to SyndicatedEntryModel\r
-                       for (RevCommit commit : commits) {\r
-                               FeedEntryModel entry = new FeedEntryModel();\r
-                               entry.title = commit.getShortMessage();\r
-                               entry.author = commit.getAuthorIdent().getName();\r
-                               entry.link = MessageFormat.format(urlPattern, gitblitUrl,\r
-                                               StringUtils.encodeURL(model.name.replace('/', fsc)), commit.getName());\r
-                               entry.published = commit.getCommitterIdent().getWhen();\r
-                               entry.contentType = "text/html";\r
-                               String message = processor.processCommitMessage(repository, model, commit.getFullMessage());\r
-                               entry.content = message;\r
-                               entry.repository = model.name;\r
-                               entry.branch = objectId;\r
-                               entry.tags = new ArrayList<String>();\r
-\r
-                               // add commit id and parent commit ids\r
-                               entry.tags.add("commit:" + commit.getName());\r
-                               for (RevCommit parent : commit.getParents()) {\r
-                                       entry.tags.add("parent:" + parent.getName());\r
+\r
+                               String urlPattern;\r
+                               if (mountParameters) {\r
+                                       // mounted parameters\r
+                                       urlPattern = "{0}/commit/{1}/{2}";\r
+                               } else {\r
+                                       // parameterized parameters\r
+                                       urlPattern = "{0}/commit/?r={1}&h={2}";\r
                                }\r
 \r
-                               // add refs to tabs list\r
-                               List<RefModel> refs = allRefs.get(commit.getId());\r
-                               if (refs != null && refs.size() > 0) {\r
-                                       for (RefModel ref : refs) {\r
-                                               entry.tags.add("ref:" + ref.getName());\r
+                               List<RevCommit> commits;\r
+                               if (StringUtils.isEmpty(searchString)) {\r
+                                       // standard log/history lookup\r
+                                       commits = JGitUtils.getRevLog(repository, objectId, offset, length);\r
+                               } else {\r
+                                       // repository search\r
+                                       commits = JGitUtils.searchRevlogs(repository, objectId, searchString, searchType,\r
+                                                       offset, length);\r
+                               }\r
+                               Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository, model.showRemoteBranches);\r
+                               BugtraqProcessor processor = new BugtraqProcessor(settings);\r
+\r
+                               // convert RevCommit to SyndicatedEntryModel\r
+                               for (RevCommit commit : commits) {\r
+                                       FeedEntryModel entry = new FeedEntryModel();\r
+                                       entry.title = commit.getShortMessage();\r
+                                       entry.author = commit.getAuthorIdent().getName();\r
+                                       entry.link = MessageFormat.format(urlPattern, gitblitUrl,\r
+                                                       StringUtils.encodeURL(model.name.replace('/', fsc)), commit.getName());\r
+                                       entry.published = commit.getCommitterIdent().getWhen();\r
+                                       entry.contentType = "text/html";\r
+                                       String message = processor.processCommitMessage(repository, model, commit.getFullMessage());\r
+                                       entry.content = message;\r
+                                       entry.repository = model.name;\r
+                                       entry.branch = objectId;\r
+                                       entry.tags = new ArrayList<String>();\r
+\r
+                                       // add commit id and parent commit ids\r
+                                       entry.tags.add("commit:" + commit.getName());\r
+                                       for (RevCommit parent : commit.getParents()) {\r
+                                               entry.tags.add("parent:" + parent.getName());\r
+                                       }\r
+\r
+                                       // add refs to tabs list\r
+                                       List<RefModel> refs = allRefs.get(commit.getId());\r
+                                       if (refs != null && refs.size() > 0) {\r
+                                               for (RefModel ref : refs) {\r
+                                                       entry.tags.add("ref:" + ref.getName());\r
+                                               }\r
                                        }\r
+                                       entries.add(entry);\r
                                }\r
-                               entries.add(entry);\r
                        }\r
                }\r
 \r
index 190872a786942052f8dcc618309cb18cec6c2e7f..da51ea98695e6bea4e88e02d444987000a390a09 100644 (file)
@@ -1667,6 +1667,24 @@ public class JGitUtils {
                return getRefs(repository, Constants.R_TAGS, fullName, maxCount);\r
        }\r
 \r
+       /**\r
+        * Returns the list of tags in the repository. If repository does not exist\r
+        * or is empty, an empty list is returned.\r
+        *\r
+        * @param repository\r
+        * @param fullName\r
+        *            if true, /refs/tags/yadayadayada is returned. If false,\r
+        *            yadayadayada is returned.\r
+        * @param maxCount\r
+        *            if < 0, all tags are returned\r
+        * @param offset\r
+        *            if maxCount provided sets the starting point of the records to return\r
+        * @return list of tags\r
+        */\r
+       public static List<RefModel> getTags(Repository repository, boolean fullName, int maxCount, int offset) {\r
+               return getRefs(repository, Constants.R_TAGS, fullName, maxCount, offset);\r
+       }\r
+\r
        /**\r
         * Returns the list of local branches in the repository. If repository does\r
         * not exist or is empty, an empty list is returned.\r
@@ -1748,6 +1766,27 @@ public class JGitUtils {
         */\r
        private static List<RefModel> getRefs(Repository repository, String refs, boolean fullName,\r
                        int maxCount) {\r
+               return getRefs(repository, refs, fullName, maxCount, 0);\r
+       }\r
+\r
+       /**\r
+        * Returns a list of references in the repository matching "refs". If the\r
+        * repository is null or empty, an empty list is returned.\r
+        *\r
+        * @param repository\r
+        * @param refs\r
+        *            if unspecified, all refs are returned\r
+        * @param fullName\r
+        *            if true, /refs/something/yadayadayada is returned. If false,\r
+        *            yadayadayada is returned.\r
+        * @param maxCount\r
+        *            if < 0, all references are returned\r
+        * @param offset\r
+        *            if maxCount provided sets the starting point of the records to return\r
+        * @return list of references\r
+        */\r
+       private static List<RefModel> getRefs(Repository repository, String refs, boolean fullName,\r
+                       int maxCount, int offset) {\r
                List<RefModel> list = new ArrayList<RefModel>();\r
                if (maxCount == 0) {\r
                        return list;\r
@@ -1771,7 +1810,14 @@ public class JGitUtils {
                        Collections.sort(list);\r
                        Collections.reverse(list);\r
                        if (maxCount > 0 && list.size() > maxCount) {\r
-                               list = new ArrayList<RefModel>(list.subList(0, maxCount));\r
+                               if (offset < 0) {\r
+                                       offset = 0;\r
+                               }\r
+                               int endIndex = offset + maxCount;\r
+                               if (endIndex > list.size()) {\r
+                                       endIndex = list.size();\r
+                               }\r
+                               list = new ArrayList<RefModel>(list.subList(offset, endIndex));\r
                        }\r
                } catch (IOException e) {\r
                        error(e, repository, "{0} failed to retrieve {1}", refs);\r