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

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