You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RefTreeBatch.java 7.2KB

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