Skip to content

Instantly share code, notes, and snippets.

@arsu-leo
Last active October 18, 2018 14:22
Show Gist options
  • Save arsu-leo/aa14a45897baf5712e162eee84ef1c6a to your computer and use it in GitHub Desktop.
Save arsu-leo/aa14a45897baf5712e162eee84ef1c6a to your computer and use it in GitHub Desktop.
#!/bin/bash
### This script is intended to be used from inside a git repository (Say any folder where git status would not fail)
### It is intended to be stored on a path folder to be used directly as follows:
### $ gitc ['comment']
###
### If comment is missing it will still prompt asking for a comment, if no input it would use the default_comment value '-.-'
default_comment='-.-'
function remove_spaces {
l=${1}
l="${l#"${l%%[![:space:]]*}"}" # remove leading whitespaces
echo "$l"
return 0
}
function test_js_file {
file=$1
output=`node -c ${file}`
status=$?
if [[ $status == 0 ]]
then
echo "Ok"
else
echo "${output}"
fi
return $status
}
function test_php_file {
file=$1
output=`php -l ${file}`
status=$?
if [[ $status == 0 ]]
then
echo "Ok"
else
echo "${output}"
fi
return $status
}
function test_file {
file=$1
action=$2
r=0
if [[ $file == *".js"* ]]; then
echo "Check ${action} file: ${file}"
test_js_file "$file"
r=$?
elif [[ $file == *".php"* ]]; then
echo "Check ${action} file: ${file}"
test_php_file "$file"
r=$?
else
:
fi
return $r
}
function git_status {
#grep git status output of files and not deleted ones
status=$(git status)
echo "${status}
"
output=$(echo "${status}" | grep -e '\.js' -e '\.php' | grep -v 'deleted:' | sed -e "s/\s//g" | sed -e "s/:.*->/:/g" )
error_files=0
total_files=0
ok_files=0
#echo "OUTPUT: ${output}"
while read -r readLine; do
if [ ! -z "$readLine" ] && [ -n "$readLine" ]; then
if [[ $total_files == 0 ]]; then
echo " ------- Cheking files syntax -------"
fi
action=$(echo "${readLine}" | awk -F ":" "{ print \$1 }")
file=$(echo "${readLine}" | awk -F ":" "{ print \$2 }")
if test_file "${file}" "${action}"; then
ok_files=$((ok_files+1))
else
error_files=$((error_files+1))
fi
total_files=$((total_files+1))
fi
done <<< "$output"
if [[ $total_files == 0 ]]; then
return 2
fi
d_error_files=$(printf '% 2d' ${error_files})
d_total_files=$(printf '% 2d' ${total_files})
echo "Total files: ${d_total_files}"
echo "Syntax errors:${d_error_files}"
if [[ $error_files == 0 ]]; then
return 0
else
return 1
fi
}
function git_commit {
comment=$1
if [ -z "$comment" ] || [ ! -n "$comment" ]; then
echo "Type your commit comment ('${default_comment}'):"
read comment
if [ -z "$comment" ] || [ ! -n "$comment" ]; then
comment=$default_comment;
fi
echo "Comment: \"$comment\""
else
echo "Using param comment: \"${comment}\""
fi
output=`git commit -m "${comment}"`
result=$?
echo "${output}"
return $result
}
comment=$1
git add . -A || { echo 'git add failed' ; exit 1; }
#Git status parsing + adding + syntax check
git_status
status_result=$?
#echo "Status result: ${status_result}"
if [[ $status_result == 1 ]]; then
exit 1;
elif [[ $status_result == 2 ]]; then
echo 'No files found to commit, bye!'
exit 1;
else
echo 'No errors, pulling ...'
fi
git pull || { echo 'git pull failed' ; exit 1; }
if ! git_commit "$comment"; then
echo "git commit -m ${comment} failed"
exit 1;
fi
git push || { echo 'git push fail' ; exit 1; }
#exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment