refactor: re-work git.rev_parse and git.cat_file

This commit is contained in:
CJ van den Berg 2025-12-17 20:45:08 +01:00
parent 4f60e906d7
commit 0017dabcf0
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -165,13 +165,7 @@ pub fn status(context_: usize) Error!void {
// parent.send(.{ module_name, context, tag, value }) catch {}; // parent.send(.{ module_name, context, tag, value }) catch {};
} }
} }
}.result, struct { }.result, log_err, exit_null(tag));
fn result(_: usize, _: tp.pid_ref, output: []const u8) void {
var it = std.mem.splitScalar(u8, output, '\n');
while (it.next()) |line| if (line.len > 0)
std.log.err("{s}: {s}", .{ module_name, line });
}
}.result, exit_null(tag));
} }
pub fn new_or_modified_files(context_: usize) Error!void { pub fn new_or_modified_files(context_: usize) Error!void {
@ -259,13 +253,7 @@ pub fn new_or_modified_files(context_: usize) Error!void {
} }
} }
} }
}.result, struct { }.result, log_err, exit_null(tag));
fn result(_: usize, _: tp.pid_ref, output: []const u8) void {
var it = std.mem.splitScalar(u8, output, '\n');
while (it.next()) |line| if (line.len > 0)
std.log.err("{s}: {s}", .{ module_name, line });
}
}.result, exit_null(tag));
} }
pub fn rev_parse(context_: usize, rev: []const u8, file_path: []const u8) Error!void { pub fn rev_parse(context_: usize, rev: []const u8, file_path: []const u8) Error!void {
@ -276,22 +264,22 @@ pub fn rev_parse(context_: usize, rev: []const u8, file_path: []const u8) Error!
try arg.writer.print("{s}", .{rev}) try arg.writer.print("{s}", .{rev})
else else
try arg.writer.print("{s}:{s}", .{ rev, file_path }); try arg.writer.print("{s}:{s}", .{ rev, file_path });
try git(context_, .{ "rev-parse", arg.written() }, struct { try git_err(context_, .{ "rev-parse", arg.written() }, struct {
fn result(context: usize, parent: tp.pid_ref, output: []const u8) void { fn result(context: usize, parent: tp.pid_ref, output: []const u8) void {
var it = std.mem.splitScalar(u8, output, '\n'); var it = std.mem.splitScalar(u8, output, '\n');
while (it.next()) |value| if (value.len > 0) while (it.next()) |value| if (value.len > 0)
parent.send(.{ module_name, context, tag, output }) catch {}; parent.send(.{ module_name, context, tag, value }) catch {};
} }
}.result, exit_null_on_error(tag)); }.result, log_err, exit_null(tag));
} }
pub fn cat_file(context_: usize, object: []const u8) Error!void { pub fn cat_file(context_: usize, object: []const u8) Error!void {
const tag = @src().fn_name; const tag = @src().fn_name;
try git(context_, .{ "cat-file", "-p", object }, struct { try git_err(context_, .{ "cat-file", "-p", object }, struct {
fn result(context: usize, parent: tp.pid_ref, output: []const u8) void { fn result(context: usize, parent: tp.pid_ref, output: []const u8) void {
parent.send(.{ module_name, context, tag, output }) catch {}; parent.send(.{ module_name, context, tag, output }) catch {};
} }
}.result, exit_null_on_error(tag)); }.result, log_err, exit_null(tag));
} }
fn git_line_output(context_: usize, comptime tag: []const u8, cmd: anytype) Error!void { fn git_line_output(context_: usize, comptime tag: []const u8, cmd: anytype) Error!void {
@ -301,13 +289,7 @@ fn git_line_output(context_: usize, comptime tag: []const u8, cmd: anytype) Erro
while (it.next()) |value| if (value.len > 0) while (it.next()) |value| if (value.len > 0)
parent.send(.{ module_name, context, tag, value }) catch {}; parent.send(.{ module_name, context, tag, value }) catch {};
} }
}.result, struct { }.result, log_err, exit_null(tag));
fn result(_: usize, _: tp.pid_ref, output: []const u8) void {
var it = std.mem.splitScalar(u8, output, '\n');
while (it.next()) |line| if (line.len > 0)
std.log.err("{s}: {s}", .{ module_name, line });
}
}.result, exit_null(tag));
} }
fn git( fn git(
@ -378,6 +360,12 @@ fn to_shell_output_handler(handler: anytype) shell.OutputHandler {
}.out; }.out;
} }
fn log_err(_: usize, _: tp.pid_ref, output: []const u8) void {
var it = std.mem.splitScalar(u8, output, '\n');
while (it.next()) |line| if (line.len > 0)
std.log.err("{s}: {s}", .{ module_name, line });
}
fn noop(_: usize, _: tp.pid_ref, _: []const u8) void {} fn noop(_: usize, _: tp.pid_ref, _: []const u8) void {}
var git_path: ?struct { var git_path: ?struct {