Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d1b6aed5f |
@@ -11,6 +11,8 @@ sirmem new my-proj project "My Project"
|
|||||||
sirmem alias my-proj owner/repo
|
sirmem alias my-proj owner/repo
|
||||||
sirmem link my-proj uses sirlaptop "dev box"
|
sirmem link my-proj uses sirlaptop "dev box"
|
||||||
sirmem rel my-proj
|
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 prefs
|
||||||
sirmem lint
|
sirmem lint
|
||||||
```
|
```
|
||||||
@@ -19,6 +21,8 @@ Data dir: `schema.md` (types and verbs), `relations.tsv` (sorted, one direction)
|
|||||||
`entities/<id>.md` (front-matter + body). Run `sirmem --help` for all commands.
|
`entities/<id>.md` (front-matter + body). Run `sirmem --help` for all commands.
|
||||||
|
|
||||||
Build: `devbox run build`, test: `devbox run test`. See AGENTS.md.
|
Build: `devbox run build`, test: `devbox run test`. See AGENTS.md.
|
||||||
|
`rm <id>` 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.
|
Extra over the spec: `init` command; env `SIRMEM_MACHINE` overrides the detected hostname.
|
||||||
|
|
||||||
MIT licensed.
|
MIT licensed.
|
||||||
|
|||||||
+58
-1
@@ -6,7 +6,7 @@ import std/[os, strutils, sequtils, algorithm, tables, times, osproc,
|
|||||||
nativesockets, deques]
|
nativesockets, deques]
|
||||||
|
|
||||||
const
|
const
|
||||||
Version* = "0.1.1"
|
Version* = "0.1.2"
|
||||||
CycleVerbs* = ["part-of", "supersedes"]
|
CycleVerbs* = ["part-of", "supersedes"]
|
||||||
DefaultVerb* = "relates-to"
|
DefaultVerb* = "relates-to"
|
||||||
|
|
||||||
@@ -493,6 +493,49 @@ proc cmdUnlink*(st: var Store, s0, verb, o0: string): string =
|
|||||||
proc suffix(note: string): string =
|
proc suffix(note: string): string =
|
||||||
if note != "": " # " & note else: ""
|
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/<id>.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 =
|
proc cmdRel*(st: Store, id0: string): string =
|
||||||
let id = st.resolve(id0)
|
let id = st.resolve(id0)
|
||||||
var lines: seq[string]
|
var lines: seq[string]
|
||||||
@@ -722,12 +765,16 @@ commands:
|
|||||||
new <id> <type> [label] create entity
|
new <id> <type> [label] create entity
|
||||||
label <id> <new label> change label
|
label <id> <new label> change label
|
||||||
alias <id> <alias> add alias
|
alias <id> <alias> add alias
|
||||||
|
unalias <id> <alias> remove alias
|
||||||
link <s> [verb] <o> [note] add relation (default verb: relates-to)
|
link <s> [verb] <o> [note] add relation (default verb: relates-to)
|
||||||
unlink <s> <verb> <o> remove relation
|
unlink <s> <verb> <o> remove relation
|
||||||
rel <id> all relations of an entity
|
rel <id> all relations of an entity
|
||||||
who <verb> <id> subjects having <verb> relation to id
|
who <verb> <id> subjects having <verb> relation to id
|
||||||
find <text> search ids, labels, aliases, bodies, notes
|
find <text> search ids, labels, aliases, bodies, notes
|
||||||
path <a> <b> [maxdepth=4] shortest undirected path
|
path <a> <b> [maxdepth=4] shortest undirected path
|
||||||
|
rm <id> [--cascade] delete entity; refuses if it has relations
|
||||||
|
(--cascade removes them too); refuses tombstones
|
||||||
|
still referenced
|
||||||
merge <from> <into> merge entity, leave redirect tombstone
|
merge <from> <into> merge entity, leave redirect tombstone
|
||||||
prefs effective preferences for current context
|
prefs effective preferences for current context
|
||||||
lint check data; exit 1 on errors
|
lint check data; exit 1 on errors
|
||||||
@@ -766,6 +813,16 @@ proc dispatch*(dir: string, args: seq[string], cwd: string,
|
|||||||
need(args, 2, "link <s> [verb] <o> [note]")
|
need(args, 2, "link <s> [verb] <o> [note]")
|
||||||
if args.len == 3: result = cmdLink(st, args[1], DefaultVerb, args[2], "")
|
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(" "))
|
else: result = cmdLink(st, args[1], args[2], args[3], args[4 .. ^1].join(" "))
|
||||||
|
of "unalias":
|
||||||
|
need(args, 2, "unalias <id> <alias>")
|
||||||
|
result = cmdUnalias(st, args[1], args[2 .. ^1].join(" "))
|
||||||
|
of "rm":
|
||||||
|
need(args, 1, "rm <id> [--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 <id> [--cascade]")
|
||||||
|
result = cmdRm(st, ids[0], casc)
|
||||||
of "unlink":
|
of "unlink":
|
||||||
need(args, 3, "unlink <s> <verb> <o>")
|
need(args, 3, "unlink <s> <verb> <o>")
|
||||||
result = cmdUnlink(st, args[1], args[2], args[3])
|
result = cmdUnlink(st, args[1], args[2], args[3])
|
||||||
|
|||||||
@@ -174,6 +174,73 @@ suite "find / path":
|
|||||||
check run(d, "path", "a", "a").contains("same entity")
|
check run(d, "path", "a", "a").contains("same entity")
|
||||||
check fails(d, "path", "a", "e", "x").contains("integer")
|
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":
|
suite "merge":
|
||||||
test "rewrites relations, unions aliases, tombstones, resolves redirects":
|
test "rewrites relations, unions aliases, tombstones, resolves redirects":
|
||||||
let d = freshDir()
|
let d = freshDir()
|
||||||
|
|||||||
Reference in New Issue
Block a user