navid/veins / commit

3da5bf0d7b9c

Navid M <navidmomtahen@gmail.com> · 2026-11-26

parents: 9f12d74de39a

Browse files at this commit · Download source

all: add vn ls subcommand
--- a/veins/src/cli/bookmark_cmd.sx+++ b/veins/src/cli/bookmark_cmd.sx@@ -341,7 +341,7 @@             } else {                 if name.len > 0 {                     msg.error_prefix()-                    stdio.eprint("bmk delete accepts exactly one name\n", {})+                    stdio.eprint("bmk rm accepts exactly one name\n", {})                     return 1                 }                 name = bytes.from_cstr(a)@@ -351,7 +351,7 @@     }     if name.len == 0 {         msg.error_prefix()-        stdio.eprint("bmk delete requires a name\n", {})+        stdio.eprint("bmk rm requires a name\n", {})         return 1     } @@ -415,7 +415,7 @@                 return 1             } else {                 msg.error_prefix()-                stdio.eprint("bmk list takes no operands\n", {})+                stdio.eprint("bmk ls takes no operands\n", {})                 return 1             }         }@@ -528,15 +528,15 @@ }  fn usage() void = trust {-    stdio.puts("usage: vn bmk <set|list|delete> ...")+    stdio.puts("usage: vn bmk <set|mv|ls|rm> ...")     stdio.puts("")     stdio.puts("manage named, movable pointers to commits (branches)")     stdio.puts("")     stdio.puts("subcommands:")     stdio.puts("  set <name> [-rev <rev>]   create or move a bookmark (default: @)")-    stdio.puts("  move <name> [-rev <rev>]  alias of set")-    stdio.puts("  list [-no-col-titles]     list bookmarks")-    stdio.puts("  delete <name>             delete a bookmark")+    stdio.puts("  mv <name> [-rev <rev>]    alias of set")+    stdio.puts("  ls [-no-col-titles]       list bookmarks")+    stdio.puts("  rm <name>                 delete a bookmark") }  pub fn run(args: list[ref u8], start: usize) i32 = trust {@@ -551,7 +551,7 @@             return 1         }     }-    if cmd_is(sub, c"set", 3) || cmd_is(sub, c"move", 4) {+    if cmd_is(sub, c"set", 3) || cmd_is(sub, c"mv", 2) {         val repo_opt = layout.discover(str.from_cstr(c"."))         match repo_opt {             some repo => {@@ -566,7 +566,7 @@             }         }     }-    if cmd_is(sub, c"list", 4) || cmd_is(sub, c"ls", 2) {+    if cmd_is(sub, c"ls", 2) {         val repo_opt = layout.discover(str.from_cstr(c"."))         match repo_opt {             some repo => {@@ -581,7 +581,7 @@             }         }     }-    if cmd_is(sub, c"delete", 6) || cmd_is(sub, c"remove", 6) || cmd_is(sub, c"forget", 6) {+    if cmd_is(sub, c"rm", 2) {         val repo_opt = layout.discover(str.from_cstr(c"."))         match repo_opt {             some repo => {--- a/veins/src/cli/help_cmd.sx+++ b/veins/src/cli/help_cmd.sx@@ -16,6 +16,7 @@     stdio.puts("  git          import from or export to git")     stdio.puts("  co           write a revision into the working directory; alias: checkout")     stdio.puts("  stat         show working-copy changes; alias: status")+    stdio.puts("  ls           list tracked files as a tree")     stdio.puts("  inf          show a repository breakdown")     stdio.puts("  log          show commit history")     stdio.puts("  show         show a commit and its diff")@@ -120,6 +121,18 @@     stdio.puts("show working-copy changes relative to the parent commit")     stdio.puts("")     stdio.puts("aliases: status")+}++fn show_ls() void = trust {+    stdio.puts("usage: vn ls [rev]")+    stdio.puts("")+    stdio.puts("list tracked files as an ASCII tree")+    stdio.puts("")+    stdio.puts("arguments:")+    stdio.puts("  [rev]                     revision to list (default: @)")+    stdio.puts("")+    stdio.puts("options:")+    stdio.puts("  -rev <rev>                revision to list") }  fn show_inf() void = trust {@@ -152,15 +165,15 @@ }  fn show_bookmark() void = trust {-    stdio.puts("usage: vn bmk <set|list|delete> ...")+    stdio.puts("usage: vn bmk <set|mv|ls|rm> ...")     stdio.puts("")     stdio.puts("manage named, movable pointers to commits (branches)")     stdio.puts("")     stdio.puts("subcommands:")     stdio.puts("  set <name> [-rev <rev>]   create or move a bookmark (default: @)")-    stdio.puts("  move <name> [-rev <rev>]  alias of set")-    stdio.puts("  list [-no-col-titles]     list bookmarks")-    stdio.puts("  delete <name>             delete a bookmark")+    stdio.puts("  mv <name> [-rev <rev>]    alias of set")+    stdio.puts("  ls [-no-col-titles]       list bookmarks")+    stdio.puts("  rm <name>                 delete a bookmark")     stdio.puts("")     stdio.puts("examples:")     stdio.puts("  vn bmk set main")@@ -269,6 +282,10 @@     }     if cmd_is(cmd, c"stat", 4) || cmd_is(cmd, c"status", 6) {         show_status()+        return 0+    }+    if cmd_is(cmd, c"ls", 2) {+        show_ls()         return 0     }     if cmd_is(cmd, c"inf", 3) {--- /dev/null+++ b/veins/src/cli/ls_cmd.sx@@ -0,0 +1,242 @@+/// `vn ls [rev]`: print the tracked file tree.+///+/// The working copy is snapshotted first, like every command that reads it.+/// Subtrees become directories in the printed tree; files, symlinks, and+/// submodules are leaves.++val str      = use("std/string")+val stdio    = use("std/stdio")+val msg      = use("../core/msg.sx")+val bytes    = use("../core/bytes.sx")+val buf      = use("../core/buf.sx")+val objid    = use("../object/id.sx")+val tree     = use("../object/tree.sx")+val graph    = use("../object/graph.sx")+val treewalk = use("../object/treewalk.sx")+val view     = use("../object/view.sx")+val store    = use("../store/store.sx")+val state    = use("../store/state.sx")+val layout   = use("../store/layout.sx")+val snapshot = use("../worktree/snapshot.sx")++fn cmd_is(a: ref u8, text: ref u8, len: usize) bool = trust {+    return bytes.Bytes.eql(bytes.from_cstr(a), bytes.bytes(text, len))+}++fn is_flag(a: ref u8) bool = trust {+    if a == none {+        return false+    }+    if @strlen(a) == 0 {+        return false+    }+    return @load8(a) == 45+}++fn copy_id(id: objid.Id) objid.Id = trust {+    if id.ptr == none {+        return objid.id(none)+    }+    val out: ref u8 = @alloc(32) as ref u8+    @memcpy(out as ref void, id.ptr as ref void, 32)+    return objid.id(out)+}++fn free_id(id: objid.Id) void = trust {+    if id.ptr != none {+        val p: mut ref void = id.ptr as ref void+        @free(p)+    }+}++/// Concatenate two byte spans into a NUL-terminated buffer. The length in the+/// returned value excludes the terminator.+fn concat(a: bytes.Bytes, b: bytes.Bytes) bytes.Bytes = trust {+    val out: ref u8 = @alloc(a.len + b.len + 1) as ref u8+    if a.len > 0 {+        @memcpy(out as ref void, a.ptr as ref void, a.len)+    }+    if b.len > 0 {+        @memcpy(@ptradd(out, a.len as i64) as ref void, b.ptr as ref void, b.len)+    }+    @store8(@ptradd(out, (a.len + b.len) as i64), 0)+    return bytes.bytes(out, a.len + b.len)+}++fn print_line(prefix: bytes.Bytes, connector: bytes.Bytes, name: bytes.Bytes) void = trust {+    val line: mut buf.Buf = buf.new(prefix.len + connector.len + name.len + 1)+    buf.Buf.put_bytes(line, prefix)+    buf.Buf.put_bytes(line, connector)+    buf.Buf.put_bytes(line, name)+    buf.Buf.put_u8(line, 0)+    stdio.print("{s}\n", {str.from_cstr(line.ptr)})+    buf.Buf.free(line)+}++fn print_tree(+    handles: list[ref store.Handle],+    tree_id: objid.Id,+    prefix: bytes.Bytes,+    depth: usize+) bool = trust {+    if depth > 4096 {+        return false+    }+    val t_opt = treewalk.read_tree(handles, tree_id)+    val t: mut ref tree.Tree = none+    match t_opt {+        some x => {+            t = x+        }+        none => {+            return false+        }+    }+    val n: usize = @len(t.entries)+    val i: mut usize = 0+    for i < n {+        val e_opt = @get(t.entries, i)+        if e_opt is some e {+            val last: bool = i + 1 == n+            val connector: mut bytes.Bytes = bytes.bytes(c"|-- ", 4)+            if last {+                connector = bytes.bytes(c"`-- ", 4)+            }+            print_line(prefix, connector, e.name)+            if e.kind == tree.KIND_SUBTREE {+                val add: mut bytes.Bytes = bytes.bytes(c"|   ", 4)+                if last {+                    add = bytes.bytes(c"    ", 4)+                }+                val child_prefix: bytes.Bytes = concat(prefix, add)+                val good: bool = print_tree(handles, e.target, child_prefix, depth + 1)+                val cpp: mut ref void = child_prefix.ptr as ref void+                @free(cpp)+                if !good {+                    tree.free_tree(t)+                    return false+                }+            }+        }+        i+++    }+    tree.free_tree(t)+    return true+}++pub fn run(args: list[ref u8], start: usize) i32 = trust {+    val rev: mut bytes.Bytes = bytes.empty()+    val end_flags: mut bool = false+    val i: mut usize = start+    for i < @len(args) {+        val o = @get(args, i)+        if o is some a {+            if !end_flags && cmd_is(a, c"--", 2) {+                end_flags = true+            } elif !end_flags && (cmd_is(a, c"-r", 2) || cmd_is(a, c"-rev", 4)) {+                if i + 1 < @len(args) {+                    val v = @get(args, i + 1)+                    if v is some rv {+                        rev = bytes.from_cstr(rv)+                        i+++                    }+                } else {+                    msg.error_prefix()+                    stdio.eprint("{s} requires a revision\n", {str.from_cstr(a)})+                    return 1+                }+            } elif !end_flags && is_flag(a) {+                msg.error_prefix()+                stdio.eprint("unknown flag: {s}\n", {str.from_cstr(a)})+                return 1+            } else {+                rev = bytes.from_cstr(a)+            }+        }+        i+++    }++    val repo_opt = layout.discover(str.from_cstr(c"."))+    val repo: mut layout.Repo = layout.Repo{root: @zeroed}+    match repo_opt {+        some r => {+            repo = r+        }+        none => {+            msg.error_prefix()+            stdio.eprint("not a veins repository\n", {})+            return 1+        }+    }++    val snap: i32 = snapshot.run(repo)+    if snap != 0 {+        layout.free_repo(repo)+        return snap+    }+    val st: store.Store = store.open_all(repo)+    val v_opt = state.current_view(repo, st.handles)+    val v: mut ref view.View = none+    match v_opt {+        some x => {+            v = x+        }+        none => {+            msg.error_prefix()+            stdio.eprint("no operation state\n", {})+            store.close(st)+            layout.free_repo(repo)+            return 1+        }+    }+    val resolved = graph.resolve(st.handles, v, rev)+    val target: mut objid.Id = objid.id_none()+    val have_target: mut bool = false+    if resolved is some id {+        target = copy_id(id)+        have_target = true+    }+    view.free_view(v)+    if !have_target {+        msg.error_prefix()+        if rev.len == 0 {+            stdio.eprint("no working-copy commit\n", {})+        } else {+            stdio.eprint("unknown revision: {s}\n", {str.view(rev.ptr, rev.len as i64)})+        }+        store.close(st)+        layout.free_repo(repo)+        return 1+    }+    val lc_opt = graph.load_commit(st.handles, target)+    val root: mut objid.Id = objid.id_none()+    val have_root: mut bool = false+    if lc_opt is some lc {+        root = copy_id(lc.c.root_tree)+        have_root = true+        graph.free_loaded(lc)+    }+    if !have_root {+        msg.error_prefix()+        stdio.eprint("cannot read commit\n", {})+        free_id(target)+        store.close(st)+        layout.free_repo(repo)+        return 1+    }++    stdio.print(".\n", {})+    val good: bool = print_tree(st.handles, root, bytes.empty(), 0)+    if !good {+        msg.error_prefix()+        stdio.eprint("cannot read tree\n", {})+    }+    free_id(root)+    free_id(target)+    store.close(st)+    layout.free_repo(repo)+    if !good {+        return 1+    }+    return 0+}--- a/veins/src/main.sx+++ b/veins/src/main.sx@@ -13,6 +13,7 @@ val help_cmd     = use("cli/help_cmd.sx") val bench_cmd    = use("cli/bench_cmd.sx") val status_cmd   = use("cli/status_cmd.sx")+val ls_cmd       = use("cli/ls_cmd.sx") val log_cmd      = use("cli/log_cmd.sx") val show_cmd     = use("cli/show_cmd.sx") val diff_cmd     = use("cli/diff_cmd.sx")@@ -500,6 +501,9 @@     }     if cmd_is(cmd, c"stat", 4) || cmd_is(cmd, c"status", 6) {         return run_status()+    }+    if cmd_is(cmd, c"ls", 2) {+        return ls_cmd.run(args, 2)     }     if cmd_is(cmd, c"inf", 3) {         return run_inf()