diff options
Diffstat (limited to 'src/fs.zig')
| -rw-r--r-- | src/fs.zig | 29 |
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(×pec.sec, &tm); const rc = c.strftime( mtime_str.ptr, mtime_str.len, |