]> source.dussan.org Git - gitblit.git/commitdiff
Added a utility class to specify gitblit.indexBranch en masse
authorJames Moger <james.moger@gitblit.com>
Tue, 27 Mar 2012 20:52:44 +0000 (16:52 -0400)
committerJames Moger <james.moger@gitblit.com>
Tue, 27 Mar 2012 20:52:44 +0000 (16:52 -0400)
build.xml
distrib/add-indexed-branch.cmd [new file with mode: 0644]
src/com/gitblit/AddIndexedBranch.java [new file with mode: 0644]

index 3bdfa85b49c3c0698524be8765ffd809c426d2da..b57a0336e770770818cffa5db5a6b6fc0b81de2c 100644 (file)
--- a/build.xml
+++ b/build.xml
                                <exclude name="com/gitblit/tests/" />\r
                                <exclude name="com/gitblit/build/**" />\r
                                <exclude name="com/gitblit/client/**" />\r
+                               <exclude name="com/gitblit/AddIndexedBranch*.class" />\r
                                <exclude name="com/gitblit/GitBlitServer*.class" />\r
                                <exclude name="com/gitblit/Launcher*.class" />\r
                                <exclude name="com/gitblit/MakeCertificate*.class" />\r
diff --git a/distrib/add-indexed-branch.cmd b/distrib/add-indexed-branch.cmd
new file mode 100644 (file)
index 0000000..e167639
--- /dev/null
@@ -0,0 +1,20 @@
+@REM --------------------------------------------------------------------------\r
+@REM This is for Lucene search integration.\r
+@REM\r
+@REM Allows you to add an indexed branch specification to the repository config\r
+@REM for all matching repositories in the specified folder.\r
+@REM\r
+@REM All repositories are included unless excluded using a --skip parameter.\r
+@REM --skip supports simple wildcard fuzzy matching however only 1 asterisk is\r
+@REM allowed per parameter.\r
+@REM\r
+@REM Always use forward-slashes for the path separator in your parameters!!\r
+@REM\r
+@REM Set FOLDER to the server's git.repositoriesFolder\r
+@REM Set BRANCH ("default" or fully qualified ref - i.e. refs/heads/master)\r
+@REM Set EXCLUSIONS for any repositories that you do not want to change\r
+@REM --------------------------------------------------------------------------\r
+@SET FOLDER=c:/gitblit/git\r
+@SET EXCLUSIONS=--skip test.git --skip group/test*\r
+@SET BRANCH=default\r
+@java -cp gitblit.jar;"%CD%\ext\*" com.gitblit.AddIndexedBranch --repositoriesFolder %FOLDER% --branch %BRANCH% %EXCLUSIONS%\r
diff --git a/src/com/gitblit/AddIndexedBranch.java b/src/com/gitblit/AddIndexedBranch.java
new file mode 100644 (file)
index 0000000..1eb9389
--- /dev/null
@@ -0,0 +1,131 @@
+/*\r
+ * Copyright 2012 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit;\r
+\r
+import java.io.File;\r
+import java.text.MessageFormat;\r
+import java.util.ArrayList;\r
+import java.util.LinkedHashSet;\r
+import java.util.List;\r
+import java.util.Set;\r
+import java.util.TreeSet;\r
+\r
+import org.eclipse.jgit.lib.RepositoryCache.FileKey;\r
+import org.eclipse.jgit.storage.file.FileBasedConfig;\r
+import org.eclipse.jgit.storage.file.FileRepository;\r
+import org.eclipse.jgit.util.FS;\r
+\r
+import com.beust.jcommander.JCommander;\r
+import com.beust.jcommander.Parameter;\r
+import com.beust.jcommander.ParameterException;\r
+import com.beust.jcommander.Parameters;\r
+import com.gitblit.utils.ArrayUtils;\r
+import com.gitblit.utils.JGitUtils;\r
+import com.gitblit.utils.StringUtils;\r
+\r
+/**\r
+ * Utility class to add an indexBranch setting to matching repositories.\r
+ * \r
+ * @author James Moger\r
+ * \r
+ */\r
+public class AddIndexedBranch {\r
+\r
+       public static void main(String... args) {\r
+               Params params = new Params();\r
+               JCommander jc = new JCommander(params);\r
+               try {\r
+                       jc.parse(args);\r
+               } catch (ParameterException t) {\r
+                       System.err.println(t.getMessage());\r
+                       jc.usage();\r
+                       return;\r
+               }\r
+               \r
+               // create a lowercase set of excluded repositories\r
+               Set<String> exclusions = new TreeSet<String>();\r
+               for (String exclude : params.exclusions) {\r
+                       exclusions.add(exclude.toLowerCase());\r
+               }\r
+               \r
+               // determine available repositories\r
+               File folder = new File(params.folder);\r
+               List<String> repoList = JGitUtils.getRepositoryList(folder, false, true);\r
+               \r
+               int modCount = 0;\r
+               int skipCount = 0;\r
+               for (String repo : repoList) {\r
+                       boolean skip = false;\r
+                       for (String exclusion : exclusions) {\r
+                               if (StringUtils.fuzzyMatch(repo, exclusion)) {\r
+                                       skip = true;\r
+                                       break;\r
+                               }\r
+                       }\r
+                       \r
+                       if (skip) {\r
+                               System.out.println("skipping " + repo);\r
+                               skipCount++;\r
+                               continue;\r
+                       }\r
+\r
+                       System.out.println(MessageFormat.format("adding [gitblit] indexBranch={0} for {1}", params.branch, repo));\r
+                       try {\r
+                               // load repository config\r
+                               File gitDir = FileKey.resolve(new File(folder, repo), FS.DETECTED);\r
+                               FileRepository repository = new FileRepository(gitDir);\r
+                               FileBasedConfig config = repository.getConfig();\r
+                               config.load();\r
+                               \r
+                               Set<String> indexedBranches = new LinkedHashSet<String>();\r
+                               indexedBranches.add(Constants.DEFAULT_BRANCH);\r
+                               \r
+                               String [] branches = config.getStringList("gitblit", null, "indexBranch");\r
+                               if (!ArrayUtils.isEmpty(branches)) {\r
+                                       for (String branch : branches) {\r
+                                               indexedBranches.add(branch);\r
+                                       }\r
+                               }\r
+                               config.setStringList("gitblit", null, "indexBranch", new ArrayList<String>(indexedBranches));\r
+                               config.save();\r
+                               modCount++;\r
+                       } catch (Exception e) {\r
+                               System.err.println(repo);\r
+                               e.printStackTrace();\r
+                       }\r
+               }\r
+               \r
+               System.out.println(MessageFormat.format("updated {0} repository configurations, skipped {1}", modCount, skipCount));\r
+       }\r
+\r
+       \r
+\r
+       /**\r
+        * JCommander Parameters class for AddIndexedBranch.\r
+        */\r
+       @Parameters(separators = " ")\r
+       private static class Params {\r
+\r
+               @Parameter(names = { "--repositoriesFolder" }, description = "The root repositories folder ", required = true)\r
+               public String folder;\r
+\r
+               @Parameter(names = { "--branch" }, description = "The branch to index", required = true)\r
+               public String branch = "default";\r
+\r
+               @Parameter(names = { "--skip" }, description = "Skip the named repository (simple fizzy matching is supported)", required = false)\r
+               public List<String> exclusions = new ArrayList<String>();\r
+       }\r
+}\r