|
#!/bin/bash |
|
|
|
# This script executes a git operation using the file modification date |
|
# (retrieved by the stat command) described by this wiki page: |
|
# https://github.com/syntechdev/powerpanel/wiki/FunWithGitAndGithub#git-commits-using-file-mod-dates |
|
|
|
# Unfortunately stat has different syntax on the Mac and on Linux (Grrr!) so to get a file date: |
|
# On a Mac system execute: |
|
# stat -f "%Sm" sql/parsed-payload-tracker.sql |
|
# On a linux system execute: |
|
# stat -c "%y" sql/parsed-payload-tracker.sql |
|
|
|
# enable for debugging to echo commands as run |
|
set -x |
|
|
|
# extended pattern matching |
|
shopt -s extglob |
|
|
|
|
|
if [ $# -le 1 ] ; then |
|
echo "This script requires at least 3 parameters, with double-quotes used if they contain spaces; and can take a 3rd param as the git add file list or the git commit message:" |
|
echo " * Param 1: Git operation (add|mv|commit)" |
|
echo " * Param 2: File to use to get date-time source" |
|
echo " * Param 3: File(s) or directory to pass to the git command op (only one param works currently)" |
|
echo " OR Git commit message (must be in quotes if it includes spaces)" |
|
exit |
|
fi |
|
|
|
gitop=$1 |
|
statfile=$2 |
|
|
|
platform='unknown' |
|
platform=`uname` |
|
#echo "uname result: ** $platform **" |
|
|
|
#if [[ "$platform" == "Darwin" ]]; then |
|
case "$platform" in |
|
"Darwin") |
|
statcmd="stat -f %Sm $statfile" |
|
;; |
|
"Linux") |
|
#elif [[ "$platform" == "Linux" ]]; then |
|
statcmd="stat -c %y $statfile" |
|
;; |
|
#else |
|
*) |
|
# Unknown platform |
|
echo "Uname returned an unexpected result so can't identify platform" |
|
exit |
|
;; |
|
esac |
|
#fi |
|
|
|
#echo "$statcmd" |
|
statdatetime=$($statcmd) |
|
echo "File $statfile datetime reported by stat: $statdatetime" |
|
#gitfiles=${3:-$statfile} |
|
#gitmessage=${@:4} |
|
|
|
#if [[ "$gitop" == ("add"|"mv") ]] ; then |
|
case "$gitop" in |
|
+("add"|"mv")) # gitop matches add or mv |
|
# Add the files in the sql/ sub-directory |
|
gitfiles=${@:3} |
|
echo "*** $gitfiles ***" |
|
#GIT_AUTHOR_DATE="`stat -f "%Sm" sql/parsed-payload-tracker.sql`" GIT_COMMITTER_DATE="`stat -f "%Sm" sql/parsed-payload-tracker.sql`" git $gitop sql/ |
|
# echo GIT_AUTHOR_DATE="$statdatetime" GIT_COMMITTER_DATE="$statdatetime" git $gitop $gitfiles |
|
GIT_AUTHOR_DATE="$statdatetime" GIT_COMMITTER_DATE="$statdatetime" git $gitop $gitfiles |
|
|
|
# Confirm that the correct files are staged for commit |
|
git status |
|
;; |
|
#elif [[ "$gitop" == "commit" ]] ; then |
|
"commit") # gitop matches commit |
|
# Commit the files in the sql/ sub-directory |
|
gitmessage=${@:3} |
|
echo "*** $gitmessage ***" |
|
# GIT_AUTHOR_DATE="`stat -f "%Sm" sql/parsed-payload-tracker.sql`" GIT_COMMITTER_DATE="`stat -f "%Sm" sql/parsed-payload-tracker.sql`" \ |
|
# git commit -m "Committing sql parsing files created in January 2014, prior to committing other changes" |
|
# echo GIT_AUTHOR_DATE="$statdatetime" GIT_COMMITTER_DATE="$statdatetime" git commit -m $gitmessage |
|
GIT_AUTHOR_DATE="$statdatetime" GIT_COMMITTER_DATE="$statdatetime" git commit -m "$gitmessage" |
|
;; |
|
#else |
|
*) |
|
echo "You entered [ $gitop ] as the Git operation but it was not recognized" |
|
;; |
|
esac |
|
#fi |