View Javadoc
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  
11  package org.eclipse.jgit.internal.diffmergetool;
12  
13  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFFTOOL_SECTION;
14  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFF_SECTION;
15  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_CMD;
16  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_GUITOOL;
17  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PATH;
18  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_PROMPT;
19  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_TOOL;
20  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_TRUST_EXIT_CODE;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.eclipse.jgit.lib.Config;
27  import org.eclipse.jgit.lib.Config.SectionParser;
28  import org.eclipse.jgit.lib.internal.BooleanTriState;
29  
30  /**
31   * Keeps track of difftool related configuration options.
32   */
33  public class DiffToolConfig {
34  
35  	/** Key for {@link Config#get(SectionParser)}. */
36  	public static final Config.SectionParser<DiffToolConfig> KEY = DiffToolConfig::new;
37  
38  	private final String toolName;
39  
40  	private final String guiToolName;
41  
42  	private final boolean prompt;
43  
44  	private final BooleanTriState trustExitCode;
45  
46  	private final Map<String, ExternalDiffTool> tools;
47  
48  	private DiffToolConfig(Config rc) {
49  		toolName = rc.getString(CONFIG_DIFF_SECTION, null, CONFIG_KEY_TOOL);
50  		guiToolName = rc.getString(CONFIG_DIFF_SECTION, null,
51  				CONFIG_KEY_GUITOOL);
52  		prompt = rc.getBoolean(CONFIG_DIFFTOOL_SECTION, toolName,
53  				CONFIG_KEY_PROMPT,
54  				true);
55  		String trustStr = rc.getString(CONFIG_DIFFTOOL_SECTION, toolName,
56  				CONFIG_KEY_TRUST_EXIT_CODE);
57  		if (trustStr != null) {
58  			trustExitCode = Boolean.parseBoolean(trustStr)
59  					? BooleanTriState.TRUE
60  					: BooleanTriState.FALSE;
61  		} else {
62  			trustExitCode = BooleanTriState.UNSET;
63  		}
64  		tools = new HashMap<>();
65  		Set<String> subsections = rc.getSubsections(CONFIG_DIFFTOOL_SECTION);
66  		for (String name : subsections) {
67  			String cmd = rc.getString(CONFIG_DIFFTOOL_SECTION, name,
68  					CONFIG_KEY_CMD);
69  			String path = rc.getString(CONFIG_DIFFTOOL_SECTION, name,
70  					CONFIG_KEY_PATH);
71  			if ((cmd != null) || (path != null)) {
72  				tools.put(name, new UserDefinedDiffTool(name, path, cmd));
73  			}
74  		}
75  	}
76  
77  	/**
78  	 * @return the default diff tool name (diff.tool)
79  	 */
80  	public String getDefaultToolName() {
81  		return toolName;
82  	}
83  
84  	/**
85  	 * @return the default GUI diff tool name (diff.guitool)
86  	 */
87  	public String getDefaultGuiToolName() {
88  		return guiToolName;
89  	}
90  
91  	/**
92  	 * @return the diff tool "prompt" option (difftool.prompt)
93  	 */
94  	public boolean isPrompt() {
95  		return prompt;
96  	}
97  
98  	/**
99  	 * @return the diff tool "trust exit code" option (difftool.trustExitCode)
100 	 */
101 	public boolean isTrustExitCode() {
102 		return trustExitCode == BooleanTriState.TRUE;
103 	}
104 
105 	/**
106 	 * @return the tools map
107 	 */
108 	public Map<String, ExternalDiffTool> getTools() {
109 		return tools;
110 	}
111 
112 	/**
113 	 * @return the tool names
114 	 */
115 	public Set<String> getToolNames() {
116 		return tools.keySet();
117 	}
118 }