git rev-parse --git-path hooks always return absolute path
Asked Answered
L

2

6

Is there an equivalent command to this that always returns the absolute path?

git rev-parse --git-path hooks

When I'm in a submodule, I get an absolute path, but when I'm in the root repository, I get

.git/hooks

Limekiln answered 30/6, 2017 at 22:40 Comment(4)
The usual way to flesh out an arbitrary path is with readlink, path=$(readlink -e "`git rev-parse --git-path hooks`") should do it. If you wanted to cook up an --absolute-git-path like the --absolute-git-dir rev-parse arg I'd think a good patch would be accepted,Subak
I was trying --absolute-git-dir but having some problems. It would just print out --absolute-git-dir when I ran git rev-parse --absolute-git-dir. Any idea why? But the readlink solution does seem to work, thanks!Limekiln
It's a brand-new option, added in 2.13. btw, I think readlink -nf would be better than the readlink -e I showed above.Subak
With Git 2.31 (Q1 2021), you will have git rev-parse --path-format=absolute --git-path hooks. See my answer below.Causeuse
G
4

Git v2.13.0 has --absolute-git-dir:

$ git rev-parse --absolute-git-dir
/Users/torek/...snip.../.git

but not --absolute-git-path, and as you note, --git-path produces a relative result:

$ git rev-parse --git-path hooks
.git/hooks

If you do have Git 2.13, though, you can combine these using the sh/bash environment variable prefix method:

$ GIT_DIR=$(git rev-parse --absolute-git-dir) git rev-parse --git-path hooks
/Users/torek/...[snip].../.git/hooks

If not—if your Git is older than 2.13—you can use readlink -f:

$ GIT_DIR=$(readlink -f $(git rev-parse --git-dir)) git rev-parse --git-path hooks
/home/vagrant/...snip.../.git/hooks

(in a certain Linux image on my laptop; this particular Linux image has Git 2.7.4 installed).

Goingover answered 30/6, 2017 at 23:38 Comment(0)
C
0

With Git 2.31 (Q1 2021), "git rev-parse"(man) can be explicitly told to give output as absolute or relative path with the --path-format=(absolute|relative) option.

So if you want to be sure and have an absolute path:

git rev-parse --path-format=absolute --git-path hooks

See commit fac60b8, commit be6e0da (13 Dec 2020) by brian m. carlson (bk2204).
(Merged by Junio C Hamano -- gitster -- in commit 9ba366f, 15 Jan 2021)

rev-parse: add option for absolute or relative path formatting

Signed-off-by: brian m. carlson

git rev-parse(man) has several options which print various paths.
Some of these paths are printed relative to the current working directory, and some are absolute.

Normally, this is not a problem, but there are times when one wants paths entirely in one format or another.
This can be done trivially if the paths are canonical, but canonicalizing paths is not possible on some shell scripting environments which lack realpath(1) and also in Go, which lacks functions that properly canonicalize paths on Windows.

To help out the scripter, let's provide an option which turns most of the paths printed by git rev-parse to be either relative to the current working directory or absolute and canonical.
Document which options are affected and which are not so that users are not confused.

This approach is cleaner and tidier than providing duplicates of existing options which are either relative or absolute.

Note that if the user needs both forms, it is possible to pass an additional option in the middle of the command line which changes the behavior of subsequent operations.

git rev-parse now includes in its man page:

--path-format=(absolute|relative)

Controls the behavior of certain other options.

  • If specified as absolute, the paths printed by those options will be absolute and canonical.
  • If specified as relative, the paths will be relative to the current working directory if that is possible.
    The default is option specific.

This option may be specified multiple times and affects only the arguments that follow it on the command line, either to the end of the command line or the next instance of this option.

The following options are modified by --path-format:

git rev-parse now includes in its man page:

--show-toplevel

Show the (by default, absolute) path of the top-level directory of the working tree.
If there is no working tree, report an error.

git rev-parse now includes in its man page:

The following options are unaffected by --path-format:

--absolute-git-dir

Like --git-dir, but its output is always the canonicalized absolute path.

--is-inside-git-dir

When the current working directory is below the repository directory print "true", otherwise "false".

--is-inside-work-tree

When the current working directory is inside the work tree of the repository print "true", otherwise "false".

--is-bare-repository

When the repository is bare print "true", otherwise "false".

--is-shallow-repository

When the repository is shallow print "true", otherwise "false".

--show-cdup

When the command is invoked from a subdirectory, show the path of the top-level directory relative to the current directory (typically a sequence of "../", or an empty string).

--show-prefix

When the command is invoked from a subdirectory, show the path of the current directory relative to the top-level directory.


Warning: The "rev-parse" command did not diagnose the lack of argument to "--path-format" option, which was introduced in v2.31 era: that has been corrected with Git 2.32 (Q2 2021).

See commit 99fc555 (17 May 2021) by Wolfgang Müller (wylfen).
(Merged by Junio C Hamano -- gitster -- in commit 99fe1c6, 22 May 2021)

rev-parse: fix segfault with missing --path-format argument

Signed-off-by: Wolfgang Müller

Calling "git rev-parse --path-format"(man) without an argument segfaults instead of giving an error message.
Commit fac60b8 ("rev-parse: add option for absolute or relative path formatting", 2020-12-13, Git v2.31.0-rc0 -- merge listed in batch #2) added the argument parsing code but forgot to handle NULL.

Returning an error makes sense here because there is no default value we could use.
Add a test case to verify.

The error message will now accurately be:

fatal: --path-format requires an argument
Causeuse answered 17/1, 2021 at 2:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.