]> source.dussan.org Git - gitblit.git/commitdiff
Centralized markdown transforms. Moved config ops to GitBlit.
authorJames Moger <james.moger@gitblit.com>
Thu, 28 Apr 2011 01:22:12 +0000 (21:22 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 28 Apr 2011 01:22:12 +0000 (21:22 -0400)
src/com/gitblit/GitBlit.java
src/com/gitblit/utils/JGitUtils.java
src/com/gitblit/utils/StringUtils.java
src/com/gitblit/wicket/RepositoryPage.java
src/com/gitblit/wicket/models/RepositoryModel.java
src/com/gitblit/wicket/pages/EditRepositoryPage.java
src/com/gitblit/wicket/pages/RepositoriesPage.java
src/com/gitblit/wicket/pages/SummaryPage.java

index c56b3cbb06f7fa38191ba6156d32293e3f2f0ba6..a626be845a8e170df03a36ccd72e21fec34fb5be 100644 (file)
@@ -1,8 +1,8 @@
 package com.gitblit;\r
 \r
 import java.io.File;\r
+import java.io.IOException;\r
 import java.util.ArrayList;\r
-import java.util.Date;\r
 import java.util.List;\r
 \r
 import javax.servlet.ServletContextEvent;\r
@@ -12,6 +12,7 @@ import javax.servlet.http.Cookie;
 import org.apache.wicket.protocol.http.WebResponse;\r
 import org.eclipse.jgit.errors.RepositoryNotFoundException;\r
 import org.eclipse.jgit.lib.Repository;\r
+import org.eclipse.jgit.lib.StoredConfig;\r
 import org.eclipse.jgit.transport.resolver.FileResolver;\r
 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;\r
 import org.slf4j.Logger;\r
@@ -93,7 +94,7 @@ public class GitBlit implements ServletContextListener {
                response.addCookie(userCookie);\r
        }\r
 \r
-       public void editRepository(RepositoryModel repository, boolean isCreate) {\r
+       public void editRepositoryModel(RepositoryModel repository, boolean isCreate) {\r
                Repository r = null;\r
                if (isCreate) {\r
                        // create repository\r
@@ -109,14 +110,21 @@ public class GitBlit implements ServletContextListener {
                        } catch (ServiceNotEnabledException e) {\r
                                logger.error("Service not enabled", e);\r
                        }\r
-               }               \r
-                               \r
+               }\r
+\r
                // update settings\r
-               JGitUtils.setRepositoryDescription(r, repository.description);\r
-               JGitUtils.setRepositoryOwner(r, repository.owner);\r
-               JGitUtils.setRepositoryUseTickets(r, repository.useTickets);\r
-               JGitUtils.setRepositoryUseDocs(r, repository.useDocs);\r
-               JGitUtils.setRepositoryRestrictedAccess(r, repository.useRestrictedAccess);\r
+               StoredConfig config = JGitUtils.readConfig(r);\r
+               config.setString("gitblit", null, "description", repository.description);\r
+               config.setString("gitblit", null, "owner", repository.owner);\r
+               config.setBoolean("gitblit", null, "useTickets", repository.useTickets);\r
+               config.setBoolean("gitblit", null, "useDocs", repository.useDocs);\r
+               config.setBoolean("gitblit", null, "restrictedAccess", repository.useRestrictedAccess);\r
+               try {\r
+                       config.save();\r
+               } catch (IOException e) {\r
+                       logger.error("Failed to save repository config!", e);\r
+               }\r
+               r.close();\r
        }\r
 \r
        public List<String> getRepositoryList() {\r
@@ -127,17 +135,7 @@ public class GitBlit implements ServletContextListener {
                List<String> list = getRepositoryList();\r
                List<RepositoryModel> repositories = new ArrayList<RepositoryModel>();\r
                for (String repo : list) {\r
-                       Repository r = getRepository(repo);\r
-                       String description = JGitUtils.getRepositoryDescription(r);\r
-                       String owner = JGitUtils.getRepositoryOwner(r);\r
-                       String group = JGitUtils.getRepositoryGroup(r);\r
-                       Date lastchange = JGitUtils.getLastChange(r);\r
-                       RepositoryModel model = new RepositoryModel(repo, description, owner, lastchange);\r
-                       model.group = group;\r
-                       model.useTickets = JGitUtils.getRepositoryUseTickets(r);\r
-                       model.useDocs = JGitUtils.getRepositoryUseDocs(r);\r
-                       model.useRestrictedAccess = JGitUtils.getRepositoryRestrictedAccess(r);\r
-                       r.close();\r
+                       RepositoryModel model = getRepositoryModel(repo);\r
                        repositories.add(model);\r
                }\r
                return repositories;\r
@@ -158,6 +156,24 @@ public class GitBlit implements ServletContextListener {
                return r;\r
        }\r
 \r
+       public RepositoryModel getRepositoryModel(String repositoryName) {\r
+               Repository r = getRepository(repositoryName);\r
+               RepositoryModel model = new RepositoryModel();\r
+               model.name = repositoryName;\r
+               model.lastChange = JGitUtils.getLastChange(r);\r
+               StoredConfig config = JGitUtils.readConfig(r);\r
+               if (config != null) {\r
+                       model.description = config.getString("gitblit", null, "description");\r
+                       model.owner = config.getString("gitblit", null, "owner");\r
+                       model.group = config.getString("gitblit", null, "group");\r
+                       model.useTickets = config.getBoolean("gitblit", "useTickets", false);\r
+                       model.useDocs = config.getBoolean("gitblit", "useDocs", false);\r
+                       model.useRestrictedAccess = config.getBoolean("gitblit", "restrictedAccess", false);\r
+               }\r
+               r.close();\r
+               return model;\r
+       }\r
+\r
        public void setupContext(IStoredSettings settings) {\r
                logger.info("Setting up GitBlit context from " + settings.toString());\r
                this.storedSettings = settings;\r
index 8814c1f4ccdf62b2fca741c37307e2d39f33bd99..4cfed0da51b062686ceed57d0a6a80408ee92e97 100644 (file)
@@ -775,98 +775,7 @@ public class JGitUtils {
                return r.toString();\r
        }\r
 \r
-       public static String getRepositoryDescription(Repository r) {\r
-               return getRepositoryConfigString(r, "description");\r
-       }\r
-       \r
-       public static void setRepositoryDescription(Repository r, String value) {\r
-               setRepositoryConfigString(r, "description", value);\r
-       }\r
-\r
-       public static String getRepositoryOwner(Repository r) {\r
-               return getRepositoryConfigString(r, "owner");\r
-       }\r
-       \r
-       public static void setRepositoryOwner(Repository r, String owner) {\r
-               setRepositoryConfigString(r, "owner", owner);\r
-       }\r
-       \r
-       public static String getRepositoryGroup(Repository r) {\r
-               return getRepositoryConfigString(r, "group");\r
-       }\r
-       \r
-       public static void setRepositoryGroup(Repository r, String group) {\r
-               setRepositoryConfigString(r, "group", group);\r
-       } \r
-       \r
-       public static boolean getRepositoryUseTickets(Repository r) {\r
-               return getRepositoryConfigBoolean(r, "useTickets", false);\r
-       }\r
-       \r
-       public static void setRepositoryUseTickets(Repository r, boolean value) {\r
-               setRepositoryConfigBoolean(r, "useTickets", value);\r
-       }\r
-       \r
-       public static boolean getRepositoryUseDocs(Repository r) {\r
-               return getRepositoryConfigBoolean(r, "useDocs", false);\r
-       }\r
-       \r
-       public static void setRepositoryUseDocs(Repository r, boolean value) {\r
-               setRepositoryConfigBoolean(r, "useDocs", value);\r
-       }\r
-       \r
-       public static boolean getRepositoryRestrictedAccess(Repository r) {\r
-               return getRepositoryConfigBoolean(r, "restrictedAccess", false);\r
-       }\r
-       \r
-       public static void setRepositoryRestrictedAccess(Repository r, boolean value) {\r
-               setRepositoryConfigBoolean(r, "restrictedAccess", value);\r
-       }       \r
-       \r
-       public static String getRepositoryConfigString(Repository r, String field) {\r
-               StoredConfig c = readConfig(r);\r
-               if (c == null) {\r
-                       return "";\r
-               }\r
-               String o = c.getString("gitblit", null, field);\r
-               return o == null ? "" : o;\r
-       }\r
-       \r
-       public static void setRepositoryConfigString(Repository r, String field, String value) {\r
-               StoredConfig c = readConfig(r);\r
-               if (c == null) {\r
-                       throw new RuntimeException("Can't find stored config for " + r);\r
-               }\r
-               c.setString("gitblit", null, field, value);\r
-               try {\r
-                       c.save();\r
-               } catch (IOException e) {\r
-                       LOGGER.error("Failed to save repository config field " + field, e);\r
-               }\r
-       }\r
-       \r
-       public static boolean getRepositoryConfigBoolean(Repository r, String field, boolean defaultValue) {\r
-               StoredConfig c = readConfig(r);\r
-               if (c == null) {\r
-                       return defaultValue;\r
-               }\r
-               return c.getBoolean("gitblit", null, field, defaultValue);              \r
-       }\r
-       \r
-       public static void setRepositoryConfigBoolean(Repository r, String field, boolean value) {\r
-               StoredConfig c = readConfig(r);\r
-               if (c == null) {\r
-                       throw new RuntimeException("Can't find stored config for " + r);\r
-               }\r
-               c.setBoolean("gitblit", null, field, value);\r
-               try {\r
-                       c.save();\r
-               } catch (IOException e) {\r
-                       LOGGER.error("Failed to save repository config field " + field, e);\r
-               }               \r
-       }\r
-\r
-       private static StoredConfig readConfig(Repository r) {\r
+       public static StoredConfig readConfig(Repository r) {\r
                StoredConfig c = r.getConfig();\r
                if (c != null) {\r
                        try {\r
index ff9fac4ff6a355c3f71a21ead3138ca612d1bb65..d52be0e48640599c190b267d69a65c7452965271 100644 (file)
@@ -1,6 +1,7 @@
 package com.gitblit.utils;\r
 \r
 import java.io.IOException;\r
+import java.io.Reader;\r
 import java.io.StringReader;\r
 import java.io.StringWriter;\r
 import java.io.UnsupportedEncodingException;\r
@@ -131,4 +132,25 @@ public class StringUtils {
                        }\r
                }\r
        }\r
+       \r
+       public static String transformMarkdown(Reader markdownReader) throws java.text.ParseException {\r
+               // Read raw markdown content and transform it to html                           \r
+               StringWriter writer = new StringWriter();\r
+               try {\r
+                       Markdown md = new Markdown();\r
+                       md.transform(markdownReader, writer);\r
+                       return writer.toString();\r
+               } catch (ParseException p) {                    \r
+                       throw new java.text.ParseException(p.getMessage(), 0);\r
+               } finally {\r
+                       try {\r
+                               markdownReader.close();\r
+                       } catch (IOException e) {\r
+                       }\r
+                       try {\r
+                               writer.close();\r
+                       } catch (IOException e) {\r
+                       }\r
+               }\r
+       }\r
 }\r
index 28cdcd037cf1dde6285ed06ecdf118c6a3fda6a7..ba3ca6926d7d15ba42fba0dfa5fbe18e22e4772a 100644 (file)
@@ -31,6 +31,7 @@ import com.gitblit.Keys;
 import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.utils.JGitUtils.SearchType;\r
 import com.gitblit.utils.StringUtils;\r
+import com.gitblit.wicket.models.RepositoryModel;\r
 import com.gitblit.wicket.pages.BranchesPage;\r
 import com.gitblit.wicket.pages.DocsPage;\r
 import com.gitblit.wicket.pages.LogPage;\r
@@ -45,10 +46,11 @@ import com.gitblit.wicket.panels.RefsPanel;
 public abstract class RepositoryPage extends BasePage {\r
 \r
        protected final String repositoryName;\r
-       protected final String objectId;\r
-       protected String description;\r
+       protected final String objectId;        \r
 \r
        private transient Repository r = null;\r
+       \r
+       private RepositoryModel m = null;\r
 \r
        private final Logger logger = LoggerFactory.getLogger(RepositoryPage.class);\r
 \r
@@ -88,14 +90,12 @@ public abstract class RepositoryPage extends BasePage {
                List<String> extraPageLinks = new ArrayList<String>();\r
 \r
                // Conditionally add tickets page\r
-               boolean checkTickets = JGitUtils.getRepositoryUseTickets(r);\r
-               if (checkTickets && JGitUtils.getTicketsBranch(r) != null) {\r
+               if (getRepositoryModel().useTickets && JGitUtils.getTicketsBranch(r) != null) {\r
                        extraPageLinks.add("tickets");\r
                }\r
 \r
                // Conditionally add docs page\r
-               boolean checkDocs = JGitUtils.getRepositoryUseDocs(r);\r
-               if (checkDocs) {\r
+               if (getRepositoryModel().useDocs) {\r
                        extraPageLinks.add("docs");\r
                }\r
 \r
@@ -150,11 +150,17 @@ public abstract class RepositoryPage extends BasePage {
                                redirectToInterceptPage(new RepositoriesPage());\r
                                return null;\r
                        }\r
-                       description = JGitUtils.getRepositoryDescription(r);\r
                        this.r = r;\r
                }\r
                return r;\r
        }\r
+       \r
+       protected RepositoryModel getRepositoryModel() {\r
+               if (m == null) {\r
+                       m = GitBlit.self().getRepositoryModel(repositoryName);\r
+               }\r
+               return m;\r
+       }\r
 \r
        protected void addRefs(Repository r, RevCommit c) {\r
                add(new RefsPanel("refsPanel", repositoryName, c, JGitUtils.getAllRefs(r)));\r
index 8df3808351b91c7b9c986c8ba064c8d474fda0ba..8840f070a51f6bc93c302c6bd90bb4c4c2dd2aee 100644 (file)
@@ -15,6 +15,10 @@ public class RepositoryModel implements Serializable {
        public boolean useDocs;\r
        public boolean useRestrictedAccess;\r
 \r
+       public RepositoryModel() {\r
+       \r
+       }\r
+       \r
        public RepositoryModel(String name, String description, String owner, Date lastchange) {\r
                this.name = name;\r
                this.description = description;\r
index e2905683bfeee4cf141ec1ce9e8b58a75c432b45..6201b22b8e154da55aab9467b4545630ca5ef0d6 100644 (file)
@@ -7,10 +7,8 @@ import org.apache.wicket.markup.html.form.CheckBox;
 import org.apache.wicket.markup.html.form.Form;\r
 import org.apache.wicket.markup.html.form.TextField;\r
 import org.apache.wicket.model.CompoundPropertyModel;\r
-import org.eclipse.jgit.lib.Repository;\r
 \r
 import com.gitblit.GitBlit;\r
-import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.wicket.AdminPage;\r
 import com.gitblit.wicket.BasePage;\r
 import com.gitblit.wicket.WicketUtils;\r
@@ -33,36 +31,30 @@ public class EditRepositoryPage extends BasePage {
                super(params);\r
                isCreate = false;\r
                String name = WicketUtils.getRepositoryName(params);\r
-               Repository r = GitBlit.self().getRepository(name);\r
-               String description = JGitUtils.getRepositoryDescription(r);\r
-               String owner = JGitUtils.getRepositoryOwner(r);\r
-               String group = JGitUtils.getRepositoryGroup(r);\r
-               RepositoryModel model = new RepositoryModel(name, description, owner, new Date());\r
-               model.group = group;\r
-               model.useTickets = JGitUtils.getRepositoryUseTickets(r);\r
-               model.useDocs = JGitUtils.getRepositoryUseDocs(r);\r
-               model.useRestrictedAccess = JGitUtils.getRepositoryRestrictedAccess(r);\r
+               RepositoryModel model = GitBlit.self().getRepositoryModel(name);\r
                setupPage(model);\r
        }\r
 \r
-       protected void setupPage(final RepositoryModel repository) {\r
+       protected void setupPage(final RepositoryModel repositoryModel) {\r
                if (isCreate) {\r
                        super.setupPage("", getString("gb.newRepository"));\r
                } else {\r
                        super.setupPage("", getString("gb.edit"));\r
                }\r
-               CompoundPropertyModel<RepositoryModel> model = new CompoundPropertyModel<RepositoryModel>(repository);\r
+               CompoundPropertyModel<RepositoryModel> model = new CompoundPropertyModel<RepositoryModel>(repositoryModel);\r
                Form<RepositoryModel> form = new Form<RepositoryModel>("editForm", model) {\r
 \r
                        private static final long serialVersionUID = 1L;\r
 \r
                        @Override\r
                        protected void onSubmit() {\r
-                               GitBlit.self().editRepository(repository, isCreate);\r
+                               GitBlit.self().editRepositoryModel(repositoryModel, isCreate);\r
                                setRedirect(true);\r
                                setResponsePage(RepositoriesPage.class);\r
                        }\r
                };\r
+               \r
+               // field names reflective match RepositoryModel fields\r
                form.add(new TextField<String>("name").setEnabled(isCreate));\r
                form.add(new TextField<String>("description"));\r
                form.add(new TextField<String>("owner"));\r
index a7ec963ac3d58875760b78d8d566c7867566cb0c..c834604d6e26c6c1255636b6545e69fa1887d875 100644 (file)
@@ -4,7 +4,6 @@ import java.io.File;
 import java.io.FileReader;\r
 import java.io.InputStream;\r
 import java.io.InputStreamReader;\r
-import java.io.StringWriter;\r
 import java.util.Collections;\r
 import java.util.Comparator;\r
 import java.util.Iterator;\r
@@ -23,7 +22,6 @@ import org.apache.wicket.markup.repeater.data.DataView;
 import org.apache.wicket.model.IModel;\r
 import org.apache.wicket.model.Model;\r
 import org.apache.wicket.resource.ContextRelativeResource;\r
-import org.tautua.markdownpapers.Markdown;\r
 \r
 import com.gitblit.GitBlit;\r
 import com.gitblit.Keys;\r
@@ -63,12 +61,7 @@ public class RepositoriesPage extends BasePage {
                                ContextRelativeResource res = WicketUtils.getResource("welcome.mkd");\r
                                InputStream is = res.getResourceStream().getInputStream();\r
                                InputStreamReader reader = new InputStreamReader(is);\r
-                               StringWriter writer = new StringWriter();\r
-                               Markdown markdown = new Markdown();\r
-                               markdown.transform(reader, writer);\r
-                               message = writer.toString().trim();\r
-                               reader.close();\r
-                               writer.close();\r
+                               message = StringUtils.transformMarkdown(reader);\r
                        } catch (Throwable t) {\r
                                message = "Failed to read default welcome message!";\r
                                error(message, t);\r
@@ -80,12 +73,7 @@ public class RepositoriesPage extends BasePage {
                                if (file.exists()) {\r
                                        try {\r
                                                FileReader reader = new FileReader(file);\r
-                                               StringWriter writer = new StringWriter();\r
-                                               Markdown markdown = new Markdown();\r
-                                               markdown.transform(reader, writer);\r
-                                               message = writer.toString().trim();\r
-                                               reader.close();\r
-                                               writer.close();\r
+                                               message = StringUtils.transformMarkdown(reader);\r
                                        } catch (Throwable t) {\r
                                                message = "Failed to read " + file;\r
                                                error(message, t);\r
index 0c4c59536a8809b5cfbc4569208a4c3618c7f47c..78a58f771d8540de16aabb2e72009a052faa2b3b 100644 (file)
@@ -57,8 +57,8 @@ public class SummaryPage extends RepositoryPage {
                }\r
 \r
                // repository description\r
-               add(new Label("repositoryDescription", description));\r
-               add(new Label("repositoryOwner", JGitUtils.getRepositoryOwner(r)));\r
+               add(new Label("repositoryDescription", getRepositoryModel().description));\r
+               add(new Label("repositoryOwner", getRepositoryModel().owner));\r
 \r
                add(WicketUtils.createTimestampLabel("repositoryLastChange", JGitUtils.getLastChange(r), getTimeZone()));\r
                if (metricsTotal == null) {\r