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

81 lines
1.7 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
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=''
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
2023-10-16 08:47:35 +00:00
run mkdir -p "$dst"
run cd "$dst"
2023-08-28 18:25:32 +00:00
if [ -d .git ] ; then
2023-09-13 08:46:02 +00:00
run git fetch origin "$branch"
run git checkout --force -B "$branch" "origin/$branch"
run git reset --hard
# 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