Files
dotfiles/.local/share/nautilus/scripts/Misc/Git clone (URLs in clipboard)

106 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# install_keyboard_shortcut=<Control><Shift>G
# Source the script 'common-functions.sh'.
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
ROOT_DIR=$(grep --only-matching "^.*scripts[^/]*" <<<"$SCRIPT_DIR")
source "$ROOT_DIR/common-functions.sh"
_main() {
local output_dir=""
# Execute initial checks.
case "${XDG_SESSION_TYPE,,}" in
"x11") _check_dependencies "command=git | command=xclip" ;;
"wayland") _check_dependencies "command=git | command=wl-paste; package=wl-clipboard" ;;
*)
_display_error_box "Unknown session type!"
_exit_script
;;
esac
_display_wait_box "2"
output_dir=$(_get_working_directory_alternative)
# Execute the function '_main_task' for each file in parallel.
_run_task_parallel "$(_get_urls_from_clipboard)" "$output_dir"
_display_result_box "$output_dir"
}
_main_task() {
local input_file=$1
local output_dir=$2
local std_output=""
cd "$output_dir" || return 1
# Run the main process.
std_output=$(git clone -q -- "$input_file" 2>&1)
_check_output "$?" "$std_output" "$input_file" "" || return 1
}
_get_urls_from_clipboard() {
local urls=""
if [[ "${XDG_SESSION_TYPE,,}" == "x11" ]]; then
urls=$(xclip -quiet -selection clipboard -o)
elif [[ "${XDG_SESSION_TYPE,,}" == "wayland" ]]; then
urls=$(wl-paste)
fi
urls=$(grep --only-matching --perl-regexp "((git|ssh|http(s)?)|(git@[\w\.]+))(.*)(\.git)" <<<"$urls")
urls=$(sort -u <<<"$urls")
if [[ -z "$urls" ]]; then
_display_error_box "There are no valid Git URLs in the clipboard!"
_exit_script
fi
_convert_text_to_filenames "$urls"
}
_get_working_directory_alternative() {
local working_directory=""
# Try to use the information provided by the file manager.
if [[ -v "CAJA_SCRIPT_CURRENT_URI" ]]; then
working_directory=$CAJA_SCRIPT_CURRENT_URI
elif [[ -v "NEMO_SCRIPT_CURRENT_URI" ]]; then
working_directory=$NEMO_SCRIPT_CURRENT_URI
elif [[ -v "NAUTILUS_SCRIPT_CURRENT_URI" ]]; then
working_directory=$NAUTILUS_SCRIPT_CURRENT_URI
fi
if [[ -n "$working_directory" ]] && [[ "$working_directory" == "file://"* ]]; then
working_directory=$(_text_uri_decode "$working_directory")
else
# Files selected in the search screen (or orther possible cases).
working_directory=""
fi
if [[ -z "$working_directory" ]]; then
# NOTE: The working directory can be detected by using the directory name
# of the first input file. Some file managers do not send the working
# directory for the scripts, so it is not precise to use the 'pwd' command.
local item_1=""
local item_2=""
item_1=$(cut -d "$FIELD_SEPARATOR" -f 1 <<<"$INPUT_FILES")
item_2=$(cut -d "$FIELD_SEPARATOR" -f 2 <<<"$INPUT_FILES")
if [[ -n "$item_1" ]]; then
if [[ -n "$item_2" ]] && [[ "$item_1" != "$item_2" ]]; then
working_directory=$(_get_filename_dir "$item_1")
elif [[ -f "$item_1" ]]; then
working_directory=$(_get_filename_dir "$item_1")
elif [[ -d "$item_1" ]]; then
working_directory=$(_get_filename_full_path "$item_1")
fi
else
working_directory=$(pwd)
fi
fi
printf "%s" "$working_directory"
}
_main "$@"