|
1 | 1 | (ns git
|
2 | 2 | (:require
|
3 | 3 | [babashka.fs :as fs]
|
4 |
| - [clojure.pprint :as pprint] |
5 | 4 | [clojure.string :as string]
|
6 | 5 | dir
|
7 | 6 | docker
|
8 |
| - [hasch.core :as hasch])) |
| 7 | + [hasch.core :as hasch] |
| 8 | + jsonrpc)) |
9 | 9 |
|
10 | 10 | (set! *warn-on-reflection* true)
|
11 | 11 |
|
|
58 | 58 |
|
59 | 59 | (defn pull [{:keys [dir ref]}]
|
60 | 60 | (docker/run-container
|
61 |
| - {:image "alpine/git:latest" |
62 |
| - :host-dir (str dir) |
63 |
| - :command (concat ["pull" "origin"] |
64 |
| - (when ref [ref]))})) |
| 61 | + (merge |
| 62 | + {:image "alpine/git:latest" |
| 63 | + :command (concat ["pull" "origin"] |
| 64 | + (when ref [ref]))} |
| 65 | + (if (string/starts-with? (str dir) "/prompts") |
| 66 | + {:working-dir (str dir) |
| 67 | + :mounts ["docker-prompts:/prompts:rw"]} |
| 68 | + {:host-dir (str dir)})))) |
65 | 69 |
|
66 | 70 | (defn clone [{:keys [dir owner repo ref ref-hash]}]
|
67 | 71 | (docker/run-container
|
68 |
| - {:image "alpine/git:latest" |
69 |
| - :host-dir (str dir) |
70 |
| - :command (concat ["clone" "--depth" "1" (format "https://github.com/%s/%s" owner repo)] |
71 |
| - (when ref ["-b" ref]) |
72 |
| - [(format "/project/%s" ref-hash)])})) |
| 72 | + (merge |
| 73 | + {:image "alpine/git:latest"} |
| 74 | + (if (string/starts-with? (str dir) "/prompts") |
| 75 | + {:working-dir (str dir) |
| 76 | + :command (concat ["clone" "--depth" "1" (format "https://github.com/%s/%s" owner repo)] |
| 77 | + (when ref ["-b" ref]) |
| 78 | + [(format "/prompts/%s" ref-hash)]) |
| 79 | + :mounts ["docker-prompts:/prompts:rw"]} |
| 80 | + {:host-dir (str dir) |
| 81 | + :command (concat ["clone" "--depth" "1" (format "https://github.com/%s/%s" owner repo)] |
| 82 | + (when ref ["-b" ref]) |
| 83 | + [(format "/project/%s" ref-hash)])})))) |
73 | 84 |
|
74 | 85 | (defn prompt-file
|
75 | 86 | "returns the path or nil if the github ref does not resolve
|
|
78 | 89 | (when-let [{:keys [ref path] :as git-ref-map} (parse-github-ref ref)]
|
79 | 90 | (let [ref-hash (hashch (select-keys git-ref-map [:owner :repo :ref]))
|
80 | 91 | dir (fs/file (prompts-cache) ref-hash)
|
81 |
| - _ (if (fs/exists? dir) |
| 92 | + m (if (fs/exists? dir) |
82 | 93 | (pull {:dir dir :ref ref})
|
83 | 94 | (clone (merge git-ref-map {:dir (fs/parent dir) :ref-hash ref-hash})))]
|
| 95 | + (when (not (= 0 (:exit-code m))) |
| 96 | + (jsonrpc/notify :error {:content (str m)})) |
84 | 97 | (if path
|
85 | 98 | (let [cached-path (fs/file dir path)]
|
86 | 99 | (if (fs/exists? cached-path)
|
|
0 commit comments