View Javadoc
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  
11  package org.eclipse.jgit.internal.diffmergetool;
12  
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_KEEP_BACKUP;
16  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_KEEP_TEMPORARIES;
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  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_WRITE_TO_TEMP;
22  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_MERGETOOL_SECTION;
23  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_MERGE_SECTION;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.eclipse.jgit.lib.Config;
30  import org.eclipse.jgit.lib.Config.SectionParser;
31  import org.eclipse.jgit.lib.internal.BooleanTriState;
32  
33  /**
34   * Keeps track of merge tool related configuration options.
35   */
36  public class MergeToolConfig {
37  
38  	/** Key for {@link Config#get(SectionParser)}. */
39  	public static final Config.SectionParser<MergeToolConfig> KEY = MergeToolConfig::new;
40  
41  	private final String toolName;
42  
43  	private final String guiToolName;
44  
45  	private final boolean prompt;
46  
47  	private final boolean keepBackup;
48  
49  	private final boolean keepTemporaries;
50  
51  	private final boolean writeToTemp;
52  
53  	private final Map<String, ExternalMergeTool> tools;
54  
55  	private MergeToolConfig(Config rc) {
56  		toolName = rc.getString(CONFIG_MERGE_SECTION, null, CONFIG_KEY_TOOL);
57  		guiToolName = rc.getString(CONFIG_MERGE_SECTION, null,
58  				CONFIG_KEY_GUITOOL);
59  		prompt = rc.getBoolean(CONFIG_MERGETOOL_SECTION, toolName,
60  				CONFIG_KEY_PROMPT, true);
61  		keepBackup = rc.getBoolean(CONFIG_MERGETOOL_SECTION,
62  				CONFIG_KEY_KEEP_BACKUP, true);
63  		keepTemporaries = rc.getBoolean(CONFIG_MERGETOOL_SECTION,
64  				CONFIG_KEY_KEEP_TEMPORARIES, false);
65  		writeToTemp = rc.getBoolean(CONFIG_MERGETOOL_SECTION,
66  				CONFIG_KEY_WRITE_TO_TEMP, false);
67  		tools = new HashMap<>();
68  		Set<String> subsections = rc.getSubsections(CONFIG_MERGETOOL_SECTION);
69  		for (String name : subsections) {
70  			String cmd = rc.getString(CONFIG_MERGETOOL_SECTION, name,
71  					CONFIG_KEY_CMD);
72  			String path = rc.getString(CONFIG_MERGETOOL_SECTION, name,
73  					CONFIG_KEY_PATH);
74  			BooleanTriState trustExitCode = BooleanTriState.FALSE;
75  			String trustStr = rc.getString(CONFIG_MERGETOOL_SECTION, name,
76  					CONFIG_KEY_TRUST_EXIT_CODE);
77  			if (trustStr != null) {
78  				trustExitCode = Boolean.valueOf(trustStr).booleanValue()
79  						? BooleanTriState.TRUE
80  						: BooleanTriState.FALSE;
81  			} else {
82  				trustExitCode = BooleanTriState.UNSET;
83  			}
84  			if ((cmd != null) || (path != null)) {
85  				tools.put(name, new UserDefinedMergeTool(name, path, cmd,
86  						trustExitCode));
87  			}
88  		}
89  	}
90  
91  	/**
92  	 * @return the default merge tool name (merge.tool)
93  	 */
94  	public String getDefaultToolName() {
95  		return toolName;
96  	}
97  
98  	/**
99  	 * @return the default GUI merge tool name (merge.guitool)
100 	 */
101 	public String getDefaultGuiToolName() {
102 		return guiToolName;
103 	}
104 
105 	/**
106 	 * @return the merge tool "prompt" option (mergetool.prompt)
107 	 */
108 	public boolean isPrompt() {
109 		return prompt;
110 	}
111 
112 	/**
113 	 * @return the tool "keep backup" option
114 	 */
115 	public boolean isKeepBackup() {
116 		return keepBackup;
117 	}
118 
119 	/**
120 	 * @return the tool "keepTemporaries" option
121 	 */
122 	public boolean isKeepTemporaries() {
123 		return keepTemporaries;
124 	}
125 
126 	/**
127 	 * @return the tool "write to temp" option
128 	 */
129 	public boolean isWriteToTemp() {
130 		return writeToTemp;
131 	}
132 
133 	/**
134 	 * @return the tools map
135 	 */
136 	public Map<String, ExternalMergeTool> getTools() {
137 		return tools;
138 	}
139 
140 	/**
141 	 * @return the tool names
142 	 */
143 	public Set<String> getToolNames() {
144 		return tools.keySet();
145 	}
146 
147 }