Jan Stanicek
Jan Stanicek

Reputation: 1281

Check that git repository exists

Is there any way how to check whether git repository exist just before perform git clone action? Something like git clone $REMOTE_URL --dry-run?

I would like to implement fast checking method which will check existence before the clone action will be performed. I expect that connection might be slow and repository might be big even more than 1GB, so I don't want to rely on time extensive operation due to simple validation. The clone will be performed asynchronously in the background when validation passes.

I found this https://superuser.com/questions/227509/git-ping-check-if-remote-repository-exists but it works from git repository context after initial clone action is done.

Maybe there is some git call which works without git repository context and return "some data" if repository on destination exist or error if doesn't.

Upvotes: 50

Views: 86830

Answers (7)

Abdel-Raouf
Abdel-Raouf

Reputation: 770

Checking that a Git repository exists on Bitbucket:

  1. Create Bitbucket App Password, please refer to this link for more info --> How to Create Bitbucket App Password
  2. Use this command as follow:

git ls-remote https://{bitbucket_app_password}@bitbucket.org/{teamName_or_username}/{repo_name}

Note: the above command will return a reference to your repo if it exists, with the heads related to your available branches inside the repo.

Example of a returned repo reference: 6bd56aec730097365aca5caa\tHEAD\n3bc42968eed7aec730097365ace74a5caa\trefs/heads/dev\n9310985c619745780a206bfc6efba069\trefs/heads/master\n

Checking that a specific Branch exists inside a Git repository on Bitbucket:

  1. Create Bitbucket App Password, please refer to this link for more info --> How to Create Bitbucket App Password
  2. use this command as follow:

git ls-remote https://{bitbucket_app_password}@bitbucket.org/{teamName_or_username}/{repo_name} -b {branch_name}

Note:

  • If the branch_name exists inside your repo, then it will return a reference to your branch as follow:

    34296d7a6bd5673009736ce74a5caa\trefs/heads/dev\n

  • If the branch_name doesn't exist inside your repo, then it won't return a reference to your branch and will return an empty response.

Upvotes: 0

にゃあ
にゃあ

Reputation: 161

git command

GIT_TERMINAL_PROMPT=0 git ls-remote https://github.com/git/git HEAD && echo repoExists || echo notExists

curl

curl https://github.com/git/git/info/refs?service=git-upload-pack && echo notExists || echo repoExists

JavaScript

Source Code

https://github.com/hellowork-mhlw/hellowork-mhlw.github.io/blob/main/api/github.js

async function handler(url) {
  const exists = await fetch('https://hellowork-mhlw.vercel.app/api/github?url=' + url).then(r => r.json())
  if (exists) {
    document.all.out.value = 'exists😀'
  } else {
    document.all.out.value = 'not exists😭'
  }
}
<input value="https://github.com/git/git" placeholder="https://gitlab.gnome.org/GNOME/gnome-shell.git" oninput="handler(this.value)" size=35>
<output id=out>exists😀</output>

Upvotes: 0

Ilyich
Ilyich

Reputation: 5776

Just send a HEAD request to https://github.com/<user>/<repo>

curl -s -o /dev/null -I -w "%{http_code}" https://github.com/<user>/<repo>

If the public repository exists, the output will be:

200

Otherwise it will be:

404

Upvotes: 1

Ravi Prajapati
Ravi Prajapati

Reputation: 3156

Try the following:

curl -u "$username:$token" https://api.github.com/repos/user/repository-name

If the Git repository does not exist, the output will be:

{
    "message": "Not Found",
    "documentation_url": "https://developer.github.com/v3"
}

If the Git repository exists, the output will be:

{
    "id": 123456,
    "name": "repository-name",
    "full_name": "user/repository-name",
    "owner": { ... } 
    ............
}

Upvotes: 2

user456814
user456814

Reputation:

You can easily do this using GitHub's API (make sure to leave out .git from the repo name):

curl https://api.github.com/repos/<user>/<repo>

If the repository is not found:

curl https://api.github.com/repos/coldhawaiian/blarblar
{
  "message": "Not Found",
  "documentation_url": "https://developer.github.com/v3"
}

Otherwise:

curl https://api.github.com/repos/coldhawaiian/git-ninja-toolkit
{
  "id": 11881218,
  "name": "git-ninja-toolkit",
  "full_name": "coldhawaiian/git-ninja-toolkit",
  "owner": {
    "login": "coldhawaiian",
    "id": 463580,
    "avatar_url": "https://avatars.githubusercontent.com/u/463580?",
    "gravatar_id": "f4de866459391939cd2eaf8b369d4d09",
    "url": "https://api.github.com/users/coldhawaiian",
    "html_url": "https://github.com/coldhawaiian",
    "followers_url": "https://api.github.com/users/coldhawaiian/followers",
    // etc...
    "type": "User",
    "site_admin": false
  },
  // etc...
}

Upvotes: 12

Leigh
Leigh

Reputation: 12496

The answer that you linked does what you want it to do.

Try running this in a folder without a repository in it:

git ls-remote https://github.com/git/git

It should show you the references on the remote, even though you haven't added a local repository with git init or done a git clone.

See more: https://www.kernel.org/pub/software/scm/git/docs/git-ls-remote.html

Upvotes: 67

Chris
Chris

Reputation: 136889

You can use ls-remote:

$ git ls-remote [email protected]:github/markup.git
794d5d36dae7c1a9a0ed3d452ad0ffa5ab2cc074    HEAD
d27b5b1c5ae84617d4a9356eaf565c7b555c4d1d    refs/heads/can_render_regex
c03cce8f271d683c9cdeb5253c9cb21b5f0e65a0    refs/heads/fix-tests-part-deux
# Snip many more lines

$ git ls-remote [email protected]:nothing/here.git
ERROR: Repository not found.
fatal: Could not read from remote repository.

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

Note that ls-remote exits 128 when this occurs, so you could redirect the command output if you don't need it and just check the command's return value.

Note that in some cases you may get prompted for input, e.g. if you try to access a GitHub https://... repository. This is because there may be a matching private repository.

Upvotes: 7

Related Questions