Skip to content

Instantly share code, notes, and snippets.

@dalf
Last active June 21, 2021 15:06
Show Gist options
  • Save dalf/c73c59f96972ee7cf904a7861f9b0cb5 to your computer and use it in GitHub Desktop.
Save dalf/c73c59f96972ee7cf904a7861f9b0cb5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
# SPDX-License-Identifier: AGPL-3.0-or-later
BUILD_COMMIT_MESSAGE="Static build"
BUILT_PATHS=(
searx/static/themes/oscar/css
searx/static/themes/oscar/js
searx/static/themes/oscar/src/generated/pygments-logicodev.less
searx/static/themes/oscar/src/generated/pygments-pointhi.less
searx/static/themes/simple/css
searx/static/themes/simple/js
searx/static/themes/simple/src/generated/pygments.less
)
CURRENT_BRANCH="$(git branch --show-current)"
STAGED_FILES=$(git diff --name-only --cached)
git_log_current_branch() {
git log "heads/${CURRENT_BRANCH}" --not --exclude="${CURRENT_BRANCH}" --branches --remotes --pretty=format:"%h"
}
is.build.commit() {
# check commit message
COMMIT_MESSAGE=$(git show -s --format=%s $1)
if [ "${COMMIT_MESSAGE}" != "${BUILD_COMMIT_MESSAGE}" ]; then
echo "Commit message of $1 is '${BUILD_COMMIT_MESSAGE}'"
return 1
fi
# check all files of the commit belongs to $BUILT_PATHS
COMMIT_FILES=$(git diff-tree --no-commit-id --name-only -r "$1")
for i in ${BUILT_PATHS[*]}; do
# remove files of ${BUILT_PATHS}
COMMIT_FILES=$(echo "${COMMIT_FILES}" | grep -v "^${i}")
done
if [ -n "${COMMIT_FILES}" ]; then
echo "Commit $1 contains files that were not build: ${COMMIT_FILES}"
return 2
fi
return 0
}
cmd.commit.drop() {
LAST_COMMIT_ID=$(git_log_current_branch | head -1)
is.build.commit "${LAST_COMMIT_ID}"
if [ $? -ne 0 ]; then
return $?
fi
echo "Drop last commit ${LAST_COMMIT_ID}"
git reset --hard HEAD~1
}
cmd.commit() {
# check for not commited files
NOT_COMMITED_FILES="$(git diff --name-only)"
if [ -n "${NOT_COMMITED_FILES}" ]; then
echo "Some files are not commited:"
echo "${NOT_COMMITED_FILES}"
return 1
fi
# check for staged files
if [ -n "${STAGED_FILES}" ]; then
echo "Some files are staged:"
echo "${STAGED_FILES}"
return 1
fi
# drop existing commit
cmd.commit.drop
if [ $? -ne 0 ]; then
return $?
fi
(
set -e
# build the themes
make themes.all
# add build files
for built_path in ${BUILT_PATHS[@]}; do
git add -v "${built_path}"
done
# check for modified files that are not staged
if [ -n "$(git diff --name-only)" ]; then
echo "make themes.all has created files that are not in BUILT_PATHS"
return 2
fi
#
git commit -m "Static build"
)
}
cmd.restore.staged() {
for i in ${BUILT_PATHS[*]}; do
STAGED_FILES_FOR_I=$(echo "${STAGED_FILES}" | grep "^${i}")
if [ -n "${STAGED_FILES_FOR_I}" ]; then
git restore --staged ${STAGED_FILES_FOR_I}
fi
done
}
cmd.restore() {
cmd.restore.staged
NOT_COMMITED_FILES="$(git diff --name-only)"
for i in ${BUILT_PATHS[*]}; do
NOT_COMMITED_FILES_FOR_I=$(echo "${NOT_COMMITED_FILES}" | grep "^${i}")
if [ -n "${NOT_COMMITED_FILES_FOR_I}" ]; then
git restore ${NOT_COMMITED_FILES_FOR_I}
fi
done
}
main() {
case $1 in
commit.drop)
# drop last commit if it was made by the "commit" command
cmd.commit.drop
;;
commit)
# call the "commit.drop" command,
# then "make themes.all"
# then commit the built files ($BUILT_PATHS).
cmd.commit
;;
restore.staged)
# after "git add ."
# remove the built files
# so only the source are commited
cmd.restore.staged
;;
restore)
# "git restore" of the built files.
cmd.restore
;;
esac
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment