Skip to content

Commit 81981e3

Browse files
committed
area: transport.c, push.c, advice.c
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 him to pull first, but he doesn't notice because a success and failure both return a block of white text. He then continues about his business, thinking it has been successfully pushed. My solution was to try to change the stderr and hint colors (red and yellow, respectively) so whenever there is a failure when pushing to a remote repository that fails, it is more noticeable. The challenge was that it seemed that stderr has never been colored and I attempted to utilize what was already established; but this meant using functions like want_color() even if it targets stdout while I wanted to target stderr. Additionally, to check for all rejection types, I did a strstr check in transport.c, but this code could be problematic if there is need for translation. Signed-off-by: Ryan Dammrose [email protected]
1 parent 3016f0c commit 81981e3

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

advice.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "config.h"
3+
#include "color.h"
34

45
int advice_push_update_rejected = 1;
56
int advice_push_non_ff_current = 1;
@@ -20,6 +21,33 @@ int advice_add_embedded_repo = 1;
2021
int advice_ignored_hook = 1;
2122
int advice_waiting_for_editor = 1;
2223

24+
static int advice_use_color = -1;
25+
static char advice_colors[][COLOR_MAXLEN] = {
26+
GIT_COLOR_RESET,
27+
GIT_COLOR_YELLOW, /* HINT */
28+
};
29+
30+
enum color_advice {
31+
ADVICE_COLOR_RESET = 0,
32+
ADVICE_COLOR_HINT = 1,
33+
};
34+
35+
static int parse_advise_color_slot(const char *slot)
36+
{
37+
if (!strcasecmp(slot, "reset"))
38+
return ADVICE_COLOR_RESET;
39+
if (!strcasecmp(slot, "advice"))
40+
return ADVICE_COLOR_HINT;
41+
return -1;
42+
}
43+
44+
static const char *advise_get_color(enum color_advice ix)
45+
{
46+
if (want_color(advice_use_color))
47+
return advice_colors[ix];
48+
return "";
49+
}
50+
2351
static struct {
2452
const char *name;
2553
int *preference;
@@ -59,7 +87,8 @@ void advise(const char *advice, ...)
5987

6088
for (cp = buf.buf; *cp; cp = np) {
6189
np = strchrnul(cp, '\n');
62-
fprintf(stderr, _("hint: %.*s\n"), (int)(np - cp), cp);
90+
fprintf(stderr, _("%shint: %.*s%s\n"), advise_get_color(ADVICE_COLOR_HINT),
91+
(int)(np - cp), cp, advise_get_color(ADVICE_COLOR_RESET));
6392
if (*np)
6493
np++;
6594
}

builtin/push.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,40 @@
1212
#include "submodule.h"
1313
#include "submodule-config.h"
1414
#include "send-pack.h"
15+
#include "color.h"
1516

1617
static const char * const push_usage[] = {
1718
N_("git push [<options>] [<repository> [<refspec>...]]"),
1819
NULL,
1920
};
2021

22+
static int push_use_color = -1;
23+
static char push_colors[][COLOR_MAXLEN] = {
24+
GIT_COLOR_RESET,
25+
GIT_COLOR_RED, /* ERROR */
26+
};
27+
28+
enum color_push {
29+
PUSH_COLOR_RESET = 0,
30+
PUSH_COLOR_ERROR = 1
31+
};
32+
33+
static int parse_push_color_slot(const char *slot)
34+
{
35+
if (!strcasecmp(slot, "reset"))
36+
return PUSH_COLOR_RESET;
37+
if (!strcasecmp(slot, "error"))
38+
return PUSH_COLOR_ERROR;
39+
return -1;
40+
}
41+
42+
static const char *push_get_color(enum color_push ix)
43+
{
44+
if (want_color(push_use_color))
45+
return push_colors[ix];
46+
return "";
47+
}
48+
2149
static int thin = 1;
2250
static int deleterefs;
2351
static const char *receivepack;
@@ -338,7 +366,9 @@ static int push_with_options(struct transport *transport, int flags)
338366
err = transport_push(transport, refspec_nr, refspec, flags,
339367
&reject_reasons);
340368
if (err != 0)
369+
fprintf(stderr, "%s", push_get_color(PUSH_COLOR_ERROR));
341370
error(_("failed to push some refs to '%s'"), transport->url);
371+
fprintf(stderr, "%s", push_get_color(PUSH_COLOR_RESET));
342372

343373
err |= transport_disconnect(transport);
344374
if (!err)

transport.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@
1818
#include "sha1-array.h"
1919
#include "sigchain.h"
2020
#include "transport-internal.h"
21+
#include "color.h"
22+
23+
static int transport_use_color = -1;
24+
static char transport_colors[][COLOR_MAXLEN] = {
25+
GIT_COLOR_RESET,
26+
GIT_COLOR_RED /* REJECTED */
27+
};
28+
29+
enum color_transport {
30+
TRANSPORT_COLOR_RESET = 0,
31+
TRANSPORT_COLOR_REJECTED = 1
32+
};
33+
34+
static int parse_transport_color_slot(const char *slot)
35+
{
36+
if (!strcasecmp(slot, "reset"))
37+
return TRANSPORT_COLOR_RESET;
38+
if (!strcasecmp(slot, "rejected"))
39+
return TRANSPORT_COLOR_REJECTED;
40+
return -1;
41+
}
42+
43+
static const char *transport_get_color(enum color_transport ix)
44+
{
45+
if (want_color(transport_use_color))
46+
return transport_colors[ix];
47+
return "";
48+
}
2149

2250
static void set_upstreams(struct transport *transport, struct ref *refs,
2351
int pretend)
@@ -326,7 +354,11 @@ static void print_ref_status(char flag, const char *summary,
326354
else
327355
fprintf(stdout, "%s\n", summary);
328356
} else {
329-
fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
357+
if (strstr(summary, "rejected") != NULL || strstr(summary, "failure") != NULL)
358+
fprintf(stderr, " %s%c %-*s%s ", transport_get_color(TRANSPORT_COLOR_REJECTED),
359+
flag, summary_width, summary, transport_get_color(TRANSPORT_COLOR_RESET));
360+
else
361+
fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
330362
if (from)
331363
fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
332364
else

0 commit comments

Comments
 (0)