{"thread":{"id":"26761","subject":"[PATCH] Name make_*_path functions more accurately","startedAt":"2011-03-17T11:26:46Z","lastAt":"2011-03-18T08:06:37Z","messageCount":3,"participants":["Carlos Martín Nieto","Junio C Hamano","Michael J Gruber"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"163553","messageId":"1300361206-6982-1-git-send-email-cmn@elego.de","threadId":"26761","inReplyTo":null,"subject":"[PATCH] Name make_*_path functions more accurately","fromName":"Carlos Martín Nieto","fromEmail":"cmn@elego.de","sentAt":"2011-03-17T11:26:46Z","receivedAt":"2011-03-17T11:26:46Z","isPatch":true,"sender":{"key":"cmn@elego.de","avatar":"https://avatars.githubusercontent.com/u/335443?v=4"},"body":"Rename the make_*_path functions so it's clearer what they do, in\nparticlar make clear what the differnce between make_absolute_path and\nmake_nonrelative_path is by renaming them real_path and absolute_path\nrespectively. make_relative_path has an understandable name and is\nrenamed to relative_path to maintain the name convention.\n\nThe function calls have been replaced 1-to-1 in their usage.\n\nSigned-off-by: Carlos Martín Nieto <cmn@elego.de>\n---\n\nOn mié, 2011-03-16 at 17:59 +0100, Michael J Gruber wrote:\n> I think you should strictly separate the renaming issue from other\n> patches (and describe/motivate the latter). It's fine to have a large\n> patch with mechanical changes (renaming) if you stick to just those.\n\n Thanks for the careful review. This patch just does the rename and\n I'll send the rest later. I also removed an indentation change that\n managed so sneak in in the last one.\n\n abspath.c              |   18 ++++++++++++++++--\n builtin/clone.c        |   10 +++++-----\n builtin/init-db.c      |    8 ++++----\n builtin/receive-pack.c |    2 +-\n cache.h                |    6 +++---\n dir.c                  |    2 +-\n environment.c          |    4 ++--\n exec_cmd.c             |    2 +-\n lockfile.c             |    4 ++--\n path.c                 |    2 +-\n setup.c                |   12 ++++++------\n t/t0000-basic.sh       |   10 +++++-----\n test-path-utils.c      |    4 ++--\n 13 files changed, 49 insertions(+), 35 deletions(-)\n\ndiff --git a/abspath.c b/abspath.c\nindex ff14068..3005aed 100644\n--- a/abspath.c\n+++ b/abspath.c\n@@ -14,7 +14,14 @@ int is_directory(const char *path)\n /* We allow \"recursive\" symbolic links. Only within reason, though. */\n #define MAXDEPTH 5\n \n-const char *make_absolute_path(const char *path)\n+/*\n+ * Use this to get the real path, i.e. resolve links. If you want an\n+ * absolute path but don't mind links, use absolute_path.\n+ *\n+ * If path is our buffer, then return path, as it's already what the\n+ * user wants.\n+ */\n+const char *real_path(const char *path)\n {\n \tstatic char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];\n \tchar cwd[1024] = \"\";\n@@ -104,7 +111,14 @@ static const char *get_pwd_cwd(void)\n \treturn cwd;\n }\n \n-const char *make_nonrelative_path(const char *path)\n+/*\n+ * Use this to get an absolute path from a relative one. If you want\n+ * to resolve links, you should use real_path.\n+ *\n+ * If the path is already absolute, then return path. As the user is\n+ * never meant to free the return value, we're safe.\n+ */\n+const char *absolute_path(const char *path)\n {\n \tstatic char buf[PATH_MAX + 1];\n \ndiff --git a/builtin/clone.c b/builtin/clone.c\nindex 2ee1fa9..404f589 100644\n--- a/builtin/clone.c\n+++ b/builtin/clone.c\n@@ -100,7 +100,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)\n \t\tpath = mkpath(\"%s%s\", repo, suffix[i]);\n \t\tif (is_directory(path)) {\n \t\t\t*is_bundle = 0;\n-\t\t\treturn xstrdup(make_nonrelative_path(path));\n+\t\t\treturn xstrdup(absolute_path(path));\n \t\t}\n \t}\n \n@@ -109,7 +109,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)\n \t\tpath = mkpath(\"%s%s\", repo, bundle_suffix[i]);\n \t\tif (!stat(path, &st) && S_ISREG(st.st_mode)) {\n \t\t\t*is_bundle = 1;\n-\t\t\treturn xstrdup(make_nonrelative_path(path));\n+\t\t\treturn xstrdup(absolute_path(path));\n \t\t}\n \t}\n \n@@ -203,7 +203,7 @@ static void setup_reference(const char *repo)\n \tstruct transport *transport;\n \tconst struct ref *extra;\n \n-\tref_git = make_absolute_path(option_reference);\n+\tref_git = real_path(option_reference);\n \n \tif (is_directory(mkpath(\"%s/.git/objects\", ref_git)))\n \t\tref_git = mkpath(\"%s/.git\", ref_git);\n@@ -411,7 +411,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)\n \n \tpath = get_repo_path(repo_name, &is_bundle);\n \tif (path)\n-\t\trepo = xstrdup(make_nonrelative_path(repo_name));\n+\t\trepo = xstrdup(absolute_path(repo_name));\n \telse if (!strchr(repo_name, ':'))\n \t\tdie(\"repository '%s' does not exist\", repo_name);\n \telse\n@@ -466,7 +466,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)\n \n \tif (safe_create_leading_directories_const(git_dir) < 0)\n \t\tdie(\"could not create leading directories of '%s'\", git_dir);\n-\tset_git_dir(make_absolute_path(git_dir));\n+\tset_git_dir(real_path(git_dir));\n \n \tif (0 <= option_verbosity)\n \t\tprintf(\"Cloning into %s%s...\\n\",\ndiff --git a/builtin/init-db.c b/builtin/init-db.c\nindex fbeb380..8f5cfd7 100644\n--- a/builtin/init-db.c\n+++ b/builtin/init-db.c\n@@ -501,7 +501,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)\n \t\tconst char *git_dir_parent = strrchr(git_dir, '/');\n \t\tif (git_dir_parent) {\n \t\t\tchar *rel = xstrndup(git_dir, git_dir_parent - git_dir);\n-\t\t\tgit_work_tree_cfg = xstrdup(make_absolute_path(rel));\n+\t\t\tgit_work_tree_cfg = xstrdup(real_path(rel));\n \t\t\tfree(rel);\n \t\t}\n \t\tif (!git_work_tree_cfg) {\n@@ -510,7 +510,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)\n \t\t\t\tdie_errno (\"Cannot access current working directory\");\n \t\t}\n \t\tif (work_tree)\n-\t\t\tset_git_work_tree(make_absolute_path(work_tree));\n+\t\t\tset_git_work_tree(real_path(work_tree));\n \t\telse\n \t\t\tset_git_work_tree(git_work_tree_cfg);\n \t\tif (access(get_git_work_tree(), X_OK))\n@@ -519,10 +519,10 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)\n \t}\n \telse {\n \t\tif (work_tree)\n-\t\t\tset_git_work_tree(make_absolute_path(work_tree));\n+\t\t\tset_git_work_tree(real_path(work_tree));\n \t}\n \n-\tset_git_dir(make_absolute_path(git_dir));\n+\tset_git_dir(real_path(git_dir));\n \n \treturn init_db(template_dir, flags);\n }\ndiff --git a/builtin/receive-pack.c b/builtin/receive-pack.c\nindex 760817d..d883585 100644\n--- a/builtin/receive-pack.c\n+++ b/builtin/receive-pack.c\n@@ -740,7 +740,7 @@ static int add_refs_from_alternate(struct alternate_object_database *e, void *un\n \tconst struct ref *extra;\n \n \te->name[-1] = '\\0';\n-\tother = xstrdup(make_absolute_path(e->base));\n+\tother = xstrdup(real_path(e->base));\n \te->name[-1] = '/';\n \tlen = strlen(other);\n \ndiff --git a/cache.h b/cache.h\nindex c782495..b22f4d9 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -716,9 +716,9 @@ static inline int is_absolute_path(const char *path)\n \treturn path[0] == '/' || has_dos_drive_prefix(path);\n }\n int is_directory(const char *);\n-const char *make_absolute_path(const char *path);\n-const char *make_nonrelative_path(const char *path);\n-const char *make_relative_path(const char *abs, const char *base);\n+const char *real_path(const char *path);\n+const char *absolute_path(const char *path);\n+const char *relative_path(const char *abs, const char *base);\n int normalize_path_copy(char *dst, const char *src);\n int longest_ancestor_length(const char *path, const char *prefix_list);\n char *strip_path_suffix(const char *path, const char *suffix);\ndiff --git a/dir.c b/dir.c\nindex 570b651..7e00e06 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -1024,7 +1024,7 @@ char *get_relative_cwd(char *buffer, int size, const char *dir)\n \t\tdie_errno(\"can't find the current directory\");\n \n \tif (!is_absolute_path(dir))\n-\t\tdir = make_absolute_path(dir);\n+\t\tdir = real_path(dir);\n \n \twhile (*dir && *dir == *cwd) {\n \t\tdir++;\ndiff --git a/environment.c b/environment.c\nindex c3efbb9..cc670b1 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -139,7 +139,7 @@ static int git_work_tree_initialized;\n void set_git_work_tree(const char *new_work_tree)\n {\n \tif (git_work_tree_initialized) {\n-\t\tnew_work_tree = make_absolute_path(new_work_tree);\n+\t\tnew_work_tree = real_path(new_work_tree);\n \t\tif (strcmp(new_work_tree, work_tree))\n \t\t\tdie(\"internal error: work tree has already been set\\n\"\n \t\t\t    \"Current worktree: %s\\nNew worktree: %s\",\n@@ -147,7 +147,7 @@ void set_git_work_tree(const char *new_work_tree)\n \t\treturn;\n \t}\n \tgit_work_tree_initialized = 1;\n-\twork_tree = xstrdup(make_absolute_path(new_work_tree));\n+\twork_tree = xstrdup(real_path(new_work_tree));\n }\n \n const char *get_git_work_tree(void)\ndiff --git a/exec_cmd.c b/exec_cmd.c\nindex 38545e8..171e841 100644\n--- a/exec_cmd.c\n+++ b/exec_cmd.c\n@@ -89,7 +89,7 @@ static void add_path(struct strbuf *out, const char *path)\n \t\tif (is_absolute_path(path))\n \t\t\tstrbuf_addstr(out, path);\n \t\telse\n-\t\t\tstrbuf_addstr(out, make_nonrelative_path(path));\n+\t\t\tstrbuf_addstr(out, absolute_path(path));\n \n \t\tstrbuf_addch(out, PATH_SEP);\n \t}\ndiff --git a/lockfile.c b/lockfile.c\nindex b0d74cd..c6fb77b 100644\n--- a/lockfile.c\n+++ b/lockfile.c\n@@ -164,10 +164,10 @@ static char *unable_to_lock_message(const char *path, int err)\n \t\t    \"If no other git process is currently running, this probably means a\\n\"\n \t\t    \"git process crashed in this repository earlier. Make sure no other git\\n\"\n \t\t    \"process is running and remove the file manually to continue.\",\n-\t\t\t    make_nonrelative_path(path), strerror(err));\n+\t\t\t    absolute_path(path), strerror(err));\n \t} else\n \t\tstrbuf_addf(&buf, \"Unable to create '%s.lock': %s\",\n-\t\t\t    make_nonrelative_path(path), strerror(err));\n+\t\t\t    absolute_path(path), strerror(err));\n \treturn strbuf_detach(&buf, NULL);\n }\n \ndiff --git a/path.c b/path.c\nindex 8951333..4d73cc9 100644\n--- a/path.c\n+++ b/path.c\n@@ -397,7 +397,7 @@ int set_shared_perm(const char *path, int mode)\n \treturn 0;\n }\n \n-const char *make_relative_path(const char *abs, const char *base)\n+const char *relative_path(const char *abs, const char *base)\n {\n \tstatic char buf[PATH_MAX + 1];\n \tint i = 0, j = 0;\ndiff --git a/setup.c b/setup.c\nindex dadc666..b7e6102 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -218,7 +218,7 @@ void setup_work_tree(void)\n \twork_tree = get_git_work_tree();\n \tgit_dir = get_git_dir();\n \tif (!is_absolute_path(git_dir))\n-\t\tgit_dir = make_absolute_path(git_dir);\n+\t\tgit_dir = real_path(get_git_dir());\n \tif (!work_tree || chdir(work_tree))\n \t\tdie(\"This operation must be run in a work tree\");\n \n@@ -229,7 +229,7 @@ void setup_work_tree(void)\n \tif (getenv(GIT_WORK_TREE_ENVIRONMENT))\n \t\tsetenv(GIT_WORK_TREE_ENVIRONMENT, \".\", 1);\n \n-\tset_git_dir(make_relative_path(git_dir, work_tree));\n+\tset_git_dir(relative_path(git_dir, work_tree));\n \tinitialized = 1;\n }\n \n@@ -309,7 +309,7 @@ const char *read_gitfile_gently(const char *path)\n \n \tif (!is_git_directory(dir))\n \t\tdie(\"Not a git repository: %s\", dir);\n-\tpath = make_absolute_path(dir);\n+\tpath = real_path(dir);\n \n \tfree(buf);\n \treturn path;\n@@ -389,7 +389,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,\n \n \tif (!prefixcmp(cwd, worktree) &&\n \t    cwd[strlen(worktree)] == '/') { /* cwd inside worktree */\n-\t\tset_git_dir(make_absolute_path(gitdirenv));\n+\t\tset_git_dir(real_path(gitdirenv));\n \t\tif (chdir(worktree))\n \t\t\tdie_errno(\"Could not chdir to '%s'\", worktree);\n \t\tcwd[len++] = '/';\n@@ -414,7 +414,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,\n \t/* --work-tree is set without --git-dir; use discovered one */\n \tif (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {\n \t\tif (offset != len && !is_absolute_path(gitdir))\n-\t\t\tgitdir = xstrdup(make_absolute_path(gitdir));\n+\t\t\tgitdir = xstrdup(real_path(gitdir));\n \t\tif (chdir(cwd))\n \t\t\tdie_errno(\"Could not come back to cwd\");\n \t\treturn setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);\n@@ -422,7 +422,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,\n \n \t/* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */\n \tif (is_bare_repository_cfg > 0) {\n-\t\tset_git_dir(offset == len ? gitdir : make_absolute_path(gitdir));\n+\t\tset_git_dir(offset == len ? gitdir : real_path(gitdir));\n \t\tif (chdir(cwd))\n \t\t\tdie_errno(\"Could not come back to cwd\");\n \t\treturn NULL;\ndiff --git a/t/t0000-basic.sh b/t/t0000-basic.sh\nindex 8deec75..f4e8f43 100755\n--- a/t/t0000-basic.sh\n+++ b/t/t0000-basic.sh\n@@ -435,7 +435,7 @@ test_expect_success 'update-index D/F conflict' '\n \ttest $numpath0 = 1\n '\n \n-test_expect_success SYMLINKS 'absolute path works as expected' '\n+test_expect_success SYMLINKS 'real path works as expected' '\n \tmkdir first &&\n \tln -s ../.git first/.git &&\n \tmkdir second &&\n@@ -443,14 +443,14 @@ test_expect_success SYMLINKS 'absolute path works as expected' '\n \tmkdir third &&\n \tdir=\"$(cd .git; pwd -P)\" &&\n \tdir2=third/../second/other/.git &&\n-\ttest \"$dir\" = \"$(test-path-utils make_absolute_path $dir2)\" &&\n+\ttest \"$dir\" = \"$(test-path-utils real_path $dir2)\" &&\n \tfile=\"$dir\"/index &&\n-\ttest \"$file\" = \"$(test-path-utils make_absolute_path $dir2/index)\" &&\n+\ttest \"$file\" = \"$(test-path-utils real_path $dir2/index)\" &&\n \tbasename=blub &&\n-\ttest \"$dir/$basename\" = \"$(cd .git && test-path-utils make_absolute_path \"$basename\")\" &&\n+\ttest \"$dir/$basename\" = \"$(cd .git && test-path-utils real_path \"$basename\")\" &&\n \tln -s ../first/file .git/syml &&\n \tsym=\"$(cd first; pwd -P)\"/file &&\n-\ttest \"$sym\" = \"$(test-path-utils make_absolute_path \"$dir2/syml\")\"\n+\ttest \"$sym\" = \"$(test-path-utils real_path \"$dir2/syml\")\"\n '\n \n test_expect_success 'very long name in the index handled sanely' '\ndiff --git a/test-path-utils.c b/test-path-utils.c\nindex d261398..e767159 100644\n--- a/test-path-utils.c\n+++ b/test-path-utils.c\n@@ -11,9 +11,9 @@ int main(int argc, char **argv)\n \t\treturn 0;\n \t}\n \n-\tif (argc >= 2 && !strcmp(argv[1], \"make_absolute_path\")) {\n+\tif (argc >= 2 && !strcmp(argv[1], \"real_path\")) {\n \t\twhile (argc > 2) {\n-\t\t\tputs(make_absolute_path(argv[2]));\n+\t\t\tputs(real_path(argv[2]));\n \t\t\targc--;\n \t\t\targv++;\n \t\t}\n-- \n1.7.4.1\n"},{"id":"163614","messageId":"7vipvgyjnl.fsf@alter.siamese.dyndns.org","threadId":"26761","inReplyTo":"1300361206-6982-1-git-send-email-cmn@elego.de","subject":"Re: [PATCH] Name make_*_path functions more accurately","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-03-18T07:25:50Z","receivedAt":"2011-03-18T07:25:50Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Carlos Martín Nieto <cmn@elego.de> writes:\n\n> Rename the make_*_path functions so it's clearer what they do, in\n> particlar make clear what the differnce between make_absolute_path and\n> make_nonrelative_path is by renaming them real_path and absolute_path\n> respectively. make_relative_path has an understandable name and is\n> renamed to relative_path to maintain the name convention.\n\nThe approach taken by this patch is a sound one, and I like it.  The\nchange does not reuse any existing name for different purpose, which means\nthere is little chance of this change interacting other topics that may be\nin flight that introduce new call sites to these renamed functions in a\nfunny way.  A (semantic) mismerge or misapplication of the patch will be\nfound by the compiler.\n\nFor example, the version of setup.c this patch is based on the version\nbefore 05f08e4 (Merge branch 'cb/setup', 2011-02-09) was merged, and the\nmerge introduced a new call site to make_absolute_path().  A few callsites\nto make_nonrelative_path() in wrapper.c were introduced at 70ec868 (Merge\nbranch 'ae/better-template-failure-report', 2011-02-09), and this patch\ndoes not touch them.\n\nAs the result, the patch cleanly applies textually, but the resulting code\ndoes not compile, and it is a good thing ;-).\n\n\nHere is a minor fix-up necessary when queuing this on top of master\n\n setup.c   |    2 +-\n wrapper.c |    4 ++--\n 2 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex eae853a..03cd84f 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -9,7 +9,7 @@ char *prefix_path(const char *prefix, int len, const char *path)\n \tconst char *orig = path;\n \tchar *sanitized;\n \tif (is_absolute_path(orig)) {\n-\t\tconst char *temp = make_absolute_path(path);\n+\t\tconst char *temp = real_path(path);\n \t\tsanitized = xmalloc(len + strlen(temp) + 1);\n \t\tstrcpy(sanitized, temp);\n \t} else {\ndiff --git a/wrapper.c b/wrapper.c\nindex 4c147d6..2829000 100644\n--- a/wrapper.c\n+++ b/wrapper.c\n@@ -209,7 +209,7 @@ int xmkstemp(char *template)\n \t\tif (!template[0])\n \t\t\ttemplate = origtemplate;\n \n-\t\tnonrelative_template = make_nonrelative_path(template);\n+\t\tnonrelative_template = absolute_path(template);\n \t\terrno = saved_errno;\n \t\tdie_errno(\"Unable to create temporary file '%s'\",\n \t\t\tnonrelative_template);\n@@ -344,7 +344,7 @@ int xmkstemp_mode(char *template, int mode)\n \t\tif (!template[0])\n \t\t\ttemplate = origtemplate;\n \n-\t\tnonrelative_template = make_nonrelative_path(template);\n+\t\tnonrelative_template = absolute_path(template);\n \t\terrno = saved_errno;\n \t\tdie_errno(\"Unable to create temporary file '%s'\",\n \t\t\tnonrelative_template);\n"},{"id":"163616","messageId":"4D83128D.6020206@drmicha.warpmail.net","threadId":"26761","inReplyTo":"7vipvgyjnl.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH] Name make_*_path functions more accurately","fromName":"Michael J Gruber","fromEmail":"git@drmicha.warpmail.net","sentAt":"2011-03-18T08:06:37Z","receivedAt":"2011-03-18T08:06:37Z","isPatch":true,"sender":{"key":"git@grubix.eu","avatar":"https://avatars.githubusercontent.com/u/233215?v=4"},"body":"Junio C Hamano venit, vidit, dixit 18.03.2011 08:25:\n> Carlos Martín Nieto <cmn@elego.de> writes:\n> \n>> Rename the make_*_path functions so it's clearer what they do, in\n>> particlar make clear what the differnce between make_absolute_path and\n>> make_nonrelative_path is by renaming them real_path and absolute_path\n>> respectively. make_relative_path has an understandable name and is\n>> renamed to relative_path to maintain the name convention.\n> \n> The approach taken by this patch is a sound one, and I like it.  The\n> change does not reuse any existing name for different purpose, which means\n> there is little chance of this change interacting other topics that may be\n> in flight that introduce new call sites to these renamed functions in a\n> funny way.  A (semantic) mismerge or misapplication of the patch will be\n> found by the compiler.\n> \n> For example, the version of setup.c this patch is based on the version\n> before 05f08e4 (Merge branch 'cb/setup', 2011-02-09) was merged, and the\n> merge introduced a new call site to make_absolute_path().  A few callsites\n> to make_nonrelative_path() in wrapper.c were introduced at 70ec868 (Merge\n> branch 'ae/better-template-failure-report', 2011-02-09), and this patch\n> does not touch them.\n> \n> As the result, the patch cleanly applies textually, but the resulting code\n> does not compile, and it is a good thing ;-).\n\n...because the \"diff --color-words\" for this version of the patch makes\nit clear you only have to rerun\n\nsed -e 's/make_absolute_path/real_path/g'\n\netc. on the affected files to resolve this \"merge-compile conflict\".\n\nJust pointing it out for those who wonder why I pestered Carlos to bring\nthe patch into this form, and why Junio started to like failed builds.\nAlso, I need to make-up for making Junio explain my recent RFD :)\n\nCheers,\nMichael\n"}]}