From 41a8542aaa4948b0c0bf4b3e0eb62abe2c5fd1e8 Mon Sep 17 00:00:00 2001 From: Tom Ice Date: Sat, 20 Apr 2024 14:15:07 -0400 Subject: [PATCH] Handle error where BSD date is being used * Users on macOS and other older distributions of Linux and Unix cannot fully utilize this application as a handful of date/time strings in here are specific to the GNU utility found on most modern version of Linux. Until every date/time case is handled between the BSD version of date and the GNU version of date, let's error out akin to how we do it if the user doesn't have every utility installed to run this script. Users can get around this by using package managers on macOS such as homebrew, macports, etc and making sure that 'date' points to the GNU version of date instead of the BSD version. Linux and Unix users can get around this by installing the GNU version of date, as well. * Removed checking OSTYPE in the format_date() function as checking if someone is on a machine that identifies as Darwin is not enough to handle other edge cases where an older version of BSD date might be present on the system. --- git-quick-stats | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/git-quick-stats b/git-quick-stats index fde7b43..332a673 100755 --- a/git-quick-stats +++ b/git-quick-stats @@ -89,6 +89,21 @@ function checkUtils() { do command -v "$u" >/dev/null 2>&1 || { echo >&2 "$u ${MSG}"; exit 1; } done + + # NOTE: The --version flag is only available in GNU date which is required + # for how the current date/time strings are used in this shell script. + # To fully support the legacy BSD date found in a default install within + # macOS and older distributions of Linux and Unix, a handful of helper + # functions can probably be created to handle every case of incompatibility + # between the two. Until that's implemented, it is probably best to warn + # the user that this will not work rather than having it silently bomb out + # during runtime. + if ! date --version >/dev/null 2>&1; then + echo "ERROR: You must have GNU date installed." + echo "If you're on macOS, please use brew to install this utility." + echo "Make sure the GNU version of date is symlinked to 'date', too." + exit 1 + fi } ################################################################################ @@ -107,16 +122,17 @@ function optionPicked() { # ARGS: $* (required): String # OUTS: String ################################################################################ -format_date() { - local date="${1}" - local outf="${2}" - local datef="${3:-"%b %d %H:%M:%S %Y %Z"}" # Tue Oct 24 13:34:22 2023 +0300 - if [[ "$OSTYPE" == "linux-gnu"* ]]; then +function format_date() { + # NOTE: While this works where it's implemented within the changelogs() + # function the first time, it then bombs out when it reaches the -d flag + # in the second half of that same code as BSD date cannot handle -d, nor + # can it handle a string such as DATE - 1 day. + local date="${1}" + local outf="${2}" + local datef="${3:-"%b %d %H:%M:%S %Y %Z"}" # Tue Oct 24 13:34:22 2023 +0300 local resp="$(date -d "${date}" "+${outf}")" - elif [[ "$OSTYPE" == "darwin"* ]]; then - local resp="$(date -j -f "${datef}" "${date}" "+${outf}")" - fi - printf "%s" "${resp}" + + printf "%s" "${resp}" } ################################################################################