threads / patch / 5995

patch, 3 partsAdd -v and -w options to git-branch

Subject: [PATCH 0/3] Add -v and -w options to git-branch

## tl;dr

7 messages between Oct 22, 2006 and Oct 22, 2006. Diffs are folded; open one to read it.

replies: 6people: 2as markdown or json

Lars Hjemli· Oct 22, 2006, 11:30 UTC · lore

This patch-series teaches git-branch to show sha1 and first line of commit message for each branch (and is a replacement for my previous patches)

Diffstat:
 Documentation/git-branch.txt |    8 ++++-
 git-branch.sh                |   71 ++++++++++++++++++++++++++++++++---------
 2 files changed, 62 insertions(+), 17 deletions(-)
Lars Hjemli· Oct 22, 2006, 11:30 UTC · re: Lars Hjemli · lore

[PATCH 1/3] Fix usagestring for git-branch

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
---
 git-branch.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Show changes to git-branch.sh +1 −1
diff --git a/git-branch.sh b/git-branch.sh
index 4379a07..b80bcda 100755
--- a/git-branch.sh
+++ b/git-branch.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-USAGE='[-l] [(-d | -D) <branchname>] | [[-f] <branchname> [<start-point>]] | -r'
+USAGE=' [-l] [-f] <branchname> [<start-point>] | (-d | -D) <branchname> | [-r]'
 LONG_USAGE='If no arguments, show available branches and mark current branch with a star.
 If one argument, create a new branch <branchname> based off of current HEAD.
 If two arguments, create a new branch <branchname> based off of <start-point>.'
-- 
1.4.3.1.g1688
Lars Hjemli· Oct 22, 2006, 11:30 UTC · re: Lars Hjemli · lore

[PATCH 2/3] Refactor git-branch

This moves the code used to display local branches into a separate function.

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
---
 git-branch.sh |   28 ++++++++++++++++------------
 1 files changed, 16 insertions(+), 12 deletions(-)
Show changes to git-branch.sh +16 −12
diff --git a/git-branch.sh b/git-branch.sh
index b80bcda..1f628a4 100755
--- a/git-branch.sh
+++ b/git-branch.sh
@@ -54,6 +54,21 @@ ls_remote_branches () {
     sort
 }
 
+ls_local_branches () {
+	git-rev-parse --symbolic --branches |
+	sort |
+	while read ref
+	do
+		if test "$headref" = "$ref"
+		then
+			pfx='*'
+		else
+			pfx=' '
+		fi
+		echo "$pfx $ref"
+	done
+}
+
 force=
 create_log=
 while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
@@ -86,18 +101,7 @@ done
 
 case "$#" in
 0)
-	git-rev-parse --symbolic --branches |
-	sort |
-	while read ref
-	do
-		if test "$headref" = "$ref"
-		then
-			pfx='*'
-		else
-			pfx=' '
-		fi
-		echo "$pfx $ref"
-	done
+	ls_local_branches
 	exit 0 ;;
 1)
 	head=HEAD ;;
-- 
1.4.3.1.g1688
Lars Hjemli· Oct 22, 2006, 11:30 UTC · re: Lars Hjemli · lore

[PATCH 3/3] Teach git-branch -v and -w options

This makes git-branch display sha1 and first line of commit message for each branch.

Additionaly, the -w option may be used to specify columnwidth for branchname (default is 20 characters)

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
---
 Documentation/git-branch.txt |    8 ++++++-
 git-branch.sh                |   47 ++++++++++++++++++++++++++++++++++++-----
 2 files changed, 48 insertions(+), 7 deletions(-)
Show changes to 2 files +48 −7

Documentation/git-branch.txt, git-branch.sh

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index d43ef1d..efbab61 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -8,7 +8,7 @@ git-branch - List, create, or delete bra
 SYNOPSIS
 --------
 [verse]
-'git-branch' [-r]
+'git-branch' [-r] [-v [-w width]]
 'git-branch' [-l] [-f] <branchname> [<start-point>]
 'git-branch' (-d | -D) <branchname>...
 
@@ -47,6 +47,12 @@ OPTIONS
 -r::
 	List only the "remote" branches.
 
+-v::
+	Show sha1 and first line of commit message for each branch
+
+-w <width>::
+	Set columnwidth for branchname display
+
 <branchname>::
 	The name of the branch to create or delete.
 	The new branch name must pass all checks defined by
diff --git a/git-branch.sh b/git-branch.sh
index 1f628a4..73839b4 100755
--- a/git-branch.sh
+++ b/git-branch.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-USAGE=' [-l] [-f] <branchname> [<start-point>] | (-d | -D) <branchname> | [-r]'
+USAGE=' [-l] [-f] <branchname> [<start-point>] | (-d | -D) <branchname> | [-r] [-v [-w width]]'
 LONG_USAGE='If no arguments, show available branches and mark current branch with a star.
 If one argument, create a new branch <branchname> based off of current HEAD.
 If two arguments, create a new branch <branchname> based off of <start-point>.'
@@ -49,12 +49,26 @@ If you are sure you want to delete it, r
 }
 
 ls_remote_branches () {
+	verbose="$1"
+	width="$2"
     git-rev-parse --symbolic --all |
     sed -ne 's|^refs/\(remotes/\)|\1|p' |
-    sort
+    sort |
+    while read ref
+    do
+		if test "$verbose" = "yes"
+		then
+			log=$(git-log --pretty=oneline --max-count=1 "$ref")
+			printf "%-*s %s\n" "$width" "$ref" "$log"
+		else
+			echo "$ref"
+		fi
+	done
 }
 
 ls_local_branches () {
+	verbose="$1"
+	width="$2"
 	git-rev-parse --symbolic --branches |
 	sort |
 	while read ref
@@ -65,12 +79,22 @@ ls_local_branches () {
 		else
 			pfx=' '
 		fi
-		echo "$pfx $ref"
+		if test "$verbose" = "yes"
+		then
+			log=$(git-log --pretty=oneline --max-count=1 "$ref")
+			printf "%s %-*s %s\n" "$pfx" "$width" "$ref" "$log"
+		else
+			echo "$pfx $ref"
+		fi
 	done
 }
 
 force=
 create_log=
+remotes=
+verbose=
+width=20
+
 while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
 do
 	case "$1" in
@@ -79,8 +103,14 @@ do
 		exit
 		;;
 	-r)
-		ls_remote_branches
-		exit
+		remotes="yes"
+		;;
+	-v)
+		verbose="yes"
+		;;
+	-w)
+		shift
+		width="$1"
 		;;
 	-f)
 		force="$1"
@@ -101,7 +131,12 @@ done
 
 case "$#" in
 0)
-	ls_local_branches
+	if test "$remotes" = "yes"
+	then
+		ls_remote_branches "$verbose" "$width"
+	else
+		ls_local_branches "$verbose" "$width"
+	fi
 	exit 0 ;;
 1)
 	head=HEAD ;;
-- 
1.4.3.1.g1688
Junio C Hamano· Oct 22, 2006, 19:35 UTC · re: Lars Hjemli · lore

Re: [PATCH 3/3] Teach git-branch -v and -w options

Lars Hjemli <hjemli@gmail.com> writes:
Show 7 quoted lines
> This makes git-branch display sha1 and first line of commit
> message for each branch.
>
> Additionaly, the -w option may be used to specify columnwidth
> for branchname (default is 20 characters)
>
> Signed-off-by: Lars Hjemli <hjemli@gmail.com>

If you are going in this direction, probably you would want to refactor 2/3 a bit differently, so that you do not have to duplicate the same printf for local and remote cases?

Lars Hjemli· Oct 22, 2006, 19:55 UTC · re: Junio C Hamano · lore

Re: [PATCH 3/3] Teach git-branch -v and -w options

On 10/22/06, Junio C Hamano <junkio@cox.net> wrote:
> If you are going in this direction, probably you would want to
> refactor 2/3 a bit differently, so that you do not have to
> duplicate the same printf for local and remote cases?
>

I actually did, but then abandoned it since it would change the output for remote branches (two spaces indent). I didn't want to cause any regressions :-)

But if that's ok, my abandoned patch contained this:
Show changes to diff +36 −21
@@ -48,14 +48,35 @@ If you are sure you want to delete it, r
    exit 0
 }

-ls_remote_branches () {
-    git-rev-parse --symbolic --all |
-    sed -ne 's|^refs/\(remotes/\)|\1|p' |
-    sort
-}
-+width=20
+sedmatch="^refs/heads/"
+sedsubst=
+verbose=
 force=
 create_log=
+
+ls_refs () {
+       git-rev-parse --symbolic --all |
+       sed -ne "s|$sedmatch|$sedsubst|p" |
+       sort |
+       while read ref
+       do
+               if test "$headref" = "$ref"
+               then
+                       pfx='*'
+               else
+                       pfx=' '
+               fi
+               if test "$verbose" = "yes"
+               then
+                       log=$(git-log --max-count=1 --pretty=oneline $ref)
+                       printf "%s %-*s %s\n" "$pfx" "$width" "$ref" "$log"
+               else
+                       echo "$pfx $ref"
+               fi
+       done
+}
+
 while case "$#,$1" in 0,*) break ;; *,-*) ;; *) break ;; esac
 do
       case "$1" in
@@ -64,8 +85,8 @@ do
               exit
               ;;
       -r)
-               ls_remote_branches
-               exit
+               sedmatch="^refs/\(remotes/\)"
+               sedsubst="\1"
               ;;
       -f)
               force="$1"
@@ -73,6 +94,13 @@ do
       -l)
               create_log="yes"
               ;;
+       -v)
+               verbose="yes"
+               ;;
+       -w)
+               shift
+               width=$1
+               ;;
       --)
               shift
               break
@@ -86,18 +114,7 @@ done

 case "$#" in
 0)
-       git-rev-parse --symbolic --branches |
-       sort |
-       while read ref
-       do
-               if test "$headref" = "$ref"
-               then
-                       pfx='*'
-               else
-                       pfx=' '
-               fi
-               echo "$pfx $ref"
-       done
+       ls_refs
       exit 0 ;;
 1)
       head=HEAD ;;


And then I added -a and -t (for "all" and "tags", obviously), and felt
I went over the top :-)

Btw: in the meantime, Kristian Høgsberg pointed me to an earlier
thread regarding making git-branch a builtin, which I'm going to look
at tonight.

So many options, so little time :-)

I'm open for suggestions/preferences etc...

--
larsh
Lars Hjemli· Oct 22, 2006, 20:50 UTC · lore

Re: [PATCH 3/3] Teach git-branch -v and -w options

[the gitlist fell off my first reply, so I've put it back...]
On 10/22/06, Junio C Hamano <junkio@cox.net> wrote:
> I was almost going to say that your patch saddened me because
> doing builtin-branch on top of Linus's packed-refs work (which
> means doing the series on top of "next") was part of my plan,
Heh, I don't want to compete with your c-skills :-)
Show 5 quoted lines
> If you are aware of Kristian's patch that is a good news.  I do
> not have much objection to add optional frills to git-branch
> listing output, but I'd rather not see it done in the shell
> implementation.  Doing anything on the current git-branch.sh
> means that would need to be re-done later.

Agreed. I'll try and come up with a builtin instead, and let you worry about more interesting stuff.

-- 
larsh

← back to recent threads