# `git rev-parse --git-dir` relative to current working directory?

2 messages from 2016-03-29 to 2016-03-29. Participants: Elliott Cable, John Keeping.
Thread: https://gitlist.dev/t/41855

## Elliott Cable, 2016-03-29 10:32

Subject: `git rev-parse --git-dir` relative to current working directory?
Message-ID: <CAPZ477NfQ7pCiHQ3V42kZ1Cic5UPP03TCFPvABR_ugSZYEn4xg@mail.gmail.com>
URL: https://gitlist.dev/e/CAPZ477NfQ7pCiHQ3V42kZ1Cic5UPP03TCFPvABR_ugSZYEn4xg%40mail.gmail.com

```
So, `git help rev-parse` [mentions the following][rev-parse], as of
2.8.0:

    --git-dir
       Show $GIT_DIR if defined. Otherwise show the path to the .git
       directory. The path shown, when relative, is relative to the
       current working directory.

However, when inside a symlinked repository, this doesn't function as
advertised:

    $ ln -s a-symlink a-git-repo
    $ cd a-symlink/.git/hooks
    $ git rev-parse --git-dir
    /Users/ec/Documents/a-git-repo/.git

From my reading of that snippet of documentation (“The path shown ... is
relative to the CWD”), I'd expect to receive `..`, not
`/absolute/path/to/a-git-repo/.git`.

Is the documentation incorrect, or is this a bug? (I'm hoping the
latter: I'm trying to write git-scripting that is sensitive to symlinks,
i.e. retains the user's CWD without unintentionally resolving symlinks
in their path during operation; and it'd be ideal if this were handled
as documented, saving me manual effort checking symlinks.)


⁓ ELLIOTTCABLE — fly safe.
  http://ell.io/tt

   [rev-parse]:
<https://git-scm.com/docs/git-rev-parse/2.8.0#_options_for_files>
      "git rev-parse documentation @2.8.0"

```

## John Keeping, 2016-03-29 10:50

Subject: Re: `git rev-parse --git-dir` relative to current working directory?
Message-ID: <20160329105014.GA1578@serenity.lan>
URL: https://gitlist.dev/e/20160329105014.GA1578%40serenity.lan
In-Reply-To: <CAPZ477NfQ7pCiHQ3V42kZ1Cic5UPP03TCFPvABR_ugSZYEn4xg@mail.gmail.com>

```
On Tue, Mar 29, 2016 at 05:32:31AM -0500, Elliott Cable wrote:
> So, `git help rev-parse` [mentions the following][rev-parse], as of
> 2.8.0:
> 
>     --git-dir
>        Show $GIT_DIR if defined. Otherwise show the path to the .git
>        directory. The path shown, when relative, is relative to the
>        current working directory.
> 
> However, when inside a symlinked repository, this doesn't function as
> advertised:
> 
>     $ ln -s a-symlink a-git-repo
>     $ cd a-symlink/.git/hooks
>     $ git rev-parse --git-dir
>     /Users/ec/Documents/a-git-repo/.git
> 
> From my reading of that snippet of documentation (“The path shown ... is
> relative to the CWD”), I'd expect to receive `..`, not
> `/absolute/path/to/a-git-repo/.git`.
> 
> Is the documentation incorrect, or is this a bug? (I'm hoping the
> latter: I'm trying to write git-scripting that is sensitive to symlinks,
> i.e. retains the user's CWD without unintentionally resolving symlinks
> in their path during operation; and it'd be ideal if this were handled
> as documented, saving me manual effort checking symlinks.)

The documentation seems correct to me, it just requires careful parsing;
I read it as:

	if the path printed it relative
	then it is relative to the current working directory

but it makes not claims about when a relative path will be printed (or
even if one ever will be, although in my testing the path is relative
only if $CWD can be stripped from the beginning of the path; in other
words only when no component of the relative path would be "../").

```
