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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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.util.ArrayList;
  50. import java.util.Arrays;
  51. import java.util.Collection;
  52. import java.util.Collections;
  53. import java.util.HashSet;
  54. import java.util.List;
  55. import org.eclipse.jgit.internal.JGitText;
  56. import org.eclipse.jgit.lib.RefUpdate.Result;
  57. import org.eclipse.jgit.revwalk.RevWalk;
  58. import org.eclipse.jgit.transport.ReceiveCommand;
  59. /**
  60. * Batch of reference updates to be applied to a repository.
  61. * <p>
  62. * The batch update is primarily useful in the transport code, where a client or
  63. * server is making changes to more than one reference at a time.
  64. */
  65. public class BatchRefUpdate {
  66. private final RefDatabase refdb;
  67. /** Commands to apply during this batch. */
  68. private final List<ReceiveCommand> commands;
  69. /** Does the caller permit a forced update on a reference? */
  70. private boolean allowNonFastForwards;
  71. /** Identity to record action as within the reflog. */
  72. private PersonIdent refLogIdent;
  73. /** Message the caller wants included in the reflog. */
  74. private String refLogMessage;
  75. /** Should the result value be appended to {@link #refLogMessage}. */
  76. private boolean refLogIncludeResult;
  77. /**
  78. * Initialize a new batch update.
  79. *
  80. * @param refdb
  81. * the reference database of the repository to be updated.
  82. */
  83. protected BatchRefUpdate(RefDatabase refdb) {
  84. this.refdb = refdb;
  85. this.commands = new ArrayList<ReceiveCommand>();
  86. }
  87. /**
  88. * @return true if the batch update will permit a non-fast-forward update to
  89. * an existing reference.
  90. */
  91. public boolean isAllowNonFastForwards() {
  92. return allowNonFastForwards;
  93. }
  94. /**
  95. * Set if this update wants to permit a forced update.
  96. *
  97. * @param allow
  98. * true if this update batch should ignore merge tests.
  99. * @return {@code this}.
  100. */
  101. public BatchRefUpdate setAllowNonFastForwards(boolean allow) {
  102. allowNonFastForwards = allow;
  103. return this;
  104. }
  105. /** @return identity of the user making the change in the reflog. */
  106. public PersonIdent getRefLogIdent() {
  107. return refLogIdent;
  108. }
  109. /**
  110. * Set the identity of the user appearing in the reflog.
  111. * <p>
  112. * The timestamp portion of the identity is ignored. A new identity with the
  113. * current timestamp will be created automatically when the update occurs
  114. * and the log record is written.
  115. *
  116. * @param pi
  117. * identity of the user. If null the identity will be
  118. * automatically determined based on the repository
  119. * configuration.
  120. * @return {@code this}.
  121. */
  122. public BatchRefUpdate setRefLogIdent(final PersonIdent pi) {
  123. refLogIdent = pi;
  124. return this;
  125. }
  126. /**
  127. * Get the message to include in the reflog.
  128. *
  129. * @return message the caller wants to include in the reflog; null if the
  130. * update should not be logged.
  131. */
  132. public String getRefLogMessage() {
  133. return refLogMessage;
  134. }
  135. /** @return {@code true} if the ref log message should show the result. */
  136. public boolean isRefLogIncludingResult() {
  137. return refLogIncludeResult;
  138. }
  139. /**
  140. * Set the message to include in the reflog.
  141. *
  142. * @param msg
  143. * the message to describe this change. It may be null if
  144. * appendStatus is null in order not to append to the reflog
  145. * @param appendStatus
  146. * true if the status of the ref change (fast-forward or
  147. * forced-update) should be appended to the user supplied
  148. * message.
  149. * @return {@code this}.
  150. */
  151. public BatchRefUpdate setRefLogMessage(String msg, boolean appendStatus) {
  152. if (msg == null && !appendStatus)
  153. disableRefLog();
  154. else if (msg == null && appendStatus) {
  155. refLogMessage = ""; //$NON-NLS-1$
  156. refLogIncludeResult = true;
  157. } else {
  158. refLogMessage = msg;
  159. refLogIncludeResult = appendStatus;
  160. }
  161. return this;
  162. }
  163. /**
  164. * Don't record this update in the ref's associated reflog.
  165. *
  166. * @return {@code this}.
  167. */
  168. public BatchRefUpdate disableRefLog() {
  169. refLogMessage = null;
  170. refLogIncludeResult = false;
  171. return this;
  172. }
  173. /** @return true if log has been disabled by {@link #disableRefLog()}. */
  174. public boolean isRefLogDisabled() {
  175. return refLogMessage == null;
  176. }
  177. /** @return commands this update will process. */
  178. public List<ReceiveCommand> getCommands() {
  179. return Collections.unmodifiableList(commands);
  180. }
  181. /**
  182. * Add a single command to this batch update.
  183. *
  184. * @param cmd
  185. * the command to add, must not be null.
  186. * @return {@code this}.
  187. */
  188. public BatchRefUpdate addCommand(ReceiveCommand cmd) {
  189. commands.add(cmd);
  190. return this;
  191. }
  192. /**
  193. * Add commands to this batch update.
  194. *
  195. * @param cmd
  196. * the commands to add, must not be null.
  197. * @return {@code this}.
  198. */
  199. public BatchRefUpdate addCommand(ReceiveCommand... cmd) {
  200. return addCommand(Arrays.asList(cmd));
  201. }
  202. /**
  203. * Add commands to this batch update.
  204. *
  205. * @param cmd
  206. * the commands to add, must not be null.
  207. * @return {@code this}.
  208. */
  209. public BatchRefUpdate addCommand(Collection<ReceiveCommand> cmd) {
  210. commands.addAll(cmd);
  211. return this;
  212. }
  213. /**
  214. * Execute this batch update.
  215. * <p>
  216. * The default implementation of this method performs a sequential reference
  217. * update over each reference.
  218. *
  219. * @param walk
  220. * a RevWalk to parse tags in case the storage system wants to
  221. * store them pre-peeled, a common performance optimization.
  222. * @param monitor
  223. * progress monitor to receive update status on.
  224. * @throws IOException
  225. * the database is unable to accept the update. Individual
  226. * command status must be tested to determine if there is a
  227. * partial failure, or a total failure.
  228. */
  229. public void execute(RevWalk walk, ProgressMonitor monitor)
  230. throws IOException {
  231. monitor.beginTask(JGitText.get().updatingReferences, commands.size());
  232. List<ReceiveCommand> commands2 = new ArrayList<ReceiveCommand>(
  233. commands.size());
  234. List<String> namesToCheck = new ArrayList<String>(commands.size());
  235. // First delete refs. This may free the name space for some of the
  236. // updates.
  237. for (ReceiveCommand cmd : commands) {
  238. try {
  239. if (cmd.getResult() == NOT_ATTEMPTED) {
  240. cmd.updateType(walk);
  241. switch (cmd.getType()) {
  242. case CREATE:
  243. namesToCheck.add(cmd.getRefName());
  244. commands2.add(cmd);
  245. break;
  246. case UPDATE:
  247. case UPDATE_NONFASTFORWARD:
  248. commands2.add(cmd);
  249. break;
  250. case DELETE:
  251. RefUpdate rud = newUpdate(cmd);
  252. monitor.update(1);
  253. cmd.setResult(rud.delete(walk));
  254. }
  255. }
  256. } catch (IOException err) {
  257. cmd.setResult(
  258. REJECTED_OTHER_REASON,
  259. MessageFormat.format(JGitText.get().lockError,
  260. err.getMessage()));
  261. }
  262. }
  263. if (!commands2.isEmpty()) {
  264. // What part of the name space is already taken
  265. Collection<String> takenNames = new HashSet<String>(refdb.getRefs(
  266. RefDatabase.ALL).keySet());
  267. Collection<String> takenPrefixes = getTakenPrefixes(takenNames);
  268. // Now to the update that may require more room in the name space
  269. for (ReceiveCommand cmd : commands2) {
  270. try {
  271. monitor.update(1);
  272. if (cmd.getResult() == NOT_ATTEMPTED) {
  273. cmd.updateType(walk);
  274. RefUpdate ru = newUpdate(cmd);
  275. SWITCH: switch (cmd.getType()) {
  276. case DELETE:
  277. // Performed in the first phase
  278. break;
  279. case UPDATE:
  280. case UPDATE_NONFASTFORWARD:
  281. monitor.update(1);
  282. RefUpdate ruu = newUpdate(cmd);
  283. cmd.setResult(ruu.update(walk));
  284. break;
  285. case CREATE:
  286. for (String prefix : getPrefixes(cmd.getRefName())) {
  287. if (takenNames.contains(prefix)) {
  288. cmd.setResult(Result.LOCK_FAILURE);
  289. break SWITCH;
  290. }
  291. }
  292. if (takenPrefixes.contains(cmd.getRefName())) {
  293. cmd.setResult(Result.LOCK_FAILURE);
  294. break SWITCH;
  295. }
  296. ru.setCheckConflicting(false);
  297. addRefToPrefixes(takenPrefixes, cmd.getRefName());
  298. takenNames.add(cmd.getRefName());
  299. cmd.setResult(ru.update(walk));
  300. }
  301. }
  302. } catch (IOException err) {
  303. cmd.setResult(REJECTED_OTHER_REASON, MessageFormat.format(
  304. JGitText.get().lockError, err.getMessage()));
  305. }
  306. }
  307. }
  308. monitor.endTask();
  309. }
  310. private static Collection<String> getTakenPrefixes(
  311. final Collection<String> names) {
  312. Collection<String> ref = new HashSet<String>();
  313. for (String name : names)
  314. ref.addAll(getPrefixes(name));
  315. return ref;
  316. }
  317. private static void addRefToPrefixes(Collection<String> prefixes,
  318. String name) {
  319. for (String prefix : getPrefixes(name)) {
  320. prefixes.add(prefix);
  321. }
  322. }
  323. static Collection<String> getPrefixes(String s) {
  324. Collection<String> ret = new HashSet<String>();
  325. int p1 = s.indexOf('/');
  326. while (p1 > 0) {
  327. ret.add(s.substring(0, p1));
  328. p1 = s.indexOf('/', p1 + 1);
  329. }
  330. return ret;
  331. }
  332. /**
  333. * Create a new RefUpdate copying the batch settings.
  334. *
  335. * @param cmd
  336. * specific command the update should be created to copy.
  337. * @return a single reference update command.
  338. * @throws IOException
  339. * the reference database cannot make a new update object for
  340. * the given reference.
  341. */
  342. protected RefUpdate newUpdate(ReceiveCommand cmd) throws IOException {
  343. RefUpdate ru = refdb.newUpdate(cmd.getRefName(), false);
  344. if (isRefLogDisabled())
  345. ru.disableRefLog();
  346. else {
  347. ru.setRefLogIdent(refLogIdent);
  348. ru.setRefLogMessage(refLogMessage, refLogIncludeResult);
  349. }
  350. switch (cmd.getType()) {
  351. case DELETE:
  352. if (!ObjectId.zeroId().equals(cmd.getOldId()))
  353. ru.setExpectedOldObjectId(cmd.getOldId());
  354. ru.setForceUpdate(true);
  355. return ru;
  356. case CREATE:
  357. case UPDATE:
  358. case UPDATE_NONFASTFORWARD:
  359. default:
  360. ru.setForceUpdate(isAllowNonFastForwards());
  361. ru.setExpectedOldObjectId(cmd.getOldId());
  362. ru.setNewObjectId(cmd.getNewId());
  363. return ru;
  364. }
  365. }
  366. }