2024-07-01 07:30:05 +00:00
|
|
|
#!/bin/bash
|
|
|
|
summary="$0 [options] <repo>"
|
|
|
|
|
2024-07-12 16:30:57 +00:00
|
|
|
ref=main
|
2024-07-01 07:30:05 +00:00
|
|
|
dst='.'
|
|
|
|
nonempty_target=false
|
|
|
|
use_home=false
|
2024-07-12 16:53:59 +00:00
|
|
|
be_aggressive=false
|
2024-07-01 07:30:05 +00:00
|
|
|
|
2024-07-17 09:54:29 +00:00
|
|
|
while getopts ":hr:d:NHa" option; do
|
|
|
|
case $option in
|
|
|
|
h) # display Help
|
|
|
|
Help
|
|
|
|
exit;;
|
|
|
|
r) # branch or tag wanted
|
|
|
|
ref="$OPTARG";;
|
|
|
|
d) # destination of clone
|
|
|
|
dst="$OPTARG";;
|
|
|
|
N) # clone to a Non-empty target. Existing files will be overwritten
|
|
|
|
nonempty_target="true";;
|
|
|
|
H) # use real home dir
|
|
|
|
use_home="false";;
|
|
|
|
a) # use git clean with the aggressive option
|
|
|
|
be_aggressive="true";;
|
|
|
|
\?) # invalid option
|
|
|
|
echo "Error: Invalid option here"
|
|
|
|
exit;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
2024-07-01 07:30:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
repo="$1"
|
|
|
|
if [ -z "$repo" ] ; then
|
2024-07-17 09:54:29 +00:00
|
|
|
exit "$0: Empty repo given\n$summary"
|
2024-07-01 07:30:05 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! $use_home ] ; then
|
|
|
|
set -a
|
|
|
|
export HOME=/dev/null
|
|
|
|
set +a
|
|
|
|
fi
|
|
|
|
|
2024-07-17 09:54:29 +00:00
|
|
|
mkdir -p "$dst"
|
|
|
|
cd "$dst"
|
2024-07-01 07:30:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
if [ -d .git ] ; then
|
|
|
|
|
2024-07-17 09:54:29 +00:00
|
|
|
git submodule update --init --recursive --force --depth=1 --remote
|
|
|
|
git fetch --tags --depth=1 --prune --prune-tags --force origin $ref
|
|
|
|
git reset --hard FETCH_HEAD
|
|
|
|
git reflog expire --expire=now --all
|
2024-07-12 16:53:59 +00:00
|
|
|
if "$be_aggressive" ; then
|
2024-07-17 09:54:29 +00:00
|
|
|
git gc --prune=now --aggressive
|
2024-07-12 16:53:59 +00:00
|
|
|
else
|
2024-07-17 09:54:29 +00:00
|
|
|
git gc --prune=now
|
2024-07-12 16:53:59 +00:00
|
|
|
fi
|
2024-07-01 07:30:05 +00:00
|
|
|
# Preserve existing files in some cases
|
2024-07-15 16:15:21 +00:00
|
|
|
if ! "$nonempty_target" ; then #we keep uncommitted files when in -N mode
|
|
|
|
git clean -qfdx
|
2024-07-01 07:30:05 +00:00
|
|
|
fi
|
2024-07-12 16:30:57 +00:00
|
|
|
|
2024-07-01 07:30:05 +00:00
|
|
|
else
|
|
|
|
clone_dst='.'
|
|
|
|
|
|
|
|
# To override an existing dir, we need to clone elsewhere first
|
|
|
|
if "$nonempty_target" ; then
|
|
|
|
clone_dst="$(mktemp -d)"
|
|
|
|
fi
|
2024-07-17 09:54:29 +00:00
|
|
|
git clone -b "$ref" --recurse-submodules --shallow-submodules --depth 1 "$repo" "$clone_dst"
|
2024-07-01 07:30:05 +00:00
|
|
|
|
|
|
|
# To override an existing dir, we then move everything to that dir
|
|
|
|
if "$nonempty_target" ; then
|
2024-07-17 09:54:29 +00:00
|
|
|
mv "$clone_dst/"{*,.*} .
|
|
|
|
rmdir "$clone_dst"
|
2024-07-01 07:30:05 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|