navid/veins / commit

ff3e5d106585

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

parents: 90af4ae62ead

Browse files at this commit · Download source

all: remote command enhancements
--- a/veins/src/cli/help_cmd.sx+++ b/veins/src/cli/help_cmd.sx@@ -14,6 +14,7 @@     stdio.puts("subcommands:")     stdio.puts("  init         create a repository")     stdio.puts("  git          import from or export to git")+    stdio.puts("  remote       manage native remotes")     stdio.puts("  clone        clone a repository over the native protocol")     stdio.puts("  fetch        fetch from a native remote")     stdio.puts("  push         push a bookmark to a native remote")@@ -63,6 +64,26 @@     stdio.puts("")     stdio.puts("options:")     stdio.puts("  -git-import:<gitdir>      import from <gitdir> (a directory containing .git)")+}++fn show_remote() void = trust {+    stdio.puts("usage: vn remote <add|set-url|list|remove> ...")+    stdio.puts("")+    stdio.puts("manage native remotes stored in .veins/config")+    stdio.puts("")+    stdio.puts("subcommands:")+    stdio.puts("  add <name> <url> [-key:<name>] [-tls-pin:<hash>]")+    stdio.puts("                            add a remote")+    stdio.puts("  set-url <name> <url> [-key:<name>] [-tls-pin:<hash>]")+    stdio.puts("                            change a remote URL (and optionally key/pin)")+    stdio.puts("  list                      list remotes")+    stdio.puts("  remove <name>             remove a remote")+    stdio.puts("")+    stdio.puts("an empty -key: or -tls-pin: value clears that setting")+    stdio.puts("")+    stdio.puts("examples:")+    stdio.puts("  vn remote add origin veins://127.0.0.1:7419/navid-m/veins2")+    stdio.puts("  vn remote set-url origin veins://example/repo -key:my-key") }  fn show_git() void = trust {@@ -544,6 +565,10 @@     }     if cmd_is(cmd, c"whoami", 6) {         show_whoami()+        return 0+    }+    if cmd_is(cmd, c"remote", 6) {+        show_remote()         return 0     }     if cmd_is(cmd, c"git", 3) {--- /dev/null+++ b/veins/src/cli/remote_cmd.sx@@ -0,0 +1,200 @@+/// `vn remote`: manage native remotes stored in `.veins/config`.++val stdio  = use("std/stdio")+val str    = use("std/string")+val bytes  = use("../core/bytes.sx")+val msg    = use("../core/msg.sx")+val config = use("../core/repoconfig.sx")+val layout = use("../store/layout.sx")+val remote = use("../git/remote.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 set_field(repo: layout.Repo, name: bytes.Bytes, field: ref u8, field_len: usize, value: bytes.Bytes) bool = trust {+    val key: bytes.Bytes = remote.key_for(name, bytes.bytes(field, field_len))+    val passed: bool = config.set(repo, key, value)+    bytes.free_owned(key)+    return passed+}++fn clear_field(repo: layout.Repo, name: bytes.Bytes, field: ref u8, field_len: usize) bool = trust {+    val key: bytes.Bytes = remote.key_for(name, bytes.bytes(field, field_len))+    val passed: bool = config.remove(repo, key)+    bytes.free_owned(key)+    return passed+}++fn run_upsert(repo: layout.Repo, args: list[ref u8], start: usize, replace: bool) i32 = trust {+    val name: mut bytes.Bytes = bytes.empty()+    val url: mut bytes.Bytes = bytes.empty()+    val key: mut option[bytes.Bytes] = none+    val pin: mut option[bytes.Bytes] = none+    val i: mut usize = start+    for i < @len(args) {+        val arg_opt = @get(args, i)+        if arg_opt is some a {+            val av: bytes.Bytes = bytes.from_cstr(a)+            if bytes.Bytes.starts_with(av, bytes.bytes(c"-key:", 5)) {+                key = some(bytes.Bytes.slice(av, 5, av.len))+            } elif bytes.Bytes.starts_with(av, bytes.bytes(c"-tls-pin:", 9)) {+                pin = some(bytes.Bytes.slice(av, 9, av.len))+            } elif av.len > 0 && bytes.Bytes.at(av, 0) == 45 {+                msg.error_prefix()+                stdio.eprint("unknown flag: {s}\n", {str.from_cstr(a)})+                return 1+            } elif name.ptr == none {+                name = av+            } elif url.ptr == none {+                url = av+            } else {+                msg.error_prefix()+                stdio.eprint("too many arguments\n", {})+                return 1+            }+        }+        i+++    }+    if name.ptr == none || url.ptr == none {+        msg.error_prefix()+        if replace {+            stdio.eprint("remote set-url requires a name and URL\n", {})+        } else {+            stdio.eprint("remote add requires a name and URL\n", {})+        }+        return 1+    }+    val existing = remote.url_of(repo, name)+    if existing is some old {+        bytes.free_owned(old)+        if !replace {+            msg.error_prefix()+            stdio.eprint("remote already exists: {s}\n", {str.view(name.ptr, name.len as i64)})+            return 1+        }+    } elif replace {+        msg.error_prefix()+        stdio.eprint("unknown remote: {s}\n", {str.view(name.ptr, name.len as i64)})+        return 1+    }+    if !set_field(repo, name, c"url", 3, url) {+        msg.error_prefix()+        stdio.eprint("cannot write repository config\n", {})+        return 1+    }+    if key is some value {+        val passed: mut bool = true+        if value.len == 0 {+            passed = clear_field(repo, name, c"key", 3)+        } else {+            passed = set_field(repo, name, c"key", 3, value)+        }+        if !passed {+            msg.error_prefix()+            stdio.eprint("cannot write repository config\n", {})+            return 1+        }+    }+    if pin is some value {+        val passed: mut bool = true+        if value.len == 0 {+            passed = clear_field(repo, name, c"tls-pin", 7)+        } else {+            passed = set_field(repo, name, c"tls-pin", 7, value)+        }+        if !passed {+            msg.error_prefix()+            stdio.eprint("cannot write repository config\n", {})+            return 1+        }+    }+    return 0+}++fn run_list(repo: layout.Repo) i32 = trust {+    val names: list[bytes.Bytes] = remote.list_remotes(repo)+    for name in names {+        val url_opt = remote.url_of(repo, name)+        val url: mut bytes.Bytes = bytes.empty()+        if url_opt is some value {+            url = value+        }+        stdio.print(+            "{s}\t{s}\n",+            {str.view(name.ptr, name.len as i64), str.view(url.ptr, url.len as i64)}+        )+        if url.ptr != none {+            bytes.free_owned(url)+        }+        bytes.free_owned(name)+    }+    @listfree(names)+    return 0+}++fn run_remove(repo: layout.Repo, args: list[ref u8], start: usize) i32 = trust {+    val name_opt = @get(args, start)+    if name_opt is none {+        msg.error_prefix()+        stdio.eprint("remote remove requires a name\n", {})+        return 1+    }+    val name: mut bytes.Bytes = bytes.empty()+    if name_opt is some n {+        name = bytes.from_cstr(n)+    }+    val existing = remote.url_of(repo, name)+    if existing is none {+        msg.error_prefix()+        stdio.eprint("unknown remote: {s}\n", {str.view(name.ptr, name.len as i64)})+        return 1+    }+    if existing is some old {+        bytes.free_owned(old)+    }+    clear_field(repo, name, c"url", 3)+    clear_field(repo, name, c"key", 3)+    clear_field(repo, name, c"tls-pin", 7)+    return 0+}++pub fn run(args: list[ref u8], start: usize) i32 = trust {+    val sub_opt = @get(args, start)+    if sub_opt is none {+        msg.error_prefix()+        stdio.eprint("expected a remote subcommand (add, set-url, list, remove)\n", {})+        return 1+    }+    val sub: mut ref u8 = none+    if sub_opt is some s {+        sub = s+    }+    val repo_opt = layout.discover(str.from_cstr(c"."))+    val repo: mut layout.Repo = layout.Repo{root: bytes.empty()}+    match repo_opt {+        some r => {+            repo = r+        }+        none => {+            msg.error_prefix()+            stdio.eprint("not a veins repository\n", {})+            return 1+        }+    }+    val status: mut i32 = 1+    if cmd_is(sub, c"add", 3) {+        status = run_upsert(repo, args, start + 1, false)+    } elif cmd_is(sub, c"set-url", 7) {+        status = run_upsert(repo, args, start + 1, true)+    } elif cmd_is(sub, c"list", 4) || cmd_is(sub, c"ls", 2) {+        status = run_list(repo)+    } elif cmd_is(sub, c"remove", 6) || cmd_is(sub, c"rm", 2) {+        status = run_remove(repo, args, start + 1)+    } else {+        msg.error_prefix()+        stdio.eprint("unknown remote subcommand: {s}\n", {str.from_cstr(sub)})+    }+    layout.free_repo(repo)+    return status+}--- a/veins/src/main.sx+++ b/veins/src/main.sx@@ -10,6 +10,7 @@ val fsck_cmd     = use("cli/fsck_cmd.sx") val selftest_cmd = use("cli/selftest_cmd.sx") val git_cmd      = use("cli/git_cmd.sx")+val remote_cmd   = use("cli/remote_cmd.sx") val clone_cmd    = use("cli/clone_cmd.sx") val fetch_cmd    = use("cli/fetch_cmd.sx") val push_cmd     = use("cli/push_cmd.sx")@@ -894,6 +895,9 @@     }     if cmd_is(cmd, c"git", 3) {         return git_cmd.run(args, 2)+    }+    if cmd_is(cmd, c"remote", 6) {+        return remote_cmd.run(args, 2)     }     if cmd_is(cmd, c"clone", 5) {         return clone_cmd.run(args, 2)