diff --git a/README.md b/README.md index 7fdf4ff..a6020a7 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ sirmem new my-proj project "My Project" sirmem alias my-proj owner/repo sirmem link my-proj uses sirlaptop "dev box" sirmem rel my-proj +sirmem unalias my-proj owner/repo # remove an alias +sirmem rm sirlaptop --cascade # delete an entity (and its relations) sirmem prefs sirmem lint ``` @@ -19,6 +21,8 @@ Data dir: `schema.md` (types and verbs), `relations.tsv` (sorted, one direction) `entities/.md` (front-matter + body). Run `sirmem --help` for all commands. Build: `devbox run build`, test: `devbox run test`. See AGENTS.md. +`rm ` refuses while the entity has relations (listed); `--cascade` removes them too and prints each one. +A merge tombstone is never removed while relations or other redirects still point to it. Extra over the spec: `init` command; env `SIRMEM_MACHINE` overrides the detected hostname. MIT licensed. diff --git a/src/sirmem/core.nim b/src/sirmem/core.nim index 8d3480b..ac772f1 100644 --- a/src/sirmem/core.nim +++ b/src/sirmem/core.nim @@ -6,7 +6,7 @@ import std/[os, strutils, sequtils, algorithm, tables, times, osproc, nativesockets, deques] const - Version* = "0.1.1" + Version* = "0.1.2" CycleVerbs* = ["part-of", "supersedes"] DefaultVerb* = "relates-to" @@ -493,6 +493,49 @@ proc cmdUnlink*(st: var Store, s0, verb, o0: string): string = proc suffix(note: string): string = if note != "": " # " & note else: "" +proc cmdUnalias*(st: var Store, id0, alias: string): string = + ## Acts on the entity named, tombstones included (no redirect resolving). + if id0 notin st.entities: fail("entity not found: " & id0) + let a = alias.strip.replace(",", " ").oneLine + var e = st.entities[id0] + let idx = e.aliases.findIt(it.toLowerAscii == a.toLowerAscii) + if a == "" or idx < 0: fail("alias not found on " & id0 & ": " & alias.strip) + e.aliases.delete(idx) + st.saveEntity(e) + "aliases of " & id0 & ": " & e.aliases.join(", ") + +proc cmdRm*(st: var Store, id: string, cascade: bool): string = + ## Deletes entities/.md. Acts on the named id (a tombstone is not resolved). + st.checkWritable + if id notin st.entities: fail("entity not found: " & id) + let e = st.entities[id] + let rels = st.rels.filterIt(it.s == id or it.o == id) + let relLines = rels.mapIt(" " & it.s & " " & it.v & " " & it.o & suffix(it.note)) + if e.redirect != "": + let pointing = toSeq(st.entities.pairs).filterIt(it[1].redirect == id).mapIt(it[0]) + if rels.len > 0 or pointing.len > 0: + var msg = "refusing to remove tombstone " & id & " (merged into " & e.redirect & + "); still referenced, resolve first:" + if rels.len > 0: msg.add "\nrelations:\n" & relLines.join("\n") + if pointing.len > 0: msg.add "\nredirects:\n" & pointing.mapIt(" " & it & " -> " & id).join("\n") + fail(msg) + elif rels.len > 0 and not cascade: + fail("refusing to remove " & id & ": it has " & $rels.len & + " relation(s) (use --cascade to remove them too):\n" & relLines.join("\n")) + var lines: seq[string] + if rels.len > 0: + st.rels = st.rels.filterIt(not (it.s == id or it.o == id)) + st.saveRels + for l in relLines: lines.add "removed relation" & l[1 .. ^1] + let path = st.entityPath(id) + let fp = if fileExists(path): path else: st.dir / "entities" / (e.fileId & ".md") + removeFile(fp) + st.entities.del id + lines.add "removed entities/" & splitFile(fp).name & ".md" + for t, te in st.entities: + if te.redirect == id: st.warnings.add "tombstone " & t & " now redirects to removed " & id + lines.join("\n") + proc cmdRel*(st: Store, id0: string): string = let id = st.resolve(id0) var lines: seq[string] @@ -722,12 +765,16 @@ commands: new [label] create entity label change label alias add alias + unalias remove alias link [verb] [note] add relation (default verb: relates-to) unlink remove relation rel all relations of an entity who subjects having relation to id find search ids, labels, aliases, bodies, notes path [maxdepth=4] shortest undirected path + rm [--cascade] delete entity; refuses if it has relations + (--cascade removes them too); refuses tombstones + still referenced merge merge entity, leave redirect tombstone prefs effective preferences for current context lint check data; exit 1 on errors @@ -766,6 +813,16 @@ proc dispatch*(dir: string, args: seq[string], cwd: string, need(args, 2, "link [verb] [note]") if args.len == 3: result = cmdLink(st, args[1], DefaultVerb, args[2], "") else: result = cmdLink(st, args[1], args[2], args[3], args[4 .. ^1].join(" ")) + of "unalias": + need(args, 2, "unalias ") + result = cmdUnalias(st, args[1], args[2 .. ^1].join(" ")) + of "rm": + need(args, 1, "rm [--cascade]") + let rest = args[1 .. ^1] + let casc = "--cascade" in rest + let ids = rest.filterIt(it != "--cascade") + if ids.len != 1: fail("usage: sirmem rm [--cascade]") + result = cmdRm(st, ids[0], casc) of "unlink": need(args, 3, "unlink ") result = cmdUnlink(st, args[1], args[2], args[3]) diff --git a/tests/test_all.nim b/tests/test_all.nim index 86d2325..a08aa8d 100644 --- a/tests/test_all.nim +++ b/tests/test_all.nim @@ -174,6 +174,73 @@ suite "find / path": check run(d, "path", "a", "a").contains("same entity") check fails(d, "path", "a", "e", "x").contains("integer") +suite "rm / unalias": + test "unalias removes, errors when absent": + let d = freshDir() + seed(d) + discard run(d, "alias", "a", "Foo") + discard run(d, "alias", "a", "bar") + check run(d, "unalias", "a", "foo") == "aliases of a: bar" + check readFile(d / "entities" / "a.md").contains("aliases: bar\n") + check fails(d, "unalias", "a", "foo").contains("alias not found") + check fails(d, "unalias", "zzz", "x").contains("entity not found") + + test "rm without relations": + let d = freshDir() + seed(d) + check run(d, "rm", "a") == "removed entities/a.md" + check not fileExists(d / "entities" / "a.md") + check fails(d, "rm", "a").contains("entity not found") + check not fileExists(d / "entities" / "a.md.tmp") + + test "rm refuses and lists relations in both directions": + let d = freshDir() + seed(d) + discard run(d, "link", "a", "part-of", "b", "why") + discard run(d, "link", "c", "part-of", "a") + let m = fails(d, "rm", "a") + check m.contains("refusing to remove a") + check m.contains(" a part-of b # why") + check m.contains(" c part-of a") + check fileExists(d / "entities" / "a.md") + check relsText(d).count("\n") == 2 + + test "rm --cascade removes relations and file, prints them": + let d = freshDir() + seed(d) + discard run(d, "link", "a", "part-of", "b", "why") + discard run(d, "link", "c", "part-of", "a") + discard run(d, "link", "c", "b") + check run(d, "rm", "a", "--cascade") == + "removed relation a part-of b # why\nremoved relation c part-of a\nremoved entities/a.md" + check relsText(d) == "b\trelates-to\tc\n" + check not fileExists(d / "entities" / "a.md") + check run(d, "lint").contains("0 errors") + + test "tombstone: rm acts on the tombstone, protected while referenced": + let d = freshDir() + seed(d) + discard run(d, "merge", "a", "b") + # tombstone a is unreferenced: rm removes it, not b + check run(d, "rm", "a") == "removed entities/a.md" + check fileExists(d / "entities" / "b.md") + # referenced by a relation + let d2 = freshDir() + seed(d2) + discard run(d2, "merge", "a", "b") + writeFile(d2 / "relations.tsv", "a\tpart-of\tc\n") + var m = fails(d2, "rm", "a", "--cascade") + check m.contains("tombstone a") and m.contains(" a part-of c") + check fileExists(d2 / "entities" / "a.md") + # referenced by another redirect + let d3 = freshDir() + seed(d3) + discard run(d3, "merge", "a", "b") + discard run(d3, "merge", "b", "c") + m = fails(d3, "rm", "b") + check m.contains("tombstone b") and m.contains("a -> b") + check fileExists(d3 / "entities" / "b.md") + suite "merge": test "rewrites relations, unions aliases, tombstones, resolves redirects": let d = freshDir()