transport: remove transport_verify_remote_names
[gitweb.git] / transport.c
index 16b2f54f22e910c27ea10c32dfbc398925cc85c1..fe96c0b8077ec6dd634a03a85134b6f2686c54c6 100644 (file)
@@ -11,6 +11,7 @@
 #include "bundle.h"
 #include "dir.h"
 #include "refs.h"
+#include "refspec.h"
 #include "branch.h"
 #include "url.h"
 #include "submodule.h"
 #include "transport-internal.h"
 #include "protocol.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 +390,7 @@ int transport_refs_pushed(struct ref *ref)
 
 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
 {
-       struct refspec rs;
+       struct refspec_item rs;
 
        if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
                return;
@@ -373,7 +424,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
@@ -522,6 +579,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) {
@@ -559,35 +619,15 @@ void transport_print_push_status(const char *dest, struct ref *refs,
        free(head);
 }
 
-void transport_verify_remote_names(int nr_heads, const char **heads)
-{
-       int i;
-
-       for (i = 0; i < nr_heads; i++) {
-               const char *local = heads[i];
-               const char *remote = strrchr(heads[i], ':');
-
-               if (*local == '+')
-                       local++;
-
-               /* A matching refspec is okay.  */
-               if (remote == local && remote[1] == '\0')
-                       continue;
-
-               remote = remote ? (remote + 1) : local;
-               if (check_refname_format(remote,
-                               REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
-                       die("remote part of refspec is not a valid name in %s",
-                               heads[i]);
-       }
-}
-
 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
 {
        struct git_transport_data *data = transport->data;
        struct send_pack_args args;
        int ret = 0;
 
+       if (transport_color_config() < 0)
+               return -1;
+
        if (!data->got_remote_heads)
                get_refs_via_connect(transport, 1, NULL);
 
@@ -1030,11 +1070,13 @@ static int run_pre_push_hook(struct transport *transport,
 }
 
 int transport_push(struct transport *transport,
-                  int refspec_nr, const char **refspec, int flags,
+                  struct refspec *rs, int flags,
                   unsigned int *reject_reasons)
 {
        *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;
@@ -1045,21 +1087,20 @@ int transport_push(struct transport *transport,
                int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
                int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
                int push_ret, ret, err;
-               struct refspec *tmp_rs;
                struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
                int i;
 
-               if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
+               if (check_push_refs(local_refs, rs->raw_nr, rs->raw) < 0)
                        return -1;
 
-               tmp_rs = parse_push_refspec(refspec_nr, refspec);
-               for (i = 0; i < refspec_nr; i++) {
+               for (i = 0; i < rs->nr; i++) {
+                       const struct refspec_item *item = &rs->items[i];
                        const char *prefix = NULL;
 
-                       if (tmp_rs[i].dst)
-                               prefix = tmp_rs[i].dst;
-                       else if (tmp_rs[i].src && !tmp_rs[i].exact_sha1)
-                               prefix = tmp_rs[i].src;
+                       if (item->dst)
+                               prefix = item->dst;
+                       else if (item->src && !item->exact_sha1)
+                               prefix = item->src;
 
                        if (prefix) {
                                const char *glob = strchr(prefix, '*');
@@ -1076,7 +1117,6 @@ int transport_push(struct transport *transport,
                                                               &ref_prefixes);
 
                argv_array_clear(&ref_prefixes);
-               free_refspec(refspec_nr, tmp_rs);
 
                if (flags & TRANSPORT_PUSH_ALL)
                        match_flags |= MATCH_REFS_ALL;
@@ -1088,7 +1128,7 @@ int transport_push(struct transport *transport,
                        match_flags |= MATCH_REFS_FOLLOW_TAGS;
 
                if (match_push_refs(local_refs, &remote_refs,
-                                   refspec_nr, refspec, match_flags)) {
+                                   rs->raw_nr, rs->raw, match_flags)) {
                        return -1;
                }
 
@@ -1119,7 +1159,7 @@ int transport_push(struct transport *transport,
 
                        if (!push_unpushed_submodules(&commits,
                                                      transport->remote,
-                                                     refspec, refspec_nr,
+                                                     rs->raw, rs->raw_nr,
                                                      transport->push_options,
                                                      pretend)) {
                                oid_array_clear(&commits);