rev-parse: add option for absolute or relative path formatting
git rev-parse 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.
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
brian m. carlson4 years agocommitted byJunio C Hamano
@ -212,6 +212,18 @@ Options for Files
@@ -212,6 +212,18 @@ Options for Files
Only the names of the variables are listed, not their value,
even if they are set.
--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-dir::
Show `$GIT_DIR` if defined. Otherwise show the path to
the .git directory. The path shown, when relative, is
@ -221,27 +233,9 @@ If `$GIT_DIR` is not defined and the current directory
@@ -221,27 +233,9 @@ If `$GIT_DIR` is not defined and the current directory
is not detected to lie in a Git repository or work tree
print a message to stderr and exit with nonzero status.
--absolute-git-dir::
Like `--git-dir`, but its output is always the canonicalized
absolute path.
--git-common-dir::
Show `$GIT_COMMON_DIR` if defined, else `$GIT_DIR`.
--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".
--resolve-git-dir <path>::
Check if <path> is a valid repository or a gitfile that
points at a valid repository, and print the location of the
@ -255,19 +249,9 @@ print a message to stderr and exit with nonzero status.
@@ -255,19 +249,9 @@ print a message to stderr and exit with nonzero status.
$GIT_OBJECT_DIRECTORY is set to /foo/bar then "git rev-parse
--git-path objects/abc" returns /foo/bar/abc.
--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.
--show-toplevel::
Show the absolute path of the top-level directory of the working
tree. If there is no working tree, report an error.
Show the (by default, absolute) path of the top-level directory
of the working tree. If there is no working tree, report an error.
--show-superproject-working-tree::
Show the absolute path of the root of the superproject's
@ -279,6 +263,36 @@ print a message to stderr and exit with nonzero status.
@@ -279,6 +263,36 @@ print a message to stderr and exit with nonzero status.
Show the path to the shared index file in split index mode, or
empty if not in split-index mode.
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.
--show-object-format[=(storage|input|output)]::
Show the object format (hash algorithm) used for the repository
for storage inside the `.git` directory, input, or output. For
@ -88,6 +104,45 @@ test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = tru
@@ -88,6 +104,45 @@ test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = tru
test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''