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.

PushProcess.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  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.io.OutputStream;
  46. import java.text.MessageFormat;
  47. import java.util.Collection;
  48. import java.util.Collections;
  49. import java.util.HashMap;
  50. import java.util.Map;
  51. import org.eclipse.jgit.errors.MissingObjectException;
  52. import org.eclipse.jgit.errors.NotSupportedException;
  53. import org.eclipse.jgit.errors.TransportException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.ProgressMonitor;
  57. import org.eclipse.jgit.lib.Ref;
  58. import org.eclipse.jgit.revwalk.RevCommit;
  59. import org.eclipse.jgit.revwalk.RevObject;
  60. import org.eclipse.jgit.revwalk.RevWalk;
  61. import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
  62. /**
  63. * Class performing push operation on remote repository.
  64. *
  65. * @see Transport#push(ProgressMonitor, Collection, OutputStream)
  66. */
  67. class PushProcess {
  68. /** Task name for {@link ProgressMonitor} used during opening connection. */
  69. static final String PROGRESS_OPENING_CONNECTION = JGitText.get().openingConnection;
  70. /** Transport used to perform this operation. */
  71. private final Transport transport;
  72. /** Push operation connection created to perform this operation */
  73. private PushConnection connection;
  74. /** Refs to update on remote side. */
  75. private final Map<String, RemoteRefUpdate> toPush;
  76. /** Revision walker for checking some updates properties. */
  77. private final RevWalk walker;
  78. /** an outputstream to write messages to */
  79. private final OutputStream out;
  80. /**
  81. * Create process for specified transport and refs updates specification.
  82. *
  83. * @param transport
  84. * transport between remote and local repository, used to create
  85. * connection.
  86. * @param toPush
  87. * specification of refs updates (and local tracking branches).
  88. *
  89. * @throws TransportException
  90. */
  91. PushProcess(final Transport transport,
  92. final Collection<RemoteRefUpdate> toPush) throws TransportException {
  93. this(transport, toPush, null);
  94. }
  95. /**
  96. * Create process for specified transport and refs updates specification.
  97. *
  98. * @param transport
  99. * transport between remote and local repository, used to create
  100. * connection.
  101. * @param toPush
  102. * specification of refs updates (and local tracking branches).
  103. * @param out
  104. * OutputStream to write messages to
  105. * @throws TransportException
  106. */
  107. PushProcess(final Transport transport,
  108. final Collection<RemoteRefUpdate> toPush, OutputStream out)
  109. throws TransportException {
  110. this.walker = new RevWalk(transport.local);
  111. this.transport = transport;
  112. this.toPush = new HashMap<String, RemoteRefUpdate>();
  113. this.out = out;
  114. for (final RemoteRefUpdate rru : toPush) {
  115. if (this.toPush.put(rru.getRemoteName(), rru) != null)
  116. throw new TransportException(MessageFormat.format(
  117. JGitText.get().duplicateRemoteRefUpdateIsIllegal, rru.getRemoteName()));
  118. }
  119. }
  120. /**
  121. * Perform push operation between local and remote repository - set remote
  122. * refs appropriately, send needed objects and update local tracking refs.
  123. * <p>
  124. * When {@link Transport#isDryRun()} is true, result of this operation is
  125. * just estimation of real operation result, no real action is performed.
  126. *
  127. * @param monitor
  128. * progress monitor used for feedback about operation.
  129. * @return result of push operation with complete status description.
  130. * @throws NotSupportedException
  131. * when push operation is not supported by provided transport.
  132. * @throws TransportException
  133. * when some error occurred during operation, like I/O, protocol
  134. * error, or local database consistency error.
  135. */
  136. PushResult execute(final ProgressMonitor monitor)
  137. throws NotSupportedException, TransportException {
  138. try {
  139. monitor.beginTask(PROGRESS_OPENING_CONNECTION,
  140. ProgressMonitor.UNKNOWN);
  141. final PushResult res = new PushResult();
  142. connection = transport.openPush();
  143. try {
  144. res.setAdvertisedRefs(transport.getURI(), connection
  145. .getRefsMap());
  146. res.peerUserAgent = connection.getPeerUserAgent();
  147. res.setRemoteUpdates(toPush);
  148. monitor.endTask();
  149. final Map<String, RemoteRefUpdate> preprocessed = prepareRemoteUpdates();
  150. if (transport.isDryRun())
  151. modifyUpdatesForDryRun();
  152. else if (!preprocessed.isEmpty())
  153. connection.push(monitor, preprocessed, out);
  154. } finally {
  155. connection.close();
  156. res.addMessages(connection.getMessages());
  157. }
  158. if (!transport.isDryRun())
  159. updateTrackingRefs();
  160. for (final RemoteRefUpdate rru : toPush.values()) {
  161. final TrackingRefUpdate tru = rru.getTrackingRefUpdate();
  162. if (tru != null)
  163. res.add(tru);
  164. }
  165. return res;
  166. } finally {
  167. walker.close();
  168. }
  169. }
  170. private Map<String, RemoteRefUpdate> prepareRemoteUpdates()
  171. throws TransportException {
  172. boolean atomic = transport.isPushAtomic();
  173. final Map<String, RemoteRefUpdate> result = new HashMap<String, RemoteRefUpdate>();
  174. for (final RemoteRefUpdate rru : toPush.values()) {
  175. final Ref advertisedRef = connection.getRef(rru.getRemoteName());
  176. final ObjectId advertisedOld = (advertisedRef == null ? ObjectId
  177. .zeroId() : advertisedRef.getObjectId());
  178. if (rru.getNewObjectId().equals(advertisedOld)) {
  179. if (rru.isDelete()) {
  180. // ref does exist neither locally nor remotely
  181. rru.setStatus(Status.NON_EXISTING);
  182. } else {
  183. // same object - nothing to do
  184. rru.setStatus(Status.UP_TO_DATE);
  185. }
  186. continue;
  187. }
  188. // caller has explicitly specified expected old object id, while it
  189. // has been changed in the mean time - reject
  190. if (rru.isExpectingOldObjectId()
  191. && !rru.getExpectedOldObjectId().equals(advertisedOld)) {
  192. rru.setStatus(Status.REJECTED_REMOTE_CHANGED);
  193. if (atomic) {
  194. return rejectAll();
  195. }
  196. continue;
  197. }
  198. if (!rru.isExpectingOldObjectId()) {
  199. rru.setExpectedOldObjectId(advertisedOld);
  200. }
  201. // create ref (hasn't existed on remote side) and delete ref
  202. // are always fast-forward commands, feasible at this level
  203. if (advertisedOld.equals(ObjectId.zeroId()) || rru.isDelete()) {
  204. rru.setFastForward(true);
  205. result.put(rru.getRemoteName(), rru);
  206. continue;
  207. }
  208. // check for fast-forward:
  209. // - both old and new ref must point to commits, AND
  210. // - both of them must be known for us, exist in repository, AND
  211. // - old commit must be ancestor of new commit
  212. boolean fastForward = true;
  213. try {
  214. RevObject oldRev = walker.parseAny(advertisedOld);
  215. final RevObject newRev = walker.parseAny(rru.getNewObjectId());
  216. if (!(oldRev instanceof RevCommit)
  217. || !(newRev instanceof RevCommit)
  218. || !walker.isMergedInto((RevCommit) oldRev,
  219. (RevCommit) newRev))
  220. fastForward = false;
  221. } catch (MissingObjectException x) {
  222. fastForward = false;
  223. } catch (Exception x) {
  224. throw new TransportException(transport.getURI(), MessageFormat.format(
  225. JGitText.get().readingObjectsFromLocalRepositoryFailed, x.getMessage()), x);
  226. }
  227. rru.setFastForward(fastForward);
  228. if (!fastForward && !rru.isForceUpdate()) {
  229. rru.setStatus(Status.REJECTED_NONFASTFORWARD);
  230. if (atomic) {
  231. return rejectAll();
  232. }
  233. } else {
  234. result.put(rru.getRemoteName(), rru);
  235. }
  236. }
  237. return result;
  238. }
  239. private Map<String, RemoteRefUpdate> rejectAll() {
  240. for (RemoteRefUpdate rru : toPush.values()) {
  241. if (rru.getStatus() == Status.NOT_ATTEMPTED) {
  242. rru.setStatus(RemoteRefUpdate.Status.REJECTED_OTHER_REASON);
  243. rru.setMessage(JGitText.get().transactionAborted);
  244. }
  245. }
  246. return Collections.emptyMap();
  247. }
  248. private void modifyUpdatesForDryRun() {
  249. for (final RemoteRefUpdate rru : toPush.values())
  250. if (rru.getStatus() == Status.NOT_ATTEMPTED)
  251. rru.setStatus(Status.OK);
  252. }
  253. private void updateTrackingRefs() {
  254. for (final RemoteRefUpdate rru : toPush.values()) {
  255. final Status status = rru.getStatus();
  256. if (rru.hasTrackingRefUpdate()
  257. && (status == Status.UP_TO_DATE || status == Status.OK)) {
  258. // update local tracking branch only when there is a chance that
  259. // it has changed; this is possible for:
  260. // -updated (OK) status,
  261. // -up to date (UP_TO_DATE) status
  262. try {
  263. rru.updateTrackingRef(walker);
  264. } catch (IOException e) {
  265. // ignore as RefUpdate has stored I/O error status
  266. }
  267. }
  268. }
  269. }
  270. }