fetch: align all "remote -> local" output
[gitweb.git] / builtin / fetch.c
index e4639d8eb1d5fda586520f10271c05a0897f2ea5..2bc609b7f7152af97c5adea0172ba90f807ac7b0 100644 (file)
@@ -15,6 +15,7 @@
 #include "submodule.h"
 #include "connected.h"
 #include "argv-array.h"
+#include "utf8.h"
 
 static const char * const builtin_fetch_usage[] = {
        N_("git fetch [<options>] [<repository> [<refspec>...]]"),
@@ -37,7 +38,7 @@ static int prune = -1; /* unspecified */
 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;
 static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
 static int tags = TAGS_DEFAULT, unshallow, update_shallow;
-static int max_children = 1;
+static int max_children = -1;
 static enum transport_family family;
 static const char *depth;
 static const char *upload_pack;
@@ -449,7 +450,57 @@ static int s_update_ref(const char *action,
                           : STORE_REF_ERROR_OTHER;
 }
 
-#define REFCOL_WIDTH  10
+static int refcol_width = 10;
+
+static void adjust_refcol_width(const struct ref *ref)
+{
+       int max, rlen, llen, len;
+
+       /* uptodate lines are only shown on high verbosity level */
+       if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid))
+               return;
+
+       max    = term_columns();
+       rlen   = utf8_strwidth(prettify_refname(ref->name));
+       llen   = utf8_strwidth(prettify_refname(ref->peer_ref->name));
+
+       /*
+        * rough estimation to see if the output line is too long and
+        * should not be counted (we can't do precise calculation
+        * anyway because we don't know if the error explanation part
+        * will be printed in update_local_ref)
+        */
+       len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
+       if (len >= max)
+               return;
+
+       if (refcol_width < rlen)
+               refcol_width = rlen;
+}
+
+static void prepare_format_display(struct ref *ref_map)
+{
+       struct ref *rm;
+
+       for (rm = ref_map; rm; rm = rm->next) {
+               if (rm->status == REF_STATUS_REJECT_SHALLOW ||
+                   !rm->peer_ref ||
+                   !strcmp(rm->name, "HEAD"))
+                       continue;
+
+               adjust_refcol_width(rm);
+       }
+}
+
+static void format_display(struct strbuf *display, char code,
+                          const char *summary, const char *error,
+                          const char *remote, const char *local)
+{
+       strbuf_addf(display, "%c %-*s ", code, TRANSPORT_SUMMARY(summary));
+       strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
+       if (error)
+               strbuf_addf(display, "  (%s)", error);
+}
 
 static int update_local_ref(struct ref *ref,
                            const char *remote,
@@ -467,9 +518,8 @@ static int update_local_ref(struct ref *ref,
 
        if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
                if (verbosity > 0)
-                       strbuf_addf(display, "= %-*s %-*s -> %s",
-                                   TRANSPORT_SUMMARY(_("[up to date]")),
-                                   REFCOL_WIDTH, remote, pretty_ref);
+                       format_display(display, '=', _("[up to date]"), NULL,
+                                      remote, pretty_ref);
                return 0;
        }
 
@@ -481,10 +531,9 @@ static int update_local_ref(struct ref *ref,
                 * If this is the head, and it's not okay to update
                 * the head, and the old value of the head isn't empty...
                 */
-               strbuf_addf(display,
-                           _("! %-*s %-*s -> %s  (can't fetch in current branch)"),
-                           TRANSPORT_SUMMARY(_("[rejected]")),
-                           REFCOL_WIDTH, remote, pretty_ref);
+               format_display(display, '!', _("[rejected]"),
+                              _("can't fetch in current branch"),
+                              remote, pretty_ref);
                return 1;
        }
 
@@ -492,11 +541,9 @@ static int update_local_ref(struct ref *ref,
            starts_with(ref->name, "refs/tags/")) {
                int r;
                r = s_update_ref("updating tag", ref, 0);
-               strbuf_addf(display, "%c %-*s %-*s -> %s%s",
-                           r ? '!' : '-',
-                           TRANSPORT_SUMMARY(_("[tag update]")),
-                           REFCOL_WIDTH, remote, pretty_ref,
-                           r ? _("  (unable to update local ref)") : "");
+               format_display(display, r ? '!' : 't', _("[tag update]"),
+                              r ? _("unable to update local ref") : NULL,
+                              remote, pretty_ref);
                return r;
        }
 
@@ -527,11 +574,9 @@ static int update_local_ref(struct ref *ref,
                    (recurse_submodules != RECURSE_SUBMODULES_ON))
                        check_for_new_submodule_commits(ref->new_oid.hash);
                r = s_update_ref(msg, ref, 0);
-               strbuf_addf(display, "%c %-*s %-*s -> %s%s",
-                           r ? '!' : '*',
-                           TRANSPORT_SUMMARY(what),
-                           REFCOL_WIDTH, remote, pretty_ref,
-                           r ? _("  (unable to update local ref)") : "");
+               format_display(display, r ? '!' : '*', what,
+                              r ? _("unable to update local ref") : NULL,
+                              remote, pretty_ref);
                return r;
        }
 
