Search code examples
gitlabraw

What is counterpart of raw.githubusercontent in gitlab?


How to view unprocessed versions of files? I want to use it for curl install script, but didn't find what counterpart is in gitlab.


Solution

  • The following schema is used in gitlab: https://gitlab.com/<group>/<repository>/-/raw/<branch>/<file>

    You will probably have to use the gitlab api because your request has to be authorized.

    Update

    As iugo commented a curl request against this url https://gitlab.com/<group>/<repository>/-/raw/<branch>/<file> redirects.

    To get the raw file we need to use the endpoint 'Get raw file from repository' from Repository files API:

    curl --header "PRIVATE-TOKEN:<token>" "https://<server>/api/v4/projects/<project-id>/repository/files/<path-to-file>?ref=<branch>"
    

    As output we get the the content (base64 encoded) of the requested file and additional metadata. To get the raw content we need to clip the content part of the returned json and decode it.

    To clip the content part we could use jq:

    jq -r '.content'
    

    --raw-output / -r With this option, if the filter's result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes.

    To decode the encoded content we could use base64

    base64 -d > file
    

    -d, --decode Decode data.

    In summary, the following should be executed:

    curl --header "PRIVATE-TOKEN:<token>" "https://<server>/api/v4/projects/<project-id>/repository/files/<path-to-file>?ref=<branch>" | jq -r '.content' | base64 -d > file