lib_sxng_static.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. STATIC_BUILD_COMMIT="[build] /static"
  4. STATIC_BUILT_PATHS=(
  5. 'searx/static/themes/simple'
  6. )
  7. static.help(){
  8. cat <<EOF
  9. static.build.: ${STATIC_BUILD_COMMIT}
  10. commit : build & commit /static folder
  11. drop : drop last commit if it was previously done by static.build.commit
  12. restore : git restore of the /static folder (after themes.all)
  13. EOF
  14. }
  15. is.static.build.commit() {
  16. local commit_sha="$1"
  17. local commit_message
  18. local commit_files
  19. # check commit message
  20. commit_message=$(git show -s --format=%s "${commit_sha}")
  21. if [ "${commit_message}" != "${STATIC_BUILD_COMMIT}" ]; then
  22. err_msg "expecting commit message: '${STATIC_BUILD_COMMIT}'"
  23. err_msg "commit message of ${commit_sha} is: '${commit_message}'"
  24. return 1
  25. fi
  26. # check all files of the commit belongs to $STATIC_BUILT_PATHS
  27. commit_files=$(git diff-tree --no-commit-id --name-only -r "${commit_sha}")
  28. for i in "${STATIC_BUILT_PATHS[@]}"; do
  29. # remove files of ${STATIC_BUILT_PATHS}
  30. commit_files=$(echo "${commit_files}" | grep -v "^${i}")
  31. done
  32. if [ -n "${commit_files}" ]; then
  33. err_msg "commit ${commit_sha} contains files not a part of ${STATIC_BUILD_COMMIT}"
  34. echo "${commit_files}" | prefix_stdout " "
  35. return 2
  36. fi
  37. return 0
  38. }
  39. static.build.drop() {
  40. # drop last commit if it was made by the static.build.commit command
  41. local last_commit_id
  42. local branch
  43. build_msg STATIC "drop last commit if it was previously done by static.build.commit"
  44. # get only last (option -n1) local commit not in remotes
  45. branch="$(git branch --show-current)"
  46. last_commit_id="$(git log -n1 "${branch}" --pretty=format:'%h'\
  47. --not --exclude="${branch}" --branches --remotes)"
  48. if [ -z "${last_commit_id}" ]; then
  49. err_msg "there are no local commits"
  50. return 1
  51. fi
  52. if ! is.static.build.commit "${last_commit_id}"; then
  53. return $?
  54. fi
  55. build_msg STATIC "drop last commit ${last_commit_id}"
  56. git reset --hard HEAD~1
  57. }
  58. static.build.commit() {
  59. # call the "static.build.drop" command, then "themes.all" then commit the
  60. # built files ($BUILT_PATHS).
  61. build_msg STATIC "build & commit /static files"
  62. # check for not committed files
  63. if [ -n "$(git diff --name-only)" ]; then
  64. err_msg "some files are not committed:"
  65. git diff --name-only | prefix_stdout " "
  66. return 1
  67. fi
  68. # check for staged files
  69. if [ -n "$(git diff --name-only --cached)" ]; then
  70. err_msg "some files are staged:"
  71. git diff --name-only --cached | prefix_stdout " "
  72. return 1
  73. fi
  74. # drop existing commit from previous build
  75. static.build.drop &>/dev/null
  76. ( set -e
  77. # fix & build the themes
  78. themes.fix
  79. themes.all
  80. # add build files
  81. for built_path in "${STATIC_BUILT_PATHS[@]}"; do
  82. git add -v "${built_path}"
  83. done
  84. # check if any file has been added (in case of no changes)
  85. if [ -z "$(git diff --name-only --cached)" ]; then
  86. build_msg STATIC "no changes applied / nothing to commit"
  87. return 0
  88. fi
  89. # check for modified files that are not staged
  90. if [ -n "$(git diff --name-only)" ]; then
  91. die 42 "themes.all has created files that are not in STATIC_BUILT_PATHS"
  92. fi
  93. git commit -m "${STATIC_BUILD_COMMIT}"
  94. )
  95. }
  96. static.build.restore() {
  97. build_msg STATIC "git-restore of the built files (/static)"
  98. git restore --staged "${STATIC_BUILT_PATHS[@]}"
  99. git restore --worktree "${STATIC_BUILT_PATHS[@]}"
  100. }