diff options
| author | Robert Günzler <r@gnzler.io> | 2026-01-29 19:16:23 +0100 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2026-01-29 21:05:54 +0100 |
| commit | 8f8ee8697131ea149568a02b77f23dc8dedae99a (patch) | |
| tree | 7fad212b20fc623905c74180c292d3a9512a9aa2 | |
| parent | 5ba0af482fe5bef57f99b1f36b7934d672859aaa (diff) | |
| download | site-8f8ee8697131ea149568a02b77f23dc8dedae99a.tar.gz | |
consolidate time access behind getLastEdit
and drop sourcing times from git for now... Signed-off-by: Robert Günzler <r@gnzler.io>
| -rw-r--r-- | src/exe.zig | 2 | ||||
| -rw-r--r-- | src/fs.zig | 59 | ||||
| -rw-r--r-- | src/gen.zig | 2 |
3 files changed, 37 insertions, 26 deletions
diff --git a/src/exe.zig b/src/exe.zig index c1f084b..6aa260d 100644 --- a/src/exe.zig +++ b/src/exe.zig @@ -75,7 +75,7 @@ fn pandocWorker(ctx: *Context, page: []const u8) !void { defer ctx.allocator().free(target_path); // get mtime of the source document - const mtime = try (fs.getGitTime(source_path) catch fs.getMtime(source_path)); + const mtime = try fs.getLastEdit(source_path); const mtime_str = try fs.formatTimespec(ctx.allocator(), mtime, "%s"); defer ctx.allocator().free(mtime_str); diff --git a/src/fs.zig b/src/fs.zig index 64c4b82..d14d7b0 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -142,30 +142,30 @@ pub fn joinPathAndFilename(alloc: std.mem.Allocator, paths: []const []const u8, return try std.fs.path.join(alloc, p.items); } -pub fn getGitTime(path: []const u8) !std.posix.timespec { - var buf = std.mem.zeroes([12]u8); - - var p = std.process.Child.init( - &.{ "git", "log", "-1", "--pretty=format:%at", "-z", "--", path }, - std.heap.page_allocator, - ); - p.stdout_behavior = .Pipe; - try p.spawn(); - _ = try p.stdout.?.readAll(&buf); - const result = try p.wait(); - - std.debug.assert(result == .Exited and result.Exited == 0); - - // parse result as ulong - const gitAuthorDate: c_ulong = c.strtoul(&buf, null, 10); - if (gitAuthorDate == 0) return error.NoGitTime; - - // calculate timespec - return .{ - .sec = std.math.cast(isize, gitAuthorDate) orelse std.math.maxInt(isize), - .nsec = 0, - }; -} +// pub fn getGitTime(path: []const u8) !std.posix.timespec { +// var buf = std.mem.zeroes([12]u8); +// +// var p = std.process.Child.init( +// &.{ "git", "log", "-1", "--pretty=format:%at", "-z", "--", path }, +// std.heap.page_allocator, +// ); +// p.stdout_behavior = .Pipe; +// try p.spawn(); +// _ = try p.stdout.?.readAll(&buf); +// const result = try p.wait(); +// +// std.debug.assert(result == .Exited and result.Exited == 0); +// +// // parse result as ulong +// const gitAuthorDate: c_ulong = c.strtoul(&buf, null, 10); +// if (gitAuthorDate == 0) return error.NoGitTime; +// +// // calculate timespec +// return .{ +// .sec = std.math.cast(isize, gitAuthorDate) orelse std.math.maxInt(isize), +// .nsec = 0, +// }; +// } pub fn getMtime(path: []const u8) !std.posix.timespec { const fd = try std.fs.cwd().openFile(path, .{ .mode = .read_only }); @@ -180,6 +180,17 @@ pub fn getMtime(path: []const u8) !std.posix.timespec { }; } +pub fn getLastEdit(path: []const u8) !std.posix.timespec { + std.fs.cwd().access(path, .{}) catch |e| switch (e) { + error.FileNotFound => return .{ + .sec = 0, + .nsec = 0, + }, + else => return e, + }; + return getMtime(path); +} + pub fn setMtime(path: []const u8, ts: std.posix.timespec) !void { const fd = try std.fs.cwd().openFile(path, .{ .mode = .write_only }); defer fd.close(); diff --git a/src/gen.zig b/src/gen.zig index 8db8b3f..aec72ca 100644 --- a/src/gen.zig +++ b/src/gen.zig @@ -157,7 +157,7 @@ fn genSite(ctx: *Context) !void { // build a single html file fn build(allocator: std.mem.Allocator, w: *std.Io.Writer, page_name: []const u8, srcpath: []const u8) !void { - const mtime = try fs.getMtime(srcpath); + const mtime = try fs.getLastEdit(srcpath); const mtime_str = try fs.formatTimespec(allocator, mtime, "%F"); defer allocator.free(mtime_str); |