関数でgitサブモジュールforeachを使用する

関数でgitサブモジュールforeachを使用する

私の目標は、特定のブランチに基づいてすべてのgitサブモジュールを更新するスクリプトを書くことです。サブモジュールに対応する分岐がない場合、マスターが使用されます。

これが私が今持っているものです:

#!/bin/bash -x

if [ -z $1 ]; then
    echo "Branch name required."
    exit
fi

function pbranch {
    exists=`git show-ref refs/heads/$branch`

    if [ -z $exists ]; then
        branch="master"
    fi

    git co $branch
    git pull origin $branch
}

branch=$1

git submodule foreach pbranch

ただし、このスクリプトを実行するとエラーが発生します。

oleq@pc ~/project> git-fetchmodules major
+ '[' -z major ']'
+ branch=major
+ git submodule foreach pbranch
Entering 'submodule'
/usr/lib/git-core/git-submodule: 1: eval: pbranch: not found
Stopping at 'submodule'; script returned non-zero status.

私の推測はgit submodule foreachevalを利用することです。文書)、この場合は正しく使用しません。

何十億もありますはい「インラインコールバック」でこのコマンドを使用する方法はありますが、関数フォームのコールバックを持つコマンドは見つかりません。この問題を解決する方法を知っていますか?

ベストアンサー1

関数を引用符の中にコールバックとして入れて問題を解決しました。

#!/bin/bash

if [ -z $1 ]; then
    echo "Branch name required."
    exit
fi

git submodule foreach "
    branch=$1;
    exists=\$(git show-ref refs/heads/\$branch | cut -d ' ' -f1);

    if [ -z \$exists ]; then
        branch='master';
    fi;

    echo Checking branch \$branch for submodule \$name.;

    git fetch --all -p;
    git co \$branch;
    git reset --hard origin/\$branch;
"

同様の変数は$1スクリプトの名前空間から来ます。同様に、$\(bar)「エスケープされた」エントリは\$branch「コールバック」で評価されます。それは簡単です。

おすすめ記事