Re: [PATCH v2 2/4] push: colorize errors

2018-04-06 Thread Johannes Schindelin
Hi Jake,

On Thu, 5 Apr 2018, Jacob Keller wrote:

> On Thu, Apr 5, 2018 at 3:48 PM, Johannes Schindelin
>  wrote:
> > From: Ryan Dammrose 
> >
> > This is an attempt to resolve an issue I experience with people that are
> > new to Git -- especially colleagues in a team setting -- where they miss
> > that their push to a remote location failed because the failure and
> > success both return a block of white text.
> >
> > An example is if I push something to a remote repository and then a
> > colleague attempts to push to the same remote repository and the push
> > fails because it requires them to pull first, but they don't notice
> > because a success and failure both return a block of white text. They
> > then continue about their business, thinking it has been successfully
> > pushed.
> >
> > This patch colorizes the errors and hints (in red and yellow,
> > respectively) so whenever there is a failure when pushing to a remote
> > repository that fails, it is more noticeable.
> >
> > [jes: fixed a couple bugs, added the color.{advice,push,transport}
> > settings, refactored to use want_color_stderr().]
> >
> > Signed-off-by: Ryan Dammrose ryandammr...@gmail.com
> > Signed-off-by: Johannes Schindelin 
> >
> > squash! push: colorize errors
> >
> > Stop talking about localized errors
> 
> Guessing you intended to remove this part after squashing?

Hah! You caught be red-handed.

This was intended as a reminder, as you probably guessed, to remove any
mentions of "localized errors" because I had verified that there was no
localized error message; besides, I replaced the call to strstr() looking
at the error message with a call to push_had_errors() (i.e. using the
ref_status instead). So there are definitely no issues about localized
errors left.

> Didn't see anything else to comment on in the actual code.

Thank you,
Dscho


Re: [PATCH v2 2/4] push: colorize errors

2018-04-05 Thread Jacob Keller
On Thu, Apr 5, 2018 at 3:48 PM, Johannes Schindelin
 wrote:
> From: Ryan Dammrose 
>
> This is an attempt to resolve an issue I experience with people that are
> new to Git -- especially colleagues in a team setting -- where they miss
> that their push to a remote location failed because the failure and
> success both return a block of white text.
>
> An example is if I push something to a remote repository and then a
> colleague attempts to push to the same remote repository and the push
> fails because it requires them to pull first, but they don't notice
> because a success and failure both return a block of white text. They
> then continue about their business, thinking it has been successfully
> pushed.
>
> This patch colorizes the errors and hints (in red and yellow,
> respectively) so whenever there is a failure when pushing to a remote
> repository that fails, it is more noticeable.
>
> [jes: fixed a couple bugs, added the color.{advice,push,transport}
> settings, refactored to use want_color_stderr().]
>
> Signed-off-by: Ryan Dammrose ryandammr...@gmail.com
> Signed-off-by: Johannes Schindelin 
>
> squash! push: colorize errors
>
> Stop talking about localized errors

Guessing you intended to remove this part after squashing?

Didn't see anything else to comment on in the actual code.

Thanks,
Jake


[PATCH v2 2/4] push: colorize errors

2018-04-05 Thread Johannes Schindelin
From: Ryan Dammrose 

This is an attempt to resolve an issue I experience with people that are
new to Git -- especially colleagues in a team setting -- where they miss
that their push to a remote location failed because the failure and
success both return a block of white text.

An example is if I push something to a remote repository and then a
colleague attempts to push to the same remote repository and the push
fails because it requires them to pull first, but they don't notice
because a success and failure both return a block of white text. They
then continue about their business, thinking it has been successfully
pushed.

This patch colorizes the errors and hints (in red and yellow,
respectively) so whenever there is a failure when pushing to a remote
repository that fails, it is more noticeable.

[jes: fixed a couple bugs, added the color.{advice,push,transport}
settings, refactored to use want_color_stderr().]

Signed-off-by: Ryan Dammrose ryandammr...@gmail.com
Signed-off-by: Johannes Schindelin 

squash! push: colorize errors

