<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="git-for-each-ref(1)">
<articleinfo>
    <title>git-for-each-ref(1)</title>
        <indexterm>
                <primary>git-for-each-ref(1)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>git-for-each-ref - Output information on each ref</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git for-each-ref</emphasis> [--count=&lt;count&gt;] [--shell|--perl|--python|--tcl]
                   [--sort=&lt;key&gt;]\* [--format=&lt;format&gt;] [&lt;pattern&gt;&#8230;]</literallayout>
</blockquote>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>Iterate over all refs that match <literal>&lt;pattern&gt;</literal> and show them
according to the given <literal>&lt;format&gt;</literal>, after sorting them according
to the given set of <literal>&lt;key&gt;</literal>.  If <literal>&lt;count&gt;</literal> is given, stop after
showing that many refs.  The interpolated values in <literal>&lt;format&gt;</literal>
can optionally be quoted as string literals in the specified
host language allowing their direct evaluation in that language.</simpara>
</simplesect>
<simplesect id="_options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
&lt;count&gt;
</term>
<listitem>
<simpara>
        By default the command shows all refs that match
        <literal>&lt;pattern&gt;</literal>.  This option makes it stop after showing
        that many refs.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;key&gt;
</term>
<listitem>
<simpara>
        A field name to sort on.  Prefix <literal>-</literal> to sort in
        descending order of the value.  When unspecified,
        <literal>refname</literal> is used.  You may use the --sort=&lt;key&gt; option
        multiple times, in which case the last key becomes the primary
        key.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;format&gt;
</term>
<listitem>
<simpara>
        A string that interpolates <literal>%(fieldname)</literal> from the
        object pointed at by a ref being shown.  If <literal>fieldname</literal>
        is prefixed with an asterisk (<literal>*</literal>) and the ref points
        at a tag object, the value for the field in the object
        tag refers is used.  When unspecified, defaults to
        <literal>%(objectname) SPC %(objecttype) TAB %(refname)</literal>.
        It also interpolates <literal>%%</literal> to <literal>%</literal>, and <literal>%xx</literal> where <literal>xx</literal>
        are hex digits interpolates to character with hex code
        <literal>xx</literal>; for example <literal>%00</literal> interpolates to <literal>\0</literal> (NUL),
        <literal>%09</literal> to <literal>\t</literal> (TAB) and <literal>%0a</literal> to <literal>\n</literal> (LF).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;pattern&gt;&#8230;
</term>
<listitem>
<simpara>
        If one or more patterns are given, only refs are shown that
        match against at least one pattern, either using fnmatch(3) or
        literally, in the latter case matching completely or from the
        beginning up to a slash.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--shell
</term>
<term>
--perl
</term>
<term>
--python
</term>
<term>
--tcl
</term>
<listitem>
<simpara>
        If given, strings that substitute <literal>%(fieldname)</literal>
        placeholders are quoted as string literals suitable for
        the specified host language.  This is meant to produce
        a scriptlet that can directly be `eval`ed.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_field_names">
<title>FIELD NAMES</title>
<simpara>Various values from structured fields in referenced objects can
be used to interpolate into the resulting output, or as sort
keys.</simpara>
<simpara>For all objects, the following names can be used:</simpara>
<variablelist>
<varlistentry>
<term>
refname
</term>
<listitem>
<simpara>
        The name of the ref (the part after $GIT_DIR/).
        For a non-ambiguous short name of the ref append <literal>:short</literal>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
objecttype
</term>
<listitem>
<simpara>
        The type of the object (<literal>blob</literal>, <literal>tree</literal>, <literal>commit</literal>, <literal>tag</literal>).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
objectsize
</term>
<listitem>
<simpara>
        The size of the object (the same as <emphasis>git-cat-file -s</emphasis> reports).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
objectname
</term>
<listitem>
<simpara>
        The object name (aka SHA-1).
</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>In addition to the above, for commit and tag objects, the header
field names (<literal>tree</literal>, <literal>parent</literal>, <literal>object</literal>, <literal>type</literal>, and <literal>tag</literal>) can
be used to specify the value in the header field.</simpara>
<simpara>Fields that have name-email-date tuple as its value (<literal>author</literal>,
<literal>committer</literal>, and <literal>tagger</literal>) can be suffixed with <literal>name</literal>, <literal>email</literal>,
and <literal>date</literal> to extract the named component.</simpara>
<simpara>The first line of the message in a commit and tag object is
<literal>subject</literal>, the remaining lines are <literal>body</literal>.  The whole message
is <literal>contents</literal>.</simpara>
<simpara>For sorting purposes, fields with numeric values sort in numeric
order (<literal>objectsize</literal>, <literal>authordate</literal>, <literal>committerdate</literal>, <literal>taggerdate</literal>).
All other fields are used to sort in their byte-value order.</simpara>
<simpara>In any case, a field name that refers to a field inapplicable to
the object referred by the ref does not cause an error.  It
returns an empty string instead.</simpara>
<simpara>As a special case for the date-type fields, you may specify a format for
the date by adding one of <literal>:default</literal>, <literal>:relative</literal>, <literal>:short</literal>, <literal>:local</literal>,
<literal>:iso8601</literal> or <literal>:rfc2822</literal> to the end of the fieldname; e.g.
<literal>%(taggerdate:relative)</literal>.</simpara>
</simplesect>
<simplesect id="_examples">
<title>EXAMPLES</title>
<simpara>An example directly producing formatted text.  Show the most recent
3 tagged commits::</simpara>
<literallayout>#!/bin/sh

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

%(*body)
' 'refs/tags'</literallayout>
<simpara>A simple example showing the use of shell eval on the output,
demonstrating the use of --shell.  List the prefixes of all heads::</simpara>
<literallayout>#!/bin/sh

git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
while read entry
do
        eval "$entry"
        echo `dirname $ref`
done</literallayout>
<simpara>A bit more elaborate report on tags, demonstrating that the format
may be an entire script::</simpara>
<literallayout>#!/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"</literallayout>
</simplesect>
</article>
