changeset 937:23e99ecee75d

Increased the number of supported formats by mygit-date and added strict validation.
author Oleksandr Gavenko <gavenkoa@gmail.com>
date Sat, 18 Jan 2020 00:19:38 +0200
parents 911c895259eb
children 33a991b082cf
files .bashrc
diffstat 1 files changed, 22 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/.bashrc	Mon Jan 13 14:00:42 2020 +0200
+++ b/.bashrc	Sat Jan 18 00:19:38 2020 +0200
@@ -251,15 +251,31 @@
   ag --nofilename --count -- "$1" | awk '{c += $1} END {print c}'
 }
 
-# "YYYY-MM-DD hh:mm:ss" or "hh:mm" for current day and random sec.
 mygit-date() {
-  if [[ "02:23" =~ ^[0-9]{2}:[0-9]{2}$ ]]; then
-      local sec=$(($RANDOM % 60))
-      if [[ ${#sec} = 1 ]]; then
-          sec=0$sec
-      fi
+  if [[ -n "$2" ]]; then
+    echo Provide only one arg.
+    return 1;
+  fi
+  local sec=$(($RANDOM % 60))
+  if [[ ${#sec} = 1 ]]; then
+      sec=0$sec
+  fi
+  if [[ "$1" =~ ^[0-9]{2}:[0-9]{2}$ ]]; then
       local yyyymmdd=`date +%F`
       set "$yyyymmdd $1:$sec"
+  elif [[ "$1" =~ ^[0-9]{2}' '[0-9]{2}:[0-9]{2}$ ]]; then
+      local yyyymm=`date +%Y-%m`
+      set "$yyyymm-$1:$sec"
+  elif [[ "$1" =~ ^[0-9]{2}-[0-9]{2}' '[0-9]{2}:[0-9]{2}$ ]]; then
+      local yyyy=`date +%Y`
+      set "$yyyy-$1:$sec"
+  elif [[ "$1" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}' '[0-9]{2}:[0-9]{2}$ ]]; then
+      set "$1:$sec"
+  elif [[ "$1" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}' '[0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]; then
+      :
+  else
+    echo 'Unsupported format, try hh:mm, "dd hh:mm", "MM-dd hh:mm", "yyyy-MM-dd hh:mm", "yyyy-MM-dd hh:mm:ss"'
+    return 1;
   fi
   echo GIT_COMMITTER_DATE='"'"$1"'"' git commit --amend --no-edit --date '"'"$1"'"'
   GIT_COMMITTER_DATE="$1" git commit --amend --no-edit --date "$1"