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