Map where filter needed.
This commit is contained in:
parent
dbd6f8e1e9
commit
d63e2f9ad0
132
pre-commit
132
pre-commit
|
@ -9,9 +9,9 @@
|
||||||
; below.
|
; below.
|
||||||
|
|
||||||
(def *config-path* (os.path.join (.get os.environ "GIT_DIR" "./.git") "pccs"))
|
(def *config-path* (os.path.join (.get os.environ "GIT_DIR" "./.git") "pccs"))
|
||||||
(def *modified* (.compile re "^[MA]\s+(?P<name>.*)$"))
|
(def *git-modified-pattern* (.compile re "^[MA]\s+(?P<name>.*)$"))
|
||||||
|
|
||||||
(def *checks*
|
(def *checks*
|
||||||
[
|
[
|
||||||
; {
|
; {
|
||||||
; "output" "Checking for debugger commands in Javascript..."
|
; "output" "Checking for debugger commands in Javascript..."
|
||||||
|
@ -58,105 +58,110 @@
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
(defn get-git-value [cmd]
|
(defn get-git-response [cmd]
|
||||||
(let [[fullcmd (+ ["git"] cmd)]
|
(let [[fullcmd (+ ["git"] cmd)]
|
||||||
[process (subprocess.Popen fullcmd
|
[process (subprocess.Popen fullcmd
|
||||||
:stdout subprocess.PIPE
|
:stdout subprocess.PIPE
|
||||||
:stderr subprocess.PIPE)]
|
:stderr subprocess.PIPE)]
|
||||||
[(, out err) (.communicate process)]]
|
[(, out err) (.communicate process)]]
|
||||||
(, out err process.returncode)))
|
(, out err process.returncode)))
|
||||||
|
|
||||||
(defn run-git-command [cmd]
|
(defn run-git-command [cmd]
|
||||||
(let [[fullcmd (+ ["git"] cmd)]]
|
(let [[fullcmd (+ ["git"] cmd)]]
|
||||||
(subprocess.call fullcmd
|
(subprocess.call fullcmd
|
||||||
:stdout subprocess.PIPE
|
:stdout subprocess.PIPE
|
||||||
:stderr subprocess.PIPE)))
|
:stderr subprocess.PIPE)))
|
||||||
|
|
||||||
(defn get-shell-value [fullcmd]
|
(defn get-shell-response [fullcmd]
|
||||||
(let [[process (subprocess.Popen fullcmd
|
(let [[process (subprocess.Popen fullcmd
|
||||||
:stdout subprocess.PIPE
|
:stdout subprocess.PIPE
|
||||||
:stderr subprocess.PIPE
|
:stderr subprocess.PIPE
|
||||||
:shell True)]
|
:shell True)]
|
||||||
[(, out err) (.communicate process)]]
|
[(, out err) (.communicate process)]]
|
||||||
(, out err process.returncode)))
|
(, out err process.returncode)))
|
||||||
|
|
||||||
(defn derive-max-code [code-pairs]
|
(defn derive-max-code [code-pairs]
|
||||||
(reduce
|
(reduce
|
||||||
(fn [m i] (if (> (abs (get i 0)) (abs m)) (get i 0) m))
|
(fn [m i] (if (> (abs (get i 0)) (abs m)) (get i 0) m))
|
||||||
code-pairs 0))
|
code-pairs 0))
|
||||||
|
|
||||||
(defn derive-message-bodies [code-pairs]
|
(defn derive-message-bodies [code-pairs]
|
||||||
(lmap (fn [i] (get i 1)) code-pairs))
|
(lmap (fn [i] (get i 1)) code-pairs))
|
||||||
|
|
||||||
(defn matches-file [filename match-files]
|
|
||||||
(any (map (fn [match-file] (-> (.compile re match-file)
|
|
||||||
(.match filename)))
|
|
||||||
match-files)))
|
|
||||||
|
|
||||||
; Hy is overeager to return an iterators which is consumed during
|
|
||||||
; later traversal. lmap returns a concrete list instead.
|
|
||||||
|
|
||||||
(defn lmap (pred iter) (list (map pred iter)))
|
(defn lmap (pred iter) (list (map pred iter)))
|
||||||
|
|
||||||
|
(defn encode-shell-messages [prefix messages]
|
||||||
|
(lmap (fn [line] (.format "{}{}" prefix (.decode line "utf-8")))
|
||||||
|
(.splitlines messages)))
|
||||||
|
|
||||||
(defn run-external-checker [filename check]
|
(defn run-external-checker [filename check]
|
||||||
(let [[cmd (-> (get check "command") (.format
|
(let [[cmd (-> (get check "command")
|
||||||
:filename filename
|
(.format
|
||||||
:config_path *config-path*))]
|
:filename filename
|
||||||
[(, out err returncode) (get-shell-value cmd)]]
|
:config_path *config-path*))]
|
||||||
(if (or (and out (= (.get check "error_condition" "error") "output"))
|
[(, out err returncode) (get-shell-response cmd)]]
|
||||||
err
|
(if (or (and out (= (.get check "error_condition" "error") "output"))
|
||||||
|
err
|
||||||
(not (= returncode 0)))
|
(not (= returncode 0)))
|
||||||
(let [[prefix (if (get check "print_filename")
|
(let [[prefix (if (get check "print_filename")
|
||||||
(.format "\t{}:" filename)
|
(.format "\t{}:" filename)
|
||||||
"\t")]
|
"\t")]
|
||||||
[output (+
|
[output (+ (encode-shell-messages prefix out)
|
||||||
(lmap (fn [line] (.format "{}{}" prefix (.decode line "utf-8")))
|
(if err (encode-shell-messages prefix err) []))]]
|
||||||
(.splitlines out))
|
|
||||||
(if err [err] []))]]
|
|
||||||
[(or returncode 1) output])
|
[(or returncode 1) output])
|
||||||
[0 []])))
|
[0 []])))
|
||||||
|
|
||||||
|
(defn matches-file [filename match-files]
|
||||||
|
(any (map (fn [match-file] (-> (.compile re match-file)
|
||||||
|
(.match filename)))
|
||||||
|
match-files)))
|
||||||
|
|
||||||
(defn check-scan-wanted [filename check]
|
(defn check-scan-wanted [filename check]
|
||||||
(cond [(and (in "match_files" check)
|
(cond [(and (in "match_files" check)
|
||||||
(not (matches-file filename (get check "match_files")))) false]
|
(not (matches-file filename (get check "match_files")))) false]
|
||||||
[(and (in "ignore_files" check)
|
[(and (in "ignore_files" check)
|
||||||
(matches-file filename (get check "ignore_files"))) false]
|
(matches-file filename (get check "ignore_files"))) false]
|
||||||
[true true]))
|
[true true]))
|
||||||
|
|
||||||
|
(defn tap [x] (print x) x)
|
||||||
|
|
||||||
(defn check-files [filenames check]
|
(defn check-files [filenames check]
|
||||||
(let [[filenames-to-check
|
(let [[filenames-to-check
|
||||||
(lmap (fn [filename] (check-scan-wanted filename check)) filenames)]
|
(filter (fn [filename] (check-scan-wanted filename check)) filenames)]
|
||||||
[scan-results
|
[results-of-checks
|
||||||
(lmap (fn [filename]
|
(lmap (fn [filename]
|
||||||
(run-external-checker filename check)) filenames-to-check)]
|
(run-external-checker filename check)) filenames-to-check)]
|
||||||
[messages (+ [(get check "output")] (derive-message-bodies scan-results))]]
|
[messages (+ [(get check "output")]
|
||||||
[(derive-max-code scan-results) messages]))
|
(derive-message-bodies results-of-checks))]]
|
||||||
|
[(derive-max-code results-of-checks) messages]))
|
||||||
|
|
||||||
(defn gather-all-filenames []
|
(defn gather-all-filenames []
|
||||||
(let [[build-filenames
|
(let [[build-filenames
|
||||||
(fn [filenames]
|
(fn [filenames]
|
||||||
(map
|
(map
|
||||||
(fn [f] (os.path.join (get filenames 0) f)) (get filenames 2)))]]
|
(fn [f] (os.path.join (get filenames 0) f)) (get filenames 2)))]]
|
||||||
(list (flatten (list-comp (build-filenames o) [o (.walk os ".")])))))
|
(list
|
||||||
|
(flatten
|
||||||
; I removed the originally recommended "-u" command from stash; it was
|
(list-comp (build-filenames o) [o (.walk os ".")])))))
|
||||||
; "cleaning up" far too zealously, deleting my node_modules directory
|
|
||||||
|
|
||||||
(defn gather-staged-filenames [against]
|
(defn gather-staged-filenames [against]
|
||||||
(let [[(, out err returncode)
|
(let [[(, out err returncode)
|
||||||
(get-git-value ["diff-index" "--name-status" against])]
|
(get-git-response ["diff-index" "--name-status" against])]
|
||||||
[lines (.splitlines out)]
|
[lines (.splitlines out)]
|
||||||
[matcher (fn [line] (.match *modified* (.decode line "utf-8")))]]
|
[matcher
|
||||||
(list (filter
|
(fn [line]
|
||||||
(fn [x] (not (= x "")))
|
(.match *git-modified-pattern* (.decode line "utf-8")))]]
|
||||||
(list-comp (.group match "name") [match (map matcher lines)] match)))))
|
(list
|
||||||
|
(filter
|
||||||
|
(fn [x] (not (= x "")))
|
||||||
|
(list-comp (.group match "name") [match (map matcher lines)] match)))))
|
||||||
|
|
||||||
(defn scan-files [scan-all-files against]
|
(defn run-checks-for [scan-all-files against]
|
||||||
(do
|
(do
|
||||||
(run-git-command ["stash" "--keep-index"])
|
(run-git-command ["stash" "--keep-index"])
|
||||||
(let [[filenames-to-scan
|
(let [[filenames-to-scan
|
||||||
(if scan-all-files
|
(if scan-all-files
|
||||||
(gather-all-filenames)
|
(gather-all-filenames)
|
||||||
(gather-staged-filenames against))]
|
(gather-staged-filenames against))]
|
||||||
[results-of-scan
|
[results-of-scan
|
||||||
(lmap (fn [check] (check-files filenames-to-scan check)) *checks*)]
|
(lmap (fn [check] (check-files filenames-to-scan check)) *checks*)]
|
||||||
|
@ -167,13 +172,12 @@
|
||||||
(run-git-command ["reset" "--hard"])
|
(run-git-command ["reset" "--hard"])
|
||||||
(run-git-command ["stash" "pop" "--quiet" "--index"])
|
(run-git-command ["stash" "pop" "--quiet" "--index"])
|
||||||
exit-code))))
|
exit-code))))
|
||||||
|
|
||||||
(defn get-head-tag []
|
(defn get-head-tag []
|
||||||
(let [[empty-repository-hash "4b825dc642cb6eb9a060e54bf8d69288fbee4904"]
|
(let [[empty-repository-hash "4b825dc642cb6eb9a060e54bf8d69288fbee4904"]
|
||||||
[(, out err returncode) (get-git-value ["rev-parse" "--verify HEAD"])]]
|
[(, out err returncode) (get-git-response ["rev-parse" "--verify HEAD"])]]
|
||||||
(if err empty-repository-hash "HEAD")))
|
(if err empty-repository-hash "HEAD")))
|
||||||
|
|
||||||
(defmain [&rest args]
|
(defmain [&rest args]
|
||||||
(let [[scan-all-files (and (> (len args) 1) (= (get args 2) "--all-files"))]]
|
(let [[scan-all-files (and (> (len args) 1) (= (get args 2) "--all-files"))]]
|
||||||
(sys.exit (scan-files scan-all-files (get-head-tag)))))
|
(sys.exit (run-checks-for scan-all-files (get-head-tag)))))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue