View Javadoc
1   /*
2    * Copyright (C) 2016, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  /** Batch update a {@link RefTreeDatabase}. */
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 	 * Execute an update from {@link RefTreeUpdate} or {@link RefTreeRename}.
137 	 *
138 	 * @param rw
139 	 *            current RevWalk handling the update or rename.
140 	 * @param todo
141 	 *            commands to execute. Must never be a bootstrap reference name.
142 	 * @throws IOException
143 	 *             the storage system is unable to read or write data.
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 			// apply set rejection information on commands.
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 }