Skip to content

Instantly share code, notes, and snippets.

@emeric-martineau
Created December 7, 2020 17:01
Show Gist options
  • Save emeric-martineau/78b5004ccebc9dc1943c1625f05c653c to your computer and use it in GitHub Desktop.
Save emeric-martineau/78b5004ccebc9dc1943c1625f05c653c to your computer and use it in GitHub Desktop.
Funny script to replace git commit and change time of commit
#!/bin/sh
COUNTER_FILE="${HOME}/bin/.counter_date"
get_counter() {
# Read counter file
if [ -f "${COUNTER_FILE}" ]; then
# Check if file is same day that today
COUNTER_FILE_DATE=$(date -r "${COUNTER_FILE}" +"%Y%m%d")
CURRENT_FILE_DATE=$(date +"%Y%m%d")
if [ "${COUNTER_FILE_DATE}" -eq "${CURRENT_FILE_DATE}" ]; then
COUNTER=$(cat "${COUNTER_FILE}")
COUNTER=$(expr ${COUNTER} + 1)
else
# Reinit counter file
COUNTER=0
rm -f "${COUNTER_FILE}"
fi
else
COUNTER=0
fi
}
# Check only if git commit
if [ "$1" = "commit" ]; then
if [ -d '.git' ]; then
GIT_LOCAL_CONFIG=$(git config --list --local)
IS_GITHUB_REPOSITORY=$(echo ${GIT_LOCAL_CONFIG} | grep 'github')
IS_EMAIL=$(echo ${GIT_LOCAL_CONFIG}| grep 'user.email')
IS_USERNAME=$(echo ${GIT_LOCAL_CONFIG} | grep 'user.name')
if [ -n "${IS_GITHUB_REPOSITORY}" ]; then
if [ -n "${IS_EMAIL}" ] && [ -n "${IS_USERNAME}" ]; then
get_counter
# Take today, set hour to 19:00 and add minutes
NEW_DATE=$(date -d "19:00 today + ${COUNTER} minutes" +'%Y-%m-%d %H:%M:%S')
echo "** Change happy hour ! **"
echo ">> ${NEW_DATE}"
export GIT_COMMITTER_DATE="${NEW_DATE}"
export GIT_AUTHOR_DATE="${NEW_DATE}"
else
echo "fatal: this is github repository without username and email set !" >&2
exit 255
fi
fi
else
echo "fatal: not a git repository: .git" >&2
exit 128
fi
fi
git "$@"
RESULT=$?
unset GIT_COMMITTER_DATE
unset GIT_AUTHOR_DATE
# Counter set, save it only if git OK
if [ -n "${COUNTER}" ] && [ ${RESULT} -eq 0 ]; then
echo "${COUNTER}" > "${COUNTER_FILE}"
fi
exit ${RESULT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment