Makefile: simplify building of templates

When we install Git we also install a set of default templates that both git-init(1) and git-clone(1) populate into our build directories. The way the pristine templates are laid out in our source directory is somewhat weird though: instead of reconstructing the actual directory hierarchy in "templates/", we represent directory separators with "--". The only reason I could come up with for why we have this is the "branches/" directory, which is supposed to be empty when installing it. And as Git famously doesn't store empty directories at all we have to work around this limitation. Now the thing is that the "branches/" directory is a leftover to how branches used to be stored in the dark ages. gitrepository-layout(5) lists this directory as "slightly deprecated", which I would claim is a strong understatement. I have never encountered anybody using it today and would be surprised if it even works as expected. So having the "--" hack in place for an item that is basically unused, unmaintained and deprecated doesn't only feel unreasonable, but installing that entry by default may also cause confusion for users that do not know what this is supposed to be in the first place. Remove this directory from our templates and, now that we do not require the workaround anymore, restructure the templates to form a proper hierarchy. This makes it way easier for build systems to install these templates into place. We should likely think about removing support for "branch/" altogether, but that is outside of the scope of this patch series. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 6, 2024 at 14:24 UTC ed060aa0a35f5d0e201af45c6a32c405db4c2654
19 files changed +37 -37
contrib/buildsystems/CMakeLists.txt
+12 -22
@@ -99,8 +99,8 @@ project(git
99 #TODO Enable NLS on windows natively
100
101 #macros for parsing the Makefile for sources and scripts
102 -macro(parse_makefile_for_sources list_var regex)
103 - file(STRINGS ${CMAKE_SOURCE_DIR}/Makefile ${list_var} REGEX "^${regex} \\+=(.*)")
102 +macro(parse_makefile_for_sources list_var makefile regex)
103 + file(STRINGS ${makefile} ${list_var} REGEX "^${regex} \\+=(.*)")
104 string(REPLACE "${regex} +=" "" ${list_var} ${${list_var}})
105 string(REPLACE "$(COMPAT_OBJS)" "" ${list_var} ${${list_var}}) #remove "$(COMPAT_OBJS)" This is only for libgit.
106 string(STRIP ${${list_var}} ${list_var}) #remove trailing/leading whitespaces
@@ -662,7 +662,7 @@ include_directories(${CMAKE_BINARY_DIR})
662
663 #build
664 #libgit
665 -parse_makefile_for_sources(libgit_SOURCES "LIB_OBJS")
665 +parse_makefile_for_sources(libgit_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "LIB_OBJS")
666
667 list(TRANSFORM libgit_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
668 list(TRANSFORM compat_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
@@ -680,13 +680,13 @@ list(APPEND libgit_SOURCES "${CMAKE_BINARY_DIR}/version-def.h")
680 add_library(libgit ${libgit_SOURCES} ${compat_SOURCES})
681
682 #libxdiff
683 -parse_makefile_for_sources(libxdiff_SOURCES "XDIFF_OBJS")
683 +parse_makefile_for_sources(libxdiff_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "XDIFF_OBJS")
684
685 list(TRANSFORM libxdiff_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
686 add_library(xdiff STATIC ${libxdiff_SOURCES})
687
688 #reftable
689 -parse_makefile_for_sources(reftable_SOURCES "REFTABLE_OBJS")
689 +parse_makefile_for_sources(reftable_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "REFTABLE_OBJS")
690
691 list(TRANSFORM reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
692 add_library(reftable STATIC ${reftable_SOURCES})
@@ -757,7 +757,7 @@ elseif(UNIX)
757 endif()
758
759 #git
760 -parse_makefile_for_sources(git_SOURCES "BUILTIN_OBJS")
760 +parse_makefile_for_sources(git_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "BUILTIN_OBJS")
761
762 list(TRANSFORM git_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
763 add_executable(git ${CMAKE_SOURCE_DIR}/git.c ${git_SOURCES})
@@ -912,24 +912,14 @@ add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/git-p4"
912 VERBATIM)
913 add_custom_target(python-gen ALL DEPENDS "${CMAKE_BINARY_DIR}/git-p4")
914
915 -#templates
916 -file(GLOB templates "${CMAKE_SOURCE_DIR}/templates/*")
917 -list(TRANSFORM templates REPLACE "${CMAKE_SOURCE_DIR}/templates/" "")
918 -list(REMOVE_ITEM templates ".gitignore")
919 -list(REMOVE_ITEM templates "Makefile")
920 -list(REMOVE_ITEM templates "blt")# Prevents an error when reconfiguring for in source builds
921 -
922 -list(REMOVE_ITEM templates "branches--")
923 -file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/templates/blt/branches) #create branches
924 -
915 +#${CMAKE_SOURCE_DIR}/Makefile templates
916 +parse_makefile_for_sources(templates ${CMAKE_SOURCE_DIR}/templates/Makefile "TEMPLATES")
917 +string(REPLACE " " ";" templates ${templates})
918 #templates have @.*@ replacement so use configure_file instead
919 foreach(tm ${templates})
927 - string(REPLACE "--" "/" blt_tm ${tm})
928 - string(REPLACE "this" "" blt_tm ${blt_tm})# for this--
929 - configure_file(${CMAKE_SOURCE_DIR}/templates/${tm} ${CMAKE_BINARY_DIR}/templates/blt/${blt_tm} @ONLY)
920 + configure_file(${CMAKE_SOURCE_DIR}/templates/${tm} ${CMAKE_BINARY_DIR}/templates/blt/${tm} @ONLY)
921 endforeach()
922
932 -
923 #translations
924 if(MSGFMT_EXE)
925 file(GLOB po_files "${CMAKE_SOURCE_DIR}/po/*.po")
@@ -1003,7 +993,7 @@ add_executable(test-fake-ssh ${CMAKE_SOURCE_DIR}/t/helper/test-fake-ssh.c)
993 target_link_libraries(test-fake-ssh common-main)
994
995 #unit-tests
1006 -parse_makefile_for_sources(unit-test_SOURCES "UNIT_TEST_OBJS")
996 +parse_makefile_for_sources(unit-test_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "UNIT_TEST_OBJS")
997 list(TRANSFORM unit-test_SOURCES REPLACE "\\$\\(UNIT_TEST_DIR\\)/" "${CMAKE_SOURCE_DIR}/t/unit-tests/")
998 add_library(unit-test-lib STATIC ${unit-test_SOURCES})
999
@@ -1069,7 +1059,7 @@ if(MSVC)
1059 endif()
1060
1061 #test-tool
1072 -parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
1062 +parse_makefile_for_sources(test-tool_SOURCES ${CMAKE_SOURCE_DIR}/Makefile "TEST_BUILTINS_OBJS")
1063 add_library(test-lib OBJECT ${CMAKE_SOURCE_DIR}/t/unit-tests/test-lib.c)
1064
1065 list(TRANSFORM test-tool_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/t/helper/")
templates/Makefile
+25 -14
@@ -29,24 +29,35 @@ all: boilerplates.made custom
29 # in a file direc--tory--file in the source. They will be
30 # just copied to the destination.
31
32 -bpsrc = $(filter-out %~,$(wildcard *--*))
33 -boilerplates.made : $(bpsrc)
34 - $(QUIET)umask 022 && ls *--* 2>/dev/null | \
35 - while read boilerplate; \
32 +TEMPLATES =
33 +TEMPLATES += description
34 +TEMPLATES += hooks/applypatch-msg.sample
35 +TEMPLATES += hooks/commit-msg.sample
36 +TEMPLATES += hooks/fsmonitor-watchman.sample
37 +TEMPLATES += hooks/post-update.sample
38 +TEMPLATES += hooks/pre-applypatch.sample
39 +TEMPLATES += hooks/pre-commit.sample
40 +TEMPLATES += hooks/pre-merge-commit.sample
41 +TEMPLATES += hooks/prepare-commit-msg.sample
42 +TEMPLATES += hooks/pre-push.sample
43 +TEMPLATES += hooks/pre-rebase.sample
44 +TEMPLATES += hooks/pre-receive.sample
45 +TEMPLATES += hooks/push-to-checkout.sample
46 +TEMPLATES += hooks/sendemail-validate.sample
47 +TEMPLATES += hooks/update.sample
48 +TEMPLATES += info/exclude
49 +
50 +boilerplates.made: $(TEMPLATES)
51 + $(QUIET)umask 022 && for template in $(TEMPLATES); \
52 do \
37 - case "$$boilerplate" in *~) continue ;; esac && \
38 - dst=`echo "$$boilerplate" | sed -e 's|^this|.|;s|--|/|g'` && \
39 - dir=`expr "$$dst" : '\(.*\)/'` && \
53 + dir=$$(dirname "$$template") && \
54 mkdir -p blt/$$dir && \
41 - case "$$boilerplate" in \
42 - *--) continue;; \
43 - esac && \
55 sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
56 -e 's|@SHELL_PATH@|$(SHELL_PATH_SQ)|' \
46 - -e 's|@PERL_PATH@|$(PERL_PATH_SQ)|g' $$boilerplate > \
47 - blt/$$dst && \
48 - if test -x "$$boilerplate"; then rx=rx; else rx=r; fi && \
49 - chmod a+$$rx "blt/$$dst" || exit; \
57 + -e 's|@PERL_PATH@|$(PERL_PATH_SQ)|g' $$template > \
58 + blt/$$template && \
59 + if test -x "$$template"; then rx=rx; else rx=r; fi && \
60 + chmod a+$$rx "blt/$$template" || exit; \
61 done && \
62 date >$@
63
templates/branches-- deleted
-1
@@ -1 +0,0 @@
1 -: this is just to ensure the directory exists.
templates/description renamed
templates/hooks/applypatch-msg.sample renamed
templates/hooks/commit-msg.sample renamed
templates/hooks/fsmonitor-watchman.sample renamed
templates/hooks/post-update.sample renamed
templates/hooks/pre-applypatch.sample renamed
templates/hooks/pre-commit.sample renamed
templates/hooks/pre-merge-commit.sample renamed
templates/hooks/pre-push.sample renamed
templates/hooks/pre-rebase.sample renamed
templates/hooks/pre-receive.sample renamed
templates/hooks/prepare-commit-msg.sample renamed
templates/hooks/push-to-checkout.sample renamed
templates/hooks/sendemail-validate.sample renamed
templates/hooks/update.sample renamed
templates/info/exclude renamed