]> source.dussan.org Git - gitblit.git/commitdiff
Refactored and unit tested updating HEAD feature
authorJames Moger <james.moger@gitblit.com>
Fri, 3 Feb 2012 01:02:40 +0000 (20:02 -0500)
committerJames Moger <james.moger@gitblit.com>
Fri, 3 Feb 2012 01:02:40 +0000 (20:02 -0500)
src/com/gitblit/GitBlit.java
src/com/gitblit/client/EditRepositoryDialog.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
tests/com/gitblit/tests/JGitUtilsTest.java

index 9712ea6af087b39797a42d00bb7526e3d1a1920b..a689b48ee63b4770ec069012a10c280e0d92f59d 100644 (file)
@@ -786,8 +786,8 @@ public class GitBlit implements ServletContextListener {
                        model.mailingLists = new ArrayList<String>(Arrays.asList(config.getStringList(\r
                                        "gitblit", null, "mailingList")));\r
                }\r
-               model.defaultHead = JGitUtils.getSymbolicHeadTarget(r);\r
-               model.availableHeads = JGitUtils.getAvailableHeadTargets(r);\r
+               model.HEAD = JGitUtils.getHEADRef(r);\r
+               model.availableRefs = JGitUtils.getAvailableHeadTargets(r);\r
                r.close();\r
                return model;\r
        }\r
