View Javadoc
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  
12  import static org.junit.Assert.assertArrayEquals;
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertNull;
17  import static org.junit.Assert.assertThrows;
18  import static org.junit.Assert.assertTrue;
19  
20  import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
21  import org.eclipse.jgit.junit.RepositoryTestCase;
22  import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
23  import org.eclipse.jgit.lib.ConfigConstants;
24  import org.eclipse.jgit.lib.Constants;
25  import org.eclipse.jgit.lib.Ref;
26  import org.eclipse.jgit.lib.StoredConfig;
27  import org.eclipse.jgit.revwalk.RevCommit;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  /**
32   * Unit tests of {@link RenameBranchCommand}
33   */
34  public class RenameBranchCommandTest extends RepositoryTestCase {
35  
36  	private static final String PATH = "file.txt";
37  
38  	private RevCommit head;
39  
40  	private Git git;
41  
42  	@Override
43  	@Before
44  	public void setUp() throws Exception {
45  		super.setUp();
46  		git = Git.wrap(db);
47  		writeTrashFile(PATH, "content");
48  		git.add().addFilepattern(PATH).call();
49  		head = git.commit().setMessage("add file").call();
50  		assertNotNull(head);
51  	}
52  
53  	@Test
54  	public void renameToExisting() throws Exception {
55  		assertNotNull(git.branchCreate().setName("foo").call());
56  		assertThrows(RefAlreadyExistsException.class, () -> git.branchRename()
57  				.setOldName("master").setNewName("foo").call());
58  	}
59  
60  	@Test
61  	public void renameToTag() throws Exception {
62  		Ref ref = git.tag().setName("foo").call();
63  		assertNotNull(ref);
64  		assertEquals("Unexpected tag name", Constants.R_TAGS + "foo",
65  				ref.getName());
66  		ref = git.branchRename().setNewName("foo").call();
67  		assertNotNull(ref);
68  		assertEquals("Unexpected ref name", Constants.R_HEADS + "foo",
69  				ref.getName());
70  		// Check that we can rename it back
71  		ref = git.branchRename().setOldName("foo").setNewName(Constants.MASTER)
72  				.call();
73  		assertNotNull(ref);
74  		assertEquals("Unexpected ref name",
75  				Constants.R_HEADS + Constants.MASTER, ref.getName());
76  	}
77  
78  	@Test
79  	public void renameToStupidName() throws Exception {
80  		Ref ref = git.branchRename().setNewName(Constants.R_HEADS + "foo")
81  				.call();
82  		assertEquals("Unexpected ref name",
83  				Constants.R_HEADS + Constants.R_HEADS + "foo",
84  				ref.getName());
85  		// And check that we can rename it back to a sane name
86  		ref = git.branchRename().setNewName("foo").call();
87  		assertNotNull(ref);
88  		assertEquals("Unexpected ref name", Constants.R_HEADS + "foo",
89  				ref.getName());
90  	}
91  
92  	@Test
93  	public void renameBranchNoConfigValues() throws Exception {
94  		StoredConfig config = git.getRepository().getConfig();
95  		config.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
96  				Constants.MASTER);
97  		config.save();
98  
99  		String branch = "b1";
100 		assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
101 				Constants.MASTER).isEmpty());
102 		assertNotNull(git.branchRename().setNewName(branch).call());
103 
104 		config = git.getRepository().getConfig();
105 		assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
106 				Constants.MASTER).isEmpty());
107 		assertTrue(config.getNames(ConfigConstants.CONFIG_BRANCH_SECTION,
108 				branch).isEmpty());
109 	}
110 
111 	@Test
112 	public void renameBranchSingleConfigValue() throws Exception {
113 		StoredConfig config = git.getRepository().getConfig();
114 		config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
115 				ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
116 		config.save();
117 
118 		String branch = "b1";
119 
120 		assertEquals(BranchRebaseMode.REBASE,
121 				config.getEnum(BranchRebaseMode.values(),
122 						ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
123 						ConfigConstants.CONFIG_KEY_REBASE,
124 						BranchRebaseMode.NONE));
125 		assertNull(config.getEnum(BranchRebaseMode.values(),
126 				ConfigConstants.CONFIG_BRANCH_SECTION, branch,
127 				ConfigConstants.CONFIG_KEY_REBASE, null));
128 
129 		assertNotNull(git.branchRename().setNewName(branch).call());
130 
131 		config = git.getRepository().getConfig();
132 		assertNull(config.getEnum(BranchRebaseMode.values(),
133 				ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
134 				ConfigConstants.CONFIG_KEY_REBASE, null));
135 		assertEquals(BranchRebaseMode.REBASE,
136 				config.getEnum(BranchRebaseMode.values(),
137 						ConfigConstants.CONFIG_BRANCH_SECTION, branch,
138 						ConfigConstants.CONFIG_KEY_REBASE,
139 						BranchRebaseMode.NONE));
140 	}
141 
142 	@Test
143 	public void renameBranchExistingSection() throws Exception {
144 		String branch = "b1";
145 		StoredConfig config = git.getRepository().getConfig();
146 		config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
147 				ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
148 		config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
149 				Constants.MASTER, "a", "a");
150 		config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a",
151 				"b");
152 		config.save();
153 
154 		assertNotNull(git.branchRename().setNewName(branch).call());
155 
156 		config = git.getRepository().getConfig();
157 		assertArrayEquals(new String[] { "b", "a" }, config.getStringList(
158 				ConfigConstants.CONFIG_BRANCH_SECTION, branch, "a"));
159 	}
160 
161 	@Test
162 	public void renameBranchMultipleConfigValues() throws Exception {
163 		StoredConfig config = git.getRepository().getConfig();
164 		config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
165 				ConfigConstants.CONFIG_KEY_REBASE, BranchRebaseMode.REBASE);
166 		config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
167 				Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true);
168 		config.save();
169 
170 		String branch = "b1";
171 
172 		assertEquals(BranchRebaseMode.REBASE,
173 				config.getEnum(BranchRebaseMode.values(),
174 						ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
175 						ConfigConstants.CONFIG_KEY_REBASE,
176 						BranchRebaseMode.NONE));
177 		assertNull(config.getEnum(BranchRebaseMode.values(),
178 				ConfigConstants.CONFIG_BRANCH_SECTION, branch,
179 				ConfigConstants.CONFIG_KEY_REBASE, null));
180 		assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
181 				Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, true));
182 		assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
183 				branch, ConfigConstants.CONFIG_KEY_MERGE, false));
184 
185 		assertNotNull(git.branchRename().setNewName(branch).call());
186 
187 		config = git.getRepository().getConfig();
188 		assertNull(config.getEnum(BranchRebaseMode.values(),
189 				ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
190 				ConfigConstants.CONFIG_KEY_REBASE, null));
191 		assertEquals(BranchRebaseMode.REBASE,
192 				config.getEnum(BranchRebaseMode.values(),
193 						ConfigConstants.CONFIG_BRANCH_SECTION, branch,
194 						ConfigConstants.CONFIG_KEY_REBASE,
195 						BranchRebaseMode.NONE));
196 		assertFalse(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
197 				Constants.MASTER, ConfigConstants.CONFIG_KEY_MERGE, false));
198 		assertTrue(config.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION,
199 				branch, ConfigConstants.CONFIG_KEY_MERGE, false));
200 	}
201 }