Transport* - general support for push() and implementations
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / transport / Transport.java
blob0e73a11fba5b16bf074d539cb49db833696feedd
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
10 * conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * - Neither the name of the Git Development Community nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
23 * written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 package org.spearce.jgit.transport;
42 import java.net.URISyntaxException;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.HashSet;
47 import java.util.LinkedList;
48 import java.util.List;
49 import java.util.Map;
51 import org.spearce.jgit.errors.NotSupportedException;
52 import org.spearce.jgit.errors.TransportException;
53 import org.spearce.jgit.lib.NullProgressMonitor;
54 import org.spearce.jgit.lib.ProgressMonitor;
55 import org.spearce.jgit.lib.Ref;
56 import org.spearce.jgit.lib.Repository;
58 /**
59 * Connects two Git repositories together and copies objects between them.
60 * <p>
61 * A transport can be used for either fetching (copying objects into the
62 * caller's repository from the remote repository) or pushing (copying objects
63 * into the remote repository from the caller's repository). Each transport
64 * implementation is responsible for the details associated with establishing
65 * the network connection(s) necessary for the copy, as well as actually
66 * shuffling data back and forth.
67 * <p>
68 * Transport instances and the connections they create are not thread-safe.
69 * Callers must ensure a transport is accessed by only one thread at a time.
71 public abstract class Transport {
72 /**
73 * Open a new transport instance to connect two repositories.
75 * @param local
76 * existing local repository.
77 * @param remote
78 * location of the remote repository.
79 * @return the new transport instance. Never null.
80 * @throws URISyntaxException
81 * the location is not a remote defined in the configuration
82 * file and is not a well-formed URL.
83 * @throws NotSupportedException
84 * the protocol specified is not supported.
86 public static Transport open(final Repository local, final String remote)
87 throws NotSupportedException, URISyntaxException {
88 final RemoteConfig cfg = new RemoteConfig(local.getConfig(), remote);
89 final List<URIish> uris = cfg.getURIs();
90 if (uris.size() == 0)
91 return open(local, new URIish(remote));
92 return open(local, cfg);
95 /**
96 * Open a new transport instance to connect two repositories.
98 * @param local
99 * existing local repository.
100 * @param cfg
101 * configuration describing how to connect to the remote
102 * repository.
103 * @return the new transport instance. Never null.
104 * @throws NotSupportedException
105 * the protocol specified is not supported.
107 public static Transport open(final Repository local, final RemoteConfig cfg)
108 throws NotSupportedException {
109 final Transport tn = open(local, cfg.getURIs().get(0));
110 tn.setOptionUploadPack(cfg.getUploadPack());
111 tn.fetch = cfg.getFetchRefSpecs();
112 tn.tagopt = cfg.getTagOpt();
113 tn.setOptionReceivePack(cfg.getReceivePack());
114 tn.push = cfg.getPushRefSpecs();
115 return tn;
119 * Open a new transport instance to connect two repositories.
121 * @param local
122 * existing local repository.
123 * @param remote
124 * location of the remote repository.
125 * @return the new transport instance. Never null.
126 * @throws NotSupportedException
127 * the protocol specified is not supported.
129 public static Transport open(final Repository local, final URIish remote)
130 throws NotSupportedException {
131 if (TransportGitSsh.canHandle(remote))
132 return new TransportGitSsh(local, remote);
134 else if (TransportHttp.canHandle(remote))
135 return new TransportHttp(local, remote);
137 else if (TransportSftp.canHandle(remote))
138 return new TransportSftp(local, remote);
140 else if (TransportGitAnon.canHandle(remote))
141 return new TransportGitAnon(local, remote);
143 else if (TransportBundle.canHandle(remote))
144 return new TransportBundle(local, remote);
146 else if (TransportLocal.canHandle(remote))
147 return new TransportLocal(local, remote);
149 throw new NotSupportedException("URI not supported: " + remote);
153 * Default setting for {@link #fetchThin} option.
155 public static final boolean DEFAULT_FETCH_THIN = true;
158 * Default setting for {@link #pushThin} option.
160 public static final boolean DEFAULT_PUSH_THIN = false;
163 * Specification for fetch or push operations, to fetch or push all tags.
164 * Acts as --tags.
166 public static final RefSpec REFSPEC_TAGS = new RefSpec(
167 "refs/tags/*:refs/tags/*");
170 * Specification for push operation, to push all refs under refs/heads. Acts
171 * as --all.
173 public static final RefSpec REFSPEC_PUSH_ALL = new RefSpec(
174 "refs/heads/*:refs/heads/*");
176 /** The repository this transport fetches into, or pushes out of. */
177 protected final Repository local;
179 /** The URI used to create this transport. */
180 protected final URIish uri;
182 /** Name of the upload pack program, if it must be executed. */
183 private String optionUploadPack = RemoteConfig.DEFAULT_UPLOAD_PACK;
185 /** Specifications to apply during fetch. */
186 private List<RefSpec> fetch = Collections.emptyList();
189 * How {@link #fetch(ProgressMonitor, Collection)} should handle tags.
190 * <p>
191 * We default to {@link TagOpt#NO_TAGS} so as to avoid fetching annotated
192 * tags during one-shot fetches used for later merges. This prevents
193 * dragging down tags from repositories that we do not have established
194 * tracking branches for. If we do not track the source repository, we most
195 * likely do not care about any tags it publishes.
197 private TagOpt tagopt = TagOpt.NO_TAGS;
199 /** Should fetch request thin-pack if remote repository can produce it. */
200 private boolean fetchThin = DEFAULT_FETCH_THIN;
202 /** Name of the receive pack program, if it must be executed. */
203 private String optionReceivePack = RemoteConfig.DEFAULT_RECEIVE_PACK;
205 /** Specifications to apply during push. */
206 private List<RefSpec> push = Collections.emptyList();
208 /** Should push produce thin-pack when sending objects to remote repository. */
209 private boolean pushThin = DEFAULT_PUSH_THIN;
212 * Create a new transport instance.
214 * @param local
215 * the repository this instance will fetch into, or push out of.
216 * This must be the repository passed to
217 * {@link #open(Repository, URIish)}.
218 * @param uri
219 * the URI used to access the remote repository. This must be the
220 * URI passed to {@link #open(Repository, URIish)}.
222 protected Transport(final Repository local, final URIish uri) {
223 this.local = local;
224 this.uri = uri;
228 * Get the URI this transport connects to.
229 * <p>
230 * Each transport instance connects to at most one URI at any point in time.
232 * @return the URI describing the location of the remote repository.
234 public URIish getURI() {
235 return uri;
239 * Get the name of the remote executable providing upload-pack service.
241 * @return typically "git-upload-pack".
243 public String getOptionUploadPack() {
244 return optionUploadPack;
248 * Set the name of the remote executable providing upload-pack services.
250 * @param where
251 * name of the executable.
253 public void setOptionUploadPack(final String where) {
254 if (where != null && where.length() > 0)
255 optionUploadPack = where;
256 else
257 optionUploadPack = RemoteConfig.DEFAULT_UPLOAD_PACK;
261 * Get the description of how annotated tags should be treated during fetch.
263 * @return option indicating the behavior of annotated tags in fetch.
265 public TagOpt getTagOpt() {
266 return tagopt;
270 * Set the description of how annotated tags should be treated on fetch.
272 * @param option
273 * method to use when handling annotated tags.
275 public void setTagOpt(final TagOpt option) {
276 tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;
280 * Default setting is: {@link #DEFAULT_FETCH_THIN}
282 * @return true if fetch should request thin-pack when possible; false
283 * otherwise
284 * @see PackTransport
286 public boolean isFetchThin() {
287 return fetchThin;
291 * Set the thin-pack preference for fetch operation. Default setting is:
292 * {@link #DEFAULT_FETCH_THIN}
294 * @param fetchThin
295 * true when fetch should request thin-pack when possible; false
296 * when it shouldn't
297 * @see PackTransport
299 public void setFetchThin(final boolean fetchThin) {
300 this.fetchThin = fetchThin;
304 * Default setting is: {@value RemoteConfig#DEFAULT_RECEIVE_PACK}
306 * @return remote executable providing receive-pack service for pack
307 * transports.
308 * @see PackTransport
310 public String getOptionReceivePack() {
311 return optionReceivePack;
315 * Set remote executable providing receive-pack service for pack transports.
316 * Default setting is: {@value RemoteConfig#DEFAULT_RECEIVE_PACK}
318 * @param optionReceivePack
319 * remote executable, if null or empty default one is set;
321 public void setOptionReceivePack(String optionReceivePack) {
322 if (optionReceivePack != null && optionReceivePack.length() > 0)
323 this.optionReceivePack = optionReceivePack;
324 else
325 this.optionReceivePack = RemoteConfig.DEFAULT_RECEIVE_PACK;
329 * Default setting is: {@value #DEFAULT_PUSH_THIN}
331 * @return true if push should produce thin-pack in pack transports
332 * @see PackTransport
334 public boolean isPushThin() {
335 return pushThin;
339 * Set thin-pack preference for push operation. Default setting is:
340 * {@value #DEFAULT_PUSH_THIN}
342 * @param pushThin
343 * true when push should produce thin-pack in pack transports;
344 * false when it shouldn't
345 * @see PackTransport
347 public void setPushThin(final boolean pushThin) {
348 this.pushThin = pushThin;
352 * Fetch objects and refs from the remote repository to the local one.
353 * <p>
354 * This is a utility function providing standard fetch behavior. Local
355 * tracking refs associated with the remote repository are automatically
356 * updated if this transport was created from a {@link RemoteConfig} with
357 * fetch RefSpecs defined.
359 * @param monitor
360 * progress monitor to inform the user about our processing
361 * activity. Must not be null. Use {@link NullProgressMonitor} if
362 * progress updates are not interesting or necessary.
363 * @param toFetch
364 * specification of refs to fetch locally. May be null or the
365 * empty collection to use the specifications from the
366 * RemoteConfig.
367 * @return information describing the tracking refs updated.
368 * @throws NotSupportedException
369 * this transport implementation does not support fetching
370 * objects.
371 * @throws TransportException
372 * the remote connection could not be established or object
373 * copying (if necessary) failed.
375 public FetchResult fetch(final ProgressMonitor monitor,
376 Collection<RefSpec> toFetch) throws NotSupportedException,
377 TransportException {
378 if (toFetch == null || toFetch.isEmpty()) {
379 // If the caller did not ask for anything use the defaults.
381 if (fetch.isEmpty())
382 throw new TransportException("Nothing to fetch.");
383 toFetch = fetch;
384 } else if (!fetch.isEmpty()) {
385 // If the caller asked for something specific without giving
386 // us the local tracking branch see if we can update any of
387 // the local tracking branches without incurring additional
388 // object transfer overheads.
390 final Collection<RefSpec> tmp = new ArrayList<RefSpec>(toFetch);
391 for (final RefSpec requested : toFetch) {
392 final String reqSrc = requested.getSource();
393 for (final RefSpec configured : fetch) {
394 final String cfgSrc = configured.getSource();
395 final String cfgDst = configured.getDestination();
396 if (cfgSrc.equals(reqSrc) && cfgDst != null) {
397 tmp.add(configured);
398 break;
402 toFetch = tmp;
405 final FetchResult result = new FetchResult();
406 new FetchProcess(this, toFetch, tagopt).execute(monitor, result);
407 return result;
411 * Push objects and refs from the local repository to the remote one.
412 * <p>
413 * This is a utility function providing standard push behavior. It updates
414 * remote refs and send there necessary objects according to remote ref
415 * update specification. After successful remote ref update, associated
416 * locally stored tracking branch is updated if set up accordingly. Detailed
417 * operation result is provided after execution.
418 * <p>
419 * For setting up remote ref update specification from ref spec, see helper
420 * method {@link #findRemoteRefUpdatesFor(Collection)}, predefined refspecs ({@link #REFSPEC_TAGS},
421 * {@link #REFSPEC_PUSH_ALL}) or consider using directly
422 * {@link RemoteRefUpdate} for more possibilities.
424 * @see RemoteRefUpdate
426 * @param monitor
427 * progress monitor to inform the user about our processing
428 * activity. Must not be null. Use {@link NullProgressMonitor} if
429 * progress updates are not interesting or necessary.
430 * @param toPush
431 * specification of refs to push. May be null or the empty
432 * collection to use the specifications from the RemoteConfig
433 * converted by {@link #findRemoteRefUpdatesFor(Collection)}. No
434 * more than 1 RemoteRefUpdate with the same remoteName is
435 * allowed.
436 * @return information about results of remote refs updates, tracking refs
437 * updates and refs advertised by remote repository.
438 * @throws NotSupportedException
439 * this transport implementation does not support pusing
440 * objects.
441 * @throws TransportException
442 * the remote connection could not be established or object
443 * copying (if necessary) failed at I/O or protocol level or
444 * update specification was incorrect.
446 public PushResult push(final ProgressMonitor monitor,
447 Collection<RemoteRefUpdate> toPush) throws NotSupportedException,
448 TransportException {
449 if (toPush == null || toPush.isEmpty()) {
450 // If the caller did not ask for anything use the defaults.
451 toPush = findRemoteRefUpdatesFor(push);
452 if (toPush.isEmpty())
453 throw new TransportException("Nothing to push.");
455 final PushProcess pushProcess = new PushProcess(this, toPush);
456 return pushProcess.execute(monitor);
460 * Convert push remote refs update specification from {@link RefSpec} form
461 * to {@link RemoteRefUpdate}. Conversion expands wildcards by matching
462 * source part to local refs. expectedOldObjectId in RemoteRefUpdate is
463 * always set as null. Tracking branch is configured if RefSpec destination
464 * matches source of any fetch ref spec for this transport remote
465 * configuration.
467 * @param specs
468 * collection of RefSpec to convert.
469 * @return collection of set up {@link RemoteRefUpdate}.
470 * @throws TransportException
471 * when problem occurred during conversion or specification set
472 * up: most probably, missing objects or refs.
474 public Collection<RemoteRefUpdate> findRemoteRefUpdatesFor(
475 final Collection<RefSpec> specs) throws TransportException {
476 final List<RemoteRefUpdate> result = new LinkedList<RemoteRefUpdate>();
477 final Collection<RefSpec> procRefs = expandPushWildcardsFor(specs);
479 for (final RefSpec spec : procRefs) {
480 try {
481 final String srcRef = spec.getSource();
482 // null destination (no-colon in ref-spec) is a special case
483 final String remoteName = (spec.getDestination() == null ? spec
484 .getSource() : spec.getDestination());
485 final boolean forceUpdate = spec.isForceUpdate();
486 final String localName = findTrackingRefName(remoteName);
488 final RemoteRefUpdate rru = new RemoteRefUpdate(local, srcRef,
489 remoteName, forceUpdate, localName, null);
490 result.add(rru);
491 } catch (TransportException x) {
492 throw x;
493 } catch (Exception x) {
494 throw new TransportException(
495 "Problem with resolving push ref spec \"" + spec
496 + "\" locally: " + x.getMessage(), x);
499 return result;
503 * Begins a new connection for fetching from the remote repository.
505 * @return a fresh connection to fetch from the remote repository.
506 * @throws NotSupportedException
507 * the implementation does not support fetching.
508 * @throws TransportException
509 * the remote connection could not be established.
511 public abstract FetchConnection openFetch() throws NotSupportedException,
512 TransportException;
515 * Begins a new connection for pushing into the remote repository.
517 * @return a fresh connection to push into the remote repository.
518 * @throws NotSupportedException
519 * the implementation does not support pushing.
520 * @throws TransportException
521 * the remote connection could not be established
523 public abstract PushConnection openPush() throws NotSupportedException,
524 TransportException;
526 private Collection<RefSpec> expandPushWildcardsFor(
527 final Collection<RefSpec> specs) {
528 final Map<String, Ref> localRefs = local.getAllRefs();
529 final Collection<RefSpec> procRefs = new HashSet<RefSpec>();
531 for (final RefSpec spec : specs) {
532 if (spec.isWildcard()) {
533 for (final Ref localRef : localRefs.values()) {
534 if (spec.matchSource(localRef))
535 procRefs.add(spec.expandFromSource(localRef));
537 } else {
538 procRefs.add(spec);
541 return procRefs;
544 private String findTrackingRefName(final String remoteName) {
545 // try to find matching tracking refs
546 for (final RefSpec fetchSpec : fetch) {
547 if (fetchSpec.matchSource(remoteName)) {
548 if (fetchSpec.isWildcard())
549 return fetchSpec.expandFromSource(remoteName)
550 .getDestination();
551 else
552 return fetchSpec.getDestination();
555 return null;