install-git-hooks.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/bin/bash
  2. # Script to install pre-commit and post-commit hooks for webui
  3. # Pre-commit: formats, lints, checks, and builds code, 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 lint command
  37. npm run lint
  38. # Check if lint command succeeded
  39. if [ $? -ne 0 ]; then
  40. echo "Error: npm run lint 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 check command
  47. npm run check
  48. # Check if check command succeeded
  49. if [ $? -ne 0 ]; then
  50. echo "Error: npm run check 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. # Run the build command
  57. npm run build
  58. # Check if build command succeeded
  59. if [ $? -ne 0 ]; then
  60. echo "Error: npm run build failed"
  61. if [ $STASH_CREATED -eq 0 ]; then
  62. echo "You can restore your unstaged changes with: git stash pop"
  63. fi
  64. exit 1
  65. fi
  66. # Go back to repo root to add build output
  67. cd ../../..
  68. # Add the build output to staging area
  69. git add tools/server/public/index.html.gz
  70. if [ $STASH_CREATED -eq 0 ]; then
  71. echo "✅ Build completed. Your unstaged changes have been stashed."
  72. echo "They will be automatically restored after the commit."
  73. # Create a marker file to indicate stash was created by pre-commit hook
  74. touch .git/WEBUI_STASH_MARKER
  75. fi
  76. echo "Webui code formatted successfully"
  77. fi
  78. exit 0
  79. EOF
  80. # Create the post-commit hook
  81. cat > "$POST_COMMIT_HOOK" << 'EOF'
  82. #!/bin/bash
  83. # Check if we have a stash marker from the pre-commit hook
  84. if [ -f .git/WEBUI_STASH_MARKER ]; then
  85. echo "Restoring your unstaged changes..."
  86. git stash pop
  87. rm -f .git/WEBUI_STASH_MARKER
  88. echo "✅ Your unstaged changes have been restored."
  89. fi
  90. exit 0
  91. EOF
  92. # Make both hooks executable
  93. chmod +x "$PRE_COMMIT_HOOK"
  94. chmod +x "$POST_COMMIT_HOOK"
  95. if [ $? -eq 0 ]; then
  96. echo "✅ Pre-commit and post-commit hooks installed successfully!"
  97. echo " Pre-commit: $PRE_COMMIT_HOOK"
  98. echo " Post-commit: $POST_COMMIT_HOOK"
  99. echo ""
  100. echo "The hooks will automatically:"
  101. echo " • Format, lint, check, and build webui code before commits"
  102. echo " • Stash unstaged changes during the process"
  103. echo " • Restore your unstaged changes after the commit"
  104. echo ""
  105. echo "To test the hooks, make a change to a file in the webui directory and commit it."
  106. else
  107. echo "❌ Failed to make hooks executable"
  108. exit 1
  109. fi