#! /bin/sh for arg in $@; do option=`echo "'$arg'" | cut -d'=' -f1 | tr -d "'"` value=`echo "'$arg'" | cut -d'=' -f2- | tr -d "'"` if [ "$value" == "$option" ]; then value=""; fi case "$option" in pull) action="pull";; push) action="push";; -d|--git-dir) GIT_DIR=$value;; -r|--git-remote) GIT_REMOTE=$value;; -b|--git-branch) GIT_BRANCH=$value;; -s|--use-stash) USE_STASH=1;; *) GIT_REMOTE=$arg;; esac done GIT_DIR=${GIT_DIR:-$HOME/usr/git} GIT_REMOTE=${GIT_REMOTE:-origin} GIT_BRANCH=${GIT_BRANCH:-master} USE_STASH=${USE_STASH:-0} RESET="\033[0m" RED="\033[0;31m" GREEN="\033[0;32m" BROWN="\033[0;33m" PURPLE="\033[0;35m" CYAN="\033[0;36m" YELLOW="\033[1;33m" echo -e "remote:${YELLOW}${GIT_REMOTE}${RESET}:${PURPLE}${GIT_BRANCH}${RESET} USE_STASH=${RED}${USE_STASH}${RESET}" for gitdir in ${GIT_DIR}; do echo -e "enter ${BROWN}${gitdir}${RESET}" && cd ${gitdir} || return 1 for dir in $(ls -1); do STASH=0 [ -d ${dir} ] && [ -e ${dir}/.git/config ] || continue # SEARCH REMOTE echo -e " enter ${BROWN}${dir}${RESET}" && cd $dir || exit 1 git remote show ${GIT_REMOTE} >/dev/null 2>&1 if [ $? -ne 0 ]; then echo -e " ${RED}${GIT_REMOTE} remote not found ${RESET}" && cd .. && continue fi # CHECKOUT echo -ne " ${CYAN}checkout $YELLOW$GIT_BRANCH$RESET ... " && git checkout -q ${GIT_BRANCH} 2>/dev/null && echo -e "${GREEN}OK${RESET}" if [ $? -ne 0 ]; then echo -e "${RED}KO${RESET}" && cd .. && continue fi # GC + CLEAN echo -ne " ${CYAN}clean$RESET ... " && git prune && git gc --quiet && git repack && git diff --quiet && echo -e "${GREEN}OK${RESET}" if [ $? -ne 0 ]; then if [ "$action" = "push" ]; then echo -e "dirty but ${GREEN}OK${RESET}" elif [ $USE_STASH -eq 1 ]; then STASH=1 echo -e "${RED}head is not clean, ${CYAN}git stash save${RESET}" && git stash save -q else echo -e "${RED}head is not clean, USE_STASH=0 ... ${CYAN}leave${RESET}" && cd .. && continue fi fi # PULL if [ "$action" != "push" ] ; then echo -ne " ${CYAN}pull${RESET}: ${YELLOW}${GIT_REMOTE}${RESET}:${PURPLE}${GIT_BRANCH}${RESET} ... " && \ git pull -q ${GIT_REMOTE} ${GIT_BRANCH} && echo -e "${GREEN}OK${RESET}" if [ $? -ne 0 ]; then echo -e "${RED}KO${RESET}" && cd .. && continue fi fi # PUSH if [ "$action" != "pull" ] ; then echo -ne " ${CYAN}push${RESET}: ${YELLOW}${GIT_REMOTE}${RESET}:${PURPLE}${GIT_BRANCH}${RESET} ... " && \ git push -q ${GIT_REMOTE} ${GIT_BRANCH} && echo -e "${GREEN}OK${RESET}" if [ $? -ne 0 ]; then echo -e "${RED}KO${RESET}" && cd .. && continue fi fi if [ $STASH -eq 1 ]; then echo -e " ${RED}was not clean, ${CYAN}git stash pop${RESET}" && git stash pop -q fi cd .. done echo -e "leave ${BROWN}${gitdir}${RESET}" done