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
91 @Override
92 public void execute(RevWalk rw, ProgressMonitor monitor)
93 throws IOException {
94 List<Command> todo = new ArrayList<>(getCommands().size());
95 for (ReceiveCommand c : getCommands()) {
96 if (!isAllowNonFastForwards()) {
97 if (c.getType() == UPDATE) {
98 c.updateType(rw);
99 }
100 if (c.getType() == UPDATE_NONFASTFORWARD) {
101 c.setResult(REJECTED_NONFASTFORWARD);
102 if (isAtomic()) {
103 ReceiveCommand.abort(getCommands());
104 return;
105 } else {
106 continue;
107 }
108 }
109 }
110 todo.add(new Command(rw, c));
111 }
112 init(rw);
113 execute(rw, todo);
114 }
115
116 void init(RevWalk rw) throws IOException {
117 src = refdb.getBootstrap().exactRef(refdb.getTxnCommitted());
118 if (src != null && src.getObjectId() != null) {
119 RevCommit c = rw.parseCommit(src.getObjectId());
120 parentCommitId = c;
121 parentTreeId = c.getTree();
122 tree = RefTree.read(rw.getObjectReader(), c.getTree());
123 } else {
124 parentCommitId = ObjectId.zeroId();
125 parentTreeId = new ObjectInserter.Formatter()
126 .idFor(OBJ_TREE, new byte[] {});
127 tree = RefTree.newEmptyTree();
128 }
129 }
130
131 @Nullable
132 Ref exactRef(ObjectReader reader, String name) throws IOException {
133 return tree.exactRef(reader, name);
134 }
135
136
137
138
139
140
141
142
143
144
145
146 void execute(RevWalk rw, List<Command> todo) throws IOException {
147 for (Command c : todo) {
148 if (c.getResult() != NOT_ATTEMPTED) {
149 Command.abort(todo, null);
150 return;
151 }
152 if (refdb.conflictsWithBootstrap(c.getRefName())) {
153 c.setResult(REJECTED_OTHER_REASON, MessageFormat
154 .format(JGitText.get().invalidRefName, c.getRefName()));
155 Command.abort(todo, null);
156 return;
157 }
158 }
159
160 if (apply(todo) && newCommitId != null) {
161 commit(rw, todo);
162 }
163 }
164
165 private boolean apply(List<Command> todo) throws IOException {
166 if (!tree.apply(todo)) {
167
168 return false;
169 }
170
171 Repository repo = refdb.getRepository();
172 try (ObjectInserter ins = repo.newObjectInserter()) {
173 CommitBuilder b = new CommitBuilder();
174 b.setTreeId(tree.writeTree(ins));
175 if (parentTreeId.equals(b.getTreeId())) {
176 for (Command c : todo) {
177 c.setResult(OK);
178 }
179 return true;
180 }
181 if (!parentCommitId.equals(ObjectId.zeroId())) {
182 b.setParentId(parentCommitId);
183 }
184
185 author = getRefLogIdent();
186 if (author == null) {
187 author = new PersonIdent(repo);
188 }
189 b.setAuthor(author);
190 b.setCommitter(author);
191 b.setMessage(getRefLogMessage());
192 newCommitId = ins.insert(b);
193 ins.flush();
194 }
195 return true;
196 }
197
198 private void commit(RevWalk rw, List<Command> todo) throws IOException {
199 ReceiveCommand commit = new ReceiveCommand(
200 parentCommitId, newCommitId,
201 refdb.getTxnCommitted());
202 updateBootstrap(rw, commit);
203
204 if (commit.getResult() == OK) {
205 for (Command c : todo) {
206 c.setResult(OK);
207 }
208 } else {
209 Command.abort(todo, commit.getResult().name());
210 }
211 }
212
213 private void updateBootstrap(RevWalk rw, ReceiveCommand commit)
214 throws IOException {
215 BatchRefUpdate u = refdb.getBootstrap().newBatchUpdate();
216 u.setAllowNonFastForwards(true);
217 u.setPushCertificate(getPushCertificate());
218 if (isRefLogDisabled()) {
219 u.disableRefLog();
220 } else {
221 u.setRefLogIdent(author);
222 u.setRefLogMessage(getRefLogMessage(), false);
223 }
224 u.addCommand(commit);
225 u.execute(rw, NullProgressMonitor.INSTANCE);
226 }
227 }