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