Add --bare option, default to working directory clones

This commit is contained in:
vr8ce 2020-07-28 23:32:39 -05:00 committed by Alex Cabal
parent e27c63b41e
commit 24e8d1cec5

View file

@ -3,7 +3,7 @@ set -e
set -o pipefail
usage(){
fmt <<EOF
cat <<EOF
DESCRIPTION
Syncs books from standardebooks.org GitHub org to specified folder.
@ -13,6 +13,7 @@ USAGE
With -v or --verbosity 1, display general progress updates.
With -vv or --verbosity 2, display general progress updates and verbose git output.
With --update-only, only sync existing repositories, do not download new repositories.
With -b or --bare, clone a bare repository (for a server) instead of a working directory
With --token TOKEN, specify a GitHub access token to use for request. Useful for when you hit the rate limit.
DIRECTORY should be where the repositories should go.
@ -48,6 +49,7 @@ verbosity=0
updateOnly="false"
githubToken=""
target=""
bare=""
while [ $# -gt 0 ]; do
case "$1" in
@ -75,6 +77,10 @@ while [ $# -gt 0 ]; do
githubToken="$2"
shift 2
;;
-b|--bare)
bare="--bare"
shift 1
;;
*)
break ;;
esac
@ -164,13 +170,21 @@ fi
repoUrls=$(printf "%s" "${repoUrls}" | grep -v -e "/tools.git\$" -e "/web.git\$" -e "/manual.git\$" | awk 'NF')
printf "%s\n" "${repoUrls}" | while IFS= read -r repoUrl; do
# make sure it's not an empty string
[ -n "${repoUrl}" ] || continue
[ -d "${repoUrl##*/}" ] && continue
# strip everything prior to the last segment of the name
repoName="${repoUrl##*/}"
if [ "${bare}" = "" ]; then
repoName="${repoName%.git}"
fi
# if the repo already exists, skip it (handled in the update above)
[ -d "${repoName}" ] && continue
repoNameLength=$(printf "%s" "${repoName}" | wc -m)
if [ "${repoNameLength}" -ge 100 ]; then
if dirs=( "${repoName%%.git}"*/ ) && [[ -d ${dirs[0]} ]]; then
if dirs=( "${repoName}"*/ ) && [[ -d ${dirs[0]} ]]; then
continue
fi
fi
@ -180,9 +194,9 @@ printf "%s\n" "${repoUrls}" | while IFS= read -r repoUrl; do
fi
if [ "${verbosity}" -lt 2 ]; then
git clone -q --bare "${repoUrl}"
git clone -q ${bare} "${repoUrl}"
else
git clone -v --bare "${repoUrl}"
git clone -v ${bare} "${repoUrl}"
fi
if ! [ -d "${repoName}" ]; then
@ -196,11 +210,14 @@ printf "%s\n" "${repoUrls}" | while IFS= read -r repoUrl; do
sed -E "s/<[^>]+?>//g" |
sed -E "s|url:https://standardebooks.org/ebooks/||g" |
sed -E "s|/|_|g").git"
if [ "${bare}" = "" ]; then
properName="${properName%.git}"
fi
if [ "${repoUrl##*/}" != "${properName}" ]; then
if [ "${repoName}" != "${properName}" ]; then
if [ "${verbosity}" -gt 0 ]; then
printf "Moving %s to %s\n" "${repoName}" "${properName}"
fi
mv "${repoName}" "${properName}"
fi
done
done