diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index c35093216172b9fa0f3cacba28c7707362991055..4d99099af660c3dcd0ccfd157b81f263e21d2127 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -285,7 +285,7 @@ fn diff_printchange(comptime testt: string, comptime replacement: string, item: fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module, alloc: std.mem.Allocator) !void { for (list) |mod| { if (mod.type == .system_lib or mod.type == .framework) continue; - if (std.mem.eql(u8, mod.id, "root")) { + if (std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) { try w.writeAll(" pub const _root = \"\";\n"); continue; } @@ -325,7 +325,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Modul mod.short_id(), mod.short_id(), }); - if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) { + if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) { try w.print( \\ .import = .{{ "{s}", .{{ .path = dirs._{s} ++ "/{s}" }} }}, \\ diff --git a/src/cmd/generate.zig b/src/cmd/generate.zig index f33a146ca1ebd36a7abbdb2f3b4db634be29a21c..8807b50970feacfa5f4181b555609ba7a5acd8b1 100644 --- a/src/cmd/generate.zig +++ b/src/cmd/generate.zig @@ -284,7 +284,7 @@ fn print_pkg_data_to(w: std.fs.File.Writer, alloc: std.mem.Allocator, cachepath: .hg => @panic("TODO"), .http => @panic("TODO"), } - if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) { + if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) { try w.print(" .name = \"{s}\",\n", .{mod.name}); try w.print(" .entry = \"/{}/{s}/{s}\",\n", .{ std.zig.fmtEscapes(fixed_path), try mod.pin(alloc, cachepath), mod.main }); diff --git a/src/common.zig b/src/common.zig index 5bbe38bdbe90d1b141acf3ae207b82d9ea810724..39f194f573d61bcc3435956f2622ffb75932d415 100644 --- a/src/common.zig +++ b/src/common.zig @@ -46,7 +46,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO } return zigmod.Module{ .type = .local, - .id = "root", + .id = zigmod.Module.ROOT, .name = "root", .main = m.main, .deps = try moduledeps.toOwnedSlice(), @@ -230,7 +230,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO .system_lib, .framework => { return zigmod.Module{ .type = d.type, - .id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Sha3_384, d.path), + .id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Shake(96), d.path), .name = d.path, .only_os = d.only_os, .except_os = d.except_os, @@ -274,7 +274,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO dd.for_build = d.for_build; const save = dd; if (d.type != .local) dd.clean_path = extras.trimPrefix(modpath, cachepath)[1..]; - if (dd.id.len == 0) dd.id = &u.random_string(48); + if (std.mem.eql(u8, &dd.id, &zigmod.Dep.EMPTY)) dd.id = u.random_string(48); if (d.name.len > 0) dd.name = d.name; if (d.main.len > 0) dd.main = d.main; 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 .type = std.meta.stringToEnum(zigmod.Dep.Type, iter.next().?).?, .path = iter.next().?, .version = iter.next().?, - .id = "", + .id = zigmod.Dep.EMPTY, .name = "", .main = "", .yaml = null, .deps = &.{}, - .parent_id = "", }; try list.append([4]string{ try asdep.clean_path(alloc), diff --git a/src/util/dep.zig b/src/util/dep.zig index e2c9ed154b09a49121e1de3f6cd6d0aa8cdd1bde..532c09f81af04f1b348ce4063bbb6557d4dcf13a 100644 --- a/src/util/dep.zig +++ b/src/util/dep.zig @@ -15,7 +15,7 @@ pub const Dep = struct { type: Type, path: string, - id: string, + id: [48]u8, name: string, main: string, version: string, @@ -28,7 +28,8 @@ pub const Dep = struct { deps: []zigmod.Dep, keep: bool = false, for_build: bool = false, - parent_id: string, + + pub const EMPTY = (" " ** 48).*; pub const Type = @import("./dep_type.zig").DepType; diff --git a/src/util/funcs.zig b/src/util/funcs.zig index 94d6004a37ed79ffe0d29cd5844a5944c47d8e0f..dcc238cdfe41473275c1dd3d6d969ac08787aae3 100644 --- a/src/util/funcs.zig +++ b/src/util/funcs.zig @@ -132,9 +132,9 @@ pub fn validate_hash(alloc: std.mem.Allocator, input: string, file_path: string) const data = try file.reader().readAllAlloc(alloc, gb); const expected = hash.string; const actual = switch (hash.id) { - .blake3 => try do_hash(alloc, std.crypto.hash.Blake3, data), - .sha256 => try do_hash(alloc, std.crypto.hash.sha2.Sha256, data), - .sha512 => try do_hash(alloc, std.crypto.hash.sha2.Sha512, data), + .blake3 => &try do_hash(alloc, std.crypto.hash.Blake3, data), + .sha256 => &try do_hash(alloc, std.crypto.hash.sha2.Sha256, data), + .sha512 => &try do_hash(alloc, std.crypto.hash.sha2.Sha512, data), }; const result = std.mem.startsWith(u8, actual, expected); if (!result) { @@ -143,13 +143,13 @@ pub fn validate_hash(alloc: std.mem.Allocator, input: string, file_path: string) return result; } -pub fn do_hash(alloc: std.mem.Allocator, comptime algo: type, data: string) !string { +pub fn do_hash(alloc: std.mem.Allocator, comptime algo: type, data: string) ![algo.digest_length * 2]u8 { var h = algo.init(.{}); var out: [algo.digest_length]u8 = undefined; h.update(data); h.final(&out); const hex = try std.fmt.allocPrint(alloc, "{x}", .{std.fmt.fmtSliceHexLower(out[0..])}); - return hex; + return hex[0 .. algo.digest_length * 2].*; } /// Returns the result of running `git rev-parse HEAD` diff --git a/src/util/modfile.zig b/src/util/modfile.zig index 8025a0e7703d32c583778e5db14a39c18a0d59e9..d5ce967013841afa07ac88910e8ec7c032678f91 100644 --- a/src/util/modfile.zig +++ b/src/util/modfile.zig @@ -15,7 +15,7 @@ const mb = kb * 1024; pub const ModFile = struct { const Self = @This(); - id: string, + id: [48]u8, name: string, main: string, c_include_dirs: []const string, @@ -52,7 +52,9 @@ pub const ModFile = struct { } pub fn from_mapping(alloc: std.mem.Allocator, mapping: yaml.Mapping) !Self { - const id = mapping.get_string("id") orelse ""; + var id = zigmod.Dep.EMPTY; + std.mem.copyForwards(u8, &id, mapping.get_string("id") orelse &u.random_string(48)); + const name = mapping.get_string("name") orelse ""; const main = mapping.get_string("main") orelse ""; @@ -61,7 +63,7 @@ pub const ModFile = struct { } return Self{ - .id = if (id.len == 0) &u.random_string(48) else id, + .id = id, .name = name, .main = main, .c_include_dirs = try mapping.get_string_array(alloc, "c_include_dirs"), @@ -120,10 +122,13 @@ pub const ModFile = struct { } } + var id = zigmod.Dep.EMPTY; + std.mem.copyForwards(u8, &id, item.mapping.get_string("id") orelse ""); + try dep_list.append(zigmod.Dep{ .type = dep_type, .path = path, - .id = item.mapping.get_string("id") orelse "", + .id = id, .name = name, .main = main, .version = version.?, @@ -136,7 +141,6 @@ pub const ModFile = struct { .deps = try dep_list_by_name(alloc, item.mapping, &.{"dependencies"}, for_build), .keep = std.mem.eql(u8, "true", item.mapping.get_string("keep") orelse ""), .for_build = for_build, - .parent_id = mapping.get_string("id") orelse "", }); } } diff --git a/src/util/module.zig b/src/util/module.zig index 9f2ff87a6735641eb43ae6d74970f7eb09492fa1..61a7872fdd2b76ff8cadeffa3b1fe2afbd39bd9d 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -13,7 +13,7 @@ const common = @import("./../common.zig"); pub const Module = struct { type: zigmod.Dep.Type, - id: string, + id: [48]u8, name: string, main: string, c_include_dirs: []const string = &.{}, @@ -28,6 +28,8 @@ pub const Module = struct { for_build: bool = false, min_zig_version: ?std.SemanticVersion, + pub const ROOT: [48]u8 = ("root" ++ (" " ** 44)).*; + pub fn from(alloc: std.mem.Allocator, dep: zigmod.Dep, cachepath: string, options: *common.CollectOptions) !Module { var moddeps = std.ArrayList(Module).init(alloc); errdefer moddeps.deinit(); @@ -37,9 +39,13 @@ pub const Module = struct { try moddeps.append(founddep); } } + + var id = dep.id; + if (std.mem.eql(u8, &id, &zigmod.Dep.EMPTY)) id = u.random_string(48); + return Module{ .type = dep.type, - .id = if (dep.id.len > 0) dep.id else &u.random_string(48), + .id = id, .name = dep.name, .main = dep.main, .c_include_dirs = dep.c_include_dirs, @@ -57,7 +63,7 @@ pub const Module = struct { } pub fn eql(self: Module, another: Module) bool { - return std.mem.eql(u8, self.id, another.id); + return std.mem.eql(u8, &self.id, &another.id); } pub fn get_hash(self: Module, alloc: std.mem.Allocator, cdpath: string) !string { @@ -131,8 +137,8 @@ pub const Module = struct { return false; } - pub fn short_id(self: Module) string { - return u.slice(u8, self.id, 0, 12); + pub fn short_id(self: *const Module) string { + return u.slice(u8, &self.id, 0, @min(12, std.mem.indexOfScalar(u8, &self.id, ' ') orelse self.id.len)); } pub fn minZigVersion(self: Module) ?std.SemanticVersion {