RemoteConfig.java

  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> and others
  5.  *
  6.  * This program and the accompanying materials are made available under the
  7.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  8.  * https://www.eclipse.org/org/documents/edl-v10.php.
  9.  *
  10.  * SPDX-License-Identifier: BSD-3-Clause
  11.  */

  12. package org.eclipse.jgit.transport;

  13. import java.io.Serializable;
  14. import java.net.URISyntaxException;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17. import java.util.List;

  18. import org.eclipse.jgit.lib.Config;

  19. /**
  20.  * A remembered remote repository, including URLs and RefSpecs.
  21.  * <p>
  22.  * A remote configuration remembers one or more URLs for a frequently accessed
  23.  * remote repository as well as zero or more fetch and push specifications
  24.  * describing how refs should be transferred between this repository and the
  25.  * remote repository.
  26.  */
  27. public class RemoteConfig implements Serializable {
  28.     private static final long serialVersionUID = 1L;

  29.     private static final String SECTION = "remote"; //$NON-NLS-1$

  30.     private static final String KEY_URL = "url"; //$NON-NLS-1$

  31.     private static final String KEY_PUSHURL = "pushurl"; //$NON-NLS-1$

  32.     private static final String KEY_FETCH = "fetch"; //$NON-NLS-1$

  33.     private static final String KEY_PUSH = "push"; //$NON-NLS-1$

  34.     private static final String KEY_UPLOADPACK = "uploadpack"; //$NON-NLS-1$

  35.     private static final String KEY_RECEIVEPACK = "receivepack"; //$NON-NLS-1$

  36.     private static final String KEY_TAGOPT = "tagopt"; //$NON-NLS-1$

  37.     private static final String KEY_MIRROR = "mirror"; //$NON-NLS-1$

  38.     private static final String KEY_TIMEOUT = "timeout"; //$NON-NLS-1$

  39.     private static final boolean DEFAULT_MIRROR = false;

  40.     /** Default value for {@link #getUploadPack()} if not specified. */
  41.     public static final String DEFAULT_UPLOAD_PACK = "git-upload-pack"; //$NON-NLS-1$

  42.     /** Default value for {@link #getReceivePack()} if not specified. */
  43.     public static final String DEFAULT_RECEIVE_PACK = "git-receive-pack"; //$NON-NLS-1$

  44.     /**
  45.      * Parse all remote blocks in an existing configuration file, looking for
  46.      * remotes configuration.
  47.      *
  48.      * @param rc
  49.      *            the existing configuration to get the remote settings from.
  50.      *            The configuration must already be loaded into memory.
  51.      * @return all remotes configurations existing in provided repository
  52.      *         configuration. Returned configurations are ordered
  53.      *         lexicographically by names.
  54.      * @throws java.net.URISyntaxException
  55.      *             one of the URIs within the remote's configuration is invalid.
  56.      */
  57.     public static List<RemoteConfig> getAllRemoteConfigs(Config rc)
  58.             throws URISyntaxException {
  59.         final List<String> names = new ArrayList<>(rc
  60.                 .getSubsections(SECTION));
  61.         Collections.sort(names);

  62.         final List<RemoteConfig> result = new ArrayList<>(names
  63.                 .size());
  64.         for (String name : names)
  65.             result.add(new RemoteConfig(rc, name));
  66.         return result;
  67.     }

  68.     private String name;

  69.     private List<URIish> uris;

  70.     private List<URIish> pushURIs;

  71.     private List<RefSpec> fetch;

  72.     private List<RefSpec> push;

  73.     private String uploadpack;

  74.     private String receivepack;

  75.     private TagOpt tagopt;

  76.     private boolean mirror;

  77.     private int timeout;

  78.     /**
  79.      * Parse a remote block from an existing configuration file.
  80.      * <p>
  81.      * This constructor succeeds even if the requested remote is not defined
  82.      * within the supplied configuration file. If that occurs then there will be
  83.      * no URIs and no ref specifications known to the new instance.
  84.      *
  85.      * @param rc
  86.      *            the existing configuration to get the remote settings from.
  87.      *            The configuration must already be loaded into memory.
  88.      * @param remoteName
  89.      *            subsection key indicating the name of this remote.
  90.      * @throws java.net.URISyntaxException
  91.      *             one of the URIs within the remote's configuration is invalid.
  92.      */
  93.     public RemoteConfig(Config rc, String remoteName)
  94.             throws URISyntaxException {
  95.         name = remoteName;

  96.         String[] vlst;
  97.         String val;

  98.         vlst = rc.getStringList(SECTION, name, KEY_URL);
  99.         UrlConfig urls = new UrlConfig(rc);
  100.         uris = new ArrayList<>(vlst.length);
  101.         for (String s : vlst) {
  102.             uris.add(new URIish(urls.replace(s)));
  103.         }
  104.         String[] plst = rc.getStringList(SECTION, name, KEY_PUSHURL);
  105.         pushURIs = new ArrayList<>(plst.length);
  106.         for (String s : plst) {
  107.             pushURIs.add(new URIish(s));
  108.         }
  109.         if (pushURIs.isEmpty()) {
  110.             // Would default to the uris. If we have pushinsteadof, we must
  111.             // supply rewritten push uris.
  112.             if (urls.hasPushReplacements()) {
  113.                 for (String s : vlst) {
  114.                     String replaced = urls.replacePush(s);
  115.                     if (!s.equals(replaced)) {
  116.                         pushURIs.add(new URIish(replaced));
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.         fetch = rc.getRefSpecs(SECTION, name, KEY_FETCH);
  122.         push = rc.getRefSpecs(SECTION, name, KEY_PUSH);
  123.         val = rc.getString(SECTION, name, KEY_UPLOADPACK);
  124.         if (val == null) {
  125.             val = DEFAULT_UPLOAD_PACK;
  126.         }
  127.         uploadpack = val;

  128.         val = rc.getString(SECTION, name, KEY_RECEIVEPACK);
  129.         if (val == null) {
  130.             val = DEFAULT_RECEIVE_PACK;
  131.         }
  132.         receivepack = val;

  133.         try {
  134.             val = rc.getString(SECTION, name, KEY_TAGOPT);
  135.             tagopt = TagOpt.fromOption(val);
  136.         } catch (IllegalArgumentException e) {
  137.             // C git silently ignores invalid tagopt values.
  138.             tagopt = TagOpt.AUTO_FOLLOW;
  139.         }
  140.         mirror = rc.getBoolean(SECTION, name, KEY_MIRROR, DEFAULT_MIRROR);
  141.         timeout = rc.getInt(SECTION, name, KEY_TIMEOUT, 0);
  142.     }

  143.     /**
  144.      * Update this remote's definition within the configuration.
  145.      *
  146.      * @param rc
  147.      *            the configuration file to store ourselves into.
  148.      */
  149.     public void update(Config rc) {
  150.         final List<String> vlst = new ArrayList<>();

  151.         vlst.clear();
  152.         for (URIish u : getURIs())
  153.             vlst.add(u.toPrivateString());
  154.         rc.setStringList(SECTION, getName(), KEY_URL, vlst);

  155.         vlst.clear();
  156.         for (URIish u : getPushURIs())
  157.             vlst.add(u.toPrivateString());
  158.         rc.setStringList(SECTION, getName(), KEY_PUSHURL, vlst);

  159.         vlst.clear();
  160.         for (RefSpec u : getFetchRefSpecs())
  161.             vlst.add(u.toString());
  162.         rc.setStringList(SECTION, getName(), KEY_FETCH, vlst);

  163.         vlst.clear();
  164.         for (RefSpec u : getPushRefSpecs())
  165.             vlst.add(u.toString());
  166.         rc.setStringList(SECTION, getName(), KEY_PUSH, vlst);

  167.         set(rc, KEY_UPLOADPACK, getUploadPack(), DEFAULT_UPLOAD_PACK);
  168.         set(rc, KEY_RECEIVEPACK, getReceivePack(), DEFAULT_RECEIVE_PACK);
  169.         set(rc, KEY_TAGOPT, getTagOpt().option(), TagOpt.AUTO_FOLLOW.option());
  170.         set(rc, KEY_MIRROR, mirror, DEFAULT_MIRROR);
  171.         set(rc, KEY_TIMEOUT, timeout, 0);
  172.     }

  173.     private void set(final Config rc, final String key,
  174.             final String currentValue, final String defaultValue) {
  175.         if (defaultValue.equals(currentValue))
  176.             unset(rc, key);
  177.         else
  178.             rc.setString(SECTION, getName(), key, currentValue);
  179.     }

  180.     private void set(final Config rc, final String key,
  181.             final boolean currentValue, final boolean defaultValue) {
  182.         if (defaultValue == currentValue)
  183.             unset(rc, key);
  184.         else
  185.             rc.setBoolean(SECTION, getName(), key, currentValue);
  186.     }

  187.     private void set(final Config rc, final String key, final int currentValue,
  188.             final int defaultValue) {
  189.         if (defaultValue == currentValue)
  190.             unset(rc, key);
  191.         else
  192.             rc.setInt(SECTION, getName(), key, currentValue);
  193.     }

  194.     private void unset(Config rc, String key) {
  195.         rc.unset(SECTION, getName(), key);
  196.     }

  197.     /**
  198.      * Get the local name this remote configuration is recognized as.
  199.      *
  200.      * @return name assigned by the user to this configuration block.
  201.      */
  202.     public String getName() {
  203.         return name;
  204.     }

  205.     /**
  206.      * Get all configured URIs under this remote.
  207.      *
  208.      * @return the set of URIs known to this remote.
  209.      */
  210.     public List<URIish> getURIs() {
  211.         return Collections.unmodifiableList(uris);
  212.     }

  213.     /**
  214.      * Add a new URI to the end of the list of URIs.
  215.      *
  216.      * @param toAdd
  217.      *            the new URI to add to this remote.
  218.      * @return true if the URI was added; false if it already exists.
  219.      */
  220.     public boolean addURI(URIish toAdd) {
  221.         if (uris.contains(toAdd))
  222.             return false;
  223.         return uris.add(toAdd);
  224.     }

  225.     /**
  226.      * Remove a URI from the list of URIs.
  227.      *
  228.      * @param toRemove
  229.      *            the URI to remove from this remote.
  230.      * @return true if the URI was added; false if it already exists.
  231.      */
  232.     public boolean removeURI(URIish toRemove) {
  233.         return uris.remove(toRemove);
  234.     }

  235.     /**
  236.      * Get all configured push-only URIs under this remote.
  237.      *
  238.      * @return the set of URIs known to this remote.
  239.      */
  240.     public List<URIish> getPushURIs() {
  241.         return Collections.unmodifiableList(pushURIs);
  242.     }

  243.     /**
  244.      * Add a new push-only URI to the end of the list of URIs.
  245.      *
  246.      * @param toAdd
  247.      *            the new URI to add to this remote.
  248.      * @return true if the URI was added; false if it already exists.
  249.      */
  250.     public boolean addPushURI(URIish toAdd) {
  251.         if (pushURIs.contains(toAdd))
  252.             return false;
  253.         return pushURIs.add(toAdd);
  254.     }

  255.     /**
  256.      * Remove a push-only URI from the list of URIs.
  257.      *
  258.      * @param toRemove
  259.      *            the URI to remove from this remote.
  260.      * @return true if the URI was added; false if it already exists.
  261.      */
  262.     public boolean removePushURI(URIish toRemove) {
  263.         return pushURIs.remove(toRemove);
  264.     }

  265.     /**
  266.      * Remembered specifications for fetching from a repository.
  267.      *
  268.      * @return set of specs used by default when fetching.
  269.      */
  270.     public List<RefSpec> getFetchRefSpecs() {
  271.         return Collections.unmodifiableList(fetch);
  272.     }

  273.     /**
  274.      * Add a new fetch RefSpec to this remote.
  275.      *
  276.      * @param s
  277.      *            the new specification to add.
  278.      * @return true if the specification was added; false if it already exists.
  279.      */
  280.     public boolean addFetchRefSpec(RefSpec s) {
  281.         if (fetch.contains(s))
  282.             return false;
  283.         return fetch.add(s);
  284.     }

  285.     /**
  286.      * Override existing fetch specifications with new ones.
  287.      *
  288.      * @param specs
  289.      *            list of fetch specifications to set. List is copied, it can be
  290.      *            modified after this call.
  291.      */
  292.     public void setFetchRefSpecs(List<RefSpec> specs) {
  293.         fetch.clear();
  294.         fetch.addAll(specs);
  295.     }

  296.     /**
  297.      * Override existing push specifications with new ones.
  298.      *
  299.      * @param specs
  300.      *            list of push specifications to set. List is copied, it can be
  301.      *            modified after this call.
  302.      */
  303.     public void setPushRefSpecs(List<RefSpec> specs) {
  304.         push.clear();
  305.         push.addAll(specs);
  306.     }

  307.     /**
  308.      * Remove a fetch RefSpec from this remote.
  309.      *
  310.      * @param s
  311.      *            the specification to remove.
  312.      * @return true if the specification existed and was removed.
  313.      */
  314.     public boolean removeFetchRefSpec(RefSpec s) {
  315.         return fetch.remove(s);
  316.     }

  317.     /**
  318.      * Remembered specifications for pushing to a repository.
  319.      *
  320.      * @return set of specs used by default when pushing.
  321.      */
  322.     public List<RefSpec> getPushRefSpecs() {
  323.         return Collections.unmodifiableList(push);
  324.     }

  325.     /**
  326.      * Add a new push 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 addPushRefSpec(RefSpec s) {
  333.         if (push.contains(s))
  334.             return false;
  335.         return push.add(s);
  336.     }

  337.     /**
  338.      * Remove a push RefSpec from this remote.
  339.      *
  340.      * @param s
  341.      *            the specification to remove.
  342.      * @return true if the specification existed and was removed.
  343.      */
  344.     public boolean removePushRefSpec(RefSpec s) {
  345.         return push.remove(s);
  346.     }

  347.     /**
  348.      * Override for the location of 'git-upload-pack' on the remote system.
  349.      * <p>
  350.      * This value is only useful for an SSH style connection, where Git is
  351.      * asking the remote system to execute a program that provides the necessary
  352.      * network protocol.
  353.      *
  354.      * @return location of 'git-upload-pack' on the remote system. If no
  355.      *         location has been configured the default of 'git-upload-pack' is
  356.      *         returned instead.
  357.      */
  358.     public String getUploadPack() {
  359.         return uploadpack;
  360.     }

  361.     /**
  362.      * Override for the location of 'git-receive-pack' on the remote system.
  363.      * <p>
  364.      * This value is only useful for an SSH style connection, where Git is
  365.      * asking the remote system to execute a program that provides the necessary
  366.      * network protocol.
  367.      *
  368.      * @return location of 'git-receive-pack' on the remote system. If no
  369.      *         location has been configured the default of 'git-receive-pack' is
  370.      *         returned instead.
  371.      */
  372.     public String getReceivePack() {
  373.         return receivepack;
  374.     }

  375.     /**
  376.      * Get the description of how annotated tags should be treated during fetch.
  377.      *
  378.      * @return option indicating the behavior of annotated tags in fetch.
  379.      */
  380.     public TagOpt getTagOpt() {
  381.         return tagopt;
  382.     }

  383.     /**
  384.      * Set the description of how annotated tags should be treated on fetch.
  385.      *
  386.      * @param option
  387.      *            method to use when handling annotated tags.
  388.      */
  389.     public void setTagOpt(TagOpt option) {
  390.         tagopt = option != null ? option : TagOpt.AUTO_FOLLOW;
  391.     }

  392.     /**
  393.      * Whether pushing to the remote automatically deletes remote refs which
  394.      * don't exist on the source side.
  395.      *
  396.      * @return true if pushing to the remote automatically deletes remote refs
  397.      *         which don't exist on the source side.
  398.      */
  399.     public boolean isMirror() {
  400.         return mirror;
  401.     }

  402.     /**
  403.      * Set the mirror flag to automatically delete remote refs.
  404.      *
  405.      * @param m
  406.      *            true to automatically delete remote refs during push.
  407.      */
  408.     public void setMirror(boolean m) {
  409.         mirror = m;
  410.     }

  411.     /**
  412.      * Get timeout (in seconds) before aborting an IO operation.
  413.      *
  414.      * @return timeout (in seconds) before aborting an IO operation.
  415.      */
  416.     public int getTimeout() {
  417.         return timeout;
  418.     }

  419.     /**
  420.      * Set the timeout before willing to abort an IO call.
  421.      *
  422.      * @param seconds
  423.      *            number of seconds to wait (with no data transfer occurring)
  424.      *            before aborting an IO read or write operation with this
  425.      *            remote.  A timeout of 0 will block indefinitely.
  426.      */
  427.     public void setTimeout(int seconds) {
  428.         timeout = seconds;
  429.     }
  430. }