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
;
34 * Git utilities for working with configuration
36 public class GitConfigUtil
{
38 * A private constructor for utility class
40 private GitConfigUtil() {
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
);
56 h
.addParameters("--null", "--get-regexp", keyMask
);
57 String output
= h
.run();
60 while ((pos
= output
.indexOf('\n', start
)) != -1) {
61 String key
= output
.substring(start
, pos
);
63 if ((pos
= output
.indexOf('\u0000', start
)) == -1) {
66 String value
= output
.substring(start
, pos
);
68 result
.put(key
, value
);
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
);
86 h
.addParameters("--null", "--get-all", key
);
87 String output
= h
.run();
90 while ((pos
= output
.indexOf('\u0000', start
)) != -1) {
91 String value
= output
.substring(start
, pos
);
93 result
.add(new Pair
<String
, String
>(key
, value
));
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
109 public static String
getValue(Project project
, VirtualFile root
, @NonNls String key
) throws VcsException
{
110 GitSimpleHandler h
= new GitSimpleHandler(project
, root
, GitCommand
.CONFIG
);
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) {
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"})
134 public static Boolean
getBoolValue(final Project project
, final VirtualFile root
, @NonNls final String key
) throws VcsException
{
135 String value
= getValue(project
, root
, key
);
139 value
= value
.trim();
140 if (value
.length() == 0) {
143 if ("yes".equals(value
) || "true".equals(value
)) {
146 if ("no".equals(value
) || "false".equals(value
)) {
147 return Boolean
.FALSE
;
150 int i
= Integer
.parseInt(value
);
153 catch (NumberFormatException ex
) {
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;
168 encoding
= getValue(project
, root
, "i18n.commitencoding");
170 catch (VcsException e
) {
173 if (encoding
== null || encoding
.length() == 0) {
174 encoding
= GitUtil
.UTF8_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;
189 encoding
= getValue(project
, root
, "i18n.logoutputencoding");
191 catch (VcsException e
) {
194 if (encoding
== null || encoding
.length() == 0) {
195 encoding
= getCommitEncoding(project
, root
);
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
);
222 h
.ignoreErrorCode(1);
223 h
.addParameters("--unset", key
);
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
);
240 h
.ignoreErrorCode(1);
241 h
.addParameters(key
, value
);