]> source.dussan.org Git - gitblit.git/commitdiff
Allow administrative modification of the default branch/tag referenced by HEAD.
authorPhilip L. McMahon <philip.l.mcmahon@gmail.com>
Fri, 27 Jan 2012 05:20:21 +0000 (21:20 -0800)
committerPhilip L. McMahon <philip.l.mcmahon@gmail.com>
Fri, 27 Jan 2012 05:20:21 +0000 (21:20 -0800)
This allows control over the default branch after clone, which is equivalent to running:
  git symbolic-ref HEAD refs/heads/mybranch

src/com/gitblit/GitBlit.java
src/com/gitblit/models/RepositoryModel.java
src/com/gitblit/utils/JGitUtils.java
src/com/gitblit/wicket/GitBlitWebApp.properties
src/com/gitblit/wicket/pages/EditRepositoryPage.html
src/com/gitblit/wicket/pages/EditRepositoryPage.java

index bf3660d02e1049e4ddb27fc2bf4f7e88cfa04bb6..b6bf7bf930aa5cbe21e337e185ca55097f790a07 100644 (file)
@@ -69,6 +69,7 @@ import com.gitblit.models.FederationModel;
 import com.gitblit.models.FederationProposal;\r
 import com.gitblit.models.FederationSet;\r
 import com.gitblit.models.Metric;\r
+import com.gitblit.models.RefModel;\r
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.ServerSettings;\r
 import com.gitblit.models.ServerStatus;\r
@@ -786,6 +787,10 @@ public class GitBlit implements ServletContextListener {
                        model.mailingLists = new ArrayList<String>(Arrays.asList(config.getStringList(\r
                                        "gitblit", null, "mailingList")));\r
                }\r
+               model.defaultHead = JGitUtils.getDefaultHead(r);\r
+               model.availableHeads = new ArrayList<RefModel>();\r
+               model.availableHeads.addAll(JGitUtils.getLocalBranches(r, true, -1));\r
+               model.availableHeads.addAll(JGitUtils.getTags(r, true, -1));\r
                r.close();\r
                return model;\r
        }\r
@@ -981,6 +986,9 @@ public class GitBlit implements ServletContextListener {
                // update settings\r
                if (r != null) {\r
                        updateConfiguration(r, repository);\r
+                       if (repository.defaultHead != null) {\r
+                               JGitUtils.setDefaultHead(r, repository.defaultHead.reference);\r
+                       }\r
                        r.close();\r
                }\r
        }\r
index ad0adaa6bbfd3ae159dacd35e2d35792db5b67a7..d3e91588b0fc58b2ba60cd00d13cd17cc9bb1c67 100644 (file)
@@ -58,6 +58,8 @@ public class RepositoryModel implements Serializable, Comparable<RepositoryModel
        public List<String> preReceiveScripts;\r
        public List<String> postReceiveScripts;\r
        public List<String> mailingLists;\r
+       public RefModel defaultHead;\r
+       public List<RefModel> availableHeads;\r
 \r
        private String displayName;\r
        \r
index a540c2aa481b44d4e343f0a2d380b1fbdf329849..0750b07853f49a58e1512fc4ff735dd420b04a39 100644 (file)
@@ -1155,6 +1155,50 @@ public class JGitUtils {
                return object;\r
        }\r
 \r
