118

I am building a (somewhat limited) Git client. To set up a repository, you enter the URL to the remote repo. I want to check whether the user has read+write access to that repository. If not, I present an authentication dialog.

I check 'read' access with git ls-remote <url>.

Is there an analogous way to check 'write' access, without cloning the repo first? (I know I could git clone <url> and then git push --dry-run)

Yang Meyer
  • 5,169
  • 5
  • 38
  • 51
  • 2
    Well, technically to push a commit into a repo you don't need to clone it first. But remember that pushing a chain of commits and update remote references are two different operations and you may have a permission for the former, but no permission for the latter. Also certain references could be unmodifiable. – user3159253 Apr 02 '14 at 12:17
  • 'git push --dry-run' is mentioned in this post as a way to check write access, when you have cloned. however for some of my remotes, this opens a password prompt & hangs indefinitely. there doesn't seem to be a non-interactive way to check if you have write access, even if you do have a clone of the repo. how can i check write access to a git repository, if i do have a clone of it? – rektide Jan 30 '20 at 23:10
  • 3
    A very easy way to check is whether you see an edit 'pencil' icon in the top right of the README.MD on the main Code page of the repo (scroll down to it if there's a long list of top level files/folders). Do this when you are logged in to Github, obviously. – Spike Nov 13 '20 at 16:04

3 Answers3

118

If the git repo is in github, open any file in the repo, then click 'edit', github will show something like this:

You’re editing a file in a project you don’t have write access to. We’ve created a fork of this project for you to commit your proposed changes to. Submitting a change to this file will write it to a new branch in your fork, so you can send a pull request.enter code here

guyskk
  • 1,978
  • 1
  • 11
  • 12
  • 34
    You're not wrong -- Github does indeed show this behaviour. However, the OP asked for something analogous to `git ls-remote` that their new tool could use, so the Github web UI probably wouldn't suit them. – RJHunter Oct 30 '16 at 07:03
  • 17
    There are more git repositories than just github repositories ;) – René Roth Feb 07 '18 at 08:59
  • 7
    Regarding the GUI, the tooltip can also indicate whether you can "Edit" or "Fork then edit" if no access. This is true with both Github proper and GH Enterprise. – Scott Prive Sep 19 '18 at 13:41
  • 3
    Forking a repo copies the entire repo, right? The repo could be very large. That seems like a pretty hefty operation just to check what your access level is... – Kyle Delaney Jan 28 '19 at 22:35
  • 10
    @KyleDelaney You don't need to click on the edit button, just hover over it. It will show "Edit this file" or "Edit the file in your fork of this project" depending on whether you have write access or not. – joulev Jun 06 '20 at 08:35
13

With the latest GitHub API you can check the permissions on a particular repo for a USERNAME (yours or another user) with the following command (you'll need to authenticate using your personal access token) e.g.:

curl -u your_username:your_access_token \
 -H "Accept: application/vnd.github.v3+json" \
 https://api.github.com/repos/octocat/hello-world/collaborators/USERNAME/permission

The response will show what permissions the USERNAME has for that repo. Otherwise it will report:

"message": "Not Found"

Note that you should have push access to the repo in question or it will fail with

{
  "message": "Must have push access to view collaborator permission.",
  "documentation_url": "https://docs.github.com/rest/reference/repos#get-repository-permissions-for-a-user"
}
weirdan
  • 2,264
  • 21
  • 25
Pierz
  • 5,588
  • 37
  • 55
1

You may perform git push git+ssh://host.org/path/to/repo some_ref without cloning.

But remember that pushing a chain of commits and update remote references are two different operations and you may have a permission for the former, but no permission for the latter. Also certain references could be unmodifiable.

Community
  • 1
  • 1
user3159253
  • 15,770
  • 3
  • 26
  • 46
  • 2
    This method would work from a working directory with a ref. Is there a way to do this without setting up a dummy repo and creating a commit first? – Yang Meyer Apr 02 '14 at 13:03
  • 18
    Yeah, who wants all these random dummy commits? Isn't there a cleaner way? – Dan Bolser Aug 03 '16 at 11:30
  • Haven't tried this myself, but perhaps the `--dry-run` parameter could be used to test for push access without pushing a real commit? https://git-scm.com/docs/git-push#Documentation/git-push.txt---dry-run – Nathan Friend Dec 03 '20 at 19:00
  • `--dry-run` can't be used for this purpose. Check this answer for explanation: https://stackoverflow.com/questions/25403573/how-can-one-dry-run-a-git-push-to-check-whether-one-has-write-permissions-to-a-r – Nahiyan Jan 08 '21 at 06:35