What's the proper way to "go get" a private repository?

huangapple go评论96阅读模式
英文:

What's the proper way to "go get" a private repository?

问题

我正在寻找一种使$ go get与私有仓库配合使用的方法,在尝试了很多谷歌搜索后。

第一次尝试:

$ go get -v gitlab.com/secmask/awserver-go
Fetching https://gitlab.com/secmask/awserver-go?go-get=1
https fetch failed.
Fetching http://gitlab.com/secmask/awserver-go?go-get=1
Parsing meta tags from http://gitlab.com/secmask/awserver-go?go-get=1 (status code 200)
import "gitlab.com/secmask/awserver-go": parse http://gitlab.com/secmask/awserver-go?go-get=1: no go-import meta tags
package gitlab.com/secmask/awserver-go: unrecognized import path "gitlab.com/secmask/awserver-go

是的,它没有看到元标签,因为我不知道如何提供登录信息。

第二次尝试:

按照https://gist.github.com/shurcooL/6927554的说明进行操作。将配置添加到.gitconfig文件中。

insteadOf = https://gitlab.com/ $ go get -v gitlab.com/secmask/awserver-go --> 不起作用 $ go get -v gitlab.com/secmask/awserver-go.git --> 起作用,但我得到了src/gitlab.com/secmask/awserer-go.git

是的,它可以工作,但我的项目名称带有.git扩展名,我可以将其重命名为原始名称,但每次使用$ go get都这样做不太好,还有其他方法吗?

英文:

I'm searching for the way to get $ go get work with private repository, after many google try.

The first try:

$ go get -v gitlab.com/secmask/awserver-go
Fetching https://gitlab.com/secmask/awserver-go?go-get=1
https fetch failed.
Fetching http://gitlab.com/secmask/awserver-go?go-get=1
Parsing meta tags from http://gitlab.com/secmask/awserver-go?go-get=1 (status code 200)
import "gitlab.com/secmask/awserver-go": parse http://gitlab.com/secmask/awserver-go?go-get=1: no go-import meta tags
package gitlab.com/secmask/awserver-go: unrecognized import path "gitlab.com/secmask/awserver-go

Yep, it did not see the meta tags because I could not know how to provide login information.

The second try:

Follow https://gist.github.com/shurcooL/6927554. Add config to .gitconfig.

insteadOf = https://gitlab.com/ $ go get -v gitlab.com/secmask/awserver-go --> not work $ go get -v gitlab.com/secmask/awserver-go.git --> work but I got src/gitlab.com/secmask/awserer-go.git

Yes it work but with .git extension with my project name, I can rename it to original but do it everytime $ go get is not so good, is there an otherway?

答案1

得分: 226

你有一件事情需要配置。示例是基于GitHub的,但这不应该改变过程:

$ git config --global url.git@github.com:.insteadOf https://github.com/
$ cat ~/.gitconfig


insteadOf = https://github.com/
$ go get github.com/private/repo

为了使Go模块工作(使用Go 1.11或更新版本),你还需要设置GOPRIVATE变量,以避免使用公共服务器获取代码:

export GOPRIVATE=github.com/private/repo

英文:

You have one thing to configure. The example is based on GitHub but this shouldn't change the process:

$ git config --global url.git@github.com:.insteadOf https://github.com/
$ cat ~/.gitconfig
insteadOf = https://github.com/ $ go get github.com/private/repo

For Go modules to work (with Go 1.11 or newer), you'll also need to set the GOPRIVATE variable, to avoid using the public servers to fetch the code:

export GOPRIVATE=github.com/private/repo

答案2

得分: 56

正确的方法是手动将存储库放在正确的位置。一旦存储库在那里,您可以使用go get -u来更新包,并使用go install来安装它。一个名为github.com/secmask/awserver-go的包应该放在$GOPATH/src/github.com/secmask/awserver-go中。

您需要输入以下命令:

cd $GOPATH/src/github.com/secmask
git clone git@github.com:secmask/awserver-go.git
英文:

The proper way is to manually put the repository in the right place. Once the repository is there, you can use go get -u to update the package and go install to install it. A package named

github.com/secmask/awserver-go

goes into

$GOPATH/src/github.com/secmask/awserver-go

The commands you type are:

cd $GOPATH/src/github.com/secmask
git clone git@github.com:secmask/awserver-go.git

答案3

得分: 56

