Skip to content

Commit 8fa54ad

Browse files
bmhowe23justinlietz
authored andcommitted
Auto-install a pre-push hook if it exists in the repo (NVIDIA#33)
Signed-off-by: Ben Howe <[email protected]>
1 parent bf76fbd commit 8fa54ad

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

CMakeLists.txt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,68 @@ if (CUDAQX_INCLUDE_TESTS)
9191
endif()
9292
endif()
9393

94+
# Hooks setup. If the repo contains a custom pre-push hook, attempt to install
95+
# it. If the user has a different one installed, then warn them but do not fail
96+
# configuration.
97+
# ==============================================================================
98+
99+
# Define the directory where your hooks are stored
100+
set(SRC_HOOK_PRE_PUSH "${CMAKE_SOURCE_DIR}/.githooks/pre-push")
101+
102+
if(EXISTS "${SRC_HOOK_PRE_PUSH}")
103+
# Get the Git hooks directory from the Git configuration
104+
execute_process(
105+
COMMAND git config --get core.hooksPath
106+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
107+
OUTPUT_VARIABLE GIT_HOOKS_DIR
108+
OUTPUT_STRIP_TRAILING_WHITESPACE
109+
)
110+
111+
# Determine the target hooks directory
112+
if(GIT_HOOKS_DIR)
113+
set(TARGET_HOOKS_DIR "${GIT_HOOKS_DIR}")
114+
else()
115+
set(TARGET_HOOKS_DIR "${CMAKE_SOURCE_DIR}/.git/hooks")
116+
endif()
117+
set(DST_HOOK_PRE_PUSH "${TARGET_HOOKS_DIR}/pre-push")
118+
119+
if(EXISTS "${DST_HOOK_PRE_PUSH}")
120+
# Compare the contents of the src and dst hook.
121+
execute_process(
122+
COMMAND git hash-object "${DST_HOOK_PRE_PUSH}"
123+
OUTPUT_VARIABLE SHA_DST
124+
OUTPUT_STRIP_TRAILING_WHITESPACE
125+
)
126+
execute_process(
127+
COMMAND git hash-object "${SRC_HOOK_PRE_PUSH}"
128+
OUTPUT_VARIABLE SHA_SRC
129+
OUTPUT_STRIP_TRAILING_WHITESPACE
130+
)
131+
if(NOT SHA_SRC STREQUAL SHA_DST)
132+
message(WARNING
133+
"You already have a ${DST_HOOK_PRE_PUSH} script installed. "
134+
"This configuration script will not overwrite it despite the fact that "
135+
"it is strongly recommended to use ${SRC_HOOK_PRE_PUSH} in your environment."
136+
"\nProceed with caution!")
137+
endif()
138+
else()
139+
if(EXISTS "${TARGET_HOOKS_DIR}")
140+
file(COPY "${SRC_HOOK_PRE_PUSH}"
141+
DESTINATION "${TARGET_HOOKS_DIR}"
142+
FILE_PERMISSIONS
143+
OWNER_READ OWNER_WRITE OWNER_EXECUTE
144+
GROUP_READ GROUP_EXECUTE
145+
WORLD_READ WORLD_EXECUTE)
146+
message(STATUS "Git pre-push hook installed to ${TARGET_HOOKS_DIR}")
147+
else()
148+
message(WARNING
149+
"The Git hooks directory does not exist: ${TARGET_HOOKS_DIR}\n"
150+
"Are you sure this is a Git repository? Hook cannot be installed."
151+
)
152+
endif()
153+
endif()
154+
endif()
155+
94156
# Directory setup
95157
# ==============================================================================
96158

0 commit comments

Comments
 (0)