Skip to content

Commit ad8c871

Browse files
committed
wip: new implementation in janet
1 parent 5e1df39 commit ad8c871

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

git-skm/cli.janet

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/env janet
2+
(use ./init)
3+
4+
(defn cli/trust [args]
5+
(if (first args)
6+
(trust (first args))
7+
(error "no commit hash to trust given")))
8+
9+
(defn cli/generate-allowed-signers [args]
10+
(generate-allowed-signers))
11+
12+
(defn cli/verify-commit [args]
13+
(if (first args)
14+
(verify-commit (first args))
15+
(verify-commit "HEAD")))
16+
17+
(defn cli/help []
18+
(print `simple key management
19+
available subcommands:
20+
help - show this help
21+
generate - generate the allowed_signers file
22+
verify-commit - verify a specific commit (or HEAD if no commit ref was given)
23+
trust - set trust anchor (this is the last commit hash that you trust)`))
24+
25+
(defn main [_ & args]
26+
(case (first args)
27+
"help" (cli/help)
28+
"verify-commit" (cli/verify-commit (slice args 1 -1))
29+
"generate" (cli/generate-allowed-signers (slice args 1 -1))
30+
"trust" (cli/trust (slice args 1 -1))
31+
(cli/help)))

git-skm/init.janet

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(:import ./util :export true)
2+
3+
(defn verify-one-commit [commit]
4+
# Verify a commit using the allowed_signers from its parent
5+
)
6+
7+
(defn verify-commit [commit])
8+
9+
(defn generate-allowed-signers [repo])
10+
11+
(defn trust [repo commit])

git-skm/parser.janet

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
(def commit-grammar (peg/compile
2+
~{:main (replace (* "tree " (capture :object-id) "\n"
3+
:parents
4+
"author " :person "\n"
5+
"committer " :person "\n"
6+
(opt (* (capture :gpgsig)))
7+
"\n"
8+
(capture (to -1)))
9+
,(fn [& args]
10+
(if (= (length args) 6)
11+
{:tree (args 0) :parents (args 1) :author (args 2) :committer (args 3) :gpgsig (args 4) :message (args 5)}
12+
{:tree (args 0) :parents (args 1) :author (args 2) :committer (args 3) :message (args 4)})))
13+
:parents (replace (some (* "parent " (capture :object-id) "\n"))
14+
,(fn [& x] x))
15+
:object-id (repeat 40 :w)
16+
:person (replace (* (capture (to (* " " :timestamp))) " " :timestamp)
17+
,|{:author $0 :timestamp $1})
18+
:timestamp (replace (* (capture :unix-time) " " (capture :offset))
19+
,|{:time $0 :offset $1})
20+
:unix-time (repeat 10 :d)
21+
:offset (* (+ "+" "-") (repeat 4 :d))
22+
:gpgsig (+ (* "gpgsig -----BEGIN SSH SIGNATURE-----" (thru "-----END SSH SIGNATURE-----\n"))
23+
(* "gpgsig -----BEGIN PGP SIGNATURE-----" (thru "-----END PGP SIGNATURE-----\n \n")))
24+
}))
25+
26+
(defn parse-commit [commit]
27+
(peg/match commit-grammar commit))
28+
29+
(defn render-commit [parsed-commit])

git-skm/util.janet

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
(import spork/sh)
2+
(import spork/path)
3+
4+
(defn get-repo-root []
5+
(if (dyn :repo-root)
6+
(dyn :repo-root)
7+
(let [repo-root (sh/exec-slurp "git" "rev-parse" "--git-dir")]
8+
(setdyn :repo-root repo-root)
9+
repo-root)))
10+
11+
(defn get-repo-top-level []
12+
(if (dyn :repo-top-level)
13+
(dyn :repo-top-level)
14+
(let [repo-top-level (sh/exec-slurp "git" "rev-parse" "--show-toplevel")]
15+
(setdyn :repo-top-level repo-top-level)
16+
repo-top-level)))
17+
18+
(defn get-allowed-signers-absolute-path []
19+
(if (dyn :allowed-signers-absolute-path)
20+
(dyn :allowed-signers-absolute-path)
21+
(try
22+
(let [allowed-signers-absolute-path (sh/exec-slurp "git" "config" "--local" "skm.allowedSignersFile")]
23+
(let [stat (os/stat allowed-signers-absolute-path)]
24+
(if (or (not stat) (not= (stat :mode) :file))
25+
(error "allowedSignersFile does not exist or is not a file")))
26+
(setdyn :allowed-signers-absolute-path allowed-signers-absolute-path))
27+
([err]
28+
(do
29+
(setdyn :allowed-signers-absolute-path (path/join (get-repo-top-level) ".allowed_signers"))
30+
(sh/exec-slurp "git" "config" "--local" "skm.allowedSignersFile" (dyn :allowed-signers-absolute-path)))))))
31+
32+
(defn get-allowed-signers-relative-path []
33+
(if (dyn :allowed-signers-relative-path)
34+
(dyn :allowed-signers-relative-path)
35+
(path/relpath (get-repo-top-level) (get-allowed-signers-absolute-path))))

project.janet

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(declare-project
2+
:name "git-skm"
3+
:description "git simple key management - manages ssh keys for git repos"
4+
:dependencies ["https://github.com/janet-lang/spork"]
5+
:author "tionis.dev"
6+
:license "MIT"
7+
:url "https://tasadar.net/tionis/git-skm"
8+
:repo "git+https://tasadar.net/tionis/git-skm")
9+
10+
(declare-source
11+
:source ["git-skm"])
12+
13+
(declare-executable
14+
:name "git-skm"
15+
:entry "git-skm/cli.janet"
16+
:install true)

0 commit comments

Comments
 (0)