1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-26 22:06:11 +01:00
AMR/.github/prehooks/pre-commit

108 lines
4.8 KiB
Plaintext
Raw Normal View History

2022-08-20 20:17:14 +02:00
#!/bin/sh
2022-10-05 09:12:22 +02:00
# ==================================================================== #
# TITLE #
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE #
# https://github.com/msberends/AMR #
# #
# CITE AS #
# Berends MS, Luz CF, Friedrich AW, Sinha BNM, Albers CJ, Glasner C #
# (2022). AMR: An R Package for Working with Antimicrobial Resistance #
# Data. Journal of Statistical Software, 104(3), 1-31. #
# doi:10.18637/jss.v104.i03 #
# #
2022-12-27 15:16:15 +01:00
# Developed at the University of Groningen and the University Medical #
# Center Groningen in The Netherlands, in collaboration with many #
# colleagues from around the world, see our website. #
2022-10-05 09:12:22 +02:00
# #
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
2022-08-20 20:17:14 +02:00
echo "Running pre-commit hook..."
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if command -v Rscript > /dev/null; then
2023-01-05 14:46:44 +01:00
if [ "$(Rscript -e 'cat(all(c('"'pkgload'"', '"'devtools'"', '"'dplyr'"') %in% rownames(installed.packages())))')" = "TRUE" ]; then
2022-08-28 10:31:50 +02:00
Rscript -e "source('data-raw/_pre_commit_hook.R')"
2023-01-06 19:21:04 +01:00
currentpkg=$(Rscript -e "cat(pkgload::pkg_name())")
2023-02-18 13:08:08 +01:00
echo "- Adding changed files in ./data-raw and ./man to this commit"
2023-02-18 14:58:02 +01:00
git add data-raw/*
2022-08-20 20:17:14 +02:00
git add man/*
2022-10-05 09:12:22 +02:00
git add R/sysdata.rda
git add NAMESPACE
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-10-05 09:12:22 +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
2023-01-06 19:21:04 +01:00
currenttagfull=$(git describe --tags --abbrev=0)
currenttag=$(git describe --tags --abbrev=0 | sed 's/v//')
2022-10-21 19:31:46 +02:00
# assume main branch to be 'main' or 'master', pick the right name:
2023-01-06 19:21:04 +01:00
defaultbranch=$(git branch | cut -c 3- | grep -E '^master$|^main$')
2022-08-20 20:17:14 +02:00
if [ "$currenttag" = "" ]; then
# there is no tag, so set tag to 0.0.1 and commit index to current count
currenttag="0.0.1"
2023-01-06 19:21:04 +01:00
currentcommit=$(git rev-list --count ${defaultbranch})
2023-02-18 13:08:08 +01:00
echo "- no git tags found, create one in format 'v(x).(y).(z)' - curently ${currentcommit} previous commits in '${defaultbranch}'"
2022-08-20 20:17:14 +02:00
else
# there is a tag, so base version number on that
2023-01-06 19:21:04 +01:00
currentcommit=$(git rev-list --count ${currenttagfull}..${defaultbranch})
2022-08-20 20:17:14 +02:00
if (( "$currentcommit" == 0 )); then
# tag is new, so this must become the version number
currentversion="$currenttag"
fi
2023-02-18 13:08:08 +01:00
echo "- latest tag is '${currenttagfull}', with ${currentcommit} previous commits in '${defaultbranch}'"
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-10-05 09:12:22 +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
2023-02-18 13:08:08 +01:00
echo "- updated version number and date in ./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
2023-02-18 13:08:08 +01:00
echo "- updated version number in ./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-10-05 09:12:22 +02:00
echo "- no NEWS.md found!"
2022-08-20 20:17:14 +02:00
fi
2022-10-05 09:12:22 +02:00
echo ""