| ... | ... | @@ -4,6 +4,7 @@ const top = @This(); |
| 4 | 4 | const time = @import("time"); |
| 5 | 5 | const extras = @import("extras"); |
| 6 | 6 | const tracer = @import("tracer"); |
| 7 | const nfs = @import("nfs"); |
| 7 | 8 | |
| 8 | 9 | // 40 is length of sha1 hash |
| 9 | 10 | pub const Id = *const [40]u8; |
| ... | ... | @@ -55,7 +56,7 @@ pub fn version(alloc: std.mem.Allocator) !string { |
| 55 | 56 | /// Returns the result of running `git rev-parse HEAD` |
| 56 | 57 | /// dir must already be pointing at the .git folder |
| 57 | 58 | // TODO this doesnt handle when there are 0 commits |
| 58 | | pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !?CommitId { |
| 59 | pub fn getHEAD(alloc: std.mem.Allocator, dir: nfs.Dir) !?CommitId { |
| 59 | 60 | const t = tracer.trace(@src(), "", .{}); |
| 60 | 61 | defer t.end(); |
| 61 | 62 | |
| ... | ... | @@ -63,7 +64,10 @@ pub fn getHEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !?CommitId { |
| 63 | 64 | |
| 64 | 65 | if (std.mem.startsWith(u8, h, "ref:")) { |
| 65 | 66 | blk: { |
| 66 | | const reffile = dir.readFileAlloc(alloc, h[5..], 1024) catch |err| switch (err) { |
| 67 | var buf: [std.fs.max_path_bytes]u8 = undefined; |
| 68 | @memcpy((&buf).ptr, h[5..]); |
| 69 | buf[h[5..].len] = 0; |
| 70 | const reffile = dir.readFileAlloc(alloc, buf[0..h[5..].len :0], 1024) catch |err| switch (err) { |
| 67 | 71 | error.FileNotFound => break :blk, |
| 68 | 72 | else => |e| return e, |
| 69 | 73 | }; |
| ... | ... | @@ -101,13 +105,13 @@ fn ensureObjId(comptime T: type, input: string) T { |
| 101 | 105 | // TODO make this return a Reader when we implement it ourselves |
| 102 | 106 | // https://git-scm.com/book/en/v2/Git-Internals-Git-Objects |
| 103 | 107 | // https://git-scm.com/book/en/v2/Git-Internals-Packfiles |
| 104 | | pub fn getObject(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: Id) !string { |
| 108 | pub fn getObject(alloc: std.mem.Allocator, dir: nfs.Dir, obj: Id) !string { |
| 105 | 109 | const t = tracer.trace(@src(), " {s}", .{obj}); |
| 106 | 110 | defer t.end(); |
| 107 | 111 | |
| 108 | 112 | const result = try std.process.Child.run(.{ |
| 109 | 113 | .allocator = alloc, |
| 110 | | .cwd_dir = dir, |
| 114 | .cwd_dir = dir.to_std(), |
| 111 | 115 | .argv = &.{ "git", "cat-file", "-p", obj }, |
| 112 | 116 | .max_output_bytes = 1024 * 1024 * 1024, |
| 113 | 117 | }); |
| ... | ... | @@ -118,13 +122,13 @@ pub fn getObject(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: Id) !string { |
| 118 | 122 | // TODO make this inspect .git/objects manually |
| 119 | 123 | // https://git-scm.com/book/en/v2/Git-Internals-Git-Objects |
| 120 | 124 | // https://git-scm.com/book/en/v2/Git-Internals-Packfiles |
| 121 | | pub fn getObjectSize(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: Id) !u64 { |
| 125 | pub fn getObjectSize(alloc: std.mem.Allocator, dir: nfs.Dir, obj: Id) !u64 { |
| 122 | 126 | const t = tracer.trace(@src(), " {s}", .{obj}); |
| 123 | 127 | defer t.end(); |
| 124 | 128 | |
| 125 | 129 | const result = try std.process.Child.run(.{ |
| 126 | 130 | .allocator = alloc, |
| 127 | | .cwd_dir = dir, |
| 131 | .cwd_dir = dir.to_std(), |
| 128 | 132 | .argv = &.{ "git", "cat-file", "-s", obj }, |
| 129 | 133 | }); |
| 130 | 134 | extras.assertLog(result.term == .Exited and result.term.Exited == 0, "{s}", .{result.stderr}); |
| ... | ... | @@ -134,13 +138,13 @@ pub fn getObjectSize(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: Id) !u64 { |
| 134 | 138 | // TODO make this inspect .git manually |
| 135 | 139 | // https://git-scm.com/book/en/v2/Git-Internals-Git-Objects |
| 136 | 140 | // https://git-scm.com/book/en/v2/Git-Internals-Packfiles |
| 137 | | pub fn isType(alloc: std.mem.Allocator, dir: std.fs.Dir, maybeobj: Id, typ: Tree.Object.Id.Tag) !bool { |
| 141 | pub fn isType(alloc: std.mem.Allocator, dir: nfs.Dir, maybeobj: Id, typ: Tree.Object.Id.Tag) !bool { |
| 138 | 142 | const t = tracer.trace(@src(), " {s} = {s} ?", .{ maybeobj, @tagName(typ) }); |
| 139 | 143 | defer t.end(); |
| 140 | 144 | |
| 141 | 145 | const result = try std.process.Child.run(.{ |
| 142 | 146 | .allocator = alloc, |
| 143 | | .cwd_dir = dir, |
| 147 | .cwd_dir = dir.to_std(), |
| 144 | 148 | .argv = &.{ "git", "cat-file", "-t", maybeobj }, |
| 145 | 149 | }); |
| 146 | 150 | std.debug.assert(result.term == .Exited and result.term.Exited == 0); |
| ... | ... | @@ -151,13 +155,13 @@ pub fn isType(alloc: std.mem.Allocator, dir: std.fs.Dir, maybeobj: Id, typ: Tree |
| 151 | 155 | // TODO make this inspect .git manually |
| 152 | 156 | // https://git-scm.com/book/en/v2/Git-Internals-Git-Objects |
| 153 | 157 | // https://git-scm.com/book/en/v2/Git-Internals-Packfiles |
| 154 | | pub fn getType(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: Id) !Tree.Object.Id.Tag { |
| 158 | pub fn getType(alloc: std.mem.Allocator, dir: nfs.Dir, obj: Id) !Tree.Object.Id.Tag { |
| 155 | 159 | const t = tracer.trace(@src(), " {s}", .{obj}); |
| 156 | 160 | defer t.end(); |
| 157 | 161 | |
| 158 | 162 | const result = try std.process.Child.run(.{ |
| 159 | 163 | .allocator = alloc, |
| 160 | | .cwd_dir = dir, |
| 164 | .cwd_dir = dir.to_std(), |
| 161 | 165 | .argv = &.{ "git", "cat-file", "-t", obj }, |
| 162 | 166 | }); |
| 163 | 167 | std.debug.assert(result.term == .Exited and result.term.Exited == 0); |
| ... | ... | @@ -169,13 +173,13 @@ pub fn getType(alloc: std.mem.Allocator, dir: std.fs.Dir, obj: Id) !Tree.Object. |
| 169 | 173 | // TODO make a version of this that accepts an array of sub_paths and searches all of them at once, so as to not lose spot in history when searching for many old paths |
| 170 | 174 | // https://git-scm.com/book/en/v2/Git-Internals-Git-Objects |
| 171 | 175 | // https://git-scm.com/book/en/v2/Git-Internals-Packfiles |
| 172 | | pub fn revList(alloc: std.mem.Allocator, dir: std.fs.Dir, comptime count: u31, from: CommitId, sub_path: string) !string { |
| 176 | pub fn revList(alloc: std.mem.Allocator, dir: nfs.Dir, comptime count: u31, from: CommitId, sub_path: string) !string { |
| 173 | 177 | const t = tracer.trace(@src(), "({d}) {s} -- {s}", .{ count, from.id, sub_path }); |
| 174 | 178 | defer t.end(); |
| 175 | 179 | |
| 176 | 180 | const result = try std.process.Child.run(.{ |
| 177 | 181 | .allocator = alloc, |
| 178 | | .cwd_dir = dir, |
| 182 | .cwd_dir = dir.to_std(), |
| 179 | 183 | .argv = &.{ |
| 180 | 184 | "git", |
| 181 | 185 | "rev-list", |
| ... | ... | @@ -412,14 +416,14 @@ pub const Tree = struct { |
| 412 | 416 | |
| 413 | 417 | // TODO make this inspect .git manually |
| 414 | 418 | // TODO make this return a Reader when we implement it ourselves |
| 415 | | pub fn getTreeDiff(alloc: std.mem.Allocator, dir: std.fs.Dir, commitid: CommitId, parentid: ?CommitId) !string { |
| 419 | pub fn getTreeDiff(alloc: std.mem.Allocator, dir: nfs.Dir, commitid: CommitId, parentid: ?CommitId) !string { |
| 416 | 420 | const t = tracer.trace(@src(), "", .{}); |
| 417 | 421 | defer t.end(); |
| 418 | 422 | |
| 419 | 423 | if (parentid == null) { |
| 420 | 424 | const result = try std.process.Child.run(.{ |
| 421 | 425 | .allocator = alloc, |
| 422 | | .cwd_dir = dir, |
| 426 | .cwd_dir = dir.to_std(), |
| 423 | 427 | // 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is a hardcode for the empty tree in git sha1 |
| 424 | 428 | // result of `printf | git hash-object -t tree --stdin` |
| 425 | 429 | .argv = &.{ "git", "diff-tree", "-p", "--raw", "4b825dc642cb6eb9a060e54bf8d69288fbee4904", commitid.id }, |
| ... | ... | @@ -430,7 +434,7 @@ pub fn getTreeDiff(alloc: std.mem.Allocator, dir: std.fs.Dir, commitid: CommitId |
| 430 | 434 | } |
| 431 | 435 | const result = try std.process.Child.run(.{ |
| 432 | 436 | .allocator = alloc, |
| 433 | | .cwd_dir = dir, |
| 437 | .cwd_dir = dir.to_std(), |
| 434 | 438 | .argv = &.{ "git", "diff-tree", "-p", "--raw", parentid.?.id, commitid.id }, |
| 435 | 439 | .max_output_bytes = 1024 * 1024 * 1024, |
| 436 | 440 | }); |
| ... | ... | @@ -681,13 +685,13 @@ pub const TreeDiff = struct { |
| 681 | 685 | }; |
| 682 | 686 | }; |
| 683 | 687 | |
| 684 | | pub fn getBranches(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const Ref { |
| 688 | pub fn getBranches(alloc: std.mem.Allocator, dir: nfs.Dir) ![]const Ref { |
| 685 | 689 | const t = tracer.trace(@src(), "", .{}); |
| 686 | 690 | defer t.end(); |
| 687 | 691 | |
| 688 | 692 | const result = try std.process.Child.run(.{ |
| 689 | 693 | .allocator = alloc, |
| 690 | | .cwd_dir = dir, |
| 694 | .cwd_dir = dir.to_std(), |
| 691 | 695 | .argv = &.{ "git", "show-ref", "--heads" }, |
| 692 | 696 | .max_output_bytes = 1024 * 1024 * 1024, |
| 693 | 697 | }); |
| ... | ... | @@ -707,7 +711,7 @@ pub fn getBranches(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const Ref { |
| 707 | 711 | return list.toOwnedSlice(); |
| 708 | 712 | } |
| 709 | 713 | |
| 710 | | pub fn getTags(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const Ref { |
| 714 | pub fn getTags(alloc: std.mem.Allocator, dir: nfs.Dir) ![]const Ref { |
| 711 | 715 | const t = tracer.trace(@src(), "", .{}); |
| 712 | 716 | defer t.end(); |
| 713 | 717 | |
| ... | ... | @@ -717,7 +721,7 @@ pub fn getTags(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const Ref { |
| 717 | 721 | // 7dfd3948a9095f0253bfba60fed52895ffbf84bb refs/tags/15.3.2 |
| 718 | 722 | const result = try std.process.Child.run(.{ |
| 719 | 723 | .allocator = alloc, |
| 720 | | .cwd_dir = dir, |
| 724 | .cwd_dir = dir.to_std(), |
| 721 | 725 | .argv = &.{ "git", "show-ref", "--tags", "--dereference" }, |
| 722 | 726 | .max_output_bytes = 1024 * 1024 * 1024, |
| 723 | 727 | }); |