Using forked package import in Go

Suppose you have a repository at github.com/someone/repo and you fork it to github.com/you/repo. You want to use your fork instead of the main repo, so you do a

go get github.com/you/repo

Now all the import paths in this repo will be "broken", meaning, if there are multiple packages in the repository that reference each other via absolute URLs, they will reference the source, not the fork.

Is there a better way as cloning it manually into the right path?

git clone [email protected]:you/repo.git $GOPATH/src/github.com/someone/repo

Solution 1:

If you are using go modules. You could use replace directive

The replace directive allows you to supply another import path that might be another module located in VCS (GitHub or elsewhere), or on your local filesystem with a relative or absolute file path. The new import path from the replace directive is used without needing to update the import paths in the actual source code.

So you could do below in your go.mod file

module github.com/yogeshlonkar/openapi-to-postman

go 1.12

require (
    github.com/someone/repo v1.20.0
)

replace github.com/someone/repo => github.com/you/repo v3.2.1

where v3.2.1 is tag on your repo. Also can be done through CLI

go mod edit -replace="github.com/someone/[email protected]=github.com/you/[email protected]"

Solution 2:

To handle pull requests

  • fork a repository github.com/someone/repo to github.com/you/repo
  • download original code: go get github.com/someone/repo
  • be there: cd "$(go env GOPATH)/src"/github.com/someone/repo
  • enable uploading to your fork: git remote add myfork https://github.com/you/repo.git
  • upload your changes to your repo: git push myfork

http://blog.campoy.cat/2014/03/github-and-go-forking-pull-requests-and.html

To use a package in your project

https://github.com/golang/go/wiki/PackageManagementTools

Solution 3:

One way to solve it is that suggested by Ivan Rave and http://blog.campoy.cat/2014/03/github-and-go-forking-pull-requests-and.html -- the way of forking.

Another one is to workaround the golang behavior. When you go get, golang lays out your directories under same name as in the repository URI, and this is where the trouble begins.

If, instead, you issue your own git clone, you can clone your repository onto your filesystem on a path named after the original repository.

Assuming original repository is in github.com/awsome-org/tool and you fork it onto github.com/awesome-you/tool, you can:

cd $GOPATH
mkdir -p {src,bin,pkg}
mkdir -p src/github.com/awesome-org/
cd src/github.com/awesome-org/
git clone [email protected]:awesome-you/tool.git # OR: git clone https://github.com/awesome-you/tool.git
cd tool/
go get ./...

golang is perfectly happy to continue with this repository and doesn't actually care some upper directory has the name awesome-org while the git remote is awesome-you. All import for awesome-org are resovled via the directory you have just created, which is your local working set.

In more length, please see my blog post: Forking Golang repositories on GitHub and managing the import path

edit: fixed directory path