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