From 4310dcc06ba135b6297df576c5425461af27732b Mon Sep 17 00:00:00 2001 From: Matei Dibu Date: Thu, 10 Oct 2024 11:56:33 +0300 Subject: [PATCH 1/5] add support for git+https sources --- fixtures/basic.zon | 4 ++++ src/Dependency.zig | 1 + src/codegen.zig | 36 +++++++++++++++++++++++++----------- src/fetch.zig | 8 +++++++- src/parse.zig | 17 +++++++++++++++-- 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/fixtures/basic.zon b/fixtures/basic.zon index c9ef3ea..5e2accd 100644 --- a/fixtures/basic.zon +++ b/fixtures/basic.zon @@ -15,5 +15,9 @@ .url = "https://gist.github.com/antlilja/8372900fcc09e38d7b0b6bbaddad3904/archive/6c3321e0969ff2463f8335da5601986cf2108690.tar.gz", .hash = "1220363c7e27b2d3f39de6ff6e90f9537a0634199860fea237a55ddb1e1717f5d6a5", }, + .ziggy = .{ + .url = "git+https://github.com/kristoff-it/ziggy#c66f47bc632c66668d61fa06eda112b41d6e5130", + .hash = "1220115ff095a3c970cc90fce115294ba67d6fbc4927472dc856abc51e2a1a9364d7", + }, }, } diff --git a/src/Dependency.zig b/src/Dependency.zig index f3e8c4f..64ccc44 100644 --- a/src/Dependency.zig +++ b/src/Dependency.zig @@ -1,3 +1,4 @@ url: []const u8, +rev: []const u8, nix_hash: []const u8, done: bool, diff --git a/src/codegen.zig b/src/codegen.zig index 0074553..769d3e3 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -11,7 +11,7 @@ pub fn write(alloc: Allocator, out: anytype, deps: StringHashMap(Dependency)) !v try out.writeAll( \\# generated by zon2nix (https://github.com/nix-community/zon2nix) \\ - \\{ linkFarm, fetchzip }: + \\{ linkFarm, fetchzip, fetchgit }: \\ \\linkFarm "zig-packages" [ \\ @@ -28,16 +28,30 @@ pub fn write(alloc: Allocator, out: anytype, deps: StringHashMap(Dependency)) !v for (entries) |entry| { const key = entry.key_ptr.*; const dep = entry.value_ptr.*; - try out.print( - \\ {{ - \\ name = "{s}"; - \\ path = fetchzip {{ - \\ url = "{s}"; - \\ hash = "{s}"; - \\ }}; - \\ }} - \\ - , .{ key, dep.url, dep.nix_hash }); + if (std.mem.startsWith(u8, dep.url, "https://")) { + try out.print( + \\ {{ + \\ name = "{s}"; + \\ path = fetchzip {{ + \\ url = "{s}"; + \\ hash = "{s}"; + \\ }}; + \\ }} + \\ + , .{ key, dep.url, dep.nix_hash }); + } else if (std.mem.startsWith(u8, dep.url, "git+https://")) { + try out.print( + \\ {{ + \\ name = "{s}"; + \\ path = fetchgit {{ + \\ url = "{s}"; + \\ rev = "{s}"; + \\ hash = "{s}"; + \\ }}; + \\ }} + \\ + , .{ key, dep.url[4..], dep.rev, dep.nix_hash }); + } else {} } try out.writeAll("]\n"); diff --git a/src/fetch.zig b/src/fetch.zig index 459b461..c4b1c07 100644 --- a/src/fetch.zig +++ b/src/fetch.zig @@ -35,7 +35,13 @@ pub fn fetch(alloc: Allocator, deps: *StringHashMap(Dependency)) !void { } var child = try alloc.create(ChildProcess); - const ref = try fmt.allocPrint(alloc, "tarball+{s}", .{dep.url}); + const ref = ref: { + if (dep.rev.len == 0) { + break :ref try fmt.allocPrint(alloc, "tarball+{s}", .{dep.url}); + } else { + break :ref try fmt.allocPrint(alloc, "{s}?rev={s}", .{ dep.url, dep.rev }); + } + }; const argv = &[_][]const u8{ nix, "flake", "prefetch", "--json", "--extra-experimental-features", "flakes nix-command", ref }; child.* = ChildProcess.init(argv, alloc); child.stdin_behavior = .Ignore; diff --git a/src/parse.zig b/src/parse.zig index fda3b6e..6fee2d1 100644 --- a/src/parse.zig +++ b/src/parse.zig @@ -32,6 +32,7 @@ pub fn parse(alloc: Allocator, deps: *StringHashMap(Dependency), file: File) !vo for (deps_init.ast.fields) |dep_idx| { var dep: Dependency = .{ .url = undefined, + .rev = undefined, .nix_hash = undefined, .done = false, }; @@ -47,7 +48,18 @@ pub fn parse(alloc: Allocator, deps: *StringHashMap(Dependency), file: File) !vo const name = try parseFieldName(alloc, ast, dep_field_idx); if (mem.eql(u8, name, "url")) { - dep.url = try parseString(alloc, ast, dep_field_idx); + const url = try parseString(alloc, ast, dep_field_idx); + if (std.mem.startsWith(u8, url, "https://")) { + dep.url = url; + } else if (std.mem.startsWith(u8, url, "git+https://")) { + const url_start = std.mem.indexOf(u8, url, "git+https://") orelse return error.InvalidInput; + const url_end = std.mem.indexOf(u8, url[url_start..], "#") orelse return error.InvalidInput; + const raw_url = url[url_start .. url_start + url_end]; + const hash_start = url_end + url_start + 1; // +1 to skip the '#' + const git_hash = url[hash_start..]; + dep.url = raw_url; + dep.rev = git_hash; + } has_url = true; } else if (mem.eql(u8, name, "hash")) { hash = try parseString(alloc, ast, dep_field_idx); @@ -87,8 +99,9 @@ test parse { try parse(alloc, &deps, basic); basic.close(); - try testing.expectEqual(deps.count(), 3); + try testing.expectEqual(deps.count(), 4); try testing.expectEqualStrings(deps.get("122048992ca58a78318b6eba4f65c692564be5af3b30fbef50cd4abeda981b2e7fa5").?.url, "https://github.com/ziglibs/known-folders/archive/fa75e1bc672952efa0cf06160bbd942b47f6d59b.tar.gz"); try testing.expectEqualStrings(deps.get("122089a8247a693cad53beb161bde6c30f71376cd4298798d45b32740c3581405864").?.url, "https://github.com/ziglibs/diffz/archive/90353d401c59e2ca5ed0abe5444c29ad3d7489aa.tar.gz"); try testing.expectEqualStrings(deps.get("1220363c7e27b2d3f39de6ff6e90f9537a0634199860fea237a55ddb1e1717f5d6a5").?.url, "https://gist.github.com/antlilja/8372900fcc09e38d7b0b6bbaddad3904/archive/6c3321e0969ff2463f8335da5601986cf2108690.tar.gz"); + try testing.expectEqualStrings(deps.get("1220115ff095a3c970cc90fce115294ba67d6fbc4927472dc856abc51e2a1a9364d7").?.url, "git+https://github.com/kristoff-it/ziggy#c66f47bc632c66668d61fa06eda112b41d6e5130"); } From 9e737b009112c5c676a155e5aa6e24877462b1d2 Mon Sep 17 00:00:00 2001 From: Matei Dibu Date: Fri, 11 Oct 2024 18:00:34 +0300 Subject: [PATCH 2/5] simplify git+https:// handling --- src/codegen.zig | 16 ++++++++-------- src/fetch.zig | 2 +- src/parse.zig | 11 ++++++----- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/codegen.zig b/src/codegen.zig index 769d3e3..b9d6f80 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -28,30 +28,30 @@ pub fn write(alloc: Allocator, out: anytype, deps: StringHashMap(Dependency)) !v for (entries) |entry| { const key = entry.key_ptr.*; const dep = entry.value_ptr.*; - if (std.mem.startsWith(u8, dep.url, "https://")) { + if (dep.rev.len != 0) { try out.print( \\ {{ \\ name = "{s}"; - \\ path = fetchzip {{ + \\ path = fetchgit {{ \\ url = "{s}"; + \\ rev = "{s}"; \\ hash = "{s}"; \\ }}; \\ }} \\ - , .{ key, dep.url, dep.nix_hash }); - } else if (std.mem.startsWith(u8, dep.url, "git+https://")) { + , .{ key, dep.url, dep.rev, dep.nix_hash }); + } else { try out.print( \\ {{ \\ name = "{s}"; - \\ path = fetchgit {{ + \\ path = fetchzip {{ \\ url = "{s}"; - \\ rev = "{s}"; \\ hash = "{s}"; \\ }}; \\ }} \\ - , .{ key, dep.url[4..], dep.rev, dep.nix_hash }); - } else {} + , .{ key, dep.url, dep.nix_hash }); + } } try out.writeAll("]\n"); diff --git a/src/fetch.zig b/src/fetch.zig index c4b1c07..858510d 100644 --- a/src/fetch.zig +++ b/src/fetch.zig @@ -39,7 +39,7 @@ pub fn fetch(alloc: Allocator, deps: *StringHashMap(Dependency)) !void { if (dep.rev.len == 0) { break :ref try fmt.allocPrint(alloc, "tarball+{s}", .{dep.url}); } else { - break :ref try fmt.allocPrint(alloc, "{s}?rev={s}", .{ dep.url, dep.rev }); + break :ref try fmt.allocPrint(alloc, "git+{s}?rev={s}", .{ dep.url, dep.rev }); } }; const argv = &[_][]const u8{ nix, "flake", "prefetch", "--json", "--extra-experimental-features", "flakes nix-command", ref }; diff --git a/src/parse.zig b/src/parse.zig index 6fee2d1..a925e9f 100644 --- a/src/parse.zig +++ b/src/parse.zig @@ -52,10 +52,9 @@ pub fn parse(alloc: Allocator, deps: *StringHashMap(Dependency), file: File) !vo if (std.mem.startsWith(u8, url, "https://")) { dep.url = url; } else if (std.mem.startsWith(u8, url, "git+https://")) { - const url_start = std.mem.indexOf(u8, url, "git+https://") orelse return error.InvalidInput; - const url_end = std.mem.indexOf(u8, url[url_start..], "#") orelse return error.InvalidInput; - const raw_url = url[url_start .. url_start + url_end]; - const hash_start = url_end + url_start + 1; // +1 to skip the '#' + const url_end = std.mem.indexOf(u8, url[0..], "#").?; + const raw_url = url[4..url_end]; + const hash_start = url_end + 1; // +1 to skip the '#' const git_hash = url[hash_start..]; dep.url = raw_url; dep.rev = git_hash; @@ -103,5 +102,7 @@ test parse { try testing.expectEqualStrings(deps.get("122048992ca58a78318b6eba4f65c692564be5af3b30fbef50cd4abeda981b2e7fa5").?.url, "https://github.com/ziglibs/known-folders/archive/fa75e1bc672952efa0cf06160bbd942b47f6d59b.tar.gz"); try testing.expectEqualStrings(deps.get("122089a8247a693cad53beb161bde6c30f71376cd4298798d45b32740c3581405864").?.url, "https://github.com/ziglibs/diffz/archive/90353d401c59e2ca5ed0abe5444c29ad3d7489aa.tar.gz"); try testing.expectEqualStrings(deps.get("1220363c7e27b2d3f39de6ff6e90f9537a0634199860fea237a55ddb1e1717f5d6a5").?.url, "https://gist.github.com/antlilja/8372900fcc09e38d7b0b6bbaddad3904/archive/6c3321e0969ff2463f8335da5601986cf2108690.tar.gz"); - try testing.expectEqualStrings(deps.get("1220115ff095a3c970cc90fce115294ba67d6fbc4927472dc856abc51e2a1a9364d7").?.url, "git+https://github.com/kristoff-it/ziggy#c66f47bc632c66668d61fa06eda112b41d6e5130"); + const git_dep = deps.get("1220115ff095a3c970cc90fce115294ba67d6fbc4927472dc856abc51e2a1a9364d7").?; + try testing.expectEqualStrings(git_dep.url, "https://github.com/kristoff-it/ziggy"); + try testing.expectEqualStrings(git_dep.rev, "c66f47bc632c66668d61fa06eda112b41d6e5130"); } From b2814825b30b0339cd0639ee221f84a3106c09fd Mon Sep 17 00:00:00 2001 From: Matei Dibu Date: Wed, 27 Nov 2024 12:42:45 +0200 Subject: [PATCH 3/5] continue on parse error parsing dependencies with no deps fails --- src/parse.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/parse.zig b/src/parse.zig index a925e9f..501501e 100644 --- a/src/parse.zig +++ b/src/parse.zig @@ -41,7 +41,8 @@ pub fn parse(alloc: Allocator, deps: *StringHashMap(Dependency), file: File) !vo var has_hash = false; const dep_init = ast.fullStructInit(&buf, dep_idx) orelse { - return error.parseError; + std.log.warn("failed to get dependencies", .{}); + continue; }; for (dep_init.ast.fields) |dep_field_idx| { From ec9c05037c5d96e361d082e9ee1913d6500638b1 Mon Sep 17 00:00:00 2001 From: Matei Dibu Date: Wed, 27 Nov 2024 12:44:23 +0200 Subject: [PATCH 4/5] add test for dep with deps with no deps --- fixtures/basic.zon | 6 ++++++ src/parse.zig | 11 +++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/fixtures/basic.zon b/fixtures/basic.zon index 5e2accd..ec4be00 100644 --- a/fixtures/basic.zon +++ b/fixtures/basic.zon @@ -15,9 +15,15 @@ .url = "https://gist.github.com/antlilja/8372900fcc09e38d7b0b6bbaddad3904/archive/6c3321e0969ff2463f8335da5601986cf2108690.tar.gz", .hash = "1220363c7e27b2d3f39de6ff6e90f9537a0634199860fea237a55ddb1e1717f5d6a5", }, + // git+https test .ziggy = .{ .url = "git+https://github.com/kristoff-it/ziggy#c66f47bc632c66668d61fa06eda112b41d6e5130", .hash = "1220115ff095a3c970cc90fce115294ba67d6fbc4927472dc856abc51e2a1a9364d7", }, + // this has deps (github.com/zigimg/zigimg and codeberg.org/atman/zg) with no deps + .vaxis = .{ + .url = "git+https://github.com/rockorager/libvaxis#1fd920a7aea1bb040c7c028f4bbf0af2ea58e1d1", + .hash = "1220feaa655e14cbb4baf59fe746f09a17fc6949be46ad64dd5044982f4fc1bb57c7", + }, }, } diff --git a/src/parse.zig b/src/parse.zig index 501501e..b1ea18c 100644 --- a/src/parse.zig +++ b/src/parse.zig @@ -99,11 +99,14 @@ test parse { try parse(alloc, &deps, basic); basic.close(); - try testing.expectEqual(deps.count(), 4); + try testing.expectEqual(deps.count(), 5); try testing.expectEqualStrings(deps.get("122048992ca58a78318b6eba4f65c692564be5af3b30fbef50cd4abeda981b2e7fa5").?.url, "https://github.com/ziglibs/known-folders/archive/fa75e1bc672952efa0cf06160bbd942b47f6d59b.tar.gz"); try testing.expectEqualStrings(deps.get("122089a8247a693cad53beb161bde6c30f71376cd4298798d45b32740c3581405864").?.url, "https://github.com/ziglibs/diffz/archive/90353d401c59e2ca5ed0abe5444c29ad3d7489aa.tar.gz"); try testing.expectEqualStrings(deps.get("1220363c7e27b2d3f39de6ff6e90f9537a0634199860fea237a55ddb1e1717f5d6a5").?.url, "https://gist.github.com/antlilja/8372900fcc09e38d7b0b6bbaddad3904/archive/6c3321e0969ff2463f8335da5601986cf2108690.tar.gz"); - const git_dep = deps.get("1220115ff095a3c970cc90fce115294ba67d6fbc4927472dc856abc51e2a1a9364d7").?; - try testing.expectEqualStrings(git_dep.url, "https://github.com/kristoff-it/ziggy"); - try testing.expectEqualStrings(git_dep.rev, "c66f47bc632c66668d61fa06eda112b41d6e5130"); + const ziggy = deps.get("1220115ff095a3c970cc90fce115294ba67d6fbc4927472dc856abc51e2a1a9364d7").?; + try testing.expectEqualStrings(ziggy.url, "https://github.com/kristoff-it/ziggy"); + try testing.expectEqualStrings(ziggy.rev, "c66f47bc632c66668d61fa06eda112b41d6e5130"); + const vaxis = deps.get("1220feaa655e14cbb4baf59fe746f09a17fc6949be46ad64dd5044982f4fc1bb57c7").?; + try testing.expectEqualStrings(vaxis.url, "https://github.com/rockorager/libvaxis"); + try testing.expectEqualStrings(vaxis.rev, "1fd920a7aea1bb040c7c028f4bbf0af2ea58e1d1"); } From fa3df6c80cdd1512689c1d0d95a6c54c04514312 Mon Sep 17 00:00:00 2001 From: Matei Dibu Date: Wed, 27 Nov 2024 12:44:49 +0200 Subject: [PATCH 5/5] defer test file close --- src/parse.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parse.zig b/src/parse.zig index b1ea18c..fe86368 100644 --- a/src/parse.zig +++ b/src/parse.zig @@ -96,8 +96,8 @@ test parse { var deps = StringHashMap(Dependency).init(alloc); const basic = try fs.cwd().openFile("fixtures/basic.zon", .{}); + defer basic.close(); try parse(alloc, &deps, basic); - basic.close(); try testing.expectEqual(deps.count(), 5); try testing.expectEqualStrings(deps.get("122048992ca58a78318b6eba4f65c692564be5af3b30fbef50cd4abeda981b2e7fa5").?.url, "https://github.com/ziglibs/known-folders/archive/fa75e1bc672952efa0cf06160bbd942b47f6d59b.tar.gz");