branch: add --column

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Nguyễn Thái Ngọc Duy
2012-04-13 17:54:38 +07:00
committed by Junio C Hamano
parent dbfae68969
commit ebe31ef2ed
5 changed files with 119 additions and 5 deletions

View File

@ -862,6 +862,10 @@ column.ui::
+ +
This option defaults to 'never'. This option defaults to 'never'.
column.branch::
Specify whether to output branch listing in `git branch` in columns.
See `column.ui` for details.
commit.status:: commit.status::
A boolean to enable/disable inclusion of status information in the A boolean to enable/disable inclusion of status information in the
commit message template when using an editor to prepare the commit commit message template when using an editor to prepare the commit

View File

@ -10,6 +10,7 @@ SYNOPSIS
[verse] [verse]
'git branch' [--color[=<when>] | --no-color] [-r | -a] 'git branch' [--color[=<when>] | --no-color] [-r | -a]
[--list] [-v [--abbrev=<length> | --no-abbrev]] [--list] [-v [--abbrev=<length> | --no-abbrev]]
[--column[=<options>] | --no-column]
[(--merged | --no-merged | --contains) [<commit>]] [<pattern>...] [(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>] 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
'git branch' (-m | -M) [<oldbranch>] <newbranch> 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@ -107,6 +108,14 @@ OPTIONS
default to color output. default to color output.
Same as `--color=never`. Same as `--color=never`.
--column[=<options>]::
--no-column::
Display branch listing in columns. See configuration variable
column.branch for option syntax.`--column` and `--no-column`
without options are equivalent to 'always' and 'never' respectively.
+
This option is only applicable in non-verbose mode.
-r:: -r::
--remotes:: --remotes::
List or delete (if used with -d) the remote-tracking branches. List or delete (if used with -d) the remote-tracking branches.

View File

@ -2168,7 +2168,7 @@ builtin/prune.o builtin/reflog.o reachable.o: reachable.h
builtin/commit.o builtin/revert.o wt-status.o: wt-status.h builtin/commit.o builtin/revert.o wt-status.o: wt-status.h
builtin/tar-tree.o archive-tar.o: tar.h builtin/tar-tree.o archive-tar.o: tar.h
connect.o transport.o url.o http-backend.o: url.h connect.o transport.o url.o http-backend.o: url.h
column.o help.o pager.o: column.h builtin/branch.o column.o help.o pager.o: column.h
http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h
http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h

View File

@ -15,6 +15,8 @@
#include "branch.h" #include "branch.h"
#include "diff.h" #include "diff.h"
#include "revision.h" #include "revision.h"
#include "string-list.h"
#include "column.h"
static const char * const builtin_branch_usage[] = { static const char * const builtin_branch_usage[] = {
"git branch [options] [-r | -a] [--merged | --no-merged]", "git branch [options] [-r | -a] [--merged | --no-merged]",
@ -53,6 +55,9 @@ static enum merge_filter {
} merge_filter; } merge_filter;
static unsigned char merge_filter_ref[20]; 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) static int parse_branch_color_slot(const char *var, int ofs)
{ {
if (!strcasecmp(var+ofs, "plain")) if (!strcasecmp(var+ofs, "plain"))
@ -70,6 +75,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) 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")) { if (!strcmp(var, "color.branch")) {
branch_use_color = git_config_colorbool(var, value); branch_use_color = git_config_colorbool(var, value);
return 0; return 0;
@ -474,7 +481,12 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
else if (verbose) else if (verbose)
/* " f7c0c00 [ahead 58, behind 197] vcs-svn: drop obj_pool.h" */ /* " f7c0c00 [ahead 58, behind 197] vcs-svn: drop obj_pool.h" */
add_verbose_info(&out, item, verbose, abbrev); add_verbose_info(&out, item, verbose, abbrev);
if (column_active(colopts)) {
assert(!verbose && "--column and --verbose are incompatible");
string_list_append(&output, out.buf);
} else {
printf("%s\n", out.buf); printf("%s\n", out.buf);
}
strbuf_release(&name); strbuf_release(&name);
strbuf_release(&out); strbuf_release(&out);
} }
@ -727,6 +739,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
opt_parse_merge_filter, (intptr_t) "HEAD", opt_parse_merge_filter, (intptr_t) "HEAD",
}, },
OPT_COLUMN(0, "column", &colopts, "list branches in columns"),
OPT_END(), OPT_END(),
}; };
@ -749,6 +762,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
} }
hashcpy(merge_filter_ref, head_sha1); hashcpy(merge_filter_ref, head_sha1);
argc = parse_options(argc, argv, prefix, options, builtin_branch_usage, argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
0); 0);
@ -760,12 +774,22 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
if (abbrev == -1) if (abbrev == -1)
abbrev = DEFAULT_ABBREV; abbrev = DEFAULT_ABBREV;
finalize_colopts(&colopts, -1);
if (verbose) {
if (explicitly_enable_column(colopts))
die(_("--column and --verbose are incompatible"));
colopts = 0;
}
if (delete) if (delete)
return delete_branches(argc, argv, delete > 1, kinds); return delete_branches(argc, argv, delete > 1, kinds);
else if (list) else if (list) {
return print_ref_list(kinds, detached, verbose, abbrev, int ret = print_ref_list(kinds, detached, verbose, abbrev,
with_commit, argv); with_commit, argv);
print_columns(&output, colopts, NULL);
string_list_clear(&output, 0);
return ret;
}
else if (edit_description) { else if (edit_description) {
const char *branch_name; const char *branch_name;
struct strbuf branch_ref = STRBUF_INIT; struct strbuf branch_ref = STRBUF_INIT;

View File

@ -160,6 +160,83 @@ test_expect_success 'git branch --list -d t should fail' '
test_path_is_missing .git/refs/heads/t test_path_is_missing .git/refs/heads/t
' '
test_expect_success 'git branch --column' '
COLUMNS=81 git branch --column=column >actual &&
cat >expected <<\EOF &&
a/b/c bam foo l * master n o/p r
abc bar j/k m/m master2 o/o q
EOF
test_cmp expected actual
'
test_expect_success 'git branch --column with an extremely long branch name' '
long=this/is/a/part/of/long/branch/name &&
long=z$long/$long/$long/$long &&
test_when_finished "git branch -d $long" &&
git branch $long &&
COLUMNS=80 git branch --column=column >actual &&
cat >expected <<EOF &&
a/b/c
abc
bam
bar
foo
j/k
l
m/m
* master
master2
n
o/o
o/p
q
r
$long
EOF
test_cmp expected actual
'
test_expect_success 'git branch with column.*' '
git config column.ui column &&
git config column.branch "dense" &&
COLUMNS=80 git branch >actual &&
git config --unset column.branch &&
git config --unset column.ui &&
cat >expected <<\EOF &&
a/b/c bam foo l * master n o/p r
abc bar j/k m/m master2 o/o q
EOF
test_cmp expected actual
'
test_expect_success 'git branch --column -v should fail' '
test_must_fail git branch --column -v
'
test_expect_success 'git branch -v with column.ui ignored' '
git config column.ui column &&
COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
git config --unset column.ui &&
cat >expected <<\EOF &&
a/b/c
abc
bam
bar
foo
j/k
l
m/m
* master
master2
n
o/o
o/p
q
r
EOF
test_cmp expected actual
'
mv .git/config .git/config-saved mv .git/config .git/config-saved
test_expect_success 'git branch -m q q2 without config should succeed' ' test_expect_success 'git branch -m q q2 without config should succeed' '