Making Cgit Repos Installable as Go Packages
Putting my private Git server to good use
In my quest to learn the real ins and outs of Go by going through The Go Programming Language, I decided to use my newly minted private repo hub to store exercises from the book as packages I can reuse for later exercises. For example, I decided to make the lissajous example from Chapter 1 into a separate installable package. The lissajous package is used to create a GIF of a Lissajous curve, which was a staple visual effect in old sci-fi movies.
However, simply "go-getting" from the repo's URL, as you would in the case of GitHub, isn't that simple. There were multiple hiccups along the way which I had to overcome. What follows is my best attempt to piece together what I did to finally enable Go package installation from my private Git server. Hopefully this account will serve two purposes:
-
Set people straight who are looking for answers to this same question.
-
Serve as a reference for my future self in case I have to do this again.
Steps
Inject the appropriate HTML meta tag from Nginx
This excellent blog post by Anirudh
Oppiliappan set me on the right path for fixing an error I encountered
early on involving a missing go-import something-or-other:
I ended up putting the sub_filter stuff inside the location @cgit
block:
1 location @cgit {
2 include fastcgi_params;
3 fastcgi_param SCRIPT_FILENAME /usr/lib/cgit/cgit.cgi;
4 fastcgi_param PATH_INFO $uri;
5 fastcgi_param QUERY_STRING $args;
6 fastcgi_param HTTP_HOST $server_name;
7 fastcgi_pass unix:/run/fcgiwrap.socket;
8
9 # Make our repos go-gettable.
10 sub_filter '</head>'
11 '<meta name="go-import" content="$host$uri git https://$host$uri"></head>';
12
13 sub_filter_once on ;
14 }
Remembering the semicolons here is important. You can always use sudo nginx -t to test whether your current config is valid.
Configure Git to use SSH for the Git server
Add this block to your home directory's .gitconfig file, making the
appropriate substitution for "example.com":
1[url "git@example.com:"]
2 insteadOf = https://git.example.com/
Alternatively, you can issue the equivalent command line invocation:
git config --global url."git@example.com".insteadOf "https://git.example.com/"
Now, whenever you invoke go get, you'll be prompted for your SSH
password.
Note that, in this case, the git in git@example.com refers to the
Linux user on your remote server that owns the directory containing
all your Git repos. This syntax mirrors the one used when logging into
the same server via SSH, viz. ssh git@example.com.
Set GOPRIVATE (optional?)
I'm not sure I need to set this in my case, since AFAICT this has more to do with close-sourcing code, which isn't my intention here. But I threw it in just in case.
Since I want all my repos to be potentially installable as Go packages for now, so I use a glob to indicate that:
go env -w GOPRIVATE=git.brandonirizarry.xyz/*
Initially, I had set GOPRIVATE to point to the lissajous repo
only, though this glob technique should work also (and be way easier
to maintain.)