navid/veins / commit
5109821a249c
Navid M <navidmomtahen@gmail.com> · 2026-11-26
parents: 72310a02581c
Browse files at this commit · Download source
--- a/src/src/cli/new_cmd.sx+++ b/src/src/cli/new_cmd.sx@@ -10,6 +10,8 @@ val types = use("../object/types.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")@@ -18,11 +20,45 @@ val gitmap = use("../git/map.sx") val snapshot = use("../worktree/snapshot.sx") val checkout = use("../worktree/checkout.sx")++val NOTHING_TO_COMMIT: i32 = 2 fn copy_id(id: objid.Id) objid.Id = trust { 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 wc_has_changes(handles: list[ref store.Handle], lc: ref graph.LoadedCommit) bool = trust {+ if @len(lc.c.parents) > 0 {+ val p = @get(lc.c.parents, 0)+ val pid: mut objid.Id = objid.id_none()+ if p is some x {+ pid = x+ }+ val pl_opt = graph.load_commit(handles, pid)+ val changed: mut bool = true+ match pl_opt {+ some parent => {+ changed = !objid.Id.eql(parent.c.root_tree, lc.c.root_tree)+ graph.free_loaded(parent)+ }+ none => {+ }+ }+ return changed+ }+ val t_opt = treewalk.read_tree(handles, lc.c.root_tree)+ val changed: mut bool = true+ match t_opt {+ some t => {+ changed = @len(t.entries) > 0+ tree.free_tree(t)+ }+ none => {+ }+ }+ return changed } fn copy_renames(rs: list[ref commit.Rename]) list[ref commit.Rename] = trust {@@ -99,7 +135,7 @@ return out } -pub fn describe(repo: layout.Repo, args: list[bytes.Bytes]) i32 = trust {+fn describe_impl(repo: layout.Repo, args: list[bytes.Bytes], require_changes: bool) i32 = trust { val snap: i32 = snapshot.run(repo) if snap != 0 { return snap@@ -148,7 +184,27 @@ lc = x } none => {+ }+ }+ val changed: bool = wc_has_changes(st.handles, lc)+ val same_message: bool = bytes.Bytes.eql(message, lc.c.message)+ if !changed && (require_changes || same_message) {+ if require_changes {+ stdio.puts("nothing to commit, working copy clean")+ }+ store.close(st)+ txn.abort(tx)+ graph.free_loaded(lc)+ val mp: mut ref void = message.ptr as ref void+ @free(mp)+ if wc.ptr != none {+ val wp: mut ref void = wc.ptr as ref void+ @free(wp)+ }+ if require_changes {+ return NOTHING_TO_COMMIT }+ return 0 } val sig = identity.signature(repo) val parents: mut list[objid.Id] = lists.new[objid.Id]()@@ -193,6 +249,24 @@ @free(wp) } identity.free_sig(sig)+ return status+}++pub fn describe(repo: layout.Repo, args: list[bytes.Bytes]) i32 = trust {+ return describe_impl(repo, args, false)+}++pub fn commit_changes(repo: layout.Repo, args: list[bytes.Bytes]) i32 = trust {+ val d: i32 = describe_impl(repo, args, true)+ if d == NOTHING_TO_COMMIT {+ return 0+ }+ if d != 0 {+ return d+ }+ val empty: list[bytes.Bytes] = lists.new[bytes.Bytes]()+ val status: i32 = new_change(repo, empty)+ @listfree(empty) return status } --- a/src/src/main.sx+++ b/src/src/main.sx@@ -24,6 +24,16 @@ return false } 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 run_init(args: list[ref u8], start: usize) i32 = trust {@@ -59,12 +69,18 @@ fn run_fsck(args: list[ref u8], start: usize) i32 = trust { val mut_path: mut str.StringView = str.from_cstr(c".") val verbose: mut bool = false+ 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 cmd_is(a, c"-v", 2) || cmd_is(a, c"--verbose", 9) {+ if !end_flags && cmd_is(a, c"--", 2) {+ end_flags = true+ } elif !end_flags && (cmd_is(a, c"-v", 2) || cmd_is(a, c"--verbose", 9)) { verbose = true+ } elif !end_flags && is_flag(a) {+ stdio.eprint("vn: unknown flag: {s}\n", {str.from_cstr(a)})+ return 1 } else { mut_path = str.from_cstr(a) }@@ -104,11 +120,14 @@ fn run_log(args: list[ref u8], start: usize) i32 = trust { 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 cmd_is(a, c"-r", 2) || cmd_is(a, c"--rev", 5) {+ 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", 5)) { if i + 1 < @len(args) { val v = @get(args, i + 1) if v is some rv {@@ -115,8 +134,14 @@ rev = bytes.from_cstr(rv) i++ }+ } else {+ stdio.eprint("vn: {s} requires a revision\n", {str.from_cstr(a)})+ return 1 }- } elif !cmd_is(a, c"--", 2) {+ } elif !end_flags && is_flag(a) {+ stdio.eprint("vn: unknown flag: {s}\n", {str.from_cstr(a)})+ return 1+ } else { rev = bytes.from_cstr(a) } }@@ -137,11 +162,14 @@ fn run_diff(args: list[ref u8], start: usize) i32 = trust { val revs: 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 cmd_is(a, c"-r", 2) || cmd_is(a, c"--rev", 5) {+ 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", 5)) { if i + 1 < @len(args) { val v = @get(args, i + 1) if v is some rv {@@ -148,8 +176,16 @@ @append(revs, bytes.from_cstr(rv)) i++ }+ } else {+ stdio.eprint("vn: {s} requires a revision\n", {str.from_cstr(a)})+ @listfree(revs)+ return 1 }- } elif !cmd_is(a, c"--", 2) {+ } elif !end_flags && is_flag(a) {+ stdio.eprint("vn: unknown flag: {s}\n", {str.from_cstr(a)})+ @listfree(revs)+ return 1+ } else { @append(revs, bytes.from_cstr(a)) } }@@ -172,11 +208,18 @@ fn run_new(args: list[ref u8], start: usize) i32 = trust { val revs: 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 !cmd_is(a, c"--", 2) {+ if !end_flags && cmd_is(a, c"--", 2) {+ end_flags = true+ } elif !end_flags && is_flag(a) {+ stdio.eprint("vn: unknown flag: {s}\n", {str.from_cstr(a)})+ @listfree(revs)+ return 1+ } else { @append(revs, bytes.from_cstr(a)) } }@@ -197,13 +240,16 @@ } } -fn parse_message(args: list[ref u8], start: usize) list[bytes.Bytes] = trust {+fn parse_message(args: list[ref u8], start: usize) result[list[bytes.Bytes], i32] = trust { 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 cmd_is(a, c"-m", 2) || cmd_is(a, c"--message", 9) {+ 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", 9)) { if i + 1 < @len(args) { val v = @get(args, i + 1) if v is some rv {@@ -210,18 +256,35 @@ @append(parts, bytes.from_cstr(rv)) i++ }+ } else {+ stdio.eprint("vn: {s} requires a message\n", {str.from_cstr(a)})+ @listfree(parts)+ return err 1 }- } elif !cmd_is(a, c"--", 2) {+ } elif !end_flags && is_flag(a) {+ stdio.eprint("vn: unknown flag: {s}\n", {str.from_cstr(a)})+ @listfree(parts)+ return err 1+ } else { @append(parts, bytes.from_cstr(a)) } } i++ }- return parts+ return ok parts } fn run_describe(args: list[ref u8], start: usize) i32 = trust {- val parts = parse_message(args, start)+ val parsed = parse_message(args, start)+ val parts: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()+ match parsed {+ ok x => {+ parts = x+ }+ err code => {+ return code+ }+ } val repo_opt = open_repo_or_error(str.from_cstr(c".")) match repo_opt { some repo => {@@ -238,19 +301,21 @@ } fn run_commit(args: list[ref u8], start: usize) i32 = trust {- val parts = parse_message(args, start)+ val parsed = parse_message(args, start)+ val parts: mut list[bytes.Bytes] = lists.new[bytes.Bytes]()+ match parsed {+ ok x => {+ parts = x+ }+ err code => {+ return code+ }+ } val repo_opt = open_repo_or_error(str.from_cstr(c".")) match repo_opt { some repo => {- val d: i32 = new_cmd.describe(repo, parts)+ val n: i32 = new_cmd.commit_changes(repo, parts) @listfree(parts)- if d != 0 {- layout.free_repo(repo)- return d- }- val empty: list[bytes.Bytes] = lists.new[bytes.Bytes]()- val n: i32 = new_cmd.new_change(repo, empty)- @listfree(empty) layout.free_repo(repo) return n }