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

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