Luce packages
LuceLuce BaseLuciaOS

dymokomi/luc

Source of release 0.24.3

0.24.3 / src / release.lucb

## Releasing without tokens.
##
## `luc publish -m "notes"` publishes the version in package.prisma. The registry is for
## installing and updating; GitHub keeps the history. So publish sends one snapshot: the
## files of HEAD without the development paths (`tests`, `dev` and those the package
## declares, which installs leave out anyway), as a commit with no parents whose message
## names HEAD (`Source-Commit:`), under the release tag `v<version>` with the notes. No
## branch and no history go up, and HEAD must already be on GitHub (`origin`). It creates
## the registry repository if needed and points the `pkg` remote at it; the tag is local
## only while it is pushed. The registry turns the tag into the release.
##
## `luc git-credential` is a Git credential helper. publish configures it for the
## registry only: the helper mints a five-minute git:write password from the saved
## session for the push. The registry takes release tags only; there is nothing to fetch.
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[], environment: const str[]? = none) -> 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)
    var count: usize = 0
    if let overrides = environment: count = overrides.length
    let variables = try new c.str[count] --- in memory.heap
    defer free(variables) in memory.heap
    var set: usize
    defer release_arguments(variables, &set)
    if let overrides = environment:
        while set < overrides.length:
            variables[set] = (c.str)try strings.copy(overrides[set])
            set += 1
    let ran = try process.run((c.str)"git", argv, (c.str)directory, variables if count > 0 else none)
    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, environment: const str[]? = none) -> !:
    var result = try git(root, arguments, environment)
    defer result.close()
    if result.status == 0: return
    try io.stderr().write(result.err)
    error(invalid, what)

## The tree of HEAD without the development paths, written through a scratch index so
## the working tree, the real index and the refs stay as they are; its id in `id`.
func snapshot_tree(root: str, definition: pkg.Definition*, id: u8[40]*) -> !:
    var located = try git(root, ["rev-parse", "--absolute-git-dir"])
    defer located.close()
    if located.status != 0: error(invalid, "could not find the Git directory")
    var index_buffer: u8[1200]
    let index = try strings.format(index_buffer, f"GIT_INDEX_FILE={strings.trim(located.out)}/luc-publish-index")
    let environment = [index]
    try must(root, ["read-tree", "HEAD"], "could not read HEAD's tree", environment)
    # luce-pkg's default development paths, then the package's own
    var removed: str[18]
    removed[0] = "tests"
    removed[1] = "dev"
    for i in 0..<definition.development_count: removed[2 + i] = definition.development[i]
    var arguments: str[24]
    let leading = ["rm", "--cached", "-r", "-q", "--ignore-unmatch", "--"]
    for i in 0..<leading.length: arguments[i] = leading[i]
    let count = 2 + definition.development_count
    for i in 0..<count: arguments[leading.length + i] = removed[i]
    try must(root, arguments[..<leading.length + count], "could not leave out the development paths", environment)
    var written = try git(root, ["write-tree"], environment)
    defer written.close()
    var cleared = try git(root, ["read-tree", "--empty"], environment)
    cleared.close()
    if written.status != 0:
        try io.stderr().write(written.err)
        error(invalid, "could not write the release's tree")
    let tree = strings.trim(written.out)
    if tree.length != 40: error(invalid, "Git wrote an unexpected tree id")
    for i in 0..<(usize)40: (*id)[i] = tree.bytes[i]

## `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")

    # GitHub keeps the history: what is released must already be there.
    var fetched = try git(root, ["fetch", "-q", "origin"])
    let reachable = fetched.status == 0
    fetched.close()
    if not reachable: error(invalid, "could not fetch origin, the package's GitHub repository; publish needs HEAD pushed there first")
    var holding = try git(root, ["branch", "-r", "--contains", "HEAD"])
    defer holding.close()
    var on_github = false
    let branches = try strings.split(holding.out, '\n')
    defer free(branches)
    for line in branches:
        if strings.starts_with(strings.trim(line), "origin/"): on_github = true
    if not on_github: error(invalid, "HEAD is not on GitHub yet: push it to origin first (GitHub keeps the history; the registry gets the release)")
    var head = try git(root, ["rev-parse", "HEAD"])
    defer head.close()
    let source = strings.trim(head.out)
    if source.length != 40: error(invalid, "could not read HEAD's commit")

    var tree: u8[40]
    try snapshot_tree(root, &definition, &tree)
    var title_buffer: u8[200]
    let title = try strings.format(title_buffer, f"{definition.name} {definition.version}")
    var trailer_buffer: u8[64]
    let trailer = try strings.format(trailer_buffer, f"Source-Commit: {source}")
    var snapshot = try git(root, ["commit-tree", (str)tree[0..], "-m", title, "-m", trailer])
    defer snapshot.close()
    if snapshot.status != 0:
        try io.stderr().write(snapshot.err)
        error(invalid, "could not make the release commit")
    let commit = strings.trim(snapshot.out)

    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 must(root, ["tag", "-a", tag, commit, "-m", notes], "could not create the release tag")
    # The tag lives on the registry only: the local one goes whatever the push did.
    var pushed = try git(root, ["push", "pkg", try strings.format(tag_ref, f"refs/tags/{tag}")])
    defer pushed.close()
    var dropped = try git(root, ["tag", "-d", tag])
    dropped.close()
    if pushed.status != 0:
        # The registry's reason is in Git's output.
        try io.stderr().write(pushed.err)
        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