{"thread":{"id":"10994","subject":"[PATCH] Add config_int() method to the Git perl module","startedAt":"2007-11-23T18:04:52Z","lastAt":"2007-11-28T07:58:59Z","messageCount":6,"participants":["Jakub Narebski","Junio C Hamano","Wincent Colaiuta","Jeff King","Dan Zwell"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"60770","messageId":"200711231904.53551.jnareb@gmail.com","threadId":"10994","inReplyTo":null,"subject":"[PATCH] Add config_int() method to the Git perl module","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2007-11-23T18:04:52Z","receivedAt":"2007-11-23T18:04:52Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Integer variables can have optional 'k', 'm' or 'g' suffix.\nconfig_int() method will return simple decimal number, taking\ncare of those suffixes.\n\nSigned-off-by: Jakub Narebski <jnareb@gmail.com>\n---\nFor completness (I don't think anything in Perl uses / tries to use\ninteger configuration variables).\n\n(Commit message could be better. Hmmm...)\n\n perl/Git.pm |   31 +++++++++++++++++++++++++++++++\n 1 files changed, 31 insertions(+), 0 deletions(-)\n\ndiff --git a/perl/Git.pm b/perl/Git.pm\nindex dca92c8..7468460 100644\n--- a/perl/Git.pm\n+++ b/perl/Git.pm\n@@ -549,6 +549,37 @@ sub config_bool {\n \t};\n }\n \n+=item config_int ( VARIABLE )\n+\n+Retrieve the integer configuration C<VARIABLE>. The return value\n+is simple decimal number.  An optional value suffix of 'k', 'm',\n+or 'g' in the config file will cause the value to be multiplied\n+by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output.\n+It would return C<undef> if configuration variable is not defined,\n+\n+Must be called on a repository instance.\n+\n+This currently wraps command('config') so it is not so fast.\n+\n+=cut\n+\n+sub config_int {\n+\tmy ($self, $var) = @_;\n+\t$self->repo_path()\n+\t\tor throw Error::Simple(\"not a repository\");\n+\n+\ttry {\n+\t\treturn $self->command_oneline('config', '--int', '--get', $var);\n+\t} catch Git::Error::Command with {\n+\t\tmy $E = shift;\n+\t\tif ($E->value() == 1) {\n+\t\t\t# Key not found.\n+\t\t\treturn undef;\n+\t\t} else {\n+\t\t\tthrow $E;\n+\t\t}\n+\t};\n+}\n \n =item ident ( TYPE | IDENTSTR )\n \n-- \n1.5.3.5\n"},{"id":"60784","messageId":"7vabp4u40y.fsf@gitster.siamese.dyndns.org","threadId":"10994","inReplyTo":"200711231904.53551.jnareb@gmail.com","subject":"Re: [PATCH] Add config_int() method to the Git perl module","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-11-23T19:59:25Z","receivedAt":"2007-11-23T19:59:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jakub Narebski <jnareb@gmail.com> writes:\n\n> Integer variables can have optional 'k', 'm' or 'g' suffix.\n> config_int() method will return simple decimal number, taking\n> care of those suffixes.\n\nGood.  I forgot about --int option to \"git config\" already.\n\nMaybe in a similar way, we might want to add --color to \"git\nconfig\" to return ANSI sequence, so that Git::config_color() can\nwork without even loading Term::ANSIColor?\n"},{"id":"60789","messageId":"5B17707B-115E-45C8-895F-BD7020A3355A@wincent.com","threadId":"10994","inReplyTo":"7vabp4u40y.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] Add config_int() method to the Git perl module","fromName":"Wincent Colaiuta","fromEmail":"win@wincent.com","sentAt":"2007-11-23T20:57:20Z","receivedAt":"2007-11-23T20:57:20Z","isPatch":true,"sender":{"key":"greg@hurrell.net","avatar":"https://avatars.githubusercontent.com/u/7074?v=4"},"body":"El 23/11/2007, a las 20:59, Junio C Hamano escribió:\n\n> Jakub Narebski <jnareb@gmail.com> writes:\n>\n>> Integer variables can have optional 'k', 'm' or 'g' suffix.\n>> config_int() method will return simple decimal number, taking\n>> care of those suffixes.\n>\n> Good.  I forgot about --int option to \"git config\" already.\n>\n> Maybe in a similar way, we might want to add --color to \"git\n> config\" to return ANSI sequence, so that Git::config_color() can\n> work without even loading Term::ANSIColor?\n\nOr failing that, it would be very easy to remove the dependency on  \nTerm::ANSIColor by adding something like this to Git.pm:\n\n+my $COLOR = {\n+       \"reset\"         => \"\\e[0m\",\n+       \"normal\"        => \"\",\n+       \"black\"         => \"\\e[30m\",\n+       \"red\"           => \"\\e[31m\",\n+       \"green\"         => \"\\e[32m\",\n+       \"yellow\"        => \"\\e[33m\",\n+       \"blue\"          => \"\\e[34m\",\n+       \"magenta\"       => \"\\e[35m\",\n+       \"cyan\"          => \"\\e[36m\",\n+       \"white\"         => \"\\e[37m\",\n+       \"bold\"          => \"\\e[1m\",\n+       \"ul\"            => \"\\e[4m\",\n+       \"blink\"         => \"\\e[5m\",\n+       \"reverse\"       => \"\\e[7m\",\n+       \"on_red\"        => \"\\e[41m\",\n+};\n+\n+sub color {\n+       my $desired_color = shift;\n+       return $COLOR->{$desired_color} || \"\";\n+}\n\nCheers,\nWincent\n"},{"id":"60808","messageId":"20071124114839.GC17861@sigill.intra.peff.net","threadId":"10994","inReplyTo":"7vabp4u40y.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] Add config_int() method to the Git perl module","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2007-11-24T11:48:40Z","receivedAt":"2007-11-24T11:48:40Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Nov 23, 2007 at 11:59:25AM -0800, Junio C Hamano wrote:\n\n> Maybe in a similar way, we might want to add --color to \"git\n> config\" to return ANSI sequence, so that Git::config_color() can\n> work without even loading Term::ANSIColor?\n\nWe would also need a way of parsing the 'default' values, so git-config\nwould need a way of saying \"turn this value into its internal\nrepresentation\" (or the perl script would have to specify its defaults\nas raw ANSI codes, which is a bit ugly, but is that the C programs do).\n\n-Peff\n"},{"id":"61177","messageId":"7vir3mal9u.fsf_-_@gitster.siamese.dyndns.org","threadId":"10994","inReplyTo":"7vabp4u40y.fsf@gitster.siamese.dyndns.org","subject":"[PATCH] git-config --get-color: get configured color","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-11-28T07:20:45Z","receivedAt":"2007-11-28T07:20:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This new option allows scripts to grab color setting from the user\nconfiguration, translated to ANSI color escape sequence.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n\n Junio C Hamano <gitster@pobox.com> writes:\n\n > Jakub Narebski <jnareb@gmail.com> writes:\n >\n >> Integer variables can have optional 'k', 'm' or 'g' suffix.\n >> config_int() method will return simple decimal number, taking\n >> care of those suffixes.\n >\n > Good.  I forgot about --int option to \"git config\" already.\n >\n > Maybe in a similar way, we might want to add --color to \"git\n > config\" to return ANSI sequence, so that Git::config_color() can\n > work without even loading Term::ANSIColor?\n\n Documentation/git-config.txt |   16 ++++++++++++\n builtin-config.c             |   55 +++++++++++++++++++++++++++++++++++++++--\n 2 files changed, 68 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-config.txt b/Documentation/git-config.txt\nindex a592b61..ed3076f 100644\n--- a/Documentation/git-config.txt\n+++ b/Documentation/git-config.txt\n@@ -20,6 +20,7 @@ SYNOPSIS\n 'git-config' [<file-option>] --rename-section old_name new_name\n 'git-config' [<file-option>] --remove-section name\n 'git-config' [<file-option>] [-z|--null] -l | --list\n+'git-config' [<file-option>] --get-color name [default]\n \n DESCRIPTION\n -----------\n@@ -134,6 +135,12 @@ See also <<FILES>>.\n \toutput without getting confused e.g. by values that\n \tcontain line breaks.\n \n+--get-color name default::\n+\n+\tFind the color configured for `name` (e.g. `color.diff.new`) and\n+\toutput it as the ANSI color escape sequence to the standard\n+\toutput.  The optional `default` parameter is used if the\n+\tconfiguration for `name` is missing.\n \n [[FILES]]\n FILES\n@@ -292,6 +299,15 @@ To add a new proxy, without altering any of the existing ones, use\n % git config core.gitproxy '\"proxy-command\" for example.com'\n ------------\n \n+An example to use customized color from the configuration in your\n+script:\n+\n+------------\n+#!/bin/sh\n+WS=$(git config --get-color color.diff.whitespace \"blue reverse\")\n+RESET==$(git config --get-color \"\" \"reset\")\n+echo \"${WS}your whitespace color or blue reverse${RESET}\"\n+------------\n \n include::config.txt[]\n \ndiff --git a/builtin-config.c b/builtin-config.c\nindex f672c9c..4c9ded3 100644\n--- a/builtin-config.c\n+++ b/builtin-config.c\n@@ -1,8 +1,9 @@\n #include \"builtin.h\"\n #include \"cache.h\"\n+#include \"color.h\"\n \n static const char git_config_set_usage[] =\n-\"git-config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int ] [ -z | --null ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list\";\n+\"git-config [ --global | --system | [ -f | --file ] config-file ] [ --bool | --int ] [ -z | --null ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list | --get-color var [default]\";\n \n static char *key;\n static regex_t *key_regexp;\n@@ -161,6 +162,53 @@ char *normalize_value(const char *key, const char *value)\n \treturn normalized;\n }\n \n+static int get_color_found;\n+static const char *get_color_slot;\n+static char parsed_color[COLOR_MAXLEN];\n+\n+static int git_get_color_config(const char *var, const char *value)\n+{\n+\tif (!strcmp(var, get_color_slot)) {\n+\t\tcolor_parse(value, var, parsed_color);\n+\t\tget_color_found = 1;\n+\t}\n+\treturn 0;\n+}\n+\n+static int get_color(int argc, const char **argv)\n+{\n+\t/*\n+\t * grab the color setting for the given slot from the configuration,\n+\t * or parse the default value if missing, and return ANSI color\n+\t * escape sequence.\n+\t *\n+\t * e.g.\n+\t * git config --get-color color.diff.whitespace \"blue reverse\"\n+\t */\n+\tconst char *def_color = NULL;\n+\n+\tswitch (argc) {\n+\tdefault:\n+\t\tusage(git_config_set_usage);\n+\tcase 2:\n+\t\tdef_color = argv[1];\n+\t\t/* fallthru */\n+\tcase 1:\n+\t\tget_color_slot = argv[0];\n+\t\tbreak;\n+\t}\n+\n+\tget_color_found = 0;\n+\tparsed_color[0] = '\\0';\n+\tgit_config(git_get_color_config);\n+\n+\tif (!get_color_found && def_color)\n+\t\tcolor_parse(def_color, \"command line\", parsed_color);\n+\n+\tfputs(parsed_color, stdout);\n+\treturn 0;\n+}\n+\n int cmd_config(int argc, const char **argv, const char *prefix)\n {\n \tint nongit = 0;\n@@ -234,8 +282,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)\n \t\t\t\treturn 1;\n \t\t\t}\n \t\t\treturn 0;\n-\t\t}\n-\t\telse\n+\t\t} else if (!strcmp(argv[1], \"--get-color\")) {\n+\t\t\treturn get_color(argc-2, argv+2);\n+\t\t} else\n \t\t\tbreak;\n \t\targc--;\n \t\targv++;\n-- \n1.5.3.6.2039.g0495\n"},{"id":"61184","messageId":"474D1FC3.50108@zwell.net","threadId":"10994","inReplyTo":"7vir3mal9u.fsf_-_@gitster.siamese.dyndns.org","subject":"Re: [PATCH] git-config --get-color: get configured color","fromName":"Dan Zwell","fromEmail":"dzwell@gmail.com","sentAt":"2007-11-28T07:58:59Z","receivedAt":"2007-11-28T07:58:59Z","isPatch":true,"sender":{"key":"dzwell@gmail.com","avatar":null},"body":"Junio C Hamano wrote:\n> +--get-color name default::\n> +\n> +\tFind the color configured for `name` (e.g. `color.diff.new`) and\n> +\toutput it as the ANSI color escape sequence to the standard\n> +\toutput.  The optional `default` parameter is used if the\n> +\tconfiguration for `name` is missing.\nPerhaps you could mention here that the second parameter is a normal \nstring, not an ansi code? Just adding 'for example, \"blue\"' would \nprobably help.\n\n > +RESET==$(git config --get-color \"\" \"reset\")\n\nThis is a typo, no?\n+RESET=$(git config --get-color \"\" \"reset\")\n\nDan\n"}]}