-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathpull-repository.sh
executable file
·228 lines (206 loc) · 5.48 KB
/
pull-repository.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/sh
#
# Pulls or clones the repository given as url or directory.
# Fast-forward merges remote origin branches.
# Allows to run a command for each updated branch.
#
# Usage: ./pull-repository.sh [-b branches] [url|dir] [-- comand [args...]]
#
# The "-b" option defines a whitespace-separated list of branches to merge.
#
# Copyright 2016, Sebastian Tschan
# https://blueimp.net
#
# Licensed under the MIT license:
# https://opensource.org/licenses/MIT
#
# Exit immediately if a command exits with a non-zero status:
set -e
# Color codes:
c031='\033[0;31m' # red
c032='\033[0;32m' # green
c033='\033[0;33m' # yellow
c036='\033[0;36m' # cyan
c0='\033[0m' # no color
# Prints the given string in a highlight color:
highlight() {
echo "${c036}$1${c0}"
}
# Prints the given string in a success color:
success() {
echo "${c032}$1${c0}"
}
# Prints the given string in a warning color:
warning() {
echo "${c033}$1${c0}"
}
# Prints the given string in an error color:
error() {
echo "${c031}$1${c0}"
}
# Prints the given error message and exits:
error_exit() {
error "$1" >&2
echo "Usage: $0 [-b branches] [-c command] url" >&2
exit 1
}
# Prints a list of all local branches that track an upstream branch:
local_upstream_branches() {
git for-each-ref --format='%(refname:short) %(upstream)' refs/heads/ |
grep -w refs/remotes | grep -Eo '^[^ ]+'
}
# Checks if the given branch exists:
branch_exists() {
if ! git show-ref --verify -q "refs/remotes/origin/$1"; then
echo "$(highlight "$1") $(warning 'not found')" >&2
return 1
fi
}
# Attempts a fast-forward merge for the given branch:
fast_forward_merge() {
branch_exists "$1" || return $?
status=0
tracking_status=$(git for-each-ref --format='%(push:trackshort)' \
"refs/heads/$1")
if [ "$tracking_status" = '<>' ]; then
echo "$(highlight "$1") $(error 'has diverged')" >&2
return 1
elif [ "$tracking_status" = '=' ]; then
echo "$(highlight "$1") $(success 'is up-to-date')"
return
elif [ "$tracking_status" = '>' ]; then
echo "$(highlight "$1") $(success 'is ahead')"
return
elif [ "$tracking_status" = '<' ]; then
# Create a local copy of the remote branch:
git branch --quiet "origin/$1" "origin/$1"
# Put the working copy into a detached-head state, to allow branch merges:
git checkout --detach --quiet
# Attempt a fast-forward merge with the local copy of the remote branch:
git fetch --quiet . "origin/$1:$1" || status=$?
# Check out the previous working copy:
git checkout --quiet -
# Delete the local copy of the remote branch:
git branch -D --quiet "origin/$1"
else
# The remote branch has not been fetched yet:
git fetch --quiet origin "$1:$1" || status=$?
fi
if [ $status -eq 0 ]; then
echo "$(highlight "$1") $(success 'has been updated')"
else
echo "$(highlight "$1") $(error 'update failed')" >&2
fi
return $status
}
# Checks if fast-forward merge is enabled:
fast_forward_merge_enabled() {
if [ "$(git config merge.ff)" = 'no' ]; then
error 'git fast-forward merge disabled' >&2
global_arg=
[ "$(git config --global merge.ff)" = 'no' ] && global_arg=' --global'
echo 'Run the following command to enable it:' >&2
highlight "git config$global_arg merge.ff yes" >&2
exit 1
fi
}
# Returns the selected branches:
get_branches() {
if [ -z "$BRANCHES" ]; then
BRANCHES=$(local_upstream_branches)
fi
printf %s "$BRANCHES"
}
# Pulls the selected branches of the given repository directory:
pull() {
cd "$1"
fast_forward_merge_enabled || return $?
git fetch --quiet origin
pull_status=0
for branch in $(get_branches); do
fast_forward_merge "$branch" || pull_status=$?
done
return $pull_status
}
# Clones the given repository url:
clone() {
if git clone --quiet "$1" "$2"; then
echo "$(highlight "$2") $(success 'cloned')"
cd "$2"
else
echo "$(highlight "$1") $(error 'clone failed')" >&2
return 1
fi
}
# Clones or pulls the given repository:
update_repository() {
if [ -d "$1" ]; then
pull "$1"
else
repo_base=$(basename "$1")
dir=${repo_base%.git}
if [ -d "$dir" ]; then
pull "$dir"
else
clone "$1" "$dir"
fi
fi
return $?
}
# Checks out the given branch, throwing away local changes:
clean_checkout() {
git checkout --force --quiet "$1"
}
# Runs the COMMAND on the given branch:
execute_on_branch() {
branch_exists "$1" || return $?
branch=$1
shift
stashed=false
if ! git diff --quiet || ! git diff --cached --quiet; then
# Stash the current working directory changes:
git stash --quiet
stashed=true
fi
# Remember the current branch:
current_branch=$(git rev-parse --abbrev-ref HEAD)
# Checkout the given one:
[ "$current_branch" != "$branch" ] && clean_checkout "$branch"
status=0
# Execute the given command:
eval "$@" || status=$?
# Checkout the original branch:
[ "$current_branch" != "$branch" ] && clean_checkout "$current_branch"
if [ "$stashed" = true ]; then
# Re-apply the stashed changes:
git stash pop --quiet
fi
return $status
}
# Execute the given command on each selected branch:
execute() {
[ "$#" -eq 0 ] && return
execute_status=0
for branch in $(get_branches); do
execute_on_branch "$branch" "$@" || execute_status=$?
done
return $execute_status
}
if [ "$1" = -b ]; then
BRANCHES=$2
shift 2
fi
if [ "$2" = -- ]; then
REPO=$1
shift 2
elif [ "$1" = -- ]; then
REPO=.
shift
elif [ -n "$1" ]; then
REPO=$1
shift
else
REPO=.
fi
update_repository "$REPO"
execute "$@"