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.2KB

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