diff options
Diffstat (limited to 'mac/.qutebrowser/userscripts/gitclone')
| -rwxr-xr-x | mac/.qutebrowser/userscripts/gitclone | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/mac/.qutebrowser/userscripts/gitclone b/mac/.qutebrowser/userscripts/gitclone new file mode 100755 index 0000000..ad62d17 --- /dev/null +++ b/mac/.qutebrowser/userscripts/gitclone @@ -0,0 +1,74 @@ +#!/bin/sh +# +# Author: palb91 +# Date: 2022 +# +# Clone a git repository directly from qutebrowser +# +# In config.py: +# bind('gc', 'spawn -u -- gitclone') +# +# # Run a shell command after successful clone +# import os +# os.environ['QUTE_POST_CLONE'] = 'notify-send "git clone" "${QUTE_URL}"' + +set -e + +# Local storage +BASE_DIR="${HOME}"/Public/repos +TEMP_DIR="$(mktemp -d)" + +# Get informations from userscripts variables +QUTE_URL="${QUTE_URL%%\#*}" +QUTE_URL="${QUTE_URL%%\?*}" +QUTE_URL="${QUTE_URL%%\&*}" + +DOMAIN="${QUTE_URL#*//}" +DOMAIN="${DOMAIN%%/*}" + +REPO="${QUTE_URL#*"${DOMAIN}"/}" +REPO="${REPO%/}" +REPO="${REPO##*/}" +[ "${REPO#\.}" != "${REPO}" ] && REPO="_${REPO}" + +BASE_REPO="${BASE_DIR}/${DOMAIN}/${REPO}" +TEMP_REPO="${TEMP_DIR}/${DOMAIN}/${REPO}" + +# logging +info() { printf 'message-info "%s"\n' "${*}" >>"${QUTE_FIFO}"; } +warn() { printf 'message-warning "%s"\n' "${*}" >>"${QUTE_FIFO}"; } +err() { + printf 'message-error "%s"\n' "${*}" >>"${QUTE_FIFO}" + return 1 +} +clean() { + rm -rf "${TEMP_DIR}" + exit "${1:-0}" +} + +# Check repo exists +if [ -d "${BASE_REPO}"/.git ]; then + warn "${REPO} already cloned in ${BASE_REPO}" + clean 0 + +# Try cloning +else + info "Cloning ${DOMAIN}/${REPO}..." + git clone "${QUTE_URL}" "${TEMP_REPO}" || + err "Error while cloning ${DOMAIN}/${REPO}, is it a repository?" + + if [ ! -d "${TEMP_REPO}"/.git ]; then + err 'An error occured, cloning failed...' + clean 2 + fi +fi + +# Move the temp folder to its final destination +[ -d "${BASE_REPO%/*}" ] || mkdir -p "${BASE_REPO%/*}" +mv "${TEMP_REPO}" "${BASE_REPO}" +info "${REPO} successfully cloned in ${BASE_REPO}" + +# Run post hook +if [ -n "${QUTE_POST_CLONE}" ]; then + eval "${QUTE_POST_CLONE}" +fi |
