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 protected RenameBranchCommand(Repository repo) {
83 super(repo);
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public Ref call() throws GitAPIException, RefNotFoundException, InvalidRefNameException,
100 RefAlreadyExistsException, DetachedHeadException {
101 checkCallable();
102
103 if (newName == null)
104 throw new InvalidRefNameException(MessageFormat.format(JGitText
105 .get().branchNameInvalid, "<null>"));
106
107 try {
108 String fullOldName;
109 String fullNewName;
110 if (repo.findRef(newName) != null)
111 throw new RefAlreadyExistsException(MessageFormat.format(
112 JGitText.get().refAlreadyExists1, newName));
113 if (oldName != null) {
114 Ref ref = repo.findRef(oldName);
115 if (ref == null)
116 throw new RefNotFoundException(MessageFormat.format(
117 JGitText.get().refNotResolved, oldName));
118 if (ref.getName().startsWith(Constants.R_TAGS))
119 throw new RefNotFoundException(MessageFormat.format(
120 JGitText.get().renameBranchFailedBecauseTag,
121 oldName));
122 fullOldName = ref.getName();
123 } else {
124 fullOldName = repo.getFullBranch();
125 if (fullOldName == null) {
126 throw new NoHeadException(
127 JGitText.get().invalidRepositoryStateNoHead);
128 }
129 if (ObjectId.isId(fullOldName))
130 throw new DetachedHeadException();
131 }
132
133 if (fullOldName.startsWith(Constants.R_REMOTES))
134 fullNewName = Constants.R_REMOTES + newName;
135 else {
136 fullNewName = Constants.R_HEADS + newName;
137 }
138
139 if (!Repository.isValidRefName(fullNewName))
140 throw new InvalidRefNameException(MessageFormat.format(JGitText
141 .get().branchNameInvalid, fullNewName));
142
143 RefRename rename = repo.renameRef(fullOldName, fullNewName);
144 Result renameResult = rename.rename();
145
146 setCallable(false);
147
148 if (Result.RENAMED != renameResult)
149 throw new JGitInternalException(MessageFormat.format(JGitText
150 .get().renameBranchUnexpectedResult, renameResult
151 .name()));
152
153 if (fullNewName.startsWith(Constants.R_HEADS)) {
154 String shortOldName = fullOldName.substring(Constants.R_HEADS
155 .length());
156 final StoredConfig repoConfig = repo.getConfig();
157
158 for (String name : repoConfig.getNames(
159 ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName)) {
160 String[] values = repoConfig.getStringList(
161 ConfigConstants.CONFIG_BRANCH_SECTION,
162 shortOldName, name);
163 if (values.length == 0)
164 continue;
165
166
167 String[] existing = repoConfig.getStringList(
168 ConfigConstants.CONFIG_BRANCH_SECTION, newName,
169 name);
170 if (existing.length > 0) {
171 String[] newValues = new String[values.length
172 + existing.length];
173 System.arraycopy(existing, 0, newValues, 0,
174 existing.length);
175 System.arraycopy(values, 0, newValues, existing.length,
176 values.length);
177 values = newValues;
178 }
179
180 repoConfig.setStringList(
181 ConfigConstants.CONFIG_BRANCH_SECTION, newName,
182 name, Arrays.asList(values));
183 }
184 repoConfig.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
185 shortOldName);
186 repoConfig.save();
187 }
188
189 Ref resultRef = repo.findRef(newName);
190 if (resultRef == null)
191 throw new JGitInternalException(
192 JGitText.get().renameBranchFailedUnknownReason);
193 return resultRef;
194 } catch (IOException ioe) {
195 throw new JGitInternalException(ioe.getMessage(), ioe);
196 }
197 }
198
199
200
201
202
203
204 public RenameBranchCommand setNewName(String newName) {
205 checkCallable();
206 this.newName = newName;
207 return this;
208 }
209
210
211
212
213
214
215
216 public RenameBranchCommand setOldName(String oldName) {
217 checkCallable();
218 this.oldName = oldName;
219 return this;
220 }
221 }