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 11KB

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