tools-build 2021-09-08

FYI, git-rev-count throws NPE if not in a git repo. I use the following to make build.clj work in a docker image build (where there is no git repo - since we have a multi project repo and we are not top level).

(defn git-count-revs-safe
  "Get version without git."
  (^String [^String fallback]
   (try
     (b/git-count-revs nil)
     (catch NullPointerException _
       fallback))))
(def version (format "0.1.%s" (git-count-revs-safe "NOGIT")))
Could we have something like that in git-count-revs ?

We have the same issue. I was thinking of taking the value from an environment variable

you could pass the fallback as (System/getenv) and have both ?! wdyt?

Alex Miller (Clojure team) 2021-09-08T12:32:59.101800Z

Why are using this in a non-git project?

Alex Miller (Clojure team) 2021-09-08T12:33:41.102400Z

Oh sorry, didn’t read all above

I build a docker image and I use a task in build.clj . The directory I copy is not the git root and no .git is copied over (multi project git )

Alex Miller (Clojure team) 2021-09-08T12:36:54.104200Z

This seems like a good approach in your build and that seems like the right place to handle it

Alex Miller (Clojure team) 2021-09-08T12:37:21.105200Z

But it would maybe be useful to have a predicate for whether you’re in a git repo?

Personally I'd prefer a consistent mechanism for building the version string. If I wanted to use a git revision count, I'd want things to blow up if I wasn't in a git repo. And if I wanted to use something else, like an environment variable, I'm not sure I'd want a fall back either

Throwing in my two bits, I think this is the sort of thing which would be good to include in build.clj by @seancorfield or a similar library, or manually in your project. It doesn't feel like part of what tools.build is trying to provide.

Like @tvaughan I would want my version to blow up if I accidentally ran the build in a non-git context. But anyone can wrap their call to git-rev-count in try/catch without needing a library function for it.

Alex Miller (Clojure team) 2021-09-08T16:12:29.106100Z

as I said above, I think handling this in your build (not in git-rev-count) is the right place

Alex Miller (Clojure team) 2021-09-08T16:12:59.106300Z

but it might be useful to have some way to proactively change behavior rather than reacting to an exception

Alex Miller (Clojure team) 2021-09-08T21:17:24.106700Z

I think you're missing the point, which is to avoid having an exception in the first place. I think if technology is sufficient for that

1

Oh sure, I was just being cheeky.

Alex Miller (Clojure team) 2021-09-08T22:22:13.108300Z

why are you doing it?

I explained in the ask post -- probably in more detail than you care to know since, of course, I'm doing something a bit weird 🙂