manage_static.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. is.build.commit() {
  14. local commit_sha="$1"
  15. local commit_message
  16. local commit_files
  17. # check commit message
  18. commit_message=$(git show -s --format=%s "${commit_sha}")
  19. if [ "${commit_message}" != "${BUILD_COMMIT_MESSAGE}" ]; then
  20. echo "Commit message of ${commit_sha} is '${commit_message}'"
  21. return 1
  22. fi
  23. # check all files of the commit belongs to $BUILT_PATHS
  24. commit_files=$(git diff-tree --no-commit-id --name-only -r "${commit_sha}")
  25. for i in ${BUILT_PATHS[*]}; do
  26. # remove files of ${BUILT_PATHS}
  27. commit_files=$(echo "${commit_files}" | grep -v "^${i}")
  28. done
  29. if [ -n "${commit_files}" ]; then
  30. echo "Commit $1 contains files that were not build: ${commit_files}"
  31. return 2
  32. fi
  33. return 0
  34. }
  35. static.build.commit.drop() {
  36. local last_commit_id
  37. local branch
  38. # get only last (option -n1) local commit not in remotes
  39. branch="$(git branch --show-current)"
  40. last_commit_id="$(git log -n1 "${branch}" --pretty=format:'%h'\
  41. --not --exclude="${branch}" --branches --remotes)"
  42. if [ -z "${last_commit_id}" ]; then
  43. echo "Empty branch"
  44. return 1
  45. fi
  46. if ! is.build.commit "${last_commit_id}"; 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. local staged_files
  54. # check for not commited files
  55. if [ -n "$(git diff --name-only)" ]; then
  56. echo "Some files are not commited:"
  57. echo "${NOT_COMMITED_FILES}"
  58. return 1
  59. fi
  60. staged_files=$(git diff --name-only --cached)
  61. # check for staged files
  62. if [ -n "${staged_files}" ]; then
  63. echo "Some files are staged:"
  64. echo "${staged_files}"
  65. return 1
  66. fi
  67. # drop existing commit
  68. if static.commit.drop; 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. git commit -m "${BUILD_COMMIT_MESSAGE}"
  85. )
  86. }
  87. main() {
  88. case $1 in
  89. static.build.commit.drop)
  90. # drop last commit if it was made by the "commit" command
  91. static.build.commit.drop
  92. ;;
  93. static.build.commit)
  94. # call the "static.build.commit.drop" command,
  95. # then "make themes.all"
  96. # then commit the built files ($BUILT_PATHS).
  97. static.build.commit
  98. ;;
  99. static.git.restore.staged)
  100. # after "git add ."
  101. # remove the built files
  102. # so only the source are commited
  103. git restore --staged "${BUILT_PATHS[@]}"
  104. ;;
  105. static.git.restore)
  106. # "git restore" of the built files.
  107. git restore --worktree --staged "${BUILT_PATHS[@]}"
  108. ;;
  109. esac
  110. }
  111. main "$@"