「Git」- for-each-ref

  CREATED BY JENKINSBOT

git-for-each-ref,输出每个ref的信息。

命令行语法格式

git for-each-ref [--count=<count>] [--shell|--perl|--python|--tcl]
						  [(--sort=<key>)...] [--format=<format>] [<pattern>...]
						  [--points-at <object>] [(--merged | --no-merged) [<object>]]
						  [--contains [<object>]] [--no-contains [<object>]]

简单示例

下面的示例直接格式化输出文本来显示最近3个打标签的提交:

git for-each-ref --count=3 --sort='-*authordate'           \
    --format='From: %(*authorname) %(*authoremail)
Subject: %(*subject)
Date: %(*authordate)
Ref: %(*refname)

%(*body)
' 'refs/tags'

下面的例子显示了对输出结果使用shell eval,展示了–shell选项的使用,列出所有头的前缀:

#!/bin/sh

git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
while read entry
do
	   eval "$entry"
	   echo `dirname $ref`
done

下面的示例演示了更详细的tag报告,说明格式可能是一个完整的脚本:

#!/bin/sh

fmt='
	   r=%(refname)
	   t=%(*objecttype)
	   T=${r#refs/tags/}

	   o=%(*objectname)
	   n=%(*authorname)
	   e=%(*authoremail)
	   s=%(*subject)
	   d=%(*authordate)
	   b=%(*body)

	   kind=Tag
	   if test "z$t" = z
	   then
			   # could be a lightweight tag
			   t=%(objecttype)
			   kind="Lightweight tag"
			   o=%(objectname)
			   n=%(authorname)
			   e=%(authoremail)
			   s=%(subject)
			   d=%(authordate)
			   b=%(body)
	   fi
	   echo "$kind $T points at a $t object $o"
	   if test "z$t" = zcommit
	   then
			   echo "The commit was authored by $n $e
at $d, and titled

   $s

Its message reads as:
"
			   echo "$b" | sed -e "s/^/    /"
			   echo
	   fi
'

eval=`git for-each-ref --shell --format="$fmt" \
	   --sort='*objecttype' \
	   --sort=-taggerdate \
	   refs/tags`
eval "$eval"

下面的示例演示了%(if)…%(then)…%(else)…%(end)的使用,在当前的branch前显示星号:

#!/bin/bash

git for-each-ref --format="%(if)%(HEAD)%(then)* %(else)  %(end)%(refname:short)" refs/heads/

下面的示例演示了%(if)…%(then)…%(end)的使用,如果存在authorname则进行显示:

#!/bin/bash

git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Authored by: %(authorname)%(end)"

参考文献

man 1 git-for-each-ref, version Git 2.14.2
Git Manual/git-for-each-ref