Skip to content

Instantly share code, notes, and snippets.

@artifactsauce
Last active January 9, 2017 01:00
Show Gist options
  • Save artifactsauce/e3fd27c84abc208d39da2a90160268d0 to your computer and use it in GitHub Desktop.
Save artifactsauce/e3fd27c84abc208d39da2a90160268d0 to your computer and use it in GitHub Desktop.
ディレクトリを定期的に監視してGitリポジトリに間違ったEmailをコミットしない ref: http://qiita.com/artifactsauce/items/10141d6473bbb0e64cb1
$ git config user.email [email protected]
$ watch-git-email.sh /PATH/TO/DIRECTORY/ROOT [email protected] &
$ pkill -KILL -f watch-git-email.sh
while true; do
sleep $INTERVAL
# something to do
done
$ rm -rf .git
$ git init
echo -e "[$(date +"%x %T")] \033[35m[ERROR] $*\033[m" >&2
#!/usr/bin/env bash
set -eu
: ${DEBUG_MODE:=0}
: ${INTERVAL:=1} #監視間隔, 秒で指定
_.error() {
echo -e "[$(date +"%x %T")] \033[35m[ERROR] $*\033[m" >&2
}
_.debug() {
[ "$DEBUG_MODE" -eq 0 ] && return 0
echo -e "[$(date +"%x %T")] \033[34m[DEBUG] $*\033[m" >&2
}
_.get_stamp() {
ls -l --full-time $1 | awk '{print $1 $6 $7 $8 $9}' | openssl sha
}
_.run() {
local target_directory=$1
local expect_email=$2
_.debug "Find in $target_directory"
while read; do
_.debug "Change directory: $REPLY"
cd $(dirname $REPLY)
local actual_email=$(git config user.email)
if [ "$expect_email" = "$actual_email" ]; then
continue
fi
_.debug "Change git user emal in: $REPLY"
git config user.email $expect_email
done <<EOS
$(find $target_directory -type d -name ".git")
EOS
}
_.watch() {
local target_directory=$1
local last=$(_.get_stamp $target_directory)
while true; do
sleep $INTERVAL
local current=$(_.get_stamp $target_directory)
_.debug "Stamp: $current"
if [ "$last" = "$current" ]; then
continue
fi
_.run $*
last=$current
done
}
if [ $# -ne 2 ]; then
_.error "not enough arguments"
exit 1
fi
TARGET_DIRECTORY=$1
EXPECT_EMAIL=$2
_.watch $TARGET_DIRECTORY $EXPECT_EMAIL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment