Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
5.7 kB · 217 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218import albedo_bytes/scalarsimport albedo_sdk/extensionimport albedo_sdk/local_command.{type Runner}import albedo_sdk/locationimport albedo_sdk/worktreeimport gleam/boolimport gleam/listimport gleam/option.{type Option, None, Some}import gleam/resultimport gleam/string
const git_suffix = "/.git"
pub fn extension() -> extension.Extension { extension.Extension( "git", "Git worktrees for isolated parallel work.", [], [extension.WorktreePlugin(provider())], extension.no_initialise, tables: [], uses: [], )}
pub fn provider() -> worktree.Provider { worktree.Provider( kind: "git", detect: detect, create: create, list: list_worktrees, remove: remove, )}
fn detect(run: Runner, dir: String) -> Option(worktree.Repo) { let start = location.normalise_absolute_path(dir) use checkout <- option.map( worktree.find_checkout(start, fn(dir) { let dot_git = worktree.join_path(dir, ".git") worktree.is_dir(dot_git) || worktree.is_file(dot_git) }), ) worktree.Repo( kind: "git", root: main_root(run, checkout), checkout: checkout, subdir: worktree.relative_subdir(checkout, start), )}
fn main_root(run: Runner, checkout: String) -> String { git( run, ["rev-parse", "--path-format=absolute", "--git-common-dir"], checkout, ) |> result.map(string.trim) |> result.try(fn(common) { worktree.strip_suffix(common, git_suffix) |> option.to_result("") }) |> result.unwrap(checkout)}
fn git(run: Runner, args: List(String), cwd: String) -> Result(String, String) { worktree.exec(run, "git", ["--no-optional-locks", ..args], cwd, 10_000)}
fn create( run: Runner, repo: worktree.Repo, spec: worktree.CreateSpec,) -> Result(#(Option(String), Option(String)), String) { let branch = worktree.present(spec.branch) let revision = case worktree.present(spec.revision) { Some(rev) -> [rev] None -> [] } let args = case branch, revision { Some(name), [] -> case git( run, ["show-ref", "--verify", "--quiet", "refs/heads/" <> name], repo.checkout, ) { Ok(_) -> ["worktree", "add", spec.path, name] Error(_) -> ["worktree", "add", "-b", name, spec.path] } Some(name), _ -> ["worktree", "add", "-b", name, spec.path, ..revision] None, _ -> ["worktree", "add", "--detach", spec.path, ..revision] } use _ <- result.try(worktree.exec(run, "git", args, repo.checkout, 30_000)) let head = git(run, ["rev-parse", "--short", "HEAD"], spec.path) |> option.from_result Ok(#(worktree.present(head), branch))}
fn list_worktrees( run: Runner, repo: worktree.Repo,) -> Result(List(worktree.VcsEntry), String) { git(run, ["worktree", "list", "--porcelain", "-z"], repo.root) |> result.map(fn(output) { string.split(output, "\u{0}") |> split_blocks([], []) |> list.index_map(fn(block, index) { entry(repo, block, index) }) |> option.values })}
fn split_blocks( fields: List(String), current: List(String), blocks: List(List(String)),) -> List(List(String)) { case fields { [] -> case current { [] -> list.reverse(blocks) _ -> list.reverse([list.reverse(current), ..blocks]) } ["", ..rest] -> split_blocks(rest, current, blocks) [field, ..rest] -> case string.starts_with(field, "worktree "), current { True, [_, ..] -> split_blocks(rest, [field], [list.reverse(current), ..blocks]) _, _ -> split_blocks(rest, [field, ..current], blocks) } }}
fn field(block: List(String), key: String) -> Option(String) { list.find_map(block, fn(line) { case string.split_once(line, " ") { Ok(#(name, value)) if name == key -> Ok(value) _ -> Error(Nil) } }) |> option.from_result}
/// git lists the main worktree first.fn entry( repo: worktree.Repo, block: List(String), index: Int,) -> Option(worktree.VcsEntry) { use path <- option.map(field(block, "worktree")) let name = case index == 0 || path == repo.root { True -> "default" False -> string.split(path, "/") |> list.last |> result.unwrap(path) } worktree.VcsEntry( name: name, path: Some(path), revision: field(block, "HEAD") |> option.map(scalars.take(_, 8)), branch: field(block, "branch") |> option.map(fn(ref) { case ref { "refs/heads/" <> name -> name _ -> ref } }), )}
fn remove( run: Runner, repo: worktree.Repo, _name: String, path: String, force: Bool,) -> Result(Nil, String) { use <- bool.guard( !worktree.is_dir(path), git(run, ["worktree", "prune"], repo.root) |> result.replace(Nil), ) use _ <- result.try(case force { True -> Ok(Nil) False -> guard_detached_commits(run, path) }) let args = case force { True -> ["worktree", "remove", "--force", path] False -> ["worktree", "remove", path] } worktree.exec(run, "git", args, repo.root, 15_000) |> result.replace(Nil)}
/// git worktree remove silently drops commits that only a detached HEAD points at.fn guard_detached_commits(run: Runner, path: String) -> Result(Nil, String) { let contained = git( run, [ "for-each-ref", "--contains", "HEAD", "--format=%(refname)", "refs/heads", "refs/tags", ], path, ) case contained { Ok(refs) -> case string.trim(refs) { "" -> Error( "worktree has unmerged detached commits at HEAD; create a branch or merge them, or pass force=True", ) _ -> Ok(Nil) } Error(_) -> Ok(Nil) }}