Stop talking about localized errors
---
 advice.c   | 49 ++--
 builtin/push.c | 44 -
 config.c   |  2 +-
 transport.c| 67 +-
 4 files changed, 157 insertions(+), 5 deletions(-)

diff --git a/advice.c b/advice.c
index 406efc183ba..2121c1811fd 100644
--- a/advice.c
+++ b/advice.c
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "config.h"
+#include "color.h"
 
 int advice_push_update_rejected = 1;
 int advice_push_non_ff_current = 1;
@@ -20,6 +21,33 @@ int advice_add_embedded_repo = 1;
 int advice_ignored_hook = 1;
 int advice_waiting_for_editor = 1;
 
+static int advice_use_color = -1;
+static char advice_colors[][COLOR_MAXLEN] = {
+   GIT_COLOR_RESET,
+   GIT_COLOR_YELLOW,   /* HINT */
+};
+
+enum color_advice {
+   ADVICE_COLOR_RESET = 0,
+   ADVICE_COLOR_HINT = 1,
+};
+
+static int parse_advise_color_slot(const char *slot)
+{
+   if (!strcasecmp(slot, "reset"))
+   return ADVICE_COLOR_RESET;
+   if (!strcasecmp(slot, "advice"))
+   return ADVICE_COLOR_HINT;
+   return -1;
+}
+
+static const char *advise_get_color(enum color_advice ix)
+{
+   if (want_color_stderr(advice_use_color))
+   return advice_colors[ix];
+   return "";
+}
+
 static struct {
const char *name;
int *preference;
@@ -59,7 +87,10 @@ void advise(const char *advice, ...)
 
for (cp = buf.buf; *cp; cp = np) {
np = strchrnul(cp, '\n');
-   fprintf(stderr, _("hint: %.*s\n"), (int)(np - cp), cp);
+   fprintf(stderr, _("%shint: %.*s%s\n"),
+   advise_get_color(ADVICE_COLOR_HINT),
+   (int)(np - cp), cp,
+   advise_get_color(ADVICE_COLOR_RESET));
if (*np)
np++;
}
@@ -68,9 +99,23 @@ void advise(const char *advice, ...)
 
 int git_default_advice_config(const char *var, const char *value)
 {
-   const char *k;
+   const char *k, *slot_name;
int i;
 
+   if (!strcmp(var, "color.advice")) {
+   advice_use_color = git_config_colorbool(var, value);
+   return 0;
+   }
+
+   if (skip_prefix(var, "color.advice.", _name)) {
+   int slot = parse_advise_color_slot(slot_name);
+   if (slot < 0)
+   return 0;
+   if (!value)
+   return config_error_nonbool(var);
+   return color_parse(value, advice_colors[slot]);
+   }
+
if (!skip_prefix(var, "advice.", ))
return 0;
 
diff --git a/builtin/push.c b/builtin/push.c
index 013c20d6164..ac3705370e1 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -12,12 +12,40 @@
 #include "submodule.h"
 #include "submodule-config.h"
 #include "send-pack.h"
+#include "color.h"
 
 static const char * const push_usage[] = {
N_("git push [] [ [...]]"),
NULL,
 };
 
+static int push_use_color = -1;
+static char push_colors[][COLOR_MAXLEN] = {
+   GIT_COLOR_RESET,
+   GIT_COLOR_RED,  /* ERROR */
+};
+
+enum color_push {
+   PUSH_COLOR_RESET = 0,
+   PUSH_COLOR_ERROR = 1
+};
+
+static int parse_push_color_slot(const char *slot)
+{
+   if (!strcasecmp(slot, "reset"))
+   return PUSH_COLOR_RESET;
+   if (!strcasecmp(slot, "error"))
+   return PUSH_COLOR_ERROR;
+   return -1;
+}
+
+static const char *push_get_color(enum color_push ix)
+{
+   if (want_color_stderr(push_use_color))
+   return push_colors[ix];
+   return "";
+}
+
 static int thin = 1;
 static int deleterefs;
 static const char *receivepack;
@@ -337,8 +365,11 @@ static int push_with_options(struct transport *transport, 
int flags)
fprintf(stderr, _("Pushing to