View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
5    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6    * and other copyright owners as documented in the project's IP log.
7    *
8    * This program and the accompanying materials are made available
9    * under the terms of the Eclipse Distribution License v1.0 which
10   * accompanies this distribution, is reproduced below, and is
11   * available at http://www.eclipse.org/org/documents/edl-v10.php
12   *
13   * All rights reserved.
14   *
15   * Redistribution and use in source and binary forms, with or
16   * without modification, are permitted provided that the following
17   * conditions are met:
18   *
19   * - Redistributions of source code must retain the above copyright
20   *   notice, this list of conditions and the following disclaimer.
21   *
22   * - Redistributions in binary form must reproduce the above
23   *   copyright notice, this list of conditions and the following
24   *   disclaimer in the documentation and/or other materials provided
25   *   with the distribution.
26   *
27   * - Neither the name of the Eclipse Foundation, Inc. nor the
28   *   names of its contributors may be used to endorse or promote
29   *   products derived from this software without specific prior
30   *   written permission.
31   *
32   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package org.eclipse.jgit.transport;
48  
49  import static org.eclipse.jgit.lib.Constants.CHARSET;
50  
51  import java.io.BufferedReader;
52  import java.io.IOException;
53  import java.io.InputStreamReader;
54  import java.io.OutputStream;
55  import java.io.PrintStream;
56  import java.lang.ref.WeakReference;
57  import java.lang.reflect.Field;
58  import java.lang.reflect.Modifier;
59  import java.net.URISyntaxException;
60  import java.net.URL;
61  import java.text.MessageFormat;
62  import java.util.ArrayList;
63  import java.util.Collection;
64  import java.util.Collections;
65  import java.util.Enumeration;
66  import java.util.LinkedHashSet;
67  import java.util.LinkedList;
68  import java.util.List;
69  import java.util.Map;
70  import java.util.Vector;
71  import java.util.concurrent.CopyOnWriteArrayList;
72  
73  import org.eclipse.jgit.api.errors.AbortedByHookException;
74  import org.eclipse.jgit.errors.NotSupportedException;
75  import org.eclipse.jgit.errors.TransportException;
76  import org.eclipse.jgit.hooks.Hooks;
77  import org.eclipse.jgit.hooks.PrePushHook;
78  import org.eclipse.jgit.internal.JGitText;
79  import org.eclipse.jgit.lib.Constants;
80  import org.eclipse.jgit.lib.ObjectChecker;
81  import org.eclipse.jgit.lib.ObjectId;
82  import org.eclipse.jgit.lib.ProgressMonitor;
83  import org.eclipse.jgit.lib.Ref;
84  import org.eclipse.jgit.lib.Repository;
85  import org.eclipse.jgit.storage.pack.PackConfig;
86  
87  /**
88   * Connects two Git repositories together and copies objects between them.
89   * <p>
90   * A transport can be used for either fetching (copying objects into the
91   * caller's repository from the remote repository) or pushing (copying objects
92   * into the remote repository from the caller's repository). Each transport
93   * implementation is responsible for the details associated with establishing
94   * the network connection(s) necessary for the copy, as well as actually
95   * shuffling data back and forth.
96   * <p>
97   * Transport instances and the connections they create are not thread-safe.
98   * Callers must ensure a transport is accessed by only one thread at a time.
99   */
100 public abstract class Transport implements AutoCloseable {
101 	/** Type of operation a Transport is being opened for. */
102 	public enum Operation {
103 		/** Transport is to fetch objects locally. */
104 		FETCH,
105 		/** Transport is to push objects remotely. */
106 		PUSH;
107 	}
108 
109 	private static final List<WeakReference<TransportProtocol>> protocols =
110 		new CopyOnWriteArrayList<>();
111 
112 	static {
113 		// Registration goes backwards in order of priority.
114 		register(TransportLocal.PROTO_LOCAL);
115 		register(TransportBundleFile.PROTO_BUNDLE);
116 		register(TransportAmazonS3.PROTO_S3);
117 		register(TransportGitAnon.PROTO_GIT);
118 		register(TransportSftp.PROTO_SFTP);
119 		register(TransportHttp.PROTO_FTP);
120 		register(TransportHttp.PROTO_HTTP);
121 		register(TransportGitSsh.PROTO_SSH);
122 
123 		registerByService();
124 	}
125 
126 	private static void registerByService() {
127 		ClassLoader ldr = Thread.currentThread().getContextClassLoader();
128 		if (ldr == null)
129 			ldr = Transport.class.getClassLoader();
130 		Enumeration<URL> catalogs = catalogs(ldr);
131 		while (catalogs.hasMoreElements())
132 			scan(ldr, catalogs.nextElement());
133 	}
134 
135 	private static Enumeration<URL> catalogs(ClassLoader ldr) {
136 		try {
137 			String prefix = "META-INF/services/"; //$NON-NLS-1$
138 			String name = prefix + Transport.class.getName();
139 			return ldr.getResources(name);
140 		} catch (IOException err) {
141 			return new Vector<URL>().elements();
142 		}
143 	}
144 
145 	private static void scan(ClassLoader ldr, URL url) {
146 		try (BufferedReader br = new BufferedReader(
147 				new InputStreamReader(url.openStream(), CHARSET))) {
148 			String line;
149 			while ((line = br.readLine()) != null) {
150 				line = line.trim();
151 				if (line.length() == 0)
152 					continue;
153 				int comment = line.indexOf('#');
154 				if (comment == 0)
155 					continue;
156 				if (comment != -1)
157 					line = line.substring(0, comment).trim();
158 				load(ldr, line);
159 			}
160 		} catch (IOException e) {
161 			// Ignore errors
162 		}
163 	}
164 
165 	private static void load(ClassLoader ldr, String cn) {
166 		Class<?> clazz;
167 		try {
168 			clazz = Class.forName(cn, false, ldr);
169 		} catch (ClassNotFoundException notBuiltin) {
170 			// Doesn't exist, even though the service entry is present.
171 			//
172 			return;
173 		}
174 
175 		for (Field f : clazz.getDeclaredFields()) {
176 			if ((f.getModifiers() & Modifier.STATIC) == Modifier.STATIC
177 					&& TransportProtocol.class.isAssignableFrom(f.getType())) {
178 				TransportProtocol proto;
179 				try {
180 					proto = (TransportProtocol) f.get(null);
181 				} catch (IllegalArgumentException e) {
182 					// If we cannot access the field, don't.
183 					continue;
184 				} catch (IllegalAccessException e) {
185 					// If we cannot access the field, don't.
186 					continue;
187 				}
188 				if (proto != null)
189 					register(proto);
190 			}
191 		}
192 	}
193 
194 	/**
195 	 * Register a TransportProtocol instance for use during open.
196 	 * <p>
197 	 * Protocol definitions are held by WeakReference, allowing them to be
198 	 * garbage collected when the calling application drops all strongly held
199 	 * references to the TransportProtocol. Therefore applications should use a
200 	 * singleton pattern as described in
201 	 * {@link org.eclipse.jgit.transport.TransportProtocol}'s class
202 	 * documentation to ensure their protocol does not get disabled by garbage
203 	 * collection earlier than expected.
204 	 * <p>
205 	 * The new protocol is registered in front of all earlier protocols, giving
206 	 * it higher priority than the built-in protocol definitions.
207 	 *
208 	 * @param proto
209 	 *            the protocol definition. Must not be null.
210 	 */
211 	public static void register(TransportProtocol proto) {
212 		protocols.add(0, new WeakReference<>(proto));
213 	}
214 
215 	/**
216 	 * Unregister a TransportProtocol instance.
217 	 * <p>
218 	 * Unregistering a protocol usually isn't necessary, as protocols are held
219 	 * by weak references and will automatically clear when they are garbage
220 	 * collected by the JVM. Matching is handled by reference equality, so the
221 	 * exact reference given to {@link #register(TransportProtocol)} must be
222 	 * used.
223 	 *
224 	 * @param proto
225 	 *            the exact object previously given to register.
226 	 */
227 	public static void unregister(TransportProtocol proto) {
228 		for (WeakReference<TransportProtocol> ref : protocols) {
229 			TransportProtocol refProto = ref.get();
230 			if (refProto == null || refProto == proto)
231 				protocols.remove(ref);
232 		}
233 	}
234 
235 	/**
236 	 * Obtain a copy of the registered protocols.
237 	 *
238 	 * @return an immutable copy of the currently registered protocols.
239 	 */
240 	public static List<TransportProtocol> getTransportProtocols() {
241 		int cnt = protocols.size();
242 		List<TransportProtocol> res = new ArrayList<>(cnt);
243 		for (WeakReference<TransportProtocol> ref : protocols) {
244 			TransportProtocol proto = ref.get();
245 			if (proto != null)
246 				res.add(proto);
247 			else
248 				protocols.remove(ref);
249 		}
250 		return Collections.unmodifiableList(res);
251 	}
252 
253 	/**
254 	 * Open a new transport instance to connect two repositories.
255 	 * <p>
256 	 * This method assumes
257 	 * {@link org.eclipse.jgit.transport.Transport.Operation#FETCH}.
258 	 *
259 	 * @param local
260 	 *            existing local repository.
261 	 * @param remote
262 	 *            location of the remote repository - may be URI or remote
263 	 *            configuration name.
264 	 * @return the new transport instance. Never null. In case of multiple URIs
265 	 *         in remote configuration, only the first is chosen.
266 	 * @throws java.net.URISyntaxException
267 	 *             the location is not a remote defined in the configuration
268 	 *             file and is not a well-formed URL.
269 	 * @throws org.eclipse.jgit.errors.NotSupportedException
270 	 *             the protocol specified is not supported.
271 	 * @throws org.eclipse.jgit.errors.TransportException
272 	 *             the transport cannot open this URI.
273 	 */
274 	public static Transport open(Repository local, String remote)
275 			throws NotSupportedException, URISyntaxException,
276 			TransportException {
277 		return open(local, remote, Operation.FETCH);
278 	}
279 
280 	/**
281 	 * Open a new transport instance to connect two repositories.
282 	 *
283 	 * @param local
284 	 *            existing local repository.
285 	 * @param remote
286 	 *            location of the remote repository - may be URI or remote
287 	 *            configuration name.
288 	 * @param op
289 	 *            planned use of the returned Transport; the URI may differ
290 	 *            based on the type of connection desired.
291 	 * @return the new transport instance. Never null. In case of multiple URIs
292 	 *         in remote configuration, only the first is chosen.
293 	 * @throws java.net.URISyntaxException
294 	 *             the location is not a remote defined in the configuration
295 	 *             file and is not a well-formed URL.
296 	 * @throws org.eclipse.jgit.errors.NotSupportedException
297 	 *             the protocol specified is not supported.
298 	 * @throws org.eclipse.jgit.errors.TransportException
299 	 *             the transport cannot open this URI.
300 	 */
301 	public static Transport open(final Repository local, final String remote,
302 			final Operation op) throws NotSupportedException,
303 			URISyntaxException, TransportException {
304 		if (local != null) {
305 			final RemoteConfig cfg = new RemoteConfig(local.getConfig(), remote);
306 			if (doesNotExist(cfg))
307 				return open(local, new URIish(remote), null);
308 			return open(local, cfg, op);
309 		} else
310 			return open(new URIish(remote));
311 
312 	}
313 
314 	/**
315 	 * Open new transport instances to connect two repositories.
316 	 * <p>
317 	 * This method assumes
318 	 * {@link org.eclipse.jgit.transport.Transport.Operation#FETCH}.
319 	 *
320 	 * @param local
321 	 *            existing local repository.
322 	 * @param remote
323 	 *            location of the remote repository - may be URI or remote
324 	 *            configuration name.
325 	 * @return the list of new transport instances for every URI in remote
326 	 *         configuration.
327 	 * @throws java.net.URISyntaxException
328 	 *             the location is not a remote defined in the configuration
329 	 *             file and is not a well-formed URL.
330 	 * @throws org.eclipse.jgit.errors.NotSupportedException
331 	 *             the protocol specified is not supported.
332 	 * @throws org.eclipse.jgit.errors.TransportException
333 	 *             the transport cannot open this URI.
334 	 */
335 	public static List<Transport> openAll(final Repository local,
336 			final String remote) throws NotSupportedException,
337 			URISyntaxException, TransportException {
338 		return openAll(local, remote, Operation.FETCH);
339 	}
340 
341 	/**
342 	 * Open new transport instances to connect two repositories.
343 	 *
344 	 * @param local
345 	 *            existing local repository.
346 	 * @param remote
347 	 *            location of the remote repository - may be URI or remote
348 	 *            configuration name.
349 	 * @param op
350 	 *            planned use of the returned Transport; the URI may differ
351 	 *            based on the type of connection desired.
352 	 * @return the list of new transport instances for every URI in remote
353 	 *         configuration.
354 	 * @throws java.net.URISyntaxException
355 	 *             the location is not a remote defined in the configuration
356 	 *             file and is not a well-formed URL.
357 	 * @throws org.eclipse.jgit.errors.NotSupportedException
358 	 *             the protocol specified is not supported.
359 	 * @throws org.eclipse.jgit.errors.TransportException
360 	 *             the transport cannot open this URI.
361 	 */
362 	public static List<Transport> openAll(final Repository local,
363 			final String remote, final Operation op)
364 			throws NotSupportedException, URISyntaxException,
365 			TransportException {
366 		final RemoteConfig cfg = new RemoteConfig(local.getConfig(), remote);
367 		if (doesNotExist(cfg)) {
368 			final ArrayList<Transport> transports = new ArrayList<>(1);
369 			transports.add(open(local, new URIish(remote), null));
370 			return transports;
371 		}
372 		return openAll(local, cfg, op);
373 	}
374 
375 	/**
376 	 * Open a new transport instance to connect two repositories.
377 	 * <p>
378 	 * This method assumes
379 	 * {@link org.eclipse.jgit.transport.Transport.Operation#FETCH}.
380 	 *
381 	 * @param local
382 	 *            existing local repository.
383 	 * @param cfg
384 	 *            configuration describing how to connect to the remote
385 	 *            repository.
386 	 * @return the new transport instance. Never null. In case of multiple URIs
387 	 *         in remote configuration, only the first is chosen.
388 	 * @throws org.eclipse.jgit.errors.NotSupportedException
389 	 *             the protocol specified is not supported.
390 	 * @throws org.eclipse.jgit.errors.TransportException
391 	 *             the transport cannot open this URI.
392 	 * @throws java.lang.IllegalArgumentException
393 	 *             if provided remote configuration doesn't have any URI
394 	 *             associated.
395 	 */
396 	public static Transport open(Repository local, RemoteConfig cfg)
397 			throws NotSupportedException, TransportException {
398 		return open(local, cfg, Operation.FETCH);
399 	}
400 
401 	/**
402 	 * Open a new transport instance to connect two repositories.
403 	 *
404 	 * @param local
405 	 *            existing local repository.
406 	 * @param cfg
407 	 *            configuration describing how to connect to the remote
408 	 *            repository.
409 	 * @param op
410 	 *            planned use of the returned Transport; the URI may differ
411 	 *            based on the type of connection desired.
412 	 * @return the new transport instance. Never null. In case of multiple URIs
413 	 *         in remote configuration, only the first is chosen.
414 	 * @throws org.eclipse.jgit.errors.NotSupportedException
415 	 *             the protocol specified is not supported.
416 	 * @throws org.eclipse.jgit.errors.TransportException
417 	 *             the transport cannot open this URI.
418 	 * @throws java.lang.IllegalArgumentException
419 	 *             if provided remote configuration doesn't have any URI
420 	 *             associated.
421 	 */
422 	public static Transport open(final Repository local,
423 			final RemoteConfig cfg, final Operation op)
424 			throws NotSupportedException, TransportException {
425 		final List<URIish> uris = getURIs(cfg, op);
426 		if (uris.isEmpty())
427 			throw new IllegalArgumentException(MessageFormat.format(
428 					JGitText.get().remoteConfigHasNoURIAssociated, cfg.getName()));
429 		final Transport tn = open(local, uris.get(0), cfg.getName());
430 		tn.applyConfig(cfg);
431 		return tn;
432 	}
433 
434 	/**
435 	 * Open new transport instances to connect two repositories.
436 	 * <p>
437 	 * This method assumes
438 	 * {@link org.eclipse.jgit.transport.Transport.Operation#FETCH}.
439 	 *
440 	 * @param local
441 	 *            existing local repository.
442 	 * @param cfg
443 	 *            configuration describing how to connect to the remote
444 	 *            repository.
445 	 * @return the list of new transport instances for every URI in remote
446 	 *         configuration.
447 	 * @throws org.eclipse.jgit.errors.NotSupportedException
448 	 *             the protocol specified is not supported.
449 	 * @throws org.eclipse.jgit.errors.TransportException
450 	 *             the transport cannot open this URI.
451 	 */
452 	public static List<Transport> openAll(final Repository local,
453 			final RemoteConfig cfg) throws NotSupportedException,
454 			TransportException {
455 		return openAll(local, cfg, Operation.FETCH);
456 	}
457 
458 	/**
459 	 * Open new transport instances to connect two repositories.
460 	 *
461 	 * @param local
462 	 *            existing local repository.
463 	 * @param cfg
464 	 *            configuration describing how to connect to the remote
465 	 *            repository.
466 	 * @param op
467 	 *            planned use of the returned Transport; the URI may differ
468 	 *            based on the type of connection desired.
469 	 * @return the list of new transport instances for every URI in remote
470 	 *         configuration.
471 	 * @throws org.eclipse.jgit.errors.NotSupportedException
472 	 *             the protocol specified is not supported.
473 	 * @throws org.eclipse.jgit.errors.TransportException
474 	 *             the transport cannot open this URI.
475 	 */
476 	public static List<Transport> openAll(final Repository local,
477 			final RemoteConfig cfg, final Operation op)
478 			throws NotSupportedException, TransportException {
479 		final List<URIish> uris = getURIs(cfg, op);
480 		final List<Transport> transports = new ArrayList<>(uris.size());
481 		for (URIish uri : uris) {
482 			final Transport tn = open(local, uri, cfg.getName());
483 			tn.applyConfig(cfg);
484 			transports.add(tn);
485 		}
486 		return transports;
487 	}
488 
489 	private static List<URIish> getURIs(final RemoteConfig cfg,
490 			final Operation op) {
491 		switch (op) {
492 		case FETCH:
493 			return cfg.getURIs();
494 		case PUSH: {
495 			List<URIish> uris = cfg.getPushURIs();
496 			if (uris.isEmpty())
497 				uris = cfg.getURIs();
498 			return uris;
499 		}
500 		default:
501 			throw new IllegalArgumentException(op.toString());
502 		}
503 	}
504 
505 	private static boolean doesNotExist(RemoteConfig cfg) {
506 		return cfg.getURIs().isEmpty() && cfg.getPushURIs().isEmpty();
507 	}
508 
509 	/**
510 	 * Open a new transport instance to connect two repositories.
511 	 *
512 	 * @param local
513 	 *            existing local repository.
514 	 * @param uri
515 	 *            location of the remote repository.
516 	 * @return the new transport instance. Never null.
517 	 * @throws org.eclipse.jgit.errors.NotSupportedException
518 	 *             the protocol specified is not supported.
519 	 * @throws org.eclipse.jgit.errors.TransportException
520 	 *             the transport cannot open this URI.
521 	 */
522 	public static Transport open(Repository local, URIish uri)
523 			throws NotSupportedException, TransportException {
524 		return open(local, uri, null);
525 	}
526 
527 	/**
528 	 * Open a new transport instance to connect two repositories.
529 	 *
530 	 * @param local
531 	 *            existing local repository.
532 	 * @param uri
533 	 *            location of the remote repository.
534 	 * @param remoteName
535 	 *            name of the remote, if the remote as configured in
536 	 *            {@code local}; otherwise null.
537 	 * @return the new transport instance. Never null.
538 	 * @throws org.eclipse.jgit.errors.NotSupportedException
539 	 *             the protocol specified is not supported.
540 	 * @throws org.eclipse.jgit.errors.TransportException
541 	 *             the transport cannot open this URI.
542 	 */
543 	public static Transport open(Repository local, URIish uri, String remoteName)
544 			throws NotSupportedException, TransportException {
545 		for (WeakReference<TransportProtocol> ref : protocols) {
546 			TransportProtocol proto = ref.get();
547 			if (proto == null) {
548 				protocols.remove(ref);
549 				continue;
550 			}
551 
552 			if (proto.canHandle(uri, local, remoteName)) {
553 				Transport tn = proto.open(uri, local, remoteName);
554 				tn.prePush = Hooks.prePush(local, tn.hookOutRedirect);
555 				tn.prePush.setRemoteLocation(uri.toString());
556 				tn.prePush.setRemoteName(remoteName);
557 				return tn;
558 			}
559 		}
560 
561 		throw new NotSupportedException(MessageFormat.format(JGitText.get().URINotSupported, uri));
562 	}
563 
564 	/**
565 	 * Open a new transport with no local repository.
566 	 * <p>
567 	 * Note that the resulting transport instance can not be used for fetching
568 	 * or pushing, but only for reading remote refs.
569 	 *
570 	 * @param uri a {@link org.eclipse.jgit.transport.URIish} object.
571 	 * @return new Transport instance
572 	 * @throws org.eclipse.jgit.errors.NotSupportedException
573 	 * @throws org.eclipse.jgit.errors.TransportException
574 	 */
575 	public static Transport open(URIish uri) throws NotSupportedException, TransportException {
576 		for (WeakReference<TransportProtocol> ref : protocols) {
577 			TransportProtocol proto = ref.get();
578 			if (proto == null) {
579 				protocols.remove(ref);
580 				continue;
581 			}
582 
583 			if (proto.canHandle(uri, null, null))
584 				return proto.open(uri);
585 		}
586 
587 		throw new NotSupportedException(MessageFormat.format(JGitText.get().URINotSupported, uri));
588 	}
589 
590 	/**
591 	 * Convert push remote refs update specification from
592 	 * {@link org.eclipse.jgit.transport.RefSpec} form to
593 	 * {@link org.eclipse.jgit.transport.RemoteRefUpdate}. Conversion expands
594 	 * wildcards by matching source part to local refs. expectedOldObjectId in
595 	 * RemoteRefUpdate is set when specified in leases. Tracking branch is
596 	 * configured if RefSpec destination matches source of any fetch ref spec
597 	 * for this transport remote configuration.
598 	 *
599 	 * @param db
600 	 *            local database.
601 	 * @param specs
602 	 *            collection of RefSpec to convert.
603 	 * @param leases
604 	 *            map from ref to lease (containing expected old object id)
605 	 * @param fetchSpecs
606 	 *            fetch specifications used for finding localtracking refs. May
607 	 *            be null or empty collection.
608 	 * @return collection of set up
609 	 *         {@link org.eclipse.jgit.transport.RemoteRefUpdate}.
610 	 * @throws java.io.IOException
611 	 *             when problem occurred during conversion or specification set
612 	 *             up: most probably, missing objects or refs.
613 	 * @since 4.7
614 	 */
615 	public static Collection<RemoteRefUpdate> findRemoteRefUpdatesFor(
616 			final Repository db, final Collection<RefSpec> specs,
617 			final Map<String, RefLeaseSpec> leases,
618 			Collection<RefSpec> fetchSpecs) throws IOException {
619 		if (fetchSpecs == null)
620 			fetchSpecs = Collections.emptyList();
621 		final List<RemoteRefUpdate> result = new LinkedList<>();
622 		final Collection<RefSpec> procRefs = expandPushWildcardsFor(db, specs);
623 
624 		for (RefSpec spec : procRefs) {
625 			String srcSpec = spec.getSource();
626 			final Ref srcRef = db.findRef(srcSpec);
627 			if (srcRef != null)
628 				srcSpec = srcRef.getName();
629 
630 			String destSpec = spec.getDestination();
631 			if (destSpec == null) {
632 				// No destination (no-colon in ref-spec), DWIMery assumes src
633 				//
634 				destSpec = srcSpec;
635 			}
636 
637 			if (srcRef != null && !destSpec.startsWith(Constants.R_REFS)) {
638 				// Assume the same kind of ref at the destination, e.g.
639 				// "refs/heads/foo:master", DWIMery assumes master is also
640 				// under "refs/heads/".
641 				//
642 				final String n = srcRef.getName();
643 				final int kindEnd = n.indexOf('/', Constants.R_REFS.length());
644 				destSpec = n.substring(0, kindEnd + 1) + destSpec;
645 			}
646 
647 			final boolean forceUpdate = spec.isForceUpdate();
648 			final String localName = findTrackingRefName(destSpec, fetchSpecs);
649 			final RefLeaseSpec leaseSpec = leases.get(destSpec);
650 			final ObjectId expected = leaseSpec == null ? null :
651 				db.resolve(leaseSpec.getExpected());
652 			final RemoteRefUpdate rru = new RemoteRefUpdate(db, srcSpec,
653 					destSpec, forceUpdate, localName, expected);
654 			result.add(rru);
655 		}
656 		return result;
657 	}
658 
659 	/**
660 	 * Convert push remote refs update specification from
661 	 * {@link org.eclipse.jgit.transport.RefSpec} form to
662 	 * {@link org.eclipse.jgit.transport.RemoteRefUpdate}. Conversion expands
663 	 * wildcards by matching source part to local refs. expectedOldObjectId in
664 	 * RemoteRefUpdate is always set as null. Tracking branch is configured if
665 	 * RefSpec destination matches source of any fetch ref spec for this
666 	 * transport remote configuration.
667 	 *
668 	 * @param db
669 	 *            local database.
670 	 * @param specs
671 	 *            collection of RefSpec to convert.
672 	 * @param fetchSpecs
673 	 *            fetch specifications used for finding localtracking refs. May
674 	 *            be null or empty collection.
675 	 * @return collection of set up
676 	 *         {@link org.eclipse.jgit.transport.RemoteRefUpdate}.
677 	 * @throws java.io.IOException
678 	 *             when problem occurred during conversion or specification set
679 	 *             up: most probably, missing objects or refs.
680 	 */
681 	public static Collection<RemoteRefUpdate> findRemoteRefUpdatesFor(
682 			final Repository db, final Collection<RefSpec> specs,
683 			Collection<RefSpec> fetchSpecs) throws IOException {
684 		return findRemoteRefUpdatesFor(db, specs, Collections.emptyMap(),
685 					       fetchSpecs);
686 	}
687 
688 	private static Collection<RefSpec> expandPushWildcardsFor(
689 			final Repository db, final Collection<RefSpec> specs)
690 			throws IOException {
691 		final List<Ref> localRefs = db.getRefDatabase().getRefs();
692 		final Collection<RefSpec> procRefs = new LinkedHashSet<>();
693 
694 		for (RefSpec spec : specs) {
695 			if (spec.isWildcard()) {
696 				for (Ref localRef : localRefs) {
697 					if (spec.matchSource(localRef))
698 						procRefs.add(spec.expandFromSource(localRef));
699 				}
700 			} else {
701 				procRefs.add(spec);
702 			}
703 		}
704 		return procRefs;
705 	}
706 
707 	private static String findTrackingRefName(final String remoteName,
708 			final Collection<RefSpec> fetchSpecs) {
709 		// try to find matching tracking refs
710 		for (RefSpec fetchSpec : fetchSpecs) {
711 			if (fetchSpec.matchSource(remoteName)) {
712 				if (fetchSpec.isWildcard())
713 					return fetchSpec.expandFromSource(remoteName)
714 							.getDestination();
715 				else
716 					return fetchSpec.getDestination();
717 			}
718 		}
719 		return null;
720 	}
721 
722 	/**
723 	 * Default setting for {@link #fetchThin} option.
724 	 */
725 	public static final boolean DEFAULT_FETCH_THIN = true;
726 
727 	/**
728 	 * Default setting for {@link #pushThin} option.
729 	 */
730 	public static final boolean DEFAULT_PUSH_THIN = false;
731 
732 	/**
733 	 * Specification for fetch or push operations, to fetch or push all tags.
734 	 * Acts as --tags.
735 	 */
736 	public static final RefSpec REFSPEC_TAGS = new RefSpec(
737 			"refs/tags/*:refs/tags/*"); //$NON-NLS-1$
738 
739 	/**
740 	 * Specification for push operation, to push all refs under refs/heads. Acts
741 	 * as --all.
742 	 */
743 	public static final RefSpec REFSPEC_PUSH_ALL = new RefSpec(
744 			"refs/heads/*:refs/heads/*"); //$NON-NLS-1$
745 
746 	/** The repository this transport fetches into, or pushes out of. */
747 	protected final Repository local;
748 
749 	/** The URI used to create this transport. */
750 	protected final URIish uri;
751 
752 	/** Name of the upload pack program, if it must be executed. */
753 	private String optionUploadPack = RemoteConfig.DEFAULT_UPLOAD_PACK;
754 
755 	/** Specifications to apply during fetch. */
756 	private List<RefSpec> fetch = Collections.emptyList();
757 
758 	/**
759 	 * How {@link #fetch(ProgressMonitor, Collection)} should handle tags.
760 	 * <p>
761 	 * We default to {@link TagOpt#NO_TAGS} so as to avoid fetching annotated
762 	 * tags during one-shot fetches used for later merges. This prevents
763 	 * dragging down tags from repositories that we do not have established
764 	 * tracking branches for. If we do not track the source repository, we most
765 	 * likely do not care about any tags it publishes.
766 	 */
767 	private TagOpt tagopt = TagOpt.NO_TAGS;
768 
769 	/** Should fetch request thin-pack if remote repository can produce it. */
770 	private boolean fetchThin = DEFAULT_FETCH_THIN;
771 
772 	/** Name of the receive pack program, if it must be executed. */
773 	private String optionReceivePack = RemoteConfig.DEFAULT_RECEIVE_PACK;
774 
775 	/** Specifications to apply during push. */
776 	private List<RefSpec> push = Collections.emptyList();
777 
778 	/** Should push produce thin-pack when sending objects to remote repository. */
779 	private boolean pushThin = DEFAULT_PUSH_THIN;
780 
781 	/** Should push be all-or-nothing atomic behavior? */
782 	private boolean pushAtomic;
783 
784 	/** Should push just check for operation result, not really push. */
785 	private boolean dryRun;
786 
787 	/** Should an incoming (fetch) transfer validate objects? */
788 	private ObjectChecker objectChecker;
789 
790 	/** Should refs no longer on the source be pruned from the destination? */
791 	private boolean removeDeletedRefs;
792 
793 	private long filterBlobLimit = -1;
794 
795 	/** Timeout in seconds to wait before aborting an IO read or write. */
796 	private int timeout;
797 
798 	/** Pack configuration used by this transport to make pack file. */
799 	private PackConfig packConfig;
800 
801 	/** Assists with authentication the connection. */
802 	private CredentialsProvider credentialsProvider;
803 
804 	/** The option strings associated with the push operation. */
805 	private List<String> pushOptions;
806 
807 	private PrintStream hookOutRedirect;
808 
809 	private PrePushHook prePush;
810 	/**
811 	 * Create a new transport instance.
812 	 *
813 	 * @param local
814 	 *            the repository this instance will fetch into, or push out of.
815 	 *            This must be the repository passed to
816 	 *            {@link #open(Repository, URIish)}.
817 	 * @param uri
818 	 *            the URI used to access the remote repository. This must be the
819 	 *            URI passed to {@link #open(Repository, URIish)}.
820 	 */
821 	protected Transport(Repository local, URIish uri) {
822 		final TransferConfig tc = local.getConfig().get(TransferConfig.KEY);
823 		this.local = local;
824 		this.uri = uri;
825 		this.objectChecker = tc.newObjectChecker();
826 		this.credentialsProvider = CredentialsProvider.getDefault();
827 		prePush = Hooks.prePush(local, hookOutRedirect);
828 	}
829 
830 	/**
831 	 * Create a minimal transport instance not tied to a single repository.
832 	 *
833 	 * @param uri
834 	 *            a {@link org.eclipse.jgit.transport.URIish} object.
835 	 */
836 	protected Transport(URIish uri) {
837 		this.uri = uri;
838 		this.local = null;
839 		this.objectChecker = new ObjectChecker();
840 		this.credentialsProvider = CredentialsProvider.getDefault();
841 	}
842 
843 	/**
844 	 * Get the URI this transport connects to.
845 	 * <p>
846 	 * Each transport instance connects to at most one URI at any point in time.
847 	 *
848 	 * @return the URI describing the location of the remote repository.
849 	 */
850 	public URIish getURI() {
851 		return uri;
852 	}
853 
854 	/**
855 	 * Get the name of the remote executable providing upload-pack service.
856 	 *
857 	 * @return typically "git-upload-pack".
858 	 */
859 	public String getOptionUploadPack() {
860 		return optionUploadPack;
861 	}
862 
863 	/**
864 	 * Set the name of the remote executable providing upload-pack services.
865 	 *
866 	 * @param where
867 	 *            name of the executable.
868 	 */
869 	public void setOptionUploadPack(String where) {
870 		if (where != null && where.length() > 0)
871 			optionUploadPack = where;
872 		else
873 			optionUploadPack = RemoteConfig.DEFAULT_UPLOAD_PACK;
874 	}
875 
876 	/**
877 	 * Get the description of how annotated tags should be treated during fetch.
878 	 *
879 	 * @return option indicating the behavior of annotated tags in fetch.
880 	 */
881 	public TagOpt getTagOpt() {
882 		return tagopt;
883 	}
884 
885 	/**
886 	 * Set the description of how annotated tags should be treated on fetch.
887 	 *
888 	 * @param option
889 	 *            method to use when handling annotated tags.
890 	 */
891 	public void setTagOpt(TagOpt option) {
892 		tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;
893 	}
894 
895 	/**
896 	 * Default setting is: {@link #DEFAULT_FETCH_THIN}
897 	 *
898 	 * @return true if fetch should request thin-pack when possible; false
899 	 *         otherwise
900 	 * @see PackTransport
901 	 */
902 	public boolean isFetchThin() {
903 		return fetchThin;
904 	}
905 
906 	/**
907 	 * Set the thin-pack preference for fetch operation. Default setting is:
908 	 * {@link #DEFAULT_FETCH_THIN}
909 	 *
910 	 * @param fetchThin
911 	 *            true when fetch should request thin-pack when possible; false
912 	 *            when it shouldn't
913 	 * @see PackTransport
914 	 */
915 	public void setFetchThin(boolean fetchThin) {
916 		this.fetchThin = fetchThin;
917 	}
918 
919 	/**
920 	 * Whether fetch will verify if received objects are formatted correctly.
921 	 *
922 	 * @return true if fetch will verify received objects are formatted
923 	 *         correctly. Validating objects requires more CPU time on the
924 	 *         client side of the connection.
925 	 */
926 	public boolean isCheckFetchedObjects() {
927 		return getObjectChecker() != null;
928 	}
929 
930 	/**
931 	 * Configure if checking received objects is enabled
932 	 *
933 	 * @param check
934 	 *            true to enable checking received objects; false to assume all
935 	 *            received objects are valid.
936 	 * @see #setObjectChecker(ObjectChecker)
937 	 */
938 	public void setCheckFetchedObjects(boolean check) {
939 		if (check && objectChecker == null)
940 			setObjectChecker(new ObjectChecker());
941 		else if (!check && objectChecker != null)
942 			setObjectChecker(null);
943 	}
944 
945 	/**
946 	 * Get configured object checker for received objects
947 	 *
948 	 * @return configured object checker for received objects, or null.
949 	 * @since 3.6
950 	 */
951 	public ObjectChecker getObjectChecker() {
952 		return objectChecker;
953 	}
954 
955 	/**
956 	 * Set the object checker to verify each received object with
957 	 *
958 	 * @param impl
959 	 *            if non-null the object checking instance to verify each
960 	 *            received object with; null to disable object checking.
961 	 * @since 3.6
962 	 */
963 	public void setObjectChecker(ObjectChecker impl) {
964 		objectChecker = impl;
965 	}
966 
967 	/**
968 	 * Default setting is:
969 	 * {@link org.eclipse.jgit.transport.RemoteConfig#DEFAULT_RECEIVE_PACK}
970 	 *
971 	 * @return remote executable providing receive-pack service for pack
972 	 *         transports.
973 	 * @see PackTransport
974 	 */
975 	public String getOptionReceivePack() {
976 		return optionReceivePack;
977 	}
978 
979 	/**
980 	 * Set remote executable providing receive-pack service for pack transports.
981 	 * Default setting is:
982 	 * {@link org.eclipse.jgit.transport.RemoteConfig#DEFAULT_RECEIVE_PACK}
983 	 *
984 	 * @param optionReceivePack
985 	 *            remote executable, if null or empty default one is set;
986 	 */
987 	public void setOptionReceivePack(String optionReceivePack) {
988 		if (optionReceivePack != null && optionReceivePack.length() > 0)
989 			this.optionReceivePack = optionReceivePack;
990 		else
991 			this.optionReceivePack = RemoteConfig.DEFAULT_RECEIVE_PACK;
992 	}
993 
994 	/**
995 	 * Default setting is: {@value #DEFAULT_PUSH_THIN}
996 	 *
997 	 * @return true if push should produce thin-pack in pack transports
998 	 * @see PackTransport
999 	 */
1000 	public boolean isPushThin() {
1001 		return pushThin;
1002 	}
1003 
1004 	/**
1005 	 * Set thin-pack preference for push operation. Default setting is:
1006 	 * {@value #DEFAULT_PUSH_THIN}
1007 	 *
1008 	 * @param pushThin
1009 	 *            true when push should produce thin-pack in pack transports;
1010 	 *            false when it shouldn't
1011 	 * @see PackTransport
1012 	 */
1013 	public void setPushThin(boolean pushThin) {
1014 		this.pushThin = pushThin;
1015 	}
1016 
1017 	/**
1018 	 * Default setting is false.
1019 	 *
1020 	 * @return true if push requires all-or-nothing atomic behavior.
1021 	 * @since 4.2
1022 	 */
1023 	public boolean isPushAtomic() {
1024 		return pushAtomic;
1025 	}
1026 
1027 	/**
1028 	 * Request atomic push (all references succeed, or none do).
1029 	 * <p>
1030 	 * Server must also support atomic push. If the server does not support the
1031 	 * feature the push will abort without making changes.
1032 	 *
1033 	 * @param atomic
1034 	 *            true when push should be an all-or-nothing operation.
1035 	 * @see PackTransport
1036 	 * @since 4.2
1037 	 */
1038 	public void setPushAtomic(boolean atomic) {
1039 		this.pushAtomic = atomic;
1040 	}
1041 
1042 	/**
1043 	 * Whether destination refs should be removed if they no longer exist at the
1044 	 * source repository.
1045 	 *
1046 	 * @return true if destination refs should be removed if they no longer
1047 	 *         exist at the source repository.
1048 	 */
1049 	public boolean isRemoveDeletedRefs() {
1050 		return removeDeletedRefs;
1051 	}
1052 
1053 	/**
1054 	 * Set whether or not to remove refs which no longer exist in the source.
1055 	 * <p>
1056 	 * If true, refs at the destination repository (local for fetch, remote for
1057 	 * push) are deleted if they no longer exist on the source side (remote for
1058 	 * fetch, local for push).
1059 	 * <p>
1060 	 * False by default, as this may cause data to become unreachable, and
1061 	 * eventually be deleted on the next GC.
1062 	 *
1063 	 * @param remove true to remove refs that no longer exist.
1064 	 */
1065 	public void setRemoveDeletedRefs(boolean remove) {
1066 		removeDeletedRefs = remove;
1067 	}
1068 
1069 	/**
1070 	 * @return the last value passed to {@link #setFilterBlobLimit}, or -1 if
1071 	 *         it was never invoked.
1072 	 * @since 5.0
1073 	 */
1074 	public long getFilterBlobLimit() {
1075 		return filterBlobLimit;
1076 	}
1077 
1078 	/**
1079 	 * @param bytes exclude blobs of size greater than this
1080 	 * @since 5.0
1081 	 */
1082 	public void setFilterBlobLimit(long bytes) {
1083 		filterBlobLimit = bytes;
1084 	}
1085 
1086 	/**
1087 	 * Apply provided remote configuration on this transport.
1088 	 *
1089 	 * @param cfg
1090 	 *            configuration to apply on this transport.
1091 	 */
1092 	public void applyConfig(RemoteConfig cfg) {
1093 		setOptionUploadPack(cfg.getUploadPack());
1094 		setOptionReceivePack(cfg.getReceivePack());
1095 		setTagOpt(cfg.getTagOpt());
1096 		fetch = cfg.getFetchRefSpecs();
1097 		push = cfg.getPushRefSpecs();
1098 		timeout = cfg.getTimeout();
1099 	}
1100 
1101 	/**
1102 	 * Whether push operation should just check for possible result and not
1103 	 * really update remote refs
1104 	 *
1105 	 * @return true if push operation should just check for possible result and
1106 	 *         not really update remote refs, false otherwise - when push should
1107 	 *         act normally.
1108 	 */
1109 	public boolean isDryRun() {
1110 		return dryRun;
1111 	}
1112 
1113 	/**
1114 	 * Set dry run option for push operation.
1115 	 *
1116 	 * @param dryRun
1117 	 *            true if push operation should just check for possible result
1118 	 *            and not really update remote refs, false otherwise - when push
1119 	 *            should act normally.
1120 	 */
1121 	public void setDryRun(boolean dryRun) {
1122 		this.dryRun = dryRun;
1123 	}
1124 
1125 	/**
1126 	 * Get timeout (in seconds) before aborting an IO operation.
1127 	 *
1128 	 * @return timeout (in seconds) before aborting an IO operation.
1129 	 */
1130 	public int getTimeout() {
1131 		return timeout;
1132 	}
1133 
1134 	/**
1135 	 * Set the timeout before willing to abort an IO call.
1136 	 *
1137 	 * @param seconds
1138 	 *            number of seconds to wait (with no data transfer occurring)
1139 	 *            before aborting an IO read or write operation with this
1140 	 *            remote.
1141 	 */
1142 	public void setTimeout(int seconds) {
1143 		timeout = seconds;
1144 	}
1145 
1146 	/**
1147 	 * Get the configuration used by the pack generator to make packs.
1148 	 *
1149 	 * If {@link #setPackConfig(PackConfig)} was previously given null a new
1150 	 * PackConfig is created on demand by this method using the source
1151 	 * repository's settings.
1152 	 *
1153 	 * @return the pack configuration. Never null.
1154 	 */
1155 	public PackConfig getPackConfig() {
1156 		if (packConfig == null)
1157 			packConfig = new PackConfig(local);
1158 		return packConfig;
1159 	}
1160 
1161 	/**
1162 	 * Set the configuration used by the pack generator.
1163 	 *
1164 	 * @param pc
1165 	 *            configuration controlling packing parameters. If null the
1166 	 *            source repository's settings will be used.
1167 	 */
1168 	public void setPackConfig(PackConfig pc) {
1169 		packConfig = pc;
1170 	}
1171 
1172 	/**
1173 	 * A credentials provider to assist with authentication connections..
1174 	 *
1175 	 * @param credentialsProvider
1176 	 *            the credentials provider, or null if there is none
1177 	 */
1178 	public void setCredentialsProvider(CredentialsProvider credentialsProvider) {
1179 		this.credentialsProvider = credentialsProvider;
1180 	}
1181 
1182 	/**
1183 	 * The configured credentials provider.
1184 	 *
1185 	 * @return the credentials provider, or null if no credentials provider is
1186 	 *         associated with this transport.
1187 	 */
1188 	public CredentialsProvider getCredentialsProvider() {
1189 		return credentialsProvider;
1190 	}
1191 
1192 	/**
1193 	 * Get the option strings associated with the push operation
1194 	 *
1195 	 * @return the option strings associated with the push operation
1196 	 * @since 4.5
1197 	 */
1198 	public List<String> getPushOptions() {
1199 		return pushOptions;
1200 	}
1201 
1202 	/**
1203 	 * Sets the option strings associated with the push operation.
1204 	 *
1205 	 * @param pushOptions
1206 	 *            null if push options are unsupported
1207 	 * @since 4.5
1208 	 */
1209 	public void setPushOptions(List<String> pushOptions) {
1210 		this.pushOptions = pushOptions;
1211 	}
1212 
1213 	/**
1214 	 * Fetch objects and refs from the remote repository to the local one.
1215 	 * <p>
1216 	 * This is a utility function providing standard fetch behavior. Local
1217 	 * tracking refs associated with the remote repository are automatically
1218 	 * updated if this transport was created from a
1219 	 * {@link org.eclipse.jgit.transport.RemoteConfig} with fetch RefSpecs
1220 	 * defined.
1221 	 *
1222 	 * @param monitor
1223 	 *            progress monitor to inform the user about our processing
1224 	 *            activity. Must not be null. Use
1225 	 *            {@link org.eclipse.jgit.lib.NullProgressMonitor} if progress
1226 	 *            updates are not interesting or necessary.
1227 	 * @param toFetch
1228 	 *            specification of refs to fetch locally. May be null or the
1229 	 *            empty collection to use the specifications from the
1230 	 *            RemoteConfig. Source for each RefSpec can't be null.
1231 	 * @return information describing the tracking refs updated.
1232 	 * @throws org.eclipse.jgit.errors.NotSupportedException
1233 	 *             this transport implementation does not support fetching
1234 	 *             objects.
1235 	 * @throws org.eclipse.jgit.errors.TransportException
1236 	 *             the remote connection could not be established or object
1237 	 *             copying (if necessary) failed or update specification was
1238 	 *             incorrect.
1239 	 */
1240 	public FetchResult fetch(final ProgressMonitor monitor,
1241 			Collection<RefSpec> toFetch) throws NotSupportedException,
1242 			TransportException {
1243 		if (toFetch == null || toFetch.isEmpty()) {
1244 			// If the caller did not ask for anything use the defaults.
1245 			//
1246 			if (fetch.isEmpty())
1247 				throw new TransportException(JGitText.get().nothingToFetch);
1248 			toFetch = fetch;
1249 		} else if (!fetch.isEmpty()) {
1250 			// If the caller asked for something specific without giving
1251 			// us the local tracking branch see if we can update any of
1252 			// the local tracking branches without incurring additional
1253 			// object transfer overheads.
1254 			//
1255 			final Collection<RefSpec> tmp = new ArrayList<>(toFetch);
1256 			for (RefSpec requested : toFetch) {
1257 				final String reqSrc = requested.getSource();
1258 				for (RefSpec configured : fetch) {
1259 					final String cfgSrc = configured.getSource();
1260 					final String cfgDst = configured.getDestination();
1261 					if (cfgSrc.equals(reqSrc) && cfgDst != null) {
1262 						tmp.add(configured);
1263 						break;
1264 					}
1265 				}
1266 			}
1267 			toFetch = tmp;
1268 		}
1269 
1270 		final FetchResult result = new FetchResult();
1271 		new FetchProcess(this, toFetch).execute(monitor, result);
1272 
1273 		local.autoGC(monitor);
1274 
1275 		return result;
1276 	}
1277 
1278 	/**
1279 	 * Push objects and refs from the local repository to the remote one.
1280 	 * <p>
1281 	 * This is a utility function providing standard push behavior. It updates
1282 	 * remote refs and send there necessary objects according to remote ref
1283 	 * update specification. After successful remote ref update, associated
1284 	 * locally stored tracking branch is updated if set up accordingly. Detailed
1285 	 * operation result is provided after execution.
1286 	 * <p>
1287 	 * For setting up remote ref update specification from ref spec, see helper
1288 	 * method {@link #findRemoteRefUpdatesFor(Collection)}, predefined refspecs
1289 	 * ({@link #REFSPEC_TAGS}, {@link #REFSPEC_PUSH_ALL}) or consider using
1290 	 * directly {@link org.eclipse.jgit.transport.RemoteRefUpdate} for more
1291 	 * possibilities.
1292 	 * <p>
1293 	 * When {@link #isDryRun()} is true, result of this operation is just
1294 	 * estimation of real operation result, no real action is performed.
1295 	 *
1296 	 * @see RemoteRefUpdate
1297 	 * @param monitor
1298 	 *            progress monitor to inform the user about our processing
1299 	 *            activity. Must not be null. Use
1300 	 *            {@link org.eclipse.jgit.lib.NullProgressMonitor} if progress
1301 	 *            updates are not interesting or necessary.
1302 	 * @param toPush
1303 	 *            specification of refs to push. May be null or the empty
1304 	 *            collection to use the specifications from the RemoteConfig
1305 	 *            converted by {@link #findRemoteRefUpdatesFor(Collection)}. No
1306 	 *            more than 1 RemoteRefUpdate with the same remoteName is
1307 	 *            allowed. These objects are modified during this call.
1308 	 * @param out
1309 	 *            output stream to write messages to
1310 	 * @return information about results of remote refs updates, tracking refs
1311 	 *         updates and refs advertised by remote repository.
1312 	 * @throws org.eclipse.jgit.errors.NotSupportedException
1313 	 *             this transport implementation does not support pushing
1314 	 *             objects.
1315 	 * @throws org.eclipse.jgit.errors.TransportException
1316 	 *             the remote connection could not be established or object
1317 	 *             copying (if necessary) failed at I/O or protocol level or
1318 	 *             update specification was incorrect.
1319 	 * @since 3.0
1320 	 */
1321 	public PushResult push(final ProgressMonitor monitor,
1322 			Collection<RemoteRefUpdate> toPush, OutputStream out)
1323 			throws NotSupportedException,
1324 			TransportException {
1325 		if (toPush == null || toPush.isEmpty()) {
1326 			// If the caller did not ask for anything use the defaults.
1327 			try {
1328 				toPush = findRemoteRefUpdatesFor(push);
1329 			} catch (final IOException e) {
1330 				throw new TransportException(MessageFormat.format(
1331 						JGitText.get().problemWithResolvingPushRefSpecsLocally, e.getMessage()), e);
1332 			}
1333 			if (toPush.isEmpty())
1334 				throw new TransportException(JGitText.get().nothingToPush);
1335 		}
1336 		if (prePush != null) {
1337 			try {
1338 				prePush.setRefs(toPush);
1339 				prePush.call();
1340 			} catch (AbortedByHookException | IOException e) {
1341 				throw new TransportException(e.getMessage(), e);
1342 			}
1343 		}
1344 
1345 		final PushProcess pushProcess = new PushProcess(this, toPush, out);
1346 		return pushProcess.execute(monitor);
1347 	}
1348 
1349 	/**
1350 	 * Push objects and refs from the local repository to the remote one.
1351 	 * <p>
1352 	 * This is a utility function providing standard push behavior. It updates
1353 	 * remote refs and sends necessary objects according to remote ref update
1354 	 * specification. After successful remote ref update, associated locally
1355 	 * stored tracking branch is updated if set up accordingly. Detailed
1356 	 * operation result is provided after execution.
1357 	 * <p>
1358 	 * For setting up remote ref update specification from ref spec, see helper
1359 	 * method {@link #findRemoteRefUpdatesFor(Collection)}, predefined refspecs
1360 	 * ({@link #REFSPEC_TAGS}, {@link #REFSPEC_PUSH_ALL}) or consider using
1361 	 * directly {@link org.eclipse.jgit.transport.RemoteRefUpdate} for more
1362 	 * possibilities.
1363 	 * <p>
1364 	 * When {@link #isDryRun()} is true, result of this operation is just
1365 	 * estimation of real operation result, no real action is performed.
1366 	 *
1367 	 * @see RemoteRefUpdate
1368 	 * @param monitor
1369 	 *            progress monitor to inform the user about our processing
1370 	 *            activity. Must not be null. Use
1371 	 *            {@link org.eclipse.jgit.lib.NullProgressMonitor} if progress
1372 	 *            updates are not interesting or necessary.
1373 	 * @param toPush
1374 	 *            specification of refs to push. May be null or the empty
1375 	 *            collection to use the specifications from the RemoteConfig
1376 	 *            converted by {@link #findRemoteRefUpdatesFor(Collection)}. No
1377 	 *            more than 1 RemoteRefUpdate with the same remoteName is
1378 	 *            allowed. These objects are modified during this call.
1379 	 * @return information about results of remote refs updates, tracking refs
1380 	 *         updates and refs advertised by remote repository.
1381 	 * @throws org.eclipse.jgit.errors.NotSupportedException
1382 	 *             this transport implementation does not support pushing
1383 	 *             objects.
1384 	 * @throws org.eclipse.jgit.errors.TransportException
1385 	 *             the remote connection could not be established or object
1386 	 *             copying (if necessary) failed at I/O or protocol level or
1387 	 *             update specification was incorrect.
1388 	 */
1389 	public PushResult push(final ProgressMonitor monitor,
1390 			Collection<RemoteRefUpdate> toPush) throws NotSupportedException,
1391 			TransportException {
1392 		return push(monitor, toPush, null);
1393 	}
1394 
1395 	/**
1396 	 * Convert push remote refs update specification from
1397 	 * {@link org.eclipse.jgit.transport.RefSpec} form to
1398 	 * {@link org.eclipse.jgit.transport.RemoteRefUpdate}. Conversion expands
1399 	 * wildcards by matching source part to local refs. expectedOldObjectId in
1400 	 * RemoteRefUpdate is always set as null. Tracking branch is configured if
1401 	 * RefSpec destination matches source of any fetch ref spec for this
1402 	 * transport remote configuration.
1403 	 * <p>
1404 	 * Conversion is performed for context of this transport (database, fetch
1405 	 * specifications).
1406 	 *
1407 	 * @param specs
1408 	 *            collection of RefSpec to convert.
1409 	 * @return collection of set up
1410 	 *         {@link org.eclipse.jgit.transport.RemoteRefUpdate}.
1411 	 * @throws java.io.IOException
1412 	 *             when problem occurred during conversion or specification set
1413 	 *             up: most probably, missing objects or refs.
1414 	 */
1415 	public Collection<RemoteRefUpdate> findRemoteRefUpdatesFor(
1416 			final Collection<RefSpec> specs) throws IOException {
1417 		return findRemoteRefUpdatesFor(local, specs, Collections.emptyMap(),
1418 					       fetch);
1419 	}
1420 
1421 	/**
1422 	 * Convert push remote refs update specification from
1423 	 * {@link org.eclipse.jgit.transport.RefSpec} form to
1424 	 * {@link org.eclipse.jgit.transport.RemoteRefUpdate}. Conversion expands
1425 	 * wildcards by matching source part to local refs. expectedOldObjectId in
1426 	 * RemoteRefUpdate is set according to leases. Tracking branch is configured
1427 	 * if RefSpec destination matches source of any fetch ref spec for this
1428 	 * transport remote configuration.
1429 	 * <p>
1430 	 * Conversion is performed for context of this transport (database, fetch
1431 	 * specifications).
1432 	 *
1433 	 * @param specs
1434 	 *            collection of RefSpec to convert.
1435 	 * @param leases
1436 	 *            map from ref to lease (containing expected old object id)
1437 	 * @return collection of set up
1438 	 *         {@link org.eclipse.jgit.transport.RemoteRefUpdate}.
1439 	 * @throws java.io.IOException
1440 	 *             when problem occurred during conversion or specification set
1441 	 *             up: most probably, missing objects or refs.
1442 	 * @since 4.7
1443 	 */
1444 	public Collection<RemoteRefUpdate> findRemoteRefUpdatesFor(
1445 			final Collection<RefSpec> specs,
1446 			final Map<String, RefLeaseSpec> leases) throws IOException {
1447 		return findRemoteRefUpdatesFor(local, specs, leases,
1448 					       fetch);
1449 	}
1450 
1451 	/**
1452 	 * Begins a new connection for fetching from the remote repository.
1453 	 * <p>
1454 	 * If the transport has no local repository, the fetch connection can only
1455 	 * be used for reading remote refs.
1456 	 *
1457 	 * @return a fresh connection to fetch from the remote repository.
1458 	 * @throws org.eclipse.jgit.errors.NotSupportedException
1459 	 *             the implementation does not support fetching.
1460 	 * @throws org.eclipse.jgit.errors.TransportException
1461 	 *             the remote connection could not be established.
1462 	 */
1463 	public abstract FetchConnection openFetch() throws NotSupportedException,
1464 			TransportException;
1465 
1466 	/**
1467 	 * Begins a new connection for pushing into the remote repository.
1468 	 *
1469 	 * @return a fresh connection to push into the remote repository.
1470 	 * @throws org.eclipse.jgit.errors.NotSupportedException
1471 	 *             the implementation does not support pushing.
1472 	 * @throws org.eclipse.jgit.errors.TransportException
1473 	 *             the remote connection could not be established
1474 	 */
1475 	public abstract PushConnection openPush() throws NotSupportedException,
1476 			TransportException;
1477 
1478 	/**
1479 	 * {@inheritDoc}
1480 	 * <p>
1481 	 * Close any resources used by this transport.
1482 	 * <p>
1483 	 * If the remote repository is contacted by a network socket this method
1484 	 * must close that network socket, disconnecting the two peers. If the
1485 	 * remote repository is actually local (same system) this method must close
1486 	 * any open file handles used to read the "remote" repository.
1487 	 * <p>
1488 	 * {@code AutoClosable.close()} declares that it throws {@link Exception}.
1489 	 * Implementers shouldn't throw checked exceptions. This override narrows
1490 	 * the signature to prevent them from doing so.
1491 	 */
1492 	@Override
1493 	public abstract void close();
1494 }