[add-default-config] add --default option to git config.

2017-11-28 Thread Soukaina NAIT HMID
From: Soukaina NAIT HMID 

Signed-off-by: Soukaina NAIT HMID 
---
 Documentation/git-config.txt |   4 ++
 builtin/config.c |  34 -
 config.c |  10 +++
 config.h |   1 +
 t/t1300-repo-config.sh   | 161 +++
 5 files changed, 209 insertions(+), 1 deletion(-)

diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index 4edd09fc6b074..5d5cd58fdae37 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -179,6 +179,10 @@ See also <>.
specified user.  This option has no effect when setting the
value (but you can use `git config section.variable ~/`
from the command line to let your shell do the expansion).
+--color::
+   Find the color configured for `name` (e.g. `color.diff.new`) and
+   output it as the ANSI color escape sequence to the standard
+   output. 
 
 -z::
 --null::
diff --git a/builtin/config.c b/builtin/config.c
index d13daeeb55927..5e5b998b7c892 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -30,6 +30,7 @@ static int end_null;
 static int respect_includes_opt = -1;
 static struct config_options config_options;
 static int show_origin;
+static const char *default_value;
 
 #define ACTION_GET (1<<0)
 #define ACTION_GET_ALL (1<<1)
@@ -52,6 +53,8 @@ static int show_origin;
 #define TYPE_INT (1<<1)
 #define TYPE_BOOL_OR_INT (1<<2)
 #define TYPE_PATH (1<<3)
