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

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