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 					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 	 * Execute an update from {@link RefTreeUpdate} or {@link RefTreeRename}.
133 	 *
134 	 * @param rw
135 	 *            current RevWalk handling the update or rename.
136 	 * @param todo
137 	 *            commands to execute. Must never be a bootstrap reference name.
138 	 * @throws IOException
139 	 *             the storage system is unable to read or write data.
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 			// apply set rejection information on commands.
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 }