git_update/git_update.sh

81 lines
2.5 KiB
Bash
Raw Normal View History

2024-07-01 07:30:05 +00:00
#!/bin/bash
2024-07-22 09:07:59 +00:00
Help(){
echo "
NAME
git_update.sh
SYNOPSIS
git_update.sh [-h] [-r ref] [-d dest] [-H] [-a] repository
2024-07-22 09:07:59 +00:00
OPTIONS
-h prints the help.
2024-07-22 09:07:59 +00:00
-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. Directory must be empty if a new clone is to be made.
2024-07-22 09:07:59 +00:00
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.
-H allows the $HOME directory to be used by git_update.sh. By default, git_update.sh cannot access $HOME to prevent default behavior.
If you need the global .gitconfig located in your $HOME to be used, you should supply the -H option.
-a specifies that the aggressive option of the git garbage collection must be used. Only advised when changes happen in many different objects. Will slow down the execution.
2024-07-22 09:07:59 +00:00
DESCRIPTION
This script will replace the destination with the wanted commit of a git repository. The history is not preserved but tags are. Untracked files remain.
2024-07-22 09:07:59 +00:00
The git commands have been chosen so as to minimize the memory and bandwidth usages."
}
#variables
summary="$0 [options] <repo>"
ref=main
dst='.'
use_home=false
be_aggressive="false"
while getopts ":hr:d:H" option; do
2024-07-17 09:54:29 +00:00
case $option in
h) # display Help
Help
exit;;
r) # desired branch or tag
2024-07-17 09:54:29 +00:00
ref="$OPTARG";;
d) # destination of clone
dst="$OPTARG";;
H) # use real home dir
use_home="true";;
a) #use -a in git gc call
2024-07-17 09:54:29 +00:00
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
echo "updating..."
2024-07-17 09:54:29 +00:00
git fetch --tags --depth=1 --prune --prune-tags --force origin $ref
git reset --hard --recurse-submodules FETCH_HEAD
git submodule update --init --recursive --force --depth=1 --remote
#garbage collection of anything unreachable at the moment
2024-07-17 09:54:29 +00:00
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
elsels
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
else
echo "cloning..."
2024-07-01 07:30:05 +00:00
clone_dst='.'
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
fi