Skip to content

Commit 8fb9d1c

Browse files
committed
Better error handling when invoking git. Don't depend on bash anymore
1 parent 63e9251 commit 8fb9d1c

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

  • src
    • main/clojure/buildversion_plugin
    • test/clojure/buildversion_plugin/test

src/main/clojure/buildversion_plugin/git.clj

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@
1010
(defn run-git
1111
([args] (run-git "." args))
1212
([project-dir args]
13-
(sh/proc "bash" "-c" (str "cd " project-dir "; git " args))))
13+
(apply sh/proc `("git" ~@args :dir ~project-dir))))
1414

1515
(defn run-git-wait
1616
([args] (run-git-wait "." args))
1717
([project-dir args]
18-
(sh/stream-to-string (run-git project-dir args) :out)))
18+
(let [proc (run-git project-dir args)
19+
ok (zero? (sh/exit-code proc))
20+
stdout (sh/stream-to-string proc :out)
21+
stderr (sh/stream-to-string proc :err)]
22+
(if ok
23+
stdout
24+
(throw (RuntimeException.
25+
(str "Execution of Git command failed: " stderr)))))))
1926

2027
(defn git-describe-log-lines [log-lines-seq]
2128
"Given a seq of \"git log\" output lines, return map with :git-tag (most recent tag)
@@ -31,7 +38,7 @@
3138
"Return map with :git-tag (most recent tag on current branch (always following \"first-parent\" on merges))
3239
and :git-tag-delta (number of commits -couting on first-parent paths only- from :git-tag to HEAD) "
3340

34-
(let [p (run-git dir "log --oneline --decorate=short --first-parent")
41+
(let [p (run-git dir ["log" "--oneline" "--decorate=short" "--first-parent"])
3542
lines (line-seq (reader (:out p)))
3643
[tag delta] (git-describe-log-lines lines)]
3744
(sh/destroy p)
@@ -41,7 +48,7 @@ and :git-tag-delta (number of commits -couting on first-parent paths only- from
4148
"Infer the current project version from tags on the source-control system"
4249

4350
(let [tstamp-format (:tstamp-format config-map)
44-
commit-tstamp (-> (run-git-wait dir "log -n 1 --format='%ct'")
51+
commit-tstamp (-> (run-git-wait dir ["log" "-n" "1" "--format=%ct"])
4552
trim-newline
4653
(Long/parseLong)
4754
(* 1000)
@@ -51,7 +58,7 @@ and :git-tag-delta (number of commits -couting on first-parent paths only- from
5158
commit-tstamp)
5259

5360
[short-hash long-hash] (split
54-
(run-git-wait dir "log -n 1 --format='%h %H'")
61+
(run-git-wait dir [ "log" "-n" "1" "--format=%h %H" ])
5562
#" " )
5663

5764
versioning-properties {:build-tag "N/A"

src/test/clojure/buildversion_plugin/test/git.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
;;
4848
(defn- get-commit-hash-by-description [descr]
4949
"Given a git commit description, it returns the hash of a commit matching that description"
50-
(let [git (git/run-git-wait sample-project-dir (str "log --all --format=format:%H --grep='^" descr "$'"))
50+
(let [git (git/run-git-wait sample-project-dir ["log" "--all" "--format=format:%H" (str "--grep=^" descr "$")])
5151
commit-hash git]
5252
(is (not (blank? commit-hash)) (str "Couldn't find hash for commit descr" descr))
5353
commit-hash))
@@ -56,7 +56,7 @@
5656
"Checkout on a particular commit and validate inferred versions match the given regexp patterns"
5757

5858
(let [commit-hash (get-commit-hash-by-description commit-descr)
59-
checkout (git/run-git sample-project-dir (str "checkout " commit-hash))
59+
checkout (git/run-git sample-project-dir [ "checkout" commit-hash])
6060
checkout-OK (zero? (sh/exit-code checkout))
6161
actual-versions (git/infer-project-version sample-project-dir {})]
6262

@@ -72,7 +72,7 @@
7272
;;
7373
(deftest test-run-git
7474
(is (re-seq #"git version [\d\.]+"
75-
(git/run-git-wait sample-project-dir "--version"))))
75+
(git/run-git-wait sample-project-dir ["--version"]))))
7676

7777
(deftest test-infer-project-versions
7878

@@ -116,7 +116,7 @@
116116
;; (see sample-git-project-fixture above)
117117
(are [commitish expected-tag expected-delta]
118118
(do
119-
(git/run-git-wait sample-project-dir (str "checkout " commitish))
119+
(git/run-git-wait sample-project-dir [ "checkout" commitish])
120120
(= (git/git-describe-first-parent sample-project-dir)
121121
{:git-tag expected-tag :git-tag-delta expected-delta}))
122122

0 commit comments

Comments
 (0)