-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit-repo-watcher
More file actions
executable file
·163 lines (133 loc) · 4.12 KB
/
git-repo-watcher
File metadata and controls
executable file
·163 lines (133 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
default_interval_in_seconds=10
default_hooks_file="$script_dir""/git-repo-watcher-hooks"
print_usage() {
echo ""
echo "NAME"
echo " git-repo-watcher -- keeps a git repository in sync with its origin"
echo "SYNOPSIS"
echo " git-repo-watcher -d <directory> [-h <hooks-file>] [-i <interval>]"
echo "DESCRIPTION"
echo " The following options are available:"
echo " -d The path to the git repository"
echo " -i Watch interval time in seconds (defaults to 10 seconds)"
echo " -h Custom hooks file"
echo ""
exit 1
}
while getopts ":d:i:h:" options; do
case "${options}" in
d)
git_repository_dir=${OPTARG}
;;
h)
hooks_file=${OPTARG}
;;
i)
interval_in_seconds=${OPTARG}
;;
*)
print_usage
;;
esac
done
shift $((OPTIND - 1))
# Validating given git directory
if [[ -z "$git_repository_dir" ]]; then
echo -e "\nERROR: Git directory (-d) not given!" >&2
print_usage
fi
if [[ ! -d "$git_repository_dir/.git" ]] || [[ ! -r "$git_repository_dir/.git" ]]; then
echo "ERROR: Git directory (-d) not found: '$git_repository_dir/.git'!" >&2
print_usage
fi
if [[ ! -w "$git_repository_dir/.git" ]]; then
echo "ERROR: Missing write permissions for the git directory (-d): '$git_repository_dir/.git'!" >&2
print_usage
fi
# Convert to absolute file path if necessary
if [[ "$git_repository_dir" != /* ]]; then
git_repository_dir="$PWD/$git_repository_dir"
fi
# Validating given hook file
[[ -z "${hooks_file}" ]] && hooks_file="$default_hooks_file"
if [[ -f "${hooks_file}" ]] && [[ -r "${hooks_file}" ]]; then
# shellcheck source=git-repo-watcher-hooks
source "${hooks_file}"
else
echo "ERROR: Hooks (-h) file not found: '$hooks_file'" >&2
print_usage
fi
# Validating given interval
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
if [[ -f ".git/index.lock" ]]; then
echo "ERROR: Git repository is locked, waiting to unlock" >&2
sleep $interval_in_seconds
continue
fi
git fetch
repo_name=$(basename -s .git "$(git config --get remote.origin.url)")
previous_branch="$branch"
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
if [[ -z $branch ]]; then
echo "ERROR: Unable to get branch" >&2
exit 1
fi
if [[ -n $previous_branch ]] && [[ "$previous_branch" != "$branch" ]]; then
hook "branch_changed" "$repo_name" "$branch" "$previous_branch"
fi
upstream="$(git rev-parse --abbrev-ref --symbolic-full-name "@{u}" 2>/dev/null)"
# upstream was not configured
if [[ -z "$upstream" ]]; then
hook "upstream_not_set" "$repo_name" "$branch"
sleep $interval_in_seconds
continue
fi
git_local=$(git rev-parse @)
git_remote=$(git rev-parse "$upstream")
git_base=$(git merge-base @ "$upstream")
if [[ -z $started ]]; then
started=true
hook "startup" "$repo_name" "$branch"
fi
if [[ "$git_local" == "$git_remote" ]]; then
hook "no_changes" "$repo_name" "$branch"
elif [[ "$git_local" == "$git_base" ]]; then
hook "pull_change" "$repo_name" "$branch"
elif [[ "$git_remote" == "$git_base" ]]; then
hook "local_change" "$repo_name" "$branch"
else
hook "diverged" "$repo_name" "$branch"
fi
sleep $interval_in_seconds
done