From 5ad074c68135d6f3d846bad8bd68ecaf6d6d42df Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Sat, 19 Apr 2025 23:41:30 +0200 Subject: [PATCH] feat: start git client module --- build.zig | 10 ++++++++++ src/git.zig | 21 +++++++++++++++++++++ src/tui/mainview.zig | 6 ++++++ 3 files changed, 37 insertions(+) create mode 100644 src/git.zig diff --git a/build.zig b/build.zig index ac0c004..cbd51b8 100644 --- a/build.zig +++ b/build.zig @@ -420,6 +420,15 @@ pub fn build_exe( }, }); + const git_mod = b.createModule(.{ + .root_source_file = b.path("src/git.zig"), + .imports = &.{ + .{ .name = "thespian", .module = thespian_mod }, + .{ .name = "cbor", .module = cbor_mod }, + .{ .name = "shell", .module = shell_mod }, + }, + }); + const ripgrep_mod = b.createModule(.{ .root_source_file = b.path("src/ripgrep.zig"), .imports = &.{ @@ -486,6 +495,7 @@ pub fn build_exe( .{ .name = "Buffer", .module = Buffer_mod }, .{ .name = "keybind", .module = keybind_mod }, .{ .name = "shell", .module = shell_mod }, + .{ .name = "git", .module = git_mod }, .{ .name = "ripgrep", .module = ripgrep_mod }, .{ .name = "theme", .module = themes_dep.module("theme") }, .{ .name = "themes", .module = themes_dep.module("themes") }, diff --git a/src/git.zig b/src/git.zig new file mode 100644 index 0000000..1febeb3 --- /dev/null +++ b/src/git.zig @@ -0,0 +1,21 @@ +const std = @import("std"); +const tp = @import("thespian"); +const shell = @import("shell"); + +const git_binary = "git"; + +pub fn get_current_branch(allocator: std.mem.Allocator) !void { + const git_current_branch_cmd = tp.message.fmt(.{ git_binary, "rev-parse", "--abbrev-ref", "HEAD" }); + const handlers = struct { + fn out(_: usize, parent: tp.pid_ref, _: []const u8, output: []const u8) void { + var it = std.mem.splitScalar(u8, output, '\n'); + while (it.next()) |line| if (line.len > 0) + parent.send(.{ "git", "current_branch", line }) catch {}; + } + }; + try shell.execute(allocator, git_current_branch_cmd, .{ + .out = handlers.out, + .err = shell.log_err_handler, + .exit = shell.log_exit_err_handler, + }); +} diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 7d68dcb..113f1d0 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -2,6 +2,7 @@ const std = @import("std"); const tp = @import("thespian"); const cbor = @import("cbor"); const tracy = @import("tracy"); +const git = @import("git"); const ripgrep = @import("ripgrep"); const root = @import("root"); const location_history = @import("location_history"); @@ -827,6 +828,11 @@ const cmds = struct { } pub const find_in_files_query_meta: Meta = .{ .arguments = &.{.string} }; + pub fn git_branch(self: *Self, _: Ctx) Result { + try git.get_current_branch(self.allocator); + } + pub const git_branch_meta: Meta = .{ .description = "Get the current git branch" }; + pub fn shell_execute_log(self: *Self, ctx: Ctx) Result { if (!try ctx.args.match(.{ tp.string, tp.more })) return error.InvalidShellArgument;