Tuesday, June 5, 2012

git branch-info

I have too many branches. And sometimes I forgot which one is which. So yesterday I sat down and wrote a git alias to tell me:
branch-info = "!sh -c 'git branch --list --no-color | \
    sed -e \"s/*/ /\" | \
    while read branch; do \
    git log -1 --format=format:\"%Cred$branch:%Cblue %s %Cgreen%h%Creset (%ar)\" $branch; \
    done'"
Gives me a nice output like this:
:: whot@yabbi:~/xorg/xserver (for-keith)> git branch-info
dtrace-input-abi:  dix: add dtrace probes to input API  c0b0a9b (3 months ago)
extmod-changes:  DRI2: Remove prototype for DRI2DestroyDrawable  8ba2980 (11 months ago)
fedora-17-branch:  os: make timers signal-safe 7089841 (6 weeks ago)
master:  Xext: include dix-config.h 594b4a4(12 days ago)
mt-devel: Switch to new listener handling for touch events 3ee84fc (6 months ago)
mt-devel2: dix: conditionally update the cursor sprite 9edb3fd(6 months ago)
multitouch: blah 8ecec2e(4 months ago)
...
Or, the same thing aligned in columns, though without colours (column doesn't parse the escape sequences for colors so the columns are misaligned).
branch-info = "!sh -c 'git branch --list --no-color | \
    sed -e \"s/*/ /\" | \
    while read branch; do \
    git log -1 --format=format:\"$branch:|%s|%h|%ar\n\" $branch; done | \
    column -t -s\"|\"'"

4 comments:

  1. I recomment to use
    git --no-pager log -1 ...

    instead, otherwise at least on my setup we end with a lot of extra space between each branch info

    ReplyDelete
  2. I like it! Wanted to sort them by date though.

    This is horrible but works.. :P

    branches-notsorted = "!sh -c 'git branch --list --no-color | \
    sed -e \"s/*/ /\" | \
    while read branch; do \
    git log -1 --format=format:\"%ct %Cred$branch:%Cblue %s %Cgreen%h%Creset (%ar)\n\" $branch; \
    done'"

    branches = "!sh -c 'git branches-notsorted | sort | cut -d\" \" -f2-'"

    ReplyDelete
  3. @Unknown: if I use --no-pager, I need an extra \n at the end of the format string for the colour version. Which, interestingly enough, I need for the non-colour version anyways. I suspect there's some setting somewhere that interferes.

    ReplyDelete
  4. Maybe this is an implied feature request: "git branch -v" should use color and include the commit date from the tip of each branch.

    ReplyDelete

Comments are moderated thanks to spammers