Reputation: 3289
I'm using CMake to do an out-of-core/out-of-source build of my project, and want to include the result of a "git describe --tags --dirty" as a version number in the project (just for reference on bugs). However, any attempt to try and tell git where the code repo is results in an error like "fatal: Not a git repository (or any of the parent directories): .git".
Is there a way to specify where git should look for the repo, other than the current working directory?
Upvotes: 3
Views: 1272
Reputation: 1326746
You can specify --git-dir on the command line
Beware: this would not work fully before Git 2.21 (Feb. 2019):
"git --work-tree=$there --git-dir=$here describe --dirty
" did not work correctly as it did not pay attention to the location of the worktree specified by the user by mistake, which has been corrected.
See commit c801170, commit 2ed5c8e (03 Feb 2019) by Sebastian Staudt (koraktor
).
(Merged by Junio C Hamano -- gitster
-- in commit a1e1900, 07 Feb 2019)
describe
: setup working tree for--dirty
We don't use
NEED_WORK_TREE
when running thegit-describe
builtin, since you should be able to describe a commit even in a bare repository. However, the--dirty
flag does need a working tree. Since we don't callsetup_work_tree()
, it uses whatever directory we happen to be in. That's unlikely to match our index, meaning we'd say "dirty" even when the real working tree is clean.We can fix that by calling
setup_work_tree()
once we know that the user has asked for--dirty
.The
--broken
option also needs a working tree. But because its implementation callsgit-diff-index
we don‘t have to setup the working tree in thegit-describe
process.
Upvotes: 1
Reputation: 212404
You can specify --git-dir
on the command line, or set GIT_DIR
in the environment, or write the path into a file named .git
: echo gitdir: /path/to/.git > .git
or make a soft link to the repo: ln -s /path/to/.git
.
(The last two options are probably the least desirable, and merely mentioned here for completeness.)
Upvotes: 3
Reputation: 12383
Or you can use the WORKING_DIRECTORY
-argument to execute_process()
from cmake. This is what I do:
execute_process(COMMAND "${GIT_EXECUTABLE}" describe --tags --dirty
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
RESULT_VARIABLE res
OUTPUT_VARIABLE out
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
And then I have the version in ${out}
.
Upvotes: 4