diff --git a/build.zig b/build.zig index cdc1868..9265dc8 100644 --- a/build.zig +++ b/build.zig @@ -406,6 +406,11 @@ pub fn build_exe( }, }); + const VcsStatus_mod = b.createModule(.{ + .root_source_file = b.path("src/VcsStatus.zig"), + .imports = &.{}, + }); + const color_mod = b.createModule(.{ .root_source_file = b.path("src/color.zig"), }); @@ -575,6 +580,7 @@ pub fn build_exe( .{ .name = "dizzy", .module = dizzy_dep.module("dizzy") }, .{ .name = "fuzzig", .module = fuzzig_dep.module("fuzzig") }, .{ .name = "git", .module = git_mod }, + .{ .name = "VcsStatus", .module = VcsStatus_mod }, }, }); @@ -628,6 +634,7 @@ pub fn build_exe( .{ .name = "help.md", .module = help_mod }, .{ .name = "fuzzig", .module = fuzzig_dep.module("fuzzig") }, .{ .name = "zeit", .module = zeit_mod }, + .{ .name = "VcsStatus", .module = VcsStatus_mod }, }, }); diff --git a/src/Project.zig b/src/Project.zig index 5dbf46c..e71c915 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -8,6 +8,7 @@ const Buffer = @import("Buffer"); const fuzzig = @import("fuzzig"); const tracy = @import("tracy"); const git = @import("git"); +const VcsStatus = @import("VcsStatus"); const file_type_config = @import("file_type_config"); const builtin = @import("builtin"); @@ -30,7 +31,6 @@ logger_lsp: log.Logger, logger_git: log.Logger, workspace: ?[]const u8 = null, -branch: ?[]const u8 = null, walker: ?tp.pid = null, @@ -40,8 +40,12 @@ state: struct { workspace_path: State = .none, current_branch: State = .none, workspace_files: State = .none, + status: State = .none, } = .{}, +status: VcsStatus = .{}, +status_request: ?tp.pid = null, + const Self = @This(); const OutOfMemoryError = error{OutOfMemory}; @@ -91,7 +95,6 @@ pub fn init(allocator: std.mem.Allocator, name: []const u8) OutOfMemoryError!Sel pub fn deinit(self: *Self) void { if (self.walker) |pid| pid.send(.{"stop"}) catch {}; if (self.workspace) |p| self.allocator.free(p); - if (self.branch) |p| self.allocator.free(p); var i_ = self.file_language_server_name.iterator(); while (i_.next()) |p| { self.allocator.free(p.key_ptr.*); @@ -591,6 +594,25 @@ pub fn get_mru_position(self: *Self, from: tp.pid_ref, file_path: []const u8) Cl from.send(.{"none"}) catch return error.ClientFailed; } +pub fn request_vcs_status(self: *Self, from: tp.pid_ref) ClientError!void { + switch (self.state.status) { + .none => return error.ClientFailed, + .failed => return, + .running => { + if (self.status_request) |_| return; + self.status_request = from.clone(); + }, + .done => { + if (self.status_request) |_| return; + self.status_request = from.clone(); + self.state.status = .running; + git.status(@intFromPtr(self)) catch { + self.state.status = .failed; + }; + }, + } +} + pub fn request_tasks(self: *Self, from: tp.pid_ref) ClientError!void { var message: std.Io.Writer.Allocating = .init(self.allocator); defer message.deinit(); @@ -2054,6 +2076,10 @@ pub fn query_git(self: *Self) void { git.current_branch(@intFromPtr(self)) catch { self.state.current_branch = .failed; }; + self.state.status = .running; + git.status(@intFromPtr(self)) catch { + self.state.status = .failed; + }; } fn start_walker(self: *Self) void { @@ -2067,7 +2093,9 @@ fn start_walker(self: *Self) void { pub fn process_git(self: *Self, parent: tp.pid_ref, m: tp.message) (OutOfMemoryError || error{Exit})!void { var value: []const u8 = undefined; var path: []const u8 = undefined; - if (try m.match(.{ tp.any, tp.any, "workspace_path", tp.null_ })) { + if (try m.match(.{ tp.any, tp.any, "status", tp.more })) { + return self.process_status(m); + } else if (try m.match(.{ tp.any, tp.any, "workspace_path", tp.null_ })) { self.state.workspace_path = .done; self.start_walker(); try self.loaded(parent); @@ -2083,8 +2111,8 @@ pub fn process_git(self: *Self, parent: tp.pid_ref, m: tp.message) (OutOfMemoryE self.state.current_branch = .done; try self.loaded(parent); } else if (try m.match(.{ tp.any, tp.any, "current_branch", tp.extract(&value) })) { - if (self.branch) |p| self.allocator.free(p); - self.branch = try self.allocator.dupe(u8, value); + if (self.status.branch) |p| self.allocator.free(p); + self.status.branch = try self.allocator.dupe(u8, value); self.state.current_branch = .done; try self.loaded(parent); } else if (try m.match(.{ tp.any, tp.any, "workspace_files", tp.extract(&path) })) { @@ -2107,3 +2135,54 @@ pub fn process_git(self: *Self, parent: tp.pid_ref, m: tp.message) (OutOfMemoryE self.logger_git.err("git", tp.unexpected(m)); } } + +fn process_status(self: *Self, m: tp.message) (OutOfMemoryError || error{Exit})!void { + const any = cbor.any; + const extract = cbor.extract; + const null_ = cbor.null_; + + var value: []const u8 = undefined; + var ahead: []const u8 = undefined; + var behind: []const u8 = undefined; + + if (self.state.status == .done) + self.status.reset(self.allocator); + + if (try m.match(.{ any, any, "status", "#", "branch.oid", extract(&value) })) { + // commit | (initial) + } else if (try m.match(.{ any, any, "status", "#", "branch.head", extract(&value) })) { + if (self.status.branch) |p| self.allocator.free(p); + self.status.branch = try self.allocator.dupe(u8, value); + } else if (try m.match(.{ any, any, "status", "#", "branch.upstream", extract(&value) })) { + // upstream-branch + } else if (try m.match(.{ any, any, "status", "#", "branch.ab", extract(&ahead), extract(&behind) })) { + if (self.status.ahead) |p| self.allocator.free(p); + self.status.ahead = try self.allocator.dupe(u8, ahead); + if (self.status.behind) |p| self.allocator.free(p); + self.status.behind = try self.allocator.dupe(u8, behind); + } else if (try m.match(.{ any, any, "status", "#", "stash", extract(&value) })) { + if (self.status.stash) |p| self.allocator.free(p); + self.status.stash = try self.allocator.dupe(u8, value); + } else if (try m.match(.{ any, any, "status", "1", tp.more })) { + // ordinary file: + self.status.changed += 1; + } else if (try m.match(.{ any, any, "status", "2", tp.more })) { + // rename or copy: + self.status.changed += 1; + } else if (try m.match(.{ any, any, "status", "u", tp.more })) { + // unmerged file:

+ self.status.changed += 1; + } else if (try m.match(.{ any, any, "status", "?", tp.more })) { + // untracked file: + self.status.untracked += 1; + } else if (try m.match(.{ any, any, "status", "!", tp.more })) { + // ignored file: + } else if (try m.match(.{ any, any, "status", null_ })) { + self.state.status = .done; + if (self.status_request) |from| { + from.send(.{ "vcs_status", self.status }) catch {}; + from.deinit(); + self.status_request = null; + } + } +} diff --git a/src/VcsStatus.zig b/src/VcsStatus.zig new file mode 100644 index 0000000..0e5b35e --- /dev/null +++ b/src/VcsStatus.zig @@ -0,0 +1,21 @@ +branch: ?[]const u8 = null, +ahead: ?[]const u8 = null, +behind: ?[]const u8 = null, +stash: ?[]const u8 = null, +changed: usize = 0, +untracked: usize = 0, + +pub fn reset(self: *@This(), allocator: std.mem.Allocator) void { + if (self.branch) |p| allocator.free(p); + if (self.ahead) |p| allocator.free(p); + if (self.behind) |p| allocator.free(p); + if (self.stash) |p| allocator.free(p); + self.branch = null; + self.ahead = null; + self.behind = null; + self.stash = null; + self.changed = 0; + self.untracked = 0; +} + +const std = @import("std"); diff --git a/src/project_manager.zig b/src/project_manager.zig index f11cb36..46cd226 100644 --- a/src/project_manager.zig +++ b/src/project_manager.zig @@ -124,6 +124,13 @@ pub fn request_tasks(allocator: std.mem.Allocator) (ProjectError || CallError)!t return (try get()).pid.call(allocator, request_timeout, .{ "request_tasks", project }); } +pub fn request_vcs_status() (ProjectManagerError || ProjectError)!void { + const project = tp.env.get().str("project"); + if (project.len == 0) + return error.NoProject; + return send(.{ "request_vcs_status", project }); +} + pub fn add_task(task: []const u8) (ProjectManagerError || ProjectError)!void { const project = tp.env.get().str("project"); if (project.len == 0) @@ -371,6 +378,8 @@ const Process = struct { self.request_path_files(from, project_directory, max, path) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; } else if (try cbor.match(m.buf, .{ "request_tasks", tp.extract(&project_directory) })) { self.request_tasks(from, project_directory) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; + } else if (try cbor.match(m.buf, .{ "request_vcs_status", tp.extract(&project_directory) })) { + self.request_vcs_status(from, project_directory) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; } else if (try cbor.match(m.buf, .{ "add_task", tp.extract(&project_directory), tp.extract(&task) })) { self.add_task(project_directory, task) catch |e| return from.forward_error(e, @errorReturnTrace()) catch error.ClientFailed; } else if (try cbor.match(m.buf, .{ "delete_task", tp.extract(&project_directory), tp.extract(&task) })) { @@ -511,6 +520,11 @@ const Process = struct { try project.delete_task(task); } + fn request_vcs_status(self: *Process, from: tp.pid_ref, project_directory: []const u8) (ProjectError || Project.ClientError)!void { + const project = self.projects.get(project_directory) orelse return error.NoProject; + try project.request_vcs_status(from); + } + fn did_open(self: *Process, project_directory: []const u8, file_path: []const u8, file_type: []const u8, language_server: []const u8, version: usize, text: []const u8) (ProjectError || Project.StartLspError || CallError || cbor.Error)!void { const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".did_open" }); defer frame.deinit(); diff --git a/src/tui/status/branch.zig b/src/tui/status/branch.zig index b723997..140e090 100644 --- a/src/tui/status/branch.zig +++ b/src/tui/status/branch.zig @@ -4,7 +4,8 @@ const tp = @import("thespian"); const EventHandler = @import("EventHandler"); const Plane = @import("renderer").Plane; const command = @import("command"); -const git = @import("git"); +const project_manager = @import("project_manager"); +const VcsStatus = @import("VcsStatus"); const Widget = @import("../Widget.zig"); const Button = @import("../Button.zig"); @@ -19,14 +20,7 @@ const changed_symbol = "+"; const untracked_symbol = "?"; allocator: std.mem.Allocator, -workspace_path: ?[]const u8 = null, -branch: ?[]const u8 = null, -ahead: ?[]const u8 = null, -behind: ?[]const u8 = null, -stash: ?[]const u8 = null, -changed: usize = 0, -untracked: usize = 0, -done: bool = true, +status: VcsStatus = .{}, const Self = @This(); const ButtonType = Button.Options(Self).ButtonType; @@ -51,31 +45,29 @@ pub fn create( } pub fn ctx_init(self: *Self) error{OutOfMemory}!void { - try tui.message_filters().add(MessageFilter.bind(self, receive_git)); - git.workspace_path(0) catch {}; + try tui.message_filters().add(MessageFilter.bind(self, receive_filter)); + project_manager.request_vcs_status() catch {}; } pub fn ctx_deinit(self: *Self) void { tui.message_filters().remove_ptr(self); - if (self.branch) |p| self.allocator.free(p); - if (self.ahead) |p| self.allocator.free(p); - if (self.behind) |p| self.allocator.free(p); + self.status.reset(self.allocator); } fn on_click(self: *Self, _: *ButtonType, _: Widget.Pos) void { - self.refresh_git_status(); - command.executeName("show_git_status", .{}) catch {}; + self.refresh_vcs_status(); + command.executeName("show_vcs_status", .{}) catch {}; } -fn refresh_git_status(self: *Self) void { - if (self.workspace_path) |_| git.status(0) catch {}; +fn refresh_vcs_status(self: *Self) void { + if (self.status.branch) |_| project_manager.request_vcs_status() catch {}; } pub fn receive(self: *Self, _: *ButtonType, _: tp.pid_ref, m: tp.message) error{Exit}!bool { if (try m.match(.{ "E", tp.more })) return self.process_event(m); if (try m.match(.{ "PRJ", "open" })) - self.refresh_git_status(); + project_manager.request_vcs_status() catch {}; return false; } @@ -84,13 +76,13 @@ fn process_event(self: *Self, m: tp.message) error{Exit}!bool { try m.match(.{ tp.any, "save", tp.more }) or try m.match(.{ tp.any, "open", tp.more }) or try m.match(.{ tp.any, "close" })) - self.refresh_git_status(); + self.refresh_vcs_status(); return false; } -fn receive_git(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool { - return if (try match(m.buf, .{ "git", more })) - self.process_git(m) +fn receive_filter(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool { + return if (try match(m.buf, .{ "vcs_status", tp.more })) + self.process_vcs_status(m) else if (try match(m.buf, .{"focus_in"})) self.process_focus_in() else @@ -98,100 +90,41 @@ fn receive_git(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bo } fn process_focus_in(self: *Self) MessageFilter.Error!bool { - self.refresh_git_status(); + self.refresh_vcs_status(); return false; } -fn process_git(self: *Self, m: tp.message) MessageFilter.Error!bool { - var value: []const u8 = undefined; - if (try match(m.buf, .{ any, any, "workspace_path", null_ })) { - // do nothing, we do not have a git workspace - } else if (try match(m.buf, .{ any, any, "workspace_path", extract(&value) })) { - if (self.workspace_path) |p| self.allocator.free(p); - self.workspace_path = try self.allocator.dupe(u8, value); - // git.current_branch(0) catch {}; - git.status(0) catch {}; - } else if (try match(m.buf, .{ any, any, "current_branch", extract(&value) })) { - if (self.branch) |p| self.allocator.free(p); - self.branch = try self.allocator.dupe(u8, value); - } else if (try match(m.buf, .{ any, any, "status", tp.more })) { - return self.process_status(m); - } else { - return false; - } - return true; -} - -fn process_status(self: *Self, m: tp.message) MessageFilter.Error!bool { +fn process_vcs_status(self: *Self, m: tp.message) MessageFilter.Error!bool { defer if (tui.frames_rendered() > 0) Widget.need_render(); - var value: []const u8 = undefined; - var ahead: []const u8 = undefined; - var behind: []const u8 = undefined; + var status: VcsStatus = .{}; + self.status.reset(self.allocator); + if (!try match(m.buf, .{ any, extract(&status) })) return true; - if (self.done) { - self.done = false; - self.changed = 0; - self.untracked = 0; - if (self.ahead) |p| self.allocator.free(p); - self.ahead = null; - if (self.behind) |p| self.allocator.free(p); - self.behind = null; - if (self.stash) |p| self.allocator.free(p); - self.stash = null; - } + if (status.branch) |branch| self.status.branch = try self.allocator.dupe(u8, branch); + if (status.ahead) |ahead| self.status.ahead = try self.allocator.dupe(u8, ahead); + if (status.behind) |behind| self.status.behind = try self.allocator.dupe(u8, behind); + if (status.stash) |stash| self.status.stash = try self.allocator.dupe(u8, stash); - if (try match(m.buf, .{ any, any, "status", "#", "branch.oid", extract(&value) })) { - // commit | (initial) - } else if (try match(m.buf, .{ any, any, "status", "#", "branch.head", extract(&value) })) { - if (self.branch) |p| self.allocator.free(p); - self.branch = try self.allocator.dupe(u8, value); - } else if (try match(m.buf, .{ any, any, "status", "#", "branch.upstream", extract(&value) })) { - // upstream-branch - } else if (try match(m.buf, .{ any, any, "status", "#", "branch.ab", extract(&ahead), extract(&behind) })) { - if (self.ahead) |p| self.allocator.free(p); - self.ahead = try self.allocator.dupe(u8, ahead); - if (self.behind) |p| self.allocator.free(p); - self.behind = try self.allocator.dupe(u8, behind); - } else if (try match(m.buf, .{ any, any, "status", "#", "stash", extract(&value) })) { - if (self.stash) |p| self.allocator.free(p); - self.stash = try self.allocator.dupe(u8, value); - } else if (try match(m.buf, .{ any, any, "status", "1", tp.more })) { - // ordinary file: - self.changed += 1; - } else if (try match(m.buf, .{ any, any, "status", "2", tp.more })) { - // rename or copy: - self.changed += 1; - } else if (try match(m.buf, .{ any, any, "status", "u", tp.more })) { - // unmerged file:

- self.changed += 1; - } else if (try match(m.buf, .{ any, any, "status", "?", tp.more })) { - // untracked file: - self.untracked += 1; - } else if (try match(m.buf, .{ any, any, "status", "!", tp.more })) { - // ignored file: - } else if (try match(m.buf, .{ any, any, "status", null_ })) { - self.done = true; - } else return false; return true; } fn format(self: *Self, buf: []u8) []const u8 { - const branch = self.branch orelse return ""; + const branch = self.status.branch orelse return ""; var fbs = std.io.fixedBufferStream(buf); const writer = fbs.writer(); writer.print(" {s}{s}", .{ branch_symbol, branch }) catch {}; - if (self.ahead) |ahead| if (ahead.len > 1 and ahead[1] != '0') + if (self.status.ahead) |ahead| if (ahead.len > 1 and ahead[1] != '0') writer.print(" {s}{s}", .{ ahead_symbol, ahead[1..] }) catch {}; - if (self.behind) |behind| if (behind.len > 1 and behind[1] != '0') + if (self.status.behind) |behind| if (behind.len > 1 and behind[1] != '0') writer.print(" {s}{s}", .{ behind_symbol, behind[1..] }) catch {}; - if (self.stash) |stash| if (stash.len > 0 and stash[0] != '0') + if (self.status.stash) |stash| if (stash.len > 0 and stash[0] != '0') writer.print(" {s}{s}", .{ stash_symbol, stash }) catch {}; - if (self.changed > 0) - writer.print(" {s}{d}", .{ changed_symbol, self.changed }) catch {}; - if (self.untracked > 0) - writer.print(" {s}{d}", .{ untracked_symbol, self.untracked }) catch {}; + if (self.status.changed > 0) + writer.print(" {s}{d}", .{ changed_symbol, self.status.changed }) catch {}; + if (self.status.untracked > 0) + writer.print(" {s}{d}", .{ untracked_symbol, self.status.untracked }) catch {}; writer.print(" ", .{}) catch {}; return fbs.getWritten(); }