1
|
#!/bin/sh
|
2
|
#
|
3
|
# git_checkout.sh
|
4
|
#
|
5
|
# part of pfSense (https://www.pfsense.org)
|
6
|
# Copyright (c) 2004-2019 Rubicon Communications, LLC (Netgate)
|
7
|
# All rights reserved.
|
8
|
#
|
9
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
10
|
# you may not use this file except in compliance with the License.
|
11
|
# You may obtain a copy of the License at
|
12
|
#
|
13
|
# http://www.apache.org/licenses/LICENSE-2.0
|
14
|
#
|
15
|
# Unless required by applicable law or agreed to in writing, software
|
16
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
17
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18
|
# See the License for the specific language governing permissions and
|
19
|
# limitations under the License.
|
20
|
|
21
|
export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
|
22
|
|
23
|
scripts_path=$(dirname $(realpath $0))
|
24
|
|
25
|
if [ ! -f "${scripts_path}/common.subr" ]; then
|
26
|
echo >&2 "ERROR: common.subr is missing"
|
27
|
exit 1
|
28
|
fi
|
29
|
|
30
|
. ${scripts_path}/common.subr
|
31
|
|
32
|
usage() {
|
33
|
cat >&2 <<END
|
34
|
Usage: $(basename $0) -r repo_url -d destdir [-b branch] [-h]
|
35
|
|
36
|
Options:
|
37
|
-r repo_url -- URL of desired git repository
|
38
|
-d destdir -- Directory to clone
|
39
|
-b branch -- Branch or tag to clone (default: master)
|
40
|
-h -- Show this help and exit
|
41
|
|
42
|
Environment:
|
43
|
GIT_BIN -- Path to git binary
|
44
|
END
|
45
|
exit 1
|
46
|
}
|
47
|
|
48
|
branch="master"
|
49
|
while getopts r:d:b:h opt; do
|
50
|
case "$opt" in
|
51
|
r)
|
52
|
repo_url=$OPTARG
|
53
|
;;
|
54
|
d)
|
55
|
destdir=$OPTARG
|
56
|
;;
|
57
|
b)
|
58
|
branch=$OPTARG
|
59
|
;;
|
60
|
*)
|
61
|
usage
|
62
|
;;
|
63
|
esac
|
64
|
done
|
65
|
|
66
|
[ -z "$repo_url" ] \
|
67
|
&& err "repository URL is not defined"
|
68
|
|
69
|
[ -z "$destdir" ] \
|
70
|
&& err "destdir is not defined"
|
71
|
|
72
|
[ -e $destdir -a ! -d $destdir ] \
|
73
|
&& err "destdir already exists and is not a directory"
|
74
|
|
75
|
git=${GIT_BIN:-$(which git)}
|
76
|
if [ ! -x "${git}" ]; then
|
77
|
err "git binary is missing"
|
78
|
fi
|
79
|
|
80
|
if [ -d "${destdir}/.git" ]; then
|
81
|
current_url=$(${git} -C ${destdir} config --get remote.origin.url)
|
82
|
|
83
|
[ "${current_url}" != "${repo_url}" ] \
|
84
|
&& err \
|
85
|
"destination directory contains a different git repository"
|
86
|
|
87
|
run "Removing local changes from git repo ${repo_url} (${branch})" \
|
88
|
"${git} -C ${destdir} reset -q --hard"
|
89
|
run "Removing leftovers from git repo ${repo_url} (${branch})" \
|
90
|
"${git} -C ${destdir} clean -qfd"
|
91
|
run "Retrieving updates from git repo ${repo_url} (${branch})" \
|
92
|
"${git} -C ${destdir} fetch -q origin"
|
93
|
run "Updating git repo ${repo_url} (${branch})" \
|
94
|
"git -C ${destdir} checkout -q ${branch}"
|
95
|
|
96
|
# Detect if it's a branch and rebase it
|
97
|
if ${git} -C ${destdir} show-ref -q --verify refs/heads/${branch}; then
|
98
|
run "Rebasing git repo ${repo_url} (${branch})" \
|
99
|
"git -C ${destdir} rebase -q origin/${branch}"
|
100
|
fi
|
101
|
else
|
102
|
run "Cloning git repository ${repo_url} (${branch})" \
|
103
|
"git clone -q -b ${branch} ${repo_url} ${destdir}"
|
104
|
fi
|
105
|
|
106
|
exit 0
|