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 java.nio.charset.StandardCharsets.UTF_8; 50 import static java.util.Objects.requireNonNull; 51 52 import java.io.BufferedReader; 53 import java.io.IOException; 54 import java.io.InputStreamReader; 55 import java.io.OutputStream; 56 import java.io.PrintStream; 57 import java.lang.ref.WeakReference; 58 import java.lang.reflect.Field; 59 import java.lang.reflect.Modifier; 60 import java.net.URISyntaxException; 61 import java.net.URL; 62 import java.text.MessageFormat; 63 import java.util.ArrayList; 64 import java.util.Collection; 65 import java.util.Collections; 66 import java.util.Enumeration; 67 import java.util.LinkedHashSet; 68 import java.util.LinkedList; 69 import java.util.List; 70 import java.util.Map; 71 import java.util.Vector; 72 import java.util.concurrent.CopyOnWriteArrayList; 73 74 import org.eclipse.jgit.annotations.NonNull; 75 import org.eclipse.jgit.api.errors.AbortedByHookException; 76 import org.eclipse.jgit.errors.NotSupportedException; 77 import org.eclipse.jgit.errors.TransportException; 78 import org.eclipse.jgit.hooks.Hooks; 79 import org.eclipse.jgit.hooks.PrePushHook; 80 import org.eclipse.jgit.internal.JGitText; 81 import org.eclipse.jgit.lib.Constants; 82 import org.eclipse.jgit.lib.ObjectChecker; 83 import org.eclipse.jgit.lib.ObjectId; 84 import org.eclipse.jgit.lib.ProgressMonitor; 85 import org.eclipse.jgit.lib.Ref; 86 import org.eclipse.jgit.lib.Repository; 87 import org.eclipse.jgit.storage.pack.PackConfig; 88 89 /** 90 * Connects two Git repositories together and copies objects between them. 91 * <p> 92 * A transport can be used for either fetching (copying objects into the 93 * caller's repository from the remote repository) or pushing (copying objects 94 * into the remote repository from the caller's repository). Each transport 95 * implementation is responsible for the details associated with establishing 96 * the network connection(s) necessary for the copy, as well as actually 97 * shuffling data back and forth. 98 * <p> 99 * Transport instances and the connections they create are not thread-safe. 100 * Callers must ensure a transport is accessed by only one thread at a time. 101 */ 102 public abstract class Transport implements AutoCloseable { 103 /** Type of operation a Transport is being opened for. */ 104 public enum Operation { 105 /** Transport is to fetch objects locally. */ 106 FETCH, 107 /** Transport is to push objects remotely. */ 108 PUSH; 109 } 110 111 private static final List<WeakReference<TransportProtocol>> protocols = 112 new CopyOnWriteArrayList<>(); 113 114 static { 115 // Registration goes backwards in order of priority. 116 register(TransportLocal.PROTO_LOCAL); 117 register(TransportBundleFile.PROTO_BUNDLE); 118 register(TransportAmazonS3.PROTO_S3); 119 register(TransportGitAnon.PROTO_GIT); 120 register(TransportSftp.PROTO_SFTP); 121 register(TransportHttp.PROTO_FTP); 122 register(TransportHttp.PROTO_HTTP); 123 register(TransportGitSsh.PROTO_SSH); 124 125 registerByService(); 126 } 127 128 private static void registerByService() { 129 ClassLoader ldr = Thread.currentThread().getContextClassLoader(); 130 if (ldr == null) 131 ldr = Transport.class.getClassLoader(); 132 Enumeration<URL> catalogs = catalogs(ldr); 133 while (catalogs.hasMoreElements()) 134 scan(ldr, catalogs.nextElement()); 135 } 136 137 private static Enumeration<URL> catalogs(ClassLoader ldr) { 138 try { 139 String prefix = "META-INF/services/"; //$NON-NLS-1$ 140 String name = prefix + Transport.class.getName(); 141 return ldr.getResources(name); 142 } catch (IOException err) { 143 return new Vector<URL>().elements(); 144 } 145 } 146 147 private static void scan(ClassLoader ldr, URL url) { 148 try (BufferedReader br = new BufferedReader( 149 new InputStreamReader(url.openStream(), UTF_8))) { 150 String line; 151 while ((line = br.readLine()) != null) { 152 line = line.trim(); 153 if (line.length() == 0) 154 continue; 155 int comment = line.indexOf('#'); 156 if (comment == 0) 157 continue; 158 if (comment != -1) 159 line = line.substring(0, comment).trim(); 160 load(ldr, line); 161 } 162 } catch (IOException e) { 163 // Ignore errors 164 } 165 } 166 167 private static void load(ClassLoader ldr, String cn) { 168 Class<?> clazz; 169 try { 170 clazz = Class.forName(cn, false, ldr); 171 } catch (ClassNotFoundException notBuiltin) { 172 // Doesn't exist, even though the service entry is present. 173 // 174 return; 175 } 176 177 for (Field f : clazz.getDeclaredFields()) { 178 if ((f.getModifiers() & Modifier.STATIC) == Modifier.STATIC 179 && TransportProtocol.class.isAssignableFrom(f.getType())) { 180 TransportProtocol proto; 181 try { 182 proto = (TransportProtocol) f.get(null); 183 } catch (IllegalArgumentException | IllegalAccessException e) { 184 // If we cannot access the field, don't. 185 continue; 186 } 187 if (proto != null) 188 register(proto); 189 } 190 } 191 } 192 193 /** 194 * Register a TransportProtocol instance for use during open. 195 * <p> 196 * Protocol definitions are held by WeakReference, allowing them to be 197 * garbage collected when the calling application drops all strongly held 198 * references to the TransportProtocol. Therefore applications should use a 199 * singleton pattern as described in 200 * {@link org.eclipse.jgit.transport.TransportProtocol}'s class 201 * documentation to ensure their protocol does not get disabled by garbage 202 * collection earlier than expected. 203 * <p> 204 * The new protocol is registered in front of all earlier protocols, giving 205 * it higher priority than the built-in protocol definitions. 206 * 207 * @param proto 208 * the protocol definition. Must not be null. 209 */ 210 public static void register(TransportProtocol proto) { 211 protocols.add(0, new WeakReference<>(proto)); 212 } 213 214 /** 215 * Unregister a TransportProtocol instance. 216 * <p> 217 * Unregistering a protocol usually isn't necessary, as protocols are held 218 * by weak references and will automatically clear when they are garbage 219 * collected by the JVM. Matching is handled by reference equality, so the 220 * exact reference given to {@link #register(TransportProtocol)} must be 221 * used. 222 * 223 * @param proto 224 * the exact object previously given to register. 225 */ 226 public static void unregister(TransportProtocol proto) { 227 for (WeakReference<TransportProtocol> ref : protocols) { 228 TransportProtocol refProto = ref.get(); 229 if (refProto == null || refProto == proto) 230 protocols.remove(ref); 231 } 232 } 233 234 /** 235 * Obtain a copy of the registered protocols. 236 * 237 * @return an immutable copy of the currently registered protocols. 238 */ 239 public static List<TransportProtocol> getTransportProtocols() { 240 int cnt = protocols.size(); 241 List<TransportProtocol> res = new ArrayList<>(cnt); 242 for (WeakReference<TransportProtocol> ref : protocols) { 243 TransportProtocol proto = ref.get(); 244 if (proto != null) 245 res.add(proto); 246 else 247 protocols.remove(ref); 248 } 249 return Collections.unmodifiableList(res); 250 } 251 252 /** 253 * Open a new transport instance to connect two repositories. 254 * <p> 255 * This method assumes 256 * {@link org.eclipse.jgit.transport.Transport.Operation#FETCH}. 257 * 258 * @param local 259 * existing local repository. 260 * @param remote 261 * location of the remote repository - may be URI or remote 262 * configuration name. 263 * @return the new transport instance. Never null. In case of multiple URIs 264 * in remote configuration, only the first is chosen. 265 * @throws java.net.URISyntaxException 266 * the location is not a remote defined in the configuration 267 * file and is not a well-formed URL. 268 * @throws org.eclipse.jgit.errors.NotSupportedException 269 * the protocol specified is not supported. 270 * @throws org.eclipse.jgit.errors.TransportException 271 * the transport cannot open this URI. 272 */ 273 public static Transport open(Repository local, String remote) 274 throws NotSupportedException, URISyntaxException, 275 TransportException { 276 return open(local, remote, Operation.FETCH); 277 } 278 279 /** 280 * Open a new transport instance to connect two repositories. 281 * 282 * @param local 283 * existing local repository. 284 * @param remote 285 * location of the remote repository - may be URI or remote 286 * configuration name. 287 * @param op 288 * planned use of the returned Transport; the URI may differ 289 * based on the type of connection desired. 290 * @return the new transport instance. Never null. In case of multiple URIs 291 * in remote configuration, only the first is chosen. 292 * @throws java.net.URISyntaxException 293 * the location is not a remote defined in the configuration 294 * file and is not a well-formed URL. 295 * @throws org.eclipse.jgit.errors.NotSupportedException 296 * the protocol specified is not supported. 297 * @throws org.eclipse.jgit.errors.TransportException 298 * the transport cannot open this URI. 299 */ 300 public static Transport open(final Repository local, final String remote, 301 final Operation op) throws NotSupportedException, 302 URISyntaxException, TransportException { 303 if (local != null) { 304 final RemoteConfigteConfig.html#RemoteConfig">RemoteConfig cfg = new RemoteConfig(local.getConfig(), remote); 305 if (doesNotExist(cfg)) { 306 return open(local, new URIish(remote), null); 307 } 308 return open(local, cfg, op); 309 } 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 RemoteConfigteConfig.html#RemoteConfig">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 RemoteRefUpdateefUpdate.html#RemoteRefUpdate">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 } 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 RefSpechtml#RefSpec">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">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 FilterSpec filterSpec = FilterSpec.NO_FILTER; 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 blob limit value set with {@link #setFilterBlobLimit} or 1071 * {@link #setFilterSpec(FilterSpec)}, or -1 if no blob limit value 1072 * was set 1073 * @since 5.0 1074 * @deprecated Use {@link #getFilterSpec()} instead 1075 */ 1076 @Deprecated 1077 public final long getFilterBlobLimit() { 1078 return filterSpec.getBlobLimit(); 1079 } 1080 1081 /** 1082 * @param bytes exclude blobs of size greater than this 1083 * @since 5.0 1084 * @deprecated Use {@link #setFilterSpec(FilterSpec)} instead 1085 */ 1086 @Deprecated 1087 public final void setFilterBlobLimit(long bytes) { 1088 setFilterSpec(FilterSpec.withBlobLimit(bytes)); 1089 } 1090 1091 /** 1092 * @return the last filter spec set with {@link #setFilterSpec(FilterSpec)}, 1093 * or {@link FilterSpec#NO_FILTER} if it was never invoked. 1094 * @since 5.4 1095 */ 1096 public final FilterSpec getFilterSpec() { 1097 return filterSpec; 1098 } 1099 1100 /** 1101 * @param filter a new filter to use for this transport 1102 * @since 5.4 1103 */ 1104 public final void setFilterSpec(@NonNull FilterSpec filter) { 1105 filterSpec = requireNonNull(filter); 1106 } 1107 1108 /** 1109 * Apply provided remote configuration on this transport. 1110 * 1111 * @param cfg 1112 * configuration to apply on this transport. 1113 */ 1114 public void applyConfig(RemoteConfig cfg) { 1115 setOptionUploadPack(cfg.getUploadPack()); 1116 setOptionReceivePack(cfg.getReceivePack()); 1117 setTagOpt(cfg.getTagOpt()); 1118 fetch = cfg.getFetchRefSpecs(); 1119 push = cfg.getPushRefSpecs(); 1120 timeout = cfg.getTimeout(); 1121 } 1122 1123 /** 1124 * Whether push operation should just check for possible result and not 1125 * really update remote refs 1126 * 1127 * @return true if push operation should just check for possible result and 1128 * not really update remote refs, false otherwise - when push should 1129 * act normally. 1130 */ 1131 public boolean isDryRun() { 1132 return dryRun; 1133 } 1134 1135 /** 1136 * Set dry run option for push operation. 1137 * 1138 * @param dryRun 1139 * true if push operation should just check for possible result 1140 * and not really update remote refs, false otherwise - when push 1141 * should act normally. 1142 */ 1143 public void setDryRun(boolean dryRun) { 1144 this.dryRun = dryRun; 1145 } 1146 1147 /** 1148 * Get timeout (in seconds) before aborting an IO operation. 1149 * 1150 * @return timeout (in seconds) before aborting an IO operation. 1151 */ 1152 public int getTimeout() { 1153 return timeout; 1154 } 1155 1156 /** 1157 * Set the timeout before willing to abort an IO call. 1158 * 1159 * @param seconds 1160 * number of seconds to wait (with no data transfer occurring) 1161 * before aborting an IO read or write operation with this 1162 * remote. 1163 */ 1164 public void setTimeout(int seconds) { 1165 timeout = seconds; 1166 } 1167 1168 /** 1169 * Get the configuration used by the pack generator to make packs. 1170 * 1171 * If {@link #setPackConfig(PackConfig)} was previously given null a new 1172 * PackConfig is created on demand by this method using the source 1173 * repository's settings. 1174 * 1175 * @return the pack configuration. Never null. 1176 */ 1177 public PackConfig getPackConfig() { 1178 if (packConfig == null) 1179 packConfig = new PackConfig(local); 1180 return packConfig; 1181 } 1182 1183 /** 1184 * Set the configuration used by the pack generator. 1185 * 1186 * @param pc 1187 * configuration controlling packing parameters. If null the 1188 * source repository's settings will be used. 1189 */ 1190 public void setPackConfig(PackConfig pc) { 1191 packConfig = pc; 1192 } 1193 1194 /** 1195 * A credentials provider to assist with authentication connections.. 1196 * 1197 * @param credentialsProvider 1198 * the credentials provider, or null if there is none 1199 */ 1200 public void setCredentialsProvider(CredentialsProvider credentialsProvider) { 1201 this.credentialsProvider = credentialsProvider; 1202 } 1203 1204 /** 1205 * The configured credentials provider. 1206 * 1207 * @return the credentials provider, or null if no credentials provider is 1208 * associated with this transport. 1209 */ 1210 public CredentialsProvider getCredentialsProvider() { 1211 return credentialsProvider; 1212 } 1213 1214 /** 1215 * Get the option strings associated with the push operation 1216 * 1217 * @return the option strings associated with the push operation 1218 * @since 4.5 1219 */ 1220 public List<String> getPushOptions() { 1221 return pushOptions; 1222 } 1223 1224 /** 1225 * Sets the option strings associated with the push operation. 1226 * 1227 * @param pushOptions 1228 * null if push options are unsupported 1229 * @since 4.5 1230 */ 1231 public void setPushOptions(List<String> pushOptions) { 1232 this.pushOptions = pushOptions; 1233 } 1234 1235 /** 1236 * Fetch objects and refs from the remote repository to the local one. 1237 * <p> 1238 * This is a utility function providing standard fetch behavior. Local 1239 * tracking refs associated with the remote repository are automatically 1240 * updated if this transport was created from a 1241 * {@link org.eclipse.jgit.transport.RemoteConfig} with fetch RefSpecs 1242 * defined. 1243 * 1244 * @param monitor 1245 * progress monitor to inform the user about our processing 1246 * activity. Must not be null. Use 1247 * {@link org.eclipse.jgit.lib.NullProgressMonitor} if progress 1248 * updates are not interesting or necessary. 1249 * @param toFetch 1250 * specification of refs to fetch locally. May be null or the 1251 * empty collection to use the specifications from the 1252 * RemoteConfig. Source for each RefSpec can't be null. 1253 * @return information describing the tracking refs updated. 1254 * @throws org.eclipse.jgit.errors.NotSupportedException 1255 * this transport implementation does not support fetching 1256 * objects. 1257 * @throws org.eclipse.jgit.errors.TransportException 1258 * the remote connection could not be established or object 1259 * copying (if necessary) failed or update specification was 1260 * incorrect. 1261 */ 1262 public FetchResult fetch(final ProgressMonitor monitor, 1263 Collection<RefSpec> toFetch) throws NotSupportedException, 1264 TransportException { 1265 if (toFetch == null || toFetch.isEmpty()) { 1266 // If the caller did not ask for anything use the defaults. 1267 // 1268 if (fetch.isEmpty()) 1269 throw new TransportException(JGitText.get().nothingToFetch); 1270 toFetch = fetch; 1271 } else if (!fetch.isEmpty()) { 1272 // If the caller asked for something specific without giving 1273 // us the local tracking branch see if we can update any of 1274 // the local tracking branches without incurring additional 1275 // object transfer overheads. 1276 // 1277 final Collection<RefSpec> tmp = new ArrayList<>(toFetch); 1278 for (RefSpec requested : toFetch) { 1279 final String reqSrc = requested.getSource(); 1280 for (RefSpec configured : fetch) { 1281 final String cfgSrc = configured.getSource(); 1282 final String cfgDst = configured.getDestination(); 1283 if (cfgSrc.equals(reqSrc) && cfgDst != null) { 1284 tmp.add(configured); 1285 break; 1286 } 1287 } 1288 } 1289 toFetch = tmp; 1290 } 1291 1292 final FetchResultesult.html#FetchResult">FetchResult result = new FetchResult(); 1293 new FetchProcess(this, toFetch).execute(monitor, result); 1294 1295 local.autoGC(monitor); 1296 1297 return result; 1298 } 1299 1300 /** 1301 * Push objects and refs from the local repository to the remote one. 1302 * <p> 1303 * This is a utility function providing standard push behavior. It updates 1304 * remote refs and send there necessary objects according to remote ref 1305 * update specification. After successful remote ref update, associated 1306 * locally stored tracking branch is updated if set up accordingly. Detailed 1307 * operation result is provided after execution. 1308 * <p> 1309 * For setting up remote ref update specification from ref spec, see helper 1310 * method {@link #findRemoteRefUpdatesFor(Collection)}, predefined refspecs 1311 * ({@link #REFSPEC_TAGS}, {@link #REFSPEC_PUSH_ALL}) or consider using 1312 * directly {@link org.eclipse.jgit.transport.RemoteRefUpdate} for more 1313 * possibilities. 1314 * <p> 1315 * When {@link #isDryRun()} is true, result of this operation is just 1316 * estimation of real operation result, no real action is performed. 1317 * 1318 * @see RemoteRefUpdate 1319 * @param monitor 1320 * progress monitor to inform the user about our processing 1321 * activity. Must not be null. Use 1322 * {@link org.eclipse.jgit.lib.NullProgressMonitor} if progress 1323 * updates are not interesting or necessary. 1324 * @param toPush 1325 * specification of refs to push. May be null or the empty 1326 * collection to use the specifications from the RemoteConfig 1327 * converted by {@link #findRemoteRefUpdatesFor(Collection)}. No 1328 * more than 1 RemoteRefUpdate with the same remoteName is 1329 * allowed. These objects are modified during this call. 1330 * @param out 1331 * output stream to write messages to 1332 * @return information about results of remote refs updates, tracking refs 1333 * updates and refs advertised by remote repository. 1334 * @throws org.eclipse.jgit.errors.NotSupportedException 1335 * this transport implementation does not support pushing 1336 * objects. 1337 * @throws org.eclipse.jgit.errors.TransportException 1338 * the remote connection could not be established or object 1339 * copying (if necessary) failed at I/O or protocol level or 1340 * update specification was incorrect. 1341 * @since 3.0 1342 */ 1343 public PushResult push(final ProgressMonitor monitor, 1344 Collection<RemoteRefUpdate> toPush, OutputStream out) 1345 throws NotSupportedException, 1346 TransportException { 1347 if (toPush == null || toPush.isEmpty()) { 1348 // If the caller did not ask for anything use the defaults. 1349 try { 1350 toPush = findRemoteRefUpdatesFor(push); 1351 } catch (final IOException e) { 1352 throw new TransportException(MessageFormat.format( 1353 JGitText.get().problemWithResolvingPushRefSpecsLocally, e.getMessage()), e); 1354 } 1355 if (toPush.isEmpty()) 1356 throw new TransportException(JGitText.get().nothingToPush); 1357 } 1358 if (prePush != null) { 1359 try { 1360 prePush.setRefs(toPush); 1361 prePush.call(); 1362 } catch (AbortedByHookException | IOException e) { 1363 throw new TransportException(e.getMessage(), e); 1364 } 1365 } 1366 1367 final PushProcess.html#PushProcess">PushProcess pushProcess = new PushProcess(this, toPush, out); 1368 return pushProcess.execute(monitor); 1369 } 1370 1371 /** 1372 * Push objects and refs from the local repository to the remote one. 1373 * <p> 1374 * This is a utility function providing standard push behavior. It updates 1375 * remote refs and sends necessary objects according to remote ref update 1376 * specification. After successful remote ref update, associated locally 1377 * stored tracking branch is updated if set up accordingly. Detailed 1378 * operation result is provided after execution. 1379 * <p> 1380 * For setting up remote ref update specification from ref spec, see helper 1381 * method {@link #findRemoteRefUpdatesFor(Collection)}, predefined refspecs 1382 * ({@link #REFSPEC_TAGS}, {@link #REFSPEC_PUSH_ALL}) or consider using 1383 * directly {@link org.eclipse.jgit.transport.RemoteRefUpdate} for more 1384 * possibilities. 1385 * <p> 1386 * When {@link #isDryRun()} is true, result of this operation is just 1387 * estimation of real operation result, no real action is performed. 1388 * 1389 * @see RemoteRefUpdate 1390 * @param monitor 1391 * progress monitor to inform the user about our processing 1392 * activity. Must not be null. Use 1393 * {@link org.eclipse.jgit.lib.NullProgressMonitor} if progress 1394 * updates are not interesting or necessary. 1395 * @param toPush 1396 * specification of refs to push. May be null or the empty 1397 * collection to use the specifications from the RemoteConfig 1398 * converted by {@link #findRemoteRefUpdatesFor(Collection)}. No 1399 * more than 1 RemoteRefUpdate with the same remoteName is 1400 * allowed. These objects are modified during this call. 1401 * @return information about results of remote refs updates, tracking refs 1402 * updates and refs advertised by remote repository. 1403 * @throws org.eclipse.jgit.errors.NotSupportedException 1404 * this transport implementation does not support pushing 1405 * objects. 1406 * @throws org.eclipse.jgit.errors.TransportException 1407 * the remote connection could not be established or object 1408 * copying (if necessary) failed at I/O or protocol level or 1409 * update specification was incorrect. 1410 */ 1411 public PushResult push(final ProgressMonitor monitor, 1412 Collection<RemoteRefUpdate> toPush) throws NotSupportedException, 1413 TransportException { 1414 return push(monitor, toPush, null); 1415 } 1416 1417 /** 1418 * Convert push remote refs update specification from 1419 * {@link org.eclipse.jgit.transport.RefSpec} form to 1420 * {@link org.eclipse.jgit.transport.RemoteRefUpdate}. Conversion expands 1421 * wildcards by matching source part to local refs. expectedOldObjectId in 1422 * RemoteRefUpdate is always set as null. Tracking branch is configured if 1423 * RefSpec destination matches source of any fetch ref spec for this 1424 * transport remote configuration. 1425 * <p> 1426 * Conversion is performed for context of this transport (database, fetch 1427 * specifications). 1428 * 1429 * @param specs 1430 * collection of RefSpec to convert. 1431 * @return collection of set up 1432 * {@link org.eclipse.jgit.transport.RemoteRefUpdate}. 1433 * @throws java.io.IOException 1434 * when problem occurred during conversion or specification set 1435 * up: most probably, missing objects or refs. 1436 */ 1437 public Collection<RemoteRefUpdate> findRemoteRefUpdatesFor( 1438 final Collection<RefSpec> specs) throws IOException { 1439 return findRemoteRefUpdatesFor(local, specs, Collections.emptyMap(), 1440 fetch); 1441 } 1442 1443 /** 1444 * Convert push remote refs update specification from 1445 * {@link org.eclipse.jgit.transport.RefSpec} form to 1446 * {@link org.eclipse.jgit.transport.RemoteRefUpdate}. Conversion expands 1447 * wildcards by matching source part to local refs. expectedOldObjectId in 1448 * RemoteRefUpdate is set according to leases. Tracking branch is configured 1449 * if RefSpec destination matches source of any fetch ref spec for this 1450 * transport remote configuration. 1451 * <p> 1452 * Conversion is performed for context of this transport (database, fetch 1453 * specifications). 1454 * 1455 * @param specs 1456 * collection of RefSpec to convert. 1457 * @param leases 1458 * map from ref to lease (containing expected old object id) 1459 * @return collection of set up 1460 * {@link org.eclipse.jgit.transport.RemoteRefUpdate}. 1461 * @throws java.io.IOException 1462 * when problem occurred during conversion or specification set 1463 * up: most probably, missing objects or refs. 1464 * @since 4.7 1465 */ 1466 public Collection<RemoteRefUpdate> findRemoteRefUpdatesFor( 1467 final Collection<RefSpec> specs, 1468 final Map<String, RefLeaseSpec> leases) throws IOException { 1469 return findRemoteRefUpdatesFor(local, specs, leases, 1470 fetch); 1471 } 1472 1473 /** 1474 * Begins a new connection for fetching from the remote repository. 1475 * <p> 1476 * If the transport has no local repository, the fetch connection can only 1477 * be used for reading remote refs. 1478 * 1479 * @return a fresh connection to fetch from the remote repository. 1480 * @throws org.eclipse.jgit.errors.NotSupportedException 1481 * the implementation does not support fetching. 1482 * @throws org.eclipse.jgit.errors.TransportException 1483 * the remote connection could not be established. 1484 */ 1485 public abstract FetchConnection openFetch() throws NotSupportedException, 1486 TransportException; 1487 1488 /** 1489 * Begins a new connection for pushing into the remote repository. 1490 * 1491 * @return a fresh connection to push into the remote repository. 1492 * @throws org.eclipse.jgit.errors.NotSupportedException 1493 * the implementation does not support pushing. 1494 * @throws org.eclipse.jgit.errors.TransportException 1495 * the remote connection could not be established 1496 */ 1497 public abstract PushConnection openPush() throws NotSupportedException, 1498 TransportException; 1499 1500 /** 1501 * {@inheritDoc} 1502 * <p> 1503 * Close any resources used by this transport. 1504 * <p> 1505 * If the remote repository is contacted by a network socket this method 1506 * must close that network socket, disconnecting the two peers. If the 1507 * remote repository is actually local (same system) this method must close 1508 * any open file handles used to read the "remote" repository. 1509 * <p> 1510 * {@code AutoClosable.close()} declares that it throws {@link Exception}. 1511 * Implementers shouldn't throw checked exceptions. This override narrows 1512 * the signature to prevent them from doing so. 1513 */ 1514 @Override 1515 public abstract void close(); 1516 }