我在我们公司的gitlab上使用go get时遇到了问题。
我花了几分钟的时间寻找解决方案。我找到了以下方法:

  1. 首先,你需要在以下网址获取私有令牌:
    https://gitlab.mycompany.com/profile/account

  2. 配置你的git,添加额外的头部信息,包括私有令牌:

     $ git config --global http.extraheader "PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN"
    
  3. 配置你的git,将请求从http转换为ssh

     $ git config --global url."git@gitlab.mycompany.com:".insteadOf "https://gitlab.mycompany.com/"
    
  4. 最后,你可以正常使用go get命令:

     $ go get gitlab.com/company/private_repo
    
英文:

I had a problem with go get using private repository on gitlab from our company.
I lost a few minutes trying to find a solution. And I did find this one:

  1. You need to get a private token at:<br>
    https://gitlab.mycompany.com/profile/account

  2. Configure you git to add extra header with your private token:

     $ git config --global http.extraheader &quot;PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN&quot;
    
  3. Configure your git to convert requests from http to ssh:

     $ git config --global url.&quot;git@gitlab.mycompany.com:&quot;.insteadOf &quot;https://gitlab.mycompany.com/&quot;
    
  4. Finally you can use your go get normally:

     $ go get gitlab.com/company/private_repo
    

答案4

得分: 45

对于使用私有GitLab的用户,这里有一段可能会有所帮助的代码片段:https://gist.github.com/MicahParks/1ba2b19c39d1e5fccc3e892837b10e21

以下是代码片段的内容:

问题

go命令行工具需要能够从您的私有GitLab获取依赖项,但需要进行身份验证。

假设您的私有GitLab托管在privategitlab.company.com上。

环境变量

建议设置以下环境变量:

export GO111MODULE=on
export GOPRIVATE=privategitlab.company.com # 如果使用多个私有仓库,则使用逗号分隔

上述行最适合放在您的shell启动文件中,比如~/.bashrc

解释

GO111MODULE=on告诉Golang命令行工具您正在使用模块。我没有在私有GitLab上测试过不使用Golang模块的项目。

GOPRIVATE=privategitlab.company.com告诉Golang命令行工具不要使用公共互联网资源来处理列出的主机名(如公共模块代理)。

从私有GitLab获取个人访问令牌

为了使这些说明具备未来的适用性,请按照GitLab文档中的指南进行操作。
我知道Golang命令行工具需要read_api权限才能正常工作,我还怀疑可能需要read_repository权限,但尚未确认。

设置~/.netrc

为了使Golang命令行工具能够对GitLab进行身份验证,最好使用~/.netrc文件。

如果该文件不存在,请运行以下命令创建它:

touch ~/.netrc
chmod 600 ~/.netrc

现在编辑文件内容以匹配以下内容:

machine privategitlab.company.com login USERNAME_HERE password TOKEN_HERE

其中,将USERNAME_HERE替换为您的GitLab用户名,将TOKEN_HERE替换为前一部分获取的访问令牌。

常见错误

不要使用类似以下内容设置全局git配置:

git config --global url.&quot;git@privategitlab.company.com:&quot;.insteadOf &quot;https://privategitlab.company.com&quot;

我认为在撰写本文时,Golang命令行工具不完全支持SSH git,这可能会与~/.netrc产生冲突。

附加内容:SSH配置文件

对于常规使用git工具而不是Golang命令行工具,最好设置~/.ssh/config文件。为此,请运行以下命令:

mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config

请注意,上述文件和目录的权限对于大多数Linux系统上SSH的默认配置是必要的。

然后,编辑~/.ssh/config文件以匹配以下内容:

Host privategitlab.company.com
  Hostname privategitlab.company.com
  User USERNAME_HERE
  IdentityFile ~/.ssh/id_rsa

请注意,上述文件中的空格很重要,如果不正确将使文件无效。

其中,USERNAME_HERE是您的GitLab用户名,~/.ssh/id_rsa是您文件系统中SSH 私钥的路径。您已经将其公钥上传到GitLab。这里有一些说明

英文:

For people using private GitLabs, here's a snippet that may help: https://gist.github.com/MicahParks/1ba2b19c39d1e5fccc3e892837b10e21

Also pasted below:

Problem

The go command line tool needs to be able to fetch dependencies from your private GitLab, but authenticaiton is required.

This assumes your private GitLab is hosted at privategitlab.company.com.

