|
14 | 14 | [clojure.tools.gitlibs.config :as config])
|
15 | 15 | (:import
|
16 | 16 | [java.lang ProcessBuilder$Redirect]
|
17 |
| - [java.io File FilenameFilter IOException])) |
| 17 | + [java.io File FilenameFilter InputStream IOException StringWriter])) |
18 | 18 |
|
19 | 19 | (set! *warn-on-reflection* true)
|
20 | 20 |
|
|
24 | 24 | (binding [*out* *err*]
|
25 | 25 | (apply println msgs)))
|
26 | 26 |
|
| 27 | +(defn capture |
| 28 | + "Reads from input-stream until EOF and returns a String (or nil if 0 length). |
| 29 | + Takes same opts as clojure.java.io/copy - :buffer and :encoding" |
| 30 | + [^InputStream input-stream] |
| 31 | + (let [writer (StringWriter.)] |
| 32 | + (jio/copy input-stream writer) |
| 33 | + (let [s (str/trim (.toString writer))] |
| 34 | + (when-not (zero? (.length s)) |
| 35 | + s)))) |
| 36 | + |
27 | 37 | (defn- run-git
|
28 | 38 | [& args]
|
29 | 39 | (let [{:gitlibs/keys [command debug terminal]} @config/CONFIG
|
|
34 | 44 | _ (when debug (.redirectError proc-builder ProcessBuilder$Redirect/INHERIT))
|
35 | 45 | _ (when-not terminal (.put (.environment proc-builder) "GIT_TERMINAL_PROMPT" "0"))
|
36 | 46 | proc (.start proc-builder)
|
37 |
| - exit (.waitFor proc) |
38 |
| - out (slurp (.getInputStream proc)) |
39 |
| - err (slurp (.getErrorStream proc))] ;; if debug is true, stderr will be redirected instead |
40 |
| - {:args command-args, :exit exit, :out out, :err err}))) |
| 47 | + out (future (capture (.getInputStream proc))) |
| 48 | + err (future (capture (.getErrorStream proc))) ;; if debug is true, stderr will be redirected instead |
| 49 | + exit (.waitFor proc)] |
| 50 | + {:args command-args, :exit exit, :out @out, :err @err}))) |
41 | 51 |
|
42 | 52 | ;; dirs
|
43 | 53 |
|
|
135 | 145 | (when (zero? exit)
|
136 | 146 | (keyword (str/trimr out)))))
|
137 | 147 |
|
138 |
| -;; git merge-base --is-ancestor <maybe-ancestor-commit> <descendant-commit> |
| 148 | +;; git merge-base --is-ancestor <maybe-ancestor-commit> <descendant-commit> |
139 | 149 | (defn- ancestor?
|
140 | 150 | [git-dir x y]
|
141 | 151 | (let [{:keys [exit err] :as ret} (run-git "--git-dir" git-dir "merge-base" "--is-ancestor" x y)]
|
|
0 commit comments