【发布时间】:2015-08-07 10:25:26
【问题描述】:
我遇到的git for-each-ref 的最高级用法涉及eval。例如,the last example in the git-for-each-ref man page 使用eval 来执行fmt 变量的内容:
#!/bin/sh
fmt='
r=%(refname)
# ... omitted, for conciseness ...
'
eval=`git for-each-ref --shell --format="$fmt" \
# ... omitted, for conciseness ...
refs/tags`
eval "$eval"
但是,the use of eval is associated with security risks;尽可能避免它被认为是一种好的做法。
这是一个真实的例子,改编自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/)"
在这个特定的例子中,我怎样才能避免使用eval?我已经查看了Zenexer's answer 中列出的选项,但我找不到一个可以解决问题的选项。我正在寻找尽可能便携(跨不同外壳)的解决方案。
【问题讨论】:
-
恕我直言,这样做是不合时宜的,即使手册页建议这样做。你会找到比 eval 更好的方法。例如,您可以使用
git for-each-ref创建一个 json、xml、csv(或其他)文件,并将其用作 shell 脚本或 shell 函数的输入。