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