]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: provide access to the current line number
authorBert Wesarg <bert.wesarg@googlemail.com>
Mon, 27 Jan 2020 07:04:31 +0000 (08:04 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Feb 2020 18:52:10 +0000 (10:52 -0800)
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>
config.c
config.h
t/helper/test-config.c
t/t1308-config-set.sh

index 18a6bdd9ffe587434ae5e7aea82d6ca2cde5c0d0..222e49a7c868fe3eefc012c486f5660b5c911c2e 100644 (file)
--- a/config.c
+++ b/config.c
@@ -3338,6 +3338,14 @@ enum config_scope current_config_scope(void)
                return current_parsing_scope;
 }
 
+int current_config_line(void)
+{
+       if (current_config_kvi)
+               return current_config_kvi->linenr;
+       else
+               return cf->linenr;
+}
+
 int lookup_config(const char **mapping, int nr_mapping, const char *var)
 {
        int i;
index fe0addb0dc96cd1150c61c5028e0d41424153ee5..9b3773f77826513226608a592e3295d7a6ea5e3f 100644 (file)
--- a/config.h
+++ b/config.h
@@ -309,6 +309,7 @@ int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
 enum config_scope current_config_scope(void);
 const char *current_config_origin_type(void);
 const char *current_config_name(void);
+int current_config_line(void);
 
 /**
  * Include Directives
index 1e3bc7c8f4a57e131bb40a2044d1cbbabe754211..234c722b485e1ecdf9f00ba56d119c8364a6f949 100644 (file)
@@ -48,6 +48,7 @@ static int iterate_cb(const char *var, const char *value, void *data)
        printf("value=%s\n", value ? value : "(null)");
        printf("origin=%s\n", current_config_origin_type());
        printf("name=%s\n", current_config_name());
+       printf("lno=%d\n", current_config_line());
        printf("scope=%s\n", config_scope_name(current_config_scope()));
 
        return 0;
index fba0abe429ace841f720cc8a41281bdf08943053..3a527e3a8438a583bfd8704ad23f9ae2e48ac382 100755 (executable)
@@ -238,8 +238,8 @@ test_expect_success 'error on modifying repo config without repo' '
 
 cmdline_config="'foo.bar=from-cmdline'"
 test_expect_success 'iteration shows correct origins' '
-       echo "[foo]bar = from-repo" >.git/config &&
-       echo "[foo]bar = from-home" >.gitconfig &&
+       printf "[ignore]\n\tthis = please\n[foo]bar = from-repo\n" >.git/config &&
+       printf "[foo]\n\tbar = from-home\n" >.gitconfig &&
        if test_have_prereq MINGW
        then
                # Use Windows path (i.e. *not* $HOME)
@@ -253,18 +253,28 @@ test_expect_success 'iteration shows correct origins' '
        value=from-home
        origin=file
        name=$HOME_GITCONFIG
+       lno=2
        scope=global
 
+       key=ignore.this
+       value=please
+       origin=file
+       name=.git/config
+       lno=2
+       scope=local
+
        key=foo.bar
        value=from-repo
        origin=file
        name=.git/config
+       lno=3
        scope=local
 
        key=foo.bar
        value=from-cmdline
        origin=command line
        name=
+       lno=-1
        scope=command
        EOF
        GIT_CONFIG_PARAMETERS=$cmdline_config test-tool config iterate >actual &&