{"id":7506,"date":"2015-11-23T14:22:34","date_gmt":"2015-11-23T12:22:34","guid":{"rendered":"http:\/\/code.openark.org\/blog\/?p=7506"},"modified":"2015-11-24T16:17:38","modified_gmt":"2015-11-24T14:17:38","slug":"forking-golang-repositories-on-github-and-managing-the-import-path","status":"publish","type":"post","link":"https:\/\/code.openark.org\/blog\/development\/forking-golang-repositories-on-github-and-managing-the-import-path","title":{"rendered":"Forking Golang repositories on GitHub and managing the import path"},"content":{"rendered":"<p>Problem: there&#8217;s an awesome Golang project on GitHub which you want to fork. You want to develop &amp; collaborate on that fork, but the golang import\u00a0path, in your source code, still references the original\u00a0path, breaking everything.<\/p>\n<p>A couple solutions offered below. First, though, let&#8217;s\u00a0get some names.<\/p>\n<h3>A sample case, the problem at hand<\/h3>\n<p>There&#8217;s an awesome tool on <strong><em>http:\/\/github.com\/awsome-org\/tool<\/em><\/strong>. You successfully fork it onto\u00a0<strong><em>http:\/\/github.com\/awesome-you\/tool<\/em><\/strong>.<\/p>\n<p>You want to collaborate on\u00a0<strong><em>http:\/\/github.com\/awesome-you\/tool<\/em><\/strong>; you wish to\u00a0pull, commit &amp; push. Maybe you want to send pull requests to the origin.<\/p>\n<p>The following is commonly found throughout <strong>.go<\/strong> files in the repository:<\/p>\n<blockquote>\n<pre>import (\r\n    \"github.com\/awesome-org\/tool\/config\"\r\n    \"github.com\/awesome-org\/tool\/driver\"\r\n    \"github.com\/awesome-org\/tool\/net\"\r\n    \"github.com\/awesome-org\/tool\/util\"\r\n)<\/pre>\n<\/blockquote>\n<p>If you:<\/p>\n<blockquote>\n<pre>go get\u00a0http:\/\/github.com\/awesome-you\/tool<\/pre>\n<\/blockquote>\n<p><em>golang<\/em>\u00a0creates your <strong>$GOPATH\/src\/github.com\/awesome-you\/tool\/<\/strong>, which is awesome. However, as you resolve dependencies via<\/p>\n<blockquote>\n<pre>cd\u00a0$GOPATH\/src\/github.com\/awesome-you\/tool\/ ; go get .\/...<\/pre>\n<\/blockquote>\n<p><em>golang<\/em> digs into the source code, finds references to\u00a0<strong>github.com\/awesome-org\/tool\/config<\/strong>,\u00a0<strong>github.com\/awesome-org\/tool\/driver<\/strong> etc, and fetches <em>those<\/em> from\u00a0<strong>http:\/\/github.com\/awsome-org\/tool<\/strong> and onto\u00a0<strong>$GOPATH\/src\/github.com\/awesome-org\/tool\/<\/strong>, which is not awesome. You actually have two copies of the code, one from your fork, one from the origin, and your own fork will be largely ignored as it mostly points back to the origin.<\/p>\n<h3>A bad solution<\/h3>\n<p>The dirty, bad solution would be for you to go over the source code and replace <strong>&#8220;github.com\/awesome-org\/tool&#8221;<\/strong>\u00a0entries with <strong>&#8220;github.com\/awesome-you\/tool&#8221;<\/strong>. It is bad\u00a0for two reasons:<\/p>\n<ul>\n<li>You will not be able to further pull changes from upstream<\/li>\n<li>You will not be able to pull-request and push your own changes upstream<\/li>\n<\/ul>\n<p><!--more-->When I say &#8220;You will not\u00a0be able&#8221; I mean &#8220;in a reasonable, developer-friendly manner&#8221;. The code will be incompatible with upstream and you have effectively detached your code. You will need to keep editing and re-editing those entries anytime you wish to pull\/push upstream.<\/p>\n<h3>Solution #1:\u00a0add remote<\/h3>\n<p>Described in\u00a0<a href=\"http:\/\/blog.campoy.cat\/2014\/03\/github-and-go-forking-pull-requests-and.html\">GitHub and Go: forking, pull requests, and go-getting<\/a>, follow these procedures:<\/p>\n<blockquote>\n<pre>go get\u00a0http:\/\/github.com\/awesome-org\/tool\r\ngit remote add <strong>awesome-you-fork<\/strong>\u00a0http:\/\/github.com\/awesome-you\/tool<\/pre>\n<\/blockquote>\n<p>You&#8217;re adding your repository as\u00a0<a href=\"http:\/\/git-scm.com\/book\/en\/v2\/Git-Basics-Working-with-Remotes\">remote<\/a>. You will from now on need to explicitly:<\/p>\n<blockquote>\n<pre>git pull --rebase <strong>awesome-you-fork<\/strong>\r\ngit push <strong>awesome-you-fork<\/strong><\/pre>\n<\/blockquote>\n<p>If you forget to add the <strong>&#8220;awesome-you-fork&#8221;<\/strong> argument, you are pulling and pushing from upstream.<\/p>\n<h3>Solution #2: cheat &#8220;go get&#8221;, DIY<\/h3>\n<p>The problem began with the <strong>go get<\/strong> command, which copied the URI path onto <strong>$GOPATH\/src<\/strong>. However <strong>go get<\/strong> implicitly issues a git clone, and we can do the same ourselves. We will dirty our hands just once, and then benefit from an ambiguous-less environment.<\/p>\n<p>We will now create our git repository in the name of <strong>awesome-org<\/strong> but with the contents of <strong>awesome-you<\/strong>:<\/p>\n<blockquote>\n<pre>cd $GOPATH\r\nmkdir -p {src,bin,pkg}\r\nmkdir -p <strong>src\/github.com\/awesome-org\/<\/strong>\r\ncd src\/github.com\/awesome-org\/\r\ngit clone git@github.com:<strong>awesome-you\/tool.git<\/strong> # OR: git clone https:\/\/github.com\/<strong>awesome-you\/tool.git<\/strong>\r\ncd tool\/\r\ngo get .\/...<\/pre>\n<\/blockquote>\n<p>The\u00a0<strong>mkdir -p {src,bin,pkg}<\/strong> is there just in case you do not have anything setup in your <strong>$GOPATH<\/strong>. We then create the repository path under the name of <strong>awesome-org<\/strong>, but once inside clone from\u00a0<strong>awesome-you<\/strong>.<\/p>\n<p>The source code&#8217;s import path fits your\u00a0directory layout now, but as you push\/pull you are only speaking to your own <strong>awesome-you<\/strong> repository.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: there&#8217;s an awesome Golang project on GitHub which you want to fork. You want to develop &amp; collaborate on that fork, but the golang import\u00a0path, in your source code, still references the original\u00a0path, breaking everything. A couple solutions offered below. First, though, let&#8217;s\u00a0get some names. A sample case, the problem at hand There&#8217;s an [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[53],"tags":[123,124],"class_list":["post-7506","post","type-post","status-publish","format-standard","hentry","category-development","tag-github","tag-golang"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bZZp-1X4","_links":{"self":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/7506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/comments?post=7506"}],"version-history":[{"count":25,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/7506\/revisions"}],"predecessor-version":[{"id":7531,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/7506\/revisions\/7531"}],"wp:attachment":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/media?parent=7506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/categories?post=7506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/tags?post=7506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}