git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v7 06/9] branch: add --column

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Mar 13, 2012, 12:09 UTC
Message-ID
<dc618ca926bdbdbb96ae59615cbbe57cde9f763f.1331638663.git.pclouds@gmail.com>
In-Reply-To
<dfa792c463da82097de69b1fba5dc54147dd1951.1331638663.git.pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/config.txt     |    4 ++
 Documentation/git-branch.txt |    9 +++++
 Makefile                     |    2 +-
 builtin/branch.c             |   32 +++++++++++++++--
 t/t3200-branch.sh            |   77 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 119 insertions(+), 5 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index aacf8ab..31878b7 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -847,6 +847,10 @@ column.ui::
 +
 	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::
 	A boolean to enable/disable inclusion of status information in the
 	commit message template when using an editor to prepare the commit
diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 0427e80..ba5cccb 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -10,6 +10,7 @@ SYNOPSIS
 [verse]
 'git branch' [--color[=<when>] | --no-color] [-r | -a]
 	[--list] [-v [--abbrev=<length> | --no-abbrev]]
+	[--column[=<options>] | --no-column]
 	[(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -107,6 +108,14 @@ OPTIONS
 	default to color output.
 	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::
 --remotes::
 	List or delete (if used with -d) the remote-tracking branches.
diff --git a/Makefile b/Makefile
index 0998f0d..320d3f8 100644
--- a/Makefile
+++ b/Makefile
@@ -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/tar-tree.o archive-tar.o: tar.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.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h
 
diff --git a/builtin/branch.c b/builtin/branch.c
index cb17bc3..2da0168 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -15,6 +15,8 @@
 #include "branch.h"
 #include "diff.h"
 #include "revision.h"
+#include "string-list.h"
+#include "column.h"
 
 static const char * const builtin_branch_usage[] = {
 	"git branch [options] [-r | -a] [--merged | --no-merged]",
@@ -53,6 +55,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 +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)
 {
+	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;
@@ -474,7 +481,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 (COL_ENABLE(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);
 }
@@ -727,6 +739,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
 			opt_parse_merge_filter, (intptr_t) "HEAD",
 		},
+		OPT_COLUMN(0, "column", &colopts, "list branches in columns"),
 		OPT_END(),
 	};
 
@@ -749,6 +762,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);
 
@@ -760,12 +774,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);
-	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;
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index dd1aceb..9f82d5e 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -160,6 +160,83 @@ test_expect_success 'git branch --list -d t should fail' '
 	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
 
 test_expect_success 'git branch -m q q2 without config should succeed' '
