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;
  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. status = Result.NOT_ATTEMPTED;
  163. }
  164. /**
  165. * Create a new command for {@link BaseReceivePack}.
  166. *
  167. * @param oldId
  168. * the old object id; must not be null. Use
  169. * {@link ObjectId#zeroId()} to indicate a ref creation.
  170. * @param newId
  171. * the new object id; must not be null. Use
  172. * {@link ObjectId#zeroId()} to indicate a ref deletion.
  173. * @param name
  174. * name of the ref being affected.
  175. * @param type
  176. * type of the command.
  177. * @since 2.0
  178. */
  179. public ReceiveCommand(final ObjectId oldId, final ObjectId newId,
  180. final String name, final Type type) {
  181. this.oldId = oldId;
  182. this.newId = newId;
  183. this.name = name;
  184. this.type = type;
  185. }
  186. /** @return the old value the client thinks the ref has. */
  187. public ObjectId getOldId() {
  188. return oldId;
  189. }
  190. /** @return the requested new value for this ref. */
  191. public ObjectId getNewId() {
  192. return newId;
  193. }
  194. /** @return the name of the ref being updated. */
  195. public String getRefName() {
  196. return name;
  197. }
  198. /** @return the type of this command; see {@link Type}. */
  199. public Type getType() {
  200. return type;
  201. }
  202. /** @return the ref, if this was advertised by the connection. */
  203. public Ref getRef() {
  204. return ref;
  205. }
  206. /** @return the current status code of this command. */
  207. public Result getResult() {
  208. return status;
  209. }
  210. /** @return the message associated with a failure status. */
  211. public String getMessage() {
  212. return message;
  213. }
  214. /**
  215. * Set the status of this command.
  216. *
  217. * @param s
  218. * the new status code for this command.
  219. */
  220. public void setResult(final Result s) {
  221. setResult(s, null);
  222. }
  223. /**
  224. * Set the status of this command.
  225. *
  226. * @param s
  227. * new status code for this command.
  228. * @param m
  229. * optional message explaining the new status.
  230. */
  231. public void setResult(final Result s, final String m) {
  232. status = s;
  233. message = m;
  234. }
  235. /**
  236. * Update the type of this command by checking for fast-forward.
  237. * <p>
  238. * If the command's current type is UPDATE, a merge test will be performed
  239. * using the supplied RevWalk to determine if {@link #getOldId()} is fully
  240. * merged into {@link #getNewId()}. If some commits are not merged the
  241. * update type is changed to {@link Type#UPDATE_NONFASTFORWARD}.
  242. *
  243. * @param walk
  244. * an instance to perform the merge test with. The caller must
  245. * allocate and release this object.
  246. * @throws IOException
  247. * either oldId or newId is not accessible in the repository
  248. * used by the RevWalk. This usually indicates data corruption,
  249. * and the command cannot be processed.
  250. */
  251. public void updateType(RevWalk walk) throws IOException {
  252. if (typeIsCorrect)
  253. return;
  254. if (type == Type.UPDATE && !AnyObjectId.equals(oldId, newId)) {
  255. RevObject o = walk.parseAny(oldId);
  256. RevObject n = walk.parseAny(newId);
  257. if (!(o instanceof RevCommit)
  258. || !(n instanceof RevCommit)
  259. || !walk.isMergedInto((RevCommit) o, (RevCommit) n))
  260. setType(Type.UPDATE_NONFASTFORWARD);
  261. }
  262. typeIsCorrect = true;
  263. }
  264. /**
  265. * Execute this command during a receive-pack session.
  266. * <p>
  267. * Sets the status of the command as a side effect.
  268. *
  269. * @param rp
  270. * receive-pack session.
  271. * @since 2.0
  272. */
  273. public void execute(final BaseReceivePack rp) {
  274. try {
  275. final RefUpdate ru = rp.getRepository().updateRef(getRefName());
  276. ru.setRefLogIdent(rp.getRefLogIdent());
  277. switch (getType()) {
  278. case DELETE:
  279. if (!ObjectId.zeroId().equals(getOldId())) {
  280. // We can only do a CAS style delete if the client
  281. // didn't bork its delete request by sending the
  282. // wrong zero id rather than the advertised one.
  283. //
  284. ru.setExpectedOldObjectId(getOldId());
  285. }
  286. ru.setForceUpdate(true);
  287. setResult(ru.delete(rp.getRevWalk()));
  288. break;
  289. case CREATE:
  290. case UPDATE:
  291. case UPDATE_NONFASTFORWARD:
  292. ru.setForceUpdate(rp.isAllowNonFastForwards());
  293. ru.setExpectedOldObjectId(getOldId());
  294. ru.setNewObjectId(getNewId());
  295. ru.setRefLogMessage("push", true);
  296. setResult(ru.update(rp.getRevWalk()));
  297. break;
  298. }
  299. } catch (IOException err) {
  300. reject(err);
  301. }
  302. }
  303. void setRef(final Ref r) {
  304. ref = r;
  305. }
  306. void setType(final Type t) {
  307. type = t;
  308. }
  309. void setTypeFastForwardUpdate() {
  310. type = Type.UPDATE;
  311. typeIsCorrect = true;
  312. }
  313. /**
  314. * Set the result of this command.
  315. *
  316. * @param r
  317. * the new result code for this command.
  318. */
  319. public void setResult(RefUpdate.Result r) {
  320. switch (r) {
  321. case NOT_ATTEMPTED:
  322. setResult(Result.NOT_ATTEMPTED);
  323. break;
  324. case LOCK_FAILURE:
  325. case IO_FAILURE:
  326. setResult(Result.LOCK_FAILURE);
  327. break;
  328. case NO_CHANGE:
  329. case NEW:
  330. case FORCED:
  331. case FAST_FORWARD:
  332. setResult(Result.OK);
  333. break;
  334. case REJECTED:
  335. setResult(Result.REJECTED_NONFASTFORWARD);
  336. break;
  337. case REJECTED_CURRENT_BRANCH:
  338. setResult(Result.REJECTED_CURRENT_BRANCH);
  339. break;
  340. default:
  341. setResult(Result.REJECTED_OTHER_REASON, r.name());
  342. break;
  343. }
  344. }
  345. void reject(IOException err) {
  346. setResult(Result.REJECTED_OTHER_REASON, MessageFormat.format(
  347. JGitText.get().lockError, err.getMessage()));
  348. }
  349. @Override
  350. public String toString() {
  351. return getType().name() + ": " + getOldId().name() + " "
  352. + getNewId().name() + " " + getRefName();
  353. }
  354. }