navid/veins / commit

d99aa6ed5a2d

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

parents: ce5d2696b2bf

Browse files at this commit · Download source

all: cfg subcommand
--- a/docs/SPECIFICATION_V1.adoc+++ b/docs/SPECIFICATION_V1.adoc@@ -1101,9 +1101,11 @@ | `vn access add\|list\|remove\|anonymous` | Manage a served repository's access file (server side, local only). | `vn whoami [<remote>]` | Report the role and key fingerprint the server grants. | `vn fsck [-verbose]`, `vn gc` | Verification and garbage collection.-| `vn config` | Read and write configuration.+| `vn config`, `vn cfg` | Read and write configuration. | `vn help [<command>]`, `vn -help`, `vn version`, `vn -version` | Show usage or version information. |===++`vn cfg` reads and writes `.veins/config` by default. With `-global`, it edits the user-level `~/.local/config/veins/veins.toml`. `vn cfg <key>` prints a value, `vn cfg <key> <value>` sets one, `vn cfg -unset <key>` removes one, and `vn cfg` with no arguments lists the configuration.  `vn log` prints commits newest first in aligned columns with no borders. Column titles are shown by default and can be omitted with `-no-col-titles`. Output is colored when standard output is a terminal; setting `NO_COLOR` disables color. --- a/docs/SPECIFICATION_V1.html+++ b/docs/SPECIFICATION_V1.html@@ -4,7 +4,7 @@ <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0">-<meta name="generator" content="Asciidoctor 2.0.26">+<meta name="generator" content="Asciidoctor 2.0.20"> <title>veins: Version Control System Specification</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700"> <style>@@ -116,6 +116,7 @@ :not(pre):not([class^=L])>code{font-size:.9375em;font-style:normal!important;letter-spacing:0;padding:.1em .5ex;word-spacing:-.15em;background:#f7f7f8;border-radius:4px;line-height:1.45;text-rendering:optimizeSpeed} pre{color:rgba(0,0,0,.9);font-family:"Droid Sans Mono","DejaVu Sans Mono",monospace;line-height:1.45;text-rendering:optimizeSpeed} pre code,pre pre{color:inherit;font-size:inherit;line-height:inherit}+pre>code{display:block} pre.nowrap,pre.nowrap pre{white-space:pre;word-wrap:normal} em em{font-style:normal} strong strong{font-weight:400}@@ -139,7 +140,7 @@ #content::before{content:none} #header>h1:first-child{color:rgba(0,0,0,.85);margin-top:2.25rem;margin-bottom:0} #header>h1:first-child+#toc{margin-top:8px;border-top:1px solid #dddddf}-#header>h1:only-child{border-bottom:1px solid #dddddf;padding-bottom:8px}+#header>h1:only-child,body.toc2 #header>h1:nth-last-child(2){border-bottom:1px solid #dddddf;padding-bottom:8px} #header .details{border-bottom:1px solid #dddddf;line-height:1.45;padding-top:.25em;padding-bottom:.25em;padding-left:.25em;color:rgba(0,0,0,.6);display:flex;flex-flow:row wrap} #header .details span:first-child{margin-left:-.125em} #header .details span.email a{color:rgba(0,0,0,.85)}@@ -161,7 +162,6 @@ #toctitle{color:#7a2518;font-size:1.2em} @media screen and (min-width:768px){#toctitle{font-size:1.375em} body.toc2{padding-left:15em;padding-right:0}-body.toc2 #header>h1:nth-last-child(2){border-bottom:1px solid #dddddf;padding-bottom:8px} #toc.toc2{margin-top:0!important;background:#f8f8f7;position:fixed;width:15em;left:0;top:0;border-right:1px solid #e7e7e9;border-top-width:0!important;border-bottom-width:0!important;z-index:1000;padding:1.25em 1em;height:100%;overflow:auto} #toc.toc2 #toctitle{margin-top:0;margin-bottom:.8rem;font-size:1.2em} #toc.toc2>ul{font-size:.9em;margin-bottom:0}@@ -218,7 +218,6 @@ .literalblock pre,.listingblock>.content>pre:not(.highlight),.listingblock>.content>pre[class=highlight],.listingblock>.content>pre[class^="highlight "]{background:#f7f7f8} .literalblock.output pre{color:#f7f7f8;background:rgba(0,0,0,.9)} .listingblock>.content{position:relative}-.listingblock pre>code{display:block} .listingblock code[data-lang]::before{display:none;content:attr(data-lang);position:absolute;font-size:.75em;top:.425rem;right:.5rem;line-height:1;text-transform:uppercase;color:inherit;opacity:.5} .listingblock:hover code[data-lang]::before{display:block} .listingblock.terminal pre .command::before{content:attr(data-prompt);padding-right:.5em;color:inherit;opacity:.5}@@ -328,7 +327,7 @@ a.image object{pointer-events:none} sup.footnote,sup.footnoteref{font-size:.875em;position:static;vertical-align:super} sup.footnote a,sup.footnoteref a{text-decoration:none}-sup.footnote a:active,sup.footnoteref a:active,#footnotes .footnote a:first-of-type:active{text-decoration:underline}+sup.footnote a:active,sup.footnoteref a:active{text-decoration:underline} #footnotes{padding-top:.75em;padding-bottom:.75em;margin-bottom:.625em} #footnotes hr{width:20%;min-width:6.25em;margin:-.25em 0 .75em;border-width:1px 0 0} #footnotes .footnote{padding:0 .375em 0 .225em;line-height:1.3334;font-size:.875em;margin-left:1.2em;margin-bottom:.2em}@@ -434,120 +433,6 @@ @media amzn-kf8{#header,#content,#footnotes,#footer{padding:0}} </style> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">-<style>-pre.rouge table td { padding: 5px; }-pre.rouge table pre { margin: 0; }-pre.rouge, pre.rouge .w {-  color: #24292f;-  background-color: #f6f8fa;-}-pre.rouge .k, pre.rouge .kd, pre.rouge .kn, pre.rouge .kp, pre.rouge .kr, pre.rouge .kt, pre.rouge .kv {-  color: #cf222e;-}-pre.rouge .gr {-  color: #f6f8fa;-}-pre.rouge .gd {-  color: #82071e;-  background-color: #ffebe9;-}-pre.rouge .nb {-  color: #953800;-}-pre.rouge .nc {-  color: #953800;-}-pre.rouge .no {-  color: #953800;-}-pre.rouge .nn {-  color: #953800;-}-pre.rouge .sr {-  color: #116329;-}-pre.rouge .na {-  color: #116329;-}-pre.rouge .nt {-  color: #116329;-}-pre.rouge .gi {-  color: #116329;-  background-color: #dafbe1;-}-pre.rouge .kc {-  color: #0550ae;-}-pre.rouge .l, pre.rouge .ld, pre.rouge .m, pre.rouge .mb, pre.rouge .mf, pre.rouge .mh, pre.rouge .mi, pre.rouge .il, pre.rouge .mo, pre.rouge .mx {-  color: #0550ae;-}-pre.rouge .sb {-  color: #0550ae;-}-pre.rouge .bp {-  color: #0550ae;-}-pre.rouge .ne {-  color: #0550ae;-}-pre.rouge .nl {-  color: #0550ae;-}-pre.rouge .py {-  color: #0550ae;-}-pre.rouge .nv, pre.rouge .vc, pre.rouge .vg, pre.rouge .vi, pre.rouge .vm {-  color: #0550ae;-}-pre.rouge .o, pre.rouge .ow {-  color: #0550ae;-}-pre.rouge .gh {-  color: #0550ae;-  font-weight: bold;-}-pre.rouge .gu {-  color: #0550ae;-  font-weight: bold;-}-pre.rouge .s, pre.rouge .sa, pre.rouge .sc, pre.rouge .dl, pre.rouge .sd, pre.rouge .s2, pre.rouge .se, pre.rouge .sh, pre.rouge .sx, pre.rouge .s1, pre.rouge .ss {-  color: #0a3069;-}-pre.rouge .nd {-  color: #8250df;-}-pre.rouge .nf, pre.rouge .fm {-  color: #8250df;-}-pre.rouge .err {-  color: #f6f8fa;-  background-color: #82071e;-}-pre.rouge .c, pre.rouge .ch, pre.rouge .cd, pre.rouge .cm, pre.rouge .cp, pre.rouge .cpf, pre.rouge .c1, pre.rouge .cs {-  color: #6e7781;-}-pre.rouge .gl {-  color: #6e7781;-}-pre.rouge .gt {-  color: #6e7781;-}-pre.rouge .ni {-  color: #24292f;-}-pre.rouge .si {-  color: #24292f;-}-pre.rouge .ge {-  color: #24292f;-  font-style: italic;-}-pre.rouge .gs {-  color: #24292f;-  font-weight: bold;-}-</style> </head> <body class="book toc2 toc-left"> <div id="header">@@ -3138,7 +3023,7 @@ <td class="tableblock halign-left valign-top"><p class="tableblock">Verification and garbage collection.</p></td> </tr> <tr>-<td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn config</code></p></td>+<td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn config</code>, <code>vn cfg</code></p></td> <td class="tableblock halign-left valign-top"><p class="tableblock">Read and write configuration.</p></td> </tr> <tr>@@ -3148,6 +3033,9 @@ </tbody> </table> <div class="paragraph">+<p><code>vn cfg</code> reads and writes <code>.veins/config</code> by default. With <code>-global</code>, it edits the user-level <code>~/.local/config/veins/veins.toml</code>. <code>vn cfg &lt;key&gt;</code> prints a value, <code>vn cfg &lt;key&gt; &lt;value&gt;</code> sets one, <code>vn cfg -unset &lt;key&gt;</code> removes one, and <code>vn cfg</code> with no arguments lists the configuration.</p>+</div>+<div class="paragraph"> <p><code>vn log</code> prints commits newest first in aligned columns with no borders. Column titles are shown by default and can be omitted with <code>-no-col-titles</code>. Output is colored when standard output is a terminal; setting <code>NO_COLOR</code> disables color.</p> </div> <div class="paragraph">@@ -3538,7 +3426,7 @@ <div id="footer"> <div id="footer-text"> Version 0.1-draft<br>-Last updated 2026-11-26 10:52:45 UTC+Last updated 2026-11-28 00:07:58 UTC </div> </div> </body>--- /dev/null+++ b/veins/src/cli/cfg_cmd.sx@@ -0,0 +1,424 @@+/// `vn cfg`: read and write local repository or user configuration.++val stdio      = use("std/stdio")+val str        = use("std/string")+val bytes      = use("../core/bytes.sx")+val lists      = use("../core/lists.sx")+val prim       = use("../core/primitives.sx")+val msg        = use("../core/msg.sx")+val files      = use("../io/files.sx")+val layout     = use("../store/layout.sx")+val repoconfig = use("../core/repoconfig.sx")+val userconfig = use("../core/userconfig.sx")++fn trim(b: bytes.Bytes) bytes.Bytes = trust {+    val start: mut usize = 0+    val end: mut usize = b.len+    for start < end {+        val c: u8 = bytes.Bytes.at(b, start)+        if c != 32 && c != 9 && c != 13 && c != 10 {+            break+        }+        start+++    }+    for end > start {+        val c2: u8 = bytes.Bytes.at(b, end - 1)+        if c2 != 32 && c2 != 9 && c2 != 13 && c2 != 10 {+            break+        }+        end--+    }+    return bytes.Bytes.slice(b, start, end)+}++fn is_flag(a: ref u8) bool = trust {+    if a == none {+        return false+    }+    if @strlen(a) == 0 {+        return false+    }+    return @load8(a) == 45+}++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 bytes_is(b: bytes.Bytes, text: ref u8, len: usize) bool = trust {+    return bytes.Bytes.eql(b, bytes.bytes(text, len))+}++fn free_entries(entries: list[ref prim.KeyValue]) void = trust {+    for kv in entries {+        if kv.key.ptr != none {+            val kp: mut ref void = kv.key.ptr as ref void+            @free(kp)+        }+        if kv.value.ptr != none {+            val vp: mut ref void = kv.value.ptr as ref void+            @free(vp)+        }+        val ep: mut ref void = kv as ref void+        @free(ep)+    }+    @listfree(entries)+}++fn sort_entries(entries: mut list[ref prim.KeyValue]) void = trust {+    val n: usize = @len(entries)+    val i: mut usize = 1+    for i < n {+        val cur = @get(entries, i)+        val c: mut ref prim.KeyValue = none+        if cur is some v {+            c = v+        }+        val j: mut usize = i+        for j > 0 {+            val prev_opt = @get(entries, j - 1)+            val prev: mut ref prim.KeyValue = none+            if prev_opt is some p {+                prev = p+            }+            if bytes.Bytes.cmp(prev.key, c.key) <= 0 {+                break+            }+            @set(entries, j, prev)+            j--+        }+        @set(entries, j, c)+        i+++    }+}++fn repo_entries(repo: layout.Repo) list[ref prim.KeyValue] = trust {+    val entries: mut list[ref prim.KeyValue] = lists.new[ref prim.KeyValue]()+    val path: str.StringView = layout.config_path(repo)+    val data_opt = files.read_all(path)+    files.free_path(path)+    match data_opt {+        some data => {+            val pos: mut usize = 0+            val start: mut usize = 0+            for pos <= data.len {+                val at_end: bool = pos == data.len+                if at_end || bytes.Bytes.at(data, pos) == 10 {+                    val line: bytes.Bytes = trim(+                        bytes.Bytes.slice(data, start, pos)+                    )+                    if line.len > 0 && bytes.Bytes.at(line, 0) != 35 {+                        val eq: mut i64 = -1+                        val i: mut usize = 0+                        for i < line.len {+                            if bytes.Bytes.at(line, i) == 61 {+                                eq = i as i64+                                break+                            }+                            i+++                        }+                        if eq >= 0 {+                            val key: bytes.Bytes = trim(+                                bytes.Bytes.slice(line, 0, eq as usize)+                            )+                            if key.len > 0 {+                                val value: bytes.Bytes = trim(+                                    bytes.Bytes.slice(+                                        line,+                                        eq as usize + 1,+                                        line.len+                                    )+                                )+                                val kv: mut ref prim.KeyValue = @alloc(+                                    @sizeof(prim.KeyValue)+                                ) as ref prim.KeyValue+                                kv.key = bytes.Bytes.dup(key)+                                kv.value = bytes.Bytes.dup(value)+                                @append(entries, kv)+                            }+                        }+                    }+                    start = pos + 1+                }+                pos+++            }+            files.free_buffer(data)+        }+        none => {+        }+    }+    sort_entries(entries)+    return entries+}++fn print_entry(key: bytes.Bytes, value: bytes.Bytes) void = trust {+    stdio.print(+        "{s} = {s}\n",+        {str.view(key.ptr, key.len as i64), str.view(value.ptr, value.len as i64)}+    )+}++fn cfg_list(root: layout.Repo, global: bool) i32 = trust {+    if global {+        val email = userconfig.author_email()+        if email is some v {+            print_entry(bytes.bytes(c"user.email", 10), v)+            bytes.free_owned(v)+        }+        val name = userconfig.author_name()+        if name is some v {+            print_entry(bytes.bytes(c"user.name", 9), v)+            bytes.free_owned(v)+        }+        return 0+    }+    val entries = repo_entries(root)+    for kv in entries {+        print_entry(kv.key, kv.value)+    }+    free_entries(entries)+    return 0+}++fn cfg_get(root: layout.Repo, global: bool, key: bytes.Bytes) i32 = trust {+    val value: mut option[bytes.Bytes] = none+    if global {+        if bytes_is(key, c"user.name", 9) {+            value = userconfig.author_name()+        } elif bytes_is(key, c"user.email", 10) {+            value = userconfig.author_email()+        }+    } else {+        value = repoconfig.get(root, key)+    }+    match value {+        some v => {+            stdio.print("{s}\n", {str.view(v.ptr, v.len as i64)})+            bytes.free_owned(v)+            return 0+        }+        none => {+            msg.error_prefix()+            stdio.eprint("unknown config key: {s}\n", {str.view(key.ptr, key.len as i64)})+            return 1+        }+    }+}++fn cfg_set(root: layout.Repo, global: bool, key: bytes.Bytes, value: bytes.Bytes) i32 = trust {+    if global {+        if !userconfig.set(key, value) {+            msg.error_prefix()+            stdio.eprint("cannot write user config\n", {})+            return 1+        }+        return 0+    }+    if !repoconfig.set(root, key, value) {+        msg.error_prefix()+        stdio.eprint("cannot write repository config\n", {})+        return 1+    }+    return 0+}++fn cfg_unset(root: layout.Repo, global: bool, key: bytes.Bytes) i32 = trust {+    if global {+        if !userconfig.remove(key) {+            msg.error_prefix()+            stdio.eprint("cannot write user config\n", {})+            return 1+        }+        return 0+    }+    if !repoconfig.remove(root, key) {+        msg.error_prefix()+        stdio.eprint("cannot write repository config\n", {})+        return 1+    }+    return 0+}++fn usage() void = trust {+    stdio.puts("usage: vn cfg [<key>] [<value>]")+    stdio.puts("       vn cfg [<get|set|unset|list>] [<key>] [<value>]")+    stdio.puts("       vn cfg -list")+    stdio.puts("       vn cfg -unset <key>")+    stdio.puts("")+    stdio.puts("read and write local repository configuration")+    stdio.puts("")+    stdio.puts("  vn cfg                      list all keys")+    stdio.puts("  vn cfg <key>                print a value")+    stdio.puts("  vn cfg <key> <value>        set a value")+    stdio.puts("  vn cfg -unset <key>         remove a key")+    stdio.puts("  vn cfg -global ...          use the user config instead")+}++pub fn run(args: list[ref u8], start: usize) i32 = trust {+    val first_opt = @get(args, start)+    if first_opt is some first {+        if cmd_is(first, c"help", 4) || cmd_is(first, c"-h", 2)+        || cmd_is(first, c"--help", 6) {+            usage()+            return 0+        }+    }+    val global: mut bool = false+    val list: mut bool = false+    val unset: mut bool = false+    val end_flags: mut bool = false+    val positional: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()+    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"-global", 7) || cmd_is(a, c"--global", 8)+            || cmd_is(a, c"-g", 2)) {+                global = true+            } elif !end_flags+            && (cmd_is(a, c"-list", 5) || cmd_is(a, c"--list", 6)+            || cmd_is(a, c"-l", 2)) {+                list = true+            } elif !end_flags && (cmd_is(a, c"-unset", 6) || cmd_is(a, c"--unset", 7)) {+                unset = true+            } elif !end_flags && is_flag(a) {+                msg.error_prefix()+                stdio.eprint("unknown flag: {s}\n", {str.from_cstr(a)})+                @listfree(positional)+                return 1+            } else {+                @append(positional, bytes.from_cstr(a))+            }+        }+        i+++    }++    if @len(positional) == 1 {+        val first = @get(positional, 0)+        if first is some key {+            if bytes_is(key, c"list", 4) || bytes_is(key, c"ls", 2) {+                list = true+                @listfree(positional)+                positional = lists.new[bytes.Bytes]()+            }+        }+    }+    if @len(positional) == 2 {+        val first = @get(positional, 0)+        if first is some key {+            if bytes_is(key, c"get", 3) {+                val second = @get(positional, 1)+                @listfree(positional)+                positional = lists.new[bytes.Bytes]()+                if second is some k {+                    @append(positional, k)+                }+            } elif bytes_is(key, c"unset", 5) || bytes_is(key, c"rm", 2)+            || bytes_is(key, c"remove", 6) {+                unset = true+                val second = @get(positional, 1)+                @listfree(positional)+                positional = lists.new[bytes.Bytes]()+                if second is some k {+                    @append(positional, k)+                }+            }+        }+    }+    if @len(positional) == 3 {+        val first = @get(positional, 0)+        if first is some key {+            if bytes_is(key, c"set", 3) {+                val second = @get(positional, 1)+                val third = @get(positional, 2)+                @listfree(positional)+                positional = lists.new[bytes.Bytes]()+                if second is some k {+                    @append(positional, k)+                }+                if third is some v {+                    @append(positional, v)+                }+            }+        }+    }++    if list {+        if @len(positional) != 0 {+            msg.error_prefix()+            stdio.eprint("cfg list takes no operands\n", {})+            @listfree(positional)+            return 1+        }+    } elif unset {+        if @len(positional) != 1 {+            msg.error_prefix()+            stdio.eprint("cfg unset requires one key\n", {})+            @listfree(positional)+            return 1+        }+    } elif @len(positional) > 2 {+        msg.error_prefix()+        stdio.eprint("cfg accepts a key and one value\n", {})+        @listfree(positional)+        return 1+    }++    val repo: mut layout.Repo = layout.Repo{root: bytes.empty()}+    val have_repo: mut bool = false+    if !global {+        val repo_opt = layout.discover(str.from_cstr(c"."))+        match repo_opt {+            some r => {+                repo = r+                have_repo = true+            }+            none => {+                msg.error_prefix()+                stdio.eprint("not a veins repository\n", {})+                @listfree(positional)+                return 1+            }+        }+    }++    val status: mut i32 = 0+    if list || @len(positional) == 0 {+        status = cfg_list(repo, global)+    } elif unset {+        val key_opt = @get(positional, 0)+        val key: mut bytes.Bytes = bytes.empty()+        if key_opt is some k {+            key = k+        }+        status = cfg_unset(repo, global, key)+    } elif @len(positional) == 1 {+        val key_opt = @get(positional, 0)+        val key: mut bytes.Bytes = bytes.empty()+        if key_opt is some k {+            key = k+        }+        status = cfg_get(repo, global, key)+    } else {+        val key_opt = @get(positional, 0)+        val value_opt = @get(positional, 1)+        val key: mut bytes.Bytes = bytes.empty()+        val value: mut bytes.Bytes = bytes.empty()+        if key_opt is some k {+            key = k+        }+        if value_opt is some v {+            value = v+        }+        status = cfg_set(repo, global, key, value)+    }++    if have_repo {+        layout.free_repo(repo)+    }+    @listfree(positional)+    return status+}--- a/veins/src/cli/help_cmd.sx+++ b/veins/src/cli/help_cmd.sx@@ -38,6 +38,7 @@     stdio.puts("  new          start a new change")     stdio.puts("  bmk          manage named branch pointers")     stdio.puts("  tag          manage named tags")+    stdio.puts("  cfg          read and write configuration")     stdio.puts("  rm           untrack paths, keeping files on disk by default")     stdio.puts("  mv           move a tracked file and record the rename")     stdio.puts("  ex           add paths or globs to .vnexclude")@@ -288,6 +289,21 @@     stdio.puts("")     stdio.puts("the latest tag is the one whose target commit has the newest")     stdio.puts("committer timestamp; equal timestamps use natural name order")+}++fn show_cfg() void = trust {+    stdio.puts("usage: vn cfg [<key>] [<value>]")+    stdio.puts("       vn cfg [<get|set|unset|list>] [<key>] [<value>]")+    stdio.puts("       vn cfg -list")+    stdio.puts("       vn cfg -unset <key>")+    stdio.puts("")+    stdio.puts("read and write local repository configuration")+    stdio.puts("")+    stdio.puts("  vn cfg                      list all keys")+    stdio.puts("  vn cfg <key>                print a value")+    stdio.puts("  vn cfg <key> <value>        set a value")+    stdio.puts("  vn cfg -unset <key>         remove a key")+    stdio.puts("  vn cfg -global ...          use the user config instead") }  fn show_diff() void = trust {@@ -526,6 +542,10 @@     }     if cmd_is(cmd, c"tag", 3) {         show_tag()+        return 0+    }+    if cmd_is(cmd, c"cfg", 3) || cmd_is(cmd, c"config", 6) {+        show_cfg()         return 0     }     if cmd_is(cmd, c"new", 3) {--- a/veins/src/core/userconfig.sx+++ b/veins/src/core/userconfig.sx@@ -240,4 +240,183 @@ /// The configured author email, if the user file exists and defines one. pub fn author_email() option[bytes.Bytes] = trust {     return read_value(false)+}+++fn ensure_config_dir() bool = trust {+    val home = fs.home_dir_cstr()+    match home {+        some h => {+            val base: str.StringView = str.from_cstr(h)+            val local: str.StringView = files.join(+                base,+                str.from_cstr(c".local")+            )+            val config: str.StringView = files.join(+                local,+                str.from_cstr(c"config")+            )+            val dir: str.StringView = files.join(+                config,+                str.from_cstr(c"veins")+            )+            val made_local: bool = files.exists_dir(local)+            || files.mkdir_all(local)+            val made_config: bool = made_local+            && (files.exists_dir(config) || files.mkdir_all(config))+            val made_dir: bool = made_config+            && (files.exists_dir(dir) || files.mkdir_all(dir))+            files.free_path(local)+            files.free_path(config)+            files.free_path(dir)+            return made_dir+        }+        none => {+            return false+        }+    }+}++fn update_value(key: bytes.Bytes, value: bytes.Bytes, clear: bool) bool = trust {+    val want_name: bool = bytes.Bytes.eql(key, bytes.bytes(c"user.name", 9))+    val want_email: bool = bytes.Bytes.eql(key, bytes.bytes(c"user.email", 10))+    if !want_name && !want_email {+        return false+    }+    val path_opt = config_path()+    val path: mut str.StringView = str.view(none, 0)+    match path_opt {+        some p => {+            path = p+        }+        none => {+            return false+        }+    }+    if !ensure_config_dir() {+        files.free_path(path)+        return false+    }++    val kept: mut buf.Buf = buf.new(256)+    val data_opt = files.read_all(path)+    if data_opt is some data {+        val in_user: mut bool = false+        val pos: mut usize = 0+        val start: mut usize = 0+        for pos <= data.len {+            val at_end: bool = pos == data.len+            if at_end || bytes.Bytes.at(data, pos) == 10 {+                val line: bytes.Bytes = bytes.Bytes.slice(data, start, pos)+                val t: bytes.Bytes = trim(line)+                val keep: mut bool = true+                if t.len > 0 && bytes.Bytes.at(t, 0) == 91 {+                    val close: mut i64 = -1+                    val j: mut usize = 1+                    for j < t.len {+                        if bytes.Bytes.at(t, j) == 93 {+                            close = j as i64+                            break+                        }+                        j+++                    }+                    if close > 0 {+                        val table: bytes.Bytes = trim(+                            bytes.Bytes.slice(t, 1, close as usize)+                        )+                        in_user = bytes.Bytes.eql(table, bytes.bytes(c"user", 4))+                    }+                } elif t.len > 0 && bytes.Bytes.at(t, 0) != 35 {+                    val eq: mut i64 = -1+                    val k: mut usize = 0+                    for k < t.len {+                        if bytes.Bytes.at(t, k) == 61 {+                            eq = k as i64+                            break+                        }+                        k+++                    }+                    if eq >= 0 {+                        val name: bytes.Bytes = trim(+                            bytes.Bytes.slice(t, 0, eq as usize)+                        )+                        if in_user {+                            if (want_name+                                && bytes.Bytes.eql(name, bytes.bytes(c"name", 4)))+                            || (want_email+                                && bytes.Bytes.eql(name, bytes.bytes(c"email", 5))) {+                                keep = false+                            }+                        } else {+                            if (want_name+                            && bytes.Bytes.eql(name, bytes.bytes(c"user.name", 9)))+                            || (want_email+                            && bytes.Bytes.eql(+                                name,+                                bytes.bytes(c"user.email", 10)+                            )) {+                                keep = false+                            }+                        }+                    }+                }+                if keep {+                    buf.Buf.put_bytes(kept, line)+                    if !at_end {+                        buf.Buf.put_u8(kept, 10)+                    }+                }+                start = pos + 1+            }+            pos+++        }+        files.free_buffer(data)+    }++    val final: mut buf.Buf = buf.new(kept.len + 64)+    if !clear {+        if want_name {+            buf.Buf.put_bytes(final, bytes.bytes(c"user.name", 9))+        } else {+            buf.Buf.put_bytes(final, bytes.bytes(c"user.email", 10))+        }+        buf.Buf.put_bytes(final, bytes.bytes(c" = \"", 4))+        val i: mut usize = 0+        for i < value.len {+            val c: u8 = bytes.Bytes.at(value, i)+            if c == 34 {+                buf.Buf.put_bytes(final, bytes.bytes(c"\\\"", 2))+            } elif c == 92 {+                buf.Buf.put_bytes(final, bytes.bytes(c"\\\\", 2))+            } elif c == 10 {+                buf.Buf.put_bytes(final, bytes.bytes(c"\\n", 2))+            } elif c == 13 {+                buf.Buf.put_bytes(final, bytes.bytes(c"\\r", 2))+            } elif c == 9 {+                buf.Buf.put_bytes(final, bytes.bytes(c"\\t", 2))+            } else {+                buf.Buf.put_u8(final, c)+            }+            i+++        }+        buf.Buf.put_bytes(final, bytes.bytes(c"\"\n", 2))+    }+    buf.Buf.put_bytes(final, buf.Buf.view(kept))+    val out: bytes.Bytes = bytes.Bytes.dup(buf.Buf.view(final))+    val wrote: bool = files.write_all(path, out)+    files.free_buffer(out)+    buf.Buf.free(final)+    buf.Buf.free(kept)+    files.free_path(path)+    return wrote+}++/// Set a user-level identity key (`user.name` or `user.email`).+pub fn set(key: bytes.Bytes, value: bytes.Bytes) bool = trust {+    return update_value(key, value, false)+}++/// Remove a user-level identity key.+pub fn remove(key: bytes.Bytes) bool = trust {+    return update_value(key, bytes.empty(), true) }--- a/veins/src/main.sx+++ b/veins/src/main.sx@@ -33,6 +33,7 @@ val new_cmd      = use("cli/new_cmd.sx") val bookmark_cmd = use("cli/bookmark_cmd.sx") val tag_cmd      = use("cli/tag_cmd.sx")+val cfg_cmd      = use("cli/cfg_cmd.sx") val op_cmd       = use("cli/op_cmd.sx") val inf_cmd      = use("cli/inf_cmd.sx") val rm_cmd       = use("cli/rm_cmd.sx")@@ -974,6 +975,9 @@     }     if cmd_is(cmd, c"tag", 3) {         return tag_cmd.run(args, 2)+    }+    if cmd_is(cmd, c"cfg", 3) || cmd_is(cmd, c"config", 6) {+        return cfg_cmd.run(args, 2)     }     if cmd_is(cmd, c"ex", 2) {         return ex_cmd.run(args, 2)--- a/xveins/CMakeLists.txt+++ b/xveins/CMakeLists.txt@@ -147,6 +147,7 @@     src/git_object.cpp     src/git.cpp     src/tag.cpp+    src/cfg.cpp )  target_include_directories(vn PRIVATE src)--- /dev/null+++ b/xveins/src/cfg.cpp@@ -0,0 +1,223 @@+#include "local_commands.h"++#include "store.h"++#include <algorithm>+#include <iostream>+#include <map>+#include <optional>+#include <string>+#include <vector>++namespace vn {++namespace {++std::string_view cfg_trim(std::string_view text) {+  while (!text.empty() &&+         (text.front() == ' ' || text.front() == '\t' ||+          text.front() == '\r' || text.front() == '\n'))+    text.remove_prefix(1);+  while (!text.empty() &&+         (text.back() == ' ' || text.back() == '\t' ||+          text.back() == '\r' || text.back() == '\n'))+    text.remove_suffix(1);+  return text;+}++bool cfg_is_flag(std::string_view arg) {+  return !arg.empty() && arg.front() == '-';+}++std::map<std::string, std::string> repo_config_entries(const fs::path &root) {+  std::map<std::string, std::string> out;+  const auto data = read_file_opt(root / ".veins" / "config");+  if (!data)+    return out;+  std::string_view text(reinterpret_cast<const char *>(data->data()),+                        data->size());+  std::size_t start = 0;+  while (start <= text.size()) {+    std::size_t end = text.find('\n', start);+    if (end == std::string_view::npos)+      end = text.size();+    const std::string_view line = cfg_trim(text.substr(start, end - start));+    if (!line.empty() && line.front() != '#') {+      const std::size_t eq = line.find('=');+      if (eq != std::string_view::npos) {+        const std::string_view key = cfg_trim(line.substr(0, eq));+        if (!key.empty())+          out[std::string(key)] = std::string(cfg_trim(line.substr(eq + 1)));+      }+    }+    if (end == text.size())+      break;+    start = end + 1;+  }+  return out;+}++void cfg_usage() {+  std::cout << "usage: vn cfg [<key>] [<value>]\n";+  std::cout << "       vn cfg [<get|set|unset|list>] [<key>] [<value>]\n";+  std::cout << "       vn cfg -list\n";+  std::cout << "       vn cfg -unset <key>\n";+  std::cout << "\n";+  std::cout << "read and write local repository configuration\n";+  std::cout << "\n";+  std::cout << "  vn cfg                      list all keys\n";+  std::cout << "  vn cfg <key>                print a value\n";+  std::cout << "  vn cfg <key> <value>        set a value\n";+  std::cout << "  vn cfg -unset <key>         remove a key\n";+  std::cout << "  vn cfg -global ...          use the user config instead\n";+}++int cfg_list(const fs::path *root, bool global) {+  std::map<std::string, std::string> entries;+  if (global) {+    if (auto name = user_config_author_name())+      entries["user.name"] = *name;+    if (auto email = user_config_author_email())+      entries["user.email"] = *email;+  } else {+    entries = repo_config_entries(*root);+  }+  for (const auto &entry : entries)+    std::cout << entry.first << " = " << entry.second << "\n";+  return 0;+}++int cfg_get(const fs::path *root, bool global, const std::string &key) {+  std::optional<std::string> value;+  if (global) {+    if (key == "user.name")+      value = user_config_author_name();+    else if (key == "user.email")+      value = user_config_author_email();+  } else {+    value = repo_config_value(*root, key);+  }+  if (!value) {+    error_prefix();+    std::cout << "unknown config key: " << key << "\n";+    return 1;+  }+  std::cout << *value << "\n";+  return 0;+}++int cfg_set(const fs::path *root, bool global, const std::string &key,+            const std::string &value) {+  const bool ok =+      global ? user_config_set(key, value) : repo_config_set(*root, key, value);+  if (!ok) {+    error_prefix();+    std::cout << "cannot write " << (global ? "user" : "repository")+              << " config\n";+    return 1;+  }+  return 0;+}++int cfg_unset(const fs::path *root, bool global, const std::string &key) {+  const bool ok =+      global ? user_config_remove(key) : repo_config_remove(*root, key);+  if (!ok) {+    error_prefix();+    std::cout << "cannot write " << (global ? "user" : "repository")+              << " config\n";+    return 1;+  }+  return 0;+}++} // namespace++int cfg_run(const std::vector<std::string> &args) {+  if (args.size() == 3 &&+      (args[2] == "help" || args[2] == "-h" || args[2] == "--help")) {+    cfg_usage();+    return 0;+  }+  bool global = false;+  bool list = false;+  bool unset = false;+  bool end_flags = false;+  std::vector<std::string> positional;+  for (std::size_t i = 2; i < args.size(); ++i) {+    const std::string &arg = args[i];+    if (!end_flags && arg == "--") {+      end_flags = true;+    } else if (!end_flags && (arg == "-global" || arg == "--global" ||+                              arg == "-g")) {+      global = true;+    } else if (!end_flags && (arg == "-list" || arg == "--list" ||+                              arg == "-l")) {+      list = true;+    } else if (!end_flags && (arg == "-unset" || arg == "--unset")) {+      unset = true;+    } else if (!end_flags && cfg_is_flag(arg)) {+      error_prefix();+      std::cout << "unknown flag: " << arg << "\n";+      return 1;+    } else {+      positional.push_back(arg);+    }+  }++  if (positional.size() == 1 &&+      (positional[0] == "list" || positional[0] == "ls")) {+    list = true;+    positional.clear();+  } else if (positional.size() == 2 && positional[0] == "get") {+    positional.erase(positional.begin());+  } else if (positional.size() == 3 && positional[0] == "set") {+    positional.erase(positional.begin());+  } else if (positional.size() == 2 &&+             (positional[0] == "unset" || positional[0] == "rm" ||+              positional[0] == "remove")) {+    unset = true;+    positional.erase(positional.begin());+  }++  if (list) {+    if (!positional.empty()) {+      error_prefix();+      std::cout << "cfg list takes no operands\n";+      return 1;+    }+  } else if (unset) {+    if (positional.size() != 1) {+      error_prefix();+      std::cout << "cfg unset requires one key\n";+      return 1;+    }+  } else if (positional.size() > 2) {+    error_prefix();+    std::cout << "cfg accepts a key and one value\n";+    return 1;+  }++  std::optional<fs::path> root;+  if (!global) {+    root = discover_repo(".");+    if (!root) {+      error_prefix();+      std::cout << "not a veins repository\n";+      return 1;+    }+  }++  if (list)+    return cfg_list(root ? &*root : nullptr, global);+  if (unset)+    return cfg_unset(root ? &*root : nullptr, global, positional[0]);+  if (positional.empty())+    return cfg_list(root ? &*root : nullptr, global);+  if (positional.size() == 1)+    return cfg_get(root ? &*root : nullptr, global, positional[0]);+  return cfg_set(root ? &*root : nullptr, global, positional[0],+                 positional[1]);+}++} // namespace vn--- a/xveins/src/cli.cpp+++ b/xveins/src/cli.cpp@@ -1914,6 +1914,7 @@   std::cout << "  new          start a new change\n";   std::cout << "  bmk          manage named branch pointers\n";   std::cout << "  tag          manage named tags\n";+  std::cout << "  cfg          read and write configuration\n";   std::cout       << "  rm           untrack paths, keeping files on disk by default\n";   std::cout << "  mv           move a tracked file and record the rename\n";@@ -2095,6 +2096,19 @@   std::cout << "committer timestamp; equal timestamps use natural name order\n"; } +void show_cfg() {+  std::cout << "usage: vn cfg [<key>] [<value>]\n";+  std::cout << "       vn cfg [<get|set|unset|list>] [<key>] [<value>]\n";+  std::cout << "       vn cfg -list\n";+  std::cout << "       vn cfg -unset <key>\n\n";+  std::cout << "read and write local repository configuration\n\n";+  std::cout << "  vn cfg                      list all keys\n";+  std::cout << "  vn cfg <key>                print a value\n";+  std::cout << "  vn cfg <key> <value>        set a value\n";+  std::cout << "  vn cfg -unset <key>         remove a key\n";+  std::cout << "  vn cfg -global ...          use the user config instead\n";+}+ void show_rm() {   std::cout << "usage: vn rm [-delete] <path>...\n\n";   std::cout << "remove paths from the working-copy tree\n\n";@@ -2252,6 +2266,8 @@     show_bmk();   else if (cmd == "tag")     show_tag();+  else if (cmd == "cfg")+    show_cfg();   else if (cmd == "rm")     show_rm();   else if (cmd == "mv")@@ -2350,6 +2366,8 @@     return bmk_run(args);   if (cmd == "tag")     return tag_run(args);+  if (cmd == "cfg" || cmd == "config")+    return cfg_run(args);   if (cmd == "rm")     return rm_run(args);   if (cmd == "mv")--- a/xveins/src/common.cpp+++ b/xveins/src/common.cpp@@ -1374,6 +1374,117 @@ } std::optional<std::string> user_config_author_email() {   return user_config_value(false);+}++static std::string toml_quote(std::string_view value) {+  std::string out;+  out.push_back('"');+  for (char c : value) {+    switch (c) {+    case '"':+      out += "\\\"";+      break;+    case '\\':+      out += "\\\\";+      break;+    case '\n':+      out += "\\n";+      break;+    case '\r':+      out += "\\r";+      break;+    case '\t':+      out += "\\t";+      break;+    default:+      out.push_back(c);+      break;+    }+  }+  out.push_back('"');+  return out;+}++static bool update_user_config(std::string_view key,+                               const std::optional<std::string> &value) {+  const bool want_name = key == "user.name";+  const bool want_email = key == "user.email";+  if (!want_name && !want_email)+    return false;+  const fs::path path = user_config_path();+  if (path.empty())+    return false;++  std::vector<std::string> lines;+  if (const auto data = read_file_opt(path)) {+    std::string_view text(reinterpret_cast<const char *>(data->data()),+                          data->size());+    std::size_t start = 0;+    while (start <= text.size()) {+      std::size_t end = text.find('\n', start);+      if (end == std::string_view::npos)+        end = text.size();+      lines.emplace_back(text.substr(start, end - start));+      if (end == text.size())+        break;+      start = end + 1;+    }+  }++  std::vector<std::string> out;+  out.reserve(lines.size() + 1);+  bool in_user = false;+  for (const auto &line : lines) {+    const std::string_view t = trim_view(line);+    if (!t.empty() && t.front() == '[') {+      const std::size_t close = t.find(']');+      in_user = close != std::string_view::npos &&+                trim_view(t.substr(1, close - 1)) == "user";+      out.push_back(line);+      continue;+    }+    std::size_t eq = t.find('=');+    if (t.empty() || t.front() == '#' || eq == std::string_view::npos) {+      out.push_back(line);+      continue;+    }+    const std::string_view k = trim_view(t.substr(0, eq));+    const bool remove_name =+        (in_user && want_name && k == "name") ||+        (!in_user && want_name && k == "user.name");+    const bool remove_email =+        (in_user && want_email && k == "email") ||+        (!in_user && want_email && k == "user.email");+    if (!remove_name && !remove_email)+      out.push_back(line);+  }++  if (value) {+    std::string entry = want_name ? "user.name = " : "user.email = ";+    entry += toml_quote(*value);+    out.insert(out.begin(), std::move(entry));+  } else {+    while (!out.empty() && trim_view(out.back()).empty())+      out.pop_back();+  }++  std::string body;+  for (std::size_t i = 0; i < out.size(); ++i) {+    if (i != 0)+      body.push_back('\n');+    body += out[i];+  }+  if (!out.empty())+    body.push_back('\n');+  return write_file(path, bytes_of(body));+}++bool user_config_set(std::string_view key, std::string_view value) {+  return update_user_config(key, std::string(value));+}++bool user_config_remove(std::string_view key) {+  return update_user_config(key, std::nullopt); }  static std::optional<std::string> env_value(const char *name) {--- a/xveins/src/common.h+++ b/xveins/src/common.h@@ -384,6 +384,8 @@ Sig identity_signature(const fs::path &repo_root); std::optional<std::string> user_config_author_name(); std::optional<std::string> user_config_author_email();+bool user_config_set(std::string_view key, std::string_view value);+bool user_config_remove(std::string_view key);  // --------------------------------------------------------------------------- // Ignore rules--- a/xveins/src/local_commands.h+++ b/xveins/src/local_commands.h@@ -9,6 +9,7 @@ // its own arguments (args[0] is the program, args[1] the command). int bmk_run(const std::vector<std::string> &args); int tag_run(const std::vector<std::string> &args);+int cfg_run(const std::vector<std::string> &args); int rm_run(const std::vector<std::string> &args); int mv_run(const std::vector<std::string> &args); int ex_run(const std::vector<std::string> &args);