jean-cloud-services/provisioning/roles/deploy_all/files/bin/git_update.sh

103 lines
2.0 KiB
Bash
Raw Normal View History

2023-08-28 18:25:32 +00:00
#!/bin/bash
2023-09-13 08:46:02 +00:00
2023-08-28 18:25:32 +00:00
declare -A usage
declare -A varia
summary="$0 [options] <repo>"
usage[b]="Branch of git repo"
varia[b]=branch
branch=master
2024-06-15 15:59:53 +00:00
usage[t]="Tog of git repo"
varia[t]=tag
tag=
2023-08-28 18:25:32 +00:00
usage[d]="Destination of clone"
varia[d]=dst
dst='.'
usage[i]="privkey used to ssh pull"
varia[i]=privkey
privkey=''
2023-09-13 08:46:02 +00:00
usage[N]="Clone to a Non-empty target. Existing files will be overwriten"
varia[N]=nonempty_target
nonempty_target=false
2023-10-16 08:47:35 +00:00
usage[K]="Remote host key file (known_hosts) for ssh connections"
varia[K]=hostkeyfile
hostkeyfile=''
2024-01-02 16:50:14 +00:00
usage[H]="Use real home dir"
varia[H]=use_home
use_home=false
2023-08-28 18:25:32 +00:00
. driglibash-args
2023-09-07 17:50:05 +00:00
2023-08-28 18:25:32 +00:00
# Some SSH options
ssh_opt='ssh'
if [ -n "$privkey" ] ; then
ssh_opt="$ssh_opt -i $privkey"
fi
2023-10-16 08:47:35 +00:00
if [ -n "$hostkeyfile" ] ; then
ssh_opt="$ssh_opt -o 'UserKnownHostsFile $hostkeyfile'"
fi
2023-08-28 18:25:32 +00:00
repo="$1"
if [ -z "$repo" ] ; then
die "$0: Empty repo given\n$summary"
fi
2024-01-02 16:50:14 +00:00
if [ ! $use_home ] ; then
set -a
export HOME=/dev/null
set +a
fi
2023-10-16 08:47:35 +00:00
run mkdir -p "$dst"
run cd "$dst"
2023-08-28 18:25:32 +00:00
2024-06-15 15:59:53 +00:00
2023-08-28 18:25:32 +00:00
if [ -d .git ] ; then
2024-06-15 15:59:53 +00:00
# Compute git branch and tag
tagref=
if [ -n "$tag" ] ; then
tagref="tags/$tag"
fi
run git fetch origin "$branch" --tags
run git checkout --force $tagref -B "$branch"
run git reset --hard # TODO we can keep some files?
2023-09-13 08:46:02 +00:00
# Preserve existing files in some cases
if ! "$nonempty_target" ; then
git clean -qffdx
fi
run git submodule update --init --recursive --force --recommend-shallow
run git submodule foreach git fetch
run git submodule foreach git checkout --force HEAD
run git submodule foreach git reset --hard
run git submodule foreach git clean -fdx
2023-08-28 18:25:32 +00:00
else
2023-09-13 08:46:02 +00:00
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 "$branch" --single-branch --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
2023-08-28 18:25:32 +00:00
fi