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.

RenameBranchCommand.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  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.api;
  45. import java.io.IOException;
  46. import java.text.MessageFormat;
  47. import org.eclipse.jgit.JGitText;
  48. import org.eclipse.jgit.api.errors.DetachedHeadException;
  49. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  50. import org.eclipse.jgit.api.errors.JGitInternalException;
  51. import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
  52. import org.eclipse.jgit.api.errors.RefNotFoundException;
  53. import org.eclipse.jgit.lib.ConfigConstants;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.Ref;
  57. import org.eclipse.jgit.lib.RefRename;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.lib.StoredConfig;
  60. import org.eclipse.jgit.lib.RefUpdate.Result;
  61. /**
  62. * Used to rename branches.
  63. *
  64. * @see <a
  65. * href="http://www.kernel.org/pub/software/scm/git/docs/git-branch.html"
  66. * >Git documentation about Branch</a>
  67. */
  68. public class RenameBranchCommand extends GitCommand<Ref> {
  69. private String oldName;
  70. private String newName;
  71. /**
  72. * @param repo
  73. */
  74. protected RenameBranchCommand(Repository repo) {
  75. super(repo);
  76. }
  77. /**
  78. * @throws RefNotFoundException
  79. * if the old branch can not be found (branch with provided old
  80. * name does not exist or old name resolves to a tag)
  81. * @throws InvalidRefNameException
  82. * if the provided new name is <code>null</code> or otherwise
  83. * invalid
  84. * @throws RefAlreadyExistsException
  85. * if a branch with the new name already exists
  86. * @throws DetachedHeadException
  87. * if rename is tried without specifying the old name and HEAD
  88. * is detached
  89. */
  90. public Ref call() throws RefNotFoundException, InvalidRefNameException,
  91. RefAlreadyExistsException, DetachedHeadException {
  92. checkCallable();
  93. if (newName == null)
  94. throw new InvalidRefNameException(MessageFormat.format(JGitText
  95. .get().branchNameInvalid, "<null>"));
  96. try {
  97. String fullOldName;
  98. String fullNewName;
  99. if (repo.getRef(newName) != null)
  100. throw new RefAlreadyExistsException(MessageFormat.format(
  101. JGitText.get().refAlreadExists, newName));
  102. if (oldName != null) {
  103. Ref ref = repo.getRef(oldName);
  104. if (ref == null)
  105. throw new RefNotFoundException(MessageFormat.format(
  106. JGitText.get().refNotResolved, oldName));
  107. if (ref.getName().startsWith(Constants.R_TAGS))
  108. throw new RefNotFoundException(MessageFormat.format(
  109. JGitText.get().renameBranchFailedBecauseTag,
  110. oldName));
  111. fullOldName = ref.getName();
  112. } else {
  113. fullOldName = repo.getFullBranch();
  114. if (ObjectId.isId(fullOldName))
  115. throw new DetachedHeadException();
  116. }
  117. if (fullOldName.startsWith(Constants.R_REMOTES))
  118. fullNewName = Constants.R_REMOTES + newName;
  119. else {
  120. fullNewName = Constants.R_HEADS + newName;
  121. }
  122. if (!Repository.isValidRefName(fullNewName))
  123. throw new InvalidRefNameException(MessageFormat.format(JGitText
  124. .get().branchNameInvalid, fullNewName));
  125. RefRename rename = repo.renameRef(fullOldName, fullNewName);
  126. Result renameResult = rename.rename();
  127. setCallable(false);
  128. boolean ok = Result.RENAMED == renameResult;
  129. if (ok) {
  130. if (fullNewName.startsWith(Constants.R_HEADS)) {
  131. // move the upstream configuration over to the new branch
  132. String shortOldName = fullOldName
  133. .substring(Constants.R_HEADS.length());
  134. final StoredConfig repoConfig = repo.getConfig();
  135. String oldRemote = repoConfig.getString(
  136. ConfigConstants.CONFIG_BRANCH_SECTION,
  137. shortOldName, ConfigConstants.CONFIG_KEY_REMOTE);
  138. if (oldRemote != null) {
  139. repoConfig.setString(
  140. ConfigConstants.CONFIG_BRANCH_SECTION, newName,
  141. ConfigConstants.CONFIG_KEY_REMOTE, oldRemote);
  142. }
  143. String oldMerge = repoConfig.getString(
  144. ConfigConstants.CONFIG_BRANCH_SECTION,
  145. shortOldName, ConfigConstants.CONFIG_KEY_MERGE);
  146. if (oldMerge != null) {
  147. repoConfig.setString(
  148. ConfigConstants.CONFIG_BRANCH_SECTION, newName,
  149. ConfigConstants.CONFIG_KEY_MERGE, oldMerge);
  150. }
  151. repoConfig
  152. .unsetSection(
  153. ConfigConstants.CONFIG_BRANCH_SECTION,
  154. shortOldName);
  155. repoConfig.save();
  156. }
  157. } else
  158. throw new JGitInternalException(MessageFormat.format(JGitText
  159. .get().renameBranchUnexpectedResult, renameResult
  160. .name()));
  161. Ref resultRef = repo.getRef(newName);
  162. if (resultRef == null)
  163. throw new JGitInternalException(
  164. JGitText.get().renameBranchFailedUnknownReason);
  165. return resultRef;
  166. } catch (IOException ioe) {
  167. throw new JGitInternalException(ioe.getMessage(), ioe);
  168. }
  169. }
  170. /**
  171. * @param newName
  172. * the new name
  173. * @return this instance
  174. */
  175. public RenameBranchCommand setNewName(String newName) {
  176. checkCallable();
  177. this.newName = newName;
  178. return this;
  179. }
  180. /**
  181. * @param oldName
  182. * the name of the branch to rename; if not set, the currently
  183. * checked out branch (if any) will be renamed
  184. * @return this instance
  185. */
  186. public RenameBranchCommand setOldName(String oldName) {
  187. checkCallable();
  188. this.oldName = oldName;
  189. return this;
  190. }
  191. }