cmd_fetch_pack(): return early if finish_connect() fails
[gitweb.git] / builtin / branch.c
index f12b626a0ece679cd18a77345c3591b15d5f6cbc..297be4ec4075e7a5877366217d780d6c29b3271b 100644 (file)
 #include "branch.h"
 #include "diff.h"
 #include "revision.h"
+#include "string-list.h"
+#include "column.h"
+#include "utf8.h"
 
 static const char * const builtin_branch_usage[] = {
-       "git branch [options] [-r | -a] [--merged | --no-merged]",
-       "git branch [options] [-l] [-f] <branchname> [<start-point>]",
-       "git branch [options] [-r] (-d | -D) <branchname>...",
-       "git branch [options] (-m | -M) [<oldbranch>] <newbranch>",
+       N_("git branch [options] [-r | -a] [--merged | --no-merged]"),
+       N_("git branch [options] [-l] [-f] <branchname> [<start-point>]"),
+       N_("git branch [options] [-r] (-d | -D) <branchname>..."),
+       N_("git branch [options] (-m | -M) [<oldbranch>] <newbranch>"),
        NULL
 };
 
@@ -53,6 +56,9 @@ static enum merge_filter {
 } merge_filter;
 static unsigned char merge_filter_ref[20];
 
+static struct string_list output = STRING_LIST_INIT_DUP;
+static unsigned int colopts;
+
 static int parse_branch_color_slot(const char *var, int ofs)
 {
        if (!strcasecmp(var+ofs, "plain"))
@@ -70,6 +76,8 @@ static int parse_branch_color_slot(const char *var, int ofs)
 
 static int git_branch_config(const char *var, const char *value, void *cb)
 {
+       if (!prefixcmp(var, "column."))
+               return git_column_config(var, value, "branch", &colopts);
        if (!strcmp(var, "color.branch")) {
                branch_use_color = git_config_colorbool(var, value);
                return 0;
@@ -242,7 +250,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 struct ref_item {
        char *name;
        char *dest;
-       unsigned int kind, len;
+       unsigned int kind, width;
        struct commit *commit;
 };
 
@@ -347,14 +355,14 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
        newitem->name = xstrdup(refname);
        newitem->kind = kind;
        newitem->commit = commit;
-       newitem->len = strlen(refname);
+       newitem->width = utf8_strwidth(refname);
        newitem->dest = resolve_symref(orig_refname, prefix);
        /* adjust for "remotes/" */
        if (newitem->kind == REF_REMOTE_BRANCH &&
            ref_list->kinds != REF_REMOTE_BRANCH)
-               newitem->len += 8;
-       if (newitem->len > ref_list->maxwidth)
-               ref_list->maxwidth = newitem->len;
+               newitem->width += 8;
+       if (newitem->width > ref_list->maxwidth)
+               ref_list->maxwidth = newitem->width;
 
        return 0;
 }
@@ -384,6 +392,7 @@ static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
                int show_upstream_ref)
 {
        int ours, theirs;
+       char *ref = NULL;
        struct branch *branch = branch_get(branch_name);
 
        if (!stat_tracking_info(branch, &ours, &theirs)) {
@@ -394,16 +403,29 @@ static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
                return;
        }
 
-       strbuf_addch(stat, '[');
        if (show_upstream_ref)
-               strbuf_addf(stat, "%s: ",
-                       shorten_unambiguous_ref(branch->merge[0]->dst, 0));
-       if (!ours)
-               strbuf_addf(stat, _("behind %d] "), theirs);
-       else if (!theirs)
-               strbuf_addf(stat, _("ahead %d] "), ours);
-       else
-               strbuf_addf(stat, _("ahead %d, behind %d] "), ours, theirs);
+               ref = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
+       if (!ours) {
+               if (ref)
+                       strbuf_addf(stat, _("[%s: behind %d]"), ref, theirs);
+               else
+                       strbuf_addf(stat, _("[behind %d]"), theirs);
+
+       } else if (!theirs) {
+               if (ref)
+                       strbuf_addf(stat, _("[%s: ahead %d]"), ref, ours);
+               else
+                       strbuf_addf(stat, _("[ahead %d]"), ours);
+       } else {
+               if (ref)
+                       strbuf_addf(stat, _("[%s: ahead %d, behind %d]"),
+                                   ref, ours, theirs);
+               else
+                       strbuf_addf(stat, _("[ahead %d, behind %d]"),
+                                   ours, theirs);
+       }
+       strbuf_addch(stat, ' ');
+       free(ref);
 }
 
 static int matches_merge_filter(struct commit *commit)
@@ -469,11 +491,12 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
        }
 
        strbuf_addf(&name, "%s%s", prefix, item->name);
-       if (verbose)
+       if (verbose) {
+               int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf);
                strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
-                           maxwidth, name.buf,
+                           maxwidth + utf8_compensation, name.buf,
                            branch_get_color(BRANCH_COLOR_RESET));
-       else
+       else
                strbuf_addf(&out, "%c %s%s%s", c, branch_get_color(color),
                            name.buf, branch_get_color(BRANCH_COLOR_RESET));
 
@@ -482,7 +505,12 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
        else if (verbose)
                /* " f7c0c00 [ahead 58, behind 197] vcs-svn: drop obj_pool.h" */
                add_verbose_info(&out, item, verbose, abbrev);
-       printf("%s\n", out.buf);
+       if (column_active(colopts)) {
+               assert(!verbose && "--column and --verbose are incompatible");
+               string_list_append(&output, out.buf);
+       } else {
+               printf("%s\n", out.buf);
+       }
        strbuf_release(&name);
        strbuf_release(&out);
 }
@@ -493,8 +521,8 @@ static int calc_maxwidth(struct ref_list *refs)
        for (i = 0; i < refs->index; i++) {
                if (!matches_merge_filter(refs->list[i].commit))
                        continue;
-               if (refs->list[i].len > w)
-                       w = refs->list[i].len;
+               if (refs->list[i].width > w)
+                       w = refs->list[i].width;
        }
        return w;
 }
@@ -507,12 +535,12 @@ static void show_detached(struct ref_list *ref_list)
        if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
                struct ref_item item;
                item.name = xstrdup(_("(no branch)"));
-               item.len = strlen(item.name);
+               item.width = utf8_strwidth(item.name);
                item.kind = REF_LOCAL_BRANCH;
                item.dest = NULL;
                item.commit = head_commit;
-               if (item.len > ref_list->maxwidth)
-                       ref_list->maxwidth = item.len;
+               if (item.width > ref_list->maxwidth)
+                       ref_list->maxwidth = item.width;
                print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
                free(item.name);
        }
