summaryrefslogtreecommitdiff
path: root/advice.c
diff options
context:
space:
mode:
authorRyan Dammrose <ryandammrose@gmail.com>2018-04-21 10:10:00 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-04-24 01:38:47 (GMT)
commit960786e7618942357c45e7e4c234f113e0aea9b1 (patch)
tree1edc46bc7944e13bc08c8ad4794e39bc14ed1cc3 /advice.c
parent295d949cfabc18fb588f269c77d1f8ff4a613686 (diff)
downloadgit-960786e7618942357c45e7e4c234f113e0aea9b1.zip
git-960786e7618942357c45e7e4c234f113e0aea9b1.tar.gz
git-960786e7618942357c45e7e4c234f113e0aea9b1.tar.bz2
push: colorize errors
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 ryandammrose@gmail.com Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'advice.c')
-rw-r--r--advice.c49
1 files changed, 47 insertions, 2 deletions
diff --git a/advice.c b/advice.c
index 406efc1..89fda1d 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, "hint"))
+ 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.", &slot_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.", &k))
return 0;