navid/veins / commit
3cf035edf3fd
Navid M <navidmomtahen@gmail.com> · 2026-11-26
parents: 3da5bf0d7b9c
Browse files at this commit · Download source
--- a/veins/src/cli/help_cmd.sx+++ b/veins/src/cli/help_cmd.sx@@ -16,7 +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(" ls list tracked files as a tree; -plain for paths only") stdio.puts(" inf show a repository breakdown") stdio.puts(" log show commit history") stdio.puts(" show show a commit and its diff")@@ -124,7 +124,7 @@ } fn show_ls() void = trust {- stdio.puts("usage: vn ls [rev]")+ stdio.puts("usage: vn ls [rev] [-plain]") stdio.puts("") stdio.puts("list tracked files as an ASCII tree") stdio.puts("")@@ -133,6 +133,7 @@ stdio.puts("") stdio.puts("options:") stdio.puts(" -rev <rev> revision to list")+ stdio.puts(" -plain, -p print one tracked path per line") } fn show_inf() void = trust {--- a/veins/src/cli/ls_cmd.sx+++ b/veins/src/cli/ls_cmd.sx@@ -9,6 +9,7 @@ val msg = use("../core/msg.sx") val bytes = use("../core/bytes.sx") val buf = use("../core/buf.sx")+val lists = use("../core/lists.sx") val objid = use("../object/id.sx") val tree = use("../object/tree.sx") val graph = use("../object/graph.sx")@@ -71,6 +72,99 @@ buf.Buf.put_u8(line, 0) stdio.print("{s}\n", {str.from_cstr(line.ptr)}) buf.Buf.free(line)+}++fn join_path(prefix: bytes.Bytes, name: bytes.Bytes) bytes.Bytes = trust {+ if prefix.len == 0 {+ return concat(bytes.empty(), name)+ }+ val out: ref u8 = @alloc(prefix.len + 1 + name.len + 1) as ref u8+ @memcpy(out as ref void, prefix.ptr as ref void, prefix.len)+ @store8(@ptradd(out, prefix.len as i64), 47)+ if name.len > 0 {+ @memcpy(@ptradd(out, (prefix.len + 1) as i64) as ref void, name.ptr as ref void, name.len)+ }+ @store8(@ptradd(out, (prefix.len + 1 + name.len) as i64), 0)+ return bytes.bytes(out, prefix.len + 1 + name.len)+}++fn collect_plain(+ handles: list[ref store.Handle],+ tree_id: objid.Id,+ prefix: bytes.Bytes,+ out: mut list[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 i: mut usize = 0+ for i < @len(t.entries) {+ val e_opt = @get(t.entries, i)+ if e_opt is some e {+ val full: bytes.Bytes = join_path(prefix, e.name)+ if e.kind == tree.KIND_SUBTREE {+ val good: bool = collect_plain(handles, e.target, full, out, depth + 1)+ val fp: mut ref void = full.ptr as ref void+ @free(fp)+ if !good {+ tree.free_tree(t)+ return false+ }+ } else {+ @append(out, full)+ }+ }+ i+++ }+ tree.free_tree(t)+ return true+}++fn sort_paths(xs: mut list[bytes.Bytes]) void = trust {+ val i: mut usize = 1+ for i < @len(xs) {+ val cur = @get(xs, i)+ val c: mut bytes.Bytes = bytes.empty()+ if cur is some v {+ c = v+ }+ val j: mut usize = i+ for j > 0 {+ val prev_opt = @get(xs, j - 1)+ val prev: mut bytes.Bytes = bytes.empty()+ if prev_opt is some v2 {+ prev = v2+ }+ if bytes.Bytes.cmp(prev, c) <= 0 {+ break+ }+ @set(xs, j, prev)+ j--+ }+ @set(xs, j, c)+ i+++ }+}++fn free_paths(xs: list[bytes.Bytes]) void = trust {+ for p in xs {+ if p.ptr != none {+ val pp: mut ref void = p.ptr as ref void+ @free(pp)+ }+ }+ @listfree(xs) } fn print_tree(@@ -126,6 +220,7 @@ pub fn run(args: list[ref u8], start: usize) i32 = trust { val rev: mut bytes.Bytes = bytes.empty()+ val plain: mut bool = false val end_flags: mut bool = false val i: mut usize = start for i < @len(args) {@@ -133,6 +228,8 @@ if o is some a { if !end_flags && cmd_is(a, c"--", 2) { end_flags = true+ } elif !end_flags && (cmd_is(a, c"-plain", 6) || cmd_is(a, c"-p", 2)) {+ plain = 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)@@ -225,8 +322,21 @@ return 1 } - stdio.print(".\n", {})- val good: bool = print_tree(st.handles, root, bytes.empty(), 0)+ val good: mut bool = false+ if plain {+ val paths: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()+ good = collect_plain(st.handles, root, bytes.empty(), paths, 0)+ if good {+ sort_paths(paths)+ for p in paths {+ stdio.print("{s}\n", {str.from_cstr(p.ptr)})+ }+ }+ free_paths(paths)+ } else {+ stdio.print(".\n", {})+ good = print_tree(st.handles, root, bytes.empty(), 0)+ } if !good { msg.error_prefix() stdio.eprint("cannot read tree\n", {})