install-git-hooks.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/bin/bash
  2. # Script to install pre-commit and pre-push hooks for webui
  3. # Pre-commit: formats code and runs checks
  4. # Pre-push: builds the project, stashes unstaged changes
  5. REPO_ROOT=$(git rev-parse --show-toplevel)
  6. PRE_COMMIT_HOOK="$REPO_ROOT/.git/hooks/pre-commit"
  7. PRE_PUSH_HOOK="$REPO_ROOT/.git/hooks/pre-push"
  8. echo "Installing pre-commit and pre-push hooks for webui..."
  9. # Create the pre-commit hook
  10. cat > "$PRE_COMMIT_HOOK" << 'EOF'
  11. #!/bin/bash
  12. # Check if there are any changes in the webui directory
  13. if git diff --cached --name-only | grep -q "^tools/server/webui/"; then
  14. echo "Formatting and checking webui code..."
  15. # Change to webui directory and run format
  16. cd tools/server/webui
  17. # Check if npm is available and package.json exists
  18. if [ ! -f "package.json" ]; then
  19. echo "Error: package.json not found in tools/server/webui"
  20. exit 1
  21. fi
  22. # Run the format command
  23. npm run format
  24. # Check if format command succeeded
  25. if [ $? -ne 0 ]; then
  26. echo "Error: npm run format failed"
  27. exit 1
  28. fi
  29. # Run the lint command
  30. npm run lint
  31. # Check if lint command succeeded
  32. if [ $? -ne 0 ]; then
  33. echo "Error: npm run lint failed"
  34. exit 1
  35. fi
  36. # Run the check command
  37. npm run check
  38. # Check if check command succeeded
  39. if [ $? -ne 0 ]; then
  40. echo "Error: npm run check failed"
  41. exit 1
  42. fi
  43. # Go back to repo root
  44. cd ../../..
  45. echo "✅ Webui code formatted and checked successfully"
  46. fi
  47. exit 0
  48. EOF
  49. # Create the pre-push hook
  50. cat > "$PRE_PUSH_HOOK" << 'EOF'
  51. #!/bin/bash
  52. # Check if there are any webui changes that need building
  53. WEBUI_CHANGES=$(git diff --name-only @{push}..HEAD | grep "^tools/server/webui/" || true)
  54. if [ -n "$WEBUI_CHANGES" ]; then
  55. echo "Webui changes detected, checking if build is up-to-date..."
  56. # Change to webui directory
  57. cd tools/server/webui
  58. # Check if npm is available and package.json exists
  59. if [ ! -f "package.json" ]; then
  60. echo "Error: package.json not found in tools/server/webui"
  61. exit 1
  62. fi
  63. # Check if build output exists and is newer than source files
  64. BUILD_FILE="../public/index.html.gz"
  65. NEEDS_BUILD=false
  66. if [ ! -f "$BUILD_FILE" ]; then
  67. echo "Build output not found, building..."
  68. NEEDS_BUILD=true
  69. else
  70. # Check if any source files are newer than the build output
  71. if find src -newer "$BUILD_FILE" -type f | head -1 | grep -q .; then
  72. echo "Source files are newer than build output, rebuilding..."
  73. NEEDS_BUILD=true
  74. fi
  75. fi
  76. if [ "$NEEDS_BUILD" = true ]; then
  77. echo "Building webui..."
  78. # Stash any unstaged changes to avoid conflicts during build
  79. echo "Checking for unstaged changes..."
  80. if ! git diff --quiet || ! git diff --cached --quiet --diff-filter=A; then
  81. echo "Stashing unstaged changes..."
  82. git stash push --include-untracked -m "Pre-push hook: stashed unstaged changes"
  83. STASH_CREATED=$?
  84. else
  85. echo "No unstaged changes to stash"
  86. STASH_CREATED=1
  87. fi
  88. # Run the build command
  89. npm run build
  90. # Check if build command succeeded
  91. if [ $? -ne 0 ]; then
  92. echo "Error: npm run build failed"
  93. if [ $STASH_CREATED -eq 0 ]; then
  94. echo "You can restore your unstaged changes with: git stash pop"
  95. fi
  96. exit 1
  97. fi
  98. # Go back to repo root
  99. cd ../../..
  100. # Check if build output was created/updated
  101. if [ -f "tools/server/public/index.html.gz" ]; then
  102. # Add the build output and commit it
  103. git add tools/server/public/index.html.gz
  104. if ! git diff --cached --quiet; then
  105. echo "Committing updated build output..."
  106. git commit -m "chore: update webui build output"
  107. echo "✅ Build output committed successfully"
  108. else
  109. echo "Build output unchanged"
  110. fi
  111. else
  112. echo "Error: Build output not found after build"
  113. if [ $STASH_CREATED -eq 0 ]; then
  114. echo "You can restore your unstaged changes with: git stash pop"
  115. fi
  116. exit 1
  117. fi
  118. if [ $STASH_CREATED -eq 0 ]; then
  119. echo "✅ Build completed. Your unstaged changes have been stashed."
  120. echo "They will be automatically restored after the push."
  121. # Create a marker file to indicate stash was created by pre-push hook
  122. touch .git/WEBUI_PUSH_STASH_MARKER
  123. fi
  124. else
  125. echo "✅ Build output is up-to-date"
  126. fi
  127. echo "✅ Webui ready for push"
  128. fi
  129. exit 0
  130. EOF
  131. # Create the post-push hook (for restoring stashed changes after push)
  132. cat > "$REPO_ROOT/.git/hooks/post-push" << 'EOF'
  133. #!/bin/bash
  134. # Check if we have a stash marker from the pre-push hook
  135. if [ -f .git/WEBUI_PUSH_STASH_MARKER ]; then
  136. echo "Restoring your unstaged changes after push..."
  137. git stash pop
  138. rm -f .git/WEBUI_PUSH_STASH_MARKER
  139. echo "✅ Your unstaged changes have been restored."
  140. fi
  141. exit 0
  142. EOF
  143. # Make all hooks executable
  144. chmod +x "$PRE_COMMIT_HOOK"
  145. chmod +x "$PRE_PUSH_HOOK"
  146. chmod +x "$REPO_ROOT/.git/hooks/post-push"
  147. if [ $? -eq 0 ]; then
  148. echo "✅ Git hooks installed successfully!"
  149. echo " Pre-commit: $PRE_COMMIT_HOOK"
  150. echo " Pre-push: $PRE_PUSH_HOOK"
  151. echo " Post-push: $REPO_ROOT/.git/hooks/post-push"
  152. echo ""
  153. echo "The hooks will automatically:"
  154. echo " • Format and check webui code before commits (pre-commit)"
  155. echo " • Build webui code before pushes (pre-push)"
  156. echo " • Stash unstaged changes during build process"
  157. echo " • Restore your unstaged changes after the push"
  158. echo ""
  159. echo "To test the hooks:"
  160. echo " • Make a change to a file in the webui directory and commit it (triggers format/check)"
  161. echo " • Push your commits to trigger the build process"
  162. else
  163. echo "❌ Failed to make hooks executable"
  164. exit 1
  165. fi