Search code examples
gitgitlabgitlab-api

How to retrieve raw file content from a repository commit with gitlab api?


We can get list of repository commits by:

GET /projects/:id/repository/commits?path=:thefilename

We can get raw file content by blob id:

GET /projects/:id/repository/blobs/:sha/raw

However, the first api doesn't return blob id of each repository commit, and there is no known api that could retrieve raw file content using commit id instead. I need to retrieve the raw file content from a past repository commit instead of only the latest commit.


Solution

  • That's actually not true. The API directly supports getting files from arbitrary references, as git directly supports:

    GET /projects/:id/repository/files/:file_path/raw

    • file_path (required) - Url encoded full path to new file. Ex. lib%2Fclass%2Erb
    • ref (required) - The name of branch, tag or commit

    Ex.:

    curl --request GET --header 'PRIVATE-TOKEN: ' 'https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fmodels%2Fkey%2Erb/raw?ref=master'

    Read More at Get raw file from repository