A step-by-step tutorial on how to use the gitlab_clone
function from the provided code:
Step 1: Create a Folder for Scripts
- Open a terminal or command prompt.
- Run the following command to create a folder named "scripts" in your home directory:
mkdir $HOME/scripts
Step 2: Save the Function in a File
- Open a text editor of your choice.
- Copy the following code block:
#####GITLAB CLONE WITH TOKEN############################################
gitlab_clone() {
get_token() {
echo "Secrets file not found or GITLAB_TOKEN is empty. Please provide Git username and personal access token:"
read -r -p "Username: " username
read -r -p "Token: " token
echo "export GITLAB_TOKEN=$username:$token" > $HOME/scripts/secrets.sh
source $HOME/scripts/secrets.sh
}
if [[ -f $HOME/scripts/secrets.sh ]]; then
source $HOME/scripts/secrets.sh
fi
local repo_url=$1
local cred=${GITLAB_TOKEN}
if [[ -z $cred ]]; then
get_token
cred=${GITLAB_TOKEN} # Assign the value of GITLAB_TOKEN after calling get_token
fi
local url=$(echo "$repo_url" | sed "s|https://|https://$cred@|")
git clone "$url"
}
alias gclone="gitlab_clone"
- Paste the copied code into the text editor.
- Save the file with the name "git_func.sh" inside the "scripts" folder created in Step 1. Make sure to keep the
.sh
extension for shell script files.
Step 3: Source the File in .bashrc
- Open a terminal or command prompt.
- Run the following command to open the .bashrc file in a text editor:
nano ~/.bashrc
- Scroll to the end of the file and add the following line:
source $HOME/scripts/git_func.sh
- Press Ctrl+X to exit the text editor, then press Y to save the changes, and Enter to confirm the file name.
Step 4: Activate the Changes
- In the same terminal or command prompt, run the following command to reload the .bashrc file and activate the changes:
source ~/.bashrc
Step 5: Using the gitlab_clone
Function
- Now, you can use the
gitlab_clone
function, which is aliased togclone
, in your terminal. - Run the following command to clone a GitLab repository:
gclone <repository_url>
Replace <repository_url>
with the actual URL of the repository you want to clone.
- If the
GITLAB_TOKEN
is not set or the secrets file doesn't exist, the function will prompt you to provide your Git username and personal access token. - Enter your Git username when prompted and press Enter.
- Enter your personal access token when prompted (the input will be hidden) and press Enter.
- The function will save the GitLab token in the
secrets.sh
file and automatically source it for future use. - The repository will be cloned using the provided credentials.
You have now set up the gitlab_clone
function and can use it to clone GitLab repositories conveniently.
Top comments (0)