wyag

Write yourself a git
Log | Files | Refs | README

commit 033414702664ab80f973b9304a6329fc6efe9d40
parent bde0623549f145c4227232a82a60c0db3032d296
Author: Martin Ashby <martin@ashbysoft.com>
Date:   Wed, 14 Aug 2024 23:13:23 +0100

Tidy up functions a bit

remove info logging, it's unnecessary

Diffstat:
Msrc/root.zig | 36++++++++++++++++++++----------------
1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/src/root.zig b/src/root.zig @@ -48,27 +48,14 @@ pub fn doMain() !void { } } else if (cat_file.wasExecuted) { if (cat_file_ref.value) |ref| { - var repo = try repo_find(a, std.fs.cwd()); - defer repo.deinit(); - const kind = try repo.read_object(a, ref, std.io.getStdOut().writer().any()); - std.log.info("Object kind: {s}", .{@tagName(kind)}); + try catFile(a, std.fs.cwd(), ref, std.io.getStdOut().writer().any()); } else { std.log.err("No ref provided to cat-file", .{}); return error.InvalidArgs; } } else if (hash_object.wasExecuted) { if (hash_object_input.value) |input| { - var repo = try repo_find(a, std.fs.cwd()); - defer repo.deinit(); - const cwd = std.fs.cwd(); - const stat = try cwd.statFile(input); - var infile = try std.fs.cwd().openFile(input, .{}); - const rdr = infile.reader(); - const sha = try repo.write_object(a, stat.size, rdr, .blob, hash_object_write.waspresent); - defer a.free(sha); - const stdout = std.io.getStdOut().writer(); - try stdout.writeAll(sha); - try stdout.writeByte('\n'); + try hashObject(a, std.fs.cwd(), input, hash_object_write.waspresent, std.io.getStdOut().writer().any()); } else { std.log.err("No file supplied to hash-object", .{}); return error.InvalidArgs; @@ -83,6 +70,24 @@ pub fn doMain() !void { } } +fn catFile(a: std.mem.Allocator, cwd: std.fs.Dir, ref: []const u8, writer: std.io.AnyWriter) !void { + var repo = try repo_find(a, cwd); + defer repo.deinit(); + _ = try repo.read_object(a, ref, writer); +} + +fn hashObject(a: std.mem.Allocator, cwd: std.fs.Dir, file: []const u8, write: bool, writer: std.io.AnyWriter) !void { + var repo = try repo_find(a, cwd); + defer repo.deinit(); + const stat = try cwd.statFile(file); + var infile = try std.fs.cwd().openFile(file, .{}); + const rdr = infile.reader(); + const sha = try repo.write_object(a, stat.size, rdr, .blob, write); + defer a.free(sha); + try writer.writeAll(sha); + try writer.writeByte('\n'); +} + pub const GitRepository = struct { worktree: Dir, gitdir: Dir, @@ -195,7 +200,6 @@ pub const GitRepository = struct { // now rename it into place once you have the hash if (write) { const path = try std.fs.path.join(a, &.{ "objects", ref[0..2], ref[2..] }); - std.log.info("renaming {s} to {s}", .{ tmpfilepath, path }); try self.gitdir.rename(tmpfilepath, path); } return try ca.dupe(u8, ref);