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.

GcReflogTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 java.io.File;
  13. import java.util.Collections;
  14. import org.eclipse.jgit.api.Git;
  15. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  16. import org.eclipse.jgit.lib.Constants;
  17. import org.eclipse.jgit.lib.ObjectId;
  18. import org.eclipse.jgit.lib.RefUpdate;
  19. import org.eclipse.jgit.revwalk.RevCommit;
  20. import org.eclipse.jgit.util.FileUtils;
  21. import org.junit.Test;
  22. public class GcReflogTest extends GcTestCase {
  23. @Test
  24. public void testPruneNone() throws Exception {
  25. BranchBuilder bb = tr.branch("refs/heads/master");
  26. bb.commit().add("A", "A").add("B", "B").create();
  27. bb.commit().add("A", "A2").add("B", "B2").create();
  28. new File(repo.getDirectory(), Constants.LOGS + "/refs/heads/master")
  29. .delete();
  30. stats = gc.getStatistics();
  31. assertEquals(8, stats.numberOfLooseObjects);
  32. gc.setExpireAgeMillis(0);
  33. fsTick();
  34. gc.prune(Collections.<ObjectId> emptySet());
  35. stats = gc.getStatistics();
  36. assertEquals(8, stats.numberOfLooseObjects);
  37. tr.blob("x");
  38. stats = gc.getStatistics();
  39. assertEquals(9, stats.numberOfLooseObjects);
  40. fsTick();
  41. gc.prune(Collections.<ObjectId> emptySet());
  42. stats = gc.getStatistics();
  43. assertEquals(8, stats.numberOfLooseObjects);
  44. }
  45. @Test
  46. public void testPackRepoWithCorruptReflog() throws Exception {
  47. // create a reflog entry "0000... 0000... foobar" by doing an initial
  48. // refupdate for HEAD which points to a non-existing ref. The
  49. // All-Projects repo of gerrit instances had such entries
  50. RefUpdate ru = repo.updateRef(Constants.HEAD);
  51. ru.link("refs/to/garbage");
  52. tr.branch("refs/heads/master").commit().add("A", "A").add("B", "B")
  53. .create();
  54. // make sure HEAD exists
  55. Git.wrap(repo).checkout().setName("refs/heads/master").call();
  56. gc.gc();
  57. }
  58. @Test
  59. public void testPackCommitsAndLooseOneNoReflog() throws Exception {
  60. BranchBuilder bb = tr.branch("refs/heads/master");
  61. RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
  62. bb.commit().add("A", "A2").add("B", "B2").create();
  63. tr.update("refs/heads/master", first);
  64. stats = gc.getStatistics();
  65. assertEquals(8, stats.numberOfLooseObjects);
  66. assertEquals(0, stats.numberOfPackedObjects);
  67. FileUtils.delete(new File(repo.getDirectory(), "logs/HEAD"),
  68. FileUtils.RETRY | FileUtils.SKIP_MISSING);
  69. FileUtils.delete(
  70. new File(repo.getDirectory(), "logs/refs/heads/master"),
  71. FileUtils.RETRY | FileUtils.SKIP_MISSING);
  72. gc.gc();
  73. stats = gc.getStatistics();
  74. assertEquals(4, stats.numberOfLooseObjects);
  75. assertEquals(4, stats.numberOfPackedObjects);
  76. assertEquals(1, stats.numberOfPackFiles);
  77. }
  78. @Test
  79. public void testPackCommitsAndLooseOneWithPruneNowNoReflog()
  80. throws Exception {
  81. BranchBuilder bb = tr.branch("refs/heads/master");
  82. RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
  83. bb.commit().add("A", "A2").add("B", "B2").create();
  84. tr.update("refs/heads/master", first);
  85. stats = gc.getStatistics();
  86. assertEquals(8, stats.numberOfLooseObjects);
  87. assertEquals(0, stats.numberOfPackedObjects);
  88. FileUtils.delete(new File(repo.getDirectory(), "logs/HEAD"),
  89. FileUtils.RETRY | FileUtils.SKIP_MISSING);
  90. FileUtils.delete(
  91. new File(repo.getDirectory(), "logs/refs/heads/master"),
  92. FileUtils.RETRY | FileUtils.SKIP_MISSING);
  93. gc.setExpireAgeMillis(0);
  94. gc.gc();
  95. stats = gc.getStatistics();
  96. assertEquals(0, stats.numberOfLooseObjects);
  97. assertEquals(4, stats.numberOfPackedObjects);
  98. assertEquals(1, stats.numberOfPackFiles);
  99. }
  100. }