navid/veins / commit

d4cdcb663b3b

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

parents: 9d43af5809a4

Browse files at this commit · Download source

all: tag functionality
--- a/docs/SPECIFICATION_V1.adoc+++ b/docs/SPECIFICATION_V1.adoc@@ -477,6 +477,17 @@ * Change IDs are displayed in lowercase base32 (RFC 4648 alphabet), as the shortest unique prefix with a minimum of 4 characters. * If two or more visible commits share a change ID, the change is *divergent*. `vn log` MUST flag divergence, and the commits are addressed by commit ID until one is abandoned. * Commands accept change IDs, commit ID prefixes, bookmark names, and `@` (the working-copy commit) wherever a revision is expected. A change ID that resolves to one visible commit refers to that commit.++[[sec-tags]]+=== Tags++Tags are named, movable pointers to commits. They are stored in `view.tags` and sorted by name. Unlike bookmarks, tags are release markers: they are exported as git tags and do not carry remote tracking state.++* `vn tag set <name> [-rev <rev>]` creates or moves a tag. The default revision is `@`. The command snapshots the working copy first, records one operation, and rejects names that are not valid git ref names.+* `vn tag ls [-no-col-titles]` lists tags with their target commit and change ID. A `@` marker identifies the tag whose target is the working-copy commit.+* `vn tag rm <name>` deletes a tag and records one operation. Removing a non-existent tag is an error.+* `vn tag latest` prints only the tag name and no prefix or decoration. The latest tag is the tag whose target commit has the greatest committer timestamp; ties are broken by natural name order, where digit runs compare numerically (so `v0.0.10` follows `v0.0.9`). If no tags exist the command fails.+* Tags are exported to `.veins/git` as `refs/tags/<name>` by `vn git export` and are imported from git `refs/tags` by `vn git import`. `vn git push` pushes the requested bookmark; it does not push tags.  [[sec-wc]] == Working copy@@ -735,7 +746,7 @@  === Refs -Bookmarks map to git branches (`refs/heads/<name>`), and tags to git tags. A bookmark's remote tracking state is stored in `remote_refs` of the view and mirrored under `refs/remotes/<remote>/` in the mirror. Fetching moves remote refs, not local bookmarks. Local bookmarks move on explicit command or merge.+Bookmarks map to git branches (`refs/heads/<name>`), and tags to git tags (`refs/tags/<name>`). Tags are created natively with `vn tag set` or imported from git. A bookmark's remote tracking state is stored in `remote_refs` of the view and mirrored under `refs/remotes/<remote>/` in the mirror. Fetching moves remote refs, not local bookmarks. Local bookmarks move on explicit command or merge.  === Pull request workflow @@ -1080,7 +1091,7 @@ | `vn resolve` | See <<sec-merge,Merging and conflicts>>. | `vn regenerate` | Run configured regeneration commands for `merge=regenerate` paths. | `vn undo`, `vn op log`, `vn op restore <op>`, `vn op revert <op>` | Operation log commands.-| `vn bookmark set\|list\|delete`, `vn tag` | Manage named pointers.+| `vn bookmark set\|list\|delete`, `vn tag set\|ls\|rm\|latest` | Manage named pointers. | `vn fetch`, `vn push` | Native protocol. | `vn git import [-source:<gitdir>] [path]`, `vn git export`, `vn git fetch`, `vn git push`, `vn git remote` | Git bridge. | `vn serve -stdio <repo>`, `vn serve -listen <addr:port> -root <dir> ...` | Run the native server over stdio or TLS.--- a/docs/SPECIFICATION_V1.html+++ b/docs/SPECIFICATION_V1.html@@ -601,6 +601,7 @@ <li><a href="#_concurrency">5.2. Concurrency</a></li> <li><a href="#_undo_and_restore">5.3. Undo and restore</a></li> <li><a href="#_change_ids">5.4. Change IDs</a></li>+<li><a href="#sec-tags">5.5. Tags</a></li> </ul> </li> <li><a href="#sec-wc">6. Working copy</a>@@ -1680,8 +1681,33 @@ </ul> </div> </div>+<div class="sect2">+<h3 id="sec-tags">5.5. Tags</h3>+<div class="paragraph">+<p>Tags are named, movable pointers to commits. They are stored in <code>view.tags</code> and sorted by name. Unlike bookmarks, tags are release markers: they are exported as git tags and do not carry remote tracking state.</p> </div>+<div class="ulist">+<ul>+<li>+<p><code>vn tag set &lt;name&gt; [-rev &lt;rev&gt;]</code> creates or moves a tag. The default revision is <code>@</code>. The command snapshots the working copy first, records one operation, and rejects names that are not valid git ref names.</p>+</li>+<li>+<p><code>vn tag ls [-no-col-titles]</code> lists tags with their target commit and change ID. A <code>@</code> marker identifies the tag whose target is the working-copy commit.</p>+</li>+<li>+<p><code>vn tag rm &lt;name&gt;</code> deletes a tag and records one operation. Removing a non-existent tag is an error.</p>+</li>+<li>+<p><code>vn tag latest</code> prints only the tag name and no prefix or decoration. The latest tag is the tag whose target commit has the greatest committer timestamp; ties are broken by natural name order, where digit runs compare numerically (so <code>v0.0.10</code> follows <code>v0.0.9</code>). If no tags exist the command fails.</p>+</li>+<li>+<p>Tags are exported to <code>.veins/git</code> as <code>refs/tags/&lt;name&gt;</code> by <code>vn git export</code> and are imported from git <code>refs/tags</code> by <code>vn git import</code>. <code>vn git push</code> pushes the requested bookmark; it does not push tags.</p>+</li>+</ul> </div>+</div>+</div>+</div> <div class="sect1"> <h2 id="sec-wc">6. Working copy</h2> <div class="sectionbody">@@ -2253,7 +2279,7 @@ <div class="sect2"> <h3 id="_refs">8.6. Refs</h3> <div class="paragraph">-<p>Bookmarks map to git branches (<code>refs/heads/&lt;name&gt;</code>), and tags to git tags. A bookmark&#8217;s remote tracking state is stored in <code>remote_refs</code> of the view and mirrored under <code>refs/remotes/&lt;remote&gt;/</code> in the mirror. Fetching moves remote refs, not local bookmarks. Local bookmarks move on explicit command or merge.</p>+<p>Bookmarks map to git branches (<code>refs/heads/&lt;name&gt;</code>), and tags to git tags (<code>refs/tags/&lt;name&gt;</code>). Tags are created natively with <code>vn tag set</code> or imported from git. A bookmark&#8217;s remote tracking state is stored in <code>remote_refs</code> of the view and mirrored under <code>refs/remotes/&lt;remote&gt;/</code> in the mirror. Fetching moves remote refs, not local bookmarks. Local bookmarks move on explicit command or merge.</p> </div> </div> <div class="sect2">@@ -3077,7 +3103,7 @@ <td class="tableblock halign-left valign-top"><p class="tableblock">Operation log commands.</p></td> </tr> <tr>-<td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn bookmark set|list|delete</code>, <code>vn tag</code></p></td>+<td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn bookmark set|list|delete</code>, <code>vn tag set|ls|rm|latest</code></p></td> <td class="tableblock halign-left valign-top"><p class="tableblock">Manage named pointers.</p></td> </tr> <tr>--- a/veins/src/cli/help_cmd.sx+++ b/veins/src/cli/help_cmd.sx@@ -37,6 +37,7 @@     stdio.puts("  resolve      resolve a conflicted path")     stdio.puts("  new          start a new change")     stdio.puts("  bmk          manage named branch pointers")+    stdio.puts("  tag          manage named tags")     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")@@ -271,6 +272,21 @@     stdio.puts("  vn bmk set main")     stdio.puts("  vn new main")     stdio.puts("  vn co main")+}++fn show_tag() void = trust {+    stdio.puts("usage: vn tag <set|ls|rm|latest> ...")+    stdio.puts("")+    stdio.puts("manage named pointers to commits (tags)")+    stdio.puts("")+    stdio.puts("subcommands:")+    stdio.puts("  set <name> [-rev <rev>]   create or move a tag (default: @)")+    stdio.puts("  ls [-no-col-titles]       list tags")+    stdio.puts("  rm <name>                 delete a tag")+    stdio.puts("  latest                    print the latest tag name")+    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_diff() void = trust {@@ -497,6 +513,10 @@     }     if cmd_is(cmd, c"bmk", 3) {         show_bookmark()+        return 0+    }+    if cmd_is(cmd, c"tag", 3) {+        show_tag()         return 0     }     if cmd_is(cmd, c"new", 3) {--- /dev/null+++ b/veins/src/cli/tag_cmd.sx@@ -0,0 +1,680 @@+/// `vn tag`: manage named pointers to commits (tags).+///+/// `vn tag set <name>` creates or moves a tag at the working-copy commit,+/// `vn tag ls` lists tags, `vn tag rm <name>` deletes one, and+/// `vn tag latest` prints the name of the newest tag.++val str      = use("std/string")+val stdio    = use("std/stdio")+val msg      = use("../core/msg.sx")+val terminal = use("std/terminal")+val bytes    = use("../core/bytes.sx")+val buf      = use("../core/buf.sx")+val prim     = use("../core/primitives.sx")+val objid    = use("../object/id.sx")+val view     = use("../object/view.sx")+val graph    = use("../object/graph.sx")+val changeid = use("../object/changeid.sx")+val store    = use("../store/store.sx")+val layout   = use("../store/layout.sx")+val state    = use("../store/state.sx")+val txn      = use("../store/txn.sx")+val viewops  = use("../store/viewops.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)+    }+}++fn dup_z(b: bytes.Bytes) bytes.Bytes = trust {+    val out: ref u8 = @alloc(b.len + 1) as ref u8+    if b.len > 0 {+        @memcpy(out as ref void, b.ptr as ref void, b.len)+    }+    @store8(@ptradd(out, b.len as i64), 0)+    return bytes.bytes(out, b.len)+}++fn contains_seq(name: bytes.Bytes, needle: bytes.Bytes) bool = trust {+    if needle.len == 0 || needle.len > name.len {+        return false+    }+    val last: usize = name.len - needle.len+    val i: mut usize = 0+    for i <= last {+        if bytes.Bytes.eql(bytes.Bytes.slice(name, i, i + needle.len), needle) {+            return true+        }+        i+++    }+    return false+}++/// Tags are exported as git tags, so apply the same ref-name restrictions as+/// bookmarks.+fn valid_tag_name(name: bytes.Bytes) bool = trust {+    if name.len == 0 || name.len > prim.MAX_NAME_LEN {+        return false+    }+    val first: u8 = bytes.Bytes.at(name, 0)+    if first == 45 || first == 47 || first == 46 {+        return false+    }+    val last: u8 = bytes.Bytes.at(name, name.len - 1)+    if last == 47 || last == 46 {+        return false+    }+    val i: mut usize = 0+    for i < name.len {+        val c: u8 = bytes.Bytes.at(name, i)+        if c == 0 || c == 32 || c < 32 || c == 127 {+            return false+        }+        if c == 126 || c == 94 || c == 58 || c == 63 || c == 42 || c == 91 || c == 92 {+            return false+        }+        i+++    }+    if contains_seq(name, bytes.bytes(c"..", 2)) {+        return false+    }+    if contains_seq(name, bytes.bytes(c"@{", 2)) {+        return false+    }+    if contains_seq(name, bytes.bytes(c"//", 2)) {+        return false+    }+    if bytes.Bytes.eql(name, bytes.bytes(c"@", 1)) {+        return false+    }+    val lock: bytes.Bytes = bytes.bytes(c".lock", 5)+    if name.len >= lock.len+    && bytes.Bytes.eql(bytes.Bytes.slice(name, name.len - lock.len, name.len), lock) {+        return false+    }+    return true+}++fn make_description(prefix: bytes.Bytes, name: bytes.Bytes) bytes.Bytes = trust {+    val b: mut buf.Buf = buf.new(prefix.len + 1 + name.len)+    buf.Buf.put_bytes(b, prefix)+    buf.Buf.put_u8(b, 32)+    buf.Buf.put_bytes(b, name)+    val out: bytes.Bytes = bytes.Bytes.dup(buf.Buf.view(b))+    buf.Buf.free(b)+    return out+}++fn short_commit(id: objid.Id, n: usize) str.StringView = trust {+    val hex: ref u8 = objid.Id.hex(id)+    val mut_width: mut usize = 12+    if n > 0 {+        mut_width = n+    }+    if mut_width > 64 {+        mut_width = 64+    }+    return str.view(hex, mut_width as i64)+}++fn short_change(cid: objid.ChangeId) bytes.Bytes = trust {+    val full: bytes.Bytes = changeid.encode(cid)+    val width: mut usize = 8+    if full.len < width {+        width = full.len+    }+    val out: bytes.Bytes = dup_z(bytes.Bytes.slice(full, 0, width))+    changeid.free(full)+    return out+}++fn print_cell(text: str.StringView, width: usize) void = trust {+    stdio.print("{s}", {text})+    val pad: mut i64 = width as i64 - text.len+    for pad > 0 {+        stdio.print(" ", {})+        pad--+    }+}++fn find_tag(v: ref view.View, name: bytes.Bytes) option[usize] = trust {+    val i: mut usize = 0+    for i < @len(v.tags) {+        val o = @get(v.tags, i)+        if o is some t {+            if bytes.Bytes.eql(t.name, name) {+                return some i+            }+        }+        i+++    }+    return none+}++/// Compare tag names as natural versions: digit runs compare numerically so+/// v0.0.10 sorts after v0.0.9.+fn natural_compare(a: bytes.Bytes, b: bytes.Bytes) i64 = trust {+    val i: mut usize = 0+    val j: mut usize = 0+    for i < a.len && j < b.len {+        val ca: u8 = bytes.Bytes.at(a, i)+        val cb: u8 = bytes.Bytes.at(b, j)+        val da: bool = ca >= 48 && ca <= 57+        val db: bool = cb >= 48 && cb <= 57+        if da && db {+            val si: mut usize = i+            val sj: mut usize = j+            for si < a.len+            && (bytes.Bytes.at(a, si) >= 48 && bytes.Bytes.at(a, si) <= 57) {+                si+++            }+            for sj < b.len+            && (bytes.Bytes.at(b, sj) >= 48 && bytes.Bytes.at(b, sj) <= 57) {+                sj+++            }+            val ni: mut usize = i+            val nj: mut usize = j+            for ni < si && bytes.Bytes.at(a, ni) == 48 {+                ni+++            }+            for nj < sj && bytes.Bytes.at(b, nj) == 48 {+                nj+++            }+            val la: usize = si - ni+            val lb: usize = sj - nj+            if la != lb {+                if la < lb {+                    return -1+                }+                return 1+            }+            val k: mut usize = 0+            for k < la {+                val xa: u8 = bytes.Bytes.at(a, ni + k)+                val xb: u8 = bytes.Bytes.at(b, nj + k)+                if xa != xb {+                    if xa < xb {+                        return -1+                    }+                    return 1+                }+                k+++            }+            i = si+            j = sj+        } else {+            if ca != cb {+                if ca < cb {+                    return -1+                }+                return 1+            }+            i+++            j+++        }+    }+    if i < a.len {+        return 1+    }+    if j < b.len {+        return -1+    }+    return 0+}++fn run_set(repo: layout.Repo, args: list[ref u8], start: usize) i32 = trust {+    val name: mut bytes.Bytes = bytes.empty()+    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 {+                if name.len == 0 {+                    name = bytes.from_cstr(a)+                } elif rev.len == 0 {+                    rev = bytes.from_cstr(a)+                } else {+                    msg.error_prefix()+                    stdio.eprint("tag set accepts a name and one revision\n", {})+                    return 1+                }+            }+        }+        i+++    }+    if name.len == 0 {+        msg.error_prefix()+        stdio.eprint("tag set requires a name\n", {})+        return 1+    }+    if !valid_tag_name(name) {+        msg.error_prefix()+        stdio.eprint(+            "invalid tag name: {s}\n",+            {str.view(name.ptr, name.len as i64)}+        )+        return 1+    }++    val snap: i32 = snapshot.run(repo)+    if snap != 0 {+        return snap+    }+    val tx_opt = txn.begin(repo)+    val tx: mut ref txn.Txn = none+    match tx_opt {+        some t => {+            tx = t+        }+        none => {+            msg.error_prefix()+            stdio.eprint("cannot load repository state\n", {})+            return 1+        }+    }+    val st: store.Store = store.open_all(repo)+    val resolved = graph.resolve(st.handles, tx.view, rev)+    val target: mut objid.Id = objid.id_none()+    val mut_found: mut bool = false+    if resolved is some id {+        target = copy_id(id)+        mut_found = true+    }+    if !mut_found {+        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)+        txn.abort(tx)+        return 1+    }++    val existing = find_tag(tx.view, name)+    val changed: mut bool = true+    if existing is some idx {+        val to = @get(tx.view.tags, idx)+        if to is some t {+            if objid.Id.eql(t.target, target) {+                changed = false+            } else {+                t.target = copy_id(target)+            }+        }+    } else {+        val t: mut ref view.TagRef = @alloc(@sizeof(view.TagRef)) as ref view.TagRef+        t.name = bytes.Bytes.dup(name)+        t.target = copy_id(target)+        @append(tx.view.tags, t)+        viewops.sort_tags(tx.view.tags)+    }++    if !changed {+        msg.info_prefix()+        stdio.print(+            "tag {s} is already at {s}\n",+            {str.view(name.ptr, name.len as i64), short_commit(target, 0)}+        )+        store.close(st)+        txn.abort(tx)+        free_id(target)+        return 0+    }++    viewops.rebuild_heads(tx.view)+    val description: bytes.Bytes = make_description(bytes.bytes(c"tag set", 7), name)+    val status: i32 = txn.finish(tx, description)+    if status == 0 {+        msg.info_prefix()+        stdio.print(+            "tag {s} set to {s}\n",+            {str.view(name.ptr, name.len as i64), short_commit(target, 0)}+        )+    }+    val dp: mut ref void = description.ptr as ref void+    @free(dp)+    store.close(st)+    free_id(target)+    return status+}++fn run_delete(repo: layout.Repo, args: list[ref u8], start: usize) i32 = trust {+    val name: 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 && is_flag(a) {+                msg.error_prefix()+                stdio.eprint("unknown flag: {s}\n", {str.from_cstr(a)})+                return 1+            } else {+                if name.len > 0 {+                    msg.error_prefix()+                    stdio.eprint("tag rm accepts exactly one name\n", {})+                    return 1+                }+                name = bytes.from_cstr(a)+            }+        }+        i+++    }+    if name.len == 0 {+        msg.error_prefix()+        stdio.eprint("tag rm requires a name\n", {})+        return 1+    }++    val tx_opt = txn.begin(repo)+    val tx: mut ref txn.Txn = none+    match tx_opt {+        some t => {+            tx = t+        }+        none => {+            msg.error_prefix()+            stdio.eprint("cannot load repository state\n", {})+            return 1+        }+    }+    val existing = find_tag(tx.view, name)+    val idx: mut usize = 0+    val mut_found: mut bool = false+    if existing is some i2 {+        idx = i2+        mut_found = true+    }+    if !mut_found {+        msg.error_prefix()+        stdio.eprint(+            "no such tag: {s}\n",+            {str.view(name.ptr, name.len as i64)}+        )+        txn.abort(tx)+        return 1+    }+    val _ = @remove(tx.view.tags, idx)+    viewops.rebuild_heads(tx.view)+    val description: bytes.Bytes = make_description(bytes.bytes(c"tag delete", 10), name)+    val status: i32 = txn.finish(tx, description)+    if status == 0 {+        msg.info_prefix()+        stdio.print("deleted tag {s}\n", {str.view(name.ptr, name.len as i64)})+    }+    val dp: mut ref void = description.ptr as ref void+    @free(dp)+    return status+}++fn run_list(repo: layout.Repo, args: list[ref u8], start: usize) i32 = trust {+    val col_titles: mut bool = true+    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"-no-col-titles", 14) {+                col_titles = false+            } elif !end_flags && is_flag(a) {+                msg.error_prefix()+                stdio.eprint("unknown flag: {s}\n", {str.from_cstr(a)})+                return 1+            } else {+                msg.error_prefix()+                stdio.eprint("tag ls takes no operands\n", {})+                return 1+            }+        }+        i+++    }+    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 => {+            store.close(st)+            msg.error_prefix()+            stdio.eprint("no operation state\n", {})+            return 1+        }+    }+    val wc_opt = viewops.working_copy(v)+    val wc: mut objid.Id = objid.id_none()+    val have_wc: mut bool = false+    if wc_opt is some id {+        wc = id+        have_wc = true+    }+    val w_commit: usize = 12+    val w_change: usize = 8+    val w_name: mut usize = 3+    for t in v.tags {+        if t.name.len > w_name {+            w_name = t.name.len+        }+    }+    if col_titles {+        print_cell(str.from_cstr(c"Tag"), w_name)+        stdio.print("  ", {})+        print_cell(str.from_cstr(c"Commit"), w_commit)+        stdio.print("  ", {})+        print_cell(str.from_cstr(c"Change"), w_change)+        stdio.print("  Target\n", {})+    }+    for t in v.tags {+        val name_z: bytes.Bytes = dup_z(t.name)+        print_cell(str.view(name_z.ptr, name_z.len as i64), w_name)+        stdio.print("  ", {})+        val current: bool = have_wc && objid.Id.eql(t.target, wc)+        val commit_z: bytes.Bytes = dup_z(+            bytes.bytes(short_commit(t.target, 0).ptr, 12)+        )+        val change_z: mut bytes.Bytes = bytes.empty()+        val lc_opt = graph.load_commit(st.handles, t.target)+        if lc_opt is some lc {+            change_z = short_change(lc.c.change_id)+            graph.free_loaded(lc)+        }+        print_cell(str.view(commit_z.ptr, commit_z.len as i64), w_commit)+        stdio.print("  ", {})+        print_cell(str.view(change_z.ptr, change_z.len as i64), w_change)+        stdio.print("  ", {})+        if current {+            terminal.print(str.from_cstr(c"@"), terminal.Color.Red)+        }+        stdio.print("\n", {})+        val np: mut ref void = name_z.ptr as ref void+        @free(np)+        val cp: mut ref void = commit_z.ptr as ref void+        @free(cp)+        if change_z.ptr != none {+            val chp: mut ref void = change_z.ptr as ref void+            @free(chp)+        }+    }+    if @len(v.tags) == 0 {+        msg.info_prefix()+        stdio.print("no tags\n", {})+    }+    if have_wc {+        free_id(wc)+    }+    view.free_view(v)+    store.close(st)+    return 0+}++fn run_latest(repo: layout.Repo, args: list[ref u8], start: usize) i32 = trust {+    val i: mut usize = start+    for i < @len(args) {+        val o = @get(args, i)+        if o is some _ {+            msg.error_prefix()+            stdio.eprint("tag latest takes no operands\n", {})+            return 1+        }+        i+++    }+    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 => {+            store.close(st)+            msg.error_prefix()+            stdio.eprint("no operation state\n", {})+            return 1+        }+    }+    if @len(v.tags) == 0 {+        view.free_view(v)+        store.close(st)+        msg.error_prefix()+        stdio.eprint("no tags\n", {})+        return 1+    }++    val best_name: mut bytes.Bytes = bytes.empty()+    val best_time: mut i64 = -9223372036854775807+    for t in v.tags {+        val time: mut i64 = -9223372036854775807+        val lc_opt = graph.load_commit(st.handles, t.target)+        if lc_opt is some lc {+            time = lc.c.committer.seconds+            graph.free_loaded(lc)+        }+        val better: bool = best_name.len == 0 || time > best_time+        || (time == best_time && natural_compare(t.name, best_name) > 0)+        if better {+            best_name = t.name+            best_time = time+        }+    }+    val out: bytes.Bytes = dup_z(best_name)+    stdio.print("{s}\n", {str.view(out.ptr, out.len as i64)})+    val op: mut ref void = out.ptr as ref void+    @free(op)+    view.free_view(v)+    store.close(st)+    return 0+}++fn usage() void = trust {+    stdio.puts("usage: vn tag <set|ls|rm|latest> ...")+    stdio.puts("")+    stdio.puts("manage named pointers to commits (tags)")+    stdio.puts("")+    stdio.puts("subcommands:")+    stdio.puts("  set <name> [-rev <rev>]   create or move a tag (default: @)")+    stdio.puts("  ls [-no-col-titles]       list tags")+    stdio.puts("  rm <name>                 delete a tag")+    stdio.puts("  latest                    print the latest tag name")+}++pub fn run(args: list[ref u8], start: usize) i32 = trust {+    val sub_opt = @get(args, start)+    val sub: mut ref u8 = none+    match sub_opt {+        some s => {+            sub = s+        }+        none => {+            usage()+            return 1+        }+    }+    if cmd_is(sub, c"help", 4) || cmd_is(sub, c"-h", 2) {+        usage()+        return 0+    }+    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 status: mut i32 = 1+    if cmd_is(sub, c"set", 3) {+        status = run_set(repo, args, start + 1)+    } elif cmd_is(sub, c"ls", 2) || cmd_is(sub, c"list", 4) {+        status = run_list(repo, args, start + 1)+    } elif cmd_is(sub, c"rm", 2) || cmd_is(sub, c"remove", 6) {+        status = run_delete(repo, args, start + 1)+    } elif cmd_is(sub, c"latest", 6) {+        status = run_latest(repo, args, start + 1)+    } else {+        msg.error_prefix()+        stdio.eprint("unknown tag subcommand: {s}\n", {str.from_cstr(sub)})+        usage()+    }+    layout.free_repo(repo)+    return status+}--- a/veins/src/main.sx+++ b/veins/src/main.sx@@ -32,6 +32,7 @@ val resolve_cmd  = use("cli/resolve_cmd.sx") 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 op_cmd       = use("cli/op_cmd.sx") val inf_cmd      = use("cli/inf_cmd.sx") val rm_cmd       = use("cli/rm_cmd.sx")@@ -970,6 +971,9 @@     }     if cmd_is(cmd, c"bmk", 3) {         return bookmark_cmd.run(args, 2)+    }+    if cmd_is(cmd, c"tag", 3) {+        return tag_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@@ -146,6 +146,7 @@     src/net_commands.cpp     src/git_object.cpp     src/git.cpp+    src/tag.cpp )  target_include_directories(vn PRIVATE src)--- a/xveins/src/cli.cpp+++ b/xveins/src/cli.cpp@@ -1913,6 +1913,7 @@   std::cout << "  resolve      resolve a conflicted path\n";   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       << "  rm           untrack paths, keeping files on disk by default\n";   std::cout << "  mv           move a tracked file and record the rename\n";@@ -2082,6 +2083,18 @@   std::cout << "  vn co main\n"; } +void show_tag() {+  std::cout << "usage: vn tag <set|ls|rm|latest> ...\n\n";+  std::cout << "manage named pointers to commits (tags)\n\n";+  std::cout << "subcommands:\n";+  std::cout << "  set <name> [-rev <rev>]   create or move a tag (default: @)\n";+  std::cout << "  ls [-no-col-titles]       list tags\n";+  std::cout << "  rm <name>                 delete a tag\n";+  std::cout << "  latest                    print the latest tag name\n\n";+  std::cout << "the latest tag is the one whose target commit has the newest\n";+  std::cout << "committer timestamp; equal timestamps use natural name order\n";+}+ void show_rm() {   std::cout << "usage: vn rm [-delete] <path>...\n\n";   std::cout << "remove paths from the working-copy tree\n\n";@@ -2229,6 +2242,8 @@     show_fsck();   else if (cmd == "bmk")     show_bmk();+  else if (cmd == "tag")+    show_tag();   else if (cmd == "rm")     show_rm();   else if (cmd == "mv")@@ -2323,6 +2338,8 @@     return run_checkout(args);   if (cmd == "bmk")     return bmk_run(args);+  if (cmd == "tag")+    return tag_run(args);   if (cmd == "rm")     return rm_run(args);   if (cmd == "mv")--- a/xveins/src/local_commands.h+++ b/xveins/src/local_commands.h@@ -8,6 +8,7 @@ // Local-only commands. Each receives the full argv-style vector and indexes // 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 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);--- /dev/null+++ b/xveins/src/tag.cpp@@ -0,0 +1,424 @@+#include "local_commands.h"++#include "store.h"+#include "worktree.h"++#include <algorithm>+#include <cstring>+#include <iostream>+#include <limits>++namespace vn {++namespace {++bool tag_is_flag(std::string_view arg) {+  return !arg.empty() && arg.front() == '-';+}++bool tag_contains(std::string_view name, std::string_view needle) {+  return !needle.empty() && name.find(needle) != std::string_view::npos;+}++bool valid_tag_name(std::string_view name) {+  if (name.empty() || name.size() > MAX_NAME_LEN)+    return false;+  const auto first = static_cast<unsigned char>(name.front());+  if (first == '-' || first == '/' || first == '.')+    return false;+  const auto last = static_cast<unsigned char>(name.back());+  if (last == '/' || last == '.')+    return false;+  for (char raw : name) {+    const auto c = static_cast<unsigned char>(raw);+    if (c == 0 || c == ' ' || c < 32 || c == 127)+      return false;+    if (c == '~' || c == '^' || c == ':' || c == '?' || c == '*' ||+        c == '[' || c == '\\')+      return false;+  }+  if (tag_contains(name, "..") || tag_contains(name, "@{") ||+      tag_contains(name, "//"))+    return false;+  if (name == "@")+    return false;+  if (name.size() >= 5 && name.substr(name.size() - 5) == ".lock")+    return false;+  return true;+}++std::string short_commit(const Id &id, std::size_t n = 12) {+  if (n == 0)+    n = 12;+  n = std::min<std::size_t>(n, 64);+  return hex_encode(id).substr(0, n);+}++void print_cell(std::string_view text, std::size_t width) {+  std::cout << text;+  for (std::size_t i = text.size(); i < width; ++i)+    std::cout << ' ';+}++bool is_digit(char c) { return c >= '0' && c <= '9'; }++std::size_t skip_leading_zeroes(std::string_view text, std::size_t begin,+                                std::size_t end) {+  while (begin < end && text[begin] == '0')+    ++begin;+  return begin;+}++// Compare tag names as natural versions so v0.0.10 sorts after v0.0.9.+int natural_compare(std::string_view a, std::string_view b) {+  std::size_t i = 0;+  std::size_t j = 0;+  while (i < a.size() && j < b.size()) {+    if (is_digit(a[i]) && is_digit(b[j])) {+      std::size_t ai = i;+      std::size_t bj = j;+      while (ai < a.size() && is_digit(a[ai]))+        ++ai;+      while (bj < b.size() && is_digit(b[bj]))+        ++bj;+      const std::size_t an = skip_leading_zeroes(a, i, ai);+      const std::size_t bn = skip_leading_zeroes(b, j, bj);+      const std::size_t alen = ai - an;+      const std::size_t blen = bj - bn;+      if (alen != blen)+        return alen < blen ? -1 : 1;+      for (std::size_t k = 0; k < alen; ++k) {+        const auto ac = static_cast<unsigned char>(a[an + k]);+        const auto bc = static_cast<unsigned char>(b[bn + k]);+        if (ac != bc)+          return ac < bc ? -1 : 1;+      }+      i = ai;+      j = bj;+    } else {+      const auto ac = static_cast<unsigned char>(a[i]);+      const auto bc = static_cast<unsigned char>(b[j]);+      if (ac != bc)+        return ac < bc ? -1 : 1;+      ++i;+      ++j;+    }+  }+  if (i < a.size())+    return 1;+  if (j < b.size())+    return -1;+  return 0;+}++int tag_set(const std::vector<std::string> &args, std::size_t start) {+  std::string name;+  std::string rev;+  bool end_flags = false;+  for (std::size_t i = start; i < args.size(); ++i) {+    const std::string &arg = args[i];+    if (!end_flags && arg == "--") {+      end_flags = true;+    } else if (!end_flags && (arg == "-r" || arg == "-rev")) {+      if (i + 1 >= args.size()) {+        error_prefix();+        std::cout << arg << " requires a revision\n";+        return 1;+      }+      rev = args[++i];+    } else if (!end_flags && tag_is_flag(arg)) {+      error_prefix();+      std::cout << "unknown flag: " << arg << "\n";+      return 1;+    } else if (name.empty()) {+      name = arg;+    } else if (rev.empty()) {+      rev = arg;+    } else {+      error_prefix();+      std::cout << "tag set accepts a name and one revision\n";+      return 1;+    }+  }+  if (name.empty()) {+    error_prefix();+    std::cout << "tag set requires a name\n";+    return 1;+  }+  if (!valid_tag_name(name)) {+    error_prefix();+    std::cout << "invalid tag name: " << name << "\n";+    return 1;+  }++  const auto root = discover_repo(".");+  if (!root) {+    error_prefix();+    std::cout << "not a veins repository\n";+    return 1;+  }+  const int snap = snapshot_run(*root);+  if (snap != 0)+    return snap;++  Store store = Store::open_all(*root);+  auto tx = Txn::begin(*root, store);+  if (!tx) {+    error_prefix();+    std::cout << "cannot load repository state\n";+    return 1;+  }+  auto target = resolve_revision(store, tx->view(), rev);+  if (!target) {+    error_prefix();+    if (rev.empty())+      std::cout << "no working-copy commit\n";+    else+      std::cout << "unknown revision: " << rev << "\n";+    return 1;+  }++  bool changed = true;+  if (const auto existing = find_tag(tx->view(), name)) {+    auto &tag = tx->view().tags[*existing];+    if (std::memcmp(tag.target.data(), target->data(), ID_LEN) == 0) {+      changed = false;+    } else {+      tag.target = *target;+    }+  } else {+    tx->view().tags.push_back(TagRef{name, *target});+  }++  if (!changed) {+    info_prefix();+    std::cout << "tag " << name << " is already at " << short_commit(*target)+              << "\n";+    return 0;+  }++  rebuild_heads(tx->view());+  const auto status = tx->finish("tag set " + name);+  if (!status)+    return 1;+  info_prefix();+  std::cout << "tag " << name << " set to " << short_commit(*target) << "\n";+  return 0;+}++int tag_delete(const std::vector<std::string> &args, std::size_t start) {+  std::string name;+  bool end_flags = false;+  for (std::size_t i = start; i < args.size(); ++i) {+    const std::string &arg = args[i];+    if (!end_flags && arg == "--") {+      end_flags = true;+    } else if (!end_flags && tag_is_flag(arg)) {+      error_prefix();+      std::cout << "unknown flag: " << arg << "\n";+      return 1;+    } else if (!name.empty()) {+      error_prefix();+      std::cout << "tag rm accepts exactly one name\n";+      return 1;+    } else {+      name = arg;+    }+  }+  if (name.empty()) {+    error_prefix();+    std::cout << "tag rm requires a name\n";+    return 1;+  }++  const auto root = discover_repo(".");+  if (!root) {+    error_prefix();+    std::cout << "not a veins repository\n";+    return 1;+  }+  Store store = Store::open_all(*root);+  auto tx = Txn::begin(*root, store);+  if (!tx) {+    error_prefix();+    std::cout << "cannot load repository state\n";+    return 1;+  }+  const auto existing = find_tag(tx->view(), name);+  if (!existing) {+    error_prefix();+    std::cout << "no such tag: " << name << "\n";+    return 1;+  }+  auto &tags = tx->view().tags;+  tags.erase(tags.begin() + static_cast<std::ptrdiff_t>(*existing));+  rebuild_heads(tx->view());+  const auto status = tx->finish("tag delete " + name);+  if (!status)+    return 1;+  info_prefix();+  std::cout << "deleted tag " << name << "\n";+  return 0;+}++int tag_list(const std::vector<std::string> &args, std::size_t start) {+  bool col_titles = true;+  bool end_flags = false;+  for (std::size_t i = start; i < args.size(); ++i) {+    const std::string &arg = args[i];+    if (!end_flags && arg == "--") {+      end_flags = true;+    } else if (!end_flags && arg == "-no-col-titles") {+      col_titles = false;+    } else if (!end_flags && tag_is_flag(arg)) {+      error_prefix();+      std::cout << "unknown flag: " << arg << "\n";+      return 1;+    } else {+      error_prefix();+      std::cout << "tag ls takes no operands\n";+      return 1;+    }+  }++  const auto root = discover_repo(".");+  if (!root) {+    error_prefix();+    std::cout << "not a veins repository\n";+    return 1;+  }+  Store store = Store::open_all(*root);+  auto view = current_view(*root, store);+  if (!view) {+    error_prefix();+    std::cout << "no operation state\n";+    return 1;+  }+  const auto wc = working_copy_id(*view);++  constexpr std::size_t w_commit = 12;+  constexpr std::size_t w_change = 8;+  std::size_t w_name = 3;+  for (const auto &tag : view->tags)+    w_name = std::max(w_name, tag.name.size());++  if (col_titles) {+    print_cell("Tag", w_name);+    std::cout << "  ";+    print_cell("Commit", w_commit);+    std::cout << "  ";+    print_cell("Change", w_change);+    std::cout << "  Target\n";+  }++  for (const auto &tag : view->tags) {+    print_cell(tag.name, w_name);+    std::cout << "  ";+    print_cell(short_commit(tag.target), w_commit);+    std::cout << "  ";+    std::string change_text;+    if (auto loaded = load_commit(store, tag.target))+      change_text = base32_encode(loaded->commit.change_id).substr(0, 8);+    print_cell(change_text, w_change);+    std::cout << "  ";+    if (wc && std::memcmp(wc->data(), tag.target.data(), ID_LEN) == 0)+      print_colored("@", Color::Red, false, false);+    std::cout << "\n";+  }++  if (view->tags.empty()) {+    info_prefix();+    std::cout << "no tags\n";+  }+  return 0;+}++int tag_latest(const std::vector<std::string> &args, std::size_t start) {+  for (std::size_t i = start; i < args.size(); ++i) {+    error_prefix();+    std::cout << "tag latest takes no operands\n";+    return 1;+  }++  const auto root = discover_repo(".");+  if (!root) {+    error_prefix();+    std::cout << "not a veins repository\n";+    return 1;+  }+  Store store = Store::open_all(*root);+  auto view = current_view(*root, store);+  if (!view) {+    error_prefix();+    std::cout << "no operation state\n";+    return 1;+  }+  if (view->tags.empty()) {+    error_prefix();+    std::cout << "no tags\n";+    return 1;+  }++  const TagRef *best = nullptr;+  std::int64_t best_time = std::numeric_limits<std::int64_t>::min();+  for (const auto &tag : view->tags) {+    std::int64_t time = std::numeric_limits<std::int64_t>::min();+    if (auto loaded = load_commit(store, tag.target))+      time = loaded->commit.committer.seconds;+    if (best == nullptr || time > best_time ||+        (time == best_time && natural_compare(tag.name, best->name) > 0)) {+      best = &tag;+      best_time = time;+    }+  }+  if (!best) {+    error_prefix();+    std::cout << "no tags\n";+    return 1;+  }+  std::cout << best->name << "\n";+  return 0;+}++void tag_usage() {+  std::cout << "usage: vn tag <set|ls|rm|latest> ...\n";+  std::cout << "\n";+  std::cout << "manage named pointers to commits (tags)\n";+  std::cout << "\n";+  std::cout << "subcommands:\n";+  std::cout << "  set <name> [-rev <rev>]   create or move a tag (default: @)\n";+  std::cout << "  ls [-no-col-titles]       list tags\n";+  std::cout << "  rm <name>                 delete a tag\n";+  std::cout << "  latest                    print the latest tag name\n";+  std::cout << "\n";+  std::cout << "the latest tag is the one whose target commit has the newest\n";+  std::cout << "committer timestamp; equal timestamps use natural name order\n";+}++} // namespace++int tag_run(const std::vector<std::string> &args) {+  if (args.size() <= 2) {+    tag_usage();+    return 1;+  }+  const std::string &sub = args[2];+  if (sub == "set")+    return tag_set(args, 3);+  if (sub == "ls" || sub == "list")+    return tag_list(args, 3);+  if (sub == "rm" || sub == "remove")+    return tag_delete(args, 3);+  if (sub == "latest")+    return tag_latest(args, 3);+  if (sub == "help" || sub == "-h") {+    tag_usage();+    return 0;+  }+  error_prefix();+  std::cout << "unknown tag subcommand: " << sub << "\n";+  tag_usage();+  return 1;+}++} // namespace vn