Skip to content

Instantly share code, notes, and snippets.

@yszheda
Last active November 8, 2023 09:01
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yszheda/6125a32e37834cc8ab75 to your computer and use it in GitHub Desktop.
Save yszheda/6125a32e37834cc8ab75 to your computer and use it in GitHub Desktop.
svn pre-commit hook to check lua syntax errors, check lua global vars, and prevent committing large png
#!/bin/sh
# PRE-COMMIT HOOK
#
# The pre-commit hook is invoked before a Subversion txn is
# committed. Subversion runs this hook by invoking a program
# (script, executable, binary, etc.) named 'pre-commit' (for which
# this file is a template), with the following ordered arguments:
#
# [1] REPOS-PATH (the path to this repository)
# [2] TXN-NAME (the name of the txn about to be committed)
#
# [STDIN] LOCK-TOKENS ** the lock tokens are passed via STDIN.
#
# If STDIN contains the line "LOCK-TOKENS:\n" (the "\n" denotes a
# single newline), the lines following it are the lock tokens for
# this commit. The end of the list is marked by a line containing
# only a newline character.
#
# Each lock token line consists of a URI-escaped path, followed
# by the separator character '|', followed by the lock token string,
# followed by a newline.
#
# The default working directory for the invocation is undefined, so
# the program should set one explicitly if it cares.
#
# If the hook program exits with success, the txn is committed; but
# if it exits with failure (non-zero), the txn is aborted, no commit
# takes place, and STDERR is returned to the client. The hook
# program can use the 'svnlook' utility to help it examine the txn.
#
# On a Unix system, the normal procedure is to have 'pre-commit'
# invoke other programs to do the real work, though it may do the
# work itself too.
#
# *** NOTE: THE HOOK PROGRAM MUST NOT MODIFY THE TXN, EXCEPT ***
# *** FOR REVISION PROPERTIES (like svn:log or svn:author). ***
#
# This is why we recommend using the read-only 'svnlook' utility.
# In the future, Subversion may enforce the rule that pre-commit
# hooks should not modify the versioned data in txns, or else come
# up with a mechanism to make it safe to do so (by informing the
# committing client of the changes). However, right now neither
# mechanism is implemented, so hook writers just have to be careful.
#
# Note that 'pre-commit' must be executable by the user(s) who will
# invoke it (typically the user httpd runs as), and that user must
# have filesystem-level permission to access the repository.
#
# On a Windows system, you should name the hook program
# 'pre-commit.bat' or 'pre-commit.exe',
# but the basic idea is the same.
#
# The hook program typically does not inherit the environment of
# its parent process. For example, a common problem is for the
# PATH environment variable to not be set to its usual value, so
# that subprograms fail to launch unless invoked via absolute path.
# If you're having unexpected problems with a hook program, the
# culprit may be unusual (or missing) environment variables.
#
# Here is an example hook script, for a Unix /bin/sh interpreter.
# For more examples and pre-written hooks, see those in
# the Subversion repository at
# http://svn.apache.org/repos/asf/subversion/trunk/tools/hook-scripts/ and
# http://svn.apache.org/repos/asf/subversion/trunk/contrib/hook-scripts/
LOG="/tmp/svn.log"
touch ${LOG}
REPOS="$1"
TXN="$2"
echo "REPOS: $REPOS" > ${LOG}
echo "TXN: $TXN" >> ${LOG}
SVNLOOK=""
# lua compiler
LUAC=""
# lua file extension
LUA_EXT="lua"
# png file extension
PNG_EXT="png"
MSG_MIN_CHAR_NUM=3
MAX_PNG_SIZE=2048
PROHIBITED_FILES=(
# Put your prohibited files here.
)
TMP_DIR="/tmp/svn"
if [[ -d ${TMP_DIR} ]]; then
rm -r ${TMP_DIR}
fi
mkdir -p ${TMP_DIR}
function check_lua_syntax {
local lua_file=$1
echo `${LUAC} ${lua_file} 2>&1`
}
# Put the allowed global varibale names in ${ALLOWED_SYM_FILE}
ALLOWED_SYM_FILE=""
# Get lua-releng from https://github.com/openresty/nginx-devel-utils/blob/master/lua-releng
LUA_RELENG=""
function check_lua_global_vars {
local lua_file=$1
sym_file="${lua_file}.sym"
${LUA_RELENG} ${lua_file} > ${sym_file}
cat ${sym_file} | grep GLOBAL | awk '{print $NF}' | grep -v -f ${ALLOWED_SYM_FILE}
}
function create_file {
local file_name=$1
# Create tmp file and copy content
tmp_file="${TMP_DIR}/${file_name}"
mkdir -p "$(dirname "${tmp_file}")" && touch "${tmp_file}"
${SVNLOOK} cat -t "${TXN}" "${REPOS}" "${file_name}" > ${tmp_file}
}
# Make sure that the log message contains some text.
commit_msg=`$SVNLOOK log -t "$TXN" "$REPOS" | sed 's/[[:space:]]//g'`
echo ${commit_msg} >> ${LOG}
if [[ `echo ${commit_msg} | wc -c` -lt ${MSG_MIN_CHAR_NUM} ]]; then
echo "Please write a meaningful comment when committing" 1>&2
exit 1
fi
changed_info_str=`${SVNLOOK} changed -t "${TXN}" "${REPOS}"`
IFS=$'\n' read -rd '' -a changed_infos <<<"${changed_info_str}"
lua_error_msg=""
png_error_msg=""
for changed_info in "${changed_infos[@]}"; do
# Prevent commiting file that contains space in its filename
echo ${changed_info} >> ${LOG}
operation=`echo ${changed_info} | awk '{print $1}'`
if [[ ${operation} = "A" ]] && [[ `echo ${changed_info} | awk '{print NF}'` -gt 2 ]]; then
echo "Please do not commit file that contains space in its filename!" 1>&2
exit 1
fi
file_name=`echo ${changed_info} | awk '{print $2}'`
echo "operation: ${operation}, file: ${file_name}, ext: ${ext}" >> ${LOG}
# Check prohibit-commit files
for prohibited_file in ${PROHIBITED_FILES[@]}; do
if [[ ${file_name} = ${prohibited_file} ]]; then
echo "${file_name} is not allowed to be changed!" 1>&2
exit 1
fi
done
ext=`echo ${file_name} | awk -F"." '{print $NF}'`
if [[ ${operation} = "U" ]] || [[ ${operation} = "A" ]]; then
tmp_file="${TMP_DIR}/${file_name}"
# Check lua syntax
# Check lua global variable
if [[ ${ext} = ${LUA_EXT} ]]; then
create_file ${file_name}
echo "Check syntax of ${tmp_file}" >> ${LOG}
error_msg=`check_lua_syntax ${tmp_file}`
if [[ `echo ${error_msg} | sed 's/\n//g'` != "" ]]; then
lua_error_msg="${lua_error_msg}\n${error_msg}"
fi
echo "Check global variables of ${tmp_file}" >> ${LOG}
error_msg=`check_lua_global_vars ${tmp_file}`
if [[ `echo ${error_msg} | sed 's/\n//g'` != "" ]]; then
lua_error_msg="${lua_error_msg}\nPlease do not use global variables: ${error_msg}"
fi
fi
# Check file size
if [[ ${ext} = ${PNG_EXT} ]]; then
create_file ${file_name}
png_info=`file ${tmp_file} | sed 's/,//g'`
png_width=`echo ${png_info} | awk '{print $5}' | bc`
png_height=`echo ${png_info} | awk '{print $7}' | bc`
if [[ ${png_width} -gt ${MAX_PNG_SIZE} ]] || [[ ${png_height} -gt ${MAX_PNG_SIZE} ]]; then
png_error_msg="${png_error_msg}\n${file_name} is too large: ${png_width} x ${png_height}"
fi
fi
fi
done
rm -r ${TMP_DIR}
if [[ ${lua_error_msg} != "" ]] || [[ ${png_error_msg} != "" ]]; then
if [[ ${lua_error_msg} != "" ]]; then
echo "lua error: ${lua_error_msg}" >> ${LOG}
echo "Please fix the error in your lua program:${lua_error_msg}" 1>&2
fi
if [[ ${png_error_msg} != "" ]]; then
echo "png error: ${png_error_msg}" >> ${LOG}
echo "Please do not commit pictures which are larger than 2048 x 2048:${png_error_msg}" 1>&2
fi
exit 1
fi
# Check that the author of this commit has the rights to perform
# the commit on the files and directories being modified.
# commit-access-control.pl "$REPOS" "$TXN" commit-access-control.cfg || exit 1
# All checks passed, so allow the commit.
exit 0
@hari0905
Copy link

hari0905 commented Nov 6, 2023

hi sir you have a YouTube channel?

@yszheda
Copy link
Author

yszheda commented Nov 8, 2023

hi sir you have a YouTube channel?

no sir ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment