manage_static.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. BUILD_COMMIT_MESSAGE="Static build"
  5. BUILT_PATHS=(
  6. searx/static/themes/oscar/css
  7. searx/static/themes/oscar/js
  8. searx/static/themes/oscar/src/generated/pygments-logicodev.less
  9. searx/static/themes/oscar/src/generated/pygments-pointhi.less
  10. searx/static/themes/simple/css
  11. searx/static/themes/simple/js
  12. searx/static/themes/simple/src/generated/pygments.less
  13. )
  14. CURRENT_BRANCH="$(git branch --show-current)"
  15. STAGED_FILES=$(git diff --name-only --cached)
  16. git_log_current_branch() {
  17. git log "heads/${CURRENT_BRANCH}" --not --exclude="${CURRENT_BRANCH}" --branches --remotes --pretty=format:"%h"
  18. }
  19. is.build.commit() {
  20. COMMIT_SHA=$1
  21. # check commit message
  22. COMMIT_MESSAGE=$(git show -s --format=%s ${COMMIT_SHA})
  23. if [ "${COMMIT_MESSAGE}" != "${BUILD_COMMIT_MESSAGE}" ]; then
  24. echo "Commit message of ${COMMIT_SHA} is '${COMMIT_MESSAGE}'"
  25. return 1
  26. fi
  27. # check all files of the commit belongs to $BUILT_PATHS
  28. COMMIT_FILES=$(git diff-tree --no-commit-id --name-only -r "${COMMIT_SHA}")
  29. for i in ${BUILT_PATHS[*]}; do
  30. # remove files of ${BUILT_PATHS}
  31. COMMIT_FILES=$(echo "${COMMIT_FILES}" | grep -v "^${i}")
  32. done
  33. if [ -n "${COMMIT_FILES}" ]; then
  34. echo "Commit $1 contains files that were not build: ${COMMIT_FILES}"
  35. return 2
  36. fi
  37. return 0
  38. }
  39. static.build.commit.drop() {
  40. LAST_COMMIT_ID=$(git_log_current_branch | head -1)
  41. if [ -z "${LAST_COMMIT_ID}" ]; then
  42. echo "Empty branch"
  43. return 1
  44. fi
  45. is.build.commit "${LAST_COMMIT_ID}"
  46. if [ $? -ne 0 ]; then
  47. return $?
  48. fi
  49. echo "Drop last commit ${LAST_COMMIT_ID}"
  50. git reset --hard HEAD~1
  51. }
  52. static.build.commit() {
  53. # check for not commited files
  54. NOT_COMMITED_FILES="$(git diff --name-only)"
  55. if [ -n "${NOT_COMMITED_FILES}" ]; then
  56. echo "Some files are not commited:"
  57. echo "${NOT_COMMITED_FILES}"
  58. return 1
  59. fi
  60. # check for staged files
  61. if [ -n "${STAGED_FILES}" ]; then
  62. echo "Some files are staged:"
  63. echo "${STAGED_FILES}"
  64. return 1
  65. fi
  66. # drop existing commit
  67. static.commit.drop
  68. if [ $? -ne 0 ]; then
  69. return $?
  70. fi
  71. (
  72. set -e
  73. # build the themes
  74. make themes.all
  75. # add build files
  76. for built_path in ${BUILT_PATHS[@]}; do
  77. git add -v "${built_path}"
  78. done
  79. # check for modified files that are not staged
  80. if [ -n "$(git diff --name-only)" ]; then
  81. echo "make themes.all has created files that are not in BUILT_PATHS"
  82. return 2
  83. fi
  84. #
  85. git commit -m "Static build"
  86. )
  87. }
  88. static.git.restore.staged() {
  89. for i in ${BUILT_PATHS[*]}; do
  90. STAGED_FILES_FOR_I=$(echo "${STAGED_FILES}" | grep "^${i}")
  91. if [ -n "${STAGED_FILES_FOR_I}" ]; then
  92. git restore --staged ${STAGED_FILES_FOR_I}
  93. fi
  94. done
  95. }
  96. static.git.restore() {
  97. static.git.restore.staged
  98. NOT_COMMITED_FILES="$(git diff --name-only)"
  99. for i in ${BUILT_PATHS[*]}; do
  100. NOT_COMMITED_FILES_FOR_I=$(echo "${NOT_COMMITED_FILES}" | grep "^${i}")
  101. if [ -n "${NOT_COMMITED_FILES_FOR_I}" ]; then
  102. git restore ${NOT_COMMITED_FILES_FOR_I}
  103. fi
  104. done
  105. }
  106. main() {
  107. case $1 in
  108. static.build.commit.drop)
  109. # drop last commit if it was made by the "commit" command
  110. static.build.commit.drop
  111. ;;
  112. static.build.commit)
  113. # call the "static.build.commit.drop" command,
  114. # then "make themes.all"
  115. # then commit the built files ($BUILT_PATHS).
  116. static.build.commit
  117. ;;
  118. static.git.restore.staged)
  119. # after "git add ."
  120. # remove the built files
  121. # so only the source are commited
  122. static.git.restore.staged
  123. ;;
  124. static.git.restore)
  125. # "git restore" of the built files.
  126. static.git.restore
  127. ;;
  128. esac
  129. }
  130. main "$@"