AMR/.github/prehooks/pre-commit

135 lines
5.7 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: #
2022-10-05 09:12:22 +02:00
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE CODE: #
2022-10-05 09:12:22 +02:00
# https://github.com/msberends/AMR #
# #
# PLEASE CITE THIS SOFTWARE AS: #
2022-10-05 09:12:22 +02:00
# 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. #
2023-05-27 10:39:22 +02:00
# https://doi.org/10.18637/jss.v104.i03 #
2022-10-05 09:12:22 +02:00
# #
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/ #
# ==================================================================== #
########################################
# This script runs before every commit #
########################################
2024-04-23 10:33:26 +02:00
2024-04-24 11:58:16 +02:00
# try to fetch the commit message from the standard location
COMMIT_MSG_PATH=".git/COMMIT_EDITMSG"
if [ -f "$COMMIT_MSG_PATH" ]; then
2024-06-04 20:17:54 +02:00
COMMIT_MSG=$(cat "$COMMIT_MSG_PATH")
2024-04-24 11:58:16 +02:00
else
2024-06-04 20:17:54 +02:00
echo "Commit message file not found."
COMMIT_MSG=""
2024-04-24 11:58:16 +02:00
fi
# check the commit message, cancel commit if needed
if [[ "$COMMIT_MSG" =~ \(no-check\)|\(no-checks\)|\(no-verify\) ]]; then
2024-04-24 09:50:32 +02:00
echo "Not running pre-commit checks:"
echo "Commit message contains '(no-check)', '(no-checks)', or '(no-verify)."
echo ""
2024-04-23 10:33:26 +02:00
exit 0
fi
echo "Running pre-commit checks..."
2022-08-20 20:17:14 +02:00
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
Rscript -e "source('data-raw/_pre_commit_checks.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})
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
2023-07-20 08:24:27 +02:00
# 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
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
2024-06-04 20:17:54 +02:00
# Prepend the version number to the commit message
if [ -f "$COMMIT_MSG_PATH" ]; then
2024-06-04 20:21:12 +02:00
COMMIT_MSG=$(cat "$COMMIT_MSG_PATH")
echo "Current commit message: $COMMIT_MSG" # Debug line
echo "(v${currentversion}) ${COMMIT_MSG}" > "$COMMIT_MSG_PATH"
echo "- prepended version number to commit message"
echo "Updated commit message: $(cat "$COMMIT_MSG_PATH")" # Debug line
2024-06-04 20:17:54 +02:00
else
echo ""
echo "Commit message file not found. Unable to prepend version number."
fi
2022-10-05 09:12:22 +02:00
echo ""