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.

BatchRefUpdate.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * Copyright (C) 2008-2012, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED;
  46. import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_OTHER_REASON;
  47. import java.io.IOException;
  48. import java.text.MessageFormat;
  49. import java.time.Duration;
  50. import java.util.ArrayList;
  51. import java.util.Arrays;
  52. import java.util.Collection;
  53. import java.util.Collections;
  54. import java.util.HashSet;
  55. import java.util.List;
  56. import java.util.concurrent.TimeoutException;
  57. import org.eclipse.jgit.annotations.Nullable;
  58. import org.eclipse.jgit.errors.MissingObjectException;
  59. import org.eclipse.jgit.internal.JGitText;
  60. import org.eclipse.jgit.revwalk.RevWalk;
  61. import org.eclipse.jgit.transport.PushCertificate;
  62. import org.eclipse.jgit.transport.ReceiveCommand;
  63. import org.eclipse.jgit.util.time.ProposedTimestamp;
  64. /**
  65. * Batch of reference updates to be applied to a repository.
  66. * <p>
  67. * The batch update is primarily useful in the transport code, where a client or
  68. * server is making changes to more than one reference at a time.
  69. */
  70. public class BatchRefUpdate {
  71. /**
  72. * Maximum delay the calling thread will tolerate while waiting for a
  73. * {@code MonotonicClock} to resolve associated {@link ProposedTimestamp}s.
  74. * <p>
  75. * A default of 5 seconds was chosen by guessing. A common assumption is
  76. * clock skew between machines on the same LAN using an NTP server also on
  77. * the same LAN should be under 5 seconds. 5 seconds is also not that long
  78. * for a large `git push` operation to complete.
  79. *
  80. * @since 4.9
  81. */
  82. protected static final Duration MAX_WAIT = Duration.ofSeconds(5);
  83. private final RefDatabase refdb;
  84. /** Commands to apply during this batch. */
  85. private final List<ReceiveCommand> commands;
  86. /** Does the caller permit a forced update on a reference? */
  87. private boolean allowNonFastForwards;
  88. /** Identity to record action as within the reflog. */
  89. private PersonIdent refLogIdent;
  90. /** Message the caller wants included in the reflog. */
  91. private String refLogMessage;
  92. /** Should the result value be appended to {@link #refLogMessage}. */
  93. private boolean refLogIncludeResult;
  94. /**
  95. * Should reflogs be written even if the configured default for this ref is
  96. * not to write it.
  97. */
  98. private boolean forceRefLog;
  99. /** Push certificate associated with this update. */
  100. private PushCertificate pushCert;
  101. /** Whether updates should be atomic. */
  102. private boolean atomic;
  103. /** Push options associated with this update. */
  104. private List<String> pushOptions;
  105. /** Associated timestamps that should be blocked on before update. */
  106. private List<ProposedTimestamp> timestamps;
  107. /**
  108. * Initialize a new batch update.
  109. *
  110. * @param refdb
  111. * the reference database of the repository to be updated.
  112. */
  113. protected BatchRefUpdate(RefDatabase refdb) {
  114. this.refdb = refdb;
  115. this.commands = new ArrayList<>();
  116. this.atomic = refdb.performsAtomicTransactions();
  117. }
  118. /**
  119. * Whether the batch update will permit a non-fast-forward update to an
  120. * existing reference.
  121. *
  122. * @return true if the batch update will permit a non-fast-forward update to
  123. * an existing reference.
  124. */
  125. public boolean isAllowNonFastForwards() {
  126. return allowNonFastForwards;
  127. }
  128. /**
  129. * Set if this update wants to permit a forced update.
  130. *
  131. * @param allow
  132. * true if this update batch should ignore merge tests.
  133. * @return {@code this}.
  134. */
  135. public BatchRefUpdate setAllowNonFastForwards(boolean allow) {
  136. allowNonFastForwards = allow;
  137. return this;
  138. }
  139. /**
  140. * Get identity of the user making the change in the reflog.
  141. *
  142. * @return identity of the user making the change in the reflog.
  143. */
  144. public PersonIdent getRefLogIdent() {
  145. return refLogIdent;
  146. }
  147. /**
  148. * Set the identity of the user appearing in the reflog.
  149. * <p>
  150. * The timestamp portion of the identity is ignored. A new identity with the
  151. * current timestamp will be created automatically when the update occurs
  152. * and the log record is written.
  153. *
  154. * @param pi
  155. * identity of the user. If null the identity will be
  156. * automatically determined based on the repository
  157. * configuration.
  158. * @return {@code this}.
  159. */
  160. public BatchRefUpdate setRefLogIdent(PersonIdent pi) {
  161. refLogIdent = pi;
  162. return this;
  163. }
  164. /**
  165. * Get the message to include in the reflog.
  166. *
  167. * @return message the caller wants to include in the reflog; null if the
  168. * update should not be logged.
  169. */
  170. @Nullable
  171. public String getRefLogMessage() {
  172. return refLogMessage;
  173. }
  174. /**
  175. * Check whether the reflog message should include the result of the update,
  176. * such as fast-forward or force-update.
  177. * <p>
  178. * Describes the default for commands in this batch that do not override it
  179. * with
  180. * {@link org.eclipse.jgit.transport.ReceiveCommand#setRefLogMessage(String, boolean)}.
  181. *
  182. * @return true if the message should include the result.
  183. */
  184. public boolean isRefLogIncludingResult() {
  185. return refLogIncludeResult;
  186. }
  187. /**
  188. * Set the message to include in the reflog.
  189. * <p>
  190. * Repository implementations may limit which reflogs are written by
  191. * default, based on the project configuration. If a repo is not configured
  192. * to write logs for this ref by default, setting the message alone may have
  193. * no effect. To indicate that the repo should write logs for this update in
  194. * spite of configured defaults, use {@link #setForceRefLog(boolean)}.
  195. * <p>
  196. * Describes the default for commands in this batch that do not override it
  197. * with
  198. * {@link org.eclipse.jgit.transport.ReceiveCommand#setRefLogMessage(String, boolean)}.
  199. *
  200. * @param msg
  201. * the message to describe this change. If null and appendStatus
  202. * is false, the reflog will not be updated.
  203. * @param appendStatus
  204. * true if the status of the ref change (fast-forward or
  205. * forced-update) should be appended to the user supplied
  206. * message.
  207. * @return {@code this}.
  208. */
  209. public BatchRefUpdate setRefLogMessage(String msg, boolean appendStatus) {
  210. if (msg == null && !appendStatus)
  211. disableRefLog();
  212. else if (msg == null && appendStatus) {
  213. refLogMessage = ""; //$NON-NLS-1$
  214. refLogIncludeResult = true;
  215. } else {
  216. refLogMessage = msg;
  217. refLogIncludeResult = appendStatus;
  218. }
  219. return this;
  220. }
  221. /**
  222. * Don't record this update in the ref's associated reflog.
  223. * <p>
  224. * Equivalent to {@code setRefLogMessage(null, false)}.
  225. *
  226. * @return {@code this}.
  227. */
  228. public BatchRefUpdate disableRefLog() {
  229. refLogMessage = null;
  230. refLogIncludeResult = false;
  231. return this;
  232. }
  233. /**
  234. * Force writing a reflog for the updated ref.
  235. *
  236. * @param force whether to force.
  237. * @return {@code this}
  238. * @since 4.9
  239. */
  240. public BatchRefUpdate setForceRefLog(boolean force) {
  241. forceRefLog = force;
  242. return this;
  243. }
  244. /**
  245. * Check whether log has been disabled by {@link #disableRefLog()}.
  246. *
  247. * @return true if disabled.
  248. */
  249. public boolean isRefLogDisabled() {
  250. return refLogMessage == null;
  251. }
  252. /**
  253. * Check whether the reflog should be written regardless of repo defaults.
  254. *
  255. * @return whether force writing is enabled.
  256. * @since 4.9
  257. */
  258. protected boolean isForceRefLog() {
  259. return forceRefLog;
  260. }
  261. /**
  262. * Request that all updates in this batch be performed atomically.
  263. * <p>
  264. * When atomic updates are used, either all commands apply successfully, or
  265. * none do. Commands that might have otherwise succeeded are rejected with
  266. * {@code REJECTED_OTHER_REASON}.
  267. * <p>
  268. * This method only works if the underlying ref database supports atomic
  269. * transactions, i.e.
  270. * {@link org.eclipse.jgit.lib.RefDatabase#performsAtomicTransactions()}
  271. * returns true. Calling this method with true if the underlying ref
  272. * database does not support atomic transactions will cause all commands to
  273. * fail with {@code
  274. * REJECTED_OTHER_REASON}.
  275. *
  276. * @param atomic
  277. * whether updates should be atomic.
  278. * @return {@code this}
  279. * @since 4.4
  280. */
  281. public BatchRefUpdate setAtomic(boolean atomic) {
  282. this.atomic = atomic;
  283. return this;
  284. }
  285. /**
  286. * Whether updates should be atomic.
  287. *
  288. * @return atomic whether updates should be atomic.
  289. * @since 4.4
  290. */
  291. public boolean isAtomic() {
  292. return atomic;
  293. }
  294. /**
  295. * Set a push certificate associated with this update.
  296. * <p>
  297. * This usually includes commands to update the refs in this batch, but is not
  298. * required to.
  299. *
  300. * @param cert
  301. * push certificate, may be null.
  302. * @since 4.1
  303. */
  304. public void setPushCertificate(PushCertificate cert) {
  305. pushCert = cert;
  306. }
  307. /**
  308. * Set the push certificate associated with this update.
  309. * <p>
  310. * This usually includes commands to update the refs in this batch, but is not
  311. * required to.
  312. *
  313. * @return push certificate, may be null.
  314. * @since 4.1
  315. */
  316. protected PushCertificate getPushCertificate() {
  317. return pushCert;
  318. }
  319. /**
  320. * Get commands this update will process.
  321. *
  322. * @return commands this update will process.
  323. */
  324. public List<ReceiveCommand> getCommands() {
  325. return Collections.unmodifiableList(commands);
  326. }
  327. /**
  328. * Add a single command to this batch update.
  329. *
  330. * @param cmd
  331. * the command to add, must not be null.
  332. * @return {@code this}.
  333. */
  334. public BatchRefUpdate addCommand(ReceiveCommand cmd) {
  335. commands.add(cmd);
  336. return this;
  337. }
  338. /**
  339. * Add commands to this batch update.
  340. *
  341. * @param cmd
  342. * the commands to add, must not be null.
  343. * @return {@code this}.
  344. */
  345. public BatchRefUpdate addCommand(ReceiveCommand... cmd) {
  346. return addCommand(Arrays.asList(cmd));
  347. }
  348. /**
  349. * Add commands to this batch update.
  350. *
  351. * @param cmd
  352. * the commands to add, must not be null.
  353. * @return {@code this}.
  354. */
  355. public BatchRefUpdate addCommand(Collection<ReceiveCommand> cmd) {
  356. commands.addAll(cmd);
  357. return this;
  358. }
  359. /**
  360. * Gets the list of option strings associated with this update.
  361. *
  362. * @return push options that were passed to {@link #execute}; prior to calling
  363. * {@link #execute}, always returns null.
  364. * @since 4.5
  365. */
  366. @Nullable
  367. public List<String> getPushOptions() {
  368. return pushOptions;
  369. }
  370. /**
  371. * Set push options associated with this update.
  372. * <p>
  373. * Implementations must call this at the top of {@link #execute(RevWalk,
  374. * ProgressMonitor, List)}.
  375. *
  376. * @param options options passed to {@code execute}.
  377. * @since 4.9
  378. */
  379. protected void setPushOptions(List<String> options) {
  380. pushOptions = options;
  381. }
  382. /**
  383. * Get list of timestamps the batch must wait for.
  384. *
  385. * @return list of timestamps the batch must wait for.
  386. * @since 4.6
  387. */
  388. public List<ProposedTimestamp> getProposedTimestamps() {
  389. if (timestamps != null) {
  390. return Collections.unmodifiableList(timestamps);
  391. }
  392. return Collections.emptyList();
  393. }
  394. /**
  395. * Request the batch to wait for the affected timestamps to resolve.
  396. *
  397. * @param ts
  398. * a {@link org.eclipse.jgit.util.time.ProposedTimestamp} object.
  399. * @return {@code this}.
  400. * @since 4.6
  401. */
  402. public BatchRefUpdate addProposedTimestamp(ProposedTimestamp ts) {
  403. if (timestamps == null) {
  404. timestamps = new ArrayList<>(4);
  405. }
  406. timestamps.add(ts);
  407. return this;
  408. }
  409. /**
  410. * Execute this batch update.
  411. * <p>
  412. * The default implementation of this method performs a sequential reference
  413. * update over each reference.
  414. * <p>
  415. * Implementations must respect the atomicity requirements of the underlying
  416. * database as described in {@link #setAtomic(boolean)} and
  417. * {@link org.eclipse.jgit.lib.RefDatabase#performsAtomicTransactions()}.
  418. *
  419. * @param walk
  420. * a RevWalk to parse tags in case the storage system wants to
  421. * store them pre-peeled, a common performance optimization.
  422. * @param monitor
  423. * progress monitor to receive update status on.
  424. * @param options
  425. * a list of option strings; set null to execute without
  426. * @throws java.io.IOException
  427. * the database is unable to accept the update. Individual
  428. * command status must be tested to determine if there is a
  429. * partial failure, or a total failure.
  430. * @since 4.5
  431. */
  432. public void execute(RevWalk walk, ProgressMonitor monitor,
  433. List<String> options) throws IOException {
  434. if (atomic && !refdb.performsAtomicTransactions()) {
  435. for (ReceiveCommand c : commands) {
  436. if (c.getResult() == NOT_ATTEMPTED) {
  437. c.setResult(REJECTED_OTHER_REASON,
  438. JGitText.get().atomicRefUpdatesNotSupported);
  439. }
  440. }
  441. return;
  442. }
  443. if (!blockUntilTimestamps(MAX_WAIT)) {
  444. return;
  445. }
  446. if (options != null) {
  447. setPushOptions(options);
  448. }
  449. monitor.beginTask(JGitText.get().updatingReferences, commands.size());
  450. List<ReceiveCommand> commands2 = new ArrayList<>(
  451. commands.size());
  452. // First delete refs. This may free the name space for some of the
  453. // updates.
  454. for (ReceiveCommand cmd : commands) {
  455. try {
  456. if (cmd.getResult() == NOT_ATTEMPTED) {
  457. if (isMissing(walk, cmd.getOldId())
  458. || isMissing(walk, cmd.getNewId())) {
  459. cmd.setResult(ReceiveCommand.Result.REJECTED_MISSING_OBJECT);
  460. continue;
  461. }
  462. cmd.updateType(walk);
  463. switch (cmd.getType()) {
  464. case CREATE:
  465. commands2.add(cmd);
  466. break;
  467. case UPDATE:
  468. case UPDATE_NONFASTFORWARD:
  469. commands2.add(cmd);
  470. break;
  471. case DELETE:
  472. RefUpdate rud = newUpdate(cmd);
  473. monitor.update(1);
  474. cmd.setResult(rud.delete(walk));
  475. }
  476. }
  477. } catch (IOException err) {
  478. cmd.setResult(
  479. REJECTED_OTHER_REASON,
  480. MessageFormat.format(JGitText.get().lockError,
  481. err.getMessage()));
  482. }
  483. }
  484. if (!commands2.isEmpty()) {
  485. // Perform updates that may require more room in the name space
  486. for (ReceiveCommand cmd : commands2) {
  487. try {
  488. if (cmd.getResult() == NOT_ATTEMPTED) {
  489. cmd.updateType(walk);
  490. RefUpdate ru = newUpdate(cmd);
  491. switch (cmd.getType()) {
  492. case DELETE:
  493. // Performed in the first phase
  494. break;
  495. case UPDATE:
  496. case UPDATE_NONFASTFORWARD:
  497. RefUpdate ruu = newUpdate(cmd);
  498. cmd.setResult(ruu.update(walk));
  499. break;
  500. case CREATE:
  501. cmd.setResult(ru.update(walk));
  502. break;
  503. }
  504. }
  505. } catch (IOException err) {
  506. cmd.setResult(REJECTED_OTHER_REASON, MessageFormat.format(
  507. JGitText.get().lockError, err.getMessage()));
  508. } finally {
  509. monitor.update(1);
  510. }
  511. }
  512. }
  513. monitor.endTask();
  514. }
  515. private static boolean isMissing(RevWalk walk, ObjectId id)
  516. throws IOException {
  517. if (id.equals(ObjectId.zeroId())) {
  518. return false; // Explicit add or delete is not missing.
  519. }
  520. try {
  521. walk.parseAny(id);
  522. return false;
  523. } catch (MissingObjectException e) {
  524. return true;
  525. }
  526. }
  527. /**
  528. * Wait for timestamps to be in the past, aborting commands on timeout.
  529. *
  530. * @param maxWait
  531. * maximum amount of time to wait for timestamps to resolve.
  532. * @return true if timestamps were successfully waited for; false if
  533. * commands were aborted.
  534. * @since 4.6
  535. */
  536. protected boolean blockUntilTimestamps(Duration maxWait) {
  537. if (timestamps == null) {
  538. return true;
  539. }
  540. try {
  541. ProposedTimestamp.blockUntil(timestamps, maxWait);
  542. return true;
  543. } catch (TimeoutException | InterruptedException e) {
  544. String msg = JGitText.get().timeIsUncertain;
  545. for (ReceiveCommand c : commands) {
  546. if (c.getResult() == NOT_ATTEMPTED) {
  547. c.setResult(REJECTED_OTHER_REASON, msg);
  548. }
  549. }
  550. return false;
  551. }
  552. }
  553. /**
  554. * Execute this batch update without option strings.
  555. *
  556. * @param walk
  557. * a RevWalk to parse tags in case the storage system wants to
  558. * store them pre-peeled, a common performance optimization.
  559. * @param monitor
  560. * progress monitor to receive update status on.
  561. * @throws java.io.IOException
  562. * the database is unable to accept the update. Individual
  563. * command status must be tested to determine if there is a
  564. * partial failure, or a total failure.
  565. */
  566. public void execute(RevWalk walk, ProgressMonitor monitor)
  567. throws IOException {
  568. execute(walk, monitor, null);
  569. }
  570. /**
  571. * Get all path prefixes of a ref name.
  572. *
  573. * @param name
  574. * ref name.
  575. * @return path prefixes of the ref name. For {@code refs/heads/foo}, returns
  576. * {@code refs} and {@code refs/heads}.
  577. * @since 4.9
  578. */
  579. protected static Collection<String> getPrefixes(String name) {
  580. Collection<String> ret = new HashSet<>();
  581. addPrefixesTo(name, ret);
  582. return ret;
  583. }
  584. /**
  585. * Add prefixes of a ref name to an existing collection.
  586. *
  587. * @param name
  588. * ref name.
  589. * @param out
  590. * path prefixes of the ref name. For {@code refs/heads/foo},
  591. * returns {@code refs} and {@code refs/heads}.
  592. * @since 4.9
  593. */
  594. protected static void addPrefixesTo(String name, Collection<String> out) {
  595. int p1 = name.indexOf('/');
  596. while (p1 > 0) {
  597. out.add(name.substring(0, p1));
  598. p1 = name.indexOf('/', p1 + 1);
  599. }
  600. }
  601. /**
  602. * Create a new RefUpdate copying the batch settings.
  603. *
  604. * @param cmd
  605. * specific command the update should be created to copy.
  606. * @return a single reference update command.
  607. * @throws java.io.IOException
  608. * the reference database cannot make a new update object for
  609. * the given reference.
  610. */
  611. protected RefUpdate newUpdate(ReceiveCommand cmd) throws IOException {
  612. RefUpdate ru = refdb.newUpdate(cmd.getRefName(), false);
  613. if (isRefLogDisabled(cmd)) {
  614. ru.disableRefLog();
  615. } else {
  616. ru.setRefLogIdent(refLogIdent);
  617. ru.setRefLogMessage(getRefLogMessage(cmd), isRefLogIncludingResult(cmd));
  618. ru.setForceRefLog(isForceRefLog(cmd));
  619. }
  620. ru.setPushCertificate(pushCert);
  621. switch (cmd.getType()) {
  622. case DELETE:
  623. if (!ObjectId.zeroId().equals(cmd.getOldId()))
  624. ru.setExpectedOldObjectId(cmd.getOldId());
  625. ru.setForceUpdate(true);
  626. return ru;
  627. case CREATE:
  628. case UPDATE:
  629. case UPDATE_NONFASTFORWARD:
  630. default:
  631. ru.setForceUpdate(isAllowNonFastForwards());
  632. ru.setExpectedOldObjectId(cmd.getOldId());
  633. ru.setNewObjectId(cmd.getNewId());
  634. return ru;
  635. }
  636. }
  637. /**
  638. * Check whether reflog is disabled for a command.
  639. *
  640. * @param cmd
  641. * specific command.
  642. * @return whether the reflog is disabled, taking into account the state from
  643. * this instance as well as overrides in the given command.
  644. * @since 4.9
  645. */
  646. protected boolean isRefLogDisabled(ReceiveCommand cmd) {
  647. return cmd.hasCustomRefLog() ? cmd.isRefLogDisabled() : isRefLogDisabled();
  648. }
  649. /**
  650. * Get reflog message for a command.
  651. *
  652. * @param cmd
  653. * specific command.
  654. * @return reflog message, taking into account the state from this instance as
  655. * well as overrides in the given command.
  656. * @since 4.9
  657. */
  658. protected String getRefLogMessage(ReceiveCommand cmd) {
  659. return cmd.hasCustomRefLog() ? cmd.getRefLogMessage() : getRefLogMessage();
  660. }
  661. /**
  662. * Check whether the reflog message for a command should include the result.
  663. *
  664. * @param cmd
  665. * specific command.
  666. * @return whether the reflog message should show the result, taking into
  667. * account the state from this instance as well as overrides in the
  668. * given command.
  669. * @since 4.9
  670. */
  671. protected boolean isRefLogIncludingResult(ReceiveCommand cmd) {
  672. return cmd.hasCustomRefLog()
  673. ? cmd.isRefLogIncludingResult() : isRefLogIncludingResult();
  674. }
  675. /**
  676. * Check whether the reflog for a command should be written regardless of repo
  677. * defaults.
  678. *
  679. * @param cmd
  680. * specific command.
  681. * @return whether force writing is enabled.
  682. * @since 4.9
  683. */
  684. protected boolean isForceRefLog(ReceiveCommand cmd) {
  685. Boolean isForceRefLog = cmd.isForceRefLog();
  686. return isForceRefLog != null ? isForceRefLog.booleanValue()
  687. : isForceRefLog();
  688. }
  689. /** {@inheritDoc} */
  690. @Override
  691. public String toString() {
  692. StringBuilder r = new StringBuilder();
  693. r.append(getClass().getSimpleName()).append('[');
  694. if (commands.isEmpty())
  695. return r.append(']').toString();
  696. r.append('\n');
  697. for (ReceiveCommand cmd : commands) {
  698. r.append(" "); //$NON-NLS-1$
  699. r.append(cmd);
  700. r.append(" (").append(cmd.getResult()); //$NON-NLS-1$
  701. if (cmd.getMessage() != null) {
  702. r.append(": ").append(cmd.getMessage()); //$NON-NLS-1$
  703. }
  704. r.append(")\n"); //$NON-NLS-1$
  705. }
  706. return r.append(']').toString();
  707. }
  708. }