-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdegit
executable file
·215 lines (197 loc) · 7.01 KB
/
degit
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
#!/usr/bin/env bash
set -euo pipefail
YP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)"
source ${YP_DIR}/sh/common.inc.sh
#- degit 1.0
## Usage: degit -- URI
## Straightforward project scaffolding.
## Downloads a history-less git repository from Bitbucket/Github/Gitlab/Sourcehut.
## Requires git version 2.26+ for --history.
##
## A bash subset version of https://github.com/Rich-Harris/degit.
## - no optimizations that include reading/writing access.json and map.json,
## thus only offline if given a git branch/tag/hash
## - no '--mode', no git mode, just tar mode
## - add '--history' to clone git history (but not the blobs)
##
## -h, --help Display this help and exit
## -v, --version Output version information and exit
TMP_DEGIT=$(mktemp -d -t yplatform.XXXXXXXXXX)
function on_exit() {
rm -rf ${TMP_DEGIT}
}
trap on_exit EXIT
HISTORY=false
if { getopt --test >/dev/null 2>&1 && false; } || [[ "$?" = "4" ]] || false; then
ARGS=$(getopt -o hv -l help,history,version -n $(basename ${BASH_SOURCE[0]}) -- "$@") || sh_script_usage
eval set -- "${ARGS}"
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--history)
HISTORY=true
shift
;;
-h|--help)
sh_script_usage
;;
-v|--version)
sh_script_version
;;
--)
shift
break
;;
-*)
sh_script_usage
;;
*)
break
;;
esac
done
# [[ $# -eq 0 ]] || sh_script_usage
# example URIs
# https://github.com/ysoftwareab/yplatform
# https://gitlab.com/mikecardwell/safernode
# https://bitbucket.org/atlassian/openapi-diff
# https://git.sr.ht/~nsh/emacs.d
# ysoftwareab/yplatform
# github:ysoftwareab/yplatform
# https://github.com/ysoftwareab/yplatform/bin
# https://github.com/ysoftwareab/yplatform/bin#<commit-ish>
URI="$1"
[[ "${URI}" =~ ^.+://.+$ ]] || {
REPO_SERVICE="$(echo "${URI}" | cut -d":" -f1)"
URI_RPATH_FRAGMENT="$(echo "${URI}" | cut -d":" -f2)"
[[ "${URI}" =~ ^[^:]+:.+$ ]] || REPO_SERVICE=github
case ${REPO_SERVICE} in
bitbucket)
URI="https://bitbucket.org/${URI_RPATH_FRAGMENT}"
;;
github)
URI="https://github.com/${URI_RPATH_FRAGMENT}"
;;
gitlab)
URI="https://gitlab.com/${URI_RPATH_FRAGMENT}"
;;
git.sr.ht)
URI="https://git.sr.ht/${URI_RPATH_FRAGMENT}"
;;
*)
echo_err "Unknown service ${REPO_SERVICE}."
exit 1
esac
}
exe ${YP_DIR}/bin/parse-uri "${URI}"
# TODO sourcing in Bash 3 (macos) gives: parse-uri: line 47: echo: write error: Broken pipe
# source <(${YP_DIR}/bin/parse-uri "${URI}")
eval "$(${YP_DIR}/bin/parse-uri "${URI}")"
REPO_SERVICE=${URI_HOST}
REPO_SERVICE=${REPO_SERVICE/%.com/}
REPO_SERVICE=${REPO_SERVICE/%.org/}
REPO_ORG="$(echo "${URI_RPATH}" | tr "/" "\n" | head -n1)"
REPO_NAME="$(echo "${URI_RPATH}" | tr "/" "\n" | head -n2 | tail -n1 | sed "s/\.git$//")"
REPO_DIR="/$(echo "${URI_RPATH}" | tr "/" "\n" | tail -n +3 | tr "\n" "/")/"
REPO_DIR="$(echo "${REPO_DIR}" | tr -s "/")"
REPO_REF=${URI_FRAGMENT:-HEAD}
REPO_URL=${URI_SCHEME}://${URI_USER}${URI_USER:+@}${URI_HOST}/${REPO_ORG}/${REPO_NAME}
if echo "${REPO_REF}" | grep -q "^[0-9a-f]\+$"; then
REPO_HASH="${REPO_REF}"
else
REPO_HASH=$(git ls-remote ${REPO_URL} | grep $'\t'"\(refs/\(heads\|tags\)/\)\?${REPO_REF}$" | head -n1 | cut -d$'\t' -f1)
[[ -n "${REPO_HASH}" ]] || {
echo_err "Git ref ${REPO_REF} could not be found."
exit 1
}
fi
TAR_FILE=${HOME}/.degit/${REPO_SERVICE}/${REPO_ORG}/${REPO_NAME}/${REPO_HASH}.tar.gz
case ${URI_HOST} in
bitbucket.org)
TAR_URL="https://bitbucket.org/${REPO_ORG}/${REPO_NAME}/get/${REPO_HASH}.tar.gz"
;;
git.sr.ht)
TAR_URL="https://git.sr.ht/${REPO_ORG}/${REPO_NAME}/archive/${REPO_HASH}.tar.gz"
;;
github.com)
TAR_URL="https://github.com/${REPO_ORG}/${REPO_NAME}/archive/${REPO_HASH}.tar.gz"
;;
gitlab.com)
TAR_URL="https://gitlab.com/${REPO_ORG}/${REPO_NAME}/-/archive/${REPO_HASH}/${REPO_NAME}.tar.gz"
;;
*)
echo_err "Unknown host ${URI_HOST}."
exit 1
esac
mkdir -p $(dirname "${TAR_FILE}")
if [[ -e "${TAR_FILE}" ]]; then
echo_info "Using cached ${TAR_FILE}..."
else
echo_do "Fetching ${TAR_URL} to ${TAR_FILE}..."
curl -qfsSL "${TAR_URL}" -o "${TAR_FILE}"
echo_done
fi
# wrapping directory is sometimes not stable (${REPO_ORG}-)?${REPO_NAME}-(${REPO_HASH}|${REPO_HASH_SHORT})
# TAR_COMPONENT_1=$(tar -tf "${TAR_FILE}" | head -n1 | cut -d"/" -f1 || exit_allow_sigpipe)
TAR_COMPONENT_1=$(tar -tf "${TAR_FILE}" | sed -n '1p' | cut -d"/" -f1)
echo_info "Cloning into ${PWD}..."
# see https://www.gnu.org/software/tar/
# --verbatim-files-from introduced in 1.29 2016-05-16
# --skip-old-files introduced in 1.27 2013-10-05
# --transform introduced in 1.15.91 2006-06-16
if tar --version | head -n1 | grep -q "^tar (GNU tar) \(1\.29\|1\.[3-9][0-9]\+\)"; then
echo_do "Extracting ${TAR_FILE}${REPO_DIR} with GNU tar..."
tar -xf "${TAR_FILE}" \
--skip-old-files \
--files-from <(tar -tf "${TAR_FILE}" | grep "^${TAR_COMPONENT_1}${REPO_DIR}.\+[^/]$") \
--verbatim-files-from \
--transform "s,^${TAR_COMPONENT_1}${REPO_DIR},,"
else
echo_do "Extracting ${TAR_FILE}${REPO_DIR} with old-GNU/non-GNU tar..."
[[ -z "$(find . -mindepth 1)" ]] || {
echo_err "Extracting with old-GNU/non-GNU tar requires an empty folder."
find . -print0 | xargs -0 ls -lad >&2
exit 1
}
tar -xf "${TAR_FILE}" \
--files-from <(tar -tf "${TAR_FILE}" | grep "^${TAR_COMPONENT_1}${REPO_DIR}.\+[^/]$")
(
cd "${TAR_COMPONENT_1}"
find . -mindepth 1 -maxdepth 1 -print0 | xargs -0 -I{} mv "{}" ../
cd -
rm -r "${TAR_COMPONENT_1}"
)
fi
echo_done
[[ "${HISTORY}" != "true" ]] || {
# --filter=blob:none introduced in 2.26 2020-03-22
exe git clone --filter=blob:none --no-checkout ${REPO_URL} ${TMP_DEGIT}
exe mv ${TMP_DEGIT}/.git .
exe git reset --soft ${REPO_HASH} || {
# NOTE: a hack to retry due to spurious CI errors due to .git/index.lock file exists
# NOTE: checking for .git/index.lock reveals the file is not there
exe ls -la .
exe ls -la .git
exe sleep 5
exe git reset --soft ${REPO_HASH}
exit 1
}
exe git add . || {
# NOTE: a hack to retry due to spurious CI errors due to .git/index.lock file exists
# NOTE: checking for .git/index.lock reveals the file is not there
exe ls -la .
exe ls -la .git
exe sleep 5
exe git add .
exit 1
}
# unsure why some files are still staged
exe git reset -q || {
# NOTE: a hack to retry due to spurious CI errors due to .git/index.lock file exists
# NOTE: checking for .git/index.lock reveals the file is not there
exe ls -la .
exe ls -la .git
exe sleep 5
exe git reset -q
}
}