manage_static.sh 3.5 KB

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