Skip to content

Instantly share code, notes, and snippets.

@mvsantos
Created April 10, 2014 16:14
Show Gist options
  • Save mvsantos/10398433 to your computer and use it in GitHub Desktop.
Save mvsantos/10398433 to your computer and use it in GitHub Desktop.
#!/bin/sh -e
# git-cache-meta -- file owner and permissions caching.
# Based on the solution proposed by <jidanni @ jidanni.org> found at
# http://thread.gmane.org/gmane.comp.version-control.git/104971/focus=105133
IFS=$'\n' # avoid problems when finding spaces in file names
: ${GIT_CACHE_META_FILE=.git_cache_meta}
list_dirs() {
for D in $(git ls-files | /bin/grep -F "/" | sed -r "s/\/[^\/]+$//g" | sort -u);
do
if [[ "$D" =~ "/" ]]; then
while [[ "$D" =~ "/" ]]; do
echo "$D"
D="$(echo "$D" | sed -r "s/\/[^\/]+$//g")"
done
else
echo "$D"
fi
done | sort -u | xargs -I '{}' find '{}' -type d -prune \
\( -printf 'chown %U:%G "%p/"\n' \) \
\( -printf 'chmod %#m "%p/"\n' \) \
\( -printf 'touch -c -h -m -d "%TY-%Tm-%Td %TH:%TM:%TS %Tz" "%p/"\n' \);
}
list_files() {
git ls-files -z | xargs -0r -I '{}' find '{}' \
\( -printf 'chown %U:%G "%p"\n' \) \
\( -printf 'chmod %#m "%p"\n' \) \
\( -printf 'touch -c -h -m -d "%TY-%Tm-%Td %TH:%TM:%TS %Tz" "%p"\n' \);
}
case $@ in
--store|--stdout)
case $1 in --store) exec > $GIT_CACHE_META_FILE; esac
list_dirs; list_files;;
--apply) sh -e $GIT_CACHE_META_FILE;;
*) 1>&2 cat <<EOF; exit 1;;
Usage: $0 --store|--stdout|--apply
# Storage file: ${GIT_CACHE_META_FILE}
#
# Example:
# In your local git repo:
# git bundle create mybundle.bdl master
# git-cache-meta --store
# scp mybundle.bdl .git_cache_meta machine2
# Then on machine2:
# git init
# git pull mybundle.bdl master
# git-cache-meta --apply
EOF
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment