git_update/git_update.sh

106 lines
2.3 KiB
Bash
Raw Normal View History

2024-07-01 07:30:05 +00:00
#!/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)
2024-07-11 16:31:19 +00:00
#bien que le minimum
#bon tag, bonne branche
#historique bien viré
#si le tag change, c'est bon
2024-07-01 07:30:05 +00:00
declare -A usage
declare -A varia
summary="$0 [options] <repo>"
2024-07-11 16:31:19 +00:00
usage[r]="Reference of wanted commit"
2024-07-12 16:30:57 +00:00
varia[r]=ref
ref=main
2024-07-01 07:30:05 +00:00
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
2024-07-12 16:53:59 +00:00
usage[a]="use git clean with the aggressive option"
varia[a]=be_aggressive
be_aggressive=false
2024-07-01 07:30:05 +00:00
. 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
2024-07-15 12:32:55 +00:00
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
2024-07-12 16:53:59 +00:00
if "$be_aggressive" ; then
2024-07-15 12:32:55 +00:00
git gc --prune=now --aggressive &> /dev/null
2024-07-12 16:53:59 +00:00
else
2024-07-15 12:32:55 +00:00
git gc --prune=now &> /dev/null
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-12 16:30:57 +00:00
if ! "$nonempty_target" ; then #so we keep uncommitted files when not in -N
2024-07-01 07:30:05 +00:00
git clean -qffdx
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-12 16:30:57 +00:00
run git clone -b "$ref" --recurse-submodules --shallow-submodules --depth 1 --config core.sshCommand="$ssh_opt" "$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
run mv "$clone_dst/"{*,.*} .
run rmdir "$clone_dst"
fi
fi