@@ -168,43 +168,94 @@ Config_contains(Config *self, PyObject *py_key) {
168
168
return 1 ;
169
169
}
170
170
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
+ }
171
198
172
199
PyObject *
173
200
Config_getitem (Config * self , PyObject * py_key )
174
201
{
175
- int64_t value_int ;
176
- int err , value_bool ;
202
+ int err ;
177
203
const char * value_str ;
178
- const char * key ;
179
- PyObject * py_value , * tmp ;
180
204
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 )
183
207
return NULL ;
184
208
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 );
187
227
if (err < 0 )
188
- goto cleanup ;
228
+ return NULL ;
189
229
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 ;
196
232
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
+ }
203
235
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 ;
206
257
207
- return py_value ;
258
+ return PyLong_FromLongLong ( value ) ;
208
259
}
209
260
210
261
int
@@ -369,6 +420,8 @@ PyMethodDef Config_methods[] = {
369
420
METHOD (Config , add_file , METH_VARARGS | METH_KEYWORDS ),
370
421
METHOD (Config , get_multivar , METH_VARARGS ),
371
422
METHOD (Config , set_multivar , METH_VARARGS ),
423
+ METHOD (Config , get_bool , METH_O ),
424
+ METHOD (Config , get_int , METH_O ),
372
425
{NULL }
373
426
};
374
427
0 commit comments