Luce packages
LuceLuce BaseLuciaOS

dymokomi/luc

Source of release 0.24.0

0.24.0 / src / release.lucb

## Releasing without tokens.
##
## `luc publish -m "notes"` pushes the current branch to the registry and publishes the
## version in package.prisma: it creates the registry repository if needed, points the
## `pkg` remote at it, tags `v<version>` with the notes, and pushes the tag. The registry
## turns that tag into the release.
##
## `luc git-credential` is a Git credential helper. publish configures it for the
## registry only, so plain `git push pkg` and `git fetch pkg` need no password either:
## the helper mints a five-minute git:write password from the saved session.
import c
import interop
import io
import memory
from luce_std import process
import strings
from luce_pkg import pkg
import credentials
import project_file
import registry_http

pub let invalid = pkg.invalid

## One git run: its status and output, borrowed from `ran`, which close releases.
struct Output:
    var status: i32
    var out: str
    var err: str
    var ran: interop.Owned[process.Completed]

    func close():
        self.ran.release()

func git(root: str, arguments: const str[]) -> Output!:
    let argv = try new c.str[arguments.length] --- in memory.heap
    defer free(argv) in memory.heap
    var filled: usize
    defer release_arguments(argv, &filled)
    while filled < arguments.length:
        argv[filled] = (c.str)try strings.copy(arguments[filled])
        filled += 1
    let directory = try strings.copy(root)
    defer strings.release(directory)
    let ran = try process.run((c.str)"git", argv, (c.str)directory)
    return Output(status = ran.value.exit_code, out = ran.value.output, err = ran.value.error_output, ran = ran)

func release_arguments(arguments: c.str[], filled: usize*):
    for i in 0..<*filled: strings.release((str)arguments[i])

## Run git and fail with its own message when it does.
func must(root: str, arguments: const str[], what: str) -> !:
    var result = try git(root, arguments)
    defer result.close()
    if result.status == 0: return
    try io.stderr().write(result.err)
    error(invalid, what)

## Push the current branch to the registry. One push carries at most what the registry
## takes in a pack (its objects and bytes are bounded); a history larger than that, a long
## repository published the first time, goes up in steps: the commits the registry lacks,
## oldest first, as many to a push as fit, the step halved each time the registry refuses
## one as too large.
func push_branch(root: str) -> !:
    var whole = try git(root, ["push", "pkg", "HEAD"])
    defer whole.close()
    if whole.status == 0: return
    if not too_large(whole.err):
        try io.stderr().write(whole.err)
        error(invalid, "the push was refused")
    var head = try git(root, ["rev-parse", "--abbrev-ref", "HEAD"])
    defer head.close()
    let branch = strings.trim(head.out)
    var listing = try git(root, ["rev-list", "--reverse", "--first-parent", "HEAD", "--not", "--remotes=pkg"])
    defer listing.close()
    let commits = try lines_of(listing.out)
    defer free(commits) in memory.heap
    var done: usize = 0
    var step = commits.length // 2 if commits.length > 1 else 1
    while done < commits.length:
        let last = (done + step if done + step < commits.length else commits.length) - 1
        var spelled: u8[512]
        let refspec = try strings.format(spelled, f"{commits[last]}:refs/heads/{branch}")
        var pushed = try git(root, ["push", "pkg", refspec])
        defer pushed.close()
        if pushed.status == 0:
            done = last + 1
            continue
        if not too_large(pushed.err) or step == 1:
            try io.stderr().write(pushed.err)
            error(invalid, "one commit is larger than the registry takes in a push" if step == 1 and too_large(pushed.err) else "the push was refused")
        step = step // 2

## Whether the registry refused a push for one of the limits on what a push carries: a
## named limit of its pack, or the request's size, which the server answers with 413 or by
## closing the connection before Git has sent it all (`RPC failed`).
func too_large(err: str) -> bool:
    return strings.contains(err, "limit exceeded") or strings.contains(err, "HTTP 413") or strings.contains(err, "RPC failed")

## The lines of `text`, each a slice of it, in a heap list the caller frees.
func lines_of(text: str) -> str[]!:
    var count: usize = 0
    for byte in text.bytes:
        if byte == '\n': count += 1
    let lines = try new str[count + 1] --- in memory.heap
    var used: usize = 0
    var start: usize = 0
    for i in 0..<text.length:
        if text.bytes[i] != '\n': continue
        if i > start:
            lines[used] = (str)text.bytes[start..<i]
            used += 1
        start = i + 1
    if start < text.length:
        lines[used] = (str)text.bytes[start..]
        used += 1
    return lines[..<used]

