Files
ziran/cmd/package_lock.zi
T
waozi f40b9875eb Publish package cache entries atomically and lock each entry
Parallel ziran processes resolving one locked package cloned straight
into the shared cache path; the loser's clone failed and left an entry
every later build refused. Clone into a private <id>.tmp- sibling,
check it out, then publish with a rename that never replaces, and hold
<id>.lock while cloning, validating, repairing or fetching an entry.
2026-11-07 15:41:28 -03:00

1375 lines
56 KiB
Ziran

Diagnostics :: #import "diagnostics"
#import "package_common"
#import "package_manifest"
#import "package_source"
#import "json_scan"
#import "file_linux"
#import "byte_text_linux"
#import "c_string"
#scope_export
PkgLocked :: struct {
id: string
name: string
url: string
ref: string
commit: string
path: string
source: bool
edges: [24]PkgNamedValue
edge_count: s32
refs: [24]PkgNamedValue
ref_count: s32
manifest: PkgManifest
}
PkgLock :: struct {
packages: [64]PkgLocked
count: s32
root_name: string
root_edges: [24]PkgNamedValue
root_edge_count: s32
root_refs: [24]PkgNamedValue
root_ref_count: s32
tool_url: string
tool_ref: string
tool_commit: string
tool_path: string
valid: bool
}
PkgPending :: struct {
owner: s32
alias: string
url: string
ref: string
source: bool
refresh: bool
}
PkgBuilder :: struct {
bytes: []u8
at: s64
valid: bool
}
PkgNewLock :: () -> *PkgLock {
return cast(*PkgLock)(PkgAllocZero(cast(u64)size_of(PkgLock)))
}
PkgJsonText :: (json: string, at: s32) -> string {
span: JsonSpan = ReadString(json, at)
if !span.valid { return "" }
return json[span.start:span.end]
}
PkgJsonField :: (json: string, object: s32, name: string) -> string {
return PkgJsonText(json, FieldAt(json, object, name))
}
PkgParseEdges :: (json: string, object: s32, output: *PkgNamedValue,
count: *s32) -> bool {
if object < 0 || object >= cast(s32)json.count || json[object] != cast(u8)123 {
return false
}
cursor: s32 = Space(json, object + 1)
while cursor < cast(s32)json.count && json[cursor] != cast(u8)125 {
if count.* >= 24 { return false }
key: JsonSpan = ReadString(json, cursor)
if !key.valid { return false }
cursor = Space(json, key.after)
if cursor >= cast(s32)json.count || json[cursor] != cast(u8)58 { return false }
cursor = Space(json, cursor + 1)
value: string = PkgJsonText(json, cursor)
if value.count == 0 { return false }
output[count.*].name = json[key.start:key.end]
output[count.*].value = value
count.* += 1
cursor = ValueEnd(json, cursor, 0)
if cursor < 0 { return false }
cursor = Space(json, cursor)
if cursor < cast(s32)json.count && json[cursor] == cast(u8)44 {
cursor = Space(json, cursor + 1)
} else if cursor < cast(s32)json.count && json[cursor] != cast(u8)125 {
return false
}
}
return cursor < cast(s32)json.count && json[cursor] == cast(u8)125
}
PkgReadLock :: (path: string, output: *PkgLock) -> bool {
content: PkgReadResult = PkgRead(path)
if !content.valid { return false }
json: string = content.text
start: s32 = Space(json, 0)
if start < 0 || start >= cast(s32)json.count ||
json[start] != cast(u8)123 { return false }
format_at: s32 = FieldAt(json, 0, "format")
if format_at < 0 || json[format_at] != cast(u8)49 { return false }
packages_at: s32 = FieldAt(json, 0, "packages")
count: s32 = ArrayCount(json, packages_at)
if count < 0 || count > 64 { return false }
index: s32 = 0
while index < count {
item: s32 = ElementAt(json, packages_at, index)
if item < 0 { return false }
locked: *PkgLocked = *output.packages[index]
locked.id = PkgJsonField(json, item, "id")
locked.name = PkgJsonField(json, item, "name")
locked.url = PkgJsonField(json, item, "url")
locked.ref = PkgJsonField(json, item, "ref")
locked.commit = PkgReadIdentity(json, item)
if locked.id.count == 0 || locked.name.count == 0 ||
locked.url.count == 0 || locked.ref.count == 0 ||
locked.commit.count == 0 ||
!PkgParseEdges(json, FieldAt(json, item, "dependencies"),
*locked.edges[0], *locked.edge_count) { return false }
refs_at: s32 = FieldAt(json, item, "dependency_refs")
if refs_at >= 0 && !PkgParseEdges(json, refs_at, *locked.refs[0],
*locked.ref_count) { return false }
source_at: s32 = FieldAt(json, item, "source")
if source_at >= 0 {
if MatchLiteral(json, source_at, "true") { locked.source = true }
else if !MatchLiteral(json, source_at, "false") { return false }
if locked.source && locked.edge_count > 0 { return false }
}
index += 1
}
output.count = count
root: s32 = FieldAt(json, 0, "root")
output.root_name = PkgJsonField(json, root, "name")
if output.root_name.count == 0 || !PkgParseEdges(json,
FieldAt(json, root, "dependencies"), *output.root_edges[0],
*output.root_edge_count) { return false }
refs_at: s32 = FieldAt(json, root, "dependency_refs")
if refs_at >= 0 && !PkgParseEdges(json, refs_at, *output.root_refs[0],
*output.root_ref_count) { return false }
tool: s32 = FieldAt(json, 0, "toolchain")
output.tool_url = PkgJsonField(json, tool, "url")
output.tool_ref = PkgJsonField(json, tool, "ref")
output.tool_commit = PkgReadIdentity(json, tool)
if output.tool_url.count == 0 || output.tool_ref.count == 0 ||
output.tool_commit.count == 0 { return false }
output.valid = true
return true
}
PkgEdgeValue :: (edges: *PkgNamedValue, count: s32, name: string) -> string {
index: s32 = 0
while index < count {
if edges[index].name == name { return PkgJoin("", edges[index].value) }
index += 1
}
return ""
}
PkgFindById :: (locked: *PkgLock, id: string) -> s32 {
index: s32 = 0
while index < locked.count {
if locked.packages[index].id == id { return index }
index += 1
}
return -1
}
// A declaration matches its locked package by the same ref, or by a full
// commit naming the commit that package is locked at: one manifest may follow
// a branch while another pins the commit that branch locked.
PkgRefMatches :: (locked: *PkgLocked, ref: string) -> bool {
if PkgValidCommit(ref) { return locked.commit == ref }
return locked.ref == ref
}
// Overrides select a source, not a particular spelling of its incoming alias.
// A root override therefore also applies to transitive pins of that URL.
PkgSourceOverride :: (overrides: *PkgOverrides, current: *PkgManifest,
graph: *PkgLock, url: string, alias: string, root: string) -> string {
if overrides.count == 0 { return "" }
result: string = PkgFindOverride(overrides, alias, root)
index: s32 = 0
while index < current.dependency_count {
dep: *PkgDependency = *current.dependencies[index]
if PkgSameSource(dep.git, url) {
path: string = PkgFindOverride(overrides, dep.alias, root)
if path.count > 0 {
if result.count > 0 && result != path {
unused PkgFail("conflicting local overrides for one package source")
return "!"
}
result = path
}
}
index += 1
}
index = 0
while index < graph.count {
item: *PkgLocked = *graph.packages[index]
if PkgSameSource(item.url, url) {
path: string = PkgFindOverride(overrides, item.name, root)
owner: s32 = -1
while owner < graph.count {
count: s32 = graph.root_edge_count
edges: *PkgNamedValue = *graph.root_edges[0]
if owner >= 0 {
count = graph.packages[owner].edge_count
edges = *graph.packages[owner].edges[0]
}
edge: s32 = 0
while edge < count {
if edges[edge].value == item.id {
candidate: string = PkgFindOverride(overrides, edges[edge].name, root)
if candidate.count > 0 {
if path.count > 0 && path != candidate { return "!" }
path = candidate
}
}
edge += 1
}
owner += 1
}
if path.count > 0 {
if result.count > 0 && result != path { return "!" }
result = path
}
}
index += 1
}
return result
}
// Keep the declared ref separate from the selected source commit. In local
// development several exact declarations may intentionally select one HEAD.
// The saved edge still detects ref drift; only an explicit source override
// permits a declaration that does not name the selected locked revision.
PkgEdgeRefMatches :: (target: *PkgLocked, ref: string, saved: string,
override: string) -> bool {
if saved.count > 0 && saved != ref { return false }
return PkgRefMatches(target, ref) || (saved == ref && override.count > 0)
}
// A source checkout and a package of one repository commit are separate lock
// entries: a project may pin a game package and also read that game's files
// as a source checkout. The source entry's id names its kind.
PkgLockId :: (url: string, commit: string, source: bool) -> string {
if source { return PkgSourceId(PkgJoin(PkgSourceKey(url), "\nsource"), commit) }
return PkgSourceId(PkgSourceKey(url), commit)
}
// Locks written before source entries had their own id keep the package id
// form; that id is unambiguous while no package entry shares it.
PkgLockIdMatches :: (locked: *PkgLock, index: s32) -> bool {
item: *PkgLocked = *locked.packages[index]
if item.id == PkgLockId(item.url, item.commit, item.source) { return true }
if !item.source || item.id != PkgLockId(item.url, item.commit, false) { return false }
other: s32 = 0
while other < locked.count {
if other != index && locked.packages[other].id == item.id { return false }
other += 1
}
return true
}
PkgFindBySource :: (locked: *PkgLock, url: string, ref: string, source: bool) -> s32 {
index: s32 = 0
while index < locked.count {
if locked.packages[index].ref == ref &&
locked.packages[index].source == source &&
PkgSameSource(locked.packages[index].url, url) {
return index
}
index += 1
}
return -1
}
PkgValidCommit :: (commit: string) -> bool {
if commit.count != 40 { return false }
index: s64 = 0
while index < 40 {
ch: u8 = commit[index]
if !((ch >= cast(u8)48 && ch <= cast(u8)57) ||
(ch >= cast(u8)97 && ch <= cast(u8)102)) { return false }
index += 1
}
return true
}
// Development identities are path-scoped, explicitly uncommitted revisions.
// They never pass PkgValidCommit or enter the immutable source cache.
PkgValidDevelopment :: (identity: string) -> bool {
if identity.count != 71 || identity[0:7] != "unborn:" { return false }
index: s64 = 7
while index < identity.count {
ch: u8 = identity[index]
if !((ch >= cast(u8)48 && ch <= cast(u8)57) ||
(ch >= cast(u8)97 && ch <= cast(u8)102)) { return false }
index += 1
}
return true
}
PkgReadIdentity :: (json: string, object: s32) -> string {
commit: string = PkgJsonField(json, object, "commit")
development: string = PkgJsonField(json, object, "development")
if FieldAt(json, object, "development") < 0 && PkgValidCommit(commit) {
return commit
}
if FieldAt(json, object, "commit") < 0 && PkgValidDevelopment(development) {
return development
}
return ""
}
// The commit checked out at directory, or "" unless directory is the top
// of its own checkout: inside another repository, such as an uninitialized
// submodule, HEAD is the enclosing repository's commit. --show-prefix is
// empty only at the top, so the trimmed output is then the commit alone.
PkgGitHead :: (directory: string) -> string {
args: [6]string
args[0] = "git"
args[1] = "-C"
args[2] = directory
args[3] = "rev-parse"
args[4] = "--show-prefix"
args[5] = "HEAD^{commit}"
result: PkgProcessResult = PkgCapture(args[0:6], "")
if result.code != 0 || !PkgValidCommit(result.text) { return "" }
return PkgJoin("", result.text)
}
// Only an explicit override may select an unborn checkout. Verify the actual
// canonical root, then a symbolic HEAD whose branch ref is genuinely absent;
// a corrupt/detached HEAD must not be mistaken for an unborn repository.
PkgOverrideIdentity :: (directory: string) -> string {
commit: string = PkgGitHead(directory)
if commit.count > 0 { return commit }
args: [6]string
args[0] = "git"
args[1] = "-C"
args[2] = directory
args[3] = "rev-parse"
args[4] = "--show-toplevel"
result: PkgProcessResult = PkgCapture(args[0:5], "")
canonical: string = PkgCanonical(directory)
if result.code != 0 || canonical.count == 0 || result.text != canonical {
unused PkgFail(PkgJoin("override is not the top of a Git checkout: ", directory))
return ""
}
args[3] = "symbolic-ref"
args[4] = "--quiet"
args[5] = "HEAD"
result = PkgCapture(args[:], "")
if result.code == 0 && result.text.count > 11 && result.text[0:11] == "refs/heads/" {
args[3] = "show-ref"
args[4] = "--verify"
args[5] = PkgJoin("", result.text)
// show-ref without --quiet reports a missing ref with status 128;
// --quiet distinguishes absence (1) from corrupt Git metadata (128).
quiet: [7]string
index: s32 = 0
while index < 6 { quiet[index] = args[index]; index += 1 }
quiet[6] = "--quiet"
if PkgCapture(quiet[:], "").code == 1 {
return PkgJoin("unborn:", PkgDigest(canonical))
}
}
unused PkgFail(PkgJoin("override has no valid commit or unborn branch: ", directory))
return ""
}
// Runs a Git command whose argument at url_index names a repository. When
// that transport fails, such as SSH without a key in CI or HTTPS without
// credentials for a private repository, the same repository is tried once
// over the other transport. Neither attempt prompts for a password.
PkgRemoteCapture :: (args: []string, url_index: s32) -> PkgProcessResult {
names: [13]string
values: [13]string
count: s32 = PkgQuietGitEnv(names[:], values[:])
names[count] = "GIT_TERMINAL_PROMPT"
values[count] = "0"
count += 1
url: string = args[url_index]
result: PkgProcessResult = PkgCaptureEnv(args, "", names[0:count], values[0:count])
alternate: string = PkgAlternateURL(url)
if result.code == 0 || alternate.count == 0 { return result }
args[url_index] = alternate
retried: PkgProcessResult = PkgCaptureEnv(args, "", names[0:count], values[0:count])
args[url_index] = url
if retried.code == 0 { return retried }
PkgShowToolFailure(result.text)
return result
}
PkgRemoteCommit :: (url: string, ref: string) -> string {
if !PkgGitURL(url) { return "" }
// A full commit hash names itself; ls-remote only matches branches and tags.
if PkgValidCommit(ref) { return ref }
args: [4]string
args[0] = "git"
args[1] = "ls-remote"
args[2] = url
args[3] = ref
result: PkgProcessResult = PkgRemoteCapture(args[:], 2)
if result.code != 0 || result.text.count < 40 { return "" }
head_ref: string = ref
tag_ref: string = ref
if ref.count < 5 || ref[0:5] != "refs/" {
head_ref = PkgJoin("refs/heads/", ref)
tag_ref = PkgJoin("refs/tags/", ref)
} else if ref.count >= 10 && ref[0:10] == "refs/tags/" {
head_ref = ""
} else if ref.count >= 11 && ref[0:11] == "refs/heads/" {
tag_ref = ""
}
peeled_ref: string = PkgJoin(tag_ref, "^{}")
head: string = ""
tag: string = ""
peeled: string = ""
at: s64 = 0
while at < cast(s64)result.text.count {
end: s64 = at
while end < cast(s64)result.text.count &&
result.text[end] != cast(u8)10 { end += 1 }
if end - at < 41 || result.text[at + 40] != cast(u8)9 { return "" }
commit: string = result.text[at:at + 40]
if !PkgValidCommit(commit) { return "" }
name: string = result.text[at + 41:end]
if name == head_ref {
if head.count > 0 && head != commit { return "" }
head = commit
} else if name == tag_ref {
if tag.count > 0 && tag != commit { return "" }
tag = commit
} else if name == peeled_ref {
if peeled.count > 0 && peeled != commit { return "" }
peeled = commit
}
at = end + 1
}
if head.count > 0 { return PkgJoin("", head) }
if peeled.count > 0 { return PkgJoin("", peeled) }
return PkgJoin("", tag)
}
PkgCacheRoot :: () -> string {
cache: string = PkgGetenv("XDG_CACHE_HOME")
if cache.count == 0 { cache = PkgJoin(PkgGetenv("HOME"), "/.cache") }
return PkgJoin(cache, "/ziran")
}
// Only resolver-owned, direct cache entries with their own .git directory
// may be materialized. Never follow a source-entry or .git redirection.
PkgCheckoutAt :: (destination: string, commit: string) -> bool {
if !PkgValidCommit(commit) { return false }
prefix: string = PkgJoin(PkgCacheRoot(), "/sources/")
if destination.count <= prefix.count || destination[0:prefix.count] != prefix {
return false
}
leaf: string = destination[prefix.count:]
for ch: leaf { if ch == cast(u8)47 { return false } }
sources: string = PkgCanonical(PkgJoin(PkgCacheRoot(), "/sources"))
canonical: string = PkgCanonical(destination)
if sources.count == 0 || canonical != PkgJoin3(sources, "/", leaf) ||
!DirectoryExists(PkgJoin(destination, "/.git")) ||
PathExists(PkgJoin(destination, "/.git/commondir")) ||
PkgCanonical(PkgJoin(destination, "/.git")) != PkgJoin(canonical, "/.git") {
return false
}
args: [9]string
args[0] = "git"
args[1] = "-C"
args[2] = destination
args[3] = "rev-parse"
args[4] = "--show-toplevel"
top: PkgProcessResult = PkgCapture(args[0:5], "")
if top.code != 0 || PkgCanonical(top.text) != canonical { return false }
args[3] = "config"
args[4] = "--get"
args[5] = "core.worktree"
if PkgCapture(args[0:6], "").text.count > 0 { return false }
args[4] = "--bool"
args[5] = "--get"
args[6] = "core.sparseCheckout"
sparse: PkgProcessResult = PkgCapture(args[0:7], "")
if sparse.code != 1 && (sparse.code != 0 || sparse.text != "false") { return false }
// Sparse and assume-unchanged index flags can hide absent source files.
// Porcelain quotes embedded newlines; bounded capture must not silently
// accept a partial index or status listing.
args[3] = "ls-files"
args[4] = "-v"
flags: PkgProcessResult = PkgCaptureRaw(args[0:5], "")
if flags.code != 0 || flags.text.count >= 1048575 { return false }
at: s64 = 0
while at < cast(s64)flags.text.count {
if flags.text[at] != cast(u8)72 { return false }
while at < cast(s64)flags.text.count && flags.text[at] != cast(u8)10 { at += 1 }
at += 1
}
args[3] = "-c"
args[4] = "core.fsmonitor=false"
args[5] = "status"
args[6] = "--porcelain=v1"
args[7] = "--untracked-files=all"
args[8] = "--ignored=matching"
status: PkgProcessResult = PkgCaptureRaw(args[:], "")
if status.code != 0 || status.text.count >= 1048575 { return false }
missing: bool = false
local_data: bool = false
at = 0
while at < cast(s64)status.text.count {
end: s64 = at
while end < cast(s64)status.text.count && status.text[end] != cast(u8)10 { end += 1 }
if end - at < 4 { return false }
kind: string = status.text[at:at + 2]
if kind == "??" || kind == "!!" { local_data = true }
else if kind == " D" || kind == "D " { missing = true }
else { return false }
at = end + 1
}
head: string = PkgGitHead(destination)
if head == commit && !missing { return true }
// Even ignored files may obstruct restoration or be overwritten by Git.
// Refuse ambiguous damage rather than resetting or cleaning local data.
if local_data { return false }
if head != commit {
args[3] = "checkout"
args[4] = "--quiet"
args[5] = "--detach"
args[6] = commit
if PkgCapture(args[0:7], "").code != 0 { return false }
}
if missing {
args[3] = "restore"
args[4] = PkgJoin("--source=", commit)
args[5] = "--staged"
args[6] = "--worktree"
args[7] = "--"
args[8] = "."
if PkgCapture(args[:], "").code != 0 { return false }
}
args[3] = "status"
args[4] = "--porcelain=v1"
args[5] = "--untracked-files=no"
status = PkgCapture(args[0:6], "")
return status.code == 0 && status.text.count == 0 && PkgGitHead(destination) == commit
}
// Never delete an entry after a failed checkout: it may contain owner data
// or be an unsupported checkout. A later command must not call it ready.
PkgDropCheckout :: (destination: string) {
unused PkgFail(PkgJoin("package cache incomplete, modified or unsupported; preserved at ", destination))
}
#scope_file
libc :: #system_library "libc";
PkgMkdtempRaw :: (template: *u8) -> *u8 #foreign libc "mkdtemp";
PkgFlockRaw :: (fd: s32, operation: s32) -> s32 #foreign libc "flock";
PkgFcntlRaw :: (fd: s32, command: s32, value: ..any) -> s32 #foreign libc "fcntl";
#scope_export
// Takes the exclusive advisory lock at PATH, creating the file. Git children
// must not inherit it: a detached gc or maintenance run would keep the lock.
PkgLockEntry :: (path: string) -> FileHandle {
lock: FileHandle = OpenUpdate(path)
// F_SETFD, FD_CLOEXEC and LOCK_EX are the same on Linux and Darwin.
if lock.valid && (PkgFcntlRaw(lock.fd, 2, 1) != 0 || PkgFlockRaw(lock.fd, 2) != 0) {
unused CloseFile(lock)
lock.valid = false
}
return lock
}
// Closing the descriptor releases its lock.
PkgUnlockEntry :: (lock: FileHandle) {
if lock.valid { unused CloseFile(lock) }
}
// A new private directory named "<id>.tmp-XXXXXX" beside the entries.
PkgTemporaryEntry :: (sources: string, id: string) -> string {
name: [4096]u8
if !CopyCString(PkgJoin3(sources, "/", PkgJoin(id, ".tmp-XXXXXX")), name[:]) ||
PkgMkdtempRaw(*name[0]) == null { return "" }
return PkgJoin("", TextFromCString(*name[0]))
}
// Removes a temporary directory this process created, never an entry.
PkgRemoveTemporary :: (path: string) {
args: [4]string
args[0] = "rm"
args[1] = "-rf"
args[2] = "--"
args[3] = path
unused PkgCapture(args[:], "")
}
// Concurrent commands share one cache. A fresh entry is cloned into a private
// "<id>.tmp-" sibling and published with a rename that never replaces, so a
// half-written clone is never at the entry path. "<id>.lock" serializes
// cloning, validation, repair and fetching of one entry. Only the exact
// entry name is ever read, so a crashed run's temporary sibling or lock file
// is never mistaken for an entry.
PkgCheckout :: (url: string, commit: string, offline: bool) -> string {
if !PkgGitURL(url) || !PkgValidCommit(commit) { return "" }
sources: string = PkgJoin(PkgCacheRoot(), "/sources")
id: string = PkgSourceId(PkgSourceKey(url), commit)
destination: string = PkgJoin3(sources, "/", id)
if !PathExists(destination) && (offline || !PkgMkdirs(sources)) { return "" }
// An unwritable cache cannot take the lock; it then cannot be modified
// either, and the non-replacing rename still keeps publication safe.
lock: FileHandle = PkgLockEntry(PkgJoin(destination, ".lock"))
path: string = ""
if PathExists(destination) { path = PkgUseEntry(destination, url, commit, offline) }
else if !offline { path = PkgPublishEntry(sources, id, destination, url, commit) }
PkgUnlockEntry(lock)
return path
}
// Validates a published entry and repairs or fetches it only when healthy.
PkgUseEntry :: (destination: string, url: string, commit: string, offline: bool) -> string {
origin: [6]string
origin[0] = "git"
origin[1] = "-C"
origin[2] = destination
origin[3] = "config"
origin[4] = "--get-all"
origin[5] = "remote.origin.url"
actual: PkgProcessResult = PkgCapture(origin[:], "")
if actual.code != 0 || !PkgSameSource(actual.text, url) {
PkgDropCheckout(destination)
return ""
}
head: string = PkgGitHead(destination)
if (head == commit || !offline) && PkgCheckoutAt(destination, commit) {
return destination
}
// Only fetch into a validated, healthy cache. Existing objects repair
// missing payload offline; network access is needed only for an absent
// target commit. Never fetch into an arbitrary or modified checkout.
if !offline && head != commit && PkgCheckoutAt(destination, head) {
fetch: [5]string
fetch[0] = "git"
fetch[1] = "-C"
fetch[2] = destination
fetch[3] = "fetch"
fetch[4] = url
if PkgRemoteCapture(fetch[:], 4).code == 0 &&
PkgCheckoutAt(destination, commit) { return destination }
}
PkgDropCheckout(destination)
return ""
}
// Clones into a private sibling, checks it out and publishes it. A failed
// attempt removes only its own temporary directory. When another process
// published first, its entry is validated like any existing one.
PkgPublishEntry :: (sources: string, id: string, destination: string, url: string,
commit: string) -> string {
temporary: string = PkgTemporaryEntry(sources, id)
if temporary.count == 0 {
unused PkgFail(PkgJoin("cannot create a temporary package checkout in ", sources))
return ""
}
args: [6]string
args[0] = "git"
args[1] = "clone"
args[2] = "--quiet"
args[3] = "--no-checkout"
args[4] = url
args[5] = temporary
if PkgRemoteCapture(args[:], 4).code != 0 || !PkgCheckoutAt(temporary, commit) {
PkgRemoveTemporary(temporary)
unused PkgFail(PkgJoin3("cannot check out ", url, PkgJoin(" at ", commit)))
return ""
}
if RenameNoReplace(temporary, destination) { return destination }
PkgRemoveTemporary(temporary)
if !PathExists(destination) {
unused PkgFail(PkgJoin("cannot publish package cache entry ", destination))
return ""
}
return PkgUseEntry(destination, url, commit, false)
}
PkgAppend :: (builder: *PkgBuilder, value: string) -> void {
if !builder.valid { return }
next: s64 = PkgCopy(builder.bytes, builder.at, value)
if next < 0 { builder.valid = false; return }
builder.at = next
}
PkgAppendJson :: (builder: *PkgBuilder, value: string) -> void {
PkgAppend(builder, "\"")
index: s64 = 0
while index < cast(s64)value.count {
ch: u8 = value[index]
if ch == cast(u8)34 { PkgAppend(builder, "\\\"") }
else if ch == cast(u8)92 { PkgAppend(builder, "\\\\") }
else if ch == cast(u8)10 { PkgAppend(builder, "\\n") }
else if ch < cast(u8)32 { builder.valid = false }
else { PkgAppend(builder, value[index:index + 1]) }
index += 1
}
PkgAppend(builder, "\"")
}
PkgWriteEdges :: (builder: *PkgBuilder, edges: *PkgNamedValue, count: s32) -> void {
PkgAppend(builder, "{")
written: s32 = 0
while written < count {
best: s32 = -1
index: s32 = 0
while index < count {
earlier: s32 = 0
other: s32 = 0
while other < count {
if PkgLexLess(edges[other].name, edges[index].name) { earlier += 1 }
other += 1
}
if earlier == written { best = index; break }
index += 1
}
if best < 0 { builder.valid = false; return }
if written > 0 { PkgAppend(builder, ", ") }
PkgAppendJson(builder, edges[best].name)
PkgAppend(builder, ": ")
PkgAppendJson(builder, edges[best].value)
written += 1
}
PkgAppend(builder, "}")
}
PkgWriteRefs :: (builder: *PkgBuilder, locked: *PkgLock,
edges: *PkgNamedValue, count: s32, refs: *PkgNamedValue, ref_count: s32) -> void {
values: [24]PkgNamedValue
index: s32 = 0
while index < count {
values[index].name = edges[index].name
values[index].value = PkgEdgeValue(refs, ref_count, edges[index].name)
if values[index].value.count == 0 {
target: s32 = PkgFindById(locked, edges[index].value)
if target < 0 { builder.valid = false; return }
values[index].value = locked.packages[target].ref
}
index += 1
}
PkgWriteEdges(builder, *values[0], count)
}
PkgWriteLock :: (root: string, locked: *PkgLock) -> bool {
memory: *u8 = PkgAlloc(4194304)
if memory == null { return false }
builder: PkgBuilder
builder.bytes = BytesFromPointer(memory, 4194304)
builder.valid = true
PkgAppend(*builder, "{\n \"format\": 1,\n \"packages\": [\n")
written: s32 = 0
while written < locked.count {
best: s32 = -1
index: s32 = 0
while index < locked.count {
earlier: s32 = 0
other: s32 = 0
while other < locked.count {
if PkgLexLess(locked.packages[other].id,
locked.packages[index].id) {
earlier += 1
}
other += 1
}
if earlier == written { best = index; break }
index += 1
}
if best < 0 { return false }
item: *PkgLocked = *locked.packages[best]
if written > 0 { PkgAppend(*builder, ",\n") }
PkgAppend(*builder, " {\n ")
if PkgValidDevelopment(item.commit) { PkgAppend(*builder, "\"development\": ") }
else { PkgAppend(*builder, "\"commit\": ") }
PkgAppendJson(*builder, item.commit)
PkgAppend(*builder, ",\n \"dependencies\": ")
PkgWriteEdges(*builder, *item.edges[0], item.edge_count)
if item.ref_count > 0 {
PkgAppend(*builder, ",\n \"dependency_refs\": ")
PkgWriteRefs(*builder, locked, *item.edges[0], item.edge_count,
*item.refs[0], item.ref_count)
}
PkgAppend(*builder, ",\n \"id\": ")
PkgAppendJson(*builder, item.id)
PkgAppend(*builder, ",\n \"name\": ")
PkgAppendJson(*builder, item.name)
PkgAppend(*builder, ",\n \"ref\": ")
PkgAppendJson(*builder, item.ref)
if item.source { PkgAppend(*builder, ",\n \"source\": true") }
PkgAppend(*builder, ",\n \"url\": ")
PkgAppendJson(*builder, item.url)
PkgAppend(*builder, "\n }")
written += 1
}
PkgAppend(*builder, "\n ],\n \"root\": {\n \"dependencies\": ")
PkgWriteEdges(*builder, *locked.root_edges[0], locked.root_edge_count)
if locked.root_ref_count > 0 {
PkgAppend(*builder, ",\n \"dependency_refs\": ")
PkgWriteRefs(*builder, locked, *locked.root_edges[0], locked.root_edge_count,
*locked.root_refs[0], locked.root_ref_count)
}
PkgAppend(*builder, ",\n \"name\": ")
PkgAppendJson(*builder, locked.root_name)
PkgAppend(*builder, "\n },\n \"toolchain\": {\n ")
if PkgValidDevelopment(locked.tool_commit) { PkgAppend(*builder, "\"development\": ") }
else { PkgAppend(*builder, "\"commit\": ") }
PkgAppendJson(*builder, locked.tool_commit)
PkgAppend(*builder, ",\n \"ref\": ")
PkgAppendJson(*builder, locked.tool_ref)
PkgAppend(*builder, ",\n \"url\": ")
PkgAppendJson(*builder, locked.tool_url)
PkgAppend(*builder, "\n }\n}\n")
if !builder.valid { return false }
path: string = PkgJoin(root, "/ziran.lock")
temporary: string = PkgJoin(root, "/ziran.lock.tmp")
if !PkgWrite(temporary, TextFromBytes(builder.bytes[0:builder.at])) { return false }
return RenamePath(temporary, path)
}
PkgResolveLock :: (root: string, refresh: string) -> bool {
current: *PkgManifest = PkgNewManifest()
old: *PkgLock = PkgNewLock()
resolved: *PkgLock = PkgNewLock()
overrides: *PkgOverrides = PkgNewOverrides()
if current == null || old == null || resolved == null || overrides == null {
return false
}
if !PkgParseManifest(PkgJoin(root, "/ziran.toml"), current) ||
!PkgParseOverrides(PkgJoin(root, "/ziran.local.toml"), overrides) { return false }
PkgReadLock(PkgJoin(root, "/ziran.lock"), old)
targeted: bool = refresh != "" && refresh != "*" && refresh != "ziran"
if targeted && old.valid && PkgValidDevelopment(old.tool_commit) {
unused PkgFail("targeted update cannot preserve an unborn toolchain snapshot; run ziran lock or ziran update")
return false
}
if refresh == "ziran" {
if !old.valid || current.name != old.root_name ||
!PkgSameSource(current.tool_git, old.tool_url) ||
current.tool_ref != old.tool_ref ||
current.dependency_count != old.root_edge_count {
PkgFail("toolchain update needs a matching ziran.lock")
return false
}
index: s32 = 0
while index < current.dependency_count {
dep: *PkgDependency = *current.dependencies[index]
id: string = PkgEdgeValue(*old.root_edges[0],
old.root_edge_count, dep.alias)
target: s32 = PkgFindById(old, id)
if target < 0 || !PkgSameSource(old.packages[target].url, dep.git) ||
!PkgEdgeRefMatches(*old.packages[target], dep.ref,
PkgEdgeValue(*old.root_refs[0], old.root_ref_count, dep.alias),
PkgSourceOverride(overrides, current, old, dep.git, dep.alias, root)) ||
old.packages[target].source != dep.source {
PkgFail("dependencies differ from ziran.lock; run ziran lock")
return false
}
index += 1
}
old.tool_commit = PkgRemoteCommit(old.tool_url, old.tool_ref)
if !PkgValidCommit(old.tool_commit) {
PkgFail("cannot resolve Ziran toolchain ref")
return false
}
return PkgWriteLock(root, old)
}
return PkgResolveGraph(root, refresh, false, current, old, overrides, resolved) &&
PkgWriteLock(root, resolved)
}
// Resolve the declared graph into resolved, keeping the pins of old where a
// declaration is unchanged. Offline resolution only accepts exact commits for
// declarations that ziran.lock does not pin yet.
PkgResolveGraph :: (root: string, refresh: string, offline: bool,
current: *PkgManifest, old: *PkgLock, overrides: *PkgOverrides,
resolved: *PkgLock) -> bool {
targeted: bool = refresh != "" && refresh != "*" && refresh != "ziran"
if refresh != "" && refresh != "*" && refresh != "ziran" {
found: bool = false
index: s32 = 0
while index < current.dependency_count {
if PkgFoldEqual(current.dependencies[index].alias, refresh) { found = true }
index += 1
}
if !found { return false }
}
pending: [256]PkgPending
pending_count: s32 = 0
index: s32 = 0
while index < current.dependency_count {
dependency: *PkgDependency = *current.dependencies[index]
pending[pending_count].owner = -1
pending[pending_count].alias = dependency.alias
pending[pending_count].url = dependency.git
pending[pending_count].ref = dependency.ref
pending[pending_count].source = dependency.source
pending[pending_count].refresh = refresh == "*" ||
(refresh != "" && PkgFoldEqual(refresh, dependency.alias))
pending_count += 1
index += 1
}
if refresh != "" && refresh != "*" && refresh != "ziran" {
index = 0
while index < pending_count {
if pending[index].refresh {
first: PkgPending = pending[0]
pending[0] = pending[index]
pending[index] = first
break
}
index += 1
}
}
while pending_count > 0 {
next: PkgPending = pending[0]
index = 1
while index < pending_count {
pending[index - 1] = pending[index]
index += 1
}
pending_count -= 1
found: s32 = PkgFindBySource(resolved, next.url, next.ref, next.source)
if found < 0 {
if resolved.count >= 64 { return false }
found = resolved.count
item: *PkgLocked = *resolved.packages[found]
item.url = next.url
item.ref = next.ref
override: string = PkgSourceOverride(overrides, current, old,
next.url, next.alias, root)
previous: s32 = PkgFindBySource(old, next.url, next.ref, next.source)
// A targeted update reads an unrelated package's pinned manifest,
// even when its development override has newer or dirty inputs.
if targeted && old.valid && previous >= 0 && !next.refresh {
if PkgValidDevelopment(old.packages[previous].commit) {
unused PkgFail("targeted update cannot preserve an unborn package snapshot; run ziran lock or ziran update")
return false
}
item.commit = old.packages[previous].commit
item.path = PkgCheckout(next.url, item.commit, offline)
} else if override.count > 0 {
item.commit = PkgOverrideIdentity(override)
item.path = override
if item.commit.count == 0 {
return false
}
} else {
if previous >= 0 && PkgValidCommit(old.packages[previous].commit) && !next.refresh &&
(!PkgValidCommit(next.ref) || old.packages[previous].commit == next.ref) {
item.commit = old.packages[previous].commit
} else if offline && !PkgValidCommit(next.ref) {
unused PkgFail(PkgJoin3("cannot resolve ", next.alias,
PkgJoin3(" ", next.ref, " offline; ziran.lock does not pin it")))
return false
} else {
item.commit = PkgRemoteCommit(next.url, next.ref)
}
item.path = PkgCheckout(next.url, item.commit, offline)
}
if (!PkgValidCommit(item.commit) && !PkgValidDevelopment(item.commit)) || item.path.count == 0 {
return false
}
// Manifests may name one commit by different refs, a branch in
// one and that branch's commit in another. They are one package:
// a second copy would give the lock two entries with one id. The
// entry keeps the branch; pins match it by commit (PkgRefMatches).
source_id: string = PkgLockId(item.url, item.commit, next.source)
same: s32 = PkgFindById(resolved, source_id)
if same >= 0 {
found = same
if PkgValidCommit(resolved.packages[same].ref) && !PkgValidCommit(next.ref) {
resolved.packages[same].ref = next.ref
}
} else {
// A source package is a pinned checkout for platform builds. It
// has no manifest, modules, or dependencies of its own.
item.source = next.source
if item.source {
item.name = PkgJoin("", next.alias)
} else {
if !PkgParseManifest(PkgJoin(item.path, "/ziran.toml"),
*item.manifest) { return false }
item.name = item.manifest.name
}
item.id = source_id
resolved.count += 1
index = 0
while index < item.manifest.dependency_count {
if pending_count >= 256 { return false }
child: *PkgDependency = *item.manifest.dependencies[index]
entry: PkgPending
entry.owner = found
entry.alias = child.alias
entry.url = child.git
entry.ref = child.ref
entry.source = child.source
entry.refresh = next.refresh
if next.refresh {
shift: s32 = pending_count
while shift > index {
pending[shift] = pending[shift - 1]
shift -= 1
}
pending[index] = entry
} else {
pending[pending_count] = entry
}
pending_count += 1
index += 1
}
}
}
if resolved.packages[found].source != next.source { return false }
if next.owner < 0 {
if resolved.root_edge_count >= 24 { return false }
resolved.root_edges[resolved.root_edge_count].name = next.alias
resolved.root_edges[resolved.root_edge_count].value = resolved.packages[found].id
resolved.root_refs[resolved.root_ref_count].name = next.alias
resolved.root_refs[resolved.root_ref_count].value = next.ref
resolved.root_ref_count += 1
resolved.root_edge_count += 1
} else {
parent: *PkgLocked = *resolved.packages[next.owner]
if parent.edge_count >= 24 { return false }
parent.edges[parent.edge_count].name = next.alias
parent.edges[parent.edge_count].value = resolved.packages[found].id
parent.refs[parent.ref_count].name = next.alias
parent.refs[parent.ref_count].value = next.ref
parent.ref_count += 1
parent.edge_count += 1
}
}
if !PkgGitURL(current.tool_git) { return false }
resolved.root_name = current.name
resolved.tool_url = current.tool_git
resolved.tool_ref = current.tool_ref
tool_override: string = PkgFindOverride(overrides, "ziran", root)
if targeted && old.valid && PkgValidCommit(old.tool_commit) && PkgSameSource(old.tool_url, resolved.tool_url) &&
old.tool_ref == resolved.tool_ref {
resolved.tool_commit = old.tool_commit
} else if tool_override.count > 0 {
resolved.tool_commit = PkgOverrideIdentity(tool_override)
if resolved.tool_commit.count == 0 {
return false
}
} else if old.valid && PkgValidCommit(old.tool_commit) && PkgSameSource(old.tool_url, resolved.tool_url) &&
old.tool_ref == resolved.tool_ref && refresh != "*" &&
refresh != "ziran" {
resolved.tool_commit = old.tool_commit
} else if offline {
unused PkgFail("cannot resolve the Ziran toolchain offline; ziran.lock does not pin it")
return false
} else {
resolved.tool_commit = PkgRemoteCommit(resolved.tool_url, resolved.tool_ref)
}
return PkgValidCommit(resolved.tool_commit) || PkgValidDevelopment(resolved.tool_commit)
}
// Validation uses the saved graph. Only afterwards substitute actual local
// source identities in memory, merging aliases that now select one checkout.
// No lock is rewritten by a read/build command.
PkgApplyOverrideIdentities :: (root: string, overrides: *PkgOverrides,
current: *PkgManifest, locked: *PkgLock) -> bool {
index: s32 = 0
while index < locked.count {
item: *PkgLocked = *locked.packages[index]
path: string = PkgSourceOverride(overrides, current, locked,
item.url, item.name, root)
if path.count > 0 {
commit: string = PkgOverrideIdentity(path)
if commit.count == 0 { return false }
old_id: string = item.id
item.commit = commit
item.id = PkgLockId(item.url, commit, item.source)
owner: s32 = -1
while owner < locked.count {
edges: *PkgNamedValue = *locked.root_edges[0]
count: s32 = locked.root_edge_count
if owner >= 0 {
edges = *locked.packages[owner].edges[0]
count = locked.packages[owner].edge_count
}
edge: s32 = 0
while edge < count {
if edges[edge].value == old_id { edges[edge].value = item.id }
edge += 1
}
owner += 1
}
}
index += 1
}
index = 0
while index < locked.count {
other: s32 = index + 1
while other < locked.count {
if locked.packages[other].id == locked.packages[index].id {
if locked.packages[other].source != locked.packages[index].source ||
locked.packages[other].path != locked.packages[index].path ||
locked.packages[other].name != locked.packages[index].name ||
locked.packages[other].edge_count != locked.packages[index].edge_count { return false }
edge: s32 = 0
while edge < locked.packages[index].edge_count {
expected: *PkgNamedValue = *locked.packages[index].edges[edge]
if PkgEdgeValue(*locked.packages[other].edges[0],
locked.packages[other].edge_count, expected.name) != expected.value { return false }
edge += 1
}
shift: s32 = other + 1
while shift < locked.count {
locked.packages[shift - 1] = locked.packages[shift]
shift += 1
}
locked.count -= 1
} else { other += 1 }
}
index += 1
}
return true
}
// Names the package, where its manifest was read and what disagrees with the
// lock, so a stale local override is not reported as a bare fetch failure.
PkgLockMismatch :: (item: *PkgLocked, override: string, detail: string) -> bool {
source: string = PkgJoin("package cache ", item.path)
if override.count > 0 { source = PkgJoin("local override ", override) }
unused PkgFail(PkgJoin(PkgJoin3(item.name, PkgJoin3(" (", source, ") "), detail),
"; run `ziran lock` or commit the override"))
return false
}
PkgCheckoutGraph :: (root: string, locked: *PkgLock, offline: bool,
locked_mode: bool, current: *PkgManifest) -> bool {
return PkgCheckoutLockedGraph(root, locked, offline, locked_mode, current, true)
}
// A local override is the developer's working source: when its declared
// dependencies differ from the inventory ziran.lock records, resolve the graph
// from the actual manifests in memory, as `ziran lock` would, without writing
// the lock. Packages without an override, the root manifest and --locked builds
// keep the exact lock check. On failure locked is unchanged and the caller
// reports the original mismatch.
PkgCheckoutOverrideGraph :: (root: string, locked: *PkgLock, offline: bool,
current: *PkgManifest, overrides: *PkgOverrides, name: string) -> bool {
old: *PkgLock = PkgNewLock()
resolved: *PkgLock = PkgNewLock()
if old == null || resolved == null ||
!PkgReadLock(PkgJoin(root, "/ziran.lock"), old) ||
!PkgResolveGraph(root, "", offline, current, old, overrides, resolved) ||
!PkgCheckoutLockedGraph(root, resolved, offline, false, current, false) {
return false
}
if !Diagnostics.IsJson() {
PkgPrint(PkgJoin3("ziran: note: the local override of ", name,
" declares dependencies ziran.lock does not record; resolved them for this build (run `ziran lock` before committing)\n"), true)
}
index: s32 = 0
while index < resolved.count {
locked.packages[index] = resolved.packages[index]
index += 1
}
index = 0
while index < resolved.root_edge_count {
locked.root_edges[index] = resolved.root_edges[index]
index += 1
}
index = 0
while index < resolved.root_ref_count {
locked.root_refs[index] = resolved.root_refs[index]
index += 1
}
locked.count = resolved.count
locked.root_name = resolved.root_name
locked.root_edge_count = resolved.root_edge_count
locked.root_ref_count = resolved.root_ref_count
locked.tool_url = resolved.tool_url
locked.tool_ref = resolved.tool_ref
locked.tool_commit = resolved.tool_commit
locked.tool_path = resolved.tool_path
locked.valid = true
return true
}
PkgCheckoutLockedGraph :: (root: string, locked: *PkgLock, offline: bool,
locked_mode: bool, current: *PkgManifest, resolve: bool) -> bool {
overrides: *PkgOverrides = PkgNewOverrides()
if overrides == null ||
!PkgParseOverrides(PkgJoin(root, "/ziran.local.toml"), overrides) ||
!PkgParseManifest(PkgJoin(root, "/ziran.toml"), current) { return false }
if locked_mode && overrides.count > 0 {
PkgFail("local overrides are not allowed with --locked")
return false
}
if locked_mode {
development: bool = PkgValidDevelopment(locked.tool_commit)
index: s32 = 0
while index < locked.count {
development = development || PkgValidDevelopment(locked.packages[index].commit)
index += 1
}
if development {
unused PkgFail("development-only ziran.lock cannot be used with --locked; run ziran lock without overrides")
return false
}
}
if current.name != locked.root_name ||
current.dependency_count != locked.root_edge_count ||
!PkgSameSource(current.tool_git, locked.tool_url) ||
current.tool_ref != locked.tool_ref {
PkgFail("dependencies differ from ziran.lock; run ziran lock")
return false
}
index: s32 = 0
while index < current.dependency_count {
dep: *PkgDependency = *current.dependencies[index]
id: string = PkgEdgeValue(*locked.root_edges[0], locked.root_edge_count,
dep.alias)
found: s32 = PkgFindById(locked, id)
if found < 0 || !PkgSameSource(locked.packages[found].url, dep.git) ||
!PkgEdgeRefMatches(*locked.packages[found], dep.ref,
PkgEdgeValue(*locked.root_refs[0], locked.root_ref_count, dep.alias),
PkgSourceOverride(overrides, current, locked, dep.git, dep.alias, root)) ||
locked.packages[found].source != dep.source {
PkgFail("dependencies differ from ziran.lock; run ziran lock")
return false
}
index += 1
}
// Validate every saved identity before selecting another entry's graph.
index = 0
while index < locked.count {
if !PkgLockIdMatches(locked, index) { return false }
index += 1
}
index = 0
while index < locked.count {
item: *PkgLocked = *locked.packages[index]
override: string = PkgSourceOverride(overrides, current, locked,
item.url, item.name, root)
if override.count > 0 {
commit: string = PkgOverrideIdentity(override)
if commit.count == 0 { return false }
// Several locked revisions can select one checkout. Validate its
// declarations against the graph locked for that actual identity,
// rather than an older revision's dependency inventory. Keep the
// original node identity until incoming declarations are checked.
selected: s32 = PkgFindById(locked, PkgLockId(item.url, commit, item.source))
// A newer local commit has no entry of its own: the root project's
// dependency selects this source for every incoming edge, so all
// locked revisions are checked against the graph locked for it.
if selected < 0 { selected = PkgRootSelection(locked, item.url, item.source) }
if selected >= 0 && selected != index {
item.edges = locked.packages[selected].edges
item.edge_count = locked.packages[selected].edge_count
item.refs = locked.packages[selected].refs
item.ref_count = locked.packages[selected].ref_count
}
}
item.path = override
if item.path.count == 0 {
if PkgValidDevelopment(item.commit) {
unused PkgFail("development-only package requires an explicit local override; run ziran lock")
return false
}
item.path = PkgCheckout(item.url, item.commit, offline)
}
if item.path.count == 0 { return false }
if item.source {
if item.edge_count != 0 { return false }
index += 1
continue
}
if !PkgParseManifest(PkgJoin(item.path, "/ziran.toml"), *item.manifest) {
return PkgLockMismatch(item, override, "has no readable ziran.toml")
}
if item.manifest.name != item.name {
return PkgLockMismatch(item, override,
PkgJoin3("declares package ", item.manifest.name,
PkgJoin(", but ziran.lock records ", item.name)))
}
if item.manifest.dependency_count != item.edge_count {
if resolve && override.count > 0 &&
PkgCheckoutOverrideGraph(root, locked, offline, current, overrides, item.name) { return true }
return PkgLockMismatch(item, override, "declares a different number of dependencies than ziran.lock records")
}
child: s32 = 0
while child < item.manifest.dependency_count {
dep: *PkgDependency = *item.manifest.dependencies[child]
id: string = PkgEdgeValue(*item.edges[0], item.edge_count, dep.alias)
target: s32 = PkgFindById(locked, id)
if target < 0 {
if resolve && override.count > 0 &&
PkgCheckoutOverrideGraph(root, locked, offline, current, overrides, item.name) { return true }
return PkgLockMismatch(item, override,
PkgJoin3("declares ", dep.alias, ", but ziran.lock has no pin for it"))
}
if !PkgSameSource(locked.packages[target].url, dep.git) ||
!PkgEdgeRefMatches(*locked.packages[target], dep.ref,
PkgEdgeValue(*item.refs[0], item.ref_count, dep.alias),
PkgSourceOverride(overrides, current, locked, dep.git, dep.alias, root)) ||
locked.packages[target].source != dep.source {
if resolve && override.count > 0 &&
PkgCheckoutOverrideGraph(root, locked, offline, current, overrides, item.name) { return true }
return PkgLockMismatch(item, override,
PkgJoin(PkgJoin(PkgJoin3("declares ", dep.alias, " "),
PkgJoin3(dep.ref, " from ", dep.git)),
PkgJoin(PkgJoin3(", but ziran.lock pins ", locked.packages[target].name, " "),
PkgJoin3(locked.packages[target].commit, " from ", locked.packages[target].url))))
}
child += 1
}
index += 1
}
locked.tool_path = PkgFindOverride(overrides, "ziran", root)
if locked.tool_path.count == 0 {
if PkgValidDevelopment(locked.tool_commit) {
unused PkgFail("development-only toolchain requires an explicit local override; run ziran lock")
return false
}
locked.tool_path = PkgCheckout(locked.tool_url, locked.tool_commit, offline)
} else {
locked.tool_commit = PkgOverrideIdentity(locked.tool_path)
if locked.tool_commit.count == 0 { return false }
}
return locked.tool_path.count > 0 &&
PkgApplyOverrideIdentities(root, overrides, current, locked)
}
// The locked entry a root dependency selects for one source, or -1.
PkgRootSelection :: (locked: *PkgLock, url: string, source: bool) -> s32 {
index: s32 = 0
while index < locked.root_edge_count {
target: s32 = PkgFindById(locked, locked.root_edges[index].value)
if target >= 0 && locked.packages[target].source == source &&
PkgSameSource(locked.packages[target].url, url) { return target }
index += 1
}
return -1
}
PkgSelect :: (locked: *PkgLock, selector: string) -> s32 {
index: s32 = 0
while index < locked.root_edge_count {
if PkgFoldEqual(locked.root_edges[index].name, selector) {
return PkgFindById(locked, locked.root_edges[index].value)
}
index += 1
}
found: s32 = -1
index = 0
while index < locked.count {
if PkgFoldEqual(locked.packages[index].name, selector) {
if found >= 0 { return -1 }
found = index
}
index += 1
}
return found
}