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