refactor: add git.rev_parse and git.cat_file functions

This commit is contained in:
CJ van den Berg 2025-12-15 16:45:57 +01:00
parent 8546c6ca14
commit 081ff6ec4f
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -268,6 +268,32 @@ pub fn new_or_modified_files(context_: usize) Error!void {
}.result, exit_null(tag));
}
pub fn rev_parse(context_: usize, rev: []const u8, file_path: []const u8) Error!void {
const tag = @src().fn_name;
var arg: std.Io.Writer.Allocating = .init(allocator);
defer arg.deinit();
if (file_path.len == 0)
arg.writer.print("{s}", .{rev})
else
arg.writer.print("{s}:{s}", .{ rev, file_path });
try git(context_, .{ "rev-parse", arg.written() }, struct {
fn result(context: usize, parent: tp.pid_ref, output: []const u8) void {
var it = std.mem.splitScalar(u8, output, '\n');
while (it.next()) |value| if (value.len > 0)
parent.send(.{ module_name, context, tag, output }) catch {};
}
}.result, exit_null_on_error(tag));
}
pub fn cat_file(context_: usize, object: []const u8) Error!void {
const tag = @src().fn_name;
try git(context_, .{ "cat-file", "-p", object }, struct {
fn result(context: usize, parent: tp.pid_ref, output: []const u8) void {
parent.send(.{ module_name, context, tag, output }) catch {};
}
}.result, exit_null_on_error(tag));
}
fn git_line_output(context_: usize, comptime tag: []const u8, cmd: anytype) Error!void {
try git_err(context_, cmd, struct {
fn result(context: usize, parent: tp.pid_ref, output: []const u8) void {