authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-04 10:34:58 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-06-04 10:34:58 -07:00
log151e112a6a94b8bb0a8b3c3457d179d2c66ad478
tree0e398e804d3d89797f585d08568d26ecb68f498e
parent3caa1188c9e0d926b51908e42905162b4551668a

use an array for 'id' so that we're not returning pointer to a local


7 files changed, 35 insertions(+), 25 deletions(-)

src/cmd/fetch.zig+2-2
...@@ -285,7 +285,7 @@ fn diff_printchange(comptime testt: string, comptime replacement: string, item:...@@ -285,7 +285,7 @@ fn diff_printchange(comptime testt: string, comptime replacement: string, item:
285fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module, alloc: std.mem.Allocator) !void {285fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module, alloc: std.mem.Allocator) !void {
286 for (list) |mod| {286 for (list) |mod| {
287 if (mod.type == .system_lib or mod.type == .framework) continue;287 if (mod.type == .system_lib or mod.type == .framework) continue;
288 if (std.mem.eql(u8, mod.id, "root")) {288 if (std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
289 try w.writeAll(" pub const _root = \"\";\n");289 try w.writeAll(" pub const _root = \"\";\n");
290 continue;290 continue;
291 }291 }
...@@ -325,7 +325,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Modul...@@ -325,7 +325,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Modul
325 mod.short_id(),325 mod.short_id(),
326 mod.short_id(),326 mod.short_id(),
327 });327 });
328 if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) {328 if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
329 try w.print(329 try w.print(
330 \\ .import = .{{ "{s}", .{{ .path = dirs._{s} ++ "/{s}" }} }},330 \\ .import = .{{ "{s}", .{{ .path = dirs._{s} ++ "/{s}" }} }},
331 \\331 \\
src/cmd/generate.zig+1-1
...@@ -284,7 +284,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, alloc: std.mem.Allocator, cachepath:...@@ -284,7 +284,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, alloc: std.mem.Allocator, cachepath:
284 .hg => @panic("TODO"),284 .hg => @panic("TODO"),
285 .http => @panic("TODO"),285 .http => @panic("TODO"),
286 }286 }
287 if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) {287 if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
288 try w.print(" .name = \"{s}\",\n", .{mod.name});288 try w.print(" .name = \"{s}\",\n", .{mod.name});
289 try w.print(" .entry = \"/{}/{s}/{s}\",\n", .{ std.zig.fmtEscapes(fixed_path), try mod.pin(alloc, cachepath), mod.main });289 try w.print(" .entry = \"/{}/{s}/{s}\",\n", .{ std.zig.fmtEscapes(fixed_path), try mod.pin(alloc, cachepath), mod.main });
290290
src/common.zig+4-5
...@@ -46,7 +46,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO...@@ -46,7 +46,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
46 }46 }
47 return zigmod.Module{47 return zigmod.Module{
48 .type = .local,48 .type = .local,
49 .id = "root",49 .id = zigmod.Module.ROOT,
50 .name = "root",50 .name = "root",
51 .main = m.main,51 .main = m.main,
52 .deps = try moduledeps.toOwnedSlice(),52 .deps = try moduledeps.toOwnedSlice(),
...@@ -230,7 +230,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -230,7 +230,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
230 .system_lib, .framework => {230 .system_lib, .framework => {
231 return zigmod.Module{231 return zigmod.Module{
232 .type = d.type,232 .type = d.type,
233 .id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Sha3_384, d.path),233 .id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Shake(96), d.path),
234 .name = d.path,234 .name = d.path,
235 .only_os = d.only_os,235 .only_os = d.only_os,
236 .except_os = d.except_os,236 .except_os = d.except_os,
...@@ -274,7 +274,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -274,7 +274,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
274 dd.for_build = d.for_build;274 dd.for_build = d.for_build;
275 const save = dd;275 const save = dd;
276 if (d.type != .local) dd.clean_path = extras.trimPrefix(modpath, cachepath)[1..];276 if (d.type != .local) dd.clean_path = extras.trimPrefix(modpath, cachepath)[1..];
277 if (dd.id.len == 0) dd.id = &u.random_string(48);277 if (std.mem.eql(u8, &dd.id, &zigmod.Dep.EMPTY)) dd.id = u.random_string(48);
278 if (d.name.len > 0) dd.name = d.name;278 if (d.name.len > 0) dd.name = d.name;
279 if (d.main.len > 0) dd.main = d.main;279 if (d.main.len > 0) dd.main = d.main;
280 if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;280 if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;
...@@ -352,12 +352,11 @@ pub fn parse_lockfile(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const [4]str...@@ -352,12 +352,11 @@ pub fn parse_lockfile(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const [4]str
352 .type = std.meta.stringToEnum(zigmod.Dep.Type, iter.next().?).?,352 .type = std.meta.stringToEnum(zigmod.Dep.Type, iter.next().?).?,
353 .path = iter.next().?,353 .path = iter.next().?,
354 .version = iter.next().?,354 .version = iter.next().?,
355 .id = "",355 .id = zigmod.Dep.EMPTY,
356 .name = "",356 .name = "",
357 .main = "",357 .main = "",
358 .yaml = null,358 .yaml = null,
359 .deps = &.{},359 .deps = &.{},
360 .parent_id = "",
361 };360 };
362 try list.append([4]string{361 try list.append([4]string{
363 try asdep.clean_path(alloc),362 try asdep.clean_path(alloc),
src/util/dep.zig+3-2
...@@ -15,7 +15,7 @@ pub const Dep = struct {...@@ -15,7 +15,7 @@ pub const Dep = struct {
1515
16 type: Type,16 type: Type,
17 path: string,17 path: string,
18 id: string,18 id: [48]u8,
19 name: string,19 name: string,
20 main: string,20 main: string,
21 version: string,21 version: string,
...@@ -28,7 +28,8 @@ pub const Dep = struct {...@@ -28,7 +28,8 @@ pub const Dep = struct {
28 deps: []zigmod.Dep,28 deps: []zigmod.Dep,
29 keep: bool = false,29 keep: bool = false,
30 for_build: bool = false,30 for_build: bool = false,
31 parent_id: string,31
32 pub const EMPTY = (" " ** 48).*;
3233
33 pub const Type = @import("./dep_type.zig").DepType;34 pub const Type = @import("./dep_type.zig").DepType;
3435
src/util/funcs.zig+5-5
...@@ -132,9 +132,9 @@ pub fn validate_hash(alloc: std.mem.Allocator, input: string, file_path: string)...@@ -132,9 +132,9 @@ pub fn validate_hash(alloc: std.mem.Allocator, input: string, file_path: string)
132 const data = try file.reader().readAllAlloc(alloc, gb);132 const data = try file.reader().readAllAlloc(alloc, gb);
133 const expected = hash.string;133 const expected = hash.string;
134 const actual = switch (hash.id) {134 const actual = switch (hash.id) {
135 .blake3 => try do_hash(alloc, std.crypto.hash.Blake3, data),135 .blake3 => &try do_hash(alloc, std.crypto.hash.Blake3, data),
136 .sha256 => try do_hash(alloc, std.crypto.hash.sha2.Sha256, data),136 .sha256 => &try do_hash(alloc, std.crypto.hash.sha2.Sha256, data),
137 .sha512 => try do_hash(alloc, std.crypto.hash.sha2.Sha512, data),137 .sha512 => &try do_hash(alloc, std.crypto.hash.sha2.Sha512, data),
138 };138 };
139 const result = std.mem.startsWith(u8, actual, expected);139 const result = std.mem.startsWith(u8, actual, expected);
140 if (!result) {140 if (!result) {
...@@ -143,13 +143,13 @@ pub fn validate_hash(alloc: std.mem.Allocator, input: string, file_path: string)...@@ -143,13 +143,13 @@ pub fn validate_hash(alloc: std.mem.Allocator, input: string, file_path: string)
143 return result;143 return result;
144}144}
145145
146pub fn do_hash(alloc: std.mem.Allocator, comptime algo: type, data: string) !string {146pub fn do_hash(alloc: std.mem.Allocator, comptime algo: type, data: string) ![algo.digest_length * 2]u8 {
147 var h = algo.init(.{});147 var h = algo.init(.{});
148 var out: [algo.digest_length]u8 = undefined;148 var out: [algo.digest_length]u8 = undefined;
149 h.update(data);149 h.update(data);
150 h.final(&out);150 h.final(&out);
151 const hex = try std.fmt.allocPrint(alloc, "{x}", .{std.fmt.fmtSliceHexLower(out[0..])});151 const hex = try std.fmt.allocPrint(alloc, "{x}", .{std.fmt.fmtSliceHexLower(out[0..])});
152 return hex;152 return hex[0 .. algo.digest_length * 2].*;
153}153}
154154
155/// Returns the result of running `git rev-parse HEAD`155/// Returns the result of running `git rev-parse HEAD`
src/util/modfile.zig+9-5
...@@ -15,7 +15,7 @@ const mb = kb * 1024;...@@ -15,7 +15,7 @@ const mb = kb * 1024;
15pub const ModFile = struct {15pub const ModFile = struct {
16 const Self = @This();16 const Self = @This();
1717
18 id: string,18 id: [48]u8,
19 name: string,19 name: string,
20 main: string,20 main: string,
21 c_include_dirs: []const string,21 c_include_dirs: []const string,
...@@ -52,7 +52,9 @@ pub const ModFile = struct {...@@ -52,7 +52,9 @@ pub const ModFile = struct {
52 }52 }
5353
54 pub fn from_mapping(alloc: std.mem.Allocator, mapping: yaml.Mapping) !Self {54 pub fn from_mapping(alloc: std.mem.Allocator, mapping: yaml.Mapping) !Self {
55 const id = mapping.get_string("id") orelse "";55 var id = zigmod.Dep.EMPTY;
56 std.mem.copyForwards(u8, &id, mapping.get_string("id") orelse &u.random_string(48));
57
56 const name = mapping.get_string("name") orelse "";58 const name = mapping.get_string("name") orelse "";
57 const main = mapping.get_string("main") orelse "";59 const main = mapping.get_string("main") orelse "";
5860
...@@ -61,7 +63,7 @@ pub const ModFile = struct {...@@ -61,7 +63,7 @@ pub const ModFile = struct {
61 }63 }
6264
63 return Self{65 return Self{
64 .id = if (id.len == 0) &u.random_string(48) else id,66 .id = id,
65 .name = name,67 .name = name,
66 .main = main,68 .main = main,
67 .c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"),69 .c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"),
...@@ -120,10 +122,13 @@ pub const ModFile = struct {...@@ -120,10 +122,13 @@ pub const ModFile = struct {
120 }122 }
121 }123 }
122124
125 var id = zigmod.Dep.EMPTY;
126 std.mem.copyForwards(u8, &id, item.mapping.get_string("id") orelse "");
127
123 try dep_list.append(zigmod.Dep{128 try dep_list.append(zigmod.Dep{
124 .type = dep_type,129 .type = dep_type,
125 .path = path,130 .path = path,
126 .id = item.mapping.get_string("id") orelse "",131 .id = id,
127 .name = name,132 .name = name,
128 .main = main,133 .main = main,
129 .version = version.?,134 .version = version.?,
...@@ -136,7 +141,6 @@ pub const ModFile = struct {...@@ -136,7 +141,6 @@ pub const ModFile = struct {
136 .deps = try dep_list_by_name(alloc, item.mapping, &.{"dependencies"}, for_build),141 .deps = try dep_list_by_name(alloc, item.mapping, &.{"dependencies"}, for_build),
137 .keep = std.mem.eql(u8, "true", item.mapping.get_string("keep") orelse ""),142 .keep = std.mem.eql(u8, "true", item.mapping.get_string("keep") orelse ""),
138 .for_build = for_build,143 .for_build = for_build,
139 .parent_id = mapping.get_string("id") orelse "",
140 });144 });
141 }145 }
142 }146 }
src/util/module.zig+11-5
...@@ -13,7 +13,7 @@ const common = @import("./../common.zig");...@@ -13,7 +13,7 @@ const common = @import("./../common.zig");
1313
14pub const Module = struct {14pub const Module = struct {
15 type: zigmod.Dep.Type,15 type: zigmod.Dep.Type,
16 id: string,16 id: [48]u8,
17 name: string,17 name: string,
18 main: string,18 main: string,
19 c_include_dirs: []const string = &.{},19 c_include_dirs: []const string = &.{},
...@@ -28,6 +28,8 @@ pub const Module = struct {...@@ -28,6 +28,8 @@ pub const Module = struct {
28 for_build: bool = false,28 for_build: bool = false,
29 min_zig_version: ?std.SemanticVersion,29 min_zig_version: ?std.SemanticVersion,
3030
31 pub const ROOT: [48]u8 = ("root" ++ (" " ** 44)).*;
32
31 pub fn from(alloc: std.mem.Allocator, dep: zigmod.Dep, cachepath: string, options: *common.CollectOptions) !Module {33 pub fn from(alloc: std.mem.Allocator, dep: zigmod.Dep, cachepath: string, options: *common.CollectOptions) !Module {
32 var moddeps = std.ArrayList(Module).init(alloc);34 var moddeps = std.ArrayList(Module).init(alloc);
33 errdefer moddeps.deinit();35 errdefer moddeps.deinit();
...@@ -37,9 +39,13 @@ pub const Module = struct {...@@ -37,9 +39,13 @@ pub const Module = struct {
37 try moddeps.append(founddep);39 try moddeps.append(founddep);
38 }40 }
39 }41 }
42
43 var id = dep.id;
44 if (std.mem.eql(u8, &id, &zigmod.Dep.EMPTY)) id = u.random_string(48);
45
40 return Module{46 return Module{
41 .type = dep.type,47 .type = dep.type,
42 .id = if (dep.id.len > 0) dep.id else &u.random_string(48),48 .id = id,
43 .name = dep.name,49 .name = dep.name,
44 .main = dep.main,50 .main = dep.main,
45 .c_include_dirs = dep.c_include_dirs,51 .c_include_dirs = dep.c_include_dirs,
...@@ -57,7 +63,7 @@ pub const Module = struct {...@@ -57,7 +63,7 @@ pub const Module = struct {
57 }63 }
5864
59 pub fn eql(self: Module, another: Module) bool {65 pub fn eql(self: Module, another: Module) bool {
60 return std.mem.eql(u8, self.id, another.id);66 return std.mem.eql(u8, &self.id, &another.id);
61 }67 }
6268
63 pub fn get_hash(self: Module, alloc: std.mem.Allocator, cdpath: string) !string {69 pub fn get_hash(self: Module, alloc: std.mem.Allocator, cdpath: string) !string {
...@@ -131,8 +137,8 @@ pub const Module = struct {...@@ -131,8 +137,8 @@ pub const Module = struct {
131 return false;137 return false;
132 }138 }
133139
134 pub fn short_id(self: Module) string {140 pub fn short_id(self: *const Module) string {
135 return u.slice(u8, self.id, 0, 12);141 return u.slice(u8, &self.id, 0, @min(12, std.mem.indexOfScalar(u8, &self.id, ' ') orelse self.id.len));
136 }142 }
137143
138 pub fn minZigVersion(self: Module) ?std.SemanticVersion {144 pub fn minZigVersion(self: Module) ?std.SemanticVersion {