adapted a more generic, customizable and friendly script

This commit is contained in:
CJ 2014-12-16 21:26:18 +08:00
parent 399073af7b
commit 73d5209743

View file

@ -6,12 +6,23 @@ COLOR_YELLOW(){ echo -en "\033[33m"; }
COLOR_BLUE() { echo -en "\033[34m"; }
COLOR_RESET() { echo -en "\033[0m"; }
function _test_git_scp()
{
command -v rsync > /dev/null || _error requires rsync
command -v git > /dev/null || _error requires git
command -v ssh > /dev/null || _error requires ssh
command -v php > /dev/null || _info optional php
command -v dos2unix > /dev/null || _info optional dos2unix
}
function set_remote()
{
remote=$1
if [ $(git remote | grep -c ^$remote$) -eq 0 ]
then
COLOR_RED
echo "Remote $remote does not exist in your git config"
COLOR_RESET
exit 1
fi
}
@ -25,22 +36,42 @@ function php_lint()
do
# check if file exists
# check if file ends with ".php"
if [ -f "$i" ] && [ "${i: -4}" == ".php" ]
then
php -l "$i" > /dev/null
[ $? -gt 0 ] && error_count[${#error_count[@]}]="$i"
fi
test ! -f "$i" && continue
case "$i" in
*\.php|*\.phtml)
php -l "$i" > /dev/null
[ $? -gt 0 ] && error_count[${#error_count[@]}]="$i"
;;
esac
done
# syntax check fails, force exit
[ ${#error_count[@]} -gt 0 ] &&
test ${#error_count[@]} -gt 0 &&
COLOR_RED &&
echo "Error: ${#error_count[@]} PHP syntax error found" &&
echo "${error_count[@]}" | tr " " '\n' &&
COLOR_RESET &&
exit 255
echo -n
return 0
}
function _dos2unix()
{
command -v dos2unix > /dev/null && dos2unix $@
return 0
}
function _sanitize()
{
git config --get-all extras.scp.sanitize | while read i
do
case $i in
php_lint) php_lint $@;; # git config --global --add extras.scp.sanitize php_lint
dos2unix) _dos2unix $@;; # git config --global --add extras.scp.sanitize dos2unix
esac
done
return $?
}
function scp_and_stage
@ -59,7 +90,6 @@ function scp_and_stage
if [ $# -ge 1 ]
then
list=$(git ls-files "$@")" "$(git ls-files -o "$@")
# list=$(echo "$@" | tr ' ' "\\n")
elif [ -n "$refhead" ]
then
git diff --stat $refhead
@ -74,11 +104,10 @@ function scp_and_stage
if [ -n "$list" ]
then
_TMP=${0///}
local _TMP=${0///}
echo "$list" > $_TMP &&
php_lint $list &&
echo Pushing to $remote \($(git config remote.$remote.url)\) &&
(which dos2unix > /dev/null && dos2unix $list || echo > /dev/null) &&
_sanitize $list &&
_info Pushing to $remote \($(git config remote.$remote.url)\) &&
rsync -rlDv --files-from=$_TMP ./ "$(git config remote.$remote.url)/" &&
git add $list &&
rm $_TMP
@ -88,8 +117,8 @@ function scp_and_stage
[ -n "$deleted" ] &&
COLOR_RED &&
echo Deleted files &&
ssh $(git config remote.$remote.url | cut -d: -f1) -t rm $deleted &&
echo Deleted remote files &&
ssh $(git config remote.$remote.url | cut -d: -f1) -t "rm $deleted" &&
echo "$deleted"
COLOR_RESET
}
@ -105,12 +134,21 @@ function reverse_scp()
rm $_TMP
}
function _info()
{
COLOR_YELLOW
test $# -gt 0 && echo "$@"
COLOR_RESET
}
function _usage()
{
echo "Usage:
git goth -h|help|?
git goth scp|stage <remote> [ref|file..] # scp and stage your files to specified remote
git goth scp|stage <remote> [<ref>] # show diff relative to <ref> and upload unstaged files to <remote>"
git scp -h|help|?
git scp <remote> [ref|file..] # scp and stage your files to specified remote
git scp <remote> [<ref>] # show diff relative to <ref> and upload unstaged files to <remote>
git rscp <remote> [<file|directory>] # copy <remote> files to current working directory
"
case $1 in
-v|verbose|--verbose) grep -A100 '^#* OPTIONS #*$' $0;;
@ -132,7 +170,7 @@ function _error()
case $(basename $0) in
git-scp)
case $1 in
''|-h|?|help|--help) shift; _usage $@;;
''|-h|?|help|--help) shift; _test_git_scp; _usage $@;;
*) scp_and_stage $@;;
esac
;;