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.

ReceiveCommand.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (C) 2008, 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.transport;
  44. import java.io.IOException;
  45. import java.text.MessageFormat;
  46. import java.util.ArrayList;
  47. import java.util.List;
  48. import org.eclipse.jgit.internal.JGitText;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. import org.eclipse.jgit.lib.Ref;
  51. import org.eclipse.jgit.lib.RefUpdate;
  52. /**
  53. * A command being processed by {@link BaseReceivePack}.
  54. * <p>
  55. * This command instance roughly translates to the server side representation of
  56. * the {@link RemoteRefUpdate} created by the client.
  57. */
  58. public class ReceiveCommand {
  59. /** Type of operation requested. */
  60. public static enum Type {
  61. /** Create a new ref; the ref must not already exist. */
  62. CREATE,
  63. /**
  64. * Update an existing ref with a fast-forward update.
  65. * <p>
  66. * During a fast-forward update no changes will be lost; only new
  67. * commits are inserted into the ref.
  68. */
  69. UPDATE,
  70. /**
  71. * Update an existing ref by potentially discarding objects.
  72. * <p>
  73. * The current value of the ref is not fully reachable from the new
  74. * value of the ref, so a successful command may result in one or more
  75. * objects becoming unreachable.
  76. */
  77. UPDATE_NONFASTFORWARD,
  78. /** Delete an existing ref; the ref should already exist. */
  79. DELETE;
  80. }
  81. /** Result of the update command. */
  82. public static enum Result {
  83. /** The command has not yet been attempted by the server. */
  84. NOT_ATTEMPTED,
  85. /** The server is configured to deny creation of this ref. */
  86. REJECTED_NOCREATE,
  87. /** The server is configured to deny deletion of this ref. */
  88. REJECTED_NODELETE,
  89. /** The update is a non-fast-forward update and isn't permitted. */
  90. REJECTED_NONFASTFORWARD,
  91. /** The update affects <code>HEAD</code> and cannot be permitted. */
  92. REJECTED_CURRENT_BRANCH,
  93. /**
  94. * One or more objects aren't in the repository.
  95. * <p>
  96. * This is severe indication of either repository corruption on the
  97. * server side, or a bug in the client wherein the client did not supply
  98. * all required objects during the pack transfer.
  99. */
  100. REJECTED_MISSING_OBJECT,
  101. /** Other failure; see {@link ReceiveCommand#getMessage()}. */
  102. REJECTED_OTHER_REASON,
  103. /** The ref could not be locked and updated atomically; try again. */
  104. LOCK_FAILURE,
  105. /** The change was completed successfully. */
  106. OK;
  107. }
  108. /**
  109. * Filter a list of commands according to result.
  110. *
  111. * @param commands
  112. * commands to filter.
  113. * @param want
  114. * desired status to filter by.
  115. * @return a copy of the command list containing only those commands with
  116. * the desired status.
  117. * @since 2.0
  118. */
  119. public static List<ReceiveCommand> filter(List<ReceiveCommand> commands,
  120. final Result want) {
  121. List<ReceiveCommand> r = new ArrayList<ReceiveCommand>(commands.size());
  122. for (final ReceiveCommand cmd : commands) {
  123. if (cmd.getResult() == want)
  124. r.add(cmd);
  125. }
  126. return r;
  127. }
  128. private final ObjectId oldId;
  129. private final ObjectId newId;
  130. private final String name;
  131. private Type type;
  132. private Ref ref;
  133. private Result status;
  134. private String message;
  135. /**
  136. * Create a new command for {@link BaseReceivePack}.
  137. *
  138. * @param oldId
  139. * the old object id; must not be null. Use
  140. * {@link ObjectId#zeroId()} to indicate a ref creation.
  141. * @param newId
  142. * the new object id; must not be null. Use
  143. * {@link ObjectId#zeroId()} to indicate a ref deletion.
  144. * @param name
  145. * name of the ref being affected.
  146. */
  147. public ReceiveCommand(final ObjectId oldId, final ObjectId newId,
  148. final String name) {
  149. this.oldId = oldId;
  150. this.newId = newId;
  151. this.name = name;
  152. type = Type.UPDATE;
  153. if (ObjectId.zeroId().equals(oldId))
  154. type = Type.CREATE;
  155. if (ObjectId.zeroId().equals(newId))
  156. type = Type.DELETE;
  157. status = Result.NOT_ATTEMPTED;
  158. }
  159. /**
  160. * Create a new command for {@link BaseReceivePack}.
  161. *
  162. * @param oldId
  163. * the old object id; must not be null. Use
  164. * {@link ObjectId#zeroId()} to indicate a ref creation.
  165. * @param newId
  166. * the new object id; must not be null. Use
  167. * {@link ObjectId#zeroId()} to indicate a ref deletion.
  168. * @param name
  169. * name of the ref being affected.
  170. * @param type
  171. * type of the command.
  172. * @since 2.0
  173. */
  174. public ReceiveCommand(final ObjectId oldId, final ObjectId newId,
  175. final String name, final Type type) {
  176. this.oldId = oldId;
  177. this.newId = newId;
  178. this.name = name;
  179. this.type = type;
  180. }
  181. /** @return the old value the client thinks the ref has. */
  182. public ObjectId getOldId() {
  183. return oldId;
  184. }
  185. /** @return the requested new value for this ref. */
  186. public ObjectId getNewId() {
  187. return newId;
  188. }
  189. /** @return the name of the ref being updated. */
  190. public String getRefName() {
  191. return name;
  192. }
  193. /** @return the type of this command; see {@link Type}. */
  194. public Type getType() {
  195. return type;
  196. }
  197. /** @return the ref, if this was advertised by the connection. */
  198. public Ref getRef() {
  199. return ref;
  200. }
  201. /** @return the current status code of this command. */
  202. public Result getResult() {
  203. return status;
  204. }
  205. /** @return the message associated with a failure status. */
  206. public String getMessage() {
  207. return message;
  208. }
  209. /**
  210. * Set the status of this command.
  211. *
  212. * @param s
  213. * the new status code for this command.
  214. */
  215. public void setResult(final Result s) {
  216. setResult(s, null);
  217. }
  218. /**
  219. * Set the status of this command.
  220. *
  221. * @param s
  222. * new status code for this command.
  223. * @param m
  224. * optional message explaining the new status.
  225. */
  226. public void setResult(final Result s, final String m) {
  227. status = s;
  228. message = m;
  229. }
  230. /**
  231. * Execute this command during a receive-pack session.
  232. * <p>
  233. * Sets the status of the command as a side effect.
  234. *
  235. * @param rp
  236. * receive-pack session.
  237. * @since 2.0
  238. */
  239. public void execute(final BaseReceivePack rp) {
  240. try {
  241. final RefUpdate ru = rp.getRepository().updateRef(getRefName());
  242. ru.setRefLogIdent(rp.getRefLogIdent());
  243. switch (getType()) {
  244. case DELETE:
  245. if (!ObjectId.zeroId().equals(getOldId())) {
  246. // We can only do a CAS style delete if the client
  247. // didn't bork its delete request by sending the
  248. // wrong zero id rather than the advertised one.
  249. //
  250. ru.setExpectedOldObjectId(getOldId());
  251. }
  252. ru.setForceUpdate(true);
  253. setResult(ru.delete(rp.getRevWalk()));
  254. break;
  255. case CREATE:
  256. case UPDATE:
  257. case UPDATE_NONFASTFORWARD:
  258. ru.setForceUpdate(rp.isAllowNonFastForwards());
  259. ru.setExpectedOldObjectId(getOldId());
  260. ru.setNewObjectId(getNewId());
  261. ru.setRefLogMessage("push", true);
  262. setResult(ru.update(rp.getRevWalk()));
  263. break;
  264. }
  265. } catch (IOException err) {
  266. setResult(Result.REJECTED_OTHER_REASON, MessageFormat.format(
  267. JGitText.get().lockError, err.getMessage()));
  268. }
  269. }
  270. void setRef(final Ref r) {
  271. ref = r;
  272. }
  273. void setType(final Type t) {
  274. type = t;
  275. }
  276. private void setResult(final RefUpdate.Result r) {
  277. switch (r) {
  278. case NOT_ATTEMPTED:
  279. setResult(Result.NOT_ATTEMPTED);
  280. break;
  281. case LOCK_FAILURE:
  282. case IO_FAILURE:
  283. setResult(Result.LOCK_FAILURE);
  284. break;
  285. case NO_CHANGE:
  286. case NEW:
  287. case FORCED:
  288. case FAST_FORWARD:
  289. setResult(Result.OK);
  290. break;
  291. case REJECTED:
  292. setResult(Result.REJECTED_NONFASTFORWARD);
  293. break;
  294. case REJECTED_CURRENT_BRANCH:
  295. setResult(Result.REJECTED_CURRENT_BRANCH);
  296. break;
  297. default:
  298. setResult(Result.REJECTED_OTHER_REASON, r.name());
  299. break;
  300. }
  301. }
  302. @Override
  303. public String toString() {
  304. return getType().name() + ": " + getOldId().name() + " "
  305. + getNewId().name() + " " + getRefName();
  306. }
  307. }