fetch-object: set exact_oid when fetching
[gitweb.git] / sideband.c
index 6d7f943e4384fa90001fbb73cc5b63e2c0cfa639..368647acf8c04decee14ceea2f03c535a206ad18 100644 (file)
@@ -1,6 +1,113 @@
 #include "cache.h"
+#include "color.h"
+#include "config.h"
 #include "pkt-line.h"
 #include "sideband.h"
+#include "help.h"
+
+struct keyword_entry {
+       /*
+        * We use keyword as config key so it should be a single alphanumeric word.
+        */
+       const char *keyword;
+       char color[COLOR_MAXLEN];
+};
+
+static struct keyword_entry keywords[] = {
+       { "hint",       GIT_COLOR_YELLOW },
+       { "warning",    GIT_COLOR_BOLD_YELLOW },
+       { "success",    GIT_COLOR_BOLD_GREEN },
+       { "error",      GIT_COLOR_BOLD_RED },
+};
+
+/* Returns a color setting (GIT_COLOR_NEVER, etc). */
+static int use_sideband_colors(void)
+{
+       static int use_sideband_colors_cached = -1;
+
+       const char *key = "color.remote";
+       struct strbuf sb = STRBUF_INIT;
+       char *value;
+       int i;
+
+       if (use_sideband_colors_cached >= 0)
+               return use_sideband_colors_cached;
+
+       if (!git_config_get_string(key, &value)) {
+               use_sideband_colors_cached = git_config_colorbool(key, value);
+       } else if (!git_config_get_string("color.ui", &value)) {
+               use_sideband_colors_cached = git_config_colorbool("color.ui", value);
+       } else {
+               use_sideband_colors_cached = GIT_COLOR_AUTO;
+       }
+
+       for (i = 0; i < ARRAY_SIZE(keywords); i++) {
+               strbuf_reset(&sb);
+               strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword);
+               if (git_config_get_string(sb.buf, &value))
+                       continue;
+               if (color_parse(value, keywords[i].color))
+                       continue;
+       }
+       strbuf_release(&sb);
+       return use_sideband_colors_cached;
+}
+
+void list_config_color_sideband_slots(struct string_list *list, const char *prefix)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(keywords); i++)
+               list_config_item(list, prefix, keywords[i].keyword);
+}
+
+/*
+ * Optionally highlight one keyword in remote output if it appears at the start
+ * of the line. This should be called for a single line only, which is
+ * passed as the first N characters of the SRC array.
+ *
+ * NEEDSWORK: use "size_t n" instead for clarity.
+ */
+static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
+{
+       int i;
+
+       if (!want_color_stderr(use_sideband_colors())) {
+               strbuf_add(dest, src, n);
+               return;
+       }
+
+       while (0 < n && isspace(*src)) {
+               strbuf_addch(dest, *src);
+               src++;
+               n--;
+       }
+
+       for (i = 0; i < ARRAY_SIZE(keywords); i++) {
+               struct keyword_entry *p = keywords + i;
+               int len = strlen(p->keyword);
+
+               if (n <= len)
+                       continue;
+               /*
+                * Match case insensitively, so we colorize output from existing
+                * servers regardless of the case that they use for their
+                * messages. We only highlight the word precisely, so
+                * "successful" stays uncolored.
+                */
+               if (!strncasecmp(p->keyword, src, len) && !isalnum(src[len])) {
+                       strbuf_addstr(dest, p->color);
+                       strbuf_add(dest, src, len);
+                       strbuf_addstr(dest, GIT_COLOR_RESET);
+                       n -= len;
+                       src += len;
+                       break;
+               }
+       }
+
+       strbuf_add(dest, src, n);
+}
+
 
 /*
  * Receive multiplexed output stream over git native protocol.
  * the remote died unexpectedly.  A flush() concludes the stream.
  */
 
-#define PREFIX "remote: "
+#define DISPLAY_PREFIX "remote: "
 
 #define ANSI_SUFFIX "\033[K"
 #define DUMB_SUFFIX "        "
@@ -48,8 +155,10 @@ int recv_sideband(const char *me, int in_stream, int out)
                len--;
                switch (band) {
                case 3:
-                       strbuf_addf(&outbuf, "%s%s%s", outbuf.len ? "\n" : "",
-                                   PREFIX, buf + 1);
+                       strbuf_addf(&outbuf, "%s%s", outbuf.len ? "\n" : "",
+                                   DISPLAY_PREFIX);
+                       maybe_colorize_sideband(&outbuf, buf + 1, len);
+
                        retval = SIDEBAND_REMOTE_ERROR;
                        break;
                case 2:
@@ -67,22 +176,24 @@ int recv_sideband(const char *me, int in_stream, int out)
                                int linelen = brk - b;
 
                                if (!outbuf.len)
-                                       strbuf_addstr(&outbuf, PREFIX);
+                                       strbuf_addstr(&outbuf, DISPLAY_PREFIX);
                                if (linelen > 0) {
-                                       strbuf_addf(&outbuf, "%.*s%s%c",
-                                                   linelen, b, suffix, *brk);
-                               } else {
-                                       strbuf_addch(&outbuf, *brk);
+                                       maybe_colorize_sideband(&outbuf, b, linelen);
+                                       strbuf_addstr(&outbuf, suffix);
                                }
+
+                               strbuf_addch(&outbuf, *brk);
                                xwrite(2, outbuf.buf, outbuf.len);
                                strbuf_reset(&outbuf);
 
                                b = brk + 1;
                        }
 
-                       if (*b)
-                               strbuf_addf(&outbuf, "%s%s",
-                                           outbuf.len ? "" : PREFIX, b);
+                       if (*b) {
+                               strbuf_addstr(&outbuf, outbuf.len ?
+                                           "" : DISPLAY_PREFIX);
+                               maybe_colorize_sideband(&outbuf, b, strlen(b));
+                       }
                        break;
                case 1:
                        write_or_die(out, buf + 1, len);