0

In my bash script ,a private repo is being cloned which prompts for username and password. The issue is even if incorrect username or password is entered and the git authentication fails - remote: Invalid username or password and the script proceeds further ignoring it .i want the git username and password read prompts to run in loop until git authentication succeeds . In other words , if incorrect username or password is entered it should be detected by bash and the read prompts should rerun in loop until git authentication is successful

#!/usr/bin/env bash

# something ....

read -e -p "Input your github username : " username
read -e -p "Input your github password : " password
git clone https://"$username":"$password"@github.com/"$username"/repo

# something ...

How do i solve this problem ?

Sachin
  • 1,217
  • 2
  • 11
  • 31
  • 1
    Are you certain about that behavior? When I try to clone a private repository using an invalid username/password, `git` says `remote: Invalid username or password.`, does not create any directory, and exits with an error status. – larsks Feb 10 '21 at 19:09
  • i am sorry for that mistake , i checked properly again , that blank folder was getting created through the bash script itself and not due to git authentication failure .updated question accordingly – Sachin Feb 10 '21 at 19:13
  • Password-based authentication is deprecated. Why aren't you using public key authentication instead, so that your script doesn't have to deal with authentication at all? – chepner Feb 10 '21 at 19:38

1 Answers1

3

Git will usually not create the directory of authentication failed. Check if the folder exists and exit if it does not:

test -d ./repo || { echo 'Dir does not exist'; exit 1; };

But before that, the git process should already exit with a non-zero exit code, so you could do this instead:

git clone "https://…@…/path/to/repo" || exit 1;

If you want to keep retrying until the command succeeds, use a loop:

read -e -p 'Input your github username: ' username
read -e -p 'Input your github password: ' password
while ! git clone "https://…@…/path/to/repo"; do
  echo 'Error cloning, please retry' >&2;
  read -e -p 'Input your github username: ' username
  read -e -p 'Input your github password: ' password
done

You might also be interested in How to get a password from a shell script without echoing for preventing shoulder surfing while entering the password.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • Actually the script is quite long ,and i dont really want to exit at the very end if incorrect username or password is entered . is it possible to rerun the username and password read prompts and attempt to clone to repo until git authentication is successful ? – Sachin Feb 10 '21 at 19:20
  • @Sachin Sure, use a loop – knittl Feb 10 '21 at 19:21
  • 1
    @Sachin Done, I have added an example with a loop. If your script becomes too complicated to maintain (for you or your team), consider switching to a different scripting language, e.g. python – knittl Feb 10 '21 at 19:31