# [PATCH] Add git-branches-script

7 messages from 2005-08-15 to 2005-08-17. Participants: Amos Waterland, Kalle Valo, Junio C Hamano, Miguel Bazdresch.
Thread: https://gitlist.dev/t/1526

## Amos Waterland, 2005-08-15 20:44

Subject: [PATCH] Add git-branches-script
Message-ID: <20050815204430.GA77829@rossby.metr.ou.edu>
URL: https://gitlist.dev/e/20050815204430.GA77829%40rossby.metr.ou.edu

```
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
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, 2005-08-16 17:53

Subject: Re: [PATCH] Add git-branches-script
Message-ID: <87hddplqdu.fsf@litku.valo.iki.fi>
URL: https://gitlist.dev/e/87hddplqdu.fsf%40litku.valo.iki.fi
In-Reply-To: <20050815204430.GA77829@rossby.metr.ou.edu>

```
Amos Waterland <apw@rossby.metr.ou.edu> writes:

> 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, 2005-08-16 17:58

Subject: [PATCH] Change git-branch to list branches
Message-ID: <87d5odlq6l.fsf_-_@litku.valo.iki.fi>
URL: https://gitlist.dev/e/87d5odlq6l.fsf_-_%40litku.valo.iki.fi
In-Reply-To: <87hddplqdu.fsf@litku.valo.iki.fi>

```
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
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, 2005-08-16 21:08

Subject: Re: [PATCH] Change git-branch to list branches
Message-ID: <7vslx98ua0.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vslx98ua0.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <87d5odlq6l.fsf_-_@litku.valo.iki.fi>

```
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.

> +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, 2005-08-17 19:48

Subject: Re: [PATCH] Change git-branch to list branches
Message-ID: <87br3w73at.fsf@litku.valo.iki.fi>
URL: https://gitlist.dev/e/87br3w73at.fsf%40litku.valo.iki.fi
In-Reply-To: <7vslx98ua0.fsf@assigned-by-dhcp.cox.net>

```
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, 2005-08-17 20:15

Subject: Re: [PATCH] Change git-branch to list branches
Message-ID: <871x4s7221.fsf@litku.valo.iki.fi>
URL: https://gitlist.dev/e/871x4s7221.fsf%40litku.valo.iki.fi
In-Reply-To: <87br3w73at.fsf@litku.valo.iki.fi>

```
Kalle Valo <Kalle.Valo@iki.fi> writes:

> 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, 2005-08-17 20:37

Subject: Re: [PATCH] Add git-branches-script
Message-ID: <20050817203758.GA1332@localhost.domain.invalid>
URL: https://gitlist.dev/e/20050817203758.GA1332%40localhost.domain.invalid
In-Reply-To: <20050815204430.GA77829@rossby.metr.ou.edu>

```
* Amos Waterland <apw@rossby.metr.ou.edu> [2005-08-16 00:48]:
> 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/

```
