#!/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] " 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 # Compute git branch and tag tagref= if [ -n "$tag" ] ; then tagref="tags/$tag" fi run git submodule update --init --recursive --force --depth=1 --remote run git fetch --tags --depth=1 --prune --prune-tags origin $ref run git reset --hard FETCH_HEAD git reflog expire --expire=now --all if "$be_aggressive" ; then git gc --prune=now --aggressive else git gc --prune=now 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