Skip to content

Instantly share code, notes, and snippets.

@MrGung
Created February 2, 2023 09:24
Show Gist options
  • Save MrGung/e376a9eb76feb6385416e6a2816275a8 to your computer and use it in GitHub Desktop.
Save MrGung/e376a9eb76feb6385416e6a2816275a8 to your computer and use it in GitHub Desktop.
Automatically modifying (certain) git commit-messages during a rebase.
(ns git.rebase.remove-noap
"Ersetzt bei einem Rebase evtl. vorhandene \\[noap\\S*?\\]\\s*
Aufruf mit `git rebase --exec \"bb -m git.rebase.remove-noap/do!\" origin/develop`"
(:require
[babashka.fs :as fs]
[clojure.string :as str]
[git.core :as git]
[common-file :as cof]
[git.hooks.impl.logging :as log]))
;; Zur Verwendung mit git rebase:
;; git rebase --exec "bb -m git.rebase.remove-noap/do!" origin/develop
;; siehe https://nebulab.com/blog/git-rebase-exec
(defn print-proc-result-on-error [result]
(let [exit (:exit result)
err (-> result :err slurp)]
(if (not= 0 exit)
(do
(log/error (format "Fehler (%s):\n%s" exit err))
true)
false)))
(def noap-pattern #"\[noap\S*?\]\s*")
(defn replace [noap-pattern current-commit-msg]
(str/replace current-commit-msg noap-pattern ""))
(defn replace-and-commit [cwd noap-pattern current-commit-msg]
(let [new-commit-msg (replace noap-pattern current-commit-msg)
tmp-file (cof/create-temp-file-with-content new-commit-msg)]
(println (format "\n\n%s\n~~~>\n%s\n\n" current-commit-msg new-commit-msg))
(let [result (git/amend-message-of-current-commit-from-file tmp-file)]
(print-proc-result-on-error result))))
(defn do! []
(log/enable-logging)
(let [cwd (.toString (fs/cwd))
current-commit-msg (git/get-message-of-latest-commit cwd)]
(when (re-find noap-pattern current-commit-msg)
(replace-and-commit cwd noap-pattern current-commit-msg))
(System/exit 0)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment