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
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
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
} 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
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
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
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
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
}\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
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
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
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
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
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
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
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
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
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
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
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
}\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