1

After looking at various methods to test the access to a (private) git repository through ssh in bash (without cloning the repository), I've noticed I have some difficulties capturing the output of the probe.

For example, running:

#!/bin/bash
has_access="$(git ls-remote git@github.com:<some github username>/<some private repository>.git)"
echo "has_access=$has_access"

Returns:

ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
has_access=

Which demonstrates that the probe is effective at determining whether ssh does not have access, but it is not effective at capturing/storing this variable in a bash script.

Therefore, I would like to ask, how can I test whether ssh access into a (private) GitHub repository is available, or not, and store the (boolean) result in a bash variable (without cloning the repository)?

a.t.
  • 2,002
  • 3
  • 26
  • 66
  • 1
    Other than the question of how to capture error output: have you seen this method of testing SSH connectivity to GitHub? https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection – Benjamin W. Oct 23 '21 at 17:50
  • 1
    Sorry, maybe it's `ssh` after all. This might help: [git: fatal: Could not read from remote repository](https://stackoverflow.com/q/13509293/3776858) – Cyrus Oct 23 '21 at 17:50
  • @Cyrus thank you, that method can lead to a false positive in case one has multiple GitHub accounts (without some clever parsing). That is why I intend to check repository access, instead of ssh-access to GitHub. – a.t. Oct 23 '21 at 17:53
  • From the related question, I found a working solution was: `my_service_status=$(git ls-remote git@github.com:$github_username/$github_repository.git 2>&1)`, followed by checking whether `my_service_status` contains the word `ERROR`, if it does, the ssh account(s) cannot access the repository, if it does not contain `ERROR`, it can access/clone the GitHub repository using ssh. – a.t. Oct 23 '21 at 21:24

0 Answers0