Search code examples
cmakeexternal-project

How to request a password for GIT_REPOSITORY using HTTPS URL


The URL understood by the git command can be in the format HTTPS or SSH.

In CMake, using ExternalProject_Add for the specified GIT_REPOSITORY any URL understood by the git command may be used.

Using HTTPS user credentials must be given in order to "clone" a private repository. For ExternalProject_Add, such mechanism exists in the form of HTTP_USERNAME and HTTP_PASSWORD when using DOWNLOAD_COMMAND.

For GIT_REPOSITORY there doesn't seem to be such a method. When using:

include( ExternalProject )

ExternalProject_Add(test
    GIT_REPOSITORY git@github.com:myuser/myprivaterepo.git
    GIT_REMOTE_NAME origin
)

on a private repository the following error is given:

fatal: could not read Username for 'https://github.com': No such device or address

Question

How can I make CMake request a password for GIT_REPOSITORY when using HTTPS connections to a private repository on ExternalProject_Add?


Solution

  • While CMake doesn't provide explicit Git options for providing user credentials (like HTTP_USERNAME and HTTP_PASSWORD), you can manipulate the Git URL so that you are prompted for login password, as mentioned here. Just specify your username with a @ in the URL:

    ExternalProject_Add(test
        GIT_REPOSITORY https://username@github.com/myuser/myprivaterepo.git
        GIT_REMOTE_NAME origin
        USES_TERMINAL_DOWNLOAD ON 
    )
    

    Note, you may also need to enable the USES_TERMINAL_DOWNLOAD option to allow terminal input to enter your credentials. You could also provide your password directly in the URL itself, but best practices do not recommend this:

    ExternalProject_Add(test
        GIT_REPOSITORY https://username:password@github.com/myuser/myprivaterepo.git
        GIT_REMOTE_NAME origin
        USES_TERMINAL_DOWNLOAD ON 
    )
    

    As a bonus, this will also work for Bitbucket accounts.