]> source.dussan.org Git - jgit.git/commitdiff
Allow deletion of HEAD ref if the repository is bare. 10/65310/2
authorMike Williams <miwilliams@google.com>
Mon, 25 Jan 2016 17:45:33 +0000 (12:45 -0500)
committerMike Williams <miwilliams@google.com>
Thu, 28 Jan 2016 16:43:59 +0000 (11:43 -0500)
Change-Id: I2281d818c9f76019e7e053e89b20214f2d663957
Signed-off-by: Mike Williams <miwilliams@google.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefUpdateTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/RefUpdate.java

index 48434189e4b6ca08162fe1046defe43beb6adf06..4d91a2368dcf8b44879288db8aa507af0ac3c582 100644 (file)
@@ -104,9 +104,14 @@ public class RefUpdateTest extends SampleDataRepositoryTestCase {
 
        private void delete(final RefUpdate ref, final Result expected,
                        final boolean exists, final boolean removed) throws IOException {
-               assertEquals(exists, db.getAllRefs().containsKey(ref.getName()));
+               delete(db, ref, expected, exists, removed);
+       }
+
+       private void delete(Repository repo, final RefUpdate ref, final Result expected,
+                       final boolean exists, final boolean removed) throws IOException {
+               assertEquals(exists, repo.getAllRefs().containsKey(ref.getName()));
                assertEquals(expected, ref.delete());
-               assertEquals(!removed, db.getAllRefs().containsKey(ref.getName()));
+               assertEquals(!removed, repo.getAllRefs().containsKey(ref.getName()));
        }
 
        @Test
@@ -232,6 +237,17 @@ public class RefUpdateTest extends SampleDataRepositoryTestCase {
                assertEquals(0, db.getReflogReader("HEAD").getReverseEntries().size());
        }
 
+       @Test
+       public void testDeleteHeadInBareRepo() throws IOException {
+               try (Repository bareRepo = createBareRepository()) {
+                       RefUpdate ref = bareRepo.updateRef(Constants.HEAD);
+                       ref.setNewObjectId(ObjectId.fromString("0123456789012345678901234567890123456789"));
+                       // Create the HEAD ref so we can delete it.
+                       assertEquals(Result.NEW, ref.update());
+                       ref = bareRepo.updateRef(Constants.HEAD);
+                       delete(bareRepo, ref, Result.NO_CHANGE, true, true);
+               }
+       }
        /**
         * Delete a loose ref and make sure the directory in refs is deleted too,
         * and the reflog dir too
index 4316cd0a1f48eec7d91e093254320eedbca0cecf..c1027f0f75d528f684dbc33e4111143da4ca66a0 100644 (file)
@@ -552,7 +552,8 @@ public abstract class RefUpdate {
         */
        public Result delete(final RevWalk walk) throws IOException {
                final String myName = getRef().getLeaf().getName();
-               if (myName.startsWith(Constants.R_HEADS)) {
+               if (myName.startsWith(Constants.R_HEADS) && !getRepository().isBare()) {
+                       // Don't allow the currently checked out branch to be deleted.
                        Ref head = getRefDatabase().getRef(Constants.HEAD);
                        while (head != null && head.isSymbolic()) {
                                head = head.getTarget();