scripts/gitclone.sh

65 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# This script clones a git repository into ~/Source/repos/<host>/<user>/<repo>
# It will be used as an alias for git
# Usage: git clone [options] <url> [directory]
# Check if the user is trying to clone a repository
action=$1
if [ "$action" != "clone" ]; then
git "$@"
exit
fi
# Initialize variables
url=""
path=""
options=""
# Parse arguments
shift # Remove 'clone' from arguments
for arg in "$@"; do
if [[ -z "$url" && "$arg" != -* ]]; then
# First non-option argument is the URL
url="$arg"
elif [[ -n "$url" && -z "$path" && "$arg" != -* ]]; then
# Second non-option argument is the path
path="$arg"
elif [[ "$arg" == -* ]]; then
# Option arguments
options="$options $arg"
fi
done
# Check if URL was provided
if [ -z "$url" ]; then
echo "Usage: git clone [options] <url> [directory]"
exit 1
fi
# Check if the user already specified a path
if [ -n "$path" ]; then
git clone $options "$url" "$path"
exit
fi
# Check if the URL is https or ssh
if [[ $url == https://* ]]; then
host=$(echo $url | cut -d/ -f3)
user=$(echo $url | cut -d/ -f4)
repo=$(echo $url | cut -d/ -f5 | cut -d. -f1)
elif [[ $url == git@* ]]; then
host=$(echo $url | cut -d: -f1 | cut -d@ -f2)
user=$(echo $url | cut -d: -f2 | cut -d/ -f1)
repo=$(echo $url | cut -d/ -f2 | cut -d. -f1)
else
echo "Invalid URL: $url"
exit 1
fi
# Clone the repository with any provided options
git clone $options "$url" ~/Source/repos/$host/$user/$repo
exit