]> source.dussan.org Git - gitblit.git/commitdiff
Added a list branches rpc
authorJames Moger <james.moger@gitblit.com>
Fri, 28 Oct 2011 12:08:00 +0000 (08:08 -0400)
committerJames Moger <james.moger@gitblit.com>
Fri, 28 Oct 2011 12:08:00 +0000 (08:08 -0400)
docs/02_rpc.mkd
src/com/gitblit/Constants.java
src/com/gitblit/GitBlitException.java
src/com/gitblit/RpcFilter.java
src/com/gitblit/RpcServlet.java
src/com/gitblit/utils/RpcUtils.java
tests/com/gitblit/tests/RpcTests.java

index 13bf8eff5d2835ae741aaa5abffecf0aa6756206..bf2cf24bbf2d36194c686b0965a5e772b78a367d 100644 (file)
@@ -18,6 +18,7 @@ The Gitblit RPC mechanism, like the Gitblit JGit servlet, syndication/feed servl
 <tr><th>req=</th><th>name=</th><th>post body</th><th>response body</th></tr>\r
 <tr><td colspan='5'><em>web.enableRpcServlet=true</em></td></tr>\r
 <tr><td>LIST_REPOSITORIES</td><td>-</td><td>-</td><td>-</td><td>Map&lt;String, RepositoryModel&gt;</td></tr>\r
+<tr><td>LIST_BRANCHES</td><td>-</td><td>-</td><td>-</td><td>Map&lt;String, List&lt;String&gt;&gt;</td></tr>\r
 <tr><td colspan='5'><em>web.enableRpcManagement=true</em></td></tr>\r
 <tr><td>CREATE_REPOSITORY</td><td>repository name</td><td><em>admin</em></td><td>RepositoryModel</td><td>-</td></tr>\r
 <tr><td>EDIT_REPOSITORY</td><td>repository name</td><td><em>admin</em></td><td>RepositoryModel</td><td>-</td></tr>\r
@@ -208,6 +209,9 @@ The original repository name is specified in the *name* url parameter.  The new
 <pre>\r
 {\r
   "bootDate": "2011-10-22T12:13:00Z",\r
+  "version": "0.7.0-SNAPSHOT",\r
+  "releaseDate": "PENDING",\r
+  "isGO": true,\r
   "systemProperties": {\r
     "file.encoding": "Cp1252",\r
     "java.home": "C:\\Program Files\\Java\\jdk1.6.0_26\\jre",\r
@@ -226,6 +230,7 @@ The original repository name is specified in the *name* url parameter.  The new
   },\r
   "heapAllocated": 128057344,\r
   "heapFree": 120399168,\r
-  "heapSize": 1899560960\r
+  "heapSize": 1899560960,\r
+  "servletContainer": "jetty/7.4.3.v20110701"\r
 }\r
 </pre>
