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 46KB

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