-- 
1.7.8.36.g69ee2
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 57 of 66 in “Column display”
  1. 00/11 Column displayNguyễn Thái Ngọc Duy, Feb 25, 2012
  2. 01/11 column: add API to print items in columnsNguyễn Thái Ngọc Duy, Feb 25, 2012
  3. 02/11 Add git-column and column mode parsingNguyễn Thái Ngọc Duy, Feb 25, 2012
  4. Ramsay JonesFeb 27, 2012
  5. Nguyen Thai Ngoc DuyFeb 28, 2012
  6. 03/11 Stop starting pager recursivelyNguyễn Thái Ngọc Duy, Feb 25, 2012
  7. 04/11 column: add columnar layoutNguyễn Thái Ngọc Duy, Feb 25, 2012
  8. 05/11 column: support columns with different widthsNguyễn Thái Ngọc Duy, Feb 25, 2012
  9. Junio C HamanoFeb 26, 2012
  10. 06/11 column: add column.ui for default column output settingsNguyễn Thái Ngọc Duy, Feb 25, 2012
  11. Junio C HamanoFeb 27, 2012
  12. Nguyen Thai Ngoc DuyFeb 27, 2012
  13. 07/11 help: reuse print_columns() for help -aNguyễn Thái Ngọc Duy, Feb 25, 2012
  14. 08/11 branch: add --columnNguyễn Thái Ngọc Duy, Feb 25, 2012
  15. 09/11 status: add --columnNguyễn Thái Ngọc Duy, Feb 25, 2012
  16. 10/11 column: support piping stdout to external git-column processNguyễn Thái Ngọc Duy, Feb 25, 2012
  17. 11/11 tag: add --columnNguyễn Thái Ngọc Duy, Feb 25, 2012
  18. Junio C HamanoFeb 26, 2012
  19. Nguyen Thai Ngoc DuyFeb 27, 2012
  20. Junio C HamanoFeb 27, 2012
  21. Junio C HamanoFeb 27, 2012
  22. Nguyen Thai Ngoc DuyFeb 27, 2012
  23. 00/10 Column displayNguyễn Thái Ngọc Duy, Feb 28, 2012
  24. 01/10 Add git-column for columnar displayNguyễn Thái Ngọc Duy, Feb 28, 2012
  25. Junio C HamanoFeb 28, 2012
  26. Nguyen Thai Ngoc DuyMar 2, 2012
  27. 02/10 Stop starting pager recursivelyNguyễn Thái Ngọc Duy, Feb 28, 2012
  28. Junio C HamanoFeb 28, 2012
  29. Junio C HamanoFeb 28, 2012
  30. Nguyen Thai Ngoc DuyFeb 29, 2012
  31. Junio C HamanoFeb 29, 2012
  32. Nguyen Thai Ngoc DuyFeb 29, 2012
  33. Junio C HamanoFeb 29, 2012
  34. 03/10 column: add columnar layoutNguyễn Thái Ngọc Duy, Feb 28, 2012
  35. Junio C HamanoFeb 28, 2012
  36. 04/10 column: add dense layout supportNguyễn Thái Ngọc Duy, Feb 28, 2012
  37. Junio C HamanoFeb 28, 2012
  38. Nguyen Thai Ngoc DuyMar 2, 2012
  39. Junio C HamanoMar 2, 2012
  40. 05/10 column: add column.ui for default column output settingsNguyễn Thái Ngọc Duy, Feb 28, 2012
  41. Junio C HamanoFeb 28, 2012
  42. 06/10 help: reuse print_columns() for help -aNguyễn Thái Ngọc Duy, Feb 28, 2012
  43. 07/10 branch: add --columnNguyễn Thái Ngọc Duy, Feb 28, 2012
  44. 08/10 status: add --columnNguyễn Thái Ngọc Duy, Feb 28, 2012
  45. 09/10 column: support piping stdout to external git-column processNguyễn Thái Ngọc Duy, Feb 28, 2012
  46. 10/10 tag: add --columnNguyễn Thái Ngọc Duy, Feb 28, 2012
  47. Thomas RastMar 2, 2012
  48. Nguyen Thai Ngoc DuyMar 11, 2012
  49. Junio C HamanoMar 12, 2012
  50. 00/9 Column displayNguyễn Thái Ngọc Duy, Mar 13, 2012
  51. Nguyen Thai Ngoc DuyMar 13, 2012
  52. 01/9 Add column layout skeleton and git-columnNguyễn Thái Ngọc Duy, Mar 13, 2012
  53. 02/9 Stop starting pager recursivelyNguyễn Thái Ngọc Duy, Mar 13, 2012
  54. 03/9 column: add columnar layoutNguyễn Thái Ngọc Duy, Mar 13, 2012
  55. 04/9 column: add dense layout supportNguyễn Thái Ngọc Duy, Mar 13, 2012
  56. 05/9 help: reuse print_columns() for help -aNguyễn Thái Ngọc Duy, Mar 13, 2012
  57. 06/9 branch: add --columnNguyễn Thái Ngọc Duy, Mar 13, 2012
  58. 07/9 status: add --columnNguyễn Thái Ngọc Duy, Mar 13, 2012
  59. 08/9 column: support piping stdout to external git-column processNguyễn Thái Ngọc Duy, Mar 13, 2012
  60. 09/9 tag: add --columnNguyễn Thái Ngọc Duy, Mar 13, 2012
  61. 10/9 ls-files: support --columnNguyễn Thái Ngọc Duy, Mar 13, 2012
  62. 11/9 column: support "denser" modeNguyễn Thái Ngọc Duy, Mar 13, 2012
  63. 12/9 column: support grouping entriesNguyễn Thái Ngọc Duy, Mar 13, 2012
  64. Junio C HamanoMar 13, 2012
  65. Nguyen Thai Ngoc DuyMar 14, 2012
  66. Junio C HamanoMar 14, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.