@@ -984,15 +984,14 @@ public class GitBlit implements ServletContextListener {
                if (r != null) {\r
                        updateConfiguration(r, repository);\r
                        // only update symbolic head if it changes\r
-                       String currentHead = JGitUtils.getSymbolicHeadTarget(r);\r
-                       if (!StringUtils.isEmpty(repository.defaultHead)  &&\r
-                                       !repository.defaultHead.equals(currentHead)) {\r
+                       String currentRef = JGitUtils.getHEADRef(r);\r
+                       if (!StringUtils.isEmpty(repository.HEAD) && !repository.HEAD.equals(currentRef)) {\r
                                logger.info(MessageFormat.format("Relinking {0} HEAD from {1} to {2}", \r
-                                               repository.name, currentHead, repository.defaultHead));\r
-                               JGitUtils.setSymbolicHeadTarget(r, repository.defaultHead);\r
-\r
-                               // clear the cache\r
-                               clearRepositoryCache(repository.name);\r
+                                               repository.name, currentRef, repository.HEAD));\r
+                               if (JGitUtils.setHEADtoRef(r, repository.HEAD)) {\r
+                                       // clear the cache\r
+                                       clearRepositoryCache(repository.name);\r
+                               }\r
                        }\r
 \r
                        // close the repository object\r
index eaf7e0cabd91f806202805ba0ec8cf2ea56dca31..4d3485dfa85e2e46461c09c4de68f3a008064859 100644 (file)
@@ -98,7 +98,7 @@ public class EditRepositoryDialog extends JDialog {
 \r
        private JComboBox ownerField;\r
 \r
-       private JComboBox defaultHeadField;\r
+       private JComboBox headRefField;\r
 \r
        private JPalette<String> usersPalette;\r
 \r
@@ -158,13 +158,13 @@ public class EditRepositoryDialog extends JDialog {
                                anRepository.origin == null ? "" : anRepository.origin, 40);\r
                originField.setEditable(false);\r
 \r
-               if (ArrayUtils.isEmpty(anRepository.availableHeads)) {\r
-                       defaultHeadField = new JComboBox();\r
-                       defaultHeadField.setEnabled(false);                     \r
+               if (ArrayUtils.isEmpty(anRepository.availableRefs)) {\r
+                       headRefField = new JComboBox();\r
+                       headRefField.setEnabled(false);                 \r
                } else {\r
-                       defaultHeadField = new JComboBox(\r
-                                       anRepository.availableHeads.toArray());\r
-                       defaultHeadField.setSelectedItem(anRepository.defaultHead);\r
+                       headRefField = new JComboBox(\r
+                                       anRepository.availableRefs.toArray());\r
+                       headRefField.setSelectedItem(anRepository.HEAD);\r
                }\r
 \r
                ownerField = new JComboBox();\r
@@ -213,8 +213,7 @@ public class EditRepositoryDialog extends JDialog {
                                descriptionField));\r
                fieldsPanel\r
                                .add(newFieldPanel(Translation.get("gb.origin"), originField));\r
-               fieldsPanel.add(newFieldPanel(Translation.get("gb.defaultHead"),\r
-                               defaultHeadField));\r
+               fieldsPanel.add(newFieldPanel(Translation.get("gb.head"), headRefField));\r
                fieldsPanel.add(newFieldPanel(Translation.get("gb.owner"), ownerField));\r
 \r
                fieldsPanel.add(newFieldPanel(Translation.get("gb.enableTickets"),\r
@@ -404,8 +403,8 @@ public class EditRepositoryDialog extends JDialog {
                repository.description = descriptionField.getText();\r
                repository.owner = ownerField.getSelectedItem() == null ? null\r
                                : ownerField.getSelectedItem().toString();\r
-               repository.defaultHead = defaultHeadField.getSelectedItem() == null ? null\r
-                               : defaultHeadField.getSelectedItem().toString();\r
+               repository.HEAD = headRefField.getSelectedItem() == null ? null\r
+                               : headRefField.getSelectedItem().toString();\r
                repository.useTickets = useTickets.isSelected();\r
                repository.useDocs = useDocs.isSelected();\r
                repository.showRemoteBranches = showRemoteBranches.isSelected();\r
index e7a0880d65a56ed9cbd01e59df198821bf59413a..b633c69e3f8bf516b817fbff798eb906be81a11e 100644 (file)
@@ -58,8 +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 String defaultHead;\r
-       public List<String> availableHeads;\r
+       public String HEAD;\r
+       public List<String> availableRefs;\r
 \r
        private String displayName;\r
        \r
index 319aca5a254a9c37f785f764f7e0500ad93452b2..c80fb8aeee89a12b9886cab08cdf78817d6ed039 100644 (file)
@@ -1163,9 +1163,9 @@ public class JGitUtils {
         * no match is found, the SHA1 is returned.\r
         *\r
         * @param repository\r
-        * @return the ref name or the SHA1 for detached HEADs\r
+        * @return the ref name or the SHA1 for a detached HEAD\r
         */\r
-       public static String getSymbolicHeadTarget(Repository repository) {\r
+       public static String getHEADRef(Repository repository) {\r
                String target = null;\r
                try {\r
                        target = repository.getFullBranch();\r
@@ -1193,58 +1193,56 @@ public class JGitUtils {
        }\r
        \r
        /**\r
-        * Sets the HEAD symbolic ref name for a repository. The HEAD will\r
-        * be detached if the name does not reference a branch.\r
+        * Sets the symbolic ref HEAD to the specified target ref. The\r
+        * HEAD will be detached if the target ref is not a branch.\r
         *\r
         * @param repository\r
-        * @param name\r
+        * @param targetRef\r
+        * @return true if successful\r
         */\r
-       public static void setSymbolicHeadTarget(Repository repository, String name) {\r
+       public static boolean setHEADtoRef(Repository repository, String targetRef) {\r
                try {\r
-                       boolean detach = !name.startsWith(Constants.R_HEADS); // detach if not a branch\r
+                        // detach HEAD if target ref is not a branch\r
+                       boolean detach = !targetRef.startsWith(Constants.R_HEADS);\r
                        RefUpdate.Result result;\r
                        RefUpdate head = repository.updateRef(Constants.HEAD, detach);\r
                        if (detach) { // Tag\r
-                               RevCommit commit = getCommit(repository, name);\r
+                               RevCommit commit = getCommit(repository, targetRef);\r
                                head.setNewObjectId(commit.getId());\r
                                result = head.forceUpdate();\r
                        } else {\r
-                               result = head.link(name);\r
+                               result = head.link(targetRef);\r
                        }\r
                        switch (result) {\r
                        case NEW:\r
                        case FORCED:\r
                        case NO_CHANGE:\r
                        case FAST_FORWARD:\r
-                               break;\r
+                               return true;                            \r
                        default:\r
-                               LOGGER.error(MessageFormat.format("{0} symbolic HEAD update to {1} returned result {2}",\r
-                                               repository.getDirectory().getAbsolutePath(), name, result));\r
+                               LOGGER.error(MessageFormat.format("{0} HEAD update to {1} returned result {2}",\r
+                                               repository.getDirectory().getAbsolutePath(), targetRef, result));\r
                        }\r
                } catch (Throwable t) {\r
-                       error(t, repository, "{0} failed to set symbolic HEAD to {1}", name);\r
+                       error(t, repository, "{0} failed to set HEAD to {1}", targetRef);\r
                }\r
+               return false;\r
        }\r
        \r
        /**\r
-        * Get the full branch and tag ref names for any potential symbolic head targets.\r
+        * Get the full branch and tag ref names for any potential HEAD targets.\r
         *\r
         * @param repository\r
         * @return a list of ref names\r
         */\r
        public static List<String> getAvailableHeadTargets(Repository repository) {\r
                List<String> targets = new ArrayList<String>();\r
-               List<RefModel> branchModels = JGitUtils.getLocalBranches(repository, true, -1);\r
-               if (branchModels.size() > 0) {\r
-                       for (RefModel branchModel : branchModels) {\r
-                               targets.add(branchModel.getName());\r
-                       }\r
+               for (RefModel branchModel : JGitUtils.getLocalBranches(repository, true, -1)) {\r
+                       targets.add(branchModel.getName());\r
                }\r
-               List<RefModel> tagModels = JGitUtils.getTags(repository, true, -1);\r
-               if (tagModels.size() > 0) {\r
-                       for (RefModel tagModel : tagModels) {\r
-                               targets.add(tagModel.getName());\r
-                       }\r
+\r
+               for (RefModel tagModel : JGitUtils.getTags(repository, true, -1)) {\r
+                       targets.add(tagModel.getName());\r
                }\r
                return targets;\r
        }\r
index f039a36a722ad13d1d60322a8bd516586bb6c862..1a8513a5fea6983ca37eee734d8ff070fc5f3637 100644 (file)
@@ -131,8 +131,7 @@ 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.headRefDescription = change the ref that HEAD links to. e.g. refs/heads/master\r
 gb.federationStrategy = federation strategy\r
 gb.federationRegistration = federation registration\r
 gb.federationResults = federation pull results\r
index 9bd8c5150393527d0896e4559afa8783f7b6a003..7c694ba10e3df37d025bb18ea3497e049f047a1b 100644 (file)
@@ -14,7 +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" tabindex="4" /> &nbsp;<span class="help-inline"><wicket:message key="gb.defaultHeadDescription"></wicket:message></span></td></tr>\r
+                               <tr><th><wicket:message key="gb.head"></wicket:message></th><td class="edit"><select wicket:id="HEAD" tabindex="4" /> &nbsp;<span class="help-inline"><wicket:message key="gb.headRefDescription"></wicket:message></span></td></tr>\r
                                <tr><th><wicket:message key="gb.owner"></wicket:message></th><td class="edit"><select wicket:id="owner" tabindex="5" /> &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="6" /> &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="7" /> &nbsp;<span class="help-inline"><wicket:message key="gb.useDocsDescription"></wicket:message></span></td></tr>\r
index ec52aaf88e615f52eb6b2d3e449dac3c985e0967..a259a995c4c04041eac6fd811924e66714ebefdc 100644 (file)
@@ -273,11 +273,11 @@ public class EditRepositoryPage extends RootSubPage {
                form.add(new TextField<String>("origin").setEnabled(false/* isCreate */));\r
                \r
                // allow relinking HEAD to a branch or tag other than master on edit repository\r
-               List<String> availableHeads = new ArrayList<String>();\r
-               if (!ArrayUtils.isEmpty(repositoryModel.availableHeads)) {\r
-                       availableHeads.addAll(repositoryModel.availableHeads);\r
+               List<String> availableRefs = new ArrayList<String>();\r
+               if (!ArrayUtils.isEmpty(repositoryModel.availableRefs)) {\r
+                       availableRefs.addAll(repositoryModel.availableRefs);\r
                }\r
-               form.add(new DropDownChoice<String>("defaultHead", availableHeads).setEnabled(!isCreate));\r
+               form.add(new DropDownChoice<String>("HEAD", availableRefs).setEnabled(!isCreate));\r
 \r
                // federation strategies - remove ORIGIN choice if this repository has\r
                // no origin.\r
index 7c3f8ab0af563f917eaccf40d56f9432de0e29a9..8d70d2fa39efda557a346e83c1b93c8fdf5d459c 100644 (file)
@@ -212,6 +212,28 @@ public class JGitUtilsTest {
                assertEquals("183474d554e6f68478a02d9d7888b67a9338cdff", list.get(0).notesRef\r
                                .getReferencedObjectId().getName());\r
        }\r
+       \r
+       @Test\r
+       public void testRelinkHEAD() throws Exception {\r
+               Repository repository = GitBlitSuite.getJGitRepository();\r
+               // confirm HEAD is master\r
+               String currentRef = JGitUtils.getHEADRef(repository);\r
+               assertEquals("refs/heads/master", currentRef);\r
+               List<String> availableHeads = JGitUtils.getAvailableHeadTargets(repository);\r
+               assertTrue(availableHeads.size() > 0);\r
+               \r
+               // set HEAD to stable-1.2\r
+               JGitUtils.setHEADtoRef(repository, "refs/heads/stable-1.2");\r
+               currentRef = JGitUtils.getHEADRef(repository);\r
+               assertEquals("refs/heads/stable-1.2", currentRef);\r
+\r
+               // restore HEAD to master\r
+               JGitUtils.setHEADtoRef(repository, "refs/heads/master");\r
+               currentRef = JGitUtils.getHEADRef(repository);\r
+               assertEquals("refs/heads/master", currentRef);\r
+               \r
+               repository.close();\r
+       }\r
 \r
        @Test\r
        public void testCreateOrphanedBranch() throws Exception {\r