Reputation: 1271
I have a function from a file I source that checks if files of interest have a git diff. How do I assigned a value to $patn such that diffs=$(git diff --name-only "$ancestor".."$source_ref" -- "$patn")
works as expected?
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
}
-- example function call
FILES=".tool-versions ./other/runner/Dockerfile"
build_diff "$FILES"
If in the script I forget about $1 and patn=( .tool-versions ./other/runner/Dockerfile )
then it works, but I am not able to pass in the list of files via $1
Upvotes: 1
Views: 48
Reputation: 798746
You don't. You make $patn
an array and then put "${patn[@]}"
where you want to use it.
$patn=(foo bar baz)
...
echo "${patn[@]}"
Upvotes: 1