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 static org.eclipse.jgit.lib.Constants.encode;
48
49 import java.io.IOException;
50
51 import org.eclipse.jgit.lib.Ref;
52 import org.eclipse.jgit.lib.RefUpdate;
53 import org.eclipse.jgit.lib.ReflogEntry;
54 import org.eclipse.jgit.lib.Repository;
55
56
57 class RefDirectoryUpdate extends RefUpdate {
58 private final RefDirectory database;
59
60 private boolean shouldDeref;
61 private LockFile lock;
62
63 RefDirectoryUpdate(RefDirectory r, Ref ref) {
64 super(ref);
65 database = r;
66 }
67
68
69 @Override
70 protected RefDirectory getRefDatabase() {
71 return database;
72 }
73
74
75 @Override
76 protected Repository getRepository() {
77 return database.getRepository();
78 }
79
80
81 @Override
82 protected boolean tryLock(boolean deref) throws IOException {
83 shouldDeref = deref;
84 Ref dst = getRef();
85 if (deref)
86 dst = dst.getLeaf();
87 String name = dst.getName();
88 lock = new LockFile(database.fileFor(name));
89 if (lock.lock()) {
90 dst = database.findRef(name);
91 setOldObjectId(dst != null ? dst.getObjectId() : null);
92 return true;
93 } else {
94 return false;
95 }
96 }
97
98
99 @Override
100 protected void unlock() {
101 if (lock != null) {
102 lock.unlock();
103 lock = null;
104 }
105 }
106
107
108 @Override
109 protected Result doUpdate(Result status) throws IOException {
110 WriteConfig wc = database.getRepository().getConfig()
111 .get(WriteConfig.KEY);
112
113 lock.setFSync(wc.getFSyncRefFiles());
114 lock.setNeedStatInformation(true);
115 lock.write(getNewObjectId());
116
117 String msg = getRefLogMessage();
118 if (msg != null) {
119 if (isRefLogIncludingResult()) {
120 String strResult = toResultString(status);
121 if (strResult != null) {
122 if (msg.length() > 0)
123 msg = msg + ": " + strResult;
124 else
125 msg = strResult;
126 }
127 }
128 database.log(isForceRefLog(), this, msg, shouldDeref);
129 }
130 if (!lock.commit())
131 return Result.LOCK_FAILURE;
132 database.stored(this, lock.getCommitSnapshot());
133 return status;
134 }
135
136 private String toResultString(Result status) {
137 switch (status) {
138 case FORCED:
139 return ReflogEntry.PREFIX_FORCED_UPDATE;
140 case FAST_FORWARD:
141 return ReflogEntry.PREFIX_FAST_FORWARD;
142 case NEW:
143 return ReflogEntry.PREFIX_CREATED;
144 default:
145 return null;
146 }
147 }
148
149
150 @Override
151 protected Result doDelete(Result status) throws IOException {
152 if (getRef().getStorage() != Ref.Storage.NEW)
153 database.delete(this);
154 return status;
155 }
156
157
158 @Override
159 protected Result doLink(String target) throws IOException {
160 WriteConfig wc = database.getRepository().getConfig()
161 .get(WriteConfig.KEY);
162
163 lock.setFSync(wc.getFSyncRefFiles());
164 lock.setNeedStatInformation(true);
165 lock.write(encode(RefDirectory.SYMREF + target + '\n'));
166
167 String msg = getRefLogMessage();
168 if (msg != null)
169 database.log(isForceRefLog(), this, msg, false);
170 if (!lock.commit())
171 return Result.LOCK_FAILURE;
172 database.storedSymbolicRef(this, lock.getCommitSnapshot(), target);
173
174 if (getRef().getStorage() == Ref.Storage.NEW)
175 return Result.NEW;
176 return Result.FORCED;
177 }
178 }