Environment variables

The following environment variables are recommended:

export GO111MODULE=on
export GOPRIVATE=privategitlab.company.com # this is comma delimited if using multiple private repos

The above lines might fit best in your shell startup, like a ~/.bashrc.

Explanation

GO111MODULE=on tells Golang command line tools you are using modules. I have not tested this with projects not using
Golang modules on a private GitLab.

GOPRIVATE=privategitlab.company.com tells Golang command line tools to not use public internet resources for the hostnames
listed (like the public module proxy).

Get a personal access token from your private GitLab

To future proof these instructions, please follow this guide from the GitLab docs.
I know that the read_api scope is required for Golang command line tools to work, and I may suspect read_repository as
well, but have not confirmed this.

Set up the ~/.netrc

In order for the Golang command line tools to authenticate to GitLab, a ~/.netrc file is best to use.

To create the file if it does not exist, run the following commands:

touch ~/.netrc
chmod 600 ~/.netrc

Now edit the contents of the file to match the following:

machine privategitlab.company.com login USERNAME_HERE password TOKEN_HERE

Where USERNAME_HERE is replaced with your GitLab username and TOKEN_HERE is replaced with the access token aquired in the
previous section.

Common mistakes

Do not set up a global git configuration with something along the lines of this:

git config --global url.&quot;git@privategitlab.company.com:&quot;.insteadOf &quot;https://privategitlab.company.com&quot;

I beleive at the time of writing this, the SSH git is not fully supported by Golang command line tools and this may cause
conflicts with the ~/.netrc.

Bonus: SSH config file

For regular use of the git tool, not the Golang command line tools, it's convient to have a ~/.ssh/config file set up.
In order to do this, run the following commands:

mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config

Please note the permissions on the files and directory above are essentail for SSH to work in it's default configuration on
most Linux systems.

Then, edit the ~/.ssh/config file to match the following:

Host privategitlab.company.com
  Hostname privategitlab.company.com
  User USERNAME_HERE
  IdentityFile ~/.ssh/id_rsa

Please note the spacing in the above file matters and will invalidate the file if it is incorrect.

Where USERNAME_HERE is your GitLab username and ~/.ssh/id_rsa is the path to your SSH private key in your file system.
You've already uploaded its public key to GitLab. Here are some instructions.

答案5

得分: 23

以上所有方法对我都没有起作用。克隆存储库是正常工作的,但我仍然遇到了“未识别的导入”错误。

根据Go v1.13的文档,我发现我们应该使用GOPRIVATE环境变量,像这样:

$ GOPRIVATE=github.com/ORGANISATION_OR_USER_NAME go get -u github.com/ORGANISATION_OR_USER_NAME/REPO_NAME
英文:

All of the above did not work for me. Cloning the repo was working correctly but I was still getting an unrecognized import error.

As it stands for Go v1.13, I found in the doc that we should use the GOPRIVATE env variable like so:

$ GOPRIVATE=github.com/ORGANISATION_OR_USER_NAME go get -u github.com/ORGANISATION_OR_USER_NAME/REPO_NAME

答案6

得分: 18

在这里生成一个GitHub OAuth令牌链接,并将您的GitHub令牌导出为环境变量:

export GITHUB_TOKEN=123

将git配置设置为使用基本身份验证URL:

git config --global url."https://$GITHUB_TOKEN:x-oauth-basic@github.com/".insteadOf "https://github.com/"

现在您可以使用go get命令获取您的私有仓库。

英文:

Generate a github oauth token here and export your github token as an environment variable:

export GITHUB_TOKEN=123

Set git config to use the basic auth url:

git config --global url.&quot;https://$GITHUB_TOKEN:x-oauth-basic@github.com/&quot;.insteadOf &quot;https://github.com/&quot;

Now you can go get your private repo.

答案7

得分: 15

如果你已经使用SSH配置了git,这个答案Ammar Bandukwala提供了一个简单的解决方法:


$ go get 在内部使用git。以下一行命令将使git,并且因此$ go get通过SSH克隆你的包。

Github:

$ git config --global url."git@github.com:".insteadOf "https://github.com/"

BitBucket:

$ git config --global url."git@bitbucket.org:".insteadOf "https://bitbucket.org/"
英文:

If you've already got git using SSH, this answer by Ammar Bandukwala is a simple workaround:


$ go get uses git internally. The following one liners will make git and consequently $ go get clone your package via SSH.

Github:

$ git config --global url.&quot;git@github.com:&quot;.insteadOf &quot;https://github.com/&quot;

BitBucket:

$ git config --global url.&quot;git@bitbucket.org:&quot;.insteadOf &quot;https://bitbucket.org/&quot;

答案8

得分: 12

我遇到了.netrc,发现它与这个问题相关。

创建一个名为~/.netrc的文件,内容如下:

machine github.com
    login <github用户名>
    password <github密码或个人访问令牌>

完成!

此外,对于最新的GO版本,您可能需要将以下内容添加到环境变量中:GOPRIVATE=github.com/<组织名|用户名>
(我已将其添加到我的.zshrc文件中)

netrc还可以改善我的开发环境设置,因为现在已经配置了我的个人GitHub访问权限,可以在整个机器上使用(就像我的SSH配置一样)。

生成GitHub个人访问令牌:https://github.com/settings/tokens

有关在Windows上使用Git的详细信息,请参见此答案

参考:netrc手册页面


如果您想继续使用SSH身份验证,请强制使用SSH进行请求

git config --global url."git@github.com:<组织名|用户名>".insteadOf "https://github.com/<组织名|用户名>"

<组织名|用户名>替换为您的GitHub组织名或用户名。


问题在于,Go尝试使用https协议访问依赖项,而我们中的许多人使用SSH进行git身份验证。因此,我们需要使用~/.netrc设置https身份验证,或者使用git config强制在所有请求中使用SSH访问特定的git位置。

有关设置git访问权限的更多方法,请参见:https://gist.github.com/technoweenie/1072829#gistcomment-2979908

英文:

I came across .netrc and found it relevant to this.

Create a file ~/.netrc with the following content:

machine github.com
    login &lt;github username&gt;
    password &lt;github password or Personal access tokens &gt;

Done!

