# Determining if a file exists in a bare repo

5 messages from 2010-05-04 to 2010-05-04. Participants: Adam Mercer, Jacob Helwig, Junio C Hamano.
Thread: https://gitlist.dev/t/23696

## Adam Mercer, 2010-05-04 16:11

Subject: Determining if a file exists in a bare repo
Message-ID: <w2q799406d61005040911p8fd7c234s5e6382298129985f@mail.gmail.com>
URL: https://gitlist.dev/e/w2q799406d61005040911p8fd7c234s5e6382298129985f%40mail.gmail.com

```
Hi

I'm trying to write a post-receive hook that generates some HTML files
from reStructured text files stored in a repository. Essentially I'm
doing this with

git show master:INSTALL | rst2html --no-raw --no-file-insertion >
/path/to/INSTALL.html

However I would like this script to fail gracefully if the INSTALL
file is not available in the repository so would like to check if this
file exists. The problem I'm having is that git-show seems to return a
zero return code even if the file you request doesn't exist, e.g.:

$ git show master:NoneExistantFile
fatal: ambiguous argument 'master:NoneExistantFile': unknown revision
or path not in the working tree.
Use '--' to separate paths from revisions
$ echo $?
0
$

is there another, scriptable, way to determine if a given file exists
in a given branch of a bare repository?

Cheers

Adam

```

## Jacob Helwig, 2010-05-04 16:18

Subject: Re: Determining if a file exists in a bare repo
Message-ID: <h2z8c9a061005040918s33bd5020sa89e95aedb7b8cb@mail.gmail.com>
URL: https://gitlist.dev/e/h2z8c9a061005040918s33bd5020sa89e95aedb7b8cb%40mail.gmail.com
In-Reply-To: <w2q799406d61005040911p8fd7c234s5e6382298129985f@mail.gmail.com>

```
On Tue, May 4, 2010 at 09:11, Adam Mercer <ramercer@gmail.com> wrote:
> Hi
>
> I'm trying to write a post-receive hook that generates some HTML files
> from reStructured text files stored in a repository. Essentially I'm
> doing this with
>
> git show master:INSTALL | rst2html --no-raw --no-file-insertion >
> /path/to/INSTALL.html
>
> However I would like this script to fail gracefully if the INSTALL
> file is not available in the repository so would like to check if this
> file exists. The problem I'm having is that git-show seems to return a
> zero return code even if the file you request doesn't exist, e.g.:
>
> $ git show master:NoneExistantFile
> fatal: ambiguous argument 'master:NoneExistantFile': unknown revision
> or path not in the working tree.
> Use '--' to separate paths from revisions
> $ echo $?
> 0
> $
>
> is there another, scriptable, way to determine if a given file exists
> in a given branch of a bare repository?
>
> Cheers
>
> Adam

What about using "git ls-tree master" to get a file listing?

```

## Adam Mercer, 2010-05-04 16:26

Subject: Re: Determining if a file exists in a bare repo
Message-ID: <o2k799406d61005040926h6d9b56f5ge48378822ec46280@mail.gmail.com>
URL: https://gitlist.dev/e/o2k799406d61005040926h6d9b56f5ge48378822ec46280%40mail.gmail.com
In-Reply-To: <h2z8c9a061005040918s33bd5020sa89e95aedb7b8cb@mail.gmail.com>

```
On Tue, May 4, 2010 at 11:18, Jacob Helwig <jacob.helwig@gmail.com> wrote:

> What about using "git ls-tree master" to get a file listing?

Good idea, thanks that seems to do the trick.

Cheers

Adam

```

## Junio C Hamano, 2010-05-04 16:39

Subject: Re: Determining if a file exists in a bare repo
Message-ID: <7v7hnjoc3i.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v7hnjoc3i.fsf%40alter.siamese.dyndns.org
In-Reply-To: <w2q799406d61005040911p8fd7c234s5e6382298129985f@mail.gmail.com>

```
Adam Mercer <ramercer@gmail.com> writes:

> Hi
>
> I'm trying to write a post-receive hook that generates some HTML files
> from reStructured text files stored in a repository. Essentially I'm
> doing this with
>
> git show master:INSTALL | rst2html --no-raw --no-file-insertion >
> /path/to/INSTALL.html
>
> However I would like this script to fail gracefully if the INSTALL
> file is not available in the repository so would like to check if this
> file exists. The problem I'm having is that git-show seems to return a
> zero return code even if the file you request doesn't exist

Even if it returned an error status, you are discarding it by placing the
process on the upstream side of the pipe, so your command line above won't
be able to catch an error anyway.  I would probably do something like this
if I were you:

    git rev-parse --verify master:INSTALL >/dev/null 2>&1 &&
    git cat-file blob master:INSTALL | rst2...

```

## Adam Mercer, 2010-05-04 16:57

Subject: Re: Determining if a file exists in a bare repo
Message-ID: <z2p799406d61005040957j93f8f4a3zfb8a9f38dbe7c72c@mail.gmail.com>
URL: https://gitlist.dev/e/z2p799406d61005040957j93f8f4a3zfb8a9f38dbe7c72c%40mail.gmail.com
In-Reply-To: <7v7hnjoc3i.fsf@alter.siamese.dyndns.org>

```
On Tue, May 4, 2010 at 11:39, Junio C Hamano <gitster@pobox.com> wrote:

> Even if it returned an error status, you are discarding it by placing the
> process on the upstream side of the pipe, so your command line above won't
> be able to catch an error anyway.

Sorry I wasn't very clear, first I trying to use git-show if the file
existed and then piping the output to rst2html...

> I would probably do something like this
> if I were you:
>
>    git rev-parse --verify master:INSTALL >/dev/null 2>&1 &&
>    git cat-file blob master:INSTALL | rst2...

Thanks, thats a better approach!

Cheers

Adam

```
