navid/veins / commit
226bac9f9ed2
Navid M <navidmomtahen@gmail.com> · 2026-11-26
parents: f49cc07d0945
Browse files at this commit · Download source
--- a/docs/SPECIFICATION_V1.adoc+++ b/docs/SPECIFICATION_V1.adoc@@ -46,7 +46,7 @@ === Naming -The product and file format are named *veins*. The command-line program is named `vn` (`vn.exe` on Windows). All command examples in this document use `vn`. The name `veins` is used for the repository directory (`.veins/`), configuration and attribute files (`.veinsignore`, `.veinsattributes`), the object-ID domain separator, and reserved header keys (`veins-change-id`, `veins-sig`). Conforming implementations of the format are not required to ship a program called `vn`.+The product and file format are named *veins*. The command-line program is named `vn` (`vn.exe` on Windows). All command examples in this document use `vn`. The name `veins` is used for the repository directory (`.veins/`), configuration and attribute files (`.vnexclude`, `.veinsattributes`), the object-ID domain separator, and reserved header keys (`veins-change-id`, `veins-sig`). Conforming implementations of the format are not required to ship a program called `vn`. === Conventions @@ -502,9 +502,15 @@ * Tracked state is determined by the tree of the working-copy commit, plus any new files that are not ignored. * veins MUST keep a stat cache (path, size, mtime in nanoseconds, inode, content ID) in `.veins/working_copy/`. A file whose stat data is unchanged MUST NOT be re-read. A file whose mtime is within a "racy" window (less than 2 seconds old relative to the cache write) MUST be re-verified by content. * New or changed files are chunked and hashed in parallel. Files are only read once.-* Ignore rules come from `.vnexclude` (gitignore syntax). If `ignore.git = true` (default), `.gitignore` files are also honored.+* Ignore rules come from `.vnexclude` (gitignore syntax), which `vn ex` appends to. If `ignore.git = true` (default), `.gitignore` files are also honored. * File modes: only the owner execute bit is tracked. Line endings are not translated; bytes on disk are bytes in the store. * Exact renames (a deleted path and an added path with the same content ID) are recorded automatically in the snapshot commit's `renames`. With `rename.autodetect = similar` (default), added/deleted pairs with similarity of at least 60% are also recorded. `rename.autodetect = off` records only renames made with `vn mv`.++=== Untracking paths++`vn rm [-delete] <path>...` removes paths from the working-copy tree. By default the files remain on disk; `-delete` also removes the matched tracked files from disk. Arguments are resolved relative to the current directory and may be literal paths or globs using `*`, `?`, and `**`. `*` and `?` do not cross directory separators. If any argument does not match a tracked path, the command MUST fail without changing the working-copy tree.++Because every command snapshots the working copy first, a path that remains on disk and is not ignored will be added back to the working-copy tree by the next snapshot. `vn rm` MUST warn about such paths. `vn ex <pattern>...` appends gitignore-style patterns to the repository-root `.vnexclude` so that removed paths stay untracked. A successful `vn rm` records one operation. === Checkout safety @@ -1065,7 +1071,8 @@ | `vn log [-rev <revset>] [-no-col-titles]` | Show history, newest first, flagging conflicted and divergent changes. | `vn show <rev>`, `vn diff [-rev <rev>]` | Display a commit or a diff. | `vn new [<rev>...]`, `vn describe [-message <message>]`, `vn commit -message <message>` | Create and describe changes. `vn commit` does nothing when the working copy has no changes.-| `vn mv`, `vn rm` | Move or remove tracked files, recording renames.+| `vn mv` | Move tracked files, recording renames.+| `vn rm [-delete] <path>...`, `vn ex <pattern>...` | Untrack paths from the working copy (files stay on disk by default), or add ignore patterns to `.vnexclude`. | `vn merge <rev>...` | Create a merge commit. Never fails; may produce conflicts. | `vn rebase -s <rev> -d <rev>` | Rewrite commits onto a new parent, auto-rebasing descendants. | `vn squash`, `vn split`, `vn abandon` | Rewrite or remove changes.--- 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.20">+<meta name="generator" content="Asciidoctor 2.0.26"> <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,7 +116,6 @@ :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}@@ -140,7 +139,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,body.toc2 #header>h1:nth-last-child(2){border-bottom:1px solid #dddddf;padding-bottom:8px}+#header>h1:only-child{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)}@@ -162,6 +161,7 @@ #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,6 +218,7 @@ .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}@@ -327,7 +328,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{text-decoration:underline}+sup.footnote a:active,sup.footnoteref a:active,#footnotes .footnote a:first-of-type: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}@@ -433,6 +434,120 @@ @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">@@ -493,7 +608,8 @@ <li><a href="#_model_review">6.1. Model <strong>(review)</strong></a></li> <li><a href="#_empty_changes">6.2. Empty changes</a></li> <li><a href="#_snapshotting">6.3. Snapshotting</a></li>-<li><a href="#_checkout_safety">6.4. Checkout safety</a></li>+<li><a href="#_untracking_paths">6.4. Untracking paths</a></li>+<li><a href="#_checkout_safety">6.5. Checkout safety</a></li> </ul> </li> <li><a href="#sec-merge">7. Merging and conflicts</a>@@ -668,7 +784,7 @@ <div class="sect2"> <h3 id="_naming">1.4. Naming</h3> <div class="paragraph">-<p>The product and file format are named <strong>veins</strong>. The command-line program is named <code>vn</code> (<code>vn.exe</code> on Windows). All command examples in this document use <code>vn</code>. The name <code>veins</code> is used for the repository directory (<code>.veins/</code>), configuration and attribute files (<code>.veinsignore</code>, <code>.veinsattributes</code>), the object-ID domain separator, and reserved header keys (<code>veins-change-id</code>, <code>veins-sig</code>). Conforming implementations of the format are not required to ship a program called <code>vn</code>.</p>+<p>The product and file format are named <strong>veins</strong>. The command-line program is named <code>vn</code> (<code>vn.exe</code> on Windows). All command examples in this document use <code>vn</code>. The name <code>veins</code> is used for the repository directory (<code>.veins/</code>), configuration and attribute files (<code>.vnexclude</code>, <code>.veinsattributes</code>), the object-ID domain separator, and reserved header keys (<code>veins-change-id</code>, <code>veins-sig</code>). Conforming implementations of the format are not required to ship a program called <code>vn</code>.</p> </div> </div> <div class="sect2">@@ -679,7 +795,10 @@ <div class="paragraph"> <p>Command-line options use Plan 9 style: a long option is a single dash followed by its name (for example <code>-message</code>, <code>-rev</code>, and <code>-verbose</code>). Short aliases MAY also be accepted. An unrecognized option MUST be reported as an error and MUST NOT be treated as an operand. <code>--</code> terminates option parsing; every argument after it is an operand.</p> </div>+<div class="paragraph">+<p>User-facing diagnostics use level prefixes: <code>error:</code> for failures, <code>warning:</code> for non-fatal problems, and <code>info:</code> for informational results. Prefixes are colored when standard output/stderr is a terminal and <code>NO_COLOR</code> is unset.</p> </div>+</div> <div class="sect2"> <h3 id="_terminology">1.6. Terminology</h3> <table class="tableblock frame-all grid-all stretch">@@ -1611,7 +1730,7 @@ <p>New or changed files are chunked and hashed in parallel. Files are only read once.</p> </li> <li>-<p>Ignore rules come from <code>.vnexclude</code> (gitignore syntax). If <code>ignore.git = true</code> (default), <code>.gitignore</code> files are also honored.</p>+<p>Ignore rules come from <code>.vnexclude</code> (gitignore syntax), which <code>vn ex</code> appends to. If <code>ignore.git = true</code> (default), <code>.gitignore</code> files are also honored.</p> </li> <li> <p>File modes: only the owner execute bit is tracked. Line endings are not translated; bytes on disk are bytes in the store.</p>@@ -1623,8 +1742,17 @@ </div> </div> <div class="sect2">-<h3 id="_checkout_safety">6.4. Checkout safety</h3>+<h3 id="_untracking_paths">6.4. Untracking paths</h3> <div class="paragraph">+<p><code>vn rm [-delete] <path>…​</code> removes paths from the working-copy tree. By default the files remain on disk; <code>-delete</code> also removes the matched tracked files from disk. Arguments are resolved relative to the current directory and may be literal paths or globs using <code><strong></code>, <code>?</code>, and <code></strong>*</code>. <code>*</code> and <code>?</code> do not cross directory separators. If any argument does not match a tracked path, the command MUST fail without changing the working-copy tree.</p>+</div>+<div class="paragraph">+<p>Because every command snapshots the working copy first, a path that remains on disk and is not ignored will be added back to the working-copy tree by the next snapshot. <code>vn rm</code> MUST warn about such paths. <code>vn ex <pattern>…​</code> appends gitignore-style patterns to the repository-root <code>.vnexclude</code> so that removed paths stay untracked. A successful <code>vn rm</code> records one operation.</p>+</div>+</div>+<div class="sect2">+<h3 id="_checkout_safety">6.5. Checkout safety</h3>+<div class="paragraph"> <p>A checkout or update that would overwrite or delete a file MUST do so only if the file’s current content matches the snapshot (i.e., it was snapshotted first). Because every command snapshots first, local edits are never lost.</p> </div> <div class="paragraph">@@ -2901,8 +3029,8 @@ <td class="tableblock halign-left valign-top"><p class="tableblock">Working-copy changes, conflicts, files needing regeneration.</p></td> </tr> <tr>-<td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn log [-rev <revset>]</code></p></td>-<td class="tableblock halign-left valign-top"><p class="tableblock">Show history, flagging conflicted and divergent changes.</p></td>+<td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn log [-rev <revset>] [-no-col-titles]</code></p></td>+<td class="tableblock halign-left valign-top"><p class="tableblock">Show history, newest first, flagging conflicted and divergent changes.</p></td> </tr> <tr> <td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn show <rev></code>, <code>vn diff [-rev <rev>]</code></p></td>@@ -2913,10 +3041,14 @@ <td class="tableblock halign-left valign-top"><p class="tableblock">Create and describe changes. <code>vn commit</code> does nothing when the working copy has no changes.</p></td> </tr> <tr>-<td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn mv</code>, <code>vn rm</code></p></td>-<td class="tableblock halign-left valign-top"><p class="tableblock">Move or remove tracked files, recording renames.</p></td>+<td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn mv</code></p></td>+<td class="tableblock halign-left valign-top"><p class="tableblock">Move tracked files, recording renames.</p></td> </tr> <tr>+<td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn rm [-delete] <path>…​</code>, <code>vn ex <pattern>…​</code></p></td>+<td class="tableblock halign-left valign-top"><p class="tableblock">Untrack paths from the working copy (files stay on disk by default), or add ignore patterns to <code>.vnexclude</code>.</p></td>+</tr>+<tr> <td class="tableblock halign-left valign-top"><p class="tableblock"><code>vn merge <rev>…​</code></p></td> <td class="tableblock halign-left valign-top"><p class="tableblock">Create a merge commit. Never fails; may produce conflicts.</p></td> </tr>@@ -2987,6 +3119,12 @@ </tbody> </table> <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">+<p><code>vn status</code> colors its change markers (<code>A</code> bright green, <code>M</code> cyan, <code>D</code> red, and <code>C</code> bright red). <code>vn diff</code> prints unified hunks with context; removed lines are red and added lines are green.</p>+</div>+<div class="paragraph"> <p>All commands that change state MUST be atomic with respect to the operation log: they either produce one operation or leave the repository unchanged.</p> </div> </div>@@ -3371,7 +3509,7 @@ <div id="footer"> <div id="footer-text"> Version 0.1-draft<br>-Last updated 2026-11-26 01:38:50 UTC+Last updated 2026-11-26 10:52:45 UTC </div> </div> </body>--- /dev/null+++ b/veins/src/cli/ex_cmd.sx@@ -0,0 +1,258 @@+/// `vn ex <pattern>...`: append ignore patterns to the repository's+/// root `.vnexclude` file.+///+/// Patterns use gitignore-style syntax. Shell users should quote patterns that+/// contain wildcards (`vn ex '*.dSYM'`) so the shell does not expand them into+/// individual file names first.++val str = use("std/string")+val stdio = use("std/stdio")+val msg = use("../core/msg.sx")+val bytes = use("../core/bytes.sx")+val buf = use("../core/buf.sx")+val lists = use("../core/lists.sx")+val names = use("../object/names.sx")+val layout = use("../store/layout.sx")+val pathspec = use("../worktree/pathspec.sx")+val files = use("../io/files.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 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 line_exists(data: bytes.Bytes, pattern: bytes.Bytes) bool = trust {+ if data.len == 0 {+ return 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 = trim(bytes.Bytes.slice(data, start, pos))+ if bytes.Bytes.eql(line, pattern) {+ return true+ }+ start = pos + 1+ }+ pos+++ }+ return false+}++fn contains_pattern(xs: list[bytes.Bytes], pattern: bytes.Bytes) bool = trust {+ for p in xs {+ if bytes.Bytes.eql(p, pattern) {+ return true+ }+ }+ return false+}++fn free_paths(xs: list[bytes.Bytes]) void = trust {+ for p in xs {+ if p.ptr != none {+ val pp: mut ref void = p.ptr as ref void+ @free(pp)+ }+ }+ @listfree(xs)+}++pub fn run(args: list[ref u8], start: usize) i32 = trust {+ val operands: mut list[ref u8] = lists.new[ref u8]()+ 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)})+ @listfree(operands)+ return 1+ } else {+ @append(operands, a)+ }+ }+ i+++ }+ if @len(operands) == 0 {+ msg.error_prefix()+ stdio.eprint("ex requires at least one pattern\n", {})+ @listfree(operands)+ return 1+ }++ 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", {})+ @listfree(operands)+ return 1+ }+ }++ val cwd_opt = pathspec.cwd_relative(repo.root)+ val cwd_rel: mut bytes.Bytes = bytes.empty()+ match cwd_opt {+ some c => {+ cwd_rel = c+ }+ none => {+ msg.error_prefix()+ stdio.eprint("cannot determine current directory\n", {})+ @listfree(operands)+ layout.free_repo(repo)+ return 1+ }+ }++ val patterns: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()+ val bad: mut bool = false+ for a in operands {+ val arg: bytes.Bytes = bytes.from_cstr(a)+ val norm_opt = pathspec.normalize(repo.root, cwd_rel, arg)+ val norm: mut bytes.Bytes = bytes.empty()+ match norm_opt {+ some n => {+ norm = n+ }+ none => {+ msg.error_prefix()+ stdio.eprint(+ "pattern is outside the repository: {s}\n",+ {str.view(arg.ptr, arg.len as i64)}+ )+ bad = true+ }+ }+ if bad {+ if norm.ptr != none {+ val np: mut ref void = norm.ptr as ref void+ @free(np)+ }+ break+ }+ if norm.len == 0 || !names.valid_path(norm) {+ msg.error_prefix()+ stdio.eprint("{s}\n", {str.view(arg.ptr, arg.len as i64)})+ stdio.eprint("not a valid exclude pattern\n", {})+ if norm.ptr != none {+ val np2: mut ref void = norm.ptr as ref void+ @free(np2)+ }+ bad = true+ break+ }+ @append(patterns, norm)+ }+ if bad {+ if cwd_rel.ptr != none {+ val cp: mut ref void = cwd_rel.ptr as ref void+ @free(cp)+ }+ free_paths(patterns)+ @listfree(operands)+ layout.free_repo(repo)+ return 1+ }++ val exclude_path: str.StringView = files.join(+ layout.root_view(repo),+ str.from_cstr(c".vnexclude")+ )+ val data_opt = files.read_all(exclude_path)+ val data: mut bytes.Bytes = bytes.empty()+ match data_opt {+ some d => {+ data = d+ }+ none => {+ }+ }++ val additions: mut buf.Buf = buf.new(64)+ val added: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()+ for pattern in patterns {+ if !line_exists(data, pattern) && !contains_pattern(added, pattern) {+ if buf.Buf.view(additions).len == 0 && data.len > 0 {+ if bytes.Bytes.at(data, data.len - 1) != 10 {+ buf.Buf.put_u8(additions, 10)+ }+ }+ @append(added, pattern)+ buf.Buf.put_bytes(additions, pattern)+ buf.Buf.put_u8(additions, 10)+ }+ }++ val count: usize = @len(added)+ val status: mut i32 = 0+ if count == 0 {+ msg.info_prefix()+ stdio.print("no new exclude rules\n", {})+ } else {+ val wrote: bool = files.append_all(exclude_path, buf.Buf.view(additions))+ if !wrote {+ msg.error_prefix()+ stdio.eprint("cannot write .vnexclude\n", {})+ status = 1+ } else {+ msg.info_prefix()+ stdio.print("added {d} exclude rule(s)\n", {count})+ }+ }++ buf.Buf.free(additions)+ @listfree(added)+ if data.ptr != none {+ files.free_buffer(data)+ }+ files.free_path(exclude_path)+ if cwd_rel.ptr != none {+ val cp2: mut ref void = cwd_rel.ptr as ref void+ @free(cp2)+ }+ free_paths(patterns)+ @listfree(operands)+ layout.free_repo(repo)+ return status+}--- a/veins/src/cli/help_cmd.sx+++ b/veins/src/cli/help_cmd.sx@@ -20,7 +20,9 @@ stdio.puts(" log show commit history") stdio.puts(" diff show a unified diff") stdio.puts(" new start a new change")- stdio.puts(" desc set the working-copy commit messag; alias: describe")+ stdio.puts(" rm untrack paths, keeping files on disk by default")+ stdio.puts(" ex add paths or globs to .vnexclude")+ stdio.puts(" desc set the working-copy commit message; alias: describe") stdio.puts(" go describe and start a new change; alias: commit") stdio.puts(" undo restore the previous operation view") stdio.puts(" op operation log commands")@@ -145,6 +147,29 @@ stdio.puts("usage: vn new [<rev>...]") stdio.puts("") stdio.puts("start a new empty change on top of the working copy or a revision")+}++fn show_rm() void = trust {+ stdio.puts("usage: vn rm [-delete] <path>...")+ stdio.puts("")+ stdio.puts("remove paths from the working-copy tree")+ stdio.puts("")+ stdio.puts("files stay on disk by default. Arguments may be paths or globs and are")+ stdio.puts("resolved relative to the current directory. Supported wildcards are")+ stdio.puts("'*', '?', and '**'; '*' and '?' do not cross directory separators.")+ stdio.puts("")+ stdio.puts("options:")+ stdio.puts(" -delete delete matching files from disk after untracking")+}++fn show_ex() void = trust {+ stdio.puts("usage: vn ex <pattern>...")+ stdio.puts("")+ stdio.puts("append ignore patterns to the repository root .vnexclude")+ stdio.puts("")+ stdio.puts("patterns use gitignore syntax and are resolved relative to the current")+ stdio.puts("directory. Quote wildcard patterns so the shell does not expand them:")+ stdio.puts(" vn ex '*.dSYM'") } fn show_describe() void = trust {@@ -229,6 +254,14 @@ } if cmd_is(cmd, c"new", 3) { show_new()+ return 0+ }+ if cmd_is(cmd, c"rm", 2) {+ show_rm()+ return 0+ }+ if cmd_is(cmd, c"ex", 2) {+ show_ex() return 0 } if cmd_is(cmd, c"desc", 4) || cmd_is(cmd, c"describe", 8) {--- /dev/null+++ b/veins/src/cli/rm_cmd.sx@@ -0,0 +1,660 @@+/// `vn rm [-delete] <path>...`: remove paths from the working-copy tree while+/// keeping the files on disk by default.+///+/// Arguments are repository paths or globs resolved against the current+/// directory. Supported wildcards are `*`, `?`, and `**`; `*` and `?` do not+/// cross directory separators. Untracking a file that is not covered by+/// `.vnexclude` or `.gitignore` only lasts until the next snapshot, so `rm`+/// warns in that case.++val str = use("std/string")+val stdio = use("std/stdio")+val msg = use("../core/msg.sx")+val fs = use("std/fs")+val bytes = use("../core/bytes.sx")+val lists = use("../core/lists.sx")+val prim = use("../core/primitives.sx")+val objid = use("../object/id.sx")+val types = use("../object/types.sx")+val names = use("../object/names.sx")+val commit = use("../object/commit.sx")+val graph = use("../object/graph.sx")+val tree = use("../object/tree.sx")+val treewalk = use("../object/treewalk.sx")+val store = use("../store/store.sx")+val layout = use("../store/layout.sx")+val txn = use("../store/txn.sx")+val viewops = use("../store/viewops.sx")+val identity = use("../core/identity.sx")+val gitmap = use("../git/map.sx")+val snapshot = use("../worktree/snapshot.sx")+val ignore = use("../worktree/ignore.sx")+val pathspec = use("../worktree/pathspec.sx")+val files = use("../io/files.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 free_path_list(xs: list[bytes.Bytes]) void = trust {+ for p in xs {+ if p.ptr != none {+ val pp: mut ref void = p.ptr as ref void+ @free(pp)+ }+ }+ @listfree(xs)+}++fn copy_kvs(xs: list[ref prim.KeyValue]) list[ref prim.KeyValue] = trust {+ val out: mut list[ref prim.KeyValue] = lists.new[ref prim.KeyValue]()+ for kv in xs {+ @append(out, commit.new_kv(bytes.Bytes.dup(kv.key), bytes.Bytes.dup(kv.value)))+ }+ return out+}++fn free_kvs(xs: list[ref prim.KeyValue]) void = trust {+ for kv in xs {+ 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 xp: mut ref void = kv as ref void+ @free(xp)+ }+ @listfree(xs)+}++fn join_path(prefix: bytes.Bytes, name: bytes.Bytes) bytes.Bytes = trust {+ if prefix.len == 0 {+ return bytes.Bytes.dup(name)+ }+ val out: ref u8 = @alloc(prefix.len + 1 + name.len) as ref u8+ @memcpy(out as ref void, prefix.ptr as ref void, prefix.len)+ @store8(@ptradd(out, prefix.len as i64), 47)+ @memcpy(+ @ptradd(out, (prefix.len + 1) as i64) as ref void,+ name.ptr as ref void,+ name.len+ )+ return bytes.bytes(out, prefix.len + 1 + name.len)+}++fn starts_with_child(path: bytes.Bytes, parent: bytes.Bytes) bool = trust {+ if path.len <= parent.len || !bytes.Bytes.starts_with(path, parent) {+ return false+ }+ return bytes.Bytes.at(path, parent.len) == 47+}++fn literal_matches(operand: bytes.Bytes, path: bytes.Bytes) bool = trust {+ if bytes.Bytes.eql(path, operand) {+ return true+ }+ return starts_with_child(path, operand)+}++fn glob_matches(operand: bytes.Bytes, path: bytes.Bytes) bool = trust {+ if ignore.glob_match(operand, path) {+ return true+ }+ val i: mut usize = 0+ for i < path.len {+ if bytes.Bytes.at(path, i) == 47 {+ if ignore.glob_match(operand, bytes.Bytes.slice(path, 0, i)) {+ return true+ }+ }+ i+++ }+ return false+}++fn operand_matches(operand: bytes.Bytes, path: bytes.Bytes) bool = trust {+ if pathspec.has_glob(operand) {+ return glob_matches(operand, path)+ }+ return literal_matches(operand, path)+}++fn contains_path(xs: list[bytes.Bytes], path: bytes.Bytes) bool = trust {+ for p in xs {+ if bytes.Bytes.eql(p, path) {+ return true+ }+ }+ return false+}++fn append_unique(xs: mut list[bytes.Bytes], path: bytes.Bytes) void = trust {+ if !contains_path(xs, path) {+ @append(xs, bytes.Bytes.dup(path))+ }+}++fn match_operands(+ handles: list[ref store.Handle],+ root_tree: objid.Id,+ operands: list[bytes.Bytes],+ matched: mut list[bool]+) list[bytes.Bytes] = trust {+ val removes: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()+ val entries: list[ref treewalk.FlatEntry] = treewalk.flatten(handles, root_tree)+ for e in entries {+ val i: mut usize = 0+ for i < @len(operands) {+ val o = @get(operands, i)+ if o is some operand {+ if operand_matches(operand, e.path) {+ @set(matched, i, true)+ append_unique(removes, e.path)+ }+ }+ i+++ }+ }+ treewalk.free_flat(entries)+ return removes+}++fn subtree_affected(prefix: bytes.Bytes, removes: list[bytes.Bytes]) bool = trust {+ if prefix.len == 0 {+ return @len(removes) > 0+ }+ for r in removes {+ if bytes.Bytes.eql(prefix, r) {+ return true+ }+ if starts_with_child(r, prefix) || starts_with_child(prefix, r) {+ return true+ }+ }+ return false+}++fn path_removed(path: bytes.Bytes, removes: list[bytes.Bytes]) bool = trust {+ for r in removes {+ if bytes.Bytes.eql(path, r) || starts_with_child(path, r) {+ return true+ }+ }+ return false+}++fn empty_tree_id(tx: mut ref txn.Txn) objid.Id = trust {+ val t: tree.Tree = tree.empty()+ val encoded = tree.encode(t.entries)+ val result: mut objid.Id = objid.id_none()+ match encoded {+ ok body => {+ result = txn.add_object(tx, types.TREE, body)+ }+ err _ => {+ }+ }+ @listfree(t.entries)+ return result+}++fn filter_tree(+ tx: mut ref txn.Txn,+ handles: list[ref store.Handle],+ tree_id: objid.Id,+ prefix: bytes.Bytes,+ removes: list[bytes.Bytes],+ depth: usize+) option[objid.Id] = trust {+ if depth > 4096 {+ return some(copy_id(tree_id))+ }+ if !subtree_affected(prefix, removes) {+ return some(copy_id(tree_id))+ }+ val t_opt = treewalk.read_tree(handles, tree_id)+ val t: mut ref tree.Tree = none+ match t_opt {+ some x => {+ t = x+ }+ none => {+ return some(copy_id(tree_id))+ }+ }+ val entries: mut list[ref tree.TreeEntry] = lists.new[ref tree.TreeEntry]()+ for e in t.entries {+ val full: bytes.Bytes = join_path(prefix, e.name)+ if path_removed(full, removes) {+ files.free_buffer(full)+ continue+ }+ if e.kind == tree.KIND_SUBTREE && subtree_affected(full, removes) {+ val child = filter_tree(tx, handles, e.target, full, removes, depth + 1)+ if child is some cid {+ @append(entries, tree.entry(bytes.Bytes.dup(e.name), e.kind, cid))+ }+ } else {+ @append(entries, tree.entry(bytes.Bytes.dup(e.name), e.kind, copy_id(e.target)))+ }+ files.free_buffer(full)+ }+ tree.free_tree(t)++ val result: mut option[objid.Id] = none+ if @len(entries) > 0 {+ val encoded = tree.encode(entries)+ match encoded {+ ok body => {+ result = some(txn.add_object(tx, types.TREE, body))+ }+ err _ => {+ }+ }+ }+ for e in entries {+ if e.name.ptr != none {+ val np: mut ref void = e.name.ptr as ref void+ @free(np)+ }+ free_id(e.target)+ val ep: mut ref void = e as ref void+ @free(ep)+ }+ @listfree(entries)+ return result+}++fn delete_one(repo: layout.Repo, rel: bytes.Bytes) bool = trust {+ val path_view: str.StringView = str.view(rel.ptr, rel.len as i64)+ val final: str.StringView = files.join(layout.root_view(repo), path_view)+ val removed: bool = files.delete(final)+ files.free_path(final)+ return removed+}++fn prune_empty_parents(repo: layout.Repo, rel: bytes.Bytes) void = trust {+ val i: mut usize = rel.len+ for i > 0 {+ val slash: mut i64 = -1+ val k: mut usize = 0+ for k < i {+ if bytes.Bytes.at(rel, k) == 47 {+ slash = k as i64+ }+ k+++ }+ if slash < 0 {+ break+ }+ val parent: bytes.Bytes = bytes.Bytes.slice(rel, 0, slash as usize)+ val path_view: str.StringView = str.view(parent.ptr, parent.len as i64)+ val abs: str.StringView = files.join(layout.root_view(repo), path_view)+ val removed: bool = fs.rmdir_path(abs)+ files.free_path(abs)+ if !removed {+ break+ }+ i = slash as usize+ }+}++fn delete_paths(repo: layout.Repo, removes: list[bytes.Bytes]) i32 = trust {+ val failed: mut usize = 0+ for rel in removes {+ if !delete_one(repo, rel) {+ failed+++ } else {+ prune_empty_parents(repo, rel)+ }+ }+ if failed > 0 {+ msg.warning_prefix()+ stdio.eprint("could not delete {d} path(s) from disk\n", {failed})+ return 1+ }+ return 0+}++fn run_removal(repo: layout.Repo, operands: list[bytes.Bytes], delete_mode: bool) i32 = trust {+ 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 wc_opt = viewops.working_copy(tx.view)+ 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+ }+ if !have_wc {+ msg.error_prefix()+ stdio.eprint("no working-copy commit\n", {})+ store.close(st)+ txn.abort(tx)+ return 1+ }+ val lc_opt = graph.load_commit(st.handles, wc)+ val lc: mut ref graph.LoadedCommit = none+ match lc_opt {+ some x => {+ lc = x+ }+ none => {+ msg.error_prefix()+ stdio.eprint("cannot read working-copy commit\n", {})+ store.close(st)+ txn.abort(tx)+ free_id(wc)+ return 1+ }+ }++ val matched: mut list[bool] = lists.new[bool]()+ val i: mut usize = 0+ for i < @len(operands) {+ @append(matched, false)+ i+++ }+ val removes: list[bytes.Bytes] = match_operands(+ st.handles,+ lc.c.root_tree,+ operands,+ matched+ )+ val unmatched: mut bool = false+ val j: mut usize = 0+ for j < @len(operands) {+ val o = @get(operands, j)+ if o is some operand {+ val m = @get(matched, j)+ val is_matched: mut bool = false+ if m is some flag {+ is_matched = flag+ }+ if !is_matched {+ msg.error_prefix()+ stdio.eprint(+ "path is not tracked: {s}\n",+ {str.view(operand.ptr, operand.len as i64)}+ )+ unmatched = true+ }+ }+ j+++ }+ if unmatched {+ free_path_list(removes)+ @listfree(matched)+ graph.free_loaded(lc)+ free_id(wc)+ store.close(st)+ txn.abort(tx)+ return 1+ }++ if !delete_mode {+ for rel in removes {+ if !ignore.path_ignored(layout.root_view(repo), rel) {+ msg.warning_prefix()+ stdio.eprint(+ "{s} is not ignored and will be re-added on the next snapshot\n",+ {str.view(rel.ptr, rel.len as i64)}+ )+ }+ }+ }++ val new_root_opt = filter_tree(tx, st.handles, lc.c.root_tree, bytes.empty(), removes, 0)+ val new_root: mut objid.Id = objid.id_none()+ if new_root_opt is some id {+ new_root = id+ } else {+ new_root = empty_tree_id(tx)+ }+ if objid.Id.is_none(new_root) || objid.Id.eql(new_root, lc.c.root_tree) {+ msg.error_prefix()+ stdio.eprint("no tracked paths to remove\n", {})+ free_id(new_root)+ free_path_list(removes)+ @listfree(matched)+ graph.free_loaded(lc)+ free_id(wc)+ store.close(st)+ txn.abort(tx)+ return 1+ }++ val sig = identity.signature(repo)+ val parents: mut list[objid.Id] = lists.new[objid.Id]()+ for p in lc.c.parents {+ @append(parents, copy_id(p))+ }+ val renames: list[ref commit.Rename] = lists.new[ref commit.Rename]()+ val extra: list[ref prim.KeyValue] = copy_kvs(lc.c.extra)+ val nc: mut ref commit.Commit = @alloc(@sizeof(commit.Commit)) as ref commit.Commit+ nc.version = 1+ nc.parents = parents+ nc.root_tree = new_root+ nc.change_id = lc.c.change_id+ nc.author = lc.c.author+ nc.committer = sig+ nc.message = lc.c.message+ nc.renames_state = commit.RENAMES_RECORDED+ nc.renames = renames+ nc.extra = extra+ val encoded: bytes.Bytes = commit.encode(nc)+ val new_id: objid.Id = txn.add_object(tx, types.COMMIT, encoded)+ viewops.remove_head(tx.view, wc)+ viewops.set_workspace(tx.view, new_id)+ viewops.rebuild_heads(tx.view)+ val status: mut i32 = txn.finish(tx, bytes.bytes(c"rm", 2))+ if status == 0 {+ val _ = gitmap.mark_native(repo, new_id)+ }++ @listfree(parents)+ @listfree(renames)+ free_kvs(extra)+ @free(nc as ref void)+ free_id(new_id)+ free_id(new_root)+ identity.free_sig(sig)+ graph.free_loaded(lc)+ free_id(wc)+ store.close(st)++ if status == 0 {+ if delete_mode {+ val del: i32 = delete_paths(repo, removes)+ if del != 0 {+ status = del+ }+ }+ msg.info_prefix()+ stdio.print("untracked {d} path(s)\n", {@len(removes)})+ }+ free_path_list(removes)+ @listfree(matched)+ return status+}++pub fn run(args: list[ref u8], start: usize) i32 = trust {+ val delete_mode: mut bool = false+ val operands: mut list[ref u8] = lists.new[ref u8]()+ 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"-delete", 7) {+ delete_mode = true+ } elif !end_flags && is_flag(a) {+ msg.error_prefix()+ stdio.eprint("unknown flag: {s}\n", {str.from_cstr(a)})+ @listfree(operands)+ return 1+ } else {+ @append(operands, a)+ }+ }+ i+++ }+ if @len(operands) == 0 {+ msg.error_prefix()+ stdio.eprint("rm requires at least one path\n", {})+ @listfree(operands)+ return 1+ }++ 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", {})+ @listfree(operands)+ return 1+ }+ }++ val snap: i32 = snapshot.run(repo)+ if snap != 0 {+ @listfree(operands)+ layout.free_repo(repo)+ return snap+ }++ val cwd_opt = pathspec.cwd_relative(repo.root)+ val cwd_rel: mut bytes.Bytes = bytes.empty()+ match cwd_opt {+ some c => {+ cwd_rel = c+ }+ none => {+ msg.error_prefix()+ stdio.eprint("cannot determine current directory\n", {})+ @listfree(operands)+ layout.free_repo(repo)+ return 1+ }+ }++ val normalized: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()+ val bad: mut bool = false+ for a in operands {+ val arg: bytes.Bytes = bytes.from_cstr(a)+ val norm_opt = pathspec.normalize(repo.root, cwd_rel, arg)+ val norm: mut bytes.Bytes = bytes.empty()+ match norm_opt {+ some n => {+ norm = n+ }+ none => {+ msg.error_prefix()+ stdio.eprint(+ "path is outside the repository: {s}\n",+ {str.view(arg.ptr, arg.len as i64)}+ )+ bad = true+ }+ }+ if bad {+ if norm.ptr != none {+ val np: mut ref void = norm.ptr as ref void+ @free(np)+ }+ break+ }+ if norm.len == 0 || !names.valid_path(norm) {+ msg.error_prefix()+ stdio.eprint(+ "invalid path: {s}\n",+ {str.view(arg.ptr, arg.len as i64)}+ )+ if norm.ptr != none {+ val np2: mut ref void = norm.ptr as ref void+ @free(np2)+ }+ bad = true+ break+ }+ @append(normalized, norm)+ }+ if bad {+ if cwd_rel.ptr != none {+ val cp: mut ref void = cwd_rel.ptr as ref void+ @free(cp)+ }+ free_path_list(normalized)+ @listfree(operands)+ layout.free_repo(repo)+ return 1+ }+ if @len(normalized) == 0 {+ msg.error_prefix()+ stdio.eprint("rm requires at least one path\n", {})+ if cwd_rel.ptr != none {+ val cp2: mut ref void = cwd_rel.ptr as ref void+ @free(cp2)+ }+ @listfree(normalized)+ @listfree(operands)+ layout.free_repo(repo)+ return 1+ }++ val status: i32 = run_removal(repo, normalized, delete_mode)+ if cwd_rel.ptr != none {+ val cp3: mut ref void = cwd_rel.ptr as ref void+ @free(cp3)+ }+ free_path_list(normalized)+ @listfree(operands)+ layout.free_repo(repo)+ return status+}--- a/veins/src/main.sx+++ b/veins/src/main.sx@@ -18,6 +18,8 @@ val new_cmd = use("cli/new_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")+val ex_cmd = use("cli/ex_cmd.sx") val lists = use("core/lists.sx") val layout = use("store/layout.sx") @@ -461,6 +463,12 @@ } if cmd_is(cmd, c"new", 3) { return run_new(args, 2)+ }+ if cmd_is(cmd, c"rm", 2) {+ return rm_cmd.run(args, 2)+ }+ if cmd_is(cmd, c"ex", 2) {+ return ex_cmd.run(args, 2) } if cmd_is(cmd, c"desc", 4) || cmd_is(cmd, c"describe", 8) { return run_describe(args, 2)--- a/veins/src/worktree/ignore.sx+++ b/veins/src/worktree/ignore.sx@@ -214,6 +214,11 @@ return false } return glob_here(pattern, pi + 1, text, ti + 1)+}++/// Match a single gitignore-style glob against a relative path.+pub fn glob_match(pattern: bytes.Bytes, path: bytes.Bytes) bool = trust {+ return glob_here(pattern, 0, path, 0) } fn component_end(text: bytes.Bytes, start: usize) usize = trust {@@ -288,6 +293,43 @@ } } return ignored_flag+}++/// Evaluate the complete ignore state for one repository-relative path,+/// including root and per-directory `.vnexclude` / `.gitignore` files.+pub fn path_ignored(repo_root: str.StringView, path: bytes.Bytes) bool = trust {+ val rules: mut Rules = empty()+ load_root(rules, repo_root)++ // Load the rules in every directory that contains the path.+ val i: mut usize = 0+ for i < path.len {+ if bytes.Bytes.at(path, i) == 47 {+ val base: bytes.Bytes = bytes.Bytes.slice(path, 0, i)+ load_file(rules, repo_root, base, str.from_cstr(c".vnexclude"))+ load_file(rules, repo_root, base, str.from_cstr(c".gitignore"))+ }+ i+++ }++ // An ignored ancestor directory keeps its contents out of snapshots.+ val result: mut bool = false+ val j: mut usize = 0+ for j < path.len {+ if bytes.Bytes.at(path, j) == 47 {+ val dir: bytes.Bytes = bytes.Bytes.slice(path, 0, j)+ if ignored(rules, dir, true) {+ result = true+ break+ }+ }+ j+++ }+ if !result {+ result = ignored(rules, path, false)+ }+ free(rules)+ return result } pub fn free(rules: Rules) void = trust {--- /dev/null+++ b/veins/src/worktree/pathspec.sx@@ -0,0 +1,152 @@+/// Convert command-line path arguments into repository-relative paths.+///+/// Paths are resolved against the current working directory so that+/// `vn rm *.dSYM` in a subdirectory only considers that directory. Glob+/// metacharacters are preserved for callers that expand patterns themselves.++val str = use("std/string")+val fs = use("std/fs")+val path = use("std/path")+val bytes = use("../core/bytes.sx")+val lists = use("../core/lists.sx")+val buf = use("../core/buf.sx")+val files = use("../io/files.sx")++/// Return the current directory relative to `root`, or none when the current+/// directory is outside the repository.+pub fn cwd_relative(root: bytes.Bytes) option[bytes.Bytes] = trust {+ val cwd_opt = fs.cwd_cstr()+ val cwd: mut ref u8 = none+ match cwd_opt {+ some c => {+ cwd = c+ }+ none => {+ return none+ }+ }+ val canon: ref u8 = path.canonicalize_path(str.from_cstr(cwd))+ @free(cwd as ref void)+ if canon == none || @strlen(canon) == 0 {+ if canon != none {+ @free(canon as ref void)+ }+ return none+ }+ val cb: bytes.Bytes = bytes.from_cstr(canon)+ val result: mut option[bytes.Bytes] = none+ if bytes.Bytes.eql(cb, root) {+ result = some(bytes.empty())+ } elif cb.len > root.len && bytes.Bytes.starts_with(cb, root)+ && bytes.Bytes.at(cb, root.len) == 47 {+ result = some(bytes.Bytes.dup(bytes.Bytes.slice(cb, root.len + 1, cb.len)))+ }+ @free(canon as ref void)+ return result+}++/// Collapse `.` and `..` components and remove repeated separators.+fn normalize_components(input: bytes.Bytes) option[bytes.Bytes] = trust {+ val comps: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()+ val i: mut usize = 0+ val start: mut usize = 0+ for i <= input.len {+ val at_end: bool = i == input.len+ if at_end || bytes.Bytes.at(input, i) == 47 {+ val comp: bytes.Bytes = bytes.Bytes.slice(input, start, i)+ if comp.len > 0 && !bytes.Bytes.eql(comp, bytes.bytes(c".", 1)) {+ if bytes.Bytes.eql(comp, bytes.bytes(c"..", 2)) {+ if @len(comps) == 0 {+ @listfree(comps)+ return none+ }+ val _ = @remove(comps, @len(comps) - 1)+ } else {+ @append(comps, comp)+ }+ }+ start = i + 1+ }+ i+++ }+ if @len(comps) == 0 {+ @listfree(comps)+ return some(bytes.empty())+ }+ val b: mut buf.Buf = buf.new(64)+ val k: mut usize = 0+ for k < @len(comps) {+ if k > 0 {+ buf.Buf.put_u8(b, 47)+ }+ val o = @get(comps, k)+ if o is some comp {+ buf.Buf.put_bytes(b, comp)+ }+ k+++ }+ val out: bytes.Bytes = bytes.Bytes.dup(buf.Buf.view(b))+ buf.Buf.free(b)+ @listfree(comps)+ return some(out)+}++/// Normalize one operand. Relative operands are joined to `cwd_rel`; absolute+/// operands must live inside `root`. The returned path is repository-relative+/// and owned by the caller.+pub fn normalize(+ root: bytes.Bytes,+ cwd_rel: bytes.Bytes,+ arg: bytes.Bytes+) option[bytes.Bytes] = trust {+ if arg.len == 0 {+ return none+ }+ val absolute: bool = bytes.Bytes.at(arg, 0) == 47+ if absolute {+ val norm_opt = normalize_components(arg)+ val norm: mut bytes.Bytes = bytes.empty()+ match norm_opt {+ some n => {+ norm = n+ }+ none => {+ return none+ }+ }+ val root_view: mut bytes.Bytes = root+ if root.len > 0 && bytes.Bytes.at(root, 0) == 47 {+ root_view = bytes.Bytes.slice(root, 1, root.len)+ }+ val result: mut option[bytes.Bytes] = none+ if norm.len > root_view.len && bytes.Bytes.starts_with(norm, root_view)+ && bytes.Bytes.at(norm, root_view.len) == 47 {+ result = some(bytes.Bytes.dup(bytes.Bytes.slice(norm, root_view.len + 1, norm.len)))+ }+ if norm.ptr != none {+ val np: mut ref void = norm.ptr as ref void+ @free(np)+ }+ return result+ }+ val base_view: str.StringView = files.to_view(cwd_rel)+ val arg_view: str.StringView = files.to_view(arg)+ val joined: str.StringView = files.join(base_view, arg_view)+ val joined_bytes: bytes.Bytes = bytes.bytes(joined.ptr, joined.len as usize)+ val norm_opt = normalize_components(joined_bytes)+ files.free_path(joined)+ return norm_opt+}++/// True when an argument contains a wildcard supported by the ignore engine.+pub fn has_glob(v: bytes.Bytes) bool = trust {+ val i: mut usize = 0+ for i < v.len {+ val c: u8 = bytes.Bytes.at(v, i)+ if c == 42 || c == 63 {+ return true+ }+ i+++ }+ return false+}