Additionally, for latest GO versions, you might need to add this to the environment variables GOPRIVATE=github.com/&lt;orgname|username&gt;
(I've added it to my .zshrc)

netrc also makes my development environment setup better as my personal github access for HTTPS is been configured now to be used across the machine (just like my SSH configuration).

Generate GitHub personal access tokens: https://github.com/settings/tokens

See this answer for its use with Git on Windows specifically

Ref: netrc man page


If you want to stick with the SSH authentication, then mask the request to use ssh forcefully

git config --global url.&quot;git@github.com:&lt;orgname|username&gt;&quot;.insteadOf &quot;https://github.com/&lt;orgname|username&gt;&quot;

Replace &lt;orgname|username&gt; with your GitHub org name or username.


Issue is that, go tries to access the dependencies using https protocol and lot of us use ssh instead to authenticate on git. So we either need to setup a https authentication using ~/.netrc or force using ssh on all request to a specific git location using git config

More methods for setting up git access: https://gist.github.com/technoweenie/1072829#gistcomment-2979908

答案9

得分: 9

这看起来像是GitLab问题5769

> 在GitLab中,由于存储库的结尾总是以.git结尾,我必须在存储库名称的末尾指定.git才能使其正常工作,例如:
>
> import "example.org/myuser/mygorepo.git"
>
> 和:
>
> $ go get example.org/myuser/mygorepo.git
>
> 看起来GitHub通过添加&quot;.git&quot;来解决这个问题。

这个问题应该在“添加对Go存储库检索的支持。#5958”中得到解决,前提是正确的元标签已经放置
尽管Go本身仍然存在一个问题:“cmd/go: go get无法在HTML5文档中发现元标签”。

英文:

That looks like the GitLab issue 5769.

> In GitLab, since the repositories always end in .git, I must specify .git at the end of the repository name to make it work, for example:
>
> import "example.org/myuser/mygorepo.git"
>
> And:
>
> $ go get example.org/myuser/mygorepo.git
>
> Looks like GitHub solves this by appending &quot;.git&quot;.

It is supposed to be resolved in “Added support for Go's repository retrieval. #5958”, provided the right meta tags are in place.
Although there is still an issue for Go itself: “cmd/go: go get cannot discover meta tag in HTML5 documents”.

答案10

得分: 9

尝试了多种解决方案后,我的问题仍然存在。在设置了~/.netrc和SSH配置文件之后,最终的解决方案是将以下行添加到我的~/.bash_profile文件中:

export GOPRIVATE="github.com/[organization]"

英文:

After trying multiple solutions my problem still persisted. The final solution after setting up the ~/.netrc and SSH config file, was to add the following line to my ~/.bash_profile

export GOPRIVATE=&quot;github.com/[organization]&quot;

答案11

得分: 6

我已经创建了一个用户特定的ssh-config,这样我的用户就可以使用正确的凭据和密钥自动登录。

首先,我需要生成一个密钥对:

ssh-keygen -t rsa -b 4096 -C "my@email.here"

并将其保存在例如~/.ssh/id_my_domain。请注意,这也是我连接到Github帐户的密钥对(私钥和公钥),所以我的密钥存储在~/.ssh/id_github_com中。

然后,我创建(或修改)了一个名为~/.ssh/config的文件,并添加了以下条目:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github_com

在另一台服务器上,"ssh-url"是admin@domain.com:username/private-repo.git,该服务器的条目将如下所示:

Host domain.com
    HostName domain.com
    User admin
    IdentityFile ~/.ssh/id_domain_com

请确保正确设置UserHostHostName

现在,我只需进入go路径,然后运行go get <package>,例如go get main,其中文件main/main.go包含了上面示例中的domain.com:username/private-repo.git包。

英文:

I have created a user specific ssh-config, so my user automatically logs in with the correct credentials and key.

First I needed to generate an key-pair

ssh-keygen -t rsa -b 4096 -C &quot;my@email.here&quot;

and saved it to e.g ~/.ssh/id_my_domain. Note that this is also the keypair (private and public) I've connected to my Github account, so mine is stored in~/.ssh/id_github_com.

I have then created (or altered) a file called ~/.ssh/config with an entry:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github_com

On another server, the "ssh-url" is admin@domain.com:username/private-repo.git and the entry for this server would have been:

Host domain.com
    HostName domain.com
    User admin
    IdentityFile ~/.ssh/id_domain_com

Just to clarify that you need ensure that the User, Host and HostName is set correctly.

Now I can just browse into the go path and then go get &lt;package&gt;, e.g go get main where the file main/main.go includes the package (from last example above) domain.com:username/private-repo.git.

答案12

得分: 4

对我来说,其他人提供的解决方案在go get过程中仍然出现以下错误:

git@gl.nimi24.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

这个解决方案需要做什么

  1. 根据其他人的说法:

    git config --global url."git@github.com:".insteadOf "https://github.com/"

  2. 从用于验证与存储库的连接的./ssh/id_rsa密钥中删除密码短语。可以通过在响应以下提示时输入空密码来完成:

    ssh-keygen -p

为什么这个方法有效

这不是一个很好的解决方法,因为在私钥上设置密码短语总是更好的选择,但它在OpenSSH内部引起了一些问题。

go get在内部使用git,而git使用openssh来打开连接。OpenSSH从.ssh/id_rsa中获取进行身份验证所需的证书。当从命令行执行git命令时,代理可以负责为您打开id_rsa文件,以便您不必每次都指定密码短语,但在go get的过程中执行时,在我的情况下,这种方法不起作用。然后,OpenSSH希望提示您输入密码,但由于调用方式的限制,它会在调试日志中打印:

read_passphrase: can't open /dev/tty: No such device or address

然后失败。如果从密钥文件中删除密码短语,OpenSSH将在没有提示的情况下访问您的密钥,这样就可以正常工作了。

可能是由于Go并发获取模块并同时打开多个SSH连接到Github(如此文章中所述)所致。这在一定程度上得到支持,因为OpenSSH的调试日志显示了与存储库的初始连接成功,但稍后由于某种原因再次尝试,并且这次选择要求输入密码短语。

然而,对我来说,使用SSH连接多路复用的解决方案在我这里不起作用。作者建议在受影响的主机的ssh配置文件中添加以下配置:

ControlMaster auto
ControlPersist 3600
ControlPath ~/.ssh/%r@%h:%p

但是,对我来说,这种方法不起作用,可能是我操作不正确。

英文:

For me, the solutions offered by others still gave the following error during go get

> git@gl.nimi24.com: Permission denied (publickey).
> fatal: Could not read from remote repository.
> Please make sure you have the correct access rights
> and the repository exists.

What this solution required

  1. As stated by others:

    git config --global url.&quot;git@github.com:&quot;.insteadOf &quot;https://github.com/&quot;

  2. Removing the passphrase from my ./ssh/id_rsa key which was used for authenticating the connection to the repository. This can be done by entering an empty password when prompted as a response to:

    ssh-keygen -p

Why this works

This is not a pretty workaround as it is always better to have a passphrase on your private key, but it was causing issues somewhere inside OpenSSH.

go get uses internally git, which uses openssh to open the connection. OpenSSH takes the certs necessary for authentication from .ssh/id_rsa. When executing git commands from the command line an agent can take care of opening the id_rsa file for you so that you do not have to specify the passphrase every time, but when executed in the belly of go get, this did not work somewhy in my case. OpenSSH wants to prompt you then for a password but since it is not possible due to how it was called, it prints to its debug log:

> read_passphrase: can't open /dev/tty: No such device or address

And just fails. If you remove the passphrase from the key file, OpenSSH will get to your key without that prompt and it works

This might be caused by Go fetching modules concurrently and opening multiple SSH connections to Github at the same time (as described in this article). This is somewhat supported by the fact that OpenSSH debug log showed the initial connection to the repository succeed, but later tried it again for some reason and this time opted to ask for a passphrase.

However the solution of using SSH connection multiplexing as put forward in the mentioned article did not work for me. For the record, the author suggested adding the collowing conf to the ssh config file for the affected host:

  ControlMaster auto
  ControlPersist 3600
  ControlPath ~/.ssh/%r@%h:%p

But as stated, for me it did not work, maybe I did it wrong

答案13

得分: 4

设置了GOPRIVATEgit config ...之后,人们在获取私有源代码时可能仍会遇到以下问题:

https fetch: Get "https://private/user/repo?go-get=1": EOF

他们无法在没有.git扩展名的情况下使用私有仓库。

原因是Go工具对于这个仓库的版本控制系统(如git、svn或其他)的协议一无所知,不像github.comgolang.org这些在Go源代码中硬编码的仓库。

然后,在获取私有仓库之前,Go工具会进行一个https查询:

https://private/user/repo?go-get=1

如果你的私有仓库不支持https请求,请使用replace直接告诉它:

require private/user/repo v1.0.0

...

replace private/user/repo => private.server/user/repo.git v1.0.0

更多信息请参考:https://golang.org/cmd/go/#hdr-Remote_import_paths

英文:

After setting up GOPRIVATE and git config ...

People may still meeting problems like this when fetching private source:

https fetch: Get &quot;https://private/user/repo?go-get=1&quot;: EOF

They can't use private repo without .git extension.

The reason is the go tool has no idea about the VCS protocol of this repo, git or svn or any other, unlike github.com or golang.org them are hardcoded into go's source.

Then the go tool will do a https query before fetching your private repo:

https://private/user/repo?go-get=1

If your private repo has no support for https request, please use replace to tell it directly :

require private/user/repo v1.0.0

...

replace private/user/repo =&gt; private.server/user/repo.git v1.0.0

https://golang.org/cmd/go/#hdr-Remote_import_paths

答案14

得分: 2

首先,我尝试了以下代码:

insteadOf = https://github.com/

但是在我的本地环境中没有起作用。

我尝试了以下命令:

ssh -t git@github.com

它显示我的 SSH 运行正常。

最后,我通过告诉 go get 将所有内容都视为私有,并使用 SSH 而不是 HTTPS 来解决了这个问题。

添加 export GOPRIVATE=*

英文:

first I tried

insteadOf = https://github.com/

but it didn't worked for my local.

I tried

ssh -t git@github.com

and it shows my ssh is fine.

finally, I fix the problem to tell the go get to consider all as private and use ssh instead of HTTPS.

adding export GOPRIVATE=*

答案15

得分: 1

请确保删除您之前的git配置文件,我遇到了相同的问题。

之前我执行了一个已过期的gitconfig,当您下次使用新的令牌执行该命令时,请确保删除之前的令牌。

英文:

Make sure you remove your previous gitconfigs, I had the same issue.

Previously I executed gitconfig whose token was expired, when you execute the command next time with new token make sure to delete previous one.

答案16

得分: 0

对于独立/最终的仓库,作为一个快速解决方案,为什么不在go.mod中使用你公司的域名来命名模块作为一个包呢?

module go.yourcompany.tld/the_repo

go.yourcompany.tld甚至不需要作为一个有效的(子)域存在...

此外,在同一个go.mod文件中,你可以使用replacement块/行来使用之前以相同方式克隆的私有仓库(在$GOPATH/src/go.yourcompany.tld中克隆的相应文件夹)(为什么我们要过多地依赖GitHub呢?)

编辑

  1. 不用说,私有仓库通常应该是一个私有仓库,通常是一个标准的git仓库,对吧?因此,为什么不只是git clone,然后在克隆的文件夹中go get呢?
英文:

For standalone/final repos, an as a quick fix, why don't just to name the module within the go.mod as a package using your company's domain ... ?

module go.yourcompany.tld/the_repo

go.yourcompany.tld don't even have to exist as a valid (sub)domain...

Also, in the same go.mod you can use replacement block/lines to use private repos previously cloned the same way (within a respective folder cloned also in $GOPATH/src/go.yourcompany.tld) (why do we have to depend too much in GitHub?)

Edit

  1. Needless to say that a private repo usually shall be a private repo, typically a standard git repo, right? With that, why not just to git clone and then go get within the cloned folder?

答案17

得分: -1

这是你要翻译的内容:

这是在Go Get中的硬编码,而不是Git的原因。所以修改Go源代码。
原因:
repoRootForImportDynamic将请求:https://....go-get

// RepoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
func RepoRootForImportPath(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
	rr, err := repoRootFromVCSPaths(importPath, security, vcsPaths)
	if err == errUnknownSite {
		rr, err = repoRootForImportDynamic(importPath, mod, security)
		if err != nil {
			err = importErrorf(importPath, "unrecognized import path %q: %v", importPath, err)
		}
	}
	if err != nil {
		rr1, err1 := repoRootFromVCSPaths(importPath, security, vcsPathsAfterDynamic)
		if err1 == nil {
			rr = rr1
			err = nil
		}
	}
}

所以将gitlab域名添加到vcsPaths中就可以了。
下载Go源代码:

vi ./src/cmd/go/internal/vcs/vcs.go

查找下面的代码:

var vcsPaths = []*vcsPath{
	// GitHub
	{
		pathPrefix: "github.com",
		regexp:     lazyregexp.New(`^(?P<root>github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
		vcs:        "git",
		repo:       "https://{root}",
		check:      noVCSSuffix,
	},

添加以下代码,XXXX是你的域名:

	// GitLab
	{
		pathPrefix: "gitlab.xxxx.com",
		regexp:     lazyregexp.New(`^(?P<root>gitlab.xxxx\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
		vcs:        "git",
		repo:       "https://{root}",
		check:      noVCSSuffix,
	},

编译并替换Go。

英文:

It's Hard Code In Go Get. Not The Git Reason. So Modify Go Source.
Reason:
repoRootForImportDynamic Will Request: https://....go-get

// RepoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
func RepoRootForImportPath(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
	rr, err := repoRootFromVCSPaths(importPath, security, vcsPaths)
	if err == errUnknownSite {
		rr, err = repoRootForImportDynamic(importPath, mod, security)
		if err != nil {
			err = importErrorf(importPath, &quot;unrecognized import path %q: %v&quot;, importPath, err)
		}
	}
	if err != nil {
		rr1, err1 := repoRootFromVCSPaths(importPath, security, vcsPathsAfterDynamic)
		if err1 == nil {
			rr = rr1
			err = nil
		}
	}

So add gitlab domain to vcsPaths will ok.
Download go source code:

vi ./src/cmd/go/internal/vcs/vcs.go    

Look for code below:

var vcsPaths = []*vcsPath{
	// GitHub
	{
		pathPrefix: &quot;github.com&quot;,
		regexp:     lazyregexp.New(`^(?P&lt;root&gt;github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
		vcs:        &quot;git&quot;,
		repo:       &quot;https://{root}&quot;,
		check:      noVCSSuffix,
	},

add Code As Follow,XXXX Is Your Domain:

	// GitLab
	{
		pathPrefix: &quot;gitlab.xxxx.com&quot;,
		regexp:     lazyregexp.New(`^(?P&lt;root&gt;gitlab.xxxx\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
		vcs:        &quot;git&quot;,
		repo:       &quot;https://{root}&quot;,
		check:      noVCSSuffix,
	},

compile and replace go.

huangapple
  • 本文由 发表于 2014年12月16日 17:00:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/27500861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定