You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Transport.java 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. package org.eclipse.jgit.transport;
  47. import java.io.IOException;
  48. import java.net.URISyntaxException;
  49. import java.text.MessageFormat;
  50. import java.util.ArrayList;
  51. import java.util.Collection;
  52. import java.util.Collections;
  53. import java.util.HashSet;
  54. import java.util.LinkedList;
  55. import java.util.List;
  56. import java.util.Map;
  57. import org.eclipse.jgit.JGitText;
  58. import org.eclipse.jgit.errors.NotSupportedException;
  59. import org.eclipse.jgit.errors.TransportException;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.NullProgressMonitor;
  62. import org.eclipse.jgit.lib.ProgressMonitor;
  63. import org.eclipse.jgit.lib.Ref;
  64. import org.eclipse.jgit.lib.Repository;
  65. import org.eclipse.jgit.storage.pack.PackConfig;
  66. import org.eclipse.jgit.util.FS;
  67. /**
  68. * Connects two Git repositories together and copies objects between them.
  69. * <p>
  70. * A transport can be used for either fetching (copying objects into the
  71. * caller's repository from the remote repository) or pushing (copying objects
  72. * into the remote repository from the caller's repository). Each transport
  73. * implementation is responsible for the details associated with establishing
  74. * the network connection(s) necessary for the copy, as well as actually
  75. * shuffling data back and forth.
  76. * <p>
  77. * Transport instances and the connections they create are not thread-safe.
  78. * Callers must ensure a transport is accessed by only one thread at a time.
  79. */
  80. public abstract class Transport {
  81. /** Type of operation a Transport is being opened for. */
  82. public enum Operation {
  83. /** Transport is to fetch objects locally. */
  84. FETCH,
  85. /** Transport is to push objects remotely. */
  86. PUSH;
  87. }
  88. /**
  89. * Open a new transport instance to connect two repositories.
  90. * <p>
  91. * This method assumes {@link Operation#FETCH}.
  92. *
  93. * @param local
  94. * existing local repository.
  95. * @param remote
  96. * location of the remote repository - may be URI or remote
  97. * configuration name.
  98. * @return the new transport instance. Never null. In case of multiple URIs
  99. * in remote configuration, only the first is chosen.
  100. * @throws URISyntaxException
  101. * the location is not a remote defined in the configuration
  102. * file and is not a well-formed URL.
  103. * @throws NotSupportedException
  104. * the protocol specified is not supported.
  105. */
  106. public static Transport open(final Repository local, final String remote)
  107. throws NotSupportedException, URISyntaxException {
  108. return open(local, remote, Operation.FETCH);
  109. }
  110. /**
  111. * Open a new transport instance to connect two repositories.
  112. *
  113. * @param local
  114. * existing local repository.
  115. * @param remote
  116. * location of the remote repository - may be URI or remote
  117. * configuration name.
  118. * @param op
  119. * planned use of the returned Transport; the URI may differ
  120. * based on the type of connection desired.
  121. * @return the new transport instance. Never null. In case of multiple URIs
  122. * in remote configuration, only the first is chosen.
  123. * @throws URISyntaxException
  124. * the location is not a remote defined in the configuration
  125. * file and is not a well-formed URL.
  126. * @throws NotSupportedException
  127. * the protocol specified is not supported.
  128. */
  129. public static Transport open(final Repository local, final String remote,
  130. final Operation op) throws NotSupportedException,
  131. URISyntaxException {
  132. final RemoteConfig cfg = new RemoteConfig(local.getConfig(), remote);
  133. if (doesNotExist(cfg))
  134. return open(local, new URIish(remote));
  135. return open(local, cfg, op);
  136. }
  137. /**
  138. * Open new transport instances to connect two repositories.
  139. * <p>
  140. * This method assumes {@link Operation#FETCH}.
  141. *
  142. * @param local
  143. * existing local repository.
  144. * @param remote
  145. * location of the remote repository - may be URI or remote
  146. * configuration name.
  147. * @return the list of new transport instances for every URI in remote
  148. * configuration.
  149. * @throws URISyntaxException
  150. * the location is not a remote defined in the configuration
  151. * file and is not a well-formed URL.
  152. * @throws NotSupportedException
  153. * the protocol specified is not supported.
  154. */
  155. public static List<Transport> openAll(final Repository local,
  156. final String remote) throws NotSupportedException,
  157. URISyntaxException {
  158. return openAll(local, remote, Operation.FETCH);
  159. }
  160. /**
  161. * Open new transport instances to connect two repositories.
  162. *
  163. * @param local
  164. * existing local repository.
  165. * @param remote
  166. * location of the remote repository - may be URI or remote
  167. * configuration name.
  168. * @param op
  169. * planned use of the returned Transport; the URI may differ
  170. * based on the type of connection desired.
  171. * @return the list of new transport instances for every URI in remote
  172. * configuration.
  173. * @throws URISyntaxException
  174. * the location is not a remote defined in the configuration
  175. * file and is not a well-formed URL.
  176. * @throws NotSupportedException
  177. * the protocol specified is not supported.
  178. */
  179. public static List<Transport> openAll(final Repository local,
  180. final String remote, final Operation op)
  181. throws NotSupportedException, URISyntaxException {
  182. final RemoteConfig cfg = new RemoteConfig(local.getConfig(), remote);
  183. if (doesNotExist(cfg)) {
  184. final ArrayList<Transport> transports = new ArrayList<Transport>(1);
  185. transports.add(open(local, new URIish(remote)));
  186. return transports;
  187. }
  188. return openAll(local, cfg, op);
  189. }
  190. /**
  191. * Open a new transport instance to connect two repositories.
  192. * <p>
  193. * This method assumes {@link Operation#FETCH}.
  194. *
  195. * @param local
  196. * existing local repository.
  197. * @param cfg
  198. * configuration describing how to connect to the remote
  199. * repository.
  200. * @return the new transport instance. Never null. In case of multiple URIs
  201. * in remote configuration, only the first is chosen.
  202. * @throws NotSupportedException
  203. * the protocol specified is not supported.
  204. * @throws IllegalArgumentException
  205. * if provided remote configuration doesn't have any URI
  206. * associated.
  207. */
  208. public static Transport open(final Repository local, final RemoteConfig cfg)
  209. throws NotSupportedException {
  210. return open(local, cfg, Operation.FETCH);
  211. }
  212. /**
  213. * Open a new transport instance to connect two repositories.
  214. *
  215. * @param local
  216. * existing local repository.
  217. * @param cfg
  218. * configuration describing how to connect to the remote
  219. * repository.
  220. * @param op
  221. * planned use of the returned Transport; the URI may differ
  222. * based on the type of connection desired.
  223. * @return the new transport instance. Never null. In case of multiple URIs
  224. * in remote configuration, only the first is chosen.
  225. * @throws NotSupportedException
  226. * the protocol specified is not supported.
  227. * @throws IllegalArgumentException
  228. * if provided remote configuration doesn't have any URI
  229. * associated.
  230. */
  231. public static Transport open(final Repository local,
  232. final RemoteConfig cfg, final Operation op)
  233. throws NotSupportedException {
  234. final List<URIish> uris = getURIs(cfg, op);
  235. if (uris.isEmpty())
  236. throw new IllegalArgumentException(MessageFormat.format(
  237. JGitText.get().remoteConfigHasNoURIAssociated, cfg.getName()));
  238. final Transport tn = open(local, uris.get(0));
  239. tn.applyConfig(cfg);
  240. return tn;
  241. }
  242. /**
  243. * Open new transport instances to connect two repositories.
  244. * <p>
  245. * This method assumes {@link Operation#FETCH}.
  246. *
  247. * @param local
  248. * existing local repository.
  249. * @param cfg
  250. * configuration describing how to connect to the remote
  251. * repository.
  252. * @return the list of new transport instances for every URI in remote
  253. * configuration.
  254. * @throws NotSupportedException
  255. * the protocol specified is not supported.
  256. */
  257. public static List<Transport> openAll(final Repository local,
  258. final RemoteConfig cfg) throws NotSupportedException {
  259. return openAll(local, cfg, Operation.FETCH);
  260. }
  261. /**
  262. * Open new transport instances to connect two repositories.
  263. *
  264. * @param local
  265. * existing local repository.
  266. * @param cfg
  267. * configuration describing how to connect to the remote
  268. * repository.
  269. * @param op
  270. * planned use of the returned Transport; the URI may differ
  271. * based on the type of connection desired.
  272. * @return the list of new transport instances for every URI in remote
  273. * configuration.
  274. * @throws NotSupportedException
  275. * the protocol specified is not supported.
  276. */
  277. public static List<Transport> openAll(final Repository local,
  278. final RemoteConfig cfg, final Operation op)
  279. throws NotSupportedException {
  280. final List<URIish> uris = getURIs(cfg, op);
  281. final List<Transport> transports = new ArrayList<Transport>(uris.size());
  282. for (final URIish uri : uris) {
  283. final Transport tn = open(local, uri);
  284. tn.applyConfig(cfg);
  285. transports.add(tn);
  286. }
  287. return transports;
  288. }
  289. private static List<URIish> getURIs(final RemoteConfig cfg,
  290. final Operation op) {
  291. switch (op) {
  292. case FETCH:
  293. return cfg.getURIs();
  294. case PUSH: {
  295. List<URIish> uris = cfg.getPushURIs();
  296. if (uris.isEmpty())
  297. uris = cfg.getURIs();
  298. return uris;
  299. }
  300. default:
  301. throw new IllegalArgumentException(op.toString());
  302. }
  303. }
  304. private static boolean doesNotExist(final RemoteConfig cfg) {
  305. return cfg.getURIs().isEmpty() && cfg.getPushURIs().isEmpty();
  306. }
  307. /**
  308. * Determines whether the transport can handle the given URIish.
  309. *
  310. * @param remote
  311. * location of the remote repository.
  312. * @param fs
  313. * type of filesystem the local repository is stored on.
  314. * @return true if the protocol is supported.
  315. */
  316. public static boolean canHandleProtocol(final URIish remote, final FS fs) {
  317. if (TransportGitSsh.canHandle(remote))
  318. return true;
  319. else if (TransportHttp.canHandle(remote))
  320. return true;
  321. else if (TransportSftp.canHandle(remote))
  322. return true;
  323. else if (TransportGitAnon.canHandle(remote))
  324. return true;
  325. else if (TransportAmazonS3.canHandle(remote))
  326. return true;
  327. else if (TransportBundleFile.canHandle(remote, fs))
  328. return true;
  329. else if (TransportLocal.canHandle(remote, fs))
  330. return true;
  331. return false;
  332. }
  333. /**
  334. * Open a new transport instance to connect two repositories.
  335. *
  336. * @param local
  337. * existing local repository.
  338. * @param remote
  339. * location of the remote repository.
  340. * @return the new transport instance. Never null.
  341. * @throws NotSupportedException
  342. * the protocol specified is not supported.
  343. */
  344. public static Transport open(final Repository local, final URIish remote)
  345. throws NotSupportedException {
  346. if (TransportGitSsh.canHandle(remote))
  347. return new TransportGitSsh(local, remote);
  348. else if (TransportHttp.canHandle(remote))
  349. return new TransportHttp(local, remote);
  350. else if (TransportSftp.canHandle(remote))
  351. return new TransportSftp(local, remote);
  352. else if (TransportGitAnon.canHandle(remote))
  353. return new TransportGitAnon(local, remote);
  354. else if (TransportAmazonS3.canHandle(remote))
  355. return new TransportAmazonS3(local, remote);
  356. else if (TransportBundleFile.canHandle(remote, local.getFS()))
  357. return new TransportBundleFile(local, remote);
  358. else if (TransportLocal.canHandle(remote, local.getFS()))
  359. return new TransportLocal(local, remote);
  360. throw new NotSupportedException(MessageFormat.format(JGitText.get().URINotSupported, remote));
  361. }
  362. /**
  363. * Convert push remote refs update specification from {@link RefSpec} form
  364. * to {@link RemoteRefUpdate}. Conversion expands wildcards by matching
  365. * source part to local refs. expectedOldObjectId in RemoteRefUpdate is
  366. * always set as null. Tracking branch is configured if RefSpec destination
  367. * matches source of any fetch ref spec for this transport remote
  368. * configuration.
  369. *
  370. * @param db
  371. * local database.
  372. * @param specs
  373. * collection of RefSpec to convert.
  374. * @param fetchSpecs
  375. * fetch specifications used for finding localtracking refs. May
  376. * be null or empty collection.
  377. * @return collection of set up {@link RemoteRefUpdate}.
  378. * @throws IOException
  379. * when problem occurred during conversion or specification set
  380. * up: most probably, missing objects or refs.
  381. */
  382. public static Collection<RemoteRefUpdate> findRemoteRefUpdatesFor(
  383. final Repository db, final Collection<RefSpec> specs,
  384. Collection<RefSpec> fetchSpecs) throws IOException {
  385. if (fetchSpecs == null)
  386. fetchSpecs = Collections.emptyList();
  387. final List<RemoteRefUpdate> result = new LinkedList<RemoteRefUpdate>();
  388. final Collection<RefSpec> procRefs = expandPushWildcardsFor(db, specs);
  389. for (final RefSpec spec : procRefs) {
  390. String srcSpec = spec.getSource();
  391. final Ref srcRef = db.getRef(srcSpec);
  392. if (srcRef != null)
  393. srcSpec = srcRef.getName();
  394. String destSpec = spec.getDestination();
  395. if (destSpec == null) {
  396. // No destination (no-colon in ref-spec), DWIMery assumes src
  397. //
  398. destSpec = srcSpec;
  399. }
  400. if (srcRef != null && !destSpec.startsWith(Constants.R_REFS)) {
  401. // Assume the same kind of ref at the destination, e.g.
  402. // "refs/heads/foo:master", DWIMery assumes master is also
  403. // under "refs/heads/".
  404. //
  405. final String n = srcRef.getName();
  406. final int kindEnd = n.indexOf('/', Constants.R_REFS.length());
  407. destSpec = n.substring(0, kindEnd + 1) + destSpec;
  408. }
  409. final boolean forceUpdate = spec.isForceUpdate();
  410. final String localName = findTrackingRefName(destSpec, fetchSpecs);
  411. final RemoteRefUpdate rru = new RemoteRefUpdate(db, srcSpec,
  412. destSpec, forceUpdate, localName, null);
  413. result.add(rru);
  414. }
  415. return result;
  416. }
  417. private static Collection<RefSpec> expandPushWildcardsFor(
  418. final Repository db, final Collection<RefSpec> specs) {
  419. final Map<String, Ref> localRefs = db.getAllRefs();
  420. final Collection<RefSpec> procRefs = new HashSet<RefSpec>();
  421. for (final RefSpec spec : specs) {
  422. if (spec.isWildcard()) {
  423. for (final Ref localRef : localRefs.values()) {
  424. if (spec.matchSource(localRef))
  425. procRefs.add(spec.expandFromSource(localRef));
  426. }
  427. } else {
  428. procRefs.add(spec);
  429. }
  430. }
  431. return procRefs;
  432. }
  433. private static String findTrackingRefName(final String remoteName,
  434. final Collection<RefSpec> fetchSpecs) {
  435. // try to find matching tracking refs
  436. for (final RefSpec fetchSpec : fetchSpecs) {
  437. if (fetchSpec.matchSource(remoteName)) {
  438. if (fetchSpec.isWildcard())
  439. return fetchSpec.expandFromSource(remoteName)
  440. .getDestination();
  441. else
  442. return fetchSpec.getDestination();
  443. }
  444. }
  445. return null;
  446. }
  447. /**
  448. * Default setting for {@link #fetchThin} option.
  449. */
  450. public static final boolean DEFAULT_FETCH_THIN = true;
  451. /**
  452. * Default setting for {@link #pushThin} option.
  453. */
  454. public static final boolean DEFAULT_PUSH_THIN = false;
  455. /**
  456. * Specification for fetch or push operations, to fetch or push all tags.
  457. * Acts as --tags.
  458. */
  459. public static final RefSpec REFSPEC_TAGS = new RefSpec(
  460. "refs/tags/*:refs/tags/*");
  461. /**
  462. * Specification for push operation, to push all refs under refs/heads. Acts
  463. * as --all.
  464. */
  465. public static final RefSpec REFSPEC_PUSH_ALL = new RefSpec(
  466. "refs/heads/*:refs/heads/*");
  467. /** The repository this transport fetches into, or pushes out of. */
  468. protected final Repository local;
  469. /** The URI used to create this transport. */
  470. protected final URIish uri;
  471. /** Name of the upload pack program, if it must be executed. */
  472. private String optionUploadPack = RemoteConfig.DEFAULT_UPLOAD_PACK;
  473. /** Specifications to apply during fetch. */
  474. private List<RefSpec> fetch = Collections.emptyList();
  475. /**
  476. * How {@link #fetch(ProgressMonitor, Collection)} should handle tags.
  477. * <p>
  478. * We default to {@link TagOpt#NO_TAGS} so as to avoid fetching annotated
  479. * tags during one-shot fetches used for later merges. This prevents
  480. * dragging down tags from repositories that we do not have established
  481. * tracking branches for. If we do not track the source repository, we most
  482. * likely do not care about any tags it publishes.
  483. */
  484. private TagOpt tagopt = TagOpt.NO_TAGS;
  485. /** Should fetch request thin-pack if remote repository can produce it. */
  486. private boolean fetchThin = DEFAULT_FETCH_THIN;
  487. /** Name of the receive pack program, if it must be executed. */
  488. private String optionReceivePack = RemoteConfig.DEFAULT_RECEIVE_PACK;
  489. /** Specifications to apply during push. */
  490. private List<RefSpec> push = Collections.emptyList();
  491. /** Should push produce thin-pack when sending objects to remote repository. */
  492. private boolean pushThin = DEFAULT_PUSH_THIN;
  493. /** Should push just check for operation result, not really push. */
  494. private boolean dryRun;
  495. /** Should an incoming (fetch) transfer validate objects? */
  496. private boolean checkFetchedObjects;
  497. /** Should refs no longer on the source be pruned from the destination? */
  498. private boolean removeDeletedRefs;
  499. /** Timeout in seconds to wait before aborting an IO read or write. */
  500. private int timeout;
  501. /** Pack configuration used by this transport to make pack file. */
  502. private PackConfig packConfig;
  503. /** Assists with authentication the connection. */
  504. private CredentialsProvider credentialsProvider;
  505. /**
  506. * Create a new transport instance.
  507. *
  508. * @param local
  509. * the repository this instance will fetch into, or push out of.
  510. * This must be the repository passed to
  511. * {@link #open(Repository, URIish)}.
  512. * @param uri
  513. * the URI used to access the remote repository. This must be the
  514. * URI passed to {@link #open(Repository, URIish)}.
  515. */
  516. protected Transport(final Repository local, final URIish uri) {
  517. final TransferConfig tc = local.getConfig().get(TransferConfig.KEY);
  518. this.local = local;
  519. this.uri = uri;
  520. this.checkFetchedObjects = tc.isFsckObjects();
  521. this.credentialsProvider = CredentialsProvider.getDefault();
  522. }
  523. /**
  524. * Get the URI this transport connects to.
  525. * <p>
  526. * Each transport instance connects to at most one URI at any point in time.
  527. *
  528. * @return the URI describing the location of the remote repository.
  529. */
  530. public URIish getURI() {
  531. return uri;
  532. }
  533. /**
  534. * Get the name of the remote executable providing upload-pack service.
  535. *
  536. * @return typically "git-upload-pack".
  537. */
  538. public String getOptionUploadPack() {
  539. return optionUploadPack;
  540. }
  541. /**
  542. * Set the name of the remote executable providing upload-pack services.
  543. *
  544. * @param where
  545. * name of the executable.
  546. */
  547. public void setOptionUploadPack(final String where) {
  548. if (where != null && where.length() > 0)
  549. optionUploadPack = where;
  550. else
  551. optionUploadPack = RemoteConfig.DEFAULT_UPLOAD_PACK;
  552. }
  553. /**
  554. * Get the description of how annotated tags should be treated during fetch.
  555. *
  556. * @return option indicating the behavior of annotated tags in fetch.
  557. */
  558. public TagOpt getTagOpt() {
  559. return tagopt;
  560. }
  561. /**
  562. * Set the description of how annotated tags should be treated on fetch.
  563. *
  564. * @param option
  565. * method to use when handling annotated tags.
  566. */
  567. public void setTagOpt(final TagOpt option) {
  568. tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;
  569. }
  570. /**
  571. * Default setting is: {@link #DEFAULT_FETCH_THIN}
  572. *
  573. * @return true if fetch should request thin-pack when possible; false
  574. * otherwise
  575. * @see PackTransport
  576. */
  577. public boolean isFetchThin() {
  578. return fetchThin;
  579. }
  580. /**
  581. * Set the thin-pack preference for fetch operation. Default setting is:
  582. * {@link #DEFAULT_FETCH_THIN}
  583. *
  584. * @param fetchThin
  585. * true when fetch should request thin-pack when possible; false
  586. * when it shouldn't
  587. * @see PackTransport
  588. */
  589. public void setFetchThin(final boolean fetchThin) {
  590. this.fetchThin = fetchThin;
  591. }
  592. /**
  593. * @return true if fetch will verify received objects are formatted
  594. * correctly. Validating objects requires more CPU time on the
  595. * client side of the connection.
  596. */
  597. public boolean isCheckFetchedObjects() {
  598. return checkFetchedObjects;
  599. }
  600. /**
  601. * @param check
  602. * true to enable checking received objects; false to assume all
  603. * received objects are valid.
  604. */
  605. public void setCheckFetchedObjects(final boolean check) {
  606. checkFetchedObjects = check;
  607. }
  608. /**
  609. * Default setting is: {@link RemoteConfig#DEFAULT_RECEIVE_PACK}
  610. *
  611. * @return remote executable providing receive-pack service for pack
  612. * transports.
  613. * @see PackTransport
  614. */
  615. public String getOptionReceivePack() {
  616. return optionReceivePack;
  617. }
  618. /**
  619. * Set remote executable providing receive-pack service for pack transports.
  620. * Default setting is: {@link RemoteConfig#DEFAULT_RECEIVE_PACK}
  621. *
  622. * @param optionReceivePack
  623. * remote executable, if null or empty default one is set;
  624. */
  625. public void setOptionReceivePack(String optionReceivePack) {
  626. if (optionReceivePack != null && optionReceivePack.length() > 0)
  627. this.optionReceivePack = optionReceivePack;
  628. else
  629. this.optionReceivePack = RemoteConfig.DEFAULT_RECEIVE_PACK;
  630. }
  631. /**
  632. * Default setting is: {@value #DEFAULT_PUSH_THIN}
  633. *
  634. * @return true if push should produce thin-pack in pack transports
  635. * @see PackTransport
  636. */
  637. public boolean isPushThin() {
  638. return pushThin;
  639. }
  640. /**
  641. * Set thin-pack preference for push operation. Default setting is:
  642. * {@value #DEFAULT_PUSH_THIN}
  643. *
  644. * @param pushThin
  645. * true when push should produce thin-pack in pack transports;
  646. * false when it shouldn't
  647. * @see PackTransport
  648. */
  649. public void setPushThin(final boolean pushThin) {
  650. this.pushThin = pushThin;
  651. }
  652. /**
  653. * @return true if destination refs should be removed if they no longer
  654. * exist at the source repository.
  655. */
  656. public boolean isRemoveDeletedRefs() {
  657. return removeDeletedRefs;
  658. }
  659. /**
  660. * Set whether or not to remove refs which no longer exist in the source.
  661. * <p>
  662. * If true, refs at the destination repository (local for fetch, remote for
  663. * push) are deleted if they no longer exist on the source side (remote for
  664. * fetch, local for push).
  665. * <p>
  666. * False by default, as this may cause data to become unreachable, and
  667. * eventually be deleted on the next GC.
  668. *
  669. * @param remove true to remove refs that no longer exist.
  670. */
  671. public void setRemoveDeletedRefs(final boolean remove) {
  672. removeDeletedRefs = remove;
  673. }
  674. /**
  675. * Apply provided remote configuration on this transport.
  676. *
  677. * @param cfg
  678. * configuration to apply on this transport.
  679. */
  680. public void applyConfig(final RemoteConfig cfg) {
  681. setOptionUploadPack(cfg.getUploadPack());
  682. setOptionReceivePack(cfg.getReceivePack());
  683. setTagOpt(cfg.getTagOpt());
  684. fetch = cfg.getFetchRefSpecs();
  685. push = cfg.getPushRefSpecs();
  686. timeout = cfg.getTimeout();
  687. }
  688. /**
  689. * @return true if push operation should just check for possible result and
  690. * not really update remote refs, false otherwise - when push should
  691. * act normally.
  692. */
  693. public boolean isDryRun() {
  694. return dryRun;
  695. }
  696. /**
  697. * Set dry run option for push operation.
  698. *
  699. * @param dryRun
  700. * true if push operation should just check for possible result
  701. * and not really update remote refs, false otherwise - when push
  702. * should act normally.
  703. */
  704. public void setDryRun(final boolean dryRun) {
  705. this.dryRun = dryRun;
  706. }
  707. /** @return timeout (in seconds) before aborting an IO operation. */
  708. public int getTimeout() {
  709. return timeout;
  710. }
  711. /**
  712. * Set the timeout before willing to abort an IO call.
  713. *
  714. * @param seconds
  715. * number of seconds to wait (with no data transfer occurring)
  716. * before aborting an IO read or write operation with this
  717. * remote.
  718. */
  719. public void setTimeout(final int seconds) {
  720. timeout = seconds;
  721. }
  722. /**
  723. * Get the configuration used by the pack generator to make packs.
  724. *
  725. * If {@link #setPackConfig(PackConfig)} was previously given null a new
  726. * PackConfig is created on demand by this method using the source
  727. * repository's settings.
  728. *
  729. * @return the pack configuration. Never null.
  730. */
  731. public PackConfig getPackConfig() {
  732. if (packConfig == null)
  733. packConfig = new PackConfig(local);
  734. return packConfig;
  735. }
  736. /**
  737. * Set the configuration used by the pack generator.
  738. *
  739. * @param pc
  740. * configuration controlling packing parameters. If null the
  741. * source repository's settings will be used.
  742. */
  743. public void setPackConfig(PackConfig pc) {
  744. packConfig = pc;
  745. }
  746. /**
  747. * A credentials provider to assist with authentication connections..
  748. *
  749. * @param credentialsProvider
  750. * the credentials provider, or null if there is none
  751. */
  752. public void setCredentialsProvider(CredentialsProvider credentialsProvider) {
  753. this.credentialsProvider = credentialsProvider;
  754. }
  755. /**
  756. * The configured credentials provider.
  757. *
  758. * @return the credentials provider, or null if no credentials provider is
  759. * associated with this transport.
  760. */
  761. public CredentialsProvider getCredentialsProvider() {
  762. return credentialsProvider;
  763. }
  764. /**
  765. * Fetch objects and refs from the remote repository to the local one.
  766. * <p>
  767. * This is a utility function providing standard fetch behavior. Local
  768. * tracking refs associated with the remote repository are automatically
  769. * updated if this transport was created from a {@link RemoteConfig} with
  770. * fetch RefSpecs defined.
  771. *
  772. * @param monitor
  773. * progress monitor to inform the user about our processing
  774. * activity. Must not be null. Use {@link NullProgressMonitor} if
  775. * progress updates are not interesting or necessary.
  776. * @param toFetch
  777. * specification of refs to fetch locally. May be null or the
  778. * empty collection to use the specifications from the
  779. * RemoteConfig. Source for each RefSpec can't be null.
  780. * @return information describing the tracking refs updated.
  781. * @throws NotSupportedException
  782. * this transport implementation does not support fetching
  783. * objects.
  784. * @throws TransportException
  785. * the remote connection could not be established or object
  786. * copying (if necessary) failed or update specification was
  787. * incorrect.
  788. */
  789. public FetchResult fetch(final ProgressMonitor monitor,
  790. Collection<RefSpec> toFetch) throws NotSupportedException,
  791. TransportException {
  792. if (toFetch == null || toFetch.isEmpty()) {
  793. // If the caller did not ask for anything use the defaults.
  794. //
  795. if (fetch.isEmpty())
  796. throw new TransportException(JGitText.get().nothingToFetch);
  797. toFetch = fetch;
  798. } else if (!fetch.isEmpty()) {
  799. // If the caller asked for something specific without giving
  800. // us the local tracking branch see if we can update any of
  801. // the local tracking branches without incurring additional
  802. // object transfer overheads.
  803. //
  804. final Collection<RefSpec> tmp = new ArrayList<RefSpec>(toFetch);
  805. for (final RefSpec requested : toFetch) {
  806. final String reqSrc = requested.getSource();
  807. for (final RefSpec configured : fetch) {
  808. final String cfgSrc = configured.getSource();
  809. final String cfgDst = configured.getDestination();
  810. if (cfgSrc.equals(reqSrc) && cfgDst != null) {
  811. tmp.add(configured);
  812. break;
  813. }
  814. }
  815. }
  816. toFetch = tmp;
  817. }
  818. final FetchResult result = new FetchResult();
  819. new FetchProcess(this, toFetch).execute(monitor, result);
  820. return result;
  821. }
  822. /**
  823. * Push objects and refs from the local repository to the remote one.
  824. * <p>
  825. * This is a utility function providing standard push behavior. It updates
  826. * remote refs and send there necessary objects according to remote ref
  827. * update specification. After successful remote ref update, associated
  828. * locally stored tracking branch is updated if set up accordingly. Detailed
  829. * operation result is provided after execution.
  830. * <p>
  831. * For setting up remote ref update specification from ref spec, see helper
  832. * method {@link #findRemoteRefUpdatesFor(Collection)}, predefined refspecs
  833. * ({@link #REFSPEC_TAGS}, {@link #REFSPEC_PUSH_ALL}) or consider using
  834. * directly {@link RemoteRefUpdate} for more possibilities.
  835. * <p>
  836. * When {@link #isDryRun()} is true, result of this operation is just
  837. * estimation of real operation result, no real action is performed.
  838. *
  839. * @see RemoteRefUpdate
  840. *
  841. * @param monitor
  842. * progress monitor to inform the user about our processing
  843. * activity. Must not be null. Use {@link NullProgressMonitor} if
  844. * progress updates are not interesting or necessary.
  845. * @param toPush
  846. * specification of refs to push. May be null or the empty
  847. * collection to use the specifications from the RemoteConfig
  848. * converted by {@link #findRemoteRefUpdatesFor(Collection)}. No
  849. * more than 1 RemoteRefUpdate with the same remoteName is
  850. * allowed. These objects are modified during this call.
  851. * @return information about results of remote refs updates, tracking refs
  852. * updates and refs advertised by remote repository.
  853. * @throws NotSupportedException
  854. * this transport implementation does not support pushing
  855. * objects.
  856. * @throws TransportException
  857. * the remote connection could not be established or object
  858. * copying (if necessary) failed at I/O or protocol level or
  859. * update specification was incorrect.
  860. */
  861. public PushResult push(final ProgressMonitor monitor,
  862. Collection<RemoteRefUpdate> toPush) throws NotSupportedException,
  863. TransportException {
  864. if (toPush == null || toPush.isEmpty()) {
  865. // If the caller did not ask for anything use the defaults.
  866. try {
  867. toPush = findRemoteRefUpdatesFor(push);
  868. } catch (final IOException e) {
  869. throw new TransportException(MessageFormat.format(
  870. JGitText.get().problemWithResolvingPushRefSpecsLocally, e.getMessage()), e);
  871. }
  872. if (toPush.isEmpty())
  873. throw new TransportException(JGitText.get().nothingToPush);
  874. }
  875. final PushProcess pushProcess = new PushProcess(this, toPush);
  876. return pushProcess.execute(monitor);
  877. }
  878. /**
  879. * Convert push remote refs update specification from {@link RefSpec} form
  880. * to {@link RemoteRefUpdate}. Conversion expands wildcards by matching
  881. * source part to local refs. expectedOldObjectId in RemoteRefUpdate is
  882. * always set as null. Tracking branch is configured if RefSpec destination
  883. * matches source of any fetch ref spec for this transport remote
  884. * configuration.
  885. * <p>
  886. * Conversion is performed for context of this transport (database, fetch
  887. * specifications).
  888. *
  889. * @param specs
  890. * collection of RefSpec to convert.
  891. * @return collection of set up {@link RemoteRefUpdate}.
  892. * @throws IOException
  893. * when problem occurred during conversion or specification set
  894. * up: most probably, missing objects or refs.
  895. */
  896. public Collection<RemoteRefUpdate> findRemoteRefUpdatesFor(
  897. final Collection<RefSpec> specs) throws IOException {
  898. return findRemoteRefUpdatesFor(local, specs, fetch);
  899. }
  900. /**
  901. * Begins a new connection for fetching from the remote repository.
  902. *
  903. * @return a fresh connection to fetch from the remote repository.
  904. * @throws NotSupportedException
  905. * the implementation does not support fetching.
  906. * @throws TransportException
  907. * the remote connection could not be established.
  908. */
  909. public abstract FetchConnection openFetch() throws NotSupportedException,
  910. TransportException;
  911. /**
  912. * Begins a new connection for pushing into the remote repository.
  913. *
  914. * @return a fresh connection to push into the remote repository.
  915. * @throws NotSupportedException
  916. * the implementation does not support pushing.
  917. * @throws TransportException
  918. * the remote connection could not be established
  919. */
  920. public abstract PushConnection openPush() throws NotSupportedException,
  921. TransportException;
  922. /**
  923. * Close any resources used by this transport.
  924. * <p>
  925. * If the remote repository is contacted by a network socket this method
  926. * must close that network socket, disconnecting the two peers. If the
  927. * remote repository is actually local (same system) this method must close
  928. * any open file handles used to read the "remote" repository.
  929. */
  930. public abstract void close();
  931. }