Skip to content

Instantly share code, notes, and snippets.

@liuxd
Last active September 7, 2017 01:36
Show Gist options
  • Save liuxd/8090860 to your computer and use it in GitHub Desktop.
Save liuxd/8090860 to your computer and use it in GitHub Desktop.
[mm.sh] 命令行下的编程小助手。包括:智能git push,代码统计,批量的PHP文件语法检查三个小功能。
#!/bin/bash
#
# Install:
# cur_dir=`pwd`
# sudo ln -s $cur_dir/mm.sh /bin/mm
#显示彩色文本信息。
function cecho {
case $2 in
info )
color=33;;
error )
color=31;;
success )
color=32;;
*)
color=1;;
esac
echo -e "\033["$color"m$1\033[0m"
}
if [ -z $1 ]; then
echo -e "\033[1m"
cat << EOF
mm 我的编程小助手。
使用:
mm <command> [args]
命令:
up 更新当前分支并提交、push
count 检查指定扩展名文件的行数。
ckphp 检查PHP代码语法。
EOF
echo -e "\033[0m"
exit
fi
case $1 in
up )
#push代码到远端git仓库
msg=$2
branch=$(git symbolic-ref HEAD 2>/dev/null \
|| git rev-parse HEAD 2>/dev/null | cut -c1-10 \
)
branch=${branch#refs/heads/}
fetch="git fetch origin"
if [ ! -z $msg ];then
add='git add .'
commit="git commit -am $msg"
fi
rebase="git rebase origin/$branch"
remote=`git remote show`
echo -e "\033[32m"$fetch"\033[0m"
echo -e "\033[32m"$rebase"\033[0m"
for i in $remote;do
echo -e "\033[32m"git push $i $branch"\033[0m"
done
if [ ! -z $msg ];then
echo -e "\033[32m"$add"\033[0m"
echo -e "\033[32m"$commit"\033[0m"
fi
if [ ! -z $msg ];then
$add
$commit
fi
$fetch
$rebase
for i in $remote;do
git push $i $branch
done;;
count )
#统计代码行数
if [ -z $2 ];then
ext="php"
else
ext=$2
fi
cnt=`find ./ -name "*.$ext" |xargs cat|grep -v ^$|wc -l`
cecho "$ext 代码行数(不算空格):" success
cecho $cnt;;
ckphp )
base_str='No syntax errors detected in '
#检查PHP脚本是否有语法错误
find . -name "*.php"|while read php_file
do
return=`php -l $php_file`
nopa="$base_str$php_file"
if [ "$nopa" != "$return" ]; then
echo $return
exit
fi
done
;;
* )
cecho '你想干啥?' error;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment