threads / patch / 1526

patchAdd git-branches-script

Subject: [PATCH] Add git-branches-script

## tl;dr

7 messages between Aug 15, 2005 and Aug 17, 2005. Diffs are folded; open one to read it.

replies: 6people: 4as markdown or json

Amos Waterland· Aug 15, 2005, 20:44 UTC · lore

For people whose workflow involves switching back and forth between a lot of branches, it can be really helpful to be able to quickly tell which branch you are on and which ones are available. This patch introduces a small script that when invoked as `git branches' prints a list of available branches with a star in front of the one you are on:

 $ cd linux-2.6/
 $ git checkout -b ppc64-cleanups
 $ git checkout -b ppc64-new-devel
 $ git checkout -b ppc64-all-merge
 $ git branches
   master
 * ppc64-all-merge
   ppc64-cleanups
   ppc64-new-devel
Signed-off-by: Amos Waterland <apw@rossby.metr.ou.edu>
---
 git-branches-script |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
 create mode 100755 git-branches-script
12ab86f36137c4ffd1fb9b878479b9befe4cf2d4
Show changes to git-branches-script +8 −0
diff --git a/git-branches-script b/git-branches-script
new file mode 100755
--- /dev/null
+++ b/git-branches-script
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+. git-sh-setup-script || die "Not a git archive"
+
+current=$(basename $(readlink $GIT_DIR/HEAD))
+
+cd $GIT_DIR/refs/heads &&
+ls | sed -e "s/^/  /" -e "s/  $current/* $current/"
Kalle Valo· Aug 16, 2005, 17:53 UTC · re: Amos Waterland · lore

Re: [PATCH] Add git-branches-script

Amos Waterland <apw@rossby.metr.ou.edu> writes:
Show 15 quoted lines
> For people whose workflow involves switching back and forth between a
> lot of branches, it can be really helpful to be able to quickly tell
> which branch you are on and which ones are available.  This patch
> introduces a small script that when invoked as `git branches' prints a
> list of available branches with a star in front of the one you are on:
>
>  $ cd linux-2.6/
>  $ git checkout -b ppc64-cleanups
>  $ git checkout -b ppc64-new-devel
>  $ git checkout -b ppc64-all-merge
>  $ git branches
>    master
>  * ppc64-all-merge
>    ppc64-cleanups
>    ppc64-new-devel

I would prefer to using some git command compared to 'ls -l .git/refs/heads/'. In my opinion there is a need for this command and it should be included.

But my immediate concern was that there is already 'git branch' command and 'git branches' is too similar. I think they should be merged. I have a patch coming up.

-- 
Kalle Valo
Kalle Valo· Aug 16, 2005, 17:58 UTC · re: Kalle Valo · lore

[PATCH] Change git-branch to list branches

If no argument provided to `git branch`, show available branches and mark current branch with star.

This is based on patch written by Amos Waterland <apw@rossby.metr.ou.edu>.
Signed-off-by: Kalle Valo <Kalle.Valo@iki.fi>
---
 git-branch-script |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
38aca3bb4ad4e6b0e10e295a87a9f5ce37ff5be4
Show changes to git-branch-script +7 −1
diff --git a/git-branch-script b/git-branch-script
--- a/git-branch-script
+++ b/git-branch-script
@@ -11,7 +11,13 @@ case "$2" in
 esac
 rev=$(git-rev-parse --revs-only --verify "$head") || exit
 
-[ -z "$branchname" ] && die "git branch: I want a branch name"
+if [ -z "$branchname" ]; then
+    current=$(basename $(readlink $GIT_DIR/HEAD))
+    cd $GIT_DIR/refs/heads &&
+    ls | sed -e "s/^/  /" -e "s/  $current/* $current/"
+    exit
+fi
+
 [ -e "$GIT_DIR/refs/heads/$branchname" ] && die "$branchname already exists"
 
 echo $rev > "$GIT_DIR/refs/heads/$branchname"
Junio C Hamano· Aug 16, 2005, 21:08 UTC · re: Kalle Valo · lore

Re: [PATCH] Change git-branch to list branches

Kalle Valo <Kalle.Valo@iki.fi> writes:
> If no argument provided to `git branch`, show available branches and
> mark current branch with star.

I like the general direction, but this particular implementation may be a bit troublesome.

Show 6 quoted lines
> +if [ -z "$branchname" ]; then
> +    current=$(basename $(readlink $GIT_DIR/HEAD))
> +    cd $GIT_DIR/refs/heads &&
> +    ls | sed -e "s/^/  /" -e "s/  $current/* $current/"
> +    exit
> +fi

I do not think we have agreed to limit ourselves to a flat namespace under refs/heads without subdirectories. Something like what git-show-branches-script does when $# == 0, perhaps?

Kalle Valo· Aug 17, 2005, 19:48 UTC · re: Junio C Hamano · lore

Re: [PATCH] Change git-branch to list branches

Junio C Hamano <junkio@cox.net> writes:
> I do not think we have agreed to limit ourselves to a flat
> namespace under refs/heads without subdirectories.  Something
> like what git-show-branches-script does when $# == 0, perhaps?
I didn't realise this. I'll send a revised patch soon.
-- 
Kalle Valo
Kalle Valo· Aug 17, 2005, 20:15 UTC · re: Kalle Valo · lore

Re: [PATCH] Change git-branch to list branches

Kalle Valo <Kalle.Valo@iki.fi> writes:
Show 7 quoted lines
> Junio C Hamano <junkio@cox.net> writes:
>
>> I do not think we have agreed to limit ourselves to a flat
>> namespace under refs/heads without subdirectories.  Something
>> like what git-show-branches-script does when $# == 0, perhaps?
>
> I didn't realise this. I'll send a revised patch soon.
Ah, but you fixed it already. Thanks!
-- 
Kalle Valo
Miguel Bazdresch· Aug 17, 2005, 20:37 UTC · re: Amos Waterland · lore

Re: [PATCH] Add git-branches-script

* Amos Waterland <apw@rossby.metr.ou.edu> [2005-08-16 00:48]:
Show 15 quoted lines
> For people whose workflow involves switching back and forth between a
> lot of branches, it can be really helpful to be able to quickly tell
> which branch you are on and which ones are available.  This patch
> introduces a small script that when invoked as `git branches' prints a
> list of available branches with a star in front of the one you are on:
> 
>  $ cd linux-2.6/
>  $ git checkout -b ppc64-cleanups
>  $ git checkout -b ppc64-new-devel
>  $ git checkout -b ppc64-all-merge
>  $ git branches
>    master
>  * ppc64-all-merge
>    ppc64-cleanups
>    ppc64-new-devel

Some might find it useful to put Amos' script in a bash function and then put the current branch in the prompt. What I did was to put this function in my .bashrc:

gitbranch () {
	. git-sh-setup-script &&
	branch=$(basename $(readlink $GIT_DIR/HEAD)) &&
	echo -n "" $branch ""
}
Then I modified my bash prompt:
export PS1='\[\033[1;31m\]\j$(gitbranch)[\w]\$ \[\033[0m\]'
                            ^^^^^^^^^^^

Now, if my current dir is not a git repo, I have my regular prompt. As soon as I cd into a git repo, I get the current branch in the prompt, as follows:

0[~]$ cd gitrepo 0 master [~/gitrepo]$ git checkout branch1 0 branch1 [~/gitrepo]$ cd .. 0[~]$

-- 
Miguel Bazdresch
http://thewizardstower.org/

← back to recent threads