Getting GIT build hash outside the current directory

∥☆過路亽.° 提交于 2020-03-19 06:04:15

问题


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?


回答1:


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}.




回答2:


You can use git with --git-dir-argument.




回答3:


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.)




回答4:


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 the git-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 call setup_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 calls git-diff-index we don‘t have to setup the working tree in the git-describe process.



来源:https://stackoverflow.com/questions/15146731/getting-git-build-hash-outside-the-current-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!