Something went wrong. Try again.
Zig bindings for all 506 operations of the Forgejo API, generated from its own description, and zfj, a command line built on them.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The repository a clone came from, read off its git remote: which host it//! is on, and which owner and repository there.//!//! The URL is asked of `git remote get-url` rather than read out of//! `.git/config`, because worktrees, `includeIf` and `url.<base>.insteadOf`//! all make that file say something other than what git would use.
const std = @import("std");const Allocator = std.mem.Allocator;const Io = std.Io;
pub const Remote = struct { /// Which remote it was: `upstream` or `origin`. name: []const u8, url: []const u8, /// The host's name, without a user or a port. host: []const u8, owner: []const u8, repo: []const u8,};
/// The remotes asked, in order: a fork's `upstream` is where its issues are.pub const remote_names: []const []const u8 = &.{ "upstream", "origin" };
/// The first of `remote_names` that the clone in the working directory has,/// or null when it has neither, or git cannot be run, or it is not a clone./// A remote whose URL names no owner and repository is passed over.pub fn findRemote(arena: Allocator, io: Io) ?Remote { for (remote_names) |name| { const url = getUrl(arena, io, name) orelse continue; if (parseUrl(url)) |location| return .{ .name = name, .url = url, .host = location.host, .owner = location.owner, .repo = location.repo, }; } return null;}
/// Where the clone's remote `name` points, or null as for `findRemote`.pub fn remoteLocation(arena: Allocator, io: Io, name: []const u8) ?Location { return parseUrl(getUrl(arena, io, name) orelse return null);}
/// The branch checked out in the working directory, or null when HEAD is/// detached, or git cannot be run, or it is not a clone.pub fn currentBranch(arena: Allocator, io: Io) ?[]const u8 { return runGit(arena, io, &.{ "git", "symbolic-ref", "--quiet", "--short", "HEAD" });}
/// The full hash of the commit `rev` names in the clone -- `HEAD`, a short/// hash, a tag -- or null outside a clone or when it names none.pub fn resolveCommit(arena: Allocator, io: Io, rev: []const u8) ?[]const u8 { const spec = std.fmt.allocPrint(arena, "{s}^{{commit}}", .{rev}) catch return null; return runGit(arena, io, &.{ "git", "rev-parse", "--verify", "--quiet", "--end-of-options", spec });}
/// The top level of the clone in the working directory, or null outside/// one, or when git cannot be run.pub fn topLevel(arena: Allocator, io: Io) ?[]const u8 { return runGit(arena, io, &.{ "git", "rev-parse", "--show-toplevel" });}
/// Runs git on the terminal, saying what it says, for a command whose/// output is for the person running zfj: `git clone`, say. Fails when git/// cannot be run; otherwise says whether it succeeded.pub fn runShown(io: Io, argv: []const []const u8) std.process.SpawnError!bool { var child = try std.process.spawn(io, .{ .argv = argv, .stdin = .ignore }); const term = child.wait(io) catch return false; return switch (term) { .exited => |code| code == 0, else => false, };}
/// Where the clone's remote `name` points, as git would use it, or null as/// for `findRemote`.pub fn remoteUrl(arena: Allocator, io: Io, name: []const u8) ?[]const u8 { return getUrl(arena, io, name);}
fn getUrl(arena: Allocator, io: Io, name: []const u8) ?[]const u8 { return runGit(arena, io, &.{ "git", "remote", "get-url", name });}
/// What a git command wrote, trimmed, or null when it failed or wrote/// nothing.fn runGit(arena: Allocator, io: Io, argv: []const []const u8) ?[]const u8 { const result = std.process.run(arena, io, .{ .argv = argv, .stdout_limit = .limited(64 * 1024), .stderr_limit = .limited(64 * 1024), }) catch return null; switch (result.term) { .exited => |code| if (code != 0) return null, else => return null, } const out = std.mem.trim(u8, result.stdout, " \t\r\n"); return if (out.len == 0) null else out;}
pub const Location = struct { host: []const u8, owner: []const u8, repo: []const u8,};
/// Where a remote URL points, in any of the forms git takes for one:/// `https://host/owner/repo.git`, `ssh://git@host:2222/owner/repo.git`, or/// the scp-like `git@host:owner/repo.git`. The owner and repository are the/// last two parts of the path, so an instance served under a path of its/// own still reads right.pub fn parseUrl(url: []const u8) ?Location { var host_and_path: []const u8 = undefined; var path_separator: u8 = '/'; if (std.mem.find(u8, url, "://")) |scheme_end| { host_and_path = url[scheme_end + 3 ..]; } else if (std.mem.findScalar(u8, url, ':')) |colon| { // scp-like: `[user@]host:path`, which git takes only when no slash // comes before the colon. if (std.mem.findScalar(u8, url[0..colon], '/') != null) return null; host_and_path = url; path_separator = ':'; } else return null;
const path_start = std.mem.findScalar(u8, host_and_path, path_separator) orelse return null; var authority = host_and_path[0..path_start]; if (std.mem.findScalarLast(u8, authority, '@')) |at| authority = authority[at + 1 ..]; if (path_separator == '/') { if (std.mem.findScalarLast(u8, authority, ':')) |colon| authority = authority[0..colon]; } if (authority.len == 0) return null;
var path = std.mem.trim(u8, host_and_path[path_start + 1 ..], "/"); if (std.mem.endsWith(u8, path, ".git")) path = path[0 .. path.len - ".git".len]; const last_slash = std.mem.findScalarLast(u8, path, '/') orelse return null; const repo = path[last_slash + 1 ..]; const before = path[0..last_slash]; const owner = before[(if (std.mem.findScalarLast(u8, before, '/')) |s| s + 1 else 0)..]; if (owner.len == 0 or repo.len == 0) return null; return .{ .host = authority, .owner = owner, .repo = repo };}
/// The host of an instance's URL, without a user or a port, for comparing/// with a remote's.pub fn urlHost(url: []const u8) ?[]const u8 { const scheme_end = std.mem.find(u8, url, "://") orelse return null; const rest = url[scheme_end + 3 ..]; var authority = rest[0 .. std.mem.findScalar(u8, rest, '/') orelse rest.len]; if (std.mem.findScalarLast(u8, authority, '@')) |at| authority = authority[at + 1 ..]; if (std.mem.findScalarLast(u8, authority, ':')) |colon| authority = authority[0..colon]; return if (authority.len == 0) null else authority;}
test "an instance's host is its URL's, without a user or a port" { try testing.expectEqualStrings("git.jcollie.dev", urlHost("https://git.jcollie.dev").?); try testing.expectEqualStrings("example.org", urlHost("http://me@example.org:3000/forge/").?); try testing.expect(urlHost("git.jcollie.dev") == null);}
const testing = std.testing;
fn expectLocation(url: []const u8, host: []const u8, owner: []const u8, repo: []const u8) !void { const location = parseUrl(url) orelse return error.TestExpectedLocation; try testing.expectEqualStrings(host, location.host); try testing.expectEqualStrings(owner, location.owner); try testing.expectEqualStrings(repo, location.repo);}
test "remote URLs in every form git takes" { try expectLocation("https://git.jcollie.dev/jeff/zfj.git", "git.jcollie.dev", "jeff", "zfj"); try expectLocation("https://git.jcollie.dev/jeff/zfj", "git.jcollie.dev", "jeff", "zfj"); try expectLocation("https://someone@codeberg.org:443/forgejo/forgejo/", "codeberg.org", "forgejo", "forgejo"); try expectLocation("ssh://git@git.jcollie.dev/jeff/zfj.git", "git.jcollie.dev", "jeff", "zfj"); try expectLocation("ssh://git@git.example.org:2222/team/thing.git", "git.example.org", "team", "thing"); try expectLocation("git@codeberg.org:forgejo/forgejo.git", "codeberg.org", "forgejo", "forgejo"); try expectLocation("codeberg.org:forgejo/forgejo", "codeberg.org", "forgejo", "forgejo"); // An instance under a path of its own: the last two parts are the repo. try expectLocation("https://example.org/forge/team/thing.git", "example.org", "team", "thing");}
test "a URL with no owner and repository is not a location" { try testing.expect(parseUrl("https://git.jcollie.dev/zfj") == null); try testing.expect(parseUrl("/home/jeff/dev/zfj") == null); try testing.expect(parseUrl("./relative/path/repo") == null); try testing.expect(parseUrl("https:///owner/repo") == null);}