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

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. }