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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 java.lang.Integer.valueOf;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertSame;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.nio.file.Files;
  52. import java.nio.file.Path;
  53. import java.util.concurrent.BrokenBarrierException;
  54. import java.util.concurrent.Callable;
  55. import java.util.concurrent.CyclicBarrier;
  56. import java.util.concurrent.ExecutorService;
  57. import java.util.concurrent.Executors;
  58. import java.util.concurrent.Future;
  59. import java.util.concurrent.TimeUnit;
  60. import org.eclipse.jgit.api.Git;
  61. import org.eclipse.jgit.junit.TestRepository.BranchBuilder;
  62. import org.eclipse.jgit.lib.ConfigConstants;
  63. import org.eclipse.jgit.lib.Constants;
  64. import org.eclipse.jgit.lib.Ref.Storage;
  65. import org.eclipse.jgit.lib.RefUpdate;
  66. import org.eclipse.jgit.lib.RefUpdate.Result;
  67. import org.eclipse.jgit.revwalk.RevBlob;
  68. import org.eclipse.jgit.revwalk.RevCommit;
  69. import org.eclipse.jgit.storage.file.FileBasedConfig;
  70. import org.junit.Test;
  71. public class GcPackRefsTest extends GcTestCase {
  72. @Test
  73. public void looseRefPacked() throws Exception {
  74. RevBlob a = tr.blob("a");
  75. tr.lightweightTag("t", a);
  76. gc.packRefs();
  77. assertSame(repo.exactRef("refs/tags/t").getStorage(), Storage.PACKED);
  78. }
  79. @Test
  80. public void emptyRefDirectoryDeleted() throws Exception {
  81. String ref = "dir/ref";
  82. tr.branch(ref).commit().create();
  83. String name = repo.findRef(ref).getName();
  84. Path dir = repo.getDirectory().toPath().resolve(name).getParent();
  85. gc.packRefs();
  86. assertFalse(Files.exists(dir));
  87. }
  88. @Test
  89. public void concurrentOnlyOneWritesPackedRefs() throws Exception {
  90. RevBlob a = tr.blob("a");
  91. tr.lightweightTag("t", a);
  92. final CyclicBarrier syncPoint = new CyclicBarrier(2);
  93. Callable<Integer> packRefs = new Callable<Integer>() {
  94. /** @return 0 for success, 1 in case of error when writing pack */
  95. public Integer call() throws Exception {
  96. syncPoint.await();
  97. try {
  98. gc.packRefs();
  99. return valueOf(0);
  100. } catch (IOException e) {
  101. return valueOf(1);
  102. }
  103. }
  104. };
  105. ExecutorService pool = Executors.newFixedThreadPool(2);
  106. try {
  107. Future<Integer> p1 = pool.submit(packRefs);
  108. Future<Integer> p2 = pool.submit(packRefs);
  109. assertEquals(1, p1.get().intValue() + p2.get().intValue());
  110. } finally {
  111. pool.shutdown();
  112. pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  113. }
  114. }
  115. @Test
  116. public void whileRefLockedRefNotPackedNoError()
  117. throws Exception {
  118. RevBlob a = tr.blob("a");
  119. tr.lightweightTag("t1", a);
  120. tr.lightweightTag("t2", a);
  121. LockFile refLock = new LockFile(new File(repo.getDirectory(),
  122. "refs/tags/t1"));
  123. try {
  124. refLock.lock();
  125. gc.packRefs();
  126. } finally {
  127. refLock.unlock();
  128. }
  129. assertSame(repo.exactRef("refs/tags/t1").getStorage(), Storage.LOOSE);
  130. assertSame(repo.exactRef("refs/tags/t2").getStorage(), Storage.PACKED);
  131. }
  132. @Test
  133. public void whileRefUpdatedRefUpdateSucceeds()
  134. throws Exception {
  135. RevBlob a = tr.blob("a");
  136. tr.lightweightTag("t", a);
  137. final RevBlob b = tr.blob("b");
  138. final CyclicBarrier refUpdateLockedRef = new CyclicBarrier(2);
  139. final CyclicBarrier packRefsDone = new CyclicBarrier(2);
  140. ExecutorService pool = Executors.newFixedThreadPool(2);
  141. try {
  142. Future<Result> result = pool.submit(new Callable<Result>() {
  143. public Result call() throws Exception {
  144. RefUpdate update = new RefDirectoryUpdate(
  145. (RefDirectory) repo.getRefDatabase(),
  146. repo.exactRef("refs/tags/t")) {
  147. @Override
  148. public boolean isForceUpdate() {
  149. try {
  150. refUpdateLockedRef.await();
  151. packRefsDone.await();
  152. } catch (InterruptedException e) {
  153. Thread.currentThread().interrupt();
  154. } catch (BrokenBarrierException e) {
  155. Thread.currentThread().interrupt();
  156. }
  157. return super.isForceUpdate();
  158. }
  159. };
  160. update.setForceUpdate(true);
  161. update.setNewObjectId(b);
  162. return update.update();
  163. }
  164. });
  165. pool.submit(new Callable<Void>() {
  166. public Void call() throws Exception {
  167. refUpdateLockedRef.await();
  168. gc.packRefs();
  169. packRefsDone.await();
  170. return null;
  171. }
  172. });
  173. assertSame(result.get(), Result.FORCED);
  174. } finally {
  175. pool.shutdownNow();
  176. pool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
  177. }
  178. assertEquals(repo.exactRef("refs/tags/t").getObjectId(), b);
  179. }
  180. @Test
  181. public void dontPackHEAD_nonBare() throws Exception {
  182. BranchBuilder bb = tr.branch("refs/heads/side");
  183. RevCommit first = bb.commit().add("A", "A").add("B", "B").create();
  184. bb.commit().add("A", "A2").add("B", "B2").create();
  185. Git git = Git.wrap(repo);
  186. // check for the unborn branch master. HEAD should point to master and
  187. // master doesn't exist.
  188. assertEquals(repo.exactRef("HEAD").getTarget().getName(),
  189. "refs/heads/master");
  190. assertNull(repo.exactRef("HEAD").getTarget().getObjectId());
  191. gc.packRefs();
  192. assertSame(repo.exactRef("HEAD").getStorage(), Storage.LOOSE);
  193. assertEquals(repo.exactRef("HEAD").getTarget().getName(),
  194. "refs/heads/master");
  195. assertNull(repo.exactRef("HEAD").getTarget().getObjectId());
  196. git.checkout().setName("refs/heads/side").call();
  197. gc.packRefs();
  198. assertSame(repo.exactRef("HEAD").getStorage(), Storage.LOOSE);
  199. // check for detached HEAD
  200. git.checkout().setName(first.getName()).call();
  201. gc.packRefs();
  202. assertSame(repo.exactRef("HEAD").getStorage(), Storage.LOOSE);
  203. }
  204. @Test
  205. public void dontPackHEAD_bare() throws Exception {
  206. BranchBuilder bb = tr.branch("refs/heads/side");
  207. bb.commit().add("A", "A").add("B", "B").create();
  208. RevCommit second = bb.commit().add("A", "A2").add("B", "B2").create();
  209. // Convert the repo to be bare
  210. FileBasedConfig cfg = repo.getConfig();
  211. cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  212. ConfigConstants.CONFIG_KEY_BARE, true);
  213. cfg.save();
  214. Git git = Git.open(repo.getDirectory());
  215. repo = (FileRepository) git.getRepository();
  216. // check for the unborn branch master. HEAD should point to master and
  217. // master doesn't exist.
  218. assertEquals(repo.exactRef("HEAD").getTarget().getName(),
  219. "refs/heads/master");
  220. assertNull(repo.exactRef("HEAD").getTarget().getObjectId());
  221. gc.packRefs();
  222. assertSame(repo.exactRef("HEAD").getStorage(), Storage.LOOSE);
  223. assertEquals(repo.exactRef("HEAD").getTarget().getName(),
  224. "refs/heads/master");
  225. assertNull(repo.exactRef("HEAD").getTarget().getObjectId());
  226. // check for non-detached HEAD
  227. repo.updateRef(Constants.HEAD).link("refs/heads/side");
  228. gc.packRefs();
  229. assertSame(repo.exactRef("HEAD").getStorage(), Storage.LOOSE);
  230. assertEquals(repo.exactRef("HEAD").getTarget().getObjectId(),
  231. second.getId());
  232. }
  233. }