1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.internal.storage.file;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.nio.file.AtomicMoveNotSupportedException;
17 import java.nio.file.StandardCopyOption;
18
19 import org.eclipse.jgit.lib.Constants;
20 import org.eclipse.jgit.lib.ObjectId;
21 import org.eclipse.jgit.lib.RefRename;
22 import org.eclipse.jgit.lib.RefUpdate;
23 import org.eclipse.jgit.lib.RefUpdate.Result;
24 import org.eclipse.jgit.revwalk.RevWalk;
25 import org.eclipse.jgit.util.FileUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30
31
32
33
34
35
36
37
38
39 class RefDirectoryRename extends RefRename {
40 private static final Logger LOG = LoggerFactory
41 .getLogger(RefDirectoryRename.class);
42
43 private final RefDirectory refdb;
44
45
46
47
48
49
50
51
52 private ObjectId objId;
53
54
55 private boolean updateHEAD;
56
57
58 private RefDirectoryUpdate tmp;
59
60 RefDirectoryRename(RefDirectoryUpdate./../../../org/eclipse/jgit/internal/storage/file/RefDirectoryUpdate.html#RefDirectoryUpdate">RefDirectoryUpdate src, RefDirectoryUpdate dst) {
61 super(src, dst);
62 refdb = src.getRefDatabase();
63 }
64
65
66 @Override
67 protected Result doRename() throws IOException {
68 if (source.getRef().isSymbolic())
69 return Result.IO_FAILURE;
70
71 objId = source.getOldObjectId();
72 updateHEAD = needToUpdateHEAD();
73 tmp = refdb.newTemporaryUpdate();
74 try (RevWalkvwalk/RevWalk.html#RevWalk">RevWalk rw = new RevWalk(refdb.getRepository())) {
75
76 tmp.setNewObjectId(objId);
77 tmp.setForceUpdate(true);
78 tmp.disableRefLog();
79 switch (tmp.update(rw)) {
80 case NEW:
81 case FORCED:
82 case NO_CHANGE:
83 break;
84 default:
85 return tmp.getResult();
86 }
87
88
89
90 if (!renameLog(source, tmp))
91 return Result.IO_FAILURE;
92
93
94
95
96 RefUpdate dst = destination;
97 if (updateHEAD) {
98 if (!linkHEAD(destination)) {
99 renameLog(tmp, source);
100 return Result.LOCK_FAILURE;
101 }
102
103
104 dst = refdb.newUpdate(Constants.HEAD, false);
105 dst.setRefLogIdent(destination.getRefLogIdent());
106 dst.setRefLogMessage(destination.getRefLogMessage(), false);
107 }
108
109
110 source.setExpectedOldObjectId(objId);
111 source.setForceUpdate(true);
112 source.disableRefLog();
113 if (source.delete(rw) != Result.FORCED) {
114 renameLog(tmp, source);
115 if (updateHEAD)
116 linkHEAD(source);
117 return source.getResult();
118 }
119
120
121 if (!renameLog(tmp, destination)) {
122 renameLog(tmp, source);
123 source.setExpectedOldObjectId(ObjectId.zeroId());
124 source.setNewObjectId(objId);
125 source.update(rw);
126 if (updateHEAD)
127 linkHEAD(source);
128 return Result.IO_FAILURE;
129 }
130
131
132 dst.setExpectedOldObjectId(ObjectId.zeroId());
133 dst.setNewObjectId(objId);
134 if (dst.update(rw) != Result.NEW) {
135
136
137 if (renameLog(destination, tmp))
138 renameLog(tmp, source);
139 source.setExpectedOldObjectId(ObjectId.zeroId());
140 source.setNewObjectId(objId);
141 source.update(rw);
142 if (updateHEAD)
143 linkHEAD(source);
144 return dst.getResult();
145 }
146
147 return Result.RENAMED;
148 } finally {
149
150 try {
151 refdb.delete(tmp);
152 } catch (IOException err) {
153 FileUtils.delete(refdb.fileFor(tmp.getName()));
154 }
155 }
156 }
157
158 private boolean renameLog(RefUpdate="../../../../../../org/eclipse/jgit/lib/RefUpdate.html#RefUpdate">RefUpdate src, RefUpdate dst) {
159 File srcLog = refdb.logFor(src.getName());
160 File dstLog = refdb.logFor(dst.getName());
161
162 if (!srcLog.exists())
163 return true;
164
165 if (!rename(srcLog, dstLog))
166 return false;
167
168 try {
169 final int levels = RefDirectory.levelsIn(src.getName()) - 2;
170 RefDirectory.delete(srcLog, levels);
171 return true;
172 } catch (IOException e) {
173 rename(dstLog, srcLog);
174 return false;
175 }
176 }
177
178 private static boolean rename(File src, File dst) {
179 try {
180 FileUtils.rename(src, dst, StandardCopyOption.ATOMIC_MOVE);
181 return true;
182 } catch (AtomicMoveNotSupportedException e) {
183 LOG.error(e.getMessage(), e);
184 } catch (IOException e) {
185
186 }
187
188 File dir = dst.getParentFile();
189 if ((dir.exists() || !dir.mkdirs()) && !dir.isDirectory())
190 return false;
191 try {
192 FileUtils.rename(src, dst, StandardCopyOption.ATOMIC_MOVE);
193 return true;
194 } catch (IOException e) {
195 LOG.error(e.getMessage(), e);
196 return false;
197 }
198 }
199
200 private boolean linkHEAD(RefUpdate target) {
201 try {
202 RefUpdate u = refdb.newUpdate(Constants.HEAD, false);
203 u.disableRefLog();
204 switch (u.link(target.getName())) {
205 case NEW:
206 case FORCED:
207 case NO_CHANGE:
208 return true;
209 default:
210 return false;
211 }
212 } catch (IOException e) {
213 return false;
214 }
215 }
216 }