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.

RenameBranchCommandTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2012, GitHub Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertNotNull;
  15. import static org.junit.Assert.assertNull;
  16. import static org.junit.Assert.assertThrows;
  17. import static org.junit.Assert.assertTrue;
  18. import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
  19. import org.eclipse.jgit.junit.RepositoryTestCase;
  20. import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
  21. import org.eclipse.jgit.lib.ConfigConstants;
  22. import org.eclipse.jgit.lib.Constants;
  23. import org.eclipse.jgit.lib.Ref;
  24. import org.eclipse.jgit.lib.StoredConfig;
  25. import org.eclipse.jgit.revwalk.RevCommit;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. /**
  29. * Unit tests of {@link RenameBranchCommand}
  30. */
  31. public class RenameBranchCommandTest extends RepositoryTestCase {
  32. private static final String PATH = "file.txt";
  33. private RevCommit head;
  34. private Git git;
  35. @Override
  36. @Before
  37. public void setUp() throws Exception {
  38. super.setUp();
  39. git = Git.wrap(db);
  40. writeTrashFile(PATH, "content");
  41. git.add().addFilepattern(PATH).call();
  42. head = git.commit().setMessage("add file").call();
  43. assertNotNull(head);
  44. }
  45. @Test
  46. public void renameToExisting() throws Exception {
  47. assertNotNull(git.branchCreate().setName("foo").call());
  48. assertThrows(RefAlreadyExistsException.class, () -> git.branchRename()
  49. .setOldName("master").setNewName("foo").call());
  50. }
  51. @Test
  52. public void renameToTag() throws Exception {
  53. Ref ref = git.tag().setName("foo").call();
  54. assertNotNull(ref);
  55. assertEquals("Unexpected tag name", Constants.R_TAGS + "foo",
  56. ref.getName());
  57. ref = git.branchRename().setNewName("foo").call();
  58. assertNotNull(ref);
  59. assertEquals("Unexpected ref name", Constants.R_HEADS + "foo",
  60. ref.getName());
  61. // Check that we can rename it back
  62. ref = git.branchRename().setOldName("foo").setNewName(Constants.MASTER)
  63. .call();
  64. assertNotNull(ref);
  65. assertEquals("Unexpected ref name",
  66. Constants.R_HEADS + Constants.MASTER, ref.getName());
  67. }
  68. @Test
  69. public void renameToStupidName() throws Exception {
  70. Ref ref = git.branchRename().setNewName(Constants.R_HEADS + "foo")
  71. .call();
  72. assertEquals("Unexpected ref name",
  73. Constants.R_HEADS + Constants.R_HEADS + "foo",
  74. ref.getName());
  75. // And check that we can rename it back to a sane name
  76. ref = git.branchRename().setNewName("foo").call();
  77. assertNotNull(ref);
  78. assertEquals("Unexpected ref name", Constants.R_HEADS + "foo",
  79. ref.getName());
  80. }
  81. @Test
  82. public void renameBranchNoConfigValues() throws Exception {
  83. StoredConfig config = git.getRepository().getConfig();
  84. config.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
  85. Constants.MASTER);
  86. config.save();
  87. String branch = "b1";
  88. assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
  89. Constants.MASTER).isEmpty());
  90. assertNotNull(git.branchRename().setNewName(branch).call());
  91. config = git.getRepository().getConfig();
  92. assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
  93. Constants.MASTER).isEmpty());
  94. assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
  95. branch).isEmpty());
  96. }
  97. @Test
  98. public void renameBranchSingleConfigValue() throws Exception {
  99. StoredConfig config = git.getRepository().getConfig();
  100. config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  101. ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
  102. config.save();
  103. String branch = "b1";
  104. assertEquals(BranchRebaseMode.REBASE,
  105. config.getEnum(BranchRebaseMode.values(),
  106. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  107. ConfigConstants.CONFIG_KEY_REBASE,
  108. BranchRebaseMode.NONE));
  109. assertNull(config.getEnum(BranchRebaseMode.values(),
  110. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  111. ConfigConstants.CONFIG_KEY_REBASE, null));
  112. assertNotNull(git.branchRename().setNewName(branch).call());
  113. config = git.getRepository().getConfig();
  114. assertNull(config.getEnum(BranchRebaseMode.values(),
  115. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  116. ConfigConstants.CONFIG_KEY_REBASE, null));
  117. assertEquals(BranchRebaseMode.REBASE,
  118. config.getEnum(BranchRebaseMode.values(),
  119. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  120. ConfigConstants.CONFIG_KEY_REBASE,
  121. BranchRebaseMode.NONE));
  122. }
  123. @Test
  124. public void renameBranchExistingSection() throws Exception {
  125. String branch = "b1";
  126. StoredConfig config = git.getRepository().getConfig();
  127. config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  128. ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
  129. config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
  130. Constants.MASTER, "a", "a");
  131. config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a",
  132. "b");
  133. config.save();
  134. assertNotNull(git.branchRename().setNewName(branch).call());
  135. config = git.getRepository().getConfig();
  136. assertArrayEquals(new String[] { "b", "a" }, config.getStringList(
  137. ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a"));
  138. }
  139. @Test
  140. public void renameBranchMultipleConfigValues() throws Exception {
  141. StoredConfig config = git.getRepository().getConfig();
  142. config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  143. ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
  144. config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  145. Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true);
  146. config.save();
  147. String branch = "b1";
  148. assertEquals(BranchRebaseMode.REBASE,
  149. config.getEnum(BranchRebaseMode.values(),
  150. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  151. ConfigConstants.CONFIG_KEY_REBASE,
  152. BranchRebaseMode.NONE));
  153. assertNull(config.getEnum(BranchRebaseMode.values(),
  154. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  155. ConfigConstants.CONFIG_KEY_REBASE, null));
  156. assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  157. Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true));
  158. assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  159. branch, ConfigConstants.CONFIG_KEY_MERGE, false));
  160. assertNotNull(git.branchRename().setNewName(branch).call());
  161. config = git.getRepository().getConfig();
  162. assertNull(config.getEnum(BranchRebaseMode.values(),
  163. ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
  164. ConfigConstants.CONFIG_KEY_REBASE, null));
  165. assertEquals(BranchRebaseMode.REBASE,
  166. config.getEnum(BranchRebaseMode.values(),
  167. ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  168. ConfigConstants.CONFIG_KEY_REBASE,
  169. BranchRebaseMode.NONE));
  170. assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  171. Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, false));
  172. assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
  173. branch, ConfigConstants.CONFIG_KEY_MERGE, false));
  174. }
  175. }