Something went wrong. Try again.
A Zig library that reads git repositories without running git, with log, show and diff output byte-identical to git's.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! What changes which commits a repository has, compared with git: replace//! refs of commits, trees, blobs and tags, chains of them and the settings//! that turn them off; grafts; and shallow clones made with `--depth`,//! `--shallow-since` and `--shallow-exclude` — each with and without a//! commit-graph, under SHA-1 and SHA-256.
const std = @import("std");const Io = std.Io;
const tool = @import("zig_git.zig");const Repo = tool.Repo;const walk_tests = @import("walk_tests.zig");
const testing = std.testing;
fn same(repo: *Repo, args: []const []const u8) !void { try tool.expectSameAsGitHow(repo, args, .{});}
fn sameIn(repo: *Repo, cwd: []const u8, args: []const []const u8) !void { try tool.expectSameAsGitHow(repo, args, .{ .cwd = cwd });}
fn line(repo: *Repo, args: []const []const u8) ![]u8 { const out = try repo.output(args); defer repo.gpa.free(out); return repo.gpa.dupe(u8, std.mem.trimEnd(u8, out, "\n"));}
/// The commands every history is compared with.const walks = [_][]const []const u8{ &.{ "log", "--format=%H %P %s" }, &.{ "log", "--format=%h %T %s", "--all" }, &.{ "rev-list", "--all" }, &.{ "rev-list", "--topo-order", "--parents", "main" }, &.{ "rev-list", "--boundary", "b1..main" }, &.{ "rev-list", "--count", "--all" }, &.{ "rev-list", "--first-parent", "main" }, &.{ "log", "--format=%s", "main", "--", "f1" }, &.{ "merge-base", "--all", "main", "b1" }, &.{ "merge-base", "--independent", "main", "b1", "b2", "b3" }, &.{ "rev-parse", "main~3", "main^2", "b1~2^{tree}" }, &.{ "show", "-s", "--format=%H %P %T", "main~2", "b2" },};
fn compareWalks(repo: *Repo, cwd: []const u8, prefix: []const []const u8) !void { var args: std.ArrayList([]const u8) = .empty; defer args.deinit(repo.gpa); for (walks) |w| { args.clearRetainingCapacity(); try args.appendSlice(repo.gpa, prefix); try args.appendSlice(repo.gpa, w); try sameIn(repo, cwd, args.items); }}
test "replace refs of commits, trees, blobs and tags read as git reads them" { const gpa = testing.allocator; var repo = try Repo.init(gpa, testing.io); defer repo.deinit(); try walk_tests.makeHistory(&repo, 7, 80);
// A commit grafted onto another parent, one replaced by another, a // tree, a blob and an annotated tag. try repo.git(&.{ "replace", "--graft", "main~2", "b2~3" }); const b3 = try line(&repo, &.{ "rev-parse", "b3" }); defer gpa.free(b3); try repo.git(&.{ "replace", "-f", "b1~1", b3 }); const t_old = try line(&repo, &.{ "rev-parse", "b2~1^{tree}" }); defer gpa.free(t_old); const t_new = try line(&repo, &.{ "rev-parse", "b4^{tree}" }); defer gpa.free(t_new); try repo.git(&.{ "replace", "-f", t_old, t_new }); const f_old = try line(&repo, &.{ "rev-parse", "main:f1" }); defer gpa.free(f_old); const f_new = try repo.hashObject("blob", "replaced\n"); defer gpa.free(f_new); try repo.git(&.{ "replace", "-f", f_old, f_new }); const tags = try line(&repo, &.{ "tag", "-l", "t*" }); defer gpa.free(tags); var it = std.mem.tokenizeScalar(u8, tags, '\n'); const tag1 = it.next().?; const tag2 = it.next().?; const id1 = try line(&repo, &.{ "rev-parse", tag1 }); defer gpa.free(id1); const id2 = try line(&repo, &.{ "rev-parse", tag2 }); defer gpa.free(id2); try repo.git(&.{ "replace", "-f", id1, id2 });
for ([_]bool{ false, true }) |graph| { if (graph) try repo.git(&.{ "commit-graph", "write", "--reachable" }); try compareWalks(&repo, ".", &.{}); try compareWalks(&repo, ".", &.{"--no-replace-objects"}); try compareWalks(&repo, ".", &.{ "-c", "core.useReplaceRefs=false" }); for ([_][]const u8{ "-p", "-t", "-s", "-e" }) |o| { for ([_][]const u8{ "main~2", "b1~1", t_old, f_old, "main:f1", id1, tag1 }) |name| { try same(&repo, &.{ "cat-file", o, name }); try same(&repo, &.{ "--no-replace-objects", "cat-file", o, name }); } } try same(&repo, &.{ "cat-file", "commit", "main~2" }); const peeled = try std.fmt.allocPrint(gpa, "{s}^{{commit}}", .{tag1}); defer gpa.free(peeled); try same(&repo, &.{ "rev-parse", "--verify", peeled }); }
// The environment turns them off, or finds them elsewhere. try repo.env.put("GIT_NO_REPLACE_OBJECTS", "0"); try compareWalks(&repo, ".", &.{}); _ = repo.env.swapRemove("GIT_NO_REPLACE_OBJECTS"); try repo.env.put("GIT_REPLACE_REF_BASE", "refs/heads/"); try compareWalks(&repo, ".", &.{}); try repo.env.put("GIT_REPLACE_REF_BASE", "refs/elsewhere/"); try compareWalks(&repo, ".", &.{}); _ = repo.env.swapRemove("GIT_REPLACE_REF_BASE"); try same(&repo, &.{ "-c", "core.useReplaceRefs=nope", "rev-parse", "HEAD" }); try same(&repo, &.{ "-c", "core.useReplaceRefs=nope", "log" });}
test "chains of replacements are followed as far as git follows them" { const gpa = testing.allocator; var repo = try Repo.init(gpa, testing.io); defer repo.deinit(); try repo.commit("one"); var ids: [7][]u8 = undefined; for (&ids, 0..) |*id, i| { const text = try std.fmt.allocPrint(gpa, "object {d}\n", .{i}); defer gpa.free(text); id.* = try repo.hashObject("blob", text); } defer for (ids) |id| gpa.free(id); for (1..ids.len) |depth| { const name = try std.fmt.allocPrint(gpa, "refs/replace/{s}", .{ids[depth - 1]}); defer gpa.free(name); try repo.git(&.{ "update-ref", name, ids[depth] }); for ([_][]const u8{ "-p", "-t", "-s", "-e" }) |o| try same(&repo, &.{ "cat-file", o, ids[0] }); } // One that loops, which git will not write itself. const name = try std.fmt.allocPrint(gpa, ".git/refs/replace/{s}", .{ids[1]}); defer gpa.free(name); const value = try std.fmt.allocPrint(gpa, "{s}\n", .{ids[0]}); defer gpa.free(value); try repo.tmp.dir.writeFile(repo.io, .{ .sub_path = name, .data = value }); try same(&repo, &.{ "cat-file", "-p", ids[0] }); try same(&repo, &.{ "cat-file", "-e", ids[0] });
// Names git passes over, and those it reads. var repo2 = try Repo.init(gpa, testing.io); defer repo2.deinit(); try repo2.commit("one"); try repo2.commit("two"); const a = try repo2.hashObject("blob", "a\n"); defer gpa.free(a); const b = try repo2.hashObject("blob", "b\n"); defer gpa.free(b); const head = try line(&repo2, &.{ "rev-parse", "HEAD" }); defer gpa.free(head); const forms = [_][]const u8{ "refs/replace/x/{s}", "refs/replace/{s}zz", "refs/replace/xy{s}" }; for (forms) |form| { const ref = try std.mem.replaceOwned(u8, gpa, form, "{s}", a); defer gpa.free(ref); try repo2.git(&.{ "update-ref", ref, b }); try same(&repo2, &.{ "cat-file", "-p", a }); try same(&repo2, &.{ "log", "--format=%s" }); try repo2.git(&.{ "update-ref", "-d", ref }); }}
test "grafts give commits other parents, as git reads them" { const gpa = testing.allocator; var repo = try Repo.init(gpa, testing.io); defer repo.deinit(); try walk_tests.makeHistory(&repo, 11, 60); // New parents that are ancestors already, since a graft that makes a // cycle is a history git sorts differently. const ids = try line(&repo, &.{ "rev-parse", "main~1", "main~3", "main~8", "main~10", "b1", "b1~3" }); defer gpa.free(ids); var it = std.mem.tokenizeScalar(u8, ids, '\n'); var v: [6][]const u8 = undefined; for (&v) |*x| x.* = it.next().?; const contents = try std.fmt.allocPrint(gpa, "# grafts\n\n{s} {s} {s}\n{s}\n{s} {s}\nzz\n{s}\t{s} \r\n{s} {s}\n", .{ v[0], v[1], v[2], // a merge made of two other commits v[3], // a root v[4], v[1], // bad: two spaces v[4], v[5], // a tab, and blank space at the end v[0], v[3], // a duplicate }); defer gpa.free(contents); try repo.tmp.dir.createDirPath(repo.io, ".git/info"); try repo.tmp.dir.writeFile(repo.io, .{ .sub_path = ".git/info/grafts", .data = contents }); for ([_]bool{ false, true }) |graph| { if (graph) try repo.git(&.{ "commit-graph", "write", "--reachable" }); try compareWalks(&repo, ".", &.{}); try compareWalks(&repo, ".", &.{ "-c", "advice.graftFileDeprecated=false" }); try same(&repo, &.{ "cat-file", "-p", "main~1" }); try same(&repo, &.{ "rev-parse", "main~1" }); } // Elsewhere, named by the environment. try repo.tmp.dir.rename(".git/info/grafts", repo.tmp.dir, "grafts", repo.io); try compareWalks(&repo, ".", &.{}); try repo.env.put("GIT_GRAFT_FILE", "grafts"); defer _ = repo.env.swapRemove("GIT_GRAFT_FILE"); try compareWalks(&repo, ".", &.{}); // A file of nothing but comments grafts nothing, and leaves the // commit-graph in use. try repo.tmp.dir.writeFile(repo.io, .{ .sub_path = "grafts", .data = "# nothing\n" }); try compareWalks(&repo, ".", &.{});}
test "shallow clones stop where git stops, with and without a commit-graph" { const gpa = testing.allocator; var repo = try Repo.init(gpa, testing.io); defer repo.deinit(); try walk_tests.makeHistory(&repo, 13, 70); const url = try std.fmt.allocPrint(gpa, "file://{s}", .{repo.path}); defer gpa.free(url); const since = try line(&repo, &.{ "log", "-1", "--format=%ct", "main~6" }); defer gpa.free(since); const since_option = try std.fmt.allocPrint(gpa, "--shallow-since={s}", .{since}); defer gpa.free(since_option); const clones = [_]struct { dir: []const u8, args: []const []const u8 }{ .{ .dir = "depth1", .args = &.{ "--depth", "1", "--no-single-branch" } }, .{ .dir = "depth4", .args = &.{ "--depth", "4", "--no-single-branch" } }, .{ .dir = "since", .args = &.{ since_option, "--no-single-branch" } }, .{ .dir = "exclude", .args = &.{ "--shallow-exclude=b2", "--no-single-branch" } }, }; for (clones) |c| { var args: std.ArrayList([]const u8) = .empty; defer args.deinit(gpa); try args.appendSlice(gpa, &.{ "clone", "-q" }); try args.appendSlice(gpa, c.args); try args.appendSlice(gpa, &.{ url, c.dir }); try repo.git(args.items); // The clone's branches, as the walks name them. for ([_][]const u8{ "b1", "b2", "b3", "b4" }) |b| { const ref = try std.fmt.allocPrint(gpa, "refs/remotes/origin/{s}", .{b}); defer gpa.free(ref); const r = try repo.runIn(c.dir, &.{ "branch", "-q", b, ref }, ""); r.deinit(gpa); } for ([_]bool{ false, true }) |graph| { if (graph) { const r = try repo.runIn(c.dir, &.{ "commit-graph", "write", "--reachable" }, ""); r.deinit(gpa); } try compareWalks(&repo, c.dir, &.{}); try sameIn(&repo, c.dir, &.{ "rev-parse", "--is-shallow-repository" }); try sameIn(&repo, c.dir, &.{ "rev-list", "--boundary", "--all" }); try sameIn(&repo, c.dir, &.{ "log", "--format=%h %p", "--topo-order", "--all" }); } } // A bad shallow file stops git, whatever the command. try repo.tmp.dir.writeFile(repo.io, .{ .sub_path = "depth4/.git/shallow", .data = "not an object ID\n" }); try sameIn(&repo, "depth4", &.{ "log", "--format=%s" }); try sameIn(&repo, "depth4", &.{ "rev-parse", "--is-shallow-repository" }); try sameIn(&repo, "depth4", &.{ "rev-parse", "HEAD~1" }); try sameIn(&repo, "depth4", &.{ "rev-parse", "HEAD" });}