Last active
November 27, 2022 23:19
-
-
Save lispyclouds/896be30072704e446eb4dce2db70febf to your computer and use it in GitHub Desktop.
A commit message helper in babashka
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(require '[babashka.process :as p] | |
'[clojure.string :as str]) | |
(def authors | |
{"rd" {:name "Rahul De" | |
:email "[email protected]"}}) | |
(defn exec | |
[cmd] | |
(-> cmd | |
(p/sh) | |
(p/check) | |
(:out) | |
(str/trim))) | |
(defn prompt | |
[message] | |
(printf "%s: " message) | |
(flush) | |
(read-line)) | |
(defn bail! | |
[msg] | |
(binding [*out* *err*] | |
(println msg)) | |
(System/exit 1)) | |
(defn primary-author? | |
[email short-name] | |
(= email (get-in authors [short-name :email]))) | |
(defn git-status | |
[] | |
(try | |
(exec "git status --porcelain") | |
(catch Exception _ ""))) | |
(defn validate-author | |
[short-name] | |
(when-not (contains? authors short-name) | |
(bail! (format "Unrecognised author %s. Choose from: %s" | |
short-name | |
(str/join ", " (keys authors)))))) | |
(defn make-co-author-msg | |
[co-authors] | |
(let [email (exec "git config --get user.email")] | |
(->> co-authors | |
(filter #(not (primary-author? email %))) | |
(map #(authors %)) | |
(map #(format "Co-authored-by: %s <%s>" | |
(% :name) | |
(% :email))) | |
(str/join \newline)))) | |
(defn main | |
[] | |
(when (empty? (git-status)) | |
(bail! "Not a valid git repo or no changes to commit.")) | |
(let [story (prompt "Story/Feature") | |
co-authors (prompt "Co-authors (short-names separated by ,)") | |
co-authors (filter seq (str/split co-authors #"\s*,\s*")) | |
_ (run! validate-author co-authors) | |
message (prompt "Message")] | |
(try | |
(exec (format "git commit --cleanup=verbatim -m \"[%s] %s\n\n%s\"" | |
story | |
message | |
(make-co-author-msg co-authors))) | |
(catch Exception ex | |
(-> ex | |
(ex-data) | |
(:out) | |
(bail!)))))) | |
(when (= *file* (System/getProperty "babashka.file")) | |
(main) | |
nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
commit
for examplecommit
,
(comma) separated list of people whom you paired with. Add more to theauthors
map and don't put the primary author's name here. Leave empty to skip.