]> source.dussan.org Git - gitblit.git/commitdiff
Improve repo folder detection. Fail gracefully on create repo error.
authorJames Moger <james.moger@gitblit.com>
Thu, 28 Apr 2011 01:56:56 +0000 (21:56 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 28 Apr 2011 01:56:56 +0000 (21:56 -0400)
src/com/gitblit/GitBlit.java
src/com/gitblit/GitBlitException.java [new file with mode: 0644]
src/com/gitblit/utils/JGitUtils.java
src/com/gitblit/wicket/BasePage.html
src/com/gitblit/wicket/pages/EditRepositoryPage.java
src/com/gitblit/wicket/pages/RepositoriesPage.java
src/com/gitblit/wicket/resources/bullet_error.png [new file with mode: 0644]
src/com/gitblit/wicket/resources/gitblit.css

index a626be845a8e170df03a36ccd72e21fec34fb5be..e557c934876cf976b62d944614df4dca0b19434b 100644 (file)
@@ -2,6 +2,7 @@ package com.gitblit;
 \r
 import java.io.File;\r
 import java.io.IOException;\r
+import java.text.MessageFormat;\r
 import java.util.ArrayList;\r
 import java.util.List;\r
 \r
@@ -30,7 +31,7 @@ public class GitBlit implements ServletContextListener {
 \r
        private FileResolver<Void> repositoryResolver;\r
 \r
-       private File repositories;\r
+       private File repositoriesFolder;\r
 \r
        private boolean exportAll;\r
 \r
@@ -93,52 +94,9 @@ public class GitBlit implements ServletContextListener {
                userCookie.setPath("/");\r
                response.addCookie(userCookie);\r
        }\r
-\r
-       public void editRepositoryModel(RepositoryModel repository, boolean isCreate) {\r
-               Repository r = null;\r
-               if (isCreate) {\r
-                       // create repository\r
-                       logger.info("create repository " + repository.name);\r
-                       r = JGitUtils.createRepository(repositories, repository.name, true);\r
-               } else {\r
-                       // load repository\r
-                       logger.info("edit repository " + repository.name);\r
-                       try {\r
-                               r = repositoryResolver.open(null, repository.name);\r
-                       } catch (RepositoryNotFoundException e) {\r
-                               logger.error("Repository not found", e);\r
-                       } catch (ServiceNotEnabledException e) {\r
-                               logger.error("Service not enabled", e);\r
-                       }\r
-               }\r
-\r
-               // update settings\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
+       \r
        public List<String> getRepositoryList() {\r
-               return JGitUtils.getRepositoryList(repositories, exportAll, storedSettings.getBoolean(Keys.git.nestedRepositories, true));\r
-       }\r
-\r
-       public List<RepositoryModel> getRepositories() {\r
-               List<String> list = getRepositoryList();\r
-               List<RepositoryModel> repositories = new ArrayList<RepositoryModel>();\r
-               for (String repo : list) {\r
-                       RepositoryModel model = getRepositoryModel(repo);\r
-                       repositories.add(model);\r
-               }\r
-               return repositories;\r
+               return JGitUtils.getRepositoryList(repositoriesFolder, exportAll, storedSettings.getBoolean(Keys.git.nestedRepositories, true));\r
        }\r
 \r
        public Repository getRepository(String repositoryName) {\r
@@ -155,7 +113,17 @@ public class GitBlit implements ServletContextListener {
                }\r
                return r;\r
        }\r
-\r
+       \r
+       public List<RepositoryModel> getRepositoryModels() {\r
+               List<String> list = getRepositoryList();\r
+               List<RepositoryModel> repositories = new ArrayList<RepositoryModel>();\r
+               for (String repo : list) {\r
+                       RepositoryModel model = getRepositoryModel(repo);\r
+                       repositories.add(model);\r
+               }\r
+               return repositories;\r
+       }\r
+       \r
        public RepositoryModel getRepositoryModel(String repositoryName) {\r
                Repository r = getRepository(repositoryName);\r
                RepositoryModel model = new RepositoryModel();\r
@@ -174,12 +142,48 @@ public class GitBlit implements ServletContextListener {
                return model;\r
        }\r
 \r
+       public void editRepositoryModel(RepositoryModel repository, boolean isCreate) throws GitBlitException {\r
+               Repository r = null;\r
+               if (isCreate) {\r
+                       if (new File(repositoriesFolder, repository.name).exists()) {\r
+                               throw new GitBlitException(MessageFormat.format("Can not create repository {0} because it already exists.", repository.name));\r
+                       }\r
+                       // create repository                    \r
+                       logger.info("create repository " + repository.name);\r
+                       r = JGitUtils.createRepository(repositoriesFolder, repository.name, true);\r
+               } else {\r
+                       // load repository\r
+                       logger.info("edit repository " + repository.name);\r
+                       try {\r
+                               r = repositoryResolver.open(null, repository.name);\r
+                       } catch (RepositoryNotFoundException e) {\r
+                               logger.error("Repository not found", e);\r
+                       } catch (ServiceNotEnabledException e) {\r
+                               logger.error("Service not enabled", e);\r
+                       }\r
+               }\r
+\r
+               // update settings\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 void setupContext(IStoredSettings settings) {\r
                logger.info("Setting up GitBlit context from " + settings.toString());\r
                this.storedSettings = settings;\r
-               repositories = new File(settings.getString(Keys.git.repositoriesFolder, "repos"));\r
+               repositoriesFolder = new File(settings.getString(Keys.git.repositoriesFolder, "repos"));\r
                exportAll = settings.getBoolean(Keys.git.exportAll, true);\r
-               repositoryResolver = new FileResolver(repositories, exportAll);\r
+               repositoryResolver = new FileResolver<Void>(repositoriesFolder, exportAll);\r
        }\r
 \r
        @Override\r
diff --git a/src/com/gitblit/GitBlitException.java b/src/com/gitblit/GitBlitException.java
new file mode 100644 (file)
index 0000000..15e38e1
--- /dev/null
@@ -0,0 +1,10 @@
+package com.gitblit;\r
+\r
+public class GitBlitException extends Exception {\r
+\r
+       private static final long serialVersionUID = 1L;\r
+\r
+       public GitBlitException(String message) {\r
+               super(message);\r
+       }\r
+}\r
index 4cfed0da51b062686ceed57d0a6a80408ee92e97..70a938ff124ce6757c74603aaa641a7a330c9900 100644 (file)
@@ -94,9 +94,10 @@ public class JGitUtils {
                                File gitFolder = new File(file, Constants.DOT_GIT);\r
                                boolean isGitRepository = gitFolder.exists() && gitFolder.isDirectory();\r
                                \r
-                               // then look for folder.git/HEAD\r
+                               // then look for folder.git/HEAD or folder/HEAD and folder/config\r
                                if (!isGitRepository) {\r
-                                       if (file.getName().endsWith(Constants.DOT_GIT_EXT) && new File(file, Constants.HEAD).exists()) {\r
+                                       if ((file.getName().endsWith(Constants.DOT_GIT_EXT) && new File(file, Constants.HEAD).exists())\r
+                                                       || (new File(file, "config").exists() && new File(file, Constants.HEAD).exists())) {\r
                                                gitFolder = file;\r
                                                isGitRepository = true;\r
                                        }\r
@@ -266,13 +267,13 @@ public class JGitUtils {
 \r
        public static String getRawContentAsString(Repository r, RevBlob blob) {\r
                byte [] content = getRawContent(r, blob);\r
-               return new String(content, Charset.forName("UTF-8"));\r
+               return new String(content, Charset.forName(Constants.CHARACTER_ENCODING));\r
        }\r
 \r
        public static String getRawContentAsString(Repository r, RevCommit commit, String blobPath) {\r
                RevObject obj = getRevObject(r, commit.getTree(), blobPath);\r
                byte [] content = getRawContent(r, (RevBlob) obj);\r
-               return new String(content, Charset.forName("UTF-8"));\r
+               return new String(content, Charset.forName(Constants.CHARACTER_ENCODING));\r
        }\r
 \r
        public static List<PathModel> getFilesInPath(Repository r, String basePath, String objectId) {\r
index b88192b562fc599ce9cbafd87f1628d541aad5b0..69183b686462f5d95e80d41efdffcb23ba3a291b 100644 (file)
                                        <img src="resources/gitblt_25.png" width="79" height="25" alt="gitblit" class="logo"/>\r
                                </wicket:link>\r
                        </a>\r
-                       <a href="/"><span wicket:id="siteName">[site name]</span></a> / <span wicket:id="repositoryName">[repository name]</span> <span wicket:id="pageName">[page name]</span>\r
-                       <div style="float:none;" wicket:id="feedback">[Feedback Panel]</div> \r
+                       <span>\r
+                               <a href="/"><span wicket:id="siteName">[site name]</span></a> / <span wicket:id="repositoryName">[repository name]</span> <span wicket:id="pageName">[page name]</span>\r
+                       </span> \r
+                       <span wicket:id="feedback">[Feedback Panel]</span>\r
                </div>\r
 \r
                <!-- page content -->\r
index 6201b22b8e154da55aab9467b4545630ca5ef0d6..45fe9e2ed3cf77f0b309260e78d7d1e2021569af 100644 (file)
@@ -9,6 +9,7 @@ import org.apache.wicket.markup.html.form.TextField;
 import org.apache.wicket.model.CompoundPropertyModel;\r
 \r
 import com.gitblit.GitBlit;\r
+import com.gitblit.GitBlitException;\r
 import com.gitblit.wicket.AdminPage;\r
 import com.gitblit.wicket.BasePage;\r
 import com.gitblit.wicket.WicketUtils;\r
@@ -48,7 +49,12 @@ public class EditRepositoryPage extends BasePage {
 \r
                        @Override\r
                        protected void onSubmit() {\r
-                               GitBlit.self().editRepositoryModel(repositoryModel, isCreate);\r
+                               try {\r
+                                       GitBlit.self().editRepositoryModel(repositoryModel, isCreate);\r
+                               } catch (GitBlitException e) {\r
+                                       error(e.getMessage());\r
+                                       return;\r
+                               }\r
                                setRedirect(true);\r
                                setResponsePage(RepositoriesPage.class);\r
                        }\r
index c834604d6e26c6c1255636b6545e69fa1887d875..768cea148cf3d21fd9ddc4e74e27ae3e29300b0d 100644 (file)
@@ -89,7 +89,7 @@ public class RepositoriesPage extends BasePage {
                }\r
                add(repositoriesMessage);\r
 \r
-               List<RepositoryModel> rows = GitBlit.self().getRepositories();\r
+               List<RepositoryModel> rows = GitBlit.self().getRepositoryModels();\r
                DataProvider dp = new DataProvider(rows);\r
                DataView<RepositoryModel> dataView = new DataView<RepositoryModel>("repository", dp) {\r
                        private static final long serialVersionUID = 1L;\r
diff --git a/src/com/gitblit/wicket/resources/bullet_error.png b/src/com/gitblit/wicket/resources/bullet_error.png
new file mode 100644 (file)
index 0000000..b2e6b75
Binary files /dev/null and b/src/com/gitblit/wicket/resources/bullet_error.png differ
index 53845466545c674a3ce65743c3e9a50dd59dde80..8317e363a5d9265a380ba9e1326422056670238d 100644 (file)
@@ -628,4 +628,23 @@ span .tagRef {
 span .headRef {\r
        background-color: #ccffcc;\r
        border-color: #ccffcc #00cc33 #00cc33 #ccffcc;\r
+}\r
+\r
+.feedbackPanelERROR {  \r
+       color: red;\r
+       list-style-image: url(bullet_error.png);\r
+       font-weight: bold;\r
+       text-align:left;\r
+       vertical-align: top;\r
+       padding:0;      \r
+       margin:0;       \r
+}\r
+\r
+.feedbackPanelINFO {\r
+       color: green;\r
+       list-style: none;\r
+       font-weight: bold;\r
+       text-align:center;\r
+       padding:0;\r
+       margin:0;\r
 }
\ No newline at end of file