FetchContent 访问私有 Git 存储库

问题描述 投票:0回答:2

有人在尝试通过 FetchContent 获取私有存储库时遇到过这样的问题吗?

CMakeLists.txt:

include(FetchContent)

FetchContent_Declare(
    {repository}
    GIT_REPOSITORY https://{username}@github.com/{company}/{repository}
    USES_TERMINAL_DOWNLOAD ON
)

FetchContent_GetProperties({repository})
if(NOT {repository}_POPULATED)
    FetchContent_Populate({repository})
endif()

输出:

MSBuild version 17.3.1+2badb37d1 for .NET Framework
  Performing download step (git clone) for '{repository}-populate'
  Cloning into '{repository}-src'...
  bash: line 1: /dev/tty: No such device or address
CUSTOMBUILD : error : failed to execute prompt script (exit code 1) [C:\git\.....vcxproj]
  fatal: could not read Password for 'https://{username}@github.com': No such file or directory

 ...

  -- Had to git clone more than once: 3 times.
  CMake Error at {repository}-subbuild/{repository}-populate-prefix/tmp/{repository}-populate-gitclone.cmake:39 (message):
    Failed to clone repository: 'https://{username}@github.com/{company}/{repository}'

...

-- Configuring incomplete, errors occurred!
git github cmake private-repository fetchcontent
2个回答
0
投票
git config --global credential.helper store

运行良好(需要存储的个人访问令牌(PAT) - 例如,您可以在克隆存储库时设置它)


0
投票

如果您尝试通过 ssh 密钥在本地使用 FetchContent,请确保以下两点:

  1. 调用时请勿使用 https url
    FetchContent_Declare
FetchContent_Declare(
    Project
    GIT_REPOSITORY [email protected]:user/Project.git
    GIT_TAG        master
    GIT_PROGRESS TRUE
    GIT_SHALLOW TRUE
)

FetchContent_MakeAvailable(Project)
  1. 确保您可以调用 git clone 而无需提供任何类型的登录名或密码。为此,我遵循了这个答案
# Add this to .profile
export SSH_AUTH_SOCK=~/.ssh/ssh-agent.$HOSTNAME.sock
ssh-add -l 2>/dev/null >/dev/null
if [ $? -ge 2 ]; then
  ssh-agent -a "$SSH_AUTH_SOCK" >/dev/null
fi
© www.soinside.com 2019 - 2024. All rights reserved.