for-each-ref: avoid loading objects to print %(objectname)
commitb74cf648036af764a176898763a0ec066a77a92f
authorJeff King <peff@peff.net>
Wed, 30 Oct 2013 06:50:16 +0000 (30 02:50 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 30 Oct 2013 17:33:46 +0000 (30 10:33 -0700)
treea5106f3db9eb220d882365d46f504c85b5be861b
parente230c568c4b9a991e3175e5f65171a566fd8e39c
for-each-ref: avoid loading objects to print %(objectname)

If you ask for-each-ref to print each ref and its object,
like:

  git for-each-ref --format='%(objectname) %(refname)'

this should involve little more work than looking at the ref
files (and packed-refs) themselves. However, for-each-ref
will actually load each object from disk just to print its
sha1. For most repositories, this isn't a big deal, but it
can be noticeable if you have a large number of refs to
print. Here are best-of-five timings for the command above
on a repo with ~10K refs:

  [before]
  real    0m0.112s
  user    0m0.092s
  sys     0m0.016s

  [after]
  real    0m0.014s
  user    0m0.012s
  sys     0m0.000s

This patch checks for %(objectname) and %(objectname:short)
before we actually parse the object (and the rest of the
code is smart enough to avoid parsing if we have filled all
of our placeholders).

Note that we can't simply move the objectname parsing code
into the early loop. If the "deref" form %(*objectname) is
used, then we do need to parse the object in order to peel
the tag. So instead of moving the code, we factor it out
into a separate function that can be called for both cases.

While we're at it, we add some basic tests for the
dereferenced placeholders, which were not tested at all
before. This helps ensure we didn't regress that case.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/for-each-ref.c
t/t6300-for-each-ref.sh