@matheusbsbs / matheus-bagliano / commits / 1349484e34
Fork 0

builtin-config: add --path option doing ~ and ~user expansion.

395de250 (Expand ~ and ~user in core.excludesfile, commit.template) introduced a C function git_config_pathname, doing ~/ and ~user/ expansion. This patch makes the feature available to scripts with 'git config --get --path'. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Dec 30, 2009 at 17:51 UTC 1349484e341a3ec2ba02a86c8fbd97ea9dc8c756
3 files changed +58 -4
Documentation/git-config.txt
+11 -3
@@ -37,11 +37,12 @@ existing values that match the regexp are updated or unset. If
37 you want to handle the lines that do *not* match the regex, just
38 prepend a single exclamation mark in front (see also <<EXAMPLES>>).
39
40 -The type specifier can be either '--int' or '--bool', which will make
40 +The type specifier can be either '--int' or '--bool', to make
41 'git-config' ensure that the variable(s) are of the given type and
42 convert the value to the canonical form (simple decimal number for int,
43 -a "true" or "false" string for bool). If no type specifier is passed,
44 -no checks or transformations are performed on the value.
43 +a "true" or "false" string for bool), or '--path', which does some
44 +path expansion (see '--path' below). If no type specifier is passed, no
45 +checks or transformations are performed on the value.
46
47 The file-option can be one of '--system', '--global' or '--file'
48 which specify where the values will be read from or written to.
@@ -136,6 +137,13 @@ See also <<FILES>>.
137 'git-config' will ensure that the output matches the format of
138 either --bool or --int, as described above.
139
140 +--path::
141 + 'git-config' will expand leading '{tilde}' to the value of
142 + '$HOME', and '{tilde}user' to the home directory for the
143 + specified user. This option has no effect when setting the
144 + value (but you can use 'git config bla {tilde}/' from the
145 + command line to let your shell do the expansion).
146 +
147 -z::
148 --null::
149 For all options that output values and/or keys, always
builtin-config.c
+19 -1
@@ -45,6 +45,7 @@ static int end_null;
45 #define TYPE_BOOL (1<<0)
46 #define TYPE_INT (1<<1)
47 #define TYPE_BOOL_OR_INT (1<<2)
48 +#define TYPE_PATH (1<<3)
49
50 static struct option builtin_config_options[] = {
51 OPT_GROUP("Config file location"),
@@ -69,6 +70,7 @@ static struct option builtin_config_options[] = {
70 OPT_BIT(0, "bool", &types, "value is \"true\" or \"false\"", TYPE_BOOL),
71 OPT_BIT(0, "int", &types, "value is decimal number", TYPE_INT),
72 OPT_BIT(0, "bool-or-int", &types, "value is --bool or --int", TYPE_BOOL_OR_INT),
73 + OPT_BIT(0, "path", &types, "value is a path (file or directory name)", TYPE_PATH),
74 OPT_GROUP("Other"),
75 OPT_BOOLEAN('z', "null", &end_null, "terminate values with NUL byte"),
76 OPT_END(),
@@ -94,6 +96,7 @@ static int show_config(const char *key_, const char *value_, void *cb)
96 {
97 char value[256];
98 const char *vptr = value;
99 + int must_free_vptr = 0;
100 int dup_error = 0;
101
102 if (!use_key_regexp && strcmp(key_, key))
@@ -123,6 +126,9 @@ static int show_config(const char *key_, const char *value_, void *cb)
126 vptr = v ? "true" : "false";
127 else
128 sprintf(value, "%d", v);
129 + } else if (types == TYPE_PATH) {
130 + git_config_pathname(&vptr, key_, value_);
131 + must_free_vptr = 1;
132 }
133 else
134 vptr = value_?value_:"";
@@ -133,6 +139,12 @@ static int show_config(const char *key_, const char *value_, void *cb)
139 }
140 else
141 printf("%s%c", vptr, term);
142 + if (must_free_vptr)
143 + /* If vptr must be freed, it's a pointer to a
144 + * dynamically allocated buffer, it's safe to cast to
145 + * const.
146 + */
147 + free((char *)vptr);
148
149 return 0;
150 }
@@ -215,7 +227,13 @@ static char *normalize_value(const char *key, const char *value)
227 if (!value)
228 return NULL;
229
218 - if (types == 0)
230 + if (types == 0 || types == TYPE_PATH)
231 + /*
232 + * We don't do normalization for TYPE_PATH here: If
233 + * the path is like ~/foobar/, we prefer to store
234 + * "~/foobar/" in the config file, and to expand the ~
235 + * when retrieving the value.
236 + */
237 normalized = xstrdup(value);
238 else {
239 normalized = xmalloc(64);
t/t1300-repo-config.sh
+28
@@ -683,6 +683,34 @@ test_expect_success 'set --bool-or-int' '
683
684 rm .git/config
685
686 +cat >expect <<\EOF
687 +[path]
688 + home = ~/
689 + normal = /dev/null
690 + trailingtilde = foo~
691 +EOF
692 +
693 +test_expect_success 'set --path' '
694 + git config --path path.home "~/" &&
695 + git config --path path.normal "/dev/null" &&
696 + git config --path path.trailingtilde "foo~" &&
697 + test_cmp expect .git/config'
698 +
699 +cat >expect <<EOF
700 +$HOME/
701 +/dev/null
702 +foo~
703 +EOF
704 +
705 +test_expect_success 'get --path' '
706 + git config --get --path path.home > result &&
707 + git config --get --path path.normal >> result &&
708 + git config --get --path path.trailingtilde >> result &&
709 + test_cmp expect result
710 +'
711 +
712 +rm .git/config
713 +
714 git config quote.leading " test"
715 git config quote.ending "test "
716 git config quote.semicolon "test;test"