Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable overwriting the pull behavior #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions git-repo-watcher
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,35 @@ print_usage() {
exit 1
}

# Executes user hooks
#
# $1 - Hook name
# $2-$4 - Hook arguments
hook() {
hook_name="$1"
shift
if [[ "$(type -t "$hook_name")" == "function" ]]; then
eval "$hook_name $*"
fi
}

# Pulls commit from remote git repository
#
# $1 - Git repository name
# $2 - Branch name
pull_change() {
git pull
exit_code=$?

commit_message=$(git log -1 --pretty=format:"%h | %an | %ad | %s")

if [[ $exit_code -eq 1 ]]; then
hook "pull_failed" "$1" "$2" "$commit_message"
else
hook "change_pulled" "$1" "$2" "$(printf '%q\n' "$commit_message")"
fi
}

while getopts ":d:i:h:o" options; do
case "${options}" in
d)
Expand Down Expand Up @@ -79,35 +108,6 @@ if [[ -z "$interval_in_seconds" ]]; then
interval_in_seconds="$default_interval_in_seconds"
fi

# Executes user hooks
#
# $1 - Hook name
# $2-$4 - Hook arguments
hook() {
hook_name="$1"
shift
if [[ "$(type -t "$hook_name")" == "function" ]]; then
eval "$hook_name $*"
fi
}

# Pulls commit from remote git repository
#
# $1 - Git repository name
# $2 - Branch name
pull_change() {
git pull
exit_code=$?

commit_message=$(git log -1 --pretty=format:"%h | %an | %ad | %s")

if [[ $exit_code -eq 1 ]]; then
hook "pull_failed" "$1" "$2" "$commit_message"
else
hook "change_pulled" "$1" "$2" "$(printf '%q\n' "$commit_message")"
fi
}

while true; do

cd "$git_repository_dir" || exit 1
Expand Down