git_update/git_update.sh

100 lines
2.7 KiB
Bash
Raw Normal View History

2024-07-01 07:30:05 +00:00
#!/bin/bash
summary="$0 [options] <repo>"
2024-07-12 16:30:57 +00:00
ref=main
2024-07-01 07:30:05 +00:00
dst='.'
nonempty_target=false
use_home=false
2024-07-12 16:53:59 +00:00
be_aggressive=false
2024-07-01 07:30:05 +00:00
2024-07-22 09:07:59 +00:00
Help(){
echo "
NAME
git_update.sh
SYNOPSIS
git_update.sh [-h] [-r ref] [-d dest] [-H] [-N] [-a] repository
OPTIONS
-h prints the help.
-r specifies the reference to the commit to be synchronized. It can be a tag or a branch. By default, it is the last commit of branch main.
-d specifies the destination of the clone or update. If non-empty, -N must be specified.
-H
-N indicates that the destination is non-empty. The files that do not conflict and if it is an update, are not committed, will be kept.
-a specifies that the aggressive option of git clean must be used. git clean is called wen -N is not specified.
If the repository to be cloned is local, and its path is passed as a relative path, the path should start from the destination.
To avoid mistakes, absolute paths are advised.
DESCRIPTION
This script will replace the destination with the wanted commit of a git repository. The history is not preserved but tags are.
The git commands have been chosen so as to minimize the memory and bandwidth usages."
}
2024-07-17 09:54:29 +00:00
while getopts ":hr:d:NHa" option; do
case $option in
h) # display Help
Help
exit;;
r) # branch or tag wanted
ref="$OPTARG";;
d) # destination of clone
dst="$OPTARG";;
N) # clone to a Non-empty target. Existing files will be overwritten
nonempty_target="true";;
H) # use real home dir
use_home="false";;
a) # use git clean with the aggressive option
be_aggressive="true";;
\?) # invalid option
echo "Error: Invalid option here"
exit;;
esac
done
shift $((OPTIND-1))
2024-07-01 07:30:05 +00:00
repo="$1"
if [ -z "$repo" ] ; then
2024-07-17 09:54:29 +00:00
exit "$0: Empty repo given\n$summary"
2024-07-01 07:30:05 +00:00
fi
if [ ! $use_home ] ; then
set -a
export HOME=/dev/null
set +a
fi
2024-07-17 09:54:29 +00:00
mkdir -p "$dst"
cd "$dst"
2024-07-01 07:30:05 +00:00
if [ -d .git ] ; then
2024-07-17 09:54:29 +00:00
git submodule update --init --recursive --force --depth=1 --remote
git fetch --tags --depth=1 --prune --prune-tags --force origin $ref
git reset --hard FETCH_HEAD
git reflog expire --expire=now --all
2024-07-12 16:53:59 +00:00
if "$be_aggressive" ; then
2024-07-17 09:54:29 +00:00
git gc --prune=now --aggressive
2024-07-12 16:53:59 +00:00
else
2024-07-17 09:54:29 +00:00
git gc --prune=now
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-15 16:15:21 +00:00
if ! "$nonempty_target" ; then #we keep uncommitted files when in -N mode
git clean -qfdx
2024-07-01 07:30:05 +00:00
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-17 09:54:29 +00:00
git clone -b "$ref" --recurse-submodules --shallow-submodules --depth 1 "$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
2024-07-17 09:54:29 +00:00
mv "$clone_dst/"{*,.*} .
rmdir "$clone_dst"
2024-07-01 07:30:05 +00:00
fi
fi