Search code examples
gitgogo-modules

How to convert the pseudo-version from Golang modules to a git commit?


I have two git repositories containing go modules, let's call them controller and api. controller imports api, so the go.mod file in the controller repo has a dependency to the module contained in the api repo.

My problem is that for the code in controller to run, I not only need the go code from api, but also some static files (which are generated from the code) contained in the repo. As far as I know, there is no way of packaging these static files into the go module so that they are automatically imported into my vendor folder.

My current approach is trying to add the folder with the static files as a sparsely checked out git submodule. However, in order to keep this submodule in sync with the imported go source code from api, I'd like to have a small shell script that automatically checks out the commit referenced by the version of the api module in the go.mod file.

While this probably works for release versions, I also want this to work for unreleased versions which have a pseudo-version of the format <semantic version>-<commit timestamp>-<commit hash prefix> in the go.mod file.

Is there any way to compute the full commit hash out of this pseudo-version? I am assuming that this is somehow possible, because go mod vendor is able to checkout the correct version, but the only process that comes to my mind is searching for commits that happened at the given time across all branches and then choosing the one where the hash prefix matches ... and that sounds really hard to do in bash.

Any ideas how to solve this? Alternative ways of importing my static files from api into controller in a way that keeps them in sync with the vendored version of the api module are also appreciated.


Solution

  • Thanks to Volker for the correct answer. It is actually quite easy to do using the embed package and works well.

    A drawback of this solution is that only files within a go package can be embedded. Since my generated files were in a top-level folder that did not contain any go code, I had to wrap them in a dummy package to embed them.