{"thread":{"id":"22066","subject":"[PATCH] builtin-config: add --path option doing ~ and ~user expansion.","startedAt":"2009-12-30T16:51:53Z","lastAt":"2010-01-01T05:15:23Z","messageCount":2,"participants":["Matthieu Moy","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"130564","messageId":"1262191913-8340-1-git-send-email-Matthieu.Moy@imag.fr","threadId":"22066","inReplyTo":null,"subject":"[PATCH] builtin-config: add --path option doing ~ and ~user expansion.","fromName":"Matthieu Moy","fromEmail":"matthieu.moy@imag.fr","sentAt":"2009-12-30T16:51:53Z","receivedAt":"2009-12-30T16:51:53Z","isPatch":true,"sender":{"key":"git@matthieu-moy.fr","avatar":"https://avatars.githubusercontent.com/u/14709?v=4"},"body":"395de250 (Expand ~ and ~user in core.excludesfile, commit.template)\nintroduced a C function git_config_pathname, doing ~/ and ~user/\nexpansion. This patch makes the feature available to scripts with 'git\nconfig --get --path'.\n\nSigned-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>\n---\n Documentation/git-config.txt |   14 +++++++++++---\n builtin-config.c             |   20 +++++++++++++++++++-\n t/t1300-repo-config.sh       |   28 ++++++++++++++++++++++++++++\n 3 files changed, 58 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/git-config.txt b/Documentation/git-config.txt\nindex f68b198..2632928 100644\n--- a/Documentation/git-config.txt\n+++ b/Documentation/git-config.txt\n@@ -37,11 +37,12 @@ existing values that match the regexp are updated or unset.  If\n you want to handle the lines that do *not* match the regex, just\n prepend a single exclamation mark in front (see also <<EXAMPLES>>).\n \n-The type specifier can be either '--int' or '--bool', which will make\n+The type specifier can be either '--int' or '--bool', to make\n 'git-config' ensure that the variable(s) are of the given type and\n convert the value to the canonical form (simple decimal number for int,\n-a \"true\" or \"false\" string for bool).  If no type specifier is passed,\n-no checks or transformations are performed on the value.\n+a \"true\" or \"false\" string for bool), or '--path', which does some\n+path expansion (see '--path' below).  If no type specifier is passed, no\n+checks or transformations are performed on the value.\n \n The file-option can be one of '--system', '--global' or '--file'\n which specify where the values will be read from or written to.\n@@ -136,6 +137,13 @@ See also <<FILES>>.\n \t'git-config' will ensure that the output matches the format of\n \teither --bool or --int, as described above.\n \n+--path::\n+\t'git-config' will expand leading '{tilde}' to the value of\n+\t'$HOME', and '{tilde}user' to the home directory for the\n+\tspecified user.  This option has no effect when setting the\n+\tvalue (but you can use 'git config bla {tilde}/' from the\n+\tcommand line to let your shell do the expansion).\n+\n -z::\n --null::\n \tFor all options that output values and/or keys, always\ndiff --git a/builtin-config.c b/builtin-config.c\nindex a2d656e..2e3ef91 100644\n--- a/builtin-config.c\n+++ b/builtin-config.c\n@@ -45,6 +45,7 @@ static int end_null;\n #define TYPE_BOOL (1<<0)\n #define TYPE_INT (1<<1)\n #define TYPE_BOOL_OR_INT (1<<2)\n+#define TYPE_PATH (1<<3)\n \n static struct option builtin_config_options[] = {\n \tOPT_GROUP(\"Config file location\"),\n@@ -69,6 +70,7 @@ static struct option builtin_config_options[] = {\n \tOPT_BIT(0, \"bool\", &types, \"value is \\\"true\\\" or \\\"false\\\"\", TYPE_BOOL),\n \tOPT_BIT(0, \"int\", &types, \"value is decimal number\", TYPE_INT),\n \tOPT_BIT(0, \"bool-or-int\", &types, \"value is --bool or --int\", TYPE_BOOL_OR_INT),\n+\tOPT_BIT(0, \"path\", &types, \"value is a path (file or directory name)\", TYPE_PATH),\n \tOPT_GROUP(\"Other\"),\n \tOPT_BOOLEAN('z', \"null\", &end_null, \"terminate values with NUL byte\"),\n \tOPT_END(),\n@@ -94,6 +96,7 @@ static int show_config(const char *key_, const char *value_, void *cb)\n {\n \tchar value[256];\n \tconst char *vptr = value;\n+\tint must_free_vptr = 0;\n \tint dup_error = 0;\n \n \tif (!use_key_regexp && strcmp(key_, key))\n@@ -123,6 +126,9 @@ static int show_config(const char *key_, const char *value_, void *cb)\n \t\t\tvptr = v ? \"true\" : \"false\";\n \t\telse\n \t\t\tsprintf(value, \"%d\", v);\n+\t} else if (types == TYPE_PATH) {\n+\t\tgit_config_pathname(&vptr, key_, value_);\n+\t\tmust_free_vptr = 1;\n \t}\n \telse\n \t\tvptr = value_?value_:\"\";\n@@ -133,6 +139,12 @@ static int show_config(const char *key_, const char *value_, void *cb)\n \t}\n \telse\n \t\tprintf(\"%s%c\", vptr, term);\n+\tif (must_free_vptr)\n+\t\t/* If vptr must be freed, it's a pointer to a\n+\t\t * dynamically allocated buffer, it's safe to cast to\n+\t\t * const.\n+\t\t*/\n+\t\tfree((char *)vptr);\n \n \treturn 0;\n }\n@@ -215,7 +227,13 @@ static char *normalize_value(const char *key, const char *value)\n \tif (!value)\n \t\treturn NULL;\n \n-\tif (types == 0)\n+\tif (types == 0 || types == TYPE_PATH)\n+\t\t/*\n+\t\t * We don't do normalization for TYPE_PATH here: If\n+\t\t * the path is like ~/foobar/, we prefer to store\n+\t\t * \"~/foobar/\" in the config file, and to expand the ~\n+\t\t * when retrieving the value.\n+\t\t */\n \t\tnormalized = xstrdup(value);\n \telse {\n \t\tnormalized = xmalloc(64);\ndiff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh\nindex 83b7294..f89d7e9 100755\n--- a/t/t1300-repo-config.sh\n+++ b/t/t1300-repo-config.sh\n@@ -683,6 +683,34 @@ test_expect_success 'set --bool-or-int' '\n \n rm .git/config\n \n+cat >expect <<\\EOF\n+[path]\n+\thome = ~/\n+\tnormal = /dev/null\n+\ttrailingtilde = foo~\n+EOF\n+\n+test_expect_success 'set --path' '\n+\tgit config --path path.home \"~/\" &&\n+\tgit config --path path.normal \"/dev/null\" &&\n+\tgit config --path path.trailingtilde \"foo~\" &&\n+\ttest_cmp expect .git/config'\n+\n+cat >expect <<EOF\n+$HOME/\n+/dev/null\n+foo~\n+EOF\n+\n+test_expect_success 'get --path' '\n+\tgit config --get --path path.home > result &&\n+\tgit config --get --path path.normal >> result &&\n+\tgit config --get --path path.trailingtilde >> result &&\n+\ttest_cmp expect result\n+'\n+\n+rm .git/config\n+\n git config quote.leading \" test\"\n git config quote.ending \"test \"\n git config quote.semicolon \"test;test\"\n-- \n1.6.6.71.g392d.dirty\n"},{"id":"130656","messageId":"7vocleo178.fsf@alter.siamese.dyndns.org","threadId":"22066","inReplyTo":"1262191913-8340-1-git-send-email-Matthieu.Moy@imag.fr","subject":"Re: [PATCH] builtin-config: add --path option doing ~ and ~user expansion.","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-01-01T05:15:23Z","receivedAt":"2010-01-01T05:15:23Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Matthieu Moy <Matthieu.Moy@imag.fr> writes:\n\n> 395de250 (Expand ~ and ~user in core.excludesfile, commit.template)\n> introduced a C function git_config_pathname, doing ~/ and ~user/\n> expansion. This patch makes the feature available to scripts with 'git\n> config --get --path'.\n\nMakes sense.  Thanks.\n"}]}