| 1 | const std = @import("std"); |
| 2 | const git = @import("git"); |
| 3 | const extras = @import("extras"); |
| 4 | const expect = @import("expect").expect; |
| 5 | const nfs = @import("nfs"); |
| 6 | |
| 7 | test { |
| 8 | const gpa = std.testing.allocator; |
| 9 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 10 | defer arena.deinit(); |
| 11 | const alloc = arena.allocator(); |
| 12 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 13 | defer git_dir.close(); |
| 14 | var repo: git.Repository = .init(git_dir, gpa); |
| 15 | defer repo.deinit(); |
| 16 | const branch_refs = try repo.getHeads(alloc); |
| 17 | const branch_names = try extras.mapBy(alloc, branch_refs, .label); |
| 18 | try expect(branch_names).toEqualStringSlice(&.{"master"}); |
| 19 | } |
| 20 | |
| 21 | test { |
| 22 | const gpa = std.testing.allocator; |
| 23 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 24 | defer arena.deinit(); |
| 25 | const alloc = arena.allocator(); |
| 26 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 27 | defer git_dir.close(); |
| 28 | var repo: git.Repository = .init(git_dir, gpa); |
| 29 | defer repo.deinit(); |
| 30 | try expect(try repo.getObjectC(alloc, "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 | const gpa = std.testing.allocator; |
| 43 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 44 | defer arena.deinit(); |
| 45 | const alloc = arena.allocator(); |
| 46 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 47 | defer git_dir.close(); |
| 48 | var repo: git.Repository = .init(git_dir, gpa); |
| 49 | defer repo.deinit(); |
| 50 | try expect((try repo.getObjectC(alloc, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).len).toEqual(229); |
| 51 | try expect((try repo.getObjectC(alloc, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).len).toEqual(45 + 47 + 55 + 58 + 0 + 18 + 6); |
| 52 | } |
| 53 | |
| 54 | test { |
| 55 | const gpa = std.testing.allocator; |
| 56 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 57 | defer arena.deinit(); |
| 58 | const alloc = arena.allocator(); |
| 59 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 60 | defer git_dir.close(); |
| 61 | var repo: git.Repository = .init(git_dir, gpa); |
| 62 | defer repo.deinit(); |
| 63 | try expect((try repo.getObjectA(alloc, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).type == .blob).toEqual(false); |
| 64 | try expect((try repo.getObjectA(alloc, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).type == .tree).toEqual(false); |
| 65 | try expect((try repo.getObjectA(alloc, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).type == .commit).toEqual(true); |
| 66 | try expect((try repo.getObjectA(alloc, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).type == .tag).toEqual(false); |
| 67 | } |
| 68 | |
| 69 | test { |
| 70 | const gpa = std.testing.allocator; |
| 71 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 72 | defer arena.deinit(); |
| 73 | const alloc = arena.allocator(); |
| 74 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 75 | defer git_dir.close(); |
| 76 | var repo: git.Repository = .init(git_dir, gpa); |
| 77 | defer repo.deinit(); |
| 78 | try expect((try repo.getObjectA(alloc, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e")).type).toEqual(.commit); |
| 79 | } |
| 80 | |
| 81 | test { |
| 82 | const gpa = std.testing.allocator; |
| 83 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 84 | defer arena.deinit(); |
| 85 | const alloc = arena.allocator(); |
| 86 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 87 | defer git_dir.close(); |
| 88 | var repo: git.Repository = .init(git_dir, gpa); |
| 89 | defer repo.deinit(); |
| 90 | const d = try repo.getCommitA(alloc, "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e"); |
| 91 | const c = d.reify(&repo); |
| 92 | try expect(c.tree.id).toEqualString("5403fecad0fde9120535321f222a061abc2849d9"); |
| 93 | try expect(c.parents.len).toEqual(1); |
| 94 | try expect(c.parents[0].id).toEqualString("c39f57f6bb01664a7146ddbfc3debe76ec135f44"); |
| 95 | try expect(c.author.name).toEqualString("Meghan Denny"); |
| 96 | try expect(c.author.email).toEqualString("hello@nektro.net"); |
| 97 | // TODO: test .at when we upgrade to 0.14 and have decl literals |
| 98 | try expect(c.committer.name).toEqualString("Meghan Denny"); |
| 99 | try expect(c.committer.email).toEqualString("hello@nektro.net"); |
| 100 | // TODO: test .at when we upgrade to 0.14 and have decl literals |
| 101 | try expect(c.message).toEqualString( |
| 102 | \\update to Zig 0.11 |
| 103 | \\ |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | test { |
| 108 | const gpa = std.testing.allocator; |
| 109 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 110 | defer arena.deinit(); |
| 111 | const alloc = arena.allocator(); |
| 112 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 113 | defer git_dir.close(); |
| 114 | var repo: git.Repository = .init(git_dir, gpa); |
| 115 | defer repo.deinit(); |
| 116 | try expect(try repo.getObjectC(alloc, "5403fecad0fde9120535321f222a061abc2849d9")).toEqualString( |
| 117 | // zig fmt: off |
| 118 | "100644 .gitattributes\x00" ++ .{0x8e,0x8d,0x2c,0xeb,0xa4,0xb3,0x27,0xce,0x9d,0xb9,0x3f,0x98,0x84,0x92,0xd0,0xe2,0x1a,0x46,0x10,0x12} ++ |
| 119 | "100644 .gitignore\x00" ++ .{0xbb,0x2a,0x57,0xbd,0x81,0xd1,0x39,0x75,0xf2,0xa7,0x4a,0xe5,0xdd,0x0e,0x65,0x2d,0xe0,0x7b,0xb8,0xa7} ++ |
| 120 | "100644 LICENSE\x00" ++ .{0x37,0xbe,0x0c,0x1c,0xfa,0x4f,0x09,0x7e,0xdb,0xb1,0xfb,0x5c,0x05,0x85,0xcd,0x18,0xcb,0x08,0xdf,0x13} ++ |
| 121 | "100644 git.zig\x00" ++ .{0xb2,0x29,0xea,0xdb,0xd5,0xd6,0x65,0x5c,0x2d,0xfb,0xac,0xa5,0xa5,0xf6,0x8f,0x2f,0x8f,0x3c,0x54,0x54} ++ |
| 122 | "100644 zig.mod\x00" ++ .{0xbb,0x3f,0x1c,0x13,0x56,0x32,0xcf,0xca,0x76,0x0b,0xd8,0x4f,0xb1,0x8a,0xcd,0xab,0x8d,0xae,0x8e,0xc3} ++ |
| 123 | "" |
| 124 | // zig fmt: on |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | test { |
| 129 | const gpa = std.testing.allocator; |
| 130 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 131 | defer arena.deinit(); |
| 132 | const alloc = arena.allocator(); |
| 133 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 134 | defer git_dir.close(); |
| 135 | var repo: git.Repository = .init(git_dir, gpa); |
| 136 | defer repo.deinit(); |
| 137 | const t = try repo.getTreeA(alloc, "5403fecad0fde9120535321f222a061abc2849d9"); |
| 138 | // TODO: std.testing.expectEqualSlices does ptr equality |
| 139 | // try expect(try extras.mapBy(alloc, t.children, .id)).toEqualSlice(&.{ |
| 140 | // .{ .blob = .{ .id = "8e8d2ceba4b327ce9db93f988492d0e21a461012" } }, |
| 141 | // .{ .blob = .{ .id = "bb2a57bd81d13975f2a74ae5dd0e652de07bb8a7" } }, |
| 142 | // .{ .blob = .{ .id = "37be0c1cfa4f097edbb1fb5c0585cd18cb08df13" } }, |
| 143 | // .{ .blob = .{ .id = "b229eadbd5d6655c2dfbaca5a5f68f2f8f3c5454" } }, |
| 144 | // .{ .blob = .{ .id = "bb3f1c135632cfca760bd84fb18acdab8dae8ec3" } }, |
| 145 | // }); |
| 146 | try expect(t.children.items.name).toEqualStringSlice(&.{ |
| 147 | ".gitattributes", |
| 148 | ".gitignore", |
| 149 | "LICENSE", |
| 150 | "git.zig", |
| 151 | "zig.mod", |
| 152 | }); |
| 153 | } |
| 154 | |
| 155 | test { |
| 156 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 157 | defer arena.deinit(); |
| 158 | const alloc = arena.allocator(); |
| 159 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 160 | defer git_dir.close(); |
| 161 | try expect(try git.getTreeDiff(alloc, git_dir, .{ .id = "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e" }, .{ .id = "c39f57f6bb01664a7146ddbfc3debe76ec135f44" })).toEqualString( |
| 162 | // zig fmt: off |
| 163 | ":100644 100644 73c7032166db0eb23c4be11a4ff8ff26ec47c582 b229eadbd5d6655c2dfbaca5a5f68f2f8f3c5454 M\tgit.zig\n" ++ |
| 164 | "\n" ++ |
| 165 | "diff --git a/git.zig b/git.zig\n" ++ |
| 166 | "index 73c7032166db0eb23c4be11a4ff8ff26ec47c582..b229eadbd5d6655c2dfbaca5a5f68f2f8f3c5454 100644\n" ++ |
| 167 | "--- a/git.zig\n" ++ |
| 168 | "+++ b/git.zig\n" ++ |
| 169 | "@@ -251,10 +251,10 @@ pub fn parseTree(alloc: std.mem.Allocator, treefile: string) !Tree {\n" ++ |
| 170 | " fn parseTreeMode(input: string) !Tree.Object.Mode {\n" ++ |
| 171 | " std.debug.assert(input.len == 6);\n" ++ |
| 172 | " return .{\n" ++ |
| 173 | "- .type = @intToEnum(Tree.Object.Type, try std.fmt.parseInt(u16, input[0..3], 10)),\n" ++ |
| 174 | "- .perm_user = @bitCast(Tree.Object.Perm, try std.fmt.parseInt(u3, input[3..][0..1], 8)),\n" ++ |
| 175 | "- .perm_group = @bitCast(Tree.Object.Perm, try std.fmt.parseInt(u3, input[4..][0..1], 8)),\n" ++ |
| 176 | "- .perm_other = @bitCast(Tree.Object.Perm, try std.fmt.parseInt(u3, input[5..][0..1], 8)),\n" ++ |
| 177 | "+ .type = @enumFromInt(try std.fmt.parseInt(u16, input[0..3], 10)),\n" ++ |
| 178 | "+ .perm_user = @bitCast(try std.fmt.parseInt(u3, input[3..][0..1], 8)),\n" ++ |
| 179 | "+ .perm_group = @bitCast(try std.fmt.parseInt(u3, input[4..][0..1], 8)),\n" ++ |
| 180 | "+ .perm_other = @bitCast(try std.fmt.parseInt(u3, input[5..][0..1], 8)),\n" ++ |
| 181 | " };\n" ++ |
| 182 | " }\n" ++ |
| 183 | " " ++ |
| 184 | "" |
| 185 | // zig fmt: on |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | test { |
| 190 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 191 | defer arena.deinit(); |
| 192 | const alloc = arena.allocator(); |
| 193 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 194 | defer git_dir.close(); |
| 195 | const t = try git.parseTreeDiff(alloc, try git.getTreeDiff(alloc, git_dir, .{ .id = "a542da41f1f0c59fdd0e1527cf5ff9de3f6a0c8e" }, .{ .id = "c39f57f6bb01664a7146ddbfc3debe76ec135f44" })); |
| 196 | _ = t; // TODO: test fields when we upgrade to 0.14 and have decl literals |
| 197 | } |
| 198 | |
| 199 | test { |
| 200 | const gpa = std.testing.allocator; |
| 201 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 202 | defer arena.deinit(); |
| 203 | const alloc = arena.allocator(); |
| 204 | const git_dir = try nfs.cwd().openDir(".git", .{}); |
| 205 | defer git_dir.close(); |
| 206 | var repo: git.Repository = .init(git_dir, gpa); |
| 207 | defer repo.deinit(); |
| 208 | const head_oid = (try git.getHEAD(alloc, git_dir)).?; |
| 209 | _ = try repo.getTreeCommits(alloc, head_oid, ""); |
| 210 | _ = try repo.getTreeCommits(alloc, head_oid, "testdata"); |
| 211 | } |
| 212 | |
| 213 | test { |
| 214 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 215 | defer arena.deinit(); |
| 216 | const alloc = arena.allocator(); |
| 217 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-9feb6b667c77c187f547deaf10cd1f04e92fe192")); // magnolia-desktop |
| 218 | } |
| 219 | test { |
| 220 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 221 | defer arena.deinit(); |
| 222 | const alloc = arena.allocator(); |
| 223 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-f2ccd4d1333aad1175550daf616b061388e1864f")); // magnolia-desktop |
| 224 | } |
| 225 | test { |
| 226 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 227 | defer arena.deinit(); |
| 228 | const alloc = arena.allocator(); |
| 229 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-47ed809ce10bdfa99351c72935467c9afae47e36")); // magnolia-desktop |
| 230 | } |
| 231 | test { |
| 232 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 233 | defer arena.deinit(); |
| 234 | const alloc = arena.allocator(); |
| 235 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-15700f9e1c919a3f0c492415d982a09f5e8122cd")); // magnolia-desktop |
| 236 | } |
| 237 | test { |
| 238 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 239 | defer arena.deinit(); |
| 240 | const alloc = arena.allocator(); |
| 241 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-8480815a00f341a9058a26bf86f34368184615d6")); // magnolia-desktop |
| 242 | } |
| 243 | test { |
| 244 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 245 | defer arena.deinit(); |
| 246 | const alloc = arena.allocator(); |
| 247 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-c37d23f45ae6bd0db6b072180d7b84566c7dc8a2")); // zig |
| 248 | } |
| 249 | test { |
| 250 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 251 | defer arena.deinit(); |
| 252 | const alloc = arena.allocator(); |
| 253 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-f58200e3f2967a06f343c9fc9dcae9de18def92a")); // zig |
| 254 | } |
| 255 | test { |
| 256 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 257 | defer arena.deinit(); |
| 258 | const alloc = arena.allocator(); |
| 259 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-d9165aacce3629d503f87cd2c3f612629127641b")); // zig |
| 260 | } |
| 261 | test { |
| 262 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 263 | defer arena.deinit(); |
| 264 | const alloc = arena.allocator(); |
| 265 | _ = try git.parseCommit(alloc, @embedFile("./testdata/commit-afdc41cfd61db23f545d9239b61695e2ae991713")); // zig |
| 266 | } |
| 267 | test { |
| 268 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 269 | defer arena.deinit(); |
| 270 | const alloc = arena.allocator(); |
| 271 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-1f7390f3999e80f775dbc0e62f1dcb071c3bed77")); // zig |
| 272 | } |
| 273 | test { |
| 274 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 275 | defer arena.deinit(); |
| 276 | const alloc = arena.allocator(); |
| 277 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-a0482e3446cea426bf16571e0000423ed5b25af0")); // linux |
| 278 | } |
| 279 | test { |
| 280 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 281 | defer arena.deinit(); |
| 282 | const alloc = arena.allocator(); |
| 283 | _ = try git.parseTreeDiff(alloc, @embedFile("./testdata/diff-e90937e756938f03d37d4cae7c82316a3a425944")); // linux |
| 284 | } |