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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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"; //$NON-NLS-1$
  66. private static final String KEY_URL = "url"; //$NON-NLS-1$
  67. private static final String KEY_PUSHURL = "pushurl"; //$NON-NLS-1$
  68. private static final String KEY_FETCH = "fetch"; //$NON-NLS-1$
  69. private static final String KEY_PUSH = "push"; //$NON-NLS-1$
  70. private static final String KEY_UPLOADPACK = "uploadpack"; //$NON-NLS-1$
  71. private static final String KEY_RECEIVEPACK = "receivepack"; //$NON-NLS-1$
  72. private static final String KEY_TAGOPT = "tagopt"; //$NON-NLS-1$
  73. private static final String KEY_MIRROR = "mirror"; //$NON-NLS-1$
  74. private static final String KEY_TIMEOUT = "timeout"; //$NON-NLS-1$
  75. private static final String KEY_INSTEADOF = "insteadof"; //$NON-NLS-1$
  76. private static final String KEY_PUSHINSTEADOF = "pushinsteadof"; //$NON-NLS-1$
  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"; //$NON-NLS-1$
  80. /** Default value for {@link #getReceivePack()} if not specified. */
  81. public static final String DEFAULT_RECEIVE_PACK = "git-receive-pack"; //$NON-NLS-1$
  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 java.net.URISyntaxException
  93. * one of the URIs within the remote's configuration is invalid.
  94. */
  95. public static List<RemoteConfig> getAllRemoteConfigs(Config rc)
  96. throws URISyntaxException {
  97. final List<String> names = new ArrayList<>(rc
  98. .getSubsections(SECTION));
  99. Collections.sort(names);
  100. final List<RemoteConfig> result = new ArrayList<>(names
  101. .size());
  102. for (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 java.net.URISyntaxException
  129. * one of the URIs within the remote's configuration is invalid.
  130. */
  131. public RemoteConfig(Config rc, 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<>(vlst.length);
  139. for (String s : vlst) {
  140. uris.add(new URIish(replaceUri(s, insteadOf)));
  141. }
  142. String[] plst = rc.getStringList(SECTION, name, KEY_PUSHURL);
  143. pushURIs = new ArrayList<>(plst.length);
  144. for (String s : plst) {
  145. pushURIs.add(new URIish(s));
  146. }
  147. if (pushURIs.isEmpty()) {
  148. // Would default to the uris. If we have pushinsteadof, we must
  149. // supply rewritten push uris.
  150. Map<String, String> pushInsteadOf = getReplacements(rc,
  151. KEY_PUSHINSTEADOF);
  152. if (!pushInsteadOf.isEmpty()) {
  153. for (String s : vlst) {
  154. String replaced = replaceUri(s, pushInsteadOf);
  155. if (!s.equals(replaced)) {
  156. pushURIs.add(new URIish(replaced));
  157. }
  158. }
  159. }
  160. }
  161. fetch = rc.getRefSpecs(SECTION, name, KEY_FETCH);
  162. push = rc.getRefSpecs(SECTION, name, KEY_PUSH);
  163. val = rc.getString(SECTION, name, KEY_UPLOADPACK);
  164. if (val == null) {
  165. val = DEFAULT_UPLOAD_PACK;
  166. }
  167. uploadpack = val;
  168. val = rc.getString(SECTION, name, KEY_RECEIVEPACK);
  169. if (val == null) {
  170. val = DEFAULT_RECEIVE_PACK;
  171. }
  172. receivepack = val;
  173. try {
  174. val = rc.getString(SECTION, name, KEY_TAGOPT);
  175. tagopt = TagOpt.fromOption(val);
  176. } catch (IllegalArgumentException e) {
  177. // C git silently ignores invalid tagopt values.
  178. tagopt = TagOpt.AUTO_FOLLOW;
  179. }
  180. mirror = rc.getBoolean(SECTION, name, KEY_MIRROR, DEFAULT_MIRROR);
  181. timeout = rc.getInt(SECTION, name, KEY_TIMEOUT, 0);
  182. }
  183. /**
  184. * Update this remote's definition within the configuration.
  185. *
  186. * @param rc
  187. * the configuration file to store ourselves into.
  188. */
  189. public void update(Config rc) {
  190. final List<String> vlst = new ArrayList<>();
  191. vlst.clear();
  192. for (URIish u : getURIs())
  193. vlst.add(u.toPrivateString());
  194. rc.setStringList(SECTION, getName(), KEY_URL, vlst);
  195. vlst.clear();
  196. for (URIish u : getPushURIs())
  197. vlst.add(u.toPrivateString());
  198. rc.setStringList(SECTION, getName(), KEY_PUSHURL, vlst);
  199. vlst.clear();
  200. for (RefSpec u : getFetchRefSpecs())
  201. vlst.add(u.toString());
  202. rc.setStringList(SECTION, getName(), KEY_FETCH, vlst);
  203. vlst.clear();
  204. for (RefSpec u : getPushRefSpecs())
  205. vlst.add(u.toString());
  206. rc.setStringList(SECTION, getName(), KEY_PUSH, vlst);
  207. set(rc, KEY_UPLOADPACK, getUploadPack(), DEFAULT_UPLOAD_PACK);
  208. set(rc, KEY_RECEIVEPACK, getReceivePack(), DEFAULT_RECEIVE_PACK);
  209. set(rc, KEY_TAGOPT, getTagOpt().option(), TagOpt.AUTO_FOLLOW.option());
  210. set(rc, KEY_MIRROR, mirror, DEFAULT_MIRROR);
  211. set(rc, KEY_TIMEOUT, timeout, 0);
  212. }
  213. private void set(final Config rc, final String key,
  214. final String currentValue, final String defaultValue) {
  215. if (defaultValue.equals(currentValue))
  216. unset(rc, key);
  217. else
  218. rc.setString(SECTION, getName(), key, currentValue);
  219. }
  220. private void set(final Config rc, final String key,
  221. final boolean currentValue, final boolean defaultValue) {
  222. if (defaultValue == currentValue)
  223. unset(rc, key);
  224. else
  225. rc.setBoolean(SECTION, getName(), key, currentValue);
  226. }
  227. private void set(final Config rc, final String key, final int currentValue,
  228. final int defaultValue) {
  229. if (defaultValue == currentValue)
  230. unset(rc, key);
  231. else
  232. rc.setInt(SECTION, getName(), key, currentValue);
  233. }
  234. private void unset(Config rc, String key) {
  235. rc.unset(SECTION, getName(), key);
  236. }
  237. private Map<String, String> getReplacements(final Config config,
  238. final String keyName) {
  239. final Map<String, String> replacements = new HashMap<>();
  240. for (String url : config.getSubsections(KEY_URL))
  241. for (String insteadOf : config.getStringList(KEY_URL, url, keyName))
  242. replacements.put(insteadOf, url);
  243. return replacements;
  244. }
  245. private String replaceUri(final String uri,
  246. final Map<String, String> replacements) {
  247. if (replacements.isEmpty())
  248. return uri;
  249. Entry<String, String> match = null;
  250. for (Entry<String, String> replacement : replacements.entrySet()) {
  251. // Ignore current entry if not longer than previous match
  252. if (match != null
  253. && match.getKey().length() > replacement.getKey().length())
  254. continue;
  255. if (!uri.startsWith(replacement.getKey()))
  256. continue;
  257. match = replacement;
  258. }
  259. if (match != null)
  260. return match.getValue() + uri.substring(match.getKey().length());
  261. else
  262. return uri;
  263. }
  264. /**
  265. * Get the local name this remote configuration is recognized as.
  266. *
  267. * @return name assigned by the user to this configuration block.
  268. */
  269. public String getName() {
  270. return name;
  271. }
  272. /**
  273. * Get all configured URIs under this remote.
  274. *
  275. * @return the set of URIs known to this remote.
  276. */
  277. public List<URIish> getURIs() {
  278. return Collections.unmodifiableList(uris);
  279. }
  280. /**
  281. * Add a new URI to the end of the list of URIs.
  282. *
  283. * @param toAdd
  284. * the new URI to add to this remote.
  285. * @return true if the URI was added; false if it already exists.
  286. */
  287. public boolean addURI(URIish toAdd) {
  288. if (uris.contains(toAdd))
  289. return false;
  290. return uris.add(toAdd);
  291. }
  292. /**
  293. * Remove a URI from the list of URIs.
  294. *
  295. * @param toRemove
  296. * the URI to remove from this remote.
  297. * @return true if the URI was added; false if it already exists.
  298. */
  299. public boolean removeURI(URIish toRemove) {
  300. return uris.remove(toRemove);
  301. }
  302. /**
  303. * Get all configured push-only URIs under this remote.
  304. *
  305. * @return the set of URIs known to this remote.
  306. */
  307. public List<URIish> getPushURIs() {
  308. return Collections.unmodifiableList(pushURIs);
  309. }
  310. /**
  311. * Add a new push-only URI to the end of the list of URIs.
  312. *
  313. * @param toAdd
  314. * the new URI to add to this remote.
  315. * @return true if the URI was added; false if it already exists.
  316. */
  317. public boolean addPushURI(URIish toAdd) {
  318. if (pushURIs.contains(toAdd))
  319. return false;
  320. return pushURIs.add(toAdd);
  321. }
  322. /**
  323. * Remove a push-only URI from the list of URIs.
  324. *
  325. * @param toRemove
  326. * the URI to remove from this remote.
  327. * @return true if the URI was added; false if it already exists.
  328. */
  329. public boolean removePushURI(URIish toRemove) {
  330. return pushURIs.remove(toRemove);
  331. }
  332. /**
  333. * Remembered specifications for fetching from a repository.
  334. *
  335. * @return set of specs used by default when fetching.
  336. */
  337. public List<RefSpec> getFetchRefSpecs() {
  338. return Collections.unmodifiableList(fetch);
  339. }
  340. /**
  341. * Add a new fetch RefSpec to this remote.
  342. *
  343. * @param s
  344. * the new specification to add.
  345. * @return true if the specification was added; false if it already exists.
  346. */
  347. public boolean addFetchRefSpec(RefSpec s) {
  348. if (fetch.contains(s))
  349. return false;
  350. return fetch.add(s);
  351. }
  352. /**
  353. * Override existing fetch specifications with new ones.
  354. *
  355. * @param specs
  356. * list of fetch specifications to set. List is copied, it can be
  357. * modified after this call.
  358. */
  359. public void setFetchRefSpecs(List<RefSpec> specs) {
  360. fetch.clear();
  361. fetch.addAll(specs);
  362. }
  363. /**
  364. * Override existing push specifications with new ones.
  365. *
  366. * @param specs
  367. * list of push specifications to set. List is copied, it can be
  368. * modified after this call.
  369. */
  370. public void setPushRefSpecs(List<RefSpec> specs) {
  371. push.clear();
  372. push.addAll(specs);
  373. }
  374. /**
  375. * Remove a fetch RefSpec from this remote.
  376. *
  377. * @param s
  378. * the specification to remove.
  379. * @return true if the specification existed and was removed.
  380. */
  381. public boolean removeFetchRefSpec(RefSpec s) {
  382. return fetch.remove(s);
  383. }
  384. /**
  385. * Remembered specifications for pushing to a repository.
  386. *
  387. * @return set of specs used by default when pushing.
  388. */
  389. public List<RefSpec> getPushRefSpecs() {
  390. return Collections.unmodifiableList(push);
  391. }
  392. /**
  393. * Add a new push RefSpec to this remote.
  394. *
  395. * @param s
  396. * the new specification to add.
  397. * @return true if the specification was added; false if it already exists.
  398. */
  399. public boolean addPushRefSpec(RefSpec s) {
  400. if (push.contains(s))
  401. return false;
  402. return push.add(s);
  403. }
  404. /**
  405. * Remove a push RefSpec from this remote.
  406. *
  407. * @param s
  408. * the specification to remove.
  409. * @return true if the specification existed and was removed.
  410. */
  411. public boolean removePushRefSpec(RefSpec s) {
  412. return push.remove(s);
  413. }
  414. /**
  415. * Override for the location of 'git-upload-pack' on the remote system.
  416. * <p>
  417. * This value is only useful for an SSH style connection, where Git is
  418. * asking the remote system to execute a program that provides the necessary
  419. * network protocol.
  420. *
  421. * @return location of 'git-upload-pack' on the remote system. If no
  422. * location has been configured the default of 'git-upload-pack' is
  423. * returned instead.
  424. */
  425. public String getUploadPack() {
  426. return uploadpack;
  427. }
  428. /**
  429. * Override for the location of 'git-receive-pack' on the remote system.
  430. * <p>
  431. * This value is only useful for an SSH style connection, where Git is
  432. * asking the remote system to execute a program that provides the necessary
  433. * network protocol.
  434. *
  435. * @return location of 'git-receive-pack' on the remote system. If no
  436. * location has been configured the default of 'git-receive-pack' is
  437. * returned instead.
  438. */
  439. public String getReceivePack() {
  440. return receivepack;
  441. }
  442. /**
  443. * Get the description of how annotated tags should be treated during fetch.
  444. *
  445. * @return option indicating the behavior of annotated tags in fetch.
  446. */
  447. public TagOpt getTagOpt() {
  448. return tagopt;
  449. }
  450. /**
  451. * Set the description of how annotated tags should be treated on fetch.
  452. *
  453. * @param option
  454. * method to use when handling annotated tags.
  455. */
  456. public void setTagOpt(TagOpt option) {
  457. tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;
  458. }
  459. /**
  460. * Whether pushing to the remote automatically deletes remote refs which
  461. * don't exist on the source side.
  462. *
  463. * @return true if pushing to the remote automatically deletes remote refs
  464. * which don't exist on the source side.
  465. */
  466. public boolean isMirror() {
  467. return mirror;
  468. }
  469. /**
  470. * Set the mirror flag to automatically delete remote refs.
  471. *
  472. * @param m
  473. * true to automatically delete remote refs during push.
  474. */
  475. public void setMirror(boolean m) {
  476. mirror = m;
  477. }
  478. /**
  479. * Get timeout (in seconds) before aborting an IO operation.
  480. *
  481. * @return timeout (in seconds) before aborting an IO operation.
  482. */
  483. public int getTimeout() {
  484. return timeout;
  485. }
  486. /**
  487. * Set the timeout before willing to abort an IO call.
  488. *
  489. * @param seconds
  490. * number of seconds to wait (with no data transfer occurring)
  491. * before aborting an IO read or write operation with this
  492. * remote. A timeout of 0 will block indefinitely.
  493. */
  494. public void setTimeout(int seconds) {
  495. timeout = seconds;
  496. }
  497. }