config: provide access to the current line number
Users are nowadays trained to see message from CLI tools in the form <file>:<lno>: … To be able to give such messages when notifying the user about configurations in any config file, it is currently only possible to get the file name (if the value originates from a file to begin with) via `current_config_name()`. Now it is also possible to query the current line number for the configuration. Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Bert Wesarg committed
Jan 27, 2020 at 08:04 UTC
f2a2327a4a6a9bccade6df2bed1fdc3a8ab37f4c
4 files changed
+22
-2
config.c
+8
@@ -3338,6 +3338,14 @@ enum config_scope current_config_scope(void)
3338
return current_parsing_scope;
3339
}
3340
3341
+int current_config_line(void)
3342
+{
3343
+ if (current_config_kvi)
3344
+ return current_config_kvi->linenr;
3345
+ else
3346
+ return cf->linenr;
3347
+}
3348
+
3349
int lookup_config(const char **mapping, int nr_mapping, const char *var)
3350
{
3351
int i;
config.h
+1
@@ -309,6 +309,7 @@ int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
309
enum config_scope current_config_scope(void);
310
const char *current_config_origin_type(void);
311
const char *current_config_name(void);
312
+int current_config_line(void);
313
314
/**
315
* Include Directives
t/helper/test-config.c
+1
@@ -48,6 +48,7 @@ static int iterate_cb(const char *var, const char *value, void *data)
48
printf("value=%s\n", value ? value : "(null)");
49
printf("origin=%s\n", current_config_origin_type());
50
printf("name=%s\n", current_config_name());
51
+ printf("lno=%d\n", current_config_line());
52
printf("scope=%s\n", config_scope_name(current_config_scope()));
53
54
return 0;
t/t1308-config-set.sh
+12
-2
@@ -238,8 +238,8 @@ test_expect_success 'error on modifying repo config without repo' '
238
239
cmdline_config="'foo.bar=from-cmdline'"
240
test_expect_success 'iteration shows correct origins' '
241
- echo "[foo]bar = from-repo" >.git/config &&
242
- echo "[foo]bar = from-home" >.gitconfig &&
241
+ printf "[ignore]\n\tthis = please\n[foo]bar = from-repo\n" >.git/config &&
242
+ printf "[foo]\n\tbar = from-home\n" >.gitconfig &&
243
if test_have_prereq MINGW
244
then
245
# Use Windows path (i.e. *not* $HOME)
@@ -253,18 +253,28 @@ test_expect_success 'iteration shows correct origins' '
253
value=from-home
254
origin=file
255
name=$HOME_GITCONFIG
256
+ lno=2
257
scope=global
258
259
+ key=ignore.this
260
+ value=please
261
+ origin=file
262
+ name=.git/config
263
+ lno=2
264
+ scope=local
265
+
266
key=foo.bar
267
value=from-repo
268
origin=file
269
name=.git/config
270
+ lno=3
271
scope=local
272
273
key=foo.bar
274
value=from-cmdline
275
origin=command line
276
name=
277
+ lno=-1
278
scope=command
279
EOF
280
GIT_CONFIG_PARAMETERS=$cmdline_config test-tool config iterate >actual &&