Skip to content

Commit f2a2327

Browse files
bertwesarggitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 923d4a5 commit f2a2327

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

config.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3338,6 +3338,14 @@ enum config_scope current_config_scope(void)
33383338
return current_parsing_scope;
33393339
}
33403340

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+
33413349
int lookup_config(const char **mapping, int nr_mapping, const char *var)
33423350
{
33433351
int i;

config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
309309
enum config_scope current_config_scope(void);
310310
const char *current_config_origin_type(void);
311311
const char *current_config_name(void);
312+
int current_config_line(void);
312313

313314
/**
314315
* Include Directives

t/helper/test-config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static int iterate_cb(const char *var, const char *value, void *data)
4848
printf("value=%s\n", value ? value : "(null)");
4949
printf("origin=%s\n", current_config_origin_type());
5050
printf("name=%s\n", current_config_name());
51+
printf("lno=%d\n", current_config_line());
5152
printf("scope=%s\n", config_scope_name(current_config_scope()));
5253

5354
return 0;

t/t1308-config-set.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ test_expect_success 'error on modifying repo config without repo' '
238238

239239
cmdline_config="'foo.bar=from-cmdline'"
240240
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 &&
243243
if test_have_prereq MINGW
244244
then
245245
# Use Windows path (i.e. *not* $HOME)
@@ -253,18 +253,28 @@ test_expect_success 'iteration shows correct origins' '
253253
value=from-home
254254
origin=file
255255
name=$HOME_GITCONFIG
256+
lno=2
256257
scope=global
257258
259+
key=ignore.this
260+
value=please
261+
origin=file
262+
name=.git/config
263+
lno=2
264+
scope=local
265+
258266
key=foo.bar
259267
value=from-repo
260268
origin=file
261269
name=.git/config
270+
lno=3
262271
scope=local
263272
264273
key=foo.bar
265274
value=from-cmdline
266275
origin=command line
267276
name=
277+
lno=-1
268278
scope=command
269279
EOF
270280
GIT_CONFIG_PARAMETERS=$cmdline_config test-tool config iterate >actual &&

0 commit comments

Comments
 (0)