+       /**\r
+        * Returns the default HEAD for a repository. Normally returns the ref HEAD points to, but if HEAD points to nothing\r
+        * it returns null.\r
+        *\r
+        * @param repository\r
+        * @return the refmodel for HEAD or null\r
+        */\r
+       public static RefModel getDefaultHead(Repository repository) {\r
+               RefModel ref = null;\r
+               try {\r
+                       Ref head = repository.getRef(Constants.HEAD);\r
+                       if (head != null) {\r
+                               Ref target = head.getTarget();\r
+                               RevWalk rw = new RevWalk(repository);\r
+                               ObjectId targetId = target.getObjectId();\r
+                               if (targetId != null) {\r
+                                       RevObject object = rw.parseAny(targetId);\r
+                                       ref = new RefModel(target.getName(), target, object);\r
+                               }\r
+                               rw.dispose();\r
+                       }\r
+               } catch (Throwable t) {\r
+                       LOGGER.error("Failed to get default head!", t);\r
+               }\r
+               return ref;\r
+       }\r
+\r
+       /**\r
+        * Sets the default HEAD symbolic ref for a repository.\r
+        *\r
+        * @param repository\r
+        * @param ref\r
+        */\r
+       public static void setDefaultHead(Repository repository, Ref ref) {\r
+               try {\r
+                       RefUpdate head = repository.updateRef(Constants.HEAD);\r
+                       RefUpdate.Result result = head.link(ref.getName());\r
+                       LOGGER.debug(MessageFormat.format("Set repository {0} default head to {1} ({2})",\r
+                                       repository.getDirectory().getAbsolutePath(), ref.getName(), result));\r
+               } catch (IOException e) {\r
+                       LOGGER.error("Failed to set default head!", e);\r
+               }\r
+       }\r
+\r
        /**\r
         * Returns all refs grouped by their associated object id.\r
         * \r
index 713fee701b64696ef4077b8d0df15391cfb4ca20..b6e45236445730e009630aa3aea0c4ca8646388e 100644 (file)
@@ -131,6 +131,8 @@ gb.registrations = federation registrations
 gb.sendProposal propose\r
 gb.status = status\r
 gb.origin = origin\r
+gb.defaultHead = default head\r
+gb.defaultHeadDescription = current branch after clone. e.g. refs/heads/master\r
 gb.federationStrategy = federation strategy\r
 gb.federationRegistration = federation registration\r
 gb.federationResults = federation pull results\r
index d282d103e7e4ce5fba3197127ce2ff42f690eacf..9878930401ed7897a01b1c19a81e57d2595e86a4 100644 (file)
@@ -14,6 +14,7 @@
                                <tr><th><wicket:message key="gb.name"></wicket:message></th><td class="edit"><input class="span6" type="text" wicket:id="name" id="name" size="40" tabindex="1" /> &nbsp;<span class="help-inline"><wicket:message key="gb.nameDescription"></wicket:message></span></td></tr>\r
                                <tr><th><wicket:message key="gb.description"></wicket:message></th><td class="edit"><input class="span6" type="text" wicket:id="description" size="40" tabindex="2" /></td></tr>\r
                                <tr><th><wicket:message key="gb.origin"></wicket:message></th><td class="edit"><input class="span7" type="text" wicket:id="origin" size="80" tabindex="3" /></td></tr>\r
+                               <tr><th><wicket:message key="gb.defaultHead"></wicket:message></th><td class="edit"><select wicket:id="defaultHead" /> &nbsp;<span class="help-inline"><wicket:message key="gb.defaultHeadDescription"></wicket:message></span></td></tr>\r
                                <tr><th><wicket:message key="gb.owner"></wicket:message></th><td class="edit"><select wicket:id="owner" tabindex="4" /> &nbsp;<span class="help-inline"><wicket:message key="gb.ownerDescription"></wicket:message></span></td></tr>\r
                                <tr><th><wicket:message key="gb.enableTickets"></wicket:message></th><td class="edit"><input type="checkbox" wicket:id="useTickets" tabindex="5" /> &nbsp;<span class="help-inline"><wicket:message key="gb.useTicketsDescription"></wicket:message></span></td></tr>\r
                                <tr><th><wicket:message key="gb.enableDocs"></wicket:message></th><td class="edit"><input type="checkbox" wicket:id="useDocs" tabindex="6" /> &nbsp;<span class="help-inline"><wicket:message key="gb.useDocsDescription"></wicket:message></span></td></tr>\r
index 7d2d64c8092a7b1fc5d816ecdcddeed53fe5066b..6c042497676ac3c1a062cc4ad9fb4ed33798e623 100644 (file)
@@ -45,6 +45,7 @@ import com.gitblit.Constants.FederationStrategy;
 import com.gitblit.GitBlit;\r
 import com.gitblit.GitBlitException;\r
 import com.gitblit.Keys;\r
+import com.gitblit.models.RefModel;\r
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.UserModel;\r
 import com.gitblit.utils.ArrayUtils;\r
@@ -271,6 +272,9 @@ public class EditRepositoryPage extends RootSubPage {
                form.add(new CheckBox("isFrozen"));\r
                // TODO enable origin definition\r
                form.add(new TextField<String>("origin").setEnabled(false/* isCreate */));\r
+               // enable alteration of the default branch after clone\r
+               form.add(new DropDownChoice<RefModel>("defaultHead", repositoryModel.availableHeads,\r
+                               new RefModelRenderer()).setEnabled(GitBlitWebSession.get().canAdmin()));\r
 \r
                // federation strategies - remove ORIGIN choice if this repository has\r
                // no origin.\r
@@ -361,6 +365,21 @@ public class EditRepositoryPage extends RootSubPage {
                }\r
        }\r
 \r
+       private class RefModelRenderer implements IChoiceRenderer<RefModel> {\r
+\r
+               private static final long serialVersionUID = 1L;\r
+\r
+               @Override\r
+               public String getDisplayValue(RefModel type) {\r
+                       return type.displayName;\r
+               }\r
+\r
+               @Override\r
+               public String getIdValue(RefModel type, int index) {\r
+                       return type.getName();\r
+               }\r
+       }\r
+\r
        private class AccessRestrictionRenderer implements IChoiceRenderer<AccessRestrictionType> {\r
 \r
                private static final long serialVersionUID = 1L;\r