1
0
mirror of https://github.com/msberends/AMR.git synced 2025-01-16 10:41:38 +01:00
AMR/.github/prehooks/pre-commit

76 lines
2.5 KiB
Plaintext
Raw Normal View History

2022-08-20 20:17:14 +02:00
#!/bin/sh
echo "Running pre-commit hook..."
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if command -v Rscript > /dev/null; then
2022-08-28 10:31:50 +02:00
if [ "$(Rscript -e 'cat(all(c('"'pkgload'"', '"'devtools'"', '"'dplyr'"', '"'styler'"') %in% rownames(installed.packages())))')" = "TRUE" ]; then
Rscript -e "source('data-raw/_pre_commit_hook.R')"
2022-08-20 20:17:14 +02:00
currentpkg=`Rscript -e "cat(pkgload::pkg_name())"`
2022-09-23 13:22:48 +02:00
echo "-> Adding 'R/sysdata.rda' and all files in folders 'data-raw' and 'man' to this commit"
2022-08-28 10:31:50 +02:00
git add data-raw/*
2022-08-20 20:17:14 +02:00
git add man/*
2022-09-23 13:22:48 +02:00
git add R/sysdata.rda
2022-08-20 20:17:14 +02:00
else
2022-08-28 21:13:26 +02:00
echo "- R package 'pkgload', 'devtools', 'dplyr', or 'styler' not installed!"
2022-08-20 20:17:14 +02:00
currentpkg="your"
fi
else
2022-08-28 21:13:26 +02:00
echo "- R is not available on your system!"
2022-08-20 20:17:14 +02:00
currentpkg="your"
fi
2022-08-28 21:13:26 +02:00
echo ""
2022-08-20 20:17:14 +02:00
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-09-23 15:18:55 +02:00
echo "Updating semantic versioning and date..."
2022-08-20 20:17:14 +02:00
# get tags from remote, and remove tags not on remote:
git fetch origin --prune --prune-tags --quiet
currenttagfull=`git describe --tags --abbrev=0`
currenttag=`git describe --tags --abbrev=0 | sed 's/v//'`
if [ "$currenttag" = "" ]; then
# there is no tag, so set tag to 0.0.1 and commit index to current count
2022-09-23 15:18:55 +02:00
echo "- no git tags found, create one in this format: 'v(x).(y).(z)'!"
2022-08-20 20:17:14 +02:00
currenttag="0.0.1"
currentcommit=`git rev-list --count HEAD`
else
# there is a tag, so base version number on that
currentcommit=`git rev-list --count ${currenttagfull}..HEAD`
if (( "$currentcommit" == 0 )); then
# tag is new, so this must become the version number
currentversion="$currenttag"
fi
2022-09-23 15:18:55 +02:00
echo "- latest tag is '${currenttagfull}', with ${currentcommit} previous commits"
2022-08-20 20:17:14 +02:00
fi
if [ "$currentversion" = "" ]; then
# combine tag (e.g. 1.2.3) and commit number (like 5) increased by 9000 to indicate beta version
currentversion="$currenttag.$((currentcommit + 9001))" # results in e.g. 1.2.3.9005
fi
2022-09-23 15:18:55 +02:00
echo "- ${currentpkg} pkg version set to ${currentversion}"
2022-08-20 20:17:14 +02:00
# set version number and date to DESCRIPTION file
sed -i -- "s/^Version: .*/Version: ${currentversion}/" DESCRIPTION
sed -i -- "s/^Date: .*/Date: $(date '+%Y-%m-%d')/" DESCRIPTION
2022-09-23 15:18:55 +02:00
echo "- updated DESCRIPTION"
2022-08-20 20:17:14 +02:00
# remove leftover on macOS
2022-08-25 19:14:07 +02:00
rm -f DESCRIPTION--
2022-08-20 20:17:14 +02:00
# add to commit
git add DESCRIPTION
# set version number to NEWS file
if [ -e "NEWS.md" ]; then
2022-08-26 14:02:08 +02:00
if [ "$currentpkg" = "your" ]; then
currentpkg=""
fi
2022-08-20 20:17:14 +02:00
sed -i -- "1s/.*/# ${currentpkg} ${currentversion}/" NEWS.md
2022-09-23 15:18:55 +02:00
echo "- updated NEWS.md"
2022-08-20 20:17:14 +02:00
# remove leftover on macOS
2022-08-25 19:14:07 +02:00
rm -f NEWS.md--
2022-08-20 20:17:14 +02:00
# add to commit
git add NEWS.md
else
2022-09-23 15:18:55 +02:00
echo "- no NEWS.md found!"
2022-08-20 20:17:14 +02:00
fi
2022-09-23 15:18:55 +02:00
echo ""