fix improper typing of path which caused crashes on .git file handling

This commit is contained in:
Squibid 2026-02-25 22:20:40 -05:00
parent eb0f31daa7
commit 6b7a176a70
Signed by: squibid
GPG key ID: BECE5684D3C4005D

View file

@ -10,7 +10,7 @@ const std = @import("std");
const util = @import("util.zig");
repo: ?*c.git_repository,
git_path: []const u8,
git_path: [:0]const u8,
repo_path: []const u8,
allocator: std.mem.Allocator,
repo_state: c_int,
@ -43,7 +43,7 @@ pub fn init(allocator: std.mem.Allocator) !?*Git {
.allocator = allocator,
.repo_state = undefined,
.repo_path = undefined,
.cache = .init(self.allocator, self.git_path),
.cache = .init(self.allocator, std.mem.span(self.git_path.ptr)),
};
if (c.git_repository_open(&self.repo, repo_root.?.ptr) != 0) {
@ -238,7 +238,7 @@ pub fn headIsDetached(self: *Git) bool {
return c.git_repository_head_detached(self.repo) == 1;
}
fn findRepoRoot(allocator: std.mem.Allocator) !?[]u8 {
fn findRepoRoot(allocator: std.mem.Allocator) !?[:0]u8 {
const s = perf.s();
defer perf.e(s, @src());
@ -260,20 +260,18 @@ fn findRepoRoot(allocator: std.mem.Allocator) !?[]u8 {
};
return switch (stat.kind) {
.directory => try allocator.dupe(u8, try cur_path.realpathZ(
.directory => try allocator.dupeZ(u8, try cur_path.realpath(
".git",
&buf,
)),
.file => {
@branchHint(.cold);
const f = try cur_path.openFile(".git", .{ .mode = .read_only });
const file_reader = f.reader(&buf);
var reader = file_reader.interface;
const read = try reader.readSliceShort(&buf);
const prefix = "gitdir: ";
if (std.mem.startsWith(u8, &buf, prefix)) {
buf[read - 1] = 0; // remove the newline
return try allocator.dupe(u8, buf[prefix.len..]);
var file_buf: [std.fs.max_path_bytes + prefix.len]u8 = undefined;
const read = try cur_path.readFile(".git", &file_buf);
if (read.len > 0 and std.mem.eql(u8, file_buf[0..prefix.len], prefix)) {
return try allocator.dupeZ(u8, read[prefix.len..read.len - 1]);
}
return null;
},