PushConfig.java

  1. /*
  2.  * Copyright (C) 2017, 2022 David Pursehouse <david.pursehouse@gmail.com> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.transport;

  11. import java.util.Locale;

  12. import org.eclipse.jgit.lib.Config;
  13. import org.eclipse.jgit.lib.ConfigConstants;
  14. import org.eclipse.jgit.util.StringUtils;

  15. /**
  16.  * Push section of a Git configuration file.
  17.  *
  18.  * @since 4.9
  19.  */
  20. public class PushConfig {

  21.     /**
  22.      * Git config values for {@code push.recurseSubmodules}.
  23.      */
  24.     public enum PushRecurseSubmodulesMode implements Config.ConfigEnum {
  25.         /**
  26.          * Verify that all submodule commits that changed in the revisions to be
  27.          * pushed are available on at least one remote of the submodule.
  28.          */
  29.         CHECK("check"), //$NON-NLS-1$

  30.         /**
  31.          * All submodules that changed in the revisions to be pushed will be
  32.          * pushed.
  33.          */
  34.         ON_DEMAND("on-demand"), //$NON-NLS-1$

  35.         /** Default behavior of ignoring submodules when pushing is retained. */
  36.         NO("false"); //$NON-NLS-1$

  37.         private final String configValue;

  38.         private PushRecurseSubmodulesMode(String configValue) {
  39.             this.configValue = configValue;
  40.         }

  41.         @Override
  42.         public String toConfigValue() {
  43.             return configValue;
  44.         }

  45.         @Override
  46.         public boolean matchConfigValue(String s) {
  47.             if (StringUtils.isEmptyOrNull(s)) {
  48.                 return false;
  49.             }
  50.             s = s.replace('-', '_');
  51.             return name().equalsIgnoreCase(s)
  52.                     || configValue.equalsIgnoreCase(s);
  53.         }
  54.     }

  55.     /**
  56.      * Git config values for {@code push.default}.
  57.      *
  58.      * @since 6.1
  59.      */
  60.     public enum PushDefault implements Config.ConfigEnum {

  61.         /**
  62.          * Do not push if there are no explicit refspecs.
  63.          */
  64.         NOTHING,

  65.         /**
  66.          * Push the current branch to an upstream branch of the same name.
  67.          */
  68.         CURRENT,

  69.         /**
  70.          * Push the current branch to an upstream branch determined by git
  71.          * config {@code branch.<currentBranch>.merge}.
  72.          */
  73.         UPSTREAM("tracking"), //$NON-NLS-1$

  74.         /**
  75.          * Like {@link #UPSTREAM}, but only if the upstream name is the same as
  76.          * the name of the current local branch.
  77.          */
  78.         SIMPLE,

  79.         /**
  80.          * Push all current local branches that match a configured push refspec
  81.          * of the remote configuration.
  82.          */
  83.         MATCHING;

  84.         private final String alias;

  85.         private PushDefault() {
  86.             alias = null;
  87.         }

  88.         private PushDefault(String alias) {
  89.             this.alias = alias;
  90.         }

  91.         @Override
  92.         public String toConfigValue() {
  93.             return name().toLowerCase(Locale.ROOT);
  94.         }

  95.         @Override
  96.         public boolean matchConfigValue(String in) {
  97.             return toConfigValue().equalsIgnoreCase(in)
  98.                     || (alias != null && alias.equalsIgnoreCase(in));
  99.         }
  100.     }

  101.     private final PushRecurseSubmodulesMode recurseSubmodules;

  102.     private final PushDefault pushDefault;

  103.     /**
  104.      * Creates a new instance.
  105.      *
  106.      * @param config
  107.      *            {@link Config} to fill the {@link PushConfig} from
  108.      * @since 6.1
  109.      */
  110.     public PushConfig(Config config) {
  111.         recurseSubmodules = config.getEnum(ConfigConstants.CONFIG_PUSH_SECTION,
  112.                 null, ConfigConstants.CONFIG_KEY_RECURSE_SUBMODULES,
  113.                 PushRecurseSubmodulesMode.NO);
  114.         pushDefault = config.getEnum(ConfigConstants.CONFIG_PUSH_SECTION, null,
  115.                 ConfigConstants.CONFIG_KEY_DEFAULT, PushDefault.SIMPLE);
  116.     }

  117.     /**
  118.      * Retrieves the value of git config {@code push.recurseSubmodules}.
  119.      *
  120.      * @return the value
  121.      * @since 6.1
  122.      */
  123.     public PushRecurseSubmodulesMode getRecurseSubmodules() {
  124.         return recurseSubmodules;
  125.     }

  126.     /**
  127.      * Retrieves the value of git config {@code push.default}.
  128.      *
  129.      * @return the value
  130.      * @since 6.1
  131.      */
  132.     public PushDefault getPushDefault() {
  133.         return pushDefault;
  134.     }
  135. }