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
45 package org.eclipse.jgit.internal.storage.file;
46
47 import java.io.File;
48 import java.io.IOException;
49
50 import org.eclipse.jgit.lib.Constants;
51 import org.eclipse.jgit.lib.ObjectId;
52 import org.eclipse.jgit.lib.RefRename;
53 import org.eclipse.jgit.lib.RefUpdate;
54 import org.eclipse.jgit.lib.RefUpdate.Result;
55 import org.eclipse.jgit.revwalk.RevWalk;
56 import org.eclipse.jgit.util.FileUtils;
57
58
59
60
61
62
63
64
65
66
67
68 class RefDirectoryRename extends RefRename {
69 private final RefDirectory refdb;
70
71
72
73
74
75
76
77
78 private ObjectId objId;
79
80
81 private boolean updateHEAD;
82
83
84 private RefDirectoryUpdate tmp;
85
86 RefDirectoryRename(RefDirectoryUpdate src, RefDirectoryUpdate dst) {
87 super(src, dst);
88 refdb = src.getRefDatabase();
89 }
90
91 @Override
92 protected Result doRename() throws IOException {
93 if (source.getRef().isSymbolic())
94 return Result.IO_FAILURE;
95
96 objId = source.getOldObjectId();
97 updateHEAD = needToUpdateHEAD();
98 tmp = refdb.newTemporaryUpdate();
99 try (final RevWalk rw = new RevWalk(refdb.getRepository())) {
100
101 tmp.setNewObjectId(objId);
102 tmp.setForceUpdate(true);
103 tmp.disableRefLog();
104 switch (tmp.update(rw)) {
105 case NEW:
106 case FORCED:
107 case NO_CHANGE:
108 break;
109 default:
110 return tmp.getResult();
111 }
112
113
114
115 if (!renameLog(source, tmp))
116 return Result.IO_FAILURE;
117
118
119
120
121 RefUpdate dst = destination;
122 if (updateHEAD) {
123 if (!linkHEAD(destination)) {
124 renameLog(tmp, source);
125 return Result.LOCK_FAILURE;
126 }
127
128
129 dst = refdb.newUpdate(Constants.HEAD, false);
130 dst.setRefLogIdent(destination.getRefLogIdent());
131 dst.setRefLogMessage(destination.getRefLogMessage(), false);
132 }
133
134
135 source.setExpectedOldObjectId(objId);
136 source.setForceUpdate(true);
137 source.disableRefLog();
138 if (source.delete(rw) != Result.FORCED) {
139 renameLog(tmp, source);
140 if (updateHEAD)
141 linkHEAD(source);
142 return source.getResult();
143 }
144
145
146 if (!renameLog(tmp, destination)) {
147 renameLog(tmp, source);
148 source.setExpectedOldObjectId(ObjectId.zeroId());
149 source.setNewObjectId(objId);
150 source.update(rw);
151 if (updateHEAD)
152 linkHEAD(source);
153 return Result.IO_FAILURE;
154 }
155
156
157 dst.setExpectedOldObjectId(ObjectId.zeroId());
158 dst.setNewObjectId(objId);
159 if (dst.update(rw) != Result.NEW) {
160
161
162 if (renameLog(destination, tmp))
163 renameLog(tmp, source);
164 source.setExpectedOldObjectId(ObjectId.zeroId());
165 source.setNewObjectId(objId);
166 source.update(rw);
167 if (updateHEAD)
168 linkHEAD(source);
169 return dst.getResult();
170 }
171
172 return Result.RENAMED;
173 } finally {
174
175 try {
176 refdb.delete(tmp);
177 } catch (IOException err) {
178 FileUtils.delete(refdb.fileFor(tmp.getName()));
179 }
180 }
181 }
182
183 private boolean renameLog(RefUpdate src, RefUpdate dst) {
184 File srcLog = refdb.getLogWriter().logFor(src.getName());
185 File dstLog = refdb.getLogWriter().logFor(dst.getName());
186
187 if (!srcLog.exists())
188 return true;
189
190 if (!rename(srcLog, dstLog))
191 return false;
192
193 try {
194 final int levels = RefDirectory.levelsIn(src.getName()) - 2;
195 RefDirectory.delete(srcLog, levels);
196 return true;
197 } catch (IOException e) {
198 rename(dstLog, srcLog);
199 return false;
200 }
201 }
202
203 private static boolean rename(File src, File dst) {
204 if (src.renameTo(dst))
205 return true;
206
207 File dir = dst.getParentFile();
208 if ((dir.exists() || !dir.mkdirs()) && !dir.isDirectory())
209 return false;
210 return src.renameTo(dst);
211 }
212
213 private boolean linkHEAD(RefUpdate target) {
214 try {
215 RefUpdate u = refdb.newUpdate(Constants.HEAD, false);
216 u.disableRefLog();
217 switch (u.link(target.getName())) {
218 case NEW:
219 case FORCED:
220 case NO_CHANGE:
221 return true;
222 default:
223 return false;
224 }
225 } catch (IOException e) {
226 return false;
227 }
228 }
229 }