Skip to content

Instantly share code, notes, and snippets.

@lispyclouds
Last active November 27, 2022 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lispyclouds/896be30072704e446eb4dce2db70febf to your computer and use it in GitHub Desktop.
Save lispyclouds/896be30072704e446eb4dce2db70febf to your computer and use it in GitHub Desktop.
A commit message helper in babashka
#!/usr/bin/env bb
(require '[babashka.process :as p]
'[clojure.string :as str])
(def authors
{"rd" {:name "Rahul De"
:email "rahul080327@gmail.com"}})
(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)
@lispyclouds
Copy link
Author

lispyclouds commented Nov 27, 2022

Usage:

  • Make sure a recent version of babashka is installed
  • Have this in your PATH as a file called commit for example
  • Make it executable
  • Go to a git repo and stage the files to be committed
  • Run commit
  • Asks you for the following (the main author info is picked automatically):
    • Story/Feature: The card number or some feature name
    • Co-authors: , (comma) separated list of people whom you paired with. Add more to the authors map and don't put the primary author's name here. Leave empty to skip.
    • Message: The message you wanna use
  • Forms the commit message as:
    [story/feature] commit message
    
    Co-authored-by: author-1
    Co-authored-by: author-2
    
  • This plays really well with GitHub if all the authors have the emails used to commit in GitHub!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment