Skip to content

Commit ca567a8

Browse files
committed
Set up git pre-push hook; Move project-specific functions under functions/projects/ directory
1 parent 8c42e33 commit ca567a8

5 files changed

Lines changed: 378 additions & 290 deletions

File tree

.githooks/.setup-hooks.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
HOOK_SOURCE=".githooks/pre-push"
4+
HOOK_DEST=".git/hooks/pre-push"
5+
6+
echo "Setting up Git hooks..."
7+
8+
# Check if source exists
9+
if [ ! -f "$HOOK_SOURCE" ]; then
10+
echo "Hook source not found: $HOOK_SOURCE"
11+
exit 1
12+
fi
13+
14+
# Copy or symlink the pre-push hook
15+
cp "$HOOK_SOURCE" "$HOOK_DEST"
16+
chmod +x "$HOOK_DEST"
17+
18+
echo "✅ Git hook installed: $HOOK_DEST"

.githooks/pre-push

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
# When pushing to GitHub. The script checks and blocks files inside BLOCK_PATH from being pushed to the GitHub repository.
4+
# This helps avoid project-specific files being shared on a public repository.
5+
# This does not affect pushes to internal GitLab remotes.
6+
7+
remote_name="$1"
8+
remote_url="$2"
9+
10+
echo "Pre-push hook triggered for remote $remote_url"
11+
12+
BLOCK_PATH="esdlvalidator/validation/functions/projects/"
13+
14+
# Apply blocking when pushing to GitHub
15+
if [[ "$remote_url" == *github.com* ]]; then
16+
echo "Pushing to GitHub. Checking files under $BLOCK_PATH ..."
17+
18+
while read local_ref local_sha remote_ref remote_sha; do
19+
# Ignore deleted branches
20+
if [[ "$local_sha" =~ ^0+$ ]]; then
21+
continue
22+
fi
23+
24+
# Check commits from remote_sha (old) to local_sha (new)
25+
range="$remote_sha..$local_sha"
26+
if [[ "$remote_sha" =~ ^0+$ ]]; then
27+
# New branch push
28+
range="$local_sha"
29+
fi
30+
31+
# List all files touched in this range
32+
pushed_files=$(git diff --name-only "$range")
33+
34+
blocked_files=$(echo "$pushed_files" | grep "^$BLOCK_PATH")
35+
36+
if [[ -n "$blocked_files" ]]; then
37+
echo "❌ Push to GitHub rejected! The following files are in the blocked '$BLOCK_PATH' directory:"
38+
echo "$blocked_files"
39+
echo "➡️ These files must not be pushed to GitHub. Push to GitLab is allowed."
40+
exit 1
41+
fi
42+
done
43+
44+
fi
45+
46+
# Allow push
47+
exit 0

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,23 @@ env\Scripts\activate.bat (Windows CMD)
160160
pip3 install -r requirements.txt
161161
```
162162

163+
### Set up Git hooks
164+
165+
At the project root, run the command in a bash terminal to set up git hooks.
166+
167+
```bash
168+
./.githooks/.setup-hooks.sh
169+
```
170+
171+
You should see the logs below after the successful setup.
172+
173+
```
174+
Setting up Git hooks...
175+
✅ Git hook installed: .git/hooks/pre-push
176+
```
177+
178+
The reason for setting up this git hook (`pre-push`) is to block project-specific functions defined under `esdlvalidator/validation/functions/projects/` from being pushed to an open GitHub repository. This does not affect when pushing to the internal gitlab repository.
179+
163180
### Testing
164181

165182
Use the 'Test' tab is vscode or execute one of the following commands from the root folder
@@ -185,6 +202,12 @@ An example how to start the service using waitress.
185202
waitress-serve --listen="*:8080" --call "esdlvalidator.api.manage:create_app"
186203
```
187204

205+
206+
.githooks/.setup-hooks.sh
207+
Setting up Git hooks...
208+
✅ Git hook installed: .git/hooks/pre-push
209+
210+
<!-- TODO: say sth. about pyesdl? -->
188211
<!-- ### Deprecated: Update static ESDL metamodel code
189212
190213
To update the ESDL code to work with the latest version of the ESDL ecore model, update esdl.ecore to the latest version

0 commit comments

Comments
 (0)