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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. @Override
  84. public void execute(RevWalk rw, ProgressMonitor monitor)
  85. throws IOException {
  86. List<Command> todo = new ArrayList<>(getCommands().size());
  87. for (ReceiveCommand c : getCommands()) {
  88. if (!isAllowNonFastForwards()) {
  89. if (c.getType() == UPDATE) {
  90. c.updateType(rw);
  91. }
  92. if (c.getType() == UPDATE_NONFASTFORWARD) {
  93. c.setResult(REJECTED_NONFASTFORWARD);
  94. ReceiveCommand.abort(getCommands());
  95. return;
  96. }
  97. }
  98. todo.add(new Command(rw, c));
  99. }
  100. init(rw);
  101. execute(rw, todo);
  102. }
  103. void init(RevWalk rw) throws IOException {
  104. src = refdb.getBootstrap().exactRef(refdb.getTxnCommitted());
  105. if (src != null && src.getObjectId() != null) {
  106. RevCommit c = rw.parseCommit(src.getObjectId());
  107. parentCommitId = c;
  108. parentTreeId = c.getTree();
  109. tree = RefTree.read(rw.getObjectReader(), c.getTree());
  110. } else {
  111. parentCommitId = ObjectId.zeroId();
  112. parentTreeId = new ObjectInserter.Formatter()
  113. .idFor(OBJ_TREE, new byte[] {});
  114. tree = RefTree.newEmptyTree();
  115. }
  116. }
  117. @Nullable
  118. Ref exactRef(ObjectReader reader, String name) throws IOException {
  119. return tree.exactRef(reader, name);
  120. }
  121. /**
  122. * Execute an update from {@link RefTreeUpdate} or {@link RefTreeRename}.
  123. *
  124. * @param rw
  125. * current RevWalk handling the update or rename.
  126. * @param todo
  127. * commands to execute. Must never be a bootstrap reference name.
  128. * @throws IOException
  129. * the storage system is unable to read or write data.
  130. */
  131. void execute(RevWalk rw, List<Command> todo) throws IOException {
  132. for (Command c : todo) {
  133. if (c.getResult() != NOT_ATTEMPTED) {
  134. Command.abort(todo, null);
  135. return;
  136. }
  137. if (refdb.conflictsWithBootstrap(c.getRefName())) {
  138. c.setResult(REJECTED_OTHER_REASON, MessageFormat
  139. .format(JGitText.get().invalidRefName, c.getRefName()));
  140. Command.abort(todo, null);
  141. return;
  142. }
  143. }
  144. if (apply(todo) && newCommitId != null) {
  145. commit(rw, todo);
  146. }
  147. }
  148. private boolean apply(List<Command> todo) throws IOException {
  149. if (!tree.apply(todo)) {
  150. // apply set rejection information on commands.
  151. return false;
  152. }
  153. Repository repo = refdb.getRepository();
  154. try (ObjectInserter ins = repo.newObjectInserter()) {
  155. CommitBuilder b = new CommitBuilder();
  156. b.setTreeId(tree.writeTree(ins));
  157. if (parentTreeId.equals(b.getTreeId())) {
  158. for (Command c : todo) {
  159. c.setResult(OK);
  160. }
  161. return true;
  162. }
  163. if (!parentCommitId.equals(ObjectId.zeroId())) {
  164. b.setParentId(parentCommitId);
  165. }
  166. author = getRefLogIdent();
  167. if (author == null) {
  168. author = new PersonIdent(repo);
  169. }
  170. b.setAuthor(author);
  171. b.setCommitter(author);
  172. b.setMessage(getRefLogMessage());
  173. newCommitId = ins.insert(b);
  174. ins.flush();
  175. }
  176. return true;
  177. }
  178. private void commit(RevWalk rw, List<Command> todo) throws IOException {
  179. ReceiveCommand commit = new ReceiveCommand(
  180. parentCommitId, newCommitId,
  181. refdb.getTxnCommitted());
  182. updateBootstrap(rw, commit);
  183. if (commit.getResult() == OK) {
  184. for (Command c : todo) {
  185. c.setResult(OK);
  186. }
  187. } else {
  188. Command.abort(todo, commit.getResult().name());
  189. }
  190. }
  191. private void updateBootstrap(RevWalk rw, ReceiveCommand commit)
  192. throws IOException {
  193. BatchRefUpdate u = refdb.getBootstrap().newBatchUpdate();
  194. u.setAllowNonFastForwards(true);
  195. u.setPushCertificate(getPushCertificate());
  196. if (isRefLogDisabled()) {
  197. u.disableRefLog();
  198. } else {
  199. u.setRefLogIdent(author);
  200. u.setRefLogMessage(getRefLogMessage(), false);
  201. }
  202. u.addCommand(commit);
  203. u.execute(rw, NullProgressMonitor.INSTANCE);
  204. }
  205. }