15

I'm using gitlab. When I go into the interface, on each branch I can download the source code as a zip, tar or whatsoever.

I am making rpm spec files for which I would need the possibility to download the tar ball using command line. since I added my rsa key i can do git clone without problems:

git clone http://gitlab/group/project.git
Cloning into 'project'...
remote: Counting objects: 1885, done.
remote: Compressing objects: 100% (826/826), done.
remote: Total 1885 (delta 1194), reused 1496 (delta 954)
Receiving objects: 100% (1885/1885), 1.30 MiB | 0 bytes/s, done.
Resolving deltas: 100% (1194/1194), done.
Checking connectivity... done

However doing

wget http://gitlab/group/project/repository/archive.zip

gets me these errors:

Resolving gitlab (gitlab)... 10.1.253.75
Connecting to gitlab (gitlab)|10.1.253.75|:80... connected.
HTTP request sent, awaiting response... 401 Unauthorized
Authorization failed.
Chris Maes
  • 35,025
  • 12
  • 111
  • 136

5 Answers5

6

Since GitLab 6.2 and issue 5253, it should be:

GET /projects/:id/repository/archive

But that seems for internal use only, since you cannot know the id of a project as a user (only its name).

Don't forget, as shown in ability.rb, that downloading an archive is linked to a permission. Make sure you have that "download_code" permission set for your project.

Here, it must be a permission issue, because, for instance:

wget http://demo.gitlab.com/gitlab/gitlab-recipes/repository/archive.zip

That works just fine, and get the content of that project without any issue.


However, as the OP Chris Maes comments and mentions in issue 6645, as as illustrated by app/models/ability.rb:

 if project && project.public?

... that "dowload_code" feature is only for public projects.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • as I said; i have put the rsa-key so I can clone without him asking for my username and pwd... but how do I get that ID you are talking about? Now I go in the interface and copy the download url from the "get tar" button – Chris Maes Mar 27 '14 at 15:46
  • @ChrisMaes that won't work: the API doesn't support yet accessing a project by its name, only by its id: https://github.com/gitlabhq/gitlabhq/issues/4921 – VonC Mar 27 '14 at 15:50
  • I'm not really using the API (don't know how); so if I'm in command line or in the interface: can I get the project ID? How? so if I have project id, the command "wget http://gitlab/projects/:123/repository/archive.tar" should work? I'm afraid not but don't really understand how to use the API... – Chris Maes Mar 27 '14 at 19:10
  • 1
    @ChrisMaes no, it would be `wget http://yourGiLabServer/projects/:id/repository/archive`: the only challenge is in finding the id of your project (but I don't see how to get the id of a project when looking around at http://demo.gitlab.com/gitlab/gitlab-recipes). However, in your case, this must be a permission issue, because `wget http://demo.gitlab.com/gitlab/gitlab-recipes/repository/archive.zip` works just fine for me. – VonC Mar 27 '14 at 19:20
  • your analysis is good, tx. But I added my rsa-key to my gitlab account. I can do "git clone http://gitlabserver/..." without issues... strange! – Chris Maes Mar 27 '14 at 19:43
  • @ChrisMaes adding the rsa allows GitHub to know who you are, but it isn't enough to grant you all the rights. I guess this is managed at the project administration level. – VonC Mar 27 '14 at 19:45
  • off course, but I am administrator myself; I have all the necessary permissions since I can clone and commit... I'll try again tomorrow when I'm at work... – Chris Maes Mar 27 '14 at 20:59
  • I tried it again, it works only for "public" projects in gitlab. Whenever I try this with a project that is "internal" or "protected", it doesn't work. I edited my question to show you the exact errors, and to show that i can clone using http. – Chris Maes Mar 28 '14 at 13:32
  • @ChrisMaes cloning and getting the archives are two different actions, managed by two different authorization levels (https://github.com/gitlabhq/gitlabhq/blob/0c5c1157af07b344350700da97d530184ef055f1/app/models/ability.rb#L43). There must be in the admin interface of those private repo a way to grant `download_code` right for your user. – VonC Mar 28 '14 at 13:34
  • I am the owner of the project... so I am supposed to have the rights to do anything with the code, including dowloading... no? – Chris Maes Mar 28 '14 at 15:10
  • @ChrisMaes not sure, I would still double check what default privileges come with ownership. But in general, I would agree with you. Although, when I look at https://github.com/gitlabhq/gitlabhq/blob/0c5c1157af07b344350700da97d530184ef055f1/app/models/ability.rb#L33, I hope this "`download_code`" feature isn't reserved *only* for public projects... – VonC Mar 28 '14 at 16:01
  • I opened a question on gitlabhq; they say it is not possible if the project is not public: https://github.com/gitlabhq/gitlabhq/issues/6645#issuecomment-38931794 – Chris Maes Mar 28 '14 at 16:03
  • @ChrisMaes that is *exactly* what I just said in my last comment: https://github.com/gitlabhq/gitlabhq/blob/0c5c1157af07b344350700da97d530184ef055f1/app/models/ability.rb#L33 – VonC Mar 28 '14 at 16:04
  • @ChrisMaes I have included this conclusion in the answer for more visibility. – VonC Mar 28 '14 at 16:07
  • ok I'm sorry I over read the last part of your comment. Thanks for your support... – Chris Maes Mar 28 '14 at 16:13
5

For me the private_token and the sha or ref parameters does not worked together. So, I changed the way, and I tell my private token via a header parameter to the Gitlab API, like this:

wget http://{{your_host}}/api/v3/projects/{{project_id}}/repository/archive?sha={{commit_sha}} --header='PRIVATE-TOKEN: {{private_token}}'
laze
  • 237
  • 2
  • 7
  • 1
    In my case it worked for downloading the compressed source-code files from an internal repository. My formulation was `wget https://gitlab.com///-/archive/master/ --header='PRIVATE-TOKEN: `. I got the full path from the web-based project page (right-clicking on the list of downloadable compressed source codes); and the access token from https://gitlab.com/profile/personal_access_tokens (in my case I selected read_repository as access choice) – XavierStuvw Nov 22 '18 at 14:15
  • This solution has worked out also in https://stackoverflow.com/a/48083881/5459638, and for me – XavierStuvw Nov 22 '18 at 14:22
  • This works for me with SaaS Gitlab as of Dec 1st 2019. You can find your Project ID on the Repo home page. I selected `v4` as my API version. I used `?ref=master` instead of `?sha=` – Felipe Alvarez Nov 30 '19 at 14:23
4

try this

curl http://$yourhost:$port/$yourgroup/$yourrepo/repository/archive.zip\?ref\=$yourbranch\&\private_token\=$yourtoken -o newpackage.zip
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • 1
    If you had read the answer you would have found that OP's issue can't be solved because the download feature is supported only for public repositories; OP asked about a non-public repository. – Max Leske Feb 06 '15 at 09:43
  • @MaxLeske OP didn't ask about non-public repo in the question. At any rate, his answer is useful and I don't know where else to have found it. NOTE: `$yourrepo` is _without_ the `.git` extension – Otheus Aug 25 '15 at 13:09
  • Our GitLab was upgraded to 8.2. This doesn't work anymore. Anyone have similar experience? – Otheus Dec 08 '15 at 02:59
0

With the newer version of gitlab, you can use

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.com/api/v4/projects/<project_id>/repository/archive?sha=<commit_sha>"

You can also pass different archive format, e.g tar.bz2

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.com/api/v4/projects/<project_id>/repository/archive.tar.bz2?sha=<commit_sha>"
Krishna
  • 6,107
  • 2
  • 40
  • 43
-1

I didn't want to have to get the project ID first, but rather use group|org and repository name similar to the Github HTTP API. This is what works for me on 11.9.0-ce.0

Private/Internal Project

curl "https://gitlab.yourdomain.com/api/v4/projects/engineering%2Faccount-service/repository/archive?sha=some-branch-name" \
  -H "Private-Token: $GITLAB_TOKEN" \
  -o /tmp/account-service-branch-name.tar.gz

Public Project

curl "https://gitlab.yourdomain.com/engineering/account-service/repository/archive.tar.gz?ref=some-branch-name" \
  -o /tmp/account-service-branch-name.tar.gz

In this example

  • "group" is engineering
  • "repository" is account-service

Don't forget the %2F between group and repo

Hope it helps anyone