+#define TYPE_COLOR (1<<4)
+
 
 static struct option builtin_config_options[] = {
OPT_GROUP(N_("Config file location")),
@@ -80,11 +83,13 @@ static struct option builtin_config_options[] = {
OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), 
TYPE_BOOL_OR_INT),
OPT_BIT(0, "path", &types, N_("value is a path (file or directory 
name)"), TYPE_PATH),
+   OPT_BIT(0, "color", &types, N_("find the color configured"), 
TYPE_COLOR),
OPT_GROUP(N_("Other")),
OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include 
directives on lookup")),
OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config 
(file, standard input, blob, command line)")),
+   OPT_STRING(0, "default", &default_value, N_("default-value"), N_("sets 
default value when no value is returned from config")),
OPT_END(),
 };
 
@@ -159,6 +164,13 @@ static int format_config(struct strbuf *buf, const char 
*key_, const char *value
return -1;
strbuf_addstr(buf, v);
free((char *)v);
+   }
+   else if (types == TYPE_COLOR) {
+   char *v = xmalloc(COLOR_MAXLEN);
+   if (git_config_color(&v, key_, value_) < 0)
+   return -1;
+   strbuf_addstr(buf, v);
+   free((char *)v);
} else if (value_) {
strbuf_addstr(buf, value_);
} else {
@@ -244,8 +256,16 @@ static int get_value(const char *key_, const char *regex_)
config_with_options(collect_config, &values,
&given_config_source, &config_options);
 
-   ret = !values.nr;
+   if (!values.nr && default_value && types) {
+   struct strbuf *item;
+   ALLOC_GROW(values.items, values.nr + 1, values.alloc);
+   item = &values.items[values.nr++];
+   if(format_config(item, key_, default_value) < 0){
+   values.nr = 0;
+   }
+   }
 
+   ret = !values.nr;
for (i = 0; i < values.nr; i++) {
struct strbuf *buf = values.items + i;
if (do_all || i == values.nr - 1)
@@ -268,6 +288,7 @@ static int get_value(const char *key_, const char *regex_)
return ret;
 }
 
+
 static char *normalize_value(const char *key, const char *value)
 {
if (!value)
@@ -281,6 +302,17 @@ static char *normalize_value(const char *key, const char 
*value)
 * when retrieving the value.
 */
return xstrdup(value);
+   if (types == TYPE_COLOR)
+   {
+   char *v = xmalloc(COLOR_MAXLEN);
+   if (git_config_color(&v, key, value) == 0)
+   {
+   free((char *)v);
+   return x

[add-default-config 3/5] add same test for new command format with --default and --color

2017-11-12 Thread Soukaina NAIT HMID
From: Soukaina NAIT HMID 

Signed-off-by: Soukaina NAIT HMID 
---
 t/t4026-color2.sh | 129 ++
 1 file changed, 129 insertions(+)
 create mode 100755 t/t4026-color2.sh

diff --git a/t/t4026-color2.sh b/t/t4026-color2.sh
new file mode 100755
index 0..695ce9dd6f8d4
--- /dev/null
+++ b/t/t4026-color2.sh
@@ -0,0 +1,129 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Timo Hirvonen
+#
+
+test_description='Test diff/status color escape codes'
+. ./test-lib.sh
+
+ESC=$(printf '\033')
+color()
+{
+   actual=$(git config --default "$1" --color no.such.slot) &&
+   test "$actual" = "${2:+$ESC}$2"
+}
+
+invalid_color()
+{
+   test_must_fail git config --get-color no.such.slot "$1"
+}
+
+test_expect_success 'reset' '
+   color "reset" "[m"
+'
+
+test_expect_success 'empty color is empty' '
+   color "" ""
+'
+
+test_expect_success 'attribute before color name' '
+   color "bold red" "[1;31m"
+'
+
+test_expect_success 'color name before attribute' '
+   color "red bold" "[1;31m"
+'
+
+test_expect_success 'attr fg bg' '
+   color "ul blue red" "[4;34;41m"
+'
+
+test_expect_success 'fg attr bg' '
+   color "blue ul red" "[4;34;41m"
+'
+
+test_expect_success 'fg bg attr' '
+   color "blue red ul" "[4;34;41m"
+'
+
+test_expect_success 'fg bg attr...' '
+   color "blue bold dim ul blink reverse" "[1;2;4;5;7;34m"
+'
+
+# note that nobold and nodim are the same code (22)
+test_expect_success 'attr negation' '
+   color "nobold nodim noul noblink noreverse" "[22;24;25;27m"
+'
+
+test_expect_success '"no-" variant of negation' '
+   color "no-bold no-blink" "[22;25m"
+'
+
+test_expect_success 'long color specification' '
+   color "254 255 bold dim ul blink reverse" 
"[1;2;4;5;7;38;5;254;48;5;255m"
+'
+
+test_expect_success 'absurdly long color specification' '
+   color \
+ "#ff #ff bold nobold dim nodim italic noitalic
+  ul noul blink noblink reverse noreverse strike nostrike" \
+ "[1;2;3;4;5;7;9;22;23;24;25;27;29;38;2;255;255;255;48;2;255;255;255m"
+'
+
+test_expect_success '0-7 are aliases for basic ANSI color names' '
+   color "0 7" "[30;47m"
+'
+
+test_expect_success '256 colors' '
+   color "254 bold 255" "[1;38;5;254;48;5;255m"
+'
+
+test_expect_success '24-bit colors' '
+   color "#ff00ff black" "[38;2;255;0;255;40m"
+'
+
+test_expect_success '"normal" yields no color at all"' '
+   color "normal black" "[40m"
+'
+
+test_expect_success '-1 is a synonym for "normal"' '
+   color "-1 black" "[40m"
+'
+
+test_expect_success 'color too small' '
+   invalid_color "-2"
+'
+
+test_expect_success 'color too big' '
+   invalid_color "256"
+'
+
+test_expect_success 'extra character after color number' '
+   invalid_color "3X"
+'
+
+test_expect_success 'extra character after color name' '
+   invalid_color "redX"
+'
+
+test_expect_success 'extra character after attribute' '
+   invalid_color "dimX"
+'
+
+test_expect_success 'unknown color slots are ignored (diff)' '
+   git config color.diff.nosuchslotwilleverbedefined white &&
+   git diff --color
+'
+
+test_expect_success 'unknown color slots are ignored (branch)' '
+   git config color.branch.nosuchslotwilleverbedefined white &&
+   git branch -a
+'
+
+test_expect_success 'unknown color slots are ignored (status)' '
+   git config color.status.nosuchslotwilleverbedefined white &&
+   { git status; ret=$?; } &&
+   case $ret in 0|1) : ok ;; *) false ;; esac
+'
+
+test_done

--
https://github.com/git/git/pull/431


[add-default-config 1/5] add --color option to git config

2017-11-12 Thread Soukaina NAIT HMID
From: Soukaina NAIT HMID 

Signed-off-by: Soukaina NAIT HMID 
---
 Documentation/git-config.txt | 4 
 builtin/config.c | 1 +
 2 files changed, 5 insertions(+)

diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index 4edd09fc6b074..5d5cd58fdae37 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -179,6 +179,10 @@ See also <>.
specified user.  This option has no effect when setting the
value (but you can use `git config section.variable ~/`
from the command line to let your shell do the expansion).
+--color::
+   Find the color configured for `name` (e.g. `color.diff.new`) and
+   output it as the ANSI color escape sequence to the standard
+   output. 
 
 -z::
 --null::
diff --git a/builtin/config.c b/builtin/config.c
index d13daeeb55927..124a682d50fa8 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -80,6 +80,7 @@ static struct option builtin_config_options[] = {
OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), 
TYPE_BOOL_OR_INT),
OPT_BIT(0, "path", &types, N_("value is a path (file or directory 
name)"), TYPE_PATH),
+   OPT_BIT(0, "color", &actions, N_("find the color configured"), 
ACTION_GET_COLOR),
OPT_GROUP(N_("Other")),
OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),

--
https://github.com/git/git/pull/431


[add-default-config 4/5] add defaults for path/int/bool

2017-11-12 Thread Soukaina NAIT HMID
From: Soukaina NAIT HMID 

Signed-off-by: Soukaina NAIT HMID 
---
 builtin/config.c  |  12 -
 t/t4026-color2.sh | 129 --
 2 files changed, 11 insertions(+), 130 deletions(-)
 delete mode 100755 t/t4026-color2.sh

diff --git a/builtin/config.c b/builtin/config.c
index 9df2d9c43bcad..eab81c5627091 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -55,6 +55,8 @@ static const char *default_value;
 #define TYPE_BOOL_OR_INT (1<<2)
 #define TYPE_PATH (1<<3)
 
+static char *normalize_value(const char *key, const char *value);
+
 static struct option builtin_config_options[] = {
OPT_GROUP(N_("Config file location")),
OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
@@ -256,8 +258,15 @@ static int get_value(const char *key_, const char *regex_)
fwrite(buf->buf, 1, buf->len, stdout);
strbuf_release(buf);
}
-   free(values.items);
 
+   if (values.nr == 0 && default_value) {
+   if(types == TYPE_INT || types == TYPE_BOOL || types == 
TYPE_BOOL_OR_INT || types == TYPE_PATH ) {
+   char* xstr = normalize_value(key, default_value);
+   fwrite(xstr, 1, strlen(xstr), stdout);
+   fwrite("\n", 1, 1, stdout);
+   }
+   }
+   free(values.items);
 free_strings:
free(key);
if (key_regexp) {
@@ -272,6 +281,7 @@ static int get_value(const char *key_, const char *regex_)
return ret;
 }
 
+
 static char *normalize_value(const char *key, const char *value)
 {
if (!value)
diff --git a/t/t4026-color2.sh b/t/t4026-color2.sh
deleted file mode 100755
index 695ce9dd6f8d4..0
--- a/t/t4026-color2.sh
+++ /dev/null
@@ -1,129 +0,0 @@
-#!/bin/sh
-#
-# Copyright (c) 2008 Timo Hirvonen
-#
-
-test_description='Test diff/status color escape codes'
-. ./test-lib.sh
-
-ESC=$(printf '\033')
-color()
-{
-   actual=$(git config --default "$1" --color no.such.slot) &&
-   test "$actual" = "${2:+$ESC}$2"
-}
-
-invalid_color()
-{
-   test_must_fail git config --get-color no.such.slot "$1"
-}
-
-test_expect_success 'reset' '
-   color "reset" "[m"
-'
-
-test_expect_success 'empty color is empty' '
-   color "" ""
-'
-
-test_expect_success 'attribute before color name' '
-   color "bold red" "[1;31m"
-'
-
-test_expect_success 'color name before attribute' '
-   color "red bold" "[1;31m"
-'
-
-test_expect_success 'attr fg bg' '
-   color "ul blue red" "[4;34;41m"
-'
-
-test_expect_success 'fg attr bg' '
-   color "blue ul red" "[4;34;41m"
-'
-
-test_expect_success 'fg bg attr' '
-   color "blue red ul" "[4;34;41m"
-'
-
-test_expect_success 'fg bg attr...' '
-   color "blue bold dim ul blink reverse" "[1;2;4;5;7;34m"
-'
-
-# note that nobold and nodim are the same code (22)
-test_expect_success 'attr negation' '
-   color "nobold nodim noul noblink noreverse" "[22;24;25;27m"
-'
-
-test_expect_success '"no-" variant of negation' '
-   color "no-bold no-blink" "[22;25m"
-'
-
-test_expect_success 'long color specification' '
-   color "254 255 bold dim ul blink reverse" 
"[1;2;4;5;7;38;5;254;48;5;255m"
-'
-
-test_expect_success 'absurdly long color specification' '
-   color \
- "#ff #ff bold nobold dim nodim italic noitalic
-  ul noul blink noblink reverse noreverse strike nostrike" \
- "[1;2;3;4;5;7;9;22;23;24;25;27;29;38;2;255;255;255;48;2;255;255;255m"
-'
-
-test_expect_success '0-7 are aliases for basic ANSI color names' '
-   color "0 7" "[30;47m"
-'
-
-test_expect_success '256 colors' '
-   color "254 bold 255" "[1;38;5;254;48;5;255m"
-'
-
-test_expect_success '24-bit colors' '
-   color "#ff00ff black" "[38;2;255;0;255;40m"
-'
-
-test_expect_success '"normal" yields no color at all"' '
-   color "normal black" "[40m"
-'
-
-test_expect_success '-1 is a synonym for "normal"' '
-   color "-1 black" "[40m"
-'
-
-test_expect_success 'color too small' '
-   invalid_color "-2"
-'
-
-test_e

[add-default-config 5/5] fix return code on default + add tests

2017-11-12 Thread Soukaina NAIT HMID
From: Soukaina NAIT HMID 

Signed-off-by: Soukaina NAIT HMID 
---
 builtin/config.c   |   9 ++-
 t/t9904-default.sh | 232 +
 2 files changed, 238 insertions(+), 3 deletions(-)
 create mode 100755 t/t9904-default.sh

diff --git a/builtin/config.c b/builtin/config.c
index eab81c5627091..29c5f55f27a57 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -261,9 +261,12 @@ static int get_value(const char *key_, const char *regex_)
 
if (values.nr == 0 && default_value) {
if(types == TYPE_INT || types == TYPE_BOOL || types == 
TYPE_BOOL_OR_INT || types == TYPE_PATH ) {
-   char* xstr = normalize_value(key, default_value);
-   fwrite(xstr, 1, strlen(xstr), stdout);
-   fwrite("\n", 1, 1, stdout);
+   if(strlen(default_value)) {
+   char* xstr = normalize_value(key, 
default_value);
+   fwrite(xstr, 1, strlen(xstr), stdout);
+   fwrite("\n", 1, 1, stdout);
+   ret = 0;
+   }
}
}
free(values.items);
diff --git a/t/t9904-default.sh b/t/t9904-default.sh
new file mode 100755
index 0..8e838f512298b
--- /dev/null
+++ b/t/t9904-default.sh
@@ -0,0 +1,232 @@
+#!/bin/sh
+
+test_description='Test default color/boolean/int/path'
+. ./test-lib.sh
+
+boolean()
+{
+   slot=$([ "$#" == 3 ] && echo $3 || echo "no.such.slot") &&
+   actual=$(git config --default "$1" --bool "$slot") &&
+   test "$actual" = "$2"
+}
+
+invalid_boolean()
+{
+   slot=$([ "$#" == 2 ] && echo $2 || echo "no.such.slot") &&
+   test_must_fail git config --default "$1" --bool "$slot"
+}
+
+test_expect_success 'empty value for boolean' '
+   invalid_boolean ""
+'
+
+test_expect_success 'true' '
+   boolean "true" "true"
+'
+
+test_expect_success '1 is true' '
+   boolean "1" "true"
+'
+
+test_expect_success 'non-zero is true' '
+   boolean "5312" "true"
+'
+
+test_expect_success 'false' '
+   boolean "false" "false"
+'
+
+test_expect_success '0 is false' '
+   boolean "0" "false"
+'
+
+test_expect_success 'invalid value' '
+   invalid_boolean "ab"
+'
+
+test_expect_success 'existing slot has priority = true' '
+   git config bool.value true &&
+   boolean "false" "true" "bool.value"
+'
+
+test_expect_success 'existing slot has priority = false' '
+   git config bool.value false &&
+   boolean "true" "false" "bool.value"
+'
+
+int()
+{
+   slot=$([ "$#" == 3 ] && echo $3 || echo "no.such.slot") &&
+   actual=$(git config --default "$1" --int "$slot") &&
+   test "$actual" = "$2"
+}
+
+invalid_int()
+{
+   slot=$([ "$#" == 2 ] && echo $2 || echo "no.such.slot") &&
+   test_must_fail git config "$1" --int "$slot"
+}
+
+test_expect_success 'empty value for int' '
+   invalid_int "" ""
+'
+
+test_expect_success 'positive' '
+   int "12345" "12345"
+'
+
+test_expect_success 'negative' '
+   int "-679032" "-679032"
+'
+
+test_expect_success 'invalid value' '
+   invalid_int "abc"
+'
+test_expect_success 'existing slot has priority = 123' '
+   git config int.value 123 &&
+   int "666" "123" "int.value"
+'
+
+test_expect_success 'existing slot with bad value' '
+   git config int.value abc &&
+   invalid_int "123" "int.value"
+'
+
+path()
+{
+   slot=$([ "$#" == 3 ] && echo $3 || echo "no.such.slot") &&
+   actual=$(git config --default "$1" --path "$slot") &&
+   test "$actual" = "$2"
+}
+
+invalid_path()
+{
+   slot=$([ "$#" == 2 ] && echo $2 || echo "no.such.slot") &&
+   test_must_fail git config "$1" --path "$slot"
+}
+
+test_expect_success 'empty path is invalid' '
+   invalid_path "" ""
+&

[add-default-config 2/5] adding default to color

2017-11-12 Thread Soukaina NAIT HMID
From: Soukaina NAIT HMID 

Signed-off-by: Soukaina NAIT HMID 
---
 builtin/config.c | 14 +-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/builtin/config.c b/builtin/config.c
index 124a682d50fa8..9df2d9c43bcad 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -30,6 +30,7 @@ static int end_null;
 static int respect_includes_opt = -1;
 static struct config_options config_options;
 static int show_origin;
+static const char *default_value;
 
 #define ACTION_GET (1<<0)
 #define ACTION_GET_ALL (1<<1)
@@ -47,6 +48,7 @@ static int show_origin;
 #define ACTION_GET_COLOR (1<<13)
 #define ACTION_GET_COLORBOOL (1<<14)
 #define ACTION_GET_URLMATCH (1<<15)
+#define ACTION_GET_COLORORDEFAULT (1<<16)
 
 #define TYPE_BOOL (1<<0)
 #define TYPE_INT (1<<1)
@@ -80,12 +82,13 @@ static struct option builtin_config_options[] = {
OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), 
TYPE_BOOL_OR_INT),
OPT_BIT(0, "path", &types, N_("value is a path (file or directory 
name)"), TYPE_PATH),
-   OPT_BIT(0, "color", &actions, N_("find the color configured"), 
ACTION_GET_COLOR),
+   OPT_BIT(0, "color", &actions, N_("find the color configured"), 
ACTION_GET_COLORORDEFAULT),
OPT_GROUP(N_("Other")),
OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include 
directives on lookup")),
OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config 
(file, standard input, blob, command line)")),
+   OPT_STRING(0, "default", &default_value, N_("default-value"), N_("sets 
default for bool/int/path/color when no value is returned from config")),
OPT_END(),
 };
 
@@ -331,6 +334,11 @@ static void get_color(const char *var, const char 
*def_color)
fputs(parsed_color, stdout);
 }
 
+static void get_color_default(const char *var)
+{
+   get_color(var, default_value);
+}
+
 static int get_colorbool_found;
 static int get_diff_color_found;
 static int get_color_ui_found;
@@ -726,6 +734,10 @@ int cmd_config(int argc, const char **argv, const char 
*prefix)
check_argc(argc, 1, 2);
get_color(argv[0], argv[1]);
}
+   else if (actions == ACTION_GET_COLORORDEFAULT) {
+   check_argc(argc, 1, 1);
+   get_color_default(argv[0]);
+   }
else if (actions == ACTION_GET_COLORBOOL) {
check_argc(argc, 1, 2);
if (argc == 2)

--
https://github.com/git/git/pull/431