Bug #14011
openbuild/run-library.sh version_from_git function dumps environment when not in a git repo
Description
there is a bug in build/run-library.sh version_from_git function.
if we are running not in a git repo, the `format_last_commit_here` function returns nothing, and as a result the line:
declare $(format_last_commit_here "git_ts=%ct git_hash=%h")
Ends up being equivalent to `declare` which dumps the environment.
I believe the fix should be to quote it:
declare "$(format_last_commit_here 'git_ts=%ct git_hash=%h')"
Updated by Joshua Randall over 6 years ago
The above fix doesn't work as the quoted version is not interpreted correctly by declare when it is actually run in a git repo.
A fix for this could be to ensure `format_last_commit_here` always returns variable declarations or else exits when git fails - something like:
format_last_commit_here() { local format="$1"; shift TZ=UTC git log -n1 --first-parent "--format=format:$format" . || (echo "git log command failed - is $(pwd) a git repo?"; exit 1) }
Or I suppose the version_from_git function could do a sanity check first to make sure it is, in fact, in a git repo.
Updated by Joshua Randall over 6 years ago
That example doesn't work either, because subshells.
Now trying this:
declare x=1 $(format_last_commit_here "git_ts=%ct git_hash=%h")