| ... | ... | @@ -1,18 +1,185 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const git = @import("git"); |
| 3 | const extras = @import("extras"); |
| 4 | const expect = @import("expect").expect; |
| 3 | 5 | |
| 4 | 6 | test { |
| 5 | | _ = &git.getBranches; |
| 7 | _ = &git.version; |
| 6 | 8 | _ = &git.getHEAD; |
| 7 | | _ = &git.getObject; |
| 8 | | _ = &git.getObjectSize; |
| 9 | 9 | _ = &git.getTags; |
| 10 | | _ = &git.getTreeDiff; |
| 11 | | _ = &git.getType; |
| 12 | | _ = &git.isType; |
| 13 | | _ = &git.parseCommit; |
| 14 | | _ = &git.parseTreeDiff; |
| 15 | | _ = &git.parseTreeDiffMeta; |
| 16 | 10 | _ = &git.revList; |
| 17 | | _ = &git.version; |
| 11 | } |
| 12 | |
| 13 | test { |
| 14 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 15 | defer arena.deinit(); |
| 16 | const alloc = arena.allocator(); |
| 17 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 18 | defer git_dir.close(); |
| 19 | const branch_refs = try git.getBranches(alloc, git_dir); |
| 20 | const branch_names = try extras.mapBy(alloc, branch_refs, .label); |
| 21 | try expect(branch_names).toEqualStringSlice(&.{"master"}); |
| 22 | } |
| 23 | |
| 24 | test { |
| 25 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 26 | defer arena.deinit(); |
| 27 | const alloc = arena.allocator(); |
| 28 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 29 | defer git_dir.close(); |
| 30 | try expect(try git.getObject(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).toEqualString( |
| 31 | \\tree 5403fecad0fde9120535321f222a061abc2849d9 |
| 32 | \\parent c39f57f6bb01664a7146ddbfc3debe76ec135f44 |
| 33 | \\author Meghan Denny <hello@nektro.net> 1692246864 -0700 |
| 34 | \\committer Meghan Denny <hello@nektro.net> 1692246864 -0700 |
| 35 | \\ |
| 36 | \\update to Zig 0.11 |
| 37 | \\ |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | test { |
| 42 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 43 | defer arena.deinit(); |
| 44 | const alloc = arena.allocator(); |
| 45 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 46 | defer git_dir.close(); |
| 47 | try expect(try git.getObjectSize(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).toEqual(229); |
| 48 | try expect(try git.getObjectSize(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).toEqual(45 + 47 + 55 + 58 + 0 + 18 + 6); |
| 49 | } |
| 50 | |
| 51 | test { |
| 52 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 53 | defer arena.deinit(); |
| 54 | const alloc = arena.allocator(); |
| 55 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 56 | defer git_dir.close(); |
| 57 | try expect(try git.isType(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e", .blob)).toEqual(false); |
| 58 | try expect(try git.isType(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e", .tree)).toEqual(false); |
| 59 | try expect(try git.isType(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e", .commit)).toEqual(true); |
| 60 | try expect(try git.isType(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e", .tag)).toEqual(false); |
| 61 | } |
| 62 | |
| 63 | test { |
| 64 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 65 | defer arena.deinit(); |
| 66 | const alloc = arena.allocator(); |
| 67 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 68 | defer git_dir.close(); |
| 69 | try expect(try git.getType(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).toEqual(.commit); |
| 70 | } |
| 71 | |
| 72 | test { |
| 73 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 74 | defer arena.deinit(); |
| 75 | const alloc = arena.allocator(); |
| 76 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 77 | defer git_dir.close(); |
| 78 | const c = try git.parseCommit(alloc, try git.getObject(alloc, git_dir, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")); |
| 79 | try expect(c.tree.id).toEqualString("5403fecad0fde9120535321f222a061abc2849d9"); |
| 80 | try expect(c.parents.len).toEqual(1); |
| 81 | try expect(c.parents[0].id).toEqualString("c39f57f6bb01664a7146ddbfc3debe76ec135f44"); |
| 82 | try expect(c.author.name).toEqualString("Meghan Denny"); |
| 83 | try expect(c.author.email).toEqualString("hello@nektro.net"); |
| 84 | // TODO: test .at when we upgrade to 0.14 and have decl literals |
| 85 | try expect(c.committer.name).toEqualString("Meghan Denny"); |
| 86 | try expect(c.committer.email).toEqualString("hello@nektro.net"); |
| 87 | // TODO: test .at when we upgrade to 0.14 and have decl literals |
| 88 | try expect(c.message).toEqualString( |
| 89 | \\update to Zig 0.11 |
| 90 | \\ |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | test { |
| 95 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 96 | defer arena.deinit(); |
| 97 | const alloc = arena.allocator(); |
| 98 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 99 | defer git_dir.close(); |
| 100 | try expect(try git.getObject(alloc, git_dir, "5403fecad0fde9120535321f222a061abc2849d9")).toEqualString( |
| 101 | \\100644 blob 8e8d2ceba4b327ce9db93f988492d0e21a461012	.gitattributes |
| 102 | \\100644 blob bb2a57bd81d13975f2a74ae5dd0e652de07bb8a7	.gitignore |
| 103 | \\100644 blob 37be0c1cfa4f097edbb1fb5c0585cd18cb08df13	LICENSE |
| 104 | \\100644 blob b229eadbd5d6655c2dfbaca5a5f68f2f8f3c5454	git.zig |
| 105 | \\100644 blob bb3f1c135632cfca760bd84fb18acdab8dae8ec3	zig.mod |
| 106 | \\ |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | test { |
| 111 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 112 | defer arena.deinit(); |
| 113 | const alloc = arena.allocator(); |
| 114 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 115 | defer git_dir.close(); |
| 116 | const t = try git.parseTree(alloc, try git.getObject(alloc, git_dir, "5403fecad0fde9120535321f222a061abc2849d9")); |
| 117 | // TODO: test fields when we upgrade to 0.14 and have decl literals |
| 118 | // try expect(try extras.mapBy(alloc, t.children, .id)).toEqualSlice(&.{ |
| 119 | // .{ .blob = .{ .id = "8e8d2ceba4b327ce9db93f988492d0e21a461012" } }, |
| 120 | // .{ .blob = .{ .id = "bb2a57bd81d13975f2a74ae5dd0e652de07bb8a7" } }, |
| 121 | // .{ .blob = .{ .id = "37be0c1cfa4f097edbb1fb5c0585cd18cb08df13" } }, |
| 122 | // .{ .blob = .{ .id = "b229eadbd5d6655c2dfbaca5a5f68f2f8f3c5454" } }, |
| 123 | // .{ .blob = .{ .id = "bb3f1c135632cfca760bd84fb18acdab8dae8ec3" } }, |
| 124 | // }); |
| 125 | try expect(try extras.mapBy(alloc, t.children, .name)).toEqualStringSlice(&.{ |
| 126 | ".gitattributes", |
| 127 | ".gitignore", |
| 128 | "LICENSE", |
| 129 | "git.zig", |
| 130 | "zig.mod", |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | test { |
| 135 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 136 | defer arena.deinit(); |
| 137 | const alloc = arena.allocator(); |
| 138 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 139 | defer git_dir.close(); |
| 140 | try expect(try git.getTreeDiff(alloc, git_dir, .{ .id = "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e" }, .{ .id = "c39f57f6bb01664a7146ddbfc3debe76ec135f44" })).toEqualString( |
| 141 | \\:100644 100644 73c7032166db0eb23c4be11a4ff8ff26ec47c582 b229eadbd5d6655c2dfbaca5a5f68f2f8f3c5454 M	git.zig |
| 142 | \\ |
| 143 | \\diff --git a/git.zig b/git.zig |
| 144 | \\index 73c7032..b229ead 100644 |
| 145 | \\--- a/git.zig |
| 146 | \\+++ b/git.zig |
| 147 | \\@@ -251,10 +251,10 @@ pub fn parseTree(alloc: std.mem.Allocator, treefile: string) !Tree { |
| 148 | \\ fn parseTreeMode(input: string) !Tree.Object.Mode { |
| 149 | \\ std.debug.assert(input.len == 6); |
| 150 | \\ return .{ |
| 151 | \\- .type = @intToEnum(Tree.Object.Type, try std.fmt.parseInt(u16, input[0..3], 10)), |
| 152 | \\- .perm_user = @bitCast(Tree.Object.Perm, try std.fmt.parseInt(u3, input[3..][0..1], 8)), |
| 153 | \\- .perm_group = @bitCast(Tree.Object.Perm, try std.fmt.parseInt(u3, input[4..][0..1], 8)), |
| 154 | \\- .perm_other = @bitCast(Tree.Object.Perm, try std.fmt.parseInt(u3, input[5..][0..1], 8)), |
| 155 | \\+ .type = @enumFromInt(try std.fmt.parseInt(u16, input[0..3], 10)), |
| 156 | \\+ .perm_user = @bitCast(try std.fmt.parseInt(u3, input[3..][0..1], 8)), |
| 157 | \\+ .perm_group = @bitCast(try std.fmt.parseInt(u3, input[4..][0..1], 8)), |
| 158 | \\+ .perm_other = @bitCast(try std.fmt.parseInt(u3, input[5..][0..1], 8)), |
| 159 | \\ }; |
| 160 | \\ } |
| 161 | \\ |
| 162 | ++ " "); |
| 163 | } |
| 164 | |
| 165 | test { |
| 166 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 167 | defer arena.deinit(); |
| 168 | const alloc = arena.allocator(); |
| 169 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 170 | defer git_dir.close(); |
| 171 | const t = try git.parseTreeDiffMeta(try git.getTreeDiff(alloc, git_dir, .{ .id = "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e" }, .{ .id = "c39f57f6bb01664a7146ddbfc3debe76ec135f44" })); |
| 172 | try expect(t.files_changed).toEqual(1); |
| 173 | try expect(t.lines_added).toEqual(4); |
| 174 | try expect(t.lines_removed).toEqual(4); |
| 175 | } |
| 176 | |
| 177 | test { |
| 178 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 179 | defer arena.deinit(); |
| 180 | const alloc = arena.allocator(); |
| 181 | var git_dir = try std.fs.cwd().openDir(".git", .{}); |
| 182 | defer git_dir.close(); |
| 183 | const t = try git.parseTreeDiff(alloc, try git.getTreeDiff(alloc, git_dir, .{ .id = "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e" }, .{ .id = "c39f57f6bb01664a7146ddbfc3debe76ec135f44" })); |
| 184 | _ = t; // TODO: test fields when we upgrade to 0.14 and have decl literals |
| 18 | 185 | } |