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.

RemoteConfig.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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.List;
  51. import org.eclipse.jgit.lib.Config;
  52. /**
  53. * A remembered remote repository, including URLs and RefSpecs.
  54. * <p>
  55. * A remote configuration remembers one or more URLs for a frequently accessed
  56. * remote repository as well as zero or more fetch and push specifications
  57. * describing how refs should be transferred between this repository and the
  58. * remote repository.
  59. */
  60. public class RemoteConfig implements Serializable {
  61. private static final long serialVersionUID = 1L;
  62. private static final String SECTION = "remote";
  63. private static final String KEY_URL = "url";
  64. private static final String KEY_PUSHURL = "pushurl";
  65. private static final String KEY_FETCH = "fetch";
  66. private static final String KEY_PUSH = "push";
  67. private static final String KEY_UPLOADPACK = "uploadpack";
  68. private static final String KEY_RECEIVEPACK = "receivepack";
  69. private static final String KEY_TAGOPT = "tagopt";
  70. private static final String KEY_MIRROR = "mirror";
  71. private static final String KEY_TIMEOUT = "timeout";
  72. private static final boolean DEFAULT_MIRROR = false;
  73. /** Default value for {@link #getUploadPack()} if not specified. */
  74. public static final String DEFAULT_UPLOAD_PACK = "git-upload-pack";
  75. /** Default value for {@link #getReceivePack()} if not specified. */
  76. public static final String DEFAULT_RECEIVE_PACK = "git-receive-pack";
  77. /**
  78. * Parse all remote blocks in an existing configuration file, looking for
  79. * remotes configuration.
  80. *
  81. * @param rc
  82. * the existing configuration to get the remote settings from.
  83. * The configuration must already be loaded into memory.
  84. * @return all remotes configurations existing in provided repository
  85. * configuration. Returned configurations are ordered
  86. * lexicographically by names.
  87. * @throws URISyntaxException
  88. * one of the URIs within the remote's configuration is invalid.
  89. */
  90. public static List<RemoteConfig> getAllRemoteConfigs(final Config rc)
  91. throws URISyntaxException {
  92. final List<String> names = new ArrayList<String>(rc
  93. .getSubsections(SECTION));
  94. Collections.sort(names);
  95. final List<RemoteConfig> result = new ArrayList<RemoteConfig>(names
  96. .size());
  97. for (final String name : names)
  98. result.add(new RemoteConfig(rc, name));
  99. return result;
  100. }
  101. private String name;
  102. private List<URIish> uris;
  103. private List<URIish> pushURIs;
  104. private List<RefSpec> fetch;
  105. private List<RefSpec> push;
  106. private String uploadpack;
  107. private String receivepack;
  108. private TagOpt tagopt;
  109. private boolean mirror;
  110. private int timeout;
  111. /**
  112. * Parse a remote block from an existing configuration file.
  113. * <p>
  114. * This constructor succeeds even if the requested remote is not defined
  115. * within the supplied configuration file. If that occurs then there will be
  116. * no URIs and no ref specifications known to the new instance.
  117. *
  118. * @param rc
  119. * the existing configuration to get the remote settings from.
  120. * The configuration must already be loaded into memory.
  121. * @param remoteName
  122. * subsection key indicating the name of this remote.
  123. * @throws URISyntaxException
  124. * one of the URIs within the remote's configuration is invalid.
  125. */
  126. public RemoteConfig(final Config rc, final String remoteName)
  127. throws URISyntaxException {
  128. name = remoteName;
  129. String[] vlst;
  130. String val;
  131. vlst = rc.getStringList(SECTION, name, KEY_URL);
  132. uris = new ArrayList<URIish>(vlst.length);
  133. for (final String s : vlst)
  134. uris.add(new URIish(s));
  135. vlst = rc.getStringList(SECTION, name, KEY_PUSHURL);
  136. pushURIs = new ArrayList<URIish>(vlst.length);
  137. for (final String s : vlst)
  138. pushURIs.add(new URIish(s));
  139. vlst = rc.getStringList(SECTION, name, KEY_FETCH);
  140. fetch = new ArrayList<RefSpec>(vlst.length);
  141. for (final String s : vlst)
  142. fetch.add(new RefSpec(s));
  143. vlst = rc.getStringList(SECTION, name, KEY_PUSH);
  144. push = new ArrayList<RefSpec>(vlst.length);
  145. for (final String s : vlst)
  146. push.add(new RefSpec(s));
  147. val = rc.getString(SECTION, name, KEY_UPLOADPACK);
  148. if (val == null)
  149. val = DEFAULT_UPLOAD_PACK;
  150. uploadpack = val;
  151. val = rc.getString(SECTION, name, KEY_RECEIVEPACK);
  152. if (val == null)
  153. val = DEFAULT_RECEIVE_PACK;
  154. receivepack = val;
  155. val = rc.getString(SECTION, name, KEY_TAGOPT);
  156. tagopt = TagOpt.fromOption(val);
  157. mirror = rc.getBoolean(SECTION, name, KEY_MIRROR, DEFAULT_MIRROR);
  158. timeout = rc.getInt(SECTION, name, KEY_TIMEOUT, 0);
  159. }
  160. /**
  161. * Update this remote's definition within the configuration.
  162. *
  163. * @param rc
  164. * the configuration file to store ourselves into.
  165. */
  166. public void update(final Config rc) {
  167. final List<String> vlst = new ArrayList<String>();
  168. vlst.clear();
  169. for (final URIish u : getURIs())
  170. vlst.add(u.toPrivateString());
  171. rc.setStringList(SECTION, getName(), KEY_URL, vlst);
  172. vlst.clear();
  173. for (final URIish u : getPushURIs())
  174. vlst.add(u.toPrivateString());
  175. rc.setStringList(SECTION, getName(), KEY_PUSHURL, vlst);
  176. vlst.clear();
  177. for (final RefSpec u : getFetchRefSpecs())
  178. vlst.add(u.toString());
  179. rc.setStringList(SECTION, getName(), KEY_FETCH, vlst);
  180. vlst.clear();
  181. for (final RefSpec u : getPushRefSpecs())
  182. vlst.add(u.toString());
  183. rc.setStringList(SECTION, getName(), KEY_PUSH, vlst);
  184. set(rc, KEY_UPLOADPACK, getUploadPack(), DEFAULT_UPLOAD_PACK);
  185. set(rc, KEY_RECEIVEPACK, getReceivePack(), DEFAULT_RECEIVE_PACK);
  186. set(rc, KEY_TAGOPT, getTagOpt().option(), TagOpt.AUTO_FOLLOW.option());
  187. set(rc, KEY_MIRROR, mirror, DEFAULT_MIRROR);
  188. set(rc, KEY_TIMEOUT, timeout, 0);
  189. }
  190. private void set(final Config rc, final String key,
  191. final String currentValue, final String defaultValue) {
  192. if (defaultValue.equals(currentValue))
  193. unset(rc, key);
  194. else
  195. rc.setString(SECTION, getName(), key, currentValue);
  196. }
  197. private void set(final Config rc, final String key,
  198. final boolean currentValue, final boolean defaultValue) {
  199. if (defaultValue == currentValue)
  200. unset(rc, key);
  201. else
  202. rc.setBoolean(SECTION, getName(), key, currentValue);
  203. }
  204. private void set(final Config rc, final String key, final int currentValue,
  205. final int defaultValue) {
  206. if (defaultValue == currentValue)
  207. unset(rc, key);
  208. else
  209. rc.setInt(SECTION, getName(), key, currentValue);
  210. }
  211. private void unset(final Config rc, final String key) {
  212. rc.unset(SECTION, getName(), key);
  213. }
  214. /**
  215. * Get the local name this remote configuration is recognized as.
  216. *
  217. * @return name assigned by the user to this configuration block.
  218. */
  219. public String getName() {
  220. return name;
  221. }
  222. /**
  223. * Get all configured URIs under this remote.
  224. *
  225. * @return the set of URIs known to this remote.
  226. */
  227. public List<URIish> getURIs() {
  228. return Collections.unmodifiableList(uris);
  229. }
  230. /**
  231. * Add a new URI to the end of the list of URIs.
  232. *
  233. * @param toAdd
  234. * the new URI to add to this remote.
  235. * @return true if the URI was added; false if it already exists.
  236. */
  237. public boolean addURI(final URIish toAdd) {
  238. if (uris.contains(toAdd))
  239. return false;
  240. return uris.add(toAdd);
  241. }
  242. /**
  243. * Remove a URI from the list of URIs.
  244. *
  245. * @param toRemove
  246. * the URI to remove from this remote.
  247. * @return true if the URI was added; false if it already exists.
  248. */
  249. public boolean removeURI(final URIish toRemove) {
  250. return uris.remove(toRemove);
  251. }
  252. /**
  253. * Get all configured push-only URIs under this remote.
  254. *
  255. * @return the set of URIs known to this remote.
  256. */
  257. public List<URIish> getPushURIs() {
  258. return Collections.unmodifiableList(pushURIs);
  259. }
  260. /**
  261. * Add a new push-only URI to the end of the list of URIs.
  262. *
  263. * @param toAdd
  264. * the new URI to add to this remote.
  265. * @return true if the URI was added; false if it already exists.
  266. */
  267. public boolean addPushURI(final URIish toAdd) {
  268. if (pushURIs.contains(toAdd))
  269. return false;
  270. return pushURIs.add(toAdd);
  271. }
  272. /**
  273. * Remove a push-only URI from the list of URIs.
  274. *
  275. * @param toRemove
  276. * the URI to remove from this remote.
  277. * @return true if the URI was added; false if it already exists.
  278. */
  279. public boolean removePushURI(final URIish toRemove) {
  280. return pushURIs.remove(toRemove);
  281. }
  282. /**
  283. * Remembered specifications for fetching from a repository.
  284. *
  285. * @return set of specs used by default when fetching.
  286. */
  287. public List<RefSpec> getFetchRefSpecs() {
  288. return Collections.unmodifiableList(fetch);
  289. }
  290. /**
  291. * Add a new fetch RefSpec to this remote.
  292. *
  293. * @param s
  294. * the new specification to add.
  295. * @return true if the specification was added; false if it already exists.
  296. */
  297. public boolean addFetchRefSpec(final RefSpec s) {
  298. if (fetch.contains(s))
  299. return false;
  300. return fetch.add(s);
  301. }
  302. /**
  303. * Override existing fetch specifications with new ones.
  304. *
  305. * @param specs
  306. * list of fetch specifications to set. List is copied, it can be
  307. * modified after this call.
  308. */
  309. public void setFetchRefSpecs(final List<RefSpec> specs) {
  310. fetch.clear();
  311. fetch.addAll(specs);
  312. }
  313. /**
  314. * Override existing push specifications with new ones.
  315. *
  316. * @param specs
  317. * list of push specifications to set. List is copied, it can be
  318. * modified after this call.
  319. */
  320. public void setPushRefSpecs(final List<RefSpec> specs) {
  321. push.clear();
  322. push.addAll(specs);
  323. }
  324. /**
  325. * Remove a fetch RefSpec from this remote.
  326. *
  327. * @param s
  328. * the specification to remove.
  329. * @return true if the specification existed and was removed.
  330. */
  331. public boolean removeFetchRefSpec(final RefSpec s) {
  332. return fetch.remove(s);
  333. }
  334. /**
  335. * Remembered specifications for pushing to a repository.
  336. *
  337. * @return set of specs used by default when pushing.
  338. */
  339. public List<RefSpec> getPushRefSpecs() {
  340. return Collections.unmodifiableList(push);
  341. }
  342. /**
  343. * Add a new push RefSpec to this remote.
  344. *
  345. * @param s
  346. * the new specification to add.
  347. * @return true if the specification was added; false if it already exists.
  348. */
  349. public boolean addPushRefSpec(final RefSpec s) {
  350. if (push.contains(s))
  351. return false;
  352. return push.add(s);
  353. }
  354. /**
  355. * Remove a push RefSpec from this remote.
  356. *
  357. * @param s
  358. * the specification to remove.
  359. * @return true if the specification existed and was removed.
  360. */
  361. public boolean removePushRefSpec(final RefSpec s) {
  362. return push.remove(s);
  363. }
  364. /**
  365. * Override for the location of 'git-upload-pack' on the remote system.
  366. * <p>
  367. * This value is only useful for an SSH style connection, where Git is
  368. * asking the remote system to execute a program that provides the necessary
  369. * network protocol.
  370. *
  371. * @return location of 'git-upload-pack' on the remote system. If no
  372. * location has been configured the default of 'git-upload-pack' is
  373. * returned instead.
  374. */
  375. public String getUploadPack() {
  376. return uploadpack;
  377. }
  378. /**
  379. * Override for the location of 'git-receive-pack' on the remote system.
  380. * <p>
  381. * This value is only useful for an SSH style connection, where Git is
  382. * asking the remote system to execute a program that provides the necessary
  383. * network protocol.
  384. *
  385. * @return location of 'git-receive-pack' on the remote system. If no
  386. * location has been configured the default of 'git-receive-pack' is
  387. * returned instead.
  388. */
  389. public String getReceivePack() {
  390. return receivepack;
  391. }
  392. /**
  393. * Get the description of how annotated tags should be treated during fetch.
  394. *
  395. * @return option indicating the behavior of annotated tags in fetch.
  396. */
  397. public TagOpt getTagOpt() {
  398. return tagopt;
  399. }
  400. /**
  401. * Set the description of how annotated tags should be treated on fetch.
  402. *
  403. * @param option
  404. * method to use when handling annotated tags.
  405. */
  406. public void setTagOpt(final TagOpt option) {
  407. tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;
  408. }
  409. /**
  410. * @return true if pushing to the remote automatically deletes remote refs
  411. * which don't exist on the source side.
  412. */
  413. public boolean isMirror() {
  414. return mirror;
  415. }
  416. /**
  417. * Set the mirror flag to automatically delete remote refs.
  418. *
  419. * @param m
  420. * true to automatically delete remote refs during push.
  421. */
  422. public void setMirror(final boolean m) {
  423. mirror = m;
  424. }
  425. /** @return timeout (in seconds) before aborting an IO operation. */
  426. public int getTimeout() {
  427. return timeout;
  428. }
  429. /**
  430. * Set the timeout before willing to abort an IO call.
  431. *
  432. * @param seconds
  433. * number of seconds to wait (with no data transfer occurring)
  434. * before aborting an IO read or write operation with this
  435. * remote. A timeout of 0 will block indefinitely.
  436. */
  437. public void setTimeout(final int seconds) {
  438. timeout = seconds;
  439. }
  440. }