Skip to content

Commit 397e8ad

Browse files
committed
Merge remote-tracking branch 'carlos/config-parse'
2 parents d882af8 + 73e9e58 commit 397e8ad

File tree

4 files changed

+92
-30
lines changed

4 files changed

+92
-30
lines changed

docs/config.rst

+7
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ The Config type
2323
set multiple times in the configuration files.
2424

2525
The :class:`Config` Mapping interface.
26+
27+
When using the mapping interface, the value is returned as a
28+
string. In order to apply the git-config parsing rules, you can use
29+
:method:`Config.get_bool` or :method:`Config.get_int`.
30+
31+
.. automethod:: pygit2.Config.get_bool
32+
.. automethod:: pygit2.Config.get_int

src/config.c

+77-24
Original file line numberDiff line numberDiff line change
@@ -168,43 +168,94 @@ Config_contains(Config *self, PyObject *py_key) {
168168
return 1;
169169
}
170170

171+
/* Get the C string value given a python string as key */
172+
static int
173+
get_string(const char **key_out, Config *self, PyObject *py_key)
174+
{
175+
PyObject *tkey;
176+
const char *key;
177+
int err;
178+
179+
key = py_str_borrow_c_str(&tkey, py_key, NULL);
180+
if (key == NULL)
181+
return -1;
182+
183+
err = git_config_get_string(key_out, self->config, key);
184+
Py_CLEAR(tkey);
185+
186+
if (err == GIT_ENOTFOUND) {
187+
PyErr_SetObject(PyExc_KeyError, py_key);
188+
return -1;
189+
}
190+
191+
if (err < 0) {
192+
Error_set(err);
193+
return -1;
194+
}
195+
196+
return 0;
197+
}
171198

172199
PyObject *
173200
Config_getitem(Config *self, PyObject *py_key)
174201
{
175-
int64_t value_int;
176-
int err, value_bool;
202+
int err;
177203
const char *value_str;
178-
const char *key;
179-
PyObject* py_value, *tmp;
180204

181-
key = py_str_borrow_c_str(&tmp, py_key, NULL);
182-
if (key == NULL)
205+
err = get_string(&value_str, self, py_key);
206+
if (err < 0)
183207
return NULL;
184208

185-
err = git_config_get_string(&value_str, self->config, key);
186-
Py_CLEAR(tmp);
209+
return to_unicode(value_str, NULL, NULL);
210+
}
211+
212+
PyDoc_STRVAR(Config_get_bool__doc__,
213+
"get_bool(key) -> Bool\n"
214+
"\n"
215+
"Look up *key* and parse its value as a boolean as per the git-config rules\n"
216+
"\n"
217+
"Truthy values are: 'true', 1, 'on' or 'yes'. Falsy values are: 'false',\n"
218+
"0, 'off' and 'no'");
219+
220+
PyObject *
221+
Config_get_bool(Config *self, PyObject *key)
222+
{
223+
int err, value;
224+
const char *value_str;
225+
226+
err = get_string(&value_str, self, key);
187227
if (err < 0)
188-
goto cleanup;
228+
return NULL;
189229

190-
if (git_config_parse_int64(&value_int, value_str) == 0)
191-
py_value = PyLong_FromLongLong(value_int);
192-
else if(git_config_parse_bool(&value_bool, value_str) == 0)
193-
py_value = PyBool_FromLong(value_bool);
194-
else
195-
py_value = to_unicode(value_str, NULL, NULL);
230+
if ((err = git_config_parse_bool(&value, value_str)) < 0)
231+
return NULL;
196232

197-
cleanup:
198-
if (err < 0) {
199-
if (err == GIT_ENOTFOUND) {
200-
PyErr_SetObject(PyExc_KeyError, py_key);
201-
return NULL;
202-
}
233+
return PyBool_FromLong(value);
234+
}
203235

204-
return Error_set(err);
205-
}
236+
PyDoc_STRVAR(Config_get_int__doc__,
237+
"get_int(key) -> int\n"
238+
"\n"
239+
"Look up *key* and parse its value as an integer as per the git-config rules\n"
240+
"\n"
241+
"A value can have a suffix 'k', 'm' or 'g' which stand for 'kilo', 'mega' and\n"
242+
"'giga' respectively");
243+
244+
PyObject *
245+
Config_get_int(Config *self, PyObject *key)
246+
{
247+
int err;
248+
int64_t value;
249+
const char *value_str;
250+
251+
err = get_string(&value_str, self, key);
252+
if (err < 0)
253+
return NULL;
254+
255+
if ((err = git_config_parse_int64(&value, value_str)) < 0)
256+
return NULL;
206257

207-
return py_value;
258+
return PyLong_FromLongLong(value);
208259
}
209260

