threads / discuss / 31788

How can I tell if anything was fetched?

Subject: How can I tell if anything was fetched?

## tl;dr

5 messages between Oct 11, 2012 and Oct 12, 2012.

replies: 4people: 5as markdown or json

Phil Lawrence· Oct 11, 2012, 16:25 UTC · lore

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· Oct 11, 2012, 16:45 UTC · re: Phil Lawrence · lore

Re: How can I tell if anything was fetched?

Phil Lawrence <prlawrence@gmail.com> writes:
Show 11 quoted lines
> 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· Oct 11, 2012, 18:29 UTC · re: Andreas Schwab · lore

Re: How can I tell if anything was fetched?

> 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 
Jeff King· Oct 12, 2012, 07:47 UTC · re: Andreas Schwab · lore

Re: How can I tell if anything was fetched?

On Thu, Oct 11, 2012 at 06:45:09PM +0200, Andreas Schwab wrote:
Show 16 quoted lines
> 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
Johannes Sixt· Oct 12, 2012, 06:28 UTC · re: Phil Lawrence · lore

Re: How can I tell if anything was fetched?

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

← back to recent threads