|
1 | 1 | (ns git
|
2 | 2 | (:require
|
3 | 3 | [babashka.fs :as fs]
|
4 |
| - [babashka.process :as p] |
| 4 | + [clojure.pprint :as pprint] |
5 | 5 | [clojure.string :as string]
|
6 | 6 | dir
|
| 7 | + docker |
7 | 8 | [hasch.core :as hasch]))
|
8 | 9 |
|
9 | 10 | (set! *warn-on-reflection* true)
|
|
55 | 56 | (fs/create-dirs default-dir)
|
56 | 57 | default-dir))))
|
57 | 58 |
|
| 59 | +(defn pull [{:keys [dir ref]}] |
| 60 | + (docker/run-container |
| 61 | + {:image "alpine/git:latest" |
| 62 | + :host-dir (str dir) |
| 63 | + :command (concat ["pull" "origin"] |
| 64 | + (when ref [ref]))})) |
| 65 | + |
| 66 | +(defn clone [{:keys [dir owner repo ref ref-hash]}] |
| 67 | + (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)])})) |
| 73 | + |
58 | 74 | (defn prompt-file
|
59 | 75 | "returns the path or nil if the github ref does not resolve
|
60 | 76 | throws if the path in the repo does not exist or if the clone fails"
|
61 | 77 | [ref]
|
62 |
| - (when-let [{:keys [owner repo ref path] :as git-ref-map} (parse-github-ref ref)] |
| 78 | + (when-let [{:keys [ref path] :as git-ref-map} (parse-github-ref ref)] |
63 | 79 | (let [ref-hash (hashch (select-keys git-ref-map [:owner :repo :ref]))
|
64 | 80 | dir (fs/file (prompts-cache) ref-hash)
|
65 | 81 | _ (if (fs/exists? dir)
|
66 |
| - (-> (apply p/process |
67 |
| - {:dir dir} |
68 |
| - (concat |
69 |
| - ["git" "pull" "origin"] |
70 |
| - (when ref [ref]))) |
71 |
| - (deref)) |
72 |
| - (-> (apply p/process |
73 |
| - {:dir (fs/parent dir)} |
74 |
| - (concat |
75 |
| - ["git" "clone" "--depth" "1" (format "https://github.com/%s/%s" owner repo)] |
76 |
| - (when ref ["-b" ref]) |
77 |
| - [ref-hash])) |
78 |
| - (deref) |
79 |
| - (p/check)))] |
| 82 | + (pull {:dir dir :ref ref}) |
| 83 | + (clone (merge git-ref-map {:dir (fs/parent dir) :ref-hash ref-hash})))] |
80 | 84 | (if path
|
81 | 85 | (let [cached-path (fs/file dir path)]
|
82 | 86 | (if (fs/exists? cached-path)
|
83 | 87 | cached-path
|
84 | 88 | (throw (ex-info "repo exists but path does not" {:ref ref}))))
|
85 | 89 | dir))))
|
86 | 90 |
|
| 91 | +(comment |
| 92 | + (prompt-file "github:docker/labs-make-runbook?path=prompts/docker/prompt.md")) |
| 93 | + |
87 | 94 | (comment
|
88 | 95 | (fs/create-dir (prompts-cache))
|
89 | 96 | (def x "github:docker/labs-make-runbook?ref=main&path=prompts/docker")
|
|
0 commit comments