How to quickly find all git repos under a directory

前端 未结 8 1477
暖寄归人
暖寄归人 2020-12-12 13:14

The following bash script is slow when scanning for .git directories because it looks at every directory. If I have a collection of large repositories it takes a long time f

8条回答
  •  鱼传尺愫
    2020-12-12 13:47

    Here is an optimized solution:

    #!/bin/bash
    # Update all git directories below current directory or specified directory
    # Skips directories that contain a file called .ignore
    
    HIGHLIGHT="\e[01;34m"
    NORMAL='\e[00m'
    
    function update {
      local d="$1"
      if [ -d "$d" ]; then
        if [ -e "$d/.ignore" ]; then 
          echo -e "\n${HIGHLIGHT}Ignoring $d${NORMAL}"
        else
          cd $d > /dev/null
          if [ -d ".git" ]; then
            echo -e "\n${HIGHLIGHT}Updating `pwd`$NORMAL"
            git pull
          else
            scan *
          fi
          cd .. > /dev/null
        fi
      fi
      #echo "Exiting update: pwd=`pwd`"
    }
    
    function scan {
      #echo "`pwd`"
      for x in $*; do
        update "$x"
      done
    }
    
    if [ "$1" != "" ]; then cd $1 > /dev/null; fi
    echo -e "${HIGHLIGHT}Scanning ${PWD}${NORMAL}"
    scan *
    

提交回复
热议问题