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.

RefDirectoryRename.java 6.8KB

Rewrite reference handling to be abstract and accurate This commit actually does three major changes to the way references are handled within JGit. Unfortunately they were easier to do as a single massive commit than to break them up into smaller units. Disambiguate symbolic references: --------------------------------- Reporting a symbolic reference such as HEAD as though it were any other normal reference like refs/heads/master causes subtle programming errors. We have been bitten by this error on several occasions, as have some downstream applications written by myself. Instead of reporting HEAD as a reference whose name differs from its "original name", report it as an actual SymbolicRef object that the application can test the type and examine the target of. With this change, Ref is now an abstract type with different subclasses for the different types. In the classical example of "HEAD" being a symbolic reference to branch "refs/heads/master", the Repository.getAllRefs() method will now return: Map<String, Ref> all = repository.getAllRefs(); SymbolicRef HEAD = (SymbolicRef) all.get("HEAD"); ObjectIdRef master = (ObjectIdRef) all.get("refs/heads/master"); assertSame(master, HEAD.getTarget()); assertSame(master.getObjectId(), HEAD.getObjectId()); assertEquals("HEAD", HEAD.getName()); assertEquals("refs/heads/master", master.getName()); A nice side-effect of this change is the storage type of the symbolic reference is no longer ambiguous with the storge type of the underlying reference it targets. In the above example, if master was only available in the packed-refs file, then the following is also true: assertSame(Ref.Storage.LOOSE, HEAD.getStorage()); assertSame(Ref.Storage.PACKED, master.getStorage()); (Prior to this change we returned the ambiguous storage of LOOSE_PACKED for HEAD, which was confusing since it wasn't actually true on disk). Another nice side-effect of this change is all intermediate symbolic references are preserved, and are therefore visible to the application when they walk the target chain. We can now correctly inspect chains of symbolic references. As a result of this change the Ref.getOrigName() method has been removed from the API. Applications should identify a symbolic reference by testing for isSymbolic() and not by using an arcane string comparsion between properties. Abstract the RefDatabase storage: --------------------------------- RefDatabase is now abstract, similar to ObjectDatabase, and a new concrete implementation called RefDirectory is used for the traditional on-disk storage layout. In the future we plan to support additional implementations, such as a pure in-memory RefDatabase for unit testing purposes. Optimize RefDirectory: ---------------------- The implementation of the in-memory reference cache, reading, and update routines has been completely rewritten. Much of the code was heavily borrowed or cribbed from the prior implementation, so copyright notices have been left intact as much as possible. The RefDirectory cache no longer confuses symbolic references with normal references. This permits the cache to resolve the value of a symbolic reference as late as possible, ensuring it is always current, without needing to maintain reverse pointers. The cache is now 2 sorted RefLists, rather than 3 HashMaps. Using sorted lists allows the implementation to reduce the in-memory footprint when storing many refs. Using specialized types for the elements allows the code to avoid additional map lookups for auxiliary stat information. To improve scan time during getRefs(), the lists are returned via a copy-on-write contract. Most callers of getRefs() do not modify the returned collections, so the copy-on-write semantics improves access on repositories with a large number of packed references. Iterator traversals of the returned Map<String,Ref> are performed using a simple merge-join of the two cache lists, ensuring we can perform the entire traversal in linear time as a function of the number of references: O(PackedRefs + LooseRefs). Scans of the loose reference space to update the cache run in O(LooseRefs log LooseRefs) time, as the directory contents are sorted before being merged against the in-memory cache. Since the majority of stable references are kept packed, there typically are only a handful of reference names to be sorted, so the sorting cost should not be very high. Locking is reduced during getRefs() by taking advantage of the copy-on-write semantics of the improved cache data structure. This permits concurrent readers to pull back references without blocking each other. If there is contention updating the cache during a scan, one or more updates are simply skipped and will get picked up again in a future scan. Writing to the $GIT_DIR/packed-refs during reference delete is now fully atomic. The file is locked, reparsed fresh, and written back out if a change is necessary. This avoids all race conditions with concurrent external updates of the packed-refs file. The RefLogWriter class has been fully folded into RefDirectory and is therefore deleted. Maintaining the reference's log is the responsiblity of the database implementation, and not all implementations will use java.io for access. Future work still remains to be done to abstract the ReflogReader class away from local disk IO. Change-Id: I26b9287c45a4b2d2be35ba2849daa316f5eec85d Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
14 years ago
Rewrite reference handling to be abstract and accurate This commit actually does three major changes to the way references are handled within JGit. Unfortunately they were easier to do as a single massive commit than to break them up into smaller units. Disambiguate symbolic references: --------------------------------- Reporting a symbolic reference such as HEAD as though it were any other normal reference like refs/heads/master causes subtle programming errors. We have been bitten by this error on several occasions, as have some downstream applications written by myself. Instead of reporting HEAD as a reference whose name differs from its "original name", report it as an actual SymbolicRef object that the application can test the type and examine the target of. With this change, Ref is now an abstract type with different subclasses for the different types. In the classical example of "HEAD" being a symbolic reference to branch "refs/heads/master", the Repository.getAllRefs() method will now return: Map<String, Ref> all = repository.getAllRefs(); SymbolicRef HEAD = (SymbolicRef) all.get("HEAD"); ObjectIdRef master = (ObjectIdRef) all.get("refs/heads/master"); assertSame(master, HEAD.getTarget()); assertSame(master.getObjectId(), HEAD.getObjectId()); assertEquals("HEAD", HEAD.getName()); assertEquals("refs/heads/master", master.getName()); A nice side-effect of this change is the storage type of the symbolic reference is no longer ambiguous with the storge type of the underlying reference it targets. In the above example, if master was only available in the packed-refs file, then the following is also true: assertSame(Ref.Storage.LOOSE, HEAD.getStorage()); assertSame(Ref.Storage.PACKED, master.getStorage()); (Prior to this change we returned the ambiguous storage of LOOSE_PACKED for HEAD, which was confusing since it wasn't actually true on disk). Another nice side-effect of this change is all intermediate symbolic references are preserved, and are therefore visible to the application when they walk the target chain. We can now correctly inspect chains of symbolic references. As a result of this change the Ref.getOrigName() method has been removed from the API. Applications should identify a symbolic reference by testing for isSymbolic() and not by using an arcane string comparsion between properties. Abstract the RefDatabase storage: --------------------------------- RefDatabase is now abstract, similar to ObjectDatabase, and a new concrete implementation called RefDirectory is used for the traditional on-disk storage layout. In the future we plan to support additional implementations, such as a pure in-memory RefDatabase for unit testing purposes. Optimize RefDirectory: ---------------------- The implementation of the in-memory reference cache, reading, and update routines has been completely rewritten. Much of the code was heavily borrowed or cribbed from the prior implementation, so copyright notices have been left intact as much as possible. The RefDirectory cache no longer confuses symbolic references with normal references. This permits the cache to resolve the value of a symbolic reference as late as possible, ensuring it is always current, without needing to maintain reverse pointers. The cache is now 2 sorted RefLists, rather than 3 HashMaps. Using sorted lists allows the implementation to reduce the in-memory footprint when storing many refs. Using specialized types for the elements allows the code to avoid additional map lookups for auxiliary stat information. To improve scan time during getRefs(), the lists are returned via a copy-on-write contract. Most callers of getRefs() do not modify the returned collections, so the copy-on-write semantics improves access on repositories with a large number of packed references. Iterator traversals of the returned Map<String,Ref> are performed using a simple merge-join of the two cache lists, ensuring we can perform the entire traversal in linear time as a function of the number of references: O(PackedRefs + LooseRefs). Scans of the loose reference space to update the cache run in O(LooseRefs log LooseRefs) time, as the directory contents are sorted before being merged against the in-memory cache. Since the majority of stable references are kept packed, there typically are only a handful of reference names to be sorted, so the sorting cost should not be very high. Locking is reduced during getRefs() by taking advantage of the copy-on-write semantics of the improved cache data structure. This permits concurrent readers to pull back references without blocking each other. If there is contention updating the cache during a scan, one or more updates are simply skipped and will get picked up again in a future scan. Writing to the $GIT_DIR/packed-refs during reference delete is now fully atomic. The file is locked, reparsed fresh, and written back out if a change is necessary. This avoids all race conditions with concurrent external updates of the packed-refs file. The RefLogWriter class has been fully folded into RefDirectory and is therefore deleted. Maintaining the reference's log is the responsiblity of the database implementation, and not all implementations will use java.io for access. Future work still remains to be done to abstract the ReflogReader class away from local disk IO. Change-Id: I26b9287c45a4b2d2be35ba2849daa316f5eec85d Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
14 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * Copyright (C) 2009, Robin Rosenberg
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import org.eclipse.jgit.lib.RefUpdate.Result;
  48. import org.eclipse.jgit.revwalk.RevWalk;
  49. /**
  50. * Rename any reference stored by {@link RefDirectory}.
  51. * <p>
  52. * This class works by first renaming the source reference to a temporary name,
  53. * then renaming the temporary name to the final destination reference.
  54. * <p>
  55. * This strategy permits switching a reference like {@code refs/heads/foo},
  56. * which is a file, to {@code refs/heads/foo/bar}, which is stored inside a
  57. * directory that happens to match the source name.
  58. */
  59. class RefDirectoryRename extends RefRename {
  60. private final RefDirectory refdb;
  61. /**
  62. * The value of the source reference at the start of the rename.
  63. * <p>
  64. * At the end of the rename the destination reference must have this same
  65. * value, otherwise we have a concurrent update and the rename must fail
  66. * without making any changes.
  67. */
  68. private ObjectId objId;
  69. /** True if HEAD must be moved to the destination reference. */
  70. private boolean updateHEAD;
  71. /** A reference we backup {@link #objId} into during the rename. */
  72. private RefDirectoryUpdate tmp;
  73. RefDirectoryRename(RefDirectoryUpdate src, RefDirectoryUpdate dst) {
  74. super(src, dst);
  75. refdb = src.getRefDatabase();
  76. }
  77. @Override
  78. protected Result doRename() throws IOException {
  79. if (source.getRef().isSymbolic())
  80. return Result.IO_FAILURE; // not supported
  81. final RevWalk rw = new RevWalk(refdb.getRepository());
  82. objId = source.getOldObjectId();
  83. updateHEAD = needToUpdateHEAD();
  84. tmp = refdb.newTemporaryUpdate();
  85. try {
  86. // First backup the source so its never unreachable.
  87. tmp.setNewObjectId(objId);
  88. tmp.setForceUpdate(true);
  89. tmp.disableRefLog();
  90. switch (tmp.update(rw)) {
  91. case NEW:
  92. case FORCED:
  93. case NO_CHANGE:
  94. break;
  95. default:
  96. return tmp.getResult();
  97. }
  98. // Save the source's log under the temporary name, we must do
  99. // this before we delete the source, otherwise we lose the log.
  100. if (!renameLog(source, tmp))
  101. return Result.IO_FAILURE;
  102. // If HEAD has to be updated, link it now to destination.
  103. // We have to link before we delete, otherwise the delete
  104. // fails because its the current branch.
  105. RefUpdate dst = destination;
  106. if (updateHEAD) {
  107. if (!linkHEAD(destination)) {
  108. renameLog(tmp, source);
  109. return Result.LOCK_FAILURE;
  110. }
  111. // Replace the update operation so HEAD will log the rename.
  112. dst = refdb.newUpdate(Constants.HEAD, false);
  113. dst.setRefLogIdent(destination.getRefLogIdent());
  114. dst.setRefLogMessage(destination.getRefLogMessage(), false);
  115. }
  116. // Delete the source name so its path is free for replacement.
  117. source.setExpectedOldObjectId(objId);
  118. source.setForceUpdate(true);
  119. source.disableRefLog();
  120. if (source.delete(rw) != Result.FORCED) {
  121. renameLog(tmp, source);
  122. if (updateHEAD)
  123. linkHEAD(source);
  124. return source.getResult();
  125. }
  126. // Move the log to the destination.
  127. if (!renameLog(tmp, destination)) {
  128. renameLog(tmp, source);
  129. source.setExpectedOldObjectId(ObjectId.zeroId());
  130. source.setNewObjectId(objId);
  131. source.update(rw);
  132. if (updateHEAD)
  133. linkHEAD(source);
  134. return Result.IO_FAILURE;
  135. }
  136. // Create the destination, logging the rename during the creation.
  137. dst.setExpectedOldObjectId(ObjectId.zeroId());
  138. dst.setNewObjectId(objId);
  139. if (dst.update(rw) != Result.NEW) {
  140. // If we didn't create the destination we have to undo
  141. // our work. Put the log back and restore source.
  142. if (renameLog(destination, tmp))
  143. renameLog(tmp, source);
  144. source.setExpectedOldObjectId(ObjectId.zeroId());
  145. source.setNewObjectId(objId);
  146. source.update(rw);
  147. if (updateHEAD)
  148. linkHEAD(source);
  149. return dst.getResult();
  150. }
  151. return Result.RENAMED;
  152. } finally {
  153. // Always try to free the temporary name.
  154. try {
  155. refdb.delete(tmp);
  156. } catch (IOException err) {
  157. refdb.fileFor(tmp.getName()).delete();
  158. }
  159. }
  160. }
  161. private boolean renameLog(RefUpdate src, RefUpdate dst) {
  162. File srcLog = refdb.logFor(src.getName());
  163. File dstLog = refdb.logFor(dst.getName());
  164. if (!srcLog.exists())
  165. return true;
  166. if (!rename(srcLog, dstLog))
  167. return false;
  168. try {
  169. final int levels = RefDirectory.levelsIn(src.getName()) - 2;
  170. RefDirectory.delete(srcLog, levels);
  171. return true;
  172. } catch (IOException e) {
  173. rename(dstLog, srcLog);
  174. return false;
  175. }
  176. }
  177. private static boolean rename(File src, File dst) {
  178. if (src.renameTo(dst))
  179. return true;
  180. File dir = dst.getParentFile();
  181. if ((dir.exists() || !dir.mkdirs()) && !dir.isDirectory())
  182. return false;
  183. return src.renameTo(dst);
  184. }
  185. private boolean linkHEAD(RefUpdate target) {
  186. try {
  187. RefUpdate u = refdb.newUpdate(Constants.HEAD, false);
  188. u.disableRefLog();
  189. switch (u.link(target.getName())) {
  190. case NEW:
  191. case FORCED:
  192. case NO_CHANGE:
  193. return true;
  194. default:
  195. return false;
  196. }
  197. } catch (IOException e) {
  198. return false;
  199. }
  200. }
  201. }