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

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