Skip to content

Commit 81b247a

Browse files
committed
Avoid blocking on large git output
1 parent 15b44f3 commit 81b247a

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Changelog
22
===========
33

4+
* next
5+
* Don't block on big git output
46
* 2.4.181 on Jun 19, 2022
57
* Sort tags in version order
68
* 2.4.176 on Jun 16, 2022

src/main/clojure/clojure/tools/gitlibs.clj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,7 @@
111111
@(future (procure "https://github.com/cognitect-labs/test-runner.git" 'cognitect-labs/test-runner "9e1098965f2089c8cf492d23c0b7520f8690440a")))
112112

113113
(tags "https://github.com/clojure/tools.gitlibs.git")
114+
115+
;; big output
116+
(tags "https://github.com/confluentinc/kafka-streams-examples.git")
114117
)

src/main/clojure/clojure/tools/gitlibs/impl.clj

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[clojure.tools.gitlibs.config :as config])
1515
(:import
1616
[java.lang ProcessBuilder$Redirect]
17-
[java.io File FilenameFilter IOException]))
17+
[java.io File FilenameFilter InputStream IOException StringWriter]))
1818

1919
(set! *warn-on-reflection* true)
2020

@@ -24,6 +24,16 @@
2424
(binding [*out* *err*]
2525
(apply println msgs)))
2626

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+
2737
(defn- run-git
2838
[& args]
2939
(let [{:gitlibs/keys [command debug terminal]} @config/CONFIG
@@ -34,10 +44,10 @@
3444
_ (when debug (.redirectError proc-builder ProcessBuilder$Redirect/INHERIT))
3545
_ (when-not terminal (.put (.environment proc-builder) "GIT_TERMINAL_PROMPT" "0"))
3646
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})))
4151

4252
;; dirs
4353

@@ -135,7 +145,7 @@
135145
(when (zero? exit)
136146
(keyword (str/trimr out)))))
137147

138-
;; git merge-base --is-ancestor <maybe-ancestor-commit> <descendant-commit>
148+
;; git merge-base --is-ancestor <maybe-ancestor-commit> <descendant-commit>
139149
(defn- ancestor?
140150
[git-dir x y]
141151
(let [{:keys [exit err] :as ret} (run-git "--git-dir" git-dir "merge-base" "--is-ancestor" x y)]

0 commit comments

Comments
 (0)