Most advanced uses of git for-each-ref
that I've come across involve eval
. For instance, the last example in the git-for-each-ref
man page uses eval
in order to execute the contents of the fmt
variable:
#!/bin/sh
fmt='
r=%(refname)
# ... omitted, for conciseness ...
'
eval=`git for-each-ref --shell --format="$fmt" \
# ... omitted, for conciseness ...
refs/tags`
eval "$eval"
However, the use of eval
is associated with security risks; avoiding it, whenever possible, is considered good practice.
Here is a real example, adapted from this answer:
#!/bin/sh
fmt='
ref=%(refname:short)
if git merge-base --is-ancestor $1 $ref; then
printf "%s\n" "$ref"
fi
'
eval "$(git for-each-ref --shell --format="$fmt" refs/heads/)"
In this particular example, how can I avoid using eval
? I've reviewed the options listed in Zenexer's answer, but I couldn't find one that does the trick. I'm looking for as portable (across different shells) a solution as possible.