11#! /bin/bash
22
3- # Script to install pre-commit and post-commit hooks for webui
4- # Pre-commit: formats code and builds, stashes unstaged changes
5- # Post-commit: automatically unstashes changes
3+ # Script to install pre-commit and pre-push hooks for webui
4+ # Pre-commit: formats code and runs checks
5+ # Pre-push: builds the project, stashes unstaged changes
66
77REPO_ROOT=$( git rev-parse --show-toplevel)
88PRE_COMMIT_HOOK=" $REPO_ROOT /.git/hooks/pre-commit"
9- POST_COMMIT_HOOK =" $REPO_ROOT /.git/hooks/post-commit "
9+ PRE_PUSH_HOOK =" $REPO_ROOT /.git/hooks/pre-push "
1010
11- echo " Installing pre-commit and post-commit hooks for webui..."
11+ echo " Installing pre-commit and pre-push hooks for webui..."
1212
1313# Create the pre-commit hook
1414cat > " $PRE_COMMIT_HOOK " << 'EOF '
1515#!/bin/bash
1616
1717# Check if there are any changes in the webui directory
1818if git diff --cached --name-only | grep -q "^tools/server/webui/"; then
19- echo "Formatting webui code..."
19+ echo "Formatting and checking webui code..."
2020
2121 # Change to webui directory and run format
2222 cd tools/server/webui
@@ -27,20 +27,12 @@ if git diff --cached --name-only | grep -q "^tools/server/webui/"; then
2727 exit 1
2828 fi
2929
30- # Stash any unstaged changes to avoid conflicts during format/build
31- echo "Stashing unstaged changes..."
32- git stash push --keep-index --include-untracked -m "Pre-commit hook: stashed unstaged changes"
33- STASH_CREATED=$?
34-
3530 # Run the format command
3631 npm run format
3732
3833 # Check if format command succeeded
3934 if [ $? -ne 0 ]; then
4035 echo "Error: npm run format failed"
41- if [ $STASH_CREATED -eq 0 ]; then
42- echo "You can restore your unstaged changes with: git stash pop"
43- fi
4436 exit 1
4537 fi
4638
@@ -50,12 +42,46 @@ if git diff --cached --name-only | grep -q "^tools/server/webui/"; then
5042 # Check if check command succeeded
5143 if [ $? -ne 0 ]; then
5244 echo "Error: npm run check failed"
53- if [ $STASH_CREATED -eq 0 ]; then
54- echo "You can restore your unstaged changes with: git stash pop"
55- fi
5645 exit 1
5746 fi
5847
48+ # Go back to repo root
49+ cd ../../..
50+
51+ echo "✅ Webui code formatted and checked successfully"
52+ fi
53+
54+ exit 0
55+ EOF
56+
57+ # Create the pre-push hook
58+ cat > " $PRE_PUSH_HOOK " << 'EOF '
59+ #!/bin/bash
60+
61+ # Check if there are any changes in the webui directory that need building
62+ if git diff --name-only HEAD~1..HEAD | grep -q "^tools/server/webui/" || git diff --name-only --cached | grep -q "^tools/server/webui/"; then
63+ echo "Building webui for push..."
64+
65+ # Change to webui directory
66+ cd tools/server/webui
67+
68+ # Check if npm is available and package.json exists
69+ if [ ! -f "package.json" ]; then
70+ echo "Error: package.json not found in tools/server/webui"
71+ exit 1
72+ fi
73+
74+ # Stash any unstaged changes to avoid conflicts during build
75+ echo "Checking for unstaged changes..."
76+ if ! git diff --quiet || ! git diff --cached --quiet --diff-filter=A; then
77+ echo "Stashing unstaged changes..."
78+ git stash push --include-untracked -m "Pre-push hook: stashed unstaged changes"
79+ STASH_CREATED=$?
80+ else
81+ echo "No unstaged changes to stash"
82+ STASH_CREATED=1
83+ fi
84+
5985 # Run the build command
6086 npm run build
6187
@@ -71,52 +97,62 @@ if git diff --cached --name-only | grep -q "^tools/server/webui/"; then
7197 # Go back to repo root to add build output
7298 cd ../../..
7399
74- # Add the build output to staging area
75- git add tools/server/public/index.html.gz
100+ # Add the build output to staging area and commit if there are changes
101+ if [ -f "tools/server/public/index.html.gz" ]; then
102+ git add tools/server/public/index.html.gz
103+ if ! git diff --cached --quiet; then
104+ git commit -m "chore: update webui build output"
105+ fi
106+ fi
76107
77108 if [ $STASH_CREATED -eq 0 ]; then
78109 echo "✅ Build completed. Your unstaged changes have been stashed."
79- echo "They will be automatically restored after the commit ."
80- # Create a marker file to indicate stash was created by pre-commit hook
81- touch .git/WEBUI_STASH_MARKER
110+ echo "They will be automatically restored after the push ."
111+ # Create a marker file to indicate stash was created by pre-push hook
112+ touch .git/WEBUI_PUSH_STASH_MARKER
82113 fi
83114
84- echo "Webui code formatted successfully"
115+ echo "✅ Webui build completed successfully"
85116fi
86117
87118exit 0
88119EOF
89120
90- # Create the post-commit hook
91- cat > " $POST_COMMIT_HOOK " << 'EOF '
121+ # Create the post-push hook (for restoring stashed changes after push)
122+ cat > " $REPO_ROOT /.git/hooks/post-push " << 'EOF '
92123#!/bin/bash
93124
94- # Check if we have a stash marker from the pre-commit hook
95- if [ -f .git/WEBUI_STASH_MARKER ]; then
96- echo "Restoring your unstaged changes..."
125+ # Check if we have a stash marker from the pre-push hook
126+ if [ -f .git/WEBUI_PUSH_STASH_MARKER ]; then
127+ echo "Restoring your unstaged changes after push ..."
97128 git stash pop
98- rm -f .git/WEBUI_STASH_MARKER
129+ rm -f .git/WEBUI_PUSH_STASH_MARKER
99130 echo "✅ Your unstaged changes have been restored."
100131fi
101132
102133exit 0
103134EOF
104135
105- # Make both hooks executable
136+ # Make all hooks executable
106137chmod +x " $PRE_COMMIT_HOOK "
107- chmod +x " $POST_COMMIT_HOOK "
138+ chmod +x " $PRE_PUSH_HOOK "
139+ chmod +x " $REPO_ROOT /.git/hooks/post-push"
108140
109141if [ $? -eq 0 ]; then
110- echo " ✅ Pre-commit and post-commit hooks installed successfully!"
111- echo " Pre-commit: $PRE_COMMIT_HOOK "
112- echo " Post-commit: $POST_COMMIT_HOOK "
142+ echo " ✅ Git hooks installed successfully!"
143+ echo " Pre-commit: $PRE_COMMIT_HOOK "
144+ echo " Pre-push: $PRE_PUSH_HOOK "
145+ echo " Post-push: $REPO_ROOT /.git/hooks/post-push"
113146 echo " "
114147 echo " The hooks will automatically:"
115- echo " • Format and build webui code before commits"
116- echo " • Stash unstaged changes during the process"
117- echo " • Restore your unstaged changes after the commit"
148+ echo " • Format and check webui code before commits (pre-commit)"
149+ echo " • Build webui code before pushes (pre-push)"
150+ echo " • Stash unstaged changes during build process"
151+ echo " • Restore your unstaged changes after the push"
118152 echo " "
119- echo " To test the hooks, make a change to a file in the webui directory and commit it."
153+ echo " To test the hooks:"
154+ echo " • Make a change to a file in the webui directory and commit it (triggers format/check)"
155+ echo " • Push your commits to trigger the build process"
120156else
121157 echo " ❌ Failed to make hooks executable"
122158 exit 1
0 commit comments