From a88472a2639e17d9737f67c3d635f1b1868358ab Mon Sep 17 00:00:00 2001 From: Matthijs Berends Date: Thu, 19 Sep 2024 14:20:03 +0200 Subject: [PATCH] test new git hook --- .github/prehooks/commit-msg | 62 ++++++++++++++ .github/prehooks/pre-commit | 88 +++++++++++++++++-- .github/prehooks/prepare-commit-msg | 126 ---------------------------- 3 files changed, 144 insertions(+), 132 deletions(-) create mode 100755 .github/prehooks/commit-msg delete mode 100755 .github/prehooks/prepare-commit-msg diff --git a/.github/prehooks/commit-msg b/.github/prehooks/commit-msg new file mode 100755 index 00000000..ad0b4422 --- /dev/null +++ b/.github/prehooks/commit-msg @@ -0,0 +1,62 @@ +#!/bin/bash + +# ==================================================================== # +# TITLE: # +# AMR: An R Package for Working with Antimicrobial Resistance Data # +# # +# SOURCE CODE: # +# 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. # +# https://doi.org/10.18637/jss.v104.i03 # +# # +# 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. # +# # +# 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/ # +# ==================================================================== # + +# Path to the commit message file +COMMIT_MSG_FILE="$1" + +# Read the original commit message +COMMIT_MSG=$(cat "$COMMIT_MSG_FILE") + +# Check if commit should skip checks +if [[ "$COMMIT_MSG" =~ no-?checks?|no-?verify ]]; then + echo "Not modifying commit message:" + echo "Commit message contains 'no-check' or 'no-verify'." + echo "" + exit 0 +fi + +# Read the version number from the temporary file +if [ -f ".git/commit_version.tmp" ]; then + currentversion=$(cat .git/commit_version.tmp) + rm -f .git/commit_version.tmp +else + echo "Version number file not found." + currentversion="" +fi + +# Prepend the version number to the commit message if available +if [ -n "$currentversion" ]; then + echo "(v${currentversion}) ${COMMIT_MSG}" > "$COMMIT_MSG_FILE" +else + echo "No version number to prepend to commit message." +fi + +exit 0 diff --git a/.github/prehooks/pre-commit b/.github/prehooks/pre-commit index 8eaf689f..73332421 100755 --- a/.github/prehooks/pre-commit +++ b/.github/prehooks/pre-commit @@ -29,10 +29,86 @@ # how to conduct AMR data analysis: https://msberends.github.io/AMR/ # # ==================================================================== # -# always add these: -git add data-raw/* -git add man/* -git add R/sysdata.rda -git add NAMESPACE +# Check if commit should skip checks +COMMIT_MSG_FILE=".git/COMMIT_EDITMSG" +if [ -f "$COMMIT_MSG_FILE" ]; then + COMMIT_MSG=$(cat "$COMMIT_MSG_FILE") + if [[ "$COMMIT_MSG" =~ no-?checks?|no-?verify ]]; then + echo "Not running prehook:" + echo "Commit message contains 'no-check' or 'no-verify'." + echo "" + exit 0 + fi +fi + +echo "Running prehook..." + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Run the R script and stage the modified files +if command -v Rscript > /dev/null; then + if [ "$(Rscript -e 'cat(all(c('"'pkgload'"', '"'devtools'"', '"'dplyr'"') %in% rownames(installed.packages())))')" = "TRUE" ]; then + Rscript -e "source('data-raw/_pre_commit_checks.R')" + currentpkg=$(Rscript -e "cat(pkgload::pkg_name())") + echo "- Adding changed files in ./data-raw and ./man to this commit" + git add data-raw/* + git add man/* + git add R/sysdata.rda + git add NAMESPACE + else + echo "- R package 'pkgload', 'devtools', or 'dplyr' not installed!" + currentpkg="your" + fi +else + echo "- R is not available on your system!" + currentpkg="your" +fi +echo "" + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +echo "Updating semantic versioning and date..." + +# 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//') + +# Assume main branch to be 'main' or 'master' +defaultbranch=$(git branch | cut -c 3- | grep -E '^master$|^main$') +if [ "$currenttag" = "" ]; then + currenttag="0.0.1" + currentcommit=$(git rev-list --count ${defaultbranch}) + echo "- No git tags found, creating one in format 'v(x).(y).(z)' - currently ${currentcommit} previous commits in '${defaultbranch}'" +else + currentcommit=$(git rev-list --count ${currenttagfull}..${defaultbranch}) + echo "- Latest tag is '${currenttagfull}', with ${currentcommit} previous commits in '${defaultbranch}'" +fi + +# Combine tag and commit number +currentversion="$currenttag.$((currentcommit + 9001))" +echo "- ${currentpkg} pkg version set to ${currentversion}" + +# Update version number and date in DESCRIPTION +sed -i -- "s/^Version: .*/Version: ${currentversion}/" DESCRIPTION +sed -i -- "s/^Date: .*/Date: $(date '+%Y-%m-%d')/" DESCRIPTION +echo "- Updated version number and date in ./DESCRIPTION" +rm -f DESCRIPTION-- git add DESCRIPTION -git add NEWS.md + +# Update version number in NEWS.md +if [ -e "NEWS.md" ]; then + if [ "$currentpkg" = "your" ]; then + currentpkg="" + fi + sed -i -- "1s/.*/# ${currentpkg} ${currentversion}/" NEWS.md + echo "- Updated version number in ./NEWS.md" + rm -f NEWS.md-- + git add NEWS.md +else + echo "- No NEWS.md found!" +fi +echo "" + +# Save the version number for use in the commit-msg hook +echo "${currentversion}" > .git/commit_version.tmp + +exit 0 diff --git a/.github/prehooks/prepare-commit-msg b/.github/prehooks/prepare-commit-msg deleted file mode 100755 index 7e458886..00000000 --- a/.github/prehooks/prepare-commit-msg +++ /dev/null @@ -1,126 +0,0 @@ -#!/bin/bash - -# ==================================================================== # -# TITLE: # -# AMR: An R Package for Working with Antimicrobial Resistance Data # -# # -# SOURCE CODE: # -# 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. # -# https://doi.org/10.18637/jss.v104.i03 # -# # -# 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. # -# # -# 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 # -######################################## - -COMMIT_MSG_FILE=".git/COMMIT_EDITMSG" - -# Read the commit message -if [ -f "$COMMIT_MSG_FILE" ]; then - COMMIT_MSG=$(cat "$COMMIT_MSG_FILE") -else - echo "Commit message file not found." - exit 1 -fi - -# 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'." - echo "" - exit 0 -fi - -echo "Running prehook..." - -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -if command -v Rscript > /dev/null; then - if [ "$(Rscript -e 'cat(all(c('"'pkgload'"', '"'devtools'"', '"'dplyr'"') %in% rownames(installed.packages())))')" = "TRUE" ]; then - Rscript -e "source('data-raw/_pre_commit_checks.R')" - currentpkg=$(Rscript -e "cat(pkgload::pkg_name())") - echo "- Adding changed files in ./data-raw and ./man to this commit" - git add data-raw/* - git add man/* - git add R/sysdata.rda - git add NAMESPACE - else - echo "- R package 'pkgload', 'devtools', or 'dplyr' not installed!" - currentpkg="your" - fi -else - echo "- R is not available on your system!" - currentpkg="your" -fi -echo "" - - -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -echo "Updating semantic versioning and date..." - -# 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//') -# assume main branch to be 'main' or 'master', pick the right name: -defaultbranch=$(git branch | cut -c 3- | grep -E '^master$|^main$') -if [ "$currenttag" = "" ]; then - # there is no tag, so set tag to 0.0.1 and commit index to current count - currenttag="0.0.1" - currentcommit=$(git rev-list --count ${defaultbranch}) - echo "- no git tags found, create one in format 'v(x).(y).(z)' - curently ${currentcommit} previous commits in '${defaultbranch}'" -else - # there is a tag, so base version number on that - currentcommit=$(git rev-list --count ${currenttagfull}..${defaultbranch}) - echo "- latest tag is '${currenttagfull}', with ${currentcommit} previous commits in '${defaultbranch}'" -fi -# 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 -echo "- ${currentpkg} pkg version set to ${currentversion}" - -# 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 -echo "- updated version number and date in ./DESCRIPTION" -# remove leftover on macOS -rm -f DESCRIPTION-- -# add to commit -git add DESCRIPTION - -# set version number to NEWS file -if [ -e "NEWS.md" ]; then - if [ "$currentpkg" = "your" ]; then - currentpkg="" - fi - sed -i -- "1s/.*/# ${currentpkg} ${currentversion}/" NEWS.md - echo "- updated version number in ./NEWS.md" - # remove leftover on macOS - rm -f NEWS.md-- - # add to commit - git add NEWS.md -else - echo "- no NEWS.md found!" -fi -echo "" - -# Prepend the version number to the commit message -echo "(v${currentversion}) ${COMMIT_MSG}" > "$COMMIT_MSG_FILE"