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

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