-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgit-clone-or-pull
More file actions
executable file
·60 lines (52 loc) · 1.86 KB
/
git-clone-or-pull
File metadata and controls
executable file
·60 lines (52 loc) · 1.86 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
#!/bin/sh
# git-clone-or-pull: clone a GitHub repository, or pull it if already cloned.
#
# Usage: git-clone-or-pull ORG REPO_NAME [DESTINATION_PARENT] [GIT_CLONE_ARGS...]
# ORG is the GitHub organization
# REPO_NAME is the repository name (without the organization)
# DESTINATION_PARENT is the directory that contains the clone directory. It
# defaults to the current directory.
# GIT_CLONE_ARGS is extra arguments to git clone. It defaults to
# "-q --single-branch --depth 1" (without the quotes).
# It is not used if the destination already exists.
SCRIPT_NAME="$(basename "$0")"
if [ "$#" -lt 2 ]; then
echo "Usage: ${SCRIPT_NAME} ORG REPO_NAME [DESTINATION_PARENT] [GIT_CLONE_ARGS...]" >&2
exit 1
fi
beginswith() { case $2 in "$1"*) true ;; *) false ;; esac }
ORG=$1
shift
REPO_NAME=$1
shift
DESTINATION_PARENT=.
if [ "$#" -ne 0 ]; then
if ! beginswith "-" "$1"; then
# Does not start with "-" and therefore isn't a command-line argument.
DESTINATION_PARENT=$1
shift
if [ ! -d "${DESTINATION_PARENT}" ]; then
mkdir -p "${DESTINATION_PARENT}"
fi
fi
fi
if [ "$#" -eq 0 ]; then
# Default command-line arguments.
set -- -q --single-branch --depth 1
fi
REPO_URL="https://github.com/${ORG}/${REPO_NAME}.git"
DESTINATION="${DESTINATION_PARENT:?}/${REPO_NAME}"
# Try twice in case of network lossage. 60 seconds is not enough to clone a branch of the JDK.
if test -d "${DESTINATION}"; then
(cd "${DESTINATION}" && (timeout 180 git pull -q || (sleep 1m && (timeout 180 git pull || true))))
else
if ! timeout 180 git clone "$@" "${REPO_URL}" "${DESTINATION}"; then
rm -rf "${DESTINATION}"
sleep 180
if ! timeout 180 git clone "$@" "${REPO_URL}" "${DESTINATION}"; then
echo "Failed to clone ${REPO_URL}"
exit 1
fi
fi
fi
echo "${SCRIPT_NAME}: ${REPO_NAME} is at $(cd "${DESTINATION}" && git rev-parse HEAD)"