]> source.dussan.org Git - jgit.git/commitdiff
Don't fail when trying to prune pack which is already gone 25/1203925/3
authorJacek Centkowski <geminica.programs@gmail.com>
Mon, 11 Nov 2024 11:48:20 +0000 (12:48 +0100)
committerLuca Milanesio <luca.milanesio@gmail.com>
Wed, 20 Nov 2024 08:54:33 +0000 (08:54 +0000)
Update the TestRepository.prunePacked so that it doesn't fail if a pack
to be pruned is already gone.
It is especially handy when the prunePacked function is called in
`TestRepository.packAndPrune` function after the repo moves on after
GC was performed.

Change-Id: I01b4ddbaddec1fdc24cfbb967e0edfe0de6c4b7c

org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcNumberOfPackFilesSinceBitmapStatisticsTest.java

index 66cf739ef15f3f2cddf080e2faffb714700f547a..c6cdfafe84b386e83f73714714e6becf57ee8704 100644 (file)
@@ -1020,7 +1020,8 @@ public class TestRepository<R extends Repository> implements AutoCloseable {
        private static void prunePacked(ObjectDirectory odb) throws IOException {
                for (Pack p : odb.getPacks()) {
                        for (MutableEntry e : p)
-                               FileUtils.delete(odb.fileFor(e.toObjectId()));
+                               FileUtils.delete(odb.fileFor(e.toObjectId()),
+                                               FileUtils.SKIP_MISSING);
                }
        }
 
index 42cb3cd0c266e7b74ee513fb2f6842d5a892ae1e..fa13bb57c8eea4335c6f50bf111a75ad8cf98330 100644 (file)
@@ -12,25 +12,12 @@ package org.eclipse.jgit.internal.storage.file;
 
 import static org.junit.Assert.assertEquals;
 
-import java.io.BufferedOutputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.nio.file.Files;
-import java.util.HashSet;
-import java.util.Set;
 import java.util.stream.StreamSupport;
 
-import org.eclipse.jgit.internal.storage.file.PackIndex.MutableEntry;
-import org.eclipse.jgit.internal.storage.pack.PackExt;
-import org.eclipse.jgit.internal.storage.pack.PackWriter;
-import org.eclipse.jgit.junit.TestRepository;
-import org.eclipse.jgit.lib.NullProgressMonitor;
-import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.PersonIdent;
-import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.util.FileUtils;
 import org.junit.Test;
 
 public class GcNumberOfPackFilesSinceBitmapStatisticsTest extends GcTestCase {
@@ -43,7 +30,7 @@ public class GcNumberOfPackFilesSinceBitmapStatisticsTest extends GcTestCase {
        @Test
        public void testShouldReportAllPackFilesWhenNoGcWasPerformed()
                        throws Exception {
-               packAndPrune();
+               tr.packAndPrune();
                long result = gc.getStatistics().numberOfPackFilesSinceBitmap;
 
                assertEquals(repo.getObjectDatabase().getPacks().size(), result);
@@ -68,7 +55,7 @@ public class GcNumberOfPackFilesSinceBitmapStatisticsTest extends GcTestCase {
 
                // progress & pack
                addCommit(parent);
-               packAndPrune();
+               tr.packAndPrune();
 
                assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
        }
@@ -88,18 +75,11 @@ public class GcNumberOfPackFilesSinceBitmapStatisticsTest extends GcTestCase {
 
                // progress & pack
                addCommit(parent);
-               packAndPrune();
+               tr.packAndPrune();
 
                assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
        }
 
-       private void packAndPrune() throws Exception {
-               try (SkipNonExistingFilesTestRepository testRepo = new SkipNonExistingFilesTestRepository(
-                               repo)) {
-                       testRepo.packAndPrune();
-               }
-       }
-
        private RevCommit addCommit(RevCommit parent) throws Exception {
                return tr.branch("master").commit()
                                .author(new PersonIdent("repo-metrics", "repo@metrics.com"))
@@ -114,60 +94,4 @@ public class GcNumberOfPackFilesSinceBitmapStatisticsTest extends GcTestCase {
                                                .spliterator(), false)
                                .count();
        }
-
-       /**
-        * The TestRepository has a {@link TestRepository#packAndPrune()} function
-        * but it fails in the last step after GC was performed as it doesn't
-        * SKIP_MISSING files. In order to circumvent it was copied and improved
-        * here.
-        */
-       private static class SkipNonExistingFilesTestRepository
-                       extends TestRepository<FileRepository> {
-               private final FileRepository repo;
-
-               private SkipNonExistingFilesTestRepository(FileRepository db) throws IOException {
-                       super(db);
-                       repo = db;
-               }
-
-               @Override
-               public void packAndPrune() throws Exception {
-                       ObjectDirectory odb = repo.getObjectDatabase();
-                       NullProgressMonitor m = NullProgressMonitor.INSTANCE;
-
-                       final PackFile pack, idx;
-                       try (PackWriter pw = new PackWriter(repo)) {
-                               Set<ObjectId> all = new HashSet<>();
-                               for (Ref r : repo.getRefDatabase().getRefs())
-                                       all.add(r.getObjectId());
-                               pw.preparePack(m, all, PackWriter.NONE);
-
-                               pack = new PackFile(odb.getPackDirectory(), pw.computeName(),
-                                               PackExt.PACK);
-                               try (OutputStream out = new BufferedOutputStream(
-                                               new FileOutputStream(pack))) {
-                                       pw.writePack(m, m, out);
-                               }
-                               pack.setReadOnly();
-
-                               idx = pack.create(PackExt.INDEX);
-                               try (OutputStream out = new BufferedOutputStream(
-                                               new FileOutputStream(idx))) {
-                                       pw.writeIndex(out);
-                               }
-                               idx.setReadOnly();
-                       }
-
-                       odb.openPack(pack);
-                       updateServerInfo();
-
-                       // alternative packAndPrune implementation that skips missing files
-                       // after GC.
-                       for (Pack p : odb.getPacks()) {
-                               for (MutableEntry e : p)
-                                       FileUtils.delete(odb.fileFor(e.toObjectId()),
-                                                       FileUtils.SKIP_MISSING);
-                       }
-               }
-       }
 }