@@ -692,55 +720,56 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
        struct commit_list *with_commit = NULL;
 
        struct option options[] = {
-               OPT_GROUP("Generic options"),
+               OPT_GROUP(N_("Generic options")),
                OPT__VERBOSE(&verbose,
-                       "show hash and subject, give twice for upstream branch"),
-               OPT__QUIET(&quiet, "suppress informational messages"),
-               OPT_SET_INT('t', "track",  &track, "set up tracking mode (see git-pull(1))",
+                       N_("show hash and subject, give twice for upstream branch")),
+               OPT__QUIET(&quiet, N_("suppress informational messages")),
+               OPT_SET_INT('t', "track",  &track, N_("set up tracking mode (see git-pull(1))"),
                        BRANCH_TRACK_EXPLICIT),
-               OPT_SET_INT( 0, "set-upstream",  &track, "change upstream info",
+               OPT_SET_INT( 0, "set-upstream",  &track, N_("change upstream info"),
                        BRANCH_TRACK_OVERRIDE),
-               OPT__COLOR(&branch_use_color, "use colored output"),
-               OPT_SET_INT('r', "remotes",     &kinds, "act on remote-tracking branches",
+               OPT__COLOR(&branch_use_color, N_("use colored output")),
+               OPT_SET_INT('r', "remotes",     &kinds, N_("act on remote-tracking branches"),
                        REF_REMOTE_BRANCH),
                {
-                       OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
-                       "print only branches that contain the commit",
+                       OPTION_CALLBACK, 0, "contains", &with_commit, N_("commit"),
+                       N_("print only branches that contain the commit"),
                        PARSE_OPT_LASTARG_DEFAULT,
                        parse_opt_with_commit, (intptr_t)"HEAD",
                },
                {
-                       OPTION_CALLBACK, 0, "with", &with_commit, "commit",
-                       "print only branches that contain the commit",
+                       OPTION_CALLBACK, 0, "with", &with_commit, N_("commit"),
+                       N_("print only branches that contain the commit"),
                        PARSE_OPT_HIDDEN | PARSE_OPT_LASTARG_DEFAULT,
                        parse_opt_with_commit, (intptr_t) "HEAD",
                },
                OPT__ABBREV(&abbrev),
 
-               OPT_GROUP("Specific git-branch actions:"),
-               OPT_SET_INT('a', "all", &kinds, "list both remote-tracking and local branches",
+               OPT_GROUP(N_("Specific git-branch actions:")),
+               OPT_SET_INT('a', "all", &kinds, N_("list both remote-tracking and local branches"),
                        REF_REMOTE_BRANCH | REF_LOCAL_BRANCH),
-               OPT_BIT('d', "delete", &delete, "delete fully merged branch", 1),
-               OPT_BIT('D', NULL, &delete, "delete branch (even if not merged)", 2),
-               OPT_BIT('m', "move", &rename, "move/rename a branch and its reflog", 1),
-               OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2),
-               OPT_BOOLEAN(0, "list", &list, "list branch names"),
-               OPT_BOOLEAN('l', "create-reflog", &reflog, "create the branch's reflog"),
+               OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1),
+               OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
+               OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
+               OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
+               OPT_BOOLEAN(0, "list", &list, N_("list branch names")),
+               OPT_BOOLEAN('l', "create-reflog", &reflog, N_("create the branch's reflog")),
                OPT_BOOLEAN(0, "edit-description", &edit_description,
-                           "edit the description for the branch"),
-               OPT__FORCE(&force_create, "force creation (when already exists)"),
+                           N_("edit the description for the branch")),
+               OPT__FORCE(&force_create, N_("force creation (when already exists)")),
                {
                        OPTION_CALLBACK, 0, "no-merged", &merge_filter_ref,
-                       "commit", "print only not merged branches",
+                       N_("commit"), N_("print only not merged branches"),
                        PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
                        opt_parse_merge_filter, (intptr_t) "HEAD",
                },
                {
                        OPTION_CALLBACK, 0, "merged", &merge_filter_ref,
-                       "commit", "print only merged branches",
+                       N_("commit"), N_("print only merged branches"),
                        PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
                        opt_parse_merge_filter, (intptr_t) "HEAD",
                },
+               OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")),
                OPT_END(),
        };
 
@@ -763,6 +792,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
        }
        hashcpy(merge_filter_ref, head_sha1);
 
+
        argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
                             0);
 
@@ -774,12 +804,22 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
        if (abbrev == -1)
                abbrev = DEFAULT_ABBREV;
+       finalize_colopts(&colopts, -1);
+       if (verbose) {
+               if (explicitly_enable_column(colopts))
+                       die(_("--column and --verbose are incompatible"));
+               colopts = 0;
+       }
 
        if (delete)
                return delete_branches(argc, argv, delete > 1, kinds, quiet);
-       else if (list)
-               return print_ref_list(kinds, detached, verbose, abbrev,
-                                     with_commit, argv);
+       else if (list) {
+               int ret = print_ref_list(kinds, detached, verbose, abbrev,
+                                        with_commit, argv);
+               print_columns(&output, colopts, NULL);
+               string_list_clear(&output, 0);
+               return ret;
+       }
        else if (edit_description) {
                const char *branch_name;
                struct strbuf branch_ref = STRBUF_INIT;