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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The reachability filters `branch`, `tag` and `for-each-ref` share://! `--contains`, `--no-contains`, `--merged`, `--no-merged` and//! `--points-at`, read and answered as git does.//!//! A ref is kept when it contains at least one `--contains` commit and//! none of the `--no-contains` ones, is reachable from at least one//! `--merged` commit and from none of the `--no-merged` ones, and points//! at a `--points-at` object, directly or through tags. A ref that does//! not lead to a commit is kept by none of the first four. A name that is//! not an object stops git with its usage status, and so does a commit//! that is not one.
const std = @import("std");const Allocator = std.mem.Allocator;const Io = std.Io;
const git = @import("git");const ObjectId = git.ObjectId;const Diagnostics = git.Diagnostics;const tool = @import("zig_git.zig");const Context = tool.Context;const revisions = @import("revisions.zig");const history = @import("history.zig");
/// The filters as given.pub const Filters = struct { contains: std.ArrayList([]const u8) = .empty, no_contains: std.ArrayList([]const u8) = .empty, merged: std.ArrayList([]const u8) = .empty, no_merged: std.ArrayList([]const u8) = .empty, points_at: std.ArrayList([]const u8) = .empty,
pub fn deinit(f: *Filters, gpa: Allocator) void { f.contains.deinit(gpa); f.no_contains.deinit(gpa); f.merged.deinit(gpa); f.no_merged.deinit(gpa); f.points_at.deinit(gpa); }
/// Whether any filter was given. pub fn active(f: *const Filters) bool { return f.contains.items.len != 0 or f.no_contains.items.len != 0 or f.merged.items.len != 0 or f.no_merged.items.len != 0 or f.points_at.items.len != 0; }
/// Reads `arg`, and `next` if it takes it, as one of the filters: /// null if it is not one, else how many arguments it took. pub fn parse(f: *Filters, gpa: Allocator, arg: []const u8, next: ?[]const u8) !?usize { const eq = std.mem.findScalar(u8, arg, '='); const name = arg[0 .. eq orelse arg.len]; const value: ?[]const u8 = if (eq) |e| arg[e + 1 ..] else null; const Name = enum { @"--contains", @"--no-contains", @"--merged", @"--no-merged", @"--points-at" }; const n = std.meta.stringToEnum(Name, name) orelse return null; const list = switch (n) { .@"--contains" => &f.contains, .@"--no-contains" => &f.no_contains, .@"--merged" => &f.merged, .@"--no-merged" => &f.no_merged, .@"--points-at" => &f.points_at, }; if (value) |v| { try list.append(gpa, v); return 1; } // `--points-at` needs its object; the others default to HEAD, and // take the next argument only if it is not an option. if (n == .@"--points-at") { try list.append(gpa, next orelse return error.MissingValue); return 2; } if (next) |v| if (v.len == 0 or v[0] != '-') { try list.append(gpa, v); return 2; }; try list.append(gpa, "HEAD"); return 1; }};
/// The filters resolved against a repository.pub const Prepared = struct { gpa: Allocator, io: Io, h: *history.History, db: git.ObjectDatabase, contains: []git.Commits.Index, no_contains: []git.Commits.Index, merged: []git.Commits.Index, no_merged: []git.Commits.Index, points_at: []ObjectId, any: bool,
pub fn deinit(p: *Prepared) void { p.gpa.free(p.contains); p.gpa.free(p.no_contains); p.gpa.free(p.merged); p.gpa.free(p.no_merged); p.gpa.free(p.points_at); }
/// Whether the ref whose value is `id` passes every filter. pub fn keeps(p: *Prepared, id: ObjectId, stderr: *Io.Writer) !bool { if (!p.any) return true; if (p.points_at.len != 0) { var found = false; var current = id; for (0..git.RefStore.max_peel_depth) |_| { for (p.points_at) |x| if (x.eql(current)) { found = true; }; if (found) break; const h = p.db.readHeader(p.io, current, null) catch break; if (h.kind != .tag) break; var o = p.db.read(p.io, p.gpa, current, null) catch break; defer o.deinit(p.gpa); current = git.Tag.targetOf(current.object_format, o.data) orelse break; } if (!found) return false; } if (p.contains.len == 0 and p.no_contains.len == 0 and p.merged.len == 0 and p.no_merged.len == 0) return true; const peeled = git.RefStore.peel(p.io, p.gpa, p.db, id, null) catch return false; if (peeled.kind != .commit) return false; const store = &p.h.store; const c = try store.name(peeled.id); var diag: Diagnostics = .{}; const Ancestry = struct { fn of(pp: *Prepared, a: git.Commits.Index, d: git.Commits.Index, dg: *Diagnostics, w: *Io.Writer) !bool { return git.merge_base.isAncestor(pp.gpa, &pp.h.store, a, d, dg) catch |err| return pp.h.fail(err, dg, null, w); } }; if (p.contains.len != 0) { var any = false; for (p.contains) |x| if (try Ancestry.of(p, x, c, &diag, stderr)) { any = true; break; }; if (!any) return false; } for (p.no_contains) |x| if (try Ancestry.of(p, x, c, &diag, stderr)) return false; if (p.merged.len != 0) { var any = false; for (p.merged) |x| if (try Ancestry.of(p, c, x, &diag, stderr)) { any = true; break; }; if (!any) return false; } for (p.no_merged) |x| if (try Ancestry.of(p, c, x, &diag, stderr)) return false; return true; }};
/// Resolves the filters, saying what git says of a name it cannot use:/// a usage error.pub fn prepare(f: *const Filters, ctx: Context, repo: *git.Repository, revs: *revisions.Revisions, h: *history.History, stderr: *Io.Writer) !Prepared { const gpa = ctx.gpa; var p: Prepared = .{ .gpa = gpa, .io = ctx.io, .h = h, .db = repo.objects(), .contains = &.{}, .no_contains = &.{}, .merged = &.{}, .no_merged = &.{}, .points_at = &.{}, .any = f.active(), }; errdefer p.deinit(); var points: std.ArrayList(ObjectId) = .empty; errdefer points.deinit(gpa); for (f.points_at.items) |text| { var diag: Diagnostics = .{}; const found = revs.resolver.resolve(text, &diag) catch { revs.resolver.clearMessages(); try stderr.print("error: malformed object name '{s}'\n", .{text}); return error.UsageQuiet; }; try revs.flushMessages(stderr, 1, false); try points.append(gpa, found.id); } p.points_at = try points.toOwnedSlice(gpa); inline for (.{ "contains", "no_contains", "merged", "no_merged" }) |field| { var list: std.ArrayList(git.Commits.Index) = .empty; errdefer list.deinit(gpa); for (@field(f, field).items) |text| { var diag: Diagnostics = .{}; const found = revs.resolver.resolve(text, &diag) catch { revs.resolver.clearMessages(); try stderr.print("error: malformed object name {s}\n", .{text}); return error.UsageQuiet; }; try revs.flushMessages(stderr, 1, false); const c = try revs.resolver.peelToCommit(found.id) orelse { revs.resolver.clearMessages(); const kind = found.kind orelse git.ObjectKind.blob; try stderr.print("error: object {f} is a {s}, not a commit\nerror: no such commit {s}\n", .{ found.id, kind.name(), text }); return error.UsageQuiet; }; try list.append(gpa, try h.store.name(c)); } @field(p, field) = try list.toOwnedSlice(gpa); } return p;}