Skip to content

Instantly share code, notes, and snippets.

@dtsolis
Created January 9, 2025 11:31
Show Gist options
  • Save dtsolis/714eaab94cfe34537292f0273dedf6e5 to your computer and use it in GitHub Desktop.
Save dtsolis/714eaab94cfe34537292f0273dedf6e5 to your computer and use it in GitHub Desktop.
This script automates the process of cloning a Git repository using a specified SSH key. It prompts the user to select an SSH key from the available keys in the ~/.ssh directory, and optionally allows the user to set a custom author name and email for the commits. The script ensures that the specified SSH key is used for the cloning process and …
#!/bin/bash
#
# Copyright 2025, Dimitris-Sotiris Tsolis
# MIT License, http://www.opensource.org/licenses/mit-license.php
# exit when any command fails
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
BO='\033[0;33m' # Brown/Orange
NC='\033[0m' # No Color
SSH_DIR=~/.ssh
if [[ "$1" ]]; then
TARGET_FOLDER=$2
# ask user to select one of the available ssh keys in the ssh directory
echo ""
echo -e "${BO}Available ssh keys in ${SSH_DIR}:${NC}"
ls -1 $SSH_DIR | grep -v known_hosts | grep -v config | grep -v .pub | grep -v .ppk | grep -v .pem | grep -v .pub | grep -v .bak | grep -v .old | grep -v .orig | grep -v .swp
echo ""
echo -e "Please select one of the above ssh keys to use for cloning the git repo ${BO}(leave empty to use default)${NC}:"
read -p "Enter the name of the ssh key file: " SSH_CERT_FILE
if [[ -n "$SSH_CERT_FILE" && ! -f $SSH_DIR/$SSH_CERT_FILE ]]; then
echo -e "${RED}The file $SSH_DIR/$SSH_CERT_FILE does not exist. Exiting...${NC}"
exit 1
fi
# ask user to provide a name for the author of the commits
read -p "Enter the name of the author of the commits (if empty, will use \"$(git config --global user.name)\"): " USER_NAME
# ask user to provide an email for the author of the commits
read -p "Enter the email of the author of the commits (if empty, will use \"$(git config --global user.email)\"): " USER_EMAIL
echo ""
# clone the project
if [[ -n "$SSH_CERT_FILE" ]]; then
if ! (GIT_SSH_COMMAND="ssh -i ${SSH_DIR}/${SSH_CERT_FILE} -F /dev/null" git clone $1 $TARGET_FOLDER) then
echo ""
echo -e "${RED}Clone failed. Exiting...${NC}"
exit 1
fi
elif ! (git clone $1 $TARGET_FOLDER) then
echo ""
echo -e "${RED}Clone failed. Exiting...${NC}"
exit 1
fi
if [[ -z "$TARGET_FOLDER" ]]; then
# get the folder name from the git url
TARGET_FOLDER=$(echo $1 | awk -F'/' '{print $NF}' | sed 's/.git//')
fi
# get into the cloned folder
cd $TARGET_FOLDER
echo ""
echo "Moved to folder: $TARGET_FOLDER..."
if [[ -n "${USER_NAME}" ]]; then
echo "Setting up git user name..."
git config user.name "${USER_NAME}"
fi
if [[ -n "${USER_EMAIL}" ]]; then
echo "Setting up git user email..."
git config user.email "${USER_EMAIL}"
fi
if [[ -n "$SSH_CERT_FILE" ]]; then
echo "Setting up specific ssh key"
git config core.sshCommand "ssh -i ${SSH_DIR}/${SSH_CERT_FILE} -F /dev/null"
fi
echo -e "${GREEN}All DONE!!!${NC}"
else
echo -e "${RED}Please provide a git url to clone the repository${NC}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment