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

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