Subject: git-attic

From: Paul Gideon Dann <[email-masked]>
Date: Tue, 29 Jul 2008 12:57:38 +0100

Hello all,

I looked around everywhere for a script that could make it easy for me 
to store stale branches in refs/attic for possible later retrieval.  I 
don't like such branches lying around in refs/heads and used to tag them 
to clear them out of the way, but then that makes "git push origin 
--tags" unusable, since I don't want to push my stale branches out...

Anyway, I wrote my own little script in the end and thought it might be 
useful to others.

Please Cc [email-masked] in replies,
Paul Gideon Dann

Usage: git-attic
       git-attic store <ref> [<commit>]
       git-attic remove <ref>

#!/bin/bash
###
# Original Author: Paul Gideon Dann <[email-masked]>
###

showUsage() {
  echo "Usage: git-attic"
  echo "       git-attic store <ref> [<commit>]"
  echo "       git-attic remove <ref>"
}

case $1 in
  "store")
    if [[ ! $2 ]]; then
      showUsage
      exit 1
    fi
    if [[ $3 ]]; then
      OBJECT_SHA1=`git-rev-parse --revs-only $3`
      if [[ ! $OBJECT_SHA1 ]]; then
        echo "$3 is not a recognisable commit object."
        exit 1
      fi
    else
      OBJECT_SHA1=`git show-ref -h HEAD | awk '{print $1}'`
    fi
    git update-ref refs/attic/$2 $OBJECT_SHA1
    echo "$2 stored in attic."
    ;;

  "remove")
    if [[ ! $2 ]]; then
      showUsage
      exit 1
    fi
    OBJECT_SHA1=`git show-ref attic/$2 | awk '{print $1}'`
    if [[ ! $OBJECT_SHA1 ]]; then
      echo "$2 does not exist in the attic!"
      exit 1
    fi
    git update-ref -d refs/attic/$2 $OBJECT_SHA1
    echo "$2 removed from attic."
    ;;

  *)
    if [[ $1 ]]; then
      showUsage
    else
      # display contents of attic
      git for-each-ref --format="%(refname)" refs/attic | awk -F / 
'{print $3}'
    fi
    ;;
esac