Advertisement
Guest User

gitprune

a guest
Aug 17th, 2012
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.59 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function prune() {
  4.    
  5.     local time_spec="30 days ago"
  6.    
  7.     if ! which git &>/dev/null; then
  8.             echo "Please install a recent version of Git" >&2
  9.             echo "See http://git.or.cz for more information about Git" >&2
  10.             exit 1
  11.         fi
  12.  
  13.     if test $(git rev-parse "HEAD@{$time_spec}") = "$(git rev-parse HEAD)"; then
  14.         local TMOUT=20
  15.         echo "You are about to remove *all* commits made before the very last one you made"
  16.         read
  17.     fi
  18.  
  19.     # Something like that
  20.     git filter-branch --parent-filter \
  21.     'test $(git rev-parse "HEAD@{$time_spec}") = "$GIT_COMMIT" || cat ' \
  22.     HEAD
  23.     if test "$?" != "0"; then
  24.         die "Please make sure you did '$SCRIPT_NAME commit' before removing old files."
  25.     fi
  26.  
  27.     # See git mailing list
  28.     # "Trying to use git filter-branch to compress history by removing
  29.     # large, obsolete binary files"
  30.  
  31.     git reset --soft # was '--hard' on the post...
  32.     rm -rf .git/refs/original/
  33.     #vi .git/packed-refs # Use vi to remove the line referring to
  34.     # refs/original...  No need since we have linear, no tags, nothing
  35.     # special
  36.     git reflog expire --all --expire=now --expire-unreachable=0
  37.  
  38.     echo "Committing the removal action"
  39.  
  40.     # Make sure we are able to tell in a commit that on this
  41.     # date, a cleanup was made
  42.     local removal_date=$( date +"%a, %d %b %Y %H:%M:%S %z" )
  43.     local witness_file=.git-home-history-last-removal
  44.     local msg="Removed older than '$time_spec' on $removal_date"
  45.     echo "$msg" > ${witness_file}
  46.     git add $witness_file
  47.     git commit -m"$msg" $witness_file
  48.  
  49.     # Finally make sure everything is ok, and remove old stuff
  50.     git gc --prune=$timespec
  51. }
  52.  
  53. prune
  54. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement