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.internal.storage.reftree;
45
46 import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
47 import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED;
48 import static org.eclipse.jgit.transport.ReceiveCommand.Result.OK;
49 import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_NONFASTFORWARD;
50 import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_REASON;
51 import static org.eclipse.jgit.transport.ReceiveCommand.Type.UPDATE;
52 import static org.eclipse.jgit.transport.ReceiveCommand.Type.UPDATE_NONFASTFORWARD;
53
54 import java.io.IOException;
55 import java.text.MessageFormat;
56 import java.util.ArrayList;
57 import java.util.List;
58
59 import org.eclipse.jgit.annotations.Nullable;
60 import org.eclipse.jgit.internal.JGitText;
61 import org.eclipse.jgit.lib.BatchRefUpdate;
62 import org.eclipse.jgit.lib.CommitBuilder;
63 import org.eclipse.jgit.lib.NullProgressMonitor;
64 import org.eclipse.jgit.lib.ObjectId;
65 import org.eclipse.jgit.lib.ObjectInserter;
66 import org.eclipse.jgit.lib.ObjectReader;
67 import org.eclipse.jgit.lib.PersonIdent;
68 import org.eclipse.jgit.lib.ProgressMonitor;
69 import org.eclipse.jgit.lib.Ref;
70 import org.eclipse.jgit.lib.Repository;
71 import org.eclipse.jgit.revwalk.RevCommit;
72 import org.eclipse.jgit.revwalk.RevWalk;
73 import org.eclipse.jgit.transport.ReceiveCommand;
74
75
76 class RefTreeBatch extends BatchRefUpdate {
77 private final RefTreeDatabase refdb;
78 private Ref src;
79 private ObjectId parentCommitId;
80 private ObjectId parentTreeId;
81 private RefTree tree;
82 private PersonIdent author;
83 private ObjectId newCommitId;
84
85 RefTreeBatch(RefTreeDatabase refdb) {
86 super(refdb);
87 this.refdb = refdb;
88 }
89
90 @Override
91 public void execute(RevWalk rw, ProgressMonitor monitor)
92 throws IOException {
93 List<Command> todo = new ArrayList<>(getCommands().size());
94 for (ReceiveCommand c : getCommands()) {
95 if (!isAllowNonFastForwards()) {
96 if (c.getType() == UPDATE) {
97 c.updateType(rw);
98 }
99 if (c.getType() == UPDATE_NONFASTFORWARD) {
100 c.setResult(REJECTED_NONFASTFORWARD);
101 if (isAtomic()) {
102 ReceiveCommand.abort(getCommands());
103 return;
104 } else {
105 continue;
106 }
107 }
108 }
109 todo.add(new Command(rw, c));
110 }
111 init(rw);
112 execute(rw, todo);
113 }
114
115 void init(RevWalk rw) throws IOException {
116 src = refdb.getBootstrap().exactRef(refdb.getTxnCommitted());
117 if (src != null && src.getObjectId() != null) {
118 RevCommit c = rw.parseCommit(src.getObjectId());
119 parentCommitId = c;
120 parentTreeId = c.getTree();
121 tree = RefTree.read(rw.getObjectReader(), c.getTree());
122 } else {
123 parentCommitId = ObjectId.zeroId();
124 parentTreeId = new ObjectInserter.Formatter()
125 .idFor(OBJ_TREE, new byte[] {});
126 tree = RefTree.newEmptyTree();
127 }
128 }
129
130 @Nullable
131 Ref exactRef(ObjectReader reader, String name) throws IOException {
132 return tree.exactRef(reader, name);
133 }
134
135
136
137
138
139
140
141
142
143
144
145 void execute(RevWalk rw, List<Command> todo) throws IOException {
146 for (Command c : todo) {
147 if (c.getResult() != NOT_ATTEMPTED) {
148 Command.abort(todo, null);
149 return;
150 }
151 if (refdb.conflictsWithBootstrap(c.getRefName())) {
152 c.setResult(REJECTED_OTHER_REASON, MessageFormat
153 .format(JGitText.get().invalidRefName, c.getRefName()));
154 Command.abort(todo, null);
155 return;
156 }
157 }
158
159 if (apply(todo) && newCommitId != null) {
160 commit(rw, todo);
161 }
162 }
163
164 private boolean apply(List<Command> todo) throws IOException {
165 if (!tree.apply(todo)) {
166
167 return false;
168 }
169
170 Repository repo = refdb.getRepository();
171 try (ObjectInserter ins = repo.newObjectInserter()) {
172 CommitBuilder b = new CommitBuilder();
173 b.setTreeId(tree.writeTree(ins));
174 if (parentTreeId.equals(b.getTreeId())) {
175 for (Command c : todo) {
176 c.setResult(OK);
177 }
178 return true;
179 }
180 if (!parentCommitId.equals(ObjectId.zeroId())) {
181 b.setParentId(parentCommitId);
182 }
183
184 author = getRefLogIdent();
185 if (author == null) {
186 author = new PersonIdent(repo);
187 }
188 b.setAuthor(author);
189 b.setCommitter(author);
190 b.setMessage(getRefLogMessage());
191 newCommitId = ins.insert(b);
192 ins.flush();
193 }
194 return true;
195 }
196
197 private void commit(RevWalk rw, List<Command> todo) throws IOException {
198 ReceiveCommand commit = new ReceiveCommand(
199 parentCommitId, newCommitId,
200 refdb.getTxnCommitted());
201 updateBootstrap(rw, commit);
202
203 if (commit.getResult() == OK) {
204 for (Command c : todo) {
205 c.setResult(OK);
206 }
207 } else {
208 Command.abort(todo, commit.getResult().name());
209 }
210 }
211
212 private void updateBootstrap(RevWalk rw, ReceiveCommand commit)
213 throws IOException {
214 BatchRefUpdate u = refdb.getBootstrap().newBatchUpdate();
215 u.setAllowNonFastForwards(true);
216 u.setPushCertificate(getPushCertificate());
217 if (isRefLogDisabled()) {
218 u.disableRefLog();
219 } else {
220 u.setRefLogIdent(author);
221 u.setRefLogMessage(getRefLogMessage(), false);
222 }
223 u.addCommand(commit);
224 u.execute(rw, NullProgressMonitor.INSTANCE);
225 }
226 }