summaryrefslogtreecommitdiff
path: root/transport.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 /transport.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 'transport.c')
-rw-r--r--transport.c67
1 files changed, 66 insertions, 1 deletions
diff --git a/transport.c b/transport.c
index 94eccf2..f72081a 100644
--- a/transport.c
+++ b/transport.c
@@ -19,6 +19,56 @@
#include "sigchain.h"
#include "transport-internal.h"
#include "object-store.h"
+#include "color.h"
+
+static int transport_use_color = -1;
+static char transport_colors[][COLOR_MAXLEN] = {
+ GIT_COLOR_RESET,
+ GIT_COLOR_RED /* REJECTED */
+};
+
+enum color_transport {
+ TRANSPORT_COLOR_RESET = 0,
+ TRANSPORT_COLOR_REJECTED = 1
+};
+
+static int transport_color_config(void)
+{
+ const char *keys[] = {
+ "color.transport.reset",
+ "color.transport.rejected"
+ }, *key = "color.transport";
+ char *value;
+ int i;
+ static int initialized;
+
+ if (initialized)
+ return 0;
+ initialized = 1;
+
+ if (!git_config_get_string(key, &value))
+ transport_use_color = git_config_colorbool(key, value);
+
+ if (!want_color_stderr(transport_use_color))
+ return 0;
+
+ for (i = 0; i < ARRAY_SIZE(keys); i++)
+ if (!git_config_get_string(keys[i], &value)) {
+ if (!value)
+ return config_error_nonbool(keys[i]);
+ if (color_parse(value, transport_colors[i]) < 0)
+ return -1;
+ }
+
+ return 0;
+}
+
+static const char *transport_get_color(enum color_transport ix)
+{
+ if (want_color_stderr(transport_use_color))
+ return transport_colors[ix];
+ return "";
+}
static void set_upstreams(struct transport *transport, struct ref *refs,
int pretend)
@@ -339,7 +389,13 @@ static void print_ref_status(char flag, const char *summary,
else
fprintf(stdout, "%s\n", summary);
} else {
- fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
+ const char *red = "", *reset = "";
+ if (push_had_errors(to)) {
+ red = transport_get_color(TRANSPORT_COLOR_REJECTED);
+ reset = transport_get_color(TRANSPORT_COLOR_RESET);
+ }
+ fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
+ summary, reset);
if (from)
fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
else
@@ -488,6 +544,9 @@ void transport_print_push_status(const char *dest, struct ref *refs,
char *head;
int summary_width = transport_summary_width(refs);
+ if (transport_color_config() < 0)
+ warning(_("could not parse transport.color.* config"));
+
head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
if (verbose) {
@@ -554,6 +613,9 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re
struct send_pack_args args;
int ret;
+ if (transport_color_config() < 0)
+ return -1;
+
if (!data->got_remote_heads) {
struct ref *tmp_refs;
connect_setup(transport, 1);
@@ -998,6 +1060,9 @@ int transport_push(struct transport *transport,
*reject_reasons = 0;
transport_verify_remote_names(refspec_nr, refspec);
+ if (transport_color_config() < 0)
+ return -1;
+
if (transport->vtable->push_refs) {
struct ref *remote_refs;
struct ref *local_refs = get_local_heads();