106 lines
2.3 KiB
Bash
Executable File
106 lines
2.3 KiB
Bash
Executable File
#!/bin/bash
|
||
# Clone un dépôt git au bon endroit
|
||
# Stocker un minimum de données (et donc nettoyer)
|
||
# Télécharger un minimum de données
|
||
# En cas de conflit donner raison au remote (on écrase les versions locales)
|
||
|
||
#bien que le minimum
|
||
#bon tag, bonne branche
|
||
#historique bien viré
|
||
#si le tag change, c'est bon
|
||
|
||
|
||
declare -A usage
|
||
declare -A varia
|
||
|
||
summary="$0 [options] <repo>"
|
||
|
||
usage[r]="Reference of wanted commit"
|
||
varia[r]=ref
|
||
ref=main
|
||
|
||
usage[d]="Destination of clone"
|
||
varia[d]=dst
|
||
dst='.'
|
||
|
||
usage[i]="privkey used to ssh pull"
|
||
varia[i]=privkey
|
||
privkey=''
|
||
|
||
usage[N]="Clone to a Non-empty target. Existing files will be overwritten"
|
||
varia[N]=nonempty_target
|
||
nonempty_target=false
|
||
|
||
usage[K]="Remote host key file (known_hosts) for ssh connections"
|
||
varia[K]=hostkeyfile
|
||
hostkeyfile=''
|
||
|
||
usage[H]="Use real home dir"
|
||
varia[H]=use_home
|
||
use_home=false
|
||
|
||
usage[a]="use git clean with the aggressive option"
|
||
varia[a]=be_aggressive
|
||
be_aggressive=false
|
||
|
||
. driglibash-args
|
||
|
||
|
||
# Some SSH options
|
||
ssh_opt='ssh'
|
||
if [ -n "$privkey" ] ; then
|
||
ssh_opt="$ssh_opt -i $privkey"
|
||
fi
|
||
|
||
if [ -n "$hostkeyfile" ] ; then
|
||
ssh_opt="$ssh_opt -o 'UserKnownHostsFile $hostkeyfile'"
|
||
fi
|
||
|
||
repo="$1"
|
||
if [ -z "$repo" ] ; then
|
||
die "$0: Empty repo given\n$summary"
|
||
fi
|
||
|
||
if [ ! $use_home ] ; then
|
||
set -a
|
||
export HOME=/dev/null
|
||
set +a
|
||
fi
|
||
|
||
run mkdir -p "$dst"
|
||
run cd "$dst"
|
||
|
||
|
||
if [ -d .git ] ; then
|
||
|
||
run git submodule update --init --recursive --force --depth=1 --remote &> /dev/null
|
||
run git fetch --tags --depth=1 --prune --prune-tags origin $ref &> /dev/null
|
||
run git reset --hard FETCH_HEAD &> /dev/null
|
||
git reflog expire --expire=now --all &> /dev/null
|
||
if "$be_aggressive" ; then
|
||
git gc --prune=now --aggressive &> /dev/null
|
||
else
|
||
git gc --prune=now &> /dev/null
|
||
fi
|
||
# Preserve existing files in some cases
|
||
if ! "$nonempty_target" ; then #so we keep uncommitted files when not in -N
|
||
git clean -qffdx
|
||
fi
|
||
|
||
else
|
||
clone_dst='.'
|
||
|
||
# To override an existing dir, we need to clone elsewhere first
|
||
if "$nonempty_target" ; then
|
||
clone_dst="$(mktemp -d)"
|
||
fi
|
||
run git clone -b "$ref" --recurse-submodules --shallow-submodules --depth 1 --config core.sshCommand="$ssh_opt" "$repo" "$clone_dst"
|
||
|
||
# To override an existing dir, we then move everything to that dir
|
||
if "$nonempty_target" ; then
|
||
run mv "$clone_dst/"{*,.*} .
|
||
run rmdir "$clone_dst"
|
||
fi
|
||
fi
|
||
|