[PATCH] Make git-prune-script a bit more careful.
- From
Junio C Hamano <junkio@cox.net>
- Date
- May 1, 2005, 21:27 UTC
- Message-ID
- <7vk6miskl4.fsf_-_@assigned-by-dhcp.cox.net>
- In-Reply-To
- <7vwtqismpn.fsf@assigned-by-dhcp.cox.net>
As discussed on the git list, this patch makes the sample script git-prune-script a bit more careful by keeping the objects referenced from the current cache from being reclaimed.
At the same time it also fixes and enhances the following:
- Optional command line parameters can specify which commit heads to start reachablity test from. Earlier we had a hardcoded .git/HEAD. It now defaults to the contents of .git/HEAD and .git/refs/*/* files.
- It does not assume SHA1_FILE_DIRECTORY is .git/objects/ but uses the value from the environment if there is one.
- It runs xargs with "-r" option to prevent it from making "rm" command barf if there is nothing to remove.
Signed-off-by: Junio C Hamano <junkio@cox.net> ---
git-prune-script | 32 +++++++++++++++++++++++++++++++- 1 files changed, 31 insertions(+), 1 deletion(-)
# - date handling: handle "AM"/"PM" on time # + [PATCH] Make git-prune-script a bit more careful. --- k/git-prune-script +++ l/git-prune-script
@@ -1,2 +1,32 @@ #!/bin/sh -git-fsck-cache --unreachable $(cat .git/HEAD ) | grep unreachable | cut -d' ' -f3 | sed 's:^\(..\):.git/objects/\1/:' | xargs rm + +tmp=.git-prune-script-$$ +trap 'rm -f $tmp-*' 0 1 2 3 15 + +# Defaulting to include .git/refs/*/* may be debatable from the +# purist POV but power users can always give explicit parameters +# to the script anyway. +case "$#" in +0) set x $(cat .git/HEAD .git/refs/*/*); shift ;; +esac + +git-fsck-cache --unreachable "$@" | +sed -ne 's/unreachable [^ ][^ ]* //p' | +sort >$tmp-unreachable + +# This makes extra objects to be kept if the cache has an entry +# with an unusual name like "this\n0 0123...abcdef 0 file", but +# we are trying not to discard information and keeping extra in +# an unusual situation would be OK. +git-ls-files --stage | +sed -ne 's|^[0-7][0-7]* \([0-9a-f][0-9a-f]*\) [0-3] .*|\1|p' | +sort >$tmp-keep + +comm -23 $tmp-unreachable $tmp-keep | +sed -e 's|\(..\)|\1/|' | { + case "$SHA1_FILE_DIRECTORY" in + '') cd .git/objects/ ;; + *) cd "$SHA1_FILE_DIRECTORY" ;; + esac || exit + xargs -r echo rm -f +}