install-git-hooks.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/bash
  2. # Script to install pre-commit and post-commit hooks for webui
  3. # Pre-commit: formats code and builds, stashes unstaged changes
  4. # Post-commit: automatically unstashes changes
  5. REPO_ROOT=$(git rev-parse --show-toplevel)
  6. PRE_COMMIT_HOOK="$REPO_ROOT/.git/hooks/pre-commit"
  7. POST_COMMIT_HOOK="$REPO_ROOT/.git/hooks/post-commit"
  8. echo "Installing pre-commit and post-commit 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 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. # Stash any unstaged changes to avoid conflicts during format/build
  23. echo "Stashing unstaged changes..."
  24. git stash push --keep-index --include-untracked -m "Pre-commit hook: stashed unstaged changes"
  25. STASH_CREATED=$?
  26. # Run the format command
  27. npm run format
  28. # Check if format command succeeded
  29. if [ $? -ne 0 ]; then
  30. echo "Error: npm run format failed"
  31. if [ $STASH_CREATED -eq 0 ]; then
  32. echo "You can restore your unstaged changes with: git stash pop"
  33. fi
  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. if [ $STASH_CREATED -eq 0 ]; then
  42. echo "You can restore your unstaged changes with: git stash pop"
  43. fi
  44. exit 1
  45. fi
  46. # Run the build command
  47. npm run build
  48. # Check if build command succeeded
  49. if [ $? -ne 0 ]; then
  50. echo "Error: npm run build failed"
  51. if [ $STASH_CREATED -eq 0 ]; then
  52. echo "You can restore your unstaged changes with: git stash pop"
  53. fi
  54. exit 1
  55. fi
  56. # Go back to repo root to add build output
  57. cd ../../..
  58. # Add the build output to staging area
  59. git add tools/server/public/index.html.gz
  60. if [ $STASH_CREATED -eq 0 ]; then
  61. echo "✅ Build completed. Your unstaged changes have been stashed."
  62. echo "They will be automatically restored after the commit."
  63. # Create a marker file to indicate stash was created by pre-commit hook
  64. touch .git/WEBUI_STASH_MARKER
  65. fi
  66. echo "Webui code formatted successfully"
  67. fi
  68. exit 0
  69. EOF
  70. # Create the post-commit hook
  71. cat > "$POST_COMMIT_HOOK" << 'EOF'
  72. #!/bin/bash
  73. # Check if we have a stash marker from the pre-commit hook
  74. if [ -f .git/WEBUI_STASH_MARKER ]; then
  75. echo "Restoring your unstaged changes..."
  76. git stash pop
  77. rm -f .git/WEBUI_STASH_MARKER
  78. echo "✅ Your unstaged changes have been restored."
  79. fi
  80. exit 0
  81. EOF
  82. # Make both hooks executable
  83. chmod +x "$PRE_COMMIT_HOOK"
  84. chmod +x "$POST_COMMIT_HOOK"
  85. if [ $? -eq 0 ]; then
  86. echo "✅ Pre-commit and post-commit hooks installed successfully!"
  87. echo " Pre-commit: $PRE_COMMIT_HOOK"
  88. echo " Post-commit: $POST_COMMIT_HOOK"
  89. echo ""
  90. echo "The hooks will automatically:"
  91. echo " • Format and build webui code before commits"
  92. echo " • Stash unstaged changes during the process"
  93. echo " • Restore your unstaged changes after the commit"
  94. echo ""
  95. echo "To test the hooks, make a change to a file in the webui directory and commit it."
  96. else
  97. echo "❌ Failed to make hooks executable"
  98. exit 1
  99. fi