Sunday, 10 January 2016

How to Download git project using curl


       Recently there was a requirement to download a git project from gitlab using wget or curl command instead of the usual git clone command. While this may sound simple , i didn't find the solution in an easy way , I've to google for sometime to find the answer. Finally i fount out  a curl command to download a git project . I am sharing it here to save some time for others. First thing my git project is http and not https so i didn't cover how to download a https git project , which may involve passing cacert file. Below steps were followed to download a git project using curl command.


Please note in the above URL. <<scribbling-group>> is the Namespace in which the project resides,  in this case <<scribbling-group>> is the groups Namespace not users Namespace. This just for your information , this doesn't make any difference in the way you download a git project using curl.  In Enterprise environment mostly the project resides in the Groups Namespace as projects are not owned by a specific individual but by a Team.  Then <<testing.git>> is the actual project name.

And as you can see in the above picture , the <<testing.git>> is a private project , so not all users can read and write to it only the authorized users can do it. So in order to download the project we need to pass on some kind of authorization to the gitlab for it to validate and approve the download operation.  Whereas you can pass username and password , but i don't know how to do that in curl, so there is an another way to authenticate, that's called "Private Access Token" . I believe every user in gitlab will have this token created. Mine was available in http://gitlab.*********.com/profile/account link.

Thought we have the git project URL  http://gitlab.*******.com/scribbling-group/testing.git , we can't use it as it is in curl to download, gitlab has an option to download the project in different archive format like zip, tar, tar.gz, tar.bz2 . So we are going to the copy the link of tar.gz dowload and use it in curl command.


In the above URL "archive.tar.gz" is the "testing.git" project in archive format tar.gz and "ref=master" is the branch name as you know a git repository can have multiple branchesNow as the project is private project we need to pass the "private access token" to authenticate.


Now if you execute the above command as it is, the output would get printed to your stdout, i.e your screen, so redirect it to a file or extract it after downloading by piping it to tar -xz

http://gitlab.*******.com/scribbling-group/testing/repository/archive.tar.gz?ref=master&private_token=*******************' > testing.tar.gz

http://gitlab.*******.com/scribbling-group/testing/repository/archive.tar.gz?ref=master&private_token=*******************' | tar -xz 

[root@testserverr ~]$ curl -L http://gitlab.*******.com/scribbling-group/testing/repository/archive.tar.gz?ref=master&private_token=*******************' | tar -xz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 21738  100 21738    0     0  42168      0 --:--:-- --:--:-- --:--:-- 42374

Now we have downloaded a git project using curl command. I will share more details if i come across other git related work.