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