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

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