Zig NEWS

LI Yu
LI Yu

Posted on

tip: use any git branch in `build.zig.zon`

Note to myself :)

Normally zig pkg can be used like below

zig fetch --save https://github.com/liyu1981/zcmd.zig/archive/refs/tags/v0.2.2.tar.gz
Enter fullscreen mode Exit fullscreen mode

But what if we need to use the other branch (like the main branch)? There may be no release yet as it is still in developing.

One way is to clone the wanted pkg from github to local and zig fetch --save <cloned_pkg_dir>, that's one solution. The cloned pkg can be managed as git submodule.

The other way I found is, for any github branch, there is a URL provided by github.com in tar.gz (which zig requires) format. So we can use that.

For example, use my zcmd.zig repo at here as example.

get download zip link

From the Download ZIP menu we can copy the link. It will be something like https://github.com/liyu1981/zcmd.zig/archive/refs/heads/main.zip, then simply replace the final zip to tar.gz, we can use it as follows

zig fetch --save https://github.com/liyu1981/jstring.zig/archive/refs/heads/main.tar.gz
Enter fullscreen mode Exit fullscreen mode

zig will then use the main branch in build.zig.zon, and whenever we zig fetch again, we can update this dep to most recent commit.

Replacing main to other branch name then will enable us to use any other branch.

This method can be further extended to use branch snapshot from github to lock dep in any revision.

E.g.

zig fetch https://github.com/liyu1981/zcmd.zig/archive/9c83abcd6bb116ddc13694f773d97f9ba41c92dd.tar.gz
Enter fullscreen mode Exit fullscreen mode

will lock zcmd.zig in revision of commit 9c83abcd6bb116ddc13694f773d97f9ba41c92dd.

Latest comments (1)

Collapse
 
wsw0108 profile image
Shiwei Wang