Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使bash脚本拉入所有git子目录。感觉我的bash脚本没有得到很好的优化_Bash_Git - Fatal编程技术网

使bash脚本拉入所有git子目录。感觉我的bash脚本没有得到很好的优化

使bash脚本拉入所有git子目录。感觉我的bash脚本没有得到很好的优化,bash,git,Bash,Git,我已经厌倦了必须对从git存储库中提取的每个目录运行“git pull”。所以我做了这个剧本。我对bash不是很有经验(写的第一个脚本就是这个)。所以我认为我的方法可能有点愚蠢,或者可以用更好的方法来实现。以下是脚本: #!/bin/bash set -e; current_dir=$PWD # check if argument has been supplied if [ $# -eq 0 ]; then echo "Please pass a directory"; e

我已经厌倦了必须对从git存储库中提取的每个目录运行“git pull”。所以我做了这个剧本。我对bash不是很有经验(写的第一个脚本就是这个)。所以我认为我的方法可能有点愚蠢,或者可以用更好的方法来实现。以下是脚本:

#!/bin/bash

set -e;
current_dir=$PWD

# check if argument has been supplied
if [ $# -eq 0 ]; then
    echo "Please pass a directory";
    exit 1;
# declare $PASSED variable as the supplied argument
else
    PASSED=$1;
fi

# Check if passed argument is a directory
if [ -d "${PASSED}" ]; then
    git_directory=false;
    # for every directory with .git in it
    find . -name ".git"|while read fname; do
        #TODO: FIND BETTER WAY FOR THIS
        git_directory=true;
        cd "$current_dir/$fname";
        cd ..;
        echo "Pulling from remote repo";
        git pull origin master;
        cd "$current_dir";
    done
    if ! "$git_directory"; then
        echo "No git directories were found, are you sure you gave the correct directory?";
        exit 1;
    fi

else
    echo "Please pass a directory";
    exit 1;
fi
我觉得“#待办事项:为此找到更好的方法”下的部分写得很糟糕,可能还有更好的方法


有人有什么建议吗?

您可以让
查找
为您完成所有工作:

find "$1" -name .git -print -execdir git pull origin master \;
避免在阅读可能包含换行符或反斜杠的内容时出现
问题

您没有对任何事情使用变量
$PASSED
。(无论如何,私有变量都要避免大写。)

强迫您的所有项目将其主远程设备设置为上游
和主分支
是一个非常简单的假设,它会回来咬您。

cd“$fname/。”
在子shell中会更简单。使用子shell意味着不需要在之后显式返回
cd
find "$1" -name ".git" -type d -execdir git pull upstream master \;