1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.api;
45
46 import java.io.IOException;
47 import java.text.MessageFormat;
48 import java.util.Arrays;
49
50 import org.eclipse.jgit.api.errors.DetachedHeadException;
51 import org.eclipse.jgit.api.errors.GitAPIException;
52 import org.eclipse.jgit.api.errors.InvalidRefNameException;
53 import org.eclipse.jgit.api.errors.JGitInternalException;
54 import org.eclipse.jgit.api.errors.NoHeadException;
55 import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
56 import org.eclipse.jgit.api.errors.RefNotFoundException;
57 import org.eclipse.jgit.internal.JGitText;
58 import org.eclipse.jgit.lib.ConfigConstants;
59 import org.eclipse.jgit.lib.Constants;
60 import org.eclipse.jgit.lib.ObjectId;
61 import org.eclipse.jgit.lib.Ref;
62 import org.eclipse.jgit.lib.RefRename;
63 import org.eclipse.jgit.lib.RefUpdate.Result;
64 import org.eclipse.jgit.lib.Repository;
65 import org.eclipse.jgit.lib.StoredConfig;
66
67
68
69
70
71
72
73
74 public class RenameBranchCommand extends GitCommand<Ref> {
75 private String oldName;
76
77 private String newName;
78
79
80
81
82
83
84
85
86
87 protected RenameBranchCommand(Repository repo) {
88 super(repo);
89 }
90
91
92 @Override
93 public Ref call() throws GitAPIException, RefNotFoundException, InvalidRefNameException,
94 RefAlreadyExistsException, DetachedHeadException {
95 checkCallable();
96
97 if (newName == null)
98 throw new InvalidRefNameException(MessageFormat.format(JGitText
99 .get().branchNameInvalid, "<null>"));
100
101 try {
102 String fullOldName;
103 String fullNewName;
104 if (repo.findRef(newName) != null)
105 throw new RefAlreadyExistsException(MessageFormat.format(
106 JGitText.get().refAlreadyExists1, newName));
107 if (oldName != null) {
108 Ref ref = repo.findRef(oldName);
109 if (ref == null)
110 throw new RefNotFoundException(MessageFormat.format(
111 JGitText.get().refNotResolved, oldName));
112 if (ref.getName().startsWith(Constants.R_TAGS))
113 throw new RefNotFoundException(MessageFormat.format(
114 JGitText.get().renameBranchFailedBecauseTag,
115 oldName));
116 fullOldName = ref.getName();
117 } else {
118 fullOldName = repo.getFullBranch();
119 if (fullOldName == null) {
120 throw new NoHeadException(
121 JGitText.get().invalidRepositoryStateNoHead);
122 }
123 if (ObjectId.isId(fullOldName))
124 throw new DetachedHeadException();
125 }
126
127 if (fullOldName.startsWith(Constants.R_REMOTES))
128 fullNewName = Constants.R_REMOTES + newName;
129 else {
130 fullNewName = Constants.R_HEADS + newName;
131 }
132
133 if (!Repository.isValidRefName(fullNewName))
134 throw new InvalidRefNameException(MessageFormat.format(JGitText
135 .get().branchNameInvalid, fullNewName));
136
137 RefRename rename = repo.renameRef(fullOldName, fullNewName);
138 Result renameResult = rename.rename();
139
140 setCallable(false);
141
142 if (Result.RENAMED != renameResult)
143 throw new JGitInternalException(MessageFormat.format(JGitText
144 .get().renameBranchUnexpectedResult, renameResult
145 .name()));
146
147 if (fullNewName.startsWith(Constants.R_HEADS)) {
148 String shortOldName = fullOldName.substring(Constants.R_HEADS
149 .length());
150 final StoredConfig repoConfig = repo.getConfig();
151
152 for (String name : repoConfig.getNames(
153 ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName)) {
154 String[] values = repoConfig.getStringList(
155 ConfigConstants.CONFIG_BRANCH_SECTION,
156 shortOldName, name);
157 if (values.length == 0)
158 continue;
159
160
161 String[] existing = repoConfig.getStringList(
162 ConfigConstants.CONFIG_BRANCH_SECTION, newName,
163 name);
164 if (existing.length > 0) {
165 String[] newValues = new String[values.length
166 + existing.length];
167 System.arraycopy(existing, 0, newValues, 0,
168 existing.length);
169 System.arraycopy(values, 0, newValues, existing.length,
170 values.length);
171 values = newValues;
172 }
173
174 repoConfig.setStringList(
175 ConfigConstants.CONFIG_BRANCH_SECTION, newName,
176 name, Arrays.asList(values));
177 }
178 repoConfig.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
179 shortOldName);
180 repoConfig.save();
181 }
182
183 Ref resultRef = repo.findRef(newName);
184 if (resultRef == null)
185 throw new JGitInternalException(
186 JGitText.get().renameBranchFailedUnknownReason);
187 return resultRef;
188 } catch (IOException ioe) {
189 throw new JGitInternalException(ioe.getMessage(), ioe);
190 }
191 }
192
193
194
195
196
197
198
199
200 public RenameBranchCommand setNewName(String newName) {
201 checkCallable();
202 this.newName = newName;
203 return this;
204 }
205
206
207
208
209
210
211
212
213
214 public RenameBranchCommand setOldName(String oldName) {
215 checkCallable();
216 this.oldName = oldName;
217 return this;
218 }
219 }