DiffToolConfig.java

  1. /*
  2.  * Copyright (C) 2018-2021, 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_DIFFTOOL_SECTION;
  12. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFF_SECTION;
  13. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CMD;
  14. import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_GUITOOL;
  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 java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.Set;

  22. import org.eclipse.jgit.lib.Config;
  23. import org.eclipse.jgit.lib.Config.SectionParser;
  24. import org.eclipse.jgit.lib.internal.BooleanTriState;

  25. /**
  26.  * Keeps track of difftool related configuration options.
  27.  */
  28. public class DiffToolConfig {

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

  31.     private final String toolName;

  32.     private final String guiToolName;

  33.     private final boolean prompt;

  34.     private final BooleanTriState trustExitCode;

  35.     private final Map<String, ExternalDiffTool> tools;

  36.     private DiffToolConfig(Config rc) {
  37.         toolName = rc.getString(CONFIG_DIFF_SECTION, null, CONFIG_KEY_TOOL);
  38.         guiToolName = rc.getString(CONFIG_DIFF_SECTION, null,
  39.                 CONFIG_KEY_GUITOOL);
  40.         prompt = rc.getBoolean(CONFIG_DIFFTOOL_SECTION, toolName,
  41.                 CONFIG_KEY_PROMPT,
  42.                 true);
  43.         String trustStr = rc.getString(CONFIG_DIFFTOOL_SECTION, toolName,
  44.                 CONFIG_KEY_TRUST_EXIT_CODE);
  45.         if (trustStr != null) {
  46.             trustExitCode = Boolean.parseBoolean(trustStr)
  47.                     ? BooleanTriState.TRUE
  48.                     : BooleanTriState.FALSE;
  49.         } else {
  50.             trustExitCode = BooleanTriState.UNSET;
  51.         }
  52.         tools = new HashMap<>();
  53.         Set<String> subsections = rc.getSubsections(CONFIG_DIFFTOOL_SECTION);
  54.         for (String name : subsections) {
  55.             String cmd = rc.getString(CONFIG_DIFFTOOL_SECTION, name,
  56.                     CONFIG_KEY_CMD);
  57.             String path = rc.getString(CONFIG_DIFFTOOL_SECTION, name,
  58.                     CONFIG_KEY_PATH);
  59.             if ((cmd != null) || (path != null)) {
  60.                 tools.put(name, new UserDefinedDiffTool(name, path, cmd));
  61.             }
  62.         }
  63.     }

  64.     /**
  65.      * @return the default diff tool name (diff.tool)
  66.      */
  67.     public String getDefaultToolName() {
  68.         return toolName;
  69.     }

  70.     /**
  71.      * @return the default GUI diff tool name (diff.guitool)
  72.      */
  73.     public String getDefaultGuiToolName() {
  74.         return guiToolName;
  75.     }

  76.     /**
  77.      * @return the diff tool "prompt" option (difftool.prompt)
  78.      */
  79.     public boolean isPrompt() {
  80.         return prompt;
  81.     }

  82.     /**
  83.      * @return the diff tool "trust exit code" option (difftool.trustExitCode)
  84.      */
  85.     public boolean isTrustExitCode() {
  86.         return trustExitCode == BooleanTriState.TRUE;
  87.     }

  88.     /**
  89.      * @return the tools map
  90.      */
  91.     public Map<String, ExternalDiffTool> getTools() {
  92.         return tools;
  93.     }

  94.     /**
  95.      * @return the tool names
  96.      */
  97.     public Set<String> getToolNames() {
  98.         return tools.keySet();
  99.     }
  100. }