@@ -545,11 +590,9 @@ static int update_local_ref(struct ref *ref,
                    (recurse_submodules != RECURSE_SUBMODULES_ON))
                        check_for_new_submodule_commits(ref->new_oid.hash);
                r = s_update_ref("fast-forward", ref, 1);
-               strbuf_addf(display, "%c %-*s %-*s -> %s%s",
-                           r ? '!' : ' ',
-                           TRANSPORT_SUMMARY_WIDTH, quickref.buf,
-                           REFCOL_WIDTH, remote, pretty_ref,
-                           r ? _("  (unable to update local ref)") : "");
+               format_display(display, r ? '!' : ' ', quickref.buf,
+                              r ? _("unable to update local ref") : NULL,
+                              remote, pretty_ref);
                strbuf_release(&quickref);
                return r;
        } else if (force || ref->force) {
@@ -562,18 +605,14 @@ static int update_local_ref(struct ref *ref,
                    (recurse_submodules != RECURSE_SUBMODULES_ON))
                        check_for_new_submodule_commits(ref->new_oid.hash);
                r = s_update_ref("forced-update", ref, 1);
-               strbuf_addf(display, "%c %-*s %-*s -> %s  (%s)",
-                           r ? '!' : '+',
-                           TRANSPORT_SUMMARY_WIDTH, quickref.buf,
-                           REFCOL_WIDTH, remote, pretty_ref,
-                           r ? _("unable to update local ref") : _("forced update"));
+               format_display(display, r ? '!' : '+', quickref.buf,
+                              r ? _("unable to update local ref") : _("forced update"),
+                              remote, pretty_ref);
                strbuf_release(&quickref);
                return r;
        } else {
-               strbuf_addf(display, "! %-*s %-*s -> %s  %s",
-                           TRANSPORT_SUMMARY(_("[rejected]")),
-                           REFCOL_WIDTH, remote, pretty_ref,
-                           _("(non-fast-forward)"));
+               format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
+                              remote, pretty_ref);
                return 1;
        }
 }
@@ -607,7 +646,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
 
        fp = fopen(filename, "a");
        if (!fp)
-               return error(_("cannot open %s: %s\n"), filename, strerror(errno));
+               return error_errno(_("cannot open %s"), filename);
 
        if (raw_url)
                url = transport_anonymize_url(raw_url);
@@ -620,6 +659,8 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
                goto abort;
        }
 
+       prepare_format_display(ref_map);
+
        /*
         * We do a pass for each fetch_head_status type in their enum order, so
         * merged entries are written before not-for-merge. That lets readers
@@ -714,11 +755,10 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
                                rc |= update_local_ref(ref, what, rm, &note);
                                free(ref);
                        } else
-                               strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
-                                           TRANSPORT_SUMMARY_WIDTH,
-                                           *kind ? kind : "branch",
-                                           REFCOL_WIDTH,
-                                           *what ? what : "HEAD");
+                               format_display(&note, '*',
+                                              *kind ? kind : "branch", NULL,
+                                              *what ? what : "HEAD",
+                                              "FETCH_HEAD");
                        if (note.len) {
                                if (verbosity >= 0 && !shown_url) {
                                        fprintf(stderr, _("From %.*s\n"),
@@ -812,13 +852,15 @@ static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
 
        if (verbosity >= 0) {
                for (ref = stale_refs; ref; ref = ref->next) {
+                       struct strbuf sb = STRBUF_INIT;
                        if (!shown_url) {
                                fprintf(stderr, _("From %.*s\n"), url_len, url);
                                shown_url = 1;
                        }
-                       fprintf(stderr, " x %-*s %-*s -> %s\n",
-                               TRANSPORT_SUMMARY(_("[deleted]")),
-                               REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
+                       format_display(&sb, '-', _("[deleted]"), NULL,
+                                      _("(none)"), prettify_refname(ref->name));
+                       fprintf(stderr, " %s\n",sb.buf);
+                       strbuf_release(&sb);
                        warn_dangling_symref(stderr, dangling_msg, ref->name);
                }
        }
@@ -848,7 +890,7 @@ static int truncate_fetch_head(void)
        FILE *fp = fopen_for_writing(filename);
 
        if (!fp)
-               return error(_("cannot open %s: %s\n"), filename, strerror(errno));
+               return error_errno(_("cannot open %s"), filename);
        fclose(fp);
        return 0;
 }