about summary refs log tree commit diff
path: root/src/fs.zig
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2025-10-17 22:51:16 +0200
committerRobert Günzler <r@gnzler.io>2025-10-18 00:01:46 +0200
commit973e5fdb82e0037ba5fc2ff74cf40d32a0e3466b (patch)
tree62ec670e836b1e69081c9889fffab4b551de6528 /src/fs.zig
parent55efe8b67b850b34c1483ab3be0947d80508a893 (diff)
downloadsite-973e5fdb82e0037ba5fc2ff74cf40d32a0e3466b.tar.gz
get mtime from git
but fallback to mtime anyway

Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to 'src/fs.zig')
-rw-r--r--src/fs.zig29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/fs.zig b/src/fs.zig
index 53acc07..045db89 100644
--- a/src/fs.zig
+++ b/src/fs.zig
@@ -1,7 +1,9 @@
 const std = @import("std");
 const c = @cImport({
+    @cInclude("stdlib.h");
     @cInclude("time.h");
 });
+const exe = @import("exe.zig");
 
 const log = std.log.scoped(.fs);
 
@@ -141,6 +143,29 @@ pub fn joinPathAndFilename(alloc: std.mem.Allocator, paths: []const []const u8,
     return try std.fs.path.join(alloc, paths2);
 }
 
+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);
+
+    // 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 });
     defer fd.close();
@@ -155,10 +180,10 @@ pub fn getMtime(path: []const u8) !std.posix.timespec {
 }
 
 // use libc strftime to format time string
-pub fn formatMtime(alloc: std.mem.Allocator, mtime: std.posix.timespec, fmt: [*c]const u8) ![]const u8 {
+pub fn formatTimespec(alloc: std.mem.Allocator, timespec: std.posix.timespec, fmt: [*c]const u8) ![]const u8 {
     const mtime_str = try alloc.alloc(u8, std.os.linux.PATH_MAX);
     var tm: c.struct_tm = undefined;
-    _ = c.localtime_r(&mtime.sec, &tm);
+    _ = c.localtime_r(&timespec.sec, &tm);
     const rc = c.strftime(
         mtime_str.ptr,
         mtime_str.len,