{"thread":{"id":"11141","subject":"[PATCH] Color support for \"git-add -i\"","startedAt":"2007-12-05T10:59:04Z","lastAt":"2007-12-07T12:34:51Z","messageCount":18,"participants":["Junio C Hamano","Johannes Sixt","Wincent Colaiuta","Jeff King","Eric Wong"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"62000","messageId":"7vbq95tnk7.fsf@gitster.siamese.dyndns.org","threadId":"11141","inReplyTo":null,"subject":"[PATCH] Color support for \"git-add -i\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-05T10:59:04Z","receivedAt":"2007-12-05T10:59:04Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This is mostly lifted from earlier series by Dan Zwell, but updated to\nuse \"git config --get-color\" to make it simpler and more consistent with\ncommands written in C.\n\nA new configuration color.interactive variable is like color.diff and\ncolor.status, and controls if \"git-add -i\" uses color.\n\nA set of configuration variables, color.interactive.<slot>, are used to\ndefine what color is used for the prompt, header, and help text.\n\nFor perl scripts, Git.pm provides $repo->get_color() method, which takes\nthe slot name and the default color, and returns the terminal escape\nsequence to color the output text.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n\n * I'd like to have the colored \"add -i\" by 1.5.4-rc0 so I updated the\n   series myself.  Ack, improvements, fixes?\n\n Documentation/config.txt  |   13 +++++\n git-add--interactive.perl |  121 +++++++++++++++++++++++++++++++++++++-------\n perl/Git.pm               |   20 +++++++\n 3 files changed, 134 insertions(+), 20 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 72a33e9..c94f252 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -391,6 +391,19 @@ color.diff.<slot>::\n \twhitespace).  The values of these variables may be specified as\n \tin color.branch.<slot>.\n \n+color.interactive::\n+\tWhen true (or `always`), always use colors in `git add\n+\t--interactive`.  When false (or `never`), never.  When set to\n+\t`auto`, use colors only when the output is to the\n+\tterminal. Defaults to false.\n+\n+color.interactive.<slot>::\n+\tUse customized color for `git add --interactive`\n+\toutput. `<slot>` may be `prompt`, `header`, or `help`, for\n+\tthree distinct types of normal output from interactive\n+\tprograms.  The values of these variables may be specified as\n+\tin color.branch.<slot>.\n+\n color.pager::\n \tA boolean to enable/disable colored output when the pager is in\n \tuse (default is true).\ndiff --git a/git-add--interactive.perl b/git-add--interactive.perl\nindex 335c2c6..3bcccfb 100755\n--- a/git-add--interactive.perl\n+++ b/git-add--interactive.perl\n@@ -1,6 +1,57 @@\n #!/usr/bin/perl -w\n \n use strict;\n+use Git;\n+\n+# Prompt colors:\n+my ($use_color, $prompt_color, $header_color, $help_color, $normal_color);\n+# Diff colors:\n+my ($diff_use_color, $new_color, $old_color, $fraginfo_color,\n+    $metainfo_color, $whitespace_color);\n+my $color_config = qx(git config --get color.interactive);\n+if ($color_config=~/true|always/ || -t STDOUT && $color_config=~/auto/) {\n+\tif (!$@) {\n+\t\t$use_color = 1;\n+\t\t# Set interactive colors:\n+\n+\t\t# Grab the 3 main colors in git color string format, with sane\n+\t\t# (visible) defaults:\n+\t\tmy $repo = Git->repository();\n+\t\t$prompt_color = $repo->get_color(\"color.interactive.prompt\", \"bold blue\");\n+\t\t$header_color = $repo->get_color(\"color.interactive.header\", \"bold\");\n+\t\t$help_color = $repo->get_color(\"color.interactive.help\", \"red bold\");\n+\t\t$normal_color = $repo->get_color(\"\", \"reset\");\n+\n+\t\t# Do we also set diff colors?\n+\t\tmy $diff_colors = Git::config($repo, \"color.diff\");\n+\t\tif ($diff_colors =~ /true/ ||\n+\t\t    (-t STDOUT && $diff_colors =~ /auto/)) {\n+\t\t\t$diff_use_color = 1;\n+\t\t\t$new_color = $repo->get_color(\"color.diff.new\", \"green\");\n+\t\t\t$old_color = $repo->get_color(\"color.diff.old\", \"red\");\n+\t\t\t$fraginfo_color = $repo->get_color(\"color.diff.frag\", \"cyan\");\n+\t\t\t$metainfo_color = $repo->get_color(\"color.diff.meta\", \"bold\");\n+\t\t\t$whitespace_color = $repo->get_color(\"color.diff.whitespace\", \"normal red\");\n+\t\t}\n+\t}\n+}\n+\n+sub colored {\n+\tmy $color = shift;\n+\tmy $string = join(\"\", @_);\n+\n+\tif ($use_color) {\n+\t\t# Put a color code at the beginning of each line, a reset at the end\n+\t\t# color after newlines that are not at the end of the string\n+\t\t$string =~ s/(\\n+)(.)/$1$color$2/g;\n+\t\t# reset before newlines\n+\t\t$string =~ s/(\\n+)/$normal_color$1/g;\n+\t\t# codes at beginning and end (if necessary):\n+\t\t$string =~ s/^/$color/;\n+\t\t$string =~ s/$/$normal_color/ unless $string =~ /\\n$/;\n+\t}\n+\treturn $string;\n+}\n \n # command line options\n my $patch_mode;\n@@ -246,10 +297,20 @@ sub is_valid_prefix {\n sub highlight_prefix {\n \tmy $prefix = shift;\n \tmy $remainder = shift;\n-\treturn $remainder unless defined $prefix;\n-\treturn is_valid_prefix($prefix) ?\n-\t    \"[$prefix]$remainder\" :\n-\t    \"$prefix$remainder\";\n+\n+\tif (!defined $prefix) {\n+\t\treturn $remainder;\n+\t}\n+\n+\tif (!is_valid_prefix($prefix)) {\n+\t\treturn \"$prefix$remainder\";\n+\t}\n+\n+\tif (!$use_color) {\n+\t\treturn \"[$prefix]$remainder\";\n+\t}\n+\n+\treturn \"$prompt_color$prefix$normal_color$remainder\";\n }\n \n sub list_and_choose {\n@@ -266,7 +327,7 @@ sub list_and_choose {\n \t\t\tif (!$opts->{LIST_FLAT}) {\n \t\t\t\tprint \"     \";\n \t\t\t}\n-\t\t\tprint \"$opts->{HEADER}\\n\";\n+\t\t\tprint colored $header_color, \"$opts->{HEADER}\\n\";\n \t\t}\n \t\tfor ($i = 0; $i < @stuff; $i++) {\n \t\t\tmy $chosen = $chosen[$i] ? '*' : ' ';\n@@ -304,7 +365,7 @@ sub list_and_choose {\n \n \t\treturn if ($opts->{LIST_ONLY});\n \n-\t\tprint $opts->{PROMPT};\n+\t\tprint colored $prompt_color, $opts->{PROMPT};\n \t\tif ($opts->{SINGLETON}) {\n \t\t\tprint \"> \";\n \t\t}\n@@ -371,7 +432,7 @@ sub list_and_choose {\n }\n \n sub singleton_prompt_help_cmd {\n-\tprint <<\\EOF ;\n+\tprint colored $help_color, <<\\EOF ;\n Prompt help:\n 1          - select a numbered item\n foo        - select item based on unique prefix\n@@ -380,7 +441,7 @@ EOF\n }\n \n sub prompt_help_cmd {\n-\tprint <<\\EOF ;\n+\tprint colored $help_color, <<\\EOF ;\n Prompt help:\n 1          - select a single item\n 3-5        - select a range of items\n@@ -477,6 +538,31 @@ sub parse_diff {\n \treturn @hunk;\n }\n \n+sub colored_diff_hunk {\n+\tmy ($text) = @_;\n+\t# return the text, so that it can be passed to print()\n+\tmy @ret;\n+\tfor (@$text) {\n+\t\tif (!$diff_use_color) {\n+\t\t\tpush @ret, $_;\n+\t\t\tnext;\n+\t\t}\n+\n+\t\tif (/^\\+/) {\n+\t\t\tpush @ret, colored($new_color, $_);\n+\t\t} elsif (/^\\-/) {\n+\t\t\tpush @ret, colored($old_color, $_);\n+\t\t} elsif (/^\\@/) {\n+\t\t\tpush @ret, colored($fraginfo_color, $_);\n+\t\t} elsif (/^ /) {\n+\t\t\tpush @ret, colored($normal_color, $_);\n+\t\t} else {\n+\t\t\tpush @ret, colored($metainfo_color, $_);\n+\t\t}\n+\t}\n+\treturn @ret;\n+}\n+\n sub hunk_splittable {\n \tmy ($text) = @_;\n \n@@ -671,7 +757,7 @@ sub coalesce_overlapping_hunks {\n }\n \n sub help_patch_cmd {\n-\tprint <<\\EOF ;\n+\tprint colored $help_color, <<\\EOF ;\n y - stage this hunk\n n - do not stage this hunk\n a - stage this and all the remaining hunks in the file\n@@ -710,9 +796,7 @@ sub patch_update_file {\n \tmy ($ix, $num);\n \tmy $path = shift;\n \tmy ($head, @hunk) = parse_diff($path);\n-\tfor (@{$head->{TEXT}}) {\n-\t\tprint;\n-\t}\n+\tprint colored_diff_hunk($head->{TEXT});\n \t$num = scalar @hunk;\n \t$ix = 0;\n \n@@ -754,10 +838,8 @@ sub patch_update_file {\n \t\tif (hunk_splittable($hunk[$ix]{TEXT})) {\n \t\t\t$other .= '/s';\n \t\t}\n-\t\tfor (@{$hunk[$ix]{TEXT}}) {\n-\t\t\tprint;\n-\t\t}\n-\t\tprint \"Stage this hunk [y/n/a/d$other/?]? \";\n+\t\tprint colored_diff_hunk($hunk[$ix]{TEXT});\n+\t\tprint colored $prompt_color, \"Stage this hunk [y/n/a/d$other/?]? \";\n \t\tmy $line = <STDIN>;\n \t\tif ($line) {\n \t\t\tif ($line =~ /^y/i) {\n@@ -811,7 +893,7 @@ sub patch_update_file {\n \t\t\telsif ($other =~ /s/ && $line =~ /^s/) {\n \t\t\t\tmy @split = split_hunk($hunk[$ix]{TEXT});\n \t\t\t\tif (1 < @split) {\n-\t\t\t\t\tprint \"Split into \",\n+\t\t\t\t\tprint colored $header_color, \"Split into \",\n \t\t\t\t\tscalar(@split), \" hunks.\\n\";\n \t\t\t\t}\n \t\t\t\tsplice(@hunk, $ix, 1,\n@@ -894,8 +976,7 @@ sub diff_cmd {\n \t\t\t\t     HEADER => $status_head, },\n \t\t\t\t   @mods);\n \treturn if (!@them);\n-\tsystem(qw(git diff-index -p --cached HEAD --),\n-\t       map { $_->{VALUE} } @them);\n+\tsystem(qw(git diff -p --cached HEAD --), map { $_->{VALUE} } @them);\n }\n \n sub quit_cmd {\n@@ -904,7 +985,7 @@ sub quit_cmd {\n }\n \n sub help_cmd {\n-\tprint <<\\EOF ;\n+\tprint colored $help_color, <<\\EOF ;\n status        - show paths with changes\n update        - add working tree state to the staged set of changes\n revert        - revert staged set of changes back to the HEAD version\ndiff --git a/perl/Git.pm b/perl/Git.pm\nindex 7468460..0f7156e 100644\n--- a/perl/Git.pm\n+++ b/perl/Git.pm\n@@ -581,6 +581,26 @@ sub config_int {\n \t};\n }\n \n+=item get_color ( SLOT, COLOR )\n+\n+Finds color for SLOT from the configuration, while defaulting to COLOR,\n+and returns the ANSI color escape sequence:\n+\n+\tprint $repo->get_color(\"color.interactive.prompt\", \"underline blue white\");\n+\tprint \"some text\";\n+\tprint $repo->get_color(\"\", \"normal\");\n+\n+=cut\n+\n+sub get_color {\n+\tmy ($self, $slot, $default) = @_;\n+\tmy $color = $self->command_oneline('config', '--get-color', $slot, $default);\n+\tif (!defined $color) {\n+\t\t$color = \"\";\n+\t}\n+\treturn $color;\n+}\n+\n =item ident ( TYPE | IDENTSTR )\n \n =item ident_person ( TYPE | IDENTSTR | IDENTARRAY )\n-- \n1.5.3.7-2134-g53f9\n"},{"id":"62011","messageId":"475697BC.2090701@viscovery.net","threadId":"11141","inReplyTo":"7vbq95tnk7.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] Color support for \"git-add -i\"","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2007-12-05T12:21:16Z","receivedAt":"2007-12-05T12:21:16Z","isPatch":true,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Junio C Hamano schrieb:\n> +color.interactive::\n> +\tWhen true (or `always`), always use colors in `git add\n> +\t--interactive`.  When false (or `never`), never.  When set to\n> +\t`auto`, use colors only when the output is to the\n> +\tterminal. Defaults to false.\n\nAny particular reason why color.interactive = true should be different from\ncolor.diff = true? See 57f2b842 (\"color.diff = true\" is not \"always\" anymore)\n\n-- Hannes\n"},{"id":"62015","messageId":"D72C7087-61D9-4AEC-AA7F-52A1268F1EA8@wincent.com","threadId":"11141","inReplyTo":"475697BC.2090701@viscovery.net","subject":"Re: [PATCH] Color support for \"git-add -i\"","fromName":"Wincent Colaiuta","fromEmail":"win@wincent.com","sentAt":"2007-12-05T13:52:56Z","receivedAt":"2007-12-05T13:52:56Z","isPatch":true,"sender":{"key":"greg@hurrell.net","avatar":"https://avatars.githubusercontent.com/u/7074?v=4"},"body":"El 5/12/2007, a las 13:21, Johannes Sixt escribió:\n\n> Junio C Hamano schrieb:\n>> +color.interactive::\n>> +\tWhen true (or `always`), always use colors in `git add\n>> +\t--interactive`.  When false (or `never`), never.  When set to\n>> +\t`auto`, use colors only when the output is to the\n>> +\tterminal. Defaults to false.\n>\n> Any particular reason why color.interactive = true should be  \n> different from\n> color.diff = true? See 57f2b842 (\"color.diff = true\" is not \"always\"  \n> anymore)\n\nI wonder when you'd ever run an interactive command and not be in a  \nterminal? ie. it doesn't make much sense to ever do \"git add -i >  \nfoo\", except perhaps from the test suite.\n\nNotwithstanding, consistency for the sake of consistency is probably a  \ngood thing.\n\nCheers,\nWincent\n"},{"id":"62016","messageId":"EDEE4359-2706-4F8F-B8B8-A29334E752E2@wincent.com","threadId":"11141","inReplyTo":"7vbq95tnk7.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] Color support for \"git-add -i\"","fromName":"Wincent Colaiuta","fromEmail":"win@wincent.com","sentAt":"2007-12-05T13:55:20Z","receivedAt":"2007-12-05T13:55:20Z","isPatch":true,"sender":{"key":"greg@hurrell.net","avatar":"https://avatars.githubusercontent.com/u/7074?v=4"},"body":"El 5/12/2007, a las 11:59, Junio C Hamano escribió:\n\n> This is mostly lifted from earlier series by Dan Zwell, but updated to\n> use \"git config --get-color\" to make it simpler and more consistent  \n> with\n> commands written in C.\n\nTested here and seems to work well. This is a nice little bit of  \nusability polish.\n\nCheers,\nWincent\n"},{"id":"62049","messageId":"7v1wa0q5zh.fsf@gitster.siamese.dyndns.org","threadId":"11141","inReplyTo":"475697BC.2090701@viscovery.net","subject":"Re: [PATCH] Color support for \"git-add -i\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-05T19:46:58Z","receivedAt":"2007-12-05T19:46:58Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Sixt <j.sixt@viscovery.net> writes:\n\n> Junio C Hamano schrieb:\n>> +color.interactive::\n>> +\tWhen true (or `always`), always use colors in `git add\n>> +\t--interactive`.  When false (or `never`), never.  When set to\n>> +\t`auto`, use colors only when the output is to the\n>> +\tterminal. Defaults to false.\n>\n> Any particular reason why color.interactive = true should be different from\n> color.diff = true? See 57f2b842 (\"color.diff = true\" is not \"always\" anymore)\n\nLeftover from the original series.  Thanks for noticing.\n\nWe will probably want that \"git config --colorbool\" thing we discussed\nearlier.\n"},{"id":"62080","messageId":"1196906706-11170-1-git-send-email-gitster@pobox.com","threadId":"11141","inReplyTo":"475697BC.2090701@viscovery.net","subject":"[PATCH 1/3] Documentation: color.* = true means \"auto\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-06T02:05:03Z","receivedAt":"2007-12-06T02:05:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"We forgot to document the earlier sanity-fix.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n Documentation/config.txt |   14 +++++++-------\n 1 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 72a33e9..0e45ec5 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -359,8 +359,8 @@ clean.requireForce::\n \n color.branch::\n \tA boolean to enable/disable color in the output of\n-\tgitlink:git-branch[1]. May be set to `true` (or `always`),\n-\t`false` (or `never`) or `auto`, in which case colors are used\n+\tgitlink:git-branch[1]. May be set to `always`,\n+\t`false` (or `never`) or `auto` (or `true`), in which case colors are used\n \tonly when the output is to a terminal. Defaults to false.\n \n color.branch.<slot>::\n@@ -378,9 +378,9 @@ second is the background.  The position of the attribute, if any,\n doesn't matter.\n \n color.diff::\n-\tWhen true (or `always`), always use colors in patch.\n-\tWhen false (or `never`), never.  When set to `auto`, use\n-\tcolors only when the output is to the terminal.\n+\tWhen set to `always`, always use colors in patch.\n+\tWhen false (or `never`), never.  When set to `true` or `auto`, use\n+\tcolors only when the output is to the terminal. Defaults to false.\n \n color.diff.<slot>::\n \tUse customized color for diff colorization.  `<slot>` specifies\n@@ -397,8 +397,8 @@ color.pager::\n \n color.status::\n \tA boolean to enable/disable color in the output of\n-\tgitlink:git-status[1]. May be set to `true` (or `always`),\n-\t`false` (or `never`) or `auto`, in which case colors are used\n+\tgitlink:git-status[1]. May be set to `always`,\n+\t`false` (or `never`) or `auto` (or `true`), in which case colors are used\n \tonly when the output is to a terminal. Defaults to false.\n \n color.status.<slot>::\n-- \n1.5.3.7-2132-gbd1cf\n"},{"id":"62082","messageId":"1196906706-11170-2-git-send-email-gitster@pobox.com","threadId":"11141","inReplyTo":"1196906706-11170-1-git-send-email-gitster@pobox.com","subject":"[PATCH 2/3] git config --get-colorbool","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-06T02:05:04Z","receivedAt":"2007-12-06T02:05:04Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This adds an option to help scripts find out color settings from\nthe configuration file.\n\n    git config --get-colorbool color.diff\n\ninspects color.diff variable, and exits with status 0 (i.e. success) if\ncolor is to be used.  It exits with status 1 otherwise.\n\nIf a script wants \"true\"/\"false\" answer to the standard output of the\ncommand, it can pass an additional boolean parameter to its command\nline, telling if its standard output is a terminal, like this:\n\n    git config --get-colorbool color.diff true\n\nWhen called like this, the command outputs \"true\" to its standard output\nif color is to be used (i.e. \"color.diff\" says \"always\", \"auto\", or\n\"true\"), and \"false\" otherwise.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n Documentation/git-config.txt |   10 ++++++++++\n builtin-branch.c             |    2 +-\n builtin-config.c             |   41 ++++++++++++++++++++++++++++++++++++++++-\n color.c                      |    6 ++++--\n color.h                      |    2 +-\n diff.c                       |    2 +-\n wt-status.c                  |    2 +-\n 7 files changed, 58 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-config.txt b/Documentation/git-config.txt\nindex 7640450..98509b4 100644\n--- a/Documentation/git-config.txt\n+++ b/Documentation/git-config.txt\n@@ -21,6 +21,7 @@ SYNOPSIS\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+'git-config' [<file-option>] --get-colorbool name [stdout-is-tty]\n \n DESCRIPTION\n -----------\n@@ -135,6 +136,15 @@ See also <<FILES>>.\n \toutput without getting confused e.g. by values that\n \tcontain line breaks.\n \n+--get-colorbool name [stdout-is-tty]::\n+\n+\tFind the color setting for `name` (e.g. `color.diff`) and output\n+\t\"true\" or \"false\".  `stdout-is-tty` should be either \"true\" or\n+\t\"false\", and is taken into account when configuration says\n+\t\"auto\".  If `stdout-is-tty` is missing, then checks the standard\n+\toutput of the command itself, and exits with status 0 if color\n+\tis to be used, or exits with status 1 otherwise.\n+\n --get-color name default::\n \n \tFind the color configured for `name` (e.g. `color.diff.new`) and\ndiff --git a/builtin-branch.c b/builtin-branch.c\nindex c64768b..089cae5 100644\n--- a/builtin-branch.c\n+++ b/builtin-branch.c\n@@ -65,7 +65,7 @@ static int parse_branch_color_slot(const char *var, int ofs)\n static int git_branch_config(const char *var, const char *value)\n {\n \tif (!strcmp(var, \"color.branch\")) {\n-\t\tbranch_use_color = git_config_colorbool(var, value);\n+\t\tbranch_use_color = git_config_colorbool(var, value, -1);\n \t\treturn 0;\n \t}\n \tif (!prefixcmp(var, \"color.branch.\")) {\ndiff --git a/builtin-config.c b/builtin-config.c\nindex 6175dc3..d10b03f 100644\n--- a/builtin-config.c\n+++ b/builtin-config.c\n@@ -3,7 +3,7 @@\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 | --get-color var [default]\";\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] | --get-colorbool name [stdout-is-tty]\";\n \n static char *key;\n static regex_t *key_regexp;\n@@ -208,6 +208,43 @@ static int get_color(int argc, const char **argv)\n \treturn 0;\n }\n \n+static int stdout_is_tty;\n+static int get_colorbool_found;\n+static int git_get_colorbool_config(const char *var, const char *value)\n+{\n+\tif (!strcmp(var, get_color_slot))\n+\t\tget_colorbool_found =\n+\t\t\tgit_config_colorbool(var, value, stdout_is_tty);\n+\treturn 0;\n+}\n+\n+static int get_colorbool(int argc, const char **argv)\n+{\n+\t/*\n+\t * git config --get-colorbool <slot> [<stdout-is-tty>]\n+\t *\n+\t * returns \"true\" or \"false\" depending on how <slot>\n+\t * is configured.\n+\t */\n+\n+\tif (argc == 2)\n+\t\tstdout_is_tty = git_config_bool(\"command line\", argv[1]);\n+\telse if (argc == 1)\n+\t\tstdout_is_tty = isatty(1);\n+\telse\n+\t\tusage(git_config_set_usage);\n+\tget_colorbool_found = 0;\n+\tget_color_slot = argv[0];\n+\tgit_config(git_get_colorbool_config);\n+\n+\tif (argc == 1) {\n+\t\treturn get_colorbool_found ? 0 : 1;\n+\t} else {\n+\t\tprintf(\"%s\\n\", get_colorbool_found ? \"true\" : \"false\");\n+\t\treturn 0;\n+\t}\n+}\n+\n int cmd_config(int argc, const char **argv, const char *prefix)\n {\n \tint nongit = 0;\n@@ -283,6 +320,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)\n \t\t\treturn 0;\n \t\t} else if (!strcmp(argv[1], \"--get-color\")) {\n \t\t\treturn get_color(argc-2, argv+2);\n+\t\t} else if (!strcmp(argv[1], \"--get-colorbool\")) {\n+\t\t\treturn get_colorbool(argc-2, argv+2);\n \t\t} else\n \t\t\tbreak;\n \t\targc--;\ndiff --git a/color.c b/color.c\nindex 97cfbda..7bd424a 100644\n--- a/color.c\n+++ b/color.c\n@@ -116,7 +116,7 @@ bad:\n \tdie(\"bad config value '%s' for variable '%s'\", value, var);\n }\n \n-int git_config_colorbool(const char *var, const char *value)\n+int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)\n {\n \tif (value) {\n \t\tif (!strcasecmp(value, \"never\"))\n@@ -133,7 +133,9 @@ int git_config_colorbool(const char *var, const char *value)\n \n \t/* any normal truth value defaults to 'auto' */\n  auto_color:\n-\tif (isatty(1) || (pager_in_use && pager_use_color)) {\n+\tif (stdout_is_tty < 0)\n+\t\tstdout_is_tty = isatty(1);\n+\tif (stdout_is_tty || (pager_in_use && pager_use_color)) {\n \t\tchar *term = getenv(\"TERM\");\n \t\tif (term && strcmp(term, \"dumb\"))\n \t\t\treturn 1;\ndiff --git a/color.h b/color.h\nindex 6809800..ff63513 100644\n--- a/color.h\n+++ b/color.h\n@@ -4,7 +4,7 @@\n /* \"\\033[1;38;5;2xx;48;5;2xxm\\0\" is 23 bytes */\n #define COLOR_MAXLEN 24\n \n-int git_config_colorbool(const char *var, const char *value);\n+int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);\n void color_parse(const char *var, const char *value, char *dst);\n int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);\n int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...);\ndiff --git a/diff.c b/diff.c\nindex 6b54959..be6cf68 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -146,7 +146,7 @@ int git_diff_ui_config(const char *var, const char *value)\n \t\treturn 0;\n \t}\n \tif (!strcmp(var, \"diff.color\") || !strcmp(var, \"color.diff\")) {\n-\t\tdiff_use_color_default = git_config_colorbool(var, value);\n+\t\tdiff_use_color_default = git_config_colorbool(var, value, -1);\n \t\treturn 0;\n \t}\n \tif (!strcmp(var, \"diff.renames\")) {\ndiff --git a/wt-status.c b/wt-status.c\nindex d35386d..02dbb75 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -391,7 +391,7 @@ void wt_status_print(struct wt_status *s)\n int git_status_config(const char *k, const char *v)\n {\n \tif (!strcmp(k, \"status.color\") || !strcmp(k, \"color.status\")) {\n-\t\twt_status_use_color = git_config_colorbool(k, v);\n+\t\twt_status_use_color = git_config_colorbool(k, v, -1);\n \t\treturn 0;\n \t}\n \tif (!prefixcmp(k, \"status.color.\") || !prefixcmp(k, \"color.status.\")) {\n-- \n1.5.3.7-2132-gbd1cf\n"},{"id":"62083","messageId":"1196906706-11170-3-git-send-email-gitster@pobox.com","threadId":"11141","inReplyTo":"1196906706-11170-2-git-send-email-gitster@pobox.com","subject":"[PATCH 3/3] Color support for \"git-add -i\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-06T02:05:05Z","receivedAt":"2007-12-06T02:05:05Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This is mostly lifted from earlier series by Dan Zwell, but updated to\nuse \"git config --get-color\" and \"git config --get-colorbool\" to make it\nsimpler and more consistent with commands written in C.\n\nA new configuration color.interactive variable is like color.diff and\ncolor.status, and controls if \"git-add -i\" uses color.\n\nA set of configuration variables, color.interactive.<slot>, are used to\ndefine what color is used for the prompt, header, and help text.\n\nFor perl scripts, Git.pm provides $repo->get_color() method, which takes\nthe slot name and the default color, and returns the terminal escape\nsequence to color the output text.  $repo->get_colorbool() method can be\nused to check if color is set to be used for a given operation.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n Documentation/config.txt  |   12 +++++\n git-add--interactive.perl |  119 +++++++++++++++++++++++++++++++++++++--------\n perl/Git.pm               |   35 +++++++++++++\n 3 files changed, 146 insertions(+), 20 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 0e45ec5..736fcd7 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -391,6 +391,18 @@ color.diff.<slot>::\n \twhitespace).  The values of these variables may be specified as\n \tin color.branch.<slot>.\n \n+color.interactive::\n+\tWhen set to `always`, always use colors in `git add --interactive`.\n+\tWhen false (or `never`), never.  When set to `true` or `auto`, use\n+\tcolors only when the output is to the terminal. Defaults to false.\n+\n+color.interactive.<slot>::\n+\tUse customized color for `git add --interactive`\n+\toutput. `<slot>` may be `prompt`, `header`, or `help`, for\n+\tthree distinct types of normal output from interactive\n+\tprograms.  The values of these variables may be specified as\n+\tin color.branch.<slot>.\n+\n color.pager::\n \tA boolean to enable/disable colored output when the pager is in\n \tuse (default is true).\ndiff --git a/git-add--interactive.perl b/git-add--interactive.perl\nindex 335c2c6..1019a72 100755\n--- a/git-add--interactive.perl\n+++ b/git-add--interactive.perl\n@@ -1,6 +1,55 @@\n #!/usr/bin/perl -w\n \n use strict;\n+use Git;\n+\n+# Prompt colors:\n+my ($prompt_color, $header_color, $help_color, $normal_color);\n+# Diff colors:\n+my ($new_color, $old_color, $fraginfo_color, $metainfo_color, $whitespace_color);\n+\n+my ($use_color, $diff_use_color);\n+my $repo = Git->repository();\n+\n+$use_color = $repo->get_colorbool('color.interactive');\n+\n+if ($use_color) {\n+\t# Set interactive colors:\n+\n+\t# Grab the 3 main colors in git color string format, with sane\n+\t# (visible) defaults:\n+\t$prompt_color = $repo->get_color(\"color.interactive.prompt\", \"bold blue\");\n+\t$header_color = $repo->get_color(\"color.interactive.header\", \"bold\");\n+\t$help_color = $repo->get_color(\"color.interactive.help\", \"red bold\");\n+\t$normal_color = $repo->get_color(\"\", \"reset\");\n+\n+\t# Do we also set diff colors?\n+\t$diff_use_color = $repo->get_colorbool('color.diff');\n+\tif ($diff_use_color) {\n+\t\t$new_color = $repo->get_color(\"color.diff.new\", \"green\");\n+\t\t$old_color = $repo->get_color(\"color.diff.old\", \"red\");\n+\t\t$fraginfo_color = $repo->get_color(\"color.diff.frag\", \"cyan\");\n+\t\t$metainfo_color = $repo->get_color(\"color.diff.meta\", \"bold\");\n+\t\t$whitespace_color = $repo->get_color(\"color.diff.whitespace\", \"normal red\");\n+\t}\n+}\n+\n+sub colored {\n+\tmy $color = shift;\n+\tmy $string = join(\"\", @_);\n+\n+\tif ($use_color) {\n+\t\t# Put a color code at the beginning of each line, a reset at the end\n+\t\t# color after newlines that are not at the end of the string\n+\t\t$string =~ s/(\\n+)(.)/$1$color$2/g;\n+\t\t# reset before newlines\n+\t\t$string =~ s/(\\n+)/$normal_color$1/g;\n+\t\t# codes at beginning and end (if necessary):\n+\t\t$string =~ s/^/$color/;\n+\t\t$string =~ s/$/$normal_color/ unless $string =~ /\\n$/;\n+\t}\n+\treturn $string;\n+}\n \n # command line options\n my $patch_mode;\n@@ -246,10 +295,20 @@ sub is_valid_prefix {\n sub highlight_prefix {\n \tmy $prefix = shift;\n \tmy $remainder = shift;\n-\treturn $remainder unless defined $prefix;\n-\treturn is_valid_prefix($prefix) ?\n-\t    \"[$prefix]$remainder\" :\n-\t    \"$prefix$remainder\";\n+\n+\tif (!defined $prefix) {\n+\t\treturn $remainder;\n+\t}\n+\n+\tif (!is_valid_prefix($prefix)) {\n+\t\treturn \"$prefix$remainder\";\n+\t}\n+\n+\tif (!$use_color) {\n+\t\treturn \"[$prefix]$remainder\";\n+\t}\n+\n+\treturn \"$prompt_color$prefix$normal_color$remainder\";\n }\n \n sub list_and_choose {\n@@ -266,7 +325,7 @@ sub list_and_choose {\n \t\t\tif (!$opts->{LIST_FLAT}) {\n \t\t\t\tprint \"     \";\n \t\t\t}\n-\t\t\tprint \"$opts->{HEADER}\\n\";\n+\t\t\tprint colored $header_color, \"$opts->{HEADER}\\n\";\n \t\t}\n \t\tfor ($i = 0; $i < @stuff; $i++) {\n \t\t\tmy $chosen = $chosen[$i] ? '*' : ' ';\n@@ -304,7 +363,7 @@ sub list_and_choose {\n \n \t\treturn if ($opts->{LIST_ONLY});\n \n-\t\tprint $opts->{PROMPT};\n+\t\tprint colored $prompt_color, $opts->{PROMPT};\n \t\tif ($opts->{SINGLETON}) {\n \t\t\tprint \"> \";\n \t\t}\n@@ -371,7 +430,7 @@ sub list_and_choose {\n }\n \n sub singleton_prompt_help_cmd {\n-\tprint <<\\EOF ;\n+\tprint colored $help_color, <<\\EOF ;\n Prompt help:\n 1          - select a numbered item\n foo        - select item based on unique prefix\n@@ -380,7 +439,7 @@ EOF\n }\n \n sub prompt_help_cmd {\n-\tprint <<\\EOF ;\n+\tprint colored $help_color, <<\\EOF ;\n Prompt help:\n 1          - select a single item\n 3-5        - select a range of items\n@@ -477,6 +536,31 @@ sub parse_diff {\n \treturn @hunk;\n }\n \n+sub colored_diff_hunk {\n+\tmy ($text) = @_;\n+\t# return the text, so that it can be passed to print()\n+\tmy @ret;\n+\tfor (@$text) {\n+\t\tif (!$diff_use_color) {\n+\t\t\tpush @ret, $_;\n+\t\t\tnext;\n+\t\t}\n+\n+\t\tif (/^\\+/) {\n+\t\t\tpush @ret, colored($new_color, $_);\n+\t\t} elsif (/^\\-/) {\n+\t\t\tpush @ret, colored($old_color, $_);\n+\t\t} elsif (/^\\@/) {\n+\t\t\tpush @ret, colored($fraginfo_color, $_);\n+\t\t} elsif (/^ /) {\n+\t\t\tpush @ret, colored($normal_color, $_);\n+\t\t} else {\n+\t\t\tpush @ret, colored($metainfo_color, $_);\n+\t\t}\n+\t}\n+\treturn @ret;\n+}\n+\n sub hunk_splittable {\n \tmy ($text) = @_;\n \n@@ -671,7 +755,7 @@ sub coalesce_overlapping_hunks {\n }\n \n sub help_patch_cmd {\n-\tprint <<\\EOF ;\n+\tprint colored $help_color, <<\\EOF ;\n y - stage this hunk\n n - do not stage this hunk\n a - stage this and all the remaining hunks in the file\n@@ -710,9 +794,7 @@ sub patch_update_file {\n \tmy ($ix, $num);\n \tmy $path = shift;\n \tmy ($head, @hunk) = parse_diff($path);\n-\tfor (@{$head->{TEXT}}) {\n-\t\tprint;\n-\t}\n+\tprint colored_diff_hunk($head->{TEXT});\n \t$num = scalar @hunk;\n \t$ix = 0;\n \n@@ -754,10 +836,8 @@ sub patch_update_file {\n \t\tif (hunk_splittable($hunk[$ix]{TEXT})) {\n \t\t\t$other .= '/s';\n \t\t}\n-\t\tfor (@{$hunk[$ix]{TEXT}}) {\n-\t\t\tprint;\n-\t\t}\n-\t\tprint \"Stage this hunk [y/n/a/d$other/?]? \";\n+\t\tprint colored_diff_hunk($hunk[$ix]{TEXT});\n+\t\tprint colored $prompt_color, \"Stage this hunk [y/n/a/d$other/?]? \";\n \t\tmy $line = <STDIN>;\n \t\tif ($line) {\n \t\t\tif ($line =~ /^y/i) {\n@@ -811,7 +891,7 @@ sub patch_update_file {\n \t\t\telsif ($other =~ /s/ && $line =~ /^s/) {\n \t\t\t\tmy @split = split_hunk($hunk[$ix]{TEXT});\n \t\t\t\tif (1 < @split) {\n-\t\t\t\t\tprint \"Split into \",\n+\t\t\t\t\tprint colored $header_color, \"Split into \",\n \t\t\t\t\tscalar(@split), \" hunks.\\n\";\n \t\t\t\t}\n \t\t\t\tsplice(@hunk, $ix, 1,\n@@ -894,8 +974,7 @@ sub diff_cmd {\n \t\t\t\t     HEADER => $status_head, },\n \t\t\t\t   @mods);\n \treturn if (!@them);\n-\tsystem(qw(git diff-index -p --cached HEAD --),\n-\t       map { $_->{VALUE} } @them);\n+\tsystem(qw(git diff -p --cached HEAD --), map { $_->{VALUE} } @them);\n }\n \n sub quit_cmd {\n@@ -904,7 +983,7 @@ sub quit_cmd {\n }\n \n sub help_cmd {\n-\tprint <<\\EOF ;\n+\tprint colored $help_color, <<\\EOF ;\n status        - show paths with changes\n update        - add working tree state to the staged set of changes\n revert        - revert staged set of changes back to the HEAD version\ndiff --git a/perl/Git.pm b/perl/Git.pm\nindex 7468460..a2812ea 100644\n--- a/perl/Git.pm\n+++ b/perl/Git.pm\n@@ -581,6 +581,41 @@ sub config_int {\n \t};\n }\n \n+=item get_colorbool ( NAME )\n+\n+Finds if color should be used for NAMEd operation from the configuration,\n+and returns boolean (true for \"use color\", false for \"do not use color\").\n+\n+=cut\n+\n+sub get_colorbool {\n+\tmy ($self, $var) = @_;\n+\tmy $stdout_to_tty = (-t STDOUT) ? \"true\" : \"false\";\n+\tmy $use_color = $self->command_oneline('config', '--get-colorbool',\n+\t\t\t\t\t       $var, $stdout_to_tty);\n+\treturn ($use_color eq 'true');\n+}\n+\n+=item get_color ( SLOT, COLOR )\n+\n+Finds color for SLOT from the configuration, while defaulting to COLOR,\n+and returns the ANSI color escape sequence:\n+\n+\tprint $repo->get_color(\"color.interactive.prompt\", \"underline blue white\");\n+\tprint \"some text\";\n+\tprint $repo->get_color(\"\", \"normal\");\n+\n+=cut\n+\n+sub get_color {\n+\tmy ($self, $slot, $default) = @_;\n+\tmy $color = $self->command_oneline('config', '--get-color', $slot, $default);\n+\tif (!defined $color) {\n+\t\t$color = \"\";\n+\t}\n+\treturn $color;\n+}\n+\n =item ident ( TYPE | IDENTSTR )\n \n =item ident_person ( TYPE | IDENTSTR | IDENTARRAY )\n-- \n1.5.3.7-2132-gbd1cf\n"},{"id":"62079","messageId":"1196906706-11170-4-git-send-email-gitster@pobox.com","threadId":"11141","inReplyTo":"1196906706-11170-3-git-send-email-gitster@pobox.com","subject":"[PATCH 0/3] Reroll colorized \"git add -i\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-06T02:05:06Z","receivedAt":"2007-12-06T02:05:06Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Another try of the colorized \"git add -i\" series.\n\n[PATCH 1/3] Documentation: color.* = true means \"auto\"\n\nThis is a documentation fix that has already been applied to 'master'\n(not pushed out yet as of this writing).\n\n[PATCH 2/3] git config --get-colorbool\n\nThis allows scripts to figure out if they should use colors.\n\n[PATCH 3/3] Color support for \"git-add -i\"\n\nThis is Dan's colorized git-add -i, but uses --get-color and\n--get-colorbool options to git-config.\n"},{"id":"62104","messageId":"20071206053059.GF5499@coredump.intra.peff.net","threadId":"11141","inReplyTo":"1196906706-11170-2-git-send-email-gitster@pobox.com","subject":"Re: [PATCH 2/3] git config --get-colorbool","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2007-12-06T05:30:59Z","receivedAt":"2007-12-06T05:30:59Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"[Eric Wong cc'd because of git-svn relevance]\n\nOn Wed, Dec 05, 2007 at 06:05:04PM -0800, Junio C Hamano wrote:\n\n> This adds an option to help scripts find out color settings from\n> the configuration file.\n> \n>     git config --get-colorbool color.diff\n> \n> inspects color.diff variable, and exits with status 0 (i.e. success) if\n> color is to be used.  It exits with status 1 otherwise.\n\nThere is no way to differentiate between \"do not use color\" and \"no\nvalue found\". This makes it impossible to use this to implement the\nrequired \"try color.diff, fallback to diff.color\" behavior.\n\nWe could simply make it\n\n  git config --get-colorbool diff\n\nwhich would check both (and when diff.color support is finally dropped,\njust remove it from there).\n\ngit-svn should probably be moved to this interface (it still has the\ncolor.diff == true means \"always\" behavior), but it can't be until the\nfallback behavior is implemented.\n\nAlso, your patch doesn't seem to implement the color.pager/pager.color\nbehavior, either (which is probably not important for git-add -i, but is\nused by git-svn).\n\nAnyway, below is a totally untested (I don't even have svn installed,\nbut it passes perl -wc!) patch for git-svn to use the new \"true means\nauto\" behavior for color.diff. It would be nice to replace this with\na working --get-colorbool, but we should at least unify the behavior\nbefore v1.5.4.\n\n-Peff\n\n---\ndiff --git a/git-svn.perl b/git-svn.perl\nindex 9f884eb..71f6e93 100755\n--- a/git-svn.perl\n+++ b/git-svn.perl\n@@ -3979,7 +3979,12 @@ sub log_use_color {\n \t\t$dc = `git-config --get $dcvar`;\n \t}\n \tchomp($dc);\n-\tif ($dc eq 'auto') {\n+\treturn 0 if $dc eq 'never';\n+\treturn 1 if $dc eq 'always';\n+\tif ($dc ne 'auto') {\n+\t\tchomp($dc = `git-config --bool --get $dcvar`);\n+\t}\n+\tif ($dc eq 'auto' || $dc eq 'true') {\n \t\tmy $pc;\n \t\t$pc = `git-config --get color.pager`;\n \t\tif ($pc eq '') {\n@@ -3998,10 +4003,7 @@ sub log_use_color {\n \t\t}\n \t\treturn 0;\n \t}\n-\treturn 0 if $dc eq 'never';\n-\treturn 1 if $dc eq 'always';\n-\tchomp($dc = `git-config --bool --get $dcvar`);\n-\treturn ($dc eq 'true');\n+\treturn 0;\n }\n \n sub git_svn_log_cmd {\n"},{"id":"62105","messageId":"20071206053514.GA23201@coredump.intra.peff.net","threadId":"11141","inReplyTo":"20071206053059.GF5499@coredump.intra.peff.net","subject":"Re: [PATCH 2/3] git config --get-colorbool","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2007-12-06T05:35:14Z","receivedAt":"2007-12-06T05:35:14Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Dec 06, 2007 at 12:30:59AM -0500, Jeff King wrote:\n\n> Also, your patch doesn't seem to implement the color.pager/pager.color\n> behavior, either (which is probably not important for git-add -i, but is\n> used by git-svn).\n\nOops, maybe I should actually read your patch more carefully before\ncriticizing. I see that you do handle pager_use_color in\ngit_config_colorbool, but I think that for --get-colorbool usage,\npager_in_use is going to be useless (wow, three forms of \"use\" in one\nclause).\n\n-Peff\n"},{"id":"62106","messageId":"20071206054056.GG5499@coredump.intra.peff.net","threadId":"11141","inReplyTo":"1196906706-11170-3-git-send-email-gitster@pobox.com","subject":"Re: [PATCH 3/3] Color support for \"git-add -i\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2007-12-06T05:40:56Z","receivedAt":"2007-12-06T05:40:56Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Dec 05, 2007 at 06:05:05PM -0800, Junio C Hamano wrote:\n\n> This is mostly lifted from earlier series by Dan Zwell, but updated to\n> use \"git config --get-color\" and \"git config --get-colorbool\" to make it\n> simpler and more consistent with commands written in C.\n\nLooks mostly sane, except for the colorbool issues I mentioned in\nresponse to 2/3.\n\n> diff --git a/git-add--interactive.perl b/git-add--interactive.perl\n> index 335c2c6..1019a72 100755\n> --- a/git-add--interactive.perl\n> +++ b/git-add--interactive.perl\n> [...]\n> +\t# Do we also set diff colors?\n> +\t$diff_use_color = $repo->get_colorbool('color.diff');\n> +\tif ($diff_use_color) {\n> +\t\t$new_color = $repo->get_color(\"color.diff.new\", \"green\");\n> +\t\t$old_color = $repo->get_color(\"color.diff.old\", \"red\");\n> +\t\t$fraginfo_color = $repo->get_color(\"color.diff.frag\", \"cyan\");\n> +\t\t$metainfo_color = $repo->get_color(\"color.diff.meta\", \"bold\");\n> +\t\t$whitespace_color = $repo->get_color(\"color.diff.whitespace\", \"normal red\");\n\ndiff.color support?\n\nBTW, I am nagging about this because we never resolved what should\nhappen. I am fine with the answer being \"we are dropping support now.\"\n\n> +sub colored {\n> +\tmy $color = shift;\n> +\tmy $string = join(\"\", @_);\n> +\n> +\tif ($use_color) {\n> +\t\t# Put a color code at the beginning of each line, a reset at the end\n> +\t\t# color after newlines that are not at the end of the string\n> +\t\t$string =~ s/(\\n+)(.)/$1$color$2/g;\n> +\t\t# reset before newlines\n> +\t\t$string =~ s/(\\n+)/$normal_color$1/g;\n> +\t\t# codes at beginning and end (if necessary):\n> +\t\t$string =~ s/^/$color/;\n> +\t\t$string =~ s/$/$normal_color/ unless $string =~ /\\n$/;\n> +\t}\n> +\treturn $string;\n> +}\n\nI still think this should go into Git.pm; I believe git-svn could make\nuse of it.\n\n>  sub highlight_prefix {\n>  \tmy $prefix = shift;\n>  \tmy $remainder = shift;\n> -\treturn $remainder unless defined $prefix;\n> -\treturn is_valid_prefix($prefix) ?\n> -\t    \"[$prefix]$remainder\" :\n> -\t    \"$prefix$remainder\";\n> +\n> +\tif (!defined $prefix) {\n> +\t\treturn $remainder;\n> +\t}\n> +\n> +\tif (!is_valid_prefix($prefix)) {\n> +\t\treturn \"$prefix$remainder\";\n> +\t}\n> +\n> +\tif (!$use_color) {\n> +\t\treturn \"[$prefix]$remainder\";\n> +\t}\n> +\n> +\treturn \"$prompt_color$prefix$normal_color$remainder\";\n\nHonestly, I find this blue+white coloring of the prefixes a bit ugly,\nbut that is not your fault. :)\n\n-Peff\n"},{"id":"62111","messageId":"7vve7cgxmw.fsf@gitster.siamese.dyndns.org","threadId":"11141","inReplyTo":"20071206053059.GF5499@coredump.intra.peff.net","subject":"Re: [PATCH 2/3] git config --get-colorbool","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-06T06:12:07Z","receivedAt":"2007-12-06T06:12:07Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> [Eric Wong cc'd because of git-svn relevance]\n>\n> On Wed, Dec 05, 2007 at 06:05:04PM -0800, Junio C Hamano wrote:\n>\n>> This adds an option to help scripts find out color settings from\n>> the configuration file.\n>> \n>>     git config --get-colorbool color.diff\n>> \n>> inspects color.diff variable, and exits with status 0 (i.e. success) if\n>> color is to be used.  It exits with status 1 otherwise.\n>\n> There is no way to differentiate between \"do not use color\" and \"no\n> value found\". This makes it impossible to use this to implement the\n> required \"try color.diff, fallback to diff.color\" behavior.\n>\n> We could simply make it\n>\n>   git config --get-colorbool diff\n>\n> which would check both (and when diff.color support is finally dropped,\n> just remove it from there).\n\nI thought about this a bit and thought that it would probably be a good\nworkaround to teach \"--get-colorbool that diff.color is a deprecated\nsynonym to color.diff, like this.\n\n-- >8 --\nconfig --get-colorbool: diff.color is a deprecated synonym to color.diff\n\nThe applications can ask for color.diff but the configuration of old\ntimer users can still instruct it to use color with diff.color this\nway.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n builtin-config.c |   18 ++++++++++++++++--\n 1 files changed, 16 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin-config.c b/builtin-config.c\nindex d10b03f..e4a12e3 100644\n--- a/builtin-config.c\n+++ b/builtin-config.c\n@@ -210,11 +210,17 @@ static int get_color(int argc, const char **argv)\n \n static int stdout_is_tty;\n static int get_colorbool_found;\n+static int get_diff_color_found;\n static int git_get_colorbool_config(const char *var, const char *value)\n {\n-\tif (!strcmp(var, get_color_slot))\n+\tif (!strcmp(var, get_color_slot)) {\n \t\tget_colorbool_found =\n \t\t\tgit_config_colorbool(var, value, stdout_is_tty);\n+\t}\n+\tif (!strcmp(var, \"diff.color\")) {\n+\t\tget_diff_color_found =\n+\t\t\tgit_config_colorbool(var, value, stdout_is_tty);\n+\t}\n \treturn 0;\n }\n \n@@ -233,10 +239,18 @@ static int get_colorbool(int argc, const char **argv)\n \t\tstdout_is_tty = isatty(1);\n \telse\n \t\tusage(git_config_set_usage);\n-\tget_colorbool_found = 0;\n+\tget_colorbool_found = -1;\n+\tget_diff_color_found = -1;\n \tget_color_slot = argv[0];\n \tgit_config(git_get_colorbool_config);\n \n+\tif (get_colorbool_found < 0) {\n+\t\tif (!strcmp(get_color_slot, \"color.diff\"))\n+\t\t\tget_colorbool_found = get_diff_color_found;\n+\t\tif (get_colorbool_found < 0)\n+\t\t\tget_colorbool_found = 0;\n+\t}\n+\n \tif (argc == 1) {\n \t\treturn get_colorbool_found ? 0 : 1;\n \t} else {\n"},{"id":"62113","messageId":"20071206061711.GD23309@coredump.intra.peff.net","threadId":"11141","inReplyTo":"7vve7cgxmw.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH 2/3] git config --get-colorbool","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2007-12-06T06:17:11Z","receivedAt":"2007-12-06T06:17:11Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Dec 05, 2007 at 10:12:07PM -0800, Junio C Hamano wrote:\n\n> I thought about this a bit and thought that it would probably be a good\n> workaround to teach \"--get-colorbool that diff.color is a deprecated\n> synonym to color.diff, like this.\n\nReasonable. Technically, one is required for status.color/color.status,\nas well, though there are no users in git-core.\n\n-Peff\n"},{"id":"62126","messageId":"20071206073858.GA23315@soma","threadId":"11141","inReplyTo":"20071206053059.GF5499@coredump.intra.peff.net","subject":"Re: [PATCH 2/3] git config --get-colorbool","fromName":"Eric Wong","fromEmail":"normalperson@yhbt.net","sentAt":"2007-12-06T07:38:59Z","receivedAt":"2007-12-06T07:38:59Z","isPatch":true,"sender":{"key":"e@80x24.org","avatar":null},"body":"Jeff King <peff@peff.net> wrote:\n> [Eric Wong cc'd because of git-svn relevance]\n> \n> On Wed, Dec 05, 2007 at 06:05:04PM -0800, Junio C Hamano wrote:\n> \n> > This adds an option to help scripts find out color settings from\n> > the configuration file.\n> > \n> >     git config --get-colorbool color.diff\n> > \n> > inspects color.diff variable, and exits with status 0 (i.e. success) if\n> > color is to be used.  It exits with status 1 otherwise.\n> \n> There is no way to differentiate between \"do not use color\" and \"no\n> value found\". This makes it impossible to use this to implement the\n> required \"try color.diff, fallback to diff.color\" behavior.\n> \n> We could simply make it\n> \n>   git config --get-colorbool diff\n> \n> which would check both (and when diff.color support is finally dropped,\n> just remove it from there).\n> \n> git-svn should probably be moved to this interface (it still has the\n> color.diff == true means \"always\" behavior), but it can't be until the\n> fallback behavior is implemented.\n> \n> Also, your patch doesn't seem to implement the color.pager/pager.color\n> behavior, either (which is probably not important for git-add -i, but is\n> used by git-svn).\n> \n> Anyway, below is a totally untested (I don't even have svn installed,\n> but it passes perl -wc!) patch for git-svn to use the new \"true means\n> auto\" behavior for color.diff. It would be nice to replace this with\n> a working --get-colorbool, but we should at least unify the behavior\n> before v1.5.4.\n\nHi Jeff,\n\nI completely agree that the color behavior should be consistent across\ngit-*.  So I'd like to just be able to just use --get-colorbool in\ngit-svn.\n\nYour patch seems to do what it says it does, though.\n\n> -Peff\n> \n> ---\n> diff --git a/git-svn.perl b/git-svn.perl\n> index 9f884eb..71f6e93 100755\n> --- a/git-svn.perl\n> +++ b/git-svn.perl\n> @@ -3979,7 +3979,12 @@ sub log_use_color {\n>  \t\t$dc = `git-config --get $dcvar`;\n>  \t}\n>  \tchomp($dc);\n> -\tif ($dc eq 'auto') {\n> +\treturn 0 if $dc eq 'never';\n> +\treturn 1 if $dc eq 'always';\n> +\tif ($dc ne 'auto') {\n> +\t\tchomp($dc = `git-config --bool --get $dcvar`);\n> +\t}\n> +\tif ($dc eq 'auto' || $dc eq 'true') {\n>  \t\tmy $pc;\n>  \t\t$pc = `git-config --get color.pager`;\n>  \t\tif ($pc eq '') {\n> @@ -3998,10 +4003,7 @@ sub log_use_color {\n>  \t\t}\n>  \t\treturn 0;\n>  \t}\n> -\treturn 0 if $dc eq 'never';\n> -\treturn 1 if $dc eq 'always';\n> -\tchomp($dc = `git-config --bool --get $dcvar`);\n> -\treturn ($dc eq 'true');\n> +\treturn 0;\n>  }\n>  \n>  sub git_svn_log_cmd {\n> -\n\n-- \nEric Wong\n"},{"id":"62191","messageId":"5055E3DF-E01D-41B5-9F59-DAD69885CAE8@wincent.com","threadId":"11141","inReplyTo":"1196906706-11170-3-git-send-email-gitster@pobox.com","subject":"Re: [PATCH 3/3] Color support for \"git-add -i\"","fromName":"Wincent Colaiuta","fromEmail":"win@wincent.com","sentAt":"2007-12-06T19:59:48Z","receivedAt":"2007-12-06T19:59:48Z","isPatch":true,"sender":{"key":"greg@hurrell.net","avatar":"https://avatars.githubusercontent.com/u/7074?v=4"},"body":"El 6/12/2007, a las 3:05, Junio C Hamano escribió:\n\n> +sub colored_diff_hunk {\n> +\tmy ($text) = @_;\n> +\t# return the text, so that it can be passed to print()\n> +\tmy @ret;\n> +\tfor (@$text) {\n> +\t\tif (!$diff_use_color) {\n> +\t\t\tpush @ret, $_;\n> +\t\t\tnext;\n> +\t\t}\n> +\n> +\t\tif (/^\\+/) {\n> +\t\t\tpush @ret, colored($new_color, $_);\n> +\t\t} elsif (/^\\-/) {\n> +\t\t\tpush @ret, colored($old_color, $_);\n> +\t\t} elsif (/^\\@/) {\n> +\t\t\tpush @ret, colored($fraginfo_color, $_);\n> +\t\t} elsif (/^ /) {\n> +\t\t\tpush @ret, colored($normal_color, $_);\n> +\t\t} else {\n> +\t\t\tpush @ret, colored($metainfo_color, $_);\n> +\t\t}\n> +\t}\n> +\treturn @ret;\n> +}\n\n\nMy one concern here is that as Git's awareness of whitespace problems  \nbecomes more sophisticated it will be harder and harder to do diff  \ncolorization in a way that matches that which is performed by the  \nbuiltin diff tools. Here I'm talking about the whole core.whitespace  \nseries which allows the user to define per-path attributes specifying  \nwhat kinds of things are to be considered whitespace errors; so far we  \nhave three classes of error proposed as far as I know: trailing  \nwhitespace, spaces before tabs, and indents with non-tabs.\n\nI think it's very important that \"git add --interactive\" be 100%  \nconsistent with the other tools here because in many cases the  \npreviewing you do during an interactive session is what you rely upon  \nto review whether a change should be committed. In other words, you  \ndon't want to think stuff is ok because \"git add --interactive\" leads  \nyou to believe that it's ok when it really isn't, and you don't want  \nto have to run \"git diff\" as a separate step either just to double  \ncheck.\n\nWe can replicate the core.whitespace logic here but it's likely to be  \nan error prone process as it involves repeating the same logic in two  \ndifferent places using two different implementations in two different  \nlanguages.\n\nWhat are the other options?\n\n- Make git-add--interactive part of builtin-add so as to be able to  \nuse the core.whitespace code directly? (ideally yes and at some point  \nin the future it seems inevitable that this will happen, but it will  \nrequire a fair bit of work)\n\n- Fork a second \"git diff-files\" process to capture the colorized  \nversion of the output? (may set off the \"kludge\" alarm)\n\n- Something else?\n\nCheers,\nWincent\n"},{"id":"62194","messageId":"7vejdzd1aw.fsf@gitster.siamese.dyndns.org","threadId":"11141","inReplyTo":"5055E3DF-E01D-41B5-9F59-DAD69885CAE8@wincent.com","subject":"Re: [PATCH 3/3] Color support for \"git-add -i\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-12-06T20:18:47Z","receivedAt":"2007-12-06T20:18:47Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Wincent Colaiuta <win@wincent.com> writes:\n\n> What are the other options?\n>\n> - Make git-add--interactive part of builtin-add so as to be able to  \n> use the core.whitespace code directly? (ideally yes and at some point  \n> in the future it seems inevitable that this will happen, but it will  \n> require a fair bit of work)\n>\n> - Fork a second \"git diff-files\" process to capture the colorized  \n> version of the output? (may set off the \"kludge\" alarm)\n>\n> - Something else?\n\n - Realize that whitespace clean-up can be risky and change semantics\n   depending on the material you are editing.  Do not do the clean-up\n   during \"add -i\", but before.  IOW, add an alias that does an\n   equivalent of:\n\n\tgit diff HEAD >tmp\n        git apply -R <tmp\n        git apply --whitespace=fix <tmp\n\n   then encourage users to clean-up their whitespace gotchas early (and\n   test the result!), before even attempting to run \"git add -i\".\n"},{"id":"62278","messageId":"AEA9FB5E-1ABA-42D6-8FC5-95BAAA9B6BC8@wincent.com","threadId":"11141","inReplyTo":"7vejdzd1aw.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH 3/3] Color support for \"git-add -i\"","fromName":"Wincent Colaiuta","fromEmail":"win@wincent.com","sentAt":"2007-12-07T12:34:51Z","receivedAt":"2007-12-07T12:34:51Z","isPatch":true,"sender":{"key":"greg@hurrell.net","avatar":"https://avatars.githubusercontent.com/u/7074?v=4"},"body":"El 6/12/2007, a las 21:18, Junio C Hamano escribió:\n\n> Wincent Colaiuta <win@wincent.com> writes:\n>\n>> What are the other options?\n>>\n>> - Make git-add--interactive part of builtin-add so as to be able to\n>> use the core.whitespace code directly? (ideally yes and at some point\n>> in the future it seems inevitable that this will happen, but it will\n>> require a fair bit of work)\n>>\n>> - Fork a second \"git diff-files\" process to capture the colorized\n>> version of the output? (may set off the \"kludge\" alarm)\n>>\n>> - Something else?\n>\n> - Realize that whitespace clean-up can be risky and change semantics\n>   depending on the material you are editing.  Do not do the clean-up\n>   during \"add -i\", but before.  IOW, add an alias that does an\n>   equivalent of:\n>\n> \tgit diff HEAD >tmp\n>        git apply -R <tmp\n>        git apply --whitespace=fix <tmp\n>\n>   then encourage users to clean-up their whitespace gotchas early (and\n>   test the result!), before even attempting to run \"git add -i\".\n\nIn principle I agree with what you're saying but I think I didn't make  \nmyself clear; I wasn't talking about actually *cleaning-up* whitespace  \nproblems from inside \"git add -i\", I was talking about *display* of  \nwhitespace problems only. In other words, I don't want the situation  \nwhere I think it's ok to stage a hunk only because I mistakenly think  \nit's free of whitespace problems. In other words, if one part of Git  \n(\"git diff\") says the diff is \"this\" and another part (\"git add -i\")  \nsays the diff is \"that\" and there's a discrepancy, then that sounds  \nlike a bug to me.\n\nSeeing as speed is not a concern here (when displaying diffs the  \nbottleneck is really how fast the user can read, not how fast git  \nemits the text), correctness is important, and git-add--interactive  \nwill eventually become part of builtin-add anyway, I personally think  \nthe optimum solution is this one: run the diff tools twice, once  \nwithout color and once with; the former is used for internal  \nprocessing as before and the later is used for display only. This  \napproach has the advantage that the whitespace colorization provided  \nby \"git diff-files\" can evolve and become more sophisticated (per-path  \nattributes etc) and \"git add -i\" will automatically benefit from it.\n\nBut I wanted to throw the question out there for input first, in any  \ncase, I have a patch implementing my proposal and I'll send it out in  \na minute.\n\nCheers,\nWincent\n"}]}