rev-parse options for absolute or relative paths
v5 cover rev-parse options for absolute or relative paths
There are a bunch of different situations in which one would like to have an absolute and canonical or a relative path from Git. In many of these cases, these values are already available from git rev-parse, but some values only come in one form or another.
Changes from v4:
* Wrap an internal function.
* Use bitflags.
* Fix memory leaks.
* Explain that --path-format can be specified multiple times and its effects if that is done.
* Expand commit message.
Changes from v3:
* Switch to a binary option instead of a number of missing path components to skip.
Changes from v2:
* Incorporate multiple missing segment support into the strbuf_realpath code.
* Switch some invocations to use DEFAULT_UNMODIFIED, which should not result in a change in behavior.
* Rebase, resolving some conflicts.
Changes from v1:
* Add a function to handle missing trailing components when canonicalizing paths and use it.
* Improve commit messages.
* Fix broken && chain.
* Fix situation where relative paths are not relative.
brian m. carlson (2):
abspath: add a function to resolve paths with missing components
rev-parse: add option for absolute or relative path formatting
Documentation/git-rev-parse.txt | 74 +++++++++++++--------- abspath.c | 64 +++++++++++++------ builtin/rev-parse.c | 106 ++++++++++++++++++++++++++++---- cache.h | 2 + t/t1500-rev-parse.sh | 57 ++++++++++++++++- 5 files changed, 242 insertions(+), 61 deletions(-)
PATCH v5 1/2 abspath: add a function to resolve paths with missing components
Currently, we have a function to resolve paths, strbuf_realpath. This function canonicalizes paths like realpath(3), but permits a trailing component to be absent from the file system. In other words, this is the behavior of the GNU realpath(1) without any arguments.
In the future, we'll need this same behavior, except that we want to allow for any number of missing trailing components, which is the behavior of GNU realpath(1) with the -m option. This is useful because we'll want to canonicalize a path that may point to a not yet present path under the .git directory. For example, a user may want to know where an arbitrary ref would be stored if it existed in the file system.
Let's refactor strbuf_realpath to move most of the code to an internal function and then pass it two flags to control its behavior. We'll add a strbuf_realpath_forgiving function that has our new behavior, and leave strbuf_realpath with the older, stricter behavior.
--- abspath.c | 64 +++++++++++++++++++++++++++++++++++++++---------------- cache.h | 2 ++ 2 files changed, 48 insertions(+), 18 deletions(-)
abspath.c +46 -18
@@ -67,19 +67,15 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)#endif /* -* Return the real path (i.e., absolute path, with symlinks resolved -* and extra slashes removed) equivalent to the specified path. (If -* you want an absolute path but don't mind links, use -* absolute_path().) Places the resolved realpath in the provided strbuf. -* -* The directory part of path (i.e., everything up to the last -* dir_sep) must denote a valid, existing directory, but the last -* component need not exist. If die_on_error is set, then die with an -* informative error message if there is a problem. Otherwise, return -* NULL on errors (without generating any output). +* If set, any number of trailing components may be missing; otherwise, only one +* may be. */ -char *strbuf_realpath (struct strbuf *resolved,const char *path, -int die_on_error) +#define REALPATH_MANY_MISSING (1 << 0) +/* Should we die if there's an error? */ +#define REALPATH_DIE_ON_ERROR (1 << 1) + +static char *strbuf_realpath_1 (struct strbuf *resolved,const char *path, +int flags) {struct strbuf remaining =STRBUF_INIT ;struct strbuf next =STRBUF_INIT ;
@@ -89,7 +85,7 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,struct stat st;if (!*path) { -if (die_on_error) +if (flags &REALPATH_DIE_ON_ERROR )die ("The empty string is not a valid path" );else goto error_out;
@@ -101,7 +97,7 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,if (!resolved->len) {/* relative path; can use CWD as the initial resolved path */ if (strbuf_getcwd (resolved)) { -if (die_on_error) +if (flags &REALPATH_DIE_ON_ERROR )die_errno ("unable to get current working directory" );else goto error_out;
@@ -129,8 +125,9 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,if (lstat (resolved->buf, &st)) {/* error out unless this was the last component */ -if (errno !=ENOENT || remaining.len) { -if (die_on_error) +if (errno !=ENOENT || + (!(flags &REALPATH_MANY_MISSING ) && remaining.len)) { +if (flags &REALPATH_DIE_ON_ERROR )die_errno ("Invalid path '%s'" , resolved->buf);else
@@ -143,7 +140,7 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,if (num_symlinks++ >MAXSYMLINKS ) { errno =ELOOP ; -if (die_on_error) +if (flags &REALPATH_DIE_ON_ERROR )die ("More than %d nested symlinks " "on path '%s'" ,MAXSYMLINKS , path);else
@@ -153,7 +150,7 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path, len =strbuf_readlink (&symlink, resolved->buf, st.st_size);if (len <0 ) { -if (die_on_error) +if (flags &REALPATH_DIE_ON_ERROR )die_errno ("Invalid symlink '%s'" , resolved->buf);else
@@ -202,6 +199,37 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,return retval; } +/* +* Return the real path (i.e., absolute path, with symlinks resolved +* and extra slashes removed) equivalent to the specified path. (If +* you want an absolute path but don't mind links, use +* absolute_path().) Places the resolved realpath in the provided strbuf. +* +* The directory part of path (i.e., everything up to the last +* dir_sep) must denote a valid, existing directory, but the last +* component need not exist. If die_on_error is set, then die with an +* informative error message if there is a problem. Otherwise, return +* NULL on errors (without generating any output). +*/ +char *strbuf_realpath (struct strbuf *resolved,const char *path, +int die_on_error) +{ +return strbuf_realpath_1 (resolved, path, + die_on_error ?REALPATH_DIE_ON_ERROR :0 ); +} + +/* +* Just like strbuf_realpath, but allows an arbitrary number of path +* components to be missing. +*/ +char *strbuf_realpath_forgiving (struct strbuf *resolved,const char *path, +int die_on_error) +{ +return strbuf_realpath_1 (resolved, path, + ((die_on_error ?REALPATH_DIE_ON_ERROR :0 ) | +REALPATH_MANY_MISSING )); +} +char *real_pathdup (const char *path,int die_on_error) {struct strbuf realpath =STRBUF_INIT ;
cache.h +2 -0
@@ -1325,6 +1325,8 @@ static inline int is_absolute_path(const char *path)int is_directory (const char *);char *strbuf_realpath (struct strbuf *resolved,const char *path,int die_on_error); +char *strbuf_realpath_forgiving (struct strbuf *resolved,const char *path, +int die_on_error);char *real_pathdup (const char *path,int die_on_error);const char *absolute_path (const char *path);char *absolute_pathdup (const char *path);
Re: [PATCH v5 1/2] abspath: add a function to resolve paths with missing components
Quoted 11 lines · brian m. carlson
Currently, we have a function to resolve paths,
strbuf_realpath. This function canonicalizes paths like realpath(3), but permits a trailing component to be absent from the file system. In other words, this is the behavior of the GNU realpath(1) without any arguments.In the future, we'll need this same behavior, except that we want to allow for any number of missing trailing components, which is the behavior of GNU realpath(1) with the -m option. This is useful because we'll want to canonicalize a path that may point to a not yet present path under the .git directory. For example, a user may want to know where an arbitrary ref would be stored if it existed in the file system.
This improved explanation helps the reader better understand why such functionality is wanted. Thanks.
abspath.c static void get_root_part(struct strbuf *resolved, struct strbuf *remaining) · line 78
Quoted 13 lines
Let's refactor
strbuf_realpathto move most of the code to an internal function and then pass it two flags to control its behavior. We'll add astrbuf_realpath_forgivingfunction that has our new behavior, and leavestrbuf_realpathwith the older, stricter behavior.Signed-off-by brian m. carlson <[email protected]>---
abspath.c +5
-0+#define REALPATH_MANY_MISSING (1 << 0) +#define REALPATH_DIE_ON_ERROR (1 << 1) + +static char *strbuf_realpath_1 (struct strbuf *resolved,const char *path, +int flags)
We normally declare these composed-of-bits "flags" arguments as unsigned rather than int.
abspath.c char *strbuf_realpath(struct strbuf *resolved, const char *path, · line 86
Quoted 4 lines
abspath.c +1
-1@@ -89,7 +85,7 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,if (!*path) { -if (die_on_error) +if (flags &REALPATH_DIE_ON_ERROR )
Nit: If you declare a local variable named die_on_error:
int die_on_error = flags & REALPATH_DIE_ON_ERROR;
then you won't have to touch the existing five places where this condition is checked, thus making the diff less noisy and the code (subjectively) a bit easier to read at a glance. Not at all worth a re-roll, though.
Aside from these minor comments, this version is a nice improvement over the last.
PATCH v5 2/2 rev-parse: add option for absolute or relative path formatting
In reply to brian m. carlsongit 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.
--- Documentation/git-rev-parse.txt | 74 +++++++++++++--------- builtin/rev-parse.c | 106 ++++++++++++++++++++++++++++---- t/t1500-rev-parse.sh | 57 ++++++++++++++++- 3 files changed, 194 insertions(+), 43 deletions(-)
Documentation/git-rev-parse.txt +44 -30
@@ -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,13 +233,42 @@ 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. +--git-common-dir:: + Show `$GIT_COMMON_DIR` if defined, else `$GIT_DIR`. + +--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 + repository. If <path> is a gitfile then the resolved path + to the real repository is printed. + +--git-path <path>:: + Resolve "$GIT_DIR/<path>" and takes other path relocation + variables such as $GIT_OBJECT_DIRECTORY, + $GIT_INDEX_FILE... into account. For example, if + $GIT_OBJECT_DIRECTORY is set to /foo/bar then "git rev-parse + --git-path objects/abc" returns /foo/bar/abc. + +--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. + +--show-superproject-working-tree:: + Show the absolute path of the root of the superproject's + working tree (if exists) that uses the current repository as + its submodule. Outputs nothing if the current repository is + not used as a submodule by any project. + +--shared-index-path:: + 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. ---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".
@@ -242,19 +283,6 @@ print a message to stderr and exit with nonzero status. --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 - repository. If <path> is a gitfile then the resolved path - to the real repository is printed. - ---git-path <path>:: - Resolve "$GIT_DIR/<path>" and takes other path relocation - variables such as $GIT_OBJECT_DIRECTORY, - $GIT_INDEX_FILE... into account. For example, if - $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
@@ -265,20 +293,6 @@ print a message to stderr and exit with nonzero status. 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-superproject-working-tree:: - Show the absolute path of the root of the superproject's - working tree (if exists) that uses the current repository as - its submodule. Outputs nothing if the current repository is - not used as a submodule by any project. - ---shared-index-path:: - Show the path to the shared index file in split index mode, or - empty if not in split-index mode. - --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
builtin/rev-parse.c +94 -12
172 lines not on this page · raw message
t/t1500-rev-parse.sh +56 -1
75 lines not on this page · raw message