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.

GcPackRefsTest.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2012, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.file;
  44. import static org.hamcrest.Matchers.lessThanOrEqualTo;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertNull;
  49. import static org.junit.Assert.assertSame;
  50. import static org.junit.Assert.assertThat;
  51. import java.io.File;
  52. import java.io.IOException;
  53. import java.nio.file.Files;
  54. import java.nio.file.Path;
  55. import java.util.concurrent.BrokenBarrierException;
  56. import java.util.concurrent.Callable;
  57. import java.util.concurrent.CyclicBarrier;
  58. import java.util.concurrent.ExecutorService;
  59. import java.util.concurrent.Executors;
  60. import java.util.concurrent.Future;
  61. import java.util.concurrent.TimeUnit;
  62. import org.eclipse.jgit.api.Git;
  63. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  64. import org.eclipse.jgit.lib.ConfigConstants;
  65. import org.eclipse.jgit.lib.Constants;
  66. import org.eclipse.jgit.lib.Ref.Storage;
  67. import org.eclipse.jgit.lib.RefUpdate;
  68. import org.eclipse.jgit.lib.RefUpdate.Result;
  69. import org.eclipse.jgit.revwalk.RevBlob;
  70. import org.eclipse.jgit.revwalk.RevCommit;
  71. import org.eclipse.jgit.storage.file.FileBasedConfig;
  72. import org.junit.Test;
  73. @SuppressWarnings("boxing")
  74. public class GcPackRefsTest extends GcTestCase {
  75. @Test
  76. public void looseRefPacked() throws Exception {
  77. RevBlob a = tr.blob("a");
  78. tr.lightweightTag("t", a);
  79. gc.packRefs();
  80. assertSame(repo.exactRef("refs/tags/t").getStorage(), Storage.PACKED);
  81. }
  82. @Test
  83. public void emptyRefDirectoryDeleted() throws Exception {
  84. String ref = "dir/ref";
  85. tr.branch(ref).commit().create();
  86. String name = repo.findRef(ref).getName();
  87. Path dir = repo.getDirectory().toPath().resolve(name).getParent();
  88. assertNotNull(dir);
  89. gc.packRefs();
  90. assertFalse(Files.exists(dir));
  91. }
  92. @Test
  93. public void concurrentOnlyOneWritesPackedRefs() throws Exception {
  94. RevBlob a = tr.blob("a");
  95. tr.lightweightTag("t", a);
  96. CyclicBarrier syncPoint = new CyclicBarrier(2);
  97. // Returns 0 for success, 1 in case of error when writing pack.
  98. Callable<Integer> packRefs = () -> {
  99. syncPoint.await();
  100. try {
  101. gc.packRefs();
  102. return 0;
  103. } catch (IOException e) {
  104. return 1;
  105. }
  106. };
  107. ExecutorService pool = Executors.newFixedThreadPool(2);
  108. try {
  109. Future<Integer> p1 = pool.submit(packRefs);
  110. Future<Integer> p2 = pool.submit(packRefs);
  111. assertThat(p1.get() + p2.get(), lessThanOrEqualTo(1));
  112. } finally {
  113. pool.shutdown();
  114. pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  115. }
  116. }
  117. @Test
  118. public void whileRefLockedRefNotPackedNoError()
  119. throws Exception {
  120. RevBlob a = tr.blob("a");
  121. tr.lightweightTag("t1", a);
  122. tr.lightweightTag("t2", a);
  123. LockFile refLock = new LockFile(new File(repo.getDirectory(),
  124. "refs/tags/t1"));
  125. try {
  126. refLock.lock();
  127. gc.packRefs();
  128. } finally {
  129. refLock.unlock();
  130. }
  131. assertSame(repo.exactRef("refs/tags/t1").getStorage(), Storage.LOOSE);
  132. assertSame(repo.exactRef("refs/tags/t2").getStorage(), Storage.PACKED);
  133. }
  134. @Test
  135. public void whileRefUpdatedRefUpdateSucceeds()
  136. throws Exception {
  137. RevBlob a = tr.blob("a");
  138. tr.lightweightTag("t", a);
  139. final RevBlob b = tr.blob("b");
  140. final CyclicBarrier refUpdateLockedRef = new CyclicBarrier(2);
  141. final CyclicBarrier packRefsDone = new CyclicBarrier(2);
  142. ExecutorService pool = Executors.newFixedThreadPool(2);
  143. try {
  144. Future<Result> result = pool.submit(new Callable<Result>() {
  145. @Override
  146. public Result call() throws Exception {
  147. RefUpdate update = new RefDirectoryUpdate(
  148. (RefDirectory) repo.getRefDatabase(),
  149. repo.exactRef("refs/tags/t")) {
  150. @Override
  151. public boolean isForceUpdate() {
  152. try {
  153. refUpdateLockedRef.await();
  154. packRefsDone.await();
  155. } catch (InterruptedException e) {
  156. Thread.currentThread().interrupt();
  157. } catch (BrokenBarrierException e) {
  158. Thread.currentThread().interrupt();
  159. }
  160. return super.isForceUpdate();
  161. }
  162. };
  163. update.setForceUpdate(true);
  164. update.setNewObjectId(b);
  165. return update.update();
  166. }
  167. });
  168. pool.submit(new Callable<Void>() {
  169. @Override
  170. public Void call() throws Exception {
  171. refUpdateLockedRef.await();
  172. gc.packRefs();
  173. packRefsDone.await();
  174. return null;
  175. }
  176. });
  177. assertSame(result.get(), Result.FORCED);
  178. } finally {
  179. pool.shutdownNow();
  180. pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  181. }
  182. assertEquals(repo.exactRef("refs/tags/t").getObjectId(), b);
  183. }
  184. @Test
  185. public void dontPackHEAD_nonBare() throws Exception {
  186. BranchBuilder bb = tr.branch("refs/heads/side");
  187. RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
  188. bb.commit().add("A", "A2").add("B", "B2").create();
  189. Git git = Git.wrap(repo);
  190. // check for the unborn branch master. HEAD should point to master and
  191. // master doesn't exist.
  192. assertEquals(repo.exactRef("HEAD").getTarget().getName(),
  193. "refs/heads/master");
  194. assertNull(repo.exactRef("HEAD").getTarget().getObjectId());
  195. gc.packRefs();
  196. assertSame(repo.exactRef("HEAD").getStorage(), Storage.LOOSE);
  197. assertEquals(repo.exactRef("HEAD").getTarget().getName(),
  198. "refs/heads/master");
  199. assertNull(repo.exactRef("HEAD").getTarget().getObjectId());
  200. git.checkout().setName("refs/heads/side").call();
  201. gc.packRefs();
  202. assertSame(repo.exactRef("HEAD").getStorage(), Storage.LOOSE);
  203. // check for detached HEAD
  204. git.checkout().setName(first.getName()).call();
  205. gc.packRefs();
  206. assertSame(repo.exactRef("HEAD").getStorage(), Storage.LOOSE);
  207. }
  208. @Test
  209. public void dontPackHEAD_bare() throws Exception {
  210. BranchBuilder bb = tr.branch("refs/heads/side");
  211. bb.commit().add("A", "A").add("B", "B").create();
  212. RevCommit second = bb.commit().add("A", "A2").add("B", "B2").create();
  213. // Convert the repo to be bare
  214. FileBasedConfig cfg = repo.getConfig();
  215. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  216. ConfigConstants.CONFIG_KEY_BARE, true);
  217. cfg.save();
  218. Git git = Git.open(repo.getDirectory());
  219. repo = (FileRepository) git.getRepository();
  220. // check for the unborn branch master. HEAD should point to master and
  221. // master doesn't exist.
  222. assertEquals(repo.exactRef("HEAD").getTarget().getName(),
  223. "refs/heads/master");
  224. assertNull(repo.exactRef("HEAD").getTarget().getObjectId());
  225. gc.packRefs();
  226. assertSame(repo.exactRef("HEAD").getStorage(), Storage.LOOSE);
  227. assertEquals(repo.exactRef("HEAD").getTarget().getName(),
  228. "refs/heads/master");
  229. assertNull(repo.exactRef("HEAD").getTarget().getObjectId());
  230. // check for non-detached HEAD
  231. repo.updateRef(Constants.HEAD).link("refs/heads/side");
  232. gc.packRefs();
  233. assertSame(repo.exactRef("HEAD").getStorage(), Storage.LOOSE);
  234. assertEquals(repo.exactRef("HEAD").getTarget().getObjectId(),
  235. second.getId());
  236. }
  237. }