{"thread":{"id":"54726","subject":"[PATCH v3 0/2] rev-parse options for absolute or relative paths","startedAt":"2020-11-27T23:21:14Z","lastAt":"2020-12-02T23:55:13Z","messageCount":8,"participants":["brian m. carlson","René Scharfe","Johannes Schindelin"],"isPatch":true,"patchVersion":3,"patchTotal":2},"messages":[{"id":"410937","messageId":"20201127231916.609852-1-sandals@crustytoothpaste.net","threadId":"54726","inReplyTo":null,"subject":"[PATCH v3 0/2] rev-parse options for absolute or relative paths","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2020-11-27T23:19:14Z","receivedAt":"2020-11-27T23:21:14Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"There are a bunch of different situations in which one would like to\nhave an absolute and canonical or a relative path from Git.  In many of\nthese cases, these values are already available from git rev-parse, but\nsome values only come in one form or another.\n\nMany operating systems, such as macOS, lack a built-in realpath command\nthat can canonicalize paths properly, and additionally some programming\nlanguages, like Go, currently do as well.  It's therefore helpful for us\nto provide a generic way to request that a path is fully canonicalized\nbefore using it.  Since users may wish for a relative path, we can\nprovide one of those as well.\n\nThe primary impetus for this was Git LFS, which needs paths to be\ncanonicalized in the same way as Git in order to function correctly.\nSee https://github.com/git-lfs/git-lfs/issues/4012 for more details.\n\nChanges from v2:\n\n* Incorporate multiple missing segment support into the strbuf_realpath\n  code.\n* Switch some invocations to use DEFAULT_UNMODIFIED, which should not\n  result in a change in behavior.\n* Rebase, resolving some conflicts.\n\nChanges from v1:\n\n* Add a function to handle missing trailing components when\n  canonicalizing paths and use it.\n* Improve commit messages.\n* Fix broken && chain.\n* Fix situation where relative paths are not relative.\n\nbrian m. carlson (2):\n  abspath: add a function to resolve paths with missing components\n  rev-parse: add option for absolute or relative path formatting\n\n Documentation/git-rev-parse.txt |  71 +++++++++++++---------\n abspath.c                       |  33 +++++++++-\n builtin/rev-parse.c             | 104 ++++++++++++++++++++++++++++----\n cache.h                         |   2 +\n t/t1500-rev-parse.sh            |  57 ++++++++++++++++-\n 5 files changed, 223 insertions(+), 44 deletions(-)\n\n"},{"id":"410938","messageId":"20201127231916.609852-3-sandals@crustytoothpaste.net","threadId":"54726","inReplyTo":"20201127231916.609852-1-sandals@crustytoothpaste.net","subject":"[PATCH v3 2/2] rev-parse: add option for absolute or relative path formatting","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2020-11-27T23:19:16Z","receivedAt":"2020-11-27T23:26:40Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"git rev-parse has several options which print various paths.  Some of\nthese paths are printed relative to the current working directory, and\nsome are absolute.\n\nNormally, this is not a problem, but there are times when one wants\npaths entirely in one format or another.  This can be done trivially if\nthe paths are canonical, but canonicalizing paths is not possible on\nsome shell scripting environments which lack realpath(1) and also in Go,\nwhich lacks functions that properly canonicalize paths on Windows.\n\nTo help out the scripter, let's provide an option which turns most of\nthe paths printed by git rev-parse to be either relative to the current\nworking directory or absolute and canonical.  Document which options are\naffected and which are not so that users are not confused.\n\nThis approach is cleaner and tidier than providing duplicates of\nexisting options which are either relative or absolute.\n\nNote that if the user needs both forms, it is possible to pass an\nadditional option in the middle of the command line which changes the\nbehavior of subsequent operations.\n\nSigned-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n---\n Documentation/git-rev-parse.txt |  71 +++++++++++++---------\n builtin/rev-parse.c             | 104 ++++++++++++++++++++++++++++----\n t/t1500-rev-parse.sh            |  57 ++++++++++++++++-\n 3 files changed, 189 insertions(+), 43 deletions(-)\n\ndiff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt\nindex 5013daa6ef..1708af8a66 100644\n--- a/Documentation/git-rev-parse.txt\n+++ b/Documentation/git-rev-parse.txt\n@@ -212,6 +212,15 @@ Options for Files\n \tOnly the names of the variables are listed, not their value,\n \teven if they are set.\n \n+--path-format=(absolute|relative)::\n+\tControls the behavior of certain other options following it on the command\n+\tline. If specified as absolute, the paths printed by those options will be\n+\tabsolute and canonical. If specified as relative, the paths will be relative\n+\tto the current working directory if that is possible.  The default is option\n+\tspecific.\n+\n+The following options are modified by `--path-format`:\n+\n --git-dir::\n \tShow `$GIT_DIR` if defined. Otherwise show the path to\n \tthe .git directory. The path shown, when relative, is\n@@ -221,13 +230,42 @@ If `$GIT_DIR` is not defined and the current directory\n is not detected to lie in a Git repository or work tree\n print a message to stderr and exit with nonzero status.\n \n+--git-common-dir::\n+\tShow `$GIT_COMMON_DIR` if defined, else `$GIT_DIR`.\n+\n+--resolve-git-dir <path>::\n+\tCheck if <path> is a valid repository or a gitfile that\n+\tpoints at a valid repository, and print the location of the\n+\trepository.  If <path> is a gitfile then the resolved path\n+\tto the real repository is printed.\n+\n+--git-path <path>::\n+\tResolve \"$GIT_DIR/<path>\" and takes other path relocation\n+\tvariables such as $GIT_OBJECT_DIRECTORY,\n+\t$GIT_INDEX_FILE... into account. For example, if\n+\t$GIT_OBJECT_DIRECTORY is set to /foo/bar then \"git rev-parse\n+\t--git-path objects/abc\" returns /foo/bar/abc.\n+\n+--show-toplevel::\n+\tShow the (by default, absolute) path of the top-level directory\n+\tof the working tree. If there is no working tree, report an error.\n+\n+--show-superproject-working-tree::\n+\tShow the absolute path of the root of the superproject's\n+\tworking tree (if exists) that uses the current repository as\n+\tits submodule.  Outputs nothing if the current repository is\n+\tnot used as a submodule by any project.\n+\n+--shared-index-path::\n+\tShow the path to the shared index file in split index mode, or\n+\tempty if not in split-index mode.\n+\n+The following options are unaffected by `--path-format`:\n+\n --absolute-git-dir::\n \tLike `--git-dir`, but its output is always the canonicalized\n \tabsolute path.\n \n---git-common-dir::\n-\tShow `$GIT_COMMON_DIR` if defined, else `$GIT_DIR`.\n-\n --is-inside-git-dir::\n \tWhen the current working directory is below the repository\n \tdirectory print \"true\", otherwise \"false\".\n@@ -242,19 +280,6 @@ print a message to stderr and exit with nonzero status.\n --is-shallow-repository::\n \tWhen the repository is shallow print \"true\", otherwise \"false\".\n \n---resolve-git-dir <path>::\n-\tCheck if <path> is a valid repository or a gitfile that\n-\tpoints at a valid repository, and print the location of the\n-\trepository.  If <path> is a gitfile then the resolved path\n-\tto the real repository is printed.\n-\n---git-path <path>::\n-\tResolve \"$GIT_DIR/<path>\" and takes other path relocation\n-\tvariables such as $GIT_OBJECT_DIRECTORY,\n-\t$GIT_INDEX_FILE... into account. For example, if\n-\t$GIT_OBJECT_DIRECTORY is set to /foo/bar then \"git rev-parse\n-\t--git-path objects/abc\" returns /foo/bar/abc.\n-\n --show-cdup::\n \tWhen the command is invoked from a subdirectory, show the\n \tpath of the top-level directory relative to the current\n@@ -265,20 +290,6 @@ print a message to stderr and exit with nonzero status.\n \tpath of the current directory relative to the top-level\n \tdirectory.\n \n---show-toplevel::\n-\tShow the absolute path of the top-level directory of the working\n-\ttree. If there is no working tree, report an error.\n-\n---show-superproject-working-tree::\n-\tShow the absolute path of the root of the superproject's\n-\tworking tree (if exists) that uses the current repository as\n-\tits submodule.  Outputs nothing if the current repository is\n-\tnot used as a submodule by any project.\n-\n---shared-index-path::\n-\tShow the path to the shared index file in split index mode, or\n-\tempty if not in split-index mode.\n-\n --show-object-format[=(storage|input|output)]::\n \tShow the object format (hash algorithm) used for the repository\n \tfor storage inside the `.git` directory, input, or output. For\ndiff --git a/builtin/rev-parse.c b/builtin/rev-parse.c\nindex 69ba7326cf..5386a6ae8b 100644\n--- a/builtin/rev-parse.c\n+++ b/builtin/rev-parse.c\n@@ -583,6 +583,73 @@ static void handle_ref_opt(const char *pattern, const char *prefix)\n \tclear_ref_exclusion(&ref_excludes);\n }\n \n+enum format_type {\n+\t/* We would like a relative path. */\n+\tFORMAT_RELATIVE,\n+\t/* We would like a canonical absolute path. */\n+\tFORMAT_CANONICAL,\n+\t/* We would like the default behavior. */\n+\tFORMAT_DEFAULT,\n+};\n+\n+enum default_type {\n+\t/* Our default is a relative path. */\n+\tDEFAULT_RELATIVE,\n+\t/* Our default is a relative path if there's a shared root. */\n+\tDEFAULT_RELATIVE_IF_SHARED,\n+\t/* Our default is a canonical absolute path. */\n+\tDEFAULT_CANONICAL,\n+\t/* Our default is not to modify the item. */\n+\tDEFAULT_UNMODIFIED,\n+};\n+\n+static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)\n+{\n+\tchar *cwd = NULL;\n+\t/*\n+\t * We don't ever produce a relative path if prefix is NULL, so set the\n+\t * prefix to the current directory so that we can produce a relative\n+\t * path whenever possible.  If we're using RELATIVE_IF_SHARED mode, then\n+\t * we want an absolute path unless the two share a common prefix, so don't\n+\t * set it in that case, since doing so causes a relative path to always\n+\t * be produced if possible.\n+\t */\n+\tif (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))\n+\t\tprefix = cwd = xgetcwd();\n+\tif (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {\n+\t\tputs(path);\n+\t} else if (format == FORMAT_RELATIVE ||\n+\t\t  (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {\n+\t\t/*\n+\t\t * In order for relative_path to work as expected, we need to\n+\t\t * make sure that both paths are absolute paths.  If we don't,\n+\t\t * we can end up with an unexpected absolute path that the user\n+\t\t * didn't want.\n+\t\t */\n+\t\tstruct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;\n+\t\tif (!is_absolute_path(path)) {\n+\t\t\tstrbuf_realpath_missing(&realbuf, path, -1, 1);\n+\t\t\tpath = realbuf.buf;\n+\t\t}\n+\t\tif (!is_absolute_path(prefix)) {\n+\t\t\tstrbuf_realpath_missing(&prefixbuf, prefix, -1, 1);\n+\t\t\tprefix = prefixbuf.buf;\n+\t\t}\n+\t\tputs(relative_path(path, prefix, &buf));\n+\t\tstrbuf_release(&buf);\n+\t} else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {\n+\t\tstruct strbuf buf = STRBUF_INIT;\n+\t\tputs(relative_path(path, prefix, &buf));\n+\t\tstrbuf_release(&buf);\n+\t} else {\n+\t\tstruct strbuf buf = STRBUF_INIT;\n+\t\tstrbuf_realpath_missing(&buf, path, -1, 1);\n+\t\tputs(buf.buf);\n+\t\tstrbuf_release(&buf);\n+\t}\n+\tfree(cwd);\n+}\n+\n int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n {\n \tint i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;\n@@ -596,6 +663,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \tstruct strbuf buf = STRBUF_INIT;\n \tconst int hexsz = the_hash_algo->hexsz;\n \tint seen_end_of_options = 0;\n+\tenum format_type format = FORMAT_DEFAULT;\n \n \tif (argc > 1 && !strcmp(\"--parseopt\", argv[1]))\n \t\treturn cmd_parseopt(argc - 1, argv + 1, prefix);\n@@ -668,8 +736,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t\tif (!argv[i + 1])\n \t\t\t\t\tdie(\"--git-path requires an argument\");\n \t\t\t\tstrbuf_reset(&buf);\n-\t\t\t\tputs(relative_path(git_path(\"%s\", argv[i + 1]),\n-\t\t\t\t\t\t   prefix, &buf));\n+\t\t\t\tprint_path(git_path(\"%s\", argv[i + 1]), prefix,\n+\t\t\t\t\t\tformat,\n+\t\t\t\t\t\tDEFAULT_RELATIVE_IF_SHARED);\n \t\t\t\ti++;\n \t\t\t\tcontinue;\n \t\t\t}\n@@ -687,6 +756,16 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t\t\tshow(arg);\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t\tif (opt_with_value(arg, \"--path-format\", &arg)) {\n+\t\t\t\tif (!strcmp(arg, \"absolute\")) {\n+\t\t\t\t\tformat = FORMAT_CANONICAL;\n+\t\t\t\t} else if (!strcmp(arg, \"relative\")) {\n+\t\t\t\t\tformat = FORMAT_RELATIVE;\n+\t\t\t\t} else {\n+\t\t\t\t\tdie(\"unknown argument to --path-format: %s\", arg);\n+\t\t\t\t}\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tif (!strcmp(arg, \"--default\")) {\n \t\t\t\tdef = argv[++i];\n \t\t\t\tif (!def)\n@@ -807,7 +886,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\tif (!strcmp(arg, \"--show-toplevel\")) {\n \t\t\t\tconst char *work_tree = get_git_work_tree();\n \t\t\t\tif (work_tree)\n-\t\t\t\t\tputs(work_tree);\n+\t\t\t\t\tprint_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);\n \t\t\t\telse\n \t\t\t\t\tdie(\"this operation must be run in a work tree\");\n \t\t\t\tcontinue;\n@@ -815,7 +894,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\tif (!strcmp(arg, \"--show-superproject-working-tree\")) {\n \t\t\t\tstruct strbuf superproject = STRBUF_INIT;\n \t\t\t\tif (get_superproject_working_tree(&superproject))\n-\t\t\t\t\tputs(superproject.buf);\n+\t\t\t\t\tprint_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED);\n \t\t\t\tstrbuf_release(&superproject);\n \t\t\t\tcontinue;\n \t\t\t}\n@@ -850,16 +929,18 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t\tconst char *gitdir = getenv(GIT_DIR_ENVIRONMENT);\n \t\t\t\tchar *cwd;\n \t\t\t\tint len;\n+\t\t\t\tenum format_type wanted = format;\n \t\t\t\tif (arg[2] == 'g') {\t/* --git-dir */\n \t\t\t\t\tif (gitdir) {\n-\t\t\t\t\t\tputs(gitdir);\n+\t\t\t\t\t\tprint_path(gitdir, prefix, format, DEFAULT_UNMODIFIED);\n \t\t\t\t\t\tcontinue;\n \t\t\t\t\t}\n \t\t\t\t\tif (!prefix) {\n-\t\t\t\t\t\tputs(\".git\");\n+\t\t\t\t\t\tprint_path(\".git\", prefix, format, DEFAULT_UNMODIFIED);\n \t\t\t\t\t\tcontinue;\n \t\t\t\t\t}\n \t\t\t\t} else {\t\t/* --absolute-git-dir */\n+\t\t\t\t\twanted = FORMAT_CANONICAL;\n \t\t\t\t\tif (!gitdir && !prefix)\n \t\t\t\t\t\tgitdir = \".git\";\n \t\t\t\t\tif (gitdir) {\n@@ -872,14 +953,14 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t\t}\n \t\t\t\tcwd = xgetcwd();\n \t\t\t\tlen = strlen(cwd);\n-\t\t\t\tprintf(\"%s%s.git\\n\", cwd, len && cwd[len-1] != '/' ? \"/\" : \"\");\n+\t\t\t\tstrbuf_reset(&buf);\n+\t\t\t\tstrbuf_addf(&buf, \"%s%s.git\", cwd, len && cwd[len-1] != '/' ? \"/\" : \"\");\n \t\t\t\tfree(cwd);\n+\t\t\t\tprint_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);\n \t\t\t\tcontinue;\n \t\t\t}\n \t\t\tif (!strcmp(arg, \"--git-common-dir\")) {\n-\t\t\t\tstrbuf_reset(&buf);\n-\t\t\t\tputs(relative_path(get_git_common_dir(),\n-\t\t\t\t\t\t   prefix, &buf));\n+\t\t\t\tprint_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);\n \t\t\t\tcontinue;\n \t\t\t}\n \t\t\tif (!strcmp(arg, \"--is-inside-git-dir\")) {\n@@ -909,8 +990,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)\n \t\t\t\tif (the_index.split_index) {\n \t\t\t\t\tconst struct object_id *oid = &the_index.split_index->base_oid;\n \t\t\t\t\tconst char *path = git_path(\"sharedindex.%s\", oid_to_hex(oid));\n-\t\t\t\t\tstrbuf_reset(&buf);\n-\t\t\t\t\tputs(relative_path(path, prefix, &buf));\n+\t\t\t\t\tprint_path(path, prefix, format, DEFAULT_RELATIVE);\n \t\t\t\t}\n \t\t\t\tcontinue;\n \t\t\t}\ndiff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh\nindex 408b97d5af..51d7d40ec1 100755\n--- a/t/t1500-rev-parse.sh\n+++ b/t/t1500-rev-parse.sh\n@@ -3,6 +3,16 @@\n test_description='test git rev-parse'\n . ./test-lib.sh\n \n+test_one () {\n+\tdir=\"$1\" &&\n+\texpect=\"$2\" &&\n+\tshift &&\n+\tshift &&\n+\techo \"$expect\" >expect &&\n+\tgit -C \"$dir\" rev-parse \"$@\" >actual &&\n+\ttest_cmp expect actual\n+}\n+\n # usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir\n test_rev_parse () {\n \td=\n@@ -60,7 +70,13 @@ ROOT=$(pwd)\n \n test_expect_success 'setup' '\n \tmkdir -p sub/dir work &&\n-\tcp -R .git repo.git\n+\tcp -R .git repo.git &&\n+\tgit checkout -B main &&\n+\ttest_commit abc &&\n+\tgit checkout -b side &&\n+\ttest_commit def &&\n+\tgit checkout main &&\n+\tgit worktree add worktree side\n '\n \n test_rev_parse toplevel false false true '' .git \"$ROOT/.git\"\n@@ -88,6 +104,45 @@ test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = tru\n \n test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''\n \n+test_expect_success 'rev-parse --path-format=absolute' '\n+\ttest_one \".\" \"$ROOT/.git\" --path-format=absolute --git-dir &&\n+\ttest_one \".\" \"$ROOT/.git\" --path-format=absolute --git-common-dir &&\n+\ttest_one \"sub/dir\" \"$ROOT/.git\" --path-format=absolute --git-dir &&\n+\ttest_one \"sub/dir\" \"$ROOT/.git\" --path-format=absolute --git-common-dir &&\n+\ttest_one \"worktree\" \"$ROOT/.git/worktrees/worktree\" --path-format=absolute --git-dir &&\n+\ttest_one \"worktree\" \"$ROOT/.git\" --path-format=absolute --git-common-dir &&\n+\ttest_one \".\" \"$ROOT\" --path-format=absolute --show-toplevel &&\n+\ttest_one \".\" \"$ROOT/.git/objects\" --path-format=absolute --git-path objects &&\n+\ttest_one \".\" \"$ROOT/.git/objects/foo/bar/baz\" --path-format=absolute --git-path objects/foo/bar/baz\n+'\n+\n+test_expect_success 'rev-parse --path-format=relative' '\n+\ttest_one \".\" \".git\" --path-format=relative --git-dir &&\n+\ttest_one \".\" \".git\" --path-format=relative --git-common-dir &&\n+\ttest_one \"sub/dir\" \"../../.git\" --path-format=relative --git-dir &&\n+\ttest_one \"sub/dir\" \"../../.git\" --path-format=relative --git-common-dir &&\n+\ttest_one \"worktree\" \"../.git/worktrees/worktree\" --path-format=relative --git-dir &&\n+\ttest_one \"worktree\" \"../.git\" --path-format=relative --git-common-dir &&\n+\ttest_one \".\" \"./\" --path-format=relative --show-toplevel &&\n+\ttest_one \".\" \".git/objects\" --path-format=relative --git-path objects &&\n+\ttest_one \".\" \".git/objects/foo/bar/baz\" --path-format=relative --git-path objects/foo/bar/baz\n+'\n+\n+test_expect_success '--path-format=relative does not affect --absolute-git-dir' '\n+\tgit rev-parse --path-format=relative --absolute-git-dir >actual &&\n+\techo \"$ROOT/.git\" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '--path-format can change in the middle of the command line' '\n+\tgit rev-parse --path-format=absolute --git-dir --path-format=relative --git-path objects/foo/bar >actual &&\n+\tcat >expect <<-EOF &&\n+\t$ROOT/.git\n+\t.git/objects/foo/bar\n+\tEOF\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'git-common-dir from worktree root' '\n \techo .git >expect &&\n \tgit rev-parse --git-common-dir >actual &&\n"},{"id":"410939","messageId":"20201127231916.609852-2-sandals@crustytoothpaste.net","threadId":"54726","inReplyTo":"20201127231916.609852-1-sandals@crustytoothpaste.net","subject":"[PATCH v3 1/2] abspath: add a function to resolve paths with missing components","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2020-11-27T23:19:15Z","receivedAt":"2020-11-27T23:27:12Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"We'd like to canonicalize paths such that we can preserve any number of\ntrailing components that may be missing.  Let's add a function to do\nthat, taking the number of components to canonicalize, and make\nstrbuf_realpath a wrapper around it that allows just one component.\n\nSigned-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n---\n abspath.c | 33 ++++++++++++++++++++++++++++++++-\n cache.h   |  2 ++\n 2 files changed, 34 insertions(+), 1 deletion(-)\n\ndiff --git a/abspath.c b/abspath.c\nindex 6f15a418bb..1d8f3d007c 100644\n--- a/abspath.c\n+++ b/abspath.c\n@@ -20,6 +20,7 @@ static void strip_last_component(struct strbuf *path)\n \t/* Find start of the last component */\n \twhile (offset < len && !is_dir_sep(path->buf[len - 1]))\n \t\tlen--;\n+\n \t/* Skip sequences of multiple path-separators */\n \twhile (offset < len && is_dir_sep(path->buf[len - 1]))\n \t\tlen--;\n@@ -66,6 +67,22 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)\n #define MAXSYMLINKS 32\n #endif\n \n+/* Count non-contiguous directory separators, not including a trailing one. */\n+static int count_dir_separators(const char *s)\n+{\n+\tint count = 0;\n+\tint last_sep = 0;\n+\tconst char *p = s;\n+\twhile (*p) {\n+\t\tint is_sep = is_dir_sep(*p++);\n+\t\tif (is_sep && !last_sep)\n+\t\t\tcount++;\n+\t\tlast_sep = is_sep;\n+\t}\n+\treturn count;\n+}\n+\n+\n /*\n  * Return the real path (i.e., absolute path, with symlinks resolved\n  * and extra slashes removed) equivalent to the specified path.  (If\n@@ -80,6 +97,16 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)\n  */\n char *strbuf_realpath(struct strbuf *resolved, const char *path,\n \t\t      int die_on_error)\n+{\n+\treturn strbuf_realpath_missing(resolved, path, 1, die_on_error);\n+}\n+\n+/*\n+ * Just like strbuf_realpath, but allows specifying how many missing components\n+ * are permitted.  -1 may be specified to allow an unlimited number.\n+ */\n+char *strbuf_realpath_missing(struct strbuf *resolved, const char *path,\n+\t\t\t      int missing_components, int die_on_error)\n {\n \tstruct strbuf remaining = STRBUF_INIT;\n \tstruct strbuf next = STRBUF_INIT;\n@@ -128,8 +155,12 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,\n \t\tstrbuf_addbuf(resolved, &next);\n \n \t\tif (lstat(resolved->buf, &st)) {\n+\t\t\tint trailing_components = count_dir_separators(remaining.buf) +\n+\t\t\t\t\t\t  (remaining.len != 0);\n \t\t\t/* error out unless this was the last component */\n-\t\t\tif (errno != ENOENT || remaining.len) {\n+\t\t\tif (errno != ENOENT ||\n+\t\t\t    !(missing_components == -1 ||\n+\t\t\t      trailing_components < missing_components)) {\n \t\t\t\tif (die_on_error)\n \t\t\t\t\tdie_errno(\"Invalid path '%s'\",\n \t\t\t\t\t\t  resolved->buf);\ndiff --git a/cache.h b/cache.h\nindex c0072d43b1..ee4bc5ec04 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1320,6 +1320,8 @@ static inline int is_absolute_path(const char *path)\n int is_directory(const char *);\n char *strbuf_realpath(struct strbuf *resolved, const char *path,\n \t\t      int die_on_error);\n+char *strbuf_realpath_missing(struct strbuf *resolved, const char *path,\n+\t\t\t      int missing_components, int die_on_error);\n char *real_pathdup(const char *path, int die_on_error);\n const char *absolute_path(const char *path);\n char *absolute_pathdup(const char *path);\n"},{"id":"410950","messageId":"374553d7-6027-0152-bf56-9395e8268fa9@web.de","threadId":"54726","inReplyTo":"20201127231916.609852-2-sandals@crustytoothpaste.net","subject":"Re: [PATCH v3 1/2] abspath: add a function to resolve paths with missing components","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2020-11-28T10:08:09Z","receivedAt":"2020-11-28T22:16:59Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 28.11.20 um 00:19 schrieb brian m. carlson:\n> We'd like to canonicalize paths such that we can preserve any number of\n> trailing components that may be missing.  Let's add a function to do\n> that, taking the number of components to canonicalize, and make\n> strbuf_realpath a wrapper around it that allows just one component.\n>\n> Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n> ---\n>  abspath.c | 33 ++++++++++++++++++++++++++++++++-\n>  cache.h   |  2 ++\n>  2 files changed, 34 insertions(+), 1 deletion(-)\n>\n> diff --git a/abspath.c b/abspath.c\n> index 6f15a418bb..1d8f3d007c 100644\n> --- a/abspath.c\n> +++ b/abspath.c\n> @@ -20,6 +20,7 @@ static void strip_last_component(struct strbuf *path)\n>  \t/* Find start of the last component */\n>  \twhile (offset < len && !is_dir_sep(path->buf[len - 1]))\n>  \t\tlen--;\n> +\n>  \t/* Skip sequences of multiple path-separators */\n>  \twhile (offset < len && is_dir_sep(path->buf[len - 1]))\n>  \t\tlen--;\n\nStray change?\n\n> @@ -66,6 +67,22 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)\n>  #define MAXSYMLINKS 32\n>  #endif\n>\n> +/* Count non-contiguous directory separators, not including a trailing one. */\n> +static int count_dir_separators(const char *s)\n> +{\n> +\tint count = 0;\n> +\tint last_sep = 0;\n> +\tconst char *p = s;\n> +\twhile (*p) {\n> +\t\tint is_sep = is_dir_sep(*p++);\n> +\t\tif (is_sep && !last_sep)\n> +\t\t\tcount++;\n> +\t\tlast_sep = is_sep;\n> +\t}\n> +\treturn count;\n> +}\n\ncount_dir_separators(\"a/\") returns 1; is this intended?  It seems to\ncontradict the \"not including a trailing one\" comment.\n\n> +\n> +\n\nNitpicking: Do we need both empty lines?\n\n>  /*\n>   * Return the real path (i.e., absolute path, with symlinks resolved\n>   * and extra slashes removed) equivalent to the specified path.  (If\n> @@ -80,6 +97,16 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)\n>   */\n>  char *strbuf_realpath(struct strbuf *resolved, const char *path,\n>  \t\t      int die_on_error)\n> +{\n> +\treturn strbuf_realpath_missing(resolved, path, 1, die_on_error);\n> +}\n> +\n> +/*\n> + * Just like strbuf_realpath, but allows specifying how many missing components\n> + * are permitted.  -1 may be specified to allow an unlimited number.\n> + */\n> +char *strbuf_realpath_missing(struct strbuf *resolved, const char *path,\n> +\t\t\t      int missing_components, int die_on_error)\n>  {\n>  \tstruct strbuf remaining = STRBUF_INIT;\n>  \tstruct strbuf next = STRBUF_INIT;\n> @@ -128,8 +155,12 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,\n>  \t\tstrbuf_addbuf(resolved, &next);\n>\n>  \t\tif (lstat(resolved->buf, &st)) {\n> +\t\t\tint trailing_components = count_dir_separators(remaining.buf) +\n> +\t\t\t\t\t\t  (remaining.len != 0);\n\nHmm, so you actually want to count path components, not separators.\nPerhaps like this?\n\n\tstatic size_t count_components(const char *p)\n\t{\n\t\tsize_t n = 0;\n\t\twhile (*p) {\n\t\t\twhile (*p && !is_dir_sep(*p))\n\t\t\t\tp++;\n\t\t\twhile (is_dir_sep(*p))\n\t\t\t\tp++;\n\t\t\tn++;\n\t\t}\n\t\treturn n;\n\t}\n\n>  \t\t\t/* error out unless this was the last component */\n> -\t\t\tif (errno != ENOENT || remaining.len) {\n> +\t\t\tif (errno != ENOENT ||\n> +\t\t\t    !(missing_components == -1 ||\n> +\t\t\t      trailing_components < missing_components)) {\n>  \t\t\t\tif (die_on_error)\n>  \t\t\t\t\tdie_errno(\"Invalid path '%s'\",\n>  \t\t\t\t\t\t  resolved->buf);\n> diff --git a/cache.h b/cache.h\n> index c0072d43b1..ee4bc5ec04 100644\n> --- a/cache.h\n> +++ b/cache.h\n> @@ -1320,6 +1320,8 @@ static inline int is_absolute_path(const char *path)\n>  int is_directory(const char *);\n>  char *strbuf_realpath(struct strbuf *resolved, const char *path,\n>  \t\t      int die_on_error);\n> +char *strbuf_realpath_missing(struct strbuf *resolved, const char *path,\n> +\t\t\t      int missing_components, int die_on_error);\n>  char *real_pathdup(const char *path, int die_on_error);\n>  const char *absolute_path(const char *path);\n>  char *absolute_pathdup(const char *path);\n>\n\n"},{"id":"410951","messageId":"X8KZ0gd85g7fhqm5@camp.crustytoothpaste.net","threadId":"54726","inReplyTo":"374553d7-6027-0152-bf56-9395e8268fa9@web.de","subject":"Re: [PATCH v3 1/2] abspath: add a function to resolve paths with missing components","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2020-11-28T18:41:22Z","receivedAt":"2020-11-28T22:17:54Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2020-11-28 at 10:08:09, René Scharfe wrote:\n> Am 28.11.20 um 00:19 schrieb brian m. carlson:\n> > We'd like to canonicalize paths such that we can preserve any number of\n> > trailing components that may be missing.  Let's add a function to do\n> > that, taking the number of components to canonicalize, and make\n> > strbuf_realpath a wrapper around it that allows just one component.\n> >\n> > Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>\n> > ---\n> >  abspath.c | 33 ++++++++++++++++++++++++++++++++-\n> >  cache.h   |  2 ++\n> >  2 files changed, 34 insertions(+), 1 deletion(-)\n> >\n> > diff --git a/abspath.c b/abspath.c\n> > index 6f15a418bb..1d8f3d007c 100644\n> > --- a/abspath.c\n> > +++ b/abspath.c\n> > @@ -20,6 +20,7 @@ static void strip_last_component(struct strbuf *path)\n> >  \t/* Find start of the last component */\n> >  \twhile (offset < len && !is_dir_sep(path->buf[len - 1]))\n> >  \t\tlen--;\n> > +\n> >  \t/* Skip sequences of multiple path-separators */\n> >  \twhile (offset < len && is_dir_sep(path->buf[len - 1]))\n> >  \t\tlen--;\n> \n> Stray change?\n\nAh, yes.  I pulled out the old code from v2, but left the whitespace.\nWill fix.\n\n> Nitpicking: Do we need both empty lines?\n\nNo, we don't.\n\n> >  /*\n> >   * Return the real path (i.e., absolute path, with symlinks resolved\n> >   * and extra slashes removed) equivalent to the specified path.  (If\n> > @@ -80,6 +97,16 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)\n> >   */\n> >  char *strbuf_realpath(struct strbuf *resolved, const char *path,\n> >  \t\t      int die_on_error)\n> > +{\n> > +\treturn strbuf_realpath_missing(resolved, path, 1, die_on_error);\n> > +}\n> > +\n> > +/*\n> > + * Just like strbuf_realpath, but allows specifying how many missing components\n> > + * are permitted.  -1 may be specified to allow an unlimited number.\n> > + */\n> > +char *strbuf_realpath_missing(struct strbuf *resolved, const char *path,\n> > +\t\t\t      int missing_components, int die_on_error)\n> >  {\n> >  \tstruct strbuf remaining = STRBUF_INIT;\n> >  \tstruct strbuf next = STRBUF_INIT;\n> > @@ -128,8 +155,12 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,\n> >  \t\tstrbuf_addbuf(resolved, &next);\n> >\n> >  \t\tif (lstat(resolved->buf, &st)) {\n> > +\t\t\tint trailing_components = count_dir_separators(remaining.buf) +\n> > +\t\t\t\t\t\t  (remaining.len != 0);\n> \n> Hmm, so you actually want to count path components, not separators.\n> Perhaps like this?\n> \n> \tstatic size_t count_components(const char *p)\n> \t{\n> \t\tsize_t n = 0;\n> \t\twhile (*p) {\n> \t\t\twhile (*p && !is_dir_sep(*p))\n> \t\t\t\tp++;\n> \t\t\twhile (is_dir_sep(*p))\n> \t\t\t\tp++;\n> \t\t\tn++;\n> \t\t}\n> \t\treturn n;\n> \t}\n\nYeah, I think that's nicer, and simpler, too.  Will reroll with that\nfix.\n-- \nbrian m. carlson (he/him or they/them)\nHouston, Texas, US\n"},{"id":"411161","messageId":"nycvar.QRO.7.76.6.2012021406020.25979@tvgsbejvaqbjf.bet","threadId":"54726","inReplyTo":"20201127231916.609852-2-sandals@crustytoothpaste.net","subject":"Re: [PATCH v3 1/2] abspath: add a function to resolve paths with missing components","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2020-12-02T13:09:49Z","receivedAt":"2020-12-02T13:11:46Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi brian,\n\nOn Fri, 27 Nov 2020, brian m. carlson wrote:\n\n> @@ -80,6 +97,16 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)\n>   */\n>  char *strbuf_realpath(struct strbuf *resolved, const char *path,\n>  \t\t      int die_on_error)\n> +{\n> +\treturn strbuf_realpath_missing(resolved, path, 1, die_on_error);\n> +}\n> +\n> +/*\n> + * Just like strbuf_realpath, but allows specifying how many missing components\n> + * are permitted.  -1 may be specified to allow an unlimited number.\n> + */\n> +char *strbuf_realpath_missing(struct strbuf *resolved, const char *path,\n> +\t\t\t      int missing_components, int die_on_error)\n\nI am having a hard time with the name `missing_components`. Taking a step\nback, I think that we are interested in essentially two modes: allow one\nmissing component, or an arbitrary amount of missing components.\n\nIf this assumption is correct, we may not even need to count components at\nall. We only need a flag indicating whether we allow only the last\ncomponent to be missing, or any number of components. Maybe something like\n`error_on_missing_parent_directory` or some such?\n\nCiao,\nDscho\n\n>  {\n>  \tstruct strbuf remaining = STRBUF_INIT;\n>  \tstruct strbuf next = STRBUF_INIT;\n> @@ -128,8 +155,12 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,\n>  \t\tstrbuf_addbuf(resolved, &next);\n>\n>  \t\tif (lstat(resolved->buf, &st)) {\n> +\t\t\tint trailing_components = count_dir_separators(remaining.buf) +\n> +\t\t\t\t\t\t  (remaining.len != 0);\n>  \t\t\t/* error out unless this was the last component */\n> -\t\t\tif (errno != ENOENT || remaining.len) {\n> +\t\t\tif (errno != ENOENT ||\n> +\t\t\t    !(missing_components == -1 ||\n> +\t\t\t      trailing_components < missing_components)) {\n>  \t\t\t\tif (die_on_error)\n>  \t\t\t\t\tdie_errno(\"Invalid path '%s'\",\n>  \t\t\t\t\t\t  resolved->buf);\n> diff --git a/cache.h b/cache.h\n> index c0072d43b1..ee4bc5ec04 100644\n> --- a/cache.h\n> +++ b/cache.h\n> @@ -1320,6 +1320,8 @@ static inline int is_absolute_path(const char *path)\n>  int is_directory(const char *);\n>  char *strbuf_realpath(struct strbuf *resolved, const char *path,\n>  \t\t      int die_on_error);\n> +char *strbuf_realpath_missing(struct strbuf *resolved, const char *path,\n> +\t\t\t      int missing_components, int die_on_error);\n>  char *real_pathdup(const char *path, int die_on_error);\n>  const char *absolute_path(const char *path);\n>  char *absolute_pathdup(const char *path);\n>\n"},{"id":"411163","messageId":"nycvar.QRO.7.76.6.2012021410560.25979@tvgsbejvaqbjf.bet","threadId":"54726","inReplyTo":"20201127231916.609852-1-sandals@crustytoothpaste.net","subject":"Re: [PATCH v3 0/2] rev-parse options for absolute or relative paths","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2020-12-02T13:12:01Z","receivedAt":"2020-12-02T13:13:50Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi brian,\n\nOn Fri, 27 Nov 2020, brian m. carlson wrote:\n\n> There are a bunch of different situations in which one would like to\n> have an absolute and canonical or a relative path from Git.  In many of\n> these cases, these values are already available from git rev-parse, but\n> some values only come in one form or another.\n>\n> Many operating systems, such as macOS, lack a built-in realpath command\n> that can canonicalize paths properly, and additionally some programming\n> languages, like Go, currently do as well.  It's therefore helpful for us\n> to provide a generic way to request that a path is fully canonicalized\n> before using it.  Since users may wish for a relative path, we can\n> provide one of those as well.\n>\n> The primary impetus for this was Git LFS, which needs paths to be\n> canonicalized in the same way as Git in order to function correctly.\n> See https://github.com/git-lfs/git-lfs/issues/4012 for more details.\n\nSounds good!\n\nI offered a step back that would allow us to do without\n`count_dir_separators()` (or `count_components()`) altogether.\n\nOtherwise, this patch series still looks good to me.\n\nThanks,\nDscho\n\n>\n> Changes from v2:\n>\n> * Incorporate multiple missing segment support into the strbuf_realpath\n>   code.\n> * Switch some invocations to use DEFAULT_UNMODIFIED, which should not\n>   result in a change in behavior.\n> * Rebase, resolving some conflicts.\n>\n> Changes from v1:\n>\n> * Add a function to handle missing trailing components when\n>   canonicalizing paths and use it.\n> * Improve commit messages.\n> * Fix broken && chain.\n> * Fix situation where relative paths are not relative.\n>\n> brian m. carlson (2):\n>   abspath: add a function to resolve paths with missing components\n>   rev-parse: add option for absolute or relative path formatting\n>\n>  Documentation/git-rev-parse.txt |  71 +++++++++++++---------\n>  abspath.c                       |  33 +++++++++-\n>  builtin/rev-parse.c             | 104 ++++++++++++++++++++++++++++----\n>  cache.h                         |   2 +\n>  t/t1500-rev-parse.sh            |  57 ++++++++++++++++-\n>  5 files changed, 223 insertions(+), 44 deletions(-)\n>\n>\n"},{"id":"411187","messageId":"X8gpMUtAHsQ/NfTl@camp.crustytoothpaste.net","threadId":"54726","inReplyTo":"nycvar.QRO.7.76.6.2012021406020.25979@tvgsbejvaqbjf.bet","subject":"Re: [PATCH v3 1/2] abspath: add a function to resolve paths with missing components","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2020-12-02T23:54:25Z","receivedAt":"2020-12-02T23:55:13Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2020-12-02 at 13:09:49, Johannes Schindelin wrote:\n> I am having a hard time with the name `missing_components`. Taking a step\n> back, I think that we are interested in essentially two modes: allow one\n> missing component, or an arbitrary amount of missing components.\n> \n> If this assumption is correct, we may not even need to count components at\n> all. We only need a flag indicating whether we allow only the last\n> component to be missing, or any number of components. Maybe something like\n> `error_on_missing_parent_directory` or some such?\n\nThat's certainly easier.  I hope to get a reroll out this week, and I'll\nsimplify when I do that.\n-- \nbrian m. carlson (he/him or they/them)\nHouston, Texas, US\n"}]}