## `luc git-credential get|store|erase`, speaking Git's helper protocol on stdin/stdout.
pub func credential_helper(action: str, session: str) -> i32!:
    if action != "get": return 0
    var buffer: u8[4096]
    var reader = io.stdin()
    var size: usize
    while size < buffer.length:
        let n = try reader.read(buffer[size..])
        if n == 0: break
        size += n
    var protocol = ""
    var host = ""
    var path = ""
    var start: usize
    for end in 0..<size:
        if buffer[end] != '\n': continue
        let line = (str)buffer[start..<end]
        start = end + 1
        let (key, value) = strings.split_once(line, "=") else continue
        if key == "protocol": protocol = value
        elif key == "host": host = value
        elif key == "path": path = value
    var origin_buffer: u8[300]
    let origin = try strings.format(origin_buffer, f"{protocol}://{host}")
    # Answer only for a registry, and only for /git/<owner>/<name>; say nothing otherwise.
    if not registry_http.valid(origin) or not strings.starts_with(path, "git/"): return 0
    let (owner, tail) = strings.split_once((str)path.bytes[4..], "/") else return 0
    var name = tail
    for i in 0..<tail.length:
        if tail.bytes[i] == '/':
            name = (str)tail.bytes[..<i]
            break
    if strings.ends_with(name, ".git"): name = (str)name.bytes[..<name.length - 4]
    var password = credentials.write_password(origin, name, session) catch: return 0
    defer password.close()
    try io.stdout().write(f"username={owner}\npassword={(str)try password.bytes()}\n")
    return 0

## Point the `pkg` remote at the registry repository and make Git ask luc, and only luc,
## for its password. The empty first value clears helpers inherited from the system, such
## as the macOS keychain, which would otherwise cache a password that expires in minutes.
func configure(root: str, origin: str, owner: str, name: str) -> !:
    var url_buffer: u8[400]
    let url = try strings.format(url_buffer, f"{origin}/git/{owner}/{name}")
    var existing = try git(root, ["remote", "get-url", "pkg"])
    let present = existing.status == 0
    existing.close()
    if present: try must(root, ["remote", "set-url", "pkg", url], "could not update the pkg remote")
    else: try must(root, ["remote", "add", "pkg", url], "could not add the pkg remote")
    var key_buffer: u8[400]
    let helper = try strings.format(key_buffer, f"credential.{origin}.helper")
    try must(root, ["config", "--local", "--replace-all", helper, ""], "could not configure Git credentials")
    try must(root, ["config", "--local", "--add", helper, "!luc git-credential"], "could not configure Git credentials")
    var path_buffer: u8[400]
    try must(root, ["config", "--local", try strings.format(path_buffer, f"credential.{origin}.useHttpPath"), "true"], "could not configure Git credentials")
    # Send a push as one request with a known length. Above Git's default 1 MiB buffer it
    # would switch to a probe request followed by chunked transfer.
    var buffer_key: u8[400]
    try must(root, ["config", "--local", try strings.format(buffer_key, f"http.{origin}.postBuffer"), "67108864"], "could not configure Git transfer")

pub func publish(root: str, origin: str, session: str, notes: str) -> i32!:
    if strings.trim(notes).length == 0:
        error(invalid, "release notes are required: luc publish -m \"what changed\"")
    var definition = try project_file.read(root)
    defer definition.close()
    var tag_buffer: u8[96]
    let tag = try strings.format(tag_buffer, f"v{definition.version}")

    var inside = try git(root, ["rev-parse", "--is-inside-work-tree"])
    let repository = inside.status == 0
    inside.close()
    if not repository: error(invalid, "this project is not a Git repository; run git init and commit first")
    var state = try git(root, ["status", "--porcelain"])
    let dirty = strings.trim(state.out).length > 0
    state.close()
    if dirty: error(invalid, "there are uncommitted changes; commit them before publishing")
    var tag_ref: u8[128]
    var tagged = try git(root, ["rev-parse", "-q", "--verify", try strings.format(tag_ref, f"refs/tags/{tag}")])
    let exists = tagged.status == 0
    tagged.close()
    if exists: error(invalid, "this version is already tagged; raise `version` in package.prisma and commit")

    if try credentials.ensure_repository(origin, definition.name, session):
        try io.stdout().write(f"created {definition.owner}/{definition.name} on the registry\n")
    try configure(root, origin, definition.owner, definition.name)
    try push_branch(root)
    try must(root, ["tag", "-a", tag, "-m", notes], "could not create the release tag")
    var pushed = try git(root, ["push", "pkg", tag])
    defer pushed.close()
    if pushed.status != 0:
        # The registry's reason is in Git's output. Drop the tag so the release can be retried.
        try io.stderr().write(pushed.err)
        var dropped = try git(root, ["tag", "-d", tag])
        dropped.close()
        error(invalid, "the registry refused the release")
    try io.stdout().write(f"published {definition.owner}/{definition.name} {definition.version}\n{origin}/{definition.owner}/{definition.name}/\n")
    return 0