# How can I tell if anything was fetched?

5 messages from 2012-10-11 to 2012-10-12. Participants: Phil Lawrence, Andreas Schwab, Enrico Weigelt, Johannes Sixt, Jeff King.
Thread: https://gitlist.dev/t/31788

## Phil Lawrence, 2012-10-11 16:25

Subject: How can I tell if anything was fetched?
Message-ID: <CAFRB3XkueafikCvwzBb+WOAG_1mEkVg1Yt6=teOuVwYd2zWVOw@mail.gmail.com>
URL: https://gitlist.dev/e/CAFRB3XkueafikCvwzBb%2BWOAG_1mEkVg1Yt6%3DteOuVwYd2zWVOw%40mail.gmail.com

```
I have a cron job that regularly fetches into a bare repo.  Currently,
the job then pushes the repo to another remote, regardless of whether
or not anything was actually fetched.  That's a waste.

What is the best way to know whether or not anything was received
during the fetch?  I don't want to be stuck trying to parse the answer
out of STDOUT and STDERR...

One approach might be to first generate a state-of-the-repo SHA1:
    # http://stackoverflow.com/a/7350019/834039
    # http://git-scm.com/book/ch9-2.html
    {
        git rev-list --objects --all
        git rev-list --objects -g --no-walk --all
        git rev-list --objects --no-walk \
            $(git fsck --unreachable |
              grep '^unreachable commit' |
              cut -d' ' -f3)
    } | sort | uniq | git hash-object -w --stdin

I could then do the git fetch, generate another state-of-the-repo SHA1
and compare...

Thoughts?

--
Phil Lawrence

```

## Andreas Schwab, 2012-10-11 16:45

Subject: Re: How can I tell if anything was fetched?
Message-ID: <m28vbdkley.fsf@igel.home>
URL: https://gitlist.dev/e/m28vbdkley.fsf%40igel.home
In-Reply-To: <CAFRB3XkueafikCvwzBb+WOAG_1mEkVg1Yt6=teOuVwYd2zWVOw@mail.gmail.com>

```
Phil Lawrence <prlawrence@gmail.com> writes:

> One approach might be to first generate a state-of-the-repo SHA1:
>     # http://stackoverflow.com/a/7350019/834039
>     # http://git-scm.com/book/ch9-2.html
>     {
>         git rev-list --objects --all
>         git rev-list --objects -g --no-walk --all
>         git rev-list --objects --no-walk \
>             $(git fsck --unreachable |
>               grep '^unreachable commit' |
>               cut -d' ' -f3)
>     } | sort | uniq | git hash-object -w --stdin

I think you'd only need to record the state of all refs (eg. the output
of `git for-each-ref') to reliably detect any changes.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## Enrico Weigelt, 2012-10-11 18:29

Subject: Re: How can I tell if anything was fetched?
Message-ID: <369f3feb-20e5-444f-b718-15d7dc8f2c61@zcs>
URL: https://gitlist.dev/e/369f3feb-20e5-444f-b718-15d7dc8f2c61%40zcs
In-Reply-To: <m28vbdkley.fsf@igel.home>

```

> I think you'd only need to record the state of all refs (eg. the
> output
> of `git for-each-ref') to reliably detect any changes.

I would just record the output of `git ls-remote . | sort -u` somewhere
and compare it next time (maybe you even wanna grep for the desired
ref namespaces).


cu
-- 
Mit freundlichen Grüßen / Kind regards 

Enrico Weigelt 
VNC - Virtual Network Consult GmbH 
Head Of Development 

Pariser Platz 4a, D-10117 Berlin
Tel.: +49 (30) 3464615-20
Fax: +49 (30) 3464615-59

enrico.weigelt@vnc.biz; www.vnc.de 

```

## Johannes Sixt, 2012-10-12 06:28

Subject: Re: How can I tell if anything was fetched?
Message-ID: <5077B88E.5060303@viscovery.net>
URL: https://gitlist.dev/e/5077B88E.5060303%40viscovery.net
In-Reply-To: <CAFRB3XkueafikCvwzBb+WOAG_1mEkVg1Yt6=teOuVwYd2zWVOw@mail.gmail.com>

```
Am 10/11/2012 18:25, schrieb Phil Lawrence:
> What is the best way to know whether or not anything was received
> during the fetch?  I don't want to be stuck trying to parse the answer
> out of STDOUT and STDERR...

what="--all"	 # or "--remotes=the-remote"
presha1s=$(git rev-parse $what)
git fetch the-remote
if test -z "$(git rev-list -1 $what --not $presha1s)"
then
	echo nothing new
fi

Error checking is left as an exercise for the user ;)

-- Hannes

```

## Jeff King, 2012-10-12 07:47

Subject: Re: How can I tell if anything was fetched?
Message-ID: <20121012074739.GD17026@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20121012074739.GD17026%40sigill.intra.peff.net
In-Reply-To: <m28vbdkley.fsf@igel.home>

```
On Thu, Oct 11, 2012 at 06:45:09PM +0200, Andreas Schwab wrote:

> Phil Lawrence <prlawrence@gmail.com> writes:
> 
> > One approach might be to first generate a state-of-the-repo SHA1:
> >     # http://stackoverflow.com/a/7350019/834039
> >     # http://git-scm.com/book/ch9-2.html
> >     {
> >         git rev-list --objects --all
> >         git rev-list --objects -g --no-walk --all
> >         git rev-list --objects --no-walk \
> >             $(git fsck --unreachable |
> >               grep '^unreachable commit' |
> >               cut -d' ' -f3)
> >     } | sort | uniq | git hash-object -w --stdin
> 
> I think you'd only need to record the state of all refs (eg. the output
> of `git for-each-ref') to reliably detect any changes.

To clarify on the "only" in your sentence: it is not just that recording
the ref changes is more efficient. But recording the object state is
actively wrong. You might have fetched ref changes that do not introduce
new objects (e.g., a rewind of history, or a branch deletion). The
before-and-after ref state is both sufficient and necessary.  I suspect
you already realized that, Andreas, but I wanted to make sure it was
clear to readers, including the OP.

Checking ref state does suffer from potential race conditions with any
other simultaneous updates.  I don't think there is any reason we
couldn't have "git fetch" produce a machine-readable output detailing
what happened. We already have the same for "git push". I think it is
simply the case that nobody has really wanted it so far.

-Peff

```
