Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PushProcess.java 8.8KB

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