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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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.annotations.NonNull;
  52. import org.eclipse.jgit.annotations.Nullable;
  53. import org.eclipse.jgit.internal.JGitText;
  54. import org.eclipse.jgit.lib.AnyObjectId;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.Ref;
  57. import org.eclipse.jgit.lib.RefUpdate;
  58. import org.eclipse.jgit.revwalk.RevCommit;
  59. import org.eclipse.jgit.revwalk.RevObject;
  60. import org.eclipse.jgit.revwalk.RevWalk;
  61. /**
  62. * A command being processed by
  63. * {@link org.eclipse.jgit.transport.ReceivePack}.
  64. * <p>
  65. * This command instance roughly translates to the server side representation of
  66. * the {@link org.eclipse.jgit.transport.RemoteRefUpdate} created by the client.
  67. */
  68. public class ReceiveCommand {
  69. /** Type of operation requested. */
  70. public enum Type {
  71. /** Create a new ref; the ref must not already exist. */
  72. CREATE,
  73. /**
  74. * Update an existing ref with a fast-forward update.
  75. * <p>
  76. * During a fast-forward update no changes will be lost; only new
  77. * commits are inserted into the ref.
  78. */
  79. UPDATE,
  80. /**
  81. * Update an existing ref by potentially discarding objects.
  82. * <p>
  83. * The current value of the ref is not fully reachable from the new
  84. * value of the ref, so a successful command may result in one or more
  85. * objects becoming unreachable.
  86. */
  87. UPDATE_NONFASTFORWARD,
  88. /** Delete an existing ref; the ref should already exist. */
  89. DELETE;
  90. }
  91. /** Result of the update command. */
  92. public enum Result {
  93. /** The command has not yet been attempted by the server. */
  94. NOT_ATTEMPTED,
  95. /** The server is configured to deny creation of this ref. */
  96. REJECTED_NOCREATE,
  97. /** The server is configured to deny deletion of this ref. */
  98. REJECTED_NODELETE,
  99. /** The update is a non-fast-forward update and isn't permitted. */
  100. REJECTED_NONFASTFORWARD,
  101. /** The update affects <code>HEAD</code> and cannot be permitted. */
  102. REJECTED_CURRENT_BRANCH,
  103. /**
  104. * One or more objects aren't in the repository.
  105. * <p>
  106. * This is severe indication of either repository corruption on the
  107. * server side, or a bug in the client wherein the client did not supply
  108. * all required objects during the pack transfer.
  109. */
  110. REJECTED_MISSING_OBJECT,
  111. /** Other failure; see {@link ReceiveCommand#getMessage()}. */
  112. REJECTED_OTHER_REASON,
  113. /** The ref could not be locked and updated atomically; try again. */
  114. LOCK_FAILURE,
  115. /** The change was completed successfully. */
  116. OK;
  117. }
  118. /**
  119. * Filter a collection of commands according to result.
  120. *
  121. * @param in
  122. * commands to filter.
  123. * @param want
  124. * desired status to filter by.
  125. * @return a copy of the command list containing only those commands with
  126. * the desired status.
  127. * @since 4.2
  128. */
  129. public static List<ReceiveCommand> filter(Iterable<ReceiveCommand> in,
  130. Result want) {
  131. List<ReceiveCommand> r;
  132. if (in instanceof Collection)
  133. r = new ArrayList<>(((Collection<?>) in).size());
  134. else
  135. r = new ArrayList<>();
  136. for (ReceiveCommand cmd : in) {
  137. if (cmd.getResult() == want)
  138. r.add(cmd);
  139. }
  140. return r;
  141. }
  142. /**
  143. * Filter a list of commands according to result.
  144. *
  145. * @param commands
  146. * commands to filter.
  147. * @param want
  148. * desired status to filter by.
  149. * @return a copy of the command list containing only those commands with
  150. * the desired status.
  151. * @since 2.0
  152. */
  153. public static List<ReceiveCommand> filter(List<ReceiveCommand> commands,
  154. Result want) {
  155. return filter((Iterable<ReceiveCommand>) commands, want);
  156. }
  157. /**
  158. * Set unprocessed commands as failed due to transaction aborted.
  159. * <p>
  160. * If a command is still
  161. * {@link org.eclipse.jgit.transport.ReceiveCommand.Result#NOT_ATTEMPTED} it
  162. * will be set to
  163. * {@link org.eclipse.jgit.transport.ReceiveCommand.Result#REJECTED_OTHER_REASON}.
  164. *
  165. * @param commands
  166. * commands to mark as failed.
  167. * @since 4.2
  168. */
  169. public static void abort(Iterable<ReceiveCommand> commands) {
  170. for (ReceiveCommand c : commands) {
  171. if (c.getResult() == NOT_ATTEMPTED) {
  172. c.setResult(REJECTED_OTHER_REASON,
  173. JGitText.get().transactionAborted);
  174. }
  175. }
  176. }
  177. /**
  178. * Check whether a command failed due to transaction aborted.
  179. *
  180. * @param cmd
  181. * command.
  182. * @return whether the command failed due to transaction aborted, as in
  183. * {@link #abort(Iterable)}.
  184. * @since 4.9
  185. */
  186. public static boolean isTransactionAborted(ReceiveCommand cmd) {
  187. return cmd.getResult() == REJECTED_OTHER_REASON
  188. && cmd.getMessage().equals(JGitText.get().transactionAborted);
  189. }
  190. /**
  191. * Create a command to switch a reference from object to symbolic.
  192. *
  193. * @param oldId
  194. * expected oldId. May be {@code zeroId} to create.
  195. * @param newTarget
  196. * new target; must begin with {@code "refs/"}.
  197. * @param name
  198. * name of the reference to make symbolic.
  199. * @return command instance.
  200. * @since 4.10
  201. */
  202. public static ReceiveCommand link(@NonNull ObjectId oldId,
  203. @NonNull String newTarget, @NonNull String name) {
  204. return new ReceiveCommand(oldId, newTarget, name);
  205. }
  206. /**
  207. * Create a command to switch a symbolic reference's target.
  208. *
  209. * @param oldTarget
  210. * expected old target. May be null to create.
  211. * @param newTarget
  212. * new target; must begin with {@code "refs/"}.
  213. * @param name
  214. * name of the reference to make symbolic.
  215. * @return command instance.
  216. * @since 4.10
  217. */
  218. public static ReceiveCommand link(@Nullable String oldTarget,
  219. @NonNull String newTarget, @NonNull String name) {
  220. return new ReceiveCommand(oldTarget, newTarget, name);
  221. }
  222. /**
  223. * Create a command to switch a reference from symbolic to object.
  224. *
  225. * @param oldTarget
  226. * expected old target.
  227. * @param newId
  228. * new object identifier. May be {@code zeroId()} to delete.
  229. * @param name
  230. * name of the reference to convert from symbolic.
  231. * @return command instance.
  232. * @since 4.10
  233. */
  234. public static ReceiveCommand unlink(@NonNull String oldTarget,
  235. @NonNull ObjectId newId, @NonNull String name) {
  236. return new ReceiveCommand(oldTarget, newId, name);
  237. }
  238. private final ObjectId oldId;
  239. private final String oldSymref;
  240. private final ObjectId newId;
  241. private final String newSymref;
  242. private final String name;
  243. private Type type;
  244. private boolean typeIsCorrect;
  245. private Ref ref;
  246. private Result status = Result.NOT_ATTEMPTED;
  247. private String message;
  248. private boolean customRefLog;
  249. private String refLogMessage;
  250. private boolean refLogIncludeResult;
  251. private Boolean forceRefLog;
  252. /**
  253. * Create a new command for
  254. * {@link org.eclipse.jgit.transport.ReceivePack}.
  255. *
  256. * @param oldId
  257. * the expected old object id; must not be null. Use
  258. * {@link org.eclipse.jgit.lib.ObjectId#zeroId()} to indicate a
  259. * ref creation.
  260. * @param newId
  261. * the new object id; must not be null. Use
  262. * {@link org.eclipse.jgit.lib.ObjectId#zeroId()} to indicate a
  263. * ref deletion.
  264. * @param name
  265. * name of the ref being affected.
  266. */
  267. public ReceiveCommand(final ObjectId oldId, final ObjectId newId,
  268. final String name) {
  269. if (oldId == null) {
  270. throw new IllegalArgumentException(
  271. JGitText.get().oldIdMustNotBeNull);
  272. }
  273. if (newId == null) {
  274. throw new IllegalArgumentException(
  275. JGitText.get().newIdMustNotBeNull);
  276. }
  277. if (name == null || name.isEmpty()) {
  278. throw new IllegalArgumentException(
  279. JGitText.get().nameMustNotBeNullOrEmpty);
  280. }
  281. this.oldId = oldId;
  282. this.oldSymref = null;
  283. this.newId = newId;
  284. this.newSymref = null;
  285. this.name = name;
  286. type = Type.UPDATE;
  287. if (ObjectId.zeroId().equals(oldId)) {
  288. type = Type.CREATE;
  289. }
  290. if (ObjectId.zeroId().equals(newId)) {
  291. type = Type.DELETE;
  292. }
  293. }
  294. /**
  295. * Create a new command for
  296. * {@link org.eclipse.jgit.transport.ReceivePack}.
  297. *
  298. * @param oldId
  299. * the old object id; must not be null. Use
  300. * {@link org.eclipse.jgit.lib.ObjectId#zeroId()} to indicate a
  301. * ref creation.
  302. * @param newId
  303. * the new object id; must not be null. Use
  304. * {@link org.eclipse.jgit.lib.ObjectId#zeroId()} to indicate a
  305. * ref deletion.
  306. * @param name
  307. * name of the ref being affected.
  308. * @param type
  309. * type of the command. Must be
  310. * {@link org.eclipse.jgit.transport.ReceiveCommand.Type#CREATE}
  311. * if {@code
  312. * oldId} is zero, or
  313. * {@link org.eclipse.jgit.transport.ReceiveCommand.Type#DELETE}
  314. * if {@code newId} is zero.
  315. * @since 2.0
  316. */
  317. public ReceiveCommand(final ObjectId oldId, final ObjectId newId,
  318. final String name, final Type type) {
  319. if (oldId == null) {
  320. throw new IllegalArgumentException(
  321. JGitText.get().oldIdMustNotBeNull);
  322. }
  323. if (newId == null) {
  324. throw new IllegalArgumentException(
  325. JGitText.get().newIdMustNotBeNull);
  326. }
  327. if (name == null || name.isEmpty()) {
  328. throw new IllegalArgumentException(
  329. JGitText.get().nameMustNotBeNullOrEmpty);
  330. }
  331. this.oldId = oldId;
  332. this.oldSymref = null;
  333. this.newId = newId;
  334. this.newSymref = null;
  335. this.name = name;
  336. switch (type) {
  337. case CREATE:
  338. if (!ObjectId.zeroId().equals(oldId)) {
  339. throw new IllegalArgumentException(
  340. JGitText.get().createRequiresZeroOldId);
  341. }
  342. break;
  343. case DELETE:
  344. if (!ObjectId.zeroId().equals(newId)) {
  345. throw new IllegalArgumentException(
  346. JGitText.get().deleteRequiresZeroNewId);
  347. }
  348. break;
  349. case UPDATE:
  350. case UPDATE_NONFASTFORWARD:
  351. if (ObjectId.zeroId().equals(newId)
  352. || ObjectId.zeroId().equals(oldId)) {
  353. throw new IllegalArgumentException(
  354. JGitText.get().updateRequiresOldIdAndNewId);
  355. }
  356. break;
  357. default:
  358. throw new IllegalStateException(
  359. JGitText.get().enumValueNotSupported0);
  360. }
  361. this.type = type;
  362. }
  363. /**
  364. * Create a command to switch a reference from object to symbolic.
  365. *
  366. * @param oldId
  367. * the old object id; must not be null. Use
  368. * {@link ObjectId#zeroId()} to indicate a ref creation.
  369. * @param newSymref
  370. * new target, must begin with {@code "refs/"}. Use {@code null}
  371. * to indicate a ref deletion.
  372. * @param name
  373. * name of the reference to make symbolic.
  374. * @since 4.10
  375. */
  376. private ReceiveCommand(ObjectId oldId, String newSymref, String name) {
  377. if (oldId == null) {
  378. throw new IllegalArgumentException(
  379. JGitText.get().oldIdMustNotBeNull);
  380. }
  381. if (name == null || name.isEmpty()) {
  382. throw new IllegalArgumentException(
  383. JGitText.get().nameMustNotBeNullOrEmpty);
  384. }
  385. this.oldId = oldId;
  386. this.oldSymref = null;
  387. this.newId = ObjectId.zeroId();
  388. this.newSymref = newSymref;
  389. this.name = name;
  390. if (AnyObjectId.isEqual(ObjectId.zeroId(), oldId)) {
  391. type = Type.CREATE;
  392. } else if (newSymref != null) {
  393. type = Type.UPDATE;
  394. } else {
  395. type = Type.DELETE;
  396. }
  397. typeIsCorrect = true;
  398. }
  399. /**
  400. * Create a command to switch a reference from symbolic to object.
  401. *
  402. * @param oldSymref
  403. * expected old target. Use {@code null} to indicate a ref
  404. * creation.
  405. * @param newId
  406. * the new object id; must not be null. Use
  407. * {@link ObjectId#zeroId()} to indicate a ref deletion.
  408. * @param name
  409. * name of the reference to convert from symbolic.
  410. * @since 4.10
  411. */
  412. private ReceiveCommand(String oldSymref, ObjectId newId, String name) {
  413. if (newId == null) {
  414. throw new IllegalArgumentException(
  415. JGitText.get().newIdMustNotBeNull);
  416. }
  417. if (name == null || name.isEmpty()) {
  418. throw new IllegalArgumentException(
  419. JGitText.get().nameMustNotBeNullOrEmpty);
  420. }
  421. this.oldId = ObjectId.zeroId();
  422. this.oldSymref = oldSymref;
  423. this.newId = newId;
  424. this.newSymref = null;
  425. this.name = name;
  426. if (oldSymref == null) {
  427. type = Type.CREATE;
  428. } else if (!AnyObjectId.isEqual(ObjectId.zeroId(), newId)) {
  429. type = Type.UPDATE;
  430. } else {
  431. type = Type.DELETE;
  432. }
  433. typeIsCorrect = true;
  434. }
  435. /**
  436. * Create a command to switch a symbolic reference's target.
  437. *
  438. * @param oldTarget
  439. * expected old target. Use {@code null} to indicate a ref
  440. * creation.
  441. * @param newTarget
  442. * new target. Use {@code null} to indicate a ref deletion.
  443. * @param name
  444. * name of the reference to make symbolic.
  445. * @since 4.10
  446. */
  447. private ReceiveCommand(@Nullable String oldTarget, String newTarget, String name) {
  448. if (name == null || name.isEmpty()) {
  449. throw new IllegalArgumentException(
  450. JGitText.get().nameMustNotBeNullOrEmpty);
  451. }
  452. this.oldId = ObjectId.zeroId();
  453. this.oldSymref = oldTarget;
  454. this.newId = ObjectId.zeroId();
  455. this.newSymref = newTarget;
  456. this.name = name;
  457. if (oldTarget == null) {
  458. if (newTarget == null) {
  459. throw new IllegalArgumentException(
  460. JGitText.get().bothRefTargetsMustNotBeNull);
  461. }
  462. type = Type.CREATE;
  463. } else if (newTarget != null) {
  464. type = Type.UPDATE;
  465. } else {
  466. type = Type.DELETE;
  467. }
  468. typeIsCorrect = true;
  469. }
  470. /**
  471. * Get the old value the client thinks the ref has.
  472. *
  473. * @return the old value the client thinks the ref has.
  474. */
  475. public ObjectId getOldId() {
  476. return oldId;
  477. }
  478. /**
  479. * Get expected old target for a symbolic reference.
  480. *
  481. * @return expected old target for a symbolic reference.
  482. * @since 4.10
  483. */
  484. @Nullable
  485. public String getOldSymref() {
  486. return oldSymref;
  487. }
  488. /**
  489. * Get the requested new value for this ref.
  490. *
  491. * @return the requested new value for this ref.
  492. */
  493. public ObjectId getNewId() {
  494. return newId;
  495. }
  496. /**
  497. * Get requested new target for a symbolic reference.
  498. *
  499. * @return requested new target for a symbolic reference.
  500. * @since 4.10
  501. */
  502. @Nullable
  503. public String getNewSymref() {
  504. return newSymref;
  505. }
  506. /**
  507. * Get the name of the ref being updated.
  508. *
  509. * @return the name of the ref being updated.
  510. */
  511. public String getRefName() {
  512. return name;
  513. }
  514. /**
  515. * Get the type of this command; see {@link Type}.
  516. *
  517. * @return the type of this command; see {@link Type}.
  518. */
  519. public Type getType() {
  520. return type;
  521. }
  522. /**
  523. * Get the ref, if this was advertised by the connection.
  524. *
  525. * @return the ref, if this was advertised by the connection.
  526. */
  527. public Ref getRef() {
  528. return ref;
  529. }
  530. /**
  531. * Get the current status code of this command.
  532. *
  533. * @return the current status code of this command.
  534. */
  535. public Result getResult() {
  536. return status;
  537. }
  538. /**
  539. * Get the message associated with a failure status.
  540. *
  541. * @return the message associated with a failure status.
  542. */
  543. public String getMessage() {
  544. return message;
  545. }
  546. /**
  547. * Set the message to include in the reflog.
  548. * <p>
  549. * Overrides the default set by {@code setRefLogMessage} on any containing
  550. * {@link org.eclipse.jgit.lib.BatchRefUpdate}.
  551. *
  552. * @param msg
  553. * the message to describe this change. If null and appendStatus is
  554. * false, the reflog will not be updated.
  555. * @param appendStatus
  556. * true if the status of the ref change (fast-forward or
  557. * forced-update) should be appended to the user supplied message.
  558. * @since 4.9
  559. */
  560. public void setRefLogMessage(String msg, boolean appendStatus) {
  561. customRefLog = true;
  562. if (msg == null && !appendStatus) {
  563. disableRefLog();
  564. } else if (msg == null && appendStatus) {
  565. refLogMessage = ""; //$NON-NLS-1$
  566. refLogIncludeResult = true;
  567. } else {
  568. refLogMessage = msg;
  569. refLogIncludeResult = appendStatus;
  570. }
  571. }
  572. /**
  573. * Don't record this update in the ref's associated reflog.
  574. * <p>
  575. * Equivalent to {@code setRefLogMessage(null, false)}.
  576. *
  577. * @since 4.9
  578. */
  579. public void disableRefLog() {
  580. customRefLog = true;
  581. refLogMessage = null;
  582. refLogIncludeResult = false;
  583. }
  584. /**
  585. * Force writing a reflog for the updated ref.
  586. *
  587. * @param force whether to force.
  588. * @since 4.9
  589. */
  590. public void setForceRefLog(boolean force) {
  591. forceRefLog = Boolean.valueOf(force);
  592. }
  593. /**
  594. * Check whether this command has a custom reflog message setting that should
  595. * override defaults in any containing
  596. * {@link org.eclipse.jgit.lib.BatchRefUpdate}.
  597. * <p>
  598. * Does not take into account whether {@code #setForceRefLog(boolean)} has
  599. * been called.
  600. *
  601. * @return whether a custom reflog is set.
  602. * @since 4.9
  603. */
  604. public boolean hasCustomRefLog() {
  605. return customRefLog;
  606. }
  607. /**
  608. * Check whether log has been disabled by {@link #disableRefLog()}.
  609. *
  610. * @return true if disabled.
  611. * @since 4.9
  612. */
  613. public boolean isRefLogDisabled() {
  614. return refLogMessage == null;
  615. }
  616. /**
  617. * Get the message to include in the reflog.
  618. *
  619. * @return message the caller wants to include in the reflog; null if the
  620. * update should not be logged.
  621. * @since 4.9
  622. */
  623. @Nullable
  624. public String getRefLogMessage() {
  625. return refLogMessage;
  626. }
  627. /**
  628. * Check whether the reflog message should include the result of the update,
  629. * such as fast-forward or force-update.
  630. *
  631. * @return true if the message should include the result.
  632. * @since 4.9
  633. */
  634. public boolean isRefLogIncludingResult() {
  635. return refLogIncludeResult;
  636. }
  637. /**
  638. * Check whether the reflog should be written regardless of repo defaults.
  639. *
  640. * @return whether force writing is enabled; {@code null} if
  641. * {@code #setForceRefLog(boolean)} was never called.
  642. * @since 4.9
  643. */
  644. @Nullable
  645. public Boolean isForceRefLog() {
  646. return forceRefLog;
  647. }
  648. /**
  649. * Set the status of this command.
  650. *
  651. * @param s
  652. * the new status code for this command.
  653. */
  654. public void setResult(Result s) {
  655. setResult(s, null);
  656. }
  657. /**
  658. * Set the status of this command.
  659. *
  660. * @param s
  661. * new status code for this command.
  662. * @param m
  663. * optional message explaining the new status.
  664. */
  665. public void setResult(Result s, String m) {
  666. status = s;
  667. message = m;
  668. }
  669. /**
  670. * Update the type of this command by checking for fast-forward.
  671. * <p>
  672. * If the command's current type is UPDATE, a merge test will be performed
  673. * using the supplied RevWalk to determine if {@link #getOldId()} is fully
  674. * merged into {@link #getNewId()}. If some commits are not merged the
  675. * update type is changed to
  676. * {@link org.eclipse.jgit.transport.ReceiveCommand.Type#UPDATE_NONFASTFORWARD}.
  677. *
  678. * @param walk
  679. * an instance to perform the merge test with. The caller must
  680. * allocate and release this object.
  681. * @throws java.io.IOException
  682. * either oldId or newId is not accessible in the repository
  683. * used by the RevWalk. This usually indicates data corruption,
  684. * and the command cannot be processed.
  685. */
  686. public void updateType(RevWalk walk) throws IOException {
  687. if (typeIsCorrect)
  688. return;
  689. if (type == Type.UPDATE && !AnyObjectId.isEqual(oldId, newId)) {
  690. RevObject o = walk.parseAny(oldId);
  691. RevObject n = walk.parseAny(newId);
  692. if (!(o instanceof RevCommit)
  693. || !(n instanceof RevCommit)
  694. || !walk.isMergedInto((RevCommit) o, (RevCommit) n))
  695. setType(Type.UPDATE_NONFASTFORWARD);
  696. }
  697. typeIsCorrect = true;
  698. }
  699. /**
  700. * Execute this command during a receive-pack session.
  701. * <p>
  702. * Sets the status of the command as a side effect.
  703. *
  704. * @param rp
  705. * receive-pack session.
  706. * @since 5.6
  707. */
  708. public void execute(ReceivePack rp) {
  709. try {
  710. String expTarget = getOldSymref();
  711. boolean detach = getNewSymref() != null
  712. || (type == Type.DELETE && expTarget != null);
  713. RefUpdate ru = rp.getRepository().updateRef(getRefName(), detach);
  714. if (expTarget != null) {
  715. if (!ru.getRef().isSymbolic() || !ru.getRef().getTarget()
  716. .getName().equals(expTarget)) {
  717. setResult(Result.LOCK_FAILURE);
  718. return;
  719. }
  720. }
  721. ru.setRefLogIdent(rp.getRefLogIdent());
  722. ru.setRefLogMessage(refLogMessage, refLogIncludeResult);
  723. switch (getType()) {
  724. case DELETE:
  725. if (!ObjectId.zeroId().equals(getOldId())) {
  726. // We can only do a CAS style delete if the client
  727. // didn't bork its delete request by sending the
  728. // wrong zero id rather than the advertised one.
  729. //
  730. ru.setExpectedOldObjectId(getOldId());
  731. }
  732. ru.setForceUpdate(true);
  733. setResult(ru.delete(rp.getRevWalk()));
  734. break;
  735. case CREATE:
  736. case UPDATE:
  737. case UPDATE_NONFASTFORWARD:
  738. ru.setForceUpdate(rp.isAllowNonFastForwards());
  739. ru.setExpectedOldObjectId(getOldId());
  740. ru.setRefLogMessage("push", true); //$NON-NLS-1$
  741. if (getNewSymref() != null) {
  742. setResult(ru.link(getNewSymref()));
  743. } else {
  744. ru.setNewObjectId(getNewId());
  745. setResult(ru.update(rp.getRevWalk()));
  746. }
  747. break;
  748. }
  749. } catch (IOException err) {
  750. reject(err);
  751. }
  752. }
  753. void setRef(Ref r) {
  754. ref = r;
  755. }
  756. void setType(Type t) {
  757. type = t;
  758. }
  759. void setTypeFastForwardUpdate() {
  760. type = Type.UPDATE;
  761. typeIsCorrect = true;
  762. }
  763. /**
  764. * Set the result of this command.
  765. *
  766. * @param r
  767. * the new result code for this command.
  768. */
  769. public void setResult(RefUpdate.Result r) {
  770. switch (r) {
  771. case NOT_ATTEMPTED:
  772. setResult(Result.NOT_ATTEMPTED);
  773. break;
  774. case LOCK_FAILURE:
  775. case IO_FAILURE:
  776. setResult(Result.LOCK_FAILURE);
  777. break;
  778. case NO_CHANGE:
  779. case NEW:
  780. case FORCED:
  781. case FAST_FORWARD:
  782. setResult(Result.OK);
  783. break;
  784. case REJECTED:
  785. setResult(Result.REJECTED_NONFASTFORWARD);
  786. break;
  787. case REJECTED_CURRENT_BRANCH:
  788. setResult(Result.REJECTED_CURRENT_BRANCH);
  789. break;
  790. case REJECTED_MISSING_OBJECT:
  791. setResult(Result.REJECTED_MISSING_OBJECT);
  792. break;
  793. case REJECTED_OTHER_REASON:
  794. setResult(Result.REJECTED_OTHER_REASON);
  795. break;
  796. default:
  797. setResult(Result.REJECTED_OTHER_REASON, r.name());
  798. break;
  799. }
  800. }
  801. void reject(IOException err) {
  802. setResult(Result.REJECTED_OTHER_REASON, MessageFormat.format(
  803. JGitText.get().lockError, err.getMessage()));
  804. }
  805. /** {@inheritDoc} */
  806. @SuppressWarnings("nls")
  807. @Override
  808. public String toString() {
  809. return getType().name() + ": " + getOldId().name() + " "
  810. + getNewId().name() + " " + getRefName();
  811. }
  812. }