Home Explore Blog CI



git

7th chunk of `Documentation/git-for-each-ref.adoc`
0b1681cb00ee7581803500ebc1b8889f67199b258285a9930000000100000c4f
 references will be sorted according to the byte-value of the
formatted string rather than the numeric value of the underlying timestamp.

Some atoms like %(align) and %(if) always require a matching %(end).
We call them "opening atoms" and sometimes denote them as %($open).

When a scripting language specific quoting is in effect, everything
between a top-level opening atom and its matching %(end) is evaluated
according to the semantics of the opening atom and only its result
from the top-level is quoted.


EXAMPLES
--------

An example directly producing formatted text.  Show the most recent
3 tagged commits:

------------
#!/bin/sh

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

%(*body)
' 'refs/tags'
------------


A simple example showing the use of shell eval on the output,
demonstrating the use of --shell.  List the prefixes of all heads:

------------
#!/bin/sh

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


A bit more elaborate report on tags, demonstrating that the format
may be an entire script:

------------
#!/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"
------------


An example to show the usage of %(if)...%(then)...%(else)...%(end).
This prefixes the current branch with a star.

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


An example to show the usage of %(if)...%(then)...%(end).
This prints the authorname, if present.

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

CAVEATS
-------

Note that the sizes of objects on disk are reported accurately, but care
should be taken in drawing conclusions about which refs or objects are
responsible for disk usage. The size of a packed non-delta object may be
much larger than the size of objects which delta against it, but the
choice of which object is the base and which is the delta is arbitrary
and is subject to change during a repack.

Note also that multiple copies of an object may be present in the object
database; in this case, it is undefined which copy's size or delta base
will be reported.

NOTES
-----

include::ref-reachability-filters.adoc[]

SEE ALSO
--------
linkgit:git-show-ref[1]

GIT
---
Part of the linkgit:git[1] suite

Title: Git For-Each-Ref Command Examples and Caveats
Summary
This section provides various examples of using the Git for-each-ref command to format and evaluate output, including sorting, scripting, and conditional statements. It also discusses caveats related to object sizes and disk usage, and references additional documentation and related commands.