MergeConfig.java

  1. /*******************************************************************************
  2.  * Copyright (c) 2014 Konrad Kügler 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.merge;

  11. import java.io.IOException;

  12. import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
  13. import org.eclipse.jgit.lib.Config;
  14. import org.eclipse.jgit.lib.Config.SectionParser;
  15. import org.eclipse.jgit.lib.ConfigConstants;
  16. import org.eclipse.jgit.lib.Repository;

  17. /**
  18.  * Holds configuration for merging into a given branch
  19.  *
  20.  * @since 3.3
  21.  */
  22. public class MergeConfig {

  23.     /**
  24.      * Get merge configuration for the current branch of the repository
  25.      *
  26.      * @param repo
  27.      *            a {@link org.eclipse.jgit.lib.Repository} object.
  28.      * @return merge configuration for the current branch of the repository
  29.      */
  30.     public static MergeConfig getConfigForCurrentBranch(Repository repo) {
  31.         try {
  32.             String branch = repo.getBranch();
  33.             if (branch != null)
  34.                 return repo.getConfig().get(getParser(branch));
  35.         } catch (IOException e) {
  36.             // ignore
  37.         }
  38.         // use defaults if branch can't be determined
  39.         return new MergeConfig();
  40.     }

  41.     /**
  42.      * Get a parser for use with
  43.      * {@link org.eclipse.jgit.lib.Config#get(SectionParser)}
  44.      *
  45.      * @param branch
  46.      *            short branch name to get the configuration for, as returned
  47.      *            e.g. by {@link org.eclipse.jgit.lib.Repository#getBranch()}
  48.      * @return a parser for use with
  49.      *         {@link org.eclipse.jgit.lib.Config#get(SectionParser)}
  50.      */
  51.     public static final SectionParser<MergeConfig> getParser(
  52.             final String branch) {
  53.         return new MergeConfigSectionParser(branch);
  54.     }

  55.     private final FastForwardMode fastForwardMode;

  56.     private final boolean squash;

  57.     private final boolean commit;

  58.     private MergeConfig(String branch, Config config) {
  59.         String[] mergeOptions = getMergeOptions(branch, config);
  60.         fastForwardMode = getFastForwardMode(config, mergeOptions);
  61.         squash = isMergeConfigOptionSet("--squash", mergeOptions); //$NON-NLS-1$
  62.         commit = !isMergeConfigOptionSet("--no-commit", mergeOptions); //$NON-NLS-1$
  63.     }

  64.     private MergeConfig() {
  65.         fastForwardMode = FastForwardMode.FF;
  66.         squash = false;
  67.         commit = true;
  68.     }

  69.     /**
  70.      * Get the fast forward mode configured for this branch
  71.      *
  72.      * @return the fast forward mode configured for this branch
  73.      */
  74.     public FastForwardMode getFastForwardMode() {
  75.         return fastForwardMode;
  76.     }

  77.     /**
  78.      * Whether merges into this branch are configured to be squash merges, false
  79.      * otherwise
  80.      *
  81.      * @return true if merges into this branch are configured to be squash
  82.      *         merges, false otherwise
  83.      */
  84.     public boolean isSquash() {
  85.         return squash;
  86.     }

  87.     /**
  88.      * Whether {@code --no-commit} option is not set.
  89.      *
  90.      * @return {@code false} if --no-commit is configured for this branch,
  91.      *         {@code true} otherwise (even if --squash is configured)
  92.      */
  93.     public boolean isCommit() {
  94.         return commit;
  95.     }

  96.     private static FastForwardMode getFastForwardMode(Config config,
  97.             String[] mergeOptions) {
  98.         for (String option : mergeOptions) {
  99.             for (FastForwardMode mode : FastForwardMode.values())
  100.                 if (mode.matchConfigValue(option))
  101.                     return mode;
  102.         }
  103.         FastForwardMode ffmode = FastForwardMode.valueOf(config.getEnum(
  104.                 ConfigConstants.CONFIG_KEY_MERGE, null,
  105.                 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
  106.         return ffmode;
  107.     }

  108.     private static boolean isMergeConfigOptionSet(String optionToLookFor,
  109.             String[] mergeOptions) {
  110.         for (String option : mergeOptions) {
  111.             if (optionToLookFor.equals(option))
  112.                 return true;
  113.         }
  114.         return false;
  115.     }

  116.     private static String[] getMergeOptions(String branch, Config config) {
  117.         String mergeOptions = config.getString(
  118.                 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
  119.                 ConfigConstants.CONFIG_KEY_MERGEOPTIONS);
  120.         if (mergeOptions != null) {
  121.             return mergeOptions.split("\\s"); //$NON-NLS-1$
  122.         }
  123.         return new String[0];
  124.     }

  125.     private static class MergeConfigSectionParser implements
  126.             SectionParser<MergeConfig> {

  127.         private final String branch;

  128.         public MergeConfigSectionParser(String branch) {
  129.             this.branch = branch;
  130.         }

  131.         @Override
  132.         public MergeConfig parse(Config cfg) {
  133.             return new MergeConfig(branch, cfg);
  134.         }

  135.         @Override
  136.         public boolean equals(Object obj) {
  137.             if (obj instanceof MergeConfigSectionParser) {
  138.                 return branch.equals(((MergeConfigSectionParser) obj).branch);
  139.             }
  140.             return false;
  141.         }

  142.         @Override
  143.         public int hashCode() {
  144.             return branch.hashCode();
  145.         }

  146.     }

  147. }