|
1 | 1 | (ns eca.features.index |
2 | 2 | (:require |
3 | 3 | [babashka.fs :as fs] |
| 4 | + [clojure.core.memoize :as memoize] |
4 | 5 | [clojure.java.shell :as shell] |
5 | | - [clojure.string :as string])) |
| 6 | + [clojure.string :as string] |
| 7 | + [eca.shared :as shared])) |
6 | 8 |
|
7 | 9 | (set! *warn-on-reflection* true) |
8 | 10 |
|
| 11 | +(def ^:private ttl-git-ls-files-ms 5000) |
| 12 | + |
| 13 | +(defn ^:private git-ls-files* [root-path] |
| 14 | + (try |
| 15 | + (some-> (shell/sh "git" "ls-files" "--others" "--exclude-standard" "--cached" |
| 16 | + :dir root-path) |
| 17 | + :out |
| 18 | + (string/split #"\n")) |
| 19 | + |
| 20 | + (catch Exception _ nil))) |
| 21 | + |
| 22 | +(def ^:private git-ls-files (memoize/ttl git-ls-files* :ttl/threshold ttl-git-ls-files-ms)) |
| 23 | + |
9 | 24 | (defn filter-allowed [file-paths root-filename config] |
10 | 25 | (reduce |
11 | 26 | (fn [files {:keys [type]}] |
12 | 27 | (case type |
13 | | - :gitignore (let [git-files (try (some->> (some-> (shell/sh "git" "ls-files" |
14 | | - :dir root-filename) |
15 | | - :out |
16 | | - (string/split #"\n")) |
17 | | - (mapv (comp str fs/canonicalize #(fs/file root-filename %))) |
18 | | - set) |
19 | | - (catch Exception _ nil))] |
20 | | - (println git-files (str (last files))) |
| 28 | + :gitignore (let [git-files (some->> (git-ls-files root-filename) |
| 29 | + (mapv (comp str fs/canonicalize #(fs/file root-filename %))) |
| 30 | + set)] |
21 | 31 | (if (seq git-files) |
22 | 32 | (filter (fn [file] |
23 | 33 | (contains? git-files (str file))) |
|
26 | 36 | files)) |
27 | 37 | file-paths |
28 | 38 | (get-in config [:index :ignoreFiles]))) |
| 39 | + |
| 40 | +(defn insert-path [tree parts] |
| 41 | + (if (empty? parts) |
| 42 | + tree |
| 43 | + (let [head (first parts) |
| 44 | + tail (rest parts)] |
| 45 | + (update tree head #(insert-path (or % {}) tail))))) |
| 46 | + |
| 47 | +(defn tree->str |
| 48 | + ([tree] (tree->str tree 0)) |
| 49 | + ([tree indent] |
| 50 | + (let [indent-str (fn [level] (apply str (repeat (* 1 level) " ")))] |
| 51 | + (apply str |
| 52 | + (mapcat (fn [[k v]] |
| 53 | + (let [current-line (str (indent-str indent) k "\n") |
| 54 | + children-str (when (seq v) |
| 55 | + (tree->str v (inc indent)))] |
| 56 | + [current-line children-str])) |
| 57 | + (sort tree)))))) |
| 58 | + |
| 59 | +(defn repo-map [db & {:keys [as-string?]}] |
| 60 | + (let [tree (reduce |
| 61 | + (fn [t {:keys [uri]}] |
| 62 | + (let [root-filename (shared/uri->filename uri) |
| 63 | + files (git-ls-files root-filename)] |
| 64 | + (merge t |
| 65 | + {root-filename |
| 66 | + (reduce |
| 67 | + (fn [tree path] |
| 68 | + (insert-path tree (clojure.string/split path #"/"))) |
| 69 | + {} |
| 70 | + files)}))) |
| 71 | + {} |
| 72 | + (:workspace-folders db))] |
| 73 | + (if as-string? |
| 74 | + (tree->str tree) |
| 75 | + tree))) |
| 76 | + |
| 77 | +(comment |
| 78 | + (require 'user) |
| 79 | + (user/with-workspace-root "file:///home/greg/dev/eca" |
| 80 | + (println (repo-map user/*db* {:as-string? true})))) |
0 commit comments