You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GcKeepFilesTest.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.file;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.File;
  15. import java.util.Iterator;
  16. import org.eclipse.jgit.internal.storage.file.PackIndex.MutableEntry;
  17. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  18. import org.junit.Test;
  19. public class GcKeepFilesTest extends GcTestCase {
  20. @Test
  21. public void testKeepFiles() throws Exception {
  22. BranchBuilder bb = tr.branch("refs/heads/master");
  23. bb.commit().add("A", "A").add("B", "B").create();
  24. stats = gc.getStatistics();
  25. assertEquals(4, stats.numberOfLooseObjects);
  26. assertEquals(0, stats.numberOfPackedObjects);
  27. assertEquals(0, stats.numberOfPackFiles);
  28. gc.gc();
  29. stats = gc.getStatistics();
  30. assertEquals(0, stats.numberOfLooseObjects);
  31. assertEquals(4, stats.numberOfPackedObjects);
  32. assertEquals(1, stats.numberOfPackFiles);
  33. Iterator<Pack> packIt = repo.getObjectDatabase().getPacks()
  34. .iterator();
  35. Pack singlePack = packIt.next();
  36. assertFalse(packIt.hasNext());
  37. String packFileName = singlePack.getPackFile().getPath();
  38. String keepFileName = packFileName.substring(0,
  39. packFileName.lastIndexOf('.')) + ".keep";
  40. File keepFile = new File(keepFileName);
  41. assertFalse(keepFile.exists());
  42. assertTrue(keepFile.createNewFile());
  43. bb.commit().add("A", "A2").add("B", "B2").create();
  44. stats = gc.getStatistics();
  45. assertEquals(4, stats.numberOfLooseObjects);
  46. assertEquals(4, stats.numberOfPackedObjects);
  47. assertEquals(1, stats.numberOfPackFiles);
  48. gc.gc();
  49. stats = gc.getStatistics();
  50. assertEquals(0, stats.numberOfLooseObjects);
  51. assertEquals(8, stats.numberOfPackedObjects);
  52. assertEquals(2, stats.numberOfPackFiles);
  53. // check that no object is packed twice
  54. Iterator<Pack> packs = repo.getObjectDatabase().getPacks()
  55. .iterator();
  56. PackIndex ind1 = packs.next().getIndex();
  57. assertEquals(4, ind1.getObjectCount());
  58. PackIndex ind2 = packs.next().getIndex();
  59. assertEquals(4, ind2.getObjectCount());
  60. for (MutableEntry e: ind1)
  61. if (ind2.hasObject(e.toObjectId()))
  62. assertFalse(
  63. "the following object is in both packfiles: "
  64. + e.toObjectId(),
  65. ind2.hasObject(e.toObjectId()));
  66. }
  67. }