Last active
November 8, 2024 17:12
-
-
Save alifeee/bb0499e1272f312b82497031d28e91f2 to your computer and use it in GitHub Desktop.
quickly clone a github repository (Gnome desktop launcher)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://gist.github.com/alifeee/bb0499e1272f312b82497031d28e91f2 | |
# place in /usr/share/applications/clone.desktop | |
[Desktop Entry] | |
Version=0.1.0 | |
Name=Git Clone | |
Comment=Clone a GitHub repository | |
Exec=/home/alifeee/Desktop/clone.sh | |
Terminal=true | |
Type=Application | |
Categories=Utility;Application; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# quickly clone a GitHub repository | |
# 1. take user input of a GitHub repository | |
# 2. attempt to pattern match to an actual repository | |
# 3. attempt to clone it | |
# 4. open in file explorer or code editor | |
# made by alifeee | |
# version 0.1.0 | |
# https://gist.github.com/alifeee/bb0499e1272f312b82497031d28e91f2 | |
BASE="/home/alifeee/git" | |
echo "will clone to ${BASE}" | |
read -p "repository: " SSH_LOC | |
echo " input: ${SSH_LOC}" | |
# e.g., [email protected]:alifeee/blog.git | |
PAT_FULL="^git@github\.com:([^\/]*)\/([^\/]*).git$" | |
# e.g., [email protected]:alifeee/blog | |
PAT_WITHOUT_EXTENSION="^git@github\.com:([^\/]*)\/([^\/]*)$" | |
# e.g., alifeee/blog | |
PAT_OWNER_REPO="^([^\/]*)\/([^\/]*)$" | |
# e.g., blog (only works when specifying default github account) | |
PAT_REPO="^([^\/]*)$" | |
DEFAULT_GITHUB_ACCOUNT="alifeee" | |
# e.g., github.com/alifeee/blog.git | |
PAT_HTTPS_DOTGIT="github\.com\/([^\/]*)\/([^\/]*).git$" | |
# e.g., github.com/alifeee/blog... | |
PAT_HTTPS="github\.com\/([^\/]*)\/([^\/]*)" | |
if [[ "${SSH_LOC}" =~ $PAT_FULL ]]; then | |
owner="${BASH_REMATCH[1]}" | |
repo="${BASH_REMATCH[2]}" | |
elif [[ "${SSH_LOC}" =~ $PAT_WITHOUT_EXTENSION ]]; then | |
owner="${BASH_REMATCH[1]}" | |
repo="${BASH_REMATCH[2]}" | |
elif [[ "${SSH_LOC}" =~ $PAT_OWNER_REPO ]]; then | |
owner="${BASH_REMATCH[1]}" | |
repo="${BASH_REMATCH[2]}" | |
elif [[ "${SSH_LOC}" =~ $PAT_REPO ]]; then | |
owner="${DEFAULT_GITHUB_ACCOUNT}" | |
repo="${BASH_REMATCH[1]}" | |
elif [[ "${SSH_LOC}" =~ $PAT_HTTPS_DOTGIT ]]; then | |
owner="${BASH_REMATCH[1]}" | |
repo="${BASH_REMATCH[2]}" | |
elif [[ "${SSH_LOC}" =~ $PAT_HTTPS ]]; then | |
owner="${BASH_REMATCH[1]}" | |
repo="${BASH_REMATCH[2]}" | |
else | |
read -s -n1 -p "no match type found :(" | |
exit 1 | |
fi | |
printf " parsed: (%s) (%s)\n" "${owner}" "${repo}" | |
cloneme="[email protected]:${owner}/${repo}.git" | |
echo " attempting to clone ${cloneme}" | |
(cd "${BASE}"; git clone "${cloneme}") | |
folder=$(echo "${cloneme}" | pcregrep -o2 "${PAT_FULL}") | |
fullpath="${BASE}/${folder}" | |
if [ ! -d "${fullpath}" ]; then | |
read -s -n1 -p " no cloned folder found. curious... press ENTER to exit" | |
exit 1 | |
else | |
openme="${BASE}/${folder}" | |
fi | |
echo "cloned to ${openme}. what now?" | |
echo " ENTER to open in terminal" | |
echo ' "ex" to open in file explorer' | |
echo ' "code" to open in VSCodium' | |
read -p ":" ACTION | |
if [ "${ACTION}" == "code" ]; then | |
codium "${openme}" | |
elif [ "${ACTION}" == "ex" ]; then | |
# open in explorer | |
xdg-open "${openme}" | |
else | |
gnome-terminal --working-directory="${openme}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment