version --build-options: report commit, too, if possible

In particular when local tags are used (or tags that are pushed to some fork) to build Git, it is very hard to figure out from which particular revision a particular Git executable was built. It gets worse when those tags are deleted, or even updated. Let's just report an exact, unabbreviated commit name in our build options. We need to be careful, though, to report when the current commit cannot be determined, e.g. when building from a tarball without any associated Git repository. This could be the case also when extracting Git's source code into an unrelated Git worktree. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Dec 15, 2017 at 00:34 UTC ed32b788c06973ef28440ef81fdde9e7e55667f1
4 files changed +10 -1
Makefile
+3 -1
@@ -1902,7 +1902,9 @@ builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
1902 version.sp version.s version.o: GIT-VERSION-FILE GIT-USER-AGENT
1903 version.sp version.s version.o: EXTRA_CPPFLAGS = \
1904 '-DGIT_VERSION="$(GIT_VERSION)"' \
1905 - '-DGIT_USER_AGENT=$(GIT_USER_AGENT_CQ_SQ)'
1905 + '-DGIT_USER_AGENT=$(GIT_USER_AGENT_CQ_SQ)' \
1906 + '-DGIT_BUILT_FROM_COMMIT="$(shell GIT_CEILING_DIRECTORIES=\"$(CURDIR)/..\" \
1907 + git rev-parse -q --verify HEAD || :)"'
1908
1909 $(BUILT_INS): git$X
1910 $(QUIET_BUILT_IN)$(RM) $@ && \
help.c
+5
@@ -413,6 +413,11 @@ int cmd_version(int argc, const char **argv, const char *prefix)
413
414 if (build_options) {
415 printf("cpu: %s\n", GIT_HOST_CPU);
416 + if (git_built_from_commit_string[0])
417 + printf("built from commit: %s\n",
418 + git_built_from_commit_string);
419 + else
420 + printf("no commit associated with this build\n");
421 printf("sizeof-long: %d\n", (int)sizeof(long));
422 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
423 }
version.c
+1
@@ -3,6 +3,7 @@
3 #include "strbuf.h"
4
5 const char git_version_string[] = GIT_VERSION;
6 +const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT;
7
8 const char *git_user_agent(void)
9 {
version.h
+1
@@ -2,6 +2,7 @@
2 #define VERSION_H
3
4 extern const char git_version_string[];
5 +extern const char git_built_from_commit_string[];
6
7 const char *git_user_agent(void);
8 const char *git_user_agent_sanitized(void);