1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-30 22:46:11 +01:00
AMR/.github/prehooks/prepare-commit-msg

127 lines
5.2 KiB
Plaintext
Raw Normal View History

2024-06-08 17:35:25 +02:00
#!/bin/bash
2022-08-20 20:17:14 +02:00
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: #
# Berends MS, Luz CF, Friedrich AW, et al. (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-06-23 16:18:42 +02:00
COMMIT_MSG_FILE=".git/COMMIT_EDITMSG"
# Read the commit message
if [ -f "$COMMIT_MSG_FILE" ]; then
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
2024-04-24 11:58:16 +02:00
else
2024-06-04 20:17:54 +02:00
echo "Commit message file not found."
exit 1
2024-04-24 11:58:16 +02:00
fi
2024-06-23 16:18:42 +02:00
# Check the commit message and skip checks if needed
if [[ "$COMMIT_MSG" =~ no-?checks?|no-?verify ]]; then
echo "Not running prehook:"
echo "Commit message contains 'no-check' or 'no-verify'."
2024-04-24 09:50:32 +02:00
echo ""
2024-04-23 10:33:26 +02:00
exit 0
fi
2024-06-23 16:18:42 +02:00
echo "Running prehook..."
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
echo "- R package 'pkgload', 'devtools', or 'dplyr' 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
2022-10-05 09:12:22 +02:00
echo ""
2024-06-23 16:18:42 +02:00
# Prepend the version number to the commit message
echo "(v${currentversion}) ${COMMIT_MSG}" > "$COMMIT_MSG_FILE"