背景
所在项目有多个git工程,且debug期间需要对照多个工程(否则会因为版本不一致而导致问题一直无法定位--血和泪的教训),因此经常遇到对多个工程执行相同git命令的场景。为解决一个个执行低效的问题,写了一个脚本,可以对多个git工程批量执行相同的git命令
shell版
#!/bin/bash
id=""
if test -f ".id"
then
id=$(cat .id)
else
while [[ ! $id =~ [0-9]{8} ]]
do
read -p "Plz input your id: " id
done
echo $id > ".id"
fi
MAIN_PROJ=ssh://${id}@地址ip/路径/路径
SUB_PROJ=(工程1 工程2 工程3 工程4 工程5)
CMD=$1
for PROJ in ${SUB_PROJ[@]}; do
echo "========${PROJ}========"
case $CMD in
clone) git clone ${MAIN_PROJ}/${PROJ}
;;
*) (cd $PROJ && git $*)
;;
esac
done
python 版
import os
import sys
def exec_command(command):
result = os.popen(command)
return ''.join(result.readlines())
dirs = [path for path in os.listdir('.') if os.path.isdir(path)]
argv = ' '.join(sys.argv[1:])
git_command = 'git ' + argv
for d in dirs:
print '========================%s============================' % d
print exec_command('cd %s && %s' % (d, git_command))