0

Is there an easy way to get the size of a public or private github repository?

I tried this method which seems to work well for public repositories (returning "size": 2994520):

echo https://github.com/torvalds/linux.git | perl -ne 'print $1 if m!([^/]+/[^/]+?)(?:\.git)?$!' | xargs -I{} curl -s -k https://api.github.com/repos/'{}' | grep size

but doesn't seem to work for private repositories (it returns the message 'Not Found')

Is there some way (via an API, the browser, or bash script) that can retrieve the size of a github repository (as distinct from a local git repository)?

Note: important that the solution works for private repositories the user has access to (not just public ones)

stevec
  • 41,291
  • 27
  • 223
  • 311
  • 1
    The same method should work with private repositories. You just have to authenticate when using the github api. – Philippe Aug 16 '20 at 21:20
  • @Philippe if I'm using git from the mac terminal (e.g pushing and pulling repos from github), would that mean I am already authenticated to the github API, or is that separate authentication process? – stevec Aug 16 '20 at 21:54
  • 2
    @stevec It's `curl` in the command chain that calls a Github API. For a private repository, you can pass a personal access token or username + password for authentication. See https://developer.github.com/v3/#authentication. – ElpieKay Aug 17 '20 at 01:17

1 Answers1

1

You need to pass a PAT(Personal Access Token) to curl. this authenticates you as a user with github, which in turn lets you access your private repos.

You can create a PAT by following instructions here Creating a Personal Access Token

Then run

echo https://github.com/torvalds/linux.git | perl -ne 'print $1 if m!([^/]+/[^/]+?)(?:\.git)?$!' | xargs -I{} curl -s -k -u <username> https://api.github.com/repos/'{}' | grep size

and when asked for, provide the token you created earlier. Be sure to replace your username in the command

you can also directly pass in the token in the command

echo https://github.com/torvalds/linux.git | perl -ne 'print $1 if m!([^/]+/[^/]+?)(?:\.git)?$!' | xargs -I{} curl -s -k -u <username>:<token>  https://api.github.com/repos/'{}' | grep size
Rohit Patil
  • 162
  • 8