View Javadoc
1   /*
2    * Copyright (C) 2012, GitHub Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.api;
44  
45  import static org.junit.Assert.assertArrayEquals;
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertNotNull;
49  import static org.junit.Assert.assertNull;
50  import static org.junit.Assert.assertTrue;
51  
52  import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
53  import org.eclipse.jgit.junit.RepositoryTestCase;
54  import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
55  import org.eclipse.jgit.lib.ConfigConstants;
56  import org.eclipse.jgit.lib.Constants;
57  import org.eclipse.jgit.lib.Ref;
58  import org.eclipse.jgit.lib.StoredConfig;
59  import org.eclipse.jgit.revwalk.RevCommit;
60  import org.junit.Before;
61  import org.junit.Rule;
62  import org.junit.Test;
63  import org.junit.rules.ExpectedException;
64  
65  /**
66   * Unit tests of {@link RenameBranchCommand}
67   */
68  public class RenameBranchCommandTest extends RepositoryTestCase {
69  
70  	private static final String PATH = "file.txt";
71  
72  	private RevCommit head;
73  
74  	private Git git;
75  
76  	@Rule
77  	public ExpectedException thrown = ExpectedException.none();
78  
79  	@Override
80  	@Before
81  	public void setUp() throws Exception {
82  		super.setUp();
83  		git = Git.wrap(db);
84  		writeTrashFile(PATH, "content");
85  		git.add().addFilepattern(PATH).call();
86  		head = git.commit().setMessage("add file").call();
87  		assertNotNull(head);
88  	}
89  
90  	@Test
91  	public void renameToExisting() throws Exception {
92  		assertNotNull(git.branchCreate().setName("foo").call());
93  		thrown.expect(RefAlreadyExistsException.class);
94  		git.branchRename().setOldName("master").setNewName("foo").call();
95  	}
96  
97  	@Test
98  	public void renameToTag() throws Exception {
99  		Ref ref = git.tag().setName("foo").call();
100 		assertNotNull(ref);
101 		assertEquals("Unexpected tag name", Constants.R_TAGS + "foo",
102 				ref.getName());
103 		ref = git.branchRename().setNewName("foo").call();
104 		assertNotNull(ref);
105 		assertEquals("Unexpected ref name", Constants.R_HEADS + "foo",
106 				ref.getName());
107 		// Check that we can rename it back
108 		ref = git.branchRename().setOldName("foo").setNewName(Constants.MASTER)
109 				.call();
110 		assertNotNull(ref);
111 		assertEquals("Unexpected ref name",
112 				Constants.R_HEADS + Constants.MASTER, ref.getName());
113 	}
114 
115 	@Test
116 	public void renameToStupidName() throws Exception {
117 		Ref ref = git.branchRename().setNewName(Constants.R_HEADS + "foo")
118 				.call();
119 		assertEquals("Unexpected ref name",
120 				Constants.R_HEADS + Constants.R_HEADS + "foo",
121 				ref.getName());
122 		// And check that we can rename it back to a sane name
123 		ref = git.branchRename().setNewName("foo").call();
124 		assertNotNull(ref);
125 		assertEquals("Unexpected ref name", Constants.R_HEADS + "foo",
126 				ref.getName());
127 	}
128 
129 	@Test
130 	public void renameBranchNoConfigValues() throws Exception {
131 		StoredConfig config = git.getRepository().getConfig();
132 		config.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
133 				Constants.MASTER);
134 		config.save();
135 
136 		String branch = "b1";
137 		assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
138 				Constants.MASTER).isEmpty());
139 		assertNotNull(git.branchRename().setNewName(branch).call());
140 
141 		config = git.getRepository().getConfig();
142 		assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
143 				Constants.MASTER).isEmpty());
144 		assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
145 				branch).isEmpty());
146 	}
147 
148 	@Test
149 	public void renameBranchSingleConfigValue() throws Exception {
150 		StoredConfig config = git.getRepository().getConfig();
151 		config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
152 				ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
153 		config.save();
154 
155 		String branch = "b1";
156 
157 		assertEquals(BranchRebaseMode.REBASE,
158 				config.getEnum(BranchRebaseMode.values(),
159 						ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
160 						ConfigConstants.CONFIG_KEY_REBASE,
161 						BranchRebaseMode.NONE));
162 		assertNull(config.getEnum(BranchRebaseMode.values(),
163 				ConfigConstants.CONFIG_BRANCH_SECTION, branch,
164 				ConfigConstants.CONFIG_KEY_REBASE, null));
165 
166 		assertNotNull(git.branchRename().setNewName(branch).call());
167 
168 		config = git.getRepository().getConfig();
169 		assertNull(config.getEnum(BranchRebaseMode.values(),
170 				ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
171 				ConfigConstants.CONFIG_KEY_REBASE, null));
172 		assertEquals(BranchRebaseMode.REBASE,
173 				config.getEnum(BranchRebaseMode.values(),
174 						ConfigConstants.CONFIG_BRANCH_SECTION, branch,
175 						ConfigConstants.CONFIG_KEY_REBASE,
176 						BranchRebaseMode.NONE));
177 	}
178 
179 	@Test
180 	public void renameBranchExistingSection() throws Exception {
181 		String branch = "b1";
182 		StoredConfig config = git.getRepository().getConfig();
183 		config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
184 				ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
185 		config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
186 				Constants.MASTER, "a", "a");
187 		config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a",
188 				"b");
189 		config.save();
190 
191 		assertNotNull(git.branchRename().setNewName(branch).call());
192 
193 		config = git.getRepository().getConfig();
194 		assertArrayEquals(new String[] { "b", "a" }, config.getStringList(
195 				ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a"));
196 	}
197 
198 	@Test
199 	public void renameBranchMultipleConfigValues() throws Exception {
200 		StoredConfig config = git.getRepository().getConfig();
201 		config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
202 				ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
203 		config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
204 				Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true);
205 		config.save();
206 
207 		String branch = "b1";
208 
209 		assertEquals(BranchRebaseMode.REBASE,
210 				config.getEnum(BranchRebaseMode.values(),
211 						ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
212 						ConfigConstants.CONFIG_KEY_REBASE,
213 						BranchRebaseMode.NONE));
214 		assertNull(config.getEnum(BranchRebaseMode.values(),
215 				ConfigConstants.CONFIG_BRANCH_SECTION, branch,
216 				ConfigConstants.CONFIG_KEY_REBASE, null));
217 		assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
218 				Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true));
219 		assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
220 				branch, ConfigConstants.CONFIG_KEY_MERGE, false));
221 
222 		assertNotNull(git.branchRename().setNewName(branch).call());
223 
224 		config = git.getRepository().getConfig();
225 		assertNull(config.getEnum(BranchRebaseMode.values(),
226 				ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
227 				ConfigConstants.CONFIG_KEY_REBASE, null));
228 		assertEquals(BranchRebaseMode.REBASE,
229 				config.getEnum(BranchRebaseMode.values(),
230 						ConfigConstants.CONFIG_BRANCH_SECTION, branch,
231 						ConfigConstants.CONFIG_KEY_REBASE,
232 						BranchRebaseMode.NONE));
233 		assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
234 				Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, false));
235 		assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
236 				branch, ConfigConstants.CONFIG_KEY_MERGE, false));
237 	}
238 }