Name make_*_path functions more accurately
Rename the make_*_path functions so it's clearer what they do, in particlar make clear what the differnce between make_absolute_path and make_nonrelative_path is by renaming them real_path and absolute_path respectively. make_relative_path has an understandable name and is renamed to relative_path to maintain the name convention. The function calls have been replaced 1-to-1 in their usage. Signed-off-by: Carlos Martín Nieto <cmn@elego.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Carlos Martín Nieto committed
Mar 17, 2011 at 12:26 UTC
e2a57aac8a8a2b786739a5a93ea9dcfd2f0fd0e2
14 files changed
+52
-38
abspath.c
+16
-2
@@ -14,7 +14,14 @@ int is_directory(const char *path)
14
/* We allow "recursive" symbolic links. Only within reason, though. */
15
#define MAXDEPTH 5
16
17
-const char *make_absolute_path(const char *path)
17
+/*
18
+ * Use this to get the real path, i.e. resolve links. If you want an
19
+ * absolute path but don't mind links, use absolute_path.
20
+ *
21
+ * If path is our buffer, then return path, as it's already what the
22
+ * user wants.
23
+ */
24
+const char *real_path(const char *path)
25
{
26
static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
27
char cwd[1024] = "";
@@ -104,7 +111,14 @@ static const char *get_pwd_cwd(void)
111
return cwd;
112
}
113
107
-const char *make_nonrelative_path(const char *path)
114
+/*
115
+ * Use this to get an absolute path from a relative one. If you want
116
+ * to resolve links, you should use real_path.
117
+ *
118
+ * If the path is already absolute, then return path. As the user is
119
+ * never meant to free the return value, we're safe.
120
+ */
121
+const char *absolute_path(const char *path)
122
{
123
static char buf[PATH_MAX + 1];
124
builtin/clone.c
+5
-5
@@ -100,7 +100,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
100
path = mkpath("%s%s", repo, suffix[i]);
101
if (is_directory(path)) {
102
*is_bundle = 0;
103
- return xstrdup(make_nonrelative_path(path));
103
+ return xstrdup(absolute_path(path));
104
}
105
}
106
@@ -109,7 +109,7 @@ static char *get_repo_path(const char *repo, int *is_bundle)
109
path = mkpath("%s%s", repo, bundle_suffix[i]);
110
if (!stat(path, &st) && S_ISREG(st.st_mode)) {
111
*is_bundle = 1;
112
- return xstrdup(make_nonrelative_path(path));
112
+ return xstrdup(absolute_path(path));
113
}
114
}
115
@@ -203,7 +203,7 @@ static void setup_reference(const char *repo)
203
struct transport *transport;
204
const struct ref *extra;
205
206
- ref_git = make_absolute_path(option_reference);
206
+ ref_git = real_path(option_reference);
207
208
if (is_directory(mkpath("%s/.git/objects", ref_git)))
209
ref_git = mkpath("%s/.git", ref_git);
@@ -411,7 +411,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
411
412
path = get_repo_path(repo_name, &is_bundle);
413
if (path)
414
- repo = xstrdup(make_nonrelative_path(repo_name));
414
+ repo = xstrdup(absolute_path(repo_name));
415
else if (!strchr(repo_name, ':'))
416
die("repository '%s' does not exist", repo_name);
417
else
@@ -466,7 +466,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
466
467
if (safe_create_leading_directories_const(git_dir) < 0)
468
die("could not create leading directories of '%s'", git_dir);
469
- set_git_dir(make_absolute_path(git_dir));
469
+ set_git_dir(real_path(git_dir));
470
471
if (0 <= option_verbosity)
472
printf("Cloning into %s%s...\n",
builtin/init-db.c
+4
-4
@@ -501,7 +501,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
501
const char *git_dir_parent = strrchr(git_dir, '/');
502
if (git_dir_parent) {
503
char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
504
- git_work_tree_cfg = xstrdup(make_absolute_path(rel));
504
+ git_work_tree_cfg = xstrdup(real_path(rel));
505
free(rel);
506
}
507
if (!git_work_tree_cfg) {
@@ -510,7 +510,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
510
die_errno ("Cannot access current working directory");
511
}
512
if (work_tree)
513
- set_git_work_tree(make_absolute_path(work_tree));
513
+ set_git_work_tree(real_path(work_tree));
514
else
515
set_git_work_tree(git_work_tree_cfg);
516
if (access(get_git_work_tree(), X_OK))
@@ -519,10 +519,10 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
519
}
520
else {
521
if (work_tree)
522
- set_git_work_tree(make_absolute_path(work_tree));
522
+ set_git_work_tree(real_path(work_tree));
523
}
524
525
- set_git_dir(make_absolute_path(git_dir));
525
+ set_git_dir(real_path(git_dir));
526
527
return init_db(template_dir, flags);
528
}
builtin/receive-pack.c
+1
-1
@@ -740,7 +740,7 @@ static int add_refs_from_alternate(struct alternate_object_database *e, void *un
740
const struct ref *extra;
741
742
e->name[-1] = '\0';
743
- other = xstrdup(make_absolute_path(e->base));
743
+ other = xstrdup(real_path(e->base));
744
e->name[-1] = '/';
745
len = strlen(other);
746
cache.h
+3
-3
@@ -731,9 +731,9 @@ static inline int is_absolute_path(const char *path)
731
return path[0] == '/' || has_dos_drive_prefix(path);
732
}
733
int is_directory(const char *);
734
-const char *make_absolute_path(const char *path);
735
-const char *make_nonrelative_path(const char *path);
736
-const char *make_relative_path(const char *abs, const char *base);
734
+const char *real_path(const char *path);
735
+const char *absolute_path(const char *path);
736
+const char *relative_path(const char *abs, const char *base);
737
int normalize_path_copy(char *dst, const char *src);
738
int longest_ancestor_length(const char *path, const char *prefix_list);
739
char *strip_path_suffix(const char *path, const char *suffix);
dir.c
+1
-1
@@ -1128,7 +1128,7 @@ char *get_relative_cwd(char *buffer, int size, const char *dir)
1128
die_errno("can't find the current directory");
1129
1130
if (!is_absolute_path(dir))
1131
- dir = make_absolute_path(dir);
1131
+ dir = real_path(dir);
1132
1133
while (*dir && *dir == *cwd) {
1134
dir++;
environment.c
+2
-2
@@ -139,7 +139,7 @@ static int git_work_tree_initialized;
139
void set_git_work_tree(const char *new_work_tree)
140
{
141
if (git_work_tree_initialized) {
142
- new_work_tree = make_absolute_path(new_work_tree);
142
+ new_work_tree = real_path(new_work_tree);
143
if (strcmp(new_work_tree, work_tree))
144
die("internal error: work tree has already been set\n"
145
"Current worktree: %s\nNew worktree: %s",
@@ -147,7 +147,7 @@ void set_git_work_tree(const char *new_work_tree)
147
return;
148
}
149
git_work_tree_initialized = 1;
150
- work_tree = xstrdup(make_absolute_path(new_work_tree));
150
+ work_tree = xstrdup(real_path(new_work_tree));
151
}
152
153
const char *get_git_work_tree(void)
exec_cmd.c
+1
-1
@@ -89,7 +89,7 @@ static void add_path(struct strbuf *out, const char *path)
89
if (is_absolute_path(path))
90
strbuf_addstr(out, path);
91
else
92
- strbuf_addstr(out, make_nonrelative_path(path));
92
+ strbuf_addstr(out, absolute_path(path));
93
94
strbuf_addch(out, PATH_SEP);
95
}
lockfile.c
+2
-2
@@ -164,10 +164,10 @@ static char *unable_to_lock_message(const char *path, int err)
164
"If no other git process is currently running, this probably means a\n"
165
"git process crashed in this repository earlier. Make sure no other git\n"
166
"process is running and remove the file manually to continue.",
167
- make_nonrelative_path(path), strerror(err));
167
+ absolute_path(path), strerror(err));
168
} else
169
strbuf_addf(&buf, "Unable to create '%s.lock': %s",
170
- make_nonrelative_path(path), strerror(err));
170
+ absolute_path(path), strerror(err));
171
return strbuf_detach(&buf, NULL);
172
}
173
path.c
+1
-1
@@ -397,7 +397,7 @@ int set_shared_perm(const char *path, int mode)
397
return 0;
398
}
399
400
-const char *make_relative_path(const char *abs, const char *base)
400
+const char *relative_path(const char *abs, const char *base)
401
{
402
static char buf[PATH_MAX + 1];
403
int i = 0, j = 0;
setup.c
+7
-7
@@ -9,7 +9,7 @@ char *prefix_path(const char *prefix, int len, const char *path)
9
const char *orig = path;
10
char *sanitized;
11
if (is_absolute_path(orig)) {
12
- const char *temp = make_absolute_path(path);
12
+ const char *temp = real_path(path);
13
sanitized = xmalloc(len + strlen(temp) + 1);
14
strcpy(sanitized, temp);
15
} else {
@@ -221,7 +221,7 @@ void setup_work_tree(void)
221
work_tree = get_git_work_tree();
222
git_dir = get_git_dir();
223
if (!is_absolute_path(git_dir))
224
- git_dir = make_absolute_path(git_dir);
224
+ git_dir = real_path(get_git_dir());
225
if (!work_tree || chdir(work_tree))
226
die("This operation must be run in a work tree");
227
@@ -232,7 +232,7 @@ void setup_work_tree(void)
232
if (getenv(GIT_WORK_TREE_ENVIRONMENT))
233
setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
234
235
- set_git_dir(make_relative_path(git_dir, work_tree));
235
+ set_git_dir(relative_path(git_dir, work_tree));
236
initialized = 1;
237
}
238
@@ -312,7 +312,7 @@ const char *read_gitfile_gently(const char *path)
312
313
if (!is_git_directory(dir))
314
die("Not a git repository: %s", dir);
315
- path = make_absolute_path(dir);
315
+ path = real_path(dir);
316
317
free(buf);
318
return path;
@@ -392,7 +392,7 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
392
393
if (!prefixcmp(cwd, worktree) &&
394
cwd[strlen(worktree)] == '/') { /* cwd inside worktree */
395
- set_git_dir(make_absolute_path(gitdirenv));
395
+ set_git_dir(real_path(gitdirenv));
396
if (chdir(worktree))
397
die_errno("Could not chdir to '%s'", worktree);
398
cwd[len++] = '/';
@@ -417,7 +417,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,
417
/* --work-tree is set without --git-dir; use discovered one */
418
if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
419
if (offset != len && !is_absolute_path(gitdir))
420
- gitdir = xstrdup(make_absolute_path(gitdir));
420
+ gitdir = xstrdup(real_path(gitdir));
421
if (chdir(cwd))
422
die_errno("Could not come back to cwd");
423
return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
@@ -425,7 +425,7 @@ static const char *setup_discovered_git_dir(const char *gitdir,
425
426
/* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
427
if (is_bare_repository_cfg > 0) {
428
- set_git_dir(offset == len ? gitdir : make_absolute_path(gitdir));
428
+ set_git_dir(offset == len ? gitdir : real_path(gitdir));
429
if (chdir(cwd))
430
die_errno("Could not come back to cwd");
431
return NULL;
t/t0000-basic.sh
+5
-5
@@ -435,7 +435,7 @@ test_expect_success 'update-index D/F conflict' '
435
test $numpath0 = 1
436
'
437
438
-test_expect_success SYMLINKS 'absolute path works as expected' '
438
+test_expect_success SYMLINKS 'real path works as expected' '
439
mkdir first &&
440
ln -s ../.git first/.git &&
441
mkdir second &&
@@ -443,14 +443,14 @@ test_expect_success SYMLINKS 'absolute path works as expected' '
443
mkdir third &&
444
dir="$(cd .git; pwd -P)" &&
445
dir2=third/../second/other/.git &&
446
- test "$dir" = "$(test-path-utils make_absolute_path $dir2)" &&
446
+ test "$dir" = "$(test-path-utils real_path $dir2)" &&
447
file="$dir"/index &&
448
- test "$file" = "$(test-path-utils make_absolute_path $dir2/index)" &&
448
+ test "$file" = "$(test-path-utils real_path $dir2/index)" &&
449
basename=blub &&
450
- test "$dir/$basename" = "$(cd .git && test-path-utils make_absolute_path "$basename")" &&
450
+ test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
451
ln -s ../first/file .git/syml &&
452
sym="$(cd first; pwd -P)"/file &&
453
- test "$sym" = "$(test-path-utils make_absolute_path "$dir2/syml")"
453
+ test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
454
'
455
456
test_expect_success 'very long name in the index handled sanely' '
test-path-utils.c
+2
-2
@@ -11,9 +11,9 @@ int main(int argc, char **argv)
11
return 0;
12
}
13
14
- if (argc >= 2 && !strcmp(argv[1], "make_absolute_path")) {
14
+ if (argc >= 2 && !strcmp(argv[1], "real_path")) {
15
while (argc > 2) {
16
- puts(make_absolute_path(argv[2]));
16
+ puts(real_path(argv[2]));
17
argc--;
18
argv++;
19
}
wrapper.c
+2
-2
@@ -209,7 +209,7 @@ int xmkstemp(char *template)
209
if (!template[0])
210
template = origtemplate;
211
212
- nonrelative_template = make_nonrelative_path(template);
212
+ nonrelative_template = absolute_path(template);
213
errno = saved_errno;
214
die_errno("Unable to create temporary file '%s'",
215
nonrelative_template);
@@ -344,7 +344,7 @@ int xmkstemp_mode(char *template, int mode)
344
if (!template[0])
345
template = origtemplate;
346
347
- nonrelative_template = make_nonrelative_path(template);
347
+ nonrelative_template = absolute_path(template);
348
errno = saved_errno;
349
die_errno("Unable to create temporary file '%s'",
350
nonrelative_template);