2017-11-06 6 views
1

私は、興味のあるファイルにgit diffがあるかどうかをチェックするファイルIソースの関数を持っています。 diffs=$(git diff --name-only "$ancestor".."$source_ref" -- "$patn")が期待どおりに動作するように、$ patnにどのように値を割り当てますか?bashは文字列中の単語をサブシェル内の個々の入力として区別する

build_diff() { 
    local patn 
    patn=${1:-"."} 

    # convert names to hash 
    local this_ref 
    this_ref=${2:-$(git rev-parse --verify HEAD)} 
    local source_ref 
    source_ref=$(git show-ref -s --heads "$this_ref" || echo "$this_ref") 
    local target_ref 
    target_ref=$(git show-ref -s --heads "${3:-master}" || echo "$3") 

    # when target and source are the same (post integration), use the branch's 
    # parent as ancestor 
    # When they are not the same, find the 'best' common node as ancestor 
    local ancestor 
    if [[ $target_ref == "$source_ref" ]]; then 
    ancestor=$(git rev-parse --verify "$target_ref"^) 
    else 
    ancestor=$(git merge-base --all "$target_ref" "$source_ref" || git merge-base --fork-point "$target_ref" "$source_ref") 
    fi 

    local diffs 
    diffs=$(git diff --name-only "$ancestor".."$source_ref" -- "${patn[@]}") 
    echo $diffs 
    if [[ -z ${diffs//[[:space:]]/} ]]; then 
    echo false 
    else 
    echo true 
    fi 
} 

- たとえば関数呼び出し

FILES=".tool-versions ./other/runner/Dockerfile" 
build_diff "$FILES" 

スクリプトの中で私はそれが動作するおよそ$ 1とpatn=(.tool-versions ./other/runner/Dockerfile)を忘れてしまったが、私$ 77

答えて

1
経由でファイルの一覧に合格することはできませんよ場合

あなたはしません。 $patnを配列にしてから、"${patn[@]}"を配置します。

$patn=(foo bar baz) 
... 
echo "${patn[@]}" 
+0

投稿後、私は自分のコードを見て、正しい行をコピーしなかったことを認識しました - しかし、あなたはすでに私にそれを打つ!どのようにして$ 1という文字列を配列形式でpatnに割り当てるのですか? – Tom

+0

あなたは '$ 1'に含まれるどのようなグロブをどのように展開するのでしょうか?代わりに '$ @'を使うことができない理由はありますか? –

+0

もしpatn = $ 1で、 ".tool-versions/other/runner/Dockerfile"を$ 1として渡すと動作しません。 ($ 2と$ 3がありますが、必要に応じてemを切り替えることができます) – Tom

関連する問題