Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

git - GitHub Api download zip or tarball link

There was a good link here about how the zip/tarball string is created

When I download a zip from github, what is the hex string at the end of the file name represent?

But I'm looking at the GitHub APIv3 and I was curious if I'm missing something.

  1. Is there a way to get the zip/tarball link via an API call?

  2. If not, is there a way I can build that string without using the git binary or library? Meaning, can I use various API calls to pull out th data a need and assemble to URL I need?

I know the second question is a little unreasonable for stackoverflow and this is a bit of a fun project for me, so I would prefer on the second question if you just kind of nudged me in the right direction as opposed to throwing down a code snippet. Or just told me if it was possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can wget your way out of the GitHub repo to get a tar file (archive):

wget --no-check-certificate https://github.com/User/repo/archive/master.tar.gz

# better, if the certificate authorities are present:
wget https://github.com/User/repo/archive/master.tar.gz

will get you a file named 'master' from the user 'User''s repo 'repo'.

The updated V3 API url is:

https://api.github.com/repos/User/repo/:archive_format/:ref
#
# two possibilities for fomat:
https://api.github.com/repos/User/repo/tarball/master
https://api.github.com/repos/User/repo/zipball/master

# from github example:
$curl -L https://api.github.com/repos/octokit/octokit.rb/tarball > octokit.tar.gz

You can then tar xpvf master, getting the full archive. It will create a directory following the naming convention described in the question you mentioned.

No git binary is needed to get an archive from GitHub, thanks to their download service "Nodeload".


ligemer proposed in an edit the following example:

Edit 2016-08-25 - Shell Example With Wget, Variables, and Untar:

#!/bin/bash -ex

# arguments:
# token = $1
# organization = $2
# repo name = $3
# branch = $4

wget --header="Authorization: token ${1}" --header="Accept:application/vnd.github.v3.raw" -O - https://api.github.com/repos/${2}/${3}/tarball/${4} | tar xz

Call via:

$ scriptName.sh token my-organization site.com master

The command above will download and extract the Github folder to the same directory as the script.


Diogo Quintela suggests in the comments:

The following example allow the download, extract and cut the top level directory

curl -L https://api.github.com/repos/octokit/octokit.rb/tarball | tar xz --strip=1 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...