\ No newline at end of file
index 450c11fba0bddbd7b65e0f256e66d0d461aca12f..25143051876398c87edfaf6621dd0c93ede18a46 100644 (file)
@@ -201,7 +201,7 @@ public class Constants {
         * a client.\r
         */\r
        public static enum RpcRequest {\r
-               LIST_REPOSITORIES, CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY,\r
+               LIST_REPOSITORIES, LIST_BRANCHES, CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY,\r
                LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER, LIST_REPOSITORY_MEMBERS,\r
                SET_REPOSITORY_MEMBERS, LIST_FEDERATION_REGISTRATIONS, LIST_FEDERATION_RESULTS,\r
                LIST_FEDERATION_PROPOSALS, LIST_FEDERATION_SETS, LIST_SETTINGS, EDIT_SETTINGS,\r
index 360f9f0af3071b0a4c4e5a322afecea65eb09c74..7ab0f99dc92d31a1ff1bca7cc850f9805f1e42dd 100644 (file)
@@ -31,6 +31,10 @@ public class GitBlitException extends IOException {
                super(message);\r
        }\r
 \r
+       public GitBlitException(Throwable cause) {\r
+               super(cause);\r
+       }\r
+\r
        /**\r
         * Exception to indicate that the client should prompt for credentials\r
         * because the requested action requires authentication.\r
index 551ca3c682f33119f724a5fb3c94765bef6eaf15..e768a5146121a3a8f36565818b5ae1206cc942c7 100644 (file)
@@ -64,7 +64,7 @@ public class RpcFilter extends AuthenticationFilter {
                        return;\r
                }\r
 \r
-               boolean adminRequest = requestType.exceeds(RpcRequest.LIST_REPOSITORIES);\r
+               boolean adminRequest = requestType.exceeds(RpcRequest.LIST_BRANCHES);\r
 \r
                // conditionally reject all rpc requests\r
                if (!GitBlit.getBoolean(Keys.web.enableRpcServlet, true)) {\r
index 53628a094e6cefe8aaea85060751604a21d0e370..b068a39bd3b2a148b6b87a3d27f79de97694b093 100644 (file)
@@ -27,11 +27,15 @@ import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;\r
 import javax.servlet.http.HttpServletResponse;\r
 \r
+import org.eclipse.jgit.lib.Repository;\r
+\r
 import com.gitblit.Constants.RpcRequest;\r
+import com.gitblit.models.RefModel;\r
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.ServerSettings;\r
 import com.gitblit.models.UserModel;\r
 import com.gitblit.utils.HttpUtils;\r
+import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.utils.RpcUtils;\r
 \r
 /**\r
@@ -86,6 +90,29 @@ public class RpcServlet extends JsonServlet {
                                repositories.put(url, model);\r
                        }\r
                        result = repositories;\r
+               } else if (RpcRequest.LIST_BRANCHES.equals(reqType)) {\r
+                       // list all branches in all repositories accessible to user\r
+                       Map<String, List<String>> allBranches = new HashMap<String, List<String>>();\r
+                       List<RepositoryModel> models = GitBlit.self().getRepositoryModels(user);\r
+                       for (RepositoryModel model : models) {\r
+                               if (!model.hasCommits) {\r
+                                       // skip empty repository\r
+                                       continue;\r
+                               }\r
+                               // get branches\r
+                               Repository repository = GitBlit.self().getRepository(model.name);\r
+                               List<RefModel> refs = JGitUtils.getLocalBranches(repository, false, -1);\r
+                               refs.addAll(JGitUtils.getRemoteBranches(repository, false, -1));\r
+                               if (refs.size() > 0) {\r
+                                       List<String> branches = new ArrayList<String>();\r
+                                       for (RefModel ref : refs) {\r
+                                               branches.add(ref.getName());\r
+                                       }\r
+                                       allBranches.put(model.name, branches);\r
+                               }\r
+                               repository.close();\r
+                       }\r
+                       result = allBranches;\r
                } else if (RpcRequest.LIST_USERS.equals(reqType)) {\r
                        // list users\r
                        List<String> names = GitBlit.self().getAllUsernames();\r
index e584151847ca9fae20ab8b49023833b0e6cb2b2e..faa910ba668b0ec3c346887fb585137cf30375a2 100644 (file)
@@ -62,6 +62,9 @@ public class RpcUtils {
        private static final Type SETS_TYPE = new TypeToken<Collection<FederationSet>>() {\r
        }.getType();\r
 \r
+       private static final Type BRANCHES_TYPE = new TypeToken<Map<String, Collection<String>>>() {\r
+       }.getType();\r
+\r
        /**\r
         * \r
         * @param remoteURL\r
@@ -385,6 +388,24 @@ public class RpcUtils {
                return status;\r
        }\r
 \r
+       /**\r
+        * Retrieves a map of all branches in the Gitblit server keyed by\r
+        * repository.\r
+        * \r
+        * @param serverUrl\r
+        * @param account\r
+        * @param password\r
+        * @return\r
+        * @throws IOException\r
+        */\r
+       public static Map<String, Collection<String>> getAllBranches(String serverUrl,\r
+                       String account, char[] password) throws IOException {\r
+               String url = asLink(serverUrl, RpcRequest.LIST_BRANCHES);\r
+               Map<String, Collection<String>> allReferences = JsonUtils.retrieveJson(url,\r
+                               BRANCHES_TYPE, account, password);\r
+               return allReferences;\r
+       }\r
+\r
        /**\r
         * Do the specified administrative action on the Gitblit server.\r
         * \r
index 2860f32ba9f18edef0c83ac94b2ae5c49e85447f..637d488e251971540c68add6a4fa708ae12b8d7f 100644 (file)
@@ -16,6 +16,7 @@
 package com.gitblit.tests;\r
 \r
 import java.io.IOException;\r
+import java.util.Collection;\r
 import java.util.HashMap;\r
 import java.util.List;\r
 import java.util.Map;\r
@@ -220,25 +221,32 @@ public class RpcTests extends TestCase {
 \r
        public void testUpdateSettings() throws Exception {\r
                Map<String, String> updated = new HashMap<String, String>();\r
-               \r
+\r
                // grab current setting\r
                ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());\r
                boolean showSizes = settings.get(Keys.web.showRepositorySizes).getBoolean(true);\r
                showSizes = !showSizes;\r
-               \r
+\r
                // update setting\r
                updated.put(Keys.web.showRepositorySizes, String.valueOf(showSizes));\r
                boolean success = RpcUtils.updateSettings(updated, "http://localhost:8080/gb", account,\r
                                password.toCharArray());\r
                assertTrue("Failed to update server settings", success);\r
-               \r
+\r
                // confirm setting change\r
                settings = RpcUtils.getSettings(url, account, password.toCharArray());\r
                boolean newValue = settings.get(Keys.web.showRepositorySizes).getBoolean(false);\r
                assertEquals(newValue, showSizes);\r
-               \r
+\r
                // restore setting\r
                newValue = !newValue;\r
                updated.put(Keys.web.showRepositorySizes, String.valueOf(newValue));\r
        }\r
+\r
+       public void testBranches() throws Exception {\r
+               Map<String, Collection<String>> branches = RpcUtils.getAllBranches(url, account,\r
+                               password.toCharArray());\r
+               assertTrue(branches != null);\r
+               assertTrue(branches.size() > 0);\r
+       }\r
 }\r