]> source.dussan.org Git - jgit.git/commitdiff
RefTreeDatabase: Expose bootstrap refs in getAdditionalRefs 15/64415/2
authorShawn Pearce <spearce@spearce.org>
Fri, 15 Jan 2016 00:11:15 +0000 (16:11 -0800)
committerShawn Pearce <spearce@spearce.org>
Fri, 15 Jan 2016 19:07:19 +0000 (11:07 -0800)
By showing the bootstrap layer in getAdditionalRefs() garbage
collector code can be more RefDatabase agnostic and not care about
the special case of RefTree and RefTreeNames for the purposes of
building up the roots to GC. Instead they can combine getRefs(ALL)
and getAdditionalRefs() and have a clean set of roots.

Change-Id: I665cd2456e9316640215b6a08bc728d1356f36d8

org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftree/RefTreeDatabaseTest.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftree/RefTreeDatabase.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftree/RefTreeNames.java

index 020d1b1b5136bcca8fa3312f76e6301c3ea334d2..1bacfe46f0cb75b75778292a5a499407d2ff5a22 100644 (file)
@@ -124,6 +124,17 @@ public class RefTreeDatabaseTest {
                assertTrue("no references", refdb.getRefs(ALL).isEmpty());
                assertTrue("no references", refdb.getRefs(R_HEADS).isEmpty());
                assertTrue("no references", refdb.getRefs(R_TAGS).isEmpty());
+               assertTrue("no references", refdb.getAdditionalRefs().isEmpty());
+       }
+
+       @Test
+       public void testGetAdditionalRefs() throws IOException {
+               update("refs/heads/master", A);
+
+               List<Ref> addl = refdb.getAdditionalRefs();
+               assertEquals(1, addl.size());
+               assertEquals("refs/txn/committed", addl.get(0).getName());
+               assertEquals(getTxnCommitted(), addl.get(0).getObjectId());
        }
 
        @Test
index 784507d88c035239db258f0d7ae07b17f18b7684..33be3b15a87f9bdbf7a19d4bdb1f134f24af2c8f 100644 (file)
@@ -194,7 +194,7 @@ public class DfsGarbageCollector {
                        refdb.refresh();
                        objdb.clearCache();
 
-                       Collection<Ref> refsBefore = RefTreeNames.allRefs(refdb);
+                       Collection<Ref> refsBefore = getAllRefs();
                        packsBefore = packsToRebuild();
                        if (packsBefore.isEmpty())
                                return true;
@@ -235,6 +235,18 @@ public class DfsGarbageCollector {
                }
        }
 
+       private Collection<Ref> getAllRefs() throws IOException {
+               Collection<Ref> refs = refdb.getRefs(RefDatabase.ALL).values();
+               List<Ref> addl = refdb.getAdditionalRefs();
+               if (!addl.isEmpty()) {
+                       List<Ref> all = new ArrayList<>(refs.size() + addl.size());
+                       all.addAll(refs);
+                       all.addAll(addl);
+                       return all;
+               }
+               return refs;
+       }
+
        private List<DfsPackFile> packsToRebuild() throws IOException {
                DfsPackFile[] packs = objdb.getPacks();
                List<DfsPackFile> out = new ArrayList<DfsPackFile>(packs.length);
index 2ce0d47348edd4e35c8584ddcacd32e7c638953d..49f9335aed0fdaa9260cb62639846e27f1e645e9 100644 (file)
@@ -629,15 +629,16 @@ public class GC {
        }
 
        /**
-        * Returns a map of all refs and additional refs (e.g. FETCH_HEAD,
+        * Returns a collection of all refs and additional refs (e.g. FETCH_HEAD,
         * MERGE_HEAD, ...)
         *
-        * @return a map where names of refs point to ref objects
+        * @return a collection of refs pointing to live objects.
         * @throws IOException
         */
        private Collection<Ref> getAllRefs() throws IOException {
-               Collection<Ref> refs = RefTreeNames.allRefs(repo.getRefDatabase());
-               List<Ref> addl = repo.getRefDatabase().getAdditionalRefs();
+               RefDatabase refdb = repo.getRefDatabase();
+               Collection<Ref> refs = refdb.getRefs(RefDatabase.ALL).values();
+               List<Ref> addl = refdb.getAdditionalRefs();
                if (!addl.isEmpty()) {
                        List<Ref> all = new ArrayList<>(refs.size() + addl.size());
                        all.addAll(refs);
index dc60311102f227d7b456c827d2fb3ca82faa7e8d..f6fdef1fa5887e25be03dad03c13123446b70035 100644 (file)
@@ -47,6 +47,8 @@ import static org.eclipse.jgit.lib.Ref.Storage.LOOSE;
 import static org.eclipse.jgit.lib.Ref.Storage.PACKED;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -250,7 +252,23 @@ public class RefTreeDatabase extends RefDatabase {
 
        @Override
        public List<Ref> getAdditionalRefs() throws IOException {
-               return Collections.emptyList();
+               Collection<Ref> txnRefs;
+               if (txnNamespace != null) {
+                       txnRefs = bootstrap.getRefs(txnNamespace).values();
+               } else {
+                       Ref r = bootstrap.exactRef(txnCommitted);
+                       if (r != null && r.getObjectId() != null) {
+                               txnRefs = Collections.singleton(r);
+                       } else {
+                               txnRefs = Collections.emptyList();
+                       }
+               }
+
+               List<Ref> otherRefs = bootstrap.getAdditionalRefs();
+               List<Ref> all = new ArrayList<>(txnRefs.size() + otherRefs.size());
+               all.addAll(txnRefs);
+               all.addAll(otherRefs);
+               return all;
        }
 
        @Override
index 239a745277531f20d87e083b71601ef61af370e6..c53d6deb218641fc75679c6f4e7a1c65a73a9091 100644 (file)
 
 package org.eclipse.jgit.internal.storage.reftree;
 
-import static org.eclipse.jgit.lib.RefDatabase.ALL;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.RefDatabase;
 
 /** Magic reference name logic for RefTrees. */
@@ -92,33 +84,6 @@ public class RefTreeNames {
                return false;
        }
 
-       /**
-        * Snapshot all references from a RefTreeDatabase and its bootstrap.
-        * <p>
-        * There may be name conflicts with multiple {@link Ref} objects containing
-        * the same name in the returned collection.
-        *
-        * @param refdb
-        *            database instance.
-        * @return all known references.
-        * @throws IOException
-        *             references cannot be enumerated.
-        */
-       public static Collection<Ref> allRefs(RefDatabase refdb)
-                       throws IOException {
-               Collection<Ref> refs = refdb.getRefs(ALL).values();
-               if (!(refdb instanceof RefTreeDatabase)) {
-                       return refs;
-               }
-
-               RefDatabase bootstrap = ((RefTreeDatabase) refdb).getBootstrap();
-               Collection<Ref> br = bootstrap.getRefs(ALL).values();
-               List<Ref> all = new ArrayList<>(refs.size() + br.size());
-               all.addAll(refs);
-               all.addAll(br);
-               return all;
-       }
-
        private RefTreeNames() {
        }
 }