33 (:import java.util.Date java.text.SimpleDateFormat)
44 (:use ; ; [clojure.tools.trace :only [dotrace deftrace]]
55 [clojure.java.io :only [reader]]
6- [clojure.string :only [trim-newline replace-first split]] )
6+ [clojure.string :only [trim-newline replace-first split join ]] )
77 (:require
88 [conch.core :as sh] ))
99
10+
11+ (def ^:dynamic *debug-fn* #(println %))
12+ (def ^:dynamic *git-cmd* " git" )
13+
14+
1015(defn run-git
1116 ([args] (run-git " ." args))
12- ([project-dir args]
13- (apply sh/proc `(" git" ~@args :dir ~project-dir))))
17+ ([project-dir args]
18+ (*debug-fn* (str " Running cmd: " *git-cmd* " " (join " " args)))
19+ (apply sh/proc `(~*git-cmd* ~@args :dir ~project-dir))))
1420
1521(defn run-git-wait
1622 ([args] (run-git-wait " ." args))
2834 " Given a seq of \" git log\" output lines, return map with :git-tag (most recent tag)
2935 and :git-tag-delta (number of commits to reach it)"
3036 (loop [i 0 , lines log-lines-seq]
31- (let [[_ hash tag] (re-find #"^(\w +) .*tag: (v[^\)\, ]+).*" (first lines))]
37+ (let [line (first lines)
38+ [_ hash tag] (re-find #"^(\w +) .*tag: (v[^\)\, ]+).*" line)]
39+ (*debug-fn* (str " Processing git-log line: " line))
3240 (if (and (not tag) (next lines))
3341 (recur (inc i) (next lines))
3442 [tag i]))))
3543
36-
3744(defn git-describe-first-parent [dir]
3845 " Return map with :git-tag (most recent tag on current branch (always following \" first-parent\" on merges))
3946and :git-tag-delta (number of commits -couting on first-parent paths only- from :git-tag to HEAD) "
@@ -44,37 +51,40 @@ and :git-tag-delta (number of commits -couting on first-parent paths only- from
4451 (sh/destroy p)
4552 {:git-tag tag, :git-tag-delta delta}))
4653
47- (defn infer-project-version [dir config-map]
54+ (defn infer-project-version [dir {:keys [tstamp-format git-cmd debug-fn]
55+ :or {tstamp-format " yyyyMMddHHmmss"
56+ git-cmd " git"
57+ debug-fn #(println %)}}]
4858 " Infer the current project version from tags on the source-control system"
4959
50- (let [tstamp-format ( :tstamp-format config-map)
51- commit-tstamp (-> (run-git-wait dir [" log" " -n" " 1" " --format=%ct" ])
52- trim-newline
53- (Long/parseLong )
54- (* 1000 )
55- Date.)
56- format-str (or tstamp-format " yyyyMMddHHmmss" )
57- commit-tstamp-str (.format (SimpleDateFormat. format-str)
58- commit-tstamp)
60+ (binding [*debug-fn* debug-fn, *git-cmd* git-cmd]
61+ ( let [ commit-tstamp (-> (run-git-wait dir [" log" " -n" " 1" " --format=%ct" ])
62+ trim-newline
63+ (Long/parseLong )
64+ (* 1000 )
65+ Date.)
66+ format-str (or tstamp-format " yyyyMMddHHmmss" )
67+ commit-tstamp-str (.format (SimpleDateFormat. format-str)
68+ commit-tstamp)
5969
60- [short-hash long-hash] (split
61- (run-git-wait dir [ " log" " -n" " 1" " --format=%h %H" ])
62- #" " )
70+ [short-hash long-hash] (split
71+ (run-git-wait dir [ " log" " -n" " 1" " --format=%h %H" ])
72+ #" " )
6373
64- versioning-properties {:build-tag " N/A"
65- :build-version " N/A"
66- :build-tag-delta " 0"
67- :build-tstamp commit-tstamp-str
68- :build-commit long-hash
69- :build-commit-abbrev short-hash }
70-
71- {:keys [git-tag git-tag-delta] } (git-describe-first-parent dir)]
74+ versioning-properties {:build-tag " N/A"
75+ :build-version " N/A"
76+ :build-tag-delta " 0"
77+ :build-tstamp commit-tstamp-str
78+ :build-commit long-hash
79+ :build-commit-abbrev short-hash }
80+
81+ {:keys [git-tag git-tag-delta] } (git-describe-first-parent dir)]
7282
73- (if (nil? git-tag)
74- versioning-properties
75-
76- (let [maven-artifact-version ((re-find #"v(.*)" git-tag) 1 )]
77- (merge versioning-properties
78- {:build-tag maven-artifact-version
79- :build-version (str (replace-first git-tag #"^v" " " ) " -" git-tag-delta " -" short-hash)
80- :build-tag-delta (str git-tag-delta) })))))
83+ (if (nil? git-tag)
84+ versioning-properties
85+
86+ (let [maven-artifact-version ((re-find #"v(.*)" git-tag) 1 )]
87+ (merge versioning-properties
88+ {:build-tag maven-artifact-version
89+ :build-version (str (replace-first git-tag #"^v" " " ) " -" git-tag-delta " -" short-hash)
90+ :build-tag-delta (str git-tag-delta) }) )))))
0 commit comments