Skip to content

Instantly share code, notes, and snippets.

@henryw374
Last active January 6, 2025 16:48
Show Gist options
  • Save henryw374/30359d93c060fb564cb4804841986030 to your computer and use it in GitHub Desktop.
Save henryw374/30359d93c060fb564cb4804841986030 to your computer and use it in GitHub Desktop.
get clojure data on what files have changed in a git repo and when
(ns git-analyze
(:require [clojure.java.process :as process]
[clojure.string :as string]
[com.widdindustries.tempo :as tempo]))
(defn changed-files []
(->>
(process/exec "git" "log" "--pretty=%x0a%cI" "--name-only" "--date=iso")
(string/split-lines)
(remove string/blank?)
(reduce
(fn [r n]
(if-let [zdt (try (tempo/zdt-parse n)
(catch Throwable _))]
(vary-meta r (fn [m] (assoc m :current-zdt zdt)))
(conj r {:changed-at (-> r meta :current-zdt) :file n})))
[])))
(defn file-freq [date-group-fn changes]
(->> changes
(reduce (fn [r {:keys [changed-at file]}]
; (sc.api/spy) (comment (eval `(sc.api/defsc ~(sc.api/last-ep-id))))
(update r {:file file
:group (date-group-fn changed-at)} (fnil inc 0)))
{})
(mapv (fn [[x freq]]
(-> x (assoc :freq freq))))))
(defn file-oriented [file-group-freq]
(->>
(->
(group-by :file file-group-freq)
(update-vals (fn [xs]
(reduce
(fn [r {:keys [group freq]}]
(-> r
(assoc-in [:groups group] freq)
(update :total (fnil + 0) freq)))
{} xs))))
(mapv (fn [[file data]]
(-> data (assoc :file file))))))
(comment
(def base (changed-files))
(def file-year-freq
(->> (file-freq (comp str tempo/zdt->year) base)
;(take 5)
))
(def by-file (file-oriented file-year-freq))
; 2 most changed files
(->> by-file
(sort-by :total)
(reverse)
(take 2))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment