如何快速找到一个目录下的所有的git回购协议 [英] How to quickly find all git repos under a directory

查看:115
本文介绍了如何快速找到一个目录下的所有的git回购协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关git的目录进行扫描,因为它看起来在每个目录时,以下bash脚本是缓慢的。如果我有大型资料库的集合它需要很长的时间寻找通过每个目录流失,寻找git的。它会去得更快,如果将回购中修剪的目录,一旦.git目录中找到。如何做到这一点,或有另一种方式编写完成同样的事情bash脚本任何想法?

 #!/斌/庆典#更新当前目录或指定目录下的所有目录中的gitHIGHLIGHT =\\ E [01;34米
师范='\\ E [00米DIR =。
如果[$ 1=!]然后DIR = $ 1;科幻
CD $ DIR>的/ dev / null的;回声-e$ {} HIGHLIGHT扫描$ {PWD} $ {}师范大学 CD - >的/ dev / null的对于在D`找到。 -name git的型D`;做
  CD $ D / ..>的/ dev / null的
  回声-e\\ n $ {} HIGHLIGHT更新`pwd` $师范大学
  混帐拉
  CD - >的/ dev / null的
DONE


特别是,你将如何使用这些选项?对于这个问题,你不能假设回购收集所有在同一目录下;他们可能是嵌套的目录中。

 
  repo1
  DIRA  DIRB
     DIRC
        repo1


解决方案

下面是一个最优的解决方案:

 #!/斌/庆典
#更新当前目录或指定目录下的所有目录中的git
包含一个名为#跳过目录.IGNOREHIGHLIGHT =\\ E [01;34米
师范='\\ E [00米功能更新{
  本地D =$ 1
  如果[-d$ D];然后
    如果[-e$ D / .IGNORE];然后
      回声-e\\ n $ {} HIGHLIGHT忽略$ D $ {}师范大学
    其他
      CD $ D>的/ dev / null的
      如果[-dgit的];然后
        回声-e\\ n $ {} HIGHLIGHT更新`pwd` $师范大学
        混帐拉
      其他
        扫描*
      科幻
      CD ..>的/ dev / null的
    科幻
  科幻
  #echo退出更新:PWD =`pwd`
}功能扫描{
  #echo`pwd`
  在$ * X;做
    更新$ X
  DONE
}如果[$ 1=!]然后是CD $ 1 GT;的/ dev / null的;科幻
回声-e$ {} HIGHLIGHT扫描$ {PWD} $ {}师范大学
扫描*

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 for find to churn through every directory, looking for .git. It would go much faster if it would prune the directories within repos, once a .git directory is found. Any ideas on how to do that, or is there another way to write a bash script that accomplishes the same thing?

#!/bin/bash

# Update all git directories below current directory or specified directory

HIGHLIGHT="\e[01;34m"
NORMAL='\e[00m'

DIR=.
if [ "$1" != "" ]; then DIR=$1; fi
cd $DIR>/dev/null; echo -e "${HIGHLIGHT}Scanning ${PWD}${NORMAL}"; cd ->/dev/null

for d in `find . -name .git -type d`; do
  cd $d/.. > /dev/null
  echo -e "\n${HIGHLIGHT}Updating `pwd`$NORMAL"
  git pull
  cd - > /dev/null
done


Specifically, how would you use these options? For this problem, you cannot assume that the collection of repos is all in the same directory; they might be within nested directories.

top
  repo1
  dirA

  dirB
     dirC
        repo1

解决方案

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 *

这篇关于如何快速找到一个目录下的所有的git回购协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