The Unified and Autonomous Development Platform
Source page
steveded5c20b49c1
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceBlobPage.html b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceBlobPage.htmlindex fbadeac373..0fb3803385 100644--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceBlobPage.html+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceBlobPage.html@@ -1,8 +1,8 @@<html xmlns:wicket><wicket:extend>- <div wicket:id="breadcrumb"></div>- <div>- <h1>This is the file content</h1>+ <div class="padder">+ <div wicket:id="breadcrumb"></div>+ <h2>This is the file content</h2></div></wicket:extend></html>\ No newline at end of filediff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceBlobPage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceBlobPage.javaindex c3aaa9f6c3..8986f5db25 100644--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceBlobPage.java+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceBlobPage.java@@ -2,6 +2,8 @@ package com.pmease.gitop.web.page.project.source;import org.apache.wicket.request.mapper.parameter.PageParameters;+import com.pmease.gitop.web.page.project.source.component.SourceBreadcrumbPanel;+@SuppressWarnings("serial")public class SourceBlobPage extends AbstractFilePage {@@ -9,8 +11,15 @@ public class SourceBlobPage extends AbstractFilePage {super(params);}+ @Override+ public void onPageInitialize() {+ super.onPageInitialize();++ add(new SourceBreadcrumbPanel("breadcrumb", projectModel, revisionModel, pathsModel));+ }+@Overrideprotected String getPageTitle() {- return null;+ return "";}}diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceTreePage.html b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceTreePage.htmlindex 6c901d855b..71dd5ad0d3 100644--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceTreePage.html+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceTreePage.html@@ -1,10 +1,13 @@<html xmlns:wicket><wicket:extend><div class="padder">- <div wicket:id="description"></div>- <div wicket:id="breadcrumb"></div>- <div wicket:id="tree"></div>- <div wicket:id="readme"></div>+ <div wicket:id="empty"></div>+ <wicket:enclosure child="tree">+ <div wicket:id="description"></div>+ <div wicket:id="breadcrumb"></div>+ <div wicket:id="tree"></div>+ <div wicket:id="readme"></div>+ </wicket:enclosure></div></wicket:extend></html>\ No newline at end of filediff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceTreePage.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceTreePage.javaindex 5cf8c76622..1e1b214eb1 100644--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceTreePage.java+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/SourceTreePage.java@@ -1,16 +1,23 @@package com.pmease.gitop.web.page.project.source;+import java.io.File;import java.util.List;+import org.apache.wicket.markup.html.WebMarkupContainer;import org.apache.wicket.request.mapper.parameter.PageParameters;import com.google.common.base.Joiner;+import com.pmease.gitop.core.Gitop;+import com.pmease.gitop.core.manager.StorageManager;import com.pmease.gitop.core.model.Project;+import com.pmease.gitop.web.model.ProjectModel;import com.pmease.gitop.web.page.PageSpec;+import com.pmease.gitop.web.page.project.source.component.EmptyRepositoryPanel;import com.pmease.gitop.web.page.project.source.component.ProjectDescriptionPanel;import com.pmease.gitop.web.page.project.source.component.ReadmePanel;import com.pmease.gitop.web.page.project.source.component.SourceBreadcrumbPanel;import com.pmease.gitop.web.page.project.source.component.SourceTreePanel;+import com.pmease.gitop.web.util.GitUtils;@SuppressWarnings("serial")public class SourceTreePage extends AbstractFilePage {@@ -35,10 +42,18 @@ public class SourceTreePage extends AbstractFilePage {protected void onPageInitialize() {super.onPageInitialize();- add(new ProjectDescriptionPanel("description", projectModel).setVisibilityAllowed(getPaths().isEmpty()));- add(new SourceBreadcrumbPanel("breadcrumb", projectModel, revisionModel, pathsModel));- add(new SourceTreePanel("tree", projectModel, revisionModel, pathsModel));- add(new ReadmePanel("readme", projectModel, revisionModel, pathsModel));+ File gitDir = Gitop.getInstance(StorageManager.class).getStorage(getProject()).ofCode();++ if (GitUtils.hasCommits(gitDir)) {+ add(new ProjectDescriptionPanel("description", projectModel).setVisibilityAllowed(getPaths().isEmpty()));+ add(new SourceBreadcrumbPanel("breadcrumb", projectModel, revisionModel, pathsModel));+ add(new SourceTreePanel("tree", projectModel, revisionModel, pathsModel));+ add(new ReadmePanel("readme", projectModel, revisionModel, pathsModel));+ add(new WebMarkupContainer("empty").setVisibilityAllowed(false));+ } else {+ add(new EmptyRepositoryPanel("empty", new ProjectModel(getProject())));+ add(new WebMarkupContainer("tree").setVisibilityAllowed(false));+ }}@Overridediff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/EmptyRepositoryPanel.html b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/EmptyRepositoryPanel.htmlnew file mode 100644index 0000000000..2736a9a4c3--- /dev/null+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/EmptyRepositoryPanel.html@@ -0,0 +1,17 @@+<html xmlns:wicket>+<wicket:panel>+ <h3>Create a new repository?</h3>+<pre><code>touch README+git init+git add README+git commit -m "first commit"+git remote add origin <span wicket:id="new"></span>+git push -u origin master+</code></pre>++ <h3>Push an existing repository?</h3>+<pre><code>git remote add origin <span wicket:id="existing"></span>+git push -u origin master+</code></pre>+</wicket:panel>+</html>\ No newline at end of filediff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/EmptyRepositoryPanel.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/EmptyRepositoryPanel.javanew file mode 100644index 0000000000..9681c68ccb--- /dev/null+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/EmptyRepositoryPanel.java@@ -0,0 +1,29 @@+package com.pmease.gitop.web.page.project.source.component;++import org.apache.wicket.markup.html.basic.Label;+import org.apache.wicket.model.IModel;+import org.apache.wicket.model.Model;++import com.pmease.gitop.core.Gitop;+import com.pmease.gitop.core.model.Project;+import com.pmease.gitop.web.util.UrlUtils;++@SuppressWarnings("serial")+public class EmptyRepositoryPanel extends ProjectPanel {++ public EmptyRepositoryPanel(String id, IModel<Project> model) {+ super(id, model);+ }++ @Override+ protected void onInitialize() {+ super.onInitialize();+ add(new Label("new", Model.of(getRepoUrl())));+ add(new Label("existing", Model.of(getRepoUrl())));+ }++ private String getRepoUrl() {+ Project project = getProject();+ return UrlUtils.concatSegments(Gitop.getInstance().guessServerUrl(), project.getPathName() + ".git");+ }+}diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/SourceTreePanel.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/SourceTreePanel.javaindex 89a8e0c944..2c2daf220b 100644--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/SourceTreePanel.java+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/source/component/SourceTreePanel.java@@ -83,7 +83,7 @@ public class SourceTreePanel extends AbstractSourcePagePanel {BookmarkablePageLink<Void> commitLink = new BookmarkablePageLink<Void>("commitlink",SourceCommitPage.class,- SourceCommitPage.newParams(getProject(), getRevision()));+ SourceCommitPage.newParams(getProject(), getLastCommit().getHash()));add(commitLink);commitLink.add(new Label("sha", new AbstractReadOnlyModel<String>() {diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/util/GitUtils.java b/gitop.web/src/main/java/com/pmease/gitop/web/util/GitUtils.javaindex ab4f1412a7..465550eaf4 100644--- a/gitop.web/src/main/java/com/pmease/gitop/web/util/GitUtils.java+++ b/gitop.web/src/main/java/com/pmease/gitop/web/util/GitUtils.java@@ -1,5 +1,6 @@package com.pmease.gitop.web.util;+import java.io.File;import java.util.regex.Pattern;import org.apache.commons.lang3.StringUtils;@@ -28,4 +29,13 @@ public class GitUtils {public static String getCommitSummary(Commit commit) {return commit.getSummary();}++ public static boolean hasCommits(File gitDir) {+ if (gitDir.exists()) {+ return new File(gitDir, "objects").list().length > 2+ || new File(gitDir, "objects/pack").list().length > 0;+ }++ return false;+ }}diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/util/UrlUtils.java b/gitop.web/src/main/java/com/pmease/gitop/web/util/UrlUtils.javaindex 1a9732339f..3551f011c7 100644--- a/gitop.web/src/main/java/com/pmease/gitop/web/util/UrlUtils.java+++ b/gitop.web/src/main/java/com/pmease/gitop/web/util/UrlUtils.java@@ -2,68 +2,164 @@ package com.pmease.gitop.web.util;import java.io.IOException;import java.io.InputStream;+import java.io.UnsupportedEncodingException;import java.net.JarURLConnection;+import java.net.URI;+import java.net.URISyntaxException;import java.net.URL;import java.net.URLConnection;+import java.net.URLEncoder;+import java.util.List;import java.util.Set;import java.util.jar.JarEntry;+import java.util.regex.Pattern;++import javax.annotation.Nullable;++import org.apache.commons.lang3.StringUtils;++import com.google.common.base.Throwables;+import com.google.common.collect.Iterables;+import com.google.common.collect.Lists;public class UrlUtils {- private UrlUtils() {- }-- public static long getLastModified(Set<URL> fileset) {- long lastModified = 0;- for (URL each : fileset) {- lastModified = Math.max(lastModified, getLastModified(each));- }+ private UrlUtils() {+ }++ public static long getLastModified(Set<URL> fileset) {+ long lastModified = 0;+ for (URL each : fileset) {+ lastModified = Math.max(lastModified, getLastModified(each));+ }++ return lastModified;+ }- return lastModified;- }+ /**+ * Copied from dropwizard+ *+ * Returns the last modified time for file:// and jar:// URLs. This is+ * slightly tricky for a couple of reasons: 1) calling getConnection on a+ * {@link URLConnection} to a file opens an {@link InputStream} to that file+ * that must then be closed — though this is not true for+ * {@code URLConnection}s to jar resources 2) calling getLastModified on+ * {@link JarURLConnection}s returns the last modified time of the jar file,+ * rather than the file within+ *+ * @param resourceURL+ * the URL to return the last modified time for+ * @return the last modified time of the resource, expressed as the number+ * of milliseconds since the epoch, or 0 if there was a problem+ */+ public static long getLastModified(URL resourceURL) {+ final String protocol = resourceURL.getProtocol();+ switch (protocol) {+ case "jar":+ try {+ final JarURLConnection jarConnection = (JarURLConnection) resourceURL+ .openConnection();+ final JarEntry entry = jarConnection.getJarEntry();+ return entry.getTime();+ } catch (IOException ignored) {+ return 0;+ }+ case "file":+ URLConnection connection = null;+ try {+ connection = resourceURL.openConnection();+ return connection.getLastModified();+ } catch (IOException ignored) {+ return 0;+ } finally {+ if (connection != null) {+ try {+ connection.getInputStream().close();+ } catch (IOException ignored) {+ // do nothing.+ }+ }+ }+ default:+ throw new IllegalArgumentException("Unsupported protocol "+ + resourceURL.getProtocol() + " for resource "+ + resourceURL);+ }+ }++ public static String encodeUrl(final String segment) {+ try {+ return URLEncoder.encode(segment, "UTF-8");+ } catch (UnsupportedEncodingException e) {+ throw Throwables.propagate(e);+ }+ }++ /**+ * Concatenate url segments into one+ *+ * @param segments+ * @return+ */+ public static String concatSegments(String[] segments) {+ StringBuffer sb = new StringBuffer();+ for (int i = 0; i < segments.length; i++) {+ if (i > 0) {+ sb.append("/");+ }++ sb.append(StringUtils.stripEnd(segments[i], "/"));+ }++ return sb.toString();+ }- /**- * Copied from dropwizard- *- * Returns the last modified time for file:// and jar:// URLs. This is slightly tricky for a couple of reasons:- * 1) calling getConnection on a {@link URLConnection} to a file opens an {@link InputStream} to that file that must- * then be closed — though this is not true for {@code URLConnection}s to jar resources- * 2) calling getLastModified on {@link JarURLConnection}s returns the last modified time of the jar file, rather- * than the file within- *- * @param resourceURL the URL to return the last modified time for- * @return the last modified time of the resource, expressed as the number of milliseconds since the epoch, or 0 if there was a problem- */- public static long getLastModified(URL resourceURL) {- final String protocol = resourceURL.getProtocol();- switch (protocol) {- case "jar":- try {- final JarURLConnection jarConnection = (JarURLConnection) resourceURL.openConnection();- final JarEntry entry = jarConnection.getJarEntry();- return entry.getTime();- } catch (IOException ignored) {- return 0;- }- case "file":- URLConnection connection = null;- try {- connection = resourceURL.openConnection();- return connection.getLastModified();- } catch (IOException ignored) {- return 0;- } finally {- if (connection != null) {- try {- connection.getInputStream().close();- } catch (IOException ignored) {- // do nothing.- }- }- }- default:- throw new IllegalArgumentException("Unsupported protocol " + resourceURL.getProtocol()- + " for resource " + resourceURL);- }- }+ public static String concatSegments(String base, String... segments) {+ List<String> list = Lists.newArrayList();+ list.add(base);+ for (String each : segments) {+ list.add(each);+ }+ return concatSegments(Iterables.toArray(list, String.class));+ }++ private static final Pattern SLASHES = Pattern.compile("//+");++ /**+ * Reduces redundant slashes to a single slash.+ *+ * @param path+ * @return null or the reduced string+ */+ public static @Nullable String removeRedundantSlashes(@Nullable String path) {+ if (path == null) {+ return null;+ }++ return SLASHES.matcher(path).replaceAll("/");+ }++ /**+ * Trims any trailing slashes from a URL path (see {@link URI#getPath()}.+ *+ * @param uri a URI with a path component+ * @return the URI with any trailing slashes from the path removed+ * @throws IllegalArgumentException if the value is no longer a valid URI after the slashes have been trimmed+ */+ public static URI trimTrailingSlashesFromPath(URI uri) {+ String path = uri.getPath();+ if (path.endsWith("/")) {+ while (path.endsWith("/")) {+ path = path.substring(0, path.length() - 1);+ }++ try {+ return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), path, uri.getQuery(), uri.getFragment());+ } catch (URISyntaxException e) {+ throw Throwables.propagate(e);+ }+ }++ return uri;+ }}