2 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
3 * and other copyright owners as documented in the project's IP log.
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
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
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.
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
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.
44 package org
.eclipse
.jgit
.transport
;
46 import java
.io
.IOException
;
47 import java
.util
.Collection
;
48 import java
.util
.HashMap
;
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
.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
;
63 * Class performing push operation on remote repository.
65 * @see Transport#push(ProgressMonitor, Collection)
68 /** Task name for {@link ProgressMonitor} used during opening connection. */
69 static final String PROGRESS_OPENING_CONNECTION
= "Opening connection";
71 /** Transport used to perform this operation. */
72 private final Transport transport
;
74 /** Push operation connection created to perform this operation */
75 private PushConnection connection
;
77 /** Refs to update on remote side. */
78 private final Map
<String
, RemoteRefUpdate
> toPush
;
80 /** Revision walker for checking some updates properties. */
81 private final RevWalk walker
;
84 * Create process for specified transport and refs updates specification.
87 * transport between remote and local repository, used to create
90 * specification of refs updates (and local tracking branches).
91 * @throws TransportException
93 PushProcess(final Transport transport
,
94 final Collection
<RemoteRefUpdate
> toPush
) throws TransportException
{
95 this.walker
= new RevWalk(transport
.local
);
96 this.transport
= transport
;
97 this.toPush
= new HashMap
<String
, RemoteRefUpdate
>();
98 for (final RemoteRefUpdate rru
: toPush
) {
99 if (this.toPush
.put(rru
.getRemoteName(), rru
) != null)
100 throw new TransportException(
101 "Duplicate remote ref update is illegal. Affected remote name: "
102 + rru
.getRemoteName());
107 * Perform push operation between local and remote repository - set remote
108 * refs appropriately, send needed objects and update local tracking refs.
110 * When {@link Transport#isDryRun()} is true, result of this operation is
111 * just estimation of real operation result, no real action is performed.
114 * progress monitor used for feedback about operation.
115 * @return result of push operation with complete status description.
116 * @throws NotSupportedException
117 * when push operation is not supported by provided transport.
118 * @throws TransportException
119 * when some error occurred during operation, like I/O, protocol
120 * error, or local database consistency error.
122 PushResult
execute(final ProgressMonitor monitor
)
123 throws NotSupportedException
, TransportException
{
124 monitor
.beginTask(PROGRESS_OPENING_CONNECTION
, ProgressMonitor
.UNKNOWN
);
126 final PushResult res
= new PushResult();
127 connection
= transport
.openPush();
129 res
.setAdvertisedRefs(transport
.getURI(), connection
.getRefsMap());
130 res
.setRemoteUpdates(toPush
);
133 final Map
<String
, RemoteRefUpdate
> preprocessed
= prepareRemoteUpdates();
134 if (transport
.isDryRun())
135 modifyUpdatesForDryRun();
136 else if (!preprocessed
.isEmpty())
137 connection
.push(monitor
, preprocessed
);
140 res
.addMessages(connection
.getMessages());
142 if (!transport
.isDryRun())
143 updateTrackingRefs();
144 for (final RemoteRefUpdate rru
: toPush
.values()) {
145 final TrackingRefUpdate tru
= rru
.getTrackingRefUpdate();
152 private Map
<String
, RemoteRefUpdate
> prepareRemoteUpdates()
153 throws TransportException
{
154 final Map
<String
, RemoteRefUpdate
> result
= new HashMap
<String
, RemoteRefUpdate
>();
155 for (final RemoteRefUpdate rru
: toPush
.values()) {
156 final Ref advertisedRef
= connection
.getRef(rru
.getRemoteName());
157 final ObjectId advertisedOld
= (advertisedRef
== null ? ObjectId
158 .zeroId() : advertisedRef
.getObjectId());
160 if (rru
.getNewObjectId().equals(advertisedOld
)) {
161 if (rru
.isDelete()) {
162 // ref does exist neither locally nor remotely
163 rru
.setStatus(Status
.NON_EXISTING
);
165 // same object - nothing to do
166 rru
.setStatus(Status
.UP_TO_DATE
);
171 // caller has explicitly specified expected old object id, while it
172 // has been changed in the mean time - reject
173 if (rru
.isExpectingOldObjectId()
174 && !rru
.getExpectedOldObjectId().equals(advertisedOld
)) {
175 rru
.setStatus(Status
.REJECTED_REMOTE_CHANGED
);
179 // create ref (hasn't existed on remote side) and delete ref
180 // are always fast-forward commands, feasible at this level
181 if (advertisedOld
.equals(ObjectId
.zeroId()) || rru
.isDelete()) {
182 rru
.setFastForward(true);
183 result
.put(rru
.getRemoteName(), rru
);
187 // check for fast-forward:
188 // - both old and new ref must point to commits, AND
189 // - both of them must be known for us, exist in repository, AND
190 // - old commit must be ancestor of new commit
191 boolean fastForward
= true;
193 RevObject oldRev
= walker
.parseAny(advertisedOld
);
194 final RevObject newRev
= walker
.parseAny(rru
.getNewObjectId());
195 if (!(oldRev
instanceof RevCommit
)
196 || !(newRev
instanceof RevCommit
)
197 || !walker
.isMergedInto((RevCommit
) oldRev
,
200 } catch (MissingObjectException x
) {
202 } catch (Exception x
) {
203 throw new TransportException(transport
.getURI(),
204 "reading objects from local repository failed: "
205 + x
.getMessage(), x
);
207 rru
.setFastForward(fastForward
);
208 if (!fastForward
&& !rru
.isForceUpdate())
209 rru
.setStatus(Status
.REJECTED_NONFASTFORWARD
);
211 result
.put(rru
.getRemoteName(), rru
);
216 private void modifyUpdatesForDryRun() {
217 for (final RemoteRefUpdate rru
: toPush
.values())
218 if (rru
.getStatus() == Status
.NOT_ATTEMPTED
)
219 rru
.setStatus(Status
.OK
);
222 private void updateTrackingRefs() {
223 for (final RemoteRefUpdate rru
: toPush
.values()) {
224 final Status status
= rru
.getStatus();
225 if (rru
.hasTrackingRefUpdate()
226 && (status
== Status
.UP_TO_DATE
|| status
== Status
.OK
)) {
227 // update local tracking branch only when there is a chance that
228 // it has changed; this is possible for:
229 // -updated (OK) status,
230 // -up to date (UP_TO_DATE) status
232 rru
.updateTrackingRef(walker
);
233 } catch (IOException e
) {
234 // ignore as RefUpdate has stored I/O error status