Something went wrong. Try again.
Reading and writing git repositories in Zig, without running git.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const mod = b.addModule("git", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); // Dates and time zones: git's date parsers, the time zone database and // strftime, none of them ported from git or glibc (ADR 0004). const datetime = b.dependency("datetime", .{ .target = target, .optimize = optimize }); mod.addImport("datetime", datetime.module("datetime"));
const test_filter = b.option( []const u8, "test-filter", "Run only the tests whose name contains this", ); const filters: []const []const u8 = if (test_filter) |f| &.{f} else &.{};
// LLVM, because the self-hosted backend that Debug uses otherwise emits // no coverage instrumentation: kcov reports nothing and Zig's fuzzer ends // with "corrupted coverage file: pcs_len was zero". const tests = b.addTest(.{ .name = "zig-git-test", .root_module = mod, .filters = filters, .use_llvm = true, });
const test_step = b.step("test", "Run tests"); test_step.dependOn(&b.addRunArtifact(tests).step);
// Again, with the repositories the tests make using SHA-256 object IDs // rather than SHA-1's. const run_tests_sha256 = b.addRunArtifact(tests); run_tests_sha256.setEnvironmentVariable("ZIG_GIT_TEST_OBJECT_FORMAT", "sha256"); test_step.dependOn(&run_tests_sha256.step);
// A step of its own for `zig build fuzz --fuzz`, which takes over the // terminal and runs until stopped, so it must not be reached by // `zig build test`. Without `--fuzz` it runs each fuzz test once on its // seeds, as `zig build test` already does. const fuzz_step = b.step("fuzz", "The tests, fuzz tests included: add --fuzz to fuzz them"); fuzz_step.dependOn(&b.addRunArtifact(tests).step);
// Compiles everything without running it. const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&tests.step);
// -- zig-git ------------------------------------------------------------- // // A few of git's plumbing commands answered by the library, printing // what git prints, for comparing the two on any repository. const tool = b.addExecutable(.{ .name = "zig-git", .root_module = b.createModule(.{ .root_source_file = b.path("tools/zig_git.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "git", .module = mod }}, }), }); b.installArtifact(tool); check_step.dependOn(&tool.step);
const run_tool = b.addRunArtifact(tool); run_tool.addPassthruArgs(); const run_step = b.step("run", "Run zig-git: zig build run -- <command> [<args>...]"); run_step.dependOn(&run_tool.step);
// Its tests compare it with git, under both object formats. const tool_tests = b.addTest(.{ .name = "zig-git-tool-test", .root_module = tool.root_module, .filters = filters, .use_llvm = true, }); check_step.dependOn(&tool_tests.step); test_step.dependOn(&b.addRunArtifact(tool_tests).step); const run_tool_tests_sha256 = b.addRunArtifact(tool_tests); run_tool_tests_sha256.setEnvironmentVariable("ZIG_GIT_TEST_OBJECT_FORMAT", "sha256"); test_step.dependOn(&run_tool_tests_sha256.step);
// -- documentation ------------------------------------------------------- // // Zig emits the API documentation as a side effect of compiling, so the // module is built as a library purely to get at it. What comes out is not // a page but a program: a WebAssembly viewer, its javascript, and a tar of // the sources it reads from. const library = b.addLibrary(.{ .name = "git", .root_module = mod }); const install_docs = b.addInstallDirectory(.{ .source_dir = library.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); const docs_step = b.step("docs", "Build the API documentation into zig-out/docs"); docs_step.dependOn(&install_docs.step);
// That viewer fetches `sources.tar` and `main.wasm` at runtime, which a // browser refuses to do from a `file://` page, so reading the docs // locally means serving them. const docs_port = b.option(u16, "docs-port", "Port for `zig build docs-serve` (default 8000)") orelse 8000;
const docs_server = b.addExecutable(.{ .name = "docs-server", .root_module = b.createModule(.{ .root_source_file = b.path("tools/docs_server.zig"), // Always the machine running the build, never whatever -Dtarget // the library is being built for. .target = b.graph.host, .optimize = .Debug, }), });
const run_docs_server = b.addRunArtifact(docs_server); run_docs_server.step.dependOn(&install_docs.step); run_docs_server.addDirectoryArg(library.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); // It runs until interrupted, so its output has to reach the terminal // rather than being captured by the build runner. run_docs_server.stdio = .inherit;
const docs_serve_step = b.step("docs-serve", "Serve the API documentation over HTTP"); docs_serve_step.dependOn(&run_docs_server.step);
// The server has a test of its own; without this it would never run. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step);
// The generator of src/width_tables.zig is run by hand with `zig run`; // its test keeps it compiling and correct in the meantime. const gen_width_tests = b.addTest(.{ .name = "gen-width-test", .root_module = b.createModule(.{ .root_source_file = b.path("tools/gen_width.zig"), .target = b.graph.host, .optimize = .Debug, }), }); test_step.dependOn(&b.addRunArtifact(gen_width_tests).step); check_step.dependOn(&gen_width_tests.step);}