View Javadoc
1   /*
2    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.transport;
46  
47  import static java.nio.charset.StandardCharsets.UTF_8;
48  import static org.eclipse.jgit.transport.ReceiveCommand.Result.NOT_ATTEMPTED;
49  import static org.eclipse.jgit.transport.ReceiveCommand.Result.OK;
50  import static org.eclipse.jgit.transport.ReceiveCommand.Result.REJECTED_NONFASTFORWARD;
51  import static org.eclipse.jgit.transport.ReceiveCommand.Type.UPDATE_NONFASTFORWARD;
52  
53  import java.io.File;
54  import java.io.IOException;
55  import java.io.OutputStreamWriter;
56  import java.io.Writer;
57  import java.text.MessageFormat;
58  import java.util.ArrayList;
59  import java.util.Collection;
60  import java.util.Collections;
61  import java.util.HashMap;
62  import java.util.HashSet;
63  import java.util.Iterator;
64  import java.util.Map;
65  import java.util.Set;
66  import java.util.concurrent.TimeUnit;
67  
68  import org.eclipse.jgit.errors.MissingObjectException;
69  import org.eclipse.jgit.errors.NotSupportedException;
70  import org.eclipse.jgit.errors.TransportException;
71  import org.eclipse.jgit.internal.JGitText;
72  import org.eclipse.jgit.internal.storage.file.LockFile;
73  import org.eclipse.jgit.internal.storage.file.PackLock;
74  import org.eclipse.jgit.lib.BatchRefUpdate;
75  import org.eclipse.jgit.lib.BatchingProgressMonitor;
76  import org.eclipse.jgit.lib.Constants;
77  import org.eclipse.jgit.lib.ObjectId;
78  import org.eclipse.jgit.lib.ObjectIdRef;
79  import org.eclipse.jgit.lib.ProgressMonitor;
80  import org.eclipse.jgit.lib.Ref;
81  import org.eclipse.jgit.lib.RefDatabase;
82  import org.eclipse.jgit.revwalk.ObjectWalk;
83  import org.eclipse.jgit.revwalk.RevWalk;
84  
85  class FetchProcess {
86  	/** Transport we will fetch over. */
87  	private final Transport transport;
88  
89  	/** List of things we want to fetch from the remote repository. */
90  	private final Collection<RefSpec> toFetch;
91  
92  	/** Set of refs we will actually wind up asking to obtain. */
93  	private final HashMap<ObjectId, Ref> askFor = new HashMap<>();
94  
95  	/** Objects we know we have locally. */
96  	private final HashSet<ObjectId> have = new HashSet<>();
97  
98  	/** Updates to local tracking branches (if any). */
99  	private final ArrayList<TrackingRefUpdate> localUpdates = new ArrayList<>();
100 
101 	/** Records to be recorded into FETCH_HEAD. */
102 	private final ArrayList<FetchHeadRecord> fetchHeadUpdates = new ArrayList<>();
103 
104 	private final ArrayList<PackLock> packLocks = new ArrayList<>();
105 
106 	private FetchConnection conn;
107 
108 	private Map<String, Ref> localRefs;
109 
110 	FetchProcess(Transport t, Collection<RefSpec> f) {
111 		transport = t;
112 		toFetch = f;
113 	}
114 
115 	void execute(ProgressMonitor monitor, FetchResult result)
116 			throws NotSupportedException, TransportException {
117 		askFor.clear();
118 		localUpdates.clear();
119 		fetchHeadUpdates.clear();
120 		packLocks.clear();
121 		localRefs = null;
122 
123 		try {
124 			executeImp(monitor, result);
125 		} finally {
126 			try {
127 			for (PackLock lock : packLocks)
128 				lock.unlock();
129 			} catch (IOException e) {
130 				throw new TransportException(e.getMessage(), e);
131 			}
132 		}
133 	}
134 
135 	private void executeImp(final ProgressMonitor monitor,
136 			final FetchResult result) throws NotSupportedException,
137 			TransportException {
138 		conn = transport.openFetch();
139 		try {
140 			result.setAdvertisedRefs(transport.getURI(), conn.getRefsMap());
141 			result.peerUserAgent = conn.getPeerUserAgent();
142 			final Set<Ref> matched = new HashSet<>();
143 			for (RefSpec spec : toFetch) {
144 				if (spec.getSource() == null)
145 					throw new TransportException(MessageFormat.format(
146 							JGitText.get().sourceRefNotSpecifiedForRefspec, spec));
147 
148 				if (spec.isWildcard())
149 					expandWildcard(spec, matched);
150 				else
151 					expandSingle(spec, matched);
152 			}
153 
154 			Collection<Ref> additionalTags = Collections.<Ref> emptyList();
155 			final TagOpt tagopt = transport.getTagOpt();
156 			if (tagopt == TagOpt.AUTO_FOLLOW)
157 				additionalTags = expandAutoFollowTags();
158 			else if (tagopt == TagOpt.FETCH_TAGS)
159 				expandFetchTags();
160 
161 			final boolean includedTags;
162 			if (!askFor.isEmpty() && !askForIsComplete()) {
163 				fetchObjects(monitor);
164 				includedTags = conn.didFetchIncludeTags();
165 
166 				// Connection was used for object transfer. If we
167 				// do another fetch we must open a new connection.
168 				//
169 				closeConnection(result);
170 			} else {
171 				includedTags = false;
172 			}
173 
174 			if (tagopt == TagOpt.AUTO_FOLLOW && !additionalTags.isEmpty()) {
175 				// There are more tags that we want to follow, but
176 				// not all were asked for on the initial request.
177 				//
178 				have.addAll(askFor.keySet());
179 				askFor.clear();
180 				for (Ref r : additionalTags) {
181 					ObjectId id = r.getPeeledObjectId();
182 					if (id == null)
183 						id = r.getObjectId();
184 					if (localHasObject(id))
185 						wantTag(r);
186 				}
187 
188 				if (!askFor.isEmpty() && (!includedTags || !askForIsComplete())) {
189 					reopenConnection();
190 					if (!askFor.isEmpty())
191 						fetchObjects(monitor);
192 				}
193 			}
194 		} finally {
195 			closeConnection(result);
196 		}
197 
198 		BatchRefUpdate batch = transport.local.getRefDatabase()
199 				.newBatchUpdate()
200 				.setAllowNonFastForwards(true)
201 				.setRefLogMessage("fetch", true); //$NON-NLS-1$
202 		try (RevWalkvWalk.html#RevWalk">RevWalk walk = new RevWalk(transport.local)) {
203 			if (monitor instanceof BatchingProgressMonitor) {
204 				((BatchingProgressMonitor) monitor).setDelayStart(
205 						250, TimeUnit.MILLISECONDS);
206 			}
207 			if (transport.isRemoveDeletedRefs()) {
208 				deleteStaleTrackingRefs(result, batch);
209 			}
210 			addUpdateBatchCommands(result, batch);
211 			for (ReceiveCommand cmd : batch.getCommands()) {
212 				cmd.updateType(walk);
213 				if (cmd.getType() == UPDATE_NONFASTFORWARD
214 						&& cmd instanceof TrackingRefUpdate.Command
215 						&& !((TrackingRefUpdate.Command) cmd).canForceUpdate())
216 					cmd.setResult(REJECTED_NONFASTFORWARD);
217 			}
218 			if (transport.isDryRun()) {
219 				for (ReceiveCommand cmd : batch.getCommands()) {
220 					if (cmd.getResult() == NOT_ATTEMPTED)
221 						cmd.setResult(OK);
222 				}
223 			} else {
224 				batch.execute(walk, monitor);
225 			}
226 		} catch (TransportException e) {
227 			throw e;
228 		} catch (IOException err) {
229 			throw new TransportException(MessageFormat.format(
230 					JGitText.get().failureUpdatingTrackingRef,
231 					getFirstFailedRefName(batch), err.getMessage()), err);
232 		}
233 
234 		if (!fetchHeadUpdates.isEmpty()) {
235 			try {
236 				updateFETCH_HEAD(result);
237 			} catch (IOException err) {
238 				throw new TransportException(MessageFormat.format(
239 						JGitText.get().failureUpdatingFETCH_HEAD, err.getMessage()), err);
240 			}
241 		}
242 	}
243 
244 	private void addUpdateBatchCommands(FetchResult result,
245 			BatchRefUpdate batch) throws TransportException {
246 		Map<String, ObjectId> refs = new HashMap<>();
247 		for (TrackingRefUpdate u : localUpdates) {
248 			// Try to skip duplicates if they'd update to the same object ID
249 			ObjectId existing = refs.get(u.getLocalName());
250 			if (existing == null) {
251 				refs.put(u.getLocalName(), u.getNewObjectId());
252 				result.add(u);
253 				batch.addCommand(u.asReceiveCommand());
254 			} else if (!existing.equals(u.getNewObjectId())) {
255 				throw new TransportException(MessageFormat
256 						.format(JGitText.get().duplicateRef, u.getLocalName()));
257 			}
258 		}
259 	}
260 
261 	private void fetchObjects(ProgressMonitor monitor)
262 			throws TransportException {
263 		try {
264 			conn.setPackLockMessage("jgit fetch " + transport.uri); //$NON-NLS-1$
265 			conn.fetch(monitor, askFor.values(), have);
266 		} finally {
267 			packLocks.addAll(conn.getPackLocks());
268 		}
269 		if (transport.isCheckFetchedObjects()
270 				&& !conn.didFetchTestConnectivity() && !askForIsComplete())
271 			throw new TransportException(transport.getURI(),
272 					JGitText.get().peerDidNotSupplyACompleteObjectGraph);
273 	}
274 
275 	private void closeConnection(FetchResult result) {
276 		if (conn != null) {
277 			conn.close();
278 			result.addMessages(conn.getMessages());
279 			conn = null;
280 		}
281 	}
282 
283 	private void reopenConnection() throws NotSupportedException,
284 			TransportException {
285 		if (conn != null)
286 			return;
287 
288 		conn = transport.openFetch();
289 
290 		// Since we opened a new connection we cannot be certain
291 		// that the system we connected to has the same exact set
292 		// of objects available (think round-robin DNS and mirrors
293 		// that aren't updated at the same time).
294 		//
295 		// We rebuild our askFor list using only the refs that the
296 		// new connection has offered to us.
297 		//
298 		final HashMap<ObjectId, Ref> avail = new HashMap<>();
299 		for (Ref r : conn.getRefs())
300 			avail.put(r.getObjectId(), r);
301 
302 		final Collection<Ref> wants = new ArrayList<>(askFor.values());
303 		askFor.clear();
304 		for (Ref want : wants) {
305 			final Ref newRef = avail.get(want.getObjectId());
306 			if (newRef != null) {
307 				askFor.put(newRef.getObjectId(), newRef);
308 			} else {
309 				removeFetchHeadRecord(want.getObjectId());
310 				removeTrackingRefUpdate(want.getObjectId());
311 			}
312 		}
313 	}
314 
315 	private void removeTrackingRefUpdate(ObjectId want) {
316 		final Iterator<TrackingRefUpdate> i = localUpdates.iterator();
317 		while (i.hasNext()) {
318 			final TrackingRefUpdate u = i.next();
319 			if (u.getNewObjectId().equals(want))
320 				i.remove();
321 		}
322 	}
323 
324 	private void removeFetchHeadRecord(ObjectId want) {
325 		final Iterator<FetchHeadRecord> i = fetchHeadUpdates.iterator();
326 		while (i.hasNext()) {
327 			final FetchHeadRecord fh = i.next();
328 			if (fh.newValue.equals(want))
329 				i.remove();
330 		}
331 	}
332 
333 	private void updateFETCH_HEAD(FetchResult result) throws IOException {
334 		File meta = transport.local.getDirectory();
335 		if (meta == null)
336 			return;
337 		final LockFileorage/file/LockFile.html#LockFile">LockFile lock = new LockFile(new File(meta, "FETCH_HEAD")); //$NON-NLS-1$
338 		try {
339 			if (lock.lock()) {
340 				try (Writer w = new OutputStreamWriter(
341 						lock.getOutputStream(), UTF_8)) {
342 					for (FetchHeadRecord h : fetchHeadUpdates) {
343 						h.write(w);
344 						result.add(h);
345 					}
346 				}
347 				lock.commit();
348 			}
349 		} finally {
350 			lock.unlock();
351 		}
352 	}
353 
354 	private boolean askForIsComplete() throws TransportException {
355 		try {
356 			try (ObjectWalkectWalk.html#ObjectWalk">ObjectWalk ow = new ObjectWalk(transport.local)) {
357 				for (ObjectId want : askFor.keySet())
358 					ow.markStart(ow.parseAny(want));
359 				for (Ref ref : localRefs().values())
360 					ow.markUninteresting(ow.parseAny(ref.getObjectId()));
361 				ow.checkConnectivity();
362 			}
363 			return true;
364 		} catch (MissingObjectException e) {
365 			return false;
366 		} catch (IOException e) {
367 			throw new TransportException(JGitText.get().unableToCheckConnectivity, e);
368 		}
369 	}
370 
371 	private void expandWildcard(RefSpec spec, Set<Ref> matched)
372 			throws TransportException {
373 		for (Ref src : conn.getRefs()) {
374 			if (spec.matchSource(src) && matched.add(src))
375 				want(src, spec.expandFromSource(src));
376 		}
377 	}
378 
379 	private void expandSingle(RefSpec spec, Set<Ref> matched)
380 			throws TransportException {
381 		String want = spec.getSource();
382 		if (ObjectId.isId(want)) {
383 			want(ObjectId.fromString(want));
384 			return;
385 		}
386 
387 		Ref src = conn.getRef(want);
388 		if (src == null) {
389 			throw new TransportException(MessageFormat.format(JGitText.get().remoteDoesNotHaveSpec, want));
390 		}
391 		if (matched.add(src)) {
392 			want(src, spec);
393 		}
394 	}
395 
396 	private boolean localHasObject(ObjectId id) throws TransportException {
397 		try {
398 			return transport.local.getObjectDatabase().has(id);
399 		} catch (IOException err) {
400 			throw new TransportException(
401 					MessageFormat.format(
402 							JGitText.get().readingObjectsFromLocalRepositoryFailed,
403 							err.getMessage()),
404 					err);
405 		}
406 	}
407 
408 	private Collection<Ref> expandAutoFollowTags() throws TransportException {
409 		final Collection<Ref> additionalTags = new ArrayList<>();
410 		final Map<String, Ref> haveRefs = localRefs();
411 		for (Ref r : conn.getRefs()) {
412 			if (!isTag(r))
413 				continue;
414 
415 			Ref local = haveRefs.get(r.getName());
416 			if (local != null)
417 				// We already have a tag with this name, don't fetch it (even if
418 				// the local is different).
419 				continue;
420 
421 			ObjectId obj = r.getPeeledObjectId();
422 			if (obj == null)
423 				obj = r.getObjectId();
424 
425 			if (askFor.containsKey(obj) || localHasObject(obj))
426 				wantTag(r);
427 			else
428 				additionalTags.add(r);
429 		}
430 		return additionalTags;
431 	}
432 
433 	private void expandFetchTags() throws TransportException {
434 		final Map<String, Ref> haveRefs = localRefs();
435 		for (Ref r : conn.getRefs()) {
436 			if (!isTag(r)) {
437 				continue;
438 			}
439 			ObjectId id = r.getObjectId();
440 			if (id == null) {
441 				continue;
442 			}
443 			final Ref local = haveRefs.get(r.getName());
444 			if (local == null || !id.equals(local.getObjectId())) {
445 				wantTag(r);
446 			}
447 		}
448 	}
449 
450 	private void wantTag(Ref r) throws TransportException {
451 		want(r, new RefSpec().setSource(r.getName())
452 				.setDestination(r.getName()).setForceUpdate(true));
453 	}
454 
455 	private void want(Ref src, RefSpec spec)
456 			throws TransportException {
457 		final ObjectId newId = src.getObjectId();
458 		if (newId == null) {
459 			throw new NullPointerException(MessageFormat.format(
460 					JGitText.get().transportProvidedRefWithNoObjectId,
461 					src.getName()));
462 		}
463 		if (spec.getDestination() != null) {
464 			final TrackingRefUpdate tru = createUpdate(spec, newId);
465 			if (newId.equals(tru.getOldObjectId()))
466 				return;
467 			localUpdates.add(tru);
468 		}
469 
470 		askFor.put(newId, src);
471 
472 		final FetchHeadRecordadRecord.html#FetchHeadRecord">FetchHeadRecord fhr = new FetchHeadRecord();
473 		fhr.newValue = newId;
474 		fhr.notForMerge = spec.getDestination() != null;
475 		fhr.sourceName = src.getName();
476 		fhr.sourceURI = transport.getURI();
477 		fetchHeadUpdates.add(fhr);
478 	}
479 
480 	private void want(ObjectId id) {
481 		askFor.put(id,
482 				new ObjectIdRef.Unpeeled(Ref.Storage.NETWORK, id.name(), id));
483 	}
484 
485 	private TrackingRefUpdate createUpdate(RefSpec spec, ObjectId newId)
486 			throws TransportException {
487 		Ref ref = localRefs().get(spec.getDestination());
488 		ObjectId oldId = ref != null && ref.getObjectId() != null
489 				? ref.getObjectId()
490 				: ObjectId.zeroId();
491 		return new TrackingRefUpdate(
492 				spec.isForceUpdate(),
493 				spec.getSource(),
494 				spec.getDestination(),
495 				oldId,
496 				newId);
497 	}
498 
499 	private Map<String, Ref> localRefs() throws TransportException {
500 		if (localRefs == null) {
501 			try {
502 				localRefs = transport.local.getRefDatabase()
503 						.getRefs(RefDatabase.ALL);
504 			} catch (IOException err) {
505 				throw new TransportException(JGitText.get().cannotListRefs, err);
506 			}
507 		}
508 		return localRefs;
509 	}
510 
511 	private void deleteStaleTrackingRefs(FetchResult result,
512 			BatchRefUpdate batch) throws IOException {
513 		Set<Ref> processed = new HashSet<>();
514 		for (Ref ref : localRefs().values()) {
515 			if (ref.isSymbolic()) {
516 				continue;
517 			}
518 			String refname = ref.getName();
519 			for (RefSpec spec : toFetch) {
520 				if (spec.matchDestination(refname)) {
521 					RefSpec s = spec.expandFromDestination(refname);
522 					if (result.getAdvertisedRef(s.getSource()) == null
523 							&& processed.add(ref)) {
524 						deleteTrackingRef(result, batch, s, ref);
525 					}
526 				}
527 			}
528 		}
529 	}
530 
531 	private void deleteTrackingRef(final FetchResult result,
532 			final BatchRefUpdate batch, final RefSpec spec, final Ref localRef) {
533 		if (localRef.getObjectId() == null)
534 			return;
535 		TrackingRefUpdate update = new TrackingRefUpdate(
536 				true,
537 				spec.getSource(),
538 				localRef.getName(),
539 				localRef.getObjectId(),
540 				ObjectId.zeroId());
541 		result.add(update);
542 		batch.addCommand(update.asReceiveCommand());
543 	}
544 
545 	private static boolean isTag(Ref r) {
546 		return isTag(r.getName());
547 	}
548 
549 	private static boolean isTag(String name) {
550 		return name.startsWith(Constants.R_TAGS);
551 	}
552 
553 	private static String getFirstFailedRefName(BatchRefUpdate batch) {
554 		for (ReceiveCommand cmd : batch.getCommands()) {
555 			if (cmd.getResult() != ReceiveCommand.Result.OK)
556 				return cmd.getRefName();
557 		}
558 		return ""; //$NON-NLS-1$
559 	}
560 }