From 3b0874c8dda624c2f79d32e3a9ff182d54312386 Mon Sep 17 00:00:00 2001 From: Andreas Schaafsma Date: Thu, 27 Mar 2025 19:22:15 +0100 Subject: [PATCH] Add git wrapper script --- gitclone.sh | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 gitclone.sh diff --git a/gitclone.sh b/gitclone.sh new file mode 100755 index 0000000..174623b --- /dev/null +++ b/gitclone.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +# This script clones a git repository into ~/Source/repos/// + +# It will be used as an alias for git + +# Usage: git clone [options] [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] [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 \ No newline at end of file