rev-parse options for absolute or relative paths
v3 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.
Many operating systems, such as macOS, lack a built-in realpath command that can canonicalize paths properly, and additionally some programming languages, like Go, currently do as well. It's therefore helpful for us to provide a generic way to request that a path is fully canonicalized before using it. Since users may wish for a relative path, we can provide one of those as well.
The primary impetus for this was Git LFS, which needs paths to be canonicalized in the same way as Git in order to function correctly. See https://github.com/git-lfs/git-lfs/issues/4012 for more details.
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 | 71 +++++++++++++--------- abspath.c | 33 +++++++++- builtin/rev-parse.c | 104 ++++++++++++++++++++++++++++---- cache.h | 2 + t/t1500-rev-parse.sh | 57 ++++++++++++++++- 5 files changed, 223 insertions(+), 44 deletions(-)
PATCH v3 1/2 abspath: add a function to resolve paths with missing components
We'd like to canonicalize paths such that we can preserve any number of trailing components that may be missing. Let's add a function to do that, taking the number of components to canonicalize, and make strbuf_realpath a wrapper around it that allows just one component.
--- abspath.c | 33 ++++++++++++++++++++++++++++++++- cache.h | 2 ++ 2 files changed, 34 insertions(+), 1 deletion(-)
abspath.c +32 -1
@@ -20,6 +20,7 @@ static void strip_last_component(struct strbuf *path)/* Find start of the last component */ while (offset < len && !is_dir_sep (path->buf[len -1 ])) len--; +/* Skip sequences of multiple path-separators */ while (offset < len &&is_dir_sep (path->buf[len -1 ])) len--;
@@ -66,6 +67,22 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)#define MAXSYMLINKS 32#endif +/* Count non-contiguous directory separators, not including a trailing one. */ +static int count_dir_separators (const char *s) +{ +int count =0 ; +int last_sep =0 ; +const char *p = s; +while (*p) { +int is_sep =is_dir_sep (*p++); +if (is_sep && !last_sep) + count++; + last_sep = is_sep; + } +return count; +} + +/* * Return the real path (i.e., absolute path, with symlinks resolved * and extra slashes removed) equivalent to the specified path. (If
@@ -80,6 +97,16 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)*/ char *strbuf_realpath (struct strbuf *resolved,const char *path,int die_on_error) +{ +return strbuf_realpath_missing (resolved, path,1 , die_on_error); +} + +/* +* Just like strbuf_realpath, but allows specifying how many missing components +* are permitted. -1 may be specified to allow an unlimited number. +*/ +char *strbuf_realpath_missing (struct strbuf *resolved,const char *path, +int missing_components,int die_on_error) {struct strbuf remaining =STRBUF_INIT ;struct strbuf next =STRBUF_INIT ;
@@ -128,8 +155,12 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,strbuf_addbuf (resolved, &next);if (lstat (resolved->buf, &st)) { +int trailing_components =count_dir_separators (remaining.buf) + + (remaining.len !=0 );/* error out unless this was the last component */ -if (errno !=ENOENT || remaining.len) { +if (errno !=ENOENT || + !(missing_components ==-1 || + trailing_components < missing_components)) {if (die_on_error)die_errno ("Invalid path '%s'" , resolved->buf);
cache.h +2 -0
@@ -1320,6 +1320,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_missing (struct strbuf *resolved,const char *path, +int missing_components,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 v3 1/2] abspath: add a function to resolve paths with missing components
Am 28.11.20 um 00:19 schrieb brian m. carlson:
Quoted 16 lines
We'd like to canonicalize paths such that we can preserve any number of trailing components that may be missing. Let's add a function to do that, taking the number of components to canonicalize, and make
strbuf_realpatha wrapper around it that allows just one component.Signed-off-by brian m. carlson <[email protected]>--- abspath.c | 33 ++++++++++++++++++++++++++++++++- cache.h | 2 ++ 2 files changed, 34 insertions(+), 1 deletion(-)abspath.c +0
-0
abspath.c static void strip_last_component(struct strbuf *path) · line 26
Quoted 7 lines
abspath.c +1
-0@@ -20,6 +20,7 @@ static void strip_last_component(struct strbuf *path)/* Find start of the last component */ while (offset < len && !is_dir_sep (path->buf[len -1 ])) len--; +/* Skip sequences of multiple path-separators */ while (offset < len &&is_dir_sep (path->buf[len -1 ])) len--;
Stray change?
Quoted 8 lines
abspath.c +4
-0@@ -66,6 +67,22 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)#define MAXSYMLINKS 32#endif +/* Count non-contiguous directory separators, not including a trailing one. */ +static int count_dir_separators (const char *s) +{ +int count =0 ;
abspath.c static void get_root_part(struct strbuf *resolved, struct strbuf *remaining) · line 83
Quoted 10 lines
abspath.c +10
-0+int last_sep =0 ; +const char *p = s; +while (*p) { +int is_sep =is_dir_sep (*p++); +if (is_sep && !last_sep) + count++; + last_sep = is_sep; + } +return count; +}
count_dir_separators("a/") returns 1; is this intended? It seems to contradict the "not including a trailing one" comment.
abspath.c static void get_root_part(struct strbuf *resolved, struct strbuf *remaining) · line 85
Quoted 2 lines
abspath.c +2
-0+ +
Nitpicking: Do we need both empty lines?
Quoted 21 lines
abspath.c +10
-0/* * Return the real path (i.e., absolute path, with symlinks resolved * and extra slashes removed) equivalent to the specified path. (If @@ -80,6 +97,16 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)*/ char *strbuf_realpath (struct strbuf *resolved,const char *path,int die_on_error) +{ +return strbuf_realpath_missing (resolved, path,1 , die_on_error); +} + +/* +* Just like strbuf_realpath, but allows specifying how many missing components +* are permitted. -1 may be specified to allow an unlimited number. +*/ +char *strbuf_realpath_missing (struct strbuf *resolved,const char *path, +int missing_components,int die_on_error) {struct strbuf remaining =STRBUF_INIT ;struct strbuf next =STRBUF_INIT ;
abspath.c char *strbuf_realpath(struct strbuf *resolved, const char *path, · line 159
Quoted 5 lines
abspath.c +2
-0@@ -128,8 +155,12 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,strbuf_addbuf (resolved, &next);if (lstat (resolved->buf, &st)) { +int trailing_components =count_dir_separators (remaining.buf) + + (remaining.len !=0 );
Hmm, so you actually want to count path components, not separators. Perhaps like this?
static size_t count_components(const char *p)
{
size_t n = 0;
while (*p) {
while (*p && !is_dir_sep(*p))
p++;
while (is_dir_sep(*p))
p++;
n++;
}
return n;
}Quoted 22 lines
abspath.c +3
-1/* error out unless this was the last component */ -if (errno !=ENOENT || remaining.len) { +if (errno !=ENOENT || + !(missing_components ==-1 || + trailing_components < missing_components)) {if (die_on_error)die_errno ("Invalid path '%s'" , resolved->buf);cache.h +2
-0@@ -1320,6 +1320,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_missing (struct strbuf *resolved,const char *path, +int missing_components,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 v3 1/2] abspath: add a function to resolve paths with missing components
Quoted 12 lines · René Scharfe
Am 28.11.20 um 00:19 schrieb brian m. carlson:
Quoted 11 lines
We'd like to canonicalize paths such that we can preserve any number of trailing components that may be missing. Let's add a function to do that, taking the number of components to canonicalize, and make
strbuf_realpatha wrapper around it that allows just one component.Signed-off-by brian m. carlson <[email protected]>--- abspath.c | 33 ++++++++++++++++++++++++++++++++- cache.h | 2 ++ 2 files changed, 34 insertions(+), 1 deletion(-)
Quoted 14 lines
abspath.c static void strip_last_component(struct strbuf *path) · line 26
Quoted 12 lines
abspath.c +1
-0@@ -20,6 +20,7 @@ static void strip_last_component(struct strbuf *path)/* Find start of the last component */ while (offset < len && !is_dir_sep (path->buf[len -1 ])) len--; +/* Skip sequences of multiple path-separators */ while (offset < len &&is_dir_sep (path->buf[len -1 ])) len--;Stray change?
Ah, yes. I pulled out the old code from v2, but left the whitespace. Will fix.
Quoted 1 line
Nitpicking: Do we need both empty lines?
No, we don't.
Quoted 30 lines
Quoted 21 lines
/*
* Return the real path (i.e., absolute path, with symlinks resolved
* and extra slashes removed) equivalent to the specified path. (Ifabspath.c +10
-0@@ -80,6 +97,16 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)*/ char *strbuf_realpath (struct strbuf *resolved,const char *path,int die_on_error) +{ +return strbuf_realpath_missing (resolved, path,1 , die_on_error); +} + +/* +* Just like strbuf_realpath, but allows specifying how many missing components +* are permitted. -1 may be specified to allow an unlimited number. +*/ +char *strbuf_realpath_missing (struct strbuf *resolved,const char *path, +int missing_components,int die_on_error) {struct strbuf remaining =STRBUF_INIT ;struct strbuf next =STRBUF_INIT ;abspath.c char *strbuf_realpath(struct strbuf *resolved, const char *path, · line 159
Quoted 5 lines
abspath.c +2
-0@@ -128,8 +155,12 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,strbuf_addbuf (resolved, &next);if (lstat (resolved->buf, &st)) { +int trailing_components =count_dir_separators (remaining.buf) + + (remaining.len !=0 );Hmm, so you actually want to count path components, not separators. Perhaps like this?
Quoted 12 lines
static size_t count_components(const char *p)
{
size_t n = 0;
while (*p) {
while (*p && !is_dir_sep(*p))
p++;
while (is_dir_sep(*p))
p++;
n++;
}
return n;
}Yeah, I think that's nicer, and simpler, too. Will reroll with that fix.
Signature · 2 lines
brian m. carlson (he/him or they/them) Houston, Texas, US
Re: [PATCH v3 1/2] abspath: add a function to resolve paths with missing components
In reply to brian m. carlsonHi brian,
Quoted 4 lines · brian m. carlson
abspath.c +0
-0@@ -80,6 +97,16 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)*/ char *strbuf_realpath (struct strbuf *resolved,const char *path,int die_on_error)
abspath.c static void get_root_part(struct strbuf *resolved, struct strbuf *remaining) · line 109
Quoted 10 lines
abspath.c +10
-0+{ +return strbuf_realpath_missing (resolved, path,1 , die_on_error); +} + +/* +* Just like strbuf_realpath, but allows specifying how many missing components +* are permitted. -1 may be specified to allow an unlimited number. +*/ +char *strbuf_realpath_missing (struct strbuf *resolved,const char *path, +int missing_components,int die_on_error)
I am having a hard time with the name missing_components. Taking a step back, I think that we are interested in essentially two modes: allow one missing component, or an arbitrary amount of missing components.
If this assumption is correct, we may not even need to count components at all. We only need a flag indicating whether we allow only the last component to be missing, or any number of components. Maybe something like error_on_missing_parent_directory or some such?
Ciao,
Dscho
Quoted 31 lines
abspath.c +5
-1{struct strbuf remaining =STRBUF_INIT ;struct strbuf next =STRBUF_INIT ;@@ -128,8 +155,12 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,strbuf_addbuf (resolved, &next);if (lstat (resolved->buf, &st)) { +int trailing_components =count_dir_separators (remaining.buf) + + (remaining.len !=0 );/* error out unless this was the last component */ -if (errno !=ENOENT || remaining.len) { +if (errno !=ENOENT || + !(missing_components ==-1 || + trailing_components < missing_components)) {if (die_on_error)die_errno ("Invalid path '%s'" , resolved->buf);cache.h +2
-0@@ -1320,6 +1320,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_missing (struct strbuf *resolved,const char *path, +int missing_components,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 v3 1/2] abspath: add a function to resolve paths with missing components
Quoted 8 lines · Johannes Schindelin
I am having a hard time with the name
missing_components. Taking a step back, I think that we are interested in essentially two modes: allow one missing component, or an arbitrary amount of missing components.If this assumption is correct, we may not even need to count components at all. We only need a flag indicating whether we allow only the last component to be missing, or any number of components. Maybe something like
error_on_missing_parent_directoryor some such?
That's certainly easier. I hope to get a reroll out this week, and I'll simplify when I do that.
Signature · 2 lines
brian m. carlson (he/him or they/them) Houston, Texas, US
PATCH v3 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 | 71 +++++++++++++--------- builtin/rev-parse.c | 104 ++++++++++++++++++++++++++++---- t/t1500-rev-parse.sh | 57 ++++++++++++++++- 3 files changed, 189 insertions(+), 43 deletions(-)
Documentation/git-rev-parse.txt +41 -30
@@ -212,6 +212,15 @@ 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 following it on the command + line. 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. + +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 +230,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 +280,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 +290,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 +92 -12
170 lines not on this page · raw message
t/t1500-rev-parse.sh +56 -1
75 lines not on this page · raw message
Hi brian,
Quoted 12 lines · brian m. carlson
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.
Many operating systems, such as macOS, lack a built-in realpath command that can canonicalize paths properly, and additionally some programming languages, like Go, currently do as well. It's therefore helpful for us to provide a generic way to request that a path is fully canonicalized before using it. Since users may wish for a relative path, we can provide one of those as well.
Quoted 3 lines
The primary impetus for this was Git LFS, which needs paths to be canonicalized in the same way as Git in order to function correctly. See https://github.com/git-lfs/git-lfs/issues/4012 for more details.
Sounds good!
I offered a step back that would allow us to do without count_dir_separators() (or count_components()) altogether.
Otherwise, this patch series still looks good to me.
Thanks,
Dscho
Quoted 29 lines
Changes from v2:
* Incorporate multiple missing segment support into the
strbuf_realpathcode.
* Switch some invocations to useDEFAULT_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 formattingDocumentation/git-rev-parse.txt | 71 +++++++++++++--------- abspath.c | 33 +++++++++- builtin/rev-parse.c | 104 ++++++++++++++++++++++++++++---- cache.h | 2 + t/t1500-rev-parse.sh | 57 ++++++++++++++++- 5 files changed, 223 insertions(+), 44 deletions(-)