git4idea: Added read/write locking functionality to git command wrapper
[fedora-idea.git] / plugins / git4idea / src / git4idea / config / GitConfigUtil.java
blob3dbe7cf81a9910d1e421c6f737e7711286ea0460
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package git4idea.config;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.util.Pair;
20 import com.intellij.openapi.vcs.VcsException;
21 import com.intellij.openapi.vfs.VirtualFile;
22 import git4idea.GitUtil;
23 import git4idea.commands.GitCommand;
24 import git4idea.commands.GitSimpleHandler;
25 import org.jetbrains.annotations.NonNls;
26 import org.jetbrains.annotations.Nullable;
28 import java.nio.charset.Charset;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
33 /**
34 * Git utilities for working with configuration
36 public class GitConfigUtil {
37 /**
38 * A private constructor for utility class
40 private GitConfigUtil() {
43 /**
44 * Get configuration values for the repository. Note that the method executes a git command.
46 * @param project the context project
47 * @param root the git root
48 * @param keyMask the keys to be queried
49 * @param result the map to put results to
50 * @throws VcsException if there is a problem with running git
52 public static void getValues(Project project, VirtualFile root, String keyMask, Map<String, String> result) throws VcsException {
53 GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.CONFIG);
54 h.setNoSSH(true);
55 h.setSilent(true);
56 h.addParameters("--null", "--get-regexp", keyMask);
57 String output = h.run();
58 int start = 0;
59 int pos;
60 while ((pos = output.indexOf('\n', start)) != -1) {
61 String key = output.substring(start, pos);
62 start = pos + 1;
63 if ((pos = output.indexOf('\u0000', start)) == -1) {
64 break;
66 String value = output.substring(start, pos);
67 start = pos + 1;
68 result.put(key, value);
72 /**
73 * Get configuration values for the repository. Note that the method executes a git command.
75 * @param project the context project
76 * @param root the git root
77 * @param key the keys to be queried
78 * @return list of pairs ({@link Pair#first} is the key, {@link Pair#second} is the value)
79 * @throws VcsException an exception
81 public static List<Pair<String, String>> getAllValues(Project project, VirtualFile root, @NonNls String key) throws VcsException {
82 List<Pair<String, String>> result = new ArrayList<Pair<String, String>>();
83 GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.CONFIG);
84 h.setNoSSH(true);
85 h.setSilent(true);
86 h.addParameters("--null", "--get-all", key);
87 String output = h.run();
88 int start = 0;
89 int pos;
90 while ((pos = output.indexOf('\u0000', start)) != -1) {
91 String value = output.substring(start, pos);
92 start = pos + 1;
93 result.add(new Pair<String, String>(key, value));
95 return result;
99 /**
100 * Get configuration value for the repository. Note that the method executes a git command.
102 * @param project the context project
103 * @param root the git root
104 * @param key the keys to be queried
105 * @return the value associated with the key or null if the value is not found
106 * @throws VcsException an exception
108 @Nullable
109 public static String getValue(Project project, VirtualFile root, @NonNls String key) throws VcsException {
110 GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.CONFIG);
111 h.setNoSSH(true);
112 h.setSilent(true);
113 h.ignoreErrorCode(1);
114 h.addParameters("--null", "--get", key);
115 String output = h.run();
116 int pos = output.indexOf('\u0000');
117 if (h.getExitCode() != 0 || pos == -1) {
118 return null;
120 return output.substring(0, pos);
124 * Get boolean configuration value for the repository. Note that the method executes a git command.
126 * @param project the context project
127 * @param root the git root
128 * @param key the keys to be queried
129 * @return the value associated with the key or null if the value is not found, value is not valid integer or boolean
130 * @throws VcsException an exception
132 @SuppressWarnings({"HardCodedStringLiteral"})
133 @Nullable
134 public static Boolean getBoolValue(final Project project, final VirtualFile root, @NonNls final String key) throws VcsException {
135 String value = getValue(project, root, key);
136 if (value == null) {
137 return null;
139 value = value.trim();
140 if (value.length() == 0) {
141 return null;
143 if ("yes".equals(value) || "true".equals(value)) {
144 return Boolean.TRUE;
146 if ("no".equals(value) || "false".equals(value)) {
147 return Boolean.FALSE;
149 try {
150 int i = Integer.parseInt(value);
151 return i != 0;
153 catch (NumberFormatException ex) {
154 return null;
159 * Get commit encoding for the specified root
161 * @param project the context project
162 * @param root the project root
163 * @return the commit encoding or UTF-8 if the encoding is note explicitly specified
165 public static String getCommitEncoding(final Project project, VirtualFile root) {
166 @NonNls String encoding = null;
167 try {
168 encoding = getValue(project, root, "i18n.commitencoding");
170 catch (VcsException e) {
171 // ignore exception
173 if (encoding == null || encoding.length() == 0) {
174 encoding = GitUtil.UTF8_ENCODING;
176 return encoding;
180 * Get log output encoding for the specified root
182 * @param project the context project
183 * @param root the project root
184 * @return the log output encoding, the commit encoding, or UTF-8 if the encoding is note explicitly specified
186 public static String getLogEncoding(final Project project, VirtualFile root) {
187 @NonNls String encoding = null;
188 try {
189 encoding = getValue(project, root, "i18n.logoutputencoding");
191 catch (VcsException e) {
192 // ignore exception
194 if (encoding == null || encoding.length() == 0) {
195 encoding = getCommitEncoding(project, root);
197 return encoding;
201 * Get encoding that GIT uses for file names.
203 * @return the encoding for file names
205 public static String getFileNameEncoding() {
206 // TODO the best guess is that the default encoding is used.
207 return Charset.defaultCharset().name();
211 * Unset the current value
213 * @param project the project
214 * @param root the git root
215 * @param key the key to unset
216 * @throws VcsException if there is a problem with running git
218 public static void unsetValue(Project project, VirtualFile root, String key) throws VcsException {
219 GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.CONFIG);
220 h.setNoSSH(true);
221 h.setSilent(true);
222 h.ignoreErrorCode(1);
223 h.addParameters("--unset", key);
224 h.run();
228 * Set the value
230 * @param project the project
231 * @param root the git root
232 * @param key the key to set
233 * @param value the value to set
234 * @throws VcsException if there is a problem with running git
236 public static void setValue(Project project, VirtualFile root, String key, String value) throws VcsException {
237 GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.CONFIG);
238 h.setNoSSH(true);
239 h.setSilent(true);
240 h.ignoreErrorCode(1);
241 h.addParameters(key, value);
242 h.run();