navid/veins / commit

2fddc3e8c59a

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

parents: b7434ae81b96

Browse files at this commit · Download source

all: fix caching issue and amend command
--- a/veins/src/cli/help_cmd.sx+++ b/veins/src/cli/help_cmd.sx@@ -40,6 +40,7 @@     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("  amend        rewrite a revision's message and its descendants")     stdio.puts("  undo         restore the previous operation view")     stdio.puts("  op           operation log commands")     stdio.puts("  fsck         verify repository integrity")@@ -305,6 +306,17 @@     stdio.puts("a non-empty commit message is required")     stdio.puts("")     stdio.puts("aliases: commit")+}++fn show_amend() void = trust {+    stdio.puts("usage: vn amend <rev> -m <message>")+    stdio.puts("")+    stdio.puts("rewrite a revision's message and update its descendants")+    stdio.puts("")+    stdio.puts("options:")+    stdio.puts("  -m, -message <message>    new commit message")+    stdio.puts("")+    stdio.puts("revisions reachable from a remote ref cannot be amended") }  fn show_undo() void = trust {@@ -469,6 +481,10 @@     }     if cmd_is(cmd, c"go", 2) || cmd_is(cmd, c"commit", 6) {         show_commit()+        return 0+    }+    if cmd_is(cmd, c"amend", 5) {+        show_amend()         return 0     }     if cmd_is(cmd, c"undo", 4) {--- a/veins/src/cli/new_cmd.sx+++ b/veins/src/cli/new_cmd.sx@@ -11,6 +11,7 @@ val types    = use("../object/types.sx") val commit   = use("../object/commit.sx") val graph    = use("../object/graph.sx")+val view     = use("../object/view.sx") val store    = use("../store/store.sx") val layout   = use("../store/layout.sx") val txn      = use("../store/txn.sx")@@ -247,6 +248,314 @@     val status: i32 = new_change_impl(repo, empty, false, st)     store.close(st)     @listfree(empty)+    return status+}++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 contains_id(ids: list[objid.Id], id: objid.Id) bool = trust {+    for x in ids {+        if objid.Id.eql(x, id) {+            return true+        }+    }+    return false+}++fn mapped_id(+    old_ids: list[objid.Id],+    new_ids: list[objid.Id],+    id: objid.Id+) option[objid.Id] = trust {+    val i: mut usize = 0+    for i < @len(old_ids) {+        val o = @get(old_ids, i)+        if o is some old {+            if objid.Id.eql(old, id) {+                val n = @get(new_ids, i)+                if n is some nid {+                    return some nid+                }+            }+        }+        i+++    }+    return none+}++fn remap_ids(+    old_ids: list[objid.Id],+    new_ids: list[objid.Id],+    ids: mut list[objid.Id]+) void = trust {+    val i: mut usize = 0+    for i < @len(ids) {+        val o = @get(ids, i)+        if o is some id {+            val m = mapped_id(old_ids, new_ids, id)+            if m is some nid {+                @set(ids, i, copy_id(nid))+            }+        }+        i+++    }+}++fn remap_amend_view(+    v: ref view.View,+    old_ids: list[objid.Id],+    new_ids: list[objid.Id]+) void = trust {+    for bm in v.bookmarks {+        val bmm: mut ref view.Bookmark = bm+        remap_ids(old_ids, new_ids, bmm.targets.adds)+        remap_ids(old_ids, new_ids, bmm.targets.removes)+    }+    for t in v.tags {+        val tm: mut ref view.TagRef = t+        val m = mapped_id(old_ids, new_ids, tm.target)+        if m is some nid {+            tm.target = copy_id(nid)+        }+    }+    for w in v.working_copies {+        val wm: mut ref view.WorkspaceRef = w+        val m = mapped_id(old_ids, new_ids, wm.target)+        if m is some nid {+            wm.target = copy_id(nid)+        }+    }+    for rr in v.remote_refs {+        val rm: mut ref view.RemoteRef = rr+        val m = mapped_id(old_ids, new_ids, rm.target)+        if m is some nid {+            rm.target = copy_id(nid)+        }+    }+}++fn amend_ready(+    handles: list[ref store.Handle],+    cid: objid.Id,+    target: objid.Id,+    old_ids: list[objid.Id]+) bool = trust {+    if objid.Id.eql(cid, target) {+        return true+    }+    val lc_opt = graph.load_commit(handles, cid)+    val ready: mut bool = false+    if lc_opt is some lc {+        for p in lc.c.parents {+            if contains_id(old_ids, p) {+                ready = true+                break+            }+        }+        graph.free_loaded(lc)+    }+    return ready+}++fn amendment_is_pushed(+    handles: list[ref store.Handle],+    v: ref view.View,+    target: objid.Id+) bool = trust {+    val roots: mut list[objid.Id] = lists.new[objid.Id]()+    for rr in v.remote_refs {+        @append(roots, copy_id(rr.target))+    }+    val pushed: mut bool = false+    if @len(roots) > 0 {+        val reachable: list[objid.Id] = graph.collect(handles, roots)+        pushed = contains_id(reachable, target)+        graph.free_ids(reachable)+    }+    graph.free_ids(roots)+    return pushed+}++fn amend_impl(repo: layout.Repo, rev: bytes.Bytes, message: bytes.Bytes) i32 = trust {+    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 handles: list[ref store.Handle] = txn.handles(tx)+    val target_opt = graph.resolve(handles, tx.view, rev)+    val target: mut objid.Id = objid.id_none()+    match target_opt {+        some tid => {+            target = copy_id(tid)+        }+        none => {+            msg.error_prefix()+            stdio.eprint("unknown revision\n", {})+            txn.abort(tx)+            return 1+        }+    }+    if amendment_is_pushed(handles, tx.view, target) {+        msg.error_prefix()+        stdio.eprint("cannot amend a revision that has been pushed\n", {})+        txn.abort(tx)+        free_id(target)+        return 1+    }+    val target_lc_opt = graph.load_commit(handles, target)+    if target_lc_opt is none {+        msg.error_prefix()+        stdio.eprint("cannot read revision\n", {})+        txn.abort(tx)+        free_id(target)+        return 1+    }+    val target_lc: mut ref graph.LoadedCommit = none+    if target_lc_opt is some lc {+        target_lc = lc+    }+    val same_message: bool = bytes.Bytes.eql(target_lc.c.message, message)+    graph.free_loaded(target_lc)+    if same_message {+        msg.info_prefix()+        stdio.print("message unchanged\n", {})+        txn.abort(tx)+        free_id(target)+        return 0+    }+    val all: list[objid.Id] = graph.all(handles, tx.view)+    val remaining: mut list[objid.Id] = lists.new[objid.Id]()+    for c in all {+        @append(remaining, copy_id(c))+    }+    graph.free_ids(all)+    val old_ids: mut list[objid.Id] = lists.new[objid.Id]()+    val new_ids: mut list[objid.Id] = lists.new[objid.Id]()+    val sig: prim.Sig = identity.signature(repo)+    val failed: mut bool = false+    val done: mut bool = false+    val target_rewritten: mut bool = false+    for @len(remaining) > 0 && !failed && !done {+        val ready_idx: mut i32 = -1+        val ri: mut usize = 0+        for ri < @len(remaining) {+            val ro = @get(remaining, ri)+            if ro is some cid {+                if amend_ready(handles, cid, target, old_ids) {+                    ready_idx = ri as i32+                    break+                }+            }+            ri+++        }+        if ready_idx < 0 {+            done = true+        } else {+            val chosen_o = @get(remaining, ready_idx as usize)+            val chosen: mut objid.Id = objid.id_none()+            if chosen_o is some c {+                chosen = c+            }+            val lc_opt = graph.load_commit(handles, chosen)+            if lc_opt is none {+                failed = true+            } else {+                val lc: mut ref graph.LoadedCommit = none+                if lc_opt is some l {+                    lc = l+                }+                val new_parents: mut list[objid.Id] = lists.new[objid.Id]()+                for p in lc.c.parents {+                    val m = mapped_id(old_ids, new_ids, p)+                    if m is some nid {+                        @append(new_parents, copy_id(nid))+                    } else {+                        @append(new_parents, copy_id(p))+                    }+                }+                val nc: mut ref commit.Commit = @alloc(@sizeof(commit.Commit)) as ref commit.Commit+                nc.version = lc.c.version+                nc.parents = new_parents+                nc.root_tree = lc.c.root_tree+                nc.change_id = lc.c.change_id+                nc.author = lc.c.author+                nc.committer = sig+                if objid.Id.eql(chosen, target) {+                    nc.message = message+                } else {+                    nc.message = lc.c.message+                }+                nc.renames_state = lc.c.renames_state+                nc.renames = lc.c.renames+                nc.extra = lc.c.extra+                val encoded: bytes.Bytes = commit.encode(nc)+                val new_id: objid.Id = txn.add_object(tx, types.COMMIT, encoded)+                @append(old_ids, copy_id(chosen))+                @append(new_ids, copy_id(new_id))+                if objid.Id.eql(chosen, target) {+                    target_rewritten = true+                }+                graph.free_ids(new_parents)+                @free(nc as ref void)+                graph.free_loaded(lc)+                free_id(new_id)+            }+            val removed: objid.Id = @remove(remaining, ready_idx as usize)+            free_id(removed)+        }+    }+    if failed || !target_rewritten {+        msg.error_prefix()+        stdio.eprint("cannot amend revision\n", {})+        txn.abort(tx)+        identity.free_sig(sig)+        graph.free_ids(old_ids)+        graph.free_ids(new_ids)+        graph.free_ids(remaining)+        free_id(target)+        return 1+    }+    remap_amend_view(tx.view, old_ids, new_ids)+    viewops.rebuild_heads(tx.view)+    val desc: mut buf.Buf = buf.new(64)+    buf.Buf.put_raw(desc, c"amend ", 6)+    buf.Buf.put_bytes(desc, rev)+    val status: i32 = txn.finish(tx, buf.Buf.view(desc))+    if status == 0 {+        for nid in new_ids {+            val _ = gitmap.mark_native(repo, nid)+        }+    }+    buf.Buf.free(desc)+    identity.free_sig(sig)+    graph.free_ids(old_ids)+    graph.free_ids(new_ids)+    graph.free_ids(remaining)+    free_id(target)+    return status+}++pub fn amend(repo: layout.Repo, rev: bytes.Bytes, args: list[bytes.Bytes]) i32 = trust {+    val message: bytes.Bytes = join_args(args)+    val status: i32 = amend_impl(repo, rev, message)+    bytes.free_owned(message)     return status } --- a/veins/src/main.sx+++ b/veins/src/main.sx@@ -679,6 +679,69 @@             @listfree(parts)             layout.free_repo(repo)             return n+        }+        none => {+            @listfree(parts)+            return 1+        }+    }+}++fn run_amend(args: list[ref u8], start: usize) i32 = trust {+    val rev: mut bytes.Bytes = bytes.empty()+    val parts: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()+    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"-m", 2) || cmd_is(a, c"-message", 8)) {+                if i + 1 < @len(args) {+                    val v = @get(args, i + 1)+                    if v is some rv {+                        @append(parts, bytes.from_cstr(rv))+                        i+++                    }+                } else {+                    msg.error_prefix()+                    stdio.eprint("{s} requires a message\n", {str.from_cstr(a)})+                    @listfree(parts)+                    return 1+                }+            } elif !end_flags && is_flag(a) {+                msg.error_prefix()+                stdio.eprint("unknown flag: {s}\n", {str.from_cstr(a)})+                @listfree(parts)+                return 1+            } elif rev.len == 0 {+                rev = bytes.from_cstr(a)+            } else {+                @append(parts, bytes.from_cstr(a))+            }+        }+        i+++    }+    if rev.len == 0 {+        msg.error_prefix()+        stdio.eprint("amend requires a revision\n", {})+        @listfree(parts)+        return 1+    }+    if message_is_blank(parts) {+        msg.error_prefix()+        stdio.eprint("amend requires a non-empty message (-message)\n", {})+        @listfree(parts)+        return 1+    }+    val repo_opt = open_repo_or_error(str.from_cstr(c"."))+    match repo_opt {+        some repo => {+            val status: i32 = new_cmd.amend(repo, rev, parts)+            @listfree(parts)+            layout.free_repo(repo)+            return status         }         none => {             @listfree(parts)@@ -904,6 +967,9 @@     }     if cmd_is(cmd, c"go", 2) || cmd_is(cmd, c"commit", 6) {         return run_commit(args, 2)+    }+    if cmd_is(cmd, c"amend", 5) {+        return run_amend(args, 2)     }     if cmd_is(cmd, c"undo", 4) {         return run_undo()--- a/veins/src/worktree/snapshot.sx+++ b/veins/src/worktree/snapshot.sx@@ -246,7 +246,7 @@         files.free_buffer(data)         return c     }-    val r: mut reader.Reader = reader.new(data.ptr, data.len)+    val r: mut reader.Reader = reader.new(@ptradd(data.ptr, 4), data.len - 4)     val version_opt = cache_u8(r)     val version: mut u8 = 0     match version_opt {