Systems in Rust
diff ongoing$ tree .git
.git
├── COMMIT_EDITMSG
├── HEAD
├── branches
├── config
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-merge-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── prepare-commit-msg.sample
│ ├── push-to-checkout.sample
│ └── update.sample
├── index
├── info
│ └── exclude
├── logs
│ ├── HEAD
│ └── refs
│ ├── heads
│ │ └── main
│ └── remotes
│ └── origin
│ ├── HEAD
│ └── main
├── objects
│ ├── 14
│ │ └── 699a133ae84e6c922ca69ffa74d4df47eae49f
│ ├── 4b
│ │ └── 41ec9ea0317e27beac7c2d77945e542c26f943
│ ├── a3
│ │ └── 1c05f02ab1855a2f7e420ee0f5b28c2d8c1008
│ ├── b9
│ │ └── 1bf6a71c95a125d9e0cd59e1149ad022b8baeb
│ ├── info
│ └── pack
│ ├── pack-6fbf46b3e979d9d021e11db53f6959e1178cbd97.idx
│ └── pack-6fbf46b3e979d9d021e11db53f6959e1178cbd97.pack
├── packed-refs
└── refs
├── heads
│ └── main
├── remotes
│ └── origin
│ ├── HEAD
│ └── main
└── tags
20 directories, 33 files./scm.py viewer{
"latest": {
"/home/user/tmp/scmpy/scm.py": [
"#!/usr/bin/env python3",
"",
"import os, sys, json, subprocess, difflib",
"",
"SCM_NAME = \".scm\"",
"DIFF_NAME = \".diff\"",
"",
"# diff helper",
"diff_lines = lambda ls_0, ls_1: [line.rstrip() for line in list(difflib.unified_diff(ls_0, ls_1))]",
"",
"# saves current version to .scm",
"def commit():",
" # Get all files that aren't hidden",
" tree = [node for node in os.walk(os.getcwd()) if \".\" not in node[0]]",
" fs = [os.path.join(node[0], n) for node in tree for n in node[2] if n[0] != \".\"]",
"",
" # Get or create .scm file",
" if not os.path.isfile(SCM_NAME) or os.path.getsize(\".scm\") == 0:",
" # create",
" latest = {f: open(f).read().splitlines() for f in fs}",
" json.dump(",
" {\"latest\": latest, \"commit\": [{\"init\": latest, \"diff\": {}}]},",
" open(SCM_NAME, \"w\"),",
" )",
" else:",
" # get",
" scm = json.loads(open(SCM_NAME, \"r\").read())",
" late = scm[\"latest\"]",
" curr = scm[\"commit\"]",
" old_fs = [f for f in curr[-1][\"init\"]] + [f for f in curr[-1][\"diff\"]]",
" new_fs = [f for f in fs if fs not in old_fs]",
" init = {f: open(f).read().splitlines() for f in new_fs if f not in old_fs}",
" # We use unified diff from difflib since it still works with patch.",
" diff = {f: diff_lines(late[f], open(f).read().splitlines()) for f in old_fs}",
" scm[\"latest\"] = {f: open(f).read().splitlines() for f in fs}",
" scm[\"commit\"].append({\"init\": init, \"diff\": diff})",
" json.dump(scm, open(SCM_NAME, \"w\"))",
"",
"",
"# pops latest without caching.",
"# not gonna ether novel files, that seems pointless?",
"def scrape():",
" (not os.path.isfile(SCM_NAME) or os.path.getsize(\".scm\") == 0) and exit()",
" [",
" open(k, \"w\").write(\"\\n\".join(v))",
" for k, v in json.loads(open(SCM_NAME, \"r\").read())[\"latest\"].items()",
" ]",
"",
"",
"# let's just roll back one, that's logically equivalent",
"def revert():",
" scrape()",
" scm = json.loads(open(SCM_NAME, \"r\").read())",
" late = scm[\"latest\"]",
" curr = scm[\"commit\"]",
" len(curr) < 2 and exit()",
" last = curr.pop()[\"diff\"]",
" for k, v in last.items():",
" open(DIFF_NAME, \"w\").write(\"\\n\".join(v))",
" os.system(\"patch -R \" + k + \" \" + DIFF_NAME)",
" json.dump(scm, open(SCM_NAME, \"w\"))",
"",
"viewer = lambda: os.system(",
" \"jq . .scm\"",
") # print(json.dumps(json.load(open(SCM_NAME)), indent=4))",
"",
"# Trivial Comment",
"",
"# Another",
"",
"__name__ == \"__main__\" and len(sys.argv) == 2 and {",
" \"commit\": commit,",
" \"scrape\": scrape,",
" \"revert\": revert,",
" \"viewer\": viewer,",
"}[sys.argv[1]]()",
""
],
"/home/user/tmp/scmpy/scm.py.orig": [
"#!/usr/bin/env python3",
"",
"import os, sys, json, subprocess, difflib",
"",
"SCM_NAME = \".scm\"",
"DIFF_NAME = \".diff\"",
"",
"# diff helper",
"diff_lines = lambda ls_0, ls_1: [line.rstrip() for line in list(difflib.unified_diff(ls_0, ls_1))]",
"",
"# saves current version to .scm",
"def commit():",
" # Get all files that aren't hidden",
" tree = [node for node in os.walk(os.getcwd()) if \".\" not in node[0]]",
" fs = [os.path.join(node[0], n) for node in tree for n in node[2] if n[0] != \".\"]",
"",
" # Get or create .scm file",
" if not os.path.isfile(SCM_NAME) or os.path.getsize(\".scm\") == 0:",
" # create",
" latest = {f: open(f).read().splitlines() for f in fs}",
" json.dump(",
" {\"latest\": latest, \"commit\": [{\"init\": latest, \"diff\": {}}]},",
" open(SCM_NAME, \"w\"),",
" )",
" else:",
" # get",
" scm = json.loads(open(SCM_NAME, \"r\").read())",
" late = scm[\"latest\"]",
" curr = scm[\"commit\"]",
" old_fs = [f for f in curr[-1][\"init\"]] + [f for f in curr[-1][\"diff\"]]",
" new_fs = [f for f in fs if fs not in old_fs]",
" init = {f: open(f).read().splitlines() for f in new_fs if f not in old_fs}",
" # We use unified diff from difflib since it still works with patch.",
" diff = {f: diff_lines(late[f], open(f).read().splitlines()) for f in old_fs}",
" scm[\"latest\"] = {f: open(f).read().splitlines() for f in fs}",
" scm[\"commit\"].append({\"init\": init, \"diff\": diff})",
" json.dump(scm, open(SCM_NAME, \"w\"))",
"",
"",
"# pops latest without caching.",
"# not gonna ether novel files, that seems pointless?",
"def scrape():",
" (not os.path.isfile(SCM_NAME) or os.path.getsize(\".scm\") == 0) and exit()",
" [",
" open(k, \"w\").write(\"\\n\".join(v))",
" for k, v in json.loads(open(SCM_NAME, \"r\").read())[\"latest\"].items()",
" ]",
"",
"",
"# let's just roll back one, that's logically equivalent",
"def revert():",
" scrape()",
" scm = json.loads(open(SCM_NAME, \"r\").read())",
" late = scm[\"latest\"]",
" curr = scm[\"commit\"]",
" len(curr) < 2 and exit()",
" last = curr.pop()[\"diff\"]",
" for k, v in last.items():",
" open(DIFF_NAME, \"w\").write(\"\\n\".join(v))",
" os.system(\"patch -R \" + k + \" \" + DIFF_NAME)",
"",
"",
"viewer = lambda: os.system(",
" \"jq . .scm\"",
") # print(json.dumps(json.load(open(SCM_NAME)), indent=4))",
"",
"__name__ == \"__main__\" and len(sys.argv) == 2 and {",
" \"commit\": commit,",
" \"scrape\": scrape,",
" \"revert\": revert,",
" \"viewer\": viewer,",
"}[sys.argv[1]]()",
""
]
},
"commit": [
{
"init": {
"/home/user/tmp/scmpy/scm.py": [
"#!/usr/bin/env python3",
"",
"import os, sys, json, subprocess, difflib",
"",
"SCM_NAME = \".scm\"",
"DIFF_NAME = \".diff\"",
"",
"# diff helper",
"diff_lines = lambda ls_0, ls_1: [line.strip() for line in list(difflib.unified_diff(ls_0, ls_1))]",
"",
"# saves current version to .scm",
"def commit():",
" # Get all files that aren't hidden",
" tree = [node for node in os.walk(os.getcwd()) if \".\" not in node[0]]",
" fs = [os.path.join(node[0], n) for node in tree for n in node[2] if n[0] != \".\"]",
"",
" # Get or create .scm file",
" if not os.path.isfile(SCM_NAME) or os.path.getsize(\".scm\") == 0:",
" # create",
" latest = {f: open(f).read().splitlines() for f in fs}",
" json.dump(",
" {\"latest\": latest, \"commit\": [{\"init\": latest, \"diff\": {}}]},",
" open(SCM_NAME, \"w\"),",
" )",
" else:",
" # get",
" scm = json.loads(open(SCM_NAME, \"r\").read())",
" late = scm[\"latest\"]",
" curr = scm[\"commit\"]",
" old_fs = [f for f in curr[-1][\"init\"]] + [f for f in curr[-1][\"diff\"]]",
" new_fs = [f for f in fs if fs not in old_fs]",
" init = {f: open(f).read().splitlines() for f in new_fs if f not in old_fs}",
" # We use unified diff from difflib since it still works with patch.",
" diff = {f: diff_lines(late[f], open(f).read().splitlines()) for f in old_fs}",
" scm[\"latest\"] = {f: open(f).read().splitlines() for f in fs}",
" scm[\"commit\"].append({\"init\": init, \"diff\": diff})",
" print(diff)",
" json.dump(scm, open(SCM_NAME, \"w\"))",
"",
"",
"# pops latest without caching.",
"# not gonna ether novel files, that seems pointless?",
"def scrape():",
" (not os.path.isfile(SCM_NAME) or os.path.getsize(\".scm\") == 0) and exit()",
" [",
" open(k, \"w\").write(\"\\n\".join(v))",
" for k, v in json.loads(open(SCM_NAME, \"r\").read())[\"latest\"].items()",
" ]",
"",
"",
"# let's just roll back one, that's logically equivalent",
"def revert():",
" scrape()",
" scm = json.loads(open(SCM_NAME, \"r\").read())",
" late = scm[\"latest\"]",
" curr = scm[\"commit\"]",
" len(curr) < 2 and exit()",
" last = curr.pop()[\"diff\"]",
" for k, v in last.items():",
" open(DIFF_NAME, \"w\").write(\"\\n\".join(v))",
" os.system(\"patch -R \" + k + \" \" + DIFF_NAME)",
"",
"",
"viewer = lambda: os.system(",
" \"jq . .scm\"",
") # print(json.dumps(json.load(open(SCM_NAME)), indent=4))",
"",
"__name__ == \"__main__\" and len(sys.argv) == 2 and {",
" \"commit\": commit,",
" \"scrape\": scrape,",
" \"revert\": revert,",
" \"viewer\": viewer,",
"}[sys.argv[1]]()"
]
},
"diff": {}
},
{
"init": {},
"diff": {
"/home/user/tmp/scmpy/scm.py": [
"---",
"+++",
"@@ -71,3 +71,5 @@",
"\"revert\": revert,",
"\"viewer\": viewer,",
"}[sys.argv[1]]()",
"+",
"+# Trivial Comment"
]
}
},
{
"init": {},
"diff": {
"/home/user/tmp/scmpy/scm.py": [
"---",
"+++",
"@@ -6,7 +6,7 @@",
" DIFF_NAME = \".diff\"",
"",
" # diff helper",
"-diff_lines = lambda ls_0, ls_1: [line.strip() for line in list(difflib.unified_diff(ls_0, ls_1))]",
"+diff_lines = lambda ls_0, ls_1: [line.rstrip() for line in list(difflib.unified_diff(ls_0, ls_1))]",
"",
" # saves current version to .scm",
" def commit():"
]
}
},
{
"init": {},
"diff": {
"/home/user/tmp/scmpy/scm.py": [
"---",
"+++",
"@@ -34,7 +34,6 @@",
" diff = {f: diff_lines(late[f], open(f).read().splitlines()) for f in old_fs}",
" scm[\"latest\"] = {f: open(f).read().splitlines() for f in fs}",
" scm[\"commit\"].append({\"init\": init, \"diff\": diff})",
"- print(diff)",
" json.dump(scm, open(SCM_NAME, \"w\"))",
"",
""
]
}
},
{
"init": {},
"diff": {
"/home/user/tmp/scmpy/scm.py": [
"---",
"+++",
"@@ -70,5 +70,3 @@",
" \"revert\": revert,",
" \"viewer\": viewer,",
" }[sys.argv[1]]()",
"-",
"-# Trivial Comment"
]
}
},
{
"init": {},
"diff": {
"/home/user/tmp/scmpy/scm.py": [
"---",
"+++",
"@@ -70,3 +70,7 @@",
" \"revert\": revert,",
" \"viewer\": viewer,",
" }[sys.argv[1]]()",
"+",
"+# Trivial Comment",
"+",
"+"
]
}
},
{
"init": {},
"diff": {
"/home/user/tmp/scmpy/scm.py": [
"---",
"+++",
"@@ -71,6 +71,5 @@",
" \"viewer\": viewer,",
" }[sys.argv[1]]()",
"",
"-# Trivial Comment",
"",
""
]
}
},
{
"init": {
"/home/user/tmp/scmpy/scm.py.orig": [
"#!/usr/bin/env python3",
"",
"import os, sys, json, subprocess, difflib",
"",
"SCM_NAME = \".scm\"",
"DIFF_NAME = \".diff\"",
"",
"# diff helper",
"diff_lines = lambda ls_0, ls_1: [line.rstrip() for line in list(difflib.unified_diff(ls_0, ls_1))]",
"",
"# saves current version to .scm",
"def commit():",
" # Get all files that aren't hidden",
" tree = [node for node in os.walk(os.getcwd()) if \".\" not in node[0]]",
" fs = [os.path.join(node[0], n) for node in tree for n in node[2] if n[0] != \".\"]",
"",
" # Get or create .scm file",
" if not os.path.isfile(SCM_NAME) or os.path.getsize(\".scm\") == 0:",
" # create",
" latest = {f: open(f).read().splitlines() for f in fs}",
" json.dump(",
" {\"latest\": latest, \"commit\": [{\"init\": latest, \"diff\": {}}]},",
" open(SCM_NAME, \"w\"),",
" )",
" else:",
" # get",
" scm = json.loads(open(SCM_NAME, \"r\").read())",
" late = scm[\"latest\"]",
" curr = scm[\"commit\"]",
" old_fs = [f for f in curr[-1][\"init\"]] + [f for f in curr[-1][\"diff\"]]",
" new_fs = [f for f in fs if fs not in old_fs]",
" init = {f: open(f).read().splitlines() for f in new_fs if f not in old_fs}",
" # We use unified diff from difflib since it still works with patch.",
" diff = {f: diff_lines(late[f], open(f).read().splitlines()) for f in old_fs}",
" scm[\"latest\"] = {f: open(f).read().splitlines() for f in fs}",
" scm[\"commit\"].append({\"init\": init, \"diff\": diff})",
" json.dump(scm, open(SCM_NAME, \"w\"))",
"",
"",
"# pops latest without caching.",
"# not gonna ether novel files, that seems pointless?",
"def scrape():",
" (not os.path.isfile(SCM_NAME) or os.path.getsize(\".scm\") == 0) and exit()",
" [",
" open(k, \"w\").write(\"\\n\".join(v))",
" for k, v in json.loads(open(SCM_NAME, \"r\").read())[\"latest\"].items()",
" ]",
"",
"",
"# let's just roll back one, that's logically equivalent",
"def revert():",
" scrape()",
" scm = json.loads(open(SCM_NAME, \"r\").read())",
" late = scm[\"latest\"]",
" curr = scm[\"commit\"]",
" len(curr) < 2 and exit()",
" last = curr.pop()[\"diff\"]",
" for k, v in last.items():",
" open(DIFF_NAME, \"w\").write(\"\\n\".join(v))",
" os.system(\"patch -R \" + k + \" \" + DIFF_NAME)",
"",
"",
"viewer = lambda: os.system(",
" \"jq . .scm\"",
") # print(json.dumps(json.load(open(SCM_NAME)), indent=4))",
"",
"__name__ == \"__main__\" and len(sys.argv) == 2 and {",
" \"commit\": commit,",
" \"scrape\": scrape,",
" \"revert\": revert,",
" \"viewer\": viewer,",
"}[sys.argv[1]]()",
"",
""
]
},
"diff": {
"/home/user/tmp/scmpy/scm.py": [
"---",
"+++",
"@@ -58,7 +58,7 @@",
" for k, v in last.items():",
" open(DIFF_NAME, \"w\").write(\"\\n\".join(v))",
" os.system(\"patch -R \" + k + \" \" + DIFF_NAME)",
"-",
"+ json.dump(scm, open(SCM_NAME, \"w\"))",
"",
" viewer = lambda: os.system(",
" \"jq . .scm\"",
"@@ -71,5 +71,5 @@",
" \"viewer\": viewer,",
" }[sys.argv[1]]()",
"",
"+# Trivial Comment",
"",
"-"
]
}
},
{
"init": {},
"diff": {
"/home/user/tmp/scmpy/scm.py.orig": [
"---",
"+++",
"@@ -71,4 +71,3 @@",
" \"viewer\": viewer,",
" }[sys.argv[1]]()",
"",
"-"
],
"/home/user/tmp/scmpy/scm.py": [
"---",
"+++",
"@@ -64,6 +64,8 @@",
" \"jq . .scm\"",
" ) # print(json.dumps(json.load(open(SCM_NAME)), indent=4))",
"",
"+# Trivial Comment",
"+",
" __name__ == \"__main__\" and len(sys.argv) == 2 and {",
" \"commit\": commit,",
" \"scrape\": scrape,",
"@@ -71,9 +73,3 @@",
" \"viewer\": viewer,",
" }[sys.argv[1]]()",
"",
"-# Trivial Comment",
"-",
"-# Another",
"-",
"-",
"-"
]
}
}
]
}Decide whether you want to navigate a file system and crush text and be rad as heck, or learn
serde, toss everything in a single file, and be cool as heck
patch for example (this will probably explode on Windows, which is a you-problem).black formatted.patch
cargo fmt
Version control includes options to view old versions and to revert a file to a previous version.
commit saves the current version of the files under control/managementrevert overwrites the current version with the previous version.git log. prefixed) file and folder in the current path, recursively..scm file
.git.scm does not exist, orgit commit!init and add - I use neither
./scm.py commit just to test equivalencies.scm.py
fs is the Python list (so a Rust Vec) that contains the full paths as strings of all non-hidden files, recursively.
.scm file (or folder) which I use to save versions.match here, but you do you!.scm.gitscmgit gets away with it, so…HashMap or a serde_json::Map).scm file.commited version of all files.Vec)
.scmfs - the current eligible files.diff I like can’t be used gracefully from Python.
diff in a subprocess.check_output, ordifflib which makes unified diffs.scm.py
.scm files in that repository, the diffs are -u diffs, and may appear “messy”\nscm.py it will explode if you don’t have an empty line at the end of your file.difflib and patch can’t handle files that end without a trailing new line..scmdiff in a JSON object / Python Dictionary / Rust HashMap / serde_json::Map.scm.scm using your preferred JSON library…
git commit - since you will explode your code all the time../scm.py scrape and get back the latest good version.
diff’ed..scm, and unconditionally write everything in “latest” to the file system.black-exploded) line.
.scm file) if you did not keep “latest” around.patch -R).scm
.scmpatch so I had to hack a bit.os.system to call patch, then clean up latter.patch in Rust.
.scm, remove .diff, and exit gracefully.scm.pypython3 -m json-tool and it was ugly so I played around a bit.jqjq on it.scm.py
json, sogit show --stat