MergeToolConfig.java

  1. /*
  2.  * Copyright (C) 2018-2022, Andre Bossert <andre.bossert@siemens.com>
  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.internal.diffmergetool;

  11. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CMD;
  12. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_GUITOOL;
  13. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_KEEP_BACKUP;
  14. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_KEEP_TEMPORARIES;
  15. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PATH;
  16. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PROMPT;
  17. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_TOOL;
  18. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_TRUST_EXIT_CODE;
  19. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_WRITE_TO_TEMP;
  20. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_MERGETOOL_SECTION;
  21. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_MERGE_SECTION;

  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.Set;

  25. import org.eclipse.jgit.lib.Config;
  26. import org.eclipse.jgit.lib.Config.SectionParser;
  27. import org.eclipse.jgit.lib.internal.BooleanTriState;

  28. /**
  29.  * Keeps track of merge tool related configuration options.
  30.  */
  31. public class MergeToolConfig {

  32.     /** Key for {@link Config#get(SectionParser)}. */
  33.     public static final Config.SectionParser<MergeToolConfig> KEY = MergeToolConfig::new;

  34.     private final String toolName;

  35.     private final String guiToolName;

  36.     private final boolean prompt;

  37.     private final boolean keepBackup;

  38.     private final boolean keepTemporaries;

  39.     private final boolean writeToTemp;

  40.     private final Map<String, ExternalMergeTool> tools;

  41.     private MergeToolConfig(Config rc) {
  42.         toolName = rc.getString(CONFIG_MERGE_SECTION, null, CONFIG_KEY_TOOL);
  43.         guiToolName = rc.getString(CONFIG_MERGE_SECTION, null,
  44.                 CONFIG_KEY_GUITOOL);
  45.         prompt = rc.getBoolean(CONFIG_MERGETOOL_SECTION, toolName,
  46.                 CONFIG_KEY_PROMPT, true);
  47.         keepBackup = rc.getBoolean(CONFIG_MERGETOOL_SECTION,
  48.                 CONFIG_KEY_KEEP_BACKUP, true);
  49.         keepTemporaries = rc.getBoolean(CONFIG_MERGETOOL_SECTION,
  50.                 CONFIG_KEY_KEEP_TEMPORARIES, false);
  51.         writeToTemp = rc.getBoolean(CONFIG_MERGETOOL_SECTION,
  52.                 CONFIG_KEY_WRITE_TO_TEMP, false);
  53.         tools = new HashMap<>();
  54.         Set<String> subsections = rc.getSubsections(CONFIG_MERGETOOL_SECTION);
  55.         for (String name : subsections) {
  56.             String cmd = rc.getString(CONFIG_MERGETOOL_SECTION, name,
  57.                     CONFIG_KEY_CMD);
  58.             String path = rc.getString(CONFIG_MERGETOOL_SECTION, name,
  59.                     CONFIG_KEY_PATH);
  60.             BooleanTriState trustExitCode = BooleanTriState.FALSE;
  61.             String trustStr = rc.getString(CONFIG_MERGETOOL_SECTION, name,
  62.                     CONFIG_KEY_TRUST_EXIT_CODE);
  63.             if (trustStr != null) {
  64.                 trustExitCode = Boolean.valueOf(trustStr).booleanValue()
  65.                         ? BooleanTriState.TRUE
  66.                         : BooleanTriState.FALSE;
  67.             } else {
  68.                 trustExitCode = BooleanTriState.UNSET;
  69.             }
  70.             if ((cmd != null) || (path != null)) {
  71.                 tools.put(name, new UserDefinedMergeTool(name, path, cmd,
  72.                         trustExitCode));
  73.             }
  74.         }
  75.     }

  76.     /**
  77.      * @return the default merge tool name (merge.tool)
  78.      */
  79.     public String getDefaultToolName() {
  80.         return toolName;
  81.     }

  82.     /**
  83.      * @return the default GUI merge tool name (merge.guitool)
  84.      */
  85.     public String getDefaultGuiToolName() {
  86.         return guiToolName;
  87.     }

  88.     /**
  89.      * @return the merge tool "prompt" option (mergetool.prompt)
  90.      */
  91.     public boolean isPrompt() {
  92.         return prompt;
  93.     }

  94.     /**
  95.      * @return the tool "keep backup" option
  96.      */
  97.     public boolean isKeepBackup() {
  98.         return keepBackup;
  99.     }

  100.     /**
  101.      * @return the tool "keepTemporaries" option
  102.      */
  103.     public boolean isKeepTemporaries() {
  104.         return keepTemporaries;
  105.     }

  106.     /**
  107.      * @return the tool "write to temp" option
  108.      */
  109.     public boolean isWriteToTemp() {
  110.         return writeToTemp;
  111.     }

  112.     /**
  113.      * @return the tools map
  114.      */
  115.     public Map<String, ExternalMergeTool> getTools() {
  116.         return tools;
  117.     }

  118.     /**
  119.      * @return the tool names
  120.      */
  121.     public Set<String> getToolNames() {
  122.         return tools.keySet();
  123.     }

  124. }