|
12 | 12 |
|
13 | 13 | A strategy may not return nil." |
14 | 14 | (:require |
| 15 | + [clojure.java.shell :refer [sh]] |
15 | 16 | [clojure.string :as string] |
16 | 17 | [clojure.tools.namespace.repl :as tools.namespace.repl] |
17 | 18 | [formatting-stack.protocols.spec :as protocols.spec] |
|
27 | 28 | (def git-command "git") |
28 | 29 |
|
29 | 30 | (speced/defn all-files |
30 | | - "This strategy unconditionally processes all files." |
| 31 | + "This strategy unconditionally processes all Clojure and ClojureScript files." |
31 | 32 | [& {:keys [^::protocols.spec/filenames files]}] |
32 | 33 | ;; This first `binding` is necessary for obtaining an absolutized list of deletions |
33 | 34 | (binding [impl/*skip-existing-files-check?* true] |
|
80 | 81 | (impl/extract-clj-files) |
81 | 82 | (into files))) |
82 | 83 |
|
| 84 | +(defn current-branch-name [] |
| 85 | + (-> (sh "git" "rev-parse" "--abbrev-ref" "HEAD") |
| 86 | + (:out) |
| 87 | + (string/split-lines) |
| 88 | + (first))) |
| 89 | + |
| 90 | +(defn default-branch-name [] |
| 91 | + (let [fallback-property-name "formatting-stack.default-branch-name" |
| 92 | + fallback-branch-name "master"] |
| 93 | + (or (not-empty (System/getProperty fallback-property-name)) |
| 94 | + (let [all-branches (->> (sh "git" "branch") |
| 95 | + :out |
| 96 | + string/split-lines |
| 97 | + (map (fn [s] |
| 98 | + (-> s (string/split #"\s+") last))) |
| 99 | + (set))] |
| 100 | + (or (some all-branches ["master" "main" "stable" "dev"]) |
| 101 | + (do |
| 102 | + (println (format "No default branch could be determined. Falling back to `%s`. |
| 103 | +You can choose another one by setting the `%s` system property." fallback-branch-name fallback-property-name)) |
| 104 | + ;; return something, for not breaking code that traditionally assumed "master": |
| 105 | + fallback-branch-name)))))) |
| 106 | + |
83 | 107 | (defn git-diff-against-default-branch |
84 | 108 | "This strategy processes all files that this branch has modified. |
85 | 109 | The diff is compared against the `:target-branch` option." |
86 | 110 | [& {:keys [target-branch impl files blacklist] |
87 | | - :or {target-branch "master" |
| 111 | + :or {target-branch (default-branch-name) |
88 | 112 | ;; We filter for Added, Copied, Modified and Renamed files, |
89 | 113 | ;; excluding Unmerged, Deleted, Type-changed, Broken (pair), and Unknown files |
90 | 114 | impl (impl/file-entries git-command "diff" "--name-only" "--diff-filter=ACMR" target-branch "--") |
|
193 | 217 | (catch Throwable _ |
194 | 218 | false)))) |
195 | 219 |
|
| 220 | +(defn refactor-nrepl-3-4-1-available? [] |
| 221 | + (locking require-lock |
| 222 | + (try |
| 223 | + (requiring-resolve 'refactor-nrepl.ns.libspecs/namespace-aliases-for) |
| 224 | + true |
| 225 | + (catch Throwable _ |
| 226 | + false)))) |
| 227 | + |
196 | 228 | (speced/defn when-refactor-nrepl |
197 | 229 | "This strategy leaves all `files` as-is iff the `refactor-nrepl` library is in the classpath; |
198 | 230 | else all `files` will be filtered out." |
|
0 commit comments