navid/veins / commit

90af4ae62ead

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

parents: d7c41100769c

Browse files at this commit · Download source

all: fixes pertaining to git imports
--- a/veins/src/cli/git_cmd.sx+++ b/veins/src/cli/git_cmd.sx@@ -90,7 +90,10 @@         if prepared != 0 {             msg.error_prefix()             if prepared == 2 {-                stdio.eprint("-source: {s} is not a git repository root (expected the directory containing .git)\n", {src})+                stdio.eprint(+                    "-source: {s} is not a git repository root (expected the directory containing .git)\n",+                    {src}+                )             } else {                 stdio.eprint("cannot prepare git mirror from {s} (code {d})\n", {src, prepared})             }--- a/veins/src/cli/help_cmd.sx+++ b/veins/src/cli/help_cmd.sx@@ -84,6 +84,8 @@     stdio.puts("usage: vn git import [-source:<gitdir>] [path]")     stdio.puts("")     stdio.puts("import commits and refs reachable from a git repository")+    stdio.puts("")+    stdio.puts("an empty working tree is checked out to the imported head")     stdio.puts("")     stdio.puts("arguments:")     stdio.puts("  [path]                    veins repository (default: .)")--- a/veins/src/cli/init_cmd.sx+++ b/veins/src/cli/init_cmd.sx@@ -19,9 +19,15 @@                 if prepared != 0 {                     msg.error_prefix()                     if prepared == 2 {-                        stdio.eprint("-git-import: {s} is not a git repository root (expected the directory containing .git)\n", {src})+                        stdio.eprint(+                            "-git-import: {s} is not a git repository root (expected the directory containing .git)\n",+                            {src}+                        )                     } else {-                        stdio.eprint("cannot prepare git mirror from {s} (code {d})\n", {src, prepared})+                        stdio.eprint(+                            "cannot prepare git mirror from {s} (code {d})\n",+                            {src, prepared}+                        )                     }                     layout.free_repo(repo)                     return 1--- a/veins/src/git/import.sx+++ b/veins/src/git/import.sx@@ -7,6 +7,7 @@ val msg       = use("../core/msg.sx") val os        = use("std/os") val time      = use("std/time")+val random    = use("std/crypto/random") val bytes     = use("../core/bytes.sx") val buf       = use("../core/buf.sx") val lists     = use("../core/lists.sx")@@ -16,6 +17,7 @@ val types     = use("../object/types.sx") val tree      = use("../object/tree.sx") val commit    = use("../object/commit.sx")+val graph     = use("../object/graph.sx") val operation = use("../object/operation.sx") val view      = use("../object/view.sx") val layout    = use("../store/layout.sx")@@ -22,6 +24,9 @@ val store     = use("../store/store.sx") val writer    = use("../store/writer.sx") val state     = use("../store/state.sx")+val txn       = use("../store/txn.sx")+val viewops   = use("../store/viewops.sx")+val identity  = use("../core/identity.sx") val files     = use("../io/files.sx") val object    = use("object.sx") val lazy      = use("lazy.sx")@@ -29,6 +34,7 @@ val refs      = use("refs.sx") val oidmap    = use("oidmap.sx") val gitmap    = use("map.sx")+val checkout  = use("../worktree/checkout.sx")   pub type ImportStats = {@@ -671,7 +677,91 @@   /// Flush object batches as packs during large imports to bound peak memory.+fn working_tree_empty(repo: layout.Repo) bool = trust {+    val names: list[ref u8] = files.list_dir(layout.root_view(repo))+    val empty: mut bool = true+    for name in names {+        if !bytes.Bytes.eql(bytes.from_cstr(name), bytes.bytes(c".veins", 6)) {+            empty = false+        }+    }+    files.free_names(names)+    return empty+}++/// Create an empty-message working-copy commit on top of an imported head so+/// snapshots amend the working copy instead of rewriting the imported branch.+fn create_working_copy(repo: layout.Repo, parent: objid.Id) 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 handles: list[ref store.Handle] = txn.handles(tx)+    val loaded = graph.load_commit(handles, parent)+    val root_tree: mut objid.Id = objid.id_none()+    match loaded {+        some lc => {+            root_tree = graph.copy_id(lc.c.root_tree)+            graph.free_loaded(lc)+        }+        none => {+            msg.error_prefix()+            stdio.eprint("cannot read imported commit\n", {})+            txn.abort(tx)+            return 1+        }+    }+    val cid: ref u8 = @alloc(16) as ref u8+    random.random_bytes(cid as ref void, 16)+    val sig = identity.signature(repo)+    val parents: mut list[objid.Id] = lists.new[objid.Id]()+    @append(parents, parent)+    val renames: list[ref commit.Rename] = lists.new[ref commit.Rename]()+    val extra: list[ref prim.KeyValue] = lists.new[ref prim.KeyValue]()+    val nc: mut ref commit.Commit = @alloc(@sizeof(commit.Commit)) as ref commit.Commit+    nc.version = 1+    nc.parents = parents+    nc.root_tree = root_tree+    nc.change_id = objid.change_id(cid)+    nc.author = sig+    nc.committer = sig+    nc.message = bytes.empty()+    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, parent)+    viewops.set_workspace(tx.view, new_id)+    viewops.rebuild_heads(tx.view)+    val status: i32 = txn.finish(tx, bytes.bytes(c"import", 6))+    @listfree(parents)+    @listfree(renames)+    @listfree(extra)+    @free(nc as ref void)+    identity.free_sig(sig)+    @free(cid as ref void)+    if root_tree.ptr != none {+        val rtp: mut ref void = root_tree.ptr as ref void+        @free(rtp)+    }+    if new_id.ptr != none {+        val np: mut ref void = new_id.ptr as ref void+        @free(np)+    }+    return status+}+ pub fn run(repo: layout.Repo) i32 = trust {+    val checkout_after: bool = working_tree_empty(repo)     val git_path: str.StringView = layout.git_dir(repo)     val git_opt = object.open(git_path)     val git: mut object.GitRepo = @zeroed@@ -1199,7 +1289,7 @@     if head_commit is some hc {         val hcid = get_id(ctx.commits_map, hc)         if hcid is some hv {-            head_id = hv+            head_id = dup_id(hv)         }     }     if @len(new_heads) == 0 && !objid.Id.is_none(head_id) {@@ -1207,12 +1297,14 @@     }     sort_head_ids(new_heads)     v.heads = new_heads-    if !objid.Id.is_none(head_id) {-        set_workspace(v, bytes.bytes(c"default", 7), head_id)-    } elif @len(new_heads) > 0 {-        val first = @get(new_heads, 0)-        if first is some fid {-            set_workspace(v, bytes.bytes(c"default", 7), fid)+    if checkout_after || !view_from_store {+        if !objid.Id.is_none(head_id) {+            set_workspace(v, bytes.bytes(c"default", 7), head_id)+        } elif @len(new_heads) > 0 {+            val first = @get(new_heads, 0)+            if first is some fid {+                set_workspace(v, bytes.bytes(c"default", 7), fid)+            }         }     } @@ -1388,8 +1480,32 @@     oidmap.free(ctx.blobs_map)     store.close(store_handle)     object.free(ctx.git)+    val work_copy_status: mut i32 = 0+    if head_ok && checkout_after && !objid.Id.is_none(head_id) {+        work_copy_status = create_working_copy(repo, head_id)+    }+    if head_id.ptr != none {+        val hip: mut ref void = head_id.ptr as ref void+        @free(hip)+    }     if !head_ok {+        return 1+    }+    if work_copy_status != 0 {+        msg.error_prefix()+        stdio.eprint(+            "imported history; cannot create working copy (code {d})\n",+            {work_copy_status}+        )         return 1+    }+    if checkout_after {+        val status: i32 = checkout.materialize_working_copy(repo)+        if status != 0 {+            msg.error_prefix()+            stdio.eprint("imported history; checkout failed (code {d})\n", {status})+            return 1+        }     }     return 0 }