210261
int
@@ -369,6 +420,8 @@ PyMethodDef Config_methods[] = {
369420
METHOD(Config, add_file, METH_VARARGS | METH_KEYWORDS),
370421
METHOD(Config, get_multivar, METH_VARARGS),
371422
METHOD(Config, set_multivar, METH_VARARGS),
423+
METHOD(Config, get_bool, METH_O),
424+
METHOD(Config, get_int, METH_O),
372425
{NULL}
373426
};
374427

src/utils.h

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#undef PyLong_Check
4848
#define PyLong_Check PyInt_Check
4949
#define PyLong_FromLong PyInt_FromLong
50+
#define PyInteger_Type PyInt_Type
5051
#define PyBytes_AS_STRING PyString_AS_STRING
5152
#define PyBytes_AsString PyString_AsString
5253
#define PyBytes_AsStringAndSize PyString_AsStringAndSize
@@ -57,6 +58,7 @@
5758
#define to_path(x) to_bytes(x)
5859
#define to_encoding(x) to_bytes(x)
5960
#else
61+
#define PyInteger_Type PyLong_Type
6062
#define to_path(x) to_unicode(x, Py_FileSystemDefaultEncoding, "strict")
6163
#define to_encoding(x) PyUnicode_DecodeASCII(x, strlen(x), "strict")
6264
#endif

test/test_config.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_new(self):
7474

7575
config_read = Config(CONFIG_FILENAME)
7676
self.assertTrue('core.bare' in config_read)
77-
self.assertFalse(config_read['core.bare'])
77+
self.assertFalse(config_read.get_bool('core.bare'))
7878
self.assertTrue('core.editor' in config_read)
7979
self.assertEqual(config_read['core.editor'], 'ed')
8080

@@ -88,9 +88,9 @@ def test_add(self):
8888

8989
config.add_file(CONFIG_FILENAME, 0)
9090
self.assertTrue('this.that' in config)
91-
self.assertTrue(config['this.that'])
91+
self.assertTrue(config.get_bool('this.that'))
9292
self.assertTrue('something.other.here' in config)
93-
self.assertFalse(config['something.other.here'])
93+
self.assertFalse(config.get_bool('something.other.here'))
9494

9595
def test_read(self):
9696
config = self.repo.config
@@ -103,11 +103,11 @@ def test_read(self):
103103
lambda: config['abc.def'])
104104

105105
self.assertTrue('core.bare' in config)
106-
self.assertFalse(config['core.bare'])
106+
self.assertFalse(config.get_bool('core.bare'))
107107
self.assertTrue('core.editor' in config)
108108
self.assertEqual(config['core.editor'], 'ed')
109109
self.assertTrue('core.repositoryformatversion' in config)
110-
self.assertEqual(config['core.repositoryformatversion'], 0)
110+
self.assertEqual(config.get_int('core.repositoryformatversion'), 0)
111111

112112
new_file = open(CONFIG_FILENAME, "w")
113113
new_file.write("[this]\n\tthat = foobar\n\tthat = foobeer\n")
@@ -129,7 +129,7 @@ def test_write(self):
129129
self.assertFalse('core.dummy1' in config)
130130
config['core.dummy1'] = 42
131131
self.assertTrue('core.dummy1' in config)
132-
self.assertEqual(config['core.dummy1'], 42)
132+
self.assertEqual(config.get_int('core.dummy1'), 42)
133133

134134
self.assertFalse('core.dummy2' in config)
135135
config['core.dummy2'] = 'foobar'

0 commit comments

Comments
 (0)