aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst
diff options
context:
space:
mode:
authorChristian Halstrick <christian.halstrick@sap.com>2014-03-26 15:27:48 +0100
committerChristian Halstrick <christian.halstrick@sap.com>2014-03-26 15:27:48 +0100
commit16436ef6d98e564d60edd1bee5026aeda0def1e6 (patch)
tree0597225b815ddf5d787b1d1e0d01414b5d96573d /org.eclipse.jgit.test/tst
parent27473d8e66bda58c51eca6f474885bd893e79f07 (diff)
downloadjgit-16436ef6d98e564d60edd1bee5026aeda0def1e6.tar.gz
jgit-16436ef6d98e564d60edd1bee5026aeda0def1e6.zip
Fix GarbageCollection not to pack HEAD
When working on a non-bare repository with a detached HEAD jgit's GC was packing the ref named "HEAD" into the packed-refs file and deleted the loose ref (the file .git/HEAD!). This made the repo unusable for native git. This is fixed by telling jgit to only pack refs starting from "refs/" Change-Id: I50018aa006f18b244d2cae2ff78b5ffe1b821d63
Diffstat (limited to 'org.eclipse.jgit.test/tst')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcPackRefsTest.java68
1 files changed, 68 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcPackRefsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcPackRefsTest.java
index 0ade902601..c7336da408 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcPackRefsTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcPackRefsTest.java
@@ -45,6 +45,7 @@ package org.eclipse.jgit.internal.storage.file;
import static java.lang.Integer.valueOf;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import java.io.File;
@@ -57,10 +58,16 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
+import org.eclipse.jgit.lib.ConfigConstants;
+import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref.Storage;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.RefUpdate.Result;
import org.eclipse.jgit.revwalk.RevBlob;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.junit.Test;
public class GcPackRefsTest extends GcTestCase {
@@ -177,4 +184,65 @@ public class GcPackRefsTest extends GcTestCase {
assertEquals(repo.getRef("refs/tags/t").getObjectId(), b);
}
+
+ @Test
+ public void dontPackHEAD_nonBare() throws Exception {
+ BranchBuilder bb = tr.branch("refs/heads/side");
+ RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
+ bb.commit().add("A", "A2").add("B", "B2").create();
+ Git git = Git.wrap(repo);
+
+ // check for the unborn branch master. HEAD should point to master and
+ // master doesn't exist.
+ assertEquals(repo.getRef("HEAD").getTarget().getName(),
+ "refs/heads/master");
+ assertNull(repo.getRef("HEAD").getTarget().getObjectId());
+ gc.packRefs();
+ assertSame(repo.getRef("HEAD").getStorage(), Storage.LOOSE);
+ assertEquals(repo.getRef("HEAD").getTarget().getName(),
+ "refs/heads/master");
+ assertNull(repo.getRef("HEAD").getTarget().getObjectId());
+
+ git.checkout().setName("refs/heads/side").call();
+ gc.packRefs();
+ assertSame(repo.getRef("HEAD").getStorage(), Storage.LOOSE);
+
+ // check for detached HEAD
+ git.checkout().setName(first.getName()).call();
+ gc.packRefs();
+ assertSame(repo.getRef("HEAD").getStorage(), Storage.LOOSE);
+ }
+
+ @Test
+ public void dontPackHEAD_bare() throws Exception {
+ BranchBuilder bb = tr.branch("refs/heads/side");
+ bb.commit().add("A", "A").add("B", "B").create();
+ RevCommit second = bb.commit().add("A", "A2").add("B", "B2").create();
+
+ // Convert the repo to be bare
+ FileBasedConfig cfg = repo.getConfig();
+ cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
+ ConfigConstants.CONFIG_KEY_BARE, true);
+ cfg.save();
+ Git git = Git.open(repo.getDirectory());
+ repo = (FileRepository) git.getRepository();
+
+ // check for the unborn branch master. HEAD should point to master and
+ // master doesn't exist.
+ assertEquals(repo.getRef("HEAD").getTarget().getName(),
+ "refs/heads/master");
+ assertNull(repo.getRef("HEAD").getTarget().getObjectId());
+ gc.packRefs();
+ assertSame(repo.getRef("HEAD").getStorage(), Storage.LOOSE);
+ assertEquals(repo.getRef("HEAD").getTarget().getName(),
+ "refs/heads/master");
+ assertNull(repo.getRef("HEAD").getTarget().getObjectId());
+
+ // check for non-detached HEAD
+ repo.updateRef(Constants.HEAD).link("refs/heads/side");
+ gc.packRefs();
+ assertSame(repo.getRef("HEAD").getStorage(), Storage.LOOSE);
+ assertEquals(repo.getRef("HEAD").getTarget().getObjectId(),
+ second.getId());
+ }
}