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?
Why are using this in a non-git project?
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 )
This seems like a good approach in your build and that seems like the right place to handle it
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.
as I said above, I think handling this in your build (not in git-rev-count) is the right place
but it might be useful to have some way to proactively change behavior rather than reacting to an exception
That's what https://github.com/IGJoshua/farolero is for 😉
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
Oh sure, I was just being cheeky.
https://ask.clojure.org/index.php/11027/should-tools-build-functions-respect-with-from-tools-alpha -- not sure whether this would be considered a bug or not?
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 🙂