Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RemoteConfig.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.transport;
  46. import java.io.Serializable;
  47. import java.net.URISyntaxException;
  48. import java.util.ArrayList;
  49. import java.util.Collections;
  50. import java.util.HashMap;
  51. import java.util.List;
  52. import java.util.Map;
  53. import java.util.Map.Entry;
  54. import org.eclipse.jgit.lib.Config;
  55. /**
  56. * A remembered remote repository, including URLs and RefSpecs.
  57. * <p>
  58. * A remote configuration remembers one or more URLs for a frequently accessed
  59. * remote repository as well as zero or more fetch and push specifications
  60. * describing how refs should be transferred between this repository and the
  61. * remote repository.
  62. */
  63. public class RemoteConfig implements Serializable {
  64. private static final long serialVersionUID = 1L;
  65. private static final String SECTION = "remote";
  66. private static final String KEY_URL = "url";
  67. private static final String KEY_PUSHURL = "pushurl";
  68. private static final String KEY_FETCH = "fetch";
  69. private static final String KEY_PUSH = "push";
  70. private static final String KEY_UPLOADPACK = "uploadpack";
  71. private static final String KEY_RECEIVEPACK = "receivepack";
  72. private static final String KEY_TAGOPT = "tagopt";
  73. private static final String KEY_MIRROR = "mirror";
  74. private static final String KEY_TIMEOUT = "timeout";
  75. private static final String KEY_INSTEADOF = "insteadof";
  76. private static final String KEY_PUSHINSTEADOF = "pushinsteadof";
  77. private static final boolean DEFAULT_MIRROR = false;
  78. /** Default value for {@link #getUploadPack()} if not specified. */
  79. public static final String DEFAULT_UPLOAD_PACK = "git-upload-pack";
  80. /** Default value for {@link #getReceivePack()} if not specified. */
  81. public static final String DEFAULT_RECEIVE_PACK = "git-receive-pack";
  82. /**
  83. * Parse all remote blocks in an existing configuration file, looking for
  84. * remotes configuration.
  85. *
  86. * @param rc
  87. * the existing configuration to get the remote settings from.
  88. * The configuration must already be loaded into memory.
  89. * @return all remotes configurations existing in provided repository
  90. * configuration. Returned configurations are ordered
  91. * lexicographically by names.
  92. * @throws URISyntaxException
  93. * one of the URIs within the remote's configuration is invalid.
  94. */
  95. public static List<RemoteConfig> getAllRemoteConfigs(final Config rc)
  96. throws URISyntaxException {
  97. final List<String> names = new ArrayList<String>(rc
  98. .getSubsections(SECTION));
  99. Collections.sort(names);
  100. final List<RemoteConfig> result = new ArrayList<RemoteConfig>(names
  101. .size());
  102. for (final String name : names)
  103. result.add(new RemoteConfig(rc, name));
  104. return result;
  105. }
  106. private String name;
  107. private List<URIish> uris;
  108. private List<URIish> pushURIs;
  109. private List<RefSpec> fetch;
  110. private List<RefSpec> push;
  111. private String uploadpack;
  112. private String receivepack;
  113. private TagOpt tagopt;
  114. private boolean mirror;
  115. private int timeout;
  116. /**
  117. * Parse a remote block from an existing configuration file.
  118. * <p>
  119. * This constructor succeeds even if the requested remote is not defined
  120. * within the supplied configuration file. If that occurs then there will be
  121. * no URIs and no ref specifications known to the new instance.
  122. *
  123. * @param rc
  124. * the existing configuration to get the remote settings from.
  125. * The configuration must already be loaded into memory.
  126. * @param remoteName
  127. * subsection key indicating the name of this remote.
  128. * @throws URISyntaxException
  129. * one of the URIs within the remote's configuration is invalid.
  130. */
  131. public RemoteConfig(final Config rc, final String remoteName)
  132. throws URISyntaxException {
  133. name = remoteName;
  134. String[] vlst;
  135. String val;
  136. vlst = rc.getStringList(SECTION, name, KEY_URL);
  137. Map<String, String> insteadOf = getReplacements(rc, KEY_INSTEADOF);
  138. uris = new ArrayList<URIish>(vlst.length);
  139. for (final String s : vlst)
  140. uris.add(new URIish(replaceUri(s, insteadOf)));
  141. Map<String, String> pushInsteadOf = getReplacements(rc,
  142. KEY_PUSHINSTEADOF);
  143. vlst = rc.getStringList(SECTION, name, KEY_PUSHURL);
  144. pushURIs = new ArrayList<URIish>(vlst.length);
  145. for (final String s : vlst)
  146. pushURIs.add(new URIish(replaceUri(s, pushInsteadOf)));
  147. vlst = rc.getStringList(SECTION, name, KEY_FETCH);
  148. fetch = new ArrayList<RefSpec>(vlst.length);
  149. for (final String s : vlst)
  150. fetch.add(new RefSpec(s));
  151. vlst = rc.getStringList(SECTION, name, KEY_PUSH);
  152. push = new ArrayList<RefSpec>(vlst.length);
  153. for (final String s : vlst)
  154. push.add(new RefSpec(s));
  155. val = rc.getString(SECTION, name, KEY_UPLOADPACK);
  156. if (val == null)
  157. val = DEFAULT_UPLOAD_PACK;
  158. uploadpack = val;
  159. val = rc.getString(SECTION, name, KEY_RECEIVEPACK);
  160. if (val == null)
  161. val = DEFAULT_RECEIVE_PACK;
  162. receivepack = val;
  163. val = rc.getString(SECTION, name, KEY_TAGOPT);
  164. tagopt = TagOpt.fromOption(val);
  165. mirror = rc.getBoolean(SECTION, name, KEY_MIRROR, DEFAULT_MIRROR);
  166. timeout = rc.getInt(SECTION, name, KEY_TIMEOUT, 0);
  167. }
  168. /**
  169. * Update this remote's definition within the configuration.
  170. *
  171. * @param rc
  172. * the configuration file to store ourselves into.
  173. */
  174. public void update(final Config rc) {
  175. final List<String> vlst = new ArrayList<String>();
  176. vlst.clear();
  177. for (final URIish u : getURIs())
  178. vlst.add(u.toPrivateString());
  179. rc.setStringList(SECTION, getName(), KEY_URL, vlst);
  180. vlst.clear();
  181. for (final URIish u : getPushURIs())
  182. vlst.add(u.toPrivateString());
  183. rc.setStringList(SECTION, getName(), KEY_PUSHURL, vlst);
  184. vlst.clear();
  185. for (final RefSpec u : getFetchRefSpecs())
  186. vlst.add(u.toString());
  187. rc.setStringList(SECTION, getName(), KEY_FETCH, vlst);
  188. vlst.clear();
  189. for (final RefSpec u : getPushRefSpecs())
  190. vlst.add(u.toString());
  191. rc.setStringList(SECTION, getName(), KEY_PUSH, vlst);
  192. set(rc, KEY_UPLOADPACK, getUploadPack(), DEFAULT_UPLOAD_PACK);
  193. set(rc, KEY_RECEIVEPACK, getReceivePack(), DEFAULT_RECEIVE_PACK);
  194. set(rc, KEY_TAGOPT, getTagOpt().option(), TagOpt.AUTO_FOLLOW.option());
  195. set(rc, KEY_MIRROR, mirror, DEFAULT_MIRROR);
  196. set(rc, KEY_TIMEOUT, timeout, 0);
  197. }
  198. private void set(final Config rc, final String key,
  199. final String currentValue, final String defaultValue) {
  200. if (defaultValue.equals(currentValue))
  201. unset(rc, key);
  202. else
  203. rc.setString(SECTION, getName(), key, currentValue);
  204. }
  205. private void set(final Config rc, final String key,
  206. final boolean currentValue, final boolean defaultValue) {
  207. if (defaultValue == currentValue)
  208. unset(rc, key);
  209. else
  210. rc.setBoolean(SECTION, getName(), key, currentValue);
  211. }
  212. private void set(final Config rc, final String key, final int currentValue,
  213. final int defaultValue) {
  214. if (defaultValue == currentValue)
  215. unset(rc, key);
  216. else
  217. rc.setInt(SECTION, getName(), key, currentValue);
  218. }
  219. private void unset(final Config rc, final String key) {
  220. rc.unset(SECTION, getName(), key);
  221. }
  222. private Map<String, String> getReplacements(final Config config,
  223. final String keyName) {
  224. final Map<String, String> replacements = new HashMap<String, String>();
  225. for (String url : config.getSubsections(KEY_URL))
  226. for (String insteadOf : config.getStringList(KEY_URL, url, keyName))
  227. replacements.put(insteadOf, url);
  228. return replacements;
  229. }
  230. private String replaceUri(final String uri,
  231. final Map<String, String> replacements) {
  232. if (replacements.isEmpty())
  233. return uri;
  234. Entry<String, String> match = null;
  235. for (Entry<String, String> replacement : replacements.entrySet()) {
  236. // Ignore current entry if not longer than previous match
  237. if (match != null
  238. && match.getKey().length() > replacement.getKey().length())
  239. continue;
  240. if (!uri.startsWith(replacement.getKey()))
  241. continue;
  242. match = replacement;
  243. }
  244. if (match != null)
  245. return match.getValue() + uri.substring(match.getKey().length());
  246. else
  247. return uri;
  248. }
  249. /**
  250. * Get the local name this remote configuration is recognized as.
  251. *
  252. * @return name assigned by the user to this configuration block.
  253. */
  254. public String getName() {
  255. return name;
  256. }
  257. /**
  258. * Get all configured URIs under this remote.
  259. *
  260. * @return the set of URIs known to this remote.
  261. */
  262. public List<URIish> getURIs() {
  263. return Collections.unmodifiableList(uris);
  264. }
  265. /**
  266. * Add a new URI to the end of the list of URIs.
  267. *
  268. * @param toAdd
  269. * the new URI to add to this remote.
  270. * @return true if the URI was added; false if it already exists.
  271. */
  272. public boolean addURI(final URIish toAdd) {
  273. if (uris.contains(toAdd))
  274. return false;
  275. return uris.add(toAdd);
  276. }
  277. /**
  278. * Remove a URI from the list of URIs.
  279. *
  280. * @param toRemove
  281. * the URI to remove from this remote.
  282. * @return true if the URI was added; false if it already exists.
  283. */
  284. public boolean removeURI(final URIish toRemove) {
  285. return uris.remove(toRemove);
  286. }
  287. /**
  288. * Get all configured push-only URIs under this remote.
  289. *
  290. * @return the set of URIs known to this remote.
  291. */
  292. public List<URIish> getPushURIs() {
  293. return Collections.unmodifiableList(pushURIs);
  294. }
  295. /**
  296. * Add a new push-only URI to the end of the list of URIs.
  297. *
  298. * @param toAdd
  299. * the new URI to add to this remote.
  300. * @return true if the URI was added; false if it already exists.
  301. */
  302. public boolean addPushURI(final URIish toAdd) {
  303. if (pushURIs.contains(toAdd))
  304. return false;
  305. return pushURIs.add(toAdd);
  306. }
  307. /**
  308. * Remove a push-only URI from the list of URIs.
  309. *
  310. * @param toRemove
  311. * the URI to remove from this remote.
  312. * @return true if the URI was added; false if it already exists.
  313. */
  314. public boolean removePushURI(final URIish toRemove) {
  315. return pushURIs.remove(toRemove);
  316. }
  317. /**
  318. * Remembered specifications for fetching from a repository.
  319. *
  320. * @return set of specs used by default when fetching.
  321. */
  322. public List<RefSpec> getFetchRefSpecs() {
  323. return Collections.unmodifiableList(fetch);
  324. }
  325. /**
  326. * Add a new fetch RefSpec to this remote.
  327. *
  328. * @param s
  329. * the new specification to add.
  330. * @return true if the specification was added; false if it already exists.
  331. */
  332. public boolean addFetchRefSpec(final RefSpec s) {
  333. if (fetch.contains(s))
  334. return false;
  335. return fetch.add(s);
  336. }
  337. /**
  338. * Override existing fetch specifications with new ones.
  339. *
  340. * @param specs
  341. * list of fetch specifications to set. List is copied, it can be
  342. * modified after this call.
  343. */
  344. public void setFetchRefSpecs(final List<RefSpec> specs) {
  345. fetch.clear();
  346. fetch.addAll(specs);
  347. }
  348. /**
  349. * Override existing push specifications with new ones.
  350. *
  351. * @param specs
  352. * list of push specifications to set. List is copied, it can be
  353. * modified after this call.
  354. */
  355. public void setPushRefSpecs(final List<RefSpec> specs) {
  356. push.clear();
  357. push.addAll(specs);
  358. }
  359. /**
  360. * Remove a fetch RefSpec from this remote.
  361. *
  362. * @param s
  363. * the specification to remove.
  364. * @return true if the specification existed and was removed.
  365. */
  366. public boolean removeFetchRefSpec(final RefSpec s) {
  367. return fetch.remove(s);
  368. }
  369. /**
  370. * Remembered specifications for pushing to a repository.
  371. *
  372. * @return set of specs used by default when pushing.
  373. */
  374. public List<RefSpec> getPushRefSpecs() {
  375. return Collections.unmodifiableList(push);
  376. }
  377. /**
  378. * Add a new push RefSpec to this remote.
  379. *
  380. * @param s
  381. * the new specification to add.
  382. * @return true if the specification was added; false if it already exists.
  383. */
  384. public boolean addPushRefSpec(final RefSpec s) {
  385. if (push.contains(s))
  386. return false;
  387. return push.add(s);
  388. }
  389. /**
  390. * Remove a push RefSpec from this remote.
  391. *
  392. * @param s
  393. * the specification to remove.
  394. * @return true if the specification existed and was removed.
  395. */
  396. public boolean removePushRefSpec(final RefSpec s) {
  397. return push.remove(s);
  398. }
  399. /**
  400. * Override for the location of 'git-upload-pack' on the remote system.
  401. * <p>
  402. * This value is only useful for an SSH style connection, where Git is
  403. * asking the remote system to execute a program that provides the necessary
  404. * network protocol.
  405. *
  406. * @return location of 'git-upload-pack' on the remote system. If no
  407. * location has been configured the default of 'git-upload-pack' is
  408. * returned instead.
  409. */
  410. public String getUploadPack() {
  411. return uploadpack;
  412. }
  413. /**
  414. * Override for the location of 'git-receive-pack' on the remote system.
  415. * <p>
  416. * This value is only useful for an SSH style connection, where Git is
  417. * asking the remote system to execute a program that provides the necessary
  418. * network protocol.
  419. *
  420. * @return location of 'git-receive-pack' on the remote system. If no
  421. * location has been configured the default of 'git-receive-pack' is
  422. * returned instead.
  423. */
  424. public String getReceivePack() {
  425. return receivepack;
  426. }
  427. /**
  428. * Get the description of how annotated tags should be treated during fetch.
  429. *
  430. * @return option indicating the behavior of annotated tags in fetch.
  431. */
  432. public TagOpt getTagOpt() {
  433. return tagopt;
  434. }
  435. /**
  436. * Set the description of how annotated tags should be treated on fetch.
  437. *
  438. * @param option
  439. * method to use when handling annotated tags.
  440. */
  441. public void setTagOpt(final TagOpt option) {
  442. tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;
  443. }
  444. /**
  445. * @return true if pushing to the remote automatically deletes remote refs
  446. * which don't exist on the source side.
  447. */
  448. public boolean isMirror() {
  449. return mirror;
  450. }
  451. /**
  452. * Set the mirror flag to automatically delete remote refs.
  453. *
  454. * @param m
  455. * true to automatically delete remote refs during push.
  456. */
  457. public void setMirror(final boolean m) {
  458. mirror = m;
  459. }
  460. /** @return timeout (in seconds) before aborting an IO operation. */
  461. public int getTimeout() {
  462. return timeout;
  463. }
  464. /**
  465. * Set the timeout before willing to abort an IO call.
  466. *
  467. * @param seconds
  468. * number of seconds to wait (with no data transfer occurring)
  469. * before aborting an IO read or write operation with this
  470. * remote. A timeout of 0 will block indefinitely.
  471. */
  472. public void setTimeout(final int seconds) {
  473. timeout = seconds;
  474. }
  475. }