navid/veins / commit

d26c4e3bfd08

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

parents: 73232b5f26fc

Browse files at this commit · Download source

main: add hash subcommand
--- /dev/null+++ b/veins/src/cli/hash_cmd.sx@@ -0,0 +1,134 @@+/// `vn hash [rev]`: print the full commit hash for a revision.++val str      = use("std/string")+val stdio    = use("std/stdio")+val msg      = use("../core/msg.sx")+val bytes    = use("../core/bytes.sx")+val objid    = use("../object/id.sx")+val graph    = use("../object/graph.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 {+    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)+    }+}++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 hex: ref u8 = objid.Id.hex(target)+    stdio.print("{s}\n", {str.view(hex, 64)})+    @free(hex as ref void)+    free_id(target)+    store.close(st)+    layout.free_repo(repo)+    return 0+}--- a/veins/src/cli/help_cmd.sx+++ b/veins/src/cli/help_cmd.sx@@ -20,6 +20,7 @@     stdio.puts("  inf          show a repository breakdown")     stdio.puts("  log          show commit history")     stdio.puts("  show         show a commit and its diff")+    stdio.puts("  hash         print the commit hash for a revision")     stdio.puts("  diff         show a unified diff")     stdio.puts("  merge        create a merge commit; never fails")     stdio.puts("  rebase       rewrite changes onto a new parent")@@ -203,6 +204,18 @@     stdio.puts("")     stdio.puts("options:")     stdio.puts("  -no-col-titles            omit the metadata labels")+}++fn show_hash() void = trust {+    stdio.puts("usage: vn hash [rev]")+    stdio.puts("")+    stdio.puts("print the full commit hash for a revision")+    stdio.puts("")+    stdio.puts("arguments:")+    stdio.puts("  [rev]                     revision to hash (default: @)")+    stdio.puts("")+    stdio.puts("options:")+    stdio.puts("  -rev <rev>                revision to hash") }  fn show_bookmark() void = trust {@@ -347,6 +360,10 @@     }     if cmd_is(cmd, c"show", 4) {         show_show()+        return 0+    }+    if cmd_is(cmd, c"hash", 4) {+        show_hash()         return 0     }     if cmd_is(cmd, c"diff", 4) {--- a/veins/src/main.sx+++ b/veins/src/main.sx@@ -16,6 +16,7 @@ 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 hash_cmd     = use("cli/hash_cmd.sx") val diff_cmd     = use("cli/diff_cmd.sx") val merge_cmd    = use("cli/merge_cmd.sx") val resolve_cmd  = use("cli/resolve_cmd.sx")@@ -777,6 +778,9 @@     }     if cmd_is(cmd, c"log", 3) {         return run_log(args, 2)+    }+    if cmd_is(cmd, c"hash", 4) {+        return hash_cmd.run(args, 2)     }     if cmd_is(cmd, c"show", 4) {         return run_show(args, 2)