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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.storage.file;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import org.eclipse.jgit.lib.Constants;
  48. import org.eclipse.jgit.lib.ObjectId;
  49. import org.eclipse.jgit.lib.RefRename;
  50. import org.eclipse.jgit.lib.RefUpdate;
  51. import org.eclipse.jgit.lib.RefUpdate.Result;
  52. import org.eclipse.jgit.revwalk.RevWalk;
  53. import org.eclipse.jgit.util.FileUtils;
  54. /**
  55. * Rename any reference stored by {@link RefDirectory}.
  56. * <p>
  57. * This class works by first renaming the source reference to a temporary name,
  58. * then renaming the temporary name to the final destination reference.
  59. * <p>
  60. * This strategy permits switching a reference like {@code refs/heads/foo},
  61. * which is a file, to {@code refs/heads/foo/bar}, which is stored inside a
  62. * directory that happens to match the source name.
  63. */
  64. class RefDirectoryRename extends RefRename {
  65. private final RefDirectory refdb;
  66. /**
  67. * The value of the source reference at the start of the rename.
  68. * <p>
  69. * At the end of the rename the destination reference must have this same
  70. * value, otherwise we have a concurrent update and the rename must fail
  71. * without making any changes.
  72. */
  73. private ObjectId objId;
  74. /** True if HEAD must be moved to the destination reference. */
  75. private boolean updateHEAD;
  76. /** A reference we backup {@link #objId} into during the rename. */
  77. private RefDirectoryUpdate tmp;
  78. RefDirectoryRename(RefDirectoryUpdate src, RefDirectoryUpdate dst) {
  79. super(src, dst);
  80. refdb = src.getRefDatabase();
  81. }
  82. @Override
  83. protected Result doRename() throws IOException {
  84. if (source.getRef().isSymbolic())
  85. return Result.IO_FAILURE; // not supported
  86. objId = source.getOldObjectId();
  87. updateHEAD = needToUpdateHEAD();
  88. tmp = refdb.newTemporaryUpdate();
  89. final RevWalk rw = new RevWalk(refdb.getRepository());
  90. try {
  91. // First backup the source so its never unreachable.
  92. tmp.setNewObjectId(objId);
  93. tmp.setForceUpdate(true);
  94. tmp.disableRefLog();
  95. switch (tmp.update(rw)) {
  96. case NEW:
  97. case FORCED:
  98. case NO_CHANGE:
  99. break;
  100. default:
  101. return tmp.getResult();
  102. }
  103. // Save the source's log under the temporary name, we must do
  104. // this before we delete the source, otherwise we lose the log.
  105. if (!renameLog(source, tmp))
  106. return Result.IO_FAILURE;
  107. // If HEAD has to be updated, link it now to destination.
  108. // We have to link before we delete, otherwise the delete
  109. // fails because its the current branch.
  110. RefUpdate dst = destination;
  111. if (updateHEAD) {
  112. if (!linkHEAD(destination)) {
  113. renameLog(tmp, source);
  114. return Result.LOCK_FAILURE;
  115. }
  116. // Replace the update operation so HEAD will log the rename.
  117. dst = refdb.newUpdate(Constants.HEAD, false);
  118. dst.setRefLogIdent(destination.getRefLogIdent());
  119. dst.setRefLogMessage(destination.getRefLogMessage(), false);
  120. }
  121. // Delete the source name so its path is free for replacement.
  122. source.setExpectedOldObjectId(objId);
  123. source.setForceUpdate(true);
  124. source.disableRefLog();
  125. if (source.delete(rw) != Result.FORCED) {
  126. renameLog(tmp, source);
  127. if (updateHEAD)
  128. linkHEAD(source);
  129. return source.getResult();
  130. }
  131. // Move the log to the destination.
  132. if (!renameLog(tmp, destination)) {
  133. renameLog(tmp, source);
  134. source.setExpectedOldObjectId(ObjectId.zeroId());
  135. source.setNewObjectId(objId);
  136. source.update(rw);
  137. if (updateHEAD)
  138. linkHEAD(source);
  139. return Result.IO_FAILURE;
  140. }
  141. // Create the destination, logging the rename during the creation.
  142. dst.setExpectedOldObjectId(ObjectId.zeroId());
  143. dst.setNewObjectId(objId);
  144. if (dst.update(rw) != Result.NEW) {
  145. // If we didn't create the destination we have to undo
  146. // our work. Put the log back and restore source.
  147. if (renameLog(destination, tmp))
  148. renameLog(tmp, source);
  149. source.setExpectedOldObjectId(ObjectId.zeroId());
  150. source.setNewObjectId(objId);
  151. source.update(rw);
  152. if (updateHEAD)
  153. linkHEAD(source);
  154. return dst.getResult();
  155. }
  156. return Result.RENAMED;
  157. } finally {
  158. // Always try to free the temporary name.
  159. try {
  160. refdb.delete(tmp);
  161. } catch (IOException err) {
  162. FileUtils.delete(refdb.fileFor(tmp.getName()));
  163. }
  164. rw.release();
  165. }
  166. }
  167. private boolean renameLog(RefUpdate src, RefUpdate dst) {
  168. File srcLog = refdb.getLogWriter().logFor(src.getName());
  169. File dstLog = refdb.getLogWriter().logFor(dst.getName());
  170. if (!srcLog.exists())
  171. return true;
  172. if (!rename(srcLog, dstLog))
  173. return false;
  174. try {
  175. final int levels = RefDirectory.levelsIn(src.getName()) - 2;
  176. RefDirectory.delete(srcLog, levels);
  177. return true;
  178. } catch (IOException e) {
  179. rename(dstLog, srcLog);
  180. return false;
  181. }
  182. }
  183. private static boolean rename(File src, File dst) {
  184. if (src.renameTo(dst))
  185. return true;
  186. File dir = dst.getParentFile();
  187. if ((dir.exists() || !dir.mkdirs()) && !dir.isDirectory())
  188. return false;
  189. return src.renameTo(dst);
  190. }
  191. private boolean linkHEAD(RefUpdate target) {
  192. try {
  193. RefUpdate u = refdb.newUpdate(Constants.HEAD, false);
  194. u.disableRefLog();
  195. switch (u.link(target.getName())) {
  196. case NEW:
  197. case FORCED:
  198. case NO_CHANGE:
  199. return true;
  200. default:
  201. return false;
  202. }
  203. } catch (IOException e) {
  204. return false;
  205